| 1 |
from typing import Union |
| 2 |
|
| 3 |
from cryptography.hazmat.primitives.asymmetric.dsa import DSAPublicKey |
| 4 |
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey |
| 5 |
from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PublicKey |
| 6 |
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey |
| 7 |
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PublicKey |
| 8 |
from cryptography.hazmat.primitives.asymmetric.x448 import X448PublicKey |
| 9 |
from cryptography.hazmat.primitives.asymmetric.padding import MGF1, PSS, PKCS1v15 |
| 10 |
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey |
| 11 |
|
| 12 |
from .algorithms import ( |
| 13 |
get_ec2_sig_alg, |
| 14 |
get_rsa_pkcs1_sig_alg, |
| 15 |
get_rsa_pss_sig_alg, |
| 16 |
is_rsa_pkcs, |
| 17 |
is_rsa_pss, |
| 18 |
) |
| 19 |
from .cose import COSEAlgorithmIdentifier |
| 20 |
from .exceptions import UnsupportedAlgorithm, UnsupportedPublicKey |
| 21 |
|
| 22 |
|
| 23 |
def verify_signature( |
| 24 |
*, |
| 25 |
public_key: Union[ |
| 26 |
EllipticCurvePublicKey, |
| 27 |
RSAPublicKey, |
| 28 |
Ed25519PublicKey, |
| 29 |
DSAPublicKey, |
| 30 |
Ed448PublicKey, |
| 31 |
X25519PublicKey, |
| 32 |
X448PublicKey, |
| 33 |
], |
| 34 |
signature_alg: COSEAlgorithmIdentifier, |
| 35 |
signature: bytes, |
| 36 |
data: bytes, |
| 37 |
) -> None: |
| 38 |
"""Verify a signature was signed with the private key corresponding to the provided |
| 39 |
public key. |
| 40 |
|
| 41 |
Args: |
| 42 |
`public_key`: A public key loaded via cryptography's `load_der_public_key`, `load_der_x509_certificate`, etc... |
| 43 |
`signature_alg`: Algorithm ID used to sign the signature |
| 44 |
`signature`: Signature to verify |
| 45 |
`data`: Data signed by private key |
| 46 |
|
| 47 |
Raises: |
| 48 |
`webauth.helpers.exceptions.UnsupportedAlgorithm` when the algorithm is not a recognized COSE algorithm ID |
| 49 |
`webauth.helpers.exceptions.UnsupportedPublicKey` when the public key is not a valid EC2, RSA, or OKP certificate |
| 50 |
`cryptography.exceptions.InvalidSignature` when the signature cannot be verified |
| 51 |
""" |
| 52 |
if isinstance(public_key, EllipticCurvePublicKey): |
| 53 |
public_key.verify(signature, data, get_ec2_sig_alg(signature_alg)) |
| 54 |
elif isinstance(public_key, RSAPublicKey): |
| 55 |
if is_rsa_pkcs(signature_alg): |
| 56 |
public_key.verify(signature, data, PKCS1v15(), get_rsa_pkcs1_sig_alg(signature_alg)) |
| 57 |
elif is_rsa_pss(signature_alg): |
| 58 |
rsa_alg = get_rsa_pss_sig_alg(signature_alg) |
| 59 |
public_key.verify( |
| 60 |
signature, |
| 61 |
data, |
| 62 |
PSS(mgf=MGF1(rsa_alg), salt_length=PSS.MAX_LENGTH), |
| 63 |
rsa_alg, |
| 64 |
) |
| 65 |
else: |
| 66 |
raise UnsupportedAlgorithm(f"Unrecognized RSA signature alg {signature_alg}") |
| 67 |
elif isinstance(public_key, Ed25519PublicKey): |
| 68 |
public_key.verify(signature, data) |
| 69 |
else: |
| 70 |
raise UnsupportedPublicKey( |
| 71 |
f"Unsupported public key for signature verification: {public_key}" |
| 72 |
) |