second-brain-sync 0.13.0

Bidirectional sync for second-brain: SSH transport, JSONL change log, conflict resolution
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::fs;
use std::io::Write;
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail, ensure};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as B64;
use chacha20poly1305::aead::{Aead, KeyInit, OsRng, Payload};
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use rand_core::RngCore;
use second_brain_core::keys::{Epoch, IdentityKey, KeyGuard, KeyId, Subkey, derive_subkey};

const KEY_LEN: usize = 32;
const NONCE_LEN: usize = 24;

const MAGIC_PLAINTEXT: u8 = 0x00;
const MAGIC_XCHACHA: u8 = 0x01;
const MAGIC_EPOCH: u8 = 0x02;
const EPOCH_PREFIX_LEN: usize = 1 + 4 + 8;

pub trait SyncEncryptor: Send + Sync {
    fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>>;
    fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>;
}

pub struct PassthroughEncryptor;

impl SyncEncryptor for PassthroughEncryptor {
    fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>> {
        Ok(plaintext.to_vec())
    }

    fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
        Ok(ciphertext.to_vec())
    }
}

pub struct XChaChaEncryptor {
    key: [u8; KEY_LEN],
}

impl XChaChaEncryptor {
    pub fn new(key: [u8; KEY_LEN]) -> Self {
        Self { key }
    }

    pub fn key_base64(&self) -> String {
        B64.encode(self.key)
    }

    fn cipher(&self) -> XChaCha20Poly1305 {
        XChaCha20Poly1305::new((&self.key).into())
    }
}

impl SyncEncryptor for XChaChaEncryptor {
    fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>> {
        let mut nonce_bytes = [0u8; NONCE_LEN];
        OsRng.fill_bytes(&mut nonce_bytes);
        let nonce = XNonce::from_slice(&nonce_bytes);

        let ct = self
            .cipher()
            .encrypt(nonce, plaintext)
            .map_err(|e| anyhow!("xchacha20poly1305 encrypt failed: {e}"))?;

        let mut framed = Vec::with_capacity(1 + NONCE_LEN + ct.len());
        framed.push(MAGIC_XCHACHA);
        framed.extend_from_slice(&nonce_bytes);
        framed.extend_from_slice(&ct);
        Ok(framed)
    }

    fn decrypt(&self, framed: &[u8]) -> Result<Vec<u8>> {
        match framed.first() {
            None => bail!("empty sync payload"),
            Some(&MAGIC_PLAINTEXT) => bail!("refusing an unencrypted sync payload"),
            Some(&MAGIC_XCHACHA) => {
                if framed.len() < 1 + NONCE_LEN {
                    bail!("encrypted payload too short to contain a nonce");
                }
                let nonce = XNonce::from_slice(&framed[1..1 + NONCE_LEN]);
                let ct = &framed[1 + NONCE_LEN..];
                self.cipher().decrypt(nonce, ct).map_err(|e| {
                    anyhow!("xchacha20poly1305 decrypt failed (bad key or tampered payload): {e}")
                })
            }
            Some(other) => bail!("unknown sync payload magic byte: {other:#x}"),
        }
    }
}

/// The wire subkey of one identity key epoch.
pub struct WireKey {
    epoch: Epoch,
    key: KeyGuard<[u8; KEY_LEN]>,
}

impl WireKey {
    pub fn derive(identity: &IdentityKey, epoch: &Epoch) -> Result<Self> {
        Ok(Self {
            epoch: *epoch,
            key: derive_subkey(identity, epoch, Subkey::Wire)?,
        })
    }

    pub fn epoch(&self) -> &Epoch {
        &self.epoch
    }

    fn cipher(&self) -> XChaCha20Poly1305 {
        XChaCha20Poly1305::new(self.key.expose().into())
    }
}

/// Seals under one epoch and opens frames of any epoch it holds. Each frame
/// names its epoch and key id, which the AEAD binds as associated data.
pub struct EpochSealer {
    keys: Vec<WireKey>,
    seal_with: Option<Epoch>,
}

