Skip to main content

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