use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError, Weak};
use anyhow::Result;
use zeroize::Zeroizing;
use super::{ArchiveKeys, Kek, KekLabel};
use crate::keys::{DeviceKeys, Envelope, Subkey, derive_subkey, newest_epoch};
use crate::sqlite_store::SqliteStore;
pub struct EnvelopeArchiveKeys {
store: Weak<SqliteStore>,
device: Option<DeviceKeys>,
derived: Mutex<HashMap<KekLabel, Zeroizing<[u8; 32]>>>,
}
fn label_of(envelope: &Envelope) -> KekLabel {
KekLabel {
epoch: envelope.epoch.number,
key_id: envelope.epoch.key_id,
}
}
impl EnvelopeArchiveKeys {
pub fn new(store: &Arc<SqliteStore>, device: Option<DeviceKeys>) -> Self {
Self {
store: Arc::downgrade(store),
device,
derived: Mutex::new(HashMap::new()),
}
}
fn envelopes(&self) -> Result<Option<(Arc<SqliteStore>, Vec<Envelope>)>> {
let (Some(store), Some(device)) = (self.store.upgrade(), &self.device) else {
return Ok(None);
};
let recipient = device.age_recipient().to_string();
let mut envelopes = store.envelopes_for_machine(store.machine_id())?;
envelopes.retain(|e| e.recipient == recipient);
Ok(Some((store, envelopes)))
}
fn cached(&self, label: &KekLabel) -> Option<Zeroizing<[u8; 32]>> {
self.derived
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(label)
.cloned()
}
fn derive(&self, envelope: &Envelope) -> Result<Option<Zeroizing<[u8; 32]>>> {
let Some(device) = &self.device else {
return Ok(None);
};
let label = label_of(envelope);
let mut derived = self.derived.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(key) = derived.get(&label) {
return Ok(Some(key.clone()));
}
let identity = envelope.open(device.age_identity())?;
let subkey = derive_subkey(&identity, &envelope.epoch, Subkey::ArchiveKek)?;
let key = Zeroizing::new(*subkey.expose());
derived.insert(label, key.clone());
Ok(Some(key))
}
}
impl ArchiveKeys for EnvelopeArchiveKeys {
fn current(&self) -> Result<Option<Kek>> {
let (Some(store), Some(device)) = (self.store.upgrade(), &self.device) else {
return Ok(None);
};
let recipient = device.age_recipient().to_string();
let Some((_, envelopes)) = store.own_identity_envelopes(&recipient)? else {
return Ok(None);
};
let mut sealable: Vec<Envelope> = envelopes
.into_iter()
.filter(|e| e.epoch.number > 0)
.collect();
while let Some(epoch) = newest_epoch(sealable.iter().map(|e| &e.epoch)) {
let at = sealable
.iter()
.position(|e| e.epoch == epoch)
.expect("newest_epoch picks from the list");
let envelope = sealable.swap_remove(at);
match self.derive(&envelope) {
Ok(key) => {
return Ok(key.map(|key| Kek {
label: label_of(&envelope),
key,
}));
}
Err(e) => {
tracing::warn!("skipping an envelope this device cannot open: {e:#}")
}
}
}
Ok(None)
}
fn by_label(&self, label: &KekLabel) -> Result<Option<Zeroizing<[u8; 32]>>> {
if let Some(key) = self.cached(label) {
return Ok(Some(key));
}
let Some((_, envelopes)) = self.envelopes()? else {
return Ok(None);
};
match envelopes.iter().find(|e| label_of(e) == *label) {
Some(envelope) => self.derive(envelope),
None => Ok(None),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::keys::{DeviceRecord, IdentityKey, IdentityTag, KeyGuard, wrap};
fn store() -> Arc<SqliteStore> {
Arc::new(SqliteStore::in_memory("local".to_string()).unwrap())
}
fn identity(byte: u8) -> IdentityKey {
IdentityKey::from_bytes(KeyGuard::new([byte; 32]))
}
fn tag() -> IdentityTag {
IdentityTag::migrated(&identity(9).key_id())
}
fn own_record(store: &SqliteStore, device: &DeviceKeys, tag: &IdentityTag) {
let record = DeviceRecord {
machine_id: "local".into(),
identity_tag: tag.as_str().to_string(),
version: 1,
name: "local".into(),
age_recipient: device.age_recipient().to_string(),
ssh_public_key: device.ssh_public_key(),
signing_public_key: device.verifying_key().to_bytes(),
recovery_fingerprints: Vec::new(),
ssh_login: None,
ssh_port: None,
ssh_host_key: None,
};
store
.insert_first_device_record(
&record.sign("local", device.signing_key()),
"local",
&device.verifying_key(),
)
.unwrap();
}
fn give(store: &SqliteStore, device: &DeviceKeys, key: &IdentityKey, epoch: u32) {
give_as(store, device, key, epoch, &tag());
}
fn give_as(
store: &SqliteStore,
device: &DeviceKeys,
key: &IdentityKey,
epoch: u32,
tag: &IdentityTag,
) {
let envelope = wrap(
key,
epoch,
tag,
&device.age_recipient(),
Some("local".into()),
)
.unwrap();
store.put_envelope(&envelope).unwrap();
}
fn expected(key: &IdentityKey, epoch: u32) -> [u8; 32] {
let epoch = crate::keys::Epoch {
number: epoch,
key_id: key.key_id(),
};
*derive_subkey(key, &epoch, Subkey::ArchiveKek)
.unwrap()
.expose()
}
#[test]
fn segments_seal_under_the_newest_epoch_and_older_ones_still_open() {
let s = store();
let device = DeviceKeys::generate().unwrap();
let (one, two) = (identity(1), identity(2));
give(&s, &device, &one, 1);
give(&s, &device, &two, 2);
own_record(&s, &device, &tag());
let keys = EnvelopeArchiveKeys::new(&s, Some(device));
let current = keys.current().unwrap().unwrap();
assert_eq!(current.label.epoch, 2);
assert_eq!(current.label.key_id, two.key_id());
assert_eq!(*current.key, expected(&two, 2));
let older = KekLabel {
epoch: 1,
key_id: one.key_id(),
};
assert_eq!(*keys.by_label(&older).unwrap().unwrap(), expected(&one, 1));
}
#[test]
fn an_envelope_wrapped_to_another_device_key_is_not_used() {
let s = store();
let device = DeviceKeys::generate().unwrap();
let other = DeviceKeys::generate().unwrap();
let (mine, theirs) = (identity(1), identity(3));
give(&s, &device, &mine, 1);
give(&s, &other, &theirs, 3);
own_record(&s, &device, &tag());
let keys = EnvelopeArchiveKeys::new(&s, Some(device));
let current = keys.current().unwrap().unwrap();
assert_eq!(current.label.epoch, 1);
let label = KekLabel {
epoch: 3,
key_id: theirs.key_id(),
};
assert!(keys.by_label(&label).unwrap().is_none());
}
#[test]
fn epoch_zero_never_seals() {
let s = store();
let device = DeviceKeys::generate().unwrap();
give(&s, &device, &identity(1), 0);
own_record(&s, &device, &tag());
let keys = EnvelopeArchiveKeys::new(&s, Some(device));
assert!(keys.current().unwrap().is_none());
}
#[test]
fn without_an_envelope_or_device_keys_nothing_seals() {
let s = store();
assert!(
EnvelopeArchiveKeys::new(&s, Some(DeviceKeys::generate().unwrap()))
.current()
.unwrap()
.is_none()
);
let device = DeviceKeys::generate().unwrap();
give(&s, &device, &identity(1), 1);
let keys = EnvelopeArchiveKeys::new(&s, None);
assert!(keys.current().unwrap().is_none());
let label = KekLabel {
epoch: 1,
key_id: identity(1).key_id(),
};
assert!(keys.by_label(&label).unwrap().is_none());
}
#[test]
fn an_epoch_this_device_holds_no_envelope_for_is_absent() {
let s = store();
let device = DeviceKeys::generate().unwrap();
give(&s, &device, &identity(1), 1);
own_record(&s, &device, &tag());
let keys = EnvelopeArchiveKeys::new(&s, Some(device));
let label = KekLabel {
epoch: 5,
key_id: identity(5).key_id(),
};
assert!(keys.by_label(&label).unwrap().is_none());
}
#[test]
fn equal_epoch_numbers_seal_under_the_lower_key_id_like_sync() {
let s = store();
let device = DeviceKeys::generate().unwrap();
let (a, b) = (identity(4), identity(5));
give(&s, &device, &a, 2);
give(&s, &device, &b, 2);
own_record(&s, &device, &tag());
let lower = a.key_id().min(b.key_id());
let keys = EnvelopeArchiveKeys::new(&s, Some(device));
assert_eq!(keys.current().unwrap().unwrap().label.key_id, lower);
}
#[test]
fn only_the_identity_of_this_devices_record_seals_but_others_still_open() {
let s = store();
let dir = tempfile::tempdir().unwrap();
let key_file = dir.path().join(crate::keys::DEVICE_KEY_FILE);
let (device, _) = DeviceKeys::load_or_create(&key_file).unwrap();
let (mine, earlier) = (identity(1), identity(6));
let other = IdentityTag::migrated(&identity(7).key_id());
give(&s, &device, &mine, 1);
give_as(&s, &device, &earlier, 3, &other);
let keys = EnvelopeArchiveKeys::new(&s, DeviceKeys::load(&key_file).unwrap());
assert!(keys.current().unwrap().is_none());
own_record(&s, &device, &tag());
let current = keys.current().unwrap().unwrap();
assert_eq!(current.label.epoch, 1);
let label = KekLabel {
epoch: 3,
key_id: earlier.key_id(),
};
assert_eq!(
*keys.by_label(&label).unwrap().unwrap(),
expected(&earlier, 3)
);
}
#[test]
fn an_envelope_that_will_not_open_is_skipped_like_sync() {
let s = store();
let device = DeviceKeys::generate().unwrap();
give(&s, &device, &identity(1), 1);
let mut broken = wrap(
&identity(2),
2,
&tag(),
&device.age_recipient(),
Some("local".into()),
)
.unwrap();
broken.ciphertext = vec![0; 64];
s.put_envelope(&broken).unwrap();
own_record(&s, &device, &tag());
let keys = EnvelopeArchiveKeys::new(&s, Some(device));
assert_eq!(keys.current().unwrap().unwrap().label.epoch, 1);
}
}