use std::error::Error;
use std::fmt;
use serde_cbor::Error as CborError;
#[cfg(feature = "key_kms")]
use aws_sdk_kms::{
error::SdkError, operation::get_public_key::GetPublicKeyError, operation::sign::SignError,
operation::verify::VerifyError,
};
#[derive(Debug)]
pub enum CoseError {
EntropyError(Box<dyn Error>),
HashingError(Box<dyn Error>),
SignatureError(Box<dyn Error>),
UnimplementedError,
UnsupportedError(String),
UnverifiedSignature,
SpecificationError(String),
SerializationError(CborError),
TagError(Option<u64>),
EncryptionError(Box<dyn Error>),
#[cfg(feature = "key_tpm")]
TpmError(tss_esapi::Error),
#[cfg(feature = "key_kms")]
AwsSignError(SdkError<SignError>),
#[cfg(feature = "key_kms")]
AwsVerifyError(SdkError<VerifyError>),
#[cfg(all(feature = "key_kms", feature = "key_openssl_pkey"))]
AwsGetPublicKeyError(SdkError<GetPublicKeyError>),
}
impl fmt::Display for CoseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CoseError::EntropyError(e) => write!(f, "Entropy error: {}", e),
CoseError::HashingError(e) => write!(f, "Hashing failed: {}", e),
CoseError::SignatureError(e) => write!(f, "Signature error: {}", e),
CoseError::UnimplementedError => write!(f, "Not implemented"),
CoseError::UnsupportedError(e) => write!(f, "Not supported: {}", e),
CoseError::UnverifiedSignature => write!(f, "Unverified signature"),
CoseError::SpecificationError(e) => write!(f, "Specification error: {}", e),
CoseError::SerializationError(e) => write!(f, "Serialization error: {}", e),
CoseError::TagError(Some(tag)) => write!(f, "Tag {} was not expected", tag),
CoseError::TagError(None) => write!(f, "Expected tag is missing"),
CoseError::EncryptionError(e) => write!(f, "Encryption error: {}", e),
#[cfg(feature = "key_tpm")]
CoseError::TpmError(e) => write!(f, "TPM error: {}", e),
#[cfg(feature = "key_kms")]
CoseError::AwsSignError(e) => write!(f, "AWS sign error: {}", e),
#[cfg(feature = "key_kms")]
CoseError::AwsVerifyError(e) => write!(f, "AWS verify error: {}", e),
#[cfg(all(feature = "key_kms", feature = "key_openssl_pkey"))]
CoseError::AwsGetPublicKeyError(e) => write!(f, "AWS GetPublicKey error: {}", e),
}
}
}
impl Error for CoseError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CoseError::SignatureError(e) => e.source(),
CoseError::SerializationError(e) => Some(e),
_ => None,
}
}
}