aws_nitro_enclaves_cose/
error.rs

1//! COSE Operation errors and causes
2
3use std::error::Error;
4use std::fmt;
5
6use serde_cbor::Error as CborError;
7
8#[cfg(feature = "key_kms")]
9use aws_sdk_kms::{
10    error::SdkError, operation::get_public_key::GetPublicKeyError, operation::sign::SignError,
11    operation::verify::VerifyError,
12};
13
14#[derive(Debug)]
15/// Aggregation of all error types returned by this library
16pub enum CoseError {
17    /// Failed to generate random bytes
18    EntropyError(Box<dyn Error>),
19    /// Computation of a cryptographic hash failed
20    HashingError(Box<dyn Error>),
21    /// Signature could not be performed due to OpenSSL error.
22    SignatureError(Box<dyn Error>),
23    /// This feature is not yet fully implemented according
24    /// to the spec.
25    UnimplementedError,
26    /// This specific configuration is not supported, although
27    /// the spec allows it.
28    UnsupportedError(String),
29    /// Could not verify signature.
30    UnverifiedSignature,
31    /// Deserialized structure does not respect the COSE specification.
32    SpecificationError(String),
33    /// Error while serializing or deserializing structures.
34    SerializationError(CborError),
35    /// Tag is missing or incorrect.
36    TagError(Option<u64>),
37    /// Encryption could not be performed due to OpenSSL error.
38    EncryptionError(Box<dyn Error>),
39    /// TPM error occured
40    #[cfg(feature = "key_tpm")]
41    TpmError(tss_esapi::Error),
42    /// AWS sign error occured
43    #[cfg(feature = "key_kms")]
44    AwsSignError(SdkError<SignError>),
45    /// AWS verify error occured
46    #[cfg(feature = "key_kms")]
47    AwsVerifyError(SdkError<VerifyError>),
48    /// AWS GetPublicKey error occured
49    #[cfg(all(feature = "key_kms", feature = "key_openssl_pkey"))]
50    AwsGetPublicKeyError(SdkError<GetPublicKeyError>),
51}
52
53impl fmt::Display for CoseError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            CoseError::EntropyError(e) => write!(f, "Entropy error: {}", e),
57            CoseError::HashingError(e) => write!(f, "Hashing failed: {}", e),
58            CoseError::SignatureError(e) => write!(f, "Signature error: {}", e),
59            CoseError::UnimplementedError => write!(f, "Not implemented"),
60            CoseError::UnsupportedError(e) => write!(f, "Not supported: {}", e),
61            CoseError::UnverifiedSignature => write!(f, "Unverified signature"),
62            CoseError::SpecificationError(e) => write!(f, "Specification error: {}", e),
63            CoseError::SerializationError(e) => write!(f, "Serialization error: {}", e),
64            CoseError::TagError(Some(tag)) => write!(f, "Tag {} was not expected", tag),
65            CoseError::TagError(None) => write!(f, "Expected tag is missing"),
66            CoseError::EncryptionError(e) => write!(f, "Encryption error: {}", e),
67            #[cfg(feature = "key_tpm")]
68            CoseError::TpmError(e) => write!(f, "TPM error: {}", e),
69            #[cfg(feature = "key_kms")]
70            CoseError::AwsSignError(e) => write!(f, "AWS sign error: {}", e),
71            #[cfg(feature = "key_kms")]
72            CoseError::AwsVerifyError(e) => write!(f, "AWS verify error: {}", e),
73            #[cfg(all(feature = "key_kms", feature = "key_openssl_pkey"))]
74            CoseError::AwsGetPublicKeyError(e) => write!(f, "AWS GetPublicKey error: {}", e),
75        }
76    }
77}
78
79impl Error for CoseError {
80    fn source(&self) -> Option<&(dyn Error + 'static)> {
81        match self {
82            CoseError::SignatureError(e) => e.source(),
83            CoseError::SerializationError(e) => Some(e),
84            _ => None,
85        }
86    }
87}