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 provided key is not the expected type")]
16    InvalidKey,
17    #[error("The cipher's MAC doesn't match the expected value")]
18    InvalidMac,
19    #[error("The key provided expects mac protected encstrings, but the mac is missing")]
20    MacNotProvided,
21    #[error("Error while decrypting EncString")]
22    KeyDecrypt,
23    #[error("The cipher key has an invalid length")]
24    InvalidKeyLen,
25    #[error("The value is not a valid UTF8 String")]
26    InvalidUtf8String,
27    #[error("Missing Key for organization with ID {0}")]
28    MissingKey(Uuid),
29    #[error("The item was missing a required field: {0}")]
30    MissingField(&'static str),
31    #[error("Missing Key for Id: {0}")]
32    MissingKeyId(String),
33    #[error("Key operation not supported by key: {0:?}")]
34    KeyOperationNotSupported(KeyOperation),
35
36    // Note: These variants will be moved into their own key store error in a follow up ticket,
37    // since the crypto error is growing too large
38    #[error("Crypto store is read-only")]
39    ReadOnlyKeyStore,
40    #[error("Invalid key store operation")]
41    InvalidKeyStoreOperation,
42
43    #[error("Insufficient KDF parameters")]
44    InsufficientKdfParameters,
45
46    #[error("EncString error, {0}")]
47    EncString(#[from] EncStringParseError),
48
49    #[error("Rsa error, {0}")]
50    Rsa(#[from] RsaError),
51
52    #[error("Fingerprint error, {0}")]
53    Fingerprint(#[from] FingerprintError),
54
55    #[error("Argon2 error, {0}")]
56    Argon(#[from] argon2::Error),
57
58    #[error("Number is zero")]
59    ZeroNumber,
60
61    #[error("Unsupported operation, {0}")]
62    OperationNotSupported(UnsupportedOperationError),
63
64    #[error("Key algorithm does not match encrypted data type")]
65    WrongKeyType,
66
67    #[error("Key ID in the COSE Encrypt0 message does not match the key ID in the key")]
68    WrongCoseKeyId,
69
70    #[error("Invalid nonce length")]
71    InvalidNonceLength,
72
73    #[error("Invalid padding")]
74    InvalidPadding,
75
76    #[error("Signature error, {0}")]
77    Signature(#[from] SignatureError),
78
79    #[error("Encoding error, {0}")]
80    Encoding(#[from] EncodingError),
81}
82
83#[derive(Debug, Error)]
84pub enum UnsupportedOperationError {
85    #[error("Encryption is not implemented for key")]
86    EncryptionNotImplementedForKey,
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>;