bitwarden_ipc/traits/
crypto_provider.rs

1use std::fmt::Debug;
2
3use super::{CommunicationBackend, CommunicationBackendReceiver, SessionRepository};
4use crate::message::{IncomingMessage, OutgoingMessage};
5
6pub trait CryptoProvider<Com, Ses>: Send + Sync + 'static
7where
8    Com: CommunicationBackend,
9    Ses: SessionRepository<Self::Session>,
10{
11    type Session: Send + Sync + 'static;
12    type SendError: Debug + Send + Sync + 'static;
13    type ReceiveError: Debug + Send + Sync + 'static;
14
15    /// Send a message.
16    ///
17    /// Calling this function may result in multiple messages being sent, depending on the
18    /// implementation of the trait. For example, if the destination does not have a
19    /// session, the function may first send a message to establish a session and then send the
20    /// original message. The implementation of this function should handle this logic.
21    ///
22    /// An error should only be returned for fatal and unrecoverable errors e.g. if the session
23    /// storage is full or cannot be accessed. Returning an error will cause the IPC client to
24    /// stop processing messages.
25    fn send(
26        &self,
27        communication: &Com,
28        sessions: &Ses,
29        message: OutgoingMessage,
30    ) -> impl std::future::Future<Output = Result<(), Self::SendError>> + Send;
31
32    /// Receive a message.
33    ///
34    /// Calling this function may also result in messages being sent, depending on the trait
35    /// implementation. For example, if an encrypted message is received from a destination that
36    /// does not have a session. The function may then try to establish a session and then
37    /// re-request the original message. The implementation of this function should handle this
38    /// logic.
39    ///
40    /// An error should only be returned for fatal and unrecoverable errors e.g. if the session
41    /// storage is full or cannot be accessed. Returning an error will cause the IPC client to
42    /// stop processing messages.
43    fn receive(
44        &self,
45        receiver: &Com::Receiver,
46        communication: &Com,
47        sessions: &Ses,
48    ) -> impl std::future::Future<Output = Result<IncomingMessage, Self::ReceiveError>> + Send + Sync;
49}
50
51pub struct NoEncryptionCryptoProvider;
52
53impl<Com, Ses> CryptoProvider<Com, Ses> for NoEncryptionCryptoProvider
54where
55    Com: CommunicationBackend,
56    Ses: SessionRepository<()>,
57{
58    type Session = ();
59    type SendError = Com::SendError;
60    type ReceiveError = <Com::Receiver as CommunicationBackendReceiver>::ReceiveError;
61
62    async fn send(
63        &self,
64        communication: &Com,
65        _sessions: &Ses,
66        message: OutgoingMessage,
67    ) -> Result<(), Self::SendError> {
68        communication.send(message).await
69    }
70
71    async fn receive(
72        &self,
73        receiver: &Com::Receiver,
74        _communication: &Com,
75        _sessions: &Ses,
76    ) -> Result<IncomingMessage, Self::ReceiveError> {
77        receiver.receive().await
78    }
79}