rvoip-rtp-core 0.2.3

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
//! SDES Client Implementation
//!
//! This module provides SDES (Security DEScriptions) client functionality for the API layer.
//! It handles SDP crypto attribute parsing, key extraction, and SRTP context setup.

use async_trait::async_trait;
use std::any::Any;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

use crate::api::client::security::{ClientSecurityConfig, ClientSecurityContext};
use crate::api::common::config::{SecurityConfig, SecurityInfo, SecurityMode, SrtpProfile};
use crate::api::common::error::SecurityError;
use crate::api::server::security::SocketHandle;
use crate::security::{
    sdes::{Sdes, SdesConfig, SdesCryptoAttribute, SdesRole},
    SecurityKeyExchange,
};
use crate::srtp::{SrtpContext, SrtpCryptoSuite, SRTP_AES128_CM_SHA1_32, SRTP_AES128_CM_SHA1_80};

/// SDES client configuration
#[derive(Debug, Clone)]
pub struct SdesClientConfig {
    /// Supported SRTP profiles in order of preference
    pub supported_profiles: Vec<SrtpProfile>,
    /// Whether to validate incoming crypto attributes strictly
    pub strict_validation: bool,
    /// Maximum number of crypto attributes to accept in an offer
    pub max_crypto_attributes: usize,
}

impl Default for SdesClientConfig {
    fn default() -> Self {
        Self {
            supported_profiles: vec![
                SrtpProfile::AesCm128HmacSha1_80,
                SrtpProfile::AesCm128HmacSha1_32,
            ],
            strict_validation: true,
            max_crypto_attributes: 8,
        }
    }
}

/// SDES client state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SdesClientState {
    /// Initial state
    Initial,
    /// Processing offer
    ProcessingOffer,
    /// Answer sent, waiting for confirmation
    AnswerSent,
    /// Key exchange completed
    Completed,
    /// Error state
    Failed,
}

/// SDES client for handling SDP crypto attribute negotiation
pub struct SdesClient {
    /// Configuration
    config: SdesClientConfig,
    /// Current state
    state: Arc<RwLock<SdesClientState>>,
    /// Core SDES implementation
    sdes: Arc<RwLock<Sdes>>,
    /// Established SRTP context (if any)
    srtp_context: Arc<RwLock<Option<SrtpContext>>>,
    /// Selected crypto attribute
    selected_crypto: Arc<RwLock<Option<SdesCryptoAttribute>>>,
}

impl SdesClient {
    /// Create a new SDES client
    pub fn new(config: SdesClientConfig) -> Self {
        // Convert API config to core SDES config
        let sdes_config = SdesConfig {
            crypto_suites: Self::convert_srtp_profiles(&config.supported_profiles),
            offer_count: config.supported_profiles.len().min(4),
        };

        let sdes = Sdes::new(sdes_config, SdesRole::Answerer);

        Self {
            config,
            state: Arc::new(RwLock::new(SdesClientState::Initial)),
            sdes: Arc::new(RwLock::new(sdes)),
            srtp_context: Arc::new(RwLock::new(None)),
            selected_crypto: Arc::new(RwLock::new(None)),
        }
    }

    /// Create SDES client from security config
    pub fn from_security_config(config: &SecurityConfig) -> Self {
        let client_config = SdesClientConfig {
            supported_profiles: config.srtp_profiles.clone(),
            strict_validation: config.required,
            max_crypto_attributes: 8,
        };
        Self::new(client_config)
    }

    /// Convert API SRTP profiles to core crypto suites
    fn convert_srtp_profiles(profiles: &[SrtpProfile]) -> Vec<SrtpCryptoSuite> {
        profiles
            .iter()
            .filter_map(|profile| {
                match profile {
                    SrtpProfile::AesCm128HmacSha1_80 => Some(SRTP_AES128_CM_SHA1_80),
                    SrtpProfile::AesCm128HmacSha1_32 => Some(SRTP_AES128_CM_SHA1_32),
                    SrtpProfile::AesGcm128 => {
                        // AES-GCM not implemented in core yet
                        debug!("AES-GCM profile not yet supported, skipping");
                        None
                    }
                    SrtpProfile::AesGcm256 => {
                        // AES-GCM not implemented in core yet
                        debug!("AES-GCM-256 profile not yet supported, skipping");
                        None
                    }
                }
            })
            .collect()
    }

    /// Get current state
    pub async fn get_state(&self) -> SdesClientState {
        *self.state.read().await
    }

    /// Check if key exchange is complete
    pub async fn is_completed(&self) -> bool {
        *self.state.read().await == SdesClientState::Completed
    }

