authentik.stages.authenticator_webauthn.algorithms

Signature algorithms offered to WebAuthn authenticators

 1"""Signature algorithms offered to WebAuthn authenticators"""
 2
 3from cryptography.hazmat.backends.openssl.backend import backend
 4from webauthn.helpers.cose import COSEAlgorithmIdentifier
 5from webauthn.helpers.decode_credential_public_key import decode_credential_public_key
 6
 7# TODO: Remove the ML-DSA entries once py_webauthn adds ML-DSA to its default supported algorithms.
 8# Offered in order of preference, so the post-quantum ML-DSA sets (FIPS 204) are only
 9# chosen by an authenticator that supports none of the classical algorithms.
10PREFERRED_PUB_KEY_ALGS = [
11    COSEAlgorithmIdentifier.EDDSA,
12    COSEAlgorithmIdentifier.ECDSA_SHA_256,
13    COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256,
14    COSEAlgorithmIdentifier.ML_DSA_44,
15    COSEAlgorithmIdentifier.ML_DSA_65,
16    COSEAlgorithmIdentifier.ML_DSA_87,
17]
18
19# TODO: Remove when OpenSSL/FIPS is updated
20# The validated OpenSSL FIPS provider implements neither Ed25519 nor ML-DSA, so only these can
21# be verified while FIPS mode is enabled.
22FIPS_PUB_KEY_ALGS = [
23    COSEAlgorithmIdentifier.ECDSA_SHA_256,
24    COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256,
25]
26
27KEY_TYPE_NAMES = {
28    COSEAlgorithmIdentifier.EDDSA: "EdDSA (Ed25519)",
29    COSEAlgorithmIdentifier.ECDSA_SHA_256: "ES256",
30    COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256: "RS256",
31    COSEAlgorithmIdentifier.ML_DSA_44: "ML-DSA-44",
32    COSEAlgorithmIdentifier.ML_DSA_65: "ML-DSA-65",
33    COSEAlgorithmIdentifier.ML_DSA_87: "ML-DSA-87",
34}
35
36
37def supported_pub_key_algs() -> list[COSEAlgorithmIdentifier]:
38    """Algorithms offered to authenticators, limited to those the current OpenSSL configuration
39    can verify. A credential is created on the authenticator with the first algorithm it supports,
40    so offering one that cannot be verified later locks the user out at their first login."""
41    if backend._fips_enabled:
42        return FIPS_PUB_KEY_ALGS
43    return PREFERRED_PUB_KEY_ALGS
44
45
46def credential_key_type(cose_public_key: bytes) -> str:
47    """Human-readable key type of a stored COSE credential public key, for logs and errors"""
48    try:
49        alg = int(decode_credential_public_key(cose_public_key).alg)
50    except Exception:  # noqa: BLE001
51        return "unknown"
52    try:
53        identifier = COSEAlgorithmIdentifier(alg)
54    except ValueError:
55        return f"COSE algorithm {alg}"
56    return KEY_TYPE_NAMES.get(identifier, identifier.name)
PREFERRED_PUB_KEY_ALGS = [<COSEAlgorithmIdentifier.EDDSA: -8>, <COSEAlgorithmIdentifier.ECDSA_SHA_256: -7>, <COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256: -257>, <COSEAlgorithmIdentifier.ML_DSA_44: -48>, <COSEAlgorithmIdentifier.ML_DSA_65: -49>, <COSEAlgorithmIdentifier.ML_DSA_87: -50>]
FIPS_PUB_KEY_ALGS = [<COSEAlgorithmIdentifier.ECDSA_SHA_256: -7>, <COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256: -257>]
KEY_TYPE_NAMES = {<COSEAlgorithmIdentifier.EDDSA: -8>: 'EdDSA (Ed25519)', <COSEAlgorithmIdentifier.ECDSA_SHA_256: -7>: 'ES256', <COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_256: -257>: 'RS256', <COSEAlgorithmIdentifier.ML_DSA_44: -48>: 'ML-DSA-44', <COSEAlgorithmIdentifier.ML_DSA_65: -49>: 'ML-DSA-65', <COSEAlgorithmIdentifier.ML_DSA_87: -50>: 'ML-DSA-87'}
def supported_pub_key_algs() -> list[webauthn.helpers.cose.COSEAlgorithmIdentifier]:
38def supported_pub_key_algs() -> list[COSEAlgorithmIdentifier]:
39    """Algorithms offered to authenticators, limited to those the current OpenSSL configuration
40    can verify. A credential is created on the authenticator with the first algorithm it supports,
41    so offering one that cannot be verified later locks the user out at their first login."""
42    if backend._fips_enabled:
43        return FIPS_PUB_KEY_ALGS
44    return PREFERRED_PUB_KEY_ALGS

Algorithms offered to authenticators, limited to those the current OpenSSL configuration can verify. A credential is created on the authenticator with the first algorithm it supports, so offering one that cannot be verified later locks the user out at their first login.

def credential_key_type(cose_public_key: bytes) -> str:
47def credential_key_type(cose_public_key: bytes) -> str:
48    """Human-readable key type of a stored COSE credential public key, for logs and errors"""
49    try:
50        alg = int(decode_credential_public_key(cose_public_key).alg)
51    except Exception:  # noqa: BLE001
52        return "unknown"
53    try:
54        identifier = COSEAlgorithmIdentifier(alg)
55    except ValueError:
56        return f"COSE algorithm {alg}"
57    return KEY_TYPE_NAMES.get(identifier, identifier.name)

Human-readable key type of a stored COSE credential public key, for logs and errors