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
use std::collections::HashMap;
use util::replay_detector::*;
use super::protection_profile::*;
use crate::{cipher, error::Error, option};
mod context_test;
mod srtcp_test;
mod srtp_test;
pub mod srtcp;
pub mod srtp;
pub const LABEL_SRTP_ENCRYPTION: u8 = 0x00;
pub const LABEL_SRTP_AUTHENTICATION_TAG: u8 = 0x01;
pub const LABEL_SRTP_SALT: u8 = 0x02;
pub const LABEL_SRTCP_ENCRYPTION: u8 = 0x03;
pub const LABEL_SRTCP_AUTHENTICATION_TAG: u8 = 0x04;
pub const LABEL_SRTCP_SALT: u8 = 0x05;
const MAX_ROC_DISORDER: u16 = 100;
pub(crate) const MAX_SEQUENCE_NUMBER: u16 = 65535;
pub(crate) const SRTCP_INDEX_SIZE: usize = 4;
#[derive(Default)]
pub struct SrtpSsrcState {
ssrc: u32,
rollover_counter: u32,
rollover_has_processed: bool,
last_sequence_number: u16,
replay_detector: Option<Box<dyn ReplayDetector>>,
}
#[derive(Default)]
pub struct SrtcpSsrcState {
srtcp_index: usize,
ssrc: u32,
replay_detector: Option<Box<dyn ReplayDetector>>,
}
impl SrtpSsrcState {
pub fn next_rollover_count(&self, sequence_number: u16) -> u32 {
let mut roc = self.rollover_counter;
if !self.rollover_has_processed {
} else if sequence_number == 0 {
if self.last_sequence_number > MAX_ROC_DISORDER {
roc += 1;
}
} else if self.last_sequence_number < MAX_ROC_DISORDER
&& sequence_number > (MAX_SEQUENCE_NUMBER - MAX_ROC_DISORDER)
{
roc -= 1;
} else if sequence_number < MAX_ROC_DISORDER
&& self.last_sequence_number > (MAX_SEQUENCE_NUMBER - MAX_ROC_DISORDER)
{
roc += 1;
}
roc
}
pub fn update_rollover_count(&mut self, sequence_number: u16) {
if !self.rollover_has_processed {
self.rollover_has_processed = true;
} else if sequence_number == 0 {
if self.last_sequence_number > MAX_ROC_DISORDER {
self.rollover_counter += 1;
}
} else if self.last_sequence_number < MAX_ROC_DISORDER
&& sequence_number > (MAX_SEQUENCE_NUMBER - MAX_ROC_DISORDER)
{
self.rollover_counter -= 1;
} else if sequence_number < MAX_ROC_DISORDER
&& self.last_sequence_number > (MAX_SEQUENCE_NUMBER - MAX_ROC_DISORDER)
{
self.rollover_counter += 1;
}
self.last_sequence_number = sequence_number;
}
}
pub struct Context {
cipher: Box<dyn cipher::Cipher + Send>,
srtp_ssrc_states: HashMap<u32, SrtpSsrcState>,
srtcp_ssrc_states: HashMap<u32, SrtcpSsrcState>,
new_srtp_replay_detector: option::ContextOption,
new_srtcp_replay_detector: option::ContextOption,
}
unsafe impl Send for Context {}
impl Context {
pub fn new(
master_key: &[u8],
master_salt: &[u8],
profile: ProtectionProfile,
srtp_ctx_opt: Option<option::ContextOption>,
srtcp_ctx_opt: Option<option::ContextOption>,
) -> Result<Context, Error> {
let key_len = profile.key_len();
let salt_len = profile.salt_len();
if master_key.len() != key_len {
return Err(Error::SrtpMasterKeyLength(key_len, master_key.len()));
} else if master_salt.len() != salt_len {
return Err(Error::SrtpSaltLength(salt_len, master_salt.len()));
}
let cipher: Box<dyn cipher::Cipher + Send> = match profile {
ProtectionProfile::AES128CMHMACSHA1_80 => {
Box::new(cipher::CipherAesCmHmacSha1::new(master_key, master_salt)?)
}
ProtectionProfile::AEADAES128GCM => {
Box::new(cipher::CipherAeadAesGcm::new(master_key, master_salt)?)
}
};
let srtp_ctx_opt = if let Some(ctx_opt) = srtp_ctx_opt {
ctx_opt
} else {
option::srtp_no_replay_protection()
};
let srtcp_ctx_opt = if let Some(ctx_opt) = srtcp_ctx_opt {
ctx_opt
} else {
option::srtcp_no_replay_protection()
};
Ok(Context {
cipher,
srtp_ssrc_states: HashMap::new(),
srtcp_ssrc_states: HashMap::new(),
new_srtp_replay_detector: srtp_ctx_opt,
new_srtcp_replay_detector: srtcp_ctx_opt,
})
}
fn get_srtp_ssrc_state(&mut self, ssrc: u32) -> Option<&mut SrtpSsrcState> {
let s = SrtpSsrcState {
ssrc,
replay_detector: Some((self.new_srtp_replay_detector)()),
..Default::default()
};
self.srtp_ssrc_states.entry(ssrc).or_insert(s);
self.srtp_ssrc_states.get_mut(&ssrc)
}
fn get_srtcp_ssrc_state(&mut self, ssrc: u32) -> Option<&mut SrtcpSsrcState> {
let s = SrtcpSsrcState {
ssrc,
replay_detector: Some((self.new_srtcp_replay_detector)()),
..Default::default()
};
self.srtcp_ssrc_states.entry(ssrc).or_insert(s);
self.srtcp_ssrc_states.get_mut(&ssrc)
}
fn get_roc(&self, ssrc: u32) -> Option<u32> {
if let Some(s) = self.srtp_ssrc_states.get(&ssrc) {
Some(s.rollover_counter)
} else {
None
}
}
fn set_roc(&mut self, ssrc: u32, roc: u32) {
if let Some(s) = self.get_srtp_ssrc_state(ssrc) {
s.rollover_counter = roc;
}
}
fn get_index(&self, ssrc: u32) -> Option<usize> {
if let Some(s) = self.srtcp_ssrc_states.get(&ssrc) {
Some(s.srtcp_index)
} else {
None
}
}
fn set_index(&mut self, ssrc: u32, index: usize) {
if let Some(s) = self.get_srtcp_ssrc_state(ssrc) {
s.srtcp_index = index;
}
}
}