rvoip-rtp-core 0.2.2

RTP/RTCP protocol implementation for the rvoip stack
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! Common configuration types
//!
//! This module defines configuration types shared between client and server APIs.

use crate::api::common::frame::MediaFrameType;
use std::net::SocketAddr;

/// Security mode for transport
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SecurityMode {
    /// No security (plain RTP)
    None,

    /// SRTP with pre-shared keys
    Srtp,

    /// DTLS-SRTP (keys negotiated via DTLS)
    DtlsSrtp,

    /// SDES-SRTP (keys exchanged via SDP Security Descriptions)
    SdesSrtp,

    /// MIKEY-SRTP (keys negotiated via MIKEY protocol)
    MikeySrtp,

    /// ZRTP-SRTP (keys negotiated via ZRTP in-media protocol)
    ZrtpSrtp,
}

/// Key exchange method for SRTP security
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyExchangeMethod {
    /// DTLS key exchange (WebRTC style)
    DtlsSrtp,
    /// SDP Security Descriptions (SIP style)
    Sdes,
    /// Multimedia Internet KEYing protocol
    Mikey,
    /// Z Real-time Transport Protocol
    Zrtp,
    /// Pre-shared key (no key exchange)
    PreSharedKey,
}

impl From<SecurityMode> for Option<KeyExchangeMethod> {
    fn from(mode: SecurityMode) -> Self {
        match mode {
            SecurityMode::None => None,
            SecurityMode::Srtp => Some(KeyExchangeMethod::PreSharedKey),
            SecurityMode::DtlsSrtp => Some(KeyExchangeMethod::DtlsSrtp),
            SecurityMode::SdesSrtp => Some(KeyExchangeMethod::Sdes),
            SecurityMode::MikeySrtp => Some(KeyExchangeMethod::Mikey),
            SecurityMode::ZrtpSrtp => Some(KeyExchangeMethod::Zrtp),
        }
    }
}

impl KeyExchangeMethod {
    /// Get the security mode for this key exchange method
    pub fn to_security_mode(&self) -> SecurityMode {
        match self {
            KeyExchangeMethod::DtlsSrtp => SecurityMode::DtlsSrtp,
            KeyExchangeMethod::Sdes => SecurityMode::SdesSrtp,
            KeyExchangeMethod::Mikey => SecurityMode::MikeySrtp,
            KeyExchangeMethod::Zrtp => SecurityMode::ZrtpSrtp,
            KeyExchangeMethod::PreSharedKey => SecurityMode::Srtp,
        }
    }

    /// Check if this method requires network-based key exchange
    pub fn requires_network_exchange(&self) -> bool {
        match self {
            KeyExchangeMethod::DtlsSrtp
            | KeyExchangeMethod::Sdes
            | KeyExchangeMethod::Mikey
            | KeyExchangeMethod::Zrtp => true,
            KeyExchangeMethod::PreSharedKey => false,
        }
    }

    /// Check if this method exchanges keys via signaling (SDP)
    pub fn uses_signaling_exchange(&self) -> bool {
        match self {
            KeyExchangeMethod::Sdes => true,
            KeyExchangeMethod::DtlsSrtp
            | KeyExchangeMethod::Mikey
            | KeyExchangeMethod::Zrtp
            | KeyExchangeMethod::PreSharedKey => false,
        }
    }

    /// Check if this method exchanges keys via media path
    pub fn uses_media_exchange(&self) -> bool {
        match self {
            KeyExchangeMethod::Zrtp => true,
            KeyExchangeMethod::DtlsSrtp
            | KeyExchangeMethod::Sdes
            | KeyExchangeMethod::Mikey
            | KeyExchangeMethod::PreSharedKey => false,
        }
    }
}

impl SecurityMode {
    /// Check if security is enabled
    pub fn is_enabled(&self) -> bool {
        match self {
            SecurityMode::None => false,
            _ => true,
        }
    }

