1use crate::{HashAlgorithm, Profile};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[non_exhaustive]
7pub enum PeerCertificateRuleId {
8 Unrestricted,
10 Preferred,
12 Cnsa2,
14 Cnsa1,
16 Bsi,
18 Fips1403,
20 Fips1402,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26#[non_exhaustive]
27pub enum PeerCurve {
28 P256,
30 P384,
32 P521,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum MlDsaParameterSet {
39 MlDsa44,
41 MlDsa65,
43 MlDsa87,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49#[non_exhaustive]
50pub enum RsaSizeRule {
51 Minimum(u16),
53 Exact(&'static [u16]),
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59#[non_exhaustive]
60pub struct PeerKeyRule {
61 pub ec_curves: &'static [PeerCurve],
63 pub rsa: Option<RsaSizeRule>,
65 pub rsa_even_bits: bool,
67 pub ed25519: bool,
69 pub ml_dsa: &'static [MlDsaParameterSet],
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75#[non_exhaustive]
76pub enum PeerKey {
77 Ec(PeerCurve),
79 Rsa(u16),
81 Ed25519,
83 MlDsa(MlDsaParameterSet),
85 Unknown,
87}
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91#[non_exhaustive]
92pub enum CertificateSignature {
93 Ecdsa(PeerCurve, HashAlgorithm),
95 RsaPss(HashAlgorithm),
97 RsaPkcs1(HashAlgorithm),
99 Ed25519,
101 MlDsa(MlDsaParameterSet),
103 Unknown,
105}
106
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109#[non_exhaustive]
110pub enum PeerCertificateViolation {
111 LeafKey(PeerKey),
113 ChainSignature(CertificateSignature),
115 IssuerKey(PeerKey),
117 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
136#[non_exhaustive]
137pub struct PeerCertificateRule {
138 pub id: PeerCertificateRuleId,
140 pub leaf_keys: PeerKeyRule,
142 pub issuer_keys: PeerKeyRule,
144 pub signatures: &'static [CertificateSignature],
146 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: false,
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: false,
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 MlDsa(MlDsa87),
325 ],
326 citations: &[
327 "NIST SP 800-52r2 section 3.3.3, section 3.2, Table 3-1 (pp. 10-11) and Table 4-1 (p. 34); FIPS 186-5 sections 5.1, 5.4, 6.4, 7; CMVP approved algorithm lists",
328 ],
329};
330static FIPS1402: PeerCertificateRule = PeerCertificateRule {
331 id: PeerCertificateRuleId::Fips1402,
332 leaf_keys: PeerKeyRule {
333 ec_curves: &[P256, P384, P521],
334 rsa: Some(RsaSizeRule::Minimum(2048)),
335 rsa_even_bits: false,
336 ed25519: false,
337 ml_dsa: &[],
338 },
339 issuer_keys: PeerKeyRule {
340 ec_curves: &[P256, P384, P521],
341 rsa: Some(RsaSizeRule::Minimum(2048)),
342 rsa_even_bits: false,
343 ed25519: false,
344 ml_dsa: &[],
345 },
346 signatures: &[
347 Ecdsa(P256, Sha256),
348 Ecdsa(P256, Sha384),
349 Ecdsa(P256, Sha512),
350 Ecdsa(P384, Sha256),
351 Ecdsa(P384, Sha384),
352 Ecdsa(P384, Sha512),
353 Ecdsa(P521, Sha256),
354 Ecdsa(P521, Sha384),
355 Ecdsa(P521, Sha512),
356 RsaPss(Sha256),
357 RsaPss(Sha384),
358 RsaPss(Sha512),
359 RsaPkcs1(Sha256),
360 RsaPkcs1(Sha384),
361 RsaPkcs1(Sha512),
362 ],
363 citations: &[
364 "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",
365 ],
366};
367
368pub fn peer_certificate_rule(profile: Profile) -> &'static PeerCertificateRule {
370 match profile {
371 Profile::Unrestricted => &UNRESTRICTED,
372 Profile::Preferred => &PREFERRED,
373 Profile::Cnsa2 => &CNSA2,
374 Profile::Cnsa1 => &CNSA1,
375 Profile::Bsi => &BSI,
376 Profile::Fips1403 => &FIPS1403,
377 Profile::Fips1402 => &FIPS1402,
378 }
379}
380
381#[cfg(test)]
382mod tests {
383 use super::*;
384
385 #[test]
386 fn peer_certificate_typed_causes_are_bounded() {
387 use crate::TlsError;
388 use std::error::Error;
389 let rows = [
390 (
391 PeerCertificateViolation::LeafKey(PeerKey::Rsa(8192)),
392 "leaf key Rsa(8192)",
393 ),
394 (
395 PeerCertificateViolation::ChainSignature(CertificateSignature::Unknown),
396 "chain signature Unknown",
397 ),
398 (
399 PeerCertificateViolation::IssuerKey(PeerKey::Unknown),
400 "issuer key Unknown",
401 ),
402 (
403 PeerCertificateViolation::Malformed,
404 "malformed certificate key or algorithm parameters",
405 ),
406 ];
407 for (violation, text) in rows {
408 assert_eq!(violation.to_string(), text);
409 assert_eq!(violation.source().map(|e| e.to_string()), None);
410 let cause = TlsError::PeerCertificate {
411 profile: Profile::Cnsa1,
412 violation,
413 };
414 assert_eq!(
415 cause,
416 TlsError::PeerCertificate {
417 profile: Profile::Cnsa1,
418 violation
419 }
420 );
421 assert_eq!(
422 cause.to_string(),
423 format!("the cnsa1 peer certificate was refused: {text}")
424 );
425 let error: &(dyn Error + 'static) = &cause;
426 assert_eq!(error.downcast_ref::<TlsError>(), Some(&cause));
427 assert_eq!(
428 error
429 .source()
430 .unwrap()
431 .downcast_ref::<PeerCertificateViolation>(),
432 Some(&violation)
433 );
434 }
435 for (error, text) in [
436 (
437 TlsError::ProtocolVersions,
438 "the provider refused the protocol versions",
439 ),
440 (TlsError::PrivateKey, "the private key was refused"),
441 (
442 TlsError::DefaultProviderInstalled,
443 "a process-default provider is already installed",
444 ),
445 (
446 TlsError::KeyMismatch,
447 "the certificate chain is empty or does not carry the key",
448 ),
449 ] {
450 assert_eq!(error.to_string(), text);
451 assert!(error.source().is_none());
452 }
453 }
454
455 #[test]
456 fn peer_certificate_literal_matrix() {
457 use CertificateSignature::*;
458 use HashAlgorithm::{Sha256, Sha384, Sha512};
459 use MlDsaParameterSet::{MlDsa65, MlDsa87};
460 use PeerCurve::{P256, P384, P521};
461 let classical = &[
462 Ecdsa(P256, Sha256),
463 Ecdsa(P256, Sha384),
464 Ecdsa(P256, Sha512),
465 Ecdsa(P384, Sha256),
466 Ecdsa(P384, Sha384),
467 Ecdsa(P384, Sha512),
468 Ecdsa(P521, Sha256),
469 Ecdsa(P521, Sha384),
470 Ecdsa(P521, Sha512),
471 RsaPss(Sha256),
472 RsaPss(Sha384),
473 RsaPss(Sha512),
474 RsaPkcs1(Sha256),
475 RsaPkcs1(Sha384),
476 RsaPkcs1(Sha512),
477 ];
478 let expanded = &[
479 Ecdsa(P256, Sha256),
480 Ecdsa(P256, Sha384),
481 Ecdsa(P256, Sha512),
482 Ecdsa(P384, Sha256),
483 Ecdsa(P384, Sha384),
484 Ecdsa(P384, Sha512),
485 Ecdsa(P521, Sha256),
486 Ecdsa(P521, Sha384),
487 Ecdsa(P521, Sha512),
488 RsaPss(Sha256),
489 RsaPss(Sha384),
490 RsaPss(Sha512),
491 RsaPkcs1(Sha256),
492 RsaPkcs1(Sha384),
493 RsaPkcs1(Sha512),
494 Ed25519,
495 MlDsa(MlDsa87),
496 ];
497 let rows: [(
498 Profile,
499 PeerCertificateRuleId,
500 PeerKeyRule,
501 PeerKeyRule,
502 &[CertificateSignature],
503 ); 7] = [
504 (
505 Profile::Unrestricted,
506 PeerCertificateRuleId::Unrestricted,
507 PeerKeyRule {
508 ec_curves: &[P256, P384, P521],
509 rsa: Some(RsaSizeRule::Minimum(2048)),
510 rsa_even_bits: false,
511 ed25519: true,
512 ml_dsa: &[MlDsa87],
513 },
514 PeerKeyRule {
515 ec_curves: &[P256, P384, P521],
516 rsa: Some(RsaSizeRule::Minimum(2048)),
517 rsa_even_bits: false,
518 ed25519: true,
519 ml_dsa: &[MlDsa87],
520 },
521 expanded,
522 ),
523 (
524 Profile::Preferred,
525 PeerCertificateRuleId::Preferred,
526 PeerKeyRule {
527 ec_curves: &[P256, P384, P521],
528 rsa: Some(RsaSizeRule::Minimum(3072)),
529 rsa_even_bits: false,
530 ed25519: false,
531 ml_dsa: &[MlDsa65, MlDsa87],
532 },
533 PeerKeyRule {
534 ec_curves: &[P256, P384, P521],
535 rsa: Some(RsaSizeRule::Minimum(3072)),
536 rsa_even_bits: false,
537 ed25519: false,
538 ml_dsa: &[],
539 },
540 classical,
541 ),
542 (
543 Profile::Cnsa2,
544 PeerCertificateRuleId::Cnsa2,
545 PeerKeyRule {
546 ec_curves: &[],
547 rsa: None,
548 rsa_even_bits: false,
549 ed25519: false,
550 ml_dsa: &[MlDsa87],
551 },
552 PeerKeyRule {
553 ec_curves: &[],
554 rsa: None,
555 rsa_even_bits: false,
556 ed25519: false,
557 ml_dsa: &[MlDsa87],
558 },
559 &[MlDsa(MlDsa87)],
560 ),
561 (
562 Profile::Cnsa1,
563 PeerCertificateRuleId::Cnsa1,
564 PeerKeyRule {
565 ec_curves: &[P384],
566 rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
567 rsa_even_bits: false,
568 ed25519: false,
569 ml_dsa: &[],
570 },
571 PeerKeyRule {
572 ec_curves: &[P384],
573 rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
574 rsa_even_bits: false,
575 ed25519: false,
576 ml_dsa: &[],
577 },
578 &[Ecdsa(P384, Sha384), RsaPss(Sha384), RsaPkcs1(Sha384)],
579 ),
580 (
581 Profile::Bsi,
582 PeerCertificateRuleId::Bsi,
583 PeerKeyRule {
584 ec_curves: &[P256, P384, P521],
585 rsa: Some(RsaSizeRule::Minimum(3000)),
586 rsa_even_bits: false,
587 ed25519: false,
588 ml_dsa: &[],
589 },
590 PeerKeyRule {
591 ec_curves: &[P256, P384, P521],
592 rsa: Some(RsaSizeRule::Minimum(3000)),
593 rsa_even_bits: false,
594 ed25519: false,
595 ml_dsa: &[],
596 },
597 &[
598 Ecdsa(P256, Sha256),
599 Ecdsa(P384, Sha384),
600 Ecdsa(P521, Sha512),
601 RsaPss(Sha256),
602 RsaPss(Sha384),
603 RsaPss(Sha512),
604 ],
605 ),
606 (
607 Profile::Fips1403,
608 PeerCertificateRuleId::Fips1403,
609 PeerKeyRule {
610 ec_curves: &[P256, P384, P521],
611 rsa: Some(RsaSizeRule::Minimum(2048)),
612 rsa_even_bits: true,
613 ed25519: false,
614 ml_dsa: &[MlDsa87],
615 },
616 PeerKeyRule {
617 ec_curves: &[P256, P384, P521],
618 rsa: Some(RsaSizeRule::Minimum(2048)),
619 rsa_even_bits: true,
620 ed25519: false,
621 ml_dsa: &[MlDsa87],
622 },
623 &[
624 Ecdsa(P256, Sha256),
625 Ecdsa(P256, Sha384),
626 Ecdsa(P256, Sha512),
627 Ecdsa(P384, Sha256),
628 Ecdsa(P384, Sha384),
629 Ecdsa(P384, Sha512),
630 Ecdsa(P521, Sha256),
631 Ecdsa(P521, Sha384),
632 Ecdsa(P521, Sha512),
633 RsaPss(Sha256),
634 RsaPss(Sha384),
635 RsaPss(Sha512),
636 RsaPkcs1(Sha256),
637 RsaPkcs1(Sha384),
638 RsaPkcs1(Sha512),
639 MlDsa(MlDsa87),
640 ],
641 ),
642 (
643 Profile::Fips1402,
644 PeerCertificateRuleId::Fips1402,
645 PeerKeyRule {
646 ec_curves: &[P256, P384, P521],
647 rsa: Some(RsaSizeRule::Minimum(2048)),
648 rsa_even_bits: false,
649 ed25519: false,
650 ml_dsa: &[],
651 },
652 PeerKeyRule {
653 ec_curves: &[P256, P384, P521],
654 rsa: Some(RsaSizeRule::Minimum(2048)),
655 rsa_even_bits: false,
656 ed25519: false,
657 ml_dsa: &[],
658 },
659 classical,
660 ),
661 ];
662 for (profile, id, leaf, issuer, signatures) in rows {
663 let rule = peer_certificate_rule(profile);
664 assert_eq!(rule.id, id);
665 assert_eq!(rule.leaf_keys, leaf);
666 assert_eq!(rule.issuer_keys, issuer);
667 assert_eq!(rule.signatures, signatures);
668 assert!(!rule.citations.is_empty());
669 }
670 }
671}