sew-call-client 0.9.6

Sew's native voice engine: LiveKit-protocol signalling and reconnect against Sew's media server, str0m transport with TURN over UDP, TCP and TLS (relay-only when asked), MLS call keys with the mandatory E2EE checks over the call credential and the call socket, the device store, and the kessel-audio bridge.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! The engine's configuration and its TLS trust.
//!
//! Production trust is the platform's (`rustls-platform-verifier`: the macOS and Windows
//! verifiers, the system roots on Linux), on one rustls crypto provider: the host's
//! ([`ClientConfig::with_crypto_provider`]), else the process default
//! (`CryptoProvider::get_default`), else the built-in ring provider (feature
//! `builtin-ring-provider`, on by default; without it the engine refuses with
//! `no_crypto_provider`). Signalling, the TURN-TLS connection and Sew's
//! client API all use it. A test-only option (feature
//! `testing`) adds a run CA **on top of** the platform roots; there is no way to trust only a
//! test CA.

use std::net::IpAddr;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use rustls::ClientConfig as TlsConfig;
use rustls::crypto::CryptoProvider;
use rustls_platform_verifier::Verifier;

use crate::device::{DeviceKeyStore, DevicePlatform, HardwareDeviceKey, StoreChoice};

/// The engine's configuration (one per app).
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ClientConfig {
    /// The host's app identifier (with feature `keyring-store`, `sew-voice.<app id>` is the OS
    /// credential store's service).
    pub app_id: String,
    /// The host's data directory (the device store's public data).
    pub data_dir: PathBuf,
    /// Where [`crate::CallClient::with_mls`] keeps the device key and its public record.
    /// Default [`StoreChoice::Auto`]: a 0600 file, warned as `key_storage_file` (with feature
    /// `keyring-store`, the OS credential store first). A host passes its own store with
    /// [`ClientConfig::with_device_key_store`].
    pub device_store: StoreChoice,
    /// The platform the device enrols as. Default from `target_os` (iOS, Android, otherwise
    /// desktop); a host sets it with [`ClientConfig::with_device_platform`].
    pub device_platform: DevicePlatform,
    /// The reconnect window (Sew's media server's `reconnect_window_ms`, 20 s): resume and full
    /// reconnects inside it; after it, a re-join through the provider.
    pub reconnect_window: Duration,
    /// How long the first connect may take before the call ends with `network`.
    pub connect_timeout: Duration,
    /// The rustls crypto provider of every TLS connection (signalling, TURN-TLS, the client
    /// API and the key routes). `None` (the default): the process default
    /// (`CryptoProvider::get_default`, e.g. one the host installed with `install_default`),
    /// else the built-in ring provider (feature `builtin-ring-provider`, on by default; without
    /// it, TLS setup fails with `no_crypto_provider`). A host sets it with
    /// [`ClientConfig::with_crypto_provider`].
    pub crypto_provider: Option<Arc<CryptoProvider>>,
    /// Whether the media connection negotiates H.264 beside VP8 (feature `video-core`): the
    /// receive sections offer it, so the media server may forward H.264 tracks, and a camera
    /// or screen may publish in it. Default [`default_video_h264`]: on where this device can
    /// show H.264 (VideoToolbox on macOS and iOS; the webview's WebCodecs elsewhere), off on
    /// Linux, which has no H.264 decoder in the engine (kessel-video decodes only VP8 there), so
    /// the media server never sends a Linux client H.264 it can't show. A host that decodes
    /// H.264 itself turns it on with [`ClientConfig::with_video_h264`].
    ///
    /// It is an upper bound: once the host declares its decoders
    /// ([`crate::CallClient::set_video_decode`]), H.264 is offered only if they list it (never
    /// on Linux), and never under [`crate::VideoCodecPreference::PreferVp8`].
    pub video_h264: bool,
    /// Test-only overrides (feature `testing`).
    #[cfg(feature = "testing")]
    pub testing: TestingOverrides,
}

