Skip to main content

bitwarden_send/
send_client.rs

1use std::{path::Path, sync::Arc};
2
3use bitwarden_core::Client;
4use bitwarden_crypto::{
5    Decryptable, EncString, IdentifyKey, OctetStreamBytes, PrimitiveEncryptable,
6};
7use bitwarden_error::bitwarden_error;
8use bitwarden_state::repository::{Repository, RepositoryError};
9use thiserror::Error;
10
11use crate::{Send, SendListView, SendView};
12
13/// Generic error type for send encryption errors.
14#[allow(missing_docs)]
15#[bitwarden_error(flat)]
16#[derive(Debug, Error)]
17pub enum SendEncryptError {
18    #[error(transparent)]
19    Crypto(#[from] bitwarden_crypto::CryptoError),
20}
21
22/// Generic error type for send decryption errors
23#[allow(missing_docs)]
24#[bitwarden_error(flat)]
25#[derive(Debug, Error)]
26pub enum SendDecryptError {
27    #[error(transparent)]
28    Crypto(#[from] bitwarden_crypto::CryptoError),
29}
30
31/// Generic error type for send encryption errors.
32#[allow(missing_docs)]
33#[derive(Debug, Error)]
34#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
35pub enum SendEncryptFileError {
36    #[error(transparent)]
37    Encrypt(#[from] SendEncryptError),
38    #[error(transparent)]
39    Io(#[from] std::io::Error),
40}
41
42/// Generic error type for send decryption errors
43#[allow(missing_docs)]
44#[derive(Debug, Error)]
45#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
46pub enum SendDecryptFileError {
47    #[error(transparent)]
48    Decrypt(#[from] SendDecryptError),
49    #[error(transparent)]
50    Io(#[from] std::io::Error),
51}
52
53#[allow(missing_docs)]
54#[bitwarden_ffi::wasm_object]
55pub struct SendClient {
56    pub(crate) client: Client,
57}
58
59#[allow(missing_docs)]
60#[bitwarden_ffi::wasm_export]
61impl SendClient {
62    /// Decrypt a [`Send`] into a [`SendView`].
63    /// This is a temporary function to support the transition to fully using the SDK for Send logic
64    pub fn decrypt_send(&self, send: Send) -> Result<SendView, SendDecryptError> {
65        self.decrypt(send)
66    }
67}
68
69impl SendClient {
70    fn new(client: Client) -> Self {
71        Self { client }
72    }
73
74    #[allow(missing_docs)]
75    pub fn decrypt(&self, send: Send) -> Result<SendView, SendDecryptError> {
76        let key_store = self.client.internal.get_key_store();
77        let send_view = key_store.decrypt(&send)?;
78        Ok(send_view)
79    }
80
81    #[allow(missing_docs)]
82    pub fn decrypt_list(&self, sends: Vec<Send>) -> Result<Vec<SendListView>, SendDecryptError> {
83        let key_store = self.client.internal.get_key_store();
84        let send_views = key_store.decrypt_list(&sends)?;
85        Ok(send_views)
86    }
87
88    #[allow(missing_docs)]
89    pub fn decrypt_file(
90        &self,
91        send: Send,
92        encrypted_file_path: &Path,
93        decrypted_file_path: &Path,
94    ) -> Result<(), SendDecryptFileError> {
95        let data = std::fs::read(encrypted_file_path)?;
96        let decrypted = self.decrypt_buffer(send, &data)?;
97        std::fs::write(decrypted_file_path, decrypted)?;
98        Ok(())
99    }
100
101    #[allow(missing_docs)]
102    pub fn decrypt_buffer(
103        &self,
104        send: Send,
105        encrypted_buffer: &[u8],
106    ) -> Result<Vec<u8>, SendDecryptError> {
107        let key_store = self.client.internal.get_key_store();
108        let mut ctx = key_store.context();
109
110        let key = Send::get_key(&mut ctx, &send.key, send.key_identifier())?;
111
112        let buf = EncString::from_buffer(encrypted_buffer)?;
113        Ok(buf.decrypt(&mut ctx, key)?)
114    }
115
116    #[allow(missing_docs)]
117    pub fn encrypt(&self, send_view: SendView) -> Result<Send, SendEncryptError> {
118        let key_store = self.client.internal.get_key_store();
119
120        let send = key_store.encrypt(send_view)?;
121
122        Ok(send)
123    }
124
125    #[allow(missing_docs)]
126    pub fn encrypt_file(
127        &self,
128        send: Send,
129        decrypted_file_path: &Path,
130        encrypted_file_path: &Path,
131    ) -> Result<(), SendEncryptFileError> {
132        let data = std::fs::read(decrypted_file_path)?;
133        let encrypted = self.encrypt_buffer(send, &data)?;
134        std::fs::write(encrypted_file_path, encrypted)?;
135        Ok(())
136    }
137
138    #[allow(missing_docs)]
139    pub fn encrypt_buffer(&self, send: Send, buffer: &[u8]) -> Result<Vec<u8>, SendEncryptError> {
140        let key_store = self.client.internal.get_key_store();
141        let mut ctx = key_store.context();
142
143        let key = Send::get_key(&mut ctx, &send.key, send.key_identifier())?;
144
145        let encrypted = OctetStreamBytes::from(buffer).encrypt(&mut ctx, key)?;
146        Ok(encrypted.to_buffer()?)
147    }
148}
149
150impl SendClient {
151    pub(crate) fn get_repository(&self) -> Result<Arc<dyn Repository<Send>>, RepositoryError> {
152        Ok(self.client.platform().state().get::<Send>()?)
153    }
154}
155
156#[allow(missing_docs)]
157pub trait SendClientExt {
158    fn sends(&self) -> SendClient;
159}
160
161impl SendClientExt for Client {
162    fn sends(&self) -> SendClient {
163        SendClient::new(self.clone())
164    }
165}