impl EpochSealer {
    /// `seal_with` must be one of `keys`' epochs; None makes an opener only.
    pub fn new(keys: Vec<WireKey>, seal_with: Option<Epoch>) -> Result<Self> {
        if let Some(epoch) = &seal_with {
            ensure!(
                keys.iter().any(|k| k.epoch == *epoch),
                "no wire key held for epoch {} (key id {})",
                epoch.number,
                epoch.key_id
            );
        }
        Ok(Self { keys, seal_with })
    }

    pub fn sealing_epoch(&self) -> Option<&Epoch> {
        self.seal_with.as_ref()
    }

    fn key(&self, epoch: &Epoch) -> Option<&WireKey> {
        self.keys.iter().find(|k| k.epoch == *epoch)
    }
}

/// The epoch an epoch-sealed frame names, None for any other frame.
pub fn frame_epoch(framed: &[u8]) -> Option<Epoch> {
    let prefix = framed.get(..EPOCH_PREFIX_LEN)?;
    if prefix[0] != MAGIC_EPOCH {
        return None;
    }
    let mut number = [0u8; 4];
    number.copy_from_slice(&prefix[1..5]);
    let mut key_id = [0u8; 8];
    key_id.copy_from_slice(&prefix[5..]);
    Some(Epoch {
        number: u32::from_be_bytes(number),
        key_id: KeyId(key_id),
    })
}

/// The epoch a sealed wire line names, None when it names none.
pub fn line_epoch(line: &str) -> Option<Epoch> {
    frame_epoch(&B64.decode(line.trim()).ok()?)
}

fn epoch_prefix(epoch: &Epoch) -> [u8; EPOCH_PREFIX_LEN] {
    let mut prefix = [0u8; EPOCH_PREFIX_LEN];
    prefix[0] = MAGIC_EPOCH;
    prefix[1..5].copy_from_slice(&epoch.number.to_be_bytes());
    prefix[5..].copy_from_slice(&epoch.key_id.0);
    prefix
}

impl SyncEncryptor for EpochSealer {
    fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>> {
        let epoch = self
            .seal_with
            .as_ref()
            .context("this sync key set only opens frames; it has no epoch to seal under")?;
        let key = self
            .key(epoch)
            .context("the sealing epoch's key vanished")?;
        let prefix = epoch_prefix(epoch);
        let mut nonce_bytes = [0u8; NONCE_LEN];
        OsRng.fill_bytes(&mut nonce_bytes);
        let ct = key
            .cipher()
            .encrypt(
                XNonce::from_slice(&nonce_bytes),
                Payload {
                    msg: plaintext,
                    aad: &prefix,
                },
            )
            .map_err(|e| anyhow!("xchacha20poly1305 encrypt failed: {e}"))?;
        let mut framed = Vec::with_capacity(EPOCH_PREFIX_LEN + NONCE_LEN + ct.len());
        framed.extend_from_slice(&prefix);
        framed.extend_from_slice(&nonce_bytes);
        framed.extend_from_slice(&ct);
        Ok(framed)
    }

    fn decrypt(&self, framed: &[u8]) -> Result<Vec<u8>> {
        match framed.first() {
            None => bail!("empty sync payload"),
            Some(&MAGIC_PLAINTEXT) => bail!("refusing an unencrypted sync payload"),
            Some(&MAGIC_XCHACHA) => {
                bail!("refusing a payload sealed with a sync.key by sync protocol 3 or older")
            }
            Some(&MAGIC_EPOCH) => {
                if framed.len() < EPOCH_PREFIX_LEN + NONCE_LEN {
                    bail!("encrypted payload too short to contain its epoch and nonce");
                }
                let epoch = frame_epoch(framed).context("frame has no epoch prefix")?;
                let (prefix, rest) = framed.split_at(EPOCH_PREFIX_LEN);
                let key = self.key(&epoch).with_context(|| {
                    format!(
                        "the payload is sealed under identity key epoch {} (key id {}), which this \
                         device does not hold; if the sender belongs to another identity, pair it \
                         with `sb pair`",
                        epoch.number, epoch.key_id
                    )
                })?;
                let (nonce, ct) = rest.split_at(NONCE_LEN);
                key.cipher()
                    .decrypt(
                        XNonce::from_slice(nonce),
                        Payload {
                            msg: ct,
                            aad: prefix,
                        },
                    )
                    .map_err(|e| {
                        anyhow!(
                            "xchacha20poly1305 decrypt failed under epoch {} (tampered payload): {e}",
                            epoch.number
                        )
                    })
            }
            Some(other) => bail!("unknown sync payload magic byte: {other:#x}"),
        }
    }
}

