Skip to main content

crypto_profile/
peer_certificate.rs

1//! Immutable certificate rules, independent of handshake signature schemes.
2use crate::{HashAlgorithm, Profile};
3
4/// A finite certificate-policy identity.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[non_exhaustive]
7pub enum PeerCertificateRuleId {
8    /// The unrestricted profile rule.
9    Unrestricted,
10    /// The preferred profile rule.
11    Preferred,
12    /// The cnsa2 profile rule.
13    Cnsa2,
14    /// The cnsa1 profile rule.
15    Cnsa1,
16    /// The bsi profile rule.
17    Bsi,
18    /// The fips1403 profile rule.
19    Fips1403,
20    /// The fips1402 profile rule.
21    Fips1402,
22}
23
24/// A supported named elliptic curve.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26#[non_exhaustive]
27pub enum PeerCurve {
28    /// NIST P-256.
29    P256,
30    /// NIST P-384.
31    P384,
32    /// NIST P-521.
33    P521,
34}
35
36/// A standardized pure ML-DSA parameter set.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum MlDsaParameterSet {
39    /// ML-DSA-44.
40    MlDsa44,
41    /// ML-DSA-65.
42    MlDsa65,
43    /// ML-DSA-87.
44    MlDsa87,
45}
46
47/// The published RSA modulus-size condition, separate from implementation limits.
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49#[non_exhaustive]
50pub enum RsaSizeRule {
51    /// A minimum modulus bit length.
52    Minimum(u16),
53    /// An exact finite list of modulus bit lengths.
54    Exact(&'static [u16]),
55}
56
57/// The key families and parameters admitted for a certificate role.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59#[non_exhaustive]
60pub struct PeerKeyRule {
61    /// Admitted EC curves.
62    pub ec_curves: &'static [PeerCurve],
63    /// RSA modulus-size condition, if RSA is admitted.
64    pub rsa: Option<RsaSizeRule>,
65    /// Whether RSA modulus bit lengths must also be even.
66    pub rsa_even_bits: bool,
67    /// Whether Ed25519 is admitted.
68    pub ed25519: bool,
69    /// Admitted pure ML-DSA parameter sets.
70    pub ml_dsa: &'static [MlDsaParameterSet],
71}
72
73/// A bounded public-key descriptor, carrying no key bytes.
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75#[non_exhaustive]
76pub enum PeerKey {
77    /// An EC key on the named curve.
78    Ec(PeerCurve),
79    /// An RSA key's actual modulus bit length.
80    Rsa(u16),
81    /// An Ed25519 key.
82    Ed25519,
83    /// A pure ML-DSA key.
84    MlDsa(MlDsaParameterSet),
85    /// An unsupported algorithm or parameter OID; input bytes are omitted.
86    Unknown,
87}
88
89/// A certificate signature algorithm, independent of CertificateVerify schemes.
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91#[non_exhaustive]
92pub enum CertificateSignature {
93    /// ECDSA with issuer curve and digest.
94    Ecdsa(PeerCurve, HashAlgorithm),
95    /// RSA-PSS with matching digest/MGF1 and digest-length salt.
96    RsaPss(HashAlgorithm),
97    /// RSA PKCS#1 v1.5 with this digest.
98    RsaPkcs1(HashAlgorithm),
99    /// Ed25519.
100    Ed25519,
101    /// Pure ML-DSA.
102    MlDsa(MlDsaParameterSet),
103    /// An unsupported algorithm or parameter OID; input bytes are omitted.
104    Unknown,
105}
106
107/// Why a peer certificate failed the profile rule. Descriptors never carry DER or key bytes.
108#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109#[non_exhaustive]
110pub enum PeerCertificateViolation {
111    /// The leaf public key is outside the rule.
112    LeafKey(PeerKey),
113    /// A selected-path certificate signature is outside the rule.
114    ChainSignature(CertificateSignature),
115    /// An issuer key, including an anchor signing its child, is outside the rule.
116    IssuerKey(PeerKey),
117    /// Certificate key or algorithm parameters cannot be decoded.
118    Malformed,
119}
120
121impl std::fmt::Display for PeerCertificateViolation {
122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        match self {
124            Self::LeafKey(key) => write!(f, "leaf key {key:?}"),
125            Self::ChainSignature(signature) => write!(f, "chain signature {signature:?}"),
126            Self::IssuerKey(key) => write!(f, "issuer key {key:?}"),
127            Self::Malformed => f.write_str("malformed certificate key or algorithm parameters"),
128        }
129    }
130}
131
132impl std::error::Error for PeerCertificateViolation {}
133
134/// Immutable cited certificate-policy data.
135#[derive(Debug, Clone, Copy, PartialEq, Eq)]
136#[non_exhaustive]
137pub struct PeerCertificateRule {
138    /// Canonical cache identity.
139    pub id: PeerCertificateRuleId,
140    /// Leaf key constraints.
141    pub leaf_keys: PeerKeyRule,
142    /// Issuing key constraints, including the trust anchor.
143    pub issuer_keys: PeerKeyRule,
144    /// Admitted path signature algorithms.
145    pub signatures: &'static [CertificateSignature],
146    /// Public source editions and locations for these constraints.
147    pub citations: &'static [&'static str],
148}
149
150use CertificateSignature::*;
151use HashAlgorithm::{Sha256, Sha384, Sha512};
152use MlDsaParameterSet::{MlDsa65, MlDsa87};
153use PeerCurve::{P256, P384, P521};
154static UNRESTRICTED: PeerCertificateRule = PeerCertificateRule {
155    id: PeerCertificateRuleId::Unrestricted,
156    leaf_keys: PeerKeyRule {
157        ec_curves: &[P256, P384, P521],
158        rsa: Some(RsaSizeRule::Minimum(2048)),
159        rsa_even_bits: false,
160        ed25519: true,
161        ml_dsa: &[MlDsa87],
162    },
163    issuer_keys: PeerKeyRule {
164        ec_curves: &[P256, P384, P521],
165        rsa: Some(RsaSizeRule::Minimum(2048)),
166        rsa_even_bits: false,
167        ed25519: true,
168        ml_dsa: &[MlDsa87],
169    },
170    signatures: &[
171        Ecdsa(P256, Sha256),
172        Ecdsa(P256, Sha384),
173        Ecdsa(P256, Sha512),
174        Ecdsa(P384, Sha256),
175        Ecdsa(P384, Sha384),
176        Ecdsa(P384, Sha512),
177        Ecdsa(P521, Sha256),
178        Ecdsa(P521, Sha384),
179        Ecdsa(P521, Sha512),
180        RsaPss(Sha256),
181        RsaPss(Sha384),
182        RsaPss(Sha512),
183        RsaPkcs1(Sha256),
184        RsaPkcs1(Sha384),
185        RsaPkcs1(Sha512),
186        Ed25519,
187        MlDsa(MlDsa87),
188    ],
189    citations: &["Crate recommendation: docs/profiles.md sections 3 and 5"],
190};
191static PREFERRED: PeerCertificateRule = PeerCertificateRule {
192    id: PeerCertificateRuleId::Preferred,
193    leaf_keys: PeerKeyRule {
194        ec_curves: &[P256, P384, P521],
195        rsa: Some(RsaSizeRule::Minimum(3072)),
196        rsa_even_bits: false,
197        ed25519: false,
198        ml_dsa: &[MlDsa65, MlDsa87],
199    },
200    issuer_keys: PeerKeyRule {
201        ec_curves: &[P256, P384, P521],
202        rsa: Some(RsaSizeRule::Minimum(3072)),
203        rsa_even_bits: false,
204        ed25519: false,
205        ml_dsa: &[],
206    },
207    signatures: &[
208        Ecdsa(P256, Sha256),
209        Ecdsa(P256, Sha384),
210        Ecdsa(P256, Sha512),
211        Ecdsa(P384, Sha256),
212        Ecdsa(P384, Sha384),
213        Ecdsa(P384, Sha512),
214        Ecdsa(P521, Sha256),
215        Ecdsa(P521, Sha384),
216        Ecdsa(P521, Sha512),
217        RsaPss(Sha256),
218        RsaPss(Sha384),
219        RsaPss(Sha512),
220        RsaPkcs1(Sha256),
221        RsaPkcs1(Sha384),
222        RsaPkcs1(Sha512),
223    ],
224    citations: &[
225        "ASD ISM September 2026, ISM-1763 p. 8, ISM-1991 p. 9, ISM-1765 p. 10, ISM-1374 p. 20",
226    ],
227};
228static CNSA2: PeerCertificateRule = PeerCertificateRule {
229    id: PeerCertificateRuleId::Cnsa2,
230    leaf_keys: PeerKeyRule {
231        ec_curves: &[],
232        rsa: None,
233        rsa_even_bits: false,
234        ed25519: false,
235        ml_dsa: &[MlDsa87],
236    },
237    issuer_keys: PeerKeyRule {
238        ec_curves: &[],
239        rsa: None,
240        rsa_even_bits: false,
241        ed25519: false,
242        ml_dsa: &[MlDsa87],
243    },
244    signatures: &[MlDsa(MlDsa87)],
245    citations: &["CNSSP 15 December 2024, Annex B paragraph 1, pp. B-1 to B-2"],
246};
247static CNSA1: PeerCertificateRule = PeerCertificateRule {
248    id: PeerCertificateRuleId::Cnsa1,
249    leaf_keys: PeerKeyRule {
250        ec_curves: &[P384],
251        rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
252        rsa_even_bits: false,
253        ed25519: false,
254        ml_dsa: &[],
255    },
256    issuer_keys: PeerKeyRule {
257        ec_curves: &[P384],
258        rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
259        rsa_even_bits: false,
260        ed25519: false,
261        ml_dsa: &[],
262    },
263    signatures: &[Ecdsa(P384, Sha384), RsaPss(Sha384), RsaPkcs1(Sha384)],
264    citations: &["RFC 8603 section 4.1; RFC 9151 sections 5.2, 5.4, 6.3, 7.2"],
265};
266static BSI: PeerCertificateRule = PeerCertificateRule {
267    id: PeerCertificateRuleId::Bsi,
268    leaf_keys: PeerKeyRule {
269        ec_curves: &[P256, P384, P521],
270        rsa: Some(RsaSizeRule::Minimum(3000)),
271        rsa_even_bits: false,
272        ed25519: false,
273        ml_dsa: &[],
274    },
275    issuer_keys: PeerKeyRule {
276        ec_curves: &[P256, P384, P521],
277        rsa: Some(RsaSizeRule::Minimum(3000)),
278        rsa_even_bits: false,
279        ed25519: false,
280        ml_dsa: &[],
281    },
282    signatures: &[
283        Ecdsa(P256, Sha256),
284        Ecdsa(P384, Sha384),
285        Ecdsa(P521, Sha512),
286        RsaPss(Sha256),
287        RsaPss(Sha384),
288        RsaPss(Sha512),
289    ],
290    citations: &["BSI TR-02102-2 version 2026-01, sections 3.4.3 and 3.6, Tables 12 and 14"],
291};
292static FIPS1403: PeerCertificateRule = PeerCertificateRule {
293    id: PeerCertificateRuleId::Fips1403,
294    leaf_keys: PeerKeyRule {
295        ec_curves: &[P256, P384, P521],
296        rsa: Some(RsaSizeRule::Minimum(2048)),
297        rsa_even_bits: true,
298        ed25519: true,
299        ml_dsa: &[MlDsa87],
300    },
301    issuer_keys: PeerKeyRule {
302        ec_curves: &[P256, P384, P521],
303        rsa: Some(RsaSizeRule::Minimum(2048)),
304        rsa_even_bits: true,
305        ed25519: true,
306        ml_dsa: &[MlDsa87],
307    },
308    signatures: &[
309        Ecdsa(P256, Sha256),
310        Ecdsa(P256, Sha384),
311        Ecdsa(P256, Sha512),
312        Ecdsa(P384, Sha256),
313        Ecdsa(P384, Sha384),
314        Ecdsa(P384, Sha512),
315        Ecdsa(P521, Sha256),
316        Ecdsa(P521, Sha384),
317        Ecdsa(P521, Sha512),
318        RsaPss(Sha256),
319        RsaPss(Sha384),
320        RsaPss(Sha512),
321        RsaPkcs1(Sha256),
322        RsaPkcs1(Sha384),
323        RsaPkcs1(Sha512),
324        Ed25519,
325        MlDsa(MlDsa87),
326    ],
327    citations: &[
328        "NIST SP 800-52r2 section 3.3.3; FIPS 186-5 sections 5.1, 5.4, 6.4, 7; CMVP approved algorithm lists",
329    ],
330};
331static FIPS1402: PeerCertificateRule = PeerCertificateRule {
332    id: PeerCertificateRuleId::Fips1402,
333    leaf_keys: PeerKeyRule {
334        ec_curves: &[P256, P384, P521],
335        rsa: Some(RsaSizeRule::Minimum(2048)),
336        rsa_even_bits: false,
337        ed25519: false,
338        ml_dsa: &[],
339    },
340    issuer_keys: PeerKeyRule {
341        ec_curves: &[P256, P384, P521],
342        rsa: Some(RsaSizeRule::Minimum(2048)),
343        rsa_even_bits: false,
344        ed25519: false,
345        ml_dsa: &[],
346    },
347    signatures: &[
348        Ecdsa(P256, Sha256),
349        Ecdsa(P256, Sha384),
350        Ecdsa(P256, Sha512),
351        Ecdsa(P384, Sha256),
352        Ecdsa(P384, Sha384),
353        Ecdsa(P384, Sha512),
354        Ecdsa(P521, Sha256),
355        Ecdsa(P521, Sha384),
356        Ecdsa(P521, Sha512),
357        RsaPss(Sha256),
358        RsaPss(Sha384),
359        RsaPss(Sha512),
360        RsaPkcs1(Sha256),
361        RsaPkcs1(Sha384),
362        RsaPkcs1(Sha512),
363    ],
364    citations: &[
365        "NIST SP 800-52r2 section 3.3.3; SP 800-131A r2 Table 2; FIPS 140-2 IG A.14 pp. 221-222; FIPS 186-4 sections 5.5 and 6.4",
366    ],
367};
368
369/// The canonical, immutable certificate rule for a profile.
370pub fn peer_certificate_rule(profile: Profile) -> &'static PeerCertificateRule {
371    match profile {
372        Profile::Unrestricted => &UNRESTRICTED,
373        Profile::Preferred => &PREFERRED,
374        Profile::Cnsa2 => &CNSA2,
375        Profile::Cnsa1 => &CNSA1,
376        Profile::Bsi => &BSI,
377        Profile::Fips1403 => &FIPS1403,
378        Profile::Fips1402 => &FIPS1402,
379    }
380}
381
382#[cfg(test)]
383mod tests {
384    use super::*;
385
386    #[test]
387    fn peer_certificate_typed_causes_are_bounded() {
388        use crate::TlsError;
389        use std::error::Error;
390        let rows = [
391            (
392                PeerCertificateViolation::LeafKey(PeerKey::Rsa(8192)),
393                "leaf key Rsa(8192)",
394            ),
395            (
396                PeerCertificateViolation::ChainSignature(CertificateSignature::Unknown),
397                "chain signature Unknown",
398            ),
399            (
400                PeerCertificateViolation::IssuerKey(PeerKey::Unknown),
401                "issuer key Unknown",
402            ),
403            (
404                PeerCertificateViolation::Malformed,
405                "malformed certificate key or algorithm parameters",
406            ),
407        ];
408        for (violation, text) in rows {
409            assert_eq!(violation.to_string(), text);
410            assert_eq!(violation.source().map(|e| e.to_string()), None);
411            let cause = TlsError::PeerCertificate {
412                profile: Profile::Cnsa1,
413                violation,
414            };
415            assert_eq!(
416                cause,
417                TlsError::PeerCertificate {
418                    profile: Profile::Cnsa1,
419                    violation
420                }
421            );
422            assert_eq!(
423                cause.to_string(),
424                format!("the cnsa1 peer certificate was refused: {text}")
425            );
426            let error: &(dyn Error + 'static) = &cause;
427            assert_eq!(error.downcast_ref::<TlsError>(), Some(&cause));
428            assert_eq!(
429                error
430                    .source()
431                    .unwrap()
432                    .downcast_ref::<PeerCertificateViolation>(),
433                Some(&violation)
434            );
435        }
436        for (error, text) in [
437            (
438                TlsError::ProtocolVersions,
439                "the provider refused the protocol versions",
440            ),
441            (TlsError::PrivateKey, "the private key was refused"),
442            (
443                TlsError::DefaultProviderInstalled,
444                "a process-default provider is already installed",
445            ),
446            (
447                TlsError::KeyMismatch,
448                "the certificate chain is empty or does not carry the key",
449            ),
450        ] {
451            assert_eq!(error.to_string(), text);
452            assert!(error.source().is_none());
453        }
454    }
455
456    #[test]
457    fn peer_certificate_literal_matrix() {
458        use CertificateSignature::*;
459        use HashAlgorithm::{Sha256, Sha384, Sha512};
460        use MlDsaParameterSet::{MlDsa65, MlDsa87};
461        use PeerCurve::{P256, P384, P521};
462        let classical = &[
463            Ecdsa(P256, Sha256),
464            Ecdsa(P256, Sha384),
465            Ecdsa(P256, Sha512),
466            Ecdsa(P384, Sha256),
467            Ecdsa(P384, Sha384),
468            Ecdsa(P384, Sha512),
469            Ecdsa(P521, Sha256),
470            Ecdsa(P521, Sha384),
471            Ecdsa(P521, Sha512),
472            RsaPss(Sha256),
473            RsaPss(Sha384),
474            RsaPss(Sha512),
475            RsaPkcs1(Sha256),
476            RsaPkcs1(Sha384),
477            RsaPkcs1(Sha512),
478        ];
479        let expanded = &[
480            Ecdsa(P256, Sha256),
481            Ecdsa(P256, Sha384),
482            Ecdsa(P256, Sha512),
483            Ecdsa(P384, Sha256),
484            Ecdsa(P384, Sha384),
485            Ecdsa(P384, Sha512),
486            Ecdsa(P521, Sha256),
487            Ecdsa(P521, Sha384),
488            Ecdsa(P521, Sha512),
489            RsaPss(Sha256),
490            RsaPss(Sha384),
491            RsaPss(Sha512),
492            RsaPkcs1(Sha256),
493            RsaPkcs1(Sha384),
494            RsaPkcs1(Sha512),
495            Ed25519,
496            MlDsa(MlDsa87),
497        ];
498        let rows: [(
499            Profile,
500            PeerCertificateRuleId,
501            PeerKeyRule,
502            PeerKeyRule,
503            &[CertificateSignature],
504        ); 7] = [
505            (
506                Profile::Unrestricted,
507                PeerCertificateRuleId::Unrestricted,
508                PeerKeyRule {
509                    ec_curves: &[P256, P384, P521],
510                    rsa: Some(RsaSizeRule::Minimum(2048)),
511                    rsa_even_bits: false,
512                    ed25519: true,
513                    ml_dsa: &[MlDsa87],
514                },
515                PeerKeyRule {
516                    ec_curves: &[P256, P384, P521],
517                    rsa: Some(RsaSizeRule::Minimum(2048)),
518                    rsa_even_bits: false,
519                    ed25519: true,
520                    ml_dsa: &[MlDsa87],
521                },
522                expanded,
523            ),
524            (
525                Profile::Preferred,
526                PeerCertificateRuleId::Preferred,
527                PeerKeyRule {
528                    ec_curves: &[P256, P384, P521],
529                    rsa: Some(RsaSizeRule::Minimum(3072)),
530                    rsa_even_bits: false,
531                    ed25519: false,
532                    ml_dsa: &[MlDsa65, MlDsa87],
533                },
534                PeerKeyRule {
535                    ec_curves: &[P256, P384, P521],
536                    rsa: Some(RsaSizeRule::Minimum(3072)),
537                    rsa_even_bits: false,
538                    ed25519: false,
539                    ml_dsa: &[],
540                },
541                classical,
542            ),
543            (
544                Profile::Cnsa2,
545                PeerCertificateRuleId::Cnsa2,
546                PeerKeyRule {
547                    ec_curves: &[],
548                    rsa: None,
549                    rsa_even_bits: false,
550                    ed25519: false,
551                    ml_dsa: &[MlDsa87],
552                },
553                PeerKeyRule {
554                    ec_curves: &[],
555                    rsa: None,
556                    rsa_even_bits: false,
557                    ed25519: false,
558                    ml_dsa: &[MlDsa87],
559                },
560                &[MlDsa(MlDsa87)],
561            ),
562            (
563                Profile::Cnsa1,
564                PeerCertificateRuleId::Cnsa1,
565                PeerKeyRule {
566                    ec_curves: &[P384],
567                    rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
568                    rsa_even_bits: false,
569                    ed25519: false,
570                    ml_dsa: &[],
571                },
572                PeerKeyRule {
573                    ec_curves: &[P384],
574                    rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
575                    rsa_even_bits: false,
576                    ed25519: false,
577                    ml_dsa: &[],
578                },
579                &[Ecdsa(P384, Sha384), RsaPss(Sha384), RsaPkcs1(Sha384)],
580            ),
581            (
582                Profile::Bsi,
583                PeerCertificateRuleId::Bsi,
584                PeerKeyRule {
585                    ec_curves: &[P256, P384, P521],
586                    rsa: Some(RsaSizeRule::Minimum(3000)),
587                    rsa_even_bits: false,
588                    ed25519: false,
589                    ml_dsa: &[],
590                },
591                PeerKeyRule {
592                    ec_curves: &[P256, P384, P521],
593                    rsa: Some(RsaSizeRule::Minimum(3000)),
594                    rsa_even_bits: false,
595                    ed25519: false,
596                    ml_dsa: &[],
597                },
598                &[
599                    Ecdsa(P256, Sha256),
600                    Ecdsa(P384, Sha384),
601                    Ecdsa(P521, Sha512),
602                    RsaPss(Sha256),
603                    RsaPss(Sha384),
604                    RsaPss(Sha512),
605                ],
606            ),
607            (
608                Profile::Fips1403,
609                PeerCertificateRuleId::Fips1403,
610                PeerKeyRule {
611                    ec_curves: &[P256, P384, P521],
612                    rsa: Some(RsaSizeRule::Minimum(2048)),
613                    rsa_even_bits: true,
614                    ed25519: true,
615                    ml_dsa: &[MlDsa87],
616                },
617                PeerKeyRule {
618                    ec_curves: &[P256, P384, P521],
619                    rsa: Some(RsaSizeRule::Minimum(2048)),
620                    rsa_even_bits: true,
621                    ed25519: true,
622                    ml_dsa: &[MlDsa87],
623                },
624                expanded,
625            ),
626            (
627                Profile::Fips1402,
628                PeerCertificateRuleId::Fips1402,
629                PeerKeyRule {
630                    ec_curves: &[P256, P384, P521],
631                    rsa: Some(RsaSizeRule::Minimum(2048)),
632                    rsa_even_bits: false,
633                    ed25519: false,
634                    ml_dsa: &[],
635                },
636                PeerKeyRule {
637                    ec_curves: &[P256, P384, P521],
638                    rsa: Some(RsaSizeRule::Minimum(2048)),
639                    rsa_even_bits: false,
640                    ed25519: false,
641                    ml_dsa: &[],
642                },
643                classical,
644            ),
645        ];
646        for (profile, id, leaf, issuer, signatures) in rows {
647            let rule = peer_certificate_rule(profile);
648            assert_eq!(rule.id, id);
649            assert_eq!(rule.leaf_keys, leaf);
650            assert_eq!(rule.issuer_keys, issuer);
651            assert_eq!(rule.signatures, signatures);
652            assert!(!rule.citations.is_empty());
653        }
654    }
655}