Skip to main content

sew_e2ee/
config.rs

1//! Configuration, labels and limits.
2//!
3//! The "server" timings come only from the server's `params` (READY `call_keys` and every
4//! `CallKeysInit`): [`Params::from_wire`] checks each against its allowed range and refuses the
5//! session otherwise. The defaults exist so `Sim` and unit tests run without a server. The
6//! "local" values are [`E2eeConfig`].
7
8use sew_proto::e2ee::CallKeysParams;
9use sew_proto::e2ee::tls::{EXPORTER_LABEL, RELEASE_INFO_LABEL};
10
11use crate::error::SessionError;
12use crate::ids::KEY_RING_SIZE;
13
14/// The MLS exporter label of per-sender frame keys (RFC 9420 §17.8: a unique label).
15pub const LABEL_FRAME_KEY: &str = EXPORTER_LABEL;
16/// The HPKE `info` label of a recorder release.
17pub const LABEL_RELEASE_INFO: &str = RELEASE_INFO_LABEL;
18/// The only ciphersuite (`MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519`).
19pub const CIPHERSUITE: u16 = 0x0001;
20/// The SDK key-derivation family every bridge uses.
21pub const KDF: &str = "hkdf";
22/// The SDK's HKDF salt for frame keys (`"LKFrameEncryptionKey"`).
23pub const LK_SALT: &[u8] = b"LKFrameEncryptionKey";
24/// `lib` in `caps`.
25pub const LIB: &str = concat!("sew-e2ee/", env!("CARGO_PKG_VERSION"));
26
27/// Decode caps, checked before any parse (equal to or above the server's caps).
28pub mod caps {
29    /// A KeyPackage.
30    pub const KEY_PACKAGE: usize = 16 * 1024;
31    /// One proposal message.
32    pub const PROPOSAL: usize = 16 * 1024;
33    /// Proposals per `proposals` dispatch.
34    pub const PROPOSALS_PER_MESSAGE: usize = 64;
35    /// A commit.
36    pub const COMMIT: usize = 64 * 1024;
37    /// A GroupInfo.
38    pub const GROUP_INFO: usize = 32 * 1024;
39    /// A Welcome.
40    pub const WELCOME: usize = 128 * 1024;
41    /// A ratchet tree.
42    pub const RATCHET_TREE: usize = 256 * 1024;
43    /// A recorder certificate.
44    pub const RECORDER_CERT: usize = 1024;
45    /// A release `enc`.
46    pub const RELEASE_ENC: usize = 32;
47    /// A release `ct` (32-byte IKM + 16-byte tag).
48    pub const RELEASE_CT: usize = 48;
49    /// A `LeafBinding` table (as JSON or TLS).
50    pub const LEAF_BINDING_TABLE: usize = 64 * 1024;
51    /// Entries of a `LeafBinding` table (the server caps a call at a few hundred leaves).
52    pub const LEAF_BINDING_ENTRIES: usize = 1024;
53    /// Any dispatch or reply body, as raw JSON.
54    pub const BODY_JSON: usize = 448 * 1024;
55    /// A `SewCallIdentity` (TLS).
56    pub const CALL_IDENTITY: usize = 4 * 1024;
57    /// A `SewDeviceCert` (TLS).
58    pub const DEVICE_CERT: usize = 2 * 1024;
59    /// The `external_sender` bytes.
60    pub const EXTERNAL_SENDER: usize = 1024;
61}
62
63/// The local (not server-relayed) timings and limits.
64#[derive(Debug, Clone, PartialEq, Eq)]
65#[non_exhaustive]
66pub struct E2eeConfig {
67    /// The missing-key check runs this long after an activation.
68    pub missing_key_check_ms: u64,
69    /// `Resyncing` / `Resetting` watchdog period.
70    pub recovery_watchdog_ms: u64,
71    /// Watchdog retries before `Failed{recovery_timeout}`.
72    pub recovery_watchdog_tries: u32,
73    /// A prepared KeyPackage is stale after this long (and past the server TTL).
74    pub prepared_kp_max_age_s: u64,
75    /// Certificate clock skew (± seconds).
76    pub clock_skew_s: u64,
77    /// Renew the device certificate when less than this is left.
78    pub cert_renew_before_s: u64,
79    /// `report` roster anomalies per leaf per hour; slot signals per hour (separate bucket).
80    pub report_max_per_hour: u32,
81    /// The joining window for roster anomalies (`engine_participant_without_leaf`,
82    /// `leaf_without_voice_state`).
83    pub roster_grace_ms: u64,
84    /// `peer_undecryptable`: failures must last this long (after the grace) before a `state` check.
85    pub peer_undecryptable_after_ms: u64,
86    /// Unknown-call buffer: messages and bytes per call, and calls.
87    pub unknown_call_max_messages: usize,
88    /// Bytes per unknown call.
89    pub unknown_call_max_bytes: usize,
90    /// Unknown calls buffered at once.
91    pub unknown_call_max_calls: usize,
92    /// Commit bytes are recorded at most this long for the security code (unused bound).
93    pub key_package_lifetime_back_s: u64,
94    /// KeyPackage lifetime forward from now (`now + 6600`, a 2 h range with the back margin).
95    pub key_package_lifetime_fwd_s: u64,
96}
97
98impl Default for E2eeConfig {
99    fn default() -> E2eeConfig {
100        E2eeConfig {
101            missing_key_check_ms: 2_000,
102            recovery_watchdog_ms: 5_000,
103            recovery_watchdog_tries: 3,
104            prepared_kp_max_age_s: 3_600,
105            clock_skew_s: 300,
106            cert_renew_before_s: 604_800,
107            report_max_per_hour: 10,
108            roster_grace_ms: 5_000,
109            peer_undecryptable_after_ms: 2_000,
110            unknown_call_max_messages: 64,
111            unknown_call_max_bytes: 1024 * 1024,
112            unknown_call_max_calls: 2,
113            key_package_lifetime_back_s: 600,
114            key_package_lifetime_fwd_s: 6_600,
115        }
116    }
117}
118
119impl E2eeConfig {
120    /// The default timings and limits (the same as `E2eeConfig::default()`); change single
121    /// fields on the result.
122    pub fn new() -> E2eeConfig {
123        E2eeConfig::default()
124    }
125}
126
127/// The server's relayed client parameters, checked.
128#[derive(Debug, Clone, PartialEq, Eq)]
129pub struct Params {
130    /// Receivers retire an old slot this long after `activate`.
131    pub retire_after_ms: u64,
132    /// A member that sent `ready` and hears no `activate` switches after this long.
133    pub client_activate_fallback_ms: u64,
134    /// Longest KeyPackage lifetime the server accepts.
135    pub key_package_max_lifetime_s: u64,
136    /// The unknown-call buffer window.
137    pub unknown_call_buffer_ms: u64,
138    /// "Securing…" shows only after this long (passed through to the UI).
139    pub securing_indicator_after_ms: u64,
140}
141
142impl Default for Params {
143    fn default() -> Params {
144        Params {
145            retire_after_ms: 10_000,
146            client_activate_fallback_ms: 2_500,
147            key_package_max_lifetime_s: 7_200,
148            unknown_call_buffer_ms: 5_000,
149            securing_indicator_after_ms: 1_500,
150        }
151    }
152}
153
154fn range(key: &'static str, v: u64, lo: u64, hi: u64) -> Result<u64, SessionError> {
155    if (lo..=hi).contains(&v) {
156        Ok(v)
157    } else {
158        Err(SessionError::Params { key })
159    }
160}
161
162impl Params {
163    /// Checks the server's `params`. A value outside its range refuses the session
164    /// (`SessionError::Params{key}`); a ring size other than 64 is `SessionError::RingSize`.
165    pub fn from_wire(p: &CallKeysParams) -> Result<Params, SessionError> {
166        if p.key_ring_size != u32::from(KEY_RING_SIZE) {
167            return Err(SessionError::RingSize {
168                got: p.key_ring_size,
169            });
170        }
171        if p.ciphersuite != CIPHERSUITE {
172            return Err(SessionError::Params { key: "ciphersuite" });
173        }
174        if !p.ciphersuites.contains(&CIPHERSUITE) {
175            return Err(SessionError::Params {
176                key: "ciphersuites",
177            });
178        }
179        if p.kdf != KDF {
180            return Err(SessionError::Params { key: "kdf" });
181        }
182        if p.exporter_label != LABEL_FRAME_KEY {
183            return Err(SessionError::Params {
184                key: "exporter_label",
185            });
186        }
187        if p.release_info_label != LABEL_RELEASE_INFO {
188            return Err(SessionError::Params {
189                key: "release_info_label",
190            });
191        }
192        Ok(Params {
193            retire_after_ms: range("retire_after_ms", p.retire_after_ms, 1_000, 60_000)?,
194            client_activate_fallback_ms: range(
195                "client_activate_fallback_ms",
196                p.client_activate_fallback_ms,
197                500,
198                10_000,
199            )?,
200            key_package_max_lifetime_s: range(
201                "key_package_max_lifetime_s",
202                p.key_package_max_lifetime_s,
203                600,
204                86_400,
205            )?,
206            unknown_call_buffer_ms: range(
207                "unknown_call_buffer_ms",
208                p.unknown_call_buffer_ms,
209                0,
210                30_000,
211            )?,
212            securing_indicator_after_ms: p.securing_indicator_after_ms,
213        })
214    }
215
216    /// The wire form of the defaults (for `Sim` and tests).
217    pub fn default_wire() -> CallKeysParams {
218        let d = Params::default();
219        let mut call_keys_params = CallKeysParams::new(
220            KDF.to_owned(),
221            LABEL_FRAME_KEY.to_owned(),
222            LABEL_RELEASE_INFO.to_owned(),
223        );
224        call_keys_params.ciphersuite = CIPHERSUITE;
225        call_keys_params.ciphersuites = vec![CIPHERSUITE];
226        call_keys_params.key_ring_size = u32::from(KEY_RING_SIZE);
227        call_keys_params.retire_after_ms = d.retire_after_ms;
228        call_keys_params.client_activate_fallback_ms = d.client_activate_fallback_ms;
229        call_keys_params.key_package_max_lifetime_s = d.key_package_max_lifetime_s;
230        call_keys_params.unknown_call_buffer_ms = d.unknown_call_buffer_ms;
231        call_keys_params.securing_indicator_after_ms = d.securing_indicator_after_ms;
232        call_keys_params
233    }
234}