1 use super::*;
2 
3 #[test]
test_parse_signature_schemes() -> Result<()>4 fn test_parse_signature_schemes() -> Result<()> {
5     let tests = vec![
6         (
7             "Translate",
8             vec![
9                 SignatureScheme::EcdsaWithP256AndSha256 as u16,
10                 SignatureScheme::EcdsaWithP384AndSha384 as u16,
11                 SignatureScheme::EcdsaWithP521AndSha512 as u16,
12                 SignatureScheme::Pkcs1WithSha256 as u16,
13                 SignatureScheme::Pkcs1WithSha384 as u16,
14                 SignatureScheme::Pkcs1WithSha512 as u16,
15             ],
16             vec![
17                 SignatureHashAlgorithm {
18                     hash: HashAlgorithm::Sha256,
19                     signature: SignatureAlgorithm::Ecdsa,
20                 },
21                 SignatureHashAlgorithm {
22                     hash: HashAlgorithm::Sha384,
23                     signature: SignatureAlgorithm::Ecdsa,
24                 },
25                 SignatureHashAlgorithm {
26                     hash: HashAlgorithm::Sha512,
27                     signature: SignatureAlgorithm::Ecdsa,
28                 },
29                 SignatureHashAlgorithm {
30                     hash: HashAlgorithm::Sha256,
31                     signature: SignatureAlgorithm::Rsa,
32                 },
33                 SignatureHashAlgorithm {
34                     hash: HashAlgorithm::Sha384,
35                     signature: SignatureAlgorithm::Rsa,
36                 },
37                 SignatureHashAlgorithm {
38                     hash: HashAlgorithm::Sha512,
39                     signature: SignatureAlgorithm::Rsa,
40                 },
41             ],
42             false,
43             None,
44         ),
45         (
46             "InvalidSignatureAlgorithm",
47             vec![
48                 SignatureScheme::EcdsaWithP256AndSha256 as u16, // Valid
49                 0x04FF, // Invalid: unknown signature with SHA-256
50             ],
51             vec![],
52             false,
53             Some(Error::ErrInvalidSignatureAlgorithm),
54         ),
55         (
56             "InvalidHashAlgorithm",
57             vec![
58                 SignatureScheme::EcdsaWithP256AndSha256 as u16, // Valid
59                 0x0003,                                         // Invalid: ECDSA with MD2
60             ],
61             vec![],
62             false,
63             Some(Error::ErrInvalidHashAlgorithm),
64         ),
65         (
66             "InsecureHashAlgorithmDenied",
67             vec![
68                 SignatureScheme::EcdsaWithP256AndSha256 as u16, // Valid
69                 SignatureScheme::EcdsaWithSha1 as u16,          // Insecure
70             ],
71             vec![SignatureHashAlgorithm {
72                 hash: HashAlgorithm::Sha256,
73                 signature: SignatureAlgorithm::Ecdsa,
74             }],
75             false,
76             None,
77         ),
78         (
79             "InsecureHashAlgorithmAllowed",
80             vec![
81                 SignatureScheme::EcdsaWithP256AndSha256 as u16, // Valid
82                 SignatureScheme::EcdsaWithSha1 as u16,          // Insecure
83             ],
84             vec![
85                 SignatureHashAlgorithm {
86                     hash: HashAlgorithm::Sha256,
87                     signature: SignatureAlgorithm::Ecdsa,
88                 },
89                 SignatureHashAlgorithm {
90                     hash: HashAlgorithm::Sha1,
91                     signature: SignatureAlgorithm::Ecdsa,
92                 },
93             ],
94             true,
95             None,
96         ),
97         (
98             "OnlyInsecureHashAlgorithm",
99             vec![
100                 SignatureScheme::EcdsaWithSha1 as u16, // Insecure
101             ],
102             vec![],
103             false,
104             Some(Error::ErrNoAvailableSignatureSchemes),
105         ),
106         (
107             "Translate",
108             vec![SignatureScheme::Ed25519 as u16],
109             vec![SignatureHashAlgorithm {
110                 hash: HashAlgorithm::Ed25519,
111                 signature: SignatureAlgorithm::Ed25519,
112             }],
113             false,
114             None,
115         ),
116     ];
117 
118     for (name, inputs, expected, insecure_hashes, want_err) in tests {
119         let output = parse_signature_schemes(&inputs, insecure_hashes);
120         if let Some(err) = want_err {
121             if let Err(output_err) = output {
122                 assert_eq!(
123                     err.to_string(),
124                     output_err.to_string(),
125                     "Expected error: {err:?}, got: {output_err:?}"
126                 );
127             } else {
128                 panic!("expect err, but got non-err for {name}");
129             }
130         } else if let Ok(output_val) = output {
131             assert_eq!(
132                 expected, output_val,
133                 "Expected signatureHashAlgorithm:\n{expected:?}\ngot:\n{output_val:?}",
134             );
135         } else {
136             panic!("expect non-err, but got err for {name}");
137         }
138     }
139 
140     Ok(())
141 }
142