Skip to main content

capsule_library/lifecycle/
enrollment.rs

1//! The client half of cross-device add (SSoT: [Device Enrollment § Cross-Device Add]): the
2//! bundles this device advertises and the cross-sign that admits the new device. The wire form —
3//! the payloads, their codec and the safety code — is `capsule_core::enrollment`, re-exported
4//! here.
5//!
6//! [Device Enrollment § Cross-Device Add]: https://docs/design/device-enrollment/#cross-device-add
7
8use capsule_core::crypto::keys::{DekPublic, DeviceDirectory, DeviceEntry};
9use capsule_core::crypto::rng;
10pub use capsule_core::enrollment::*;
11
12use super::{Result, Workspace, now_rfc3339};
13
14impl Workspace {
15    /// What this device advertises as the **initiator** of a cross-device add, with a nonce
16    /// freshly drawn by this call. Call it once per ceremony and keep the result: it is both what
17    /// A sends and what A computes its safety code over.
18    pub fn enrollment_initiator_keys(&self) -> InitiatorKeys {
19        InitiatorKeys {
20            user_id: self.account.user_id,
21            user_ik_public: self.account.user_ik.verifying_key(),
22            device_id: self.account.device.device_id,
23            dsk_public: self.device_signer.verifying_key(),
24            nonce: rng::random_array(),
25        }
26    }
27
28    /// What this device advertises as the **enrollee** of a cross-device add: the signing key it
29    /// actually signs with (a hardware signer's, when one was supplied), its DEK, and a nonce
30    /// freshly drawn by this call. Call it once per ceremony and keep the result: B commits to,
31    /// reveals, and computes its safety code over this one bundle.
32    pub fn enrollment_enrollee_keys(&self) -> EnrolleeKeys {
33        EnrolleeKeys {
34            device_id: self.account.device.device_id,
35            dsk_public: self.device_signer.verifying_key(),
36            dek_public: DekPublic(self.account.device.dek.public_bytes()),
37            nonce: rng::random_array(),
38        }
39    }
40
41    /// **Cross-sign** a new device into this account's directory: append `entry` at the next
42    /// `directory_version`, re-sign under the user IK, adopt it as this workspace's directory,
43    /// and return it for publication and for relaying to the new device.
44    ///
45    /// Call this only after the safety codes matched. Refused with
46    /// [`LifecycleError::Directory`](super::LifecycleError::Directory), leaving the directory
47    /// unchanged, when the entry's id or signing key is already listed or the entry is revoked.
48    ///
49    /// The re-signed directory is persisted beside the account before it is adopted, so
50    /// [`open`](Self::open) restores it and the next directory this device signs builds on it;
51    /// a failed write is a [`LifecycleError::Io`](super::LifecycleError::Io) and also leaves the
52    /// directory unchanged. The caller still publishes the returned directory.
53    #[tracing::instrument(skip_all, fields(device_id = %entry.device_id))]
54    pub fn cross_sign_device(&mut self, entry: DeviceEntry) -> Result<DeviceDirectory> {
55        let mut core = self.directory.core.clone();
56        core.add_device(entry, now_rfc3339()).inspect_err(|e| {
57            tracing::warn!(error = %e, "enrollment: cross-sign refused");
58        })?;
59        self.adopt_directory(core.sign(&self.account.user_ik))?;
60        tracing::info!(
61            directory_version = self.directory.core.directory_version,
62            devices = self.directory.core.devices.len(),
63            "enrollment: device cross-signed into the directory"
64        );
65        Ok(self.directory.clone())
66    }
67}
68
69#[cfg(test)]
70mod tests;