impl ClientConfig {
    /// A config with the defaults.
    pub fn new(app_id: &str, data_dir: PathBuf) -> ClientConfig {
        ClientConfig {
            app_id: app_id.to_owned(),
            data_dir,
            device_store: StoreChoice::Auto,
            device_platform: crate::device::default_platform(),
            reconnect_window: Duration::from_secs(20),
            connect_timeout: Duration::from_secs(20),
            crypto_provider: None,
            video_h264: default_video_h264(),
            #[cfg(feature = "testing")]
            testing: TestingOverrides::default(),
        }
    }

    /// Negotiates H.264 (`true`) or VP8 only (`false`) for video, instead of the
    /// [`default_video_h264`] of this platform ([`ClientConfig::video_h264`]).
    pub fn with_video_h264(mut self, on: bool) -> ClientConfig {
        self.video_h264 = on;
        self
    }

    /// Keeps the device's private key and public record in the host's own store (e.g. K2's
    /// keyring or vault) instead of the built-in file ([`StoreChoice::Host`]).
    pub fn with_device_key_store(mut self, store: Arc<dyn DeviceKeyStore>) -> ClientConfig {
        self.device_store = StoreChoice::Host(store);
        self
    }

    /// Keeps the device's private key in the host's hardware (Secure Enclave, StrongBox/TEE)
    /// through [`HardwareDeviceKey`] ([`StoreChoice::Hardware`]); the public record stays in
    /// the data directory. It replaces any other store, and the engine never falls back to a
    /// software key.
    pub fn with_hardware_device_key(mut self, key: Arc<dyn HardwareDeviceKey>) -> ClientConfig {
        self.device_store = StoreChoice::Hardware(key);
        self
    }

    /// The platform the device enrols as (instead of the `target_os` default).
    pub fn with_device_platform(mut self, platform: DevicePlatform) -> ClientConfig {
        self.device_platform = platform;
        self
    }

    /// Builds every TLS connection on `provider` (e.g. the host's own rustls provider) instead
    /// of the process default.
    pub fn with_crypto_provider(mut self, provider: Arc<CryptoProvider>) -> ClientConfig {
        self.crypto_provider = Some(provider);
        self
    }
}

/// Whether this platform negotiates H.264 by default ([`ClientConfig::video_h264`]): `false` on
/// Linux (no H.264 decoder there), `true` elsewhere.
pub const fn default_video_h264() -> bool {
    !cfg!(target_os = "linux")
}

/// TEST ONLY (feature `testing`): what the loopback lanes change.
#[cfg(feature = "testing")]
#[derive(Clone, Debug, Default)]
pub struct TestingOverrides {
    /// CA certificates (DER) trusted **in addition to** the platform roots.
    pub extra_roots: Vec<Vec<u8>>,
    /// Fixed name resolution (`turn.sew.test` → 127.0.0.1), as Chrome's
    /// `--host-resolver-rules`.
    pub resolve: Vec<(String, IpAddr)>,
    /// The media transport's source address (the TURN sockets and the host candidate bind
    /// to it), shared so a test can move the client to another "network" mid-call (`None`: the
    /// OS chooses, as in production).
    pub source_ip: Arc<std::sync::RwLock<Option<IpAddr>>>,
    /// The RTP sequence number the microphone's first packet carries in each new media session
    /// (each new PC; `None`: the engine's own random start in [0, 2^15)). Lanes set it near
    /// 65535 to make the sequence wrap early.
    pub first_seq: Option<u16>,
    /// Received video RTP packets to drop on arrival, before the reorder buffer (a lost
    /// burst the retransmissions cannot fill: str0m already counted them received). A lane sets
    /// it mid-call; each dropped packet takes one off.
    pub drop_video_packets: Arc<std::sync::atomic::AtomicU32>,
}

/// Takes one off [`TestingOverrides::drop_video_packets`]: whether to drop this packet.
#[cfg(feature = "testing")]
pub(crate) fn drop_video_packet(cfg: &ClientConfig) -> bool {
    use std::sync::atomic::Ordering;
    cfg.testing
        .drop_video_packets
        .try_update(Ordering::AcqRel, Ordering::Acquire, |n| n.checked_sub(1))
        .is_ok()
}

