oximedia-net 0.1.5

Network streaming for OxiMedia
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! DTLS (Datagram Transport Layer Security) wrapper.
//!
//! This module provides DTLS encryption for WebRTC connections.

#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]

use crate::error::{NetError, NetResult};
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use sha2::{Digest, Sha256};
use std::sync::Arc;
use tokio::net::UdpSocket;

/// DTLS role.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DtlsRole {
    /// Client role (active).
    Client,
    /// Server role (passive).
    Server,
}

impl DtlsRole {
    /// Parses from SDP setup attribute.
    #[must_use]
    pub fn from_setup(setup: &str) -> Option<Self> {
        match setup {
            "active" => Some(Self::Client),
            "passive" => Some(Self::Server),
            "actpass" => Some(Self::Server), // Default to server
            _ => None,
        }
    }

    /// Returns the SDP setup attribute.
    #[must_use]
    pub const fn to_setup(&self) -> &'static str {
        match self {
            Self::Client => "active",
            Self::Server => "passive",
        }
    }
}

/// DTLS fingerprint.
#[derive(Debug, Clone)]
pub struct DtlsFingerprint {
    /// Hash algorithm.
    pub algorithm: String,
    /// Fingerprint value (hex).
    pub value: String,
}

impl DtlsFingerprint {
    /// Creates a SHA-256 fingerprint from certificate.
    #[must_use]
    pub fn from_certificate(cert: &CertificateDer) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(cert.as_ref());
        let hash = hasher.finalize();

        let value = hash
            .iter()
            .map(|b| format!("{b:02X}"))
            .collect::<Vec<_>>()
            .join(":");

        Self {
            algorithm: "sha-256".to_string(),
            value,
        }
    }

    /// Formats for SDP.
    #[must_use]
    pub fn to_sdp(&self) -> String {
        format!("{} {}", self.algorithm, self.value)
    }
}

/// DTLS configuration.
pub struct DtlsConfig {
    /// Certificate chain.
    pub certificates: Vec<CertificateDer<'static>>,
    /// Private key.
    pub private_key: PrivateKeyDer<'static>,
    /// DTLS role.
    pub role: DtlsRole,
}

impl DtlsConfig {
    /// Creates a new configuration with self-signed certificate.
    pub fn new_self_signed(role: DtlsRole) -> NetResult<Self> {
        let (cert, key) = generate_self_signed_cert()?;

        Ok(Self {
            certificates: vec![cert],
            private_key: key,
            role,
        })
    }

    /// Gets the fingerprint.
    #[must_use]
    pub fn fingerprint(&self) -> DtlsFingerprint {
        DtlsFingerprint::from_certificate(&self.certificates[0])
    }
}

/// DTLS endpoint.
pub struct DtlsEndpoint {
    /// Configuration.
    config: DtlsConfig,
    /// UDP socket.
    socket: Arc<UdpSocket>,
}

impl DtlsEndpoint {
    /// Creates a new DTLS endpoint.
    #[must_use]
    pub fn new(config: DtlsConfig, socket: Arc<UdpSocket>) -> Self {
        Self { config, socket }
    }

    /// Performs DTLS handshake.
    ///
    /// Note: This is a simplified implementation. A production system would
    /// use a full DTLS library like openssl or rustls with DTLS support.
    pub async fn handshake(&self) -> NetResult<DtlsConnection> {
        // In a real implementation, we would:
        // 1. Perform DTLS handshake using rustls or similar
        // 2. Verify remote fingerprint
        // 3. Derive SRTP keys
        // 4. Return established connection

        // For now, return a mock connection
        Ok(DtlsConnection {
            socket: self.socket.clone(),
            srtp_key: vec![0u8; 16],
            srtp_salt: vec![0u8; 14],
        })
    }

    /// Gets the local fingerprint.
    #[must_use]
    pub fn fingerprint(&self) -> DtlsFingerprint {
        self.config.fingerprint()
    }
}

