bitwarden_crypto/
error.rs

1use std::fmt::Debug;
2
3use bitwarden_error::bitwarden_error;
4use thiserror::Error;
5use uuid::Uuid;
6
7use crate::fingerprint::FingerprintError;
8
9#[allow(missing_docs)]
10#[bitwarden_error(flat)]
11#[derive(Debug, Error)]
12pub enum CryptoError {
13    #[error("The provided key is not the expected type")]
14    InvalidKey,
15    #[error("The cipher's MAC doesn't match the expected value")]
16    InvalidMac,
17    #[error("The key provided expects mac protected encstrings, but the mac is missing")]
18    MacNotProvided,
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("Crypto store is read-only")]
32    ReadOnlyKeyStore,
33
34    #[error("Insufficient KDF parameters")]
35    InsufficientKdfParameters,
36
37    #[error("EncString error, {0}")]
38    EncString(#[from] EncStringParseError),
39
40    #[error("Rsa error, {0}")]
41    RsaError(#[from] RsaError),
42
43    #[error("Fingerprint error, {0}")]
44    FingerprintError(#[from] FingerprintError),
45
46    #[error("Argon2 error, {0}")]
47    ArgonError(#[from] argon2::Error),
48
49    #[error("Number is zero")]
50    ZeroNumber,
51
52    #[error("Unsupported operation, {0}")]
53    OperationNotSupported(UnsupportedOperation),
54
55    #[error("Key algorithm does not match encrypted data type")]
56    WrongKeyType,
57
58    #[error("Key ID in the COSE Encrypt0 message does not match the key ID in the key")]
59    WrongCoseKeyId,
60
61    #[error("Invalid nonce length")]
62    InvalidNonceLength,
63
64    #[error("Invalid padding")]
65    InvalidPadding,
66
67    #[error("Signature error, {0}")]
68    SignatureError(#[from] SignatureError),
69
70    #[error("Encoding error, {0}")]
71    EncodingError(#[from] EncodingError),
72}
73
74#[derive(Debug, Error)]
75pub enum UnsupportedOperation {
76    #[error("Encryption is not implemented for key")]
77    EncryptionNotImplementedForKey,
78}
79
80#[derive(Debug, Error)]
81pub enum EncStringParseError {
82    #[error("No type detected, missing '.' separator")]
83    NoType,
84    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
85    InvalidTypeSymm { enc_type: String, parts: usize },
86    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
87    InvalidTypeAsymm { enc_type: String, parts: usize },
88    #[error("Error decoding base64: {0}")]
89    InvalidBase64(#[from] base64::DecodeError),
90    #[error("Invalid length: expected {expected}, got {got}")]
91    InvalidLength { expected: usize, got: usize },
92    #[error("Invalid encoding {0}")]
93    InvalidCoseEncoding(coset::CoseError),
94    #[error("Algorithm missing in COSE header")]
95    CoseMissingAlgorithm,
96    #[error("Content type missing in COSE header")]
97    CoseMissingContentType,
98}
99
100#[derive(Debug, Error)]
101pub enum RsaError {
102    #[error("Unable to create public key")]
103    CreatePublicKey,
104    #[error("Unable to create private key")]
105    CreatePrivateKey,
106    #[error("Rsa error, {0}")]
107    Rsa(#[from] rsa::Error),
108}
109
110#[derive(Debug, Error)]
111pub enum SignatureError {
112    #[error("Invalid signature")]
113    InvalidSignature,
114    #[error("Invalid namespace")]
115    InvalidNamespace,
116}
117
118#[derive(Debug, Error)]
119pub enum EncodingError {
120    #[error("Invalid cose encoding")]
121    InvalidCoseEncoding,
122    #[error("Cbor serialization error")]
123    InvalidCborSerialization,
124    #[error("Missing value {0}")]
125    MissingValue(&'static str),
126    #[error("Invalid value {0}")]
127    InvalidValue(&'static str),
128    #[error("Unsupported value {0}")]
129    UnsupportedValue(&'static str),
130}
131
132/// Alias for `Result<T, CryptoError>`.
133pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;