1use crate::algorithm::{Algorithm, SignatureAlgorithm, TlsVersion};
3use crate::capability::Approval;
4use crate::peer_certificate::PeerCertificateViolation;
5use crate::profile::{Options, Profile};
6use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Family {
12 Version,
14 CipherSuite,
16 KxGroup,
18 SignatureScheme,
20 KeyAlgorithm,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum CsrError {
28 InvalidName,
30 InvalidParams,
32 Signing,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum PeerVerifierIdentity {
39 Owned {
41 rule: Option<crate::PeerCertificateRuleId>,
43 handshake_schemes: u16,
45 },
46 Opaque,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52#[non_exhaustive]
53pub enum TlsError {
54 ProtocolVersions,
56 PrivateKey,
58 DefaultProviderInstalled,
60 KeyMismatch,
62 StrictBuilderRequired {
64 entry_point: &'static str,
66 },
67 IncompatiblePeerVerifier {
69 expected: PeerVerifierIdentity,
71 actual: PeerVerifierIdentity,
73 },
74 PeerCertificate {
76 profile: Profile,
78 violation: PeerCertificateViolation,
80 },
81}
82
83impl fmt::Display for TlsError {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 Self::ProtocolVersions => f.write_str("the provider refused the protocol versions"),
87 Self::PrivateKey => f.write_str("the private key was refused"),
88 Self::DefaultProviderInstalled => {
89 f.write_str("a process-default provider is already installed")
90 }
91 Self::KeyMismatch => {
92 f.write_str("the certificate chain is empty or does not carry the key")
93 }
94 Self::IncompatiblePeerVerifier { expected, actual } => {
95 write!(
96 f,
97 "the peer verifier {actual:?} is incompatible with {expected:?}"
98 )
99 }
100 Self::StrictBuilderRequired { entry_point } => {
101 write!(f, "the peer certificate rule requires tls::{entry_point}")
102 }
103 Self::PeerCertificate { profile, violation } => {
104 write!(f, "the {profile} peer certificate was refused: {violation}")
105 }
106 }
107 }
108}
109
110impl std::error::Error for TlsError {
111 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
112 match self {
113 Self::PeerCertificate { violation, .. } => Some(violation),
114 Self::IncompatiblePeerVerifier { .. }
115 | Self::StrictBuilderRequired { .. }
116 | Self::ProtocolVersions
117 | Self::PrivateKey
118 | Self::DefaultProviderInstalled
119 | Self::KeyMismatch => None,
120 }
121 }
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126#[non_exhaustive]
127pub enum CryptoError {
128 Unavailable(Algorithm),
130 PolicyEmpty(Family),
132 PolicyDuplicate(Algorithm),
134 PolicyDrift,
136 PolicyVersionUnusable {
138 version: TlsVersion,
140 family: Family,
142 },
143 NotFips,
145 KeyGeneration(Algorithm),
147 PrivateKey,
149 Verify,
151 Sign,
153 Csr(CsrError),
155 Tls(TlsError),
157 Random,
159 AeadKey,
161 AeadSeal,
163 AeadOpen,
165 SealPlaintext,
167 SealBlob,
169 SealVersion,
171 SealEphemeralKey,
173 SealPublicKey,
175 SealPrivateKey,
177 SealOpen,
179 Primitive,
181 EmptyProfile {
183 profile: Profile,
185 options: Options,
187 category: Family,
189 },
190 KeyNotInProfile {
192 profile: Profile,
194 algorithm: SignatureAlgorithm,
196 },
197 NotApproved {
199 algorithm: Algorithm,
201 approval: Approval,
203 },
204}
205
206impl fmt::Display for CryptoError {
207 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208 match self {
209 CryptoError::Unavailable(a) => {
210 write!(f, "{a:?} is not available on this backend's module")
211 }
212 CryptoError::PolicyEmpty(fam) => write!(f, "the TLS policy names no {fam:?}"),
213 CryptoError::PolicyDuplicate(a) => write!(f, "the TLS policy names {a:?} twice"),
214 CryptoError::PolicyVersionUnusable { version, family } => write!(
215 f,
216 "the TLS policy lists {version:?} but names no {family:?} usable with it"
217 ),
218 CryptoError::PolicyDrift => {
219 f.write_str("the provider does not carry exactly the policy's algorithms")
220 }
221 CryptoError::NotFips => {
222 f.write_str("the AWS-LC FIPS module is not active, or the provider is not FIPS")
223 }
224 CryptoError::KeyGeneration(a) => write!(f, "key generation for {a:?} failed"),
225 CryptoError::PrivateKey => f.write_str("the private key was refused"),
226 CryptoError::Verify => f.write_str("the signature does not verify"),
227 CryptoError::Sign => f.write_str("signing failed"),
228 CryptoError::Csr(e) => write!(f, "the certificate signing request failed: {e:?}"),
229 CryptoError::Tls(e) => write!(f, "the TLS configuration was refused: {e:?}"),
230 CryptoError::Random => f.write_str("the random generator failed"),
231 CryptoError::AeadKey => f.write_str("the AEAD key was refused"),
232 CryptoError::AeadSeal => f.write_str("AEAD sealing failed"),
233 CryptoError::AeadOpen => f.write_str("the AEAD ciphertext does not authenticate"),
234 CryptoError::SealPlaintext => f.write_str("the sealed plaintext is empty"),
235 CryptoError::SealBlob => f.write_str("the sealed blob is too short"),
236 CryptoError::SealVersion => f.write_str("the sealed blob has an unknown version"),
237 CryptoError::SealEphemeralKey => {
238 f.write_str("the sealed blob's ephemeral key is not an uncompressed P-384 point")
239 }
240 CryptoError::SealPublicKey => {
241 f.write_str("the sealing public key is not a canonical P-384 SubjectPublicKeyInfo")
242 }
243 CryptoError::SealPrivateKey => {
244 f.write_str("the sealing private key is not a P-384 PKCS#8 key")
245 }
246 CryptoError::SealOpen => {
247 f.write_str("the sealed blob does not open under this key, domain and AAD")
248 }
249 CryptoError::Primitive => f.write_str("a cryptographic primitive failed"),
250 CryptoError::EmptyProfile {
251 profile,
252 options,
253 category,
254 } => {
255 write!(
256 f,
257 "the {profile} profile, with {options}, names no {category:?}"
258 )
259 }
260 CryptoError::KeyNotInProfile { profile, algorithm } => {
261 write!(f, "the {profile} profile does not admit {algorithm} keys")
262 }
263 CryptoError::NotApproved {
264 algorithm,
265 approval,
266 } => {
267 write!(
268 f,
269 "{algorithm:?} is not approved by the running module: {approval:?}"
270 )
271 }
272 }
273 }
274}
275
276impl std::error::Error for CryptoError {}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281 use crate::algorithm::{Algorithm, KxGroup, SignatureAlgorithm, TlsSignatureScheme};
282 use crate::capability::Approval;
283 use crate::profile::{Options, Profile};
284
285 #[test]
286 fn display_texts_are_pinned() {
287 assert_eq!(
288 CryptoError::Random.to_string(),
289 "the random generator failed"
290 );
291 assert_eq!(
292 CryptoError::NotFips.to_string(),
293 "the AWS-LC FIPS module is not active, or the provider is not FIPS"
294 );
295 assert_eq!(
296 CryptoError::Unavailable(Algorithm::Signature(SignatureAlgorithm::MlDsa87)).to_string(),
297 "Signature(MlDsa87) is not available on this backend's module"
298 );
299 assert_eq!(
300 CryptoError::PolicyEmpty(Family::KxGroup).to_string(),
301 "the TLS policy names no KxGroup"
302 );
303 assert_eq!(
304 CryptoError::PolicyVersionUnusable {
305 version: TlsVersion::Tls12,
306 family: Family::KxGroup
307 }
308 .to_string(),
309 "the TLS policy lists Tls12 but names no KxGroup usable with it"
310 );
311 assert_eq!(
312 CryptoError::PolicyDuplicate(Algorithm::Scheme(TlsSignatureScheme::Ed25519))
313 .to_string(),
314 "the TLS policy names Scheme(Ed25519) twice"
315 );
316 assert_eq!(
317 CryptoError::Csr(CsrError::InvalidName).to_string(),
318 "the certificate signing request failed: InvalidName"
319 );
320 assert_eq!(
321 CryptoError::Tls(TlsError::PrivateKey).to_string(),
322 "the TLS configuration was refused: PrivateKey"
323 );
324 }
325
326 #[test]
327 fn profile_display_texts_are_pinned() {
328 let pq = Options {
329 pq_key_exchange_required: true,
330 peer_certificates_match_profile: false,
331 };
332 let table: [(CryptoError, &str); 5] = [
333 (
334 CryptoError::EmptyProfile {
335 profile: Profile::Cnsa1,
336 options: pq,
337 category: Family::KxGroup,
338 },
339 "the cnsa1 profile, with post-quantum key exchange required, names no KxGroup",
340 ),
341 (
342 CryptoError::EmptyProfile {
343 profile: Profile::Cnsa2,
344 options: Options::default(),
345 category: Family::KeyAlgorithm,
346 },
347 "the cnsa2 profile, with no options, names no KeyAlgorithm",
348 ),
349 (
350 CryptoError::KeyNotInProfile {
351 profile: Profile::Bsi,
352 algorithm: SignatureAlgorithm::Rsa3072Sha384,
353 },
354 "the bsi profile does not admit rsa3072-sha384 keys",
355 ),
356 (
357 CryptoError::NotApproved {
358 algorithm: Algorithm::Kx(KxGroup::X25519),
359 approval: Approval::Unverified,
360 },
361 "Kx(X25519) is not approved by the running module: Unverified",
362 ),
363 (
364 CryptoError::NotApproved {
365 algorithm: Algorithm::Signature(SignatureAlgorithm::MlDsa87),
366 approval: Approval::AlgorithmTested { cavp: "A7855 (x)" },
367 },
368 "Signature(MlDsa87) is not approved by the running module: AlgorithmTested { cavp: \"A7855 (x)\" }",
369 ),
370 ];
371 for (e, text) in table {
372 assert_eq!(e.to_string(), text);
373 }
374 }
375
376 #[test]
377 fn every_other_display_text_is_pinned() {
378 let table: [(CryptoError, &str); 16] = [
379 (
380 CryptoError::PolicyDrift,
381 "the provider does not carry exactly the policy's algorithms",
382 ),
383 (
384 CryptoError::KeyGeneration(Algorithm::Signature(SignatureAlgorithm::Ed25519)),
385 "key generation for Signature(Ed25519) failed",
386 ),
387 (CryptoError::PrivateKey, "the private key was refused"),
388 (CryptoError::Verify, "the signature does not verify"),
389 (CryptoError::Sign, "signing failed"),
390 (CryptoError::AeadKey, "the AEAD key was refused"),
391 (CryptoError::AeadSeal, "AEAD sealing failed"),
392 (
393 CryptoError::AeadOpen,
394 "the AEAD ciphertext does not authenticate",
395 ),
396 (CryptoError::SealPlaintext, "the sealed plaintext is empty"),
397 (CryptoError::SealBlob, "the sealed blob is too short"),
398 (
399 CryptoError::SealVersion,
400 "the sealed blob has an unknown version",
401 ),
402 (
403 CryptoError::SealEphemeralKey,
404 "the sealed blob's ephemeral key is not an uncompressed P-384 point",
405 ),
406 (
407 CryptoError::SealPublicKey,
408 "the sealing public key is not a canonical P-384 SubjectPublicKeyInfo",
409 ),
410 (
411 CryptoError::SealPrivateKey,
412 "the sealing private key is not a P-384 PKCS#8 key",
413 ),
414 (
415 CryptoError::SealOpen,
416 "the sealed blob does not open under this key, domain and AAD",
417 ),
418 (CryptoError::Primitive, "a cryptographic primitive failed"),
419 ];
420 for (e, text) in table {
421 assert_eq!(e.to_string(), text);
422 }
423 }
424}