| 1 |
from typing import List, Optional |
| 2 |
|
| 3 |
from cryptography.hazmat.backends import default_backend |
| 4 |
from cryptography.x509 import load_der_x509_certificate |
| 5 |
from OpenSSL.crypto import X509, X509Store, X509StoreContext, X509StoreContextError |
| 6 |
|
| 7 |
from .exceptions import InvalidCertificateChain |
| 8 |
from .pem_cert_bytes_to_open_ssl_x509 import pem_cert_bytes_to_open_ssl_x509 |
| 9 |
|
| 10 |
|
| 11 |
def validate_certificate_chain( |
| 12 |
*, |
| 13 |
x5c: List[bytes], |
| 14 |
pem_root_certs_bytes: Optional[List[bytes]] = None, |
| 15 |
) -> bool: |
| 16 |
"""Validate that the certificates in x5c chain back to a known root certificate |
| 17 |
|
| 18 |
Args: |
| 19 |
`x5c`: X5C certificates from a registration response's attestation statement |
| 20 |
(optional) `pem_root_certs_bytes`: Any additional (PEM-formatted) |
| 21 |
root certificates that may complete the certificate chain |
| 22 |
|
| 23 |
Raises: |
| 24 |
`helpers.exceptions.InvalidCertificateChain` if chain cannot be validated |
| 25 |
""" |
| 26 |
if pem_root_certs_bytes is None or len(pem_root_certs_bytes) < 1: |
| 27 |
# We have no root certs to chain back to, so just pass on validation |
| 28 |
return True |
| 29 |
|
| 30 |
# Make sure we have at least one certificate to try and link back to a root cert |
| 31 |
if len(x5c) < 1: |
| 32 |
raise InvalidCertificateChain("x5c was empty") |
| 33 |
|
| 34 |
# Prepare leaf cert |
| 35 |
try: |
| 36 |
leaf_cert_bytes = x5c[0] |
| 37 |
leaf_cert_crypto = load_der_x509_certificate(leaf_cert_bytes, default_backend()) |
| 38 |
leaf_cert = X509().from_cryptography(leaf_cert_crypto) |
| 39 |
except Exception as err: |
| 40 |
raise InvalidCertificateChain(f"Could not prepare leaf cert: {err}") |
| 41 |
|
| 42 |
# Prepare any intermediate certs |
| 43 |
try: |
| 44 |
# May be an empty array, that's fine |
| 45 |
intermediate_certs_bytes = x5c[1:] |
| 46 |
intermediate_certs_crypto = [ |
| 47 |
load_der_x509_certificate(cert, default_backend()) for cert in intermediate_certs_bytes |
| 48 |
] |
| 49 |
intermediate_certs = [X509().from_cryptography(cert) for cert in intermediate_certs_crypto] |
| 50 |
except Exception as err: |
| 51 |
raise InvalidCertificateChain(f"Could not prepare intermediate certs: {err}") |
| 52 |
|
| 53 |
# Prepare a collection of possible root certificates |
| 54 |
root_certs_store = X509Store() |
| 55 |
try: |
| 56 |
for cert in pem_root_certs_bytes: |
| 57 |
root_certs_store.add_cert(pem_cert_bytes_to_open_ssl_x509(cert)) |
| 58 |
except Exception as err: |
| 59 |
raise InvalidCertificateChain(f"Could not prepare root certs: {err}") |
| 60 |
|
| 61 |
# Load certs into a "context" for validation |
| 62 |
context = X509StoreContext( |
| 63 |
store=root_certs_store, |
| 64 |
certificate=leaf_cert, |
| 65 |
chain=intermediate_certs, |
| 66 |
) |
| 67 |
|
| 68 |
# Validate the chain (will raise if it can't) |
| 69 |
try: |
| 70 |
context.verify_certificate() |
| 71 |
except X509StoreContextError: |
| 72 |
raise InvalidCertificateChain("Certificate chain could not be validated") |
| 73 |
|
| 74 |
return True |