core_crypto_keystore/
error.rs1pub use rusqlite::{Error as RusqliteError, ErrorCode as RusqliteErrorCode};
2#[cfg(target_os = "unknown")]
3use sqlite_wasm_vfs::sahpool::OpfsSAHError;
4
5#[derive(Debug, thiserror::Error)]
7pub enum CryptoKeystoreError {
8 #[error("The given key doesn't contain valid utf-8")]
9 KeyReprError(#[from] std::str::Utf8Error),
10 #[error("A transaction must be in progress to perform this operation.")]
11 MutatingOperationWithoutTransaction,
12 #[error("cannot open a new transaction as there exists another transaction currently in progress")]
13 TransactionInProgress,
14 #[error("the transaction was unexpectedly rolled back by sqlite")]
15 UnexpectedRollback,
16 #[error("failed to operate the cross-process transaction lock at {path}")]
17 TransactionLock {
18 path: String,
19 #[source]
20 source: std::io::Error,
21 },
22 #[error(transparent)]
23 TryFromSliceError(#[from] std::array::TryFromSliceError),
24 #[error("One of the Keystore locks has been poisoned")]
25 LockPoisonError,
26 #[error("The keystore has run out of keypackage bundles!")]
27 OutOfKeyPackageBundles,
28 #[error("Incorrect API usage: {0}")]
29 IncorrectApiUsage(&'static str),
30 #[error("Invalid X509 credential: {0}")]
31 InvalidX509Credential(String),
32 #[error("The credential tied to this signature keypair is different from the provided one")]
33 SignatureKeyPairDoesNotBelongToCredential,
34 #[error("The uniqueness constraint has been violated for {0}")]
35 AlreadyExists(&'static str),
36 #[error("The provided buffer is too big to be persisted in the store")]
37 BlobTooBig,
38 #[error("This database connection has been closed")]
39 Closed,
40 #[error(transparent)]
41 KeyStoreValueTransformError(#[from] postcard::Error),
42 #[error(transparent)]
43 IoError(#[from] std::io::Error),
44 #[cfg(not(target_os = "unknown"))]
45 #[error(transparent)]
46 TimeError(#[from] std::time::SystemTimeError),
47 #[error("aead::Error: {0}")]
48 AesGcmError(&'static str),
49 #[cfg(target_os = "unknown")]
50 #[error("{0}")]
51 SerdeWasmBindgenError(String),
52 #[error(transparent)]
53 DbError(#[from] RusqliteError),
54 #[error(transparent)]
55 DbMigrationError(#[from] Box<refinery::Error>),
56 #[cfg(test)]
57 #[error(transparent)]
58 MlsKeyPackageIdError(#[from] openmls::prelude::KeyPackageIdError),
59 #[cfg(test)]
60 #[error(transparent)]
61 MlsExtensionError(#[from] openmls::prelude::ExtensionError),
62 #[error("Invalid database key size, expected {expected}, got {actual}")]
63 InvalidDbKeySize { expected: usize, actual: usize },
64 #[cfg(feature = "proteus-keystore")]
65 #[error("Invalid key [{key}] size, expected {expected}, got {actual}")]
66 InvalidKeySize {
67 expected: usize,
68 actual: usize,
69 key: &'static str,
70 },
71 #[cfg(feature = "proteus-keystore")]
72 #[error(transparent)]
73 ParseIntError(#[from] std::num::ParseIntError),
74 #[cfg(feature = "proteus-keystore")]
75 #[error("Could not find a free prekey id")]
76 NoFreePrekeyId,
77 #[error("{0}")]
78 MlsKeyStoreError(String),
79 #[error(transparent)]
80 HexDecodeError(#[from] hex::FromHexError),
81 #[error(transparent)]
82 FromUtf8Error(#[from] std::string::FromUtf8Error),
83 #[cfg(target_os = "ios")]
84 #[error(transparent)]
85 HexSaltDecodeError(hex::FromHexError),
86 #[cfg(target_os = "ios")]
87 #[error(transparent)]
88 SecurityFrameworkError(#[from] security_framework::base::Error),
89 #[error("Not implemented (and probably never will)")]
90 NotImplemented,
91 #[error("Failed getting current timestamp")]
92 TimestampError,
93 #[error("Could not find {0} in keystore with value {1}")]
94 NotFound(&'static str, String),
95 #[error(transparent)]
96 SerdeJsonError(#[from] serde_json::Error),
97 #[cfg(target_os = "unknown")]
98 #[error(transparent)]
99 IdbError(#[from] idb::Error),
100 #[cfg(target_os = "unknown")]
101 #[error("Migration from version {0} is not supported")]
102 MigrationNotSupported(u32),
103 #[cfg(target_os = "unknown")]
104 #[error("CoreCrypto 10.x databases are unsupported and must be discarded")]
105 CoreCrypto10DatabaseUnsupported,
106 #[error("The migration failed: {0}")]
107 MigrationFailed(String),
108 #[cfg(target_os = "unknown")]
109 #[error("{context}")]
110 OpfsError {
111 context: &'static str,
112 #[source]
113 error: OpfsSAHError,
114 },
115 #[cfg(target_os = "unknown")]
116 #[error("{context}")]
117 LegacyOpfsMigration {
118 context: &'static str,
119 #[source]
120 error: Box<dyn std::error::Error>,
121 },
122 #[cfg(target_os = "unknown")]
123 #[error("Invalid database name {0:?}: elements separated by '/' can't be blank, '.' or '..'")]
124 InvalidDatabaseName(String),
125 #[error("The database includes migrations newer than this version of CC knows about")]
126 DatabaseFromTheFuture,
127}
128
129impl CryptoKeystoreError {
130 pub(crate) fn map_already_exists(table_name: &'static str) -> impl FnOnce(rusqlite::Error) -> Self {
136 move |err| {
137 if let rusqlite::Error::SqliteFailure(inner, _) = err
138 && matches!(
139 inner.extended_code,
140 rusqlite::ffi::SQLITE_CONSTRAINT_UNIQUE | rusqlite::ffi::SQLITE_CONSTRAINT_PRIMARYKEY
141 )
142 {
143 Self::AlreadyExists(table_name)
144 } else {
145 err.into()
146 }
147 }
148 }
149
150 #[cfg(target_os = "unknown")]
151 pub(crate) fn opfs(context: &'static str) -> impl FnOnce(OpfsSAHError) -> Self {
152 move |error| Self::OpfsError { context, error }
153 }
154
155 #[cfg(target_os = "unknown")]
156 pub(crate) fn legacy_opfs<E: std::error::Error + 'static>(context: &'static str) -> impl FnOnce(E) -> Self {
157 move |error| Self::LegacyOpfsMigration {
158 context,
159 error: Box::new(error),
160 }
161 }
162}
163
164#[cfg(target_os = "unknown")]
165#[expect(clippy::from_over_into)]
166impl Into<wasm_bindgen::JsValue> for CryptoKeystoreError {
167 fn into(self) -> wasm_bindgen::JsValue {
168 wasm_bindgen::JsValue::from_str(&self.to_string())
169 }
170}
171
172#[cfg(target_os = "unknown")]
173impl From<serde_wasm_bindgen::Error> for CryptoKeystoreError {
174 fn from(jsv: serde_wasm_bindgen::Error) -> Self {
175 Self::SerdeWasmBindgenError(jsv.to_string())
176 }
177}
178
179#[cfg(feature = "proteus-keystore")]
180impl proteus_traits::ProteusErrorCode for CryptoKeystoreError {
181 fn code(&self) -> proteus_traits::ProteusErrorKind {
182 use proteus_traits::ProteusErrorKind;
183 match self {
184 CryptoKeystoreError::KeyReprError(_) => ProteusErrorKind::DecodeError,
185 CryptoKeystoreError::TryFromSliceError(_) => ProteusErrorKind::DecodeError,
186 CryptoKeystoreError::LockPoisonError => ProteusErrorKind::OtherSystemError,
187 CryptoKeystoreError::BlobTooBig => ProteusErrorKind::IoError,
188 CryptoKeystoreError::KeyStoreValueTransformError(_) => ProteusErrorKind::DecodeError,
189 CryptoKeystoreError::IoError(_) => ProteusErrorKind::IoError,
190 CryptoKeystoreError::DbError(_) => ProteusErrorKind::IoError,
191 CryptoKeystoreError::DbMigrationError(_) => ProteusErrorKind::IoError,
192 CryptoKeystoreError::InvalidKeySize { .. } => ProteusErrorKind::InvalidArrayLen,
193 CryptoKeystoreError::ParseIntError(_) => ProteusErrorKind::DecodeError,
194 CryptoKeystoreError::HexDecodeError(_) => ProteusErrorKind::DecodeError,
195 CryptoKeystoreError::FromUtf8Error(_) => ProteusErrorKind::DecodeError,
196 _ => unreachable!(),
197 }
198 }
199}
200
201pub type CryptoKeystoreResult<T> = Result<T, CryptoKeystoreError>;