/// Established DTLS connection.
pub struct DtlsConnection {
    /// UDP socket.
    socket: Arc<UdpSocket>,
    /// SRTP master key.
    srtp_key: Vec<u8>,
    /// SRTP master salt.
    srtp_salt: Vec<u8>,
}

impl DtlsConnection {
    /// Sends encrypted data.
    pub async fn send(&self, data: &[u8]) -> NetResult<()> {
        // In a real implementation, encrypt with DTLS
        self.socket
            .send(data)
            .await
            .map_err(|e| NetError::connection(format!("Failed to send: {e}")))?;
        Ok(())
    }

    /// Receives encrypted data.
    pub async fn recv(&self, buf: &mut [u8]) -> NetResult<usize> {
        // In a real implementation, decrypt with DTLS
        self.socket
            .recv(buf)
            .await
            .map_err(|e| NetError::connection(format!("Failed to recv: {e}")))
    }

    /// Gets SRTP keying material.
    #[must_use]
    pub fn srtp_keying_material(&self) -> (&[u8], &[u8]) {
        (&self.srtp_key, &self.srtp_salt)
    }

    /// Gets the underlying socket.
    #[must_use]
    pub fn socket(&self) -> &Arc<UdpSocket> {
        &self.socket
    }
}

/// Generates a self-signed certificate.
fn generate_self_signed_cert() -> NetResult<(CertificateDer<'static>, PrivateKeyDer<'static>)> {
    use ed25519_dalek::pkcs8::EncodePrivateKey;
    use ed25519_dalek::SigningKey;
    use rand::Rng;

    let mut secret = [0u8; 32];
    rand::rng().fill_bytes(&mut secret);
    let signing_key = SigningKey::from_bytes(&secret);
    let pkcs8_doc = signing_key
        .to_pkcs8_der()
        .map_err(|e| NetError::protocol(format!("Failed to encode key as PKCS8: {e}")))?;

    // Create a simple self-signed certificate
    // In production, use rcgen or similar
    let cert_der = create_dummy_cert();
    let key_der = PrivateKeyDer::Pkcs8(pkcs8_doc.as_bytes().to_vec().into());

    Ok((cert_der, key_der))
}

/// Creates a dummy certificate for testing.
fn create_dummy_cert() -> CertificateDer<'static> {
    // This is a minimal valid X.509 certificate structure
    // In production, use rcgen or x509-certificate crate
    let cert_bytes = vec![
        0x30, 0x82, 0x01,
        0x00, // SEQUENCE
             // ... rest of certificate structure
             // This is simplified - a real cert would be much longer
    ];

    CertificateDer::from(cert_bytes)
}

// =============================================
// Extended DTLS simulation types
// =============================================

/// DTLS cipher suite.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DtlsCipherSuite {
    /// TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 (16-byte key).
    TlsEcdhEcdsaWithAes128GcmSha256,
    /// TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 (32-byte key).
    TlsEcdhEcdsaWithAes256GcmSha384,
    /// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (16-byte key).
    TlsEcdheRsaWithAes128GcmSha256,
}

impl DtlsCipherSuite {
    /// Returns the key length in bytes for this cipher suite.
    #[must_use]
    pub const fn key_length_bytes(self) -> usize {
        match self {
            Self::TlsEcdhEcdsaWithAes128GcmSha256 => 16,
            Self::TlsEcdhEcdsaWithAes256GcmSha384 => 32,
            Self::TlsEcdheRsaWithAes128GcmSha256 => 16,
        }
    }
}

/// DTLS handshake state for the simulated session.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DtlsHandshakeState {
    /// New, not yet started.
    New,
    /// Handshake in progress.
    Connecting,
    /// Handshake complete and connection established.
    Connected,
    /// Connection closed.
    Closed,
    /// Handshake failed.
    Failed,
}

/// Fingerprint hash algorithm.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FingerprintAlgorithm {
    /// SHA-256.
    Sha256,
    /// SHA-384.
    Sha384,
    /// SHA-512.
    Sha512,
}

