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("Rsa error, {0}")]
48    Rsa(#[from] RsaError),
49
50    #[error("Fingerprint error, {0}")]
51    Fingerprint(#[from] FingerprintError),
52
53    #[error("Argon2 error, {0}")]
54    Argon(#[from] argon2::Error),
55
56    #[error("Number is zero")]
57    ZeroNumber,
58
59    #[error("Unsupported operation, {0}")]
60    OperationNotSupported(UnsupportedOperationError),
61
62    #[error("Key algorithm does not match encrypted data type")]
63    WrongKeyType,
64
65    #[error("Key ID in the COSE Encrypt0 message does not match the key ID in the key")]
66    WrongCoseKeyId,
67
68    #[error("Invalid nonce length")]
69    InvalidNonceLength,
70
71    #[error("Invalid padding")]
72    InvalidPadding,
73
74    #[error("Signature error, {0}")]
75    Signature(#[from] SignatureError),
76
77    #[error("Encoding error, {0}")]
78    Encoding(#[from] EncodingError),
79}
80
81#[derive(Debug, Error)]
82pub enum UnsupportedOperationError {
83    #[error("Encryption is not implemented for key")]
84    EncryptionNotImplementedForKey,
85    #[error("Decryption is not implemented for key")]
86    DecryptionNotImplementedForKey,
87}
88
89#[derive(Debug, Error)]
90pub enum EncStringParseError {
91    #[error("No type detected, missing '.' separator")]
92    NoType,
93    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
94    InvalidTypeSymm { enc_type: String, parts: usize },
95    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
96    InvalidTypeAsymm { enc_type: String, parts: usize },
97    #[error("Error decoding base64: {0}")]
98    InvalidBase64(#[from] NotB64EncodedError),
99    #[error("Invalid length: expected {expected}, got {got}")]
100    InvalidLength { expected: usize, got: usize },
101    #[error("Invalid encoding {0}")]
102    InvalidCoseEncoding(coset::CoseError),
103    #[error("Algorithm missing in COSE header")]
104    CoseMissingAlgorithm,
105    #[error("Content type missing in COSE header")]
106    CoseMissingContentType,
107}
108
109#[derive(Debug, Error)]
110pub enum RsaError {
111    #[error("Unable to create public key")]
112    CreatePublicKey,
113    #[error("Unable to create private key")]
114    CreatePrivateKey,
115    #[error("Rsa error, {0}")]
116    Rsa(#[from] rsa::Error),
117}
118
119#[derive(Debug, Error)]
120pub enum SignatureError {
121    #[error("Invalid signature")]
122    InvalidSignature,
123    #[error("Invalid namespace")]
124    InvalidNamespace,
125}
126
127/// Error type issues en- or de-coding values
128#[derive(Debug, Error)]
129pub enum EncodingError {
130    /// An error occurred while serializing or deserializing a value using COSE
131    #[error("Invalid cose encoding")]
132    InvalidCoseEncoding,
133    /// An error occurred while serializing or deserializing a value using CBOR
134    #[error("Cbor serialization error")]
135    InvalidCborSerialization,
136    /// An error occurred while serializing or deserializing a value using Base64
137    #[error("Invalid base64 encoding")]
138    InvalidBase64Encoding,
139    /// A required value is missing from the serialized message
140    #[error("Missing value {0}")]
141    MissingValue(&'static str),
142    /// A value is invalid / outside the expected range
143    #[error("Invalid value {0}")]
144    InvalidValue(&'static str),
145    /// A value is unsupported but may be valid
146    #[error("Unsupported value {0}")]
147    UnsupportedValue(&'static str),
148}
149
150/// Alias for `Result<T, CryptoError>`.
151pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;