/// No packet is dropped (no testing override in this build).
#[cfg(not(feature = "testing"))]
pub(crate) fn drop_video_packet(_cfg: &ClientConfig) -> bool {
    false
}

/// The testing source address of the media transport, if one is set.
#[cfg(feature = "testing")]
pub(crate) fn source_ip(cfg: &ClientConfig) -> Option<IpAddr> {
    cfg.testing.source_ip.read().ok().and_then(|g| *g)
}

/// The media transport's source address: the OS chooses (no testing override in this build).
#[cfg(not(feature = "testing"))]
pub(crate) fn source_ip(_cfg: &ClientConfig) -> Option<IpAddr> {
    None
}

/// Where the engine's crypto provider came from.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ProviderSource {
    /// [`ClientConfig::with_crypto_provider`].
    Caller,
    /// `CryptoProvider::get_default` (installed by the host).
    ProcessDefault,
    /// ring, the built-in (feature `builtin-ring-provider`).
    #[cfg_attr(not(feature = "builtin-ring-provider"), allow(dead_code))]
    BuiltIn,
}

impl ProviderSource {
    fn as_str(self) -> &'static str {
        match self {
            ProviderSource::Caller => "caller",
            ProviderSource::ProcessDefault => "process default",
            ProviderSource::BuiltIn => "built-in",
        }
    }
}

/// The refusal when no provider is supplied, none is installed and the built-in is off.
#[cfg(not(feature = "builtin-ring-provider"))]
pub(crate) const NO_CRYPTO_PROVIDER: &str = "no_crypto_provider";

/// The crypto provider the engine's TLS uses: `explicit`, else the process default, else the
/// built-in ring provider (feature `builtin-ring-provider`); without that feature, an error.
/// The choice is logged at info the first time it is made and whenever it changes.
pub(crate) fn crypto_provider(
    explicit: Option<&Arc<CryptoProvider>>,
) -> Result<Arc<CryptoProvider>, rustls::Error> {
    let (provider, source) = choose_provider(explicit, CryptoProvider::get_default())?;
    log_choice(&provider, source);
    Ok(provider)
}

fn choose_provider(
    explicit: Option<&Arc<CryptoProvider>>,
    default: Option<&Arc<CryptoProvider>>,
) -> Result<(Arc<CryptoProvider>, ProviderSource), rustls::Error> {
    if let Some(p) = explicit {
        return Ok((p.clone(), ProviderSource::Caller));
    }
    if let Some(p) = default {
        return Ok((p.clone(), ProviderSource::ProcessDefault));
    }
    builtin_provider()
}

#[cfg(feature = "builtin-ring-provider")]
fn builtin_provider() -> Result<(Arc<CryptoProvider>, ProviderSource), rustls::Error> {
    Ok((
        Arc::new(rustls::crypto::ring::default_provider()),
        ProviderSource::BuiltIn,
    ))
}

#[cfg(not(feature = "builtin-ring-provider"))]
fn builtin_provider() -> Result<(Arc<CryptoProvider>, ProviderSource), rustls::Error> {
    Err(rustls::Error::General(format!(
        "{NO_CRYPTO_PROVIDER}: no rustls crypto provider was passed \
         (ClientConfig::with_crypto_provider) or installed as the process default, and the \
         built-in one is off (feature `builtin-ring-provider`)"
    )))
}

/// A provider's name: its random source's `Debug` name (`Ring` for ring's, `AwsLcRs` for
/// aws-lc-rs's; a host provider's own).
pub(crate) fn provider_name(p: &CryptoProvider) -> String {
    format!("{:?}", p.secure_random)
}

