core_crypto_keystore/
error.rs

1/// Error type to represent various errors that can happen in the KeyStore
2#[derive(Debug, thiserror::Error)]
3pub enum CryptoKeystoreError {
4    #[error("The given key doesn't contain valid utf-8")]
5    KeyReprError(#[from] std::str::Utf8Error),
6    #[error("A transaction must be in progress to perform this operation.")]
7    MutatingOperationWithoutTransaction,
8    #[error("Cannot perform the operation \"{attempted_operation:?}\" while a transaction is in progress.")]
9    TransactionInProgress { attempted_operation: String },
10    #[error(transparent)]
11    TryFromSliceError(#[from] std::array::TryFromSliceError),
12    #[error("One of the Keystore locks has been poisoned")]
13    LockPoisonError,
14    #[error("The keystore has run out of keypackage bundles!")]
15    OutOfKeyPackageBundles,
16    #[error("Incorrect API usage: {0}")]
17    IncorrectApiUsage(&'static str),
18    #[error("The credential tied to this signature keypair is different from the provided one")]
19    SignatureKeyPairDoesNotBelongToCredential,
20    #[error("The uniqueness constraint has been violated for {0}")]
21    AlreadyExists(&'static str),
22    #[error("The provided buffer is too big to be persisted in the store")]
23    BlobTooBig,
24    #[error("This database connection has been closed")]
25    Closed,
26    #[error(transparent)]
27    KeyStoreValueTransformError(#[from] postcard::Error),
28    #[error(transparent)]
29    IoError(#[from] std::io::Error),
30    #[cfg(not(target_family = "wasm"))]
31    #[error(transparent)]
32    TimeError(#[from] std::time::SystemTimeError),
33    #[cfg(target_family = "wasm")]
34    #[error(transparent)]
35    ChannelError(#[from] std::sync::mpsc::TryRecvError),
36    #[cfg(target_family = "wasm")]
37    #[error("The task has been canceled")]
38    WasmExecutorError,
39    #[error("aead::Error")]
40    AesGcmError,
41    #[cfg(target_family = "wasm")]
42    #[error("{0}")]
43    SerdeWasmBindgenError(String),
44    #[cfg(not(target_family = "wasm"))]
45    #[error(transparent)]
46    DbError(#[from] rusqlite::Error),
47    #[cfg(not(target_family = "wasm"))]
48    #[error(transparent)]
49    DbMigrationError(#[from] Box<refinery::Error>),
50    #[cfg(test)]
51    #[error(transparent)]
52    MlsKeyPackageIdError(#[from] openmls::prelude::KeyPackageIdError),
53    #[cfg(test)]
54    #[error(transparent)]
55    MlsExtensionError(#[from] openmls::prelude::ExtensionError),
56    #[error("Invalid database key size, expected {expected}, got {actual}")]
57    InvalidDbKeySize { expected: usize, actual: usize },
58    #[cfg(feature = "proteus-keystore")]
59    #[error("Invalid key [{key}] size, expected {expected}, got {actual}")]
60    InvalidKeySize {
61        expected: usize,
62        actual: usize,
63        key: &'static str,
64    },
65    #[cfg(feature = "proteus-keystore")]
66    #[error(transparent)]
67    ParseIntError(#[from] std::num::ParseIntError),
68    #[cfg(feature = "proteus-keystore")]
69    #[error("Could not find a free prekey id")]
70    NoFreePrekeyId,
71    #[error("{0}")]
72    MlsKeyStoreError(String),
73    #[error(transparent)]
74    HexDecodeError(#[from] hex::FromHexError),
75    #[error(transparent)]
76    FromUtf8Error(#[from] std::string::FromUtf8Error),
77    #[cfg(target_os = "ios")]
78    #[error(transparent)]
79    HexSaltDecodeError(hex::FromHexError),
80    #[cfg(target_os = "ios")]
81    #[error(transparent)]
82    SecurityFrameworkError(#[from] security_framework::base::Error),
83    #[cfg(target_family = "wasm")]
84    #[error("{0}")]
85    JsError(String),
86    #[error("Not implemented (and probably never will)")]
87    NotImplemented,
88    #[error("Failed getting current timestamp")]
89    TimestampError,
90    #[error("Could not find {0} in keystore with value {1}")]
91    NotFound(&'static str, String),
92    #[error(transparent)]
93    SerdeJsonError(#[from] serde_json::Error),
94    #[cfg(target_family = "wasm")]
95    #[error(transparent)]
96    IdbError(#[from] idb::Error),
97    #[cfg(target_family = "wasm")]
98    #[error(transparent)]
99    IdbErrorCryptoKeystoreV1_0_0(idb::Error),
100    #[cfg(target_family = "wasm")]
101    #[error("Migration from version {0} is not supported")]
102    MigrationNotSupported(u32),
103    #[error("The migration failed: {0}")]
104    MigrationFailed(String),
105    #[error("the provided bytes could not be interpreted as the primary key of {0}")]
106    InvalidPrimaryKeyBytes(&'static str),
107    #[error("the collection name {0} is unknown and could not be found")]
108    UnknownCollectionName(&'static str),
109}
110
111#[cfg(target_family = "wasm")]
112impl From<wasm_bindgen::JsValue> for CryptoKeystoreError {
113    fn from(jsv: wasm_bindgen::JsValue) -> Self {
114        Self::JsError(jsv.as_string().unwrap())
115    }
116}
117
118#[cfg(target_family = "wasm")]
119#[allow(clippy::from_over_into)]
120impl Into<wasm_bindgen::JsValue> for CryptoKeystoreError {
121    fn into(self) -> wasm_bindgen::JsValue {
122        wasm_bindgen::JsValue::from_str(&self.to_string())
123    }
124}
125
126#[cfg(target_family = "wasm")]
127impl From<serde_wasm_bindgen::Error> for CryptoKeystoreError {
128    fn from(jsv: serde_wasm_bindgen::Error) -> Self {
129        Self::SerdeWasmBindgenError(jsv.to_string())
130    }
131}
132
133#[cfg(feature = "proteus-keystore")]
134impl proteus_traits::ProteusErrorCode for CryptoKeystoreError {
135    fn code(&self) -> proteus_traits::ProteusErrorKind {
136        use proteus_traits::ProteusErrorKind;
137        match self {
138            CryptoKeystoreError::KeyReprError(_) => ProteusErrorKind::DecodeError,
139            CryptoKeystoreError::TryFromSliceError(_) => ProteusErrorKind::DecodeError,
140            CryptoKeystoreError::LockPoisonError => ProteusErrorKind::OtherSystemError,
141            CryptoKeystoreError::BlobTooBig => ProteusErrorKind::IoError,
142            CryptoKeystoreError::KeyStoreValueTransformError(_) => ProteusErrorKind::DecodeError,
143            CryptoKeystoreError::IoError(_) => ProteusErrorKind::IoError,
144            #[cfg(not(target_family = "wasm"))]
145            CryptoKeystoreError::DbError(_) => ProteusErrorKind::IoError,
146            #[cfg(not(target_family = "wasm"))]
147            CryptoKeystoreError::DbMigrationError(_) => ProteusErrorKind::IoError,
148            CryptoKeystoreError::InvalidKeySize { .. } => ProteusErrorKind::InvalidArrayLen,
149            CryptoKeystoreError::ParseIntError(_) => ProteusErrorKind::DecodeError,
150            CryptoKeystoreError::HexDecodeError(_) => ProteusErrorKind::DecodeError,
151            CryptoKeystoreError::FromUtf8Error(_) => ProteusErrorKind::DecodeError,
152            _ => unreachable!(),
153        }
154    }
155}
156
157/// A specialized Result for the KeyStore functions
158pub type CryptoKeystoreResult<T> = Result<T, CryptoKeystoreError>;