bitwarden_send/
send_client.rs

1use std::path::Path;
2
3use bitwarden_core::Client;
4use bitwarden_crypto::{Decryptable, EncString, Encryptable, IdentifyKey};
5use thiserror::Error;
6
7use crate::{Send, SendListView, SendView};
8
9/// Generic error type for send encryption errors.
10#[derive(Debug, Error)]
11pub enum SendEncryptError {
12    #[error(transparent)]
13    Crypto(#[from] bitwarden_crypto::CryptoError),
14    #[error(transparent)]
15    VaultLocked(#[from] bitwarden_core::VaultLockedError),
16}
17
18/// Generic error type for send decryption errors
19#[derive(Debug, Error)]
20pub enum SendDecryptError {
21    #[error(transparent)]
22    Crypto(#[from] bitwarden_crypto::CryptoError),
23    #[error(transparent)]
24    VaultLocked(#[from] bitwarden_core::VaultLockedError),
25}
26
27/// Generic error type for send encryption errors.
28#[derive(Debug, Error)]
29pub enum SendEncryptFileError {
30    #[error(transparent)]
31    Encrypt(#[from] SendEncryptError),
32    #[error(transparent)]
33    Io(#[from] std::io::Error),
34}
35
36/// Generic error type for send decryption errors
37#[derive(Debug, Error)]
38pub enum SendDecryptFileError {
39    #[error(transparent)]
40    Decrypt(#[from] SendDecryptError),
41    #[error(transparent)]
42    Io(#[from] std::io::Error),
43}
44
45pub struct SendClient {
46    client: Client,
47}
48
49impl SendClient {
50    fn new(client: Client) -> Self {
51        Self { client }
52    }
53
54    pub fn decrypt(&self, send: Send) -> Result<SendView, SendDecryptError> {
55        let key_store = self.client.internal.get_key_store();
56        let send_view = key_store.decrypt(&send)?;
57        Ok(send_view)
58    }
59
60    pub fn decrypt_list(&self, sends: Vec<Send>) -> Result<Vec<SendListView>, SendDecryptError> {
61        let key_store = self.client.internal.get_key_store();
62        let send_views = key_store.decrypt_list(&sends)?;
63        Ok(send_views)
64    }
65
66    pub fn decrypt_file(
67        &self,
68        send: Send,
69        encrypted_file_path: &Path,
70        decrypted_file_path: &Path,
71    ) -> Result<(), SendDecryptFileError> {
72        let data = std::fs::read(encrypted_file_path)?;
73        let decrypted = self.decrypt_buffer(send, &data)?;
74        std::fs::write(decrypted_file_path, decrypted)?;
75        Ok(())
76    }
77
78    pub fn decrypt_buffer(
79        &self,
80        send: Send,
81        encrypted_buffer: &[u8],
82    ) -> Result<Vec<u8>, SendDecryptError> {
83        let key_store = self.client.internal.get_key_store();
84        let mut ctx = key_store.context();
85
86        let key = Send::get_key(&mut ctx, &send.key, send.key_identifier())?;
87
88        let buf = EncString::from_buffer(encrypted_buffer)?;
89        Ok(buf.decrypt(&mut ctx, key)?)
90    }
91
92    pub fn encrypt(&self, send_view: SendView) -> Result<Send, SendEncryptError> {
93        let key_store = self.client.internal.get_key_store();
94
95        let send = key_store.encrypt(send_view)?;
96
97        Ok(send)
98    }
99
100    pub fn encrypt_file(
101        &self,
102        send: Send,
103        decrypted_file_path: &Path,
104        encrypted_file_path: &Path,
105    ) -> Result<(), SendEncryptFileError> {
106        let data = std::fs::read(decrypted_file_path)?;
107        let encrypted = self.encrypt_buffer(send, &data)?;
108        std::fs::write(encrypted_file_path, encrypted)?;
109        Ok(())
110    }
111
112    pub fn encrypt_buffer(&self, send: Send, buffer: &[u8]) -> Result<Vec<u8>, SendEncryptError> {
113        let key_store = self.client.internal.get_key_store();
114        let mut ctx = key_store.context();
115
116        let key = Send::get_key(&mut ctx, &send.key, send.key_identifier())?;
117
118        let encrypted = buffer.encrypt(&mut ctx, key)?;
119        Ok(encrypted.to_buffer()?)
120    }
121}
122
123pub trait SendClientExt {
124    fn sends(&self) -> SendClient;
125}
126
127impl SendClientExt for Client {
128    fn sends(&self) -> SendClient {
129        SendClient::new(self.clone())
130    }
131}