fn log_choice(provider: &CryptoProvider, source: ProviderSource) {
    static LAST: std::sync::Mutex<Option<(String, ProviderSource)>> = std::sync::Mutex::new(None);
    let choice = (provider_name(provider), source);
    let mut last = LAST
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    if last.as_ref() != Some(&choice) {
        tracing::info!("tls provider: {} ({})", choice.0, source.as_str());
        *last = Some(choice);
    }
}

/// The TLS client config every connection of the engine uses.
pub(crate) fn tls_config(cfg: &ClientConfig) -> Result<Arc<TlsConfig>, rustls::Error> {
    let provider = crypto_provider(cfg.crypto_provider.as_ref())?;
    #[cfg(feature = "testing")]
    let verifier = if cfg.testing.extra_roots.is_empty() {
        Verifier::new(provider.clone())?
    } else {
        let extra = cfg
            .testing
            .extra_roots
            .iter()
            .map(|d| rustls::pki_types::CertificateDer::from(d.clone()));
        Verifier::new_with_extra_roots(extra, provider.clone())?
    };
    #[cfg(not(feature = "testing"))]
    let verifier = {
        let _ = cfg;
        Verifier::new(provider.clone())?
    };
    let tls = TlsConfig::builder_with_provider(provider)
        .with_safe_default_protocol_versions()?
        .dangerous()
        .with_custom_certificate_verifier(Arc::new(verifier))
        .with_no_client_auth();
    Ok(Arc::new(tls))
}

/// The platform's trust alone (the key session's default when the engine gives it none), on the
/// process default provider, else the built-in.
pub(crate) fn platform_tls() -> Result<Arc<TlsConfig>, rustls::Error> {
    let provider = crypto_provider(None)?;
    let verifier = Verifier::new(provider.clone())?;
    let tls = TlsConfig::builder_with_provider(provider)
        .with_safe_default_protocol_versions()?
        .dangerous()
        .with_custom_certificate_verifier(Arc::new(verifier))
        .with_no_client_auth();
    Ok(Arc::new(tls))
}

impl ClientConfig {
    /// The TLS client config the engine uses for every connection (the platform's trust; in a
    /// `testing` build, plus the run CA), on the provider [`ClientConfig::crypto_provider`]
    /// describes. Hosts that set up the keys themselves
    /// ([`crate::e2ee::MlsKeys::prepare_session_join`]) pass it on.
    pub fn tls(&self) -> Result<Arc<TlsConfig>, rustls::Error> {
        tls_config(self)
    }
}

/// Resolves `host:port` (the testing overrides first).
pub(crate) async fn resolve(
    cfg: &ClientConfig,
    host: &str,
    port: u16,
) -> std::io::Result<std::net::SocketAddr> {
    let host = host.trim_start_matches('[').trim_end_matches(']');
    if let Ok(ip) = host.parse::<IpAddr>() {
        return Ok(std::net::SocketAddr::new(ip, port));
    }
    #[cfg(feature = "testing")]
    if let Some((_, ip)) = cfg.testing.resolve.iter().find(|(n, _)| n == host) {
        return Ok(std::net::SocketAddr::new(*ip, port));
    }
    #[cfg(not(feature = "testing"))]
    let _ = cfg;
    tokio::net::lookup_host((host, port))
        .await?
        .next()
        .ok_or_else(|| std::io::Error::other(format!("{host} does not resolve")))
}

