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