coven_crypto/custody/
mod.rs1use crate::{MaterialError, MemberIdentity, SecretBytes, StoreKeyring};
4use coven_foundation::files::StoreFile;
5use std::sync::Arc;
6
7mod app_secrets;
8#[cfg(any(target_os = "android", test, feature = "in-memory-keychain"))]
9mod block_store;
10#[cfg(target_os = "android")]
11mod block_store_jni;
12#[cfg(any(test, feature = "in-memory-keychain"))]
13mod block_store_test_utils;
14mod error;
15mod key_session;
16mod keychain;
17mod memory;
18mod passphrase;
19mod person_entries;
20mod platform;
21mod store_custody;
22
23pub use error::{KeyError, KeychainError};
24pub use key_session::KeySession;
25pub use keychain::{Keychain, KeyringCustody, StoreKeychain};
26pub use memory::InMemoryCustody;
27pub use passphrase::{Passphrase, PassphraseCustody};
28pub use person_entries::{PersonEntries, PersonRestoreCode, RestoreCodeChange};
29pub use store_custody::{IdentityError, StoreCustody};
30
31mod sealed {
32 pub trait Sealed {}
33}
34
35pub trait KeyMaterial: sealed::Sealed + Clone + Send + Sync + 'static {
38 const ENTRY: &'static str;
40 const FILE: StoreFile;
42 fn to_secret_bytes(&self) -> SecretBytes;
44 fn from_secret_bytes(bytes: &[u8]) -> Result<Self, MaterialError>;
46}
47
48impl sealed::Sealed for StoreKeyring {}
49impl sealed::Sealed for MemberIdentity {}
50
51impl KeyMaterial for StoreKeyring {
52 const ENTRY: &'static str = "store-keys";
53 const FILE: StoreFile = StoreFile::StoreKeys;
54 fn to_secret_bytes(&self) -> SecretBytes {
55 Self::to_secret_bytes(self)
56 }
57 fn from_secret_bytes(bytes: &[u8]) -> Result<Self, MaterialError> {
58 Self::from_secret_bytes(bytes)
59 }
60}
61
62impl KeyMaterial for MemberIdentity {
63 const ENTRY: &'static str = "member-keys";
64 const FILE: StoreFile = StoreFile::MemberKeys;
65 fn to_secret_bytes(&self) -> SecretBytes {
66 Self::to_secret_bytes(self)
67 }
68 fn from_secret_bytes(bytes: &[u8]) -> Result<Self, MaterialError> {
69 Self::from_secret_bytes(bytes)
70 }
71}
72
73pub trait Custody<T: KeyMaterial>: Send + Sync {
75 fn unlock(&self) -> Result<Option<T>, KeyError>;
78 fn persist(&self, material: &T) -> Result<(), KeyError>;
80 fn forget(&self) -> Result<(), KeyError>;
82 fn close(&self);
84}
85
86pub enum CustodyChoice<T: KeyMaterial> {
88 Keyring,
90 Passphrase(Passphrase),
92 InMemory,
95 Custom(Arc<dyn Custody<T>>),
97}
98
99impl<T: KeyMaterial> std::fmt::Debug for CustodyChoice<T> {
100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101 f.write_str(match self {
102 Self::Keyring => "CustodyChoice::Keyring",
103 Self::Passphrase(_) => "CustodyChoice::Passphrase([REDACTED])",
104 Self::InMemory => "CustodyChoice::InMemory",
105 Self::Custom(_) => "CustodyChoice::Custom(..)",
106 })
107 }
108}
109
110impl<T: KeyMaterial> Custody<T> for InMemoryCustody<T> {
111 fn unlock(&self) -> Result<Option<T>, KeyError> {
112 Ok(self.read())
113 }
114 fn persist(&self, material: &T) -> Result<(), KeyError> {
115 self.write(material);
116 Ok(())
117 }
118 fn forget(&self) -> Result<(), KeyError> {
119 self.remove();
120 Ok(())
121 }
122 fn close(&self) {
123 self.remove();
124 }
125}
126
127impl<T: KeyMaterial> Custody<T> for PassphraseCustody<T> {
128 fn unlock(&self) -> Result<Option<T>, KeyError> {
129 self.read(T::ENTRY)?
130 .map(|bytes| T::from_secret_bytes(bytes.as_bytes()).map_err(KeyError::from))
131 .transpose()
132 }
133 fn persist(&self, material: &T) -> Result<(), KeyError> {
134 self.write(T::ENTRY, material.to_secret_bytes().as_bytes())
135 }
136 fn forget(&self) -> Result<(), KeyError> {
137 self.remove()
138 }
139 fn close(&self) {
140 self.release();
141 }
142}
143
144impl<T: KeyMaterial> Custody<T> for KeyringCustody<T> {
145 fn unlock(&self) -> Result<Option<T>, KeyError> {
146 self.read(T::ENTRY)?
147 .map(|bytes| T::from_secret_bytes(bytes.as_bytes()).map_err(KeyError::from))
148 .transpose()
149 }
150 fn persist(&self, material: &T) -> Result<(), KeyError> {
151 self.write(T::ENTRY, material.to_secret_bytes().as_bytes())
152 }
153 fn forget(&self) -> Result<(), KeyError> {
154 self.remove(T::ENTRY)
155 }
156 fn close(&self) {}
157}
158
159#[cfg(test)]
160#[path = "mod_tests.rs"]
161mod tests;