/// Whether `host` names this machine (plain `ws://` and `http://` are allowed only there).
pub(crate) fn is_loopback_host(host: &str) -> bool {
    let h = host.trim_start_matches('[').trim_end_matches(']');
    h.eq_ignore_ascii_case("localhost") || h.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback())
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[cfg(feature = "builtin-ring-provider")]
    #[test]
    fn the_platform_verifier_builds() {
        let cfg = ClientConfig::new("test.app", std::env::temp_dir());
        assert!(tls_config(&cfg).is_ok());
    }

    #[cfg(feature = "builtin-ring-provider")]
    fn suites(p: &CryptoProvider) -> Vec<rustls::CipherSuite> {
        p.cipher_suites.iter().map(|s| s.suite()).collect()
    }

    #[cfg(feature = "builtin-ring-provider")]
    fn groups(p: &CryptoProvider) -> Vec<rustls::NamedGroup> {
        p.kx_groups.iter().map(|g| g.name()).collect()
    }

    /// A provider that is not ring's default: one cipher suite, one group.
    fn narrow() -> Arc<CryptoProvider> {
        let ring = rustls::crypto::ring::default_provider();
        Arc::new(CryptoProvider {
            cipher_suites: vec![rustls::crypto::ring::cipher_suite::TLS13_AES_128_GCM_SHA256],
            kx_groups: vec![rustls::crypto::ring::kx_group::X25519],
            ..ring
        })
    }

    #[test]
    fn the_explicit_provider_wins_then_the_default() {
        let explicit = narrow();
        let default = narrow();
        let (p, src) = choose_provider(Some(&explicit), Some(&default)).unwrap();
        assert!(Arc::ptr_eq(&p, &explicit), "the explicit provider is used");
        assert_eq!(src, ProviderSource::Caller);
        let (p, src) = choose_provider(None, Some(&default)).unwrap();
        assert!(Arc::ptr_eq(&p, &default), "the process default is used");
        assert_eq!(src, ProviderSource::ProcessDefault);
    }

    #[test]
    fn provider_names() {
        assert_eq!(
            provider_name(&rustls::crypto::ring::default_provider()),
            "Ring"
        );
    }

    /// Nothing in this test binary installs a process default, so the configs are ring's, as
    /// before the option existed (tests/tls_provider.rs covers an installed default; nothing
    /// here may call a rustls builder that installs one from the crate features).
    #[cfg(feature = "builtin-ring-provider")]
    #[test]
    fn without_a_default_the_configs_are_on_the_builtin_ring() {
        assert!(CryptoProvider::get_default().is_none());
        let ring = rustls::crypto::ring::default_provider();
        let (p, src) = choose_provider(None, None).unwrap();
        assert_eq!(src, ProviderSource::BuiltIn);
        assert_eq!(suites(&p), suites(&ring), "ring's suites");
        assert_eq!(groups(&p), groups(&ring), "ring's groups");
        let cfg = ClientConfig::new("test.app", std::env::temp_dir());
        for tls in [tls_config(&cfg).unwrap(), platform_tls().unwrap()] {
            assert_eq!(suites(tls.crypto_provider()), suites(&ring));
            assert_eq!(groups(tls.crypto_provider()), groups(&ring));
        }
        let explicit = narrow();
        let tls = tls_config(&cfg.with_crypto_provider(explicit.clone())).unwrap();
        assert!(Arc::ptr_eq(tls.crypto_provider(), &explicit));
    }

    /// Without the built-in and with nothing passed or installed, TLS setup is refused; an
    /// explicit provider still works.
    #[cfg(not(feature = "builtin-ring-provider"))]
    #[test]
    fn without_the_builtin_nothing_is_guessed() {
        assert!(CryptoProvider::get_default().is_none());
        let e = choose_provider(None, None).unwrap_err();
        assert!(e.to_string().contains(NO_CRYPTO_PROVIDER), "{e}");
        let cfg = ClientConfig::new("test.app", std::env::temp_dir());
        let e = tls_config(&cfg).unwrap_err();
        assert!(e.to_string().contains(NO_CRYPTO_PROVIDER), "{e}");
        let e = platform_tls().unwrap_err();
        assert!(e.to_string().contains(NO_CRYPTO_PROVIDER), "{e}");
        let explicit = narrow();
        let tls = tls_config(&cfg.with_crypto_provider(explicit.clone())).unwrap();
        assert!(Arc::ptr_eq(tls.crypto_provider(), &explicit));
    }

    #[test]
    fn loopback_hosts() {
        assert!(is_loopback_host("127.0.0.1"));
        assert!(is_loopback_host("[::1]"));
        assert!(is_loopback_host("localhost"));
        assert!(!is_loopback_host("voice.example"));
        assert!(!is_loopback_host("10.0.0.1"));
    }
}