crypto-profile 0.9.0

Named cryptographic profiles over the AWS-LC FIPS module: algorithm types, per-module capabilities and a conformance suite
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
//! Named profiles: algorithm sets a backend resolves into a TLS policy and the key algorithms it
//! admits. A profile is an algorithm set, not a validation claim; whether the running module is
//! validated for it is a separate check.
use crate::algorithm::{
    Algorithm, CipherSuite as C, KxGroup as K, SignatureAlgorithm as S, TlsSignatureScheme as T,
    TlsVersion as V,
};
use crate::capability::{Module, capability};
use crate::error::{CryptoError, Family};
use std::fmt;
use std::str::FromStr;

/// A named profile. Each one that is named for a regime follows that regime's published text, in
/// the edition [`Profile::governing_text`] names, and changes only when a new edition is published.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Profile {
    /// Every algorithm in the inventory, post-quantum key exchange first. Not a government profile.
    Unrestricted,
    /// The crate's recommendation. Not a government profile; see `docs/profiles.md`.
    Preferred,
    /// The CNSA 2.0 algorithm set of CNSSP 15 (December 2024), Annex B, on TLS 1.3.
    Cnsa2,
    /// The CNSA 1.0 algorithm set of CNSSP 15 (20 October 2016), Annex B, in RFC 9151's TLS
    /// profile. A transition profile for CNSA 1.0 counterparts. Not every RFC 9151 requirement is
    /// met: `docs/profiles.md` names the algorithm gaps (the `cnsa1` entry in section 3, sections 4
    /// and 5), and the RFC 8603 certificate profile that ยง5.4 requires is not implemented.
    Cnsa1,
    /// BSI TR-02102-2 and TR-02102-1, version 2026-01.
    Bsi,
    /// What a FIPS 140-3 boundary approves under the published texts.
    Fips1403,
    /// What a FIPS 140-2 boundary approves under the published texts.
    Fips1402,
}

/// The text a profile follows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GoverningText {
    /// The crate's own recommendation, following no published text.
    CrateRecommendation,
    /// Published documents, each with its edition.
    Published {
        /// The documents, each naming its edition.
        documents: &'static [&'static str],
    },
}

impl Profile {
    /// Every profile.
    pub const ALL: [Profile; 7] = [
        Profile::Unrestricted,
        Profile::Preferred,
        Profile::Cnsa2,
        Profile::Cnsa1,
        Profile::Bsi,
        Profile::Fips1403,
        Profile::Fips1402,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            Profile::Unrestricted => 0,
            Profile::Preferred => 1,
            Profile::Cnsa2 => 2,
            Profile::Cnsa1 => 3,
            Profile::Bsi => 4,
            Profile::Fips1403 => 5,
            Profile::Fips1402 => 6,
        }
    }

    /// The profile's stable configuration name.
    pub const fn as_str(self) -> &'static str {
        match self {
            Profile::Unrestricted => "unrestricted",
            Profile::Preferred => "preferred",
            Profile::Cnsa2 => "cnsa2",
            Profile::Cnsa1 => "cnsa1",
            Profile::Bsi => "bsi",
            Profile::Fips1403 => "fips1403",
            Profile::Fips1402 => "fips1402",
        }
    }

    /// The texts the profile follows, with each document's edition. The profile constant's doc
    /// comment also cites the standards and implementation guidance these texts rely on.
    pub const fn governing_text(self) -> GoverningText {
        match self {
            Profile::Unrestricted | Profile::Preferred => GoverningText::CrateRecommendation,
            Profile::Cnsa2 => GoverningText::Published {
                documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
            },
            Profile::Cnsa1 => GoverningText::Published {
                documents: &[
                    "CNSSP 15 (20 October 2016), Annex B, p. B-1",
                    "RFC 9151 (April 2022)",
                ],
            },
            Profile::Bsi => GoverningText::Published {
                documents: &[
                    "BSI TR-02102-2, version 2026-01",
                    "BSI TR-02102-1, version 2026-01",
                ],
            },
            Profile::Fips1403 => GoverningText::Published {
                documents: &[
                    "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
                    "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
                    "NIST SP 800-52r2",
                    "NIST SP 800-131A r2",
                ],
            },
            Profile::Fips1402 => GoverningText::Published {
                documents: &[
                    "FIPS 140-2 Annex A and Annex D (2021)",
                    "FIPS 186-4",
                    "NIST SP 800-131A r2",
                    "NIST SP 800-52r2",
                ],
            },
        }
    }
}

impl fmt::Display for Profile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A name that is not one of the crate's profiles. Names are matched exactly: no trimming and no
/// case folding.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseProfileError(pub String);

impl fmt::Display for ParseProfileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "unknown profile {:?}", self.0)
    }
}

impl std::error::Error for ParseProfileError {}

impl FromStr for Profile {
    type Err = ParseProfileError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Profile::ALL
            .into_iter()
            .find(|p| p.as_str() == s)
            .ok_or_else(|| ParseProfileError(s.to_string()))
    }
}

