use std::collections::BTreeSet;
use std::fmt;
use std::fs;
use std::io::{ErrorKind, Read, Write};
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, anyhow, bail, ensure};
use chacha20poly1305::aead::{Aead, KeyInit, Payload};
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use rand::TryRngCore;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;
use super::device::{create_private_dir, sync_dir, write_private_file};
use super::identity::{Envelope, Epoch, IdentityKey, IdentityTag, newest_epoch};
use super::subkey::{Subkey, derive_subkey};
use super::{DeviceKeys, HeldEpoch, KeyGuard, RecoveryPhrase, hex};
use crate::sqlite_store::SqliteStore;
use crate::sqlite_store::encrypt_migration::key_opens_file;
const KEY_LEN: usize = 32;
pub const STORE_KEY_FILE: &str = "store.key";
pub const NEXT_STORE_KEY_FILE: &str = "store.key.next";
pub const WRAPPED_STORE_KEY_FILE: &str = "store.key.wrapped";
const WRAPPED_VERSION: u32 = 1;
const WRAP_AAD_DOMAIN: &[u8] = b"second-brain store.key.wrapped v1";
const NONCE_LEN: usize = 24;
pub struct StoreKey(KeyGuard<[u8; KEY_LEN]>);
impl StoreKey {
pub fn generate() -> Result<Self> {
Ok(Self(KeyGuard::random()?))
}
pub fn from_bytes(bytes: KeyGuard<[u8; KEY_LEN]>) -> Self {
Self(bytes)
}
pub fn expose(&self) -> &[u8; KEY_LEN] {
self.0.expose()
}
pub(crate) fn sql_literal(&self) -> Zeroizing<String> {
let hex = Zeroizing::new(hex(self.expose()));
Zeroizing::new(format!("x'{}'", hex.as_str()))
}
pub(crate) fn duplicate(&self) -> Self {
let mut bytes = KeyGuard::new([0u8; KEY_LEN]);
bytes.expose_mut().copy_from_slice(self.expose());
Self(bytes)
}
fn from_slice(bytes: &[u8]) -> Result<Self> {
ensure!(
bytes.len() == KEY_LEN,
"a store key holds {KEY_LEN} bytes, not {}",
bytes.len()
);
let mut key = KeyGuard::new([0u8; KEY_LEN]);
key.expose_mut().copy_from_slice(bytes);
Ok(Self(key))
}
}
impl fmt::Debug for StoreKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("StoreKey(..)")
}
}
pub fn write_store_key(dir: &Path, name: &str, key: &StoreKey, device: &DeviceKeys) -> Result<()> {
write_store_key_to(dir, name, key, &[device])
}
pub(super) fn write_store_key_to(
dir: &Path,
name: &str,
key: &StoreKey,
devices: &[&DeviceKeys],
) -> Result<()> {
let recipients: Vec<age::x25519::Recipient> =
devices.iter().map(|d| d.age_recipient()).collect();
let encryptor =
age::Encryptor::with_recipients(recipients.iter().map(|r| r as &dyn age::Recipient))
.map_err(|e| anyhow!("sealing the store key to this device: {e}"))?;
let mut sealed = Vec::new();
let mut writer = encryptor
.wrap_output(&mut sealed)
.context("sealing the store key to this device")?;
writer
.write_all(key.expose())
.and_then(|()| writer.finish().map(drop))
.context("sealing the store key to this device")?;
replace_private_file(dir, name, &sealed)
}
pub fn read_store_key(path: &Path, device: &DeviceKeys) -> Result<Option<StoreKey>> {
let sealed = match fs::read(path) {
Ok(bytes) => bytes,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
};
let decryptor = age::Decryptor::new_buffered(sealed.as_slice())
.map_err(|e| anyhow!("{} is not a sealed store key: {e}", path.display()))?;
let mut reader = decryptor
.decrypt(std::iter::once(device.age_identity() as &dyn age::Identity))
.map_err(|e| anyhow!("this device's key does not open {}: {e}", path.display()))?;
let mut plaintext = Zeroizing::new(Vec::with_capacity(KEY_LEN));
reader
.read_to_end(&mut plaintext)
.with_context(|| format!("reading {}", path.display()))?;
StoreKey::from_slice(&plaintext)
.map(Some)
.with_context(|| format!("{} is corrupt", path.display()))
}
pub fn store_key_for(db: &Path, device: &DeviceKeys) -> Result<StoreKey> {
let dir = dir_of(db);
let current = read_store_key(&dir.join(STORE_KEY_FILE), device)?;
let next_path = dir.join(NEXT_STORE_KEY_FILE);
if let Some(key) = current
&& key_opens_file(db, &key)
{
remove_if_present(&next_path)?;
return Ok(key);
}
if let Some(next) = read_store_key(&next_path, device)?
&& key_opens_file(db, &next)
{
fs::rename(&next_path, dir.join(STORE_KEY_FILE))
.with_context(|| format!("promoting {}", next_path.display()))?;
sync_dir(dir)?;
return Ok(next);
}
bail!(
"no store key in {} opens the encrypted store {}; restore the data dir from a \
backup with `sb restore`",
dir.display(),
db.display()
)
}
#[derive(Serialize, Deserialize)]
struct WrappedFile {
version: u32,
identity_tag: IdentityTag,
epoch: Epoch,
nonce: String,
sealed: String,
recovery: Vec<Envelope>,
}
impl WrappedFile {
fn aad(tag: &IdentityTag, epoch: &Epoch) -> Vec<u8> {
let mut aad = WRAP_AAD_DOMAIN.to_vec();
aad.extend_from_slice(tag.as_str().as_bytes());
aad.push(0);
aad.extend_from_slice(&epoch.number.to_be_bytes());
aad.extend_from_slice(&epoch.key_id.0);
aad
}
fn seal(
key: &StoreKey,
tag: &IdentityTag,
epoch: &Epoch,
identity: &IdentityKey,
recovery: Vec<Envelope>,
) -> Result<Self> {
let wrap_key = derive_subkey(identity, epoch, Subkey::StoreWrap)?;
let cipher = XChaCha20Poly1305::new(wrap_key.expose().into());
let mut nonce = [0u8; NONCE_LEN];
OsRng
.try_fill_bytes(&mut nonce)
.map_err(|e| anyhow!("reading OS randomness: {e}"))?;
let aad = Self::aad(tag, epoch);
let sealed = cipher
.encrypt(
XNonce::from_slice(&nonce),
Payload {
msg: key.expose(),
aad: &aad,
},
)
.map_err(|_| anyhow!("sealing the store key"))?;
Ok(Self {
version: WRAPPED_VERSION,
identity_tag: tag.clone(),
epoch: *epoch,
nonce: hex(&nonce),
sealed: hex(&sealed),
recovery,
})
}
fn open(&self, identity: &IdentityKey) -> Result<StoreKey> {
ensure!(
self.version == WRAPPED_VERSION,
"{WRAPPED_STORE_KEY_FILE} has version {}, not {WRAPPED_VERSION}",
self.version
);
let wrap_key = derive_subkey(identity, &self.epoch, Subkey::StoreWrap)?;
let cipher = XChaCha20Poly1305::new(wrap_key.expose().into());
let nonce = unhex_vec(&self.nonce)?;
ensure!(
nonce.len() == NONCE_LEN,
"{WRAPPED_STORE_KEY_FILE} has a bad nonce"
);
let aad = Self::aad(&self.identity_tag, &self.epoch);
let plaintext = Zeroizing::new(
cipher
.decrypt(
XNonce::from_slice(&nonce),
Payload {
msg: &unhex_vec(&self.sealed)?,
aad: &aad,
},
)
.map_err(|_| anyhow!("the identity key does not open {WRAPPED_STORE_KEY_FILE}"))?,
);
StoreKey::from_slice(&plaintext)
}
fn recipients(&self) -> BTreeSet<&str> {
self.recovery.iter().map(|e| e.recipient.as_str()).collect()
}
fn read(dir: &Path) -> Result<Option<Self>> {
let path = dir.join(WRAPPED_STORE_KEY_FILE);
let text = match fs::read_to_string(&path) {
Ok(text) => text,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
};
serde_json::from_str(&text)
.map(Some)
.with_context(|| format!("{} is corrupt", path.display()))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WrapRefresh {
Written,
Current,
NoIdentity,
}
pub fn refresh_wrapped_store_key(
store: &SqliteStore,
data_dir: &Path,
device: &DeviceKeys,
key: &StoreKey,
) -> Result<WrapRefresh> {
store_file_in(store, data_dir)?;
let recipient = device.age_recipient().to_string();
let Some((tag, own)) = store.own_identity_envelopes(&recipient)? else {
return Ok(WrapRefresh::NoIdentity);
};
let usable: Vec<&Envelope> = own.iter().filter(|e| e.epoch.number >= 1).collect();
let Some(epoch) = newest_epoch(usable.iter().map(|e| &e.epoch)) else {
return Ok(WrapRefresh::NoIdentity);
};
let envelope = usable
.iter()
.find(|e| e.epoch == epoch)
.expect("the newest epoch comes from these envelopes");
let identity = envelope.open(device.age_identity())?;
let recovery: Vec<Envelope> = store
.envelopes_for_epoch(&tag, &epoch)?
.into_iter()
.filter(|e| e.machine_id.is_none())
.collect();
if let Some(existing) = WrappedFile::read(data_dir)?
&& existing.identity_tag == tag
&& existing.epoch == epoch
&& existing.recipients() == recovery.iter().map(|e| e.recipient.as_str()).collect()
&& existing
.open(&identity)
.is_ok_and(|k| k.expose() == key.expose())
{
return Ok(WrapRefresh::Current);
}
let file = WrappedFile::seal(key, &tag, &epoch, &identity, recovery)?;
let text = serde_json::to_vec_pretty(&file)?;
replace_private_file(data_dir, WRAPPED_STORE_KEY_FILE, &text)?;
Ok(WrapRefresh::Written)
}
pub fn unwrap_store_key_with_phrase(
backup_dir: &Path,
phrase: &RecoveryPhrase,
) -> Result<Option<StoreKey>> {
let Some(file) = WrappedFile::read(backup_dir)? else {
return Ok(None);
};
let identity = phrase.identity()?;
let recipient = phrase.recipient()?.to_string();
let envelope = file
.recovery
.iter()
.find(|e| {
e.recipient == recipient && e.identity_tag == file.identity_tag && e.epoch == file.epoch
})
.ok_or_else(|| {
anyhow!(
"this recovery phrase is not among the recipients of the backup's \
{WRAPPED_STORE_KEY_FILE}; check the phrase, or restore a backup of a device \
that trusted it"
)
})?;
let key = envelope.open(&identity)?;
file.open(&key).map(Some)
}
pub fn unwrap_store_key_with_epochs(
backup_dir: &Path,
held: &[HeldEpoch],
) -> Result<Option<StoreKey>> {
let Some(file) = WrappedFile::read(backup_dir)? else {
return Ok(None);
};
let held = held
.iter()
.find(|h| h.identity == file.identity_tag && h.epoch == file.epoch)
.ok_or_else(|| {
anyhow!(
"the backup's {WRAPPED_STORE_KEY_FILE} is sealed under epoch {} of identity {}, \
which none of the keys at hand is; use keys from a device that held it",
file.epoch.number,
file.identity_tag.as_str()
)
})?;
file.open(&held.key).map(Some)
}
pub(super) fn replace_private_file(dir: &Path, name: &str, bytes: &[u8]) -> Result<()> {
create_private_dir(dir)?;
let temp = dir.join(format!(".{name}.{}.tmp", uuid::Uuid::new_v4().simple()));
if let Err(e) = write_private_file(&temp, bytes) {
return Err(with_temp_removed(e, &temp));
}
let target = dir.join(name);
if let Err(e) = fs::rename(&temp, &target) {
let e = anyhow::Error::from(e).context(format!("installing {}", target.display()));
return Err(with_temp_removed(e, &temp));
}
sync_dir(dir)
}
fn with_temp_removed(error: anyhow::Error, temp: &Path) -> anyhow::Error {
match remove_if_present(temp) {
Ok(()) => error,
Err(cleanup) => error.context(format!("{cleanup:#}")),
}
}
pub(super) fn remove_if_present(path: &Path) -> Result<()> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
Err(e) => Err(e).with_context(|| format!("removing {}", path.display())),
}
}
pub(super) fn dir_of(path: &Path) -> &Path {
match path.parent() {
Some(dir) if !dir.as_os_str().is_empty() => dir,
_ => Path::new("."),
}
}
pub(super) fn store_file_in(store: &SqliteStore, data_dir: &Path) -> Result<PathBuf> {
let db = store.file_path()?.ok_or_else(|| {
anyhow!(
"the store has no file whose key files {} holds",
data_dir.display()
)
})?;
ensure!(
same_dir(dir_of(&db), data_dir),
"{} keeps its key files beside it, not in {}",
db.display(),
data_dir.display()
);
Ok(db)
}
pub fn store_in_data_dir(store: &SqliteStore, data_dir: &Path) -> bool {
store_file_in(store, data_dir).is_ok()
}
pub(super) fn same_dir(a: &Path, b: &Path) -> bool {
let resolve = |p: &Path| fs::canonicalize(p).unwrap_or_else(|_| PathBuf::from(p));
resolve(a) == resolve(b)
}
fn unhex_vec(text: &str) -> Result<Vec<u8>> {
ensure!(
text.len() % 2 == 0,
"odd-length hex in {WRAPPED_STORE_KEY_FILE}"
);
(0..text.len())
.step_by(2)
.map(|i| {
u8::from_str_radix(&text[i..i + 2], 16)
.map_err(|_| anyhow!("bad hex in {WRAPPED_STORE_KEY_FILE}"))
})
.collect()
}