    /// Process an SDP offer containing crypto attributes
    /// Returns SDP answer lines with selected crypto attribute
    pub async fn process_offer(&self, sdp_offer: &[String]) -> Result<Vec<String>, SecurityError> {
        let mut state = self.state.write().await;
        if *state != SdesClientState::Initial {
            return Err(SecurityError::InvalidState(
                "SDES client not in initial state".to_string(),
            ));
        }

        *state = SdesClientState::ProcessingOffer;
        drop(state);

        info!(
            "SDES client processing SDP offer with {} lines",
            sdp_offer.len()
        );

        // Extract crypto lines from SDP offer
        let crypto_lines: Vec<String> = sdp_offer
            .iter()
            .filter(|line| line.trim().starts_with("a=crypto:"))
            .cloned()
            .collect();

        if crypto_lines.is_empty() {
            *self.state.write().await = SdesClientState::Failed;
            return Err(SecurityError::Configuration(
                "No crypto attributes found in SDP offer".to_string(),
            ));
        }

        if crypto_lines.len() > self.config.max_crypto_attributes {
            warn!(
                "SDP offer contains {} crypto attributes, limiting to {}",
                crypto_lines.len(),
                self.config.max_crypto_attributes
            );
        }

        info!("Found {} crypto attributes in offer", crypto_lines.len());
        for (i, line) in crypto_lines.iter().enumerate() {
            debug!("  Crypto {}: {}", i + 1, line);
        }

        // Process through core SDES implementation
        let offer_message = crypto_lines.join("\r\n");
        let mut sdes = self.sdes.write().await;

        match sdes.process_message(offer_message.as_bytes()) {
            Ok(Some(answer_bytes)) => {
                let answer_str = String::from_utf8(answer_bytes).map_err(|_| {
                    SecurityError::CryptoError("Invalid UTF-8 in SDES answer".to_string())
                })?;

                let answer_lines: Vec<String> = answer_str.lines().map(|s| s.to_string()).collect();

                // Set up SRTP context if keys are available
                if let (Some(srtp_key), Some(srtp_suite)) =
                    (sdes.get_srtp_key(), sdes.get_srtp_suite())
                {
                    match SrtpContext::new(srtp_suite, srtp_key) {
                        Ok(context) => {
                            *self.srtp_context.write().await = Some(context);
                            info!("SDES client: SRTP context established");
                        }
                        Err(e) => {
                            error!("Failed to create SRTP context: {}", e);
                            *self.state.write().await = SdesClientState::Failed;
                            return Err(SecurityError::CryptoError(format!(
                                "Failed to create SRTP context: {}",
                                e
                            )));
                        }
                    }
                }

                *self.state.write().await = SdesClientState::Completed;
                info!("SDES client: Key exchange completed successfully");

                Ok(answer_lines)
            }
            Ok(None) => {
                *self.state.write().await = SdesClientState::Failed;
                Err(SecurityError::CryptoError(
                    "No answer generated by SDES".to_string(),
                ))
            }
            Err(e) => {
                error!("SDES processing failed: {}", e);
                *self.state.write().await = SdesClientState::Failed;
                Err(SecurityError::CryptoError(format!(
                    "SDES processing failed: {}",
                    e
                )))
            }
        }
    }

    /// Get the established SRTP context
    pub async fn get_srtp_context(&self) -> Option<Arc<RwLock<SrtpContext>>> {
        let guard = self.srtp_context.read().await;
        if guard.is_some() {
            // For now, we'll return None due to the complex nested structure
            // In a production implementation, you'd want to restructure this
            // to avoid the Arc<RwLock<Option<T>>> pattern
            None
        } else {
            None
        }
    }

    /// Get selected crypto attribute info for logging/debugging
    pub async fn get_selected_crypto_info(&self) -> Option<String> {
        self.selected_crypto
            .read()
            .await
            .as_ref()
            .map(|attr| format!("tag={}, suite={}", attr.tag, attr.crypto_suite))
    }

    /// Parse crypto attributes from SDP lines (utility method)
    pub fn parse_crypto_attributes(
        sdp_lines: &[String],
    ) -> Result<Vec<SdesCryptoAttribute>, SecurityError> {
        let mut attributes = Vec::new();

        for line in sdp_lines {
            if let Some(crypto_part) = line.strip_prefix("a=crypto:") {
                match SdesCryptoAttribute::parse(crypto_part) {
                    Ok(attr) => attributes.push(attr),
                    Err(e) => {
                        warn!("Failed to parse crypto attribute '{}': {}", crypto_part, e);
                        return Err(SecurityError::Configuration(format!(
                            "Invalid crypto attribute: {}",
                            e
                        )));
                    }
                }
            }
        }

        Ok(attributes)
    }

