Line | Count | Source |
1 | | /* |
2 | | * srtp.c |
3 | | * |
4 | | * the secure real-time transport protocol |
5 | | * |
6 | | * David A. McGrew |
7 | | * Cisco Systems, Inc. |
8 | | */ |
9 | | /* |
10 | | * |
11 | | * Copyright (c) 2001-2017, Cisco Systems, Inc. |
12 | | * All rights reserved. |
13 | | * |
14 | | * Redistribution and use in source and binary forms, with or without |
15 | | * modification, are permitted provided that the following conditions |
16 | | * are met: |
17 | | * |
18 | | * Redistributions of source code must retain the above copyright |
19 | | * notice, this list of conditions and the following disclaimer. |
20 | | * |
21 | | * Redistributions in binary form must reproduce the above |
22 | | * copyright notice, this list of conditions and the following |
23 | | * disclaimer in the documentation and/or other materials provided |
24 | | * with the distribution. |
25 | | * |
26 | | * Neither the name of the Cisco Systems, Inc. nor the names of its |
27 | | * contributors may be used to endorse or promote products derived |
28 | | * from this software without specific prior written permission. |
29 | | * |
30 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
33 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
34 | | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
35 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
36 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
37 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
38 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
39 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
40 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
41 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
42 | | * |
43 | | */ |
44 | | |
45 | | // Leave this as the top level import. Ensures the existence of defines |
46 | | #include "config.h" |
47 | | |
48 | | #include "srtp_priv.h" |
49 | | #include "stream_list_priv.h" |
50 | | #include "crypto_types.h" |
51 | | #include "err.h" |
52 | | #include "alloc.h" /* for srtp_crypto_alloc() */ |
53 | | |
54 | | #ifdef GCM |
55 | | #include "aes_gcm.h" /* for AES GCM mode */ |
56 | | #endif |
57 | | |
58 | | #ifdef OPENSSL_KDF |
59 | | #include <openssl/kdf.h> |
60 | | #include "aes_icm_ext.h" |
61 | | #endif |
62 | | |
63 | | #ifdef WOLFSSL |
64 | | #ifdef HAVE_CONFIG_H |
65 | | #include <config.h> |
66 | | #endif |
67 | | #ifndef WOLFSSL_USER_SETTINGS |
68 | | #include <wolfssl/options.h> |
69 | | #endif |
70 | | #include <wolfssl/wolfcrypt/settings.h> |
71 | | #ifdef WOLFSSL_KDF |
72 | | #include <wolfssl/wolfcrypt/kdf.h> |
73 | | #endif |
74 | | #endif |
75 | | |
76 | | #include <limits.h> |
77 | | #ifdef HAVE_NETINET_IN_H |
78 | | #include <netinet/in.h> |
79 | | #elif defined(HAVE_WINSOCK2_H) |
80 | | #include <winsock2.h> |
81 | | #endif |
82 | | |
83 | | /* the debug module for srtp */ |
84 | | srtp_debug_module_t mod_srtp = { |
85 | | false, /* debugging is off by default */ |
86 | | "srtp" /* printable name for module */ |
87 | | }; |
88 | | |
89 | | static const size_t octets_in_rtp_header = 12; |
90 | | static const size_t octets_in_rtcp_header = 8; |
91 | | static const size_t octets_in_rtp_xtn_hdr = 4; |
92 | | |
93 | | static const uint16_t xtn_hdr_one_byte_profile = 0xbede; |
94 | | static const uint16_t xtn_hdr_two_byte_profile = 0x1000; |
95 | | |
96 | | static const uint16_t cryptex_one_byte_profile = 0xc0de; |
97 | | static const uint16_t cryptex_two_byte_profile = 0xc2de; |
98 | | |
99 | | static size_t srtp_get_rtp_hdr_len(const srtp_hdr_t *hdr) |
100 | 239k | { |
101 | 239k | return octets_in_rtp_header + 4 * hdr->cc; |
102 | 239k | } |
103 | | |
104 | | /* |
105 | | * Returns the location of the header extention cast to a srtp_hdr_xtnd_t |
106 | | * struct. Will always return a value and assumes that the caller has already |
107 | | * verified that a header extension is present by checking the x bit of |
108 | | * srtp_hdr_t. |
109 | | */ |
110 | | static srtp_hdr_xtnd_t *srtp_get_rtp_xtn_hdr(const srtp_hdr_t *hdr, |
111 | | uint8_t *rtp) |
112 | 1.53k | { |
113 | 1.53k | return (srtp_hdr_xtnd_t *)(rtp + srtp_get_rtp_hdr_len(hdr)); |
114 | 1.53k | } |
115 | | |
116 | | /* |
117 | | * Returns the length of the extension header including the extension header |
118 | | * header so will return a minium of 4. Assumes the srtp_hdr_t is a valid |
119 | | * pointer and that the caller has already verified that a header extension is |
120 | | * valid by checking the x bit of the RTP header. |
121 | | */ |
122 | | static size_t srtp_get_rtp_hdr_xtnd_len(const srtp_hdr_t *hdr, |
123 | | const uint8_t *rtp) |
124 | 6.63k | { |
125 | 6.63k | const srtp_hdr_xtnd_t *xtn_hdr = |
126 | 6.63k | (const srtp_hdr_xtnd_t *)(rtp + srtp_get_rtp_hdr_len(hdr)); |
127 | 6.63k | return (ntohs(xtn_hdr->length) + 1u) * 4u; |
128 | 6.63k | } |
129 | | |
130 | | static uint16_t srtp_get_rtp_hdr_xtnd_profile(const srtp_hdr_t *hdr, |
131 | | const uint8_t *rtp) |
132 | 0 | { |
133 | 0 | const srtp_hdr_xtnd_t *xtn_hdr = |
134 | 0 | (const srtp_hdr_xtnd_t *)(rtp + srtp_get_rtp_hdr_len(hdr)); |
135 | 0 | return ntohs(xtn_hdr->profile_specific); |
136 | 0 | } |
137 | | |
138 | | static void srtp_cryptex_move_hdr_xtnd_hdr_before_csrc(const srtp_hdr_t *hdr, |
139 | | uint8_t *rtp) |
140 | 0 | { |
141 | 0 | if (hdr->cc) { |
142 | 0 | uint8_t tmp[4]; |
143 | 0 | uint8_t *xtn_hdr = rtp + srtp_get_rtp_hdr_len(hdr); |
144 | 0 | uint8_t *csrc_list = rtp + octets_in_rtp_header; |
145 | 0 | size_t csrc_list_size = hdr->cc * 4; |
146 | 0 | memcpy(tmp, xtn_hdr, 4); |
147 | 0 | memmove(csrc_list + 4, csrc_list, csrc_list_size); |
148 | 0 | memcpy(csrc_list, tmp, 4); |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | static void srtp_cryptex_move_csrc_before_hdr_xtnd_hdr(const srtp_hdr_t *hdr, |
153 | | uint8_t *rtp) |
154 | 0 | { |
155 | 0 | if (hdr->cc) { |
156 | 0 | uint8_t tmp[4]; |
157 | 0 | uint8_t *xtn_hdr = rtp + srtp_get_rtp_hdr_len(hdr); |
158 | 0 | uint8_t *csrc_list = rtp + octets_in_rtp_header; |
159 | 0 | size_t csrc_list_size = hdr->cc * 4; |
160 | 0 | memcpy(tmp, csrc_list, 4); |
161 | 0 | memmove(csrc_list, csrc_list + 4, csrc_list_size); |
162 | 0 | memcpy(xtn_hdr, tmp, 4); |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | static srtp_err_status_t srtp_cryptex_protect_init( |
167 | | const srtp_stream_ctx_t *stream, |
168 | | const srtp_hdr_t *hdr, |
169 | | const uint8_t *rtp, |
170 | | const uint8_t *srtp, |
171 | | bool *inuse, |
172 | | bool *inplace, |
173 | | size_t *enc_start) |
174 | 24.7k | { |
175 | 24.7k | if (stream->use_cryptex && (stream->rtp_services & sec_serv_conf)) { |
176 | 0 | if (hdr->cc && hdr->x == 0) { |
177 | | /* Cryptex can only encrypt CSRCs if header extension is present */ |
178 | 0 | return srtp_err_status_cryptex_err; |
179 | 0 | } |
180 | 0 | *inuse = hdr->x == 1; |
181 | 24.7k | } else { |
182 | 24.7k | *inuse = false; |
183 | 24.7k | } |
184 | | |
185 | 24.7k | *inplace = *inuse && rtp == srtp; |
186 | | |
187 | 24.7k | if (*inuse) { |
188 | 0 | *enc_start -= |
189 | 0 | (srtp_get_rtp_hdr_xtnd_len(hdr, rtp) - octets_in_rtp_xtn_hdr); |
190 | 0 | if (*inplace) { |
191 | 0 | *enc_start -= (hdr->cc * 4); |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | 24.7k | return srtp_err_status_ok; |
196 | 24.7k | } |
197 | | |
198 | | static srtp_err_status_t srtp_cryptex_protect(bool inplace, |
199 | | const srtp_hdr_t *hdr, |
200 | | uint8_t *srtp, |
201 | | srtp_cipher_t *rtp_cipher) |
202 | 0 | { |
203 | 0 | srtp_hdr_xtnd_t *xtn_hdr = srtp_get_rtp_xtn_hdr(hdr, srtp); |
204 | 0 | uint16_t profile = ntohs(xtn_hdr->profile_specific); |
205 | 0 | if (profile == xtn_hdr_one_byte_profile) { |
206 | 0 | xtn_hdr->profile_specific = htons(cryptex_one_byte_profile); |
207 | 0 | } else if (profile == xtn_hdr_two_byte_profile) { |
208 | 0 | xtn_hdr->profile_specific = htons(cryptex_two_byte_profile); |
209 | 0 | } else { |
210 | 0 | return srtp_err_status_parse_err; |
211 | 0 | } |
212 | | |
213 | 0 | if (inplace) { |
214 | 0 | srtp_cryptex_move_hdr_xtnd_hdr_before_csrc(hdr, srtp); |
215 | 0 | } else { |
216 | 0 | if (hdr->cc) { |
217 | 0 | uint8_t *cc_list = srtp + octets_in_rtp_header; |
218 | 0 | size_t cc_list_size = hdr->cc * 4; |
219 | | /* CSRCs are in dst header already, enc in place */ |
220 | 0 | srtp_err_status_t status = srtp_cipher_encrypt( |
221 | 0 | rtp_cipher, cc_list, cc_list_size, cc_list, &cc_list_size); |
222 | 0 | if (status) { |
223 | 0 | return srtp_err_status_cipher_fail; |
224 | 0 | } |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 0 | return srtp_err_status_ok; |
229 | 0 | } |
230 | | |
231 | | static void srtp_cryptex_protect_cleanup(bool inplace, |
232 | | const srtp_hdr_t *hdr, |
233 | | uint8_t *srtp) |
234 | 0 | { |
235 | 0 | if (inplace) { |
236 | 0 | srtp_cryptex_move_csrc_before_hdr_xtnd_hdr(hdr, srtp); |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | | static srtp_err_status_t srtp_cryptex_unprotect_init( |
241 | | const srtp_stream_ctx_t *stream, |
242 | | const srtp_hdr_t *hdr, |
243 | | const uint8_t *srtp, |
244 | | const uint8_t *rtp, |
245 | | bool *inuse, |
246 | | bool *inplace, |
247 | | size_t *enc_start) |
248 | 90.9k | { |
249 | 90.9k | if (stream->use_cryptex && hdr->x == 1) { |
250 | 0 | uint16_t profile = srtp_get_rtp_hdr_xtnd_profile(hdr, rtp); |
251 | 0 | *inuse = profile == cryptex_one_byte_profile || |
252 | 0 | profile == cryptex_two_byte_profile; |
253 | 90.9k | } else { |
254 | 90.9k | *inuse = false; |
255 | 90.9k | } |
256 | | |
257 | 90.9k | *inplace = *inuse && srtp == rtp; |
258 | | |
259 | 90.9k | if (*inuse) { |
260 | 0 | *enc_start -= |
261 | 0 | (srtp_get_rtp_hdr_xtnd_len(hdr, rtp) - octets_in_rtp_xtn_hdr); |
262 | 0 | if (*inplace) { |
263 | 0 | *enc_start -= (hdr->cc * 4); |
264 | 0 | } |
265 | 0 | } |
266 | | |
267 | 90.9k | return srtp_err_status_ok; |
268 | 90.9k | } |
269 | | |
270 | | static srtp_err_status_t srtp_cryptex_unprotect(bool inplace, |
271 | | const srtp_hdr_t *hdr, |
272 | | uint8_t *rtp, |
273 | | srtp_cipher_t *rtp_cipher) |
274 | 0 | { |
275 | 0 | if (inplace) { |
276 | 0 | srtp_cryptex_move_hdr_xtnd_hdr_before_csrc(hdr, rtp); |
277 | 0 | } else { |
278 | 0 | if (hdr->cc) { |
279 | 0 | uint8_t *cc_list = rtp + octets_in_rtp_header; |
280 | 0 | size_t cc_list_size = hdr->cc * 4; |
281 | | /* CSRCs are in dst header already, enc in place */ |
282 | 0 | srtp_err_status_t status = srtp_cipher_decrypt( |
283 | 0 | rtp_cipher, cc_list, cc_list_size, cc_list, &cc_list_size); |
284 | 0 | if (status) { |
285 | 0 | return srtp_err_status_cipher_fail; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | 0 | return srtp_err_status_ok; |
291 | 0 | } |
292 | | |
293 | | static void srtp_cryptex_unprotect_cleanup(bool inplace, |
294 | | const srtp_hdr_t *hdr, |
295 | | uint8_t *rtp) |
296 | 0 | { |
297 | 0 | if (inplace) { |
298 | 0 | srtp_cryptex_move_csrc_before_hdr_xtnd_hdr(hdr, rtp); |
299 | 0 | } |
300 | |
|
301 | 0 | srtp_hdr_xtnd_t *xtn_hdr = srtp_get_rtp_xtn_hdr(hdr, rtp); |
302 | 0 | uint16_t profile = ntohs(xtn_hdr->profile_specific); |
303 | 0 | if (profile == cryptex_one_byte_profile) { |
304 | 0 | xtn_hdr->profile_specific = htons(xtn_hdr_one_byte_profile); |
305 | 0 | } else if (profile == cryptex_two_byte_profile) { |
306 | 0 | xtn_hdr->profile_specific = htons(xtn_hdr_two_byte_profile); |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | | static srtp_err_status_t srtp_validate_rtp_header(const uint8_t *rtp, |
311 | | size_t pkt_octet_len) |
312 | 116k | { |
313 | 116k | const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp; |
314 | 116k | size_t rtp_header_len; |
315 | | |
316 | 116k | if (pkt_octet_len < octets_in_rtp_header) { |
317 | 23 | return srtp_err_status_bad_param; |
318 | 23 | } |
319 | | |
320 | | /* Check RTP header length */ |
321 | 116k | rtp_header_len = srtp_get_rtp_hdr_len(hdr); |
322 | 116k | if (pkt_octet_len < rtp_header_len) { |
323 | 33 | return srtp_err_status_bad_param; |
324 | 33 | } |
325 | | |
326 | | /* Verifying profile length. */ |
327 | 116k | if (hdr->x == 1) { |
328 | 3.37k | if (pkt_octet_len < rtp_header_len + octets_in_rtp_xtn_hdr) { |
329 | 13 | return srtp_err_status_bad_param; |
330 | 13 | } |
331 | | |
332 | 3.36k | rtp_header_len += srtp_get_rtp_hdr_xtnd_len(hdr, rtp); |
333 | 3.36k | if (pkt_octet_len < rtp_header_len) { |
334 | 74 | return srtp_err_status_bad_param; |
335 | 74 | } |
336 | 3.36k | } |
337 | | |
338 | 115k | return srtp_err_status_ok; |
339 | 116k | } |
340 | | |
341 | | const char *srtp_get_version_string(void) |
342 | 0 | { |
343 | | /* |
344 | | * Simply return the autotools generated string |
345 | | */ |
346 | 0 | return SRTP_VER_STRING; |
347 | 0 | } |
348 | | |
349 | | unsigned int srtp_get_version(void) |
350 | 0 | { |
351 | 0 | unsigned int major = 0, minor = 0, micro = 0; |
352 | 0 | unsigned int rv = 0; |
353 | 0 | int parse_rv; |
354 | | |
355 | | /* |
356 | | * Parse the autotools generated version |
357 | | */ |
358 | 0 | parse_rv = sscanf(SRTP_VERSION, "%u.%u.%u", &major, &minor, µ); |
359 | 0 | if (parse_rv != 3) { |
360 | | /* |
361 | | * We're expected to parse all 3 version levels. |
362 | | * If not, then this must not be an official release. |
363 | | * Return all zeros on the version |
364 | | */ |
365 | 0 | return (0); |
366 | 0 | } |
367 | | |
368 | | /* |
369 | | * We allow 8 bits for the major and minor, while |
370 | | * allowing 16 bits for the micro. 16 bits for the micro |
371 | | * may be beneficial for a continuous delivery model |
372 | | * in the future. |
373 | | */ |
374 | 0 | rv |= (major & 0xFF) << 24; |
375 | 0 | rv |= (minor & 0xFF) << 16; |
376 | 0 | rv |= micro & 0xFF; |
377 | 0 | return rv; |
378 | 0 | } |
379 | | |
380 | | static srtp_err_status_t srtp_stream_dealloc( |
381 | | srtp_stream_ctx_t *stream, |
382 | | const srtp_stream_ctx_t *stream_template) |
383 | 100k | { |
384 | 100k | srtp_err_status_t status; |
385 | 100k | srtp_session_keys_t *session_keys = NULL; |
386 | 100k | srtp_session_keys_t *template_session_keys = NULL; |
387 | | |
388 | | /* |
389 | | * we use a conservative deallocation strategy - if any deallocation |
390 | | * fails, then we report that fact without trying to deallocate |
391 | | * anything else |
392 | | */ |
393 | 100k | if (stream->session_keys) { |
394 | 207k | for (size_t i = 0; i < stream->num_master_keys; i++) { |
395 | 106k | session_keys = &stream->session_keys[i]; |
396 | | |
397 | 106k | if (stream_template && |
398 | 99.9k | stream->num_master_keys == stream_template->num_master_keys) { |
399 | 98.1k | template_session_keys = &stream_template->session_keys[i]; |
400 | 98.1k | } else { |
401 | 8.05k | template_session_keys = NULL; |
402 | 8.05k | } |
403 | | |
404 | | /* |
405 | | * deallocate cipher, if it is not the same as that in template |
406 | | */ |
407 | 106k | if (template_session_keys && |
408 | 98.1k | session_keys->rtp_cipher == template_session_keys->rtp_cipher) { |
409 | | /* do nothing */ |
410 | 92.0k | } else if (session_keys->rtp_cipher) { |
411 | 14.1k | status = srtp_cipher_dealloc(session_keys->rtp_cipher); |
412 | 14.1k | if (status) { |
413 | 0 | return status; |
414 | 0 | } |
415 | 14.1k | } |
416 | | |
417 | | /* |
418 | | * deallocate auth function, if it is not the same as that in |
419 | | * template |
420 | | */ |
421 | 106k | if (template_session_keys && |
422 | 98.1k | session_keys->rtp_auth == template_session_keys->rtp_auth) { |
423 | | /* do nothing */ |
424 | 92.0k | } else if (session_keys->rtp_auth) { |
425 | 14.1k | status = srtp_auth_dealloc(session_keys->rtp_auth); |
426 | 14.1k | if (status) { |
427 | 0 | return status; |
428 | 0 | } |
429 | 14.1k | } |
430 | | |
431 | 106k | if (template_session_keys && |
432 | 98.1k | session_keys->rtp_xtn_hdr_cipher == |
433 | 98.1k | template_session_keys->rtp_xtn_hdr_cipher) { |
434 | | /* do nothing */ |
435 | 96.0k | } else if (session_keys->rtp_xtn_hdr_cipher) { |
436 | 4.49k | status = srtp_cipher_dealloc(session_keys->rtp_xtn_hdr_cipher); |
437 | 4.49k | if (status) { |
438 | 0 | return status; |
439 | 0 | } |
440 | 4.49k | } |
441 | | |
442 | | /* |
443 | | * deallocate rtcp cipher, if it is not the same as that in |
444 | | * template |
445 | | */ |
446 | 106k | if (template_session_keys && |
447 | 98.1k | session_keys->rtcp_cipher == |
448 | 98.1k | template_session_keys->rtcp_cipher) { |
449 | | /* do nothing */ |
450 | 92.0k | } else if (session_keys->rtcp_cipher) { |
451 | 14.1k | status = srtp_cipher_dealloc(session_keys->rtcp_cipher); |
452 | 14.1k | if (status) { |
453 | 0 | return status; |
454 | 0 | } |
455 | 14.1k | } |
456 | | |
457 | | /* |
458 | | * deallocate rtcp auth function, if it is not the same as that in |
459 | | * template |
460 | | */ |
461 | 106k | if (template_session_keys && |
462 | 98.1k | session_keys->rtcp_auth == template_session_keys->rtcp_auth) { |
463 | | /* do nothing */ |
464 | 92.0k | } else if (session_keys->rtcp_auth) { |
465 | 14.1k | status = srtp_auth_dealloc(session_keys->rtcp_auth); |
466 | 14.1k | if (status) { |
467 | 0 | return status; |
468 | 0 | } |
469 | 14.1k | } |
470 | | |
471 | | /* |
472 | | * zeroize the salt value |
473 | | */ |
474 | 106k | octet_string_set_to_zero(session_keys->salt, SRTP_AEAD_SALT_LEN); |
475 | 106k | octet_string_set_to_zero(session_keys->c_salt, SRTP_AEAD_SALT_LEN); |
476 | | |
477 | 106k | if (session_keys->mki_id) { |
478 | 0 | octet_string_set_to_zero(session_keys->mki_id, |
479 | 0 | stream->mki_size); |
480 | 0 | srtp_crypto_free(session_keys->mki_id); |
481 | 0 | session_keys->mki_id = NULL; |
482 | 0 | } |
483 | | |
484 | | /* |
485 | | * deallocate key usage limit, if it is not the same as that in |
486 | | * template |
487 | | */ |
488 | 106k | if (template_session_keys && |
489 | 98.1k | session_keys->limit == template_session_keys->limit) { |
490 | | /* do nothing */ |
491 | 92.0k | } else if (session_keys->limit) { |
492 | 14.1k | srtp_crypto_free(session_keys->limit); |
493 | 14.1k | } |
494 | 106k | } |
495 | 100k | srtp_crypto_free(stream->session_keys); |
496 | 100k | } |
497 | | |
498 | 100k | status = srtp_rdbx_dealloc(&stream->rtp_rdbx); |
499 | 100k | if (status) { |
500 | 0 | return status; |
501 | 0 | } |
502 | | |
503 | 100k | if (stream_template && |
504 | 96.8k | stream->enc_xtn_hdr == stream_template->enc_xtn_hdr) { |
505 | | /* do nothing */ |
506 | 94.9k | } else if (stream->enc_xtn_hdr) { |
507 | 2.23k | srtp_crypto_free(stream->enc_xtn_hdr); |
508 | 2.23k | } |
509 | | |
510 | | /* deallocate srtp stream context */ |
511 | 100k | srtp_crypto_free(stream); |
512 | | |
513 | 100k | return srtp_err_status_ok; |
514 | 100k | } |
515 | | |
516 | | /* try to insert stream in list or deallocate it */ |
517 | | static srtp_err_status_t srtp_insert_or_dealloc_stream(srtp_stream_list_t list, |
518 | | srtp_stream_t stream, |
519 | | srtp_stream_t template) |
520 | 98.0k | { |
521 | 98.0k | srtp_err_status_t status = srtp_stream_list_insert(list, stream); |
522 | | /* on failure, ownership wasn't transferred and we need to deallocate */ |
523 | 98.0k | if (status) { |
524 | 0 | srtp_stream_dealloc(stream, template); |
525 | 0 | } |
526 | 98.0k | return status; |
527 | 98.0k | } |
528 | | |
529 | | struct remove_and_dealloc_streams_data { |
530 | | srtp_err_status_t status; |
531 | | srtp_stream_list_t list; |
532 | | srtp_stream_t template; |
533 | | }; |
534 | | |
535 | | static bool remove_and_dealloc_streams_cb(srtp_stream_t stream, void *data) |
536 | 96.9k | { |
537 | 96.9k | struct remove_and_dealloc_streams_data *d = |
538 | 96.9k | (struct remove_and_dealloc_streams_data *)data; |
539 | 96.9k | srtp_stream_list_remove(d->list, stream); |
540 | 96.9k | d->status = srtp_stream_dealloc(stream, d->template); |
541 | 96.9k | if (d->status) { |
542 | 0 | return false; |
543 | 0 | } |
544 | 96.9k | return true; |
545 | 96.9k | } |
546 | | |
547 | | static srtp_err_status_t srtp_remove_and_dealloc_streams( |
548 | | srtp_stream_list_t list, |
549 | | srtp_stream_t template) |
550 | 3.10k | { |
551 | 3.10k | struct remove_and_dealloc_streams_data data = { srtp_err_status_ok, list, |
552 | 3.10k | template }; |
553 | 3.10k | srtp_stream_list_for_each(list, remove_and_dealloc_streams_cb, &data); |
554 | 3.10k | return data.status; |
555 | 3.10k | } |
556 | | |
557 | | static srtp_err_status_t srtp_valid_policy(const srtp_policy_t *policy) |
558 | 32.2k | { |
559 | 32.2k | if (policy == NULL) { |
560 | 0 | return srtp_err_status_bad_param; |
561 | 0 | } |
562 | | |
563 | 32.2k | if (policy->key == NULL) { |
564 | 2.67k | if (policy->num_master_keys <= 0) { |
565 | 48 | return srtp_err_status_bad_param; |
566 | 48 | } |
567 | | |
568 | 2.62k | if (policy->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) { |
569 | 37 | return srtp_err_status_bad_param; |
570 | 37 | } |
571 | | |
572 | 2.58k | if (policy->use_mki) { |
573 | 0 | if (policy->mki_size == 0 || policy->mki_size > SRTP_MAX_MKI_LEN) { |
574 | 0 | return srtp_err_status_bad_param; |
575 | 0 | } |
576 | 2.58k | } else if (policy->mki_size != 0) { |
577 | 0 | return srtp_err_status_bad_param; |
578 | 0 | } |
579 | | |
580 | 19.9k | for (size_t i = 0; i < policy->num_master_keys; i++) { |
581 | 17.3k | if (policy->keys[i]->key == NULL) { |
582 | 0 | return srtp_err_status_bad_param; |
583 | 0 | } |
584 | 17.3k | if (policy->use_mki && policy->keys[i]->mki_id == NULL) { |
585 | 0 | return srtp_err_status_bad_param; |
586 | 0 | } |
587 | 17.3k | } |
588 | 29.5k | } else { |
589 | 29.5k | if (policy->use_mki || policy->mki_size != 0) { |
590 | 0 | return srtp_err_status_bad_param; |
591 | 0 | } |
592 | 29.5k | } |
593 | | |
594 | 32.1k | return srtp_err_status_ok; |
595 | 32.2k | } |
596 | | |
597 | | static srtp_err_status_t srtp_stream_alloc(srtp_stream_ctx_t **str_ptr, |
598 | | const srtp_policy_t *p) |
599 | 9.68k | { |
600 | 9.68k | srtp_stream_ctx_t *str; |
601 | 9.68k | srtp_err_status_t stat; |
602 | 9.68k | size_t i = 0; |
603 | 9.68k | srtp_session_keys_t *session_keys = NULL; |
604 | | |
605 | 9.68k | stat = srtp_valid_policy(p); |
606 | 9.68k | if (stat != srtp_err_status_ok) { |
607 | 0 | return stat; |
608 | 0 | } |
609 | | |
610 | | /* |
611 | | * This function allocates the stream context, rtp and rtcp ciphers |
612 | | * and auth functions, and key limit structure. If there is a |
613 | | * failure during allocation, we free all previously allocated |
614 | | * memory and return a failure code. The code could probably |
615 | | * be improved, but it works and should be clear. |
616 | | */ |
617 | | |
618 | | /* allocate srtp stream and set str_ptr */ |
619 | 9.68k | str = (srtp_stream_ctx_t *)srtp_crypto_alloc(sizeof(srtp_stream_ctx_t)); |
620 | 9.68k | if (str == NULL) { |
621 | 0 | return srtp_err_status_alloc_fail; |
622 | 0 | } |
623 | | |
624 | 9.68k | *str_ptr = str; |
625 | | |
626 | | /* |
627 | | *To keep backwards API compatible if someone is using multiple master |
628 | | * keys then key should be set to NULL |
629 | | */ |
630 | 9.68k | if (p->key != NULL) { |
631 | 8.88k | str->num_master_keys = 1; |
632 | 8.88k | } else { |
633 | 796 | str->num_master_keys = p->num_master_keys; |
634 | 796 | } |
635 | | |
636 | 9.68k | str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc( |
637 | 9.68k | sizeof(srtp_session_keys_t) * str->num_master_keys); |
638 | | |
639 | 9.68k | if (str->session_keys == NULL) { |
640 | 0 | srtp_stream_dealloc(str, NULL); |
641 | 0 | return srtp_err_status_alloc_fail; |
642 | 0 | } |
643 | | |
644 | 23.8k | for (i = 0; i < str->num_master_keys; i++) { |
645 | 14.1k | session_keys = &str->session_keys[i]; |
646 | | |
647 | | /* allocate cipher */ |
648 | 14.1k | stat = srtp_crypto_kernel_alloc_cipher( |
649 | 14.1k | p->rtp.cipher_type, &session_keys->rtp_cipher, |
650 | 14.1k | p->rtp.cipher_key_len, p->rtp.auth_tag_len); |
651 | 14.1k | if (stat) { |
652 | 0 | srtp_stream_dealloc(str, NULL); |
653 | 0 | return stat; |
654 | 0 | } |
655 | | |
656 | | /* allocate auth function */ |
657 | 14.1k | stat = srtp_crypto_kernel_alloc_auth( |
658 | 14.1k | p->rtp.auth_type, &session_keys->rtp_auth, p->rtp.auth_key_len, |
659 | 14.1k | p->rtp.auth_tag_len); |
660 | 14.1k | if (stat) { |
661 | 0 | srtp_stream_dealloc(str, NULL); |
662 | 0 | return stat; |
663 | 0 | } |
664 | | |
665 | | /* |
666 | | * ...and now the RTCP-specific initialization - first, allocate |
667 | | * the cipher |
668 | | */ |
669 | 14.1k | stat = srtp_crypto_kernel_alloc_cipher( |
670 | 14.1k | p->rtcp.cipher_type, &session_keys->rtcp_cipher, |
671 | 14.1k | p->rtcp.cipher_key_len, p->rtcp.auth_tag_len); |
672 | 14.1k | if (stat) { |
673 | 0 | srtp_stream_dealloc(str, NULL); |
674 | 0 | return stat; |
675 | 0 | } |
676 | | |
677 | | /* allocate auth function */ |
678 | 14.1k | stat = srtp_crypto_kernel_alloc_auth( |
679 | 14.1k | p->rtcp.auth_type, &session_keys->rtcp_auth, p->rtcp.auth_key_len, |
680 | 14.1k | p->rtcp.auth_tag_len); |
681 | 14.1k | if (stat) { |
682 | 0 | srtp_stream_dealloc(str, NULL); |
683 | 0 | return stat; |
684 | 0 | } |
685 | | |
686 | 14.1k | session_keys->mki_id = NULL; |
687 | | |
688 | | /* allocate key limit structure */ |
689 | 14.1k | session_keys->limit = (srtp_key_limit_ctx_t *)srtp_crypto_alloc( |
690 | 14.1k | sizeof(srtp_key_limit_ctx_t)); |
691 | 14.1k | if (session_keys->limit == NULL) { |
692 | 0 | srtp_stream_dealloc(str, NULL); |
693 | 0 | return srtp_err_status_alloc_fail; |
694 | 0 | } |
695 | 14.1k | } |
696 | | |
697 | 9.68k | if (p->enc_xtn_hdr && p->enc_xtn_hdr_count > 0) { |
698 | 2.23k | srtp_cipher_type_id_t enc_xtn_hdr_cipher_type; |
699 | 2.23k | size_t enc_xtn_hdr_cipher_key_len; |
700 | | |
701 | 2.23k | str->enc_xtn_hdr = (uint8_t *)srtp_crypto_alloc( |
702 | 2.23k | p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0])); |
703 | 2.23k | if (!str->enc_xtn_hdr) { |
704 | 0 | srtp_stream_dealloc(str, NULL); |
705 | 0 | return srtp_err_status_alloc_fail; |
706 | 0 | } |
707 | 2.23k | memcpy(str->enc_xtn_hdr, p->enc_xtn_hdr, |
708 | 2.23k | p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0])); |
709 | 2.23k | str->enc_xtn_hdr_count = p->enc_xtn_hdr_count; |
710 | | |
711 | | /* |
712 | | * For GCM ciphers, the corresponding ICM cipher is used for header |
713 | | * extensions encryption. |
714 | | */ |
715 | 2.23k | switch (p->rtp.cipher_type) { |
716 | 0 | case SRTP_AES_GCM_128: |
717 | 0 | enc_xtn_hdr_cipher_type = SRTP_AES_ICM_128; |
718 | 0 | enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_128_KEY_LEN_WSALT; |
719 | 0 | break; |
720 | 0 | case SRTP_AES_GCM_256: |
721 | 0 | enc_xtn_hdr_cipher_type = SRTP_AES_ICM_256; |
722 | 0 | enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; |
723 | 0 | break; |
724 | 2.23k | default: |
725 | 2.23k | enc_xtn_hdr_cipher_type = p->rtp.cipher_type; |
726 | 2.23k | enc_xtn_hdr_cipher_key_len = p->rtp.cipher_key_len; |
727 | 2.23k | break; |
728 | 2.23k | } |
729 | | |
730 | 6.72k | for (i = 0; i < str->num_master_keys; i++) { |
731 | 4.49k | session_keys = &str->session_keys[i]; |
732 | | |
733 | | /* allocate cipher for extensions header encryption */ |
734 | 4.49k | stat = srtp_crypto_kernel_alloc_cipher( |
735 | 4.49k | enc_xtn_hdr_cipher_type, &session_keys->rtp_xtn_hdr_cipher, |
736 | 4.49k | enc_xtn_hdr_cipher_key_len, 0); |
737 | 4.49k | if (stat) { |
738 | 0 | srtp_stream_dealloc(str, NULL); |
739 | 0 | return stat; |
740 | 0 | } |
741 | 4.49k | } |
742 | 7.44k | } else { |
743 | 17.1k | for (i = 0; i < str->num_master_keys; i++) { |
744 | 9.66k | session_keys = &str->session_keys[i]; |
745 | 9.66k | session_keys->rtp_xtn_hdr_cipher = NULL; |
746 | 9.66k | } |
747 | | |
748 | 7.44k | str->enc_xtn_hdr = NULL; |
749 | 7.44k | str->enc_xtn_hdr_count = 0; |
750 | 7.44k | } |
751 | | |
752 | 9.68k | str->use_cryptex = p->use_cryptex; |
753 | | |
754 | 9.68k | return srtp_err_status_ok; |
755 | 9.68k | } |
756 | | |
757 | | /* |
758 | | * srtp_stream_clone(stream_template, new) allocates a new stream and |
759 | | * initializes it using the cipher and auth of the stream_template |
760 | | * |
761 | | * the only unique data in a cloned stream is the replay database and |
762 | | * the SSRC |
763 | | */ |
764 | | |
765 | | static srtp_err_status_t srtp_stream_clone( |
766 | | const srtp_stream_ctx_t *stream_template, |
767 | | uint32_t ssrc, |
768 | | srtp_stream_ctx_t **str_ptr) |
769 | 91.2k | { |
770 | 91.2k | srtp_err_status_t status; |
771 | 91.2k | srtp_stream_ctx_t *str; |
772 | 91.2k | srtp_session_keys_t *session_keys = NULL; |
773 | 91.2k | const srtp_session_keys_t *template_session_keys = NULL; |
774 | | |
775 | 91.2k | debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", |
776 | 91.2k | (unsigned int)ntohl(ssrc)); |
777 | | |
778 | | /* allocate srtp stream and set str_ptr */ |
779 | 91.2k | str = (srtp_stream_ctx_t *)srtp_crypto_alloc(sizeof(srtp_stream_ctx_t)); |
780 | 91.2k | if (str == NULL) { |
781 | 0 | return srtp_err_status_alloc_fail; |
782 | 0 | } |
783 | 91.2k | *str_ptr = str; |
784 | | |
785 | 91.2k | str->num_master_keys = stream_template->num_master_keys; |
786 | 91.2k | str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc( |
787 | 91.2k | sizeof(srtp_session_keys_t) * str->num_master_keys); |
788 | | |
789 | 91.2k | if (str->session_keys == NULL) { |
790 | 0 | srtp_stream_dealloc(*str_ptr, stream_template); |
791 | 0 | *str_ptr = NULL; |
792 | 0 | return srtp_err_status_alloc_fail; |
793 | 0 | } |
794 | | |
795 | 183k | for (size_t i = 0; i < stream_template->num_master_keys; i++) { |
796 | 92.0k | session_keys = &str->session_keys[i]; |
797 | 92.0k | template_session_keys = &stream_template->session_keys[i]; |
798 | | |
799 | | /* set cipher and auth pointers to those of the template */ |
800 | 92.0k | session_keys->rtp_cipher = template_session_keys->rtp_cipher; |
801 | 92.0k | session_keys->rtp_auth = template_session_keys->rtp_auth; |
802 | 92.0k | session_keys->rtp_xtn_hdr_cipher = |
803 | 92.0k | template_session_keys->rtp_xtn_hdr_cipher; |
804 | 92.0k | session_keys->rtcp_cipher = template_session_keys->rtcp_cipher; |
805 | 92.0k | session_keys->rtcp_auth = template_session_keys->rtcp_auth; |
806 | | |
807 | 92.0k | if (stream_template->mki_size == 0) { |
808 | 92.0k | session_keys->mki_id = NULL; |
809 | 92.0k | } else { |
810 | 0 | session_keys->mki_id = srtp_crypto_alloc(stream_template->mki_size); |
811 | |
|
812 | 0 | if (session_keys->mki_id == NULL) { |
813 | 0 | srtp_stream_dealloc(*str_ptr, stream_template); |
814 | 0 | *str_ptr = NULL; |
815 | 0 | return srtp_err_status_init_fail; |
816 | 0 | } |
817 | 0 | memcpy(session_keys->mki_id, template_session_keys->mki_id, |
818 | 0 | stream_template->mki_size); |
819 | 0 | } |
820 | | /* Copy the salt values */ |
821 | 92.0k | memcpy(session_keys->salt, template_session_keys->salt, |
822 | 92.0k | SRTP_AEAD_SALT_LEN); |
823 | 92.0k | memcpy(session_keys->c_salt, template_session_keys->c_salt, |
824 | 92.0k | SRTP_AEAD_SALT_LEN); |
825 | | |
826 | | /* set key limit to point to that of the template */ |
827 | 92.0k | status = srtp_key_limit_clone(template_session_keys->limit, |
828 | 92.0k | &session_keys->limit); |
829 | 92.0k | if (status) { |
830 | 0 | srtp_stream_dealloc(*str_ptr, stream_template); |
831 | 0 | *str_ptr = NULL; |
832 | 0 | return status; |
833 | 0 | } |
834 | 92.0k | } |
835 | | |
836 | 91.2k | str->use_mki = stream_template->use_mki; |
837 | 91.2k | str->mki_size = stream_template->mki_size; |
838 | | |
839 | | /* initialize replay databases */ |
840 | 91.2k | status = srtp_rdbx_init( |
841 | 91.2k | &str->rtp_rdbx, srtp_rdbx_get_window_size(&stream_template->rtp_rdbx)); |
842 | 91.2k | if (status) { |
843 | 0 | srtp_stream_dealloc(*str_ptr, stream_template); |
844 | 0 | *str_ptr = NULL; |
845 | 0 | return status; |
846 | 0 | } |
847 | 91.2k | srtp_rdb_init(&str->rtcp_rdb); |
848 | 91.2k | str->allow_repeat_tx = stream_template->allow_repeat_tx; |
849 | | |
850 | | /* set ssrc to that provided */ |
851 | 91.2k | str->ssrc = ssrc; |
852 | | |
853 | | /* reset pending ROC */ |
854 | 91.2k | str->pending_roc = 0; |
855 | | |
856 | | /* set direction and security services */ |
857 | 91.2k | str->direction = stream_template->direction; |
858 | 91.2k | str->rtp_services = stream_template->rtp_services; |
859 | 91.2k | str->rtcp_services = stream_template->rtcp_services; |
860 | | |
861 | | /* copy information about extensions header encryption */ |
862 | 91.2k | str->enc_xtn_hdr = stream_template->enc_xtn_hdr; |
863 | 91.2k | str->enc_xtn_hdr_count = stream_template->enc_xtn_hdr_count; |
864 | 91.2k | str->use_cryptex = stream_template->use_cryptex; |
865 | 91.2k | return srtp_err_status_ok; |
866 | 91.2k | } |
867 | | |
868 | | /* |
869 | | * key derivation functions, internal to libSRTP |
870 | | * |
871 | | * srtp_kdf_t is a key derivation context |
872 | | * |
873 | | * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher |
874 | | * described by cipher_id, with the master key k with length in octets keylen. |
875 | | * |
876 | | * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key |
877 | | * corresponding to label l and puts it into kl; the length |
878 | | * of the key in octets is provided as keylen. this function |
879 | | * should be called once for each subkey that is derived. |
880 | | * |
881 | | * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state |
882 | | */ |
883 | | |
884 | | typedef enum { |
885 | | label_rtp_encryption = 0x00, |
886 | | label_rtp_msg_auth = 0x01, |
887 | | label_rtp_salt = 0x02, |
888 | | label_rtcp_encryption = 0x03, |
889 | | label_rtcp_msg_auth = 0x04, |
890 | | label_rtcp_salt = 0x05, |
891 | | label_rtp_header_encryption = 0x06, |
892 | | label_rtp_header_salt = 0x07 |
893 | | } srtp_prf_label; |
894 | | |
895 | 27.7k | #define MAX_SRTP_KEY_LEN 256 |
896 | | |
897 | | #if defined(OPENSSL) && defined(OPENSSL_KDF) |
898 | | #define MAX_SRTP_AESKEY_LEN 32 |
899 | | #define MAX_SRTP_SALT_LEN 14 |
900 | | |
901 | | /* |
902 | | * srtp_kdf_t represents a key derivation function. The SRTP |
903 | | * default KDF is the only one implemented at present. |
904 | | */ |
905 | | typedef struct { |
906 | | uint8_t master_key[MAX_SRTP_AESKEY_LEN]; |
907 | | uint8_t master_salt[MAX_SRTP_SALT_LEN]; |
908 | | const EVP_CIPHER *evp; |
909 | | } srtp_kdf_t; |
910 | | |
911 | | static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, |
912 | | const uint8_t *key, |
913 | | size_t key_len, |
914 | | size_t salt_len) |
915 | | { |
916 | | memset(kdf, 0x0, sizeof(srtp_kdf_t)); |
917 | | |
918 | | /* The NULL cipher has zero key length */ |
919 | | if (key_len == 0) { |
920 | | return srtp_err_status_ok; |
921 | | } |
922 | | |
923 | | if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) { |
924 | | return srtp_err_status_bad_param; |
925 | | } |
926 | | switch (key_len) { |
927 | | case SRTP_AES_256_KEYSIZE: |
928 | | kdf->evp = EVP_aes_256_ctr(); |
929 | | break; |
930 | | case SRTP_AES_192_KEYSIZE: |
931 | | kdf->evp = EVP_aes_192_ctr(); |
932 | | break; |
933 | | case SRTP_AES_128_KEYSIZE: |
934 | | kdf->evp = EVP_aes_128_ctr(); |
935 | | break; |
936 | | default: |
937 | | return srtp_err_status_bad_param; |
938 | | break; |
939 | | } |
940 | | memcpy(kdf->master_key, key, key_len); |
941 | | memcpy(kdf->master_salt, key + key_len, salt_len); |
942 | | return srtp_err_status_ok; |
943 | | } |
944 | | |
945 | | static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, |
946 | | srtp_prf_label label, |
947 | | uint8_t *key, |
948 | | size_t length) |
949 | | { |
950 | | int ret; |
951 | | |
952 | | /* The NULL cipher will not have an EVP */ |
953 | | if (!kdf->evp) { |
954 | | return srtp_err_status_ok; |
955 | | } |
956 | | octet_string_set_to_zero(key, length); |
957 | | |
958 | | /* |
959 | | * Invoke the OpenSSL SRTP KDF function |
960 | | * This is useful if OpenSSL is in FIPS mode and FIP |
961 | | * compliance is required for SRTP. |
962 | | */ |
963 | | ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key, &kdf->master_salt, NULL, |
964 | | NULL, label, key); |
965 | | if (ret == -1) { |
966 | | return (srtp_err_status_algo_fail); |
967 | | } |
968 | | |
969 | | return srtp_err_status_ok; |
970 | | } |
971 | | |
972 | | static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) |
973 | | { |
974 | | octet_string_set_to_zero(kdf->master_key, MAX_SRTP_AESKEY_LEN); |
975 | | octet_string_set_to_zero(kdf->master_salt, MAX_SRTP_SALT_LEN); |
976 | | kdf->evp = NULL; |
977 | | |
978 | | return srtp_err_status_ok; |
979 | | } |
980 | | |
981 | | #elif defined(WOLFSSL) && defined(WOLFSSL_KDF) |
982 | | #define MAX_SRTP_AESKEY_LEN AES_256_KEY_SIZE |
983 | | #define MAX_SRTP_SALT_LEN WC_SRTP_MAX_SALT |
984 | | |
985 | | /* |
986 | | * srtp_kdf_t represents a key derivation function. The SRTP |
987 | | * default KDF is the only one implemented at present. |
988 | | */ |
989 | | typedef struct { |
990 | | uint8_t master_key[MAX_SRTP_AESKEY_LEN]; |
991 | | int master_key_len; |
992 | | uint8_t master_salt[MAX_SRTP_SALT_LEN]; |
993 | | } srtp_kdf_t; |
994 | | |
995 | | static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, |
996 | | const uint8_t *key, |
997 | | size_t key_len) |
998 | | { |
999 | | size_t salt_len; |
1000 | | |
1001 | | memset(kdf, 0x0, sizeof(srtp_kdf_t)); |
1002 | | |
1003 | | switch (key_len) { |
1004 | | case SRTP_AES_ICM_256_KEY_LEN_WSALT: |
1005 | | kdf->master_key_len = AES_256_KEY_SIZE; |
1006 | | break; |
1007 | | case SRTP_AES_ICM_192_KEY_LEN_WSALT: |
1008 | | kdf->master_key_len = AES_192_KEY_SIZE; |
1009 | | break; |
1010 | | case SRTP_AES_ICM_128_KEY_LEN_WSALT: |
1011 | | kdf->master_key_len = AES_128_KEY_SIZE; |
1012 | | break; |
1013 | | default: |
1014 | | return srtp_err_status_bad_param; |
1015 | | break; |
1016 | | } |
1017 | | |
1018 | | memcpy(kdf->master_key, key, kdf->master_key_len); |
1019 | | salt_len = key_len - kdf->master_key_len; |
1020 | | memcpy(kdf->master_salt, key + kdf->master_key_len, salt_len); |
1021 | | memset(kdf->master_salt + salt_len, 0, MAX_SRTP_SALT_LEN - salt_len); |
1022 | | |
1023 | | return srtp_err_status_ok; |
1024 | | } |
1025 | | |
1026 | | static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, |
1027 | | srtp_prf_label label, |
1028 | | uint8_t *key, |
1029 | | size_t length) |
1030 | | { |
1031 | | int err; |
1032 | | |
1033 | | if (length == 0) { |
1034 | | return srtp_err_status_ok; |
1035 | | } |
1036 | | if (kdf->master_key_len == 0) { |
1037 | | return srtp_err_status_ok; |
1038 | | } |
1039 | | octet_string_set_to_zero(key, length); |
1040 | | |
1041 | | PRIVATE_KEY_UNLOCK(); |
1042 | | err = wc_SRTP_KDF_label(kdf->master_key, kdf->master_key_len, |
1043 | | kdf->master_salt, MAX_SRTP_SALT_LEN, -1, NULL, |
1044 | | label, key, length); |
1045 | | PRIVATE_KEY_LOCK(); |
1046 | | if (err < 0) { |
1047 | | debug_print(mod_srtp, "wolfSSL SRTP KDF error: %d", err); |
1048 | | return (srtp_err_status_algo_fail); |
1049 | | } |
1050 | | |
1051 | | return srtp_err_status_ok; |
1052 | | } |
1053 | | |
1054 | | static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) |
1055 | | { |
1056 | | octet_string_set_to_zero(kdf->master_key, MAX_SRTP_AESKEY_LEN); |
1057 | | kdf->master_key_len = 0; |
1058 | | octet_string_set_to_zero(kdf->master_salt, MAX_SRTP_SALT_LEN); |
1059 | | |
1060 | | return srtp_err_status_ok; |
1061 | | } |
1062 | | |
1063 | | #else /* if OPENSSL_KDF || WOLFSSL_KDF */ |
1064 | | |
1065 | | /* |
1066 | | * srtp_kdf_t represents a key derivation function. The SRTP |
1067 | | * default KDF is the only one implemented at present. |
1068 | | */ |
1069 | | typedef struct { |
1070 | | srtp_cipher_t *cipher; /* cipher used for key derivation */ |
1071 | | } srtp_kdf_t; |
1072 | | |
1073 | | static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, |
1074 | | const uint8_t *key, |
1075 | | size_t key_len) |
1076 | 13.8k | { |
1077 | 13.8k | srtp_cipher_type_id_t cipher_id; |
1078 | 13.8k | srtp_err_status_t stat; |
1079 | | |
1080 | 13.8k | switch (key_len) { |
1081 | 704 | case SRTP_AES_ICM_256_KEY_LEN_WSALT: |
1082 | 704 | cipher_id = SRTP_AES_ICM_256; |
1083 | 704 | break; |
1084 | 0 | case SRTP_AES_ICM_192_KEY_LEN_WSALT: |
1085 | 0 | cipher_id = SRTP_AES_ICM_192; |
1086 | 0 | break; |
1087 | 13.1k | case SRTP_AES_ICM_128_KEY_LEN_WSALT: |
1088 | 13.1k | cipher_id = SRTP_AES_ICM_128; |
1089 | 13.1k | break; |
1090 | 0 | default: |
1091 | 0 | return srtp_err_status_bad_param; |
1092 | 0 | break; |
1093 | 13.8k | } |
1094 | | |
1095 | 13.8k | stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, key_len, 0); |
1096 | 13.8k | if (stat) { |
1097 | 0 | return stat; |
1098 | 0 | } |
1099 | | |
1100 | 13.8k | stat = srtp_cipher_init(kdf->cipher, key); |
1101 | 13.8k | if (stat) { |
1102 | 0 | srtp_cipher_dealloc(kdf->cipher); |
1103 | 0 | return stat; |
1104 | 0 | } |
1105 | 13.8k | return srtp_err_status_ok; |
1106 | 13.8k | } |
1107 | | |
1108 | | static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, |
1109 | | srtp_prf_label label, |
1110 | | uint8_t *key, |
1111 | | size_t length) |
1112 | 66.9k | { |
1113 | 66.9k | srtp_err_status_t status; |
1114 | 66.9k | v128_t nonce; |
1115 | | |
1116 | | /* set eigth octet of nonce to <label>, set the rest of it to zero */ |
1117 | 66.9k | v128_set_to_zero(&nonce); |
1118 | 66.9k | nonce.v8[7] = label; |
1119 | | |
1120 | 66.9k | status = srtp_cipher_set_iv(kdf->cipher, (uint8_t *)&nonce, |
1121 | 66.9k | srtp_direction_encrypt); |
1122 | 66.9k | if (status) { |
1123 | 0 | return status; |
1124 | 0 | } |
1125 | | |
1126 | | /* generate keystream output */ |
1127 | 66.9k | octet_string_set_to_zero(key, length); |
1128 | 66.9k | status = srtp_cipher_encrypt(kdf->cipher, key, length, key, &length); |
1129 | 66.9k | if (status) { |
1130 | 0 | return status; |
1131 | 0 | } |
1132 | | |
1133 | 66.9k | return srtp_err_status_ok; |
1134 | 66.9k | } |
1135 | | |
1136 | | static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) |
1137 | 13.8k | { |
1138 | 13.8k | srtp_err_status_t status; |
1139 | 13.8k | status = srtp_cipher_dealloc(kdf->cipher); |
1140 | 13.8k | if (status) { |
1141 | 0 | return status; |
1142 | 0 | } |
1143 | 13.8k | kdf->cipher = NULL; |
1144 | 13.8k | return srtp_err_status_ok; |
1145 | 13.8k | } |
1146 | | #endif /* else OPENSSL_KDF || WOLFSSL_KDF */ |
1147 | | |
1148 | | /* |
1149 | | * end of key derivation functions |
1150 | | */ |
1151 | | |
1152 | | /* Get the base key length corresponding to a given combined key+salt |
1153 | | * length for the given cipher. |
1154 | | * TODO: key and salt lengths should be separate fields in the policy. */ |
1155 | | static inline size_t base_key_length(const srtp_cipher_type_t *cipher, |
1156 | | size_t key_length) |
1157 | 27.7k | { |
1158 | 27.7k | switch (cipher->id) { |
1159 | 23.1k | case SRTP_NULL_CIPHER: |
1160 | 23.1k | return 0; |
1161 | 3.23k | case SRTP_AES_ICM_128: |
1162 | 3.23k | case SRTP_AES_ICM_192: |
1163 | 4.64k | case SRTP_AES_ICM_256: |
1164 | | /* The legacy modes are derived from |
1165 | | * the configured key length on the policy */ |
1166 | 4.64k | return key_length - SRTP_SALT_LEN; |
1167 | 0 | case SRTP_AES_GCM_128: |
1168 | 0 | return key_length - SRTP_AEAD_SALT_LEN; |
1169 | 0 | case SRTP_AES_GCM_256: |
1170 | 0 | return key_length - SRTP_AEAD_SALT_LEN; |
1171 | 0 | default: |
1172 | 0 | return key_length; |
1173 | 27.7k | } |
1174 | 27.7k | } |
1175 | | |
1176 | | /* Get the key length that the application should supply for the given cipher */ |
1177 | | static inline size_t full_key_length(const srtp_cipher_type_t *cipher) |
1178 | 27.7k | { |
1179 | 27.7k | switch (cipher->id) { |
1180 | 23.1k | case SRTP_NULL_CIPHER: |
1181 | 23.1k | return 0; |
1182 | 3.23k | case SRTP_AES_ICM_128: |
1183 | 3.23k | return SRTP_AES_ICM_128_KEY_LEN_WSALT; |
1184 | 0 | case SRTP_AES_ICM_192: |
1185 | 0 | return SRTP_AES_ICM_192_KEY_LEN_WSALT; |
1186 | 1.40k | case SRTP_AES_ICM_256: |
1187 | 1.40k | return SRTP_AES_ICM_256_KEY_LEN_WSALT; |
1188 | 0 | case SRTP_AES_GCM_128: |
1189 | 0 | return SRTP_AES_GCM_128_KEY_LEN_WSALT; |
1190 | 0 | case SRTP_AES_GCM_256: |
1191 | 0 | return SRTP_AES_GCM_256_KEY_LEN_WSALT; |
1192 | 0 | default: |
1193 | 0 | return 0; |
1194 | 27.7k | } |
1195 | 27.7k | } |
1196 | | |
1197 | | /* Get the key length that the application should supply for the given auth */ |
1198 | | static inline size_t full_auth_key_length(const srtp_auth_type_t *auth) |
1199 | 27.7k | { |
1200 | 27.7k | switch (auth->id) { |
1201 | 23.1k | case SRTP_NULL_AUTH: |
1202 | 23.1k | return 0; |
1203 | 4.64k | case SRTP_HMAC_SHA1: |
1204 | 4.64k | return SRTP_AES_ICM_128_KEY_LEN_WSALT; |
1205 | 0 | default: |
1206 | 0 | return 0; |
1207 | 27.7k | } |
1208 | 27.7k | } |
1209 | | |
1210 | | srtp_err_status_t srtp_get_session_keys(srtp_stream_ctx_t *stream, |
1211 | | size_t mki_index, |
1212 | | srtp_session_keys_t **session_keys) |
1213 | 119k | { |
1214 | 119k | if (stream->use_mki) { |
1215 | 0 | if (mki_index >= stream->num_master_keys) { |
1216 | 0 | return srtp_err_status_bad_mki; |
1217 | 0 | } |
1218 | 0 | *session_keys = &stream->session_keys[mki_index]; |
1219 | 0 | return srtp_err_status_ok; |
1220 | 0 | } |
1221 | | |
1222 | 119k | *session_keys = &stream->session_keys[0]; |
1223 | 119k | return srtp_err_status_ok; |
1224 | 119k | } |
1225 | | |
1226 | | void srtp_inject_mki(uint8_t *mki_tag_location, |
1227 | | const srtp_session_keys_t *session_keys, |
1228 | | size_t mki_size) |
1229 | 0 | { |
1230 | 0 | if (mki_size > 0) { |
1231 | | // Write MKI into memory |
1232 | 0 | memcpy(mki_tag_location, session_keys->mki_id, mki_size); |
1233 | 0 | } |
1234 | 0 | } |
1235 | | |
1236 | | srtp_err_status_t srtp_stream_init_keys(srtp_session_keys_t *session_keys, |
1237 | | const srtp_master_key_t *master_key, |
1238 | | size_t mki_size) |
1239 | 13.8k | { |
1240 | 13.8k | srtp_err_status_t stat; |
1241 | 13.8k | srtp_kdf_t kdf; |
1242 | 13.8k | uint8_t tmp_key[MAX_SRTP_KEY_LEN]; |
1243 | 13.8k | size_t input_keylen, full_keylen; |
1244 | 13.8k | size_t kdf_keylen = 30, rtp_keylen, rtcp_keylen; |
1245 | 13.8k | size_t rtp_base_key_len, rtp_salt_len; |
1246 | 13.8k | size_t rtcp_base_key_len, rtcp_salt_len; |
1247 | | |
1248 | | /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */ |
1249 | | /* TODO: kdf algorithm, master key length, and master salt length should |
1250 | | * be part of srtp_policy_t. |
1251 | | */ |
1252 | | |
1253 | | /* initialize key limit to maximum value */ |
1254 | 13.8k | srtp_key_limit_set(session_keys->limit, 0xffffffffffffLL); |
1255 | | |
1256 | 13.8k | if (mki_size != 0) { |
1257 | 0 | if (master_key->mki_id == NULL) { |
1258 | 0 | return srtp_err_status_bad_param; |
1259 | 0 | } |
1260 | 0 | session_keys->mki_id = srtp_crypto_alloc(mki_size); |
1261 | |
|
1262 | 0 | if (session_keys->mki_id == NULL) { |
1263 | 0 | return srtp_err_status_init_fail; |
1264 | 0 | } |
1265 | 0 | memcpy(session_keys->mki_id, master_key->mki_id, mki_size); |
1266 | 13.8k | } else { |
1267 | 13.8k | session_keys->mki_id = NULL; |
1268 | 13.8k | } |
1269 | | |
1270 | | /* Find the maximum key length */ |
1271 | 13.8k | input_keylen = full_key_length(session_keys->rtp_cipher->type); |
1272 | 13.8k | full_keylen = full_auth_key_length(session_keys->rtp_auth->type); |
1273 | 13.8k | if (full_keylen > input_keylen) { |
1274 | 426 | input_keylen = full_keylen; |
1275 | 426 | } |
1276 | 13.8k | full_keylen = full_key_length(session_keys->rtcp_cipher->type); |
1277 | 13.8k | if (full_keylen > input_keylen) { |
1278 | 0 | input_keylen = full_keylen; |
1279 | 0 | } |
1280 | 13.8k | full_keylen = full_auth_key_length(session_keys->rtcp_auth->type); |
1281 | 13.8k | if (full_keylen > input_keylen) { |
1282 | 0 | input_keylen = full_keylen; |
1283 | 0 | } |
1284 | | |
1285 | 13.8k | rtp_keylen = srtp_cipher_get_key_length(session_keys->rtp_cipher); |
1286 | 13.8k | rtcp_keylen = srtp_cipher_get_key_length(session_keys->rtcp_cipher); |
1287 | 13.8k | rtp_base_key_len = |
1288 | 13.8k | base_key_length(session_keys->rtp_cipher->type, rtp_keylen); |
1289 | 13.8k | rtp_salt_len = rtp_keylen - rtp_base_key_len; |
1290 | | |
1291 | | /* |
1292 | | * We assume that the `key` buffer provided by the caller has a length |
1293 | | * equal to the greater of `rtp_keylen` and `rtcp_keylen`. Since we are |
1294 | | * about to read `input_keylen` bytes from it, we need to check that we will |
1295 | | * not overrun. |
1296 | | */ |
1297 | 13.8k | if ((rtp_keylen < input_keylen) && (rtcp_keylen < input_keylen)) { |
1298 | 0 | return srtp_err_status_bad_param; |
1299 | 0 | } |
1300 | | |
1301 | 13.8k | if (rtp_keylen > kdf_keylen) { |
1302 | 704 | kdf_keylen = rtp_keylen; |
1303 | 704 | } |
1304 | | |
1305 | 13.8k | if (rtcp_keylen > kdf_keylen) { |
1306 | 0 | kdf_keylen = rtcp_keylen; |
1307 | 0 | } |
1308 | | |
1309 | 13.8k | if (input_keylen > kdf_keylen) { |
1310 | 0 | kdf_keylen = input_keylen; |
1311 | 0 | } |
1312 | | |
1313 | 13.8k | if (kdf_keylen == SRTP_AES_GCM_128_KEY_LEN_WSALT || |
1314 | 13.8k | kdf_keylen == SRTP_AES_GCM_256_KEY_LEN_WSALT) { |
1315 | 0 | kdf_keylen += 2; /* AES-CTR mode is always used for KDF */ |
1316 | 0 | } |
1317 | | |
1318 | 13.8k | debug_print(mod_srtp, "input key len: %zu", input_keylen); |
1319 | 13.8k | debug_print(mod_srtp, "srtp key len: %zu", rtp_keylen); |
1320 | 13.8k | debug_print(mod_srtp, "srtcp key len: %zu", rtcp_keylen); |
1321 | 13.8k | debug_print(mod_srtp, "base key len: %zu", rtp_base_key_len); |
1322 | 13.8k | debug_print(mod_srtp, "kdf key len: %zu", kdf_keylen); |
1323 | 13.8k | debug_print(mod_srtp, "rtp salt len: %zu", rtp_salt_len); |
1324 | | |
1325 | | /* |
1326 | | * Make sure the key given to us is 'zero' appended. GCM |
1327 | | * mode uses a shorter master SALT (96 bits), but still relies on |
1328 | | * the legacy CTR mode KDF, which uses a 112 bit master SALT. |
1329 | | */ |
1330 | 13.8k | memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN); |
1331 | 13.8k | memcpy(tmp_key, master_key->key, input_keylen); |
1332 | | |
1333 | | /* initialize KDF state */ |
1334 | | #if defined(OPENSSL) && defined(OPENSSL_KDF) |
1335 | | stat = srtp_kdf_init(&kdf, tmp_key, rtp_base_key_len, rtp_salt_len); |
1336 | | #else |
1337 | 13.8k | stat = srtp_kdf_init(&kdf, tmp_key, kdf_keylen); |
1338 | 13.8k | #endif |
1339 | 13.8k | if (stat) { |
1340 | | /* zeroize temp buffer */ |
1341 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1342 | 0 | return srtp_err_status_init_fail; |
1343 | 0 | } |
1344 | | |
1345 | | /* generate encryption key */ |
1346 | 13.8k | stat = srtp_kdf_generate(&kdf, label_rtp_encryption, tmp_key, |
1347 | 13.8k | rtp_base_key_len); |
1348 | 13.8k | if (stat) { |
1349 | | /* zeroize temp buffer */ |
1350 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1351 | 0 | return srtp_err_status_init_fail; |
1352 | 0 | } |
1353 | 13.8k | debug_print(mod_srtp, "cipher key: %s", |
1354 | 13.8k | srtp_octet_string_hex_string(tmp_key, rtp_base_key_len)); |
1355 | | |
1356 | | /* |
1357 | | * if the cipher in the srtp context uses a salt, then we need |
1358 | | * to generate the salt value |
1359 | | */ |
1360 | 13.8k | if (rtp_salt_len > 0) { |
1361 | 2.74k | debug_print0(mod_srtp, "found rtp_salt_len > 0, generating salt"); |
1362 | | |
1363 | | /* generate encryption salt, put after encryption key */ |
1364 | 2.74k | stat = srtp_kdf_generate(&kdf, label_rtp_salt, |
1365 | 2.74k | tmp_key + rtp_base_key_len, rtp_salt_len); |
1366 | 2.74k | if (stat) { |
1367 | | /* zeroize temp buffer */ |
1368 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1369 | 0 | return srtp_err_status_init_fail; |
1370 | 0 | } |
1371 | 2.74k | memcpy(session_keys->salt, tmp_key + rtp_base_key_len, |
1372 | 2.74k | SRTP_AEAD_SALT_LEN); |
1373 | 2.74k | } |
1374 | 13.8k | if (rtp_salt_len > 0) { |
1375 | 2.74k | debug_print(mod_srtp, "cipher salt: %s", |
1376 | 2.74k | srtp_octet_string_hex_string(tmp_key + rtp_base_key_len, |
1377 | 2.74k | rtp_salt_len)); |
1378 | 2.74k | } |
1379 | | |
1380 | | /* initialize cipher */ |
1381 | 13.8k | stat = srtp_cipher_init(session_keys->rtp_cipher, tmp_key); |
1382 | 13.8k | if (stat) { |
1383 | | /* zeroize temp buffer */ |
1384 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1385 | 0 | return srtp_err_status_init_fail; |
1386 | 0 | } |
1387 | | |
1388 | 13.8k | if (session_keys->rtp_xtn_hdr_cipher) { |
1389 | | /* generate extensions header encryption key */ |
1390 | 4.44k | size_t rtp_xtn_hdr_keylen; |
1391 | 4.44k | size_t rtp_xtn_hdr_base_key_len; |
1392 | 4.44k | size_t rtp_xtn_hdr_salt_len; |
1393 | 4.44k | srtp_kdf_t tmp_kdf; |
1394 | 4.44k | srtp_kdf_t *xtn_hdr_kdf; |
1395 | | |
1396 | 4.44k | if (session_keys->rtp_xtn_hdr_cipher->type != |
1397 | 4.44k | session_keys->rtp_cipher->type) { |
1398 | | /* |
1399 | | * With GCM ciphers, the header extensions are still encrypted using |
1400 | | * the corresponding ICM cipher. |
1401 | | * See https://tools.ietf.org/html/rfc7714#section-8.3 |
1402 | | */ |
1403 | 0 | uint8_t tmp_xtn_hdr_key[MAX_SRTP_KEY_LEN]; |
1404 | 0 | rtp_xtn_hdr_keylen = |
1405 | 0 | srtp_cipher_get_key_length(session_keys->rtp_xtn_hdr_cipher); |
1406 | 0 | rtp_xtn_hdr_base_key_len = base_key_length( |
1407 | 0 | session_keys->rtp_xtn_hdr_cipher->type, rtp_xtn_hdr_keylen); |
1408 | 0 | rtp_xtn_hdr_salt_len = |
1409 | 0 | rtp_xtn_hdr_keylen - rtp_xtn_hdr_base_key_len; |
1410 | 0 | if (rtp_xtn_hdr_salt_len > rtp_salt_len) { |
1411 | 0 | switch (session_keys->rtp_cipher->type->id) { |
1412 | 0 | case SRTP_AES_GCM_128: |
1413 | 0 | case SRTP_AES_GCM_256: |
1414 | | /* |
1415 | | * The shorter GCM salt is padded to the required ICM salt |
1416 | | * length. |
1417 | | */ |
1418 | 0 | rtp_xtn_hdr_salt_len = rtp_salt_len; |
1419 | 0 | break; |
1420 | 0 | default: |
1421 | | /* zeroize temp buffer */ |
1422 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1423 | 0 | return srtp_err_status_bad_param; |
1424 | 0 | } |
1425 | 0 | } |
1426 | 0 | memset(tmp_xtn_hdr_key, 0x0, MAX_SRTP_KEY_LEN); |
1427 | 0 | memcpy(tmp_xtn_hdr_key, master_key->key, |
1428 | 0 | (rtp_xtn_hdr_base_key_len + rtp_xtn_hdr_salt_len)); |
1429 | 0 | xtn_hdr_kdf = &tmp_kdf; |
1430 | | |
1431 | | /* initialize KDF state */ |
1432 | | #if defined(OPENSSL) && defined(OPENSSL_KDF) |
1433 | | stat = |
1434 | | srtp_kdf_init(xtn_hdr_kdf, tmp_xtn_hdr_key, |
1435 | | rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len); |
1436 | | #else |
1437 | 0 | stat = srtp_kdf_init(xtn_hdr_kdf, tmp_xtn_hdr_key, kdf_keylen); |
1438 | 0 | #endif |
1439 | 0 | octet_string_set_to_zero(tmp_xtn_hdr_key, MAX_SRTP_KEY_LEN); |
1440 | 0 | if (stat) { |
1441 | | /* zeroize temp buffer */ |
1442 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1443 | 0 | return srtp_err_status_init_fail; |
1444 | 0 | } |
1445 | 4.44k | } else { |
1446 | | /* Reuse main KDF. */ |
1447 | 4.44k | rtp_xtn_hdr_keylen = rtp_keylen; |
1448 | 4.44k | rtp_xtn_hdr_base_key_len = rtp_base_key_len; |
1449 | 4.44k | rtp_xtn_hdr_salt_len = rtp_salt_len; |
1450 | 4.44k | xtn_hdr_kdf = &kdf; |
1451 | 4.44k | } |
1452 | | |
1453 | 4.44k | stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_encryption, |
1454 | 4.44k | tmp_key, rtp_xtn_hdr_base_key_len); |
1455 | 4.44k | if (stat) { |
1456 | | /* zeroize temp buffer */ |
1457 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1458 | 0 | return srtp_err_status_init_fail; |
1459 | 0 | } |
1460 | 4.44k | debug_print( |
1461 | 4.44k | mod_srtp, "extensions cipher key: %s", |
1462 | 4.44k | srtp_octet_string_hex_string(tmp_key, rtp_xtn_hdr_base_key_len)); |
1463 | | |
1464 | | /* |
1465 | | * if the cipher in the srtp context uses a salt, then we need |
1466 | | * to generate the salt value |
1467 | | */ |
1468 | 4.44k | if (rtp_xtn_hdr_salt_len > 0) { |
1469 | 1.48k | debug_print0(mod_srtp, |
1470 | 1.48k | "found rtp_xtn_hdr_salt_len > 0, generating salt"); |
1471 | | |
1472 | | /* generate encryption salt, put after encryption key */ |
1473 | 1.48k | stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_salt, |
1474 | 1.48k | tmp_key + rtp_xtn_hdr_base_key_len, |
1475 | 1.48k | rtp_xtn_hdr_salt_len); |
1476 | 1.48k | if (stat) { |
1477 | | /* zeroize temp buffer */ |
1478 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1479 | 0 | return srtp_err_status_init_fail; |
1480 | 0 | } |
1481 | 1.48k | } |
1482 | 4.44k | if (rtp_xtn_hdr_salt_len > 0) { |
1483 | 1.48k | debug_print( |
1484 | 1.48k | mod_srtp, "extensions cipher salt: %s", |
1485 | 1.48k | srtp_octet_string_hex_string(tmp_key + rtp_xtn_hdr_base_key_len, |
1486 | 1.48k | rtp_xtn_hdr_salt_len)); |
1487 | 1.48k | } |
1488 | | |
1489 | | /* initialize extensions header cipher */ |
1490 | 4.44k | stat = srtp_cipher_init(session_keys->rtp_xtn_hdr_cipher, tmp_key); |
1491 | 4.44k | if (stat) { |
1492 | | /* zeroize temp buffer */ |
1493 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1494 | 0 | return srtp_err_status_init_fail; |
1495 | 0 | } |
1496 | | |
1497 | 4.44k | if (xtn_hdr_kdf != &kdf) { |
1498 | | /* release memory for custom header extension encryption kdf */ |
1499 | 0 | stat = srtp_kdf_clear(xtn_hdr_kdf); |
1500 | 0 | if (stat) { |
1501 | | /* zeroize temp buffer */ |
1502 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1503 | 0 | return srtp_err_status_init_fail; |
1504 | 0 | } |
1505 | 0 | } |
1506 | 4.44k | } |
1507 | | |
1508 | | /* generate authentication key */ |
1509 | 13.8k | stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth, tmp_key, |
1510 | 13.8k | srtp_auth_get_key_length(session_keys->rtp_auth)); |
1511 | 13.8k | if (stat) { |
1512 | | /* zeroize temp buffer */ |
1513 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1514 | 0 | return srtp_err_status_init_fail; |
1515 | 0 | } |
1516 | 13.8k | debug_print(mod_srtp, "auth key: %s", |
1517 | 13.8k | srtp_octet_string_hex_string( |
1518 | 13.8k | tmp_key, srtp_auth_get_key_length(session_keys->rtp_auth))); |
1519 | | |
1520 | | /* initialize auth function */ |
1521 | 13.8k | stat = srtp_auth_init(session_keys->rtp_auth, tmp_key); |
1522 | 13.8k | if (stat) { |
1523 | | /* zeroize temp buffer */ |
1524 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1525 | 0 | return srtp_err_status_init_fail; |
1526 | 0 | } |
1527 | | |
1528 | | /* |
1529 | | * ...now initialize SRTCP keys |
1530 | | */ |
1531 | | |
1532 | 13.8k | rtcp_base_key_len = |
1533 | 13.8k | base_key_length(session_keys->rtcp_cipher->type, rtcp_keylen); |
1534 | 13.8k | rtcp_salt_len = rtcp_keylen - rtcp_base_key_len; |
1535 | 13.8k | debug_print(mod_srtp, "rtcp salt len: %zu", rtcp_salt_len); |
1536 | | |
1537 | | /* generate encryption key */ |
1538 | 13.8k | stat = srtp_kdf_generate(&kdf, label_rtcp_encryption, tmp_key, |
1539 | 13.8k | rtcp_base_key_len); |
1540 | 13.8k | if (stat) { |
1541 | | /* zeroize temp buffer */ |
1542 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1543 | 0 | return srtp_err_status_init_fail; |
1544 | 0 | } |
1545 | | |
1546 | | /* |
1547 | | * if the cipher in the srtp context uses a salt, then we need |
1548 | | * to generate the salt value |
1549 | | */ |
1550 | 13.8k | if (rtcp_salt_len > 0) { |
1551 | 2.74k | debug_print0(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt"); |
1552 | | |
1553 | | /* generate encryption salt, put after encryption key */ |
1554 | 2.74k | stat = srtp_kdf_generate(&kdf, label_rtcp_salt, |
1555 | 2.74k | tmp_key + rtcp_base_key_len, rtcp_salt_len); |
1556 | 2.74k | if (stat) { |
1557 | | /* zeroize temp buffer */ |
1558 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1559 | 0 | return srtp_err_status_init_fail; |
1560 | 0 | } |
1561 | 2.74k | memcpy(session_keys->c_salt, tmp_key + rtcp_base_key_len, |
1562 | 2.74k | SRTP_AEAD_SALT_LEN); |
1563 | 2.74k | } |
1564 | 13.8k | debug_print(mod_srtp, "rtcp cipher key: %s", |
1565 | 13.8k | srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len)); |
1566 | 13.8k | if (rtcp_salt_len > 0) { |
1567 | 2.74k | debug_print(mod_srtp, "rtcp cipher salt: %s", |
1568 | 2.74k | srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len, |
1569 | 2.74k | rtcp_salt_len)); |
1570 | 2.74k | } |
1571 | | |
1572 | | /* initialize cipher */ |
1573 | 13.8k | stat = srtp_cipher_init(session_keys->rtcp_cipher, tmp_key); |
1574 | 13.8k | if (stat) { |
1575 | | /* zeroize temp buffer */ |
1576 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1577 | 0 | return srtp_err_status_init_fail; |
1578 | 0 | } |
1579 | | |
1580 | | /* generate authentication key */ |
1581 | 13.8k | stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth, tmp_key, |
1582 | 13.8k | srtp_auth_get_key_length(session_keys->rtcp_auth)); |
1583 | 13.8k | if (stat) { |
1584 | | /* zeroize temp buffer */ |
1585 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1586 | 0 | return srtp_err_status_init_fail; |
1587 | 0 | } |
1588 | | |
1589 | 13.8k | debug_print( |
1590 | 13.8k | mod_srtp, "rtcp auth key: %s", |
1591 | 13.8k | srtp_octet_string_hex_string( |
1592 | 13.8k | tmp_key, srtp_auth_get_key_length(session_keys->rtcp_auth))); |
1593 | | |
1594 | | /* initialize auth function */ |
1595 | 13.8k | stat = srtp_auth_init(session_keys->rtcp_auth, tmp_key); |
1596 | 13.8k | if (stat) { |
1597 | | /* zeroize temp buffer */ |
1598 | 0 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1599 | 0 | return srtp_err_status_init_fail; |
1600 | 0 | } |
1601 | | |
1602 | | /* clear memory then return */ |
1603 | 13.8k | stat = srtp_kdf_clear(&kdf); |
1604 | 13.8k | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
1605 | 13.8k | if (stat) { |
1606 | 0 | return srtp_err_status_init_fail; |
1607 | 0 | } |
1608 | | |
1609 | 13.8k | return srtp_err_status_ok; |
1610 | 13.8k | } |
1611 | | |
1612 | | srtp_err_status_t srtp_stream_init_all_master_keys(srtp_stream_ctx_t *srtp, |
1613 | | const srtp_policy_t *p) |
1614 | 9.47k | { |
1615 | 9.47k | srtp_err_status_t status = srtp_err_status_ok; |
1616 | 9.47k | if (p->key != NULL) { |
1617 | 8.69k | if (p->use_mki) { |
1618 | 0 | return srtp_err_status_bad_param; |
1619 | 0 | } |
1620 | 8.69k | srtp_master_key_t single_master_key; |
1621 | 8.69k | srtp->num_master_keys = 1; |
1622 | 8.69k | srtp->use_mki = false; |
1623 | 8.69k | srtp->mki_size = 0; |
1624 | 8.69k | single_master_key.key = p->key; |
1625 | 8.69k | single_master_key.mki_id = NULL; |
1626 | 8.69k | status = srtp_stream_init_keys(&srtp->session_keys[0], |
1627 | 8.69k | &single_master_key, 0); |
1628 | 8.69k | } else { |
1629 | 779 | if (p->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS) { |
1630 | 0 | return srtp_err_status_bad_param; |
1631 | 0 | } |
1632 | 779 | if (p->use_mki && p->mki_size == 0) { |
1633 | 0 | return srtp_err_status_bad_param; |
1634 | 0 | } |
1635 | | |
1636 | 779 | srtp->num_master_keys = p->num_master_keys; |
1637 | 779 | srtp->use_mki = p->use_mki; |
1638 | 779 | srtp->mki_size = p->mki_size; |
1639 | | |
1640 | 5.95k | for (size_t i = 0; i < srtp->num_master_keys; i++) { |
1641 | 5.17k | status = srtp_stream_init_keys(&srtp->session_keys[i], p->keys[i], |
1642 | 5.17k | srtp->mki_size); |
1643 | 5.17k | if (status) { |
1644 | 0 | return status; |
1645 | 0 | } |
1646 | 5.17k | } |
1647 | 779 | } |
1648 | | |
1649 | 9.47k | return status; |
1650 | 9.47k | } |
1651 | | |
1652 | | static srtp_err_status_t srtp_stream_init(srtp_stream_ctx_t *srtp, |
1653 | | const srtp_policy_t *p) |
1654 | 9.68k | { |
1655 | 9.68k | srtp_err_status_t err; |
1656 | | |
1657 | 9.68k | err = srtp_valid_policy(p); |
1658 | 9.68k | if (err != srtp_err_status_ok) { |
1659 | 0 | return err; |
1660 | 0 | } |
1661 | | |
1662 | 9.68k | debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)", |
1663 | 9.68k | (unsigned int)p->ssrc.value); |
1664 | | |
1665 | | /* initialize replay database */ |
1666 | | /* |
1667 | | * window size MUST be at least 64. MAY be larger. Values more than |
1668 | | * 2^15 aren't meaningful due to how extended sequence numbers are |
1669 | | * calculated. |
1670 | | * Let a window size of 0 imply the default value. |
1671 | | */ |
1672 | | |
1673 | 9.68k | if (p->window_size != 0 && |
1674 | 8.31k | (p->window_size < 64 || p->window_size >= 0x8000)) |
1675 | 203 | return srtp_err_status_bad_param; |
1676 | | |
1677 | 9.47k | if (p->window_size != 0) { |
1678 | 8.11k | err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size); |
1679 | 8.11k | } else { |
1680 | 1.36k | err = srtp_rdbx_init(&srtp->rtp_rdbx, 128); |
1681 | 1.36k | } |
1682 | 9.47k | if (err) { |
1683 | 0 | return err; |
1684 | 0 | } |
1685 | | |
1686 | | /* set the SSRC value */ |
1687 | 9.47k | srtp->ssrc = htonl(p->ssrc.value); |
1688 | | |
1689 | | /* reset pending ROC */ |
1690 | 9.47k | srtp->pending_roc = 0; |
1691 | | |
1692 | | /* set the security service flags */ |
1693 | 9.47k | srtp->rtp_services = p->rtp.sec_serv; |
1694 | 9.47k | srtp->rtcp_services = p->rtcp.sec_serv; |
1695 | | |
1696 | | /* |
1697 | | * set direction to unknown - this flag gets checked in srtp_protect(), |
1698 | | * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and |
1699 | | * gets set appropriately if it is set to unknown. |
1700 | | */ |
1701 | 9.47k | srtp->direction = dir_unknown; |
1702 | | |
1703 | | /* initialize SRTCP replay database */ |
1704 | 9.47k | srtp_rdb_init(&srtp->rtcp_rdb); |
1705 | | |
1706 | | /* initialize allow_repeat_tx */ |
1707 | 9.47k | srtp->allow_repeat_tx = p->allow_repeat_tx; |
1708 | | |
1709 | | /* DAM - no RTCP key limit at present */ |
1710 | | |
1711 | | /* initialize keys */ |
1712 | 9.47k | err = srtp_stream_init_all_master_keys(srtp, p); |
1713 | 9.47k | if (err) { |
1714 | 0 | srtp_rdbx_dealloc(&srtp->rtp_rdbx); |
1715 | 0 | return err; |
1716 | 0 | } |
1717 | | |
1718 | 9.47k | return srtp_err_status_ok; |
1719 | 9.47k | } |
1720 | | |
1721 | | /* |
1722 | | * srtp_event_reporter is an event handler function that merely |
1723 | | * reports the events that are reported by the callbacks |
1724 | | */ |
1725 | | |
1726 | | void srtp_event_reporter(srtp_event_data_t *data) |
1727 | 0 | { |
1728 | 0 | srtp_err_report(srtp_err_level_warning, |
1729 | 0 | "srtp: in stream 0x%x: ", (unsigned int)data->ssrc); |
1730 | |
|
1731 | 0 | switch (data->event) { |
1732 | 0 | case event_ssrc_collision: |
1733 | 0 | srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n"); |
1734 | 0 | break; |
1735 | 0 | case event_key_soft_limit: |
1736 | 0 | srtp_err_report(srtp_err_level_warning, |
1737 | 0 | "\tkey usage soft limit reached\n"); |
1738 | 0 | break; |
1739 | 0 | case event_key_hard_limit: |
1740 | 0 | srtp_err_report(srtp_err_level_warning, |
1741 | 0 | "\tkey usage hard limit reached\n"); |
1742 | 0 | break; |
1743 | 0 | case event_packet_index_limit: |
1744 | 0 | srtp_err_report(srtp_err_level_warning, |
1745 | 0 | "\tpacket index limit reached\n"); |
1746 | 0 | break; |
1747 | 0 | default: |
1748 | 0 | srtp_err_report(srtp_err_level_warning, |
1749 | 0 | "\tunknown event reported to handler\n"); |
1750 | 0 | } |
1751 | 0 | } |
1752 | | |
1753 | | /* |
1754 | | * srtp_event_handler is a global variable holding a pointer to the |
1755 | | * event handler function; this function is called for any unexpected |
1756 | | * event that needs to be handled out of the SRTP data path. see |
1757 | | * srtp_event_t in srtp.h for more info |
1758 | | * |
1759 | | * it is okay to set srtp_event_handler to NULL, but we set |
1760 | | * it to the srtp_event_reporter. |
1761 | | */ |
1762 | | |
1763 | | static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter; |
1764 | | |
1765 | | srtp_err_status_t srtp_install_event_handler(srtp_event_handler_func_t func) |
1766 | 2 | { |
1767 | | /* |
1768 | | * note that we accept NULL arguments intentionally - calling this |
1769 | | * function with a NULL arguments removes an event handler that's |
1770 | | * been previously installed |
1771 | | */ |
1772 | | |
1773 | | /* set global event handling function */ |
1774 | 2 | srtp_event_handler = func; |
1775 | 2 | return srtp_err_status_ok; |
1776 | 2 | } |
1777 | | |
1778 | | /* |
1779 | | * Check if the given extension header id is / should be encrypted. |
1780 | | * Returns true if yes, otherwise false. |
1781 | | */ |
1782 | | static bool srtp_protect_extension_header(srtp_stream_ctx_t *stream, uint8_t id) |
1783 | 7.06k | { |
1784 | 7.06k | uint8_t *enc_xtn_hdr = stream->enc_xtn_hdr; |
1785 | 7.06k | size_t count = stream->enc_xtn_hdr_count; |
1786 | | |
1787 | 7.06k | if (!enc_xtn_hdr || count <= 0) { |
1788 | 0 | return false; |
1789 | 0 | } |
1790 | | |
1791 | 13.3k | while (count > 0) { |
1792 | 8.26k | if (*enc_xtn_hdr == id) { |
1793 | 1.93k | return true; |
1794 | 1.93k | } |
1795 | | |
1796 | 6.32k | enc_xtn_hdr++; |
1797 | 6.32k | count--; |
1798 | 6.32k | } |
1799 | 5.12k | return false; |
1800 | 7.06k | } |
1801 | | |
1802 | | /* |
1803 | | * extensions header encryption RFC 6904 |
1804 | | */ |
1805 | | static srtp_err_status_t srtp_process_header_encryption( |
1806 | | srtp_stream_ctx_t *stream, |
1807 | | srtp_hdr_xtnd_t *xtn_hdr, |
1808 | | srtp_session_keys_t *session_keys) |
1809 | 1.53k | { |
1810 | 1.53k | srtp_err_status_t status; |
1811 | 1.53k | uint8_t keystream[257]; /* Maximum 2 bytes header + 255 bytes data. */ |
1812 | 1.53k | size_t keystream_pos; |
1813 | 1.53k | uint8_t *xtn_hdr_data = ((uint8_t *)xtn_hdr) + octets_in_rtp_xtn_hdr; |
1814 | 1.53k | uint8_t *xtn_hdr_end = |
1815 | 1.53k | xtn_hdr_data + (ntohs(xtn_hdr->length) * sizeof(uint32_t)); |
1816 | | |
1817 | 1.53k | if (ntohs(xtn_hdr->profile_specific) == xtn_hdr_one_byte_profile) { |
1818 | | /* RFC 5285, section 4.2. One-Byte Header */ |
1819 | 3.32k | while (xtn_hdr_data < xtn_hdr_end) { |
1820 | 2.97k | uint8_t xid = (*xtn_hdr_data & 0xf0) >> 4; |
1821 | 2.97k | size_t xlen = (*xtn_hdr_data & 0x0f) + 1; |
1822 | 2.97k | size_t xlen_with_header = 1 + xlen; |
1823 | 2.97k | xtn_hdr_data++; |
1824 | | |
1825 | 2.97k | if (xtn_hdr_data + xlen > xtn_hdr_end) { |
1826 | 20 | return srtp_err_status_parse_err; |
1827 | 20 | } |
1828 | | |
1829 | 2.95k | if (xid == 15) { |
1830 | | /* found header 15, stop further processing */ |
1831 | 199 | break; |
1832 | 199 | } |
1833 | | |
1834 | 2.75k | status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher, |
1835 | 2.75k | keystream, &xlen_with_header); |
1836 | 2.75k | if (status) { |
1837 | 0 | return srtp_err_status_cipher_fail; |
1838 | 0 | } |
1839 | | |
1840 | 2.75k | if (srtp_protect_extension_header(stream, xid)) { |
1841 | 1.51k | keystream_pos = 1; |
1842 | 4.62k | while (xlen > 0) { |
1843 | 3.11k | *xtn_hdr_data ^= keystream[keystream_pos++]; |
1844 | 3.11k | xtn_hdr_data++; |
1845 | 3.11k | xlen--; |
1846 | 3.11k | } |
1847 | 1.51k | } else { |
1848 | 1.24k | xtn_hdr_data += xlen; |
1849 | 1.24k | } |
1850 | | |
1851 | | /* skip padding bytes */ |
1852 | 3.34k | while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) { |
1853 | 583 | xtn_hdr_data++; |
1854 | 583 | } |
1855 | 2.75k | } |
1856 | 967 | } else if ((ntohs(xtn_hdr->profile_specific) & 0xfff0) == |
1857 | 967 | xtn_hdr_two_byte_profile) { |
1858 | | /* RFC 5285, section 4.3. Two-Byte Header */ |
1859 | 7.11k | while (xtn_hdr_data + 1 < xtn_hdr_end) { |
1860 | 6.20k | uint8_t xid = *xtn_hdr_data; |
1861 | 6.20k | size_t xlen = *(xtn_hdr_data + 1); |
1862 | 6.20k | size_t xlen_with_header = 2 + xlen; |
1863 | 6.20k | xtn_hdr_data += 2; |
1864 | | |
1865 | 6.20k | if (xtn_hdr_data + xlen > xtn_hdr_end) { |
1866 | 18 | return srtp_err_status_parse_err; |
1867 | 18 | } |
1868 | | |
1869 | 6.18k | status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher, |
1870 | 6.18k | keystream, &xlen_with_header); |
1871 | 6.18k | if (status) { |
1872 | 0 | return srtp_err_status_cipher_fail; |
1873 | 0 | } |
1874 | | |
1875 | 6.18k | if (xlen > 0 && srtp_protect_extension_header(stream, xid)) { |
1876 | 419 | keystream_pos = 2; |
1877 | 3.48k | while (xlen > 0) { |
1878 | 3.06k | *xtn_hdr_data ^= keystream[keystream_pos++]; |
1879 | 3.06k | xtn_hdr_data++; |
1880 | 3.06k | xlen--; |
1881 | 3.06k | } |
1882 | 5.76k | } else { |
1883 | 5.76k | xtn_hdr_data += xlen; |
1884 | 5.76k | } |
1885 | | |
1886 | | /* skip padding bytes. */ |
1887 | 8.34k | while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) { |
1888 | 2.16k | xtn_hdr_data++; |
1889 | 2.16k | } |
1890 | 6.18k | } |
1891 | 931 | } else { |
1892 | | /* unsupported extension header format. */ |
1893 | 36 | return srtp_err_status_parse_err; |
1894 | 36 | } |
1895 | | |
1896 | 1.45k | return srtp_err_status_ok; |
1897 | 1.53k | } |
1898 | | |
1899 | | /* |
1900 | | * AEAD uses a new IV formation method. This function implements |
1901 | | * section 8.1. (SRTP IV Formation for AES-GCM) of RFC7714. |
1902 | | * The calculation is defined as, where (+) is the xor operation: |
1903 | | * |
1904 | | * |
1905 | | * 0 0 0 0 0 0 0 0 0 0 1 1 |
1906 | | * 0 1 2 3 4 5 6 7 8 9 0 1 |
1907 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ |
1908 | | * |00|00| SSRC | ROC | SEQ |---+ |
1909 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
1910 | | * | |
1911 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
1912 | | * | Encryption Salt |->(+) |
1913 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
1914 | | * | |
1915 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
1916 | | * | Initialization Vector |<--+ |
1917 | | * +--+--+--+--+--+--+--+--+--+--+--+--+* |
1918 | | * |
1919 | | * Input: *session_keys - pointer to SRTP stream context session keys, |
1920 | | * used to retrieve the SALT |
1921 | | * *iv - Pointer to receive the calculated IV |
1922 | | * *seq - The ROC and SEQ value to use for the |
1923 | | * IV calculation. |
1924 | | * *hdr - The RTP header, used to get the SSRC value |
1925 | | * |
1926 | | */ |
1927 | | |
1928 | | static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys, |
1929 | | v128_t *iv, |
1930 | | srtp_xtd_seq_num_t *seq, |
1931 | | const srtp_hdr_t *hdr) |
1932 | 0 | { |
1933 | 0 | v128_t in; |
1934 | 0 | v128_t salt; |
1935 | |
|
1936 | 0 | uint32_t local_roc = (uint32_t)(*seq >> 16); |
1937 | 0 | uint16_t local_seq = (uint16_t)*seq; |
1938 | |
|
1939 | 0 | memset(&in, 0, sizeof(v128_t)); |
1940 | 0 | memset(&salt, 0, sizeof(v128_t)); |
1941 | |
|
1942 | 0 | in.v16[5] = htons(local_seq); |
1943 | 0 | local_roc = htonl(local_roc); |
1944 | 0 | memcpy(&in.v16[3], &local_roc, sizeof(local_roc)); |
1945 | | |
1946 | | /* |
1947 | | * Copy in the RTP SSRC value |
1948 | | */ |
1949 | 0 | memcpy(&in.v8[2], &hdr->ssrc, 4); |
1950 | 0 | debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in)); |
1951 | | |
1952 | | /* |
1953 | | * Get the SALT value from the context |
1954 | | */ |
1955 | 0 | memcpy(salt.v8, session_keys->salt, SRTP_AEAD_SALT_LEN); |
1956 | 0 | debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt)); |
1957 | | |
1958 | | /* |
1959 | | * Finally, apply tyhe SALT to the input |
1960 | | */ |
1961 | 0 | v128_xor(iv, &in, &salt); |
1962 | 0 | } |
1963 | | |
1964 | | static srtp_err_status_t srtp_get_session_keys_for_packet( |
1965 | | srtp_stream_ctx_t *stream, |
1966 | | const uint8_t *hdr, |
1967 | | size_t pkt_octet_len, |
1968 | | size_t tag_len, |
1969 | | srtp_session_keys_t **session_keys) |
1970 | 93.3k | { |
1971 | 93.3k | if (!stream->use_mki) { |
1972 | 93.3k | *session_keys = &stream->session_keys[0]; |
1973 | 93.3k | return srtp_err_status_ok; |
1974 | 93.3k | } |
1975 | | |
1976 | 0 | size_t mki_start_location = pkt_octet_len; |
1977 | |
|
1978 | 0 | if (tag_len > mki_start_location) { |
1979 | 0 | return srtp_err_status_bad_mki; |
1980 | 0 | } |
1981 | | |
1982 | 0 | mki_start_location -= tag_len; |
1983 | |
|
1984 | 0 | if (stream->mki_size > mki_start_location) { |
1985 | 0 | return srtp_err_status_bad_mki; |
1986 | 0 | } |
1987 | | |
1988 | 0 | mki_start_location -= stream->mki_size; |
1989 | |
|
1990 | 0 | for (size_t i = 0; i < stream->num_master_keys; i++) { |
1991 | 0 | if (memcmp(hdr + mki_start_location, stream->session_keys[i].mki_id, |
1992 | 0 | stream->mki_size) == 0) { |
1993 | 0 | *session_keys = &stream->session_keys[i]; |
1994 | 0 | return srtp_err_status_ok; |
1995 | 0 | } |
1996 | 0 | } |
1997 | | |
1998 | 0 | return srtp_err_status_bad_mki; |
1999 | 0 | } |
2000 | | |
2001 | | static srtp_err_status_t srtp_get_session_keys_for_rtp_packet( |
2002 | | srtp_stream_ctx_t *stream, |
2003 | | const uint8_t *hdr, |
2004 | | size_t pkt_octet_len, |
2005 | | srtp_session_keys_t **session_keys) |
2006 | 90.9k | { |
2007 | 90.9k | size_t tag_len = 0; |
2008 | | |
2009 | | // Determine the authentication tag size |
2010 | 90.9k | if (stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_128 || |
2011 | 90.9k | stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_256) { |
2012 | 0 | tag_len = 0; |
2013 | 90.9k | } else { |
2014 | 90.9k | tag_len = srtp_auth_get_tag_length(stream->session_keys[0].rtp_auth); |
2015 | 90.9k | } |
2016 | | |
2017 | 90.9k | return srtp_get_session_keys_for_packet(stream, hdr, pkt_octet_len, tag_len, |
2018 | 90.9k | session_keys); |
2019 | 90.9k | } |
2020 | | |
2021 | | static srtp_err_status_t srtp_get_session_keys_for_rtcp_packet( |
2022 | | srtp_stream_ctx_t *stream, |
2023 | | const uint8_t *hdr, |
2024 | | size_t pkt_octet_len, |
2025 | | srtp_session_keys_t **session_keys) |
2026 | 2.34k | { |
2027 | 2.34k | size_t tag_len = 0; |
2028 | | |
2029 | | // Determine the authentication tag size |
2030 | 2.34k | if (stream->session_keys[0].rtcp_cipher->algorithm == SRTP_AES_GCM_128 || |
2031 | 2.34k | stream->session_keys[0].rtcp_cipher->algorithm == SRTP_AES_GCM_256) { |
2032 | 0 | tag_len = 0; |
2033 | 2.34k | } else { |
2034 | 2.34k | tag_len = srtp_auth_get_tag_length(stream->session_keys[0].rtcp_auth); |
2035 | 2.34k | } |
2036 | | |
2037 | 2.34k | return srtp_get_session_keys_for_packet(stream, hdr, pkt_octet_len, tag_len, |
2038 | 2.34k | session_keys); |
2039 | 2.34k | } |
2040 | | |
2041 | | static srtp_err_status_t srtp_estimate_index(srtp_rdbx_t *rdbx, |
2042 | | uint32_t roc, |
2043 | | srtp_xtd_seq_num_t *est, |
2044 | | srtp_sequence_number_t seq, |
2045 | | ssize_t *delta) |
2046 | 1.11k | { |
2047 | 1.11k | *est = (srtp_xtd_seq_num_t)(((uint64_t)roc) << 16) | seq; |
2048 | 1.11k | *delta = *est - rdbx->index; |
2049 | | |
2050 | 1.11k | if (*est > rdbx->index) { |
2051 | 547 | if (*est - rdbx->index > seq_num_median) { |
2052 | 382 | *delta = 0; |
2053 | 382 | return srtp_err_status_pkt_idx_adv; |
2054 | 382 | } |
2055 | 565 | } else if (*est < rdbx->index) { |
2056 | 358 | if (rdbx->index - *est > seq_num_median) { |
2057 | 96 | *delta = 0; |
2058 | 96 | return srtp_err_status_pkt_idx_old; |
2059 | 96 | } |
2060 | 358 | } |
2061 | | |
2062 | 634 | return srtp_err_status_ok; |
2063 | 1.11k | } |
2064 | | |
2065 | | static srtp_err_status_t srtp_get_est_pkt_index(const srtp_hdr_t *hdr, |
2066 | | srtp_stream_ctx_t *stream, |
2067 | | srtp_xtd_seq_num_t *est, |
2068 | | ssize_t *delta) |
2069 | 37.0k | { |
2070 | 37.0k | srtp_err_status_t result = srtp_err_status_ok; |
2071 | | |
2072 | 37.0k | if (stream->pending_roc) { |
2073 | 1.11k | result = srtp_estimate_index(&stream->rtp_rdbx, stream->pending_roc, |
2074 | 1.11k | est, ntohs(hdr->seq), delta); |
2075 | 35.9k | } else { |
2076 | | /* estimate packet index from seq. num. in header */ |
2077 | 35.9k | *delta = |
2078 | 35.9k | srtp_rdbx_estimate_index(&stream->rtp_rdbx, est, ntohs(hdr->seq)); |
2079 | 35.9k | } |
2080 | | |
2081 | 37.0k | debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, *est); |
2082 | | |
2083 | 37.0k | return result; |
2084 | 37.0k | } |
2085 | | |
2086 | | /* |
2087 | | * This function handles outgoing SRTP packets while in AEAD mode, |
2088 | | * which currently supports AES-GCM encryption. All packets are |
2089 | | * encrypted and authenticated. |
2090 | | */ |
2091 | | static srtp_err_status_t srtp_protect_aead(srtp_ctx_t *ctx, |
2092 | | srtp_stream_ctx_t *stream, |
2093 | | const uint8_t *rtp, |
2094 | | size_t rtp_len, |
2095 | | uint8_t *srtp, |
2096 | | size_t *srtp_len, |
2097 | | srtp_session_keys_t *session_keys) |
2098 | 0 | { |
2099 | 0 | const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp; |
2100 | 0 | size_t enc_start; /* offset to start of encrypted portion */ |
2101 | 0 | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
2102 | 0 | srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */ |
2103 | 0 | ssize_t delta; /* delta of local pkt idx and that in hdr */ |
2104 | 0 | srtp_err_status_t status; |
2105 | 0 | size_t tag_len; |
2106 | 0 | v128_t iv; |
2107 | 0 | size_t aad_len; |
2108 | |
|
2109 | 0 | debug_print0(mod_srtp, "function srtp_protect_aead"); |
2110 | | |
2111 | | /* |
2112 | | * update the key usage limit, and check it to make sure that we |
2113 | | * didn't just hit either the soft limit or the hard limit, and call |
2114 | | * the event handler if we hit either. |
2115 | | */ |
2116 | 0 | switch (srtp_key_limit_update(session_keys->limit)) { |
2117 | 0 | case srtp_key_event_normal: |
2118 | 0 | break; |
2119 | 0 | case srtp_key_event_hard_limit: |
2120 | 0 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
2121 | 0 | return srtp_err_status_key_expired; |
2122 | 0 | case srtp_key_event_soft_limit: |
2123 | 0 | default: |
2124 | 0 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
2125 | 0 | break; |
2126 | 0 | } |
2127 | | |
2128 | | /* get tag length from stream */ |
2129 | 0 | tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); |
2130 | | |
2131 | | /* check output length */ |
2132 | 0 | if (*srtp_len < rtp_len + tag_len + stream->mki_size) { |
2133 | 0 | return srtp_err_status_buffer_small; |
2134 | 0 | } |
2135 | | |
2136 | | /* |
2137 | | * find starting point for encryption and length of data to be |
2138 | | * encrypted - the encrypted portion starts after the rtp header |
2139 | | * extension, if present; otherwise, it starts after the last csrc, |
2140 | | * if any are present |
2141 | | */ |
2142 | 0 | enc_start = srtp_get_rtp_hdr_len(hdr); |
2143 | 0 | if (hdr->x == 1) { |
2144 | 0 | enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, rtp); |
2145 | 0 | } |
2146 | |
|
2147 | 0 | bool cryptex_inuse, cryptex_inplace; |
2148 | 0 | status = srtp_cryptex_protect_init(stream, hdr, rtp, srtp, &cryptex_inuse, |
2149 | 0 | &cryptex_inplace, &enc_start); |
2150 | 0 | if (status) { |
2151 | 0 | return status; |
2152 | 0 | } |
2153 | | |
2154 | 0 | if (cryptex_inuse && !cryptex_inplace && hdr->cc) { |
2155 | 0 | debug_print0(mod_srtp, |
2156 | 0 | "unsupported cryptex mode, AEAD, CC and not inplace io"); |
2157 | 0 | return srtp_err_status_cryptex_err; |
2158 | 0 | } |
2159 | | |
2160 | | /* note: the passed size is without the auth tag */ |
2161 | 0 | if (enc_start > rtp_len) { |
2162 | 0 | return srtp_err_status_parse_err; |
2163 | 0 | } |
2164 | 0 | enc_octet_len = rtp_len - enc_start; |
2165 | | |
2166 | | /* if not-inplace then need to copy full rtp header */ |
2167 | 0 | if (rtp != srtp) { |
2168 | 0 | memcpy(srtp, rtp, enc_start); |
2169 | 0 | } |
2170 | | |
2171 | | /* |
2172 | | * estimate the packet index using the start of the replay window |
2173 | | * and the sequence number from the header |
2174 | | */ |
2175 | 0 | status = srtp_get_est_pkt_index(hdr, stream, &est, &delta); |
2176 | |
|
2177 | 0 | if (status && (status != srtp_err_status_pkt_idx_adv)) { |
2178 | 0 | return status; |
2179 | 0 | } |
2180 | | |
2181 | 0 | if (status == srtp_err_status_pkt_idx_adv) { |
2182 | 0 | srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, (uint32_t)(est >> 16), |
2183 | 0 | (uint16_t)(est & 0xFFFF)); |
2184 | 0 | stream->pending_roc = 0; |
2185 | 0 | srtp_rdbx_add_index(&stream->rtp_rdbx, 0); |
2186 | 0 | } else { |
2187 | 0 | status = srtp_rdbx_check(&stream->rtp_rdbx, delta); |
2188 | 0 | if (status) { |
2189 | 0 | if (status != srtp_err_status_replay_fail || |
2190 | 0 | !stream->allow_repeat_tx) |
2191 | 0 | return status; /* we've been asked to reuse an index */ |
2192 | 0 | } |
2193 | 0 | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
2194 | 0 | } |
2195 | | |
2196 | 0 | debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est); |
2197 | | |
2198 | | /* |
2199 | | * AEAD uses a new IV formation method |
2200 | | */ |
2201 | 0 | srtp_calc_aead_iv(session_keys, &iv, &est, hdr); |
2202 | | /* shift est, put into network byte order */ |
2203 | 0 | est = be64_to_cpu(est << 16); |
2204 | |
|
2205 | 0 | status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, |
2206 | 0 | srtp_direction_encrypt); |
2207 | 0 | if (!status && session_keys->rtp_xtn_hdr_cipher) { |
2208 | 0 | iv.v32[0] = 0; |
2209 | 0 | iv.v32[1] = hdr->ssrc; |
2210 | 0 | iv.v64[1] = est; |
2211 | 0 | status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, |
2212 | 0 | (uint8_t *)&iv, srtp_direction_encrypt); |
2213 | 0 | } |
2214 | 0 | if (status) { |
2215 | 0 | return srtp_err_status_cipher_fail; |
2216 | 0 | } |
2217 | | |
2218 | 0 | if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) { |
2219 | | /* |
2220 | | * extensions header encryption RFC 6904 |
2221 | | */ |
2222 | 0 | status = srtp_process_header_encryption( |
2223 | 0 | stream, srtp_get_rtp_xtn_hdr(hdr, srtp), session_keys); |
2224 | 0 | if (status) { |
2225 | 0 | return status; |
2226 | 0 | } |
2227 | 0 | } |
2228 | | |
2229 | 0 | if (cryptex_inuse) { |
2230 | 0 | status = srtp_cryptex_protect(cryptex_inplace, hdr, srtp, |
2231 | 0 | session_keys->rtp_cipher); |
2232 | 0 | if (status) { |
2233 | 0 | return status; |
2234 | 0 | } |
2235 | 0 | } |
2236 | | |
2237 | | /* |
2238 | | * Set the AAD over the RTP header |
2239 | | */ |
2240 | 0 | aad_len = enc_start; |
2241 | 0 | status = srtp_cipher_set_aad(session_keys->rtp_cipher, srtp, aad_len); |
2242 | 0 | if (status) { |
2243 | 0 | return (srtp_err_status_cipher_fail); |
2244 | 0 | } |
2245 | | |
2246 | | /* Encrypt the payload */ |
2247 | 0 | size_t outlen = *srtp_len - enc_start; |
2248 | 0 | status = srtp_cipher_encrypt(session_keys->rtp_cipher, rtp + enc_start, |
2249 | 0 | enc_octet_len, srtp + enc_start, &outlen); |
2250 | 0 | enc_octet_len = outlen; |
2251 | 0 | if (status) { |
2252 | 0 | return srtp_err_status_cipher_fail; |
2253 | 0 | } |
2254 | | |
2255 | 0 | if (stream->use_mki) { |
2256 | 0 | srtp_inject_mki(srtp + enc_start + enc_octet_len, session_keys, |
2257 | 0 | stream->mki_size); |
2258 | 0 | } |
2259 | |
|
2260 | 0 | if (cryptex_inuse) { |
2261 | 0 | srtp_cryptex_protect_cleanup(cryptex_inplace, hdr, srtp); |
2262 | 0 | } |
2263 | |
|
2264 | 0 | *srtp_len = enc_start + enc_octet_len; |
2265 | | |
2266 | | /* increase the packet length by the length of the mki_size */ |
2267 | 0 | *srtp_len += stream->mki_size; |
2268 | |
|
2269 | 0 | return srtp_err_status_ok; |
2270 | 0 | } |
2271 | | |
2272 | | /* |
2273 | | * This function handles incoming SRTP packets while in AEAD mode, |
2274 | | * which currently supports AES-GCM encryption. All packets are |
2275 | | * encrypted and authenticated. Note, the auth tag is at the end |
2276 | | * of the packet stream and is automatically checked by GCM |
2277 | | * when decrypting the payload. |
2278 | | */ |
2279 | | static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx, |
2280 | | srtp_stream_ctx_t *stream, |
2281 | | ssize_t delta, |
2282 | | srtp_xtd_seq_num_t est, |
2283 | | const uint8_t *srtp, |
2284 | | size_t srtp_len, |
2285 | | uint8_t *rtp, |
2286 | | size_t *rtp_len, |
2287 | | srtp_session_keys_t *session_keys, |
2288 | | bool advance_packet_index) |
2289 | 0 | { |
2290 | 0 | const srtp_hdr_t *hdr = (const srtp_hdr_t *)srtp; |
2291 | 0 | size_t enc_start; /* offset to start of encrypted portion */ |
2292 | 0 | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
2293 | 0 | v128_t iv; |
2294 | 0 | srtp_err_status_t status; |
2295 | 0 | size_t tag_len; |
2296 | 0 | size_t aad_len; |
2297 | |
|
2298 | 0 | debug_print0(mod_srtp, "function srtp_unprotect_aead"); |
2299 | |
|
2300 | 0 | debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est); |
2301 | | |
2302 | | /* get tag length from stream */ |
2303 | 0 | tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); |
2304 | | |
2305 | | /* |
2306 | | * AEAD uses a new IV formation method |
2307 | | */ |
2308 | 0 | srtp_calc_aead_iv(session_keys, &iv, &est, hdr); |
2309 | 0 | status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, |
2310 | 0 | srtp_direction_decrypt); |
2311 | 0 | if (!status && session_keys->rtp_xtn_hdr_cipher) { |
2312 | 0 | iv.v32[0] = 0; |
2313 | 0 | iv.v32[1] = hdr->ssrc; |
2314 | 0 | iv.v64[1] = be64_to_cpu(est << 16); |
2315 | 0 | status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, |
2316 | 0 | (uint8_t *)&iv, srtp_direction_encrypt); |
2317 | 0 | } |
2318 | 0 | if (status) { |
2319 | 0 | return srtp_err_status_cipher_fail; |
2320 | 0 | } |
2321 | | |
2322 | 0 | enc_start = srtp_get_rtp_hdr_len(hdr); |
2323 | 0 | if (hdr->x == 1) { |
2324 | 0 | enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, srtp); |
2325 | 0 | } |
2326 | |
|
2327 | 0 | bool cryptex_inuse, cryptex_inplace; |
2328 | 0 | status = srtp_cryptex_unprotect_init(stream, hdr, srtp, rtp, &cryptex_inuse, |
2329 | 0 | &cryptex_inplace, &enc_start); |
2330 | 0 | if (status) { |
2331 | 0 | return status; |
2332 | 0 | } |
2333 | | |
2334 | 0 | if (cryptex_inuse && !cryptex_inplace && hdr->cc) { |
2335 | 0 | debug_print0(mod_srtp, |
2336 | 0 | "unsupported cryptex mode, AEAD, CC and not inplace io"); |
2337 | 0 | return srtp_err_status_cryptex_err; |
2338 | 0 | } |
2339 | | |
2340 | 0 | if (enc_start > srtp_len - tag_len - stream->mki_size) { |
2341 | 0 | return srtp_err_status_parse_err; |
2342 | 0 | } |
2343 | | |
2344 | | /* |
2345 | | * We pass the tag down to the cipher when doing GCM mode |
2346 | | */ |
2347 | 0 | enc_octet_len = srtp_len - enc_start - stream->mki_size; |
2348 | | |
2349 | | /* |
2350 | | * Sanity check the encrypted payload length against |
2351 | | * the tag size. It must always be at least as large |
2352 | | * as the tag length. |
2353 | | */ |
2354 | 0 | if (enc_octet_len < tag_len) { |
2355 | 0 | return srtp_err_status_cipher_fail; |
2356 | 0 | } |
2357 | | |
2358 | | /* check output length */ |
2359 | 0 | if (*rtp_len < srtp_len - stream->mki_size - tag_len) { |
2360 | 0 | return srtp_err_status_buffer_small; |
2361 | 0 | } |
2362 | | |
2363 | | /* if not-inplace then need to copy full rtp header */ |
2364 | 0 | if (srtp != rtp) { |
2365 | 0 | memcpy(rtp, srtp, enc_start); |
2366 | 0 | } |
2367 | | |
2368 | | /* |
2369 | | * update the key usage limit, and check it to make sure that we |
2370 | | * didn't just hit either the soft limit or the hard limit, and call |
2371 | | * the event handler if we hit either. |
2372 | | */ |
2373 | 0 | switch (srtp_key_limit_update(session_keys->limit)) { |
2374 | 0 | case srtp_key_event_normal: |
2375 | 0 | break; |
2376 | 0 | case srtp_key_event_soft_limit: |
2377 | 0 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
2378 | 0 | break; |
2379 | 0 | case srtp_key_event_hard_limit: |
2380 | 0 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
2381 | 0 | return srtp_err_status_key_expired; |
2382 | 0 | default: |
2383 | 0 | break; |
2384 | 0 | } |
2385 | | |
2386 | 0 | if (cryptex_inuse) { |
2387 | 0 | status = srtp_cryptex_unprotect(cryptex_inplace, hdr, rtp, |
2388 | 0 | session_keys->rtp_cipher); |
2389 | 0 | if (status) { |
2390 | 0 | return status; |
2391 | 0 | } |
2392 | 0 | } |
2393 | | |
2394 | | /* |
2395 | | * Set the AAD for AES-GCM, which is the RTP header |
2396 | | */ |
2397 | 0 | aad_len = enc_start; |
2398 | 0 | status = srtp_cipher_set_aad(session_keys->rtp_cipher, srtp, aad_len); |
2399 | 0 | if (status) { |
2400 | 0 | return srtp_err_status_cipher_fail; |
2401 | 0 | } |
2402 | | |
2403 | | /* Decrypt the ciphertext. This also checks the auth tag based |
2404 | | * on the AAD we just specified above */ |
2405 | 0 | status = |
2406 | 0 | srtp_cipher_decrypt(session_keys->rtp_cipher, srtp + enc_start, |
2407 | 0 | enc_octet_len, rtp + enc_start, &enc_octet_len); |
2408 | 0 | if (status) { |
2409 | 0 | return status; |
2410 | 0 | } |
2411 | | |
2412 | 0 | if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) { |
2413 | | /* |
2414 | | * extensions header encryption RFC 6904 |
2415 | | */ |
2416 | 0 | status = srtp_process_header_encryption( |
2417 | 0 | stream, srtp_get_rtp_xtn_hdr(hdr, rtp), session_keys); |
2418 | 0 | if (status) { |
2419 | 0 | return status; |
2420 | 0 | } |
2421 | 0 | } |
2422 | | |
2423 | 0 | if (cryptex_inuse) { |
2424 | 0 | srtp_cryptex_unprotect_cleanup(cryptex_inplace, hdr, rtp); |
2425 | 0 | } |
2426 | | |
2427 | | /* |
2428 | | * verify that stream is for received traffic - this check will |
2429 | | * detect SSRC collisions, since a stream that appears in both |
2430 | | * srtp_protect() and srtp_unprotect() will fail this test in one of |
2431 | | * those functions. |
2432 | | * |
2433 | | * we do this check *after* the authentication check, so that the |
2434 | | * latter check will catch any attempts to fool us into thinking |
2435 | | * that we've got a collision |
2436 | | */ |
2437 | 0 | if (stream->direction != dir_srtp_receiver) { |
2438 | 0 | if (stream->direction == dir_unknown) { |
2439 | 0 | stream->direction = dir_srtp_receiver; |
2440 | 0 | } else { |
2441 | 0 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
2442 | 0 | } |
2443 | 0 | } |
2444 | | |
2445 | | /* |
2446 | | * if the stream is a 'provisional' one, in which the template context |
2447 | | * is used, then we need to allocate a new stream at this point, since |
2448 | | * the authentication passed |
2449 | | */ |
2450 | 0 | if (stream == ctx->stream_template) { |
2451 | 0 | srtp_stream_ctx_t *new_stream; |
2452 | | |
2453 | | /* |
2454 | | * allocate and initialize a new stream |
2455 | | * |
2456 | | * note that we indicate failure if we can't allocate the new |
2457 | | * stream, and some implementations will want to not return |
2458 | | * failure here |
2459 | | */ |
2460 | 0 | status = |
2461 | 0 | srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
2462 | 0 | if (status) { |
2463 | 0 | return status; |
2464 | 0 | } |
2465 | | |
2466 | | /* add new stream to the list */ |
2467 | 0 | status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream, |
2468 | 0 | ctx->stream_template); |
2469 | 0 | if (status) { |
2470 | 0 | return status; |
2471 | 0 | } |
2472 | | |
2473 | | /* set stream (the pointer used in this function) */ |
2474 | 0 | stream = new_stream; |
2475 | 0 | } |
2476 | | |
2477 | | /* |
2478 | | * the message authentication function passed, so add the packet |
2479 | | * index into the replay database |
2480 | | */ |
2481 | 0 | if (advance_packet_index) { |
2482 | 0 | uint32_t roc_to_set = (uint32_t)(est >> 16); |
2483 | 0 | uint16_t seq_to_set = (uint16_t)(est & 0xFFFF); |
2484 | 0 | srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, roc_to_set, seq_to_set); |
2485 | 0 | stream->pending_roc = 0; |
2486 | 0 | srtp_rdbx_add_index(&stream->rtp_rdbx, 0); |
2487 | 0 | } else { |
2488 | 0 | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
2489 | 0 | } |
2490 | |
|
2491 | 0 | *rtp_len = enc_start + enc_octet_len; |
2492 | |
|
2493 | 0 | return srtp_err_status_ok; |
2494 | 0 | } |
2495 | | |
2496 | | srtp_err_status_t srtp_protect(srtp_t ctx, |
2497 | | const uint8_t *rtp, |
2498 | | size_t rtp_len, |
2499 | | uint8_t *srtp, |
2500 | | size_t *srtp_len, |
2501 | | size_t mki_index) |
2502 | 24.8k | { |
2503 | 24.8k | const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp; |
2504 | 24.8k | size_t enc_start; /* offset to start of encrypted portion */ |
2505 | 24.8k | uint8_t *auth_start; /* pointer to start of auth. portion */ |
2506 | 24.8k | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
2507 | 24.8k | srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */ |
2508 | 24.8k | ssize_t delta; /* delta of local pkt idx and that in hdr */ |
2509 | 24.8k | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
2510 | 24.8k | srtp_err_status_t status; |
2511 | 24.8k | size_t tag_len; |
2512 | 24.8k | srtp_stream_ctx_t *stream; |
2513 | 24.8k | size_t prefix_len; |
2514 | 24.8k | srtp_session_keys_t *session_keys = NULL; |
2515 | | |
2516 | 24.8k | debug_print0(mod_srtp, "function srtp_protect"); |
2517 | | |
2518 | | /* Verify RTP header */ |
2519 | 24.8k | status = srtp_validate_rtp_header(rtp, rtp_len); |
2520 | 24.8k | if (status) { |
2521 | 78 | return status; |
2522 | 78 | } |
2523 | | |
2524 | | /* check the packet length - it must at least contain a full header */ |
2525 | 24.7k | if (rtp_len < octets_in_rtp_header) { |
2526 | 0 | return srtp_err_status_bad_param; |
2527 | 0 | } |
2528 | | |
2529 | | /* |
2530 | | * look up ssrc in srtp_stream list, and process the packet with |
2531 | | * the appropriate stream. if we haven't seen this stream before, |
2532 | | * there's a template key for this srtp_session, and the cipher |
2533 | | * supports key-sharing, then we assume that a new stream using |
2534 | | * that key has just started up |
2535 | | */ |
2536 | 24.7k | stream = srtp_get_stream(ctx, hdr->ssrc); |
2537 | 24.7k | if (stream == NULL) { |
2538 | 7.58k | if (ctx->stream_template != NULL) { |
2539 | 7.55k | srtp_stream_ctx_t *new_stream; |
2540 | | |
2541 | | /* allocate and initialize a new stream */ |
2542 | 7.55k | status = |
2543 | 7.55k | srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
2544 | 7.55k | if (status) { |
2545 | 0 | return status; |
2546 | 0 | } |
2547 | | |
2548 | | /* add new stream to the list */ |
2549 | 7.55k | status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream, |
2550 | 7.55k | ctx->stream_template); |
2551 | 7.55k | if (status) { |
2552 | 0 | return status; |
2553 | 0 | } |
2554 | | |
2555 | | /* set direction to outbound */ |
2556 | 7.55k | new_stream->direction = dir_srtp_sender; |
2557 | | |
2558 | | /* set stream (the pointer used in this function) */ |
2559 | 7.55k | stream = new_stream; |
2560 | 7.55k | } else { |
2561 | | /* no template stream, so we return an error */ |
2562 | 31 | return srtp_err_status_no_ctx; |
2563 | 31 | } |
2564 | 7.58k | } |
2565 | | |
2566 | | /* |
2567 | | * verify that stream is for sending traffic - this check will |
2568 | | * detect SSRC collisions, since a stream that appears in both |
2569 | | * srtp_protect() and srtp_unprotect() will fail this test in one of |
2570 | | * those functions. |
2571 | | */ |
2572 | | |
2573 | 24.7k | if (stream->direction != dir_srtp_sender) { |
2574 | 10.9k | if (stream->direction == dir_unknown) { |
2575 | 116 | stream->direction = dir_srtp_sender; |
2576 | 10.8k | } else { |
2577 | 10.8k | srtp_handle_event(ctx, stream, event_ssrc_collision); |
2578 | 10.8k | } |
2579 | 10.9k | } |
2580 | | |
2581 | 24.7k | status = srtp_get_session_keys(stream, mki_index, &session_keys); |
2582 | 24.7k | if (status) { |
2583 | 0 | return status; |
2584 | 0 | } |
2585 | | |
2586 | | /* |
2587 | | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
2588 | | * the request to our AEAD handler. |
2589 | | */ |
2590 | 24.7k | if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || |
2591 | 24.7k | session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { |
2592 | 0 | return srtp_protect_aead(ctx, stream, rtp, rtp_len, srtp, srtp_len, |
2593 | 0 | session_keys); |
2594 | 0 | } |
2595 | | |
2596 | | /* |
2597 | | * update the key usage limit, and check it to make sure that we |
2598 | | * didn't just hit either the soft limit or the hard limit, and call |
2599 | | * the event handler if we hit either. |
2600 | | */ |
2601 | 24.7k | switch (srtp_key_limit_update(session_keys->limit)) { |
2602 | 24.7k | case srtp_key_event_normal: |
2603 | 24.7k | break; |
2604 | 0 | case srtp_key_event_soft_limit: |
2605 | 0 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
2606 | 0 | break; |
2607 | 0 | case srtp_key_event_hard_limit: |
2608 | 0 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
2609 | 0 | return srtp_err_status_key_expired; |
2610 | 0 | default: |
2611 | 0 | break; |
2612 | 24.7k | } |
2613 | | |
2614 | | /* get tag length from stream */ |
2615 | 24.7k | tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); |
2616 | | |
2617 | | /* check output length */ |
2618 | 24.7k | if (*srtp_len < rtp_len + stream->mki_size + tag_len) { |
2619 | 0 | return srtp_err_status_buffer_small; |
2620 | 0 | } |
2621 | | |
2622 | | /* |
2623 | | * find starting point for encryption and length of data to be |
2624 | | * encrypted - the encrypted portion starts after the rtp header |
2625 | | * extension, if present; otherwise, it starts after the last csrc, |
2626 | | * if any are present |
2627 | | */ |
2628 | 24.7k | enc_start = srtp_get_rtp_hdr_len(hdr); |
2629 | 24.7k | if (hdr->x == 1) { |
2630 | 2.50k | enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, rtp); |
2631 | 2.50k | } |
2632 | | |
2633 | 24.7k | bool cryptex_inuse, cryptex_inplace; |
2634 | 24.7k | status = srtp_cryptex_protect_init(stream, hdr, rtp, srtp, &cryptex_inuse, |
2635 | 24.7k | &cryptex_inplace, &enc_start); |
2636 | 24.7k | if (status) { |
2637 | 0 | return status; |
2638 | 0 | } |
2639 | | |
2640 | 24.7k | if (enc_start > rtp_len) { |
2641 | 0 | return srtp_err_status_parse_err; |
2642 | 0 | } |
2643 | 24.7k | enc_octet_len = rtp_len - enc_start; |
2644 | | |
2645 | | /* if not-inplace then need to copy full rtp header */ |
2646 | 24.7k | if (rtp != srtp) { |
2647 | 0 | memcpy(srtp, rtp, enc_start); |
2648 | 0 | } |
2649 | | |
2650 | 24.7k | if (stream->use_mki) { |
2651 | 0 | srtp_inject_mki(srtp + rtp_len, session_keys, stream->mki_size); |
2652 | 0 | } |
2653 | | |
2654 | | /* |
2655 | | * if we're providing authentication, set the auth_start and auth_tag |
2656 | | * pointers to the proper locations; otherwise, set auth_start to NULL |
2657 | | * to indicate that no authentication is needed |
2658 | | */ |
2659 | 24.7k | if (stream->rtp_services & sec_serv_auth) { |
2660 | 7.20k | auth_start = srtp; |
2661 | 7.20k | auth_tag = srtp + rtp_len + stream->mki_size; |
2662 | 17.5k | } else { |
2663 | 17.5k | auth_start = NULL; |
2664 | 17.5k | auth_tag = NULL; |
2665 | 17.5k | } |
2666 | | |
2667 | | /* |
2668 | | * estimate the packet index using the start of the replay window |
2669 | | * and the sequence number from the header |
2670 | | */ |
2671 | 24.7k | status = srtp_get_est_pkt_index(hdr, stream, &est, &delta); |
2672 | | |
2673 | 24.7k | if (status && (status != srtp_err_status_pkt_idx_adv)) { |
2674 | 51 | return status; |
2675 | 51 | } |
2676 | | |
2677 | 24.6k | if (status == srtp_err_status_pkt_idx_adv) { |
2678 | 214 | srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, (uint32_t)(est >> 16), |
2679 | 214 | (uint16_t)(est & 0xFFFF)); |
2680 | 214 | stream->pending_roc = 0; |
2681 | 214 | srtp_rdbx_add_index(&stream->rtp_rdbx, 0); |
2682 | 24.4k | } else { |
2683 | 24.4k | status = srtp_rdbx_check(&stream->rtp_rdbx, delta); |
2684 | 24.4k | if (status) { |
2685 | 15.3k | if (status != srtp_err_status_replay_fail || |
2686 | 15.3k | !stream->allow_repeat_tx) |
2687 | 78 | return status; /* we've been asked to reuse an index */ |
2688 | 15.3k | } |
2689 | 24.4k | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
2690 | 24.4k | } |
2691 | | |
2692 | 24.6k | debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est); |
2693 | | |
2694 | | /* |
2695 | | * if we're using rindael counter mode, set nonce and seq |
2696 | | */ |
2697 | 24.6k | if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 || |
2698 | 17.1k | session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 || |
2699 | 17.1k | session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) { |
2700 | 7.76k | v128_t iv; |
2701 | | |
2702 | 7.76k | iv.v32[0] = 0; |
2703 | 7.76k | iv.v32[1] = hdr->ssrc; |
2704 | 7.76k | iv.v64[1] = be64_to_cpu(est << 16); |
2705 | 7.76k | status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, |
2706 | 7.76k | srtp_direction_encrypt); |
2707 | 7.76k | if (!status && session_keys->rtp_xtn_hdr_cipher) { |
2708 | 2.12k | status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, |
2709 | 2.12k | (uint8_t *)&iv, srtp_direction_encrypt); |
2710 | 2.12k | } |
2711 | 16.8k | } else { |
2712 | 16.8k | v128_t iv; |
2713 | | |
2714 | | /* otherwise, set the index to est */ |
2715 | 16.8k | iv.v64[0] = 0; |
2716 | 16.8k | iv.v64[1] = be64_to_cpu(est); |
2717 | 16.8k | status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, |
2718 | 16.8k | srtp_direction_encrypt); |
2719 | 16.8k | if (!status && session_keys->rtp_xtn_hdr_cipher) { |
2720 | 9.97k | status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, |
2721 | 9.97k | (uint8_t *)&iv, srtp_direction_encrypt); |
2722 | 9.97k | } |
2723 | 16.8k | } |
2724 | 24.6k | if (status) { |
2725 | 0 | return srtp_err_status_cipher_fail; |
2726 | 0 | } |
2727 | | |
2728 | | /* shift est, put into network byte order */ |
2729 | 24.6k | est = be64_to_cpu(est << 16); |
2730 | | |
2731 | | /* |
2732 | | * if we're authenticating using a universal hash, put the keystream |
2733 | | * prefix into the authentication tag |
2734 | | */ |
2735 | 24.6k | if (auth_start) { |
2736 | 7.20k | prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth); |
2737 | 7.20k | if (prefix_len) { |
2738 | 0 | status = srtp_cipher_output(session_keys->rtp_cipher, auth_tag, |
2739 | 0 | &prefix_len); |
2740 | 0 | if (status) { |
2741 | 0 | return srtp_err_status_cipher_fail; |
2742 | 0 | } |
2743 | 0 | debug_print(mod_srtp, "keystream prefix: %s", |
2744 | 0 | srtp_octet_string_hex_string(auth_tag, prefix_len)); |
2745 | 0 | } |
2746 | 7.20k | } |
2747 | | |
2748 | 24.6k | if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) { |
2749 | | /* |
2750 | | * extensions header encryption RFC 6904 |
2751 | | */ |
2752 | 1.14k | status = srtp_process_header_encryption( |
2753 | 1.14k | stream, srtp_get_rtp_xtn_hdr(hdr, srtp), session_keys); |
2754 | 1.14k | if (status) { |
2755 | 46 | return status; |
2756 | 46 | } |
2757 | 1.14k | } |
2758 | | |
2759 | 24.5k | if (cryptex_inuse) { |
2760 | 0 | status = srtp_cryptex_protect(cryptex_inplace, hdr, srtp, |
2761 | 0 | session_keys->rtp_cipher); |
2762 | 0 | if (status) { |
2763 | 0 | return status; |
2764 | 0 | } |
2765 | 0 | } |
2766 | | |
2767 | | /* if we're encrypting, exor keystream into the message */ |
2768 | 24.5k | if (stream->rtp_services & sec_serv_conf) { |
2769 | 7.75k | status = srtp_cipher_encrypt(session_keys->rtp_cipher, rtp + enc_start, |
2770 | 7.75k | enc_octet_len, srtp + enc_start, |
2771 | 7.75k | &enc_octet_len); |
2772 | 7.75k | if (status) { |
2773 | 0 | return srtp_err_status_cipher_fail; |
2774 | 0 | } |
2775 | 16.8k | } else if (rtp != srtp) { |
2776 | | /* if no encryption and not-inplace then need to copy rest of packet */ |
2777 | 0 | memcpy(srtp + enc_start, rtp + enc_start, enc_octet_len); |
2778 | 0 | } |
2779 | | |
2780 | 24.5k | if (cryptex_inuse) { |
2781 | 0 | srtp_cryptex_protect_cleanup(cryptex_inplace, hdr, srtp); |
2782 | 0 | } |
2783 | | |
2784 | | /* |
2785 | | * if we're authenticating, run authentication function and put result |
2786 | | * into the auth_tag |
2787 | | */ |
2788 | 24.5k | if (auth_start) { |
2789 | | /* initialize auth func context */ |
2790 | 7.19k | status = srtp_auth_start(session_keys->rtp_auth); |
2791 | 7.19k | if (status) { |
2792 | 0 | return status; |
2793 | 0 | } |
2794 | | |
2795 | | /* run auth func over packet */ |
2796 | 7.19k | status = srtp_auth_update(session_keys->rtp_auth, auth_start, rtp_len); |
2797 | 7.19k | if (status) { |
2798 | 0 | return status; |
2799 | 0 | } |
2800 | | |
2801 | | /* run auth func over ROC, put result into auth_tag */ |
2802 | 7.19k | debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est); |
2803 | 7.19k | status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4, |
2804 | 7.19k | auth_tag); |
2805 | 7.19k | debug_print(mod_srtp, "srtp auth tag: %s", |
2806 | 7.19k | srtp_octet_string_hex_string(auth_tag, tag_len)); |
2807 | 7.19k | if (status) { |
2808 | 0 | return status; |
2809 | 0 | } |
2810 | 7.19k | } |
2811 | | |
2812 | 24.5k | *srtp_len = enc_start + enc_octet_len; |
2813 | | |
2814 | | /* increase the packet length by the length of the auth tag */ |
2815 | 24.5k | *srtp_len += tag_len; |
2816 | | |
2817 | | /* increate the packet length by the mki size if used */ |
2818 | 24.5k | *srtp_len += stream->mki_size; |
2819 | | |
2820 | 24.5k | return srtp_err_status_ok; |
2821 | 24.5k | } |
2822 | | |
2823 | | srtp_err_status_t srtp_unprotect(srtp_t ctx, |
2824 | | const uint8_t *srtp, |
2825 | | size_t srtp_len, |
2826 | | uint8_t *rtp, |
2827 | | size_t *rtp_len) |
2828 | 91.2k | { |
2829 | 91.2k | const srtp_hdr_t *hdr = (const srtp_hdr_t *)srtp; |
2830 | 91.2k | size_t enc_start; /* pointer to start of encrypted portion */ |
2831 | 91.2k | const uint8_t *auth_start; /* pointer to start of auth. portion */ |
2832 | 91.2k | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
2833 | 91.2k | const uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
2834 | 91.2k | srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */ |
2835 | 91.2k | ssize_t delta; /* delta of local pkt idx and that in hdr */ |
2836 | 91.2k | v128_t iv; |
2837 | 91.2k | srtp_err_status_t status; |
2838 | 91.2k | srtp_stream_ctx_t *stream; |
2839 | 91.2k | uint8_t tmp_tag[SRTP_MAX_TAG_LEN]; |
2840 | 91.2k | size_t tag_len, prefix_len; |
2841 | 91.2k | srtp_session_keys_t *session_keys = NULL; |
2842 | 91.2k | bool advance_packet_index = false; |
2843 | 91.2k | uint32_t roc_to_set = 0; |
2844 | 91.2k | uint16_t seq_to_set = 0; |
2845 | | |
2846 | 91.2k | debug_print0(mod_srtp, "function srtp_unprotect"); |
2847 | | |
2848 | | /* Verify RTP header */ |
2849 | 91.2k | status = srtp_validate_rtp_header(srtp, srtp_len); |
2850 | 91.2k | if (status) { |
2851 | 65 | return status; |
2852 | 65 | } |
2853 | | |
2854 | | /* check the packet length - it must at least contain a full header */ |
2855 | 91.1k | if (srtp_len < octets_in_rtp_header) { |
2856 | 0 | return srtp_err_status_bad_param; |
2857 | 0 | } |
2858 | | |
2859 | | /* |
2860 | | * look up ssrc in srtp_stream list, and process the packet with |
2861 | | * the appropriate stream. if we haven't seen this stream before, |
2862 | | * there's only one key for this srtp_session, and the cipher |
2863 | | * supports key-sharing, then we assume that a new stream using |
2864 | | * that key has just started up |
2865 | | */ |
2866 | 91.1k | stream = srtp_get_stream(ctx, hdr->ssrc); |
2867 | 91.1k | if (stream == NULL) { |
2868 | 78.8k | if (ctx->stream_template != NULL) { |
2869 | 78.7k | stream = ctx->stream_template; |
2870 | 78.7k | debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)", |
2871 | 78.7k | (unsigned int)ntohl(hdr->ssrc)); |
2872 | | |
2873 | | /* |
2874 | | * set estimated packet index to sequence number from header, |
2875 | | * and set delta equal to the same value |
2876 | | */ |
2877 | 78.7k | est = (srtp_xtd_seq_num_t)ntohs(hdr->seq); |
2878 | 78.7k | delta = (int)est; |
2879 | 78.7k | } else { |
2880 | | /* |
2881 | | * no stream corresponding to SSRC found, and we don't do |
2882 | | * key-sharing, so return an error |
2883 | | */ |
2884 | 28 | return srtp_err_status_no_ctx; |
2885 | 28 | } |
2886 | 78.8k | } else { |
2887 | 12.3k | status = srtp_get_est_pkt_index(hdr, stream, &est, &delta); |
2888 | | |
2889 | 12.3k | if (status && (status != srtp_err_status_pkt_idx_adv)) { |
2890 | 45 | return status; |
2891 | 45 | } |
2892 | | |
2893 | 12.3k | if (status == srtp_err_status_pkt_idx_adv) { |
2894 | 168 | advance_packet_index = true; |
2895 | 168 | roc_to_set = (uint32_t)(est >> 16); |
2896 | 168 | seq_to_set = (uint16_t)(est & 0xFFFF); |
2897 | 168 | } |
2898 | | |
2899 | | /* check replay database */ |
2900 | 12.3k | if (!advance_packet_index) { |
2901 | 12.1k | status = srtp_rdbx_check(&stream->rtp_rdbx, delta); |
2902 | 12.1k | if (status) { |
2903 | 105 | return status; |
2904 | 105 | } |
2905 | 12.1k | } |
2906 | 12.3k | } |
2907 | | |
2908 | 90.9k | debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est); |
2909 | | |
2910 | | /* Determine if MKI is being used and what session keys should be used */ |
2911 | 90.9k | status = srtp_get_session_keys_for_rtp_packet(stream, srtp, srtp_len, |
2912 | 90.9k | &session_keys); |
2913 | 90.9k | if (status) { |
2914 | 0 | return status; |
2915 | 0 | } |
2916 | | |
2917 | | /* |
2918 | | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
2919 | | * the request to our AEAD handler. |
2920 | | */ |
2921 | 90.9k | if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || |
2922 | 90.9k | session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { |
2923 | 0 | return srtp_unprotect_aead(ctx, stream, delta, est, srtp, srtp_len, rtp, |
2924 | 0 | rtp_len, session_keys, advance_packet_index); |
2925 | 0 | } |
2926 | | |
2927 | | /* get tag length from stream */ |
2928 | 90.9k | tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth); |
2929 | | |
2930 | | /* |
2931 | | * set the cipher's IV properly, depending on whatever cipher we |
2932 | | * happen to be using |
2933 | | */ |
2934 | 90.9k | if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 || |
2935 | 40.4k | session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 || |
2936 | 50.8k | session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) { |
2937 | | /* aes counter mode */ |
2938 | 50.8k | iv.v32[0] = 0; |
2939 | 50.8k | iv.v32[1] = hdr->ssrc; /* still in network order */ |
2940 | 50.8k | iv.v64[1] = be64_to_cpu(est << 16); |
2941 | 50.8k | status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, |
2942 | 50.8k | srtp_direction_decrypt); |
2943 | 50.8k | if (!status && session_keys->rtp_xtn_hdr_cipher) { |
2944 | 844 | status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, |
2945 | 844 | (uint8_t *)&iv, srtp_direction_decrypt); |
2946 | 844 | } |
2947 | 50.8k | } else { |
2948 | | /* no particular format - set the iv to the packet index */ |
2949 | 40.1k | iv.v64[0] = 0; |
2950 | 40.1k | iv.v64[1] = be64_to_cpu(est); |
2951 | 40.1k | status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv, |
2952 | 40.1k | srtp_direction_decrypt); |
2953 | 40.1k | if (!status && session_keys->rtp_xtn_hdr_cipher) { |
2954 | 21.0k | status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher, |
2955 | 21.0k | (uint8_t *)&iv, srtp_direction_decrypt); |
2956 | 21.0k | } |
2957 | 40.1k | } |
2958 | 90.9k | if (status) { |
2959 | 0 | return srtp_err_status_cipher_fail; |
2960 | 0 | } |
2961 | | |
2962 | | /* shift est, put into network byte order */ |
2963 | 90.9k | est = be64_to_cpu(est << 16); |
2964 | | |
2965 | 90.9k | enc_start = srtp_get_rtp_hdr_len(hdr); |
2966 | 90.9k | if (hdr->x == 1) { |
2967 | 770 | enc_start += srtp_get_rtp_hdr_xtnd_len(hdr, srtp); |
2968 | 770 | } |
2969 | | |
2970 | 90.9k | bool cryptex_inuse, cryptex_inplace; |
2971 | 90.9k | status = srtp_cryptex_unprotect_init(stream, hdr, srtp, rtp, &cryptex_inuse, |
2972 | 90.9k | &cryptex_inplace, &enc_start); |
2973 | 90.9k | if (status) { |
2974 | 0 | return status; |
2975 | 0 | } |
2976 | | |
2977 | 90.9k | if (enc_start > srtp_len - tag_len - stream->mki_size) { |
2978 | 9 | return srtp_err_status_parse_err; |
2979 | 9 | } |
2980 | 90.9k | enc_octet_len = srtp_len - enc_start - stream->mki_size - tag_len; |
2981 | | |
2982 | | /* check output length */ |
2983 | 90.9k | if (*rtp_len < srtp_len - stream->mki_size - tag_len) { |
2984 | 0 | return srtp_err_status_buffer_small; |
2985 | 0 | } |
2986 | | |
2987 | | /* if not-inplace then need to copy full rtp header */ |
2988 | 90.9k | if (srtp != rtp) { |
2989 | 0 | memcpy(rtp, srtp, enc_start); |
2990 | 0 | } |
2991 | | |
2992 | | /* |
2993 | | * if we're providing authentication, set the auth_start and auth_tag |
2994 | | * pointers to the proper locations; otherwise, set auth_start to NULL |
2995 | | * to indicate that no authentication is needed |
2996 | | */ |
2997 | 90.9k | if (stream->rtp_services & sec_serv_auth) { |
2998 | 81 | auth_start = srtp; |
2999 | 81 | auth_tag = srtp + srtp_len - tag_len; |
3000 | 90.8k | } else { |
3001 | 90.8k | auth_start = NULL; |
3002 | 90.8k | auth_tag = NULL; |
3003 | 90.8k | } |
3004 | | |
3005 | | /* |
3006 | | * if we expect message authentication, run the authentication |
3007 | | * function and compare the result with the value of the auth_tag |
3008 | | */ |
3009 | 90.9k | if (auth_start) { |
3010 | | /* |
3011 | | * if we're using a universal hash, then we need to compute the |
3012 | | * keystream prefix for encrypting the universal hash output |
3013 | | * |
3014 | | * if the keystream prefix length is zero, then we know that |
3015 | | * the authenticator isn't using a universal hash function |
3016 | | */ |
3017 | 81 | if (session_keys->rtp_auth->prefix_len != 0) { |
3018 | 0 | prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth); |
3019 | 0 | status = srtp_cipher_output(session_keys->rtp_cipher, tmp_tag, |
3020 | 0 | &prefix_len); |
3021 | 0 | debug_print(mod_srtp, "keystream prefix: %s", |
3022 | 0 | srtp_octet_string_hex_string(tmp_tag, prefix_len)); |
3023 | 0 | if (status) { |
3024 | 0 | return srtp_err_status_cipher_fail; |
3025 | 0 | } |
3026 | 0 | } |
3027 | | |
3028 | | /* initialize auth func context */ |
3029 | 81 | status = srtp_auth_start(session_keys->rtp_auth); |
3030 | 81 | if (status) { |
3031 | 0 | return status; |
3032 | 0 | } |
3033 | | |
3034 | | /* now compute auth function over packet */ |
3035 | 81 | status = srtp_auth_update(session_keys->rtp_auth, auth_start, |
3036 | 81 | srtp_len - tag_len - stream->mki_size); |
3037 | 81 | if (status) { |
3038 | 0 | return status; |
3039 | 0 | } |
3040 | | |
3041 | | /* run auth func over ROC, then write tmp tag */ |
3042 | 81 | status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4, |
3043 | 81 | tmp_tag); |
3044 | | |
3045 | 81 | debug_print(mod_srtp, "computed auth tag: %s", |
3046 | 81 | srtp_octet_string_hex_string(tmp_tag, tag_len)); |
3047 | 81 | debug_print(mod_srtp, "packet auth tag: %s", |
3048 | 81 | srtp_octet_string_hex_string(auth_tag, tag_len)); |
3049 | 81 | if (status) { |
3050 | 0 | return srtp_err_status_auth_fail; |
3051 | 0 | } |
3052 | | |
3053 | 81 | if (!srtp_octet_string_equal(tmp_tag, auth_tag, tag_len)) { |
3054 | 79 | return srtp_err_status_auth_fail; |
3055 | 79 | } |
3056 | 81 | } |
3057 | | |
3058 | | /* |
3059 | | * update the key usage limit, and check it to make sure that we |
3060 | | * didn't just hit either the soft limit or the hard limit, and call |
3061 | | * the event handler if we hit either. |
3062 | | */ |
3063 | 90.8k | switch (srtp_key_limit_update(session_keys->limit)) { |
3064 | 90.8k | case srtp_key_event_normal: |
3065 | 90.8k | break; |
3066 | 0 | case srtp_key_event_soft_limit: |
3067 | 0 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
3068 | 0 | break; |
3069 | 0 | case srtp_key_event_hard_limit: |
3070 | 0 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
3071 | 0 | return srtp_err_status_key_expired; |
3072 | 0 | default: |
3073 | 0 | break; |
3074 | 90.8k | } |
3075 | | |
3076 | 90.8k | if (hdr->x == 1 && session_keys->rtp_xtn_hdr_cipher) { |
3077 | | /* extensions header encryption RFC 6904 */ |
3078 | 391 | status = srtp_process_header_encryption( |
3079 | 391 | stream, srtp_get_rtp_xtn_hdr(hdr, rtp), session_keys); |
3080 | 391 | if (status) { |
3081 | 28 | return status; |
3082 | 28 | } |
3083 | 391 | } |
3084 | | |
3085 | 90.8k | if (cryptex_inuse) { |
3086 | 0 | status = srtp_cryptex_unprotect(cryptex_inplace, hdr, rtp, |
3087 | 0 | session_keys->rtp_cipher); |
3088 | 0 | if (status) { |
3089 | 0 | return status; |
3090 | 0 | } |
3091 | 0 | } |
3092 | | |
3093 | | /* if we're decrypting, add keystream into ciphertext */ |
3094 | 90.8k | if (stream->rtp_services & sec_serv_conf) { |
3095 | 50.8k | status = |
3096 | 50.8k | srtp_cipher_decrypt(session_keys->rtp_cipher, srtp + enc_start, |
3097 | 50.8k | enc_octet_len, rtp + enc_start, &enc_octet_len); |
3098 | 50.8k | if (status) { |
3099 | 0 | return srtp_err_status_cipher_fail; |
3100 | 0 | } |
3101 | 50.8k | } else if (rtp != srtp) { |
3102 | | /* if no encryption and not-inplace then need to copy rest of packet */ |
3103 | 0 | memcpy(rtp + enc_start, srtp + enc_start, enc_octet_len); |
3104 | 0 | } |
3105 | | |
3106 | 90.8k | if (cryptex_inuse) { |
3107 | 0 | srtp_cryptex_unprotect_cleanup(cryptex_inplace, hdr, rtp); |
3108 | 0 | } |
3109 | | |
3110 | | /* |
3111 | | * verify that stream is for received traffic - this check will |
3112 | | * detect SSRC collisions, since a stream that appears in both |
3113 | | * srtp_protect() and srtp_unprotect() will fail this test in one of |
3114 | | * those functions. |
3115 | | * |
3116 | | * we do this check *after* the authentication check, so that the |
3117 | | * latter check will catch any attempts to fool us into thinking |
3118 | | * that we've got a collision |
3119 | | */ |
3120 | 90.8k | if (stream->direction != dir_srtp_receiver) { |
3121 | 71.6k | if (stream->direction == dir_unknown) { |
3122 | 40 | stream->direction = dir_srtp_receiver; |
3123 | 71.6k | } else { |
3124 | 71.6k | srtp_handle_event(ctx, stream, event_ssrc_collision); |
3125 | 71.6k | } |
3126 | 71.6k | } |
3127 | | |
3128 | | /* |
3129 | | * if the stream is a 'provisional' one, in which the template context |
3130 | | * is used, then we need to allocate a new stream at this point, since |
3131 | | * the authentication passed |
3132 | | */ |
3133 | 90.8k | if (stream == ctx->stream_template) { |
3134 | 78.6k | srtp_stream_ctx_t *new_stream; |
3135 | | |
3136 | | /* |
3137 | | * allocate and initialize a new stream |
3138 | | * |
3139 | | * note that we indicate failure if we can't allocate the new |
3140 | | * stream, and some implementations will want to not return |
3141 | | * failure here |
3142 | | */ |
3143 | 78.6k | status = |
3144 | 78.6k | srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
3145 | 78.6k | if (status) { |
3146 | 0 | return status; |
3147 | 0 | } |
3148 | | |
3149 | | /* add new stream to the list */ |
3150 | 78.6k | status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream, |
3151 | 78.6k | ctx->stream_template); |
3152 | 78.6k | if (status) { |
3153 | 0 | return status; |
3154 | 0 | } |
3155 | | |
3156 | | /* set stream (the pointer used in this function) */ |
3157 | 78.6k | stream = new_stream; |
3158 | 78.6k | } |
3159 | | |
3160 | | /* |
3161 | | * the message authentication function passed, so add the packet |
3162 | | * index into the replay database |
3163 | | */ |
3164 | 90.8k | if (advance_packet_index) { |
3165 | 167 | srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, roc_to_set, seq_to_set); |
3166 | 167 | stream->pending_roc = 0; |
3167 | 167 | srtp_rdbx_add_index(&stream->rtp_rdbx, 0); |
3168 | 90.7k | } else { |
3169 | 90.7k | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
3170 | 90.7k | } |
3171 | | |
3172 | 90.8k | *rtp_len = enc_start + enc_octet_len; |
3173 | | |
3174 | 90.8k | return srtp_err_status_ok; |
3175 | 90.8k | } |
3176 | | |
3177 | | srtp_err_status_t srtp_init(void) |
3178 | 2 | { |
3179 | 2 | srtp_err_status_t status; |
3180 | | |
3181 | | /* initialize crypto kernel */ |
3182 | 2 | status = srtp_crypto_kernel_init(); |
3183 | 2 | if (status) { |
3184 | 0 | return status; |
3185 | 0 | } |
3186 | | |
3187 | | /* load srtp debug module into the kernel */ |
3188 | 2 | status = srtp_crypto_kernel_load_debug_module(&mod_srtp); |
3189 | 2 | if (status) { |
3190 | 0 | return status; |
3191 | 0 | } |
3192 | | |
3193 | 2 | return srtp_err_status_ok; |
3194 | 2 | } |
3195 | | |
3196 | | srtp_err_status_t srtp_shutdown(void) |
3197 | 0 | { |
3198 | 0 | srtp_err_status_t status; |
3199 | | |
3200 | | /* shut down crypto kernel */ |
3201 | 0 | status = srtp_crypto_kernel_shutdown(); |
3202 | 0 | if (status) { |
3203 | 0 | return status; |
3204 | 0 | } |
3205 | | |
3206 | | /* shutting down crypto kernel frees the srtp debug module as well */ |
3207 | | |
3208 | 0 | return srtp_err_status_ok; |
3209 | 0 | } |
3210 | | |
3211 | | srtp_stream_ctx_t *srtp_get_stream(srtp_t srtp, uint32_t ssrc) |
3212 | 214k | { |
3213 | 214k | return srtp_stream_list_get(srtp->stream_list, ssrc); |
3214 | 214k | } |
3215 | | |
3216 | | srtp_err_status_t srtp_dealloc(srtp_t session) |
3217 | 3.10k | { |
3218 | 3.10k | srtp_err_status_t status; |
3219 | | |
3220 | | /* |
3221 | | * we take a conservative deallocation strategy - if we encounter an |
3222 | | * error deallocating a stream, then we stop trying to deallocate |
3223 | | * memory and just return an error |
3224 | | */ |
3225 | | |
3226 | | /* deallocate streams */ |
3227 | 3.10k | status = srtp_remove_and_dealloc_streams(session->stream_list, |
3228 | 3.10k | session->stream_template); |
3229 | 3.10k | if (status) { |
3230 | 0 | return status; |
3231 | 0 | } |
3232 | | |
3233 | | /* deallocate stream template, if there is one */ |
3234 | 3.10k | if (session->stream_template != NULL) { |
3235 | 2.59k | status = srtp_stream_dealloc(session->stream_template, NULL); |
3236 | 2.59k | if (status) { |
3237 | 0 | return status; |
3238 | 0 | } |
3239 | 2.59k | } |
3240 | | |
3241 | | /* deallocate stream list */ |
3242 | 3.10k | status = srtp_stream_list_dealloc(session->stream_list); |
3243 | 3.10k | if (status) { |
3244 | 0 | return status; |
3245 | 0 | } |
3246 | | |
3247 | | /* deallocate session context */ |
3248 | 3.10k | srtp_crypto_free(session); |
3249 | | |
3250 | 3.10k | return srtp_err_status_ok; |
3251 | 3.10k | } |
3252 | | |
3253 | | srtp_err_status_t srtp_stream_add(srtp_t session, const srtp_policy_t *policy) |
3254 | 9.70k | { |
3255 | 9.70k | srtp_err_status_t status; |
3256 | 9.70k | srtp_stream_t tmp; |
3257 | | |
3258 | | /* sanity check arguments */ |
3259 | 9.70k | if (session == NULL) { |
3260 | 0 | return srtp_err_status_bad_param; |
3261 | 0 | } |
3262 | | |
3263 | 9.70k | status = srtp_valid_policy(policy); |
3264 | 9.70k | if (status != srtp_err_status_ok) { |
3265 | 20 | return status; |
3266 | 20 | } |
3267 | | |
3268 | | /* allocate stream */ |
3269 | 9.68k | status = srtp_stream_alloc(&tmp, policy); |
3270 | 9.68k | if (status) { |
3271 | 0 | return status; |
3272 | 0 | } |
3273 | | |
3274 | | /* initialize stream */ |
3275 | 9.68k | status = srtp_stream_init(tmp, policy); |
3276 | 9.68k | if (status) { |
3277 | 203 | srtp_stream_dealloc(tmp, NULL); |
3278 | 203 | return status; |
3279 | 203 | } |
3280 | | |
3281 | | /* |
3282 | | * set the head of the stream list or the template to point to the |
3283 | | * stream that we've just alloced and init'ed, depending on whether |
3284 | | * or not it has a wildcard SSRC value or not |
3285 | | * |
3286 | | * if the template stream has already been set, then the policy is |
3287 | | * inconsistent, so we return a bad_param error code |
3288 | | */ |
3289 | 9.47k | switch (policy->ssrc.type) { |
3290 | 1.16k | case (ssrc_any_outbound): |
3291 | 1.16k | if (session->stream_template) { |
3292 | 5 | srtp_stream_dealloc(tmp, NULL); |
3293 | 5 | return srtp_err_status_bad_param; |
3294 | 5 | } |
3295 | 1.16k | session->stream_template = tmp; |
3296 | 1.16k | session->stream_template->direction = dir_srtp_sender; |
3297 | 1.16k | break; |
3298 | 1.43k | case (ssrc_any_inbound): |
3299 | 1.43k | if (session->stream_template) { |
3300 | 5 | srtp_stream_dealloc(tmp, NULL); |
3301 | 5 | return srtp_err_status_bad_param; |
3302 | 5 | } |
3303 | 1.43k | session->stream_template = tmp; |
3304 | 1.43k | session->stream_template->direction = dir_srtp_receiver; |
3305 | 1.43k | break; |
3306 | 6.84k | case (ssrc_specific): |
3307 | 6.84k | status = srtp_insert_or_dealloc_stream(session->stream_list, tmp, |
3308 | 6.84k | session->stream_template); |
3309 | 6.84k | if (status) { |
3310 | 0 | return status; |
3311 | 0 | } |
3312 | 6.84k | break; |
3313 | 6.84k | case (ssrc_undefined): |
3314 | 26 | default: |
3315 | 26 | srtp_stream_dealloc(tmp, NULL); |
3316 | 26 | return srtp_err_status_bad_param; |
3317 | 9.47k | } |
3318 | | |
3319 | 9.44k | return srtp_err_status_ok; |
3320 | 9.47k | } |
3321 | | |
3322 | | srtp_err_status_t srtp_create(srtp_t *session, /* handle for session */ |
3323 | | const srtp_policy_t *policy) |
3324 | 3.17k | { /* SRTP policy (list) */ |
3325 | 3.17k | srtp_err_status_t stat; |
3326 | 3.17k | srtp_ctx_t *ctx; |
3327 | | |
3328 | | /* sanity check arguments */ |
3329 | 3.17k | if (session == NULL) { |
3330 | 0 | return srtp_err_status_bad_param; |
3331 | 0 | } |
3332 | | |
3333 | 3.17k | if (policy) { |
3334 | 3.17k | stat = srtp_valid_policy(policy); |
3335 | 3.17k | if (stat != srtp_err_status_ok) { |
3336 | 65 | return stat; |
3337 | 65 | } |
3338 | 3.17k | } |
3339 | | |
3340 | | /* allocate srtp context and set ctx_ptr */ |
3341 | 3.10k | ctx = (srtp_ctx_t *)srtp_crypto_alloc(sizeof(srtp_ctx_t)); |
3342 | 3.10k | if (ctx == NULL) { |
3343 | 0 | return srtp_err_status_alloc_fail; |
3344 | 0 | } |
3345 | 3.10k | *session = ctx; |
3346 | | |
3347 | 3.10k | ctx->stream_template = NULL; |
3348 | 3.10k | ctx->stream_list = NULL; |
3349 | 3.10k | ctx->user_data = NULL; |
3350 | | |
3351 | | /* allocate stream list */ |
3352 | 3.10k | stat = srtp_stream_list_alloc(&ctx->stream_list); |
3353 | 3.10k | if (stat) { |
3354 | | /* clean up everything */ |
3355 | 0 | srtp_dealloc(*session); |
3356 | 0 | *session = NULL; |
3357 | 0 | return stat; |
3358 | 0 | } |
3359 | | |
3360 | | /* |
3361 | | * loop over elements in the policy list, allocating and |
3362 | | * initializing a stream for each element |
3363 | | */ |
3364 | 12.5k | while (policy != NULL) { |
3365 | 9.70k | stat = srtp_stream_add(ctx, policy); |
3366 | 9.70k | if (stat) { |
3367 | | /* clean up everything */ |
3368 | 259 | srtp_dealloc(*session); |
3369 | 259 | *session = NULL; |
3370 | 259 | return stat; |
3371 | 259 | } |
3372 | | |
3373 | | /* set policy to next item in list */ |
3374 | 9.44k | policy = policy->next; |
3375 | 9.44k | } |
3376 | | |
3377 | 2.84k | return srtp_err_status_ok; |
3378 | 3.10k | } |
3379 | | |
3380 | | srtp_err_status_t srtp_stream_remove(srtp_t session, uint32_t ssrc) |
3381 | 1.24k | { |
3382 | 1.24k | srtp_stream_ctx_t *stream; |
3383 | 1.24k | srtp_err_status_t status; |
3384 | | |
3385 | | /* sanity check arguments */ |
3386 | 1.24k | if (session == NULL) { |
3387 | 0 | return srtp_err_status_bad_param; |
3388 | 0 | } |
3389 | | |
3390 | | /* find and remove stream from the list */ |
3391 | 1.24k | stream = srtp_stream_list_get(session->stream_list, htonl(ssrc)); |
3392 | 1.24k | if (stream == NULL) { |
3393 | 85 | return srtp_err_status_no_ctx; |
3394 | 85 | } |
3395 | | |
3396 | 1.16k | srtp_stream_list_remove(session->stream_list, stream); |
3397 | | |
3398 | | /* deallocate the stream */ |
3399 | 1.16k | status = srtp_stream_dealloc(stream, session->stream_template); |
3400 | 1.16k | if (status) { |
3401 | 0 | return status; |
3402 | 0 | } |
3403 | | |
3404 | 1.16k | return srtp_err_status_ok; |
3405 | 1.16k | } |
3406 | | |
3407 | | srtp_err_status_t srtp_update(srtp_t session, const srtp_policy_t *policy) |
3408 | 0 | { |
3409 | 0 | srtp_err_status_t stat; |
3410 | | |
3411 | | /* sanity check arguments */ |
3412 | 0 | if (session == NULL) { |
3413 | 0 | return srtp_err_status_bad_param; |
3414 | 0 | } |
3415 | | |
3416 | 0 | stat = srtp_valid_policy(policy); |
3417 | 0 | if (stat != srtp_err_status_ok) { |
3418 | 0 | return stat; |
3419 | 0 | } |
3420 | | |
3421 | 0 | while (policy != NULL) { |
3422 | 0 | stat = srtp_stream_update(session, policy); |
3423 | 0 | if (stat) { |
3424 | 0 | return stat; |
3425 | 0 | } |
3426 | | |
3427 | | /* set policy to next item in list */ |
3428 | 0 | policy = policy->next; |
3429 | 0 | } |
3430 | 0 | return srtp_err_status_ok; |
3431 | 0 | } |
3432 | | |
3433 | | struct update_template_stream_data { |
3434 | | srtp_err_status_t status; |
3435 | | srtp_t session; |
3436 | | srtp_stream_t new_stream_template; |
3437 | | srtp_stream_list_t new_stream_list; |
3438 | | }; |
3439 | | |
3440 | | static bool update_template_stream_cb(srtp_stream_t stream, void *raw_data) |
3441 | 0 | { |
3442 | 0 | struct update_template_stream_data *data = |
3443 | 0 | (struct update_template_stream_data *)raw_data; |
3444 | 0 | srtp_t session = data->session; |
3445 | 0 | uint32_t ssrc = stream->ssrc; |
3446 | 0 | srtp_xtd_seq_num_t old_index; |
3447 | 0 | srtp_rdb_t old_rtcp_rdb; |
3448 | | |
3449 | | /* old / non-template streams are copied unchanged */ |
3450 | 0 | if (stream->session_keys[0].rtp_auth != |
3451 | 0 | session->stream_template->session_keys[0].rtp_auth) { |
3452 | 0 | srtp_stream_list_remove(session->stream_list, stream); |
3453 | 0 | data->status = srtp_insert_or_dealloc_stream( |
3454 | 0 | data->new_stream_list, stream, session->stream_template); |
3455 | 0 | if (data->status) { |
3456 | 0 | return false; |
3457 | 0 | } |
3458 | 0 | return true; |
3459 | 0 | } |
3460 | | |
3461 | | /* save old extended seq */ |
3462 | 0 | old_index = stream->rtp_rdbx.index; |
3463 | 0 | old_rtcp_rdb = stream->rtcp_rdb; |
3464 | | |
3465 | | /* remove stream */ |
3466 | 0 | data->status = srtp_stream_remove(session, ntohl(ssrc)); |
3467 | 0 | if (data->status) { |
3468 | 0 | return false; |
3469 | 0 | } |
3470 | | |
3471 | | /* allocate and initialize a new stream */ |
3472 | 0 | data->status = srtp_stream_clone(data->new_stream_template, ssrc, &stream); |
3473 | 0 | if (data->status) { |
3474 | 0 | return false; |
3475 | 0 | } |
3476 | | |
3477 | | /* add new stream to the head of the new_stream_list */ |
3478 | 0 | data->status = srtp_insert_or_dealloc_stream(data->new_stream_list, stream, |
3479 | 0 | data->new_stream_template); |
3480 | 0 | if (data->status) { |
3481 | 0 | return false; |
3482 | 0 | } |
3483 | | |
3484 | | /* restore old extended seq */ |
3485 | 0 | stream->rtp_rdbx.index = old_index; |
3486 | 0 | stream->rtcp_rdb = old_rtcp_rdb; |
3487 | |
|
3488 | 0 | return true; |
3489 | 0 | } |
3490 | | |
3491 | | static srtp_err_status_t is_update_policy_compatable( |
3492 | | srtp_stream_t stream, |
3493 | | const srtp_policy_t *policy) |
3494 | 0 | { |
3495 | 0 | if (stream->use_mki != policy->use_mki) { |
3496 | 0 | return srtp_err_status_bad_param; |
3497 | 0 | } |
3498 | | |
3499 | 0 | if (stream->use_mki && stream->mki_size != policy->mki_size) { |
3500 | 0 | return srtp_err_status_bad_param; |
3501 | 0 | } |
3502 | | |
3503 | 0 | return srtp_err_status_ok; |
3504 | 0 | } |
3505 | | |
3506 | | static srtp_err_status_t update_template_streams(srtp_t session, |
3507 | | const srtp_policy_t *policy) |
3508 | 0 | { |
3509 | 0 | srtp_err_status_t status; |
3510 | 0 | srtp_stream_t new_stream_template; |
3511 | 0 | srtp_stream_list_t new_stream_list; |
3512 | |
|
3513 | 0 | status = srtp_valid_policy(policy); |
3514 | 0 | if (status != srtp_err_status_ok) { |
3515 | 0 | return status; |
3516 | 0 | } |
3517 | | |
3518 | 0 | if (session->stream_template == NULL) { |
3519 | 0 | return srtp_err_status_bad_param; |
3520 | 0 | } |
3521 | | |
3522 | 0 | status = is_update_policy_compatable(session->stream_template, policy); |
3523 | 0 | if (status != srtp_err_status_ok) { |
3524 | 0 | return status; |
3525 | 0 | } |
3526 | | |
3527 | | /* allocate new template stream */ |
3528 | 0 | status = srtp_stream_alloc(&new_stream_template, policy); |
3529 | 0 | if (status) { |
3530 | 0 | return status; |
3531 | 0 | } |
3532 | | |
3533 | | /* initialize new template stream */ |
3534 | 0 | status = srtp_stream_init(new_stream_template, policy); |
3535 | 0 | if (status) { |
3536 | 0 | srtp_crypto_free(new_stream_template); |
3537 | 0 | return status; |
3538 | 0 | } |
3539 | | |
3540 | | /* allocate new stream list */ |
3541 | 0 | status = srtp_stream_list_alloc(&new_stream_list); |
3542 | 0 | if (status) { |
3543 | 0 | srtp_crypto_free(new_stream_template); |
3544 | 0 | return status; |
3545 | 0 | } |
3546 | | |
3547 | | /* process streams */ |
3548 | 0 | struct update_template_stream_data data = { srtp_err_status_ok, session, |
3549 | 0 | new_stream_template, |
3550 | 0 | new_stream_list }; |
3551 | 0 | srtp_stream_list_for_each(session->stream_list, update_template_stream_cb, |
3552 | 0 | &data); |
3553 | 0 | if (data.status) { |
3554 | | /* free new allocations */ |
3555 | 0 | srtp_remove_and_dealloc_streams(new_stream_list, new_stream_template); |
3556 | 0 | srtp_stream_list_dealloc(new_stream_list); |
3557 | 0 | srtp_stream_dealloc(new_stream_template, NULL); |
3558 | 0 | return data.status; |
3559 | 0 | } |
3560 | | |
3561 | | /* dealloc old list / template */ |
3562 | 0 | srtp_remove_and_dealloc_streams(session->stream_list, |
3563 | 0 | session->stream_template); |
3564 | 0 | srtp_stream_list_dealloc(session->stream_list); |
3565 | 0 | srtp_stream_dealloc(session->stream_template, NULL); |
3566 | | |
3567 | | /* set new list / template */ |
3568 | 0 | session->stream_template = new_stream_template; |
3569 | 0 | session->stream_list = new_stream_list; |
3570 | 0 | return srtp_err_status_ok; |
3571 | 0 | } |
3572 | | |
3573 | | static srtp_err_status_t stream_update(srtp_t session, |
3574 | | const srtp_policy_t *policy) |
3575 | 0 | { |
3576 | 0 | srtp_err_status_t status; |
3577 | 0 | srtp_xtd_seq_num_t old_index; |
3578 | 0 | srtp_rdb_t old_rtcp_rdb; |
3579 | 0 | srtp_stream_t stream; |
3580 | |
|
3581 | 0 | status = srtp_valid_policy(policy); |
3582 | 0 | if (status != srtp_err_status_ok) { |
3583 | 0 | return status; |
3584 | 0 | } |
3585 | | |
3586 | 0 | stream = srtp_get_stream(session, htonl(policy->ssrc.value)); |
3587 | 0 | if (stream == NULL) { |
3588 | 0 | return srtp_err_status_bad_param; |
3589 | 0 | } |
3590 | | |
3591 | 0 | status = is_update_policy_compatable(stream, policy); |
3592 | 0 | if (status != srtp_err_status_ok) { |
3593 | 0 | return status; |
3594 | 0 | } |
3595 | | |
3596 | | /* save old extendard seq */ |
3597 | 0 | old_index = stream->rtp_rdbx.index; |
3598 | 0 | old_rtcp_rdb = stream->rtcp_rdb; |
3599 | |
|
3600 | 0 | status = srtp_stream_remove(session, policy->ssrc.value); |
3601 | 0 | if (status) { |
3602 | 0 | return status; |
3603 | 0 | } |
3604 | | |
3605 | 0 | status = srtp_stream_add(session, policy); |
3606 | 0 | if (status) { |
3607 | 0 | return status; |
3608 | 0 | } |
3609 | | |
3610 | 0 | stream = srtp_get_stream(session, htonl(policy->ssrc.value)); |
3611 | 0 | if (stream == NULL) { |
3612 | 0 | return srtp_err_status_fail; |
3613 | 0 | } |
3614 | | |
3615 | | /* restore old extended seq */ |
3616 | 0 | stream->rtp_rdbx.index = old_index; |
3617 | 0 | stream->rtcp_rdb = old_rtcp_rdb; |
3618 | |
|
3619 | 0 | return srtp_err_status_ok; |
3620 | 0 | } |
3621 | | |
3622 | | srtp_err_status_t srtp_stream_update(srtp_t session, |
3623 | | const srtp_policy_t *policy) |
3624 | 0 | { |
3625 | 0 | srtp_err_status_t status; |
3626 | | |
3627 | | /* sanity check arguments */ |
3628 | 0 | if (session == NULL) { |
3629 | 0 | return srtp_err_status_bad_param; |
3630 | 0 | } |
3631 | | |
3632 | 0 | status = srtp_valid_policy(policy); |
3633 | 0 | if (status != srtp_err_status_ok) { |
3634 | 0 | return status; |
3635 | 0 | } |
3636 | | |
3637 | 0 | switch (policy->ssrc.type) { |
3638 | 0 | case (ssrc_any_outbound): |
3639 | 0 | case (ssrc_any_inbound): |
3640 | 0 | status = update_template_streams(session, policy); |
3641 | 0 | break; |
3642 | 0 | case (ssrc_specific): |
3643 | 0 | status = stream_update(session, policy); |
3644 | 0 | break; |
3645 | 0 | case (ssrc_undefined): |
3646 | 0 | default: |
3647 | 0 | return srtp_err_status_bad_param; |
3648 | 0 | } |
3649 | | |
3650 | 0 | return status; |
3651 | 0 | } |
3652 | | |
3653 | | /* |
3654 | | * The default policy - provides a convenient way for callers to use |
3655 | | * the default security policy |
3656 | | * |
3657 | | * The default policy is defined in RFC 3711 |
3658 | | * (Section 5. Default and mandatory-to-implement Transforms) |
3659 | | * |
3660 | | */ |
3661 | | |
3662 | | /* |
3663 | | * NOTE: cipher_key_len is really key len (128 bits) plus salt len |
3664 | | * (112 bits) |
3665 | | */ |
3666 | | /* There are hard-coded 16's for base_key_len in the key generation code */ |
3667 | | |
3668 | | void srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p) |
3669 | 19.8k | { |
3670 | 19.8k | p->cipher_type = SRTP_AES_ICM_128; |
3671 | 19.8k | p->cipher_key_len = |
3672 | 19.8k | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */ |
3673 | 19.8k | p->auth_type = SRTP_HMAC_SHA1; |
3674 | 19.8k | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
3675 | 19.8k | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
3676 | 19.8k | p->sec_serv = sec_serv_conf_and_auth; |
3677 | 19.8k | } |
3678 | | |
3679 | | void srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p) |
3680 | 30.1k | { |
3681 | 30.1k | p->cipher_type = SRTP_AES_ICM_128; |
3682 | 30.1k | p->cipher_key_len = |
3683 | 30.1k | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */ |
3684 | 30.1k | p->auth_type = SRTP_HMAC_SHA1; |
3685 | 30.1k | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
3686 | 30.1k | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
3687 | 30.1k | p->sec_serv = sec_serv_conf_and_auth; |
3688 | 30.1k | } |
3689 | | |
3690 | | void srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p) |
3691 | 726 | { |
3692 | | /* |
3693 | | * corresponds to RFC 4568 |
3694 | | * |
3695 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
3696 | | */ |
3697 | | |
3698 | 726 | p->cipher_type = SRTP_AES_ICM_128; |
3699 | 726 | p->cipher_key_len = |
3700 | 726 | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ |
3701 | 726 | p->auth_type = SRTP_HMAC_SHA1; |
3702 | 726 | p->auth_key_len = 20; /* 160 bit key */ |
3703 | 726 | p->auth_tag_len = 4; /* 32 bit tag */ |
3704 | 726 | p->sec_serv = sec_serv_conf_and_auth; |
3705 | 726 | } |
3706 | | |
3707 | | void srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p) |
3708 | 1.44k | { |
3709 | | /* |
3710 | | * corresponds to RFC 4568 |
3711 | | * |
3712 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
3713 | | */ |
3714 | | |
3715 | 1.44k | p->cipher_type = SRTP_AES_ICM_128; |
3716 | 1.44k | p->cipher_key_len = |
3717 | 1.44k | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ |
3718 | 1.44k | p->auth_type = SRTP_NULL_AUTH; |
3719 | 1.44k | p->auth_key_len = 0; |
3720 | 1.44k | p->auth_tag_len = 0; |
3721 | 1.44k | p->sec_serv = sec_serv_conf; |
3722 | 1.44k | } |
3723 | | |
3724 | | void srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p) |
3725 | 602 | { |
3726 | | /* |
3727 | | * corresponds to RFC 4568 |
3728 | | */ |
3729 | | |
3730 | 602 | p->cipher_type = SRTP_NULL_CIPHER; |
3731 | 602 | p->cipher_key_len = |
3732 | 602 | SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */ |
3733 | 602 | p->auth_type = SRTP_HMAC_SHA1; |
3734 | 602 | p->auth_key_len = 20; |
3735 | 602 | p->auth_tag_len = 10; |
3736 | 602 | p->sec_serv = sec_serv_auth; |
3737 | 602 | } |
3738 | | |
3739 | | void srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p) |
3740 | 21.7k | { |
3741 | | /* |
3742 | | * Should only be used for testing |
3743 | | */ |
3744 | | |
3745 | 21.7k | p->cipher_type = SRTP_NULL_CIPHER; |
3746 | 21.7k | p->cipher_key_len = 0; |
3747 | 21.7k | p->auth_type = SRTP_NULL_AUTH; |
3748 | 21.7k | p->auth_key_len = 0; |
3749 | 21.7k | p->auth_tag_len = 0; |
3750 | 21.7k | p->sec_serv = sec_serv_none; |
3751 | 21.7k | } |
3752 | | |
3753 | | void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p) |
3754 | 996 | { |
3755 | | /* |
3756 | | * corresponds to RFC 6188 |
3757 | | */ |
3758 | | |
3759 | 996 | p->cipher_type = SRTP_AES_ICM_256; |
3760 | 996 | p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; |
3761 | 996 | p->auth_type = SRTP_HMAC_SHA1; |
3762 | 996 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
3763 | 996 | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
3764 | 996 | p->sec_serv = sec_serv_conf_and_auth; |
3765 | 996 | } |
3766 | | |
3767 | | void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p) |
3768 | 568 | { |
3769 | | /* |
3770 | | * corresponds to RFC 6188 |
3771 | | * |
3772 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
3773 | | */ |
3774 | | |
3775 | 568 | p->cipher_type = SRTP_AES_ICM_256; |
3776 | 568 | p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; |
3777 | 568 | p->auth_type = SRTP_HMAC_SHA1; |
3778 | 568 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
3779 | 568 | p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */ |
3780 | 568 | p->sec_serv = sec_serv_conf_and_auth; |
3781 | 568 | } |
3782 | | |
3783 | | /* |
3784 | | * AES-256 with no authentication. |
3785 | | */ |
3786 | | void srtp_crypto_policy_set_aes_cm_256_null_auth(srtp_crypto_policy_t *p) |
3787 | 740 | { |
3788 | 740 | p->cipher_type = SRTP_AES_ICM_256; |
3789 | 740 | p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT; |
3790 | 740 | p->auth_type = SRTP_NULL_AUTH; |
3791 | 740 | p->auth_key_len = 0; |
3792 | 740 | p->auth_tag_len = 0; |
3793 | 740 | p->sec_serv = sec_serv_conf; |
3794 | 740 | } |
3795 | | |
3796 | | void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(srtp_crypto_policy_t *p) |
3797 | 0 | { |
3798 | | /* |
3799 | | * corresponds to RFC 6188 |
3800 | | */ |
3801 | |
|
3802 | 0 | p->cipher_type = SRTP_AES_ICM_192; |
3803 | 0 | p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT; |
3804 | 0 | p->auth_type = SRTP_HMAC_SHA1; |
3805 | 0 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
3806 | 0 | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
3807 | 0 | p->sec_serv = sec_serv_conf_and_auth; |
3808 | 0 | } |
3809 | | |
3810 | | void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p) |
3811 | 0 | { |
3812 | | /* |
3813 | | * corresponds to RFC 6188 |
3814 | | * |
3815 | | * note that this crypto policy is intended for SRTP, but not SRTCP |
3816 | | */ |
3817 | |
|
3818 | 0 | p->cipher_type = SRTP_AES_ICM_192; |
3819 | 0 | p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT; |
3820 | 0 | p->auth_type = SRTP_HMAC_SHA1; |
3821 | 0 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
3822 | 0 | p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */ |
3823 | 0 | p->sec_serv = sec_serv_conf_and_auth; |
3824 | 0 | } |
3825 | | |
3826 | | /* |
3827 | | * AES-192 with no authentication. |
3828 | | */ |
3829 | | void srtp_crypto_policy_set_aes_cm_192_null_auth(srtp_crypto_policy_t *p) |
3830 | 0 | { |
3831 | 0 | p->cipher_type = SRTP_AES_ICM_192; |
3832 | 0 | p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT; |
3833 | 0 | p->auth_type = SRTP_NULL_AUTH; |
3834 | 0 | p->auth_key_len = 0; |
3835 | 0 | p->auth_tag_len = 0; |
3836 | 0 | p->sec_serv = sec_serv_conf; |
3837 | 0 | } |
3838 | | |
3839 | | /* |
3840 | | * AES-128 GCM mode with 16 octet auth tag. |
3841 | | */ |
3842 | | void srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p) |
3843 | 0 | { |
3844 | 0 | p->cipher_type = SRTP_AES_GCM_128; |
3845 | 0 | p->cipher_key_len = SRTP_AES_GCM_128_KEY_LEN_WSALT; |
3846 | 0 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
3847 | 0 | p->auth_key_len = 0; |
3848 | 0 | p->auth_tag_len = 16; /* 16 octet tag length */ |
3849 | 0 | p->sec_serv = sec_serv_conf_and_auth; |
3850 | 0 | } |
3851 | | |
3852 | | /* |
3853 | | * AES-256 GCM mode with 16 octet auth tag. |
3854 | | */ |
3855 | | void srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p) |
3856 | 0 | { |
3857 | 0 | p->cipher_type = SRTP_AES_GCM_256; |
3858 | 0 | p->cipher_key_len = SRTP_AES_GCM_256_KEY_LEN_WSALT; |
3859 | 0 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
3860 | 0 | p->auth_key_len = 0; |
3861 | 0 | p->auth_tag_len = 16; /* 16 octet tag length */ |
3862 | 0 | p->sec_serv = sec_serv_conf_and_auth; |
3863 | 0 | } |
3864 | | |
3865 | | /* |
3866 | | * secure rtcp functions |
3867 | | */ |
3868 | | |
3869 | | /* |
3870 | | * AEAD uses a new IV formation method. This function implements |
3871 | | * section 9.1 (SRTCP IV Formation for AES-GCM) from RFC7714. |
3872 | | * The calculation is defined as, where (+) is the xor operation: |
3873 | | * |
3874 | | * 0 1 2 3 4 5 6 7 8 9 10 11 |
3875 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ |
3876 | | * |00|00| SSRC |00|00|0+SRTCP Idx|---+ |
3877 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
3878 | | * | |
3879 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
3880 | | * | Encryption Salt |->(+) |
3881 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
3882 | | * | |
3883 | | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
3884 | | * | Initialization Vector |<--+ |
3885 | | * +--+--+--+--+--+--+--+--+--+--+--+--+* |
3886 | | * |
3887 | | * Input: *session_keys - pointer to SRTP stream context session keys, |
3888 | | * used to retrieve the SALT |
3889 | | * *iv - Pointer to recieve the calculated IV |
3890 | | * seq_num - The SEQ value to use for the IV calculation. |
3891 | | * *hdr - The RTP header, used to get the SSRC value |
3892 | | * |
3893 | | * Returns: srtp_err_status_ok if no error or srtp_err_status_bad_param |
3894 | | * if seq_num is invalid |
3895 | | * |
3896 | | */ |
3897 | | static srtp_err_status_t srtp_calc_aead_iv_srtcp( |
3898 | | srtp_session_keys_t *session_keys, |
3899 | | v128_t *iv, |
3900 | | uint32_t seq_num, |
3901 | | const srtcp_hdr_t *hdr) |
3902 | 0 | { |
3903 | 0 | v128_t in; |
3904 | 0 | v128_t salt; |
3905 | |
|
3906 | 0 | memset(&in, 0, sizeof(v128_t)); |
3907 | 0 | memset(&salt, 0, sizeof(v128_t)); |
3908 | |
|
3909 | 0 | in.v16[0] = 0; |
3910 | 0 | memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */ |
3911 | 0 | in.v16[3] = 0; |
3912 | | |
3913 | | /* |
3914 | | * The SRTCP index (seq_num) spans bits 0 through 30 inclusive. |
3915 | | * The most significant bit should be zero. |
3916 | | */ |
3917 | 0 | if (seq_num & 0x80000000UL) { |
3918 | 0 | return srtp_err_status_bad_param; |
3919 | 0 | } |
3920 | 0 | in.v32[2] = htonl(seq_num); |
3921 | |
|
3922 | 0 | debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in)); |
3923 | | |
3924 | | /* |
3925 | | * Get the SALT value from the context |
3926 | | */ |
3927 | 0 | memcpy(salt.v8, session_keys->c_salt, 12); |
3928 | 0 | debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt)); |
3929 | | |
3930 | | /* |
3931 | | * Finally, apply the SALT to the input |
3932 | | */ |
3933 | 0 | v128_xor(iv, &in, &salt); |
3934 | |
|
3935 | 0 | return srtp_err_status_ok; |
3936 | 0 | } |
3937 | | |
3938 | | /* |
3939 | | * This code handles AEAD ciphers for outgoing RTCP. We currently support |
3940 | | * AES-GCM mode with 128 or 256 bit keys. |
3941 | | */ |
3942 | | static srtp_err_status_t srtp_protect_rtcp_aead( |
3943 | | srtp_stream_ctx_t *stream, |
3944 | | const uint8_t *rtcp, |
3945 | | size_t rtcp_len, |
3946 | | uint8_t *srtcp, |
3947 | | size_t *srtcp_len, |
3948 | | srtp_session_keys_t *session_keys) |
3949 | 0 | { |
3950 | 0 | const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)rtcp; |
3951 | 0 | size_t enc_start; /* pointer to start of encrypted portion */ |
3952 | 0 | uint8_t *trailer_p; /* pointer to start of trailer */ |
3953 | 0 | uint32_t trailer; /* trailer value */ |
3954 | 0 | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
3955 | 0 | srtp_err_status_t status; |
3956 | 0 | size_t tag_len; |
3957 | 0 | uint32_t seq_num; |
3958 | 0 | v128_t iv; |
3959 | | |
3960 | | /* get tag length from stream context */ |
3961 | 0 | tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); |
3962 | | |
3963 | | /* |
3964 | | * set encryption start and encryption length - if we're not |
3965 | | * providing confidentiality, set enc_start to NULL |
3966 | | */ |
3967 | 0 | enc_start = octets_in_rtcp_header; |
3968 | 0 | enc_octet_len = rtcp_len - enc_start; |
3969 | | |
3970 | | /* check output length */ |
3971 | 0 | if (*srtcp_len < |
3972 | 0 | rtcp_len + sizeof(srtcp_trailer_t) + stream->mki_size + tag_len) { |
3973 | 0 | return srtp_err_status_buffer_small; |
3974 | 0 | } |
3975 | | |
3976 | | /* if not-inplace then need to copy full rtcp header */ |
3977 | 0 | if (rtcp != srtcp) { |
3978 | 0 | memcpy(srtcp, rtcp, enc_start); |
3979 | 0 | } |
3980 | | |
3981 | | /* NOTE: hdr->length is not usable - it refers to only the first |
3982 | | * RTCP report in the compound packet! |
3983 | | */ |
3984 | 0 | trailer_p = srtcp + enc_start + enc_octet_len + tag_len; |
3985 | |
|
3986 | 0 | if (stream->rtcp_services & sec_serv_conf) { |
3987 | 0 | trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */ |
3988 | 0 | } else { |
3989 | | /* 0 is network-order independent */ |
3990 | 0 | trailer = 0x00000000; /* set encrypt bit */ |
3991 | 0 | } |
3992 | |
|
3993 | 0 | if (stream->use_mki) { |
3994 | 0 | srtp_inject_mki(srtcp + rtcp_len + tag_len + sizeof(srtcp_trailer_t), |
3995 | 0 | session_keys, stream->mki_size); |
3996 | 0 | } |
3997 | | |
3998 | | /* |
3999 | | * check sequence number for overruns, and copy it into the packet |
4000 | | * if its value isn't too big |
4001 | | */ |
4002 | 0 | status = srtp_rdb_increment(&stream->rtcp_rdb); |
4003 | 0 | if (status) { |
4004 | 0 | return status; |
4005 | 0 | } |
4006 | 0 | seq_num = srtp_rdb_get_value(&stream->rtcp_rdb); |
4007 | 0 | trailer |= htonl(seq_num); |
4008 | 0 | debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num); |
4009 | |
|
4010 | 0 | memcpy(trailer_p, &trailer, sizeof(trailer)); |
4011 | | |
4012 | | /* |
4013 | | * Calculate and set the IV |
4014 | | */ |
4015 | 0 | status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr); |
4016 | 0 | if (status) { |
4017 | 0 | return srtp_err_status_cipher_fail; |
4018 | 0 | } |
4019 | 0 | status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv, |
4020 | 0 | srtp_direction_encrypt); |
4021 | 0 | if (status) { |
4022 | 0 | return srtp_err_status_cipher_fail; |
4023 | 0 | } |
4024 | | |
4025 | | /* |
4026 | | * Set the AAD for GCM mode |
4027 | | */ |
4028 | 0 | if (stream->rtcp_services & sec_serv_conf) { |
4029 | | /* |
4030 | | * If payload encryption is enabled, then the AAD consist of |
4031 | | * the RTCP header and the seq# at the end of the packet |
4032 | | */ |
4033 | 0 | status = srtp_cipher_set_aad(session_keys->rtcp_cipher, rtcp, |
4034 | 0 | octets_in_rtcp_header); |
4035 | 0 | if (status) { |
4036 | 0 | return srtp_err_status_cipher_fail; |
4037 | 0 | } |
4038 | 0 | } else { |
4039 | | /* |
4040 | | * Since payload encryption is not enabled, we must authenticate |
4041 | | * the entire packet as described in RFC 7714 (Section 9.3. Data |
4042 | | * Types in Unencrypted SRTCP Compound Packets) |
4043 | | */ |
4044 | 0 | status = srtp_cipher_set_aad(session_keys->rtcp_cipher, rtcp, rtcp_len); |
4045 | 0 | if (status) { |
4046 | 0 | return (srtp_err_status_cipher_fail); |
4047 | 0 | } |
4048 | 0 | } |
4049 | | /* |
4050 | | * Process the sequence# as AAD |
4051 | | */ |
4052 | 0 | status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)&trailer, |
4053 | 0 | sizeof(trailer)); |
4054 | 0 | if (status) { |
4055 | 0 | return (srtp_err_status_cipher_fail); |
4056 | 0 | } |
4057 | | |
4058 | | /* if we're encrypting, exor keystream into the message */ |
4059 | 0 | if (stream->rtcp_services & sec_serv_conf) { |
4060 | 0 | size_t out_len = *srtcp_len - enc_start; |
4061 | 0 | status = |
4062 | 0 | srtp_cipher_encrypt(session_keys->rtcp_cipher, rtcp + enc_start, |
4063 | 0 | enc_octet_len, srtcp + enc_start, &out_len); |
4064 | 0 | enc_octet_len = out_len; |
4065 | 0 | if (status) { |
4066 | 0 | return srtp_err_status_cipher_fail; |
4067 | 0 | } |
4068 | 0 | } else { |
4069 | | /* if no encryption and not-inplace then need to copy rest of packet */ |
4070 | 0 | if (rtcp != srtcp) { |
4071 | 0 | memcpy(srtcp + enc_start, rtcp + enc_start, enc_octet_len); |
4072 | 0 | } |
4073 | | |
4074 | | /* |
4075 | | * Even though we're not encrypting the payload, we need |
4076 | | * to run the cipher to get the auth tag. |
4077 | | */ |
4078 | 0 | uint8_t *auth_tag = srtcp + enc_start + enc_octet_len; |
4079 | 0 | size_t out_len = *srtcp_len - enc_start - enc_octet_len; |
4080 | 0 | status = srtp_cipher_encrypt(session_keys->rtcp_cipher, NULL, 0, |
4081 | 0 | auth_tag, &out_len); |
4082 | 0 | if (status) { |
4083 | 0 | return srtp_err_status_cipher_fail; |
4084 | 0 | } |
4085 | 0 | enc_octet_len += out_len; |
4086 | 0 | } |
4087 | | |
4088 | 0 | *srtcp_len = octets_in_rtcp_header + enc_octet_len; |
4089 | | |
4090 | | /* increase the packet length by the length of the seq_num*/ |
4091 | 0 | *srtcp_len += sizeof(srtcp_trailer_t); |
4092 | | |
4093 | | /* increase the packet by the mki_size */ |
4094 | 0 | *srtcp_len += stream->mki_size; |
4095 | |
|
4096 | 0 | return srtp_err_status_ok; |
4097 | 0 | } |
4098 | | |
4099 | | /* |
4100 | | * This function handles incoming SRTCP packets while in AEAD mode, |
4101 | | * which currently supports AES-GCM encryption. Note, the auth tag is |
4102 | | * at the end of the packet stream and is automatically checked by GCM |
4103 | | * when decrypting the payload. |
4104 | | */ |
4105 | | static srtp_err_status_t srtp_unprotect_rtcp_aead( |
4106 | | srtp_t ctx, |
4107 | | srtp_stream_ctx_t *stream, |
4108 | | const uint8_t *srtcp, |
4109 | | size_t srtcp_len, |
4110 | | uint8_t *rtcp, |
4111 | | size_t *rtcp_len, |
4112 | | srtp_session_keys_t *session_keys) |
4113 | 0 | { |
4114 | 0 | const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)srtcp; |
4115 | 0 | size_t enc_start; /* pointer to start of encrypted portion */ |
4116 | 0 | const uint8_t *trailer_p; /* pointer to start of trailer */ |
4117 | 0 | uint32_t trailer; /* trailer value */ |
4118 | 0 | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
4119 | 0 | const uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
4120 | 0 | srtp_err_status_t status; |
4121 | 0 | size_t tag_len; |
4122 | 0 | size_t tmp_len; |
4123 | 0 | uint32_t seq_num; |
4124 | 0 | v128_t iv; |
4125 | | |
4126 | | /* get tag length from stream context */ |
4127 | 0 | tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); |
4128 | |
|
4129 | 0 | enc_start = octets_in_rtcp_header; |
4130 | | |
4131 | | /* |
4132 | | * set encryption start, encryption length, and trailer |
4133 | | */ |
4134 | | /* index & E (encryption) bit follow normal data. hdr->len is the number of |
4135 | | * words (32-bit) in the normal packet minus 1 |
4136 | | */ |
4137 | | /* This should point trailer to the word past the end of the normal data. */ |
4138 | | /* This would need to be modified for optional mikey data */ |
4139 | 0 | trailer_p = srtcp + srtcp_len - sizeof(srtcp_trailer_t) - stream->mki_size; |
4140 | 0 | memcpy(&trailer, trailer_p, sizeof(trailer)); |
4141 | | |
4142 | | /* |
4143 | | * We pass the tag down to the cipher when doing GCM mode |
4144 | | */ |
4145 | 0 | enc_octet_len = srtcp_len - (octets_in_rtcp_header + |
4146 | 0 | sizeof(srtcp_trailer_t) + stream->mki_size); |
4147 | 0 | auth_tag = srtcp + (srtcp_len - tag_len - stream->mki_size - |
4148 | 0 | sizeof(srtcp_trailer_t)); |
4149 | | |
4150 | | /* |
4151 | | * check the sequence number for replays |
4152 | | */ |
4153 | | /* this is easier than dealing with bitfield access */ |
4154 | 0 | seq_num = ntohl(trailer) & SRTCP_INDEX_MASK; |
4155 | 0 | debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num); |
4156 | 0 | status = srtp_rdb_check(&stream->rtcp_rdb, seq_num); |
4157 | 0 | if (status) { |
4158 | 0 | return status; |
4159 | 0 | } |
4160 | | |
4161 | | /* |
4162 | | * Calculate and set the IV |
4163 | | */ |
4164 | 0 | status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr); |
4165 | 0 | if (status) { |
4166 | 0 | return srtp_err_status_cipher_fail; |
4167 | 0 | } |
4168 | 0 | status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv, |
4169 | 0 | srtp_direction_decrypt); |
4170 | 0 | if (status) { |
4171 | 0 | return srtp_err_status_cipher_fail; |
4172 | 0 | } |
4173 | | |
4174 | | /* check output length */ |
4175 | 0 | if (*rtcp_len < |
4176 | 0 | srtcp_len - sizeof(srtcp_trailer_t) - stream->mki_size - tag_len) { |
4177 | 0 | return srtp_err_status_buffer_small; |
4178 | 0 | } |
4179 | | |
4180 | | /* if not inplace need to copy rtcp header */ |
4181 | 0 | if (srtcp != rtcp) { |
4182 | 0 | memcpy(rtcp, srtcp, enc_start); |
4183 | 0 | } |
4184 | | |
4185 | | /* |
4186 | | * Set the AAD for GCM mode |
4187 | | */ |
4188 | 0 | if (*trailer_p & SRTCP_E_BYTE_BIT) { |
4189 | | /* |
4190 | | * If payload encryption is enabled, then the AAD consist of |
4191 | | * the RTCP header and the seq# at the end of the packet |
4192 | | */ |
4193 | 0 | status = srtp_cipher_set_aad(session_keys->rtcp_cipher, srtcp, |
4194 | 0 | octets_in_rtcp_header); |
4195 | 0 | if (status) { |
4196 | 0 | return srtp_err_status_cipher_fail; |
4197 | 0 | } |
4198 | 0 | } else { |
4199 | | /* |
4200 | | * Since payload encryption is not enabled, we must authenticate |
4201 | | * the entire packet as described in RFC 7714 (Section 9.3. Data |
4202 | | * Types in Unencrypted SRTCP Compound Packets) |
4203 | | */ |
4204 | 0 | status = srtp_cipher_set_aad( |
4205 | 0 | session_keys->rtcp_cipher, srtcp, |
4206 | 0 | (srtcp_len - tag_len - sizeof(srtcp_trailer_t) - stream->mki_size)); |
4207 | 0 | if (status) { |
4208 | 0 | return (srtp_err_status_cipher_fail); |
4209 | 0 | } |
4210 | 0 | } |
4211 | | |
4212 | | /* |
4213 | | * Process the sequence# as AAD |
4214 | | */ |
4215 | 0 | status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)&trailer, |
4216 | 0 | sizeof(trailer)); |
4217 | 0 | if (status) { |
4218 | 0 | return (srtp_err_status_cipher_fail); |
4219 | 0 | } |
4220 | | |
4221 | | /* if we're decrypting, exor keystream into the message */ |
4222 | 0 | if (*trailer_p & SRTCP_E_BYTE_BIT) { |
4223 | 0 | status = srtp_cipher_decrypt(session_keys->rtcp_cipher, |
4224 | 0 | srtcp + enc_start, enc_octet_len, |
4225 | 0 | rtcp + enc_start, &enc_octet_len); |
4226 | 0 | if (status) { |
4227 | 0 | return status; |
4228 | 0 | } |
4229 | 0 | } else { |
4230 | | /* if no encryption and not-inplace then need to copy rest of packet */ |
4231 | 0 | if (rtcp != srtcp) { |
4232 | 0 | memcpy(rtcp + enc_start, srtcp + enc_start, enc_octet_len); |
4233 | 0 | } |
4234 | | |
4235 | | /* |
4236 | | * Still need to run the cipher to check the tag |
4237 | | */ |
4238 | 0 | tmp_len = 0; |
4239 | 0 | status = srtp_cipher_decrypt(session_keys->rtcp_cipher, auth_tag, |
4240 | 0 | tag_len, NULL, &tmp_len); |
4241 | 0 | if (status) { |
4242 | 0 | return status; |
4243 | 0 | } |
4244 | 0 | } |
4245 | | |
4246 | 0 | *rtcp_len = srtcp_len; |
4247 | | |
4248 | | /* decrease the packet length by the length of the auth tag and seq_num*/ |
4249 | 0 | *rtcp_len -= (tag_len + sizeof(srtcp_trailer_t) + stream->mki_size); |
4250 | | |
4251 | | /* |
4252 | | * verify that stream is for received traffic - this check will |
4253 | | * detect SSRC collisions, since a stream that appears in both |
4254 | | * srtp_protect() and srtp_unprotect() will fail this test in one of |
4255 | | * those functions. |
4256 | | * |
4257 | | * we do this check *after* the authentication check, so that the |
4258 | | * latter check will catch any attempts to fool us into thinking |
4259 | | * that we've got a collision |
4260 | | */ |
4261 | 0 | if (stream->direction != dir_srtp_receiver) { |
4262 | 0 | if (stream->direction == dir_unknown) { |
4263 | 0 | stream->direction = dir_srtp_receiver; |
4264 | 0 | } else { |
4265 | 0 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
4266 | 0 | } |
4267 | 0 | } |
4268 | | |
4269 | | /* |
4270 | | * if the stream is a 'provisional' one, in which the template context |
4271 | | * is used, then we need to allocate a new stream at this point, since |
4272 | | * the authentication passed |
4273 | | */ |
4274 | 0 | if (stream == ctx->stream_template) { |
4275 | 0 | srtp_stream_ctx_t *new_stream; |
4276 | | |
4277 | | /* |
4278 | | * allocate and initialize a new stream |
4279 | | * |
4280 | | * note that we indicate failure if we can't allocate the new |
4281 | | * stream, and some implementations will want to not return |
4282 | | * failure here |
4283 | | */ |
4284 | 0 | status = |
4285 | 0 | srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
4286 | 0 | if (status) { |
4287 | 0 | return status; |
4288 | 0 | } |
4289 | | |
4290 | | /* add new stream to the list */ |
4291 | 0 | status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream, |
4292 | 0 | ctx->stream_template); |
4293 | 0 | if (status) { |
4294 | 0 | return status; |
4295 | 0 | } |
4296 | | |
4297 | | /* set stream (the pointer used in this function) */ |
4298 | 0 | stream = new_stream; |
4299 | 0 | } |
4300 | | |
4301 | | /* we've passed the authentication check, so add seq_num to the rdb */ |
4302 | 0 | srtp_rdb_add_index(&stream->rtcp_rdb, seq_num); |
4303 | |
|
4304 | 0 | return srtp_err_status_ok; |
4305 | 0 | } |
4306 | | |
4307 | | srtp_err_status_t srtp_protect_rtcp(srtp_t ctx, |
4308 | | const uint8_t *rtcp, |
4309 | | size_t rtcp_len, |
4310 | | uint8_t *srtcp, |
4311 | | size_t *srtcp_len, |
4312 | | size_t mki_index) |
4313 | 94.5k | { |
4314 | 94.5k | const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)rtcp; |
4315 | 94.5k | size_t enc_start; /* pointer to start of encrypted portion */ |
4316 | 94.5k | uint8_t *auth_start; /* pointer to start of auth. portion */ |
4317 | 94.5k | uint8_t *trailer_p; /* pointer to start of trailer */ |
4318 | 94.5k | uint32_t trailer; /* trailer value */ |
4319 | 94.5k | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
4320 | 94.5k | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
4321 | 94.5k | srtp_err_status_t status; |
4322 | 94.5k | size_t tag_len; |
4323 | 94.5k | srtp_stream_ctx_t *stream; |
4324 | 94.5k | size_t prefix_len; |
4325 | 94.5k | uint32_t seq_num; |
4326 | 94.5k | srtp_session_keys_t *session_keys = NULL; |
4327 | | |
4328 | | /* check the packet length - it must at least contain a full header */ |
4329 | 94.5k | if (rtcp_len < octets_in_rtcp_header) { |
4330 | 7 | return srtp_err_status_bad_param; |
4331 | 7 | } |
4332 | | |
4333 | | /* |
4334 | | * look up ssrc in srtp_stream list, and process the packet with |
4335 | | * the appropriate stream. if we haven't seen this stream before, |
4336 | | * there's only one key for this srtp_session, and the cipher |
4337 | | * supports key-sharing, then we assume that a new stream using |
4338 | | * that key has just started up |
4339 | | */ |
4340 | 94.5k | stream = srtp_get_stream(ctx, hdr->ssrc); |
4341 | 94.5k | if (stream == NULL) { |
4342 | 4.30k | if (ctx->stream_template != NULL) { |
4343 | 4.27k | srtp_stream_ctx_t *new_stream; |
4344 | | |
4345 | | /* allocate and initialize a new stream */ |
4346 | 4.27k | status = |
4347 | 4.27k | srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
4348 | 4.27k | if (status) { |
4349 | 0 | return status; |
4350 | 0 | } |
4351 | | |
4352 | | /* add new stream to the list */ |
4353 | 4.27k | status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream, |
4354 | 4.27k | ctx->stream_template); |
4355 | 4.27k | if (status) { |
4356 | 0 | return status; |
4357 | 0 | } |
4358 | | |
4359 | | /* set stream (the pointer used in this function) */ |
4360 | 4.27k | stream = new_stream; |
4361 | 4.27k | } else { |
4362 | | /* no template stream, so we return an error */ |
4363 | 30 | return srtp_err_status_no_ctx; |
4364 | 30 | } |
4365 | 4.30k | } |
4366 | | |
4367 | | /* |
4368 | | * verify that stream is for sending traffic - this check will |
4369 | | * detect SSRC collisions, since a stream that appears in both |
4370 | | * srtp_protect() and srtp_unprotect() will fail this test in one of |
4371 | | * those functions. |
4372 | | */ |
4373 | 94.5k | if (stream->direction != dir_srtp_sender) { |
4374 | 14.3k | if (stream->direction == dir_unknown) { |
4375 | 76 | stream->direction = dir_srtp_sender; |
4376 | 14.2k | } else { |
4377 | 14.2k | srtp_handle_event(ctx, stream, event_ssrc_collision); |
4378 | 14.2k | } |
4379 | 14.3k | } |
4380 | | |
4381 | 94.5k | status = srtp_get_session_keys(stream, mki_index, &session_keys); |
4382 | 94.5k | if (status) { |
4383 | 0 | return status; |
4384 | 0 | } |
4385 | | |
4386 | | /* |
4387 | | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
4388 | | * the request to our AEAD handler. |
4389 | | */ |
4390 | 94.5k | if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || |
4391 | 94.5k | session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { |
4392 | 0 | return srtp_protect_rtcp_aead(stream, rtcp, rtcp_len, srtcp, srtcp_len, |
4393 | 0 | session_keys); |
4394 | 0 | } |
4395 | | |
4396 | | /* get tag length from stream context */ |
4397 | 94.5k | tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); |
4398 | | |
4399 | | /* |
4400 | | * set encryption start and encryption length |
4401 | | */ |
4402 | 94.5k | enc_start = octets_in_rtcp_header; |
4403 | 94.5k | enc_octet_len = rtcp_len - enc_start; |
4404 | | |
4405 | | /* check output length */ |
4406 | 94.5k | if (*srtcp_len < |
4407 | 94.5k | rtcp_len + sizeof(srtcp_trailer_t) + stream->mki_size + tag_len) { |
4408 | 0 | return srtp_err_status_buffer_small; |
4409 | 0 | } |
4410 | | |
4411 | | /* if not in place then need to copy rtcp header */ |
4412 | 94.5k | if (rtcp != srtcp) { |
4413 | 0 | memcpy(srtcp, rtcp, enc_start); |
4414 | 0 | } |
4415 | | |
4416 | | /* all of the packet, except the header, gets encrypted */ |
4417 | | /* |
4418 | | * NOTE: hdr->length is not usable - it refers to only the first RTCP report |
4419 | | * in the compound packet! |
4420 | | */ |
4421 | 94.5k | trailer_p = srtcp + enc_start + enc_octet_len; |
4422 | | |
4423 | 94.5k | if (stream->rtcp_services & sec_serv_conf) { |
4424 | 58.8k | trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */ |
4425 | 58.8k | } else { |
4426 | | /* 0 is network-order independant */ |
4427 | 35.6k | trailer = 0x00000000; /* set encrypt bit */ |
4428 | 35.6k | } |
4429 | | |
4430 | 94.5k | if (stream->use_mki) { |
4431 | 0 | srtp_inject_mki(srtcp + rtcp_len + sizeof(srtcp_trailer_t), |
4432 | 0 | session_keys, stream->mki_size); |
4433 | 0 | } |
4434 | | |
4435 | | /* |
4436 | | * set the auth_start and auth_tag pointers to the proper locations |
4437 | | * (note that srtpc *always* provides authentication, unlike srtp) |
4438 | | */ |
4439 | | /* Note: This would need to change for optional mikey data */ |
4440 | 94.5k | auth_start = srtcp; |
4441 | 94.5k | auth_tag = srtcp + rtcp_len + sizeof(srtcp_trailer_t) + stream->mki_size; |
4442 | | |
4443 | | /* |
4444 | | * check sequence number for overruns, and copy it into the packet |
4445 | | * if its value isn't too big |
4446 | | */ |
4447 | 94.5k | status = srtp_rdb_increment(&stream->rtcp_rdb); |
4448 | 94.5k | if (status) { |
4449 | 2 | return status; |
4450 | 2 | } |
4451 | 94.5k | seq_num = srtp_rdb_get_value(&stream->rtcp_rdb); |
4452 | 94.5k | trailer |= htonl(seq_num); |
4453 | 94.5k | debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num); |
4454 | | |
4455 | 94.5k | memcpy(trailer_p, &trailer, sizeof(trailer)); |
4456 | | |
4457 | | /* |
4458 | | * if we're using rindael counter mode, set nonce and seq |
4459 | | */ |
4460 | 94.5k | if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 || |
4461 | 36.5k | session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 || |
4462 | 58.8k | session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) { |
4463 | 58.8k | v128_t iv; |
4464 | | |
4465 | 58.8k | iv.v32[0] = 0; |
4466 | 58.8k | iv.v32[1] = hdr->ssrc; /* still in network order! */ |
4467 | 58.8k | iv.v32[2] = htonl(seq_num >> 16); |
4468 | 58.8k | iv.v32[3] = htonl(seq_num << 16); |
4469 | 58.8k | status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv, |
4470 | 58.8k | srtp_direction_encrypt); |
4471 | | |
4472 | 58.8k | } else { |
4473 | 35.6k | v128_t iv; |
4474 | | |
4475 | | /* otherwise, just set the index to seq_num */ |
4476 | 35.6k | iv.v32[0] = 0; |
4477 | 35.6k | iv.v32[1] = 0; |
4478 | 35.6k | iv.v32[2] = 0; |
4479 | 35.6k | iv.v32[3] = htonl(seq_num); |
4480 | 35.6k | status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv, |
4481 | 35.6k | srtp_direction_encrypt); |
4482 | 35.6k | } |
4483 | 94.5k | if (status) { |
4484 | 0 | return srtp_err_status_cipher_fail; |
4485 | 0 | } |
4486 | | |
4487 | | /* |
4488 | | * if we're authenticating using a universal hash, put the keystream |
4489 | | * prefix into the authentication tag |
4490 | | */ |
4491 | | |
4492 | | /* if auth_start is non-null, then put keystream into tag */ |
4493 | 94.5k | if (auth_start) { |
4494 | | /* put keystream prefix into auth_tag */ |
4495 | 94.5k | prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth); |
4496 | 94.5k | status = srtp_cipher_output(session_keys->rtcp_cipher, auth_tag, |
4497 | 94.5k | &prefix_len); |
4498 | | |
4499 | 94.5k | debug_print(mod_srtp, "keystream prefix: %s", |
4500 | 94.5k | srtp_octet_string_hex_string(auth_tag, prefix_len)); |
4501 | | |
4502 | 94.5k | if (status) { |
4503 | 0 | return srtp_err_status_cipher_fail; |
4504 | 0 | } |
4505 | 94.5k | } |
4506 | | |
4507 | | /* if we're encrypting, exor keystream into the message */ |
4508 | 94.5k | if (stream->rtcp_services & sec_serv_conf) { |
4509 | 58.8k | status = srtp_cipher_encrypt(session_keys->rtcp_cipher, |
4510 | 58.8k | rtcp + enc_start, enc_octet_len, |
4511 | 58.8k | srtcp + enc_start, &enc_octet_len); |
4512 | 58.8k | if (status) { |
4513 | 0 | return srtp_err_status_cipher_fail; |
4514 | 0 | } |
4515 | 58.8k | } else if (rtcp != srtcp) { |
4516 | | /* if no encryption and not-inplace then need to copy rest of packet */ |
4517 | 0 | memcpy(srtcp + enc_start, rtcp + enc_start, enc_octet_len); |
4518 | 0 | } |
4519 | | |
4520 | | /* initialize auth func context */ |
4521 | 94.5k | status = srtp_auth_start(session_keys->rtcp_auth); |
4522 | 94.5k | if (status) { |
4523 | 0 | return status; |
4524 | 0 | } |
4525 | | |
4526 | | /* |
4527 | | * run auth func over packet (including trailer), and write the |
4528 | | * result at auth_tag |
4529 | | */ |
4530 | 94.5k | status = srtp_auth_compute(session_keys->rtcp_auth, auth_start, |
4531 | 94.5k | rtcp_len + sizeof(srtcp_trailer_t), auth_tag); |
4532 | 94.5k | debug_print(mod_srtp, "srtcp auth tag: %s", |
4533 | 94.5k | srtp_octet_string_hex_string(auth_tag, tag_len)); |
4534 | 94.5k | if (status) { |
4535 | 0 | return srtp_err_status_auth_fail; |
4536 | 0 | } |
4537 | | |
4538 | 94.5k | *srtcp_len = enc_start + enc_octet_len; |
4539 | | |
4540 | | /* increase the packet length by the length of the auth tag and seq_num*/ |
4541 | 94.5k | *srtcp_len += (tag_len + sizeof(srtcp_trailer_t)); |
4542 | | |
4543 | | /* increase the packet by the mki_size */ |
4544 | 94.5k | *srtcp_len += stream->mki_size; |
4545 | | |
4546 | 94.5k | return srtp_err_status_ok; |
4547 | 94.5k | } |
4548 | | |
4549 | | srtp_err_status_t srtp_unprotect_rtcp(srtp_t ctx, |
4550 | | const uint8_t *srtcp, |
4551 | | size_t srtcp_len, |
4552 | | uint8_t *rtcp, |
4553 | | size_t *rtcp_len) |
4554 | 2.39k | { |
4555 | 2.39k | const srtcp_hdr_t *hdr = (const srtcp_hdr_t *)srtcp; |
4556 | 2.39k | size_t enc_start; /* pointer to start of encrypted portion */ |
4557 | 2.39k | const uint8_t *auth_start; /* pointer to start of auth. portion */ |
4558 | 2.39k | const uint8_t *trailer_p; /* pointer to start of trailer */ |
4559 | 2.39k | uint32_t trailer; /* trailer value */ |
4560 | 2.39k | size_t enc_octet_len = 0; /* number of octets in encrypted portion */ |
4561 | 2.39k | const uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
4562 | 2.39k | uint8_t tmp_tag[SRTP_MAX_TAG_LEN]; |
4563 | 2.39k | srtp_err_status_t status; |
4564 | 2.39k | size_t auth_len; |
4565 | 2.39k | size_t tag_len; |
4566 | 2.39k | srtp_stream_ctx_t *stream; |
4567 | 2.39k | size_t prefix_len; |
4568 | 2.39k | uint32_t seq_num; |
4569 | 2.39k | bool e_bit_in_packet; /* E-bit was found in the packet */ |
4570 | 2.39k | bool sec_serv_confidentiality; /* whether confidentiality was requested */ |
4571 | 2.39k | srtp_session_keys_t *session_keys = NULL; |
4572 | | |
4573 | | /* |
4574 | | * check that the length value is sane; we'll check again once we |
4575 | | * know the tag length, but we at least want to know that it is |
4576 | | * a positive value |
4577 | | */ |
4578 | 2.39k | if (srtcp_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t)) { |
4579 | 14 | return srtp_err_status_bad_param; |
4580 | 14 | } |
4581 | | |
4582 | | /* |
4583 | | * look up ssrc in srtp_stream list, and process the packet with |
4584 | | * the appropriate stream. if we haven't seen this stream before, |
4585 | | * there's only one key for this srtp_session, and the cipher |
4586 | | * supports key-sharing, then we assume that a new stream using |
4587 | | * that key has just started up |
4588 | | */ |
4589 | 2.37k | stream = srtp_get_stream(ctx, hdr->ssrc); |
4590 | 2.37k | if (stream == NULL) { |
4591 | 830 | if (ctx->stream_template != NULL) { |
4592 | 799 | stream = ctx->stream_template; |
4593 | | |
4594 | 799 | debug_print(mod_srtp, |
4595 | 799 | "srtcp using provisional stream (SSRC: 0x%08x)", |
4596 | 799 | (unsigned int)ntohl(hdr->ssrc)); |
4597 | 799 | } else { |
4598 | | /* no template stream, so we return an error */ |
4599 | 31 | return srtp_err_status_no_ctx; |
4600 | 31 | } |
4601 | 830 | } |
4602 | | |
4603 | | /* |
4604 | | * Determine if MKI is being used and what session keys should be used |
4605 | | */ |
4606 | 2.34k | status = srtp_get_session_keys_for_rtcp_packet(stream, srtcp, srtcp_len, |
4607 | 2.34k | &session_keys); |
4608 | 2.34k | if (status) { |
4609 | 0 | return status; |
4610 | 0 | } |
4611 | | |
4612 | | /* get tag length from stream context */ |
4613 | 2.34k | tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth); |
4614 | | |
4615 | | /* check the packet length - it must contain at least a full RTCP |
4616 | | header, an auth tag (if applicable), and the SRTCP encrypted flag |
4617 | | and 31-bit index value */ |
4618 | 2.34k | if (srtcp_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t) + |
4619 | 2.34k | stream->mki_size + tag_len) { |
4620 | 2 | return srtp_err_status_bad_param; |
4621 | 2 | } |
4622 | | |
4623 | | /* |
4624 | | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
4625 | | * the request to our AEAD handler. |
4626 | | */ |
4627 | 2.34k | if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 || |
4628 | 2.34k | session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) { |
4629 | 0 | return srtp_unprotect_rtcp_aead(ctx, stream, srtcp, srtcp_len, rtcp, |
4630 | 0 | rtcp_len, session_keys); |
4631 | 0 | } |
4632 | | |
4633 | 2.34k | sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf || |
4634 | 1.76k | stream->rtcp_services == sec_serv_conf_and_auth; |
4635 | | |
4636 | | /* |
4637 | | * set encryption start, encryption length, and trailer |
4638 | | */ |
4639 | 2.34k | enc_start = octets_in_rtcp_header; |
4640 | 2.34k | enc_octet_len = srtcp_len - (octets_in_rtcp_header + tag_len + |
4641 | 2.34k | stream->mki_size + sizeof(srtcp_trailer_t)); |
4642 | | /* |
4643 | | *index & E (encryption) bit follow normal data. hdr->len is the number of |
4644 | | * words (32-bit) in the normal packet minus 1 |
4645 | | */ |
4646 | | /* This should point trailer to the word past the end of the normal data. */ |
4647 | | /* This would need to be modified for optional mikey data */ |
4648 | 2.34k | trailer_p = srtcp + srtcp_len - |
4649 | 2.34k | (tag_len + stream->mki_size + sizeof(srtcp_trailer_t)); |
4650 | 2.34k | memcpy(&trailer, trailer_p, sizeof(trailer)); |
4651 | | |
4652 | 2.34k | e_bit_in_packet = (*trailer_p & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT; |
4653 | 2.34k | if (e_bit_in_packet != sec_serv_confidentiality) { |
4654 | 35 | return srtp_err_status_cant_check; |
4655 | 35 | } |
4656 | | |
4657 | | /* |
4658 | | * set the auth_start and auth_tag pointers to the proper locations |
4659 | | * (note that srtcp *always* uses authentication, unlike srtp) |
4660 | | */ |
4661 | 2.31k | auth_start = srtcp; |
4662 | | |
4663 | | /* |
4664 | | * The location of the auth tag in the packet needs to know MKI |
4665 | | * could be present. The data needed to calculate the Auth tag |
4666 | | * must not include the MKI |
4667 | | */ |
4668 | 2.31k | auth_len = srtcp_len - tag_len - stream->mki_size; |
4669 | 2.31k | auth_tag = srtcp + auth_len + stream->mki_size; |
4670 | | |
4671 | | /* |
4672 | | * check the sequence number for replays |
4673 | | */ |
4674 | | /* this is easier than dealing with bitfield access */ |
4675 | 2.31k | seq_num = ntohl(trailer) & SRTCP_INDEX_MASK; |
4676 | 2.31k | debug_print(mod_srtp, "srtcp index: %x", (unsigned int)seq_num); |
4677 | 2.31k | status = srtp_rdb_check(&stream->rtcp_rdb, seq_num); |
4678 | 2.31k | if (status) { |
4679 | 41 | return status; |
4680 | 41 | } |
4681 | | |
4682 | | /* |
4683 | | * if we're using aes counter mode, set nonce and seq |
4684 | | */ |
4685 | 2.26k | if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 || |
4686 | 1.78k | session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 || |
4687 | 1.78k | session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) { |
4688 | 875 | v128_t iv; |
4689 | | |
4690 | 875 | iv.v32[0] = 0; |
4691 | 875 | iv.v32[1] = hdr->ssrc; /* still in network order! */ |
4692 | 875 | iv.v32[2] = htonl(seq_num >> 16); |
4693 | 875 | iv.v32[3] = htonl(seq_num << 16); |
4694 | 875 | status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv, |
4695 | 875 | srtp_direction_decrypt); |
4696 | | |
4697 | 1.39k | } else { |
4698 | 1.39k | v128_t iv; |
4699 | | |
4700 | | /* otherwise, just set the index to seq_num */ |
4701 | 1.39k | iv.v32[0] = 0; |
4702 | 1.39k | iv.v32[1] = 0; |
4703 | 1.39k | iv.v32[2] = 0; |
4704 | 1.39k | iv.v32[3] = htonl(seq_num); |
4705 | 1.39k | status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv, |
4706 | 1.39k | srtp_direction_decrypt); |
4707 | 1.39k | } |
4708 | 2.26k | if (status) { |
4709 | 0 | return srtp_err_status_cipher_fail; |
4710 | 0 | } |
4711 | | |
4712 | | /* |
4713 | | * if we're authenticating using a universal hash, put the keystream |
4714 | | * prefix into the authentication tag |
4715 | | */ |
4716 | 2.26k | prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth); |
4717 | 2.26k | if (prefix_len) { |
4718 | 0 | status = |
4719 | 0 | srtp_cipher_output(session_keys->rtcp_cipher, tmp_tag, &prefix_len); |
4720 | 0 | debug_print(mod_srtp, "keystream prefix: %s", |
4721 | 0 | srtp_octet_string_hex_string(tmp_tag, prefix_len)); |
4722 | 0 | if (status) { |
4723 | 0 | return srtp_err_status_cipher_fail; |
4724 | 0 | } |
4725 | 0 | } |
4726 | | |
4727 | | /* initialize auth func context */ |
4728 | 2.26k | status = srtp_auth_start(session_keys->rtcp_auth); |
4729 | 2.26k | if (status) { |
4730 | 0 | return status; |
4731 | 0 | } |
4732 | | |
4733 | | /* run auth func over packet, put result into tmp_tag */ |
4734 | 2.26k | status = srtp_auth_compute(session_keys->rtcp_auth, auth_start, auth_len, |
4735 | 2.26k | tmp_tag); |
4736 | 2.26k | debug_print(mod_srtp, "srtcp computed tag: %s", |
4737 | 2.26k | srtp_octet_string_hex_string(tmp_tag, tag_len)); |
4738 | 2.26k | if (status) { |
4739 | 0 | return srtp_err_status_auth_fail; |
4740 | 0 | } |
4741 | | |
4742 | | /* compare the tag just computed with the one in the packet */ |
4743 | 2.26k | debug_print(mod_srtp, "srtcp tag from packet: %s", |
4744 | 2.26k | srtp_octet_string_hex_string(auth_tag, tag_len)); |
4745 | 2.26k | if (!srtp_octet_string_equal(tmp_tag, auth_tag, tag_len)) { |
4746 | 24 | return srtp_err_status_auth_fail; |
4747 | 24 | } |
4748 | | |
4749 | | /* check output length */ |
4750 | 2.24k | if (*rtcp_len < |
4751 | 2.24k | srtcp_len - sizeof(srtcp_trailer_t) - stream->mki_size - tag_len) { |
4752 | 0 | return srtp_err_status_buffer_small; |
4753 | 0 | } |
4754 | | |
4755 | | /* if not inplace need to copy rtcp header */ |
4756 | 2.24k | if (srtcp != rtcp) { |
4757 | 0 | memcpy(rtcp, srtcp, enc_start); |
4758 | 0 | } |
4759 | | |
4760 | | /* if we're decrypting, exor keystream into the message */ |
4761 | 2.24k | if (sec_serv_confidentiality) { |
4762 | 853 | status = srtp_cipher_decrypt(session_keys->rtcp_cipher, |
4763 | 853 | srtcp + enc_start, enc_octet_len, |
4764 | 853 | rtcp + enc_start, &enc_octet_len); |
4765 | 853 | if (status) { |
4766 | 0 | return srtp_err_status_cipher_fail; |
4767 | 0 | } |
4768 | 1.39k | } else if (srtcp != rtcp) { |
4769 | | /* if no encryption and not-inplace then need to copy rest of packet */ |
4770 | 0 | memcpy(rtcp + enc_start, srtcp + enc_start, enc_octet_len); |
4771 | 0 | } |
4772 | | |
4773 | 2.24k | *rtcp_len = srtcp_len; |
4774 | | |
4775 | | /* decrease the packet length by the length of the auth tag and seq_num */ |
4776 | 2.24k | *rtcp_len -= (tag_len + sizeof(srtcp_trailer_t)); |
4777 | | |
4778 | | /* decrease the packet length by the length of the mki_size */ |
4779 | 2.24k | *rtcp_len -= stream->mki_size; |
4780 | | |
4781 | | /* |
4782 | | * verify that stream is for received traffic - this check will |
4783 | | * detect SSRC collisions, since a stream that appears in both |
4784 | | * srtp_protect() and srtp_unprotect() will fail this test in one of |
4785 | | * those functions. |
4786 | | * |
4787 | | * we do this check *after* the authentication check, so that the |
4788 | | * latter check will catch any attempts to fool us into thinking |
4789 | | * that we've got a collision |
4790 | | */ |
4791 | 2.24k | if (stream->direction != dir_srtp_receiver) { |
4792 | 1.02k | if (stream->direction == dir_unknown) { |
4793 | 34 | stream->direction = dir_srtp_receiver; |
4794 | 995 | } else { |
4795 | 995 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
4796 | 995 | } |
4797 | 1.02k | } |
4798 | | |
4799 | | /* |
4800 | | * if the stream is a 'provisional' one, in which the template context |
4801 | | * is used, then we need to allocate a new stream at this point, since |
4802 | | * the authentication passed |
4803 | | */ |
4804 | 2.24k | if (stream == ctx->stream_template) { |
4805 | 746 | srtp_stream_ctx_t *new_stream; |
4806 | | |
4807 | | /* |
4808 | | * allocate and initialize a new stream |
4809 | | * |
4810 | | * note that we indicate failure if we can't allocate the new |
4811 | | * stream, and some implementations will want to not return |
4812 | | * failure here |
4813 | | */ |
4814 | 746 | status = |
4815 | 746 | srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
4816 | 746 | if (status) { |
4817 | 0 | return status; |
4818 | 0 | } |
4819 | | |
4820 | | /* add new stream to the list */ |
4821 | 746 | status = srtp_insert_or_dealloc_stream(ctx->stream_list, new_stream, |
4822 | 746 | ctx->stream_template); |
4823 | 746 | if (status) { |
4824 | 0 | return status; |
4825 | 0 | } |
4826 | | |
4827 | | /* set stream (the pointer used in this function) */ |
4828 | 746 | stream = new_stream; |
4829 | 746 | } |
4830 | | |
4831 | | /* we've passed the authentication check, so add seq_num to the rdb */ |
4832 | 2.24k | srtp_rdb_add_index(&stream->rtcp_rdb, seq_num); |
4833 | | |
4834 | 2.24k | return srtp_err_status_ok; |
4835 | 2.24k | } |
4836 | | |
4837 | | /* |
4838 | | * user data within srtp_t context |
4839 | | */ |
4840 | | |
4841 | | void srtp_set_user_data(srtp_t ctx, void *data) |
4842 | 0 | { |
4843 | 0 | ctx->user_data = data; |
4844 | 0 | } |
4845 | | |
4846 | | void *srtp_get_user_data(srtp_t ctx) |
4847 | 0 | { |
4848 | 0 | return ctx->user_data; |
4849 | 0 | } |
4850 | | |
4851 | | srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtp( |
4852 | | srtp_crypto_policy_t *policy, |
4853 | | srtp_profile_t profile) |
4854 | 0 | { |
4855 | | /* set SRTP policy from the SRTP profile in the key set */ |
4856 | 0 | switch (profile) { |
4857 | 0 | case srtp_profile_aes128_cm_sha1_80: |
4858 | 0 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
4859 | 0 | break; |
4860 | 0 | case srtp_profile_aes128_cm_sha1_32: |
4861 | 0 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy); |
4862 | 0 | break; |
4863 | 0 | case srtp_profile_null_sha1_80: |
4864 | 0 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
4865 | 0 | break; |
4866 | | #ifdef GCM |
4867 | | case srtp_profile_aead_aes_128_gcm: |
4868 | | srtp_crypto_policy_set_aes_gcm_128_16_auth(policy); |
4869 | | break; |
4870 | | case srtp_profile_aead_aes_256_gcm: |
4871 | | srtp_crypto_policy_set_aes_gcm_256_16_auth(policy); |
4872 | | break; |
4873 | | #endif |
4874 | | /* the following profiles are not (yet) supported */ |
4875 | 0 | case srtp_profile_null_sha1_32: |
4876 | 0 | default: |
4877 | 0 | return srtp_err_status_bad_param; |
4878 | 0 | } |
4879 | | |
4880 | 0 | return srtp_err_status_ok; |
4881 | 0 | } |
4882 | | |
4883 | | srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtcp( |
4884 | | srtp_crypto_policy_t *policy, |
4885 | | srtp_profile_t profile) |
4886 | 0 | { |
4887 | | /* set SRTP policy from the SRTP profile in the key set */ |
4888 | 0 | switch (profile) { |
4889 | 0 | case srtp_profile_aes128_cm_sha1_80: |
4890 | 0 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
4891 | 0 | break; |
4892 | 0 | case srtp_profile_aes128_cm_sha1_32: |
4893 | | /* We do not honor the 32-bit auth tag request since |
4894 | | * this is not compliant with RFC 3711 */ |
4895 | 0 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
4896 | 0 | break; |
4897 | 0 | case srtp_profile_null_sha1_80: |
4898 | 0 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
4899 | 0 | break; |
4900 | | #ifdef GCM |
4901 | | case srtp_profile_aead_aes_128_gcm: |
4902 | | srtp_crypto_policy_set_aes_gcm_128_16_auth(policy); |
4903 | | break; |
4904 | | case srtp_profile_aead_aes_256_gcm: |
4905 | | srtp_crypto_policy_set_aes_gcm_256_16_auth(policy); |
4906 | | break; |
4907 | | #endif |
4908 | | /* the following profiles are not (yet) supported */ |
4909 | 0 | case srtp_profile_null_sha1_32: |
4910 | 0 | default: |
4911 | 0 | return srtp_err_status_bad_param; |
4912 | 0 | } |
4913 | | |
4914 | 0 | return srtp_err_status_ok; |
4915 | 0 | } |
4916 | | |
4917 | | void srtp_append_salt_to_key(uint8_t *key, |
4918 | | size_t bytes_in_key, |
4919 | | uint8_t *salt, |
4920 | | size_t bytes_in_salt) |
4921 | 0 | { |
4922 | 0 | memcpy(key + bytes_in_key, salt, bytes_in_salt); |
4923 | 0 | } |
4924 | | |
4925 | | size_t srtp_profile_get_master_key_length(srtp_profile_t profile) |
4926 | 0 | { |
4927 | 0 | switch (profile) { |
4928 | 0 | case srtp_profile_aes128_cm_sha1_80: |
4929 | 0 | return SRTP_AES_128_KEY_LEN; |
4930 | 0 | break; |
4931 | 0 | case srtp_profile_aes128_cm_sha1_32: |
4932 | 0 | return SRTP_AES_128_KEY_LEN; |
4933 | 0 | break; |
4934 | 0 | case srtp_profile_null_sha1_80: |
4935 | 0 | return SRTP_AES_128_KEY_LEN; |
4936 | 0 | break; |
4937 | 0 | case srtp_profile_aead_aes_128_gcm: |
4938 | 0 | return SRTP_AES_128_KEY_LEN; |
4939 | 0 | break; |
4940 | 0 | case srtp_profile_aead_aes_256_gcm: |
4941 | 0 | return SRTP_AES_256_KEY_LEN; |
4942 | 0 | break; |
4943 | | /* the following profiles are not (yet) supported */ |
4944 | 0 | case srtp_profile_null_sha1_32: |
4945 | 0 | default: |
4946 | 0 | return 0; /* indicate error by returning a zero */ |
4947 | 0 | } |
4948 | 0 | } |
4949 | | |
4950 | | size_t srtp_profile_get_master_salt_length(srtp_profile_t profile) |
4951 | 0 | { |
4952 | 0 | switch (profile) { |
4953 | 0 | case srtp_profile_aes128_cm_sha1_80: |
4954 | 0 | return SRTP_SALT_LEN; |
4955 | 0 | break; |
4956 | 0 | case srtp_profile_aes128_cm_sha1_32: |
4957 | 0 | return SRTP_SALT_LEN; |
4958 | 0 | break; |
4959 | 0 | case srtp_profile_null_sha1_80: |
4960 | 0 | return SRTP_SALT_LEN; |
4961 | 0 | break; |
4962 | 0 | case srtp_profile_aead_aes_128_gcm: |
4963 | 0 | return SRTP_AEAD_SALT_LEN; |
4964 | 0 | break; |
4965 | 0 | case srtp_profile_aead_aes_256_gcm: |
4966 | 0 | return SRTP_AEAD_SALT_LEN; |
4967 | 0 | break; |
4968 | | /* the following profiles are not (yet) supported */ |
4969 | 0 | case srtp_profile_null_sha1_32: |
4970 | 0 | default: |
4971 | 0 | return 0; /* indicate error by returning a zero */ |
4972 | 0 | } |
4973 | 0 | } |
4974 | | |
4975 | | srtp_err_status_t stream_get_protect_trailer_length(srtp_stream_ctx_t *stream, |
4976 | | bool is_rtp, |
4977 | | size_t mki_index, |
4978 | | size_t *length) |
4979 | 236M | { |
4980 | 236M | srtp_session_keys_t *session_key; |
4981 | | |
4982 | 236M | *length = 0; |
4983 | | |
4984 | 236M | if (stream->use_mki) { |
4985 | 0 | if (mki_index >= stream->num_master_keys) { |
4986 | 0 | return srtp_err_status_bad_mki; |
4987 | 0 | } |
4988 | 0 | session_key = &stream->session_keys[mki_index]; |
4989 | |
|
4990 | 0 | *length += stream->mki_size; |
4991 | |
|
4992 | 236M | } else { |
4993 | 236M | session_key = &stream->session_keys[0]; |
4994 | 236M | } |
4995 | 236M | if (is_rtp) { |
4996 | 1.47M | *length += srtp_auth_get_tag_length(session_key->rtp_auth); |
4997 | 234M | } else { |
4998 | 234M | *length += srtp_auth_get_tag_length(session_key->rtcp_auth); |
4999 | 234M | *length += sizeof(srtcp_trailer_t); |
5000 | 234M | } |
5001 | | |
5002 | 236M | return srtp_err_status_ok; |
5003 | 236M | } |
5004 | | |
5005 | | struct get_protect_trailer_length_data { |
5006 | | bool found_stream; /* whether at least one matching stream was found */ |
5007 | | size_t length; /* maximum trailer length found so far */ |
5008 | | bool is_rtp; |
5009 | | size_t mki_index; |
5010 | | }; |
5011 | | |
5012 | | static bool get_protect_trailer_length_cb(srtp_stream_t stream, void *raw_data) |
5013 | 236M | { |
5014 | 236M | struct get_protect_trailer_length_data *data = |
5015 | 236M | (struct get_protect_trailer_length_data *)raw_data; |
5016 | 236M | size_t temp_length; |
5017 | | |
5018 | 236M | if (stream_get_protect_trailer_length(stream, data->is_rtp, data->mki_index, |
5019 | 236M | &temp_length) == srtp_err_status_ok) { |
5020 | 236M | data->found_stream = true; |
5021 | 236M | if (temp_length > data->length) { |
5022 | 574 | data->length = temp_length; |
5023 | 574 | } |
5024 | 236M | } |
5025 | | |
5026 | 236M | return true; |
5027 | 236M | } |
5028 | | |
5029 | | srtp_err_status_t get_protect_trailer_length(srtp_t session, |
5030 | | bool is_rtp, |
5031 | | size_t mki_index, |
5032 | | size_t *length) |
5033 | 119k | { |
5034 | 119k | srtp_stream_ctx_t *stream; |
5035 | 119k | struct get_protect_trailer_length_data data = { false, 0, is_rtp, |
5036 | 119k | mki_index }; |
5037 | | |
5038 | 119k | if (session == NULL) { |
5039 | 0 | return srtp_err_status_bad_param; |
5040 | 0 | } |
5041 | | |
5042 | 119k | stream = session->stream_template; |
5043 | | |
5044 | 119k | if (stream != NULL) { |
5045 | 118k | data.found_stream = true; |
5046 | 118k | stream_get_protect_trailer_length(stream, is_rtp, mki_index, |
5047 | 118k | &data.length); |
5048 | 118k | } |
5049 | | |
5050 | 119k | srtp_stream_list_for_each(session->stream_list, |
5051 | 119k | get_protect_trailer_length_cb, &data); |
5052 | | |
5053 | 119k | if (!data.found_stream) { |
5054 | 2 | return srtp_err_status_bad_param; |
5055 | 2 | } |
5056 | | |
5057 | 119k | *length = data.length; |
5058 | 119k | return srtp_err_status_ok; |
5059 | 119k | } |
5060 | | |
5061 | | srtp_err_status_t srtp_get_protect_trailer_length(srtp_t session, |
5062 | | size_t mki_index, |
5063 | | size_t *length) |
5064 | 24.8k | { |
5065 | 24.8k | return get_protect_trailer_length(session, true, mki_index, length); |
5066 | 24.8k | } |
5067 | | |
5068 | | srtp_err_status_t srtp_get_protect_rtcp_trailer_length(srtp_t session, |
5069 | | size_t mki_index, |
5070 | | size_t *length) |
5071 | 94.5k | { |
5072 | 94.5k | return get_protect_trailer_length(session, false, mki_index, length); |
5073 | 94.5k | } |
5074 | | |
5075 | | /* |
5076 | | * SRTP debug interface |
5077 | | */ |
5078 | | srtp_err_status_t srtp_set_debug_module(const char *mod_name, bool v) |
5079 | 0 | { |
5080 | 0 | return srtp_crypto_kernel_set_debug_module(mod_name, v); |
5081 | 0 | } |
5082 | | |
5083 | | srtp_err_status_t srtp_list_debug_modules(void) |
5084 | 0 | { |
5085 | 0 | return srtp_crypto_kernel_list_debug_modules(); |
5086 | 0 | } |
5087 | | |
5088 | | /* |
5089 | | * srtp_log_handler is a global variable holding a pointer to the |
5090 | | * log handler function; this function is called for any log |
5091 | | * output. |
5092 | | */ |
5093 | | |
5094 | | static srtp_log_handler_func_t *srtp_log_handler = NULL; |
5095 | | static void *srtp_log_handler_data = NULL; |
5096 | | |
5097 | | static void srtp_err_handler(srtp_err_reporting_level_t level, const char *msg) |
5098 | 0 | { |
5099 | 0 | if (srtp_log_handler) { |
5100 | 0 | srtp_log_level_t log_level = srtp_log_level_error; |
5101 | 0 | switch (level) { |
5102 | 0 | case srtp_err_level_error: |
5103 | 0 | log_level = srtp_log_level_error; |
5104 | 0 | break; |
5105 | 0 | case srtp_err_level_warning: |
5106 | 0 | log_level = srtp_log_level_warning; |
5107 | 0 | break; |
5108 | 0 | case srtp_err_level_info: |
5109 | 0 | log_level = srtp_log_level_info; |
5110 | 0 | break; |
5111 | 0 | case srtp_err_level_debug: |
5112 | 0 | log_level = srtp_log_level_debug; |
5113 | 0 | break; |
5114 | 0 | } |
5115 | | |
5116 | 0 | srtp_log_handler(log_level, msg, srtp_log_handler_data); |
5117 | 0 | } |
5118 | 0 | } |
5119 | | |
5120 | | srtp_err_status_t srtp_install_log_handler(srtp_log_handler_func_t func, |
5121 | | void *data) |
5122 | 0 | { |
5123 | | /* |
5124 | | * note that we accept NULL arguments intentionally - calling this |
5125 | | * function with a NULL arguments removes a log handler that's |
5126 | | * been previously installed |
5127 | | */ |
5128 | |
|
5129 | 0 | if (srtp_log_handler) { |
5130 | 0 | srtp_install_err_report_handler(NULL); |
5131 | 0 | } |
5132 | 0 | srtp_log_handler = func; |
5133 | 0 | srtp_log_handler_data = data; |
5134 | 0 | if (srtp_log_handler) { |
5135 | 0 | srtp_install_err_report_handler(srtp_err_handler); |
5136 | 0 | } |
5137 | 0 | return srtp_err_status_ok; |
5138 | 0 | } |
5139 | | |
5140 | | srtp_err_status_t srtp_stream_set_roc(srtp_t session, |
5141 | | uint32_t ssrc, |
5142 | | uint32_t roc) |
5143 | 1.02k | { |
5144 | 1.02k | srtp_stream_t stream; |
5145 | | |
5146 | 1.02k | stream = srtp_get_stream(session, htonl(ssrc)); |
5147 | 1.02k | if (stream == NULL) { |
5148 | 60 | return srtp_err_status_bad_param; |
5149 | 60 | } |
5150 | | |
5151 | 966 | stream->pending_roc = roc; |
5152 | | |
5153 | 966 | return srtp_err_status_ok; |
5154 | 1.02k | } |
5155 | | |
5156 | | srtp_err_status_t srtp_stream_get_roc(srtp_t session, |
5157 | | uint32_t ssrc, |
5158 | | uint32_t *roc) |
5159 | 966 | { |
5160 | 966 | srtp_stream_t stream; |
5161 | | |
5162 | 966 | stream = srtp_get_stream(session, htonl(ssrc)); |
5163 | 966 | if (stream == NULL) { |
5164 | 54 | return srtp_err_status_bad_param; |
5165 | 54 | } |
5166 | | |
5167 | 912 | *roc = srtp_rdbx_get_roc(&stream->rtp_rdbx); |
5168 | | |
5169 | 912 | return srtp_err_status_ok; |
5170 | 966 | } |
5171 | | |
5172 | | #ifndef SRTP_NO_STREAM_LIST |
5173 | | |
5174 | 6.21k | #define INITIAL_STREAM_INDEX_SIZE 2 |
5175 | | |
5176 | | typedef struct list_entry { |
5177 | | uint32_t ssrc; |
5178 | | srtp_stream_t stream; |
5179 | | } list_entry; |
5180 | | |
5181 | | typedef struct srtp_stream_list_ctx_t_ { |
5182 | | list_entry *entries; |
5183 | | size_t capacity; |
5184 | | size_t size; |
5185 | | } srtp_stream_list_ctx_t_; |
5186 | | |
5187 | | srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr) |
5188 | 3.10k | { |
5189 | 3.10k | srtp_stream_list_t list = |
5190 | 3.10k | srtp_crypto_alloc(sizeof(srtp_stream_list_ctx_t_)); |
5191 | 3.10k | if (list == NULL) { |
5192 | 0 | return srtp_err_status_alloc_fail; |
5193 | 0 | } |
5194 | | |
5195 | 3.10k | list->entries = |
5196 | 3.10k | srtp_crypto_alloc(sizeof(list_entry) * INITIAL_STREAM_INDEX_SIZE); |
5197 | 3.10k | if (list->entries == NULL) { |
5198 | 0 | srtp_crypto_free(list); |
5199 | 0 | return srtp_err_status_alloc_fail; |
5200 | 0 | } |
5201 | | |
5202 | 3.10k | list->capacity = INITIAL_STREAM_INDEX_SIZE; |
5203 | 3.10k | list->size = 0; |
5204 | | |
5205 | 3.10k | *list_ptr = list; |
5206 | | |
5207 | 3.10k | return srtp_err_status_ok; |
5208 | 3.10k | } |
5209 | | |
5210 | | srtp_err_status_t srtp_stream_list_dealloc(srtp_stream_list_t list) |
5211 | 3.10k | { |
5212 | | /* list must be empty */ |
5213 | 3.10k | if (list->size != 0) { |
5214 | 0 | return srtp_err_status_fail; |
5215 | 0 | } |
5216 | | |
5217 | 3.10k | srtp_crypto_free(list->entries); |
5218 | 3.10k | srtp_crypto_free(list); |
5219 | | |
5220 | 3.10k | return srtp_err_status_ok; |
5221 | 3.10k | } |
5222 | | |
5223 | | /* |
5224 | | * inserting a new entry in the list may require reallocating memory in order |
5225 | | * to keep all the items in a contiguous memory block. |
5226 | | */ |
5227 | | srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list, |
5228 | | srtp_stream_t stream) |
5229 | 98.0k | { |
5230 | | /* |
5231 | | * there is no space to hold the new entry in the entries buffer, |
5232 | | * double the size of the buffer. |
5233 | | */ |
5234 | 98.0k | if (list->size == list->capacity) { |
5235 | 2.08k | size_t new_capacity = list->capacity * 2; |
5236 | | |
5237 | | // Check for capacity overflow. |
5238 | 2.08k | if (new_capacity < list->capacity || |
5239 | 2.08k | new_capacity > SIZE_MAX / sizeof(list_entry)) { |
5240 | 0 | return srtp_err_status_alloc_fail; |
5241 | 0 | } |
5242 | | |
5243 | 2.08k | list_entry *new_entries = |
5244 | 2.08k | srtp_crypto_alloc(sizeof(list_entry) * new_capacity); |
5245 | 2.08k | if (new_entries == NULL) { |
5246 | 0 | return srtp_err_status_alloc_fail; |
5247 | 0 | } |
5248 | | |
5249 | | // Copy previous entries into the new buffer. |
5250 | 2.08k | memcpy(new_entries, list->entries, sizeof(list_entry) * list->capacity); |
5251 | | |
5252 | | // Release previous entries. |
5253 | 2.08k | srtp_crypto_free(list->entries); |
5254 | | |
5255 | | // Assign new entries to the list. |
5256 | 2.08k | list->entries = new_entries; |
5257 | | |
5258 | | // Update list capacity. |
5259 | 2.08k | list->capacity = new_capacity; |
5260 | 2.08k | } |
5261 | | |
5262 | | // fill the first available entry |
5263 | 98.0k | size_t next_index = list->size; |
5264 | 98.0k | list->entries[next_index].ssrc = stream->ssrc; |
5265 | 98.0k | list->entries[next_index].stream = stream; |
5266 | | |
5267 | | // update size value |
5268 | 98.0k | list->size++; |
5269 | | |
5270 | 98.0k | return srtp_err_status_ok; |
5271 | 98.0k | } |
5272 | | |
5273 | | /* |
5274 | | * removing an entry from the list performs a memory move of the following |
5275 | | * entries one position back in order to keep all the entries in the buffer |
5276 | | * contiguous. |
5277 | | */ |
5278 | | void srtp_stream_list_remove(srtp_stream_list_t list, |
5279 | | srtp_stream_t stream_to_remove) |
5280 | 98.0k | { |
5281 | 98.0k | size_t end = list->size; |
5282 | | |
5283 | 99.3k | for (size_t i = 0; i < end; i++) { |
5284 | 99.3k | if (list->entries[i].ssrc == stream_to_remove->ssrc) { |
5285 | 98.0k | size_t entries_to_move = list->size - i - 1; |
5286 | 98.0k | memmove(&list->entries[i], &list->entries[i + 1], |
5287 | 98.0k | sizeof(list_entry) * entries_to_move); |
5288 | 98.0k | list->size--; |
5289 | | |
5290 | 98.0k | break; |
5291 | 98.0k | } |
5292 | 99.3k | } |
5293 | 98.0k | } |
5294 | | |
5295 | | srtp_stream_t srtp_stream_list_get(srtp_stream_list_t list, uint32_t ssrc) |
5296 | 216k | { |
5297 | 216k | size_t end = list->size; |
5298 | | |
5299 | 216k | list_entry *entries = list->entries; |
5300 | | |
5301 | 232M | for (size_t i = 0; i < end; i++) { |
5302 | 232M | if (entries[i].ssrc == ssrc) { |
5303 | 124k | return entries[i].stream; |
5304 | 124k | } |
5305 | 232M | } |
5306 | | |
5307 | 91.7k | return NULL; |
5308 | 216k | } |
5309 | | |
5310 | | void srtp_stream_list_for_each(srtp_stream_list_t list, |
5311 | | bool (*callback)(srtp_stream_t, void *), |
5312 | | void *data) |
5313 | 122k | { |
5314 | 122k | list_entry *entries = list->entries; |
5315 | | |
5316 | 122k | size_t size = list->size; |
5317 | | |
5318 | | /* |
5319 | | * the second statement of the expression needs to be recalculated on each |
5320 | | * iteration as the available number of entries may change within the given |
5321 | | * callback. |
5322 | | * Ie: in case the callback calls srtp_stream_list_remove(). |
5323 | | */ |
5324 | 236M | for (size_t i = 0; i < list->size;) { |
5325 | 236M | if (!callback(entries[i].stream, data)) { |
5326 | 0 | break; |
5327 | 0 | } |
5328 | | |
5329 | | // the entry was not removed, increase the counter. |
5330 | 236M | if (size == list->size) { |
5331 | 236M | ++i; |
5332 | 236M | } |
5333 | | |
5334 | 236M | size = list->size; |
5335 | 236M | } |
5336 | 122k | } |
5337 | | |
5338 | | #endif |