    /// Check if this mode requires SRTP
    pub fn requires_srtp(&self) -> bool {
        match self {
            SecurityMode::None => false,
            SecurityMode::Srtp
            | SecurityMode::DtlsSrtp
            | SecurityMode::SdesSrtp
            | SecurityMode::MikeySrtp
            | SecurityMode::ZrtpSrtp => true,
        }
    }

    /// Get the key exchange method for this security mode
    pub fn key_exchange_method(&self) -> Option<KeyExchangeMethod> {
        (*self).into()
    }
}

impl Default for SecurityMode {
    fn default() -> Self {
        SecurityMode::DtlsSrtp
    }
}

/// Identity validation mechanism
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IdentityValidation {
    /// No validation (use with caution)
    None,
    /// Fingerprint validation (DTLS)
    Fingerprint,
    /// Certificate validation (DTLS)
    Certificate,
    /// Custom validation
    Custom,
}

/// SRTP profiles for negotiation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SrtpProfile {
    /// AES_CM_128_HMAC_SHA1_80 (most common)
    AesCm128HmacSha1_80,
    /// AES_CM_128_HMAC_SHA1_32 (reduced auth tag for bandwidth savings)
    AesCm128HmacSha1_32,
    /// AEAD_AES_128_GCM (more secure, less overhead)
    AesGcm128,
    /// AEAD_AES_256_GCM (highest security)
    AesGcm256,
}

/// Network condition preset for buffer configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NetworkPreset {
    /// Minimal latency, good for LAN
    LowLatency,

    /// Balanced preset, good for stable broadband
    Balanced,

    /// Resilient preset, good for mobile or unstable networks
    Resilient,

    /// Maximum protection, for very unstable networks
    HighProtection,
}

/// Base transport configuration shared by client and server
#[derive(Debug, Clone)]
pub struct BaseTransportConfig {
    /// Local address to bind to
    pub local_address: Option<SocketAddr>,
    /// Whether to use RTCP multiplexing (RTP and RTCP on same port)
    pub rtcp_mux: bool,
    /// Media types enabled for this transport
    pub media_types: Vec<MediaFrameType>,
    /// Maximum transmission unit size
    pub mtu: usize,
}

/// Security information for SDP exchange
#[derive(Debug, Clone)]
pub struct SecurityInfo {
    /// Security mode (None, Srtp, DtlsSrtp)
    pub mode: SecurityMode,

    /// DTLS fingerprint (for DtlsSrtp)
    pub fingerprint: Option<String>,

    /// Fingerprint algorithm (for DtlsSrtp)
    pub fingerprint_algorithm: Option<String>,

    /// Crypto suites in order of preference
    pub crypto_suites: Vec<String>,

    /// Key parameters (for Srtp)
    pub key_params: Option<String>,

    /// Selected SRTP profile
    pub srtp_profile: Option<String>,
}

impl Default for SecurityInfo {
    fn default() -> Self {
        Self {
            mode: SecurityMode::None,
            fingerprint: None,
            fingerprint_algorithm: None,
            crypto_suites: Vec::new(),
            key_params: None,
            srtp_profile: None,
        }
    }
}

/// Predefined security profiles for common use cases
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SecurityProfile {
    /// No security - plain RTP
    Unsecured,

    /// Basic SRTP with pre-shared key (for simple deployments)
    SrtpBasic,

    /// DTLS-SRTP with self-signed certificates (common for WebRTC)
    DtlsSrtpSelfSigned,

    /// DTLS-SRTP with provided certificates (enterprise/production)
    DtlsSrtpCertificate,

    /// SDES-SRTP for SIP/SDP key exchange (telephony systems)
    SdesSrtp,

    /// MIKEY-SRTP for enterprise key management (pre-shared keys)
    MikeyPsk,

    /// MIKEY-SRTP for enterprise key management (public key exchange)
    MikeyPke,

    /// ZRTP for peer-to-peer secure calling (no PKI required)
    ZrtpP2P,

    /// Custom configuration (use the detailed SecurityConfig)
    Custom,
}