    /// Generate crypto attributes for client-initiated offers (rare, but supported)
    pub async fn generate_offer(&self) -> Result<Vec<String>, SecurityError> {
        let mut state = self.state.write().await;
        if *state != SdesClientState::Initial {
            return Err(SecurityError::InvalidState(
                "SDES client not in initial state".to_string(),
            ));
        }

        // Change role to offerer temporarily
        let sdes_config = SdesConfig {
            crypto_suites: Self::convert_srtp_profiles(&self.config.supported_profiles),
            offer_count: self.config.supported_profiles.len().min(4),
        };

        let mut offerer_sdes = Sdes::new(sdes_config, SdesRole::Offerer);
        offerer_sdes
            .init()
            .map_err(|e| SecurityError::CryptoError(format!("SDES init failed: {}", e)))?;

        // Generate offer
        match offerer_sdes.process_message(b"") {
            Ok(Some(offer_bytes)) => {
                let offer_str = String::from_utf8(offer_bytes).map_err(|_| {
                    SecurityError::CryptoError("Invalid UTF-8 in SDES offer".to_string())
                })?;

                let offer_lines: Vec<String> = offer_str.lines().map(|s| s.to_string()).collect();

                *state = SdesClientState::AnswerSent;
                info!(
                    "SDES client generated offer with {} crypto attributes",
                    offer_lines.len()
                );

                Ok(offer_lines)
            }
            Ok(None) => Err(SecurityError::CryptoError(
                "No offer generated by SDES".to_string(),
            )),
            Err(e) => {
                error!("SDES offer generation failed: {}", e);
                Err(SecurityError::CryptoError(format!(
                    "SDES offer generation failed: {}",
                    e
                )))
            }
        }
    }
}

/// SRTP-only client security context (no DTLS handshake)
/// This implementation uses pre-shared keys negotiated through SIP/SDP
#[allow(dead_code)] // retained (liveness/Drop hold or reserved); not read
pub struct SrtpClientSecurityContext {
    /// Configuration
    config: ClientSecurityConfig,
    /// SDES client for key management
    #[allow(dead_code)] // retained (liveness/Drop hold or reserved); not read
    sdes_client: Arc<SdesClient>,
    /// Remote address
    remote_addr: Arc<RwLock<Option<SocketAddr>>>,
    /// Socket handle
    socket: Arc<RwLock<Option<SocketHandle>>>,
    /// Handshake completed flag (always true for pre-shared keys)
    handshake_completed: Arc<RwLock<bool>>,
}

impl SrtpClientSecurityContext {
    /// Create a new SRTP-only client security context
    pub async fn new(config: ClientSecurityConfig) -> Result<Arc<Self>, SecurityError> {
        // Create SDES client config from security config
        let sdes_config = SdesClientConfig {
            supported_profiles: config.srtp_profiles.clone(),
            strict_validation: true,
            max_crypto_attributes: 8,
        };

        let sdes_client = Arc::new(SdesClient::new(sdes_config));

        let ctx = Self {
            config,
            sdes_client,
            remote_addr: Arc::new(RwLock::new(None)),
            socket: Arc::new(RwLock::new(None)),
            handshake_completed: Arc::new(RwLock::new(true)), // Pre-shared keys = no handshake needed
        };

        info!("Created SRTP-only client security context (pre-shared keys)");
        Ok(Arc::new(ctx))
    }
}

#[async_trait]
impl ClientSecurityContext for SrtpClientSecurityContext {
    async fn initialize(&self) -> Result<(), SecurityError> {
        debug!("Initializing SRTP-only client security context");
        // No DTLS initialization needed for pre-shared keys
        Ok(())
    }

    async fn start_handshake(&self) -> Result<(), SecurityError> {
        debug!("SRTP-only: No handshake needed for pre-shared keys");
        // Pre-shared keys don't need a handshake
        Ok(())
    }

    async fn is_handshake_complete(&self) -> Result<bool, SecurityError> {
        // Always complete for pre-shared keys
        Ok(*self.handshake_completed.read().await)
    }

    async fn wait_for_handshake(&self) -> Result<(), SecurityError> {
        // No waiting needed for pre-shared keys
        Ok(())
    }

    async fn set_remote_address(&self, addr: SocketAddr) -> Result<(), SecurityError> {
        let mut remote_addr = self.remote_addr.write().await;
        *remote_addr = Some(addr);
        debug!("SRTP-only: Set remote address to {}", addr);
        Ok(())
    }

