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