/// Seal one plaintext JSONL record into a single base64 line safe for the
/// line-oriented SSH transport. base64 is required because the AEAD frame is
/// binary and the wire is newline-delimited text.
pub fn seal_line(enc: &dyn SyncEncryptor, plaintext: &[u8]) -> Result<String> {
    let framed = enc.encrypt(plaintext)?;
    Ok(B64.encode(framed))
}

/// Open one wire line back into the plaintext JSONL record.
pub fn open_line(enc: &dyn SyncEncryptor, line: &str) -> Result<Vec<u8>> {
    let framed = B64
        .decode(line.trim())
        .context("not an encrypted sync line (plaintext lines are refused)")?;
    enc.decrypt(&framed)
}

#[cfg(test)]
pub(crate) fn frame_format() -> String {
    format!(
        "base64({MAGIC_EPOCH:#04x} | epoch u32 be | key id[8] | nonce[{NONCE_LEN}] | \
         xchacha20poly1305 ciphertext, aad = first {EPOCH_PREFIX_LEN} bytes)"
    )
}

pub fn default_key_path() -> Result<PathBuf> {
    let home = dirs::home_dir().context("cannot determine home directory")?;
    Ok(home.join(".second-brain").join("sync.key"))
}

/// Load the 32-byte sync key from `path`, generating and persisting a fresh one
/// if absent. The file is written 0600 because the key grants full read of every
/// synced memory (safety: file perms must be 0600).
pub fn load_or_create_key(path: &Path) -> Result<[u8; KEY_LEN]> {
    if let Some(key) = read_key(path)? {
        return Ok(key);
    }
    let mut key = [0u8; KEY_LEN];
    OsRng.fill_bytes(&mut key);
    if create_key(path, &key)? {
        return Ok(key);
    }
    read_key(path)?.with_context(|| {
        format!(
            "{} exists but reads as missing; is it a dangling symlink?",
            path.display()
        )
    })
}

/// Reads a `sync.key` written by sync protocol 3 or older without ever
/// creating one.
pub fn read_key_file(path: &Path) -> Result<Option<[u8; KEY_LEN]>> {
    read_key(path)
}

fn read_key(path: &Path) -> Result<Option<[u8; KEY_LEN]>> {
    let contents = match fs::read_to_string(path) {
        Ok(contents) => contents,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(e) => return Err(e).context("reading sync key"),
    };
    let raw = B64
        .decode(contents.trim())
        .context("decoding base64 sync key")?;
    let key: [u8; KEY_LEN] = raw
        .as_slice()
        .try_into()
        .map_err(|_| anyhow!("sync key must decode to {KEY_LEN} bytes, got {}", raw.len()))?;
    // correct an over-permissive key file left by an earlier write or manual copy.
    fs::set_permissions(path, fs::Permissions::from_mode(0o600))
        .context("tightening sync key permissions to 0600")?;
    Ok(Some(key))
}

