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