1use crate::{HashAlgorithm, Profile};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum PeerCertificateRuleId {
7 Unrestricted,
9 Preferred,
11 Cnsa2,
13 Cnsa1,
15 Bsi,
17 Fips1403,
19 Fips1402,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub enum PeerCurve {
26 P256,
28 P384,
30 P521,
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
36pub enum MlDsaParameterSet {
37 MlDsa44,
39 MlDsa65,
41 MlDsa87,
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum RsaSizeRule {
48 Minimum(u16),
50 Exact(&'static [u16]),
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub struct PeerKeyRule {
57 pub ec_curves: &'static [PeerCurve],
59 pub rsa: Option<RsaSizeRule>,
61 pub rsa_even_bits: bool,
63 pub ed25519: bool,
65 pub ml_dsa: &'static [MlDsaParameterSet],
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum PeerKey {
72 Ec(PeerCurve),
74 Rsa(u16),
76 Ed25519,
78 MlDsa(MlDsaParameterSet),
80 Unknown,
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum CertificateSignature {
87 Ecdsa(PeerCurve, HashAlgorithm),
89 RsaPss(HashAlgorithm),
91 RsaPkcs1(HashAlgorithm),
93 Ed25519,
95 MlDsa(MlDsaParameterSet),
97 Unknown,
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum PeerCertificateViolation {
104 LeafKey(PeerKey),
106 ChainSignature(CertificateSignature),
108 IssuerKey(PeerKey),
110 Malformed,
112}
113
114impl std::fmt::Display for PeerCertificateViolation {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 match self {
117 Self::LeafKey(key) => write!(f, "leaf key {key:?}"),
118 Self::ChainSignature(signature) => write!(f, "chain signature {signature:?}"),
119 Self::IssuerKey(key) => write!(f, "issuer key {key:?}"),
120 Self::Malformed => f.write_str("malformed certificate key or algorithm parameters"),
121 }
122 }
123}
124
125impl std::error::Error for PeerCertificateViolation {}
126
127#[derive(Debug, Clone, Copy, PartialEq, Eq)]
129pub struct PeerCertificateRule {
130 pub id: PeerCertificateRuleId,
132 pub leaf_keys: PeerKeyRule,
134 pub issuer_keys: PeerKeyRule,
136 pub signatures: &'static [CertificateSignature],
138 pub citations: &'static [&'static str],
140}
141
142use CertificateSignature::*;
143use HashAlgorithm::{Sha256, Sha384, Sha512};
144use MlDsaParameterSet::{MlDsa65, MlDsa87};
145use PeerCurve::{P256, P384, P521};
146static UNRESTRICTED: PeerCertificateRule = PeerCertificateRule {
147 id: PeerCertificateRuleId::Unrestricted,
148 leaf_keys: PeerKeyRule {
149 ec_curves: &[P256, P384, P521],
150 rsa: Some(RsaSizeRule::Minimum(2048)),
151 rsa_even_bits: false,
152 ed25519: true,
153 ml_dsa: &[MlDsa87],
154 },
155 issuer_keys: PeerKeyRule {
156 ec_curves: &[P256, P384, P521],
157 rsa: Some(RsaSizeRule::Minimum(2048)),
158 rsa_even_bits: false,
159 ed25519: true,
160 ml_dsa: &[MlDsa87],
161 },
162 signatures: &[
163 Ecdsa(P256, Sha256),
164 Ecdsa(P256, Sha384),
165 Ecdsa(P256, Sha512),
166 Ecdsa(P384, Sha256),
167 Ecdsa(P384, Sha384),
168 Ecdsa(P384, Sha512),
169 Ecdsa(P521, Sha256),
170 Ecdsa(P521, Sha384),
171 Ecdsa(P521, Sha512),
172 RsaPss(Sha256),
173 RsaPss(Sha384),
174 RsaPss(Sha512),
175 RsaPkcs1(Sha256),
176 RsaPkcs1(Sha384),
177 RsaPkcs1(Sha512),
178 Ed25519,
179 MlDsa(MlDsa87),
180 ],
181 citations: &["Crate recommendation: docs/profiles.md sections 3 and 5"],
182};
183static PREFERRED: PeerCertificateRule = PeerCertificateRule {
184 id: PeerCertificateRuleId::Preferred,
185 leaf_keys: PeerKeyRule {
186 ec_curves: &[P256, P384, P521],
187 rsa: Some(RsaSizeRule::Minimum(3072)),
188 rsa_even_bits: false,
189 ed25519: false,
190 ml_dsa: &[MlDsa65, MlDsa87],
191 },
192 issuer_keys: PeerKeyRule {
193 ec_curves: &[P256, P384, P521],
194 rsa: Some(RsaSizeRule::Minimum(3072)),
195 rsa_even_bits: false,
196 ed25519: false,
197 ml_dsa: &[],
198 },
199 signatures: &[
200 Ecdsa(P256, Sha256),
201 Ecdsa(P256, Sha384),
202 Ecdsa(P256, Sha512),
203 Ecdsa(P384, Sha256),
204 Ecdsa(P384, Sha384),
205 Ecdsa(P384, Sha512),
206 Ecdsa(P521, Sha256),
207 Ecdsa(P521, Sha384),
208 Ecdsa(P521, Sha512),
209 RsaPss(Sha256),
210 RsaPss(Sha384),
211 RsaPss(Sha512),
212 RsaPkcs1(Sha256),
213 RsaPkcs1(Sha384),
214 RsaPkcs1(Sha512),
215 ],
216 citations: &[
217 "ASD ISM September 2026, ISM-1763 p. 8, ISM-1991 p. 9, ISM-1765 p. 10, ISM-1374 p. 20",
218 ],
219};
220static CNSA2: PeerCertificateRule = PeerCertificateRule {
221 id: PeerCertificateRuleId::Cnsa2,
222 leaf_keys: PeerKeyRule {
223 ec_curves: &[],
224 rsa: None,
225 rsa_even_bits: false,
226 ed25519: false,
227 ml_dsa: &[MlDsa87],
228 },
229 issuer_keys: PeerKeyRule {
230 ec_curves: &[],
231 rsa: None,
232 rsa_even_bits: false,
233 ed25519: false,
234 ml_dsa: &[MlDsa87],
235 },
236 signatures: &[MlDsa(MlDsa87)],
237 citations: &["CNSSP 15 December 2024, Annex B paragraph 1, pp. B-1 to B-2"],
238};
239static CNSA1: PeerCertificateRule = PeerCertificateRule {
240 id: PeerCertificateRuleId::Cnsa1,
241 leaf_keys: PeerKeyRule {
242 ec_curves: &[P384],
243 rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
244 rsa_even_bits: false,
245 ed25519: false,
246 ml_dsa: &[],
247 },
248 issuer_keys: PeerKeyRule {
249 ec_curves: &[P384],
250 rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
251 rsa_even_bits: false,
252 ed25519: false,
253 ml_dsa: &[],
254 },
255 signatures: &[Ecdsa(P384, Sha384), RsaPss(Sha384), RsaPkcs1(Sha384)],
256 citations: &["RFC 8603 section 4.1; RFC 9151 sections 5.2, 5.4, 6.3, 7.2"],
257};
258static BSI: PeerCertificateRule = PeerCertificateRule {
259 id: PeerCertificateRuleId::Bsi,
260 leaf_keys: PeerKeyRule {
261 ec_curves: &[P256, P384, P521],
262 rsa: Some(RsaSizeRule::Minimum(3000)),
263 rsa_even_bits: false,
264 ed25519: false,
265 ml_dsa: &[],
266 },
267 issuer_keys: PeerKeyRule {
268 ec_curves: &[P256, P384, P521],
269 rsa: Some(RsaSizeRule::Minimum(3000)),
270 rsa_even_bits: false,
271 ed25519: false,
272 ml_dsa: &[],
273 },
274 signatures: &[
275 Ecdsa(P256, Sha256),
276 Ecdsa(P384, Sha384),
277 Ecdsa(P521, Sha512),
278 RsaPss(Sha256),
279 RsaPss(Sha384),
280 RsaPss(Sha512),
281 ],
282 citations: &["BSI TR-02102-2 version 2026-01, sections 3.4.3 and 3.6, Tables 12 and 14"],
283};
284static FIPS1403: PeerCertificateRule = PeerCertificateRule {
285 id: PeerCertificateRuleId::Fips1403,
286 leaf_keys: PeerKeyRule {
287 ec_curves: &[P256, P384, P521],
288 rsa: Some(RsaSizeRule::Minimum(2048)),
289 rsa_even_bits: true,
290 ed25519: true,
291 ml_dsa: &[MlDsa87],
292 },
293 issuer_keys: PeerKeyRule {
294 ec_curves: &[P256, P384, P521],
295 rsa: Some(RsaSizeRule::Minimum(2048)),
296 rsa_even_bits: true,
297 ed25519: true,
298 ml_dsa: &[MlDsa87],
299 },
300 signatures: &[
301 Ecdsa(P256, Sha256),
302 Ecdsa(P256, Sha384),
303 Ecdsa(P256, Sha512),
304 Ecdsa(P384, Sha256),
305 Ecdsa(P384, Sha384),
306 Ecdsa(P384, Sha512),
307 Ecdsa(P521, Sha256),
308 Ecdsa(P521, Sha384),
309 Ecdsa(P521, Sha512),
310 RsaPss(Sha256),
311 RsaPss(Sha384),
312 RsaPss(Sha512),
313 RsaPkcs1(Sha256),
314 RsaPkcs1(Sha384),
315 RsaPkcs1(Sha512),
316 Ed25519,
317 MlDsa(MlDsa87),
318 ],
319 citations: &[
320 "NIST SP 800-52r2 section 3.3.3; FIPS 186-5 sections 5.1, 5.4, 6.4, 7; CMVP approved algorithm lists",
321 ],
322};
323static FIPS1402: PeerCertificateRule = PeerCertificateRule {
324 id: PeerCertificateRuleId::Fips1402,
325 leaf_keys: PeerKeyRule {
326 ec_curves: &[P256, P384, P521],
327 rsa: Some(RsaSizeRule::Minimum(2048)),
328 rsa_even_bits: false,
329 ed25519: false,
330 ml_dsa: &[],
331 },
332 issuer_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 signatures: &[
340 Ecdsa(P256, Sha256),
341 Ecdsa(P256, Sha384),
342 Ecdsa(P256, Sha512),
343 Ecdsa(P384, Sha256),
344 Ecdsa(P384, Sha384),
345 Ecdsa(P384, Sha512),
346 Ecdsa(P521, Sha256),
347 Ecdsa(P521, Sha384),
348 Ecdsa(P521, Sha512),
349 RsaPss(Sha256),
350 RsaPss(Sha384),
351 RsaPss(Sha512),
352 RsaPkcs1(Sha256),
353 RsaPkcs1(Sha384),
354 RsaPkcs1(Sha512),
355 ],
356 citations: &[
357 "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",
358 ],
359};
360
361pub fn peer_certificate_rule(profile: Profile) -> &'static PeerCertificateRule {
363 match profile {
364 Profile::Unrestricted => &UNRESTRICTED,
365 Profile::Preferred => &PREFERRED,
366 Profile::Cnsa2 => &CNSA2,
367 Profile::Cnsa1 => &CNSA1,
368 Profile::Bsi => &BSI,
369 Profile::Fips1403 => &FIPS1403,
370 Profile::Fips1402 => &FIPS1402,
371 }
372}
373
374#[cfg(test)]
375mod tests {
376 use super::*;
377
378 #[test]
379 fn peer_certificate_typed_causes_are_bounded() {
380 use crate::TlsError;
381 use std::error::Error;
382 let rows = [
383 (
384 PeerCertificateViolation::LeafKey(PeerKey::Rsa(8192)),
385 "leaf key Rsa(8192)",
386 ),
387 (
388 PeerCertificateViolation::ChainSignature(CertificateSignature::Unknown),
389 "chain signature Unknown",
390 ),
391 (
392 PeerCertificateViolation::IssuerKey(PeerKey::Unknown),
393 "issuer key Unknown",
394 ),
395 (
396 PeerCertificateViolation::Malformed,
397 "malformed certificate key or algorithm parameters",
398 ),
399 ];
400 for (violation, text) in rows {
401 assert_eq!(violation.to_string(), text);
402 assert_eq!(violation.source().map(|e| e.to_string()), None);
403 let cause = TlsError::PeerCertificate {
404 profile: Profile::Cnsa1,
405 violation,
406 };
407 assert_eq!(
408 cause,
409 TlsError::PeerCertificate {
410 profile: Profile::Cnsa1,
411 violation
412 }
413 );
414 assert_eq!(
415 cause.to_string(),
416 format!("the cnsa1 peer certificate was refused: {text}")
417 );
418 let error: &(dyn Error + 'static) = &cause;
419 assert_eq!(error.downcast_ref::<TlsError>(), Some(&cause));
420 assert_eq!(
421 error
422 .source()
423 .unwrap()
424 .downcast_ref::<PeerCertificateViolation>(),
425 Some(&violation)
426 );
427 }
428 for (error, text) in [
429 (
430 TlsError::ProtocolVersions,
431 "the provider refused the protocol versions",
432 ),
433 (TlsError::PrivateKey, "the private key was refused"),
434 (
435 TlsError::DefaultProviderInstalled,
436 "a process-default provider is already installed",
437 ),
438 (
439 TlsError::KeyMismatch,
440 "the certificate chain is empty or does not carry the key",
441 ),
442 ] {
443 assert_eq!(error.to_string(), text);
444 assert!(error.source().is_none());
445 }
446 }
447
448 #[test]
449 fn peer_certificate_literal_matrix() {
450 use CertificateSignature::*;
451 use HashAlgorithm::{Sha256, Sha384, Sha512};
452 use MlDsaParameterSet::{MlDsa65, MlDsa87};
453 use PeerCurve::{P256, P384, P521};
454 let classical = &[
455 Ecdsa(P256, Sha256),
456 Ecdsa(P256, Sha384),
457 Ecdsa(P256, Sha512),
458 Ecdsa(P384, Sha256),
459 Ecdsa(P384, Sha384),
460 Ecdsa(P384, Sha512),
461 Ecdsa(P521, Sha256),
462 Ecdsa(P521, Sha384),
463 Ecdsa(P521, Sha512),
464 RsaPss(Sha256),
465 RsaPss(Sha384),
466 RsaPss(Sha512),
467 RsaPkcs1(Sha256),
468 RsaPkcs1(Sha384),
469 RsaPkcs1(Sha512),
470 ];
471 let expanded = &[
472 Ecdsa(P256, Sha256),
473 Ecdsa(P256, Sha384),
474 Ecdsa(P256, Sha512),
475 Ecdsa(P384, Sha256),
476 Ecdsa(P384, Sha384),
477 Ecdsa(P384, Sha512),
478 Ecdsa(P521, Sha256),
479 Ecdsa(P521, Sha384),
480 Ecdsa(P521, Sha512),
481 RsaPss(Sha256),
482 RsaPss(Sha384),
483 RsaPss(Sha512),
484 RsaPkcs1(Sha256),
485 RsaPkcs1(Sha384),
486 RsaPkcs1(Sha512),
487 Ed25519,
488 MlDsa(MlDsa87),
489 ];
490 let rows: [(
491 Profile,
492 PeerCertificateRuleId,
493 PeerKeyRule,
494 PeerKeyRule,
495 &[CertificateSignature],
496 ); 7] = [
497 (
498 Profile::Unrestricted,
499 PeerCertificateRuleId::Unrestricted,
500 PeerKeyRule {
501 ec_curves: &[P256, P384, P521],
502 rsa: Some(RsaSizeRule::Minimum(2048)),
503 rsa_even_bits: false,
504 ed25519: true,
505 ml_dsa: &[MlDsa87],
506 },
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 expanded,
515 ),
516 (
517 Profile::Preferred,
518 PeerCertificateRuleId::Preferred,
519 PeerKeyRule {
520 ec_curves: &[P256, P384, P521],
521 rsa: Some(RsaSizeRule::Minimum(3072)),
522 rsa_even_bits: false,
523 ed25519: false,
524 ml_dsa: &[MlDsa65, MlDsa87],
525 },
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: &[],
532 },
533 classical,
534 ),
535 (
536 Profile::Cnsa2,
537 PeerCertificateRuleId::Cnsa2,
538 PeerKeyRule {
539 ec_curves: &[],
540 rsa: None,
541 rsa_even_bits: false,
542 ed25519: false,
543 ml_dsa: &[MlDsa87],
544 },
545 PeerKeyRule {
546 ec_curves: &[],
547 rsa: None,
548 rsa_even_bits: false,
549 ed25519: false,
550 ml_dsa: &[MlDsa87],
551 },
552 &[MlDsa(MlDsa87)],
553 ),
554 (
555 Profile::Cnsa1,
556 PeerCertificateRuleId::Cnsa1,
557 PeerKeyRule {
558 ec_curves: &[P384],
559 rsa: Some(RsaSizeRule::Exact(&[3072, 4096])),
560 rsa_even_bits: false,
561 ed25519: false,
562 ml_dsa: &[],
563 },
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 &[Ecdsa(P384, Sha384), RsaPss(Sha384), RsaPkcs1(Sha384)],
572 ),
573 (
574 Profile::Bsi,
575 PeerCertificateRuleId::Bsi,
576 PeerKeyRule {
577 ec_curves: &[P256, P384, P521],
578 rsa: Some(RsaSizeRule::Minimum(3000)),
579 rsa_even_bits: false,
580 ed25519: false,
581 ml_dsa: &[],
582 },
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 &[
591 Ecdsa(P256, Sha256),
592 Ecdsa(P384, Sha384),
593 Ecdsa(P521, Sha512),
594 RsaPss(Sha256),
595 RsaPss(Sha384),
596 RsaPss(Sha512),
597 ],
598 ),
599 (
600 Profile::Fips1403,
601 PeerCertificateRuleId::Fips1403,
602 PeerKeyRule {
603 ec_curves: &[P256, P384, P521],
604 rsa: Some(RsaSizeRule::Minimum(2048)),
605 rsa_even_bits: true,
606 ed25519: true,
607 ml_dsa: &[MlDsa87],
608 },
609 PeerKeyRule {
610 ec_curves: &[P256, P384, P521],
611 rsa: Some(RsaSizeRule::Minimum(2048)),
612 rsa_even_bits: true,
613 ed25519: true,
614 ml_dsa: &[MlDsa87],
615 },
616 expanded,
617 ),
618 (
619 Profile::Fips1402,
620 PeerCertificateRuleId::Fips1402,
621 PeerKeyRule {
622 ec_curves: &[P256, P384, P521],
623 rsa: Some(RsaSizeRule::Minimum(2048)),
624 rsa_even_bits: false,
625 ed25519: false,
626 ml_dsa: &[],
627 },
628 PeerKeyRule {
629 ec_curves: &[P256, P384, P521],
630 rsa: Some(RsaSizeRule::Minimum(2048)),
631 rsa_even_bits: false,
632 ed25519: false,
633 ml_dsa: &[],
634 },
635 classical,
636 ),
637 ];
638 for (profile, id, leaf, issuer, signatures) in rows {
639 let rule = peer_certificate_rule(profile);
640 assert_eq!(rule.id, id);
641 assert_eq!(rule.leaf_keys, leaf);
642 assert_eq!(rule.issuer_keys, issuer);
643 assert_eq!(rule.signatures, signatures);
644 assert!(!rule.citations.is_empty());
645 }
646 }
647}