second-brain-core 0.13.3

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
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;

/// The raw 32-byte key SQLCipher encrypts one device's `brain.db` under.
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()
    }

    /// The key as SQLCipher's raw-key literal, `x'<hex>'`, which it uses
    /// as-is with no passphrase derivation.
    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(..)")
    }
}

/// Installs `key` age-encrypted to this device at `dir/name`, replacing
/// any file there, synced before it returns.
pub fn write_store_key(dir: &Path, name: &str, key: &StoreKey, device: &DeviceKeys) -> Result<()> {
    write_store_key_to(dir, name, key, &[device])
}

/// Like `write_store_key`, with the key opening for any of `devices`.
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)
}

/// None when the file is absent.
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()))
}

/// The store key that opens `db`: `store.key`, or else `store.key.next`
/// left by a rotation that renamed its copy into place but stopped before
/// promoting its key, which is then promoted.
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,
    /// This device holds no epoch of its identity that may wrap at rest
    /// (epoch 0 never does), so no backup of it can be opened by `K_id`.
    NoIdentity,
}

/// Keeps `store.key.wrapped` sealed under the newest epoch of this
/// device's identity, holding that epoch's recovery envelopes, so a
/// data-dir backup opens with the phrase or another device's `K_id`.
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)
}

/// The store key of a data-dir backup, opened through the recovery phrase:
/// the phrase opens `K_id` from an envelope beside the wrapped key. None
/// when the backup holds no `store.key.wrapped`.
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)
}

/// The store key of a data-dir backup, opened with an epoch of `K_id`
/// another device handed over or this device held. None when the backup
/// holds no `store.key.wrapped`.
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("."),
    }
}

/// The store's file, refused unless it sits in `data_dir`: a copy elsewhere
/// keeps its key files beside it, so writing them into `data_dir` would
/// leave `data_dir`'s wrap opening no store there.
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)
}

/// True when the store's file sits in `data_dir`, so its key files are
/// that dir's to write.
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()
}