impl Default for SecurityProfile {
    fn default() -> Self {
        // WebRTC-style DTLS-SRTP is a good default for modern systems
        SecurityProfile::DtlsSrtpSelfSigned
    }
}

/// Complete security configuration with reasonable defaults
/// This struct makes it easy to configure security without understanding
/// all the underlying details of DTLS-SRTP, SRTP, etc.
#[derive(Debug, Clone)]
pub struct SecurityConfig {
    /// Security profile (for common configurations)
    pub profile: SecurityProfile,

    /// Security mode (None, SRTP, DTLS-SRTP)
    pub mode: SecurityMode,

    /// Whether security is required (fail if not available)
    pub required: bool,

    /// SRTP profiles in order of preference
    pub srtp_profiles: Vec<SrtpProfile>,

    /// Certificate file path (PEM format)
    pub certificate_path: Option<String>,

    /// Private key file path (PEM format)
    pub private_key_path: Option<String>,

    /// Fingerprint algorithm for DTLS
    pub fingerprint_algorithm: String,

    /// Pre-shared key for SRTP (used when mode is Srtp)
    pub srtp_key: Option<Vec<u8>>,

    /// Require client certificate validation
    pub require_client_certificate: bool,

    /// Remote fingerprint (if known, e.g. from SDP)
    pub remote_fingerprint: Option<String>,

    /// Remote fingerprint algorithm
    pub remote_fingerprint_algorithm: Option<String>,

    /// Certificate data in DER format (for MIKEY-PKE)
    pub certificate_data: Option<Vec<u8>>,

    /// Private key data in PKCS#8 DER format (for MIKEY-PKE)
    pub private_key_data: Option<Vec<u8>>,

    /// Peer certificate data in DER format (for MIKEY-PKE)
    pub peer_certificate_data: Option<Vec<u8>>,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            profile: SecurityProfile::default(),
            mode: SecurityMode::DtlsSrtp,
            required: true,
            srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80, SrtpProfile::AesGcm128],
            certificate_path: None,
            private_key_path: None,
            fingerprint_algorithm: "sha-256".to_string(),
            srtp_key: None,
            require_client_certificate: false,
            remote_fingerprint: None,
            remote_fingerprint_algorithm: None,
            certificate_data: None,
            private_key_data: None,
            peer_certificate_data: None,
        }
    }
}

impl SecurityConfig {
    /// Create a security configuration from a predefined profile
    pub fn from_profile(profile: SecurityProfile) -> Self {
        match profile {
            SecurityProfile::Unsecured => Self {
                profile,
                mode: SecurityMode::None,
                required: false,
                srtp_profiles: vec![],
                certificate_path: None,
                private_key_path: None,
                fingerprint_algorithm: "sha-256".to_string(),
                srtp_key: None,
                require_client_certificate: false,
                remote_fingerprint: None,
                remote_fingerprint_algorithm: None,
                certificate_data: None,
                private_key_data: None,
                peer_certificate_data: None,
            },

            SecurityProfile::SrtpBasic => {
                Self {
                    profile,
                    mode: SecurityMode::Srtp,
                    required: true,
                    srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80],
                    certificate_path: None,
                    private_key_path: None,
                    fingerprint_algorithm: "sha-256".to_string(),
                    // Default key will need to be set by the user
                    srtp_key: None,
                    require_client_certificate: false,
                    remote_fingerprint: None,
                    remote_fingerprint_algorithm: None,
                    certificate_data: None,
                    private_key_data: None,
                    peer_certificate_data: None,
                }
            }

            SecurityProfile::DtlsSrtpSelfSigned => {
                Self {
                    profile,
                    mode: SecurityMode::DtlsSrtp,
                    required: true,
                    srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80, SrtpProfile::AesGcm128],
                    certificate_path: None, // Will use self-signed
                    private_key_path: None, // Will use self-signed
                    fingerprint_algorithm: "sha-256".to_string(),
                    srtp_key: None, // Not needed for DTLS-SRTP
                    require_client_certificate: false,
                    remote_fingerprint: None,
                    remote_fingerprint_algorithm: None,
                    certificate_data: None,
                    private_key_data: None,
                    peer_certificate_data: None,
                }
            }

