Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
* Version 2.2.2 (unreleased)
** Attestation: Use cryptography's certificate issuer verification when available,
supporting additional signature algorithms while retaining the legacy fallback.
** MDS: Allow omitted userVerificationDetails and attachmentHint fields, as
specified in Metadata Statement 3.1.1.

Expand Down
10 changes: 8 additions & 2 deletions fido2/attestation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,22 @@ def inner(*args, **kwargs):
def verify_x509_chain(chain: list[bytes]) -> None:
"""Verifies a chain of certificates.

Checks that the first item in the chain is signed by the next, and so on.
Checks that the first item in the chain is issued by the next, and so on.
The first item is the leaf, the last is the root.
On cryptography versions older than 40, only RSA and ECDSA signatures are
supported, and issuer names are not checked.
"""
certs = [x509.load_der_x509_certificate(der, default_backend()) for der in chain]
cert = certs.pop(0)
while certs:
child = cert
cert = certs.pop(0)
pub = cert.public_key()
try:
if hasattr(child, "verify_directly_issued_by"):
child.verify_directly_issued_by(cert)
continue

pub = cert.public_key()
if isinstance(pub, rsa.RSAPublicKey):
assert child.signature_hash_algorithm is not None # noqa: S101
pub.verify(
Expand Down
108 changes: 108 additions & 0 deletions tests/test_attestation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@
# POSSIBILITY OF SUCH DAMAGE.

import unittest
from datetime import datetime, timedelta
from importlib.metadata import version
from unittest.mock import patch

import pytest
from cryptography import x509
from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec, ed25519, ed448, rsa
from fido2 import cbor
from fido2.attestation import (
AndroidSafetynetAttestation,
Expand All @@ -47,6 +54,107 @@
from fido2.ctap2.base import AttestationResponse
from fido2.webauthn import AuthenticatorData


class _LegacyCertificate:
def __init__(self, certificate):
self._certificate = certificate

def __getattr__(self, name):
if name == "verify_directly_issued_by":
raise AttributeError(name)
return getattr(self._certificate, name)


def _certificate_chain(key, algorithm, issuer_name=None):
name = x509.Name([x509.NameAttribute(x509.NameOID.COMMON_NAME, "Test CA")])
now = datetime(2026, 1, 1)
builder = (
x509.CertificateBuilder()
.public_key(key.public_key())
.not_valid_before(now)
.not_valid_after(now + timedelta(days=365))
)
root = (
builder.subject_name(name)
.issuer_name(name)
.serial_number(1)
.sign(key, algorithm)
)
leaf = (
builder.subject_name(
x509.Name([x509.NameAttribute(x509.NameOID.COMMON_NAME, "Test leaf")])
)
.issuer_name(issuer_name or name)
.serial_number(2)
.sign(key, algorithm)
)
return [c.public_bytes(serialization.Encoding.DER) for c in (leaf, root)]


@pytest.mark.parametrize("legacy", [False, True])
@pytest.mark.parametrize("key_type", ["rsa", "ec"])
def test_certificate_chain_verification(key_type, legacy):
key = (
rsa.generate_private_key(public_exponent=65537, key_size=2048)
if key_type == "rsa"
else ec.generate_private_key(ec.SECP256R1())
)
chain = _certificate_chain(key, hashes.SHA256())
certificates = [x509.load_der_x509_certificate(der) for der in chain]
if legacy:
certificates = [_LegacyCertificate(c) for c in certificates]
with patch(
"fido2.attestation.base.x509.load_der_x509_certificate",
side_effect=certificates,
):
verify_x509_chain(chain)

tampered = chain[0][:-1] + bytes([chain[0][-1] ^ 1])
certificates = [x509.load_der_x509_certificate(der) for der in (tampered, chain[1])]
if legacy:
certificates = [_LegacyCertificate(c) for c in certificates]
with (
patch(
"fido2.attestation.base.x509.load_der_x509_certificate",
side_effect=certificates,
),
pytest.raises(InvalidSignature),
):
verify_x509_chain([tampered, chain[1]])


def test_certificate_chain_issuer_mismatch():
if not hasattr(x509.Certificate, "verify_directly_issued_by"):
pytest.skip("Requires cryptography 40 or newer")
key = ec.generate_private_key(ec.SECP256R1())
issuer = x509.Name([x509.NameAttribute(x509.NameOID.COMMON_NAME, "Wrong CA")])
with pytest.raises(InvalidData):
verify_x509_chain(_certificate_chain(key, hashes.SHA256(), issuer))


@pytest.mark.parametrize("key_type", [ed25519.Ed25519PrivateKey, ed448.Ed448PrivateKey])
def test_eddsa_certificate_chain(key_type):
key = key_type.generate()
chain = _certificate_chain(key, None)
verify_x509_chain(chain)
tampered = chain[0][:-1] + bytes([chain[0][-1] ^ 1])
with pytest.raises(InvalidSignature):
verify_x509_chain([tampered, chain[1]])


def test_mldsa_certificate_chain():
if int(version("cryptography").split(".")[0]) < 49:
pytest.skip("ML-DSA certificates require cryptography 49 or newer")
from cryptography.hazmat.primitives.asymmetric import mldsa

key = mldsa.MLDSA87PrivateKey.generate()
chain = _certificate_chain(key, None)
verify_x509_chain(chain)
tampered = chain[0][:-1] + bytes([chain[0][-1] ^ 1])
with pytest.raises(InvalidSignature):
verify_x509_chain([tampered, chain[1]])


# GS Root R2 (https://pki.goog/)
_GSR2_DER = bytes.fromhex(
"308203ba308202a2a003020102020b0400000000010f8626e60d300d06092a864886f70d0101050500304c3120301e060355040b1317476c6f62616c5369676e20526f6f74204341202d20523231133011060355040a130a476c6f62616c5369676e311330110603550403130a476c6f62616c5369676e301e170d3036313231353038303030305a170d3231313231353038303030305a304c3120301e060355040b1317476c6f62616c5369676e20526f6f74204341202d20523231133011060355040a130a476c6f62616c5369676e311330110603550403130a476c6f62616c5369676e30820122300d06092a864886f70d01010105000382010f003082010a0282010100a6cf240ebe2e6f28994542c4ab3e21549b0bd37f8470fa12b3cbbf875fc67f86d3b2305cd6fdadf17bdce5f86096099210f5d053defb7b7e7388ac52887b4aa6ca49a65ea8a78c5a11bc7a82ebbe8ce9b3ac962507974a992a072fb41e77bf8a0fb5027c1b96b8c5b93a2cbcd612b9eb597de2d006865f5e496ab5395e8834ecbc780c0898846ca8cd4bb4a07d0c794df0b82dcb21cad56c5b7de1a02984a1f9d39449cb24629120bcdd0bd5d9ccf9ea270a2b7391c69d1bacc8cbe8e0a0f42f908b4dfbb0361bf6197a85e06df26113885c9fe0930a51978a5aceafabd5f7aa09aa60bddcd95fdf72a960135e0001c94afa3fa4ea070321028e82ca03c29b8f0203010001a3819c308199300e0603551d0f0101ff040403020106300f0603551d130101ff040530030101ff301d0603551d0e041604149be20757671c1ec06a06de59b49a2ddfdc19862e30360603551d1f042f302d302ba029a0278625687474703a2f2f63726c2e676c6f62616c7369676e2e6e65742f726f6f742d72322e63726c301f0603551d230418301680149be20757671c1ec06a06de59b49a2ddfdc19862e300d06092a864886f70d01010505000382010100998153871c68978691ece04ab8440bab81ac274fd6c1b81c4378b30c9afcea2c3c6e611b4d4b29f59f051d26c1b8e983006245b6a90893b9a9334b189ac2f887884edbdd71341ac154da463fe0d32aab6d5422f53a62cd206fba2989d7dd91eed35ca23ea15b41f5dfe564432de9d539abd2a2dfb78bd0c080191c45c02d8ce8f82da4745649c505b54f15de6e44783987a87ebbf3791891bbf46f9dc1f08c358c5d01fbc36db9ef446d7946317e0afea982c1ffefab6e20c450c95f9d4d9b178c0ce501c9a0416a7353faa550b46e250ffb4c18f4fd52d98e69b1e8110fde88d8fb1d49f7aade95cf2078c26012db25408c6afc7e4238406412f79e81e1932e" # noqa E501
Expand Down
Loading