sew_e2ee/bridge/mod.rs
1//! The key-provider bridge contract.
2//!
3//! Keys leave the core only as [`KeyEvent`]s. A bridge (one over a LiveKit client SDK, in the
4//! browser or native, or the simulated SDK `virtual_sdk::VirtualSdk` of the `testing` feature)
5//! applies them to its SDK in order and answers each [`KeyEvent::Fence`] with
6//! `Input::Installed { fence }` once every earlier event is in effect.
7//!
8//! The SDK configuration is identical on every bridge ([`SDK_CONFIG`], checked by
9//! `bridge_config_constants`): per-participant keys, ratchet window 0, failure tolerance −1,
10//! ring 64, 128-bit keys, HKDF with salt `"LKFrameEncryptionKey"`, empty SIF trailer, no data
11//! packets.
12
13pub mod caps;
14pub mod epochs;
15pub mod keyring;
16// Test-only: a simulated SDK for the `testing` feature's harness (unstable, not semver-covered).
17#[cfg(feature = "testing")]
18pub mod virtual_sdk;
19
20use crate::ids::{FenceId, LkIdentity, Slot};
21use crate::keys::FrameIkm;
22
23pub use caps::Caps;
24pub use keyring::KeyRing;
25
26/// One key instruction for a bridge. Not `Clone`: IKMs are moved, never copied
27/// implicitly.
28#[derive(Debug)]
29pub enum KeyEvent {
30 /// Install a **remote** sender's key at `slot` (I1: never the local identity).
31 Install {
32 /// The sender's engine identity.
33 identity: LkIdentity,
34 /// The slot.
35 slot: Slot,
36 /// The key.
37 ikm: FrameIkm,
38 },
39 /// The local identity's key: install it **and** switch the sender to `slot`.
40 Activate {
41 /// The local identity.
42 identity: LkIdentity,
43 /// The slot.
44 slot: Slot,
45 /// The key.
46 ikm: FrameIkm,
47 },
48 /// Overwrite `slot` of `identity` with random bytes (10 s after the next activation). The JS
49 /// bridge ignores it for the local identity.
50 Retire {
51 /// The identity.
52 identity: LkIdentity,
53 /// The slot.
54 slot: Slot,
55 /// Fresh random bytes (never derived).
56 filler: FrameIkm,
57 },
58 /// The identity left the key group; no further keys (its slots are retired first).
59 Forget {
60 /// The identity.
61 identity: LkIdentity,
62 },
63 /// Answer `Input::Installed { fence: id }` when every earlier event is in effect.
64 Fence {
65 /// The fence.
66 id: FenceId,
67 },
68 /// Leaving: overwrite every remote slot with random bytes.
69 Clear,
70}
71
72impl KeyEvent {
73 /// A short name for logs and telemetry.
74 pub fn kind(&self) -> &'static str {
75 match self {
76 KeyEvent::Install { .. } => "install",
77 KeyEvent::Activate { .. } => "activate",
78 KeyEvent::Retire { .. } => "retire",
79 KeyEvent::Forget { .. } => "forget",
80 KeyEvent::Fence { .. } => "fence",
81 KeyEvent::Clear => "clear",
82 }
83 }
84
85 /// The identity it is about, if any.
86 pub fn identity(&self) -> Option<&LkIdentity> {
87 match self {
88 KeyEvent::Install { identity, .. }
89 | KeyEvent::Activate { identity, .. }
90 | KeyEvent::Retire { identity, .. }
91 | KeyEvent::Forget { identity } => Some(identity),
92 KeyEvent::Fence { .. } | KeyEvent::Clear => None,
93 }
94 }
95
96 /// The slot it is about, if any.
97 pub fn slot(&self) -> Option<Slot> {
98 match self {
99 KeyEvent::Install { slot, .. }
100 | KeyEvent::Activate { slot, .. }
101 | KeyEvent::Retire { slot, .. } => Some(*slot),
102 _ => None,
103 }
104 }
105
106 /// An explicit copy (zeroizing copies of the key bytes). Used by drivers that feed one
107 /// event to both a [`KeyRing`] shadow and a bridge, and by tests.
108 pub fn duplicate(&self) -> KeyEvent {
109 match self {
110 KeyEvent::Install {
111 identity,
112 slot,
113 ikm,
114 } => KeyEvent::Install {
115 identity: identity.clone(),
116 slot: *slot,
117 ikm: ikm.duplicate(),
118 },
119 KeyEvent::Activate {
120 identity,
121 slot,
122 ikm,
123 } => KeyEvent::Activate {
124 identity: identity.clone(),
125 slot: *slot,
126 ikm: ikm.duplicate(),
127 },
128 KeyEvent::Retire {
129 identity,
130 slot,
131 filler,
132 } => KeyEvent::Retire {
133 identity: identity.clone(),
134 slot: *slot,
135 filler: filler.duplicate(),
136 },
137 KeyEvent::Forget { identity } => KeyEvent::Forget {
138 identity: identity.clone(),
139 },
140 KeyEvent::Fence { id } => KeyEvent::Fence { id: *id },
141 KeyEvent::Clear => KeyEvent::Clear,
142 }
143 }
144}
145
146/// How a bridge acknowledged an event.
147#[derive(Debug, Clone, Copy, PartialEq, Eq)]
148pub enum SinkAck {
149 /// In effect now (a fence is answered at once).
150 Now,
151 /// A fence that resolves later; the bridge feeds `Input::Installed { fence }` then.
152 Later(FenceId),
153}
154
155/// Why a bridge refused an event. Carries no key bytes.
156#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
157pub enum SinkError {
158 /// The SDK refused a key (`set_key` returned false). Fails loudly.
159 #[error("the SDK refused the key for slot {slot}")]
160 SdkRefused {
161 /// The slot.
162 slot: u8,
163 },
164 /// The sender stayed on another slot after one re-apply.
165 #[error("sender key index stuck: expected {expected}, observed {observed}")]
166 SenderIndexStuck {
167 /// The slot it should be on.
168 expected: u8,
169 /// The slot the SDK reports.
170 observed: u8,
171 },
172 /// The engine downgraded a publication's encryption.
173 #[error("the engine downgraded a publication's encryption")]
174 EncryptionDowngraded,
175 /// `libwebrtc` debug/trace logging is on without the explicit opt-in.
176 #[error("libwebrtc debug logging would log keys; refused without the explicit opt-in")]
177 KeyLoggingRisk,
178 /// The bridge is not armed (JS) or the room is gone.
179 #[error("bridge not ready: {0}")]
180 NotReady(&'static str),
181}
182
183/// A bridge: applies [`KeyEvent`]s to an SDK, in order.
184pub trait KeySink {
185 /// Applies one event.
186 fn apply(&mut self, ev: KeyEvent) -> Result<SinkAck, SinkError>;
187 /// What this bridge's SDK can do (the only source of [`Caps`]).
188 fn caps(&self) -> Caps;
189}
190
191/// The SDK configuration every bridge uses (`bridge_config_constants`).
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193pub struct SdkConfig {
194 /// Per-participant keys (`sharedKey: false`).
195 pub shared_key: bool,
196 /// Ratchet window.
197 pub ratchet_window_size: i32,
198 /// Failure tolerance.
199 pub failure_tolerance: i32,
200 /// Key ring size.
201 pub key_ring_size: u8,
202 /// Key size in bits.
203 pub key_size_bits: u16,
204 /// The KDF.
205 pub kdf: &'static str,
206 /// The HKDF salt.
207 pub salt: &'static [u8],
208 /// The SIF trailer (always empty).
209 pub sif_trailer: &'static [u8],
210 /// Data packets are never published.
211 pub publish_data: bool,
212}
213
214/// The one configuration.
215pub const SDK_CONFIG: SdkConfig = SdkConfig {
216 shared_key: false,
217 ratchet_window_size: 0,
218 failure_tolerance: -1,
219 key_ring_size: crate::ids::KEY_RING_SIZE,
220 key_size_bits: 128,
221 kdf: crate::config::KDF,
222 salt: crate::config::LK_SALT,
223 sif_trailer: b"",
224 publish_data: false,
225};