-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Type of change ``` - [ ] Bug fix - [x] New feature development - [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc) - [ ] Build/deploy pipeline (DevOps) - [ ] Other ``` ## Objective Add encryption/decryption support for attachment files
- Loading branch information
1 parent
8b435e5
commit d70deac
Showing
9 changed files
with
472 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::{path::Path, sync::Arc}; | ||
|
||
use bitwarden::vault::{Attachment, AttachmentEncryptResult, AttachmentView, Cipher}; | ||
|
||
use crate::{Client, Result}; | ||
|
||
#[derive(uniffi::Object)] | ||
pub struct ClientAttachments(pub Arc<Client>); | ||
|
||
#[uniffi::export(async_runtime = "tokio")] | ||
impl ClientAttachments { | ||
/// Encrypt an attachment file in memory | ||
pub async fn encrypt_buffer( | ||
&self, | ||
cipher: Cipher, | ||
attachment: AttachmentView, | ||
buffer: Vec<u8>, | ||
) -> Result<AttachmentEncryptResult> { | ||
Ok(self | ||
.0 | ||
.0 | ||
.read() | ||
.await | ||
.vault() | ||
.attachments() | ||
.encrypt_buffer(cipher, attachment, &buffer) | ||
.await?) | ||
} | ||
|
||
/// Encrypt an attachment file located in the file system | ||
pub async fn encrypt_file( | ||
&self, | ||
cipher: Cipher, | ||
attachment: AttachmentView, | ||
decrypted_file_path: String, | ||
encrypted_file_path: String, | ||
) -> Result<Attachment> { | ||
Ok(self | ||
.0 | ||
.0 | ||
.read() | ||
.await | ||
.vault() | ||
.attachments() | ||
.encrypt_file( | ||
cipher, | ||
attachment, | ||
Path::new(&decrypted_file_path), | ||
Path::new(&encrypted_file_path), | ||
) | ||
.await?) | ||
} | ||
/// Decrypt an attachment file in memory | ||
pub async fn decrypt_buffer( | ||
&self, | ||
cipher: Cipher, | ||
attachment: Attachment, | ||
buffer: Vec<u8>, | ||
) -> Result<Vec<u8>> { | ||
Ok(self | ||
.0 | ||
.0 | ||
.read() | ||
.await | ||
.vault() | ||
.attachments() | ||
.decrypt_buffer(cipher, attachment, &buffer) | ||
.await?) | ||
} | ||
|
||
/// Decrypt an attachment file located in the file system | ||
pub async fn decrypt_file( | ||
&self, | ||
cipher: Cipher, | ||
attachment: Attachment, | ||
encrypted_file_path: String, | ||
decrypted_file_path: String, | ||
) -> Result<()> { | ||
Ok(self | ||
.0 | ||
.0 | ||
.read() | ||
.await | ||
.vault() | ||
.attachments() | ||
.decrypt_file( | ||
cipher, | ||
attachment, | ||
Path::new(&encrypted_file_path), | ||
Path::new(&decrypted_file_path), | ||
) | ||
.await?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::path::Path; | ||
|
||
use bitwarden_crypto::{EncString, KeyDecryptable, KeyEncryptable, LocateKey}; | ||
|
||
use super::client_vault::ClientVault; | ||
use crate::{ | ||
error::{Error, Result}, | ||
vault::{ | ||
Attachment, AttachmentEncryptResult, AttachmentFile, AttachmentFileView, AttachmentView, | ||
Cipher, | ||
}, | ||
Client, | ||
}; | ||
|
||
pub struct ClientAttachments<'a> { | ||
pub(crate) client: &'a Client, | ||
} | ||
|
||
impl<'a> ClientAttachments<'a> { | ||
pub async fn encrypt_buffer( | ||
&self, | ||
cipher: Cipher, | ||
attachment: AttachmentView, | ||
buffer: &[u8], | ||
) -> Result<AttachmentEncryptResult> { | ||
let enc = self.client.get_encryption_settings()?; | ||
let key = cipher.locate_key(enc, &None).ok_or(Error::VaultLocked)?; | ||
|
||
Ok(AttachmentFileView { | ||
cipher, | ||
attachment, | ||
contents: buffer, | ||
} | ||
.encrypt_with_key(key)?) | ||
} | ||
pub async fn encrypt_file( | ||
&self, | ||
cipher: Cipher, | ||
attachment: AttachmentView, | ||
decrypted_file_path: &Path, | ||
encrypted_file_path: &Path, | ||
) -> Result<Attachment> { | ||
let data = std::fs::read(decrypted_file_path).unwrap(); | ||
let AttachmentEncryptResult { | ||
attachment, | ||
contents, | ||
} = self.encrypt_buffer(cipher, attachment, &data).await?; | ||
std::fs::write(encrypted_file_path, contents)?; | ||
Ok(attachment) | ||
} | ||
|
||
pub async fn decrypt_buffer( | ||
&self, | ||
cipher: Cipher, | ||
attachment: Attachment, | ||
encrypted_buffer: &[u8], | ||
) -> Result<Vec<u8>> { | ||
let enc = self.client.get_encryption_settings()?; | ||
let key = cipher.locate_key(enc, &None).ok_or(Error::VaultLocked)?; | ||
|
||
AttachmentFile { | ||
cipher, | ||
attachment, | ||
contents: EncString::from_buffer(encrypted_buffer)?, | ||
} | ||
.decrypt_with_key(key) | ||
.map_err(Error::Crypto) | ||
} | ||
pub async fn decrypt_file( | ||
&self, | ||
cipher: Cipher, | ||
attachment: Attachment, | ||
encrypted_file_path: &Path, | ||
decrypted_file_path: &Path, | ||
) -> Result<()> { | ||
let data = std::fs::read(encrypted_file_path).unwrap(); | ||
let decrypted = self.decrypt_buffer(cipher, attachment, &data).await?; | ||
std::fs::write(decrypted_file_path, decrypted)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'a> ClientVault<'a> { | ||
pub fn attachments(&'a self) -> ClientAttachments<'a> { | ||
ClientAttachments { | ||
client: self.client, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.