bitwarden_send/
send_client.rs

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