impl FingerprintAlgorithm {
    /// Returns the algorithm name as used in SDP fingerprint attributes.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Sha256 => "sha-256",
            Self::Sha384 => "sha-384",
            Self::Sha512 => "sha-512",
        }
    }
}

/// A DTLS fingerprint with algorithm and hex value.
#[derive(Debug, Clone)]
pub struct DtlsFingerprintTyped {
    /// Hash algorithm.
    pub algorithm: FingerprintAlgorithm,
    /// Fingerprint hex value (colon-separated octets).
    pub value: String,
}

impl DtlsFingerprintTyped {
    /// Formats the fingerprint for an SDP attribute.
    #[must_use]
    pub fn to_sdp(&self) -> String {
        format!("{} {}", self.algorithm.name(), self.value)
    }
}

/// Simulated DTLS session (state-machine only, no real crypto).
#[derive(Debug)]
pub struct DtlsSession {
    /// Role (client or server).
    pub role: DtlsRole,
    /// Current handshake state.
    pub state: DtlsHandshakeState,
    /// Local fingerprint.
    pub local_fingerprint: DtlsFingerprintTyped,
    /// Remote fingerprint (set after receiving remote's Hello).
    pub remote_fingerprint: Option<DtlsFingerprintTyped>,
    /// Negotiated cipher suite.
    pub cipher_suite: Option<DtlsCipherSuite>,
    /// Simple session key derived at connect time (for simulation XOR).
    session_key: Vec<u8>,
}

impl DtlsSession {
    /// Creates a new DTLS session with a simulated fingerprint.
    #[must_use]
    pub fn new(role: DtlsRole) -> Self {
        // Generate a deterministic simulated fingerprint from role + timestamp.
        use std::time::{SystemTime, UNIX_EPOCH};
        let ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0xDEAD_BEEF);

        // Build a fake 32-byte fingerprint.
        let mut bytes = Vec::with_capacity(32);
        for i in 0..32u8 {
            bytes.push(
                ((ts >> (i % 8)) as u8)
                    .wrapping_add(i)
                    .wrapping_add(if role == DtlsRole::Client { 0xAA } else { 0x55 }),
            );
        }
        let value = bytes
            .iter()
            .map(|b| format!("{b:02X}"))
            .collect::<Vec<_>>()
            .join(":");