/// Narrowing applied on top of any profile. The default narrows nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Options {
    /// Keep only post-quantum key exchange (pure ML-KEM and hybrid groups), and so remove TLS 1.2,
    /// which has none. Signatures are not narrowed.
    pub pq_key_exchange_required: bool,
    /// Require peer leaf keys and certification paths to match the profile. Off by default.
    pub peer_certificates_match_profile: bool,
}

impl fmt::Display for Options {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(
            match (
                self.pq_key_exchange_required,
                self.peer_certificates_match_profile,
            ) {
                (false, false) => "no options",
                (true, false) => "post-quantum key exchange required",
                (false, true) => "peer certificates match profile",
                (true, true) => {
                    "post-quantum key exchange required, peer certificates match profile"
                }
            },
        )
    }
}

/// A profile's contents, each list in preference order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProfileSpec {
    /// Protocol versions.
    pub versions: &'static [V],
    /// Cipher suites.
    pub cipher_suites: &'static [C],
    /// Key-exchange groups.
    pub kx_groups: &'static [K],
    /// Handshake signature schemes accepted from the peer.
    pub signature_schemes: &'static [T],
    /// Key algorithms the profile admits for keys generated or used under it.
    pub key_algorithms: &'static [S],
}

impl Profile {
    /// The profile's contents. Each entry's source is cited on the constant it comes from.
    pub const fn spec(self) -> &'static ProfileSpec {
        match self {
            Profile::Unrestricted => &UNRESTRICTED,
            Profile::Preferred => &PREFERRED,
            Profile::Cnsa2 => &CNSA2,
            Profile::Cnsa1 => &CNSA1,
            Profile::Bsi => &BSI,
            Profile::Fips1403 => &FIPS1403,
            Profile::Fips1402 => &FIPS1402,
        }
    }
}

/// `unrestricted`: the crate's own set, following no published text. Not a government profile.
///
/// - Versions: TLS 1.3, TLS 1.2. Suites: all nine. Groups: all seven. Schemes: all eleven. Keys:
///   all nine (`crypto_profile::algorithm`, each family's `ALL`).
pub const UNRESTRICTED: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls13Chacha20Poly1305Sha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
        C::Tls12EcdheEcdsaChacha20Poly1305Sha256,
        C::Tls12EcdheRsaChacha20Poly1305Sha256,
    ],
    kx_groups: &[
        K::MlKem1024,
        K::Secp384r1MlKem1024,
        K::X25519MlKem768,
        K::Secp256r1MlKem768,
        K::Secp384r1,
        K::Secp256r1,
        K::X25519,
    ],
    signature_schemes: &[
        T::MlDsa87,
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::Ed25519,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
        T::RsaPkcs1Sha384,
        T::RsaPkcs1Sha512,
        T::RsaPkcs1Sha256,
    ],
    key_algorithms: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Ed25519,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
        S::Rsa2048Sha384,
    ],
};

/// `preferred`: the crate's recommendation. Not a government profile; see `docs/profiles.md`.
///
/// "ISM" is ASD, *Information security manual, Guidelines for cryptography*, September 2026;
/// SECRET-level controls ISM-1761 and ISM-1763 (p. 8), ISM-1765 (p. 10), ISM-1769 (p. 11).
/// - TLS 1.3 only: ISM-1139, p. 19.
/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: ISM-1369, p. 20; ISM-1769, p. 11.
/// - ML-KEM-1024: ISM-1995, p. 10.
/// - SecP384r1MLKEM1024, X25519MLKEM768, SecP256r1MLKEM768: ISM p. 12; ISM-1996, p. 13; FIPS 140-3 IG D.S
///   Scenario 2 and Additional Comment 8a (August 2026, pp. 223-226), which names all three.
/// - secp384r1, secp256r1: ISM-0474 and ISM-1761, p. 8; ISM-1446, p. 7.
/// - X25519 on its own excluded: FIPS 140-3 IG C.K Resolution 5 (p. 160).
/// - ChaCha20-Poly1305 excluded: ISM p. 6; SP 800-140Cr2 ยง6.1.
/// - Schemes ML-DSA-87 (ISM-1991, p. 9), ECDSA P-384, P-256, P-521 (ISM-1763, p. 8), RSA-PSS
///   with SHA-2 (ISM-1765, p. 10; ISM-1374, p. 20).
/// - Ed25519 excluded: ISM pp. 5-6.
/// - RSA PKCS#1 v1.5 schemes excluded: RFC 8446 ยง4.2.3.
/// - Keys ML-DSA-87 (ISM-1991, p. 9), ECDSA P-384, P-256, P-521 (ISM-1763, p. 8), RSA 3072 and
///   4096 (ISM-1765, p. 10); RSA 2048 excluded: ISM-1765, p. 10.
/// - ECDSA P-521 with SHA-384: ISM-1763, p. 8; SHA-2 output of at least 256 bits, ISM-1767,
///   p. 11.
pub const PREFERRED: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13],
    cipher_suites: &[C::Tls13Aes256GcmSha384, C::Tls13Aes128GcmSha256],
    kx_groups: &[
        K::MlKem1024,
        K::Secp384r1MlKem1024,
        K::X25519MlKem768,
        K::Secp256r1MlKem768,
        K::Secp384r1,
        K::Secp256r1,
    ],
    signature_schemes: &[
        T::MlDsa87,
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
    ],
    key_algorithms: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
    ],
};