            SecurityProfile::DtlsSrtpCertificate => {
                Self {
                    profile,
                    mode: SecurityMode::DtlsSrtp,
                    required: true,
                    srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80, SrtpProfile::AesGcm128],
                    // Paths need to be set by user
                    certificate_path: None,
                    private_key_path: None,
                    fingerprint_algorithm: "sha-256".to_string(),
                    srtp_key: None,                    // Not needed for DTLS-SRTP
                    require_client_certificate: false, // Optional in most deployments
                    remote_fingerprint: None,
                    remote_fingerprint_algorithm: None,
                    certificate_data: None,
                    private_key_data: None,
                    peer_certificate_data: None,
                }
            }

            SecurityProfile::SdesSrtp => Self {
                profile,
                mode: SecurityMode::SdesSrtp,
                required: true,
                srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80],
                certificate_path: None,
                private_key_path: None,
                fingerprint_algorithm: "sha-256".to_string(),
                srtp_key: None,
                require_client_certificate: false,
                remote_fingerprint: None,
                remote_fingerprint_algorithm: None,
                certificate_data: None,
                private_key_data: None,
                peer_certificate_data: None,
            },

            SecurityProfile::MikeyPsk => Self {
                profile,
                mode: SecurityMode::MikeySrtp,
                required: true,
                srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80],
                certificate_path: None,
                private_key_path: None,
                fingerprint_algorithm: "sha-256".to_string(),
                srtp_key: None,
                require_client_certificate: false,
                remote_fingerprint: None,
                remote_fingerprint_algorithm: None,
                certificate_data: None,
                private_key_data: None,
                peer_certificate_data: None,
            },

            SecurityProfile::MikeyPke => Self {
                profile,
                mode: SecurityMode::MikeySrtp,
                required: true,
                srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80],
                certificate_path: None,
                private_key_path: None,
                fingerprint_algorithm: "sha-256".to_string(),
                srtp_key: None,
                require_client_certificate: false,
                remote_fingerprint: None,
                remote_fingerprint_algorithm: None,
                certificate_data: None,
                private_key_data: None,
                peer_certificate_data: None,
            },

            SecurityProfile::ZrtpP2P => Self {
                profile,
                mode: SecurityMode::ZrtpSrtp,
                required: true,
                srtp_profiles: vec![SrtpProfile::AesCm128HmacSha1_80],
                certificate_path: None,
                private_key_path: None,
                fingerprint_algorithm: "sha-256".to_string(),
                srtp_key: None,
                require_client_certificate: false,
                remote_fingerprint: None,
                remote_fingerprint_algorithm: None,
                certificate_data: None,
                private_key_data: None,
                peer_certificate_data: None,
            },

            SecurityProfile::Custom => {
                // Use all defaults for custom profile
                Self::default()
            }
        }
    }

    /// Create an unsecured configuration (plain RTP)
    pub fn unsecured() -> Self {
        Self::from_profile(SecurityProfile::Unsecured)
    }

    /// Create a basic SRTP configuration with a pre-shared key
    pub fn srtp_with_key(key: Vec<u8>) -> Self {
        let mut config = Self::from_profile(SecurityProfile::SrtpBasic);
        config.srtp_key = Some(key);
        config
    }

    /// Create a WebRTC-compatible DTLS-SRTP configuration with self-signed certificates
    pub fn webrtc_compatible() -> Self {
        Self::from_profile(SecurityProfile::DtlsSrtpSelfSigned)
    }

    /// Create a DTLS-SRTP configuration with provided certificate files
    pub fn dtls_with_certificate(cert_path: String, key_path: String) -> Self {
        let mut config = Self::from_profile(SecurityProfile::DtlsSrtpCertificate);
        config.certificate_path = Some(cert_path);
        config.private_key_path = Some(key_path);
        config
    }

    /// Create an SDES-SRTP configuration for SIP/SDP key exchange
    pub fn sdes_srtp() -> Self {
        Self::from_profile(SecurityProfile::SdesSrtp)
    }

    /// Create a MIKEY-SRTP configuration with pre-shared key
    pub fn mikey_psk() -> Self {
        Self::from_profile(SecurityProfile::MikeyPsk)
    }

    /// Create a MIKEY-SRTP configuration with public key exchange
    pub fn mikey_pke() -> Self {
        Self::from_profile(SecurityProfile::MikeyPke)
    }

    /// Create a ZRTP configuration for peer-to-peer secure calling
    pub fn zrtp_p2p() -> Self {
        Self::from_profile(SecurityProfile::ZrtpP2P)
    }

    /// Create a configuration that supports multiple key exchange methods with fallback
    pub fn multi_method(methods: Vec<KeyExchangeMethod>) -> Self {
        let primary_method = methods
            .first()
            .copied()
            .unwrap_or(KeyExchangeMethod::DtlsSrtp);
        let mut config = Self::from_profile(SecurityProfile::Custom);
        config.mode = primary_method.to_security_mode();
        config
    }

    // Predefined profile combinations for common SIP scenarios

    /// SIP enterprise configuration (MIKEY with PSK)
    pub fn sip_enterprise() -> Self {
        Self::mikey_psk()
    }

    /// SIP operator configuration (SDES with operator keys)
    pub fn sip_operator() -> Self {
        Self::sdes_srtp()
    }

    /// SIP peer-to-peer configuration (ZRTP for P2P calls)
    pub fn sip_peer_to_peer() -> Self {
        Self::zrtp_p2p()
    }

    /// SIP<->WebRTC bridge configuration (support both SDES and DTLS-SRTP)
    pub fn sip_webrtc_bridge() -> Self {
        // Start with SDES as primary, but this could be extended to support multiple methods
        Self::multi_method(vec![KeyExchangeMethod::Sdes, KeyExchangeMethod::DtlsSrtp])
    }

    /// Set the remote party's fingerprint (e.g. from SDP)
    pub fn with_remote_fingerprint(
        mut self,
        fingerprint: String,
        algorithm: Option<String>,
    ) -> Self {
        self.remote_fingerprint = Some(fingerprint);
        self.remote_fingerprint_algorithm =
            algorithm.or_else(|| Some(self.fingerprint_algorithm.clone()));
        self
    }

    /// Make security optional (don't fail if unavailable)
    pub fn with_optional_security(mut self) -> Self {
        self.required = false;
        self
    }

    /// Set certificate data for PKE mode (DER format)
    pub fn with_certificate_data(mut self, cert_data: Vec<u8>, private_key_data: Vec<u8>) -> Self {
        self.certificate_data = Some(cert_data);
        self.private_key_data = Some(private_key_data);
        self
    }

    /// Set peer certificate data for PKE mode (DER format)
    pub fn with_peer_certificate_data(mut self, peer_cert_data: Vec<u8>) -> Self {
        self.peer_certificate_data = Some(peer_cert_data);
        self
    }

    /// Create a MIKEY-PKE configuration with certificate data
    pub fn mikey_pke_with_certificates(
        cert_data: Vec<u8>,
        private_key_data: Vec<u8>,
        peer_cert_data: Vec<u8>,
    ) -> Self {
        Self::mikey_pke()
            .with_certificate_data(cert_data, private_key_data)
            .with_peer_certificate_data(peer_cert_data)
    }
}