bitwarden_crypto/
error.rs

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