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