Skip to main content

bitwarden_crypto/
error.rs

1use std::fmt::Debug;
2
3use bitwarden_encoding::NotB64EncodedError;
4use bitwarden_error::bitwarden_error;
5use coset::iana::KeyOperation;
6use thiserror::Error;
7use uuid::Uuid;
8
9use crate::fingerprint::FingerprintError;
10
11#[allow(missing_docs)]
12#[bitwarden_error(flat)]
13#[derive(Debug, Error)]
14pub enum CryptoError {
15    #[error("The decryption operation failed")]
16    Decrypt,
17    #[error("The provided key is not the expected type")]
18    InvalidKey,
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("Key operation not supported by key: {0:?}")]
32    KeyOperationNotSupported(KeyOperation),
33
34    // Note: These variants will be moved into their own key store error in a follow up ticket,
35    // since the crypto error is growing too large
36    #[error("Crypto store is read-only")]
37    ReadOnlyKeyStore,
38    #[error("Invalid key store operation")]
39    InvalidKeyStoreOperation,
40
41    #[error("Insufficient KDF parameters")]
42    InsufficientKdfParameters,
43
44    #[error("EncString error, {0}")]
45    EncString(#[from] EncStringParseError),
46
47    #[error("The encrypted string is malformed and cannot be decrypted")]
48    UnparseableEncString,
49
50    #[error("Rsa error, {0}")]
51    Rsa(#[from] RsaError),
52
53    #[error("Fingerprint error, {0}")]
54    Fingerprint(#[from] FingerprintError),
55
56    #[error("Argon2 error, {0}")]
57    Argon(#[from] argon2::Error),
58
59    #[error("Number is zero")]
60    ZeroNumber,
61
62    #[error("Unsupported operation, {0}")]
63    OperationNotSupported(UnsupportedOperationError),
64
65    #[error("Key algorithm does not match encrypted data type")]
66    WrongKeyType,
67
68    #[error("Key ID in the COSE Encrypt0 message does not match the key ID in the key")]
69    WrongCoseKeyId,
70
71    #[error("Invalid nonce length")]
72    InvalidNonceLength,
73
74    #[error("Invalid padding")]
75    InvalidPadding,
76
77    #[error("Signature error, {0}")]
78    Signature(#[from] SignatureError),
79
80    #[error("Encoding error, {0}")]
81    Encoding(#[from] EncodingError),
82}
83
84#[derive(Debug, Error)]
85pub enum UnsupportedOperationError {
86    #[error("Encryption is not implemented for key")]
87    EncryptionNotImplementedForKey,
88    #[error("Decryption is not implemented for key")]
89    DecryptionNotImplementedForKey,
90}
91
92#[derive(Debug, Error)]
93pub enum EncStringParseError {
94    #[error("No type detected, missing '.' separator")]
95    NoType,
96    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
97    InvalidTypeSymm { enc_type: String, parts: usize },
98    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
99    InvalidTypeAsymm { enc_type: String, parts: usize },
100    #[error("Error decoding base64: {0}")]
101    InvalidBase64(#[from] NotB64EncodedError),
102    #[error("Invalid length: expected {expected}, got {got}")]
103    InvalidLength { expected: usize, got: usize },
104    #[error("Invalid encoding {0}")]
105    InvalidCoseEncoding(coset::CoseError),
106    #[error("Algorithm missing in COSE header")]
107    CoseMissingAlgorithm,
108    #[error("Content type missing in COSE header")]
109    CoseMissingContentType,
110}
111
112#[derive(Debug, Error)]
113pub enum RsaError {
114    #[error("Unable to create public key")]
115    CreatePublicKey,
116    #[error("Unable to create private key")]
117    CreatePrivateKey,
118    #[error("Rsa error, {0}")]
119    Rsa(#[from] rsa::Error),
120}
121
122#[derive(Debug, Error)]
123pub enum SignatureError {
124    #[error("Invalid signature")]
125    InvalidSignature,
126    #[error("Invalid namespace")]
127    InvalidNamespace,
128}
129
130/// Error type issues en- or de-coding values
131#[derive(Debug, Error)]
132pub enum EncodingError {
133    /// An error occurred while serializing or deserializing a value using COSE
134    #[error("Invalid cose encoding")]
135    InvalidCoseEncoding,
136    /// An error occurred while serializing or deserializing a value using CBOR
137    #[error("Cbor serialization error")]
138    InvalidCborSerialization,
139    /// An error occurred while serializing or deserializing a value using Base64
140    #[error("Invalid base64 encoding")]
141    InvalidBase64Encoding,
142    /// A required value is missing from the serialized message
143    #[error("Missing value {0}")]
144    MissingValue(&'static str),
145    /// A value is invalid / outside the expected range
146    #[error("Invalid value {0}")]
147    InvalidValue(&'static str),
148    /// A value is unsupported but may be valid
149    #[error("Unsupported value {0}")]
150    UnsupportedValue(&'static str),
151}
152
153/// Alias for `Result<T, CryptoError>`.
154pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;