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