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#[allow(missing_docs)]
10#[bitwarden_error(flat)]
11#[derive(Debug, Error)]
12pub enum CryptoError {
13    #[error("The provided key is not the expected type")]
14    InvalidKey,
15    #[error("The cipher's MAC doesn't match the expected value")]
16    InvalidMac,
17    #[error("The key provided expects mac protected encstrings, but the mac is missing")]
18    MacNotProvided,
19    #[error("Error while decrypting EncString")]
20    KeyDecrypt,
21    #[error("The cipher key has an invalid length")]
22    InvalidKeyLen,
23    #[error("The value is not a valid UTF8 String")]
24    InvalidUtf8String,
25    #[error("Missing Key for organization with ID {0}")]
26    MissingKey(Uuid),
27    #[error("The item was missing a required field: {0}")]
28    MissingField(&'static str),
29    #[error("Missing Key for Id: {0}")]
30    MissingKeyId(String),
31    #[error("Crypto store is read-only")]
32    ReadOnlyKeyStore,
33
34    #[error("Insufficient KDF parameters")]
35    InsufficientKdfParameters,
36
37    #[error("EncString error, {0}")]
38    EncString(#[from] EncStringParseError),
39
40    #[error("Rsa error, {0}")]
41    RsaError(#[from] RsaError),
42
43    #[error("Fingerprint error, {0}")]
44    FingerprintError(#[from] FingerprintError),
45
46    #[error("Argon2 error, {0}")]
47    ArgonError(#[from] argon2::Error),
48
49    #[error("Number is zero")]
50    ZeroNumber,
51
52    #[error("Unsupported operation, {0}")]
53    OperationNotSupported(UnsupportedOperation),
54
55    #[error("Key algorithm does not match encrypted data type")]
56    WrongKeyType,
57
58    #[error("Key ID in the COSE Encrypt0 message does not match the key ID in the key")]
59    WrongCoseKeyId,
60
61    #[error("Invalid nonce length")]
62    InvalidNonceLength,
63
64    #[error("Signature error, {0}")]
65    SignatureError(#[from] SignatureError),
66
67    #[error("Encoding error, {0}")]
68    EncodingError(#[from] EncodingError),
69}
70
71#[derive(Debug, Error)]
72pub enum UnsupportedOperation {
73    #[error("Encryption is not implemented for key")]
74    EncryptionNotImplementedForKey,
75}
76
77#[derive(Debug, Error)]
78pub enum EncStringParseError {
79    #[error("No type detected, missing '.' separator")]
80    NoType,
81    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
82    InvalidTypeSymm { enc_type: String, parts: usize },
83    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
84    InvalidTypeAsymm { enc_type: String, parts: usize },
85    #[error("Error decoding base64: {0}")]
86    InvalidBase64(#[from] base64::DecodeError),
87    #[error("Invalid length: expected {expected}, got {got}")]
88    InvalidLength { expected: usize, got: usize },
89    #[error("Invalid encoding {0}")]
90    InvalidCoseEncoding(coset::CoseError),
91    #[error("Algorithm missing in COSE header")]
92    CoseMissingAlgorithm,
93}
94
95#[derive(Debug, Error)]
96pub enum RsaError {
97    #[error("Unable to create public key")]
98    CreatePublicKey,
99    #[error("Unable to create private key")]
100    CreatePrivateKey,
101    #[error("Rsa error, {0}")]
102    Rsa(#[from] rsa::Error),
103}
104
105#[derive(Debug, Error)]
106pub enum SignatureError {
107    #[error("Invalid signature")]
108    InvalidSignature,
109    #[error("Invalid namespace")]
110    InvalidNamespace,
111}
112
113#[derive(Debug, Error)]
114pub enum EncodingError {
115    #[error("Invalid cose encoding")]
116    InvalidCoseEncoding,
117    #[error("Cbor serialization error")]
118    InvalidCborSerialization,
119    #[error("Missing value {0}")]
120    MissingValue(&'static str),
121    #[error("Invalid value {0}")]
122    InvalidValue(&'static str),
123    #[error("Unsupported value {0}")]
124    UnsupportedValue(&'static str),
125}
126
127/// Alias for `Result<T, CryptoError>`.
128pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;