/// `cnsa2`: the CNSA 2.0 algorithm set of CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2, on
/// TLS 1.3. No CNSA 2.0 TLS profile is published: CNSSP 15 ยถ8a, footnote 1 (p. 5).
/// - ML-KEM-1024: Annex B ยถ1 table, p. B-1.
/// - ML-DSA-87 scheme and key: Annex B ยถ1 table, p. B-1.
/// - TLS_AES_256_GCM_SHA384: Annex B ยถ1 table, p. B-1.
/// - TLS 1.3 only: ML-KEM-1024 is the set's only key establishment (Annex B ยถ1 table, p. B-1);
///   TLS 1.2 key exchange is ECDHE or FFDHE (RFC 8422, RFC 7919).
pub const CNSA2: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13],
    cipher_suites: &[C::Tls13Aes256GcmSha384],
    kx_groups: &[K::MlKem1024],
    signature_schemes: &[T::MlDsa87],
    key_algorithms: &[S::MlDsa87],
};

/// `cnsa1`: the CNSA 1.0 algorithm set of CNSSP 15 (20 October 2016), Annex B (p. B-1), in the
/// TLS 1.2 and 1.3 profile of RFC 9151 (April 2022). Transition dates: CNSSP 15 (December 2024)
/// ยถ18 (pp. 7-8).
/// - AES-256, ECDH and ECDSA on P-384, SHA-384, RSA of at least 3072 bits: CNSSP 15 (2016)
///   Annex B ยถ1 table, p. B-1.
/// - TLS 1.3 and 1.2: RFC 9151 ยง5. The ยง references below are RFC 9151's.
/// - TLS_AES_256_GCM_SHA384: ยง7.
/// - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: ยง6.
/// - secp384r1: ยง5.1, ยง7.
/// - ecdsa_secp384r1_sha384: ยง6.2, ยง7.1. rsa_pss_rsae_sha384: ยง6.2, ยง7.1. rsa_pkcs1_sha384:
///   ยง6.2; TLS 1.2 only, ยง7.1.
/// - Keys ECDSA P-384 with SHA-384 (ยง5.1); RSA 3072 and 4096 (ยง5.2).
/// - Not implemented by the stack: TLS_RSA_WITH_AES_256_GCM_SHA384 and
///   TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 (ยง6), ffdhe3072 and ffdhe4096 (ยง5.3, ยง7).
pub const CNSA1: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
    ],
    kx_groups: &[K::Secp384r1],
    signature_schemes: &[T::EcdsaNistp384Sha384, T::RsaPssSha384, T::RsaPkcs1Sha384],
    key_algorithms: &[S::EcdsaP384Sha384, S::Rsa3072Sha384, S::Rsa4096Sha384],
};

/// `bsi`: BSI TR-02102-2 (TLS) and TR-02102-1, version 2026-01.
/// - TLS 1.3 and TLS 1.2: TR-02102-2 ยง3.2, Table 2.
/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: TR-02102-2 ยง3.4.4, Table 13.
/// - ECDHE-ECDSA and ECDHE-RSA with AES-128-GCM and AES-256-GCM: TR-02102-2 ยง3.3.1.1, Table 3.
/// - secp384r1, secp256r1: TR-02102-2 Table 10 (TLS 1.3) and Table 6 (TLS 1.2).
/// - ecdsa_secp384r1_sha384, ecdsa_secp256r1_sha256, ecdsa_secp521r1_sha512 and rsa_pss_rsae
///   with SHA-384, 512, 256: TR-02102-2 ยง3.4.3, Table 11; for TLS 1.2, the Table 7 note.
/// - rsa_pkcs1_* excluded: TR-02102-2 Table 7 and Table 12 notes.
/// - Keys ECDSA P-384/SHA-384, P-256/SHA-256, P-521/SHA-512: TR-02102-2 Table 12 and Table 14.
/// - RSA keys excluded: TR-02102-2 Table 12.
/// - ML-DSA excluded: TR-02102-1 ยง5.3.4.
/// - Hybrid groups excluded: TR-02102-2 Table 10 note.
pub const BSI: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
    ],
    kx_groups: &[K::Secp384r1, K::Secp256r1],
    signature_schemes: &[
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
    ],
    key_algorithms: &[S::EcdsaP384Sha384, S::EcdsaP256Sha256, S::EcdsaP521Sha512],
};

