Skip to main content

rustcrypt_jasypt/
error.rs

1//! Error type shared by every encryptor and loader in the crate.
2
3/// Convenience alias used throughout the crate.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// All errors produced by `rustcrypt_jasypt`.
7///
8/// The enum is `#[non_exhaustive]`: new variants may be added in minor
9/// releases, so always keep a wildcard arm when matching on it.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// The password passed to a constructor was empty.
14    #[error("password cannot be empty")]
15    EmptyPassword,
16
17    /// The plaintext or ciphertext passed to encrypt/decrypt was empty.
18    #[error("value cannot be empty")]
19    EmptyValue,
20
21    /// A value was expected to look like `ENC(...)` but did not.
22    #[error("invalid encrypted format, expected ENC(...)")]
23    InvalidEncFormat,
24
25    /// The decoded Jasypt payload is too short or not block-aligned.
26    #[error("invalid jasypt encrypted data")]
27    InvalidJasyptData,
28
29    /// Authentication or padding check failed (wrong password or tampered data).
30    ///
31    /// Details are intentionally not exposed so that callers cannot use this
32    /// library as a padding/authentication oracle.
33    #[error("decryption failed")]
34    DecryptionFailed,
35
36    /// The value was not valid standard (padded) base64.
37    #[error("base64 decode error: {0}")]
38    Base64(#[from] base64::DecodeError),
39
40    /// I/O error while reading or writing a configuration file.
41    #[error("io error: {0}")]
42    Io(#[from] std::io::Error),
43
44    /// JSON parse or conversion error (feature `config`).
45    #[cfg(feature = "config")]
46    #[error("json error: {0}")]
47    Json(#[from] serde_json::Error),
48
49    /// A specific key in a map or config file could not be decrypted.
50    #[error("failed to decrypt key {key}: {source}")]
51    KeyDecrypt {
52        /// The key whose value failed to decrypt.
53        key: String,
54        /// The underlying decryption error.
55        #[source]
56        source: Box<Error>,
57    },
58
59    /// A builder parameter was out of range (for example a key size that is
60    /// not 16, 24 or 32 bytes).
61    #[error("invalid parameter: {0}")]
62    InvalidParameter(String),
63}
64
65impl Error {
66    /// Wraps `source` as a [`Error::KeyDecrypt`] for `key`.
67    pub(crate) fn key_decrypt(key: impl Into<String>, source: Error) -> Self {
68        Error::KeyDecrypt {
69            key: key.into(),
70            source: Box::new(source),
71        }
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn display_messages_match_gocrypt() {
81        assert_eq!(Error::EmptyPassword.to_string(), "password cannot be empty");
82        assert_eq!(Error::EmptyValue.to_string(), "value cannot be empty");
83        assert_eq!(
84            Error::InvalidEncFormat.to_string(),
85            "invalid encrypted format, expected ENC(...)"
86        );
87        assert_eq!(
88            Error::InvalidJasyptData.to_string(),
89            "invalid jasypt encrypted data"
90        );
91        assert_eq!(Error::DecryptionFailed.to_string(), "decryption failed");
92        assert_eq!(
93            Error::InvalidParameter("key size must be 16, 24 or 32".into()).to_string(),
94            "invalid parameter: key size must be 16, 24 or 32"
95        );
96    }
97
98    #[test]
99    fn key_decrypt_wraps_source() {
100        let err = Error::key_decrypt("DB_PASS", Error::DecryptionFailed);
101        assert_eq!(
102            err.to_string(),
103            "failed to decrypt key DB_PASS: decryption failed"
104        );
105        let source = std::error::Error::source(&err).expect("source");
106        assert_eq!(source.to_string(), "decryption failed");
107    }
108
109    #[test]
110    fn base64_error_converts() {
111        use base64::Engine;
112        let err: Error = base64::engine::general_purpose::STANDARD
113            .decode("not base64!")
114            .unwrap_err()
115            .into();
116        assert!(matches!(err, Error::Base64(_)));
117        assert!(err.to_string().starts_with("base64 decode error: "));
118    }
119}