fog_crypto/
error.rs

1/// Possible cryptographic submodule error conditions.
2#[derive(Clone, PartialEq, Eq, Debug)]
3pub enum CryptoError {
4    /// Crypto primitive uses a version this library doesn't recognize (or one it no longer
5    /// accepts).
6    UnsupportedVersion(u8),
7    /// Crypto primitive uses a version that's too old, and deemed unsafe to use.
8    OldVersion(u8),
9    /// Crypto system was unable to decrypt the contents of a Lockbox.
10    DecryptFailed,
11    /// The provided data for wasn't the expected length.
12    BadLength {
13        step: &'static str,
14        actual: usize,
15        expected: usize,
16    },
17    /// A provided cryptographic key (public or private) is weak or invalid.
18    BadKey,
19    /// The data format doesn't match spec. Can occur when decoding a lockbox or attempting to
20    /// decode from a String.
21    BadFormat(&'static str),
22    /// An operation expected a specific object but didn't get it. Eg. Signature verification
23    /// expects a specific version of hash and fails if it doesn't get a hash with that version, or
24    /// a Lockbox should be unlocked with a specific key but we attempted to use the wrong one.
25    /// This almost always indicates a logic error.
26    ObjectMismatch(&'static str),
27    /// Verification of a signature failed.
28    SignatureFailed,
29    /// The attempted operation isn't supported by the backing Vault.
30    NotSupportedByVault,
31}
32
33impl CryptoError {
34    pub fn serde_err(&self) -> String {
35        match *self {
36            CryptoError::UnsupportedVersion(version) => {
37                format!("crypto version ({}) not supported.", version)
38            }
39            CryptoError::OldVersion(version) => {
40                format!("crypto version ({}) old and deemed unsafe", version)
41            }
42            CryptoError::DecryptFailed => "could not decrypt with key".to_string(),
43            CryptoError::BadLength {
44                step,
45                actual,
46                expected,
47            } => format!(
48                "expected data length {}, but got {} on step [{}]",
49                expected, actual, step
50            ),
51            CryptoError::BadKey => "crypto key is weak or invalid".to_string(),
52            CryptoError::BadFormat(s) => format!("format of data does not match spec: {}", s),
53            CryptoError::ObjectMismatch(s) => format!("object mismatch: {}", s),
54            CryptoError::SignatureFailed => "signature verification failed".to_string(),
55            CryptoError::NotSupportedByVault => "vault doesn't support this operation".to_string(),
56        }
57    }
58}
59
60use std::fmt;
61impl fmt::Display for CryptoError {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        match *self {
64            CryptoError::UnsupportedVersion(version) => {
65                write!(f, "Chosen crypto version ({}) not supported.", version)
66            }
67            CryptoError::OldVersion(version) => {
68                write!(f, "Crypto version ({}) is old and deemed unsafe.", version)
69            }
70            CryptoError::DecryptFailed => write!(f, "Could not decrypt with key"),
71            CryptoError::BadLength {
72                step,
73                actual,
74                expected,
75            } => write!(
76                f,
77                "Expected data length {}, but got {} on step [{}]",
78                expected, actual, step
79            ),
80            CryptoError::BadKey => write!(f, "Crypto key is weak or invalid"),
81            CryptoError::BadFormat(s) => write!(f, "Format of data does not match spec: {}", s),
82            CryptoError::ObjectMismatch(s) => write!(f, "Object mismatch: {}", s),
83            CryptoError::SignatureFailed => write!(f, "Signature verification failed"),
84            CryptoError::NotSupportedByVault => write!(f, "Vault doesn't support this operation"),
85        }
86    }
87}
88
89impl std::error::Error for CryptoError {}