/// `fips1403`: NIST SP 800-140C r2 and SP 800-140D r2 with the CMVP lists (updated August 21,
/// 2026), the FIPS 140-3 Implementation Guidance (August 19, 2026), SP 800-52r2 and SP 800-131A
/// r2. An algorithm set, not a validation claim.
/// - TLS 1.3 and 1.2: SP 800-52r2 ยง3.1 (p. 8), ยง4.1 (p. 33). TLS 1.2 extended master secret
///   (RFC 7627): IG D.Q (pp. 220-221).
/// - TLS_AES_256_GCM_SHA384, TLS_AES_128_GCM_SHA256: SP 800-52r2 ยง3.3.1.2 (p. 19); CMVP
///   SP 800-140C list ยง6.2.2.1.
/// - ECDHE-ECDSA and ECDHE-RSA with AES-128-GCM and AES-256-GCM: SP 800-52r2 ยง3.3.1.1.1-2
///   (pp. 16-17).
/// - ChaCha20-Poly1305 excluded: SP 800-140Cr2 ยง6.1; SP 800-52r2 ยง3.3.1.2.
/// - ML-KEM-1024: CMVP SP 800-140D list ยง6.2.10; IG D.S Scenario 1 (p. 223).
/// - SecP384r1MLKEM1024, X25519MLKEM768, SecP256r1MLKEM768: IG D.S Scenario 2 and Additional
///   Comment 8a (pp. 223-226), the three IANA parameter sets it names.
/// - secp384r1, secp256r1: SP 800-56A r3 Appendix D, Table 24 (p. 132); SP 800-52r2 ยง3.4.2.2
///   (pp. 24-25).
/// - X25519 on its own excluded: IG C.K Resolution 5 (p. 160).
/// - ECDSA P-256, P-384, P-521 schemes and keys: SP 800-140C list ยง6.2.3.1; SP 800-186 Table 2
///   (p. 7); SP 800-131A r2 Table 2 (p. 9).
/// - ECDSA P-521 with SHA-384: FIPS 186-5 ยง6.4 (p. 23).
/// - Ed25519 scheme and key: SP 800-140C list ยง6.2.3.1; IG C.A (p. 136); IG C.K Additional
///   Comment 8 (p. 161).
/// - RSA-PSS and RSA PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-5 ยง5.4; IG C.F (pp. 142-143);
///   PKCS#1 v1.5 on TLS 1.2 only, RFC 8446 ยง4.2.3.
/// - RSA 2048, 3072, 4096 keys: FIPS 186-5 ยง5.1; SP 800-131A r2 Table 2.
/// - ML-DSA-87 scheme and key: SP 800-140C list ยง6.2.3.1 (FIPS 204).
pub const FIPS1403: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
    ],
    kx_groups: &[
        K::MlKem1024,
        K::Secp384r1MlKem1024,
        K::X25519MlKem768,
        K::Secp256r1MlKem768,
        K::Secp384r1,
        K::Secp256r1,
    ],
    signature_schemes: &[
        T::MlDsa87,
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::Ed25519,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
        T::RsaPkcs1Sha384,
        T::RsaPkcs1Sha512,
        T::RsaPkcs1Sha256,
    ],
    key_algorithms: &[
        S::MlDsa87,
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Ed25519,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
        S::Rsa2048Sha384,
    ],
};

/// `fips1402`: FIPS 140-2 Annex A and Annex D (2021-10-12 editions), FIPS 186-4, SP 800-131A r2
/// and SP 800-52r2. An algorithm set, not a validation claim.
/// - TLS 1.3 and 1.2, and the AES-GCM suites: SP 800-52r2 ยง3.1, ยง3.3.1.1.1-2, ยง3.3.1.2; TLS 1.3
///   key schedule, Annex D item 11 (RFC 8446 ยง7.1).
/// - ChaCha20-Poly1305 excluded: Annex A.
/// - secp384r1, secp256r1: Annex D item 3 (SP 800-56A r3), Table 24.
/// - ML-KEM and the hybrids excluded: Annex A; Annex D items 12-13.
/// - X25519 excluded: SP 800-56A r3 Table 24.
/// - ECDSA P-256, P-384, P-521 schemes; RSA-PSS and PKCS#1 v1.5 with SHA-256, 384, 512: FIPS 186-4
///   (Annex A, p. 2), ยง5.4-5.5, Appendix D.1.2; SP 800-131A r2 Table 2 (p. 9).
/// - Ed25519 and ML-DSA excluded: Annex A.
/// - ECDSA keys P-384/SHA-384, P-256/SHA-256, P-521/SHA-512, P-521/SHA-384: FIPS 186-4 ยง6.4
///   (pp. 29-30).
/// - RSA 3072 and 2048 keys: FIPS 186-4 ยง5.1; RSA 4096: FIPS 140-2 IG A.14 (p. 221).
pub const FIPS1402: ProfileSpec = ProfileSpec {
    versions: &[V::Tls13, V::Tls12],
    cipher_suites: &[
        C::Tls13Aes256GcmSha384,
        C::Tls13Aes128GcmSha256,
        C::Tls12EcdheEcdsaAes256GcmSha384,
        C::Tls12EcdheRsaAes256GcmSha384,
        C::Tls12EcdheEcdsaAes128GcmSha256,
        C::Tls12EcdheRsaAes128GcmSha256,
    ],
    kx_groups: &[K::Secp384r1, K::Secp256r1],
    signature_schemes: &[
        T::EcdsaNistp384Sha384,
        T::EcdsaNistp256Sha256,
        T::EcdsaNistp521Sha512,
        T::RsaPssSha384,
        T::RsaPssSha512,
        T::RsaPssSha256,
        T::RsaPkcs1Sha384,
        T::RsaPkcs1Sha512,
        T::RsaPkcs1Sha256,
    ],
    key_algorithms: &[
        S::EcdsaP384Sha384,
        S::EcdsaP256Sha256,
        S::EcdsaP521Sha512,
        S::EcdsaP521Sha384,
        S::Rsa3072Sha384,
        S::Rsa4096Sha384,
        S::Rsa2048Sha384,
    ],
};

