1use std::collections::BTreeSet;
2use std::fmt;
3use std::fs;
4use std::io::Write;
5use std::path::Path;
6
7use age::x25519::Recipient;
8use anyhow::{Context, Result, anyhow};
9use second_brain_core::keys::{
10 DEVICE_KEY_FILE, DeviceKeys, DeviceRecord, Envelope, Epoch, EpochCause, IdentityKey,
11 IdentityTag, KeyGuard, SignedRecord, load_device_keys, newest_epoch, wrap,
12};
13use second_brain_core::sqlite_store::SqliteStore;
14use serde::{Deserialize, Serialize};
15
16use crate::crypto::{EpochSealer, WireKey, read_key_file};
17use crate::protocol::HandshakeKeys;
18use crate::snapshot::SnapshotKey;
19
20mod handover;
21mod own_row;
22mod pairing;
23mod recovery;
24mod retirement;
25mod revocation;
26mod status;
27
28pub use pairing::{Joined, REMOVED, take_admission};
29pub use recovery::RecoveryState;
30pub use retirement::Retired;
31pub use revocation::{Removal, RemovalRefused};
32pub use status::{IdentityStatus, PeerStanding, RetiredPhrase};
33
34pub const LEGACY_KEY_FILE: &str = "sync.key";
35pub const MIGRATED_PEERS_FILE: &str = "migrated-peers.json";
36
37pub const MIGRATION_WINDOW_MICROS: i64 = 90 * 86_400 * 1_000_000;
40
41const MIGRATED_TAG_PREFIX: &str = "m-";
42
43#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct IdentityMismatch(pub String);
47
48impl fmt::Display for IdentityMismatch {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 f.write_str(&self.0)
51 }
52}
53
54impl std::error::Error for IdentityMismatch {}
55
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub enum Continuity {
58 NoKeyFile,
60 Migrated { identity: IdentityTag },
62 Ignored { reason: String },
65}
66
67#[derive(Debug, Serialize, Deserialize)]
68struct MigratedPeers {
69 frozen_at_micros: i64,
70 machine_ids: BTreeSet<String>,
71}
72
73pub fn migrate_sync_key(
79 store: &SqliteStore,
80 data_dir: &Path,
81 name: &str,
82 now_micros: i64,
83) -> Result<Continuity> {
84 let path = data_dir.join(LEGACY_KEY_FILE);
85 let Some(raw) = read_key_file(&path)? else {
86 return Ok(Continuity::NoKeyFile);
87 };
88 let epoch0_key = IdentityKey::from_bytes(KeyGuard::new(raw));
89 let tag = IdentityTag::migrated(&epoch0_key.key_id());
90 let epoch0 = Epoch {
91 number: 0,
92 key_id: epoch0_key.key_id(),
93 };
94 let device = load_device(data_dir)?;
95 let own = store.machine_id().to_string();
96 let recipient = device.age_recipient();
97
98 let held = store.envelopes_for_machine(&own)?;
99 if !held.is_empty()
100 && !held
101 .iter()
102 .any(|e| e.identity_tag == tag && e.epoch == epoch0)
103 {
104 return Ok(Continuity::Ignored {
105 reason: format!(
106 "{} is ignored: this device already holds an identity key",
107 path.display()
108 ),
109 });
110 }
111
112 let mut epochs = vec![epoch0];
113 let mut envelopes = vec![wrap(&epoch0_key, 0, &tag, &recipient, Some(own.clone()))?];
114 if !held
115 .iter()
116 .any(|e| e.identity_tag == tag && e.epoch.number > 0)
117 {
118 let first = IdentityKey::generate()?;
119 epochs.push(Epoch {
120 number: 1,
121 key_id: first.key_id(),
122 });
123 envelopes.push(wrap(&first, 1, &tag, &recipient, Some(own.clone()))?);
124 }
125 if store.device_record(&own)?.is_none() {
126 let row = own_row(&device, &own, &tag, name).sign(&own, device.signing_key());
127 store.insert_first_device_record(&row, &own, &device.verifying_key())?;
128 }
129 freeze_known_peers(store, data_dir, now_micros)?;
130 store.put_keys_durably(&tag, &epochs, &envelopes, now_micros)?;
133
134 let stored = store
135 .envelope(&tag, &epoch0, &recipient.to_string())?
136 .context("the wrapped sync.key is not in the store after the write")?;
137 let reopened = stored.open(device.age_identity())?;
138 anyhow::ensure!(
139 reopened.key_id() == epoch0.key_id,
140 "the wrapped sync.key opened to another key"
141 );
142 fs::remove_file(&path).with_context(|| format!("deleting {}", path.display()))?;
143 sync_dir(data_dir)?;
144 Ok(Continuity::Migrated { identity: tag })
145}
146
147fn own_row(device: &DeviceKeys, own: &str, tag: &IdentityTag, name: &str) -> DeviceRecord {
148 DeviceRecord {
149 machine_id: own.to_string(),
150 identity_tag: tag.as_str().to_string(),
151 version: 1,
152 name: name.to_string(),
153 age_recipient: device.age_recipient().to_string(),
154 ssh_public_key: device.ssh_public_key(),
155 signing_public_key: device.verifying_key().to_bytes(),
156 recovery_fingerprints: Vec::new(),
157 ssh_login: None,
158 ssh_port: None,
159 ssh_host_key: None,
160 }
161}
162
163fn freeze_known_peers(store: &SqliteStore, data_dir: &Path, now_micros: i64) -> Result<()> {
164 let path = data_dir.join(MIGRATED_PEERS_FILE);
165 if path.exists() {
166 return Ok(());
167 }
168 let frozen = MigratedPeers {
169 frozen_at_micros: now_micros,
170 machine_ids: store.known_peer_ids()?,
171 };
172 let staged = data_dir.join(format!(".{MIGRATED_PEERS_FILE}.tmp"));
173 let mut file =
174 fs::File::create(&staged).with_context(|| format!("staging {}", staged.display()))?;
175 file.write_all(&serde_json::to_vec(&frozen)?)?;
176 file.sync_all()?;
177 drop(file);
178 fs::rename(&staged, &path).with_context(|| format!("writing {}", path.display()))?;
179 sync_dir(data_dir)
180}
181
182fn load_migrated_peers(data_dir: &Path) -> Result<Option<MigratedPeers>> {
183 let path = data_dir.join(MIGRATED_PEERS_FILE);
184 match fs::read(&path) {
185 Ok(bytes) => Ok(Some(
186 serde_json::from_slice(&bytes)
187 .with_context(|| format!("reading {}", path.display()))?,
188 )),
189 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
190 Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
191 }
192}
193
194fn sync_dir(dir: &Path) -> Result<()> {
195 fs::File::open(dir)
196 .and_then(|d| d.sync_all())
197 .with_context(|| format!("syncing {}", dir.display()))
198}
199
200fn load_device(data_dir: &Path) -> Result<DeviceKeys> {
201 load_device_keys(data_dir)?.with_context(|| {
202 format!(
203 "{} is missing; the daemon creates it on start",
204 data_dir.join(DEVICE_KEY_FILE).display()
205 )
206 })
207}
208
209fn is_store_error(error: &anyhow::Error) -> bool {
212 error.chain().any(|cause| cause.is::<rusqlite::Error>())
213}
214
215fn is_migrated(tag: &IdentityTag) -> bool {
216 tag.as_str().starts_with(MIGRATED_TAG_PREFIX)
217}
218
219pub struct Keyring<'a> {
222 store: &'a SqliteStore,
223 data_dir: &'a Path,
224 device: DeviceKeys,
225 identity: Option<IdentityTag>,
226 held: Vec<(Epoch, IdentityKey)>,
227}
228
229impl<'a> Keyring<'a> {
230 pub fn load(store: &'a SqliteStore, data_dir: &'a Path) -> Result<Self> {
231 let device = load_device(data_dir)?;
232 let recipient = device.age_recipient().to_string();
233 let mut identity = None;
234 let mut held = Vec::new();
235 if let Some((tag, envelopes)) = store.own_identity_envelopes(&recipient)? {
236 for envelope in envelopes {
237 match envelope.open(device.age_identity()) {
238 Ok(key) => held.push((envelope.epoch, key)),
239 Err(e) => tracing::warn!("skipping an envelope this device cannot open: {e:#}"),
240 }
241 }
242 identity = Some(tag);
243 }
244 Ok(Self {
245 store,
246 data_dir,
247 device,
248 identity,
249 held,
250 })
251 }
252
253 pub fn identity(&self) -> Option<&IdentityTag> {
254 self.identity.as_ref()
255 }
256
257 pub fn held_epochs(&self) -> Vec<Epoch> {
258 self.held.iter().map(|(epoch, _)| *epoch).collect()
259 }
260
261 fn own(&self) -> &str {
262 self.store.machine_id()
263 }
264
265 fn device_name(&self, machine_id: &str) -> String {
268 match self.store.device_record(machine_id) {
269 Ok(Some(row)) if !row.current.record.name.is_empty() => row.current.record.name,
270 _ => machine_id.to_string(),
271 }
272 }
273
274 fn key(&self, epoch: &Epoch) -> Option<&IdentityKey> {
275 self.held.iter().find(|(e, _)| e == epoch).map(|(_, k)| k)
276 }
277
278 fn active_row(&self, machine_id: &str) -> Result<Option<SignedRecord>> {
281 let Some(tag) = &self.identity else {
282 return Ok(None);
283 };
284 Ok(self
285 .store
286 .device_record(machine_id)?
287 .filter(|row| !row.revoked && row.current.record.identity_tag == tag.as_str())
288 .map(|row| row.current))
289 }
290
291 fn recipient_of(&self, machine_id: &str) -> Result<Option<(String, Recipient)>> {
292 let Some(row) = self.active_row(machine_id)? else {
293 return Ok(None);
294 };
295 let text = row.record.age_recipient;
296 let parsed = text
297 .parse::<Recipient>()
298 .map_err(|e| anyhow!("device {machine_id} has an invalid age recipient: {e}"))?;
299 Ok(Some((text, parsed)))
300 }
301
302 fn known_to_peer(&self, peer: &str) -> Result<Vec<Epoch>> {
306 let Some(tag) = &self.identity else {
307 return Ok(Vec::new());
308 };
309 let recipient = self.recipient_of(peer)?.map(|(text, _)| text);
310 let mut known = Vec::new();
311 for epoch in self.held_epochs() {
312 let marked = match &recipient {
313 Some(recipient) => self.store.envelope(tag, &epoch, recipient)?.is_some(),
314 None => false,
315 };
316 if marked || (epoch.number == 0 && is_migrated(tag)) {
317 known.push(epoch);
318 }
319 }
320 Ok(known)
321 }
322
323 pub fn handshake_for(&self, peer: &str, declared: Option<&[Epoch]>) -> Result<HandshakeKeys> {
328 let Some(tag) = &self.identity else {
329 return Ok(HandshakeKeys::default());
330 };
331 let mut devices = Vec::new();
332 let mut active = Vec::new();
333 for row in self.store.device_records()? {
334 if row.current.record.identity_tag != tag.as_str() {
335 continue;
336 }
337 if !row.revoked {
338 active.push(row.current.record.machine_id.clone());
339 }
340 if row.current != row.first {
341 devices.push(row.first);
342 }
343 devices.push(row.current);
344 }
345 let mut envelopes = Vec::new();
346 if let Some((_, recipient)) = self.recipient_of(peer)? {
347 let known = match declared {
348 Some(declared) => declared.to_vec(),
349 None => self.known_to_peer(peer)?,
350 };
351 for (epoch, key) in &self.held {
352 if !known.contains(epoch) {
353 envelopes.push(wrap(key, epoch.number, tag, &recipient, Some(peer.into()))?);
354 }
355 }
356 }
357 Ok(HandshakeKeys {
358 identity_tag: Some(tag.clone()),
359 epochs: self.held_epochs(),
360 devices,
361 envelopes,
362 revocations: self.store.revocations(tag)?,
363 retirements: self.store.retirements(tag)?,
364 causes: self.causes()?,
365 routes: self
366 .store
367 .device_routes()?
368 .into_iter()
369 .filter(|route| active.contains(&route.machine_id))
370 .collect(),
371 })
372 }
373
374 pub fn check_peer(&self, peer: &HandshakeKeys, peer_name: &str) -> Result<()> {
378 let own = self.device_name(self.own());
379 let peer_name = self.device_name(peer_name);
380 let mismatch = match (&self.identity, &peer.identity_tag) {
381 (Some(ours), Some(theirs)) if ours == theirs => return Ok(()),
382 (None, Some(_)) => format!(
383 "{own} holds no sync identity key, so it cannot sync with {peer_name}; run \
384 `sb pair` on {peer_name}, then `sb pair <code>` on {own}; nothing was applied"
385 ),
386 (Some(_), None) => format!(
387 "{peer_name} holds no sync identity key, so it cannot sync with {own}; run \
388 `sb pair` on {own}, then `sb pair <code>` on {peer_name}; nothing was applied"
389 ),
390 (None, None) => format!(
391 "neither {own} nor {peer_name} holds a sync identity key; run `sb pair` on one \
392 of them, then `sb pair <code>` on the other; nothing was applied"
393 ),
394 (Some(_), Some(_)) => format!(
395 "{peer_name} belongs to a different sync identity than {own}; run `sb pair` on \
396 the one to keep, then `sb pair <code>` on the other; nothing was applied"
397 ),
398 };
399 Err(IdentityMismatch(mismatch).into())
400 }
401
402 pub fn accept(&self, peer: &str, keys: &HandshakeKeys, now_micros: i64) -> Result<()> {
407 self.check_peer(keys, peer)?;
408 let revoked_peer = self.is_revoked(peer)?;
409 self.accept_revocations(peer, &keys.revocations, revoked_peer)?;
413 self.accept_rows(peer, &keys.devices, now_micros)?;
414 self.accept_revocations(peer, &keys.revocations, revoked_peer)?;
415 self.accept_retirements(peer, &keys.retirements)?;
416 if let Err(refused) = self.refuse_revoked(peer) {
417 self.apply_retirements(now_micros)?;
418 return Err(refused);
419 }
420 self.accept_envelopes(peer, &keys.envelopes, &keys.causes, now_micros)?;
421 self.fill_causes(&keys.causes, now_micros)?;
422 self.mark_held_by(peer, &keys.epochs)?;
423 self.store.merge_device_routes(peer, &keys.routes)?;
424 self.apply_retirements(now_micros)?;
425 self.fill_recovery_envelopes()?;
426 if let Err(e) = self.record_peer_view(peer, keys, now_micros) {
427 tracing::warn!("cannot record what {peer}'s hello showed of this device: {e:#}");
428 }
429 Ok(())
430 }
431
432 fn accept_rows(&self, peer: &str, rows: &[SignedRecord], now_micros: i64) -> Result<()> {
433 let Some(tag) = &self.identity else {
434 return Ok(());
435 };
436 let mut pending: Vec<&SignedRecord> = rows
437 .iter()
438 .filter(|row| {
439 row.record.identity_tag == tag.as_str() && row.record.machine_id != self.own()
440 })
441 .collect();
442 let migrated = load_migrated_peers(self.data_dir)?;
443 loop {
444 let before = pending.len();
445 let mut left = Vec::new();
446 for row in pending {
447 match self.accept_row(peer, row, migrated.as_ref(), now_micros) {
448 Ok(true) => {}
449 Ok(false) => left.push(row),
450 Err(e) if is_store_error(&e) => tracing::warn!(
451 "cannot store the row for {} from {peer}: {e:#}",
452 row.record.machine_id
453 ),
454 Err(e) => tracing::debug!(
455 "not taking the row for {} from {peer}: {e:#}",
456 row.record.machine_id
457 ),
458 }
459 }
460 pending = left;
461 if pending.is_empty() || pending.len() == before {
462 return Ok(());
463 }
464 }
465 }
466
467 fn accept_row(
470 &self,
471 peer: &str,
472 row: &SignedRecord,
473 migrated: Option<&MigratedPeers>,
474 now_micros: i64,
475 ) -> Result<bool> {
476 let id = &row.record.machine_id;
477 if let Some(stored) = self.store.device_record(id)? {
478 if stored.revoked {
479 return Ok(true);
480 }
481 if row.record == stored.first.record {
482 for signature in &row.signatures {
483 if stored.first.signatures.contains(signature) || signature.signer == *id {
484 continue;
485 }
486 if let Some(signer) = self.active_row(&signature.signer)? {
487 self.store.insert_first_device_record(
488 row,
489 &signature.signer,
490 &signer.record.signing_public_key()?,
491 )?;
492 }
493 }
494 } else if row.record.version > stored.current.record.version {
495 self.store.update_device_record(row)?;
496 }
497 return Ok(true);
498 }
499
500 let direct = id == peer
501 && is_migrated(&IdentityTag::from(row.record.identity_tag.clone()))
502 && migrated.is_some_and(|m| {
503 m.machine_ids.contains(id)
504 && now_micros <= m.frozen_at_micros.saturating_add(MIGRATION_WINDOW_MICROS)
505 });
506 if direct {
507 self.store
508 .insert_first_device_record(row, id, &row.record.signing_public_key()?)?;
509 let mut countersigned = row.clone();
510 countersigned.countersign(self.own(), self.device.signing_key());
511 self.store.insert_first_device_record(
512 &countersigned,
513 self.own(),
514 &self.device.verifying_key(),
515 )?;
516 return Ok(true);
517 }
518 for signature in &row.signatures {
519 if signature.signer == *id {
520 continue;
521 }
522 if let Some(signer) = self.active_row(&signature.signer)? {
523 self.store.insert_first_device_record(
524 row,
525 &signature.signer,
526 &signer.record.signing_public_key()?,
527 )?;
528 return Ok(true);
529 }
530 }
531 Ok(false)
532 }
533
534 fn accept_envelopes(
535 &self,
536 peer: &str,
537 envelopes: &[Envelope],
538 causes: &[(Epoch, EpochCause)],
539 now_micros: i64,
540 ) -> Result<()> {
541 let Some(tag) = &self.identity else {
542 return Ok(());
543 };
544 if self.active_row(peer)?.is_none() {
545 return Ok(());
546 }
547 let recipient = self.device.age_recipient().to_string();
548 for envelope in envelopes {
549 if envelope.identity_tag != *tag
550 || envelope.recipient != recipient
551 || envelope.machine_id.as_deref() != Some(self.own())
552 || self.key(&envelope.epoch).is_some()
553 {
554 continue;
555 }
556 if let Err(e) = envelope.open(self.device.age_identity()) {
557 tracing::warn!(
558 "{peer} sent an envelope for epoch {} this device cannot open: {e:#}",
559 envelope.epoch.number
560 );
561 continue;
562 }
563 let Some(cause) = self.applicable_cause(&envelope.epoch, causes)? else {
564 tracing::warn!(
565 "{peer} sent epoch {} without the revocations behind it; it waits",
566 envelope.epoch.number
567 );
568 continue;
569 };
570 let cause = (!cause.is_empty()).then(|| cause.to_json()).transpose()?;
571 self.store
572 .put_key_epoch(tag, &envelope.epoch, cause.as_deref(), now_micros)?;
573 self.store.put_envelope(envelope)?;
574 }
575 Ok(())
576 }
577
578 fn mark_held_by(&self, peer: &str, declared: &[Epoch]) -> Result<()> {
582 let Some(tag) = &self.identity else {
583 return Ok(());
584 };
585 if peer == self.own() {
586 return Ok(());
587 }
588 let Some((text, recipient)) = self.recipient_of(peer)? else {
589 return Ok(());
590 };
591 for (epoch, key) in &self.held {
592 let marked = self.store.envelope(tag, epoch, &text)?.is_some();
593 if declared.contains(epoch) && !marked {
594 let envelope = wrap(key, epoch.number, tag, &recipient, Some(peer.into()))?;
595 self.store.put_envelope(&envelope)?;
596 } else if !declared.contains(epoch) && marked {
597 self.store.delete_envelope(tag, epoch, &text)?;
598 }
599 }
600 Ok(())
601 }
602
603 pub fn sealer_for(&self, peer: &str, declared: Option<&[Epoch]>) -> Result<EpochSealer> {
606 let epoch = self.shared_epoch(peer, declared)?;
607 EpochSealer::new(self.wire_keys()?, Some(epoch))
608 }
609
610 pub fn snapshot_key_for(&self, peer: &str, declared: Option<&[Epoch]>) -> Result<SnapshotKey> {
613 let epoch = self.shared_epoch(peer, declared)?;
614 self.snapshot_key(&epoch)
615 }
616
617 pub fn snapshot_key(&self, epoch: &Epoch) -> Result<SnapshotKey> {
620 let identity = self.key(epoch).ok_or_else(|| {
621 IdentityMismatch(format!(
622 "the snapshot is sealed under identity key epoch {} (key id {}), which this \
623 device does not hold; pair it with `sb pair`, nothing was applied",
624 epoch.number, epoch.key_id
625 ))
626 })?;
627 SnapshotKey::derive(identity, epoch)
628 }
629
630 fn shared_epoch(&self, peer: &str, declared: Option<&[Epoch]>) -> Result<Epoch> {
631 self.refuse_revoked(peer)?;
632 let shared: Vec<Epoch> = match declared {
633 Some(declared) => self
634 .held_epochs()
635 .into_iter()
636 .filter(|e| declared.contains(e))
637 .collect(),
638 None => self.known_to_peer(peer)?,
639 };
640 Ok(newest_epoch(&shared).ok_or_else(|| {
641 let (own, peer) = (self.device_name(self.own()), self.device_name(peer));
642 IdentityMismatch(format!(
643 "{own} and {peer} share no identity key epoch; re-pair {peer} with `sb pair`, \
644 nothing was sent"
645 ))
646 })?)
647 }
648
649 pub fn opener(&self) -> Result<EpochSealer> {
650 if self.identity.is_none() {
651 return Err(IdentityMismatch(
652 "this device holds no sync identity key, so it cannot open a peer's records; \
653 pair it with `sb pair` from a device that syncs, nothing was applied"
654 .to_string(),
655 )
656 .into());
657 }
658 EpochSealer::new(self.wire_keys()?, None)
659 }
660
661 fn wire_keys(&self) -> Result<Vec<WireKey>> {
662 self.held
663 .iter()
664 .map(|(epoch, key)| WireKey::derive(key, epoch))
665 .collect()
666 }
667}