nibkeep_node/library/protection.rs
1//! Protecting a library's keys and its secrets anew, after they were set up: with a new
2//! passphrase, or with a new key in the platform's keystore
3//! ([Key file](../../../../docs/spec/keys.md)). The library's passphrase and its secrets'
4//! stay apart, so that unlocking the library never unlocks its secrets.
5
6use nibkeep_crypto::rand_core::CryptoRng;
7use nibkeep_keys::{KeysError, NewProtection, SealedSecrets};
8
9use super::Library;
10use super::secrets::Compartment;
11use crate::NodeError;
12
13impl Library {
14 /// Protects the library's keys another way: with a new passphrase, or with a new key in
15 /// the platform's keystore. The keystore key of the old protection, if it had one, is
16 /// deleted once the key file is written. The library stays open, and closes and opens
17 /// again with the new protection.
18 ///
19 /// # Errors
20 ///
21 /// Returns [`NodeError::LibraryPassphrase`] if the passphrase unlocks the library's
22 /// secrets; an error if the passphrase cannot be used or the keystore fails, or if the
23 /// key file cannot be read or written, in which case nothing changed.
24 pub fn protect(
25 &mut self,
26 protection: &NewProtection<'_>,
27 rng: &mut impl CryptoRng,
28 ) -> Result<(), NodeError> {
29 if self.opens_the_other(protection, Compartment::Secrets)? {
30 return Err(NodeError::LibraryPassphrase);
31 }
32 let compartment = self.compartment.clone();
33 let mut protected = None;
34 self.update_key_file(|key_file| {
35 protected = Some(key_file.protect_replica(&compartment, protection, rng)?);
36 Ok(())
37 })?;
38 if let Some(compartment) = protected {
39 self.compartment = compartment;
40 }
41 Ok(())
42 }
43
44 /// Protects the library's secrets another way: with a new passphrase, or with a new key
45 /// in the platform's keystore, while they are unlocked, which they stay. They are sealed
46 /// before the key file is locked, as deriving a key from a passphrase, or a keystore's
47 /// prompt, takes a while; secrets keys that another program gave meanwhile are kept. The
48 /// keystore key of the old protection, if it had one, is deleted once the key file is
49 /// written.
50 ///
51 /// # Errors
52 ///
53 /// Returns [`NodeError::SecretsLocked`] if the secrets are locked; an error of the keys,
54 /// [`KeysError::NoSecrets`], if there are none; [`NodeError::LibraryPassphrase`] if the
55 /// passphrase is the library's; an error if this replica is an AI agent's; an error if
56 /// the passphrase cannot be used or the keystore fails, or if the key file cannot be read
57 /// or written, in which case nothing changed.
58 pub fn protect_secrets(
59 &mut self,
60 protection: &NewProtection<'_>,
61 rng: &mut impl CryptoRng,
62 ) -> Result<(), NodeError> {
63 self.check_not_ai_agent("hold secrets")?;
64 let Some(secrets) = self.secrets_compartment.clone() else {
65 return Err(if self.secrets_protection()?.is_some() {
66 NodeError::SecretsLocked
67 } else {
68 NodeError::Keys(KeysError::NoSecrets)
69 });
70 };
71 if self.opens_the_other(protection, Compartment::Replica)? {
72 return Err(NodeError::LibraryPassphrase);
73 }
74 let keys = self.key_file_on_disk()?.secrets(&secrets)?;
75 let (mut sealed, protected) = SealedSecrets::seal(&keys, protection, rng)?;
76 self.update_key_file(|key_file| {
77 // The secrets keys as the file on the disk holds them now.
78 let keys = key_file.secrets(&secrets)?;
79 sealed.update(&keys, &protected, rng)?;
80 key_file.put_secrets(sealed);
81 Ok(())
82 })?;
83 self.secrets_compartment = Some(protected);
84 Ok(())
85 }
86
87 /// Forgets the secrets compartment, whose key the keystore lost, as Android destroys
88 /// keys that need the person's confirmation once the screen lock is removed: it opens
89 /// never again. The library can then ask another replica for the secrets, as a new one
90 /// does; where none holds them, they are lost, and new ones can be set up.
91 ///
92 /// # Errors
93 ///
94 /// Returns [`NodeError::SecretsKeyKept`] if the secrets can still be unlocked: the
95 /// keystore has their key, or a passphrase protects them; an error of the keys,
96 /// [`KeysError::NoSecrets`], if there are none; an error if the keystore fails, or the
97 /// key file cannot be read or written, in which case nothing changed.
98 pub fn forget_lost_secrets(&mut self) -> Result<(), NodeError> {
99 self.update_key_file(|key_file| {
100 if key_file.secrets_protection().is_none() {
101 return Err(NodeError::Keys(KeysError::NoSecrets));
102 }
103 if !key_file.secrets_key_lost()? {
104 return Err(NodeError::SecretsKeyKept);
105 }
106 key_file.remove_secrets();
107 Ok(())
108 })?;
109 self.lock_secrets();
110 Ok(())
111 }
112}