/// A profile narrowed by its options, every entry implemented by the module's backend, in the
/// profile's preference order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Selection {
    /// Protocol versions.
    pub versions: Vec<V>,
    /// Cipher suites, each of a version in `versions`.
    pub cipher_suites: Vec<C>,
    /// Key-exchange groups.
    pub kx_groups: Vec<K>,
    /// Handshake signature schemes.
    pub signature_schemes: Vec<T>,
    /// Key algorithms.
    pub key_algorithms: Vec<S>,
    /// Entries unavailable on the module line after Options, in family and preference order.
    pub excluded: Vec<Algorithm>,
}

const fn is_tls12_only(t: T) -> bool {
    match t {
        T::RsaPkcs1Sha256 | T::RsaPkcs1Sha384 | T::RsaPkcs1Sha512 => true,
        T::EcdsaNistp256Sha256
        | T::EcdsaNistp384Sha384
        | T::EcdsaNistp521Sha512
        | T::Ed25519
        | T::RsaPssSha256
        | T::RsaPssSha384
        | T::RsaPssSha512
        | T::MlDsa87 => false,
    }
}

fn keep<X: Copy>(items: &[X], mut wanted: impl FnMut(X) -> bool) -> Vec<X> {
    items.iter().copied().filter(|x| wanted(*x)).collect()
}

/// The backend-independent half of a backend's `resolve`: `profile`'s lists, narrowed by
/// `options`; `pq_key_exchange_required` also removes TLS 1.2, its suites and the RSA PKCS#1 v1.5
/// handshake schemes. Records entries the module line lacks in [`Selection::excluded`], then
/// refuses a family left empty with [`CryptoError::EmptyProfile`]. Both use versions, suites,
/// groups, schemes and keys order, preserving preference order. Approval is a separate check.
pub fn select(
    profile: Profile,
    options: Options,
    module: Module,
) -> Result<Selection, CryptoError> {
    select_spec(profile, profile.spec(), options, module)
}

