Skip to main content

weirdnet_device_link/
wire.rs

1//! Messages on `/weirdnet/link/2` and the sealed transfer.
2use serde::{Deserialize, Serialize};
3use weirdnet_keys::Sealed;
4use weirdnet_net::user::NodeCert;
5use zeroize::Zeroize;
6
7/// From the existing device to the new one.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum LinkRequest {
10    /// Start pairing: the existing device's ephemeral X25519 key.
11    Hello {
12        #[serde(with = "crate::bytes")]
13        nonce: Vec<u8>,
14        #[serde(with = "crate::bytes")]
15        eph: Vec<u8>,
16    },
17    /// The sealed [`Transfer`], after the user confirmed the SAS.
18    Transfer {
19        #[serde(with = "crate::bytes")]
20        nonce: Vec<u8>,
21        sealed: Sealed,
22    },
23    /// The sealed [`Credential`] of a service link, after the user confirmed the SAS
24    /// (since 0.4). A 0.3 device cannot read it and refuses.
25    Credential {
26        #[serde(with = "crate::bytes")]
27        nonce: Vec<u8>,
28        sealed: Sealed,
29    },
30    /// The user said the codes do not match (or gave up).
31    Cancel {
32        #[serde(with = "crate::bytes")]
33        nonce: Vec<u8>,
34    },
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38pub enum LinkReply {
39    Ok,
40    Refused { reason: String },
41}
42
43impl LinkReply {
44    pub(crate) fn refused(reason: &str) -> Self {
45        LinkReply::Refused { reason: reason.to_string() }
46    }
47}
48
49/// What moves to the new device. The seed is wiped on drop and never shown by
50/// `Debug`.
51#[derive(Clone, Serialize, Deserialize)]
52pub struct Transfer {
53    pub v: u8,
54    /// The 32-byte user-key seed (what the recovery words encode).
55    #[serde(with = "crate::bytes")]
56    pub seed: Vec<u8>,
57    pub user_id: String,
58    pub profile_name: String,
59    /// App settings worth carrying over (JSON; `{}` when none).
60    pub settings: String,
61    /// The new device's node certificate, signed by the user key.
62    pub node_cert: NodeCert,
63    /// Data types the user chose to sync (none yet).
64    pub sync: Vec<String>,
65}
66
67impl Drop for Transfer {
68    fn drop(&mut self) {
69        self.seed.zeroize();
70    }
71}
72
73impl std::fmt::Debug for Transfer {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        f.debug_struct("Transfer").field("user_id", &self.user_id).field("profile_name", &self.profile_name).finish_non_exhaustive()
76    }
77}
78
79/// Version of [`Credential`].
80pub const CREDENTIAL_VERSION: u8 = 1;
81/// Largest sync bundle a credential carries (sealed and hex-encoded it stays under the
82/// 1 MiB link request limit).
83pub const MAX_BUNDLE: usize = 384 * 1024;
84
85/// What moves to a **service** device (a server linked with a certificate only): the
86/// user's public key, a node certificate, and the sync material (`bundle`, opaque here:
87/// `weirdnet_sync::LinkBundle` bytes, with the device's grant, the roster and possibly
88/// epoch keys). Never the seed. The bundle is wiped on drop and never shown by `Debug`.
89#[derive(Clone, Serialize, Deserialize)]
90pub struct Credential {
91    pub v: u8,
92    /// The user's public key (protobuf).
93    #[serde(with = "crate::bytes")]
94    pub user_key: Vec<u8>,
95    pub user_id: String,
96    pub profile_name: String,
97    /// App settings worth carrying over (JSON; `{}` when none).
98    pub settings: String,
99    /// The new device's node certificate, signed by the user key.
100    pub node_cert: NodeCert,
101    /// Data types the device syncs.
102    pub sync: Vec<String>,
103    /// Sync material for the device (secret when it carries keys).
104    #[serde(with = "crate::bytes")]
105    pub bundle: Vec<u8>,
106}
107
108impl Drop for Credential {
109    fn drop(&mut self) {
110        self.bundle.zeroize();
111    }
112}
113
114impl std::fmt::Debug for Credential {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        f.debug_struct("Credential").field("user_id", &self.user_id).field("profile_name", &self.profile_name).finish_non_exhaustive()
117    }
118}
119
120/// What arrived on the new device.
121#[derive(Debug)]
122#[non_exhaustive]
123pub enum Received {
124    /// A full link: the identity seed.
125    Full(Transfer),
126    /// A service link: a certificate and sync material, no seed.
127    Service(Credential),
128}