bitwarden_send/
client_sends.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::path::Path;

use bitwarden_core::{Client, Error};
use bitwarden_crypto::{EncString, KeyDecryptable, KeyEncryptable};

use crate::{Send, SendListView, SendView};

pub struct ClientSends<'a> {
    client: &'a Client,
}

impl<'a> ClientSends<'a> {
    fn new(client: &'a Client) -> Self {
        Self { client }
    }

    pub fn decrypt(&self, send: Send) -> Result<SendView, Error> {
        let enc = self.client.internal.get_encryption_settings()?;
        let key = enc.get_key(&None)?;

        let send_view = send.decrypt_with_key(key)?;

        Ok(send_view)
    }

    pub fn decrypt_list(&self, sends: Vec<Send>) -> Result<Vec<SendListView>, Error> {
        let enc = self.client.internal.get_encryption_settings()?;
        let key = enc.get_key(&None)?;

        let send_views = sends.decrypt_with_key(key)?;

        Ok(send_views)
    }

    pub fn decrypt_file(
        &self,
        send: Send,
        encrypted_file_path: &Path,
        decrypted_file_path: &Path,
    ) -> Result<(), Error> {
        let data = std::fs::read(encrypted_file_path)?;
        let decrypted = self.decrypt_buffer(send, &data)?;
        std::fs::write(decrypted_file_path, decrypted)?;
        Ok(())
    }

    pub fn decrypt_buffer(&self, send: Send, encrypted_buffer: &[u8]) -> Result<Vec<u8>, Error> {
        let enc = self.client.internal.get_encryption_settings()?;
        let key = enc.get_key(&None)?;
        let key = Send::get_key(&send.key, key)?;

        let buf = EncString::from_buffer(encrypted_buffer)?;
        Ok(buf.decrypt_with_key(&key)?)
    }

    pub fn encrypt(&self, send_view: SendView) -> Result<Send, Error> {
        let enc = self.client.internal.get_encryption_settings()?;
        let key = enc.get_key(&None)?;

        let send = send_view.encrypt_with_key(key)?;

        Ok(send)
    }

    pub fn encrypt_file(
        &self,
        send: Send,
        decrypted_file_path: &Path,
        encrypted_file_path: &Path,
    ) -> Result<(), Error> {
        let data = std::fs::read(decrypted_file_path)?;
        let encrypted = self.encrypt_buffer(send, &data)?;
        std::fs::write(encrypted_file_path, encrypted)?;
        Ok(())
    }

    pub fn encrypt_buffer(&self, send: Send, buffer: &[u8]) -> Result<Vec<u8>, Error> {
        let enc = self.client.internal.get_encryption_settings()?;
        let key = enc.get_key(&None)?;
        let key = Send::get_key(&send.key, key)?;

        let encrypted = buffer.encrypt_with_key(&key)?;
        Ok(encrypted.to_buffer()?)
    }
}

pub trait ClientSendsExt<'a> {
    fn sends(&'a self) -> ClientSends<'a>;
}

impl<'a> ClientSendsExt<'a> for Client {
    fn sends(&'a self) -> ClientSends<'a> {
        ClientSends::new(self)
    }
}