authentik.crypto.validators

Validate that a Certificate-Key Pair's algorithm is usable for a given purpose

 1"""Validate that a Certificate-Key Pair's algorithm is usable for a given purpose"""
 2
 3from django.utils.translation import gettext_lazy as _
 4from rest_framework.exceptions import ValidationError
 5from rest_framework.utils.representation import smart_repr
 6
 7from authentik.crypto.models import CertificateKeyPair, KeyType
 8
 9# Key types that can be signed with via JOSE/JWT. Bounded by JWTAlgorithms.from_public_key, which
10# maps RSA to RS256, EC to ES256/384/512, both Edwards curves to EdDSA (RFC 8037) and the ML-DSA
11# parameter sets to ML-DSA-44/65/87 (RFC 9964). DSA has no JWA signature algorithm at all.
12JWT_SIGNING_KEY_TYPES = [
13    KeyType.RSA,
14    KeyType.EC,
15    KeyType.ED25519,
16    KeyType.ED448,
17    KeyType.MLDSA44,
18    KeyType.MLDSA65,
19    KeyType.MLDSA87,
20]
21
22# Key types that can be encrypted to via JOSE/JWE. OAuth2Provider.encrypt hardcodes the
23# RSA-OAEP-256 key management algorithm, which has no non-RSA counterpart.
24JWE_ENCRYPTION_KEY_TYPES = [KeyType.RSA]
25
26# Key types that have an XML-DSIG signature transform in libxmlsec1.
27XML_SIGNING_KEY_TYPES = [KeyType.RSA, KeyType.EC, KeyType.DSA]
28
29# Key types the Rust server and the Go outposts can serve TLS with. Ed448 will never be supported
30# (see https://github.com/golang/go/issues/29390#issuecomment-614175576). ML-DSA (FIPS 204)
31# certificates are served by both rustls (AWS-LC) and Go 1.27's crypto/tls in TLS 1.3.
32TLS_KEY_TYPES = [
33    KeyType.RSA,
34    KeyType.EC,
35    KeyType.ED25519,
36    KeyType.MLDSA44,
37    KeyType.MLDSA65,
38    KeyType.MLDSA87,
39]
40
41
42class KeyTypeValidator:
43    """Field-level validator that ensures a Certificate-Key Pair uses a key algorithm the
44    consuming protocol can actually handle.
45
46    Accepts either a single keypair or a list of them, so it can be attached to both a
47    ForeignKey and a ManyToMany field.
48
49    A keypair whose `key_type` is null is rejected rather than allowed through: null means
50    `detect_key_type` did not recognize the certificate's public key, so we cannot vouch for it
51    being usable, and letting it through is what causes downstream signing to raise instead of
52    returning a validation error."""
53
54    allowed: list[KeyType]
55    message = _("Key type {key_type} is not supported. Supported key types are: {allowed}.")
56    unknown_message = _(
57        "Unable to determine the key type of this certificate. "
58        "Supported key types are: {allowed}."
59    )
60
61    def __init__(self, *allowed: KeyType, message: str | None = None) -> None:
62        self.allowed = list(allowed)
63        self.message = message or self.message
64
65    def __call__(self, value: CertificateKeyPair | list[CertificateKeyPair] | None):
66        keypairs = value if isinstance(value, list) else [value]
67        allowed = ", ".join(str(KeyType(key_type).label) for key_type in self.allowed)
68        for keypair in keypairs:
69            if not keypair:
70                continue
71            if not keypair.key_type:
72                raise ValidationError(self.unknown_message.format(allowed=allowed), code="invalid")
73            if keypair.key_type not in self.allowed:
74                raise ValidationError(
75                    self.message.format(key_type=KeyType(keypair.key_type).label, allowed=allowed),
76                    code="invalid",
77                )
78
79    def __repr__(self):
80        return f"<{self.__class__.__name__}(allowed={smart_repr(self.allowed)})>"
JWT_SIGNING_KEY_TYPES = [KeyType.RSA, KeyType.EC, KeyType.ED25519, KeyType.ED448, KeyType.MLDSA44, KeyType.MLDSA65, KeyType.MLDSA87]
JWE_ENCRYPTION_KEY_TYPES = [KeyType.RSA]
XML_SIGNING_KEY_TYPES = [KeyType.RSA, KeyType.EC, KeyType.DSA]
TLS_KEY_TYPES = [KeyType.RSA, KeyType.EC, KeyType.ED25519, KeyType.MLDSA44, KeyType.MLDSA65, KeyType.MLDSA87]
class KeyTypeValidator:
43class KeyTypeValidator:
44    """Field-level validator that ensures a Certificate-Key Pair uses a key algorithm the
45    consuming protocol can actually handle.
46
47    Accepts either a single keypair or a list of them, so it can be attached to both a
48    ForeignKey and a ManyToMany field.
49
50    A keypair whose `key_type` is null is rejected rather than allowed through: null means
51    `detect_key_type` did not recognize the certificate's public key, so we cannot vouch for it
52    being usable, and letting it through is what causes downstream signing to raise instead of
53    returning a validation error."""
54
55    allowed: list[KeyType]
56    message = _("Key type {key_type} is not supported. Supported key types are: {allowed}.")
57    unknown_message = _(
58        "Unable to determine the key type of this certificate. "
59        "Supported key types are: {allowed}."
60    )
61
62    def __init__(self, *allowed: KeyType, message: str | None = None) -> None:
63        self.allowed = list(allowed)
64        self.message = message or self.message
65
66    def __call__(self, value: CertificateKeyPair | list[CertificateKeyPair] | None):
67        keypairs = value if isinstance(value, list) else [value]
68        allowed = ", ".join(str(KeyType(key_type).label) for key_type in self.allowed)
69        for keypair in keypairs:
70            if not keypair:
71                continue
72            if not keypair.key_type:
73                raise ValidationError(self.unknown_message.format(allowed=allowed), code="invalid")
74            if keypair.key_type not in self.allowed:
75                raise ValidationError(
76                    self.message.format(key_type=KeyType(keypair.key_type).label, allowed=allowed),
77                    code="invalid",
78                )
79
80    def __repr__(self):
81        return f"<{self.__class__.__name__}(allowed={smart_repr(self.allowed)})>"

Field-level validator that ensures a Certificate-Key Pair uses a key algorithm the consuming protocol can actually handle.

Accepts either a single keypair or a list of them, so it can be attached to both a ForeignKey and a ManyToMany field.

A keypair whose key_type is null is rejected rather than allowed through: null means detect_key_type did not recognize the certificate's public key, so we cannot vouch for it being usable, and letting it through is what causes downstream signing to raise instead of returning a validation error.

KeyTypeValidator( *allowed: authentik.crypto.models.KeyType, message: str | None = None)
62    def __init__(self, *allowed: KeyType, message: str | None = None) -> None:
63        self.allowed = list(allowed)
64        self.message = message or self.message
message = 'Key type {key_type} is not supported. Supported key types are: {allowed}.'
unknown_message = 'Unable to determine the key type of this certificate. Supported key types are: {allowed}.'