Skip to content

Commit b136cf3

Browse files
authored
[PM-17905] Add Encrypt / Decrypt errors to vault crate (#137)
## ๐ŸŽŸ๏ธ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> https://bitwarden.atlassian.net/browse/PM-17905 ## ๐Ÿ“” Objective <!-- Describe what the purpose of this PR is, for example what bug you're fixing or new feature you're adding. --> We want to deprecate the core error enum. ## โฐ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## ๐Ÿฆฎ Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - ๐Ÿ‘ (`:+1:`) or similar for great changes - ๐Ÿ“ (`:memo:`) or โ„น๏ธ (`:information_source:`) for notes or general info - โ“ (`:question:`) for questions - ๐Ÿค” (`:thinking:`) or ๐Ÿ’ญ (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - ๐ŸŽจ (`:art:`) for suggestions / improvements - โŒ (`:x:`) or โš ๏ธ (`:warning:`) for more significant problems or concerns needing attention - ๐ŸŒฑ (`:seedling:`) or โ™ป๏ธ (`:recycle:`) for future improvements or indications of technical debt - โ› (`:pick:`) for minor or nitpick changes
1 parent 334fd3e commit b136cf3

File tree

19 files changed

+209
-82
lines changed

19 files changed

+209
-82
lines changed

โ€Žcrates/bitwarden-fido/src/authenticator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum GetSelectedCredentialError {
3333
#[error(transparent)]
3434
VaultLocked(#[from] VaultLocked),
3535
#[error(transparent)]
36-
CipherError(#[from] CipherError),
36+
CryptoError(#[from] CryptoError),
3737
}
3838

3939
#[derive(Debug, Error)]

โ€Žcrates/bitwarden-fido/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
22

33
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
4-
use bitwarden_crypto::KeyContainer;
5-
use bitwarden_vault::{CipherError, CipherView};
4+
use bitwarden_crypto::{CryptoError, KeyContainer};
5+
use bitwarden_vault::CipherView;
66
use passkey::types::webauthn::UserVerificationRequirement;
77
use reqwest::Url;
88
use schemars::JsonSchema;
@@ -56,7 +56,7 @@ pub enum Fido2CredentialAutofillViewError {
5656
InvalidGuid(#[from] InvalidGuid),
5757

5858
#[error(transparent)]
59-
CipherError(#[from] CipherError),
59+
CryptoError(#[from] CryptoError),
6060

6161
#[error(transparent)]
6262
Base64DecodeError(#[from] base64::DecodeError),

โ€Žcrates/bitwarden-uniffi/src/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ pub enum Error {
5959
Cipher(#[from] bitwarden_vault::CipherError),
6060
#[error(transparent)]
6161
Totp(#[from] bitwarden_vault::TotpError),
62+
#[error(transparent)]
63+
Decrypt(#[from] bitwarden_vault::DecryptError),
64+
#[error(transparent)]
65+
DecryptFile(#[from] bitwarden_vault::DecryptFileError),
66+
#[error(transparent)]
67+
Encrypt(#[from] bitwarden_vault::EncryptError),
68+
#[error(transparent)]
69+
EncryptFile(#[from] bitwarden_vault::EncryptFileError),
6270

6371
#[error(transparent)]
6472
Export(#[from] ExportError),

โ€Žcrates/bitwarden-uniffi/src/vault/attachments.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bitwarden_vault::{
44
Attachment, AttachmentEncryptResult, AttachmentView, Cipher, VaultClientExt,
55
};
66

7-
use crate::{Client, Result};
7+
use crate::{error::Error, Client, Result};
88

99
#[derive(uniffi::Object)]
1010
pub struct ClientAttachments(pub Arc<Client>);
@@ -23,7 +23,8 @@ impl ClientAttachments {
2323
.0
2424
.vault()
2525
.attachments()
26-
.encrypt_buffer(cipher, attachment, &buffer)?)
26+
.encrypt_buffer(cipher, attachment, &buffer)
27+
.map_err(Error::Encrypt)?)
2728
}
2829

2930
/// Encrypt an attachment file located in the file system
@@ -34,12 +35,18 @@ impl ClientAttachments {
3435
decrypted_file_path: String,
3536
encrypted_file_path: String,
3637
) -> Result<Attachment> {
37-
Ok(self.0 .0.vault().attachments().encrypt_file(
38-
cipher,
39-
attachment,
40-
Path::new(&decrypted_file_path),
41-
Path::new(&encrypted_file_path),
42-
)?)
38+
Ok(self
39+
.0
40+
.0
41+
.vault()
42+
.attachments()
43+
.encrypt_file(
44+
cipher,
45+
attachment,
46+
Path::new(&decrypted_file_path),
47+
Path::new(&encrypted_file_path),
48+
)
49+
.map_err(Error::EncryptFile)?)
4350
}
4451
/// Decrypt an attachment file in memory
4552
pub fn decrypt_buffer(
@@ -53,7 +60,8 @@ impl ClientAttachments {
5360
.0
5461
.vault()
5562
.attachments()
56-
.decrypt_buffer(cipher, attachment, &buffer)?)
63+
.decrypt_buffer(cipher, attachment, &buffer)
64+
.map_err(Error::Decrypt)?)
5765
}
5866

5967
/// Decrypt an attachment file located in the file system
@@ -64,11 +72,17 @@ impl ClientAttachments {
6472
encrypted_file_path: String,
6573
decrypted_file_path: String,
6674
) -> Result<()> {
67-
Ok(self.0 .0.vault().attachments().decrypt_file(
68-
cipher,
69-
attachment,
70-
Path::new(&encrypted_file_path),
71-
Path::new(&decrypted_file_path),
72-
)?)
75+
Ok(self
76+
.0
77+
.0
78+
.vault()
79+
.attachments()
80+
.decrypt_file(
81+
cipher,
82+
attachment,
83+
Path::new(&encrypted_file_path),
84+
Path::new(&decrypted_file_path),
85+
)
86+
.map_err(Error::DecryptFile)?)
7387
}
7488
}

โ€Žcrates/bitwarden-uniffi/src/vault/ciphers.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@ pub struct ClientCiphers(pub Arc<Client>);
1212
impl ClientCiphers {
1313
/// Encrypt cipher
1414
pub fn encrypt(&self, cipher_view: CipherView) -> Result<Cipher> {
15-
Ok(self.0 .0.vault().ciphers().encrypt(cipher_view)?)
15+
Ok(self
16+
.0
17+
.0
18+
.vault()
19+
.ciphers()
20+
.encrypt(cipher_view)
21+
.map_err(Error::Encrypt)?)
1622
}
1723

1824
/// Decrypt cipher
1925
pub fn decrypt(&self, cipher: Cipher) -> Result<CipherView> {
20-
Ok(self.0 .0.vault().ciphers().decrypt(cipher)?)
26+
Ok(self
27+
.0
28+
.0
29+
.vault()
30+
.ciphers()
31+
.decrypt(cipher)
32+
.map_err(Error::Decrypt)?)
2133
}
2234

2335
/// Decrypt cipher list
2436
pub fn decrypt_list(&self, ciphers: Vec<Cipher>) -> Result<Vec<CipherListView>> {
25-
Ok(self.0 .0.vault().ciphers().decrypt_list(ciphers)?)
37+
Ok(self
38+
.0
39+
.0
40+
.vault()
41+
.ciphers()
42+
.decrypt_list(ciphers)
43+
.map_err(Error::Decrypt)?)
2644
}
2745

2846
pub fn decrypt_fido2_credentials(
@@ -34,7 +52,8 @@ impl ClientCiphers {
3452
.0
3553
.vault()
3654
.ciphers()
37-
.decrypt_fido2_credentials(cipher_view)?)
55+
.decrypt_fido2_credentials(cipher_view)
56+
.map_err(Error::Decrypt)?)
3857
}
3958

4059
/// Move a cipher to an organization, reencrypting the cipher key if necessary

โ€Žcrates/bitwarden-uniffi/src/vault/collections.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use bitwarden_vault::{Collection, CollectionView, VaultClientExt};
44

5-
use crate::{Client, Result};
5+
use crate::{error::Error, Client, Result};
66

77
#[derive(uniffi::Object)]
88
pub struct ClientCollections(pub Arc<Client>);
@@ -11,11 +11,23 @@ pub struct ClientCollections(pub Arc<Client>);
1111
impl ClientCollections {
1212
/// Decrypt collection
1313
pub fn decrypt(&self, collection: Collection) -> Result<CollectionView> {
14-
Ok(self.0 .0.vault().collections().decrypt(collection)?)
14+
Ok(self
15+
.0
16+
.0
17+
.vault()
18+
.collections()
19+
.decrypt(collection)
20+
.map_err(Error::Decrypt)?)
1521
}
1622

1723
/// Decrypt collection list
1824
pub fn decrypt_list(&self, collections: Vec<Collection>) -> Result<Vec<CollectionView>> {
19-
Ok(self.0 .0.vault().collections().decrypt_list(collections)?)
25+
Ok(self
26+
.0
27+
.0
28+
.vault()
29+
.collections()
30+
.decrypt_list(collections)
31+
.map_err(Error::Decrypt)?)
2032
}
2133
}

โ€Žcrates/bitwarden-uniffi/src/vault/folders.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use bitwarden_vault::{Folder, FolderView, VaultClientExt};
44

5-
use crate::{Client, Result};
5+
use crate::{error::Error, Client, Result};
66

77
#[derive(uniffi::Object)]
88
pub struct ClientFolders(pub Arc<Client>);
@@ -11,16 +11,34 @@ pub struct ClientFolders(pub Arc<Client>);
1111
impl ClientFolders {
1212
/// Encrypt folder
1313
pub fn encrypt(&self, folder: FolderView) -> Result<Folder> {
14-
Ok(self.0 .0.vault().folders().encrypt(folder)?)
14+
Ok(self
15+
.0
16+
.0
17+
.vault()
18+
.folders()
19+
.encrypt(folder)
20+
.map_err(Error::Encrypt)?)
1521
}
1622

1723
/// Decrypt folder
1824
pub fn decrypt(&self, folder: Folder) -> Result<FolderView> {
19-
Ok(self.0 .0.vault().folders().decrypt(folder)?)
25+
Ok(self
26+
.0
27+
.0
28+
.vault()
29+
.folders()
30+
.decrypt(folder)
31+
.map_err(Error::Decrypt)?)
2032
}
2133

2234
/// Decrypt folder list
2335
pub fn decrypt_list(&self, folders: Vec<Folder>) -> Result<Vec<FolderView>> {
24-
Ok(self.0 .0.vault().folders().decrypt_list(folders)?)
36+
Ok(self
37+
.0
38+
.0
39+
.vault()
40+
.folders()
41+
.decrypt_list(folders)
42+
.map_err(Error::Decrypt)?)
2543
}
2644
}

โ€Žcrates/bitwarden-uniffi/src/vault/password_history.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use bitwarden_vault::{PasswordHistory, PasswordHistoryView, VaultClientExt};
44

5-
use crate::{Client, Result};
5+
use crate::{error::Error, Client, Result};
66

77
#[derive(uniffi::Object)]
88
pub struct ClientPasswordHistory(pub Arc<Client>);
@@ -16,11 +16,18 @@ impl ClientPasswordHistory {
1616
.0
1717
.vault()
1818
.password_history()
19-
.encrypt(password_history)?)
19+
.encrypt(password_history)
20+
.map_err(Error::Encrypt)?)
2021
}
2122

2223
/// Decrypt password history
2324
pub fn decrypt_list(&self, list: Vec<PasswordHistory>) -> Result<Vec<PasswordHistoryView>> {
24-
Ok(self.0 .0.vault().password_history().decrypt_list(list)?)
25+
Ok(self
26+
.0
27+
.0
28+
.vault()
29+
.password_history()
30+
.decrypt_list(list)
31+
.map_err(Error::Decrypt)?)
2532
}
2633
}

โ€Žcrates/bitwarden-vault/src/cipher/cipher.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl CipherView {
453453
pub fn decrypt_fido2_credentials(
454454
&self,
455455
enc: &dyn KeyContainer,
456-
) -> Result<Vec<Fido2CredentialView>, CipherError> {
456+
) -> Result<Vec<Fido2CredentialView>, CryptoError> {
457457
let key = self.locate_key(enc, &None)?;
458458
let cipher_key = Cipher::get_cipher_key(key, &self.key)?;
459459

โ€Žcrates/bitwarden-vault/src/error.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
use bitwarden_error::bitwarden_error;
12
use thiserror::Error;
23

4+
/// Generic error type for vault encryption errors.
5+
#[bitwarden_error(flat)]
6+
#[derive(Debug, Error)]
7+
pub enum EncryptError {
8+
#[error(transparent)]
9+
Crypto(#[from] bitwarden_crypto::CryptoError),
10+
#[error(transparent)]
11+
VaultLocked(#[from] bitwarden_core::VaultLocked),
12+
}
13+
14+
/// Generic error type for decryption errors
15+
#[bitwarden_error(flat)]
16+
#[derive(Debug, Error)]
17+
pub enum DecryptError {
18+
#[error(transparent)]
19+
Crypto(#[from] bitwarden_crypto::CryptoError),
20+
#[error(transparent)]
21+
VaultLocked(#[from] bitwarden_core::VaultLocked),
22+
}
23+
324
#[derive(Debug, Error)]
425
pub enum VaultParseError {
526
#[error(transparent)]

โ€Žcrates/bitwarden-vault/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ pub use totp::{
1818
generate_totp, generate_totp_cipher_view, Totp, TotpAlgorithm, TotpError, TotpResponse,
1919
};
2020
mod error;
21-
pub use error::VaultParseError;
21+
pub use error::{DecryptError, EncryptError, VaultParseError};
2222
mod vault_client;
2323
pub use vault_client::{VaultClient, VaultClientExt};
2424
mod mobile;
25+
pub use mobile::attachment_client::{DecryptFileError, EncryptFileError};
2526
mod sync;
2627
mod totp_client;
2728
pub use sync::{SyncRequest, SyncResponse};

0 commit comments

Comments
ย (0)