bitwarden_crypto/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::fmt::Debug;

use thiserror::Error;
use uuid::Uuid;

use crate::fingerprint::FingerprintError;

#[derive(Debug, Error)]
pub enum CryptoError {
    #[error("The provided key is not the expected type")]
    InvalidKey,
    #[error("The cipher's MAC doesn't match the expected value")]
    InvalidMac,
    #[error("The key provided expects mac protected encstrings, but the mac is missing")]
    MacNotProvided,
    #[error("Error while decrypting EncString")]
    KeyDecrypt,
    #[error("The cipher key has an invalid length")]
    InvalidKeyLen,
    #[error("The value is not a valid UTF8 String")]
    InvalidUtf8String,
    #[error("Missing Key for organization with ID {0}")]
    MissingKey(Uuid),
    #[error("The item was missing a required field: {0}")]
    MissingField(&'static str),

    #[error("Insufficient KDF parameters")]
    InsufficientKdfParameters,

    #[error("EncString error, {0}")]
    EncString(#[from] EncStringParseError),

    #[error("Rsa error, {0}")]
    RsaError(#[from] RsaError),

    #[error("Fingerprint error, {0}")]
    FingerprintError(#[from] FingerprintError),

    #[error("Argon2 error, {0}")]
    ArgonError(#[from] argon2::Error),

    #[error("Number is zero")]
    ZeroNumber,
}

#[derive(Debug, Error)]
pub enum EncStringParseError {
    #[error("No type detected, missing '.' separator")]
    NoType,
    #[error("Invalid symmetric type, got type {enc_type} with {parts} parts")]
    InvalidTypeSymm { enc_type: String, parts: usize },
    #[error("Invalid asymmetric type, got type {enc_type} with {parts} parts")]
    InvalidTypeAsymm { enc_type: String, parts: usize },
    #[error("Error decoding base64: {0}")]
    InvalidBase64(#[from] base64::DecodeError),
    #[error("Invalid length: expected {expected}, got {got}")]
    InvalidLength { expected: usize, got: usize },
}

#[derive(Debug, Error)]
pub enum RsaError {
    #[error("Unable to create public key")]
    CreatePublicKey,
    #[error("Unable to create private key")]
    CreatePrivateKey,
    #[error("Rsa error, {0}")]
    Rsa(#[from] rsa::Error),
}

/// Alias for `Result<T, CryptoError>`.
pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;