1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! CryptoError covers all errors not covered by StorageError. It is returned by
//! every function in this crate returning a Result except those used in the
//! `Storer` trait.

use base64::DecodeError;
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
    io,
};

/// Error that wraps all possible errors out of the redact-crypto crate
#[derive(Debug)]
pub enum CryptoError {
    /// Represents an error which occurred in some internal system
    InternalError {
        source: Box<dyn Error + Send + Sync>,
    },

    /// Error occurred while performing IO on the filesystem
    FsIoError { source: io::Error },

    /// File path given was not found
    FileNotFound { path: String },

    /// The requested resource was not found
    NotFound,

    /// Ciphertext failed veri fication before decryption
    CiphertextFailedVerification,

    /// Provided bytes are not the right length for the
    InvalidKeyLength { expected: usize, actual: usize },

    /// Given value was not of the right type to be downcasted to the requested type
    NotDowncastable,

    /// File path given has an invalid file name with no stem
    FilePathHasNoFileStem { path: String },

    /// File path given was invalid UTF-8
    FilePathIsInvalidUTF8,

    /// Given bytes could not be serialized to a base data type
    NotDeserializableToBaseDataType,

    /// Error happened when decoding base64 string
    Base64Decode { source: DecodeError },

    /// Wrong nonce was provided during seal/unseal operation
    WrongNonceType,
}

impl Error for CryptoError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            CryptoError::InternalError { ref source } => Some(source.as_ref()),
            CryptoError::FsIoError { ref source } => Some(source),
            CryptoError::FileNotFound { .. } => None,
            CryptoError::NotFound => None,
            CryptoError::CiphertextFailedVerification => None,
            CryptoError::InvalidKeyLength { .. } => None,
            CryptoError::NotDowncastable => None,
            CryptoError::FilePathHasNoFileStem { .. } => None,
            CryptoError::FilePathIsInvalidUTF8 => None,
            CryptoError::NotDeserializableToBaseDataType => None,
            CryptoError::Base64Decode { ref source } => Some(source),
            CryptoError::WrongNonceType => None,
        }
    }
}

impl Display for CryptoError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            CryptoError::InternalError { .. } => {
                write!(f, "Internal error occurred")
            }
            CryptoError::FsIoError { .. } => {
                write!(f, "Error occured during file system IO")
            }
            CryptoError::FileNotFound { ref path } => {
                write!(f, "Path \"{}\" not found", path)
            }
            CryptoError::NotFound => {
                write!(f, "Requested resource not found")
            }
            CryptoError::CiphertextFailedVerification => {
                write!(f, "Ciphertext failed verification before decryption")
            }
            CryptoError::InvalidKeyLength {
                ref expected,
                ref actual,
            } => {
                write!(
                    f,
                    "Provided key was not the correct length, expected: {}, actual: {}",
                    expected, actual,
                )
            }
            CryptoError::NotDowncastable => {
                write!(
                    f,
                    "Could not downcast the Types-value into the requested variant"
                )
            }
            CryptoError::FilePathHasNoFileStem { ref path } => {
                write!(
                    f,
                    "File path \"{}\" was invalid as the file name has no stem",
                    path
                )
            }
            CryptoError::FilePathIsInvalidUTF8 => {
                write!(f, "Given file path was not valid UTF-8")
            }
            CryptoError::NotDeserializableToBaseDataType => {
                write!(f, "Given bytes could not be deserialized to one of: bool, u64, i64, f64, or string")
            }
            CryptoError::Base64Decode { .. } => {
                write!(f, "Error occurred while decoding string from base64")
            }
            CryptoError::WrongNonceType => {
                write!(f, "Invalid type of nonce was provided for the operation")
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::CryptoError;

    #[test]
    fn test_to_string_internal_error() {
        let s = CryptoError::InternalError {
            source: Box::new(CryptoError::NotFound),
        }
        .to_string();
        assert_eq!(s, "Internal error occurred");
    }

    #[test]
    fn test_to_string_not_found() {
        let s = CryptoError::NotFound.to_string();
        assert_eq!(s, "Requested resource not found");
    }
}