        Self {
            role,
            state: DtlsHandshakeState::New,
            local_fingerprint: DtlsFingerprintTyped {
                algorithm: FingerprintAlgorithm::Sha256,
                value,
            },
            remote_fingerprint: None,
            cipher_suite: None,
            session_key: vec![0u8; 16],
        }
    }

    /// Performs the simulated DTLS handshake, transitioning to Connected.
    ///
    /// Returns `true` if the handshake succeeds.
    pub fn connect(&mut self) -> bool {
        self.state = DtlsHandshakeState::Connecting;

        // Select cipher suite.
        self.cipher_suite = Some(DtlsCipherSuite::TlsEcdhEcdsaWithAes128GcmSha256);

        // Derive a simple session key (deterministic for simulation).
        let key_len = self
            .cipher_suite
            .map(|cs| cs.key_length_bytes())
            .unwrap_or(16);
        self.session_key = (0..key_len as u8).collect();

        self.state = DtlsHandshakeState::Connected;
        true
    }

    /// Protects (encrypts) data with a simple XOR using the session key.
    #[must_use]
    pub fn protect(&self, data: &[u8]) -> Vec<u8> {
        if self.session_key.is_empty() {
            return data.to_vec();
        }
        data.iter()
            .enumerate()
            .map(|(i, &b)| b ^ self.session_key[i % self.session_key.len()])
            .collect()
    }

    /// Unprotects (decrypts) data with the same XOR (symmetric).
    #[must_use]
    pub fn unprotect(&self, data: &[u8]) -> Vec<u8> {
        // XOR is its own inverse.
        self.protect(data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dtls_role() {
        assert_eq!(DtlsRole::from_setup("active"), Some(DtlsRole::Client));
        assert_eq!(DtlsRole::from_setup("passive"), Some(DtlsRole::Server));
        assert_eq!(DtlsRole::Client.to_setup(), "active");
    }

    #[test]
    fn test_fingerprint_from_cert() {
        let (cert, _) = generate_self_signed_cert().expect("should succeed in test");
        let fp = DtlsFingerprint::from_certificate(&cert);
        assert_eq!(fp.algorithm, "sha-256");
        assert!(fp.value.contains(':'));
    }

    #[test]
    fn test_fingerprint_sdp() {
        let fp = DtlsFingerprint {
            algorithm: "sha-256".to_string(),
            value: "AA:BB:CC:DD".to_string(),
        };
        assert_eq!(fp.to_sdp(), "sha-256 AA:BB:CC:DD");
    }

    // ---- Extended DTLS simulation tests ----

    #[test]
    fn test_cipher_suite_key_length() {
        assert_eq!(
            DtlsCipherSuite::TlsEcdhEcdsaWithAes128GcmSha256.key_length_bytes(),
            16
        );
        assert_eq!(
            DtlsCipherSuite::TlsEcdhEcdsaWithAes256GcmSha384.key_length_bytes(),
            32
        );
        assert_eq!(
            DtlsCipherSuite::TlsEcdheRsaWithAes128GcmSha256.key_length_bytes(),
            16
        );
    }

    #[test]
    fn test_fingerprint_algorithm_name() {
        assert_eq!(FingerprintAlgorithm::Sha256.name(), "sha-256");
        assert_eq!(FingerprintAlgorithm::Sha384.name(), "sha-384");
        assert_eq!(FingerprintAlgorithm::Sha512.name(), "sha-512");
    }

    #[test]
    fn test_dtls_fingerprint_typed_sdp() {
        let fp = DtlsFingerprintTyped {
            algorithm: FingerprintAlgorithm::Sha256,
            value: "AA:BB:CC".to_string(),
        };
        assert_eq!(fp.to_sdp(), "sha-256 AA:BB:CC");
    }

    #[test]
    fn test_dtls_session_new_client() {
        let session = DtlsSession::new(DtlsRole::Client);
        assert_eq!(session.role, DtlsRole::Client);
        assert_eq!(session.state, DtlsHandshakeState::New);
        assert!(session.cipher_suite.is_none());
        assert!(session.remote_fingerprint.is_none());
        assert!(!session.local_fingerprint.value.is_empty());
    }

    #[test]
    fn test_dtls_session_new_server() {
        let session = DtlsSession::new(DtlsRole::Server);
        assert_eq!(session.role, DtlsRole::Server);
        assert_eq!(session.state, DtlsHandshakeState::New);
    }

    #[test]
    fn test_dtls_session_connect() {
        let mut session = DtlsSession::new(DtlsRole::Client);
        let ok = session.connect();
        assert!(ok);
        assert_eq!(session.state, DtlsHandshakeState::Connected);
        assert!(session.cipher_suite.is_some());
    }

    #[test]
    fn test_dtls_session_protect_unprotect() {
        let mut session = DtlsSession::new(DtlsRole::Client);
        session.connect();

        let original = b"Hello DTLS world!";
        let protected = session.protect(original);
        assert_ne!(protected, original.to_vec());

        let unprotected = session.unprotect(&protected);
        assert_eq!(unprotected, original.to_vec());
    }

    #[test]
    fn test_dtls_session_protect_empty() {
        let mut session = DtlsSession::new(DtlsRole::Server);
        session.connect();

        let protected = session.protect(b"");
        assert!(protected.is_empty());
    }

    #[test]
    fn test_dtls_handshake_state_variants() {
        let states = [
            DtlsHandshakeState::New,
            DtlsHandshakeState::Connecting,
            DtlsHandshakeState::Connected,
            DtlsHandshakeState::Closed,
            DtlsHandshakeState::Failed,
        ];
        for &s in &states {
            assert_eq!(s, s);
        }
    }
}