fn select_spec(
    profile: Profile,
    spec: &ProfileSpec,
    options: Options,
    module: Module,
) -> Result<Selection, CryptoError> {
    let pq = options.pq_key_exchange_required;
    let versions = keep(spec.versions, |v| !(pq && v == V::Tls12));
    let cipher_suites = keep(spec.cipher_suites, |c| versions.contains(&c.version()));
    let kx_groups = keep(spec.kx_groups, |k| !pq || k.is_post_quantum());
    let signature_schemes = keep(spec.signature_schemes, |t| !(pq && is_tls12_only(t)));
    let key_algorithms = spec.key_algorithms.to_vec();
    let mut excluded = Vec::new();
    let mut available = |a| {
        let on = capability(module, a).available;
        if !on {
            excluded.push(a);
        }
        on
    };
    let versions = keep(&versions, |v| available(Algorithm::Protocol(v)));
    let cipher_suites = keep(&cipher_suites, |c| available(Algorithm::Cipher(c)));
    let kx_groups = keep(&kx_groups, |k| available(Algorithm::Kx(k)));
    let signature_schemes = keep(&signature_schemes, |t| available(Algorithm::Scheme(t)));
    let key_algorithms = keep(&key_algorithms, |s| available(Algorithm::Signature(s)));
    for (empty, category) in [
        (versions.is_empty(), Family::Version),
        (cipher_suites.is_empty(), Family::CipherSuite),
        (kx_groups.is_empty(), Family::KxGroup),
        (signature_schemes.is_empty(), Family::SignatureScheme),
        (key_algorithms.is_empty(), Family::KeyAlgorithm),
    ] {
        if empty {
            return Err(CryptoError::EmptyProfile {
                profile,
                options,
                category,
            });
        }
    }
    Ok(Selection {
        versions,
        cipher_suites,
        kx_groups,
        signature_schemes,
        key_algorithms,
        excluded,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algorithm::*;
    use crate::capability::{Linkage, Module, ModuleLine};
    use crate::error::{CryptoError, Family};

    #[test]
    fn every_profile_lists_its_schemes_in_the_canonical_order() {
        assert_eq!(
            Profile::Unrestricted.spec().signature_schemes.len(),
            TlsSignatureScheme::ALL.len()
        );
        for p in Profile::ALL {
            let s = p.spec().signature_schemes;
            assert!(
                s.windows(2).all(|w| w[0].preference() < w[1].preference()),
                "{p}"
            );
        }
    }

    const M3S: Module = Module {
        line: ModuleLine::AwsLcFips3,
        linkage: Linkage::Static,
    };
    const M4S: Module = Module {
        line: ModuleLine::AwsLcFips4,
        linkage: Linkage::Static,
    };
    const PQ: Options = Options {
        peer_certificates_match_profile: false,
        pq_key_exchange_required: true,
    };
    const PKCS1: [TlsSignatureScheme; 3] = [
        TlsSignatureScheme::RsaPkcs1Sha256,
        TlsSignatureScheme::RsaPkcs1Sha384,
        TlsSignatureScheme::RsaPkcs1Sha512,
    ];

    fn no_duplicates<X: PartialEq + std::fmt::Debug>(xs: &[X]) {
        for (i, x) in xs.iter().enumerate() {
            assert!(!xs[i + 1..].contains(x), "{x:?} twice");
        }
    }

    #[test]
    fn every_spec_is_well_formed() {
        for p in Profile::ALL {
            let s = p.spec();
            no_duplicates(s.versions);
            no_duplicates(s.cipher_suites);
            no_duplicates(s.kx_groups);
            no_duplicates(s.signature_schemes);
            no_duplicates(s.key_algorithms);
            assert!(!s.versions.is_empty() && !s.kx_groups.is_empty(), "{p}");
            assert!(
                !s.signature_schemes.is_empty() && !s.key_algorithms.is_empty(),
                "{p}"
            );
            assert!(
                s.cipher_suites
                    .iter()
                    .all(|c| s.versions.contains(&c.version())),
                "{p}"
            );
            let has_pkcs1 = s.signature_schemes.iter().any(|t| PKCS1.contains(t));
            assert!(!has_pkcs1 || s.versions.contains(&TlsVersion::Tls12), "{p}");
        }
    }

    #[test]
    fn preferred_excludes_what_its_definition_names() {
        let s = Profile::Preferred.spec();
        assert_eq!(s.versions, &[TlsVersion::Tls13]);
        assert!(!s.kx_groups.contains(&KxGroup::X25519));
        assert!(
            !s.cipher_suites
                .contains(&CipherSuite::Tls13Chacha20Poly1305Sha256)
        );
        assert!(!s.signature_schemes.contains(&TlsSignatureScheme::Ed25519));
        assert!(!s.key_algorithms.contains(&SignatureAlgorithm::Ed25519));
    }

    #[test]
    fn bsi_admits_only_ecdsa_keys() {
        assert_eq!(
            Profile::Bsi.spec().key_algorithms,
            &[
                SignatureAlgorithm::EcdsaP384Sha384,
                SignatureAlgorithm::EcdsaP256Sha256,
                SignatureAlgorithm::EcdsaP521Sha512,
            ]
        );
    }

    #[test]
    fn select_records_line_exclusions_after_options() {
        for module in [
            M3S,
            Module {
                linkage: Linkage::Dynamic,
                ..M3S
            },
        ] {
            for p in [Profile::Unrestricted, Profile::Preferred, Profile::Fips1403] {
                for options in [Options::default(), PQ] {
                    let s = select(p, options, module).unwrap();
                    assert_eq!(
                        s.excluded,
                        vec![
                            Algorithm::Scheme(T::MlDsa87),
                            Algorithm::Signature(S::MlDsa87),
                        ]
                    );
                    no_duplicates(&s.excluded);
                    assert!(!s.signature_schemes.contains(&T::MlDsa87));
                    assert!(!s.key_algorithms.contains(&S::MlDsa87));
                }
            }
            for options in [Options::default(), PQ] {
                assert_eq!(
                    select(Profile::Cnsa2, options, module),
                    Err(CryptoError::EmptyProfile {
                        profile: Profile::Cnsa2,
                        options,
                        category: Family::SignatureScheme,
                    })
                );
            }
            for p in [Profile::Cnsa1, Profile::Bsi, Profile::Fips1402] {
                assert!(
                    select(p, Options::default(), module)
                        .unwrap()
                        .excluded
                        .is_empty()
                );
            }
        }
    }

    #[test]
    fn select_checks_keys_and_options_precede_line_emptiness() {
        const KEY_ONLY: ProfileSpec = ProfileSpec {
            signature_schemes: &[T::EcdsaNistp384Sha384],
            ..CNSA2
        };
        assert_eq!(
            select_spec(Profile::Cnsa2, &KEY_ONLY, Options::default(), M3S),
            Err(CryptoError::EmptyProfile {
                profile: Profile::Cnsa2,
                options: Options::default(),
                category: Family::KeyAlgorithm,
            })
        );
        const BSI_WITH_ML_DSA: ProfileSpec = ProfileSpec {
            signature_schemes: &[T::MlDsa87],
            ..BSI
        };
        for spec in [&BSI_WITH_ML_DSA, &BSI] {
            assert_eq!(
                select_spec(Profile::Bsi, spec, PQ, M3S),
                Err(CryptoError::EmptyProfile {
                    profile: Profile::Bsi,
                    options: PQ,
                    category: Family::KxGroup,
                })
            );
        }
    }

    #[test]
    fn select_refuses_each_empty_family_in_order() {
        const EMPTY: ProfileSpec = ProfileSpec {
            versions: &[],
            cipher_suites: &[],
            kx_groups: &[],
            signature_schemes: &[],
            key_algorithms: &[],
        };
        let empty = |category| {
            Err(CryptoError::EmptyProfile {
                profile: Profile::Bsi,
                options: Options::default(),
                category,
            })
        };
        let cases: [(ProfileSpec, Family); 5] = [
            (EMPTY, Family::Version),
            (
                ProfileSpec {
                    versions: BSI.versions,
                    ..EMPTY
                },
                Family::CipherSuite,
            ),
            (
                ProfileSpec {
                    kx_groups: &[],
                    signature_schemes: &[],
                    key_algorithms: &[],
                    ..BSI
                },
                Family::KxGroup,
            ),
            (
                ProfileSpec {
                    signature_schemes: &[],
                    key_algorithms: &[],
                    ..BSI
                },
                Family::SignatureScheme,
            ),
            (
                ProfileSpec {
                    key_algorithms: &[],
                    ..BSI
                },
                Family::KeyAlgorithm,
            ),
        ];
        for (spec, category) in cases {
            assert_eq!(
                select_spec(Profile::Bsi, &spec, Options::default(), M4S),
                empty(category),
                "{category:?}"
            );
        }
    }

    #[test]
    fn select_without_options_is_the_spec_on_a_module_that_has_everything() {
        for p in Profile::ALL {
            let s = select(p, Options::default(), M4S).unwrap();
            let spec = p.spec();
            assert!(s.excluded.is_empty());
            assert_eq!(s.versions, spec.versions.to_vec(), "{p}");
            assert_eq!(s.cipher_suites, spec.cipher_suites.to_vec(), "{p}");
            assert_eq!(s.kx_groups, spec.kx_groups.to_vec(), "{p}");
            assert_eq!(s.signature_schemes, spec.signature_schemes.to_vec(), "{p}");
            assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
        }
    }

    #[test]
    fn the_option_keeps_post_quantum_groups_tls13_and_no_pkcs1_schemes() {
        for p in Profile::ALL {
            let refused = match p {
                Profile::Unrestricted | Profile::Preferred | Profile::Cnsa2 | Profile::Fips1403 => {
                    false
                }
                Profile::Cnsa1 | Profile::Bsi | Profile::Fips1402 => true,
            };
            match select(p, PQ, M4S) {
                Ok(s) => {
                    assert!(!refused, "{p}");
                    let spec = p.spec();
                    assert_eq!(s.versions, vec![TlsVersion::Tls13], "{p}");
                    let suites: Vec<_> = spec
                        .cipher_suites
                        .iter()
                        .copied()
                        .filter(|c| c.version() == TlsVersion::Tls13)
                        .collect();
                    assert_eq!(s.cipher_suites, suites, "{p}");
                    let groups: Vec<_> = spec
                        .kx_groups
                        .iter()
                        .copied()
                        .filter(|k| k.is_post_quantum())
                        .collect();
                    assert_eq!(s.kx_groups, groups, "{p}");
                    let schemes: Vec<_> = spec
                        .signature_schemes
                        .iter()
                        .copied()
                        .filter(|t| !PKCS1.contains(t))
                        .collect();
                    assert_eq!(s.signature_schemes, schemes, "{p}");
                    assert_eq!(s.key_algorithms, spec.key_algorithms.to_vec(), "{p}");
                }
                Err(e) => {
                    assert!(refused, "{p}");
                    assert_eq!(
                        e,
                        CryptoError::EmptyProfile {
                            profile: p,
                            options: PQ,
                            category: Family::KxGroup
                        }
                    );
                }
            }
        }
    }

    #[test]
    fn the_option_removes_every_pkcs1_scheme_from_unrestricted_and_fips1403() {
        use TlsSignatureScheme as T;
        let expected = vec![
            T::MlDsa87,
            T::EcdsaNistp384Sha384,
            T::EcdsaNistp256Sha256,
            T::EcdsaNistp521Sha512,
            T::Ed25519,
            T::RsaPssSha384,
            T::RsaPssSha512,
            T::RsaPssSha256,
        ];
        for p in [Profile::Unrestricted, Profile::Fips1403] {
            assert_eq!(
                select(p, PQ, M4S).unwrap().signature_schemes,
                expected,
                "{p}"
            );
            assert_eq!(
                select(p, Options::default(), M4S)
                    .unwrap()
                    .signature_schemes
                    .len(),
                expected.len() + 3,
                "{p}"
            );
        }
    }

    #[test]
    fn profile_names_are_stable_and_exact() {
        assert_eq!(
            Profile::ALL.map(Profile::as_str),
            [
                "unrestricted",
                "preferred",
                "cnsa2",
                "cnsa1",
                "bsi",
                "fips1403",
                "fips1402"
            ]
        );
        for p in Profile::ALL {
            assert_eq!(p.as_str().parse::<Profile>(), Ok(p));
            assert_eq!(p.to_string(), p.as_str());
        }
        for bad in [
            "",
            "CNSA2",
            "Cnsa2",
            "FIPS1403",
            "fips140-3",
            "fips-140-3",
            " fips1403",
            "fips1403 ",
            "fips1403\n",
            " bsi",
            "cnsa",
            "bsi2026",
            "preferred,",
            "default",
        ] {
            assert_eq!(
                bad.parse::<Profile>(),
                Err(ParseProfileError(bad.to_string())),
                "{bad:?}"
            );
        }
        assert_eq!(
            ParseProfileError("FIPS1403".to_string()).to_string(),
            "unknown profile \"FIPS1403\""
        );
    }

    #[test]
    fn profile_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            Profile::ALL.map(Profile::ordinal),
            std::array::from_fn::<usize, 7, _>(|i| i)
        );
    }

    #[test]
    fn governing_texts_are_the_published_editions() {
        use GoverningText::{CrateRecommendation as Own, Published};
        let table: [(Profile, GoverningText); 7] = [
            (Profile::Unrestricted, Own),
            (Profile::Preferred, Own),
            (
                Profile::Cnsa2,
                Published {
                    documents: &["CNSSP 15 (December 2024), Annex B, pp. B-1 to B-2"],
                },
            ),
            (
                Profile::Cnsa1,
                Published {
                    documents: &[
                        "CNSSP 15 (20 October 2016), Annex B, p. B-1",
                        "RFC 9151 (April 2022)",
                    ],
                },
            ),
            (
                Profile::Bsi,
                Published {
                    documents: &[
                        "BSI TR-02102-2, version 2026-01",
                        "BSI TR-02102-1, version 2026-01",
                    ],
                },
            ),
            (
                Profile::Fips1403,
                Published {
                    documents: &[
                        "NIST SP 800-140C r2 and SP 800-140D r2, with the CMVP lists (August 2026)",
                        "FIPS 140-3 Implementation Guidance (August 2026): IG C.K, D.Q, D.S",
                        "NIST SP 800-52r2",
                        "NIST SP 800-131A r2",
                    ],
                },
            ),
            (
                Profile::Fips1402,
                Published {
                    documents: &[
                        "FIPS 140-2 Annex A and Annex D (2021)",
                        "FIPS 186-4",
                        "NIST SP 800-131A r2",
                        "NIST SP 800-52r2",
                    ],
                },
            ),
        ];
        for (p, text) in table {
            assert_eq!(p.governing_text(), text, "{p}");
        }
    }

    #[test]
    fn options_default_narrows_nothing_and_display_names_the_option() {
        assert_eq!(
            Options::default(),
            Options {
                peer_certificates_match_profile: false,
                pq_key_exchange_required: false
            }
        );
        assert_eq!(Options::default().to_string(), "no options");
        assert_eq!(
            Options {
                peer_certificates_match_profile: false,
                pq_key_exchange_required: true
            }
            .to_string(),
            "post-quantum key exchange required"
        );
    }
    #[test]
    fn peer_certificate_options_combinations_preserve_selection() {
        for (pq, peer, display) in [
            (false, false, "no options"),
            (true, false, "post-quantum key exchange required"),
            (false, true, "peer certificates match profile"),
            (
                true,
                true,
                "post-quantum key exchange required, peer certificates match profile",
            ),
        ] {
            let options = Options {
                pq_key_exchange_required: pq,
                peer_certificates_match_profile: peer,
            };
            assert_eq!(options.to_string(), display);
            for profile in Profile::ALL {
                for module in [M3S, M4S] {
                    let baseline = Options {
                        peer_certificates_match_profile: false,
                        ..options
                    };
                    let actual = select(profile, options, module);
                    let expected = select(profile, baseline, module);
                    match (actual, expected) {
                        (Ok(actual), Ok(expected)) => assert_eq!(actual, expected),
                        (
                            Err(CryptoError::EmptyProfile {
                                profile: p,
                                options: o,
                                category: f,
                            }),
                            Err(CryptoError::EmptyProfile {
                                profile: ep,
                                options: eo,
                                category: ef,
                            }),
                        ) => {
                            assert_eq!((p, o, f), (ep, options, ef));
                            assert_eq!(eo, baseline);
                        }
                        (actual, expected) => panic!("{actual:?} != {expected:?}"),
                    }
                }
            }
        }
    }
}