bitwarden_crypto/
error.rs

1use std::fmt::Debug;
2
3use bitwarden_error::bitwarden_error;
4use thiserror::Error;
5use uuid::Uuid;
6
7use crate::fingerprint::FingerprintError;
8
9#[bitwarden_error(flat)]
10#[derive(Debug, Error)]
11pub enum CryptoError {
12    #[error("The provided key is not the expected type")]
13    InvalidKey,
14    #[error("The cipher's MAC doesn't match the expected value")]
15    InvalidMac,
16    #[error("The key provided expects mac protected encstrings, but the mac is missing")]
17    MacNotProvided,
18    #[error("Error while decrypting EncString")]
19    KeyDecrypt,
20    #[error("The cipher key has an invalid length")]
21    InvalidKeyLen,
22    #[error("The value is not a valid UTF8 String")]
23    InvalidUtf8String,
24    #[error("Missing Key for organization with ID {0}")]
25    MissingKey(Uuid),
26    #[error("The item was missing a required field: {0}")]
27    MissingField(&'static str),
28    #[error("Missing Key for Id: {0}")]
29    MissingKeyId(String),
30    #[error("Crypto store is read-only")]
31    ReadOnlyKeyStore,
32
33    #[error("Insufficient KDF parameters")]
34    InsufficientKdfParameters,
35
36    #[error("EncString error, {0}")]
37    EncString(#[from] EncStringParseError),
38
39    #[error("Rsa error, {0}")]
40    RsaError(#[from] RsaError),
41
42    #[error("Fingerprint error, {0}")]
43    FingerprintError(#[from] FingerprintError),
44
45    #[error("Argon2 error, {0}")]
46    ArgonError(#[from] argon2::Error),
47
48    #[error("Number is zero")]
49    ZeroNumber,
50
51    #[error("Unsupported operation, {0}")]
52    OperationNotSupported(UnsupportedOperation),
53
54    #[error("Key algorithm does not match encrypted data type")]
55    WrongKeyType,
56
57    #[error("Invalid nonce length")]
58    InvalidNonceLength,
59}
60
61#[derive(Debug, Error)]
62pub enum UnsupportedOperation {
63    #[error("Encryption is not implemented for key")]
64    EncryptionNotImplementedForKey,
65}
66
67#[derive(Debug, Error)]
68pub enum EncStringParseError {
69    #[error("No type detected, missing '.' separator")]
70    NoType,
71    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
72    InvalidTypeSymm { enc_type: String, parts: usize },
73    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
74    InvalidTypeAsymm { enc_type: String, parts: usize },
75    #[error("Error decoding base64: {0}")]
76    InvalidBase64(#[from] base64::DecodeError),
77    #[error("Invalid length: expected {expected}, got {got}")]
78    InvalidLength { expected: usize, got: usize },
79    #[error("Invalid encoding {0}")]
80    InvalidCoseEncoding(coset::CoseError),
81    #[error("Algorithm missing in COSE header")]
82    CoseMissingAlgorithm,
83}
84
85#[derive(Debug, Error)]
86pub enum RsaError {
87    #[error("Unable to create public key")]
88    CreatePublicKey,
89    #[error("Unable to create private key")]
90    CreatePrivateKey,
91    #[error("Rsa error, {0}")]
92    Rsa(#[from] rsa::Error),
93}
94
95/// Alias for `Result<T, CryptoError>`.
96pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;