Skip to main content

sew_e2ee/
store.rs

1//! What persists: the device's certificate and ids, and the TOFU pins of the
2//! install's CA key, DS key and `external_sender` bytes. Nothing about a call is ever written
3//! (an app restart mid-call goes through the missed-epoch path).
4//!
5//! The private device key is the platform's (`DeviceSigner`); stores hold public data only.
6
7use std::collections::BTreeMap;
8
9use crate::ids::Id;
10
11/// The device's enrolled state (public data).
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct StoredDevice {
14    /// The install.
15    pub install_id: u64,
16    /// The member the device belongs to.
17    pub member_id: Id,
18    /// The device (Sew id).
19    pub device_id: Id,
20    /// The device's 65-byte SEC1 public key.
21    pub pubkey: [u8; 65],
22    /// TLS `SewDeviceCert`.
23    pub cert: Vec<u8>,
24}
25
26/// The TOFU pins of one install.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Pins {
29    /// The install.
30    pub install_id: u64,
31    /// `sew-call-ca`'s Ed25519 key.
32    pub ca_public: [u8; 32],
33    /// `sew-call-ds`'s Ed25519 key.
34    pub ds_public: [u8; 32],
35    /// The TLS `ExternalSender` bytes.
36    pub external_sender: Vec<u8>,
37}
38
39/// Where a device's state lives (web: IndexedDB `sew-e2ee` v1, store `device`, key = install
40/// id). Native stores are synchronous; the TS wrapper adapts the async IndexedDB one.
41pub trait DeviceStore {
42    /// The device of `install_id`, if enrolled.
43    fn load(&self, install_id: u64) -> Option<StoredDevice>;
44    /// Saves (replaces) the device.
45    fn save(&mut self, dev: &StoredDevice) -> Result<(), String>;
46    /// Forgets the device **and** the pins of `install_id` (a new enrollment accepts new pins).
47    fn clear(&mut self, install_id: u64);
48    /// The pins of `install_id`, if any.
49    fn load_pins(&self, install_id: u64) -> Option<Pins>;
50    /// Saves the pins (first sight only; a change is refused before this is called).
51    fn save_pins(&mut self, pins: &Pins) -> Result<(), String>;
52}
53
54/// An in-memory store (tests, the driver).
55#[derive(Debug, Default, Clone)]
56pub struct MemoryDeviceStore {
57    devices: BTreeMap<u64, StoredDevice>,
58    pins: BTreeMap<u64, Pins>,
59}
60
61impl MemoryDeviceStore {
62    /// An empty store.
63    pub fn new() -> MemoryDeviceStore {
64        MemoryDeviceStore::default()
65    }
66}
67
68impl DeviceStore for MemoryDeviceStore {
69    fn load(&self, install_id: u64) -> Option<StoredDevice> {
70        self.devices.get(&install_id).cloned()
71    }
72
73    fn save(&mut self, dev: &StoredDevice) -> Result<(), String> {
74        self.devices.insert(dev.install_id, dev.clone());
75        Ok(())
76    }
77
78    fn clear(&mut self, install_id: u64) {
79        self.devices.remove(&install_id);
80        self.pins.remove(&install_id);
81    }
82
83    fn load_pins(&self, install_id: u64) -> Option<Pins> {
84        self.pins.get(&install_id).cloned()
85    }
86
87    fn save_pins(&mut self, pins: &Pins) -> Result<(), String> {
88        self.pins.insert(pins.install_id, pins.clone());
89        Ok(())
90    }
91}