    async fn set_socket(&self, socket: SocketHandle) -> Result<(), SecurityError> {
        let mut socket_lock = self.socket.write().await;
        *socket_lock = Some(socket.clone());

        // Set remote address if available
        if let Some(remote_addr) = socket.remote_addr {
            let mut remote_addr_guard = self.remote_addr.write().await;
            *remote_addr_guard = Some(remote_addr);
        }

        debug!("SRTP-only: Set socket handle");
        Ok(())
    }

    async fn set_remote_fingerprint(
        &self,
        _fingerprint: &str,
        _algorithm: &str,
    ) -> Result<(), SecurityError> {
        debug!("SRTP-only: Fingerprint not used with pre-shared keys");
        // Not needed for pre-shared key SRTP
        Ok(())
    }

    async fn complete_handshake(
        &self,
        remote_addr: SocketAddr,
        _remote_fingerprint: &str,
    ) -> Result<(), SecurityError> {
        // Just set the remote address, no handshake needed
        self.set_remote_address(remote_addr).await?;
        debug!("SRTP-only: Complete handshake called - no handshake needed for pre-shared keys");
        Ok(())
    }

    async fn process_packet(&self, _data: &[u8]) -> Result<(), SecurityError> {
        debug!("SRTP-only: No packet processing needed for pre-shared keys");
        // No DTLS packet processing needed
        Ok(())
    }

    async fn start_packet_handler(&self) -> Result<(), SecurityError> {
        debug!("SRTP-only: No packet handler needed for pre-shared keys");
        // No DTLS packet handler needed
        Ok(())
    }

    async fn get_security_info(&self) -> Result<SecurityInfo, SecurityError> {
        let crypto_suites = self
            .config
            .srtp_profiles
            .iter()
            .map(|p| match p {
                SrtpProfile::AesCm128HmacSha1_80 => "AES_CM_128_HMAC_SHA1_80",
                SrtpProfile::AesCm128HmacSha1_32 => "AES_CM_128_HMAC_SHA1_32",
                SrtpProfile::AesGcm128 => "AEAD_AES_128_GCM",
                SrtpProfile::AesGcm256 => "AEAD_AES_256_GCM",
            })
            .map(|s| s.to_string())
            .collect::<Vec<_>>();

        Ok(SecurityInfo {
            mode: SecurityMode::Srtp,
            fingerprint: None, // No fingerprint for pre-shared keys
            fingerprint_algorithm: None,
            crypto_suites,
            key_params: Some("Pre-shared key (from SIP/SDP)".to_string()),
            srtp_profile: Some("AES_CM_128_HMAC_SHA1_80".to_string()),
        })
    }

    async fn close(&self) -> Result<(), SecurityError> {
        debug!("SRTP-only: Closing security context");
        let mut handshake_complete = self.handshake_completed.write().await;
        *handshake_complete = false;
        Ok(())
    }

    fn is_secure(&self) -> bool {
        true // Pre-shared key SRTP is secure
    }

    fn get_security_info_sync(&self) -> SecurityInfo {
        SecurityInfo {
            mode: SecurityMode::Srtp,
            fingerprint: None,
            fingerprint_algorithm: None,
            crypto_suites: vec!["AES_CM_128_HMAC_SHA1_80".to_string()],
            key_params: Some("Pre-shared key (from SIP/SDP)".to_string()),
            srtp_profile: Some("AES_CM_128_HMAC_SHA1_80".to_string()),
        }
    }

    async fn get_fingerprint(&self) -> Result<String, SecurityError> {
        Err(SecurityError::Configuration(
            "Fingerprints not used with pre-shared key SRTP".to_string(),
        ))
    }

    async fn get_fingerprint_algorithm(&self) -> Result<String, SecurityError> {
        Err(SecurityError::Configuration(
            "Fingerprint algorithm not used with pre-shared key SRTP".to_string(),
        ))
    }

    async fn has_transport(&self) -> Result<bool, SecurityError> {
        // For pre-shared keys, we don't need special DTLS transport
        Ok(self.socket.read().await.is_some())
    }

    async fn is_ready(&self) -> Result<bool, SecurityError> {
        // Check if socket is set and remote address is set
        let socket_set = self.socket.read().await.is_some();
        let remote_addr_set = self.remote_addr.read().await.is_some();

        let is_ready = socket_set && remote_addr_set;

        debug!("SRTP-only client security context ready: {}", is_ready);
        debug!("  - Socket set: {}", socket_set);
        debug!("  - Remote address set: {}", remote_addr_set);

        Ok(is_ready)
    }

    async fn process_dtls_packet(&self, _data: &[u8]) -> Result<(), SecurityError> {
        debug!("SRTP-only: No DTLS packet processing needed for pre-shared keys");
        // No DTLS processing for pre-shared keys
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    /// Get the security configuration
    fn get_config(&self) -> &ClientSecurityConfig {
        &self.config
    }
}