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}
86
87#[derive(Debug, Error)]
88pub enum EncStringParseError {
89    #[error("No type detected, missing '.' separator")]
90    NoType,
91    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
92    InvalidTypeSymm { enc_type: String, parts: usize },
93    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
94    InvalidTypeAsymm { enc_type: String, parts: usize },
95    #[error("Error decoding base64: {0}")]
96    InvalidBase64(#[from] NotB64EncodedError),
97    #[error("Invalid length: expected {expected}, got {got}")]
98    InvalidLength { expected: usize, got: usize },
99    #[error("Invalid encoding {0}")]
100    InvalidCoseEncoding(coset::CoseError),
101    #[error("Algorithm missing in COSE header")]
102    CoseMissingAlgorithm,
103    #[error("Content type missing in COSE header")]
104    CoseMissingContentType,
105}
106
107#[derive(Debug, Error)]
108pub enum RsaError {
109    #[error("Unable to create public key")]
110    CreatePublicKey,
111    #[error("Unable to create private key")]
112    CreatePrivateKey,
113    #[error("Rsa error, {0}")]
114    Rsa(#[from] rsa::Error),
115}
116
117#[derive(Debug, Error)]
118pub enum SignatureError {
119    #[error("Invalid signature")]
120    InvalidSignature,
121    #[error("Invalid namespace")]
122    InvalidNamespace,
123}
124
125/// Error type issues en- or de-coding values
126#[derive(Debug, Error)]
127pub enum EncodingError {
128    /// An error occurred while serializing or deserializing a value using COSE
129    #[error("Invalid cose encoding")]
130    InvalidCoseEncoding,
131    /// An error occurred while serializing or deserializing a value using CBOR
132    #[error("Cbor serialization error")]
133    InvalidCborSerialization,
134    /// An error occurred while serializing or deserializing a value using Base64
135    #[error("Invalid base64 encoding")]
136    InvalidBase64Encoding,
137    /// A required value is missing from the serialized message
138    #[error("Missing value {0}")]
139    MissingValue(&'static str),
140    /// A value is invalid / outside the expected range
141    #[error("Invalid value {0}")]
142    InvalidValue(&'static str),
143    /// A value is unsupported but may be valid
144    #[error("Unsupported value {0}")]
145    UnsupportedValue(&'static str),
146}
147
148/// Alias for `Result<T, CryptoError>`.
149pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;