/// False when another caller created the key first. The key is staged under a
/// name unique to this call and hard-linked into place, so a concurrent reader
/// never sees a partial key and exactly one creator wins.
fn create_key(path: &Path, key: &[u8; KEY_LEN]) -> Result<bool> {
    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        fs::create_dir_all(parent).context("creating sync key parent dir")?;
    }
    let mut staged = path.as_os_str().to_owned();
    staged.push(format!(".{:016x}.tmp", OsRng.next_u64()));
    let staged = PathBuf::from(staged);
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o600)
        .open(&staged)
        .context("staging the sync key")?;
    let written = file
        .write_all(format!("{}\n", B64.encode(key)).as_bytes())
        .and_then(|()| file.sync_all());
    drop(file);
    if let Err(e) = written {
        let left = match fs::remove_file(&staged) {
            Ok(()) => String::new(),
            Err(cleanup) => format!("; {} was left behind: {cleanup}", staged.display()),
        };
        return Err(e).context(format!("writing sync key{left}"));
    }
    let linked = fs::hard_link(&staged, path);
    if let Err(e) = fs::remove_file(&staged) {
        tracing::warn!("could not remove {}: {e}", staged.display());
    }
    match linked {
        Ok(()) => Ok(true),
        Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(false),
        Err(e) => Err(e).context("linking the sync key into place"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_key(seed: u8) -> [u8; KEY_LEN] {
        [seed; KEY_LEN]
    }

    #[test]
    fn round_trip_returns_plaintext() {
        let enc = XChaChaEncryptor::new(test_key(7));
        let plaintext = b"sensitive memory line as jsonl";
        let ct = enc.encrypt(plaintext).unwrap();
        assert_ne!(
            ct.as_slice(),
            plaintext,
            "ciphertext must differ from plaintext"
        );
        let pt = enc.decrypt(&ct).unwrap();
        assert_eq!(pt, plaintext);
    }

    #[test]
    fn nonce_is_random_per_message() {
        let enc = XChaChaEncryptor::new(test_key(3));
        let a = enc.encrypt(b"same input").unwrap();
        let b = enc.encrypt(b"same input").unwrap();
        assert_ne!(a, b, "fresh random nonce must yield distinct ciphertext");
    }

    #[test]
    fn decrypt_with_wrong_key_fails() {
        let enc = XChaChaEncryptor::new(test_key(1));
        let other = XChaChaEncryptor::new(test_key(2));
        let ct = enc.encrypt(b"secret").unwrap();
        assert!(
            other.decrypt(&ct).is_err(),
            "AEAD must reject a payload sealed under a different key"
        );
    }

    #[test]
    fn flipped_ciphertext_byte_fails() {
        let enc = XChaChaEncryptor::new(test_key(9));
        let mut ct = enc.encrypt(b"tamper me").unwrap();
        let last = ct.len() - 1;
        ct[last] ^= 0x01;
        assert!(
            enc.decrypt(&ct).is_err(),
            "Poly1305 tag must reject a single flipped byte"
        );
    }

    #[test]
    fn legacy_plaintext_frame_is_rejected() {
        let enc = XChaChaEncryptor::new(test_key(5));
        let mut legacy = vec![MAGIC_PLAINTEXT];
        legacy.extend_from_slice(b"{\"local_seq\":1}");
        assert!(enc.decrypt(&legacy).is_err());
    }

    #[test]
    fn empty_frame_is_rejected() {
        let enc = XChaChaEncryptor::new(test_key(5));
        assert!(enc.decrypt(&[]).is_err());
    }

    #[test]
    fn seal_open_line_round_trips() {
        let enc = XChaChaEncryptor::new(test_key(4));
        let record = b"{\"local_seq\":42,\"op\":\"Create\"}";
        let line = seal_line(&enc, record).unwrap();
        assert!(!line.contains('\n'), "wire line must be single-line");
        let out = open_line(&enc, &line).unwrap();
        assert_eq!(out, record);
    }

    #[test]
    fn open_line_rejects_legacy_jsonl() {
        let enc = XChaChaEncryptor::new(test_key(4));
        let legacy = "{\"local_seq\":1,\"op\":\"Create\"}";
        let err = open_line(&enc, legacy).unwrap_err();
        assert!(
            err.to_string().contains("not an encrypted sync line"),
            "unexpected error: {err:#}"
        );
    }

    #[test]
    fn open_line_rejects_base64_of_a_plaintext_frame() {
        let enc = XChaChaEncryptor::new(test_key(4));
        let mut legacy = vec![MAGIC_PLAINTEXT];
        legacy.extend_from_slice(b"{\"local_seq\":1}");
        assert!(open_line(&enc, &B64.encode(legacy)).is_err());
    }

    #[test]
    fn load_or_create_key_creates_0600_and_is_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nested").join("sync.key");
        let first = load_or_create_key(&path).unwrap();

        let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600, "key file must be 0600");

        let second = load_or_create_key(&path).unwrap();
        assert_eq!(
            first, second,
            "second load must return the same persisted key"
        );
    }

    #[test]
    fn a_dangling_key_symlink_is_an_error_naming_the_path() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("sync.key");
        std::os::unix::fs::symlink(dir.path().join("gone"), &path).unwrap();
        let error = load_or_create_key(&path).unwrap_err();
        assert!(format!("{error:#}").contains("sync.key"), "{error:#}");
    }

    #[test]
    fn callers_creating_the_key_at_once_all_get_the_one_on_disk() {
        for _ in 0..50 {
            let dir = tempfile::tempdir().unwrap();
            let path = dir.path().join("sync.key");
            let start = std::sync::Arc::new(std::sync::Barrier::new(16));
            let keys: Vec<[u8; KEY_LEN]> = (0..16)
                .map(|_| {
                    let path = path.clone();
                    let start = start.clone();
                    std::thread::spawn(move || {
                        start.wait();
                        load_or_create_key(&path).unwrap()
                    })
                })
                .collect::<Vec<_>>()
                .into_iter()
                .map(|handle| handle.join().unwrap())
                .collect();
            let on_disk = load_or_create_key(&path).unwrap();
            assert!(keys.iter().all(|key| *key == on_disk));
            assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1);
        }
    }

    fn identity(seed: u8) -> IdentityKey {
        IdentityKey::from_bytes(KeyGuard::new([seed; KEY_LEN]))
    }

    fn wire(seed: u8, number: u32) -> WireKey {
        let key = identity(seed);
        let epoch = Epoch {
            number,
            key_id: key.key_id(),
        };
        WireKey::derive(&key, &epoch).unwrap()
    }

    #[test]
    fn an_epoch_frame_opens_with_any_holder_of_its_epoch() {
        let one = wire(1, 1);
        let epoch = *one.epoch();
        let sealer = EpochSealer::new(vec![wire(0, 0), one], Some(epoch)).unwrap();
        let opener = EpochSealer::new(vec![wire(1, 1), wire(0, 0)], None).unwrap();
        let line = seal_line(&sealer, b"record").unwrap();
        assert_eq!(open_line(&opener, &line).unwrap(), b"record");
        let framed = B64.decode(&line).unwrap();
        assert_eq!(framed[0], MAGIC_EPOCH);
        assert_eq!(&framed[1..5], &1u32.to_be_bytes());
        assert_eq!(&framed[5..13], &epoch.key_id.0);
    }

    #[test]
    fn a_frame_under_an_epoch_not_held_names_the_epoch_and_pairing() {
        let sealer = EpochSealer::new(vec![wire(1, 0)], Some(*wire(1, 0).epoch())).unwrap();
        let opener = EpochSealer::new(vec![wire(2, 0)], None).unwrap();
        let line = seal_line(&sealer, b"record").unwrap();
        let err = format!("{:#}", open_line(&opener, &line).unwrap_err());
        assert!(err.contains("epoch 0"), "{err}");
        assert!(err.contains("sb pair"), "{err}");
        assert!(!err.contains("decrypt failed"), "{err}");
    }

    #[test]
    fn a_relabelled_epoch_header_fails_authentication() {
        let a = wire(1, 1);
        let mut relabelled = wire(1, 1);
        relabelled.epoch.number = 2;
        let a_epoch = *a.epoch();
        let sealer = EpochSealer::new(vec![a], Some(a_epoch)).unwrap();
        let mut framed = sealer.encrypt(b"record").unwrap();
        framed[1..5].copy_from_slice(&2u32.to_be_bytes());
        let opener = EpochSealer::new(vec![relabelled], None).unwrap();
        let err = format!("{:#}", opener.decrypt(&framed).unwrap_err());
        assert!(err.contains("tampered"), "{err}");
    }

    #[test]
    fn an_opener_only_set_refuses_to_seal_and_a_sync_key_frame_is_refused() {
        let opener = EpochSealer::new(vec![wire(1, 0)], None).unwrap();
        assert!(opener.encrypt(b"x").is_err());
        let legacy = XChaChaEncryptor::new(test_key(1)).encrypt(b"x").unwrap();
        let err = format!("{:#}", opener.decrypt(&legacy).unwrap_err());
        assert!(err.contains("protocol 3 or older"), "{err}");
        assert!(EpochSealer::new(vec![wire(1, 0)], Some(*wire(2, 0).epoch())).is_err());
    }
}