Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * srtp.c |
| 3 | * |
| 4 | * the secure real-time transport protocol |
| 5 | * |
| 6 | * David A. McGrew |
| 7 | * Cisco Systems, Inc. |
| 8 | */ |
| 9 | /* |
| 10 | * |
David McGrew | 7629bf2 | 2006-06-08 17:00:25 +0000 | [diff] [blame] | 11 | * Copyright (c) 2001-2006, Cisco Systems, Inc. |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 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 | |
jfigus | a9ac898 | 2014-10-31 14:49:31 -0400 | [diff] [blame] | 45 | #include "srtp.h" |
Teerapap Changwichukarn | 6cffe24 | 2014-09-24 11:24:07 +0800 | [diff] [blame] | 46 | #include "srtp_priv.h" |
jfigus | a9ac898 | 2014-10-31 14:49:31 -0400 | [diff] [blame] | 47 | #include "crypto_types.h" |
| 48 | #include "err.h" |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 49 | #include "ekt.h" /* for SRTP Encrypted Key Transport */ |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 50 | #include "alloc.h" /* for srtp_crypto_alloc() */ |
jfigus | 8719f95 | 2014-04-08 09:15:49 -0400 | [diff] [blame] | 51 | #ifdef OPENSSL |
| 52 | #include "aes_gcm_ossl.h" /* for AES GCM mode */ |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 53 | # ifdef OPENSSL_KDF |
| 54 | # include <openssl/kdf.h> |
| 55 | # include "aes_icm_ossl.h" /* for AES GCM mode */ |
| 56 | # endif |
jfigus | 8719f95 | 2014-04-08 09:15:49 -0400 | [diff] [blame] | 57 | #endif |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 58 | |
jfigus | a6cf208 | 2014-11-21 10:04:03 -0500 | [diff] [blame] | 59 | #include <limits.h> |
| 60 | #ifdef HAVE_NETINET_IN_H |
| 61 | # include <netinet/in.h> |
| 62 | #elif defined(HAVE_WINSOCK2_H) |
| 63 | # include <winsock2.h> |
| 64 | #endif |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 65 | |
| 66 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 67 | /* the debug module for srtp */ |
| 68 | |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 69 | srtp_debug_module_t mod_srtp = { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 70 | 0, /* debugging is off by default */ |
| 71 | "srtp" /* printable name for module */ |
| 72 | }; |
| 73 | |
| 74 | #define octets_in_rtp_header 12 |
| 75 | #define uint32s_in_rtp_header 3 |
| 76 | #define octets_in_rtcp_header 8 |
| 77 | #define uint32s_in_rtcp_header 2 |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 78 | #define octets_in_rtp_extn_hdr 4 |
| 79 | |
jfigus | 724bb29 | 2015-02-19 06:43:53 -0500 | [diff] [blame] | 80 | static srtp_err_status_t |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 81 | srtp_validate_rtp_header(void *rtp_hdr, int *pkt_octet_len) { |
| 82 | srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr; |
| 83 | |
| 84 | /* Check RTP header length */ |
| 85 | int rtp_header_len = octets_in_rtp_header + 4 * hdr->cc; |
| 86 | if (hdr->x == 1) |
| 87 | rtp_header_len += octets_in_rtp_extn_hdr; |
| 88 | |
| 89 | if (*pkt_octet_len < rtp_header_len) |
jfigus | 724bb29 | 2015-02-19 06:43:53 -0500 | [diff] [blame] | 90 | return srtp_err_status_bad_param; |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 91 | |
| 92 | /* Verifing profile length. */ |
| 93 | if (hdr->x == 1) { |
| 94 | srtp_hdr_xtnd_t *xtn_hdr = |
| 95 | (srtp_hdr_xtnd_t *)((uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc); |
| 96 | int profile_len = ntohs(xtn_hdr->length); |
| 97 | rtp_header_len += profile_len * 4; |
| 98 | /* profile length counts the number of 32-bit words */ |
| 99 | if (*pkt_octet_len < rtp_header_len) |
jfigus | 724bb29 | 2015-02-19 06:43:53 -0500 | [diff] [blame] | 100 | return srtp_err_status_bad_param; |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 101 | } |
jfigus | 724bb29 | 2015-02-19 06:43:53 -0500 | [diff] [blame] | 102 | return srtp_err_status_ok; |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 103 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 104 | |
Christian Oien | d4e3eec | 2014-10-24 10:14:08 +0200 | [diff] [blame] | 105 | const char *srtp_get_version_string () |
jfigus | f62b64d | 2014-10-08 13:53:57 -0400 | [diff] [blame] | 106 | { |
| 107 | /* |
| 108 | * Simply return the autotools generated string |
| 109 | */ |
| 110 | return SRTP_VER_STRING; |
| 111 | } |
| 112 | |
| 113 | unsigned int srtp_get_version () |
| 114 | { |
| 115 | unsigned int major = 0, minor = 0, micro = 0; |
| 116 | unsigned int rv = 0; |
jfigus | b2edbef | 2014-10-13 10:15:15 -0400 | [diff] [blame] | 117 | int parse_rv; |
jfigus | f62b64d | 2014-10-08 13:53:57 -0400 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Parse the autotools generated version |
| 121 | */ |
jfigus | b2edbef | 2014-10-13 10:15:15 -0400 | [diff] [blame] | 122 | parse_rv = sscanf(SRTP_VERSION, "%u.%u.%u", &major, &minor, µ); |
| 123 | if (parse_rv != 3) { |
| 124 | /* |
| 125 | * We're expected to parse all 3 version levels. |
| 126 | * If not, then this must not be an official release. |
| 127 | * Return all zeros on the version |
| 128 | */ |
| 129 | return (0); |
| 130 | } |
jfigus | f62b64d | 2014-10-08 13:53:57 -0400 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * We allow 8 bits for the major and minor, while |
| 134 | * allowing 16 bits for the micro. 16 bits for the micro |
| 135 | * may be beneficial for a continuous delivery model |
| 136 | * in the future. |
| 137 | */ |
| 138 | rv |= (major & 0xFF) << 24; |
| 139 | rv |= (minor & 0xFF) << 16; |
| 140 | rv |= micro & 0xFF; |
| 141 | return rv; |
| 142 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 143 | |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 144 | /* Release (maybe partially allocated) stream. */ |
jfigus | 8494490 | 2015-12-15 07:41:16 -0500 | [diff] [blame] | 145 | static void |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 146 | srtp_stream_free(srtp_stream_ctx_t *str) { |
| 147 | if (str->rtp_xtn_hdr_cipher) { |
| 148 | srtp_cipher_dealloc(str->rtp_xtn_hdr_cipher); |
| 149 | } |
| 150 | if (str->enc_xtn_hdr) { |
| 151 | srtp_crypto_free(str->enc_xtn_hdr); |
| 152 | } |
| 153 | if (str->rtcp_auth) { |
| 154 | auth_dealloc(str->rtcp_auth); |
| 155 | } |
| 156 | if (str->rtcp_cipher) { |
| 157 | srtp_cipher_dealloc(str->rtcp_cipher); |
| 158 | } |
| 159 | if (str->limit) { |
| 160 | srtp_crypto_free(str->limit); |
| 161 | } |
| 162 | if (str->rtp_auth) { |
| 163 | auth_dealloc(str->rtp_auth); |
| 164 | } |
| 165 | if (str->rtp_cipher) { |
| 166 | srtp_cipher_dealloc(str->rtp_cipher); |
| 167 | } |
| 168 | srtp_crypto_free(str); |
| 169 | } |
| 170 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 171 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 172 | srtp_stream_alloc(srtp_stream_ctx_t **str_ptr, |
| 173 | const srtp_policy_t *p) { |
| 174 | srtp_stream_ctx_t *str; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 175 | srtp_err_status_t stat; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * This function allocates the stream context, rtp and rtcp ciphers |
| 179 | * and auth functions, and key limit structure. If there is a |
| 180 | * failure during allocation, we free all previously allocated |
| 181 | * memory and return a failure code. The code could probably |
| 182 | * be improved, but it works and should be clear. |
| 183 | */ |
| 184 | |
| 185 | /* allocate srtp stream and set str_ptr */ |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 186 | str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 187 | if (str == NULL) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 188 | return srtp_err_status_alloc_fail; |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 189 | |
| 190 | memset(str, 0, sizeof(srtp_stream_ctx_t)); |
| 191 | *str_ptr = str; |
| 192 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 193 | /* allocate cipher */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 194 | stat = srtp_crypto_kernel_alloc_cipher(p->rtp.cipher_type, |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 195 | &str->rtp_cipher, |
jfigus | c13c100 | 2014-05-08 13:34:53 -0400 | [diff] [blame] | 196 | p->rtp.cipher_key_len, |
| 197 | p->rtp.auth_tag_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 198 | if (stat) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 199 | srtp_stream_free(str); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 200 | return stat; |
| 201 | } |
| 202 | |
| 203 | /* allocate auth function */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 204 | stat = srtp_crypto_kernel_alloc_auth(p->rtp.auth_type, |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 205 | &str->rtp_auth, |
| 206 | p->rtp.auth_key_len, |
| 207 | p->rtp.auth_tag_len); |
| 208 | if (stat) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 209 | srtp_stream_free(str); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 210 | return stat; |
| 211 | } |
| 212 | |
| 213 | /* allocate key limit structure */ |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 214 | str->limit = (srtp_key_limit_ctx_t*) srtp_crypto_alloc(sizeof(srtp_key_limit_ctx_t)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 215 | if (str->limit == NULL) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 216 | srtp_stream_free(str); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 217 | return srtp_err_status_alloc_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
| 221 | * ...and now the RTCP-specific initialization - first, allocate |
| 222 | * the cipher |
| 223 | */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 224 | stat = srtp_crypto_kernel_alloc_cipher(p->rtcp.cipher_type, |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 225 | &str->rtcp_cipher, |
jfigus | c13c100 | 2014-05-08 13:34:53 -0400 | [diff] [blame] | 226 | p->rtcp.cipher_key_len, |
| 227 | p->rtcp.auth_tag_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 228 | if (stat) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 229 | srtp_stream_free(str); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 230 | return stat; |
| 231 | } |
| 232 | |
| 233 | /* allocate auth function */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 234 | stat = srtp_crypto_kernel_alloc_auth(p->rtcp.auth_type, |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 235 | &str->rtcp_auth, |
| 236 | p->rtcp.auth_key_len, |
| 237 | p->rtcp.auth_tag_len); |
| 238 | if (stat) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 239 | srtp_stream_free(str); |
| 240 | return stat; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 241 | } |
| 242 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 243 | /* allocate ekt data associated with stream */ |
jfigus | c5887e7 | 2014-11-06 09:46:18 -0500 | [diff] [blame] | 244 | stat = srtp_ekt_alloc(&str->ekt, p->ekt); |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 245 | if (stat) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 246 | srtp_stream_free(str); |
| 247 | return stat; |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 250 | if (p->enc_xtn_hdr && p->enc_xtn_hdr_count > 0) { |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 251 | srtp_cipher_type_id_t enc_xtn_hdr_cipher_type; |
| 252 | int enc_xtn_hdr_cipher_key_len; |
| 253 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 254 | str->enc_xtn_hdr = (int*) srtp_crypto_alloc(p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0])); |
| 255 | if (!str->enc_xtn_hdr) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 256 | srtp_stream_free(str); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 257 | return srtp_err_status_alloc_fail; |
| 258 | } |
| 259 | memcpy(str->enc_xtn_hdr, p->enc_xtn_hdr, p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0])); |
| 260 | str->enc_xtn_hdr_count = p->enc_xtn_hdr_count; |
| 261 | |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 262 | /* For GCM ciphers, the corresponding ICM cipher is used for header extensions encryption. */ |
| 263 | switch (p->rtp.cipher_type) { |
| 264 | case SRTP_AES_128_GCM: |
| 265 | enc_xtn_hdr_cipher_type = SRTP_AES_128_ICM; |
| 266 | enc_xtn_hdr_cipher_key_len = 30; |
| 267 | break; |
| 268 | case SRTP_AES_256_GCM: |
| 269 | enc_xtn_hdr_cipher_type = SRTP_AES_256_ICM; |
| 270 | enc_xtn_hdr_cipher_key_len = 46; |
| 271 | break; |
| 272 | default: |
| 273 | enc_xtn_hdr_cipher_type = p->rtp.cipher_type; |
| 274 | enc_xtn_hdr_cipher_key_len = p->rtp.cipher_key_len; |
| 275 | break; |
| 276 | } |
| 277 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 278 | /* allocate cipher for extensions header encryption */ |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 279 | stat = srtp_crypto_kernel_alloc_cipher(enc_xtn_hdr_cipher_type, |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 280 | &str->rtp_xtn_hdr_cipher, |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 281 | enc_xtn_hdr_cipher_key_len, |
| 282 | 0); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 283 | if (stat) { |
Joachim Bauch | f3965b4 | 2015-12-14 21:30:11 +0100 | [diff] [blame] | 284 | srtp_stream_free(str); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 285 | return stat; |
| 286 | } |
| 287 | } else { |
| 288 | str->rtp_xtn_hdr_cipher = NULL; |
| 289 | str->enc_xtn_hdr = NULL; |
| 290 | str->enc_xtn_hdr_count = 0; |
| 291 | } |
| 292 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 293 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 296 | srtp_err_status_t |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 297 | srtp_stream_dealloc(srtp_stream_ctx_t *stream, srtp_stream_ctx_t *stream_template) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 298 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * we use a conservative deallocation strategy - if any deallocation |
| 302 | * fails, then we report that fact without trying to deallocate |
| 303 | * anything else |
| 304 | */ |
| 305 | |
| 306 | /* deallocate cipher, if it is not the same as that in template */ |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 307 | if (stream_template |
| 308 | && stream->rtp_cipher == stream_template->rtp_cipher) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 309 | /* do nothing */ |
| 310 | } else { |
jfigus | 3f93c3c | 2014-12-01 15:38:09 -0500 | [diff] [blame] | 311 | status = srtp_cipher_dealloc(stream->rtp_cipher); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 312 | if (status) |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 313 | return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* deallocate auth function, if it is not the same as that in template */ |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 317 | if (stream_template |
| 318 | && stream->rtp_auth == stream_template->rtp_auth) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 319 | /* do nothing */ |
| 320 | } else { |
| 321 | status = auth_dealloc(stream->rtp_auth); |
| 322 | if (status) |
| 323 | return status; |
| 324 | } |
| 325 | |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 326 | /* deallocate key usage limit, if it is not the same as that in template */ |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 327 | if (stream_template |
| 328 | && stream->limit == stream_template->limit) { |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 329 | /* do nothing */ |
| 330 | } else { |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 331 | srtp_crypto_free(stream->limit); |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 332 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 333 | |
jfigus | fb0bd87 | 2015-12-14 09:25:42 -0500 | [diff] [blame] | 334 | if (stream_template |
| 335 | && stream->rtp_xtn_hdr_cipher == stream_template->rtp_xtn_hdr_cipher) { |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 336 | /* do nothing */ |
| 337 | } else if (stream->rtp_xtn_hdr_cipher) { |
| 338 | status = srtp_cipher_dealloc(stream->rtp_xtn_hdr_cipher); |
| 339 | if (status) |
| 340 | return status; |
| 341 | } |
| 342 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 343 | /* |
| 344 | * deallocate rtcp cipher, if it is not the same as that in |
| 345 | * template |
| 346 | */ |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 347 | if (stream_template |
| 348 | && stream->rtcp_cipher == stream_template->rtcp_cipher) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 349 | /* do nothing */ |
| 350 | } else { |
jfigus | 3f93c3c | 2014-12-01 15:38:09 -0500 | [diff] [blame] | 351 | status = srtp_cipher_dealloc(stream->rtcp_cipher); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 352 | if (status) |
| 353 | return status; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * deallocate rtcp auth function, if it is not the same as that in |
| 358 | * template |
| 359 | */ |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 360 | if (stream_template |
| 361 | && stream->rtcp_auth == stream_template->rtcp_auth) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 362 | /* do nothing */ |
| 363 | } else { |
| 364 | status = auth_dealloc(stream->rtcp_auth); |
| 365 | if (status) |
| 366 | return status; |
| 367 | } |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 368 | |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 369 | status = srtp_rdbx_dealloc(&stream->rtp_rdbx); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 370 | if (status) |
| 371 | return status; |
| 372 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 373 | /* DAM - need to deallocate EKT here */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 374 | |
jfigus | fb0bd87 | 2015-12-14 09:25:42 -0500 | [diff] [blame] | 375 | if (stream_template |
| 376 | && stream->enc_xtn_hdr == stream_template->enc_xtn_hdr) { |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 377 | /* do nothing */ |
| 378 | } else if (stream->enc_xtn_hdr) { |
| 379 | srtp_crypto_free(stream->enc_xtn_hdr); |
| 380 | } |
| 381 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 382 | /* |
| 383 | * zeroize the salt value |
| 384 | */ |
| 385 | memset(stream->salt, 0, SRTP_AEAD_SALT_LEN); |
| 386 | memset(stream->c_salt, 0, SRTP_AEAD_SALT_LEN); |
| 387 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 388 | |
| 389 | /* deallocate srtp stream context */ |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 390 | srtp_crypto_free(stream); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 391 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 392 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | /* |
| 397 | * srtp_stream_clone(stream_template, new) allocates a new stream and |
| 398 | * initializes it using the cipher and auth of the stream_template |
| 399 | * |
| 400 | * the only unique data in a cloned stream is the replay database and |
| 401 | * the SSRC |
| 402 | */ |
| 403 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 404 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 405 | srtp_stream_clone(const srtp_stream_ctx_t *stream_template, |
| 406 | uint32_t ssrc, |
| 407 | srtp_stream_ctx_t **str_ptr) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 408 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 409 | srtp_stream_ctx_t *str; |
| 410 | |
| 411 | debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ssrc); |
| 412 | |
| 413 | /* allocate srtp stream and set str_ptr */ |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 414 | str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 415 | if (str == NULL) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 416 | return srtp_err_status_alloc_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 417 | *str_ptr = str; |
| 418 | |
| 419 | /* set cipher and auth pointers to those of the template */ |
| 420 | str->rtp_cipher = stream_template->rtp_cipher; |
| 421 | str->rtp_auth = stream_template->rtp_auth; |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 422 | str->rtp_xtn_hdr_cipher = stream_template->rtp_xtn_hdr_cipher; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 423 | str->rtcp_cipher = stream_template->rtcp_cipher; |
| 424 | str->rtcp_auth = stream_template->rtcp_auth; |
| 425 | |
| 426 | /* set key limit to point to that of the template */ |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 427 | status = srtp_key_limit_clone(stream_template->limit, &str->limit); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 428 | if (status) { |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 429 | srtp_crypto_free(*str_ptr); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 430 | *str_ptr = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 431 | return status; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 432 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 433 | |
| 434 | /* initialize replay databases */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 435 | status = srtp_rdbx_init(&str->rtp_rdbx, |
| 436 | srtp_rdbx_get_window_size(&stream_template->rtp_rdbx)); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 437 | if (status) { |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 438 | srtp_crypto_free(*str_ptr); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 439 | *str_ptr = NULL; |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 440 | return status; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 441 | } |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 442 | srtp_rdb_init(&str->rtcp_rdb); |
Jonathan Lennox | dcee5c6 | 2010-05-17 22:08:40 +0000 | [diff] [blame] | 443 | str->allow_repeat_tx = stream_template->allow_repeat_tx; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 444 | |
| 445 | /* set ssrc to that provided */ |
| 446 | str->ssrc = ssrc; |
| 447 | |
| 448 | /* set direction and security services */ |
| 449 | str->direction = stream_template->direction; |
| 450 | str->rtp_services = stream_template->rtp_services; |
| 451 | str->rtcp_services = stream_template->rtcp_services; |
| 452 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 453 | /* set pointer to EKT data associated with stream */ |
| 454 | str->ekt = stream_template->ekt; |
| 455 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 456 | /* Copy the salt values */ |
| 457 | memcpy(str->salt, stream_template->salt, SRTP_AEAD_SALT_LEN); |
| 458 | memcpy(str->c_salt, stream_template->c_salt, SRTP_AEAD_SALT_LEN); |
| 459 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 460 | /* copy information about extensions header encryption */ |
| 461 | str->enc_xtn_hdr = stream_template->enc_xtn_hdr; |
| 462 | str->enc_xtn_hdr_count = stream_template->enc_xtn_hdr_count; |
| 463 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 464 | /* defensive coding */ |
| 465 | str->next = NULL; |
| 466 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 467 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | |
| 471 | /* |
| 472 | * key derivation functions, internal to libSRTP |
| 473 | * |
| 474 | * srtp_kdf_t is a key derivation context |
| 475 | * |
Jonathan Lennox | 953f46f | 2010-05-18 16:35:09 +0000 | [diff] [blame] | 476 | * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher |
| 477 | * described by cipher_id, with the master key k with length in octets keylen. |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 478 | * |
| 479 | * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key |
| 480 | * corresponding to label l and puts it into kl; the length |
| 481 | * of the key in octets is provided as keylen. this function |
| 482 | * should be called once for each subkey that is derived. |
| 483 | * |
Jonathan Lennox | 953f46f | 2010-05-18 16:35:09 +0000 | [diff] [blame] | 484 | * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 485 | */ |
| 486 | |
| 487 | typedef enum { |
| 488 | label_rtp_encryption = 0x00, |
| 489 | label_rtp_msg_auth = 0x01, |
| 490 | label_rtp_salt = 0x02, |
| 491 | label_rtcp_encryption = 0x03, |
| 492 | label_rtcp_msg_auth = 0x04, |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 493 | label_rtcp_salt = 0x05, |
| 494 | label_rtp_header_encryption = 0x06, |
| 495 | label_rtp_header_salt = 0x07 |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 496 | } srtp_prf_label; |
| 497 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 498 | #define MAX_SRTP_KEY_LEN 256 |
| 499 | |
| 500 | #if defined(OPENSSL) && defined(OPENSSL_KDF) |
| 501 | #define MAX_SRTP_AESKEY_LEN 32 |
| 502 | #define MAX_SRTP_SALT_LEN 14 |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 503 | |
| 504 | /* |
| 505 | * srtp_kdf_t represents a key derivation function. The SRTP |
| 506 | * default KDF is the only one implemented at present. |
| 507 | */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 508 | typedef struct { |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 509 | uint8_t master_key[MAX_SRTP_AESKEY_LEN]; |
| 510 | uint8_t master_salt[MAX_SRTP_SALT_LEN]; |
| 511 | const EVP_CIPHER *evp; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 512 | } srtp_kdf_t; |
| 513 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 514 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 515 | static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, const uint8_t *key, int key_len, int salt_len) |
| 516 | { |
| 517 | memset(kdf, 0x0, sizeof(srtp_kdf_t)); |
Jonathan Lennox | 953f46f | 2010-05-18 16:35:09 +0000 | [diff] [blame] | 518 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 519 | /* The NULL cipher has zero key length */ |
| 520 | if (key_len == 0) return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 521 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 522 | if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) { |
| 523 | return srtp_err_status_bad_param; |
| 524 | } |
| 525 | switch (key_len) { |
| 526 | case SRTP_AES_256_KEYSIZE: |
| 527 | kdf->evp = EVP_aes_256_ctr(); |
| 528 | break; |
| 529 | case SRTP_AES_192_KEYSIZE: |
| 530 | kdf->evp = EVP_aes_192_ctr(); |
| 531 | break; |
| 532 | case SRTP_AES_128_KEYSIZE: |
| 533 | kdf->evp = EVP_aes_128_ctr(); |
| 534 | break; |
| 535 | default: |
| 536 | return srtp_err_status_bad_param; |
| 537 | break; |
| 538 | } |
| 539 | memcpy(kdf->master_key, key, key_len); |
| 540 | memcpy(kdf->master_salt, key+key_len, salt_len); |
| 541 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 542 | } |
| 543 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 544 | static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length) |
| 545 | { |
| 546 | int ret; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 547 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 548 | /* The NULL cipher will not have an EVP */ |
| 549 | if (!kdf->evp) return srtp_err_status_ok; |
| 550 | |
| 551 | octet_string_set_to_zero(key, length); |
| 552 | |
| 553 | /* |
| 554 | * Invoke the OpenSSL SRTP KDF function |
| 555 | * This is useful if OpenSSL is in FIPS mode and FIP |
| 556 | * compliance is required for SRTP. |
| 557 | */ |
| 558 | ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key, (char *)&kdf->master_salt, NULL, NULL, label, (char *)key); |
| 559 | if (ret == -1) { |
| 560 | return (srtp_err_status_algo_fail); |
| 561 | } |
| 562 | |
| 563 | return srtp_err_status_ok; |
| 564 | } |
| 565 | |
| 566 | static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) { |
Pascal Bühler | 9bdcfc7 | 2015-10-07 16:55:52 +0200 | [diff] [blame] | 567 | memset(kdf->master_key, 0x0, MAX_SRTP_AESKEY_LEN); |
| 568 | memset(kdf->master_salt, 0x0, MAX_SRTP_SALT_LEN); |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 569 | kdf->evp = NULL; |
| 570 | |
| 571 | return srtp_err_status_ok; |
| 572 | } |
| 573 | |
| 574 | #else /* if OPENSSL_KDF */ |
| 575 | |
| 576 | /* |
| 577 | * srtp_kdf_t represents a key derivation function. The SRTP |
| 578 | * default KDF is the only one implemented at present. |
| 579 | */ |
| 580 | typedef struct { |
| 581 | srtp_cipher_t *cipher; /* cipher used for key derivation */ |
| 582 | } srtp_kdf_t; |
| 583 | |
| 584 | static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, srtp_cipher_type_id_t cipher_id, const uint8_t *key, int length) |
| 585 | { |
| 586 | srtp_err_status_t stat; |
| 587 | stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, length, 0); |
| 588 | if (stat) return stat; |
| 589 | |
| 590 | stat = srtp_cipher_init(kdf->cipher, key); |
| 591 | if (stat) { |
| 592 | srtp_cipher_dealloc(kdf->cipher); |
| 593 | return stat; |
| 594 | } |
| 595 | return srtp_err_status_ok; |
| 596 | } |
| 597 | |
| 598 | static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length) |
| 599 | { |
| 600 | srtp_err_status_t status; |
| 601 | v128_t nonce; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 602 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 603 | /* set eigth octet of nonce to <label>, set the rest of it to zero */ |
| 604 | v128_set_to_zero(&nonce); |
| 605 | nonce.v8[7] = label; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 606 | |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 607 | status = srtp_cipher_set_iv(kdf->cipher, (uint8_t*)&nonce, direction_encrypt); |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 608 | if (status) return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 609 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 610 | /* generate keystream output */ |
| 611 | octet_string_set_to_zero(key, length); |
| 612 | status = srtp_cipher_encrypt(kdf->cipher, key, &length); |
| 613 | if (status) return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 614 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 615 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 616 | } |
| 617 | |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 618 | static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) { |
| 619 | srtp_err_status_t status; |
| 620 | status = srtp_cipher_dealloc(kdf->cipher); |
| 621 | if (status) return status; |
| 622 | kdf->cipher = NULL; |
| 623 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 624 | } |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 625 | #endif /* else OPENSSL_KDF */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * end of key derivation functions |
| 629 | */ |
| 630 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 631 | |
| 632 | |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 633 | /* Get the base key length corresponding to a given combined key+salt |
| 634 | * length for the given cipher. |
| 635 | * Assumption is that for AES-ICM a key length < 30 is Ismacryp using |
| 636 | * AES-128 and short salts; everything else uses a salt length of 14. |
| 637 | * TODO: key and salt lengths should be separate fields in the policy. */ |
jfigus | 9a84043 | 2014-11-19 15:48:21 -0500 | [diff] [blame] | 638 | static inline int base_key_length(const srtp_cipher_type_t *cipher, int key_length) |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 639 | { |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 640 | switch (cipher->id) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 641 | case SRTP_AES_128_ICM: |
| 642 | case SRTP_AES_192_ICM: |
| 643 | case SRTP_AES_256_ICM: |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 644 | /* The legacy modes are derived from |
| 645 | * the configured key length on the policy */ |
| 646 | return key_length - 14; |
| 647 | break; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 648 | case SRTP_AES_128_GCM: |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 649 | return 16; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 650 | break; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 651 | case SRTP_AES_256_GCM: |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 652 | return 32; |
| 653 | break; |
| 654 | default: |
| 655 | return key_length; |
| 656 | break; |
| 657 | } |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 658 | } |
| 659 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 660 | srtp_err_status_t |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 661 | srtp_stream_init_keys(srtp_stream_ctx_t *srtp, const void *key) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 662 | srtp_err_status_t stat; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 663 | srtp_kdf_t kdf; |
| 664 | uint8_t tmp_key[MAX_SRTP_KEY_LEN]; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 665 | int kdf_keylen = 30, rtp_keylen, rtcp_keylen; |
| 666 | int rtp_base_key_len, rtp_salt_len; |
| 667 | int rtcp_base_key_len, rtcp_salt_len; |
| 668 | |
| 669 | /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */ |
| 670 | /* TODO: kdf algorithm, master key length, and master salt length should |
| 671 | * be part of srtp_policy_t. */ |
jfigus | 9a84043 | 2014-11-19 15:48:21 -0500 | [diff] [blame] | 672 | rtp_keylen = srtp_cipher_get_key_length(srtp->rtp_cipher); |
| 673 | rtcp_keylen = srtp_cipher_get_key_length(srtp->rtcp_cipher); |
jfigus | 8719f95 | 2014-04-08 09:15:49 -0400 | [diff] [blame] | 674 | rtp_base_key_len = base_key_length(srtp->rtp_cipher->type, rtp_keylen); |
| 675 | rtp_salt_len = rtp_keylen - rtp_base_key_len; |
| 676 | |
| 677 | if (rtp_keylen > kdf_keylen) { |
| 678 | kdf_keylen = 46; /* AES-CTR mode is always used for KDF */ |
| 679 | } |
| 680 | |
| 681 | if (rtcp_keylen > kdf_keylen) { |
| 682 | kdf_keylen = 46; /* AES-CTR mode is always used for KDF */ |
| 683 | } |
| 684 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 685 | debug_print(mod_srtp, "srtp key len: %d", rtp_keylen); |
| 686 | debug_print(mod_srtp, "srtcp key len: %d", rtcp_keylen); |
jfigus | 8719f95 | 2014-04-08 09:15:49 -0400 | [diff] [blame] | 687 | debug_print(mod_srtp, "base key len: %d", rtp_base_key_len); |
| 688 | debug_print(mod_srtp, "kdf key len: %d", kdf_keylen); |
| 689 | debug_print(mod_srtp, "rtp salt len: %d", rtp_salt_len); |
| 690 | |
| 691 | /* |
| 692 | * Make sure the key given to us is 'zero' appended. GCM |
| 693 | * mode uses a shorter master SALT (96 bits), but still relies on |
| 694 | * the legacy CTR mode KDF, which uses a 112 bit master SALT. |
| 695 | */ |
| 696 | memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN); |
| 697 | memcpy(tmp_key, key, (rtp_base_key_len + rtp_salt_len)); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 698 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 699 | /* initialize KDF state */ |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 700 | #if defined(OPENSSL) && defined(OPENSSL_KDF) |
| 701 | stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, rtp_base_key_len, rtp_salt_len); |
| 702 | #else |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 703 | stat = srtp_kdf_init(&kdf, SRTP_AES_ICM, (const uint8_t *)tmp_key, kdf_keylen); |
jfigus | 038d2cf | 2015-05-11 14:10:11 -0400 | [diff] [blame] | 704 | #endif |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 705 | if (stat) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 706 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 707 | } |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 708 | |
| 709 | /* generate encryption key */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 710 | stat = srtp_kdf_generate(&kdf, label_rtp_encryption, |
| 711 | tmp_key, rtp_base_key_len); |
| 712 | if (stat) { |
| 713 | /* zeroize temp buffer */ |
| 714 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 715 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 716 | } |
jfigus | 8719f95 | 2014-04-08 09:15:49 -0400 | [diff] [blame] | 717 | debug_print(mod_srtp, "cipher key: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 718 | srtp_octet_string_hex_string(tmp_key, rtp_base_key_len)); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 719 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 720 | /* |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 721 | * if the cipher in the srtp context uses a salt, then we need |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 722 | * to generate the salt value |
| 723 | */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 724 | if (rtp_salt_len > 0) { |
| 725 | debug_print(mod_srtp, "found rtp_salt_len > 0, generating salt", NULL); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 726 | |
| 727 | /* generate encryption salt, put after encryption key */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 728 | stat = srtp_kdf_generate(&kdf, label_rtp_salt, |
| 729 | tmp_key + rtp_base_key_len, rtp_salt_len); |
| 730 | if (stat) { |
| 731 | /* zeroize temp buffer */ |
| 732 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 733 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 734 | } |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 735 | memcpy(srtp->salt, tmp_key + rtp_base_key_len, SRTP_AEAD_SALT_LEN); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 736 | } |
Jonathan Lennox | c0f1f1b | 2012-04-26 23:16:00 +0000 | [diff] [blame] | 737 | if (rtp_salt_len > 0) { |
| 738 | debug_print(mod_srtp, "cipher salt: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 739 | srtp_octet_string_hex_string(tmp_key + rtp_base_key_len, rtp_salt_len)); |
Jonathan Lennox | c0f1f1b | 2012-04-26 23:16:00 +0000 | [diff] [blame] | 740 | } |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 741 | |
| 742 | /* initialize cipher */ |
jfigus | 3f93c3c | 2014-12-01 15:38:09 -0500 | [diff] [blame] | 743 | stat = srtp_cipher_init(srtp->rtp_cipher, tmp_key); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 744 | if (stat) { |
| 745 | /* zeroize temp buffer */ |
| 746 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 747 | return srtp_err_status_init_fail; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 750 | if (srtp->rtp_xtn_hdr_cipher) { |
| 751 | /* generate extensions header encryption key */ |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 752 | int rtp_xtn_hdr_keylen; |
| 753 | int rtp_xtn_hdr_base_key_len; |
| 754 | int rtp_xtn_hdr_salt_len; |
| 755 | srtp_kdf_t tmp_kdf; |
| 756 | srtp_kdf_t *xtn_hdr_kdf; |
| 757 | |
| 758 | if (srtp->rtp_xtn_hdr_cipher->type != srtp->rtp_cipher->type) { |
| 759 | /* With GCM ciphers, the header extensions are still encrypted using the corresponding ICM cipher. */ |
| 760 | /* See https://tools.ietf.org/html/draft-ietf-avtcore-srtp-aes-gcm-17#section-8.3 */ |
| 761 | uint8_t tmp_xtn_hdr_key[MAX_SRTP_KEY_LEN]; |
| 762 | rtp_xtn_hdr_keylen = srtp_cipher_get_key_length(srtp->rtp_xtn_hdr_cipher); |
| 763 | rtp_xtn_hdr_base_key_len = base_key_length(srtp->rtp_xtn_hdr_cipher->type, rtp_xtn_hdr_keylen); |
| 764 | rtp_xtn_hdr_salt_len = rtp_xtn_hdr_keylen - rtp_xtn_hdr_base_key_len; |
| 765 | memset(tmp_xtn_hdr_key, 0x0, MAX_SRTP_KEY_LEN); |
| 766 | memcpy(tmp_xtn_hdr_key, key, (rtp_xtn_hdr_base_key_len + rtp_xtn_hdr_salt_len)); |
| 767 | xtn_hdr_kdf = &tmp_kdf; |
| 768 | |
| 769 | /* initialize KDF state */ |
| 770 | #if defined(OPENSSL) && defined(OPENSSL_KDF) |
| 771 | stat = srtp_kdf_init(xtn_hdr_kdf, (const uint8_t *)tmp_xtn_hdr_key, rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len); |
| 772 | #else |
| 773 | stat = srtp_kdf_init(xtn_hdr_kdf, SRTP_AES_ICM, (const uint8_t *)tmp_xtn_hdr_key, kdf_keylen); |
| 774 | #endif |
| 775 | octet_string_set_to_zero(tmp_xtn_hdr_key, MAX_SRTP_KEY_LEN); |
| 776 | if (stat) { |
| 777 | return srtp_err_status_init_fail; |
| 778 | } |
| 779 | } else { |
| 780 | /* Reuse main KDF. */ |
| 781 | rtp_xtn_hdr_keylen = rtp_keylen; |
| 782 | rtp_xtn_hdr_base_key_len = rtp_base_key_len; |
| 783 | rtp_xtn_hdr_salt_len = rtp_salt_len; |
| 784 | xtn_hdr_kdf = &kdf; |
| 785 | } |
| 786 | |
| 787 | stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_encryption, |
| 788 | tmp_key, rtp_xtn_hdr_base_key_len); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 789 | if (stat) { |
| 790 | /* zeroize temp buffer */ |
| 791 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
| 792 | return srtp_err_status_init_fail; |
| 793 | } |
| 794 | debug_print(mod_srtp, "extensions cipher key: %s", |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 795 | srtp_octet_string_hex_string(tmp_key, rtp_xtn_hdr_base_key_len)); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * if the cipher in the srtp context uses a salt, then we need |
| 799 | * to generate the salt value |
| 800 | */ |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 801 | if (rtp_xtn_hdr_salt_len > 0) { |
| 802 | debug_print(mod_srtp, "found rtp_xtn_hdr_salt_len > 0, generating salt", NULL); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 803 | |
| 804 | /* generate encryption salt, put after encryption key */ |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 805 | stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_salt, |
| 806 | tmp_key + rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 807 | if (stat) { |
| 808 | /* zeroize temp buffer */ |
| 809 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
| 810 | return srtp_err_status_init_fail; |
| 811 | } |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 812 | } |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 813 | if (rtp_xtn_hdr_salt_len > 0) { |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 814 | debug_print(mod_srtp, "extensions cipher salt: %s", |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 815 | srtp_octet_string_hex_string(tmp_key + rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len)); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* initialize extensions header cipher */ |
| 819 | stat = srtp_cipher_init(srtp->rtp_xtn_hdr_cipher, tmp_key); |
| 820 | if (stat) { |
| 821 | /* zeroize temp buffer */ |
| 822 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
| 823 | return srtp_err_status_init_fail; |
| 824 | } |
Joachim Bauch | 985769a | 2015-12-14 21:47:12 +0100 | [diff] [blame] | 825 | |
| 826 | if (xtn_hdr_kdf != &kdf) { |
| 827 | /* release memory for custom header extension encryption kdf */ |
| 828 | stat = srtp_kdf_clear(xtn_hdr_kdf); |
| 829 | if (stat) { |
| 830 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
| 831 | return srtp_err_status_init_fail; |
| 832 | } |
| 833 | } |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 834 | } |
| 835 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 836 | /* generate authentication key */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 837 | stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth, |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 838 | tmp_key, srtp_auth_get_key_length(srtp->rtp_auth)); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 839 | if (stat) { |
| 840 | /* zeroize temp buffer */ |
| 841 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 842 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 843 | } |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 844 | debug_print(mod_srtp, "auth key: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 845 | srtp_octet_string_hex_string(tmp_key, |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 846 | srtp_auth_get_key_length(srtp->rtp_auth))); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 847 | |
| 848 | /* initialize auth function */ |
| 849 | stat = auth_init(srtp->rtp_auth, tmp_key); |
| 850 | if (stat) { |
| 851 | /* zeroize temp buffer */ |
| 852 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 853 | return srtp_err_status_init_fail; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* |
| 857 | * ...now initialize SRTCP keys |
| 858 | */ |
| 859 | |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 860 | rtcp_base_key_len = base_key_length(srtp->rtcp_cipher->type, rtcp_keylen); |
| 861 | rtcp_salt_len = rtcp_keylen - rtcp_base_key_len; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 862 | debug_print(mod_srtp, "rtcp salt len: %d", rtcp_salt_len); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 863 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 864 | /* generate encryption key */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 865 | stat = srtp_kdf_generate(&kdf, label_rtcp_encryption, |
| 866 | tmp_key, rtcp_base_key_len); |
| 867 | if (stat) { |
| 868 | /* zeroize temp buffer */ |
| 869 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 870 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 871 | } |
| 872 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 873 | /* |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 874 | * if the cipher in the srtp context uses a salt, then we need |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 875 | * to generate the salt value |
| 876 | */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 877 | if (rtcp_salt_len > 0) { |
| 878 | debug_print(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt", |
| 879 | NULL); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 880 | |
| 881 | /* generate encryption salt, put after encryption key */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 882 | stat = srtp_kdf_generate(&kdf, label_rtcp_salt, |
| 883 | tmp_key + rtcp_base_key_len, rtcp_salt_len); |
| 884 | if (stat) { |
| 885 | /* zeroize temp buffer */ |
| 886 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 887 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 888 | } |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 889 | memcpy(srtp->c_salt, tmp_key + rtcp_base_key_len, SRTP_AEAD_SALT_LEN); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 890 | } |
| 891 | debug_print(mod_srtp, "rtcp cipher key: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 892 | srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len)); |
Jonathan Lennox | c0f1f1b | 2012-04-26 23:16:00 +0000 | [diff] [blame] | 893 | if (rtcp_salt_len > 0) { |
| 894 | debug_print(mod_srtp, "rtcp cipher salt: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 895 | srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len, rtcp_salt_len)); |
Jonathan Lennox | c0f1f1b | 2012-04-26 23:16:00 +0000 | [diff] [blame] | 896 | } |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 897 | |
| 898 | /* initialize cipher */ |
jfigus | 3f93c3c | 2014-12-01 15:38:09 -0500 | [diff] [blame] | 899 | stat = srtp_cipher_init(srtp->rtcp_cipher, tmp_key); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 900 | if (stat) { |
| 901 | /* zeroize temp buffer */ |
| 902 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 903 | return srtp_err_status_init_fail; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* generate authentication key */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 907 | stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth, |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 908 | tmp_key, srtp_auth_get_key_length(srtp->rtcp_auth)); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 909 | if (stat) { |
| 910 | /* zeroize temp buffer */ |
| 911 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 912 | return srtp_err_status_init_fail; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 913 | } |
| 914 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 915 | debug_print(mod_srtp, "rtcp auth key: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 916 | srtp_octet_string_hex_string(tmp_key, |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 917 | srtp_auth_get_key_length(srtp->rtcp_auth))); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 918 | |
| 919 | /* initialize auth function */ |
| 920 | stat = auth_init(srtp->rtcp_auth, tmp_key); |
| 921 | if (stat) { |
| 922 | /* zeroize temp buffer */ |
| 923 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 924 | return srtp_err_status_init_fail; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* clear memory then return */ |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 928 | stat = srtp_kdf_clear(&kdf); |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 929 | octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 930 | if (stat) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 931 | return srtp_err_status_init_fail; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 932 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 933 | return srtp_err_status_ok; |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 934 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 935 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 936 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 937 | srtp_stream_init(srtp_stream_ctx_t *srtp, |
| 938 | const srtp_policy_t *p) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 939 | srtp_err_status_t err; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 940 | |
| 941 | debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)", |
| 942 | p->ssrc.value); |
| 943 | |
| 944 | /* initialize replay database */ |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 945 | /* window size MUST be at least 64. MAY be larger. Values more than |
| 946 | * 2^15 aren't meaningful due to how extended sequence numbers are |
| 947 | * calculated. Let a window size of 0 imply the default value. */ |
| 948 | |
| 949 | if (p->window_size != 0 && (p->window_size < 64 || p->window_size >= 0x8000)) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 950 | return srtp_err_status_bad_param; |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 951 | |
| 952 | if (p->window_size != 0) |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 953 | err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 954 | else |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 955 | err = srtp_rdbx_init(&srtp->rtp_rdbx, 128); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 956 | if (err) return err; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 957 | |
| 958 | /* initialize key limit to maximum value */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 959 | #ifdef NO_64BIT_MATH |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 960 | { |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 961 | uint64_t temp; |
| 962 | temp = make64(UINT_MAX,UINT_MAX); |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 963 | srtp_key_limit_set(srtp->limit, temp); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 964 | } |
| 965 | #else |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 966 | srtp_key_limit_set(srtp->limit, 0xffffffffffffLL); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 967 | #endif |
| 968 | |
| 969 | /* set the SSRC value */ |
| 970 | srtp->ssrc = htonl(p->ssrc.value); |
| 971 | |
| 972 | /* set the security service flags */ |
| 973 | srtp->rtp_services = p->rtp.sec_serv; |
| 974 | srtp->rtcp_services = p->rtcp.sec_serv; |
| 975 | |
| 976 | /* |
| 977 | * set direction to unknown - this flag gets checked in srtp_protect(), |
| 978 | * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and |
| 979 | * gets set appropriately if it is set to unknown. |
| 980 | */ |
| 981 | srtp->direction = dir_unknown; |
| 982 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 983 | /* initialize SRTCP replay database */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 984 | srtp_rdb_init(&srtp->rtcp_rdb); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 985 | |
Jonathan Lennox | dcee5c6 | 2010-05-17 22:08:40 +0000 | [diff] [blame] | 986 | /* initialize allow_repeat_tx */ |
| 987 | /* guard against uninitialized memory: allow only 0 or 1 here */ |
| 988 | if (p->allow_repeat_tx != 0 && p->allow_repeat_tx != 1) { |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 989 | srtp_rdbx_dealloc(&srtp->rtp_rdbx); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 990 | return srtp_err_status_bad_param; |
Jonathan Lennox | dcee5c6 | 2010-05-17 22:08:40 +0000 | [diff] [blame] | 991 | } |
| 992 | srtp->allow_repeat_tx = p->allow_repeat_tx; |
| 993 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 994 | /* DAM - no RTCP key limit at present */ |
| 995 | |
David McGrew | 576e148 | 2006-06-09 21:47:44 +0000 | [diff] [blame] | 996 | /* initialize keys */ |
| 997 | err = srtp_stream_init_keys(srtp, p->key); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 998 | if (err) { |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 999 | srtp_rdbx_dealloc(&srtp->rtp_rdbx); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 1000 | return err; |
| 1001 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1002 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 1003 | /* |
| 1004 | * if EKT is in use, then initialize the EKT data associated with |
| 1005 | * the stream |
| 1006 | */ |
jfigus | c5887e7 | 2014-11-06 09:46:18 -0500 | [diff] [blame] | 1007 | err = srtp_ekt_stream_init_from_policy(srtp->ekt, p->ekt); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 1008 | if (err) { |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1009 | srtp_rdbx_dealloc(&srtp->rtp_rdbx); |
Jonathan Lennox | a1242f8 | 2010-05-17 21:46:04 +0000 | [diff] [blame] | 1010 | return err; |
| 1011 | } |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 1012 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1013 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | /* |
| 1018 | * srtp_event_reporter is an event handler function that merely |
| 1019 | * reports the events that are reported by the callbacks |
| 1020 | */ |
| 1021 | |
| 1022 | void |
| 1023 | srtp_event_reporter(srtp_event_data_t *data) { |
| 1024 | |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 1025 | srtp_err_report(srtp_err_level_warning, "srtp: in stream 0x%x: ", |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1026 | data->stream->ssrc); |
| 1027 | |
| 1028 | switch(data->event) { |
| 1029 | case event_ssrc_collision: |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 1030 | srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n"); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | case event_key_soft_limit: |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 1033 | srtp_err_report(srtp_err_level_warning, "\tkey usage soft limit reached\n"); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | case event_key_hard_limit: |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 1036 | srtp_err_report(srtp_err_level_warning, "\tkey usage hard limit reached\n"); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1037 | break; |
| 1038 | case event_packet_index_limit: |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 1039 | srtp_err_report(srtp_err_level_warning, "\tpacket index limit reached\n"); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | default: |
jfigus | 02d6f03 | 2014-11-21 10:56:42 -0500 | [diff] [blame] | 1042 | srtp_err_report(srtp_err_level_warning, "\tunknown event reported to handler\n"); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * srtp_event_handler is a global variable holding a pointer to the |
| 1048 | * event handler function; this function is called for any unexpected |
| 1049 | * event that needs to be handled out of the SRTP data path. see |
| 1050 | * srtp_event_t in srtp.h for more info |
| 1051 | * |
| 1052 | * it is okay to set srtp_event_handler to NULL, but we set |
| 1053 | * it to the srtp_event_reporter. |
| 1054 | */ |
| 1055 | |
| 1056 | static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter; |
| 1057 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1058 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1059 | srtp_install_event_handler(srtp_event_handler_func_t func) { |
| 1060 | |
| 1061 | /* |
| 1062 | * note that we accept NULL arguments intentionally - calling this |
| 1063 | * function with a NULL arguments removes an event handler that's |
| 1064 | * been previously installed |
| 1065 | */ |
| 1066 | |
| 1067 | /* set global event handling function */ |
| 1068 | srtp_event_handler = func; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1069 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1072 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1073 | /* |
| 1074 | * Check if the given extension header id is / should be encrypted. |
| 1075 | * Returns 1 if yes, otherwise 0. |
| 1076 | */ |
| 1077 | static int |
| 1078 | srtp_protect_extension_header(srtp_stream_ctx_t *stream, int id) { |
| 1079 | int* enc_xtn_hdr = stream->enc_xtn_hdr; |
| 1080 | int count = stream->enc_xtn_hdr_count; |
| 1081 | |
| 1082 | if (!enc_xtn_hdr || count <= 0) { |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | while (count > 0) { |
| 1087 | if (*enc_xtn_hdr == id) { |
| 1088 | return 1; |
| 1089 | } |
| 1090 | |
| 1091 | enc_xtn_hdr++; |
| 1092 | count--; |
| 1093 | } |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | /* |
| 1099 | * extensions header encryption RFC 6904 |
| 1100 | */ |
| 1101 | static srtp_err_status_t |
| 1102 | srtp_process_header_encryption(srtp_stream_ctx_t *stream, srtp_hdr_xtnd_t *xtn_hdr) { |
| 1103 | srtp_err_status_t status; |
| 1104 | uint8_t keystream[257]; /* Maximum 2 bytes header + 255 bytes data. */ |
| 1105 | int keystream_pos; |
| 1106 | uint8_t* xtn_hdr_data = ((uint8_t *)xtn_hdr) + octets_in_rtp_extn_hdr; |
| 1107 | uint8_t* xtn_hdr_end = xtn_hdr_data + (ntohs(xtn_hdr->length) * sizeof(uint32_t)); |
| 1108 | |
| 1109 | if (ntohs(xtn_hdr->profile_specific) == 0xbede) { |
| 1110 | /* RFC 5285, section 4.2. One-Byte Header */ |
| 1111 | while (xtn_hdr_data < xtn_hdr_end) { |
| 1112 | uint8_t xid = (*xtn_hdr_data & 0xf0) >> 4; |
| 1113 | unsigned int xlen = (*xtn_hdr_data & 0x0f) + 1; |
| 1114 | uint32_t xlen_with_header = 1+xlen; |
| 1115 | xtn_hdr_data++; |
| 1116 | |
Joachim Bauch | c1fc2ba | 2015-12-11 17:21:41 +0100 | [diff] [blame] | 1117 | if (xtn_hdr_data + xlen > xtn_hdr_end) |
| 1118 | return srtp_err_status_parse_err; |
| 1119 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1120 | if (xid == 15) { |
| 1121 | /* found header 15, stop further processing. */ |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | status = srtp_cipher_output(stream->rtp_xtn_hdr_cipher, keystream, &xlen_with_header); |
| 1126 | if (status) |
| 1127 | return srtp_err_status_cipher_fail; |
| 1128 | |
| 1129 | if (srtp_protect_extension_header(stream, xid)) { |
| 1130 | keystream_pos = 1; |
| 1131 | while (xlen > 0) { |
| 1132 | *xtn_hdr_data ^= keystream[keystream_pos++]; |
| 1133 | xtn_hdr_data++; |
| 1134 | xlen--; |
| 1135 | } |
| 1136 | } else { |
| 1137 | xtn_hdr_data += xlen; |
| 1138 | } |
| 1139 | |
| 1140 | /* skip padding bytes. */ |
| 1141 | while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) { |
| 1142 | xtn_hdr_data++; |
| 1143 | } |
| 1144 | } |
| 1145 | } else if ((ntohs(xtn_hdr->profile_specific) & 0x1fff) == 0x100) { |
| 1146 | /* RFC 5285, section 4.3. Two-Byte Header */ |
| 1147 | while (xtn_hdr_data + 1 < xtn_hdr_end) { |
| 1148 | uint8_t xid = *xtn_hdr_data; |
| 1149 | unsigned int xlen = *(xtn_hdr_data+1); |
| 1150 | uint32_t xlen_with_header = 2+xlen; |
| 1151 | xtn_hdr_data += 2; |
| 1152 | |
Joachim Bauch | c1fc2ba | 2015-12-11 17:21:41 +0100 | [diff] [blame] | 1153 | if (xtn_hdr_data + xlen > xtn_hdr_end) |
| 1154 | return srtp_err_status_parse_err; |
| 1155 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1156 | status = srtp_cipher_output(stream->rtp_xtn_hdr_cipher, keystream, &xlen_with_header); |
| 1157 | if (status) |
| 1158 | return srtp_err_status_cipher_fail; |
| 1159 | |
| 1160 | if (xlen > 0 && srtp_protect_extension_header(stream, xid)) { |
| 1161 | keystream_pos = 2; |
| 1162 | while (xlen > 0) { |
| 1163 | *xtn_hdr_data ^= keystream[keystream_pos++]; |
| 1164 | xtn_hdr_data++; |
| 1165 | xlen--; |
| 1166 | } |
| 1167 | } else { |
| 1168 | xtn_hdr_data += xlen; |
| 1169 | } |
| 1170 | |
| 1171 | /* skip padding bytes. */ |
| 1172 | while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) { |
| 1173 | xtn_hdr_data++; |
| 1174 | } |
| 1175 | } |
| 1176 | } else { |
| 1177 | /* unsupported extension header format. */ |
| 1178 | return srtp_err_status_parse_err; |
| 1179 | } |
| 1180 | |
| 1181 | return srtp_err_status_ok; |
| 1182 | } |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1183 | |
| 1184 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | * AEAD uses a new IV formation method. This function implements |
| 1187 | * section 9.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The |
| 1188 | * calculation is defined as, where (+) is the xor operation: |
| 1189 | * |
| 1190 | * |
| 1191 | * 0 0 0 0 0 0 0 0 0 0 1 1 |
| 1192 | * 0 1 2 3 4 5 6 7 8 9 0 1 |
| 1193 | * +--+--+--+--+--+--+--+--+--+--+--+--+ |
| 1194 | * |00|00| SSRC | ROC | SEQ |---+ |
| 1195 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 1196 | * | |
| 1197 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 1198 | * | Encryption Salt |->(+) |
| 1199 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 1200 | * | |
| 1201 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 1202 | * | Initialization Vector |<--+ |
| 1203 | * +--+--+--+--+--+--+--+--+--+--+--+--+* |
| 1204 | * |
| 1205 | * Input: *stream - pointer to SRTP stream context, used to retrieve |
| 1206 | * the SALT |
| 1207 | * *iv - Pointer to receive the calculated IV |
| 1208 | * *seq - The ROC and SEQ value to use for the |
| 1209 | * IV calculation. |
| 1210 | * *hdr - The RTP header, used to get the SSRC value |
| 1211 | * |
| 1212 | */ |
| 1213 | static void srtp_calc_aead_iv(srtp_stream_ctx_t *stream, v128_t *iv, |
| 1214 | srtp_xtd_seq_num_t *seq, srtp_hdr_t *hdr) |
| 1215 | { |
| 1216 | v128_t in; |
| 1217 | v128_t salt; |
| 1218 | |
| 1219 | #ifdef NO_64BIT_MATH |
| 1220 | uint32_t local_roc = ((high32(*seq) << 16) | |
| 1221 | (low32(*seq) >> 16)); |
| 1222 | uint16_t local_seq = (uint16_t) (low32(*seq)); |
| 1223 | #else |
| 1224 | uint32_t local_roc = (uint32_t)(*seq >> 16); |
| 1225 | uint16_t local_seq = (uint16_t) *seq; |
| 1226 | #endif |
| 1227 | |
| 1228 | memset(&in, 0, sizeof(v128_t)); |
| 1229 | memset(&salt, 0, sizeof(v128_t)); |
| 1230 | |
| 1231 | in.v16[5] = htons(local_seq); |
| 1232 | local_roc = htonl(local_roc); |
| 1233 | memcpy(&in.v16[3], &local_roc, sizeof(local_roc)); |
| 1234 | |
| 1235 | /* |
| 1236 | * Copy in the RTP SSRC value |
| 1237 | */ |
| 1238 | memcpy(&in.v8[2], &hdr->ssrc, 4); |
| 1239 | debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in)); |
| 1240 | |
| 1241 | /* |
| 1242 | * Get the SALT value from the context |
| 1243 | */ |
| 1244 | memcpy(salt.v8, stream->salt, SRTP_AEAD_SALT_LEN); |
| 1245 | debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt)); |
| 1246 | |
| 1247 | /* |
| 1248 | * Finally, apply tyhe SALT to the input |
| 1249 | */ |
| 1250 | v128_xor(iv, &in, &salt); |
| 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | /* |
| 1255 | * This function handles outgoing SRTP packets while in AEAD mode, |
| 1256 | * which currently supports AES-GCM encryption. All packets are |
| 1257 | * encrypted and authenticated. |
| 1258 | */ |
| 1259 | static srtp_err_status_t |
| 1260 | srtp_protect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, |
| 1261 | void *rtp_hdr, unsigned int *pkt_octet_len) |
| 1262 | { |
| 1263 | srtp_hdr_t *hdr = (srtp_hdr_t*)rtp_hdr; |
| 1264 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1265 | int enc_octet_len = 0; /* number of octets in encrypted portion */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1266 | srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */ |
| 1267 | int delta; /* delta of local pkt idx and that in hdr */ |
| 1268 | srtp_err_status_t status; |
| 1269 | uint32_t tag_len; |
| 1270 | v128_t iv; |
| 1271 | unsigned int aad_len; |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1272 | srtp_hdr_xtnd_t *xtn_hdr = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1273 | |
| 1274 | debug_print(mod_srtp, "function srtp_protect_aead", NULL); |
| 1275 | |
| 1276 | /* |
| 1277 | * update the key usage limit, and check it to make sure that we |
| 1278 | * didn't just hit either the soft limit or the hard limit, and call |
| 1279 | * the event handler if we hit either. |
| 1280 | */ |
| 1281 | switch (srtp_key_limit_update(stream->limit)) { |
| 1282 | case srtp_key_event_normal: |
| 1283 | break; |
| 1284 | case srtp_key_event_hard_limit: |
| 1285 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
| 1286 | return srtp_err_status_key_expired; |
| 1287 | case srtp_key_event_soft_limit: |
| 1288 | default: |
| 1289 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
| 1290 | break; |
| 1291 | } |
| 1292 | |
| 1293 | /* get tag length from stream */ |
| 1294 | tag_len = srtp_auth_get_tag_length(stream->rtp_auth); |
| 1295 | |
| 1296 | /* |
| 1297 | * find starting point for encryption and length of data to be |
| 1298 | * encrypted - the encrypted portion starts after the rtp header |
| 1299 | * extension, if present; otherwise, it starts after the last csrc, |
| 1300 | * if any are present |
| 1301 | */ |
| 1302 | enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc; |
| 1303 | if (hdr->x == 1) { |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1304 | xtn_hdr = (srtp_hdr_xtnd_t*)enc_start; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1305 | enc_start += (ntohs(xtn_hdr->length) + 1); |
| 1306 | } |
| 1307 | /* note: the passed size is without the auth tag */ |
jfigus | 2e95530 | 2015-11-20 10:24:39 -0500 | [diff] [blame] | 1308 | if (!((uint8_t*)enc_start <= (uint8_t*)hdr + *pkt_octet_len)) |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1309 | return srtp_err_status_parse_err; |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1310 | enc_octet_len = (int)(*pkt_octet_len - |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1311 | ((uint8_t*)enc_start - (uint8_t*)hdr)); |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1312 | if (enc_octet_len < 0) return srtp_err_status_parse_err; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1313 | |
| 1314 | /* |
| 1315 | * estimate the packet index using the start of the replay window |
| 1316 | * and the sequence number from the header |
| 1317 | */ |
| 1318 | delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); |
| 1319 | status = srtp_rdbx_check(&stream->rtp_rdbx, delta); |
| 1320 | if (status) { |
| 1321 | if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) { |
| 1322 | return status; /* we've been asked to reuse an index */ |
| 1323 | } |
| 1324 | } else { |
| 1325 | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
| 1326 | } |
| 1327 | |
| 1328 | #ifdef NO_64BIT_MATH |
| 1329 | debug_print2(mod_srtp, "estimated packet index: %08x%08x", |
| 1330 | high32(est), low32(est)); |
| 1331 | #else |
| 1332 | debug_print(mod_srtp, "estimated packet index: %016llx", est); |
| 1333 | #endif |
| 1334 | |
| 1335 | /* |
| 1336 | * AEAD uses a new IV formation method |
| 1337 | */ |
| 1338 | srtp_calc_aead_iv(stream, &iv, &est, hdr); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1339 | /* shift est, put into network byte order */ |
| 1340 | #ifdef NO_64BIT_MATH |
| 1341 | est = be64_to_cpu(make64((high32(est) << 16) | |
| 1342 | (low32(est) >> 16), |
| 1343 | low32(est) << 16)); |
| 1344 | #else |
| 1345 | est = be64_to_cpu(est << 16); |
| 1346 | #endif |
| 1347 | |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1348 | status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_encrypt); |
| 1349 | if (!status && stream->rtp_xtn_hdr_cipher) { |
| 1350 | iv.v32[0] = 0; |
| 1351 | iv.v32[1] = hdr->ssrc; |
| 1352 | iv.v64[1] = est; |
| 1353 | status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt); |
| 1354 | } |
| 1355 | if (status) { |
| 1356 | return srtp_err_status_cipher_fail; |
| 1357 | } |
| 1358 | |
| 1359 | if (xtn_hdr && stream->rtp_xtn_hdr_cipher) { |
| 1360 | /* |
| 1361 | * extensions header encryption RFC 6904 |
| 1362 | */ |
| 1363 | status = srtp_process_header_encryption(stream, xtn_hdr); |
| 1364 | if (status) { |
| 1365 | return status; |
| 1366 | } |
| 1367 | } |
| 1368 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1369 | /* |
| 1370 | * Set the AAD over the RTP header |
| 1371 | */ |
| 1372 | aad_len = (uint8_t *)enc_start - (uint8_t *)hdr; |
| 1373 | status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len); |
| 1374 | if (status) { |
| 1375 | return ( srtp_err_status_cipher_fail); |
| 1376 | } |
| 1377 | |
| 1378 | /* Encrypt the payload */ |
| 1379 | status = srtp_cipher_encrypt(stream->rtp_cipher, |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1380 | (uint8_t*)enc_start, (unsigned int *)&enc_octet_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1381 | if (status) { |
| 1382 | return srtp_err_status_cipher_fail; |
| 1383 | } |
| 1384 | /* |
| 1385 | * If we're doing GCM, we need to get the tag |
| 1386 | * and append that to the output |
| 1387 | */ |
| 1388 | status = srtp_cipher_get_tag(stream->rtp_cipher, |
| 1389 | (uint8_t*)enc_start+enc_octet_len, &tag_len); |
| 1390 | if (status) { |
| 1391 | return ( srtp_err_status_cipher_fail); |
| 1392 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1393 | |
| 1394 | /* increase the packet length by the length of the auth tag */ |
| 1395 | *pkt_octet_len += tag_len; |
| 1396 | |
| 1397 | return srtp_err_status_ok; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | /* |
| 1402 | * This function handles incoming SRTP packets while in AEAD mode, |
| 1403 | * which currently supports AES-GCM encryption. All packets are |
| 1404 | * encrypted and authenticated. Note, the auth tag is at the end |
| 1405 | * of the packet stream and is automatically checked by GCM |
| 1406 | * when decrypting the payload. |
| 1407 | */ |
| 1408 | static srtp_err_status_t |
| 1409 | srtp_unprotect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, int delta, |
| 1410 | srtp_xtd_seq_num_t est, void *srtp_hdr, unsigned int *pkt_octet_len) |
| 1411 | { |
| 1412 | srtp_hdr_t *hdr = (srtp_hdr_t*)srtp_hdr; |
| 1413 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 1414 | unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */ |
| 1415 | v128_t iv; |
| 1416 | srtp_err_status_t status; |
| 1417 | int tag_len; |
| 1418 | unsigned int aad_len; |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1419 | srtp_hdr_xtnd_t *xtn_hdr = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1420 | |
| 1421 | debug_print(mod_srtp, "function srtp_unprotect_aead", NULL); |
| 1422 | |
| 1423 | #ifdef NO_64BIT_MATH |
| 1424 | debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est), low32(est)); |
| 1425 | #else |
| 1426 | debug_print(mod_srtp, "estimated u_packet index: %016llx", est); |
| 1427 | #endif |
| 1428 | |
| 1429 | /* get tag length from stream */ |
| 1430 | tag_len = srtp_auth_get_tag_length(stream->rtp_auth); |
| 1431 | |
| 1432 | /* |
| 1433 | * AEAD uses a new IV formation method |
| 1434 | */ |
| 1435 | srtp_calc_aead_iv(stream, &iv, &est, hdr); |
| 1436 | status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_decrypt); |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1437 | if (!status && stream->rtp_xtn_hdr_cipher) { |
| 1438 | iv.v32[0] = 0; |
| 1439 | iv.v32[1] = hdr->ssrc; |
| 1440 | #ifdef NO_64BIT_MATH |
| 1441 | iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16), |
| 1442 | low32(est) << 16)); |
| 1443 | #else |
| 1444 | iv.v64[1] = be64_to_cpu(est << 16); |
| 1445 | #endif |
| 1446 | status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt); |
| 1447 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1448 | if (status) { |
| 1449 | return srtp_err_status_cipher_fail; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * find starting point for decryption and length of data to be |
| 1454 | * decrypted - the encrypted portion starts after the rtp header |
| 1455 | * extension, if present; otherwise, it starts after the last csrc, |
| 1456 | * if any are present |
| 1457 | */ |
| 1458 | enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc; |
| 1459 | if (hdr->x == 1) { |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1460 | xtn_hdr = (srtp_hdr_xtnd_t*)enc_start; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1461 | enc_start += (ntohs(xtn_hdr->length) + 1); |
| 1462 | } |
Joachim Bauch | dae5325 | 2015-12-14 22:50:10 +0100 | [diff] [blame] | 1463 | if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len))) |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1464 | return srtp_err_status_parse_err; |
| 1465 | /* |
| 1466 | * We pass the tag down to the cipher when doing GCM mode |
| 1467 | */ |
| 1468 | enc_octet_len = (unsigned int)(*pkt_octet_len - |
| 1469 | ((uint8_t*)enc_start - (uint8_t*)hdr)); |
| 1470 | |
| 1471 | /* |
| 1472 | * Sanity check the encrypted payload length against |
| 1473 | * the tag size. It must always be at least as large |
| 1474 | * as the tag length. |
| 1475 | */ |
| 1476 | if (enc_octet_len < (unsigned int) tag_len) { |
| 1477 | return srtp_err_status_cipher_fail; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * update the key usage limit, and check it to make sure that we |
| 1482 | * didn't just hit either the soft limit or the hard limit, and call |
| 1483 | * the event handler if we hit either. |
| 1484 | */ |
| 1485 | switch (srtp_key_limit_update(stream->limit)) { |
| 1486 | case srtp_key_event_normal: |
| 1487 | break; |
| 1488 | case srtp_key_event_soft_limit: |
| 1489 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
| 1490 | break; |
| 1491 | case srtp_key_event_hard_limit: |
| 1492 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
| 1493 | return srtp_err_status_key_expired; |
| 1494 | default: |
| 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * Set the AAD for AES-GCM, which is the RTP header |
| 1500 | */ |
| 1501 | aad_len = (uint8_t *)enc_start - (uint8_t *)hdr; |
| 1502 | status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len); |
| 1503 | if (status) { |
| 1504 | return ( srtp_err_status_cipher_fail); |
| 1505 | } |
| 1506 | |
Joachim Bauch | da33987 | 2015-12-11 18:09:01 +0100 | [diff] [blame] | 1507 | /* Decrypt the ciphertext. This also checks the auth tag based |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1508 | * on the AAD we just specified above */ |
| 1509 | status = srtp_cipher_decrypt(stream->rtp_cipher, (uint8_t*)enc_start, &enc_octet_len); |
| 1510 | if (status) { |
| 1511 | return status; |
| 1512 | } |
| 1513 | |
Joachim Bauch | fa1e8c2 | 2015-11-24 23:38:48 +0100 | [diff] [blame] | 1514 | if (xtn_hdr && stream->rtp_xtn_hdr_cipher) { |
| 1515 | /* |
| 1516 | * extensions header encryption RFC 6904 |
| 1517 | */ |
| 1518 | status = srtp_process_header_encryption(stream, xtn_hdr); |
| 1519 | if (status) { |
| 1520 | return status; |
| 1521 | } |
| 1522 | } |
| 1523 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1524 | /* |
| 1525 | * verify that stream is for received traffic - this check will |
| 1526 | * detect SSRC collisions, since a stream that appears in both |
| 1527 | * srtp_protect() and srtp_unprotect() will fail this test in one of |
| 1528 | * those functions. |
| 1529 | * |
| 1530 | * we do this check *after* the authentication check, so that the |
| 1531 | * latter check will catch any attempts to fool us into thinking |
| 1532 | * that we've got a collision |
| 1533 | */ |
| 1534 | if (stream->direction != dir_srtp_receiver) { |
| 1535 | if (stream->direction == dir_unknown) { |
| 1536 | stream->direction = dir_srtp_receiver; |
| 1537 | } else { |
| 1538 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * if the stream is a 'provisional' one, in which the template context |
| 1544 | * is used, then we need to allocate a new stream at this point, since |
| 1545 | * the authentication passed |
| 1546 | */ |
| 1547 | if (stream == ctx->stream_template) { |
| 1548 | srtp_stream_ctx_t *new_stream; |
| 1549 | |
| 1550 | /* |
| 1551 | * allocate and initialize a new stream |
| 1552 | * |
| 1553 | * note that we indicate failure if we can't allocate the new |
| 1554 | * stream, and some implementations will want to not return |
| 1555 | * failure here |
| 1556 | */ |
| 1557 | status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
| 1558 | if (status) { |
| 1559 | return status; |
| 1560 | } |
| 1561 | |
| 1562 | /* add new stream to the head of the stream_list */ |
| 1563 | new_stream->next = ctx->stream_list; |
| 1564 | ctx->stream_list = new_stream; |
| 1565 | |
| 1566 | /* set stream (the pointer used in this function) */ |
| 1567 | stream = new_stream; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * the message authentication function passed, so add the packet |
| 1572 | * index into the replay database |
| 1573 | */ |
| 1574 | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
| 1575 | |
| 1576 | /* decrease the packet length by the length of the auth tag */ |
| 1577 | *pkt_octet_len -= tag_len; |
| 1578 | |
| 1579 | return srtp_err_status_ok; |
| 1580 | } |
| 1581 | |
| 1582 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1583 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1584 | srtp_protect(srtp_ctx_t *ctx, void *rtp_hdr, int *pkt_octet_len) { |
Derek MacDonald | 17127da | 2006-07-12 22:22:08 +0000 | [diff] [blame] | 1585 | srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1586 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 1587 | uint32_t *auth_start; /* pointer to start of auth. portion */ |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1588 | int enc_octet_len = 0; /* number of octets in encrypted portion */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1589 | srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1590 | int delta; /* delta of local pkt idx and that in hdr */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 1591 | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1592 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1593 | int tag_len; |
| 1594 | srtp_stream_ctx_t *stream; |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 1595 | uint32_t prefix_len; |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1596 | srtp_hdr_xtnd_t *xtn_hdr = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1597 | |
| 1598 | debug_print(mod_srtp, "function srtp_protect", NULL); |
| 1599 | |
| 1600 | /* we assume the hdr is 32-bit aligned to start */ |
| 1601 | |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 1602 | /* Verify RTP header */ |
| 1603 | status = srtp_validate_rtp_header(rtp_hdr, pkt_octet_len); |
| 1604 | if (status) |
| 1605 | return status; |
| 1606 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1607 | /* check the packet length - it must at least contain a full header */ |
| 1608 | if (*pkt_octet_len < octets_in_rtp_header) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1609 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1610 | |
| 1611 | /* |
| 1612 | * look up ssrc in srtp_stream list, and process the packet with |
| 1613 | * the appropriate stream. if we haven't seen this stream before, |
| 1614 | * there's a template key for this srtp_session, and the cipher |
| 1615 | * supports key-sharing, then we assume that a new stream using |
| 1616 | * that key has just started up |
| 1617 | */ |
| 1618 | stream = srtp_get_stream(ctx, hdr->ssrc); |
| 1619 | if (stream == NULL) { |
| 1620 | if (ctx->stream_template != NULL) { |
| 1621 | srtp_stream_ctx_t *new_stream; |
| 1622 | |
| 1623 | /* allocate and initialize a new stream */ |
| 1624 | status = srtp_stream_clone(ctx->stream_template, |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 1625 | hdr->ssrc, &new_stream); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1626 | if (status) |
| 1627 | return status; |
| 1628 | |
| 1629 | /* add new stream to the head of the stream_list */ |
| 1630 | new_stream->next = ctx->stream_list; |
| 1631 | ctx->stream_list = new_stream; |
| 1632 | |
| 1633 | /* set direction to outbound */ |
| 1634 | new_stream->direction = dir_srtp_sender; |
| 1635 | |
| 1636 | /* set stream (the pointer used in this function) */ |
| 1637 | stream = new_stream; |
| 1638 | } else { |
| 1639 | /* no template stream, so we return an error */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1640 | return srtp_err_status_no_ctx; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * verify that stream is for sending traffic - this check will |
| 1646 | * detect SSRC collisions, since a stream that appears in both |
| 1647 | * srtp_protect() and srtp_unprotect() will fail this test in one of |
| 1648 | * those functions. |
| 1649 | */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1650 | if (stream->direction != dir_srtp_sender) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1651 | if (stream->direction == dir_unknown) { |
| 1652 | stream->direction = dir_srtp_sender; |
| 1653 | } else { |
| 1654 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
| 1655 | } |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
| 1660 | * the request to our AEAD handler. |
| 1661 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 1662 | if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM || |
| 1663 | stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) { |
Travis Cross | 3184400 | 2014-07-02 16:18:57 +0000 | [diff] [blame] | 1664 | return srtp_protect_aead(ctx, stream, rtp_hdr, (unsigned int*)pkt_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1665 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* |
| 1668 | * update the key usage limit, and check it to make sure that we |
| 1669 | * didn't just hit either the soft limit or the hard limit, and call |
| 1670 | * the event handler if we hit either. |
| 1671 | */ |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 1672 | switch(srtp_key_limit_update(stream->limit)) { |
| 1673 | case srtp_key_event_normal: |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1674 | break; |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 1675 | case srtp_key_event_soft_limit: |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1676 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
| 1677 | break; |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 1678 | case srtp_key_event_hard_limit: |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1679 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1680 | return srtp_err_status_key_expired; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1681 | default: |
| 1682 | break; |
| 1683 | } |
| 1684 | |
| 1685 | /* get tag length from stream */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 1686 | tag_len = srtp_auth_get_tag_length(stream->rtp_auth); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1687 | |
| 1688 | /* |
| 1689 | * find starting point for encryption and length of data to be |
| 1690 | * encrypted - the encrypted portion starts after the rtp header |
| 1691 | * extension, if present; otherwise, it starts after the last csrc, |
| 1692 | * if any are present |
| 1693 | * |
| 1694 | * if we're not providing confidentiality, set enc_start to NULL |
| 1695 | */ |
| 1696 | if (stream->rtp_services & sec_serv_conf) { |
| 1697 | enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc; |
David McGrew | 1482930 | 2005-10-10 18:53:19 +0000 | [diff] [blame] | 1698 | if (hdr->x == 1) { |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1699 | xtn_hdr = (srtp_hdr_xtnd_t *)enc_start; |
David McGrew | 1482930 | 2005-10-10 18:53:19 +0000 | [diff] [blame] | 1700 | enc_start += (ntohs(xtn_hdr->length) + 1); |
| 1701 | } |
Joachim Bauch | 3444c4b | 2015-11-13 00:03:19 +0100 | [diff] [blame] | 1702 | /* note: the passed size is without the auth tag */ |
jfigus | 2e95530 | 2015-11-20 10:24:39 -0500 | [diff] [blame] | 1703 | if (!((uint8_t*)enc_start <= (uint8_t*)hdr + *pkt_octet_len)) |
jfigus | 2583150 | 2015-11-02 10:09:43 -0500 | [diff] [blame] | 1704 | return srtp_err_status_parse_err; |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1705 | enc_octet_len = (int)(*pkt_octet_len - |
Travis Cross | dee3ee8 | 2014-07-02 15:20:12 +0000 | [diff] [blame] | 1706 | ((uint8_t*)enc_start - (uint8_t*)hdr)); |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1707 | if (enc_octet_len < 0) return srtp_err_status_parse_err; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1708 | } else { |
| 1709 | enc_start = NULL; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * if we're providing authentication, set the auth_start and auth_tag |
| 1714 | * pointers to the proper locations; otherwise, set auth_start to NULL |
| 1715 | * to indicate that no authentication is needed |
| 1716 | */ |
| 1717 | if (stream->rtp_services & sec_serv_auth) { |
| 1718 | auth_start = (uint32_t *)hdr; |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 1719 | auth_tag = (uint8_t *)hdr + *pkt_octet_len; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1720 | } else { |
| 1721 | auth_start = NULL; |
| 1722 | auth_tag = NULL; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * estimate the packet index using the start of the replay window |
| 1727 | * and the sequence number from the header |
| 1728 | */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1729 | delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); |
| 1730 | status = srtp_rdbx_check(&stream->rtp_rdbx, delta); |
Jonathan Lennox | dcee5c6 | 2010-05-17 22:08:40 +0000 | [diff] [blame] | 1731 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1732 | if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) |
Jonathan Lennox | dcee5c6 | 2010-05-17 22:08:40 +0000 | [diff] [blame] | 1733 | return status; /* we've been asked to reuse an index */ |
| 1734 | } |
| 1735 | else |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1736 | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1737 | |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1738 | #ifdef NO_64BIT_MATH |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1739 | debug_print2(mod_srtp, "estimated packet index: %08x%08x", |
| 1740 | high32(est),low32(est)); |
| 1741 | #else |
| 1742 | debug_print(mod_srtp, "estimated packet index: %016llx", est); |
| 1743 | #endif |
| 1744 | |
| 1745 | /* |
| 1746 | * if we're using rindael counter mode, set nonce and seq |
| 1747 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 1748 | if (stream->rtp_cipher->type->id == SRTP_AES_ICM || |
| 1749 | stream->rtp_cipher->type->id == SRTP_AES_256_ICM) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1750 | v128_t iv; |
| 1751 | |
| 1752 | iv.v32[0] = 0; |
| 1753 | iv.v32[1] = hdr->ssrc; |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1754 | #ifdef NO_64BIT_MATH |
| 1755 | iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16), |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 1756 | low32(est) << 16)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1757 | #else |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1758 | iv.v64[1] = be64_to_cpu(est << 16); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1759 | #endif |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 1760 | status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_encrypt); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1761 | if (!status && stream->rtp_xtn_hdr_cipher) { |
| 1762 | status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt); |
| 1763 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1764 | } else { |
| 1765 | v128_t iv; |
| 1766 | |
| 1767 | /* otherwise, set the index to est */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1768 | #ifdef NO_64BIT_MATH |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1769 | iv.v32[0] = 0; |
| 1770 | iv.v32[1] = 0; |
| 1771 | #else |
| 1772 | iv.v64[0] = 0; |
| 1773 | #endif |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1774 | iv.v64[1] = be64_to_cpu(est); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 1775 | status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_encrypt); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1776 | if (!status && stream->rtp_xtn_hdr_cipher) { |
| 1777 | status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt); |
| 1778 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1779 | } |
| 1780 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1781 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1782 | |
| 1783 | /* shift est, put into network byte order */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1784 | #ifdef NO_64BIT_MATH |
| 1785 | est = be64_to_cpu(make64((high32(est) << 16) | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1786 | (low32(est) >> 16), |
Randell Jesup | 811e144 | 2005-09-28 22:43:41 +0000 | [diff] [blame] | 1787 | low32(est) << 16)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1788 | #else |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1789 | est = be64_to_cpu(est << 16); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1790 | #endif |
| 1791 | |
| 1792 | /* |
| 1793 | * if we're authenticating using a universal hash, put the keystream |
| 1794 | * prefix into the authentication tag |
| 1795 | */ |
| 1796 | if (auth_start) { |
| 1797 | |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 1798 | prefix_len = srtp_auth_get_prefix_length(stream->rtp_auth); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1799 | if (prefix_len) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 1800 | status = srtp_cipher_output(stream->rtp_cipher, auth_tag, &prefix_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1801 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1802 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1803 | debug_print(mod_srtp, "keystream prefix: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 1804 | srtp_octet_string_hex_string(auth_tag, prefix_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1808 | if (xtn_hdr && stream->rtp_xtn_hdr_cipher) { |
| 1809 | /* |
| 1810 | * extensions header encryption RFC 6904 |
| 1811 | */ |
| 1812 | status = srtp_process_header_encryption(stream, xtn_hdr); |
| 1813 | if (status) { |
| 1814 | return status; |
| 1815 | } |
| 1816 | } |
| 1817 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1818 | /* if we're encrypting, exor keystream into the message */ |
| 1819 | if (enc_start) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 1820 | status = srtp_cipher_encrypt(stream->rtp_cipher, |
jfigus | 0380bf4 | 2015-11-18 10:58:10 -0500 | [diff] [blame] | 1821 | (uint8_t *)enc_start, (unsigned int *)&enc_octet_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1822 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1823 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | /* |
| 1827 | * if we're authenticating, run authentication function and put result |
| 1828 | * into the auth_tag |
| 1829 | */ |
| 1830 | if (auth_start) { |
| 1831 | |
| 1832 | /* initialize auth func context */ |
| 1833 | status = auth_start(stream->rtp_auth); |
| 1834 | if (status) return status; |
| 1835 | |
| 1836 | /* run auth func over packet */ |
| 1837 | status = auth_update(stream->rtp_auth, |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 1838 | (uint8_t *)auth_start, *pkt_octet_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1839 | if (status) return status; |
| 1840 | |
| 1841 | /* run auth func over ROC, put result into auth_tag */ |
David McGrew | 89fb7ea | 2005-09-26 19:33:44 +0000 | [diff] [blame] | 1842 | debug_print(mod_srtp, "estimated packet index: %016llx", est); |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 1843 | status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, auth_tag); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1844 | debug_print(mod_srtp, "srtp auth tag: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 1845 | srtp_octet_string_hex_string(auth_tag, tag_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1846 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1847 | return srtp_err_status_auth_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1848 | |
| 1849 | } |
| 1850 | |
| 1851 | if (auth_tag) { |
| 1852 | |
| 1853 | /* increase the packet length by the length of the auth tag */ |
| 1854 | *pkt_octet_len += tag_len; |
| 1855 | } |
| 1856 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1857 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1861 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1862 | srtp_unprotect(srtp_ctx_t *ctx, void *srtp_hdr, int *pkt_octet_len) { |
Derek MacDonald | 17127da | 2006-07-12 22:22:08 +0000 | [diff] [blame] | 1863 | srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1864 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 1865 | uint32_t *auth_start; /* pointer to start of auth. portion */ |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 1866 | unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 1867 | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1868 | srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1869 | int delta; /* delta of local pkt idx and that in hdr */ |
| 1870 | v128_t iv; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1871 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1872 | srtp_stream_ctx_t *stream; |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 1873 | uint8_t tmp_tag[SRTP_MAX_TAG_LEN]; |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 1874 | uint32_t tag_len, prefix_len; |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1875 | srtp_hdr_xtnd_t *xtn_hdr = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1876 | |
| 1877 | debug_print(mod_srtp, "function srtp_unprotect", NULL); |
| 1878 | |
| 1879 | /* we assume the hdr is 32-bit aligned to start */ |
| 1880 | |
Joachim Bauch | 557a787 | 2015-02-19 01:36:48 +0100 | [diff] [blame] | 1881 | /* Verify RTP header */ |
| 1882 | status = srtp_validate_rtp_header(srtp_hdr, pkt_octet_len); |
| 1883 | if (status) |
| 1884 | return status; |
| 1885 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1886 | /* check the packet length - it must at least contain a full header */ |
| 1887 | if (*pkt_octet_len < octets_in_rtp_header) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1888 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1889 | |
| 1890 | /* |
| 1891 | * look up ssrc in srtp_stream list, and process the packet with |
| 1892 | * the appropriate stream. if we haven't seen this stream before, |
| 1893 | * there's only one key for this srtp_session, and the cipher |
| 1894 | * supports key-sharing, then we assume that a new stream using |
| 1895 | * that key has just started up |
| 1896 | */ |
| 1897 | stream = srtp_get_stream(ctx, hdr->ssrc); |
| 1898 | if (stream == NULL) { |
| 1899 | if (ctx->stream_template != NULL) { |
| 1900 | stream = ctx->stream_template; |
| 1901 | debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)", |
| 1902 | hdr->ssrc); |
| 1903 | |
| 1904 | /* |
| 1905 | * set estimated packet index to sequence number from header, |
| 1906 | * and set delta equal to the same value |
| 1907 | */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1908 | #ifdef NO_64BIT_MATH |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1909 | est = (srtp_xtd_seq_num_t) make64(0,ntohs(hdr->seq)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1910 | delta = low32(est); |
| 1911 | #else |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1912 | est = (srtp_xtd_seq_num_t) ntohs(hdr->seq); |
David McGrew | c4fc00b | 2006-06-08 18:51:27 +0000 | [diff] [blame] | 1913 | delta = (int)est; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1914 | #endif |
| 1915 | } else { |
| 1916 | |
| 1917 | /* |
| 1918 | * no stream corresponding to SSRC found, and we don't do |
| 1919 | * key-sharing, so return an error |
| 1920 | */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1921 | return srtp_err_status_no_ctx; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1922 | } |
| 1923 | } else { |
| 1924 | |
| 1925 | /* estimate packet index from seq. num. in header */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1926 | delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq)); |
Marcus Sundberg | 0c324cb | 2005-10-10 17:23:47 +0000 | [diff] [blame] | 1927 | |
| 1928 | /* check replay database */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 1929 | status = srtp_rdbx_check(&stream->rtp_rdbx, delta); |
Marcus Sundberg | 0c324cb | 2005-10-10 17:23:47 +0000 | [diff] [blame] | 1930 | if (status) |
| 1931 | return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1934 | #ifdef NO_64BIT_MATH |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 1935 | debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est),low32(est)); |
| 1936 | #else |
| 1937 | debug_print(mod_srtp, "estimated u_packet index: %016llx", est); |
| 1938 | #endif |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1939 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1940 | /* |
| 1941 | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
| 1942 | * the request to our AEAD handler. |
| 1943 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 1944 | if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM || |
| 1945 | stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) { |
Travis Cross | 3184400 | 2014-07-02 16:18:57 +0000 | [diff] [blame] | 1946 | return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr, (unsigned int*)pkt_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1949 | /* get tag length from stream */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 1950 | tag_len = srtp_auth_get_tag_length(stream->rtp_auth); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1951 | |
| 1952 | /* |
| 1953 | * set the cipher's IV properly, depending on whatever cipher we |
| 1954 | * happen to be using |
| 1955 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 1956 | if (stream->rtp_cipher->type->id == SRTP_AES_ICM || |
| 1957 | stream->rtp_cipher->type->id == SRTP_AES_256_ICM) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1958 | |
| 1959 | /* aes counter mode */ |
| 1960 | iv.v32[0] = 0; |
| 1961 | iv.v32[1] = hdr->ssrc; /* still in network order */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1962 | #ifdef NO_64BIT_MATH |
| 1963 | iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16), |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1964 | low32(est) << 16)); |
| 1965 | #else |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1966 | iv.v64[1] = be64_to_cpu(est << 16); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1967 | #endif |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 1968 | status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_decrypt); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1969 | if (!status && stream->rtp_xtn_hdr_cipher) { |
| 1970 | status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_decrypt); |
| 1971 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1972 | } else { |
| 1973 | |
| 1974 | /* no particular format - set the iv to the pakcet index */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1975 | #ifdef NO_64BIT_MATH |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1976 | iv.v32[0] = 0; |
| 1977 | iv.v32[1] = 0; |
| 1978 | #else |
| 1979 | iv.v64[0] = 0; |
| 1980 | #endif |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1981 | iv.v64[1] = be64_to_cpu(est); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 1982 | status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_decrypt); |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 1983 | if (!status && stream->rtp_xtn_hdr_cipher) { |
| 1984 | status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_decrypt); |
| 1985 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1986 | } |
| 1987 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 1988 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1989 | |
| 1990 | /* shift est, put into network byte order */ |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1991 | #ifdef NO_64BIT_MATH |
| 1992 | est = be64_to_cpu(make64((high32(est) << 16) | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1993 | (low32(est) >> 16), |
| 1994 | low32(est) << 16)); |
| 1995 | #else |
Marcus Sundberg | e4e34f9 | 2005-10-02 20:19:35 +0000 | [diff] [blame] | 1996 | est = be64_to_cpu(est << 16); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 1997 | #endif |
| 1998 | |
Marcus Sundberg | 0c324cb | 2005-10-10 17:23:47 +0000 | [diff] [blame] | 1999 | /* |
| 2000 | * find starting point for decryption and length of data to be |
| 2001 | * decrypted - the encrypted portion starts after the rtp header |
| 2002 | * extension, if present; otherwise, it starts after the last csrc, |
| 2003 | * if any are present |
| 2004 | * |
| 2005 | * if we're not providing confidentiality, set enc_start to NULL |
| 2006 | */ |
| 2007 | if (stream->rtp_services & sec_serv_conf) { |
| 2008 | enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc; |
David McGrew | 1482930 | 2005-10-10 18:53:19 +0000 | [diff] [blame] | 2009 | if (hdr->x == 1) { |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 2010 | xtn_hdr = (srtp_hdr_xtnd_t *)enc_start; |
David McGrew | 1482930 | 2005-10-10 18:53:19 +0000 | [diff] [blame] | 2011 | enc_start += (ntohs(xtn_hdr->length) + 1); |
| 2012 | } |
Joachim Bauch | dae5325 | 2015-12-14 22:50:10 +0100 | [diff] [blame] | 2013 | if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len))) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2014 | return srtp_err_status_parse_err; |
Travis Cross | dee3ee8 | 2014-07-02 15:20:12 +0000 | [diff] [blame] | 2015 | enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len - |
| 2016 | ((uint8_t*)enc_start - (uint8_t*)hdr)); |
Marcus Sundberg | 0c324cb | 2005-10-10 17:23:47 +0000 | [diff] [blame] | 2017 | } else { |
| 2018 | enc_start = NULL; |
| 2019 | } |
| 2020 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2021 | /* |
| 2022 | * if we're providing authentication, set the auth_start and auth_tag |
| 2023 | * pointers to the proper locations; otherwise, set auth_start to NULL |
| 2024 | * to indicate that no authentication is needed |
| 2025 | */ |
| 2026 | if (stream->rtp_services & sec_serv_auth) { |
| 2027 | auth_start = (uint32_t *)hdr; |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 2028 | auth_tag = (uint8_t *)hdr + *pkt_octet_len - tag_len; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2029 | } else { |
| 2030 | auth_start = NULL; |
| 2031 | auth_tag = NULL; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * if we expect message authentication, run the authentication |
| 2036 | * function and compare the result with the value of the auth_tag |
| 2037 | */ |
| 2038 | if (auth_start) { |
| 2039 | |
| 2040 | /* |
| 2041 | * if we're using a universal hash, then we need to compute the |
| 2042 | * keystream prefix for encrypting the universal hash output |
| 2043 | * |
| 2044 | * if the keystream prefix length is zero, then we know that |
| 2045 | * the authenticator isn't using a universal hash function |
| 2046 | */ |
| 2047 | if (stream->rtp_auth->prefix_len != 0) { |
| 2048 | |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 2049 | prefix_len = srtp_auth_get_prefix_length(stream->rtp_auth); |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 2050 | status = srtp_cipher_output(stream->rtp_cipher, tmp_tag, &prefix_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2051 | debug_print(mod_srtp, "keystream prefix: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 2052 | srtp_octet_string_hex_string(tmp_tag, prefix_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2053 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2054 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /* initialize auth func context */ |
| 2058 | status = auth_start(stream->rtp_auth); |
| 2059 | if (status) return status; |
David McGrew | fec49dd | 2005-09-23 19:34:11 +0000 | [diff] [blame] | 2060 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2061 | /* now compute auth function over packet */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 2062 | status = auth_update(stream->rtp_auth, (uint8_t *)auth_start, |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2063 | *pkt_octet_len - tag_len); |
| 2064 | |
| 2065 | /* run auth func over ROC, then write tmp tag */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 2066 | status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, tmp_tag); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2067 | |
| 2068 | debug_print(mod_srtp, "computed auth tag: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 2069 | srtp_octet_string_hex_string(tmp_tag, tag_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2070 | debug_print(mod_srtp, "packet auth tag: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 2071 | srtp_octet_string_hex_string(auth_tag, tag_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2072 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2073 | return srtp_err_status_auth_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2074 | |
| 2075 | if (octet_string_is_eq(tmp_tag, auth_tag, tag_len)) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2076 | return srtp_err_status_auth_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Marcus Sundberg | fc4d138 | 2005-10-08 18:28:16 +0000 | [diff] [blame] | 2079 | /* |
| 2080 | * update the key usage limit, and check it to make sure that we |
| 2081 | * didn't just hit either the soft limit or the hard limit, and call |
| 2082 | * the event handler if we hit either. |
| 2083 | */ |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 2084 | switch(srtp_key_limit_update(stream->limit)) { |
| 2085 | case srtp_key_event_normal: |
Marcus Sundberg | fc4d138 | 2005-10-08 18:28:16 +0000 | [diff] [blame] | 2086 | break; |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 2087 | case srtp_key_event_soft_limit: |
Marcus Sundberg | fc4d138 | 2005-10-08 18:28:16 +0000 | [diff] [blame] | 2088 | srtp_handle_event(ctx, stream, event_key_soft_limit); |
| 2089 | break; |
jfigus | c7cdc9a | 2014-11-19 16:19:08 -0500 | [diff] [blame] | 2090 | case srtp_key_event_hard_limit: |
Marcus Sundberg | fc4d138 | 2005-10-08 18:28:16 +0000 | [diff] [blame] | 2091 | srtp_handle_event(ctx, stream, event_key_hard_limit); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2092 | return srtp_err_status_key_expired; |
Marcus Sundberg | fc4d138 | 2005-10-08 18:28:16 +0000 | [diff] [blame] | 2093 | default: |
| 2094 | break; |
| 2095 | } |
| 2096 | |
Joachim Bauch | 99a7482 | 2015-11-17 00:08:19 +0100 | [diff] [blame] | 2097 | if (xtn_hdr && stream->rtp_xtn_hdr_cipher) { |
| 2098 | /* |
| 2099 | * extensions header encryption RFC 6904 |
| 2100 | */ |
| 2101 | status = srtp_process_header_encryption(stream, xtn_hdr); |
| 2102 | if (status) { |
| 2103 | return status; |
| 2104 | } |
| 2105 | } |
| 2106 | |
Jonathan Lennox | 23dc1e2 | 2010-06-01 18:19:04 +0000 | [diff] [blame] | 2107 | /* if we're decrypting, add keystream into ciphertext */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2108 | if (enc_start) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 2109 | status = srtp_cipher_decrypt(stream->rtp_cipher, (uint8_t *)enc_start, &enc_octet_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2110 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2111 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * verify that stream is for received traffic - this check will |
| 2116 | * detect SSRC collisions, since a stream that appears in both |
| 2117 | * srtp_protect() and srtp_unprotect() will fail this test in one of |
| 2118 | * those functions. |
| 2119 | * |
| 2120 | * we do this check *after* the authentication check, so that the |
| 2121 | * latter check will catch any attempts to fool us into thinking |
| 2122 | * that we've got a collision |
| 2123 | */ |
| 2124 | if (stream->direction != dir_srtp_receiver) { |
| 2125 | if (stream->direction == dir_unknown) { |
| 2126 | stream->direction = dir_srtp_receiver; |
| 2127 | } else { |
| 2128 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * if the stream is a 'provisional' one, in which the template context |
| 2134 | * is used, then we need to allocate a new stream at this point, since |
| 2135 | * the authentication passed |
| 2136 | */ |
| 2137 | if (stream == ctx->stream_template) { |
| 2138 | srtp_stream_ctx_t *new_stream; |
| 2139 | |
| 2140 | /* |
| 2141 | * allocate and initialize a new stream |
| 2142 | * |
| 2143 | * note that we indicate failure if we can't allocate the new |
| 2144 | * stream, and some implementations will want to not return |
| 2145 | * failure here |
| 2146 | */ |
| 2147 | status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
| 2148 | if (status) |
| 2149 | return status; |
| 2150 | |
| 2151 | /* add new stream to the head of the stream_list */ |
| 2152 | new_stream->next = ctx->stream_list; |
| 2153 | ctx->stream_list = new_stream; |
| 2154 | |
| 2155 | /* set stream (the pointer used in this function) */ |
| 2156 | stream = new_stream; |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * the message authentication function passed, so add the packet |
| 2161 | * index into the replay database |
| 2162 | */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 2163 | srtp_rdbx_add_index(&stream->rtp_rdbx, delta); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2164 | |
| 2165 | /* decrease the packet length by the length of the auth tag */ |
| 2166 | *pkt_octet_len -= tag_len; |
| 2167 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2168 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2171 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2172 | srtp_init() { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2173 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2174 | |
| 2175 | /* initialize crypto kernel */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 2176 | status = srtp_crypto_kernel_init(); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2177 | if (status) |
| 2178 | return status; |
| 2179 | |
| 2180 | /* load srtp debug module into the kernel */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 2181 | status = srtp_crypto_kernel_load_debug_module(&mod_srtp); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2182 | if (status) |
| 2183 | return status; |
| 2184 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2185 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2188 | srtp_err_status_t |
Jonathan Lennox | 5ae7633 | 2010-05-15 04:48:59 +0000 | [diff] [blame] | 2189 | srtp_shutdown() { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2190 | srtp_err_status_t status; |
Jonathan Lennox | 5ae7633 | 2010-05-15 04:48:59 +0000 | [diff] [blame] | 2191 | |
| 2192 | /* shut down crypto kernel */ |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 2193 | status = srtp_crypto_kernel_shutdown(); |
Jonathan Lennox | 5ae7633 | 2010-05-15 04:48:59 +0000 | [diff] [blame] | 2194 | if (status) |
| 2195 | return status; |
| 2196 | |
| 2197 | /* shutting down crypto kernel frees the srtp debug module as well */ |
| 2198 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2199 | return srtp_err_status_ok; |
Jonathan Lennox | 5ae7633 | 2010-05-15 04:48:59 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2203 | /* |
| 2204 | * The following code is under consideration for removal. See |
| 2205 | * SRTP_MAX_TRAILER_LEN |
| 2206 | */ |
| 2207 | #if 0 |
| 2208 | |
| 2209 | /* |
| 2210 | * srtp_get_trailer_length(&a) returns the number of octets that will |
| 2211 | * be added to an RTP packet by the SRTP processing. This value |
| 2212 | * is constant for a given srtp_stream_t (i.e. between initializations). |
| 2213 | */ |
| 2214 | |
| 2215 | int |
| 2216 | srtp_get_trailer_length(const srtp_stream_t s) { |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 2217 | return srtp_auth_get_tag_length(s->rtp_auth); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | #endif |
| 2221 | |
| 2222 | /* |
| 2223 | * srtp_get_stream(ssrc) returns a pointer to the stream corresponding |
| 2224 | * to ssrc, or NULL if no stream exists for that ssrc |
| 2225 | * |
| 2226 | * this is an internal function |
| 2227 | */ |
| 2228 | |
| 2229 | srtp_stream_ctx_t * |
| 2230 | srtp_get_stream(srtp_t srtp, uint32_t ssrc) { |
| 2231 | srtp_stream_ctx_t *stream; |
| 2232 | |
| 2233 | /* walk down list until ssrc is found */ |
| 2234 | stream = srtp->stream_list; |
| 2235 | while (stream != NULL) { |
| 2236 | if (stream->ssrc == ssrc) |
| 2237 | return stream; |
| 2238 | stream = stream->next; |
| 2239 | } |
| 2240 | |
| 2241 | /* we haven't found our ssrc, so return a null */ |
| 2242 | return NULL; |
| 2243 | } |
| 2244 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2245 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2246 | srtp_dealloc(srtp_t session) { |
| 2247 | srtp_stream_ctx_t *stream; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2248 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* |
| 2251 | * we take a conservative deallocation strategy - if we encounter an |
| 2252 | * error deallocating a stream, then we stop trying to deallocate |
| 2253 | * memory and just return an error |
| 2254 | */ |
| 2255 | |
| 2256 | /* walk list of streams, deallocating as we go */ |
| 2257 | stream = session->stream_list; |
| 2258 | while (stream != NULL) { |
| 2259 | srtp_stream_t next = stream->next; |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2260 | status = srtp_stream_dealloc(stream, session->stream_template); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2261 | if (status) |
| 2262 | return status; |
| 2263 | stream = next; |
| 2264 | } |
| 2265 | |
| 2266 | /* deallocate stream template, if there is one */ |
| 2267 | if (session->stream_template != NULL) { |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2268 | status = srtp_stream_dealloc(session->stream_template, NULL); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2269 | if (status) |
| 2270 | return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | /* deallocate session context */ |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 2274 | srtp_crypto_free(session); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2275 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2276 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2280 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2281 | srtp_add_stream(srtp_t session, |
| 2282 | const srtp_policy_t *policy) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2283 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2284 | srtp_stream_t tmp; |
| 2285 | |
Marcus Sundberg | 67398e6 | 2005-10-05 12:39:51 +0000 | [diff] [blame] | 2286 | /* sanity check arguments */ |
| 2287 | if ((session == NULL) || (policy == NULL) || (policy->key == NULL)) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2288 | return srtp_err_status_bad_param; |
Marcus Sundberg | 67398e6 | 2005-10-05 12:39:51 +0000 | [diff] [blame] | 2289 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2290 | /* allocate stream */ |
| 2291 | status = srtp_stream_alloc(&tmp, policy); |
| 2292 | if (status) { |
| 2293 | return status; |
| 2294 | } |
| 2295 | |
| 2296 | /* initialize stream */ |
| 2297 | status = srtp_stream_init(tmp, policy); |
| 2298 | if (status) { |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 2299 | srtp_crypto_free(tmp); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2300 | return status; |
| 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * set the head of the stream list or the template to point to the |
| 2305 | * stream that we've just alloced and init'ed, depending on whether |
| 2306 | * or not it has a wildcard SSRC value or not |
| 2307 | * |
| 2308 | * if the template stream has already been set, then the policy is |
| 2309 | * inconsistent, so we return a bad_param error code |
| 2310 | */ |
| 2311 | switch (policy->ssrc.type) { |
| 2312 | case (ssrc_any_outbound): |
| 2313 | if (session->stream_template) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2314 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2315 | } |
| 2316 | session->stream_template = tmp; |
| 2317 | session->stream_template->direction = dir_srtp_sender; |
| 2318 | break; |
| 2319 | case (ssrc_any_inbound): |
| 2320 | if (session->stream_template) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2321 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2322 | } |
| 2323 | session->stream_template = tmp; |
| 2324 | session->stream_template->direction = dir_srtp_receiver; |
| 2325 | break; |
| 2326 | case (ssrc_specific): |
| 2327 | tmp->next = session->stream_list; |
| 2328 | session->stream_list = tmp; |
| 2329 | break; |
| 2330 | case (ssrc_undefined): |
| 2331 | default: |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 2332 | srtp_crypto_free(tmp); |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2333 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2336 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2340 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2341 | srtp_create(srtp_t *session, /* handle for session */ |
| 2342 | const srtp_policy_t *policy) { /* SRTP policy (list) */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2343 | srtp_err_status_t stat; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2344 | srtp_ctx_t *ctx; |
| 2345 | |
| 2346 | /* sanity check arguments */ |
Marcus Sundberg | 67398e6 | 2005-10-05 12:39:51 +0000 | [diff] [blame] | 2347 | if (session == NULL) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2348 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2349 | |
| 2350 | /* allocate srtp context and set ctx_ptr */ |
jfigus | ed755f5 | 2014-11-19 14:57:19 -0500 | [diff] [blame] | 2351 | ctx = (srtp_ctx_t *) srtp_crypto_alloc(sizeof(srtp_ctx_t)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2352 | if (ctx == NULL) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2353 | return srtp_err_status_alloc_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2354 | *session = ctx; |
| 2355 | |
| 2356 | /* |
| 2357 | * loop over elements in the policy list, allocating and |
| 2358 | * initializing a stream for each element |
| 2359 | */ |
| 2360 | ctx->stream_template = NULL; |
| 2361 | ctx->stream_list = NULL; |
Iñaki Baz Castillo | 241fec3 | 2014-08-21 00:51:00 +0200 | [diff] [blame] | 2362 | ctx->user_data = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2363 | while (policy != NULL) { |
| 2364 | |
| 2365 | stat = srtp_add_stream(ctx, policy); |
| 2366 | if (stat) { |
Marcus Sundberg | 67398e6 | 2005-10-05 12:39:51 +0000 | [diff] [blame] | 2367 | /* clean up everything */ |
| 2368 | srtp_dealloc(*session); |
Joachim Bauch | 8301c36 | 2015-06-07 23:33:29 +0200 | [diff] [blame] | 2369 | *session = NULL; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2370 | return stat; |
| 2371 | } |
| 2372 | |
| 2373 | /* set policy to next item in list */ |
| 2374 | policy = policy->next; |
| 2375 | } |
| 2376 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2377 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2381 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2382 | srtp_remove_stream(srtp_t session, uint32_t ssrc) { |
| 2383 | srtp_stream_ctx_t *stream, *last_stream; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2384 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* sanity check arguments */ |
| 2387 | if (session == NULL) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2388 | return srtp_err_status_bad_param; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2389 | |
| 2390 | /* find stream in list; complain if not found */ |
| 2391 | last_stream = stream = session->stream_list; |
| 2392 | while ((stream != NULL) && (ssrc != stream->ssrc)) { |
| 2393 | last_stream = stream; |
| 2394 | stream = stream->next; |
| 2395 | } |
| 2396 | if (stream == NULL) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2397 | return srtp_err_status_no_ctx; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2398 | |
| 2399 | /* remove stream from the list */ |
Jonathan Lennox | 20505b3 | 2010-05-27 19:22:25 +0000 | [diff] [blame] | 2400 | if (last_stream == stream) |
| 2401 | /* stream was first in list */ |
| 2402 | session->stream_list = stream->next; |
| 2403 | else |
| 2404 | last_stream->next = stream->next; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2405 | |
| 2406 | /* deallocate the stream */ |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2407 | status = srtp_stream_dealloc(stream, session->stream_template); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2408 | if (status) |
| 2409 | return status; |
| 2410 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2411 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2415 | srtp_err_status_t |
| 2416 | srtp_update(srtp_t session, const srtp_policy_t *policy) { |
| 2417 | srtp_err_status_t stat; |
| 2418 | |
| 2419 | /* sanity check arguments */ |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2420 | if ((session == NULL) || (policy == NULL) || (policy->key == NULL)) { |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2421 | return srtp_err_status_bad_param; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2422 | } |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2423 | |
| 2424 | while (policy != NULL) { |
| 2425 | stat = srtp_update_stream(session, policy); |
| 2426 | if (stat) { |
| 2427 | return stat; |
| 2428 | } |
| 2429 | |
| 2430 | /* set policy to next item in list */ |
| 2431 | policy = policy->next; |
| 2432 | } |
| 2433 | return srtp_err_status_ok; |
| 2434 | } |
| 2435 | |
| 2436 | |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2437 | static srtp_err_status_t |
| 2438 | update_template_streams(srtp_t session, const srtp_policy_t *policy) { |
| 2439 | srtp_err_status_t status; |
| 2440 | srtp_stream_t new_stream_template; |
| 2441 | srtp_stream_t new_stream_list = NULL; |
| 2442 | |
| 2443 | if (session->stream_template == NULL) { |
| 2444 | return srtp_err_status_bad_param; |
| 2445 | } |
| 2446 | |
| 2447 | /* allocate new template stream */ |
| 2448 | status = srtp_stream_alloc(&new_stream_template, policy); |
| 2449 | if (status) { |
| 2450 | return status; |
| 2451 | } |
| 2452 | |
| 2453 | /* initialize new template stream */ |
| 2454 | status = srtp_stream_init(new_stream_template, policy); |
| 2455 | if (status) { |
| 2456 | srtp_crypto_free(new_stream_template); |
| 2457 | return status; |
| 2458 | } |
| 2459 | |
| 2460 | /* for all old templated streams */ |
| 2461 | for (;;) { |
| 2462 | srtp_stream_t stream; |
| 2463 | uint32_t ssrc; |
| 2464 | srtp_xtd_seq_num_t old_index; |
Pascal Bühler | e0fb3df | 2015-11-25 09:33:00 +0100 | [diff] [blame] | 2465 | srtp_rdb_t old_rtcp_rdb; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2466 | |
| 2467 | stream = session->stream_list; |
| 2468 | while ((stream != NULL) && (stream->rtp_auth != session->stream_template->rtp_auth)) { |
| 2469 | stream = stream->next; |
| 2470 | } |
| 2471 | if (stream == NULL) { |
| 2472 | /* no more templated streams */ |
| 2473 | break; |
| 2474 | } |
| 2475 | |
| 2476 | /* save old extendard seq */ |
| 2477 | ssrc = stream->ssrc; |
| 2478 | old_index = stream->rtp_rdbx.index; |
Pascal Bühler | e0fb3df | 2015-11-25 09:33:00 +0100 | [diff] [blame] | 2479 | old_rtcp_rdb = stream->rtcp_rdb; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2480 | |
| 2481 | /* remove stream */ |
| 2482 | status = srtp_remove_stream(session, ssrc); |
| 2483 | if (status) { |
| 2484 | /* free new allocations */ |
| 2485 | while (new_stream_list != NULL) { |
| 2486 | srtp_stream_t next = new_stream_list->next; |
| 2487 | srtp_stream_dealloc(new_stream_list, new_stream_template); |
| 2488 | new_stream_list = next; |
| 2489 | } |
| 2490 | srtp_stream_dealloc(new_stream_template, NULL); |
| 2491 | return status; |
| 2492 | } |
| 2493 | |
| 2494 | /* allocate and initialize a new stream */ |
| 2495 | status = srtp_stream_clone(new_stream_template, ssrc, &stream); |
| 2496 | if (status) { |
| 2497 | /* free new allocations */ |
| 2498 | while (new_stream_list != NULL) { |
| 2499 | srtp_stream_t next = new_stream_list->next; |
| 2500 | srtp_stream_dealloc(new_stream_list, new_stream_template); |
| 2501 | new_stream_list = next; |
| 2502 | } |
| 2503 | srtp_stream_dealloc(new_stream_template, NULL); |
| 2504 | return status; |
| 2505 | } |
| 2506 | |
| 2507 | /* add new stream to the head of the new_stream_list */ |
| 2508 | stream->next = new_stream_list; |
| 2509 | new_stream_list = stream; |
| 2510 | |
| 2511 | /* restore old extended seq */ |
| 2512 | stream->rtp_rdbx.index = old_index; |
Pascal Bühler | e0fb3df | 2015-11-25 09:33:00 +0100 | [diff] [blame] | 2513 | stream->rtcp_rdb = old_rtcp_rdb; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2514 | } |
| 2515 | /* dealloc old template */ |
| 2516 | srtp_stream_dealloc(session->stream_template, NULL); |
| 2517 | /* set new template */ |
| 2518 | session->stream_template = new_stream_template; |
| 2519 | /* add new list */ |
| 2520 | if (new_stream_list) { |
Pascal Bühler | 102da17 | 2015-11-23 12:49:52 +0100 | [diff] [blame] | 2521 | srtp_stream_t tail = new_stream_list; |
| 2522 | while (tail->next) { |
| 2523 | tail = tail->next; |
| 2524 | } |
| 2525 | tail->next = session->stream_list; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2526 | session->stream_list = new_stream_list; |
| 2527 | } |
| 2528 | return status; |
| 2529 | } |
| 2530 | |
| 2531 | |
| 2532 | static srtp_err_status_t |
| 2533 | update_stream(srtp_t session, const srtp_policy_t *policy) { |
| 2534 | srtp_err_status_t status; |
| 2535 | srtp_xtd_seq_num_t old_index; |
Pascal Bühler | e0fb3df | 2015-11-25 09:33:00 +0100 | [diff] [blame] | 2536 | srtp_rdb_t old_rtcp_rdb; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2537 | srtp_stream_t stream; |
| 2538 | |
| 2539 | stream = srtp_get_stream(session, policy->ssrc.value); |
| 2540 | if (stream == NULL) { |
| 2541 | return srtp_err_status_bad_param; |
| 2542 | } |
| 2543 | |
| 2544 | /* save old extendard seq */ |
| 2545 | old_index = stream->rtp_rdbx.index; |
Pascal Bühler | e0fb3df | 2015-11-25 09:33:00 +0100 | [diff] [blame] | 2546 | old_rtcp_rdb = stream->rtcp_rdb; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2547 | |
| 2548 | status = srtp_remove_stream(session, policy->ssrc.value); |
| 2549 | if (status) { |
| 2550 | return status; |
| 2551 | } |
| 2552 | |
| 2553 | status = srtp_add_stream(session, policy); |
| 2554 | if (status) { |
| 2555 | return status; |
| 2556 | } |
| 2557 | |
| 2558 | stream = srtp_get_stream(session, policy->ssrc.value); |
| 2559 | if (stream == NULL) { |
| 2560 | return srtp_err_status_fail; |
| 2561 | } |
| 2562 | |
| 2563 | /* restore old extended seq */ |
| 2564 | stream->rtp_rdbx.index = old_index; |
Pascal Bühler | e0fb3df | 2015-11-25 09:33:00 +0100 | [diff] [blame] | 2565 | stream->rtcp_rdb = old_rtcp_rdb; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2566 | |
jfigus | 49848d1 | 2015-11-19 14:54:56 -0500 | [diff] [blame] | 2567 | return srtp_err_status_ok; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2571 | srtp_err_status_t |
| 2572 | srtp_update_stream(srtp_t session, const srtp_policy_t *policy) { |
| 2573 | srtp_err_status_t status; |
| 2574 | |
| 2575 | /* sanity check arguments */ |
| 2576 | if ((session == NULL) || (policy == NULL) || (policy->key == NULL)) |
| 2577 | return srtp_err_status_bad_param; |
| 2578 | |
| 2579 | switch (policy->ssrc.type) { |
| 2580 | case (ssrc_any_outbound): |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2581 | case (ssrc_any_inbound): |
| 2582 | status = update_template_streams(session, policy); |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2583 | break; |
Pascal Bühler | 356f1cd | 2015-11-10 09:05:49 +0100 | [diff] [blame] | 2584 | case (ssrc_specific): |
| 2585 | status = update_stream(session, policy); |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2586 | break; |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2587 | case (ssrc_undefined): |
| 2588 | default: |
| 2589 | return srtp_err_status_bad_param; |
| 2590 | } |
| 2591 | |
jfigus | 49848d1 | 2015-11-19 14:54:56 -0500 | [diff] [blame] | 2592 | return status; |
Pascal Bühler | bd3112a | 2015-11-06 20:29:15 +0100 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2596 | /* |
| 2597 | * the default policy - provides a convenient way for callers to use |
| 2598 | * the default security policy |
| 2599 | * |
| 2600 | * this policy is that defined in the current SRTP internet draft. |
| 2601 | * |
| 2602 | */ |
| 2603 | |
| 2604 | /* |
| 2605 | * NOTE: cipher_key_len is really key len (128 bits) plus salt len |
| 2606 | * (112 bits) |
| 2607 | */ |
| 2608 | /* There are hard-coded 16's for base_key_len in the key generation code */ |
| 2609 | |
| 2610 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2611 | srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2612 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2613 | p->cipher_type = SRTP_AES_ICM; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2614 | p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2615 | p->auth_type = SRTP_HMAC_SHA1; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2616 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
| 2617 | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
| 2618 | p->sec_serv = sec_serv_conf_and_auth; |
| 2619 | |
| 2620 | } |
| 2621 | |
| 2622 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2623 | srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2624 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2625 | p->cipher_type = SRTP_AES_ICM; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2626 | p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2627 | p->auth_type = SRTP_HMAC_SHA1; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2628 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
| 2629 | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
| 2630 | p->sec_serv = sec_serv_conf_and_auth; |
| 2631 | |
| 2632 | } |
| 2633 | |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2634 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2635 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p) { |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2636 | |
| 2637 | /* |
Jonathan Lennox | d8d5cd0 | 2010-05-17 20:08:17 +0000 | [diff] [blame] | 2638 | * corresponds to RFC 4568 |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2639 | * |
| 2640 | * note that this crypto policy is intended for SRTP, but not SRTCP |
| 2641 | */ |
| 2642 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2643 | p->cipher_type = SRTP_AES_ICM; |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2644 | p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2645 | p->auth_type = SRTP_HMAC_SHA1; |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2646 | p->auth_key_len = 20; /* 160 bit key */ |
| 2647 | p->auth_tag_len = 4; /* 32 bit tag */ |
| 2648 | p->sec_serv = sec_serv_conf_and_auth; |
| 2649 | |
| 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2654 | srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p) { |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2655 | |
| 2656 | /* |
Jonathan Lennox | d8d5cd0 | 2010-05-17 20:08:17 +0000 | [diff] [blame] | 2657 | * corresponds to RFC 4568 |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2658 | * |
| 2659 | * note that this crypto policy is intended for SRTP, but not SRTCP |
| 2660 | */ |
| 2661 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2662 | p->cipher_type = SRTP_AES_ICM; |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2663 | p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2664 | p->auth_type = SRTP_NULL_AUTH; |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2665 | p->auth_key_len = 0; |
| 2666 | p->auth_tag_len = 0; |
| 2667 | p->sec_serv = sec_serv_conf; |
| 2668 | |
| 2669 | } |
| 2670 | |
| 2671 | |
| 2672 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2673 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p) { |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* |
Jonathan Lennox | d8d5cd0 | 2010-05-17 20:08:17 +0000 | [diff] [blame] | 2676 | * corresponds to RFC 4568 |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2677 | */ |
| 2678 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2679 | p->cipher_type = SRTP_NULL_CIPHER; |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2680 | p->cipher_key_len = 0; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2681 | p->auth_type = SRTP_HMAC_SHA1; |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2682 | p->auth_key_len = 20; |
| 2683 | p->auth_tag_len = 10; |
| 2684 | p->sec_serv = sec_serv_auth; |
| 2685 | |
| 2686 | } |
| 2687 | |
jfigus | 267956d | 2014-11-06 10:49:21 -0500 | [diff] [blame] | 2688 | void |
| 2689 | srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p) { |
| 2690 | |
| 2691 | /* |
| 2692 | * Should only be used for testing |
| 2693 | */ |
| 2694 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2695 | p->cipher_type = SRTP_NULL_CIPHER; |
jfigus | 267956d | 2014-11-06 10:49:21 -0500 | [diff] [blame] | 2696 | p->cipher_key_len = 0; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2697 | p->auth_type = SRTP_NULL_AUTH; |
jfigus | 267956d | 2014-11-06 10:49:21 -0500 | [diff] [blame] | 2698 | p->auth_key_len = 0; |
| 2699 | p->auth_tag_len = 0; |
| 2700 | p->sec_serv = sec_serv_none; |
| 2701 | |
| 2702 | } |
| 2703 | |
David McGrew | a854688 | 2006-01-12 17:56:02 +0000 | [diff] [blame] | 2704 | |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2705 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2706 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p) { |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2707 | |
| 2708 | /* |
| 2709 | * corresponds to draft-ietf-avt-big-aes-03.txt |
| 2710 | */ |
| 2711 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2712 | p->cipher_type = SRTP_AES_ICM; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2713 | p->cipher_key_len = 46; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2714 | p->auth_type = SRTP_HMAC_SHA1; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2715 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
| 2716 | p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */ |
| 2717 | p->sec_serv = sec_serv_conf_and_auth; |
| 2718 | } |
| 2719 | |
| 2720 | |
| 2721 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2722 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p) { |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2723 | |
| 2724 | /* |
| 2725 | * corresponds to draft-ietf-avt-big-aes-03.txt |
| 2726 | * |
| 2727 | * note that this crypto policy is intended for SRTP, but not SRTCP |
| 2728 | */ |
| 2729 | |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2730 | p->cipher_type = SRTP_AES_ICM; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2731 | p->cipher_key_len = 46; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2732 | p->auth_type = SRTP_HMAC_SHA1; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2733 | p->auth_key_len = 20; /* default 160 bits per RFC 3711 */ |
| 2734 | p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */ |
| 2735 | p->sec_serv = sec_serv_conf_and_auth; |
| 2736 | } |
| 2737 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2738 | /* |
| 2739 | * AES-256 with no authentication. |
| 2740 | */ |
| 2741 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2742 | srtp_crypto_policy_set_aes_cm_256_null_auth (srtp_crypto_policy_t *p) |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2743 | { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2744 | p->cipher_type = SRTP_AES_ICM; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2745 | p->cipher_key_len = 46; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2746 | p->auth_type = SRTP_NULL_AUTH; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2747 | p->auth_key_len = 0; |
| 2748 | p->auth_tag_len = 0; |
| 2749 | p->sec_serv = sec_serv_conf; |
| 2750 | } |
| 2751 | |
| 2752 | #ifdef OPENSSL |
| 2753 | /* |
| 2754 | * AES-128 GCM mode with 8 octet auth tag. |
| 2755 | */ |
| 2756 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2757 | srtp_crypto_policy_set_aes_gcm_128_8_auth(srtp_crypto_policy_t *p) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2758 | p->cipher_type = SRTP_AES_128_GCM; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2759 | p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2760 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2761 | p->auth_key_len = 0; |
| 2762 | p->auth_tag_len = 8; /* 8 octet tag length */ |
| 2763 | p->sec_serv = sec_serv_conf_and_auth; |
| 2764 | } |
| 2765 | |
| 2766 | /* |
| 2767 | * AES-256 GCM mode with 8 octet auth tag. |
| 2768 | */ |
| 2769 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2770 | srtp_crypto_policy_set_aes_gcm_256_8_auth(srtp_crypto_policy_t *p) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2771 | p->cipher_type = SRTP_AES_256_GCM; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2772 | p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2773 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2774 | p->auth_key_len = 0; |
| 2775 | p->auth_tag_len = 8; /* 8 octet tag length */ |
| 2776 | p->sec_serv = sec_serv_conf_and_auth; |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * AES-128 GCM mode with 8 octet auth tag, no RTCP encryption. |
| 2781 | */ |
| 2782 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2783 | srtp_crypto_policy_set_aes_gcm_128_8_only_auth(srtp_crypto_policy_t *p) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2784 | p->cipher_type = SRTP_AES_128_GCM; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2785 | p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2786 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2787 | p->auth_key_len = 0; |
| 2788 | p->auth_tag_len = 8; /* 8 octet tag length */ |
| 2789 | p->sec_serv = sec_serv_auth; /* This only applies to RTCP */ |
| 2790 | } |
| 2791 | |
| 2792 | /* |
| 2793 | * AES-256 GCM mode with 8 octet auth tag, no RTCP encryption. |
| 2794 | */ |
| 2795 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2796 | srtp_crypto_policy_set_aes_gcm_256_8_only_auth(srtp_crypto_policy_t *p) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2797 | p->cipher_type = SRTP_AES_256_GCM; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2798 | p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2799 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2800 | p->auth_key_len = 0; |
| 2801 | p->auth_tag_len = 8; /* 8 octet tag length */ |
| 2802 | p->sec_serv = sec_serv_auth; /* This only applies to RTCP */ |
| 2803 | } |
jfigus | c13c100 | 2014-05-08 13:34:53 -0400 | [diff] [blame] | 2804 | |
| 2805 | /* |
| 2806 | * AES-128 GCM mode with 16 octet auth tag. |
| 2807 | */ |
| 2808 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2809 | srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2810 | p->cipher_type = SRTP_AES_128_GCM; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2811 | p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2812 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
jfigus | c13c100 | 2014-05-08 13:34:53 -0400 | [diff] [blame] | 2813 | p->auth_key_len = 0; |
| 2814 | p->auth_tag_len = 16; /* 16 octet tag length */ |
| 2815 | p->sec_serv = sec_serv_conf_and_auth; |
| 2816 | } |
| 2817 | |
| 2818 | /* |
| 2819 | * AES-256 GCM mode with 16 octet auth tag. |
| 2820 | */ |
| 2821 | void |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2822 | srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p) { |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2823 | p->cipher_type = SRTP_AES_256_GCM; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2824 | p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT; |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 2825 | p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */ |
jfigus | c13c100 | 2014-05-08 13:34:53 -0400 | [diff] [blame] | 2826 | p->auth_key_len = 0; |
| 2827 | p->auth_tag_len = 16; /* 16 octet tag length */ |
| 2828 | p->sec_serv = sec_serv_conf_and_auth; |
| 2829 | } |
| 2830 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2831 | #endif |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 2832 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 2833 | /* |
| 2834 | * secure rtcp functions |
| 2835 | */ |
| 2836 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2837 | /* |
| 2838 | * AEAD uses a new IV formation method. This function implements |
| 2839 | * section 10.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The |
| 2840 | * calculation is defined as, where (+) is the xor operation: |
| 2841 | * |
| 2842 | * 0 1 2 3 4 5 6 7 8 9 10 11 |
| 2843 | * +--+--+--+--+--+--+--+--+--+--+--+--+ |
| 2844 | * |00|00| SSRC |00|00|0+SRTCP Idx|---+ |
| 2845 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 2846 | * | |
| 2847 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 2848 | * | Encryption Salt |->(+) |
| 2849 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 2850 | * | |
| 2851 | * +--+--+--+--+--+--+--+--+--+--+--+--+ | |
| 2852 | * | Initialization Vector |<--+ |
| 2853 | * +--+--+--+--+--+--+--+--+--+--+--+--+* |
| 2854 | * |
| 2855 | * Input: *stream - pointer to SRTP stream context, used to retrieve |
| 2856 | * the SALT |
| 2857 | * *iv - Pointer to recieve the calculated IV |
| 2858 | * seq_num - The SEQ value to use for the IV calculation. |
| 2859 | * *hdr - The RTP header, used to get the SSRC value |
| 2860 | * |
| 2861 | */ |
| 2862 | static void srtp_calc_aead_iv_srtcp(srtp_stream_ctx_t *stream, v128_t *iv, |
| 2863 | uint32_t seq_num, srtcp_hdr_t *hdr) |
| 2864 | { |
| 2865 | v128_t in; |
| 2866 | v128_t salt; |
| 2867 | |
| 2868 | memset(&in, 0, sizeof(v128_t)); |
| 2869 | memset(&salt, 0, sizeof(v128_t)); |
| 2870 | |
| 2871 | in.v16[0] = 0; |
| 2872 | memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */ |
| 2873 | in.v16[3] = 0; |
| 2874 | in.v32[2] = 0x7FFFFFFF & htonl(seq_num); /* bit 32 is suppose to be zero */ |
| 2875 | |
| 2876 | debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in)); |
| 2877 | |
| 2878 | /* |
| 2879 | * Get the SALT value from the context |
| 2880 | */ |
| 2881 | memcpy(salt.v8, stream->c_salt, 12); |
| 2882 | debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt)); |
| 2883 | |
| 2884 | /* |
| 2885 | * Finally, apply the SALT to the input |
| 2886 | */ |
| 2887 | v128_xor(iv, &in, &salt); |
| 2888 | } |
| 2889 | |
| 2890 | /* |
| 2891 | * This code handles AEAD ciphers for outgoing RTCP. We currently support |
| 2892 | * AES-GCM mode with 128 or 256 bit keys. |
| 2893 | */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2894 | static srtp_err_status_t |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2895 | srtp_protect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream, |
Travis Cross | 3184400 | 2014-07-02 16:18:57 +0000 | [diff] [blame] | 2896 | void *rtcp_hdr, unsigned int *pkt_octet_len) |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2897 | { |
| 2898 | srtcp_hdr_t *hdr = (srtcp_hdr_t*)rtcp_hdr; |
| 2899 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 2900 | uint32_t *trailer; /* pointer to start of trailer */ |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 2901 | unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2902 | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2903 | srtp_err_status_t status; |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 2904 | uint32_t tag_len; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2905 | uint32_t seq_num; |
| 2906 | v128_t iv; |
| 2907 | uint32_t tseq; |
| 2908 | |
| 2909 | /* get tag length from stream context */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 2910 | tag_len = srtp_auth_get_tag_length(stream->rtcp_auth); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2911 | |
| 2912 | /* |
| 2913 | * set encryption start and encryption length - if we're not |
| 2914 | * providing confidentiality, set enc_start to NULL |
| 2915 | */ |
| 2916 | enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header; |
| 2917 | enc_octet_len = *pkt_octet_len - octets_in_rtcp_header; |
| 2918 | |
| 2919 | /* NOTE: hdr->length is not usable - it refers to only the first |
| 2920 | RTCP report in the compound packet! */ |
| 2921 | /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always |
| 2922 | multiples of 32-bits (RFC 3550 6.1) */ |
| 2923 | trailer = (uint32_t*)((char*)enc_start + enc_octet_len + tag_len); |
| 2924 | |
| 2925 | if (stream->rtcp_services & sec_serv_conf) { |
| 2926 | *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */ |
| 2927 | } else { |
| 2928 | enc_start = NULL; |
| 2929 | enc_octet_len = 0; |
| 2930 | /* 0 is network-order independant */ |
| 2931 | *trailer = 0x00000000; /* set encrypt bit */ |
| 2932 | } |
| 2933 | |
| 2934 | /* |
| 2935 | * set the auth_tag pointer to the proper location, which is after |
| 2936 | * the payload, but before the trailer |
| 2937 | * (note that srtpc *always* provides authentication, unlike srtp) |
| 2938 | */ |
| 2939 | /* Note: This would need to change for optional mikey data */ |
| 2940 | auth_tag = (uint8_t*)hdr + *pkt_octet_len; |
| 2941 | |
| 2942 | /* |
| 2943 | * check sequence number for overruns, and copy it into the packet |
| 2944 | * if its value isn't too big |
| 2945 | */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 2946 | status = srtp_rdb_increment(&stream->rtcp_rdb); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2947 | if (status) { |
| 2948 | return status; |
| 2949 | } |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 2950 | seq_num = srtp_rdb_get_value(&stream->rtcp_rdb); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2951 | *trailer |= htonl(seq_num); |
| 2952 | debug_print(mod_srtp, "srtcp index: %x", seq_num); |
| 2953 | |
| 2954 | /* |
| 2955 | * Calculating the IV and pass it down to the cipher |
| 2956 | */ |
| 2957 | srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 2958 | status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_encrypt); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2959 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2960 | return srtp_err_status_cipher_fail; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* |
| 2964 | * Set the AAD for GCM mode |
| 2965 | */ |
| 2966 | if (enc_start) { |
| 2967 | /* |
| 2968 | * If payload encryption is enabled, then the AAD consist of |
| 2969 | * the RTCP header and the seq# at the end of the packet |
| 2970 | */ |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 2971 | status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2972 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2973 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2974 | } |
| 2975 | } else { |
| 2976 | /* |
| 2977 | * Since payload encryption is not enabled, we must authenticate |
| 2978 | * the entire packet as described in section 10.3 in revision 07 |
| 2979 | * of the draft. |
| 2980 | */ |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 2981 | status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, *pkt_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2982 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2983 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2984 | } |
| 2985 | } |
| 2986 | /* |
jfigus | da852b0 | 2016-01-12 09:14:46 -0500 | [diff] [blame] | 2987 | * Process the sequence# as AAD |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2988 | */ |
jfigus | da852b0 | 2016-01-12 09:14:46 -0500 | [diff] [blame] | 2989 | tseq = *trailer; |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 2990 | status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t)); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2991 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 2992 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* if we're encrypting, exor keystream into the message */ |
| 2996 | if (enc_start) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 2997 | status = srtp_cipher_encrypt(stream->rtcp_cipher, |
| 2998 | (uint8_t*)enc_start, &enc_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 2999 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3000 | return srtp_err_status_cipher_fail; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3001 | } |
| 3002 | /* |
| 3003 | * Get the tag and append that to the output |
| 3004 | */ |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 3005 | status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3006 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3007 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3008 | } |
| 3009 | enc_octet_len += tag_len; |
| 3010 | } else { |
| 3011 | /* |
| 3012 | * Even though we're not encrypting the payload, we need |
| 3013 | * to run the cipher to get the auth tag. |
| 3014 | */ |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 3015 | unsigned int nolen = 0; |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3016 | status = srtp_cipher_encrypt(stream->rtcp_cipher, NULL, &nolen); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3017 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3018 | return srtp_err_status_cipher_fail; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3019 | } |
| 3020 | /* |
| 3021 | * Get the tag and append that to the output |
| 3022 | */ |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 3023 | status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3024 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3025 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3026 | } |
| 3027 | enc_octet_len += tag_len; |
| 3028 | } |
| 3029 | |
| 3030 | /* increase the packet length by the length of the auth tag and seq_num*/ |
| 3031 | *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t)); |
| 3032 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3033 | return srtp_err_status_ok; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * This function handles incoming SRTCP packets while in AEAD mode, |
| 3038 | * which currently supports AES-GCM encryption. Note, the auth tag is |
| 3039 | * at the end of the packet stream and is automatically checked by GCM |
| 3040 | * when decrypting the payload. |
| 3041 | */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3042 | static srtp_err_status_t |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3043 | srtp_unprotect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream, |
Travis Cross | 3184400 | 2014-07-02 16:18:57 +0000 | [diff] [blame] | 3044 | void *srtcp_hdr, unsigned int *pkt_octet_len) |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3045 | { |
| 3046 | srtcp_hdr_t *hdr = (srtcp_hdr_t*)srtcp_hdr; |
| 3047 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 3048 | uint32_t *trailer; /* pointer to start of trailer */ |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 3049 | unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */ |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3050 | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3051 | srtp_err_status_t status; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3052 | int tag_len; |
| 3053 | unsigned int tmp_len; |
| 3054 | uint32_t seq_num; |
| 3055 | v128_t iv; |
| 3056 | uint32_t tseq; |
| 3057 | |
| 3058 | /* get tag length from stream context */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 3059 | tag_len = srtp_auth_get_tag_length(stream->rtcp_auth); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3060 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3061 | /* |
| 3062 | * set encryption start, encryption length, and trailer |
| 3063 | */ |
| 3064 | /* index & E (encryption) bit follow normal data. hdr->len |
| 3065 | is the number of words (32-bit) in the normal packet minus 1 */ |
| 3066 | /* This should point trailer to the word past the end of the |
| 3067 | normal data. */ |
| 3068 | /* This would need to be modified for optional mikey data */ |
| 3069 | /* |
| 3070 | * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always |
| 3071 | * multiples of 32-bits (RFC 3550 6.1) |
| 3072 | */ |
| 3073 | trailer = (uint32_t*)((char*)hdr + *pkt_octet_len - sizeof(srtcp_trailer_t)); |
| 3074 | /* |
| 3075 | * We pass the tag down to the cipher when doing GCM mode |
| 3076 | */ |
| 3077 | enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header + |
| 3078 | sizeof(srtcp_trailer_t)); |
| 3079 | auth_tag = (uint8_t*)hdr + *pkt_octet_len - tag_len - sizeof(srtcp_trailer_t); |
| 3080 | |
| 3081 | if (*((unsigned char*)trailer) & SRTCP_E_BYTE_BIT) { |
| 3082 | enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header; |
| 3083 | } else { |
| 3084 | enc_octet_len = 0; |
| 3085 | enc_start = NULL; /* this indicates that there's no encryption */ |
| 3086 | } |
| 3087 | |
| 3088 | /* |
| 3089 | * check the sequence number for replays |
| 3090 | */ |
| 3091 | /* this is easier than dealing with bitfield access */ |
| 3092 | seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK; |
| 3093 | debug_print(mod_srtp, "srtcp index: %x", seq_num); |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3094 | status = srtp_rdb_check(&stream->rtcp_rdb, seq_num); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3095 | if (status) { |
| 3096 | return status; |
| 3097 | } |
| 3098 | |
| 3099 | /* |
| 3100 | * Calculate and set the IV |
| 3101 | */ |
| 3102 | srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 3103 | status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_decrypt); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3104 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3105 | return srtp_err_status_cipher_fail; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * Set the AAD for GCM mode |
| 3110 | */ |
| 3111 | if (enc_start) { |
| 3112 | /* |
| 3113 | * If payload encryption is enabled, then the AAD consist of |
| 3114 | * the RTCP header and the seq# at the end of the packet |
| 3115 | */ |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 3116 | status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3117 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3118 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3119 | } |
| 3120 | } else { |
| 3121 | /* |
| 3122 | * Since payload encryption is not enabled, we must authenticate |
| 3123 | * the entire packet as described in section 10.3 in revision 07 |
| 3124 | * of the draft. |
| 3125 | */ |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 3126 | status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, |
| 3127 | (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t))); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3128 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3129 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | /* |
jfigus | da852b0 | 2016-01-12 09:14:46 -0500 | [diff] [blame] | 3134 | * Process the sequence# as AAD |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3135 | */ |
jfigus | da852b0 | 2016-01-12 09:14:46 -0500 | [diff] [blame] | 3136 | tseq = *trailer; |
jfigus | 226ec56 | 2014-12-02 15:11:03 -0500 | [diff] [blame] | 3137 | status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t)); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3138 | if (status) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3139 | return ( srtp_err_status_cipher_fail); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | /* if we're decrypting, exor keystream into the message */ |
| 3143 | if (enc_start) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3144 | status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t*)enc_start, &enc_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3145 | if (status) { |
| 3146 | return status; |
| 3147 | } |
| 3148 | } else { |
| 3149 | /* |
| 3150 | * Still need to run the cipher to check the tag |
| 3151 | */ |
| 3152 | tmp_len = tag_len; |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3153 | status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t*)auth_tag, &tmp_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3154 | if (status) { |
| 3155 | return status; |
| 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | /* decrease the packet length by the length of the auth tag and seq_num*/ |
| 3160 | *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t)); |
| 3161 | |
| 3162 | /* |
| 3163 | * verify that stream is for received traffic - this check will |
| 3164 | * detect SSRC collisions, since a stream that appears in both |
| 3165 | * srtp_protect() and srtp_unprotect() will fail this test in one of |
| 3166 | * those functions. |
| 3167 | * |
| 3168 | * we do this check *after* the authentication check, so that the |
| 3169 | * latter check will catch any attempts to fool us into thinking |
| 3170 | * that we've got a collision |
| 3171 | */ |
| 3172 | if (stream->direction != dir_srtp_receiver) { |
| 3173 | if (stream->direction == dir_unknown) { |
| 3174 | stream->direction = dir_srtp_receiver; |
| 3175 | } else { |
| 3176 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | /* |
| 3181 | * if the stream is a 'provisional' one, in which the template context |
| 3182 | * is used, then we need to allocate a new stream at this point, since |
| 3183 | * the authentication passed |
| 3184 | */ |
| 3185 | if (stream == ctx->stream_template) { |
| 3186 | srtp_stream_ctx_t *new_stream; |
| 3187 | |
| 3188 | /* |
| 3189 | * allocate and initialize a new stream |
| 3190 | * |
| 3191 | * note that we indicate failure if we can't allocate the new |
| 3192 | * stream, and some implementations will want to not return |
| 3193 | * failure here |
| 3194 | */ |
| 3195 | status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
| 3196 | if (status) { |
| 3197 | return status; |
| 3198 | } |
| 3199 | |
| 3200 | /* add new stream to the head of the stream_list */ |
| 3201 | new_stream->next = ctx->stream_list; |
| 3202 | ctx->stream_list = new_stream; |
| 3203 | |
| 3204 | /* set stream (the pointer used in this function) */ |
| 3205 | stream = new_stream; |
| 3206 | } |
| 3207 | |
| 3208 | /* we've passed the authentication check, so add seq_num to the rdb */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3209 | srtp_rdb_add_index(&stream->rtcp_rdb, seq_num); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3210 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3211 | return srtp_err_status_ok; |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3212 | } |
| 3213 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3214 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3215 | srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len) { |
Derek MacDonald | 17127da | 2006-07-12 22:22:08 +0000 | [diff] [blame] | 3216 | srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3217 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 3218 | uint32_t *auth_start; /* pointer to start of auth. portion */ |
| 3219 | uint32_t *trailer; /* pointer to start of trailer */ |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 3220 | unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 3221 | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3222 | srtp_err_status_t status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3223 | int tag_len; |
| 3224 | srtp_stream_ctx_t *stream; |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3225 | uint32_t prefix_len; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3226 | uint32_t seq_num; |
| 3227 | |
| 3228 | /* we assume the hdr is 32-bit aligned to start */ |
Travis Cross | 8ba46eb | 2014-06-29 18:42:29 +0000 | [diff] [blame] | 3229 | |
| 3230 | /* check the packet length - it must at least contain a full header */ |
| 3231 | if (*pkt_octet_len < octets_in_rtcp_header) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3232 | return srtp_err_status_bad_param; |
Travis Cross | 8ba46eb | 2014-06-29 18:42:29 +0000 | [diff] [blame] | 3233 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3234 | /* |
| 3235 | * look up ssrc in srtp_stream list, and process the packet with |
| 3236 | * the appropriate stream. if we haven't seen this stream before, |
| 3237 | * there's only one key for this srtp_session, and the cipher |
| 3238 | * supports key-sharing, then we assume that a new stream using |
| 3239 | * that key has just started up |
| 3240 | */ |
| 3241 | stream = srtp_get_stream(ctx, hdr->ssrc); |
| 3242 | if (stream == NULL) { |
| 3243 | if (ctx->stream_template != NULL) { |
| 3244 | srtp_stream_ctx_t *new_stream; |
| 3245 | |
| 3246 | /* allocate and initialize a new stream */ |
| 3247 | status = srtp_stream_clone(ctx->stream_template, |
| 3248 | hdr->ssrc, &new_stream); |
| 3249 | if (status) |
| 3250 | return status; |
| 3251 | |
| 3252 | /* add new stream to the head of the stream_list */ |
| 3253 | new_stream->next = ctx->stream_list; |
| 3254 | ctx->stream_list = new_stream; |
| 3255 | |
| 3256 | /* set stream (the pointer used in this function) */ |
| 3257 | stream = new_stream; |
| 3258 | } else { |
| 3259 | /* no template stream, so we return an error */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3260 | return srtp_err_status_no_ctx; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | /* |
| 3265 | * verify that stream is for sending traffic - this check will |
| 3266 | * detect SSRC collisions, since a stream that appears in both |
| 3267 | * srtp_protect() and srtp_unprotect() will fail this test in one of |
| 3268 | * those functions. |
| 3269 | */ |
| 3270 | if (stream->direction != dir_srtp_sender) { |
| 3271 | if (stream->direction == dir_unknown) { |
David McGrew | c34f740 | 2006-03-09 21:17:00 +0000 | [diff] [blame] | 3272 | stream->direction = dir_srtp_sender; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3273 | } else { |
| 3274 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
| 3275 | } |
| 3276 | } |
| 3277 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3278 | /* |
| 3279 | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
| 3280 | * the request to our AEAD handler. |
| 3281 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 3282 | if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM || |
| 3283 | stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) { |
Travis Cross | 3184400 | 2014-07-02 16:18:57 +0000 | [diff] [blame] | 3284 | return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr, (unsigned int*)pkt_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3285 | } |
| 3286 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3287 | /* get tag length from stream context */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 3288 | tag_len = srtp_auth_get_tag_length(stream->rtcp_auth); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3289 | |
| 3290 | /* |
| 3291 | * set encryption start and encryption length - if we're not |
| 3292 | * providing confidentiality, set enc_start to NULL |
| 3293 | */ |
| 3294 | enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header; |
| 3295 | enc_octet_len = *pkt_octet_len - octets_in_rtcp_header; |
| 3296 | |
| 3297 | /* all of the packet, except the header, gets encrypted */ |
| 3298 | /* NOTE: hdr->length is not usable - it refers to only the first |
| 3299 | RTCP report in the compound packet! */ |
| 3300 | /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always |
| 3301 | multiples of 32-bits (RFC 3550 6.1) */ |
| 3302 | trailer = (uint32_t *) ((char *)enc_start + enc_octet_len); |
| 3303 | |
| 3304 | if (stream->rtcp_services & sec_serv_conf) { |
| 3305 | *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */ |
| 3306 | } else { |
| 3307 | enc_start = NULL; |
| 3308 | enc_octet_len = 0; |
| 3309 | /* 0 is network-order independant */ |
| 3310 | *trailer = 0x00000000; /* set encrypt bit */ |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * set the auth_start and auth_tag pointers to the proper locations |
| 3315 | * (note that srtpc *always* provides authentication, unlike srtp) |
| 3316 | */ |
| 3317 | /* Note: This would need to change for optional mikey data */ |
| 3318 | auth_start = (uint32_t *)hdr; |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 3319 | auth_tag = (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3320 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3321 | /* perform EKT processing if needed */ |
jfigus | c5887e7 | 2014-11-06 09:46:18 -0500 | [diff] [blame] | 3322 | srtp_ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len, |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3323 | srtp_rdbx_get_packet_index(&stream->rtp_rdbx)); |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3324 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3325 | /* |
| 3326 | * check sequence number for overruns, and copy it into the packet |
| 3327 | * if its value isn't too big |
| 3328 | */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3329 | status = srtp_rdb_increment(&stream->rtcp_rdb); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3330 | if (status) |
| 3331 | return status; |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3332 | seq_num = srtp_rdb_get_value(&stream->rtcp_rdb); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3333 | *trailer |= htonl(seq_num); |
| 3334 | debug_print(mod_srtp, "srtcp index: %x", seq_num); |
| 3335 | |
| 3336 | /* |
| 3337 | * if we're using rindael counter mode, set nonce and seq |
| 3338 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 3339 | if (stream->rtcp_cipher->type->id == SRTP_AES_ICM) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3340 | v128_t iv; |
| 3341 | |
| 3342 | iv.v32[0] = 0; |
| 3343 | iv.v32[1] = hdr->ssrc; /* still in network order! */ |
| 3344 | iv.v32[2] = htonl(seq_num >> 16); |
| 3345 | iv.v32[3] = htonl(seq_num << 16); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 3346 | status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_encrypt); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3347 | |
| 3348 | } else { |
| 3349 | v128_t iv; |
| 3350 | |
| 3351 | /* otherwise, just set the index to seq_num */ |
| 3352 | iv.v32[0] = 0; |
| 3353 | iv.v32[1] = 0; |
| 3354 | iv.v32[2] = 0; |
| 3355 | iv.v32[3] = htonl(seq_num); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 3356 | status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_encrypt); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3357 | } |
| 3358 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3359 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3360 | |
| 3361 | /* |
| 3362 | * if we're authenticating using a universal hash, put the keystream |
| 3363 | * prefix into the authentication tag |
| 3364 | */ |
| 3365 | |
| 3366 | /* if auth_start is non-null, then put keystream into tag */ |
| 3367 | if (auth_start) { |
| 3368 | |
| 3369 | /* put keystream prefix into auth_tag */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 3370 | prefix_len = srtp_auth_get_prefix_length(stream->rtcp_auth); |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3371 | status = srtp_cipher_output(stream->rtcp_cipher, auth_tag, &prefix_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3372 | |
| 3373 | debug_print(mod_srtp, "keystream prefix: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3374 | srtp_octet_string_hex_string(auth_tag, prefix_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3375 | |
| 3376 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3377 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | /* if we're encrypting, exor keystream into the message */ |
| 3381 | if (enc_start) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3382 | status = srtp_cipher_encrypt(stream->rtcp_cipher, |
| 3383 | (uint8_t *)enc_start, &enc_octet_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3384 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3385 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | /* initialize auth func context */ |
| 3389 | auth_start(stream->rtcp_auth); |
| 3390 | |
David McGrew | 9c70f29 | 2006-05-03 19:38:38 +0000 | [diff] [blame] | 3391 | /* |
| 3392 | * run auth func over packet (including trailer), and write the |
| 3393 | * result at auth_tag |
| 3394 | */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3395 | status = auth_compute(stream->rtcp_auth, |
David McGrew | 9c70f29 | 2006-05-03 19:38:38 +0000 | [diff] [blame] | 3396 | (uint8_t *)auth_start, |
| 3397 | (*pkt_octet_len) + sizeof(srtcp_trailer_t), |
| 3398 | auth_tag); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3399 | debug_print(mod_srtp, "srtcp auth tag: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3400 | srtp_octet_string_hex_string(auth_tag, tag_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3401 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3402 | return srtp_err_status_auth_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3403 | |
| 3404 | /* increase the packet length by the length of the auth tag and seq_num*/ |
| 3405 | *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t)); |
| 3406 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3407 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3411 | srtp_err_status_t |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3412 | srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len) { |
Derek MacDonald | 17127da | 2006-07-12 22:22:08 +0000 | [diff] [blame] | 3413 | srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3414 | uint32_t *enc_start; /* pointer to start of encrypted portion */ |
| 3415 | uint32_t *auth_start; /* pointer to start of auth. portion */ |
| 3416 | uint32_t *trailer; /* pointer to start of trailer */ |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 3417 | unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 3418 | uint8_t *auth_tag = NULL; /* location of auth_tag within packet */ |
| 3419 | uint8_t tmp_tag[SRTP_MAX_TAG_LEN]; |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3420 | uint8_t tag_copy[SRTP_MAX_TAG_LEN]; |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3421 | srtp_err_status_t status; |
Travis Cross | 1b8b1e7 | 2014-07-02 15:32:36 +0000 | [diff] [blame] | 3422 | unsigned int auth_len; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3423 | int tag_len; |
| 3424 | srtp_stream_ctx_t *stream; |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3425 | uint32_t prefix_len; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3426 | uint32_t seq_num; |
TV Sriram | 4986a36 | 2013-05-06 11:24:03 -0700 | [diff] [blame] | 3427 | int e_bit_in_packet; /* whether the E-bit was found in the packet */ |
| 3428 | int sec_serv_confidentiality; /* whether confidentiality was requested */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3429 | |
| 3430 | /* we assume the hdr is 32-bit aligned to start */ |
Travis Cross | 444a544 | 2014-07-02 16:01:01 +0000 | [diff] [blame] | 3431 | |
Travis Cross | e896bf7 | 2014-07-07 19:59:33 +0000 | [diff] [blame] | 3432 | /* check that the length value is sane; we'll check again once we |
| 3433 | know the tag length, but we at least want to know that it is |
| 3434 | a positive value */ |
| 3435 | if (*pkt_octet_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t)) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3436 | return srtp_err_status_bad_param; |
Travis Cross | e896bf7 | 2014-07-07 19:59:33 +0000 | [diff] [blame] | 3437 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3438 | /* |
| 3439 | * look up ssrc in srtp_stream list, and process the packet with |
| 3440 | * the appropriate stream. if we haven't seen this stream before, |
| 3441 | * there's only one key for this srtp_session, and the cipher |
| 3442 | * supports key-sharing, then we assume that a new stream using |
| 3443 | * that key has just started up |
| 3444 | */ |
| 3445 | stream = srtp_get_stream(ctx, hdr->ssrc); |
| 3446 | if (stream == NULL) { |
| 3447 | if (ctx->stream_template != NULL) { |
| 3448 | stream = ctx->stream_template; |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3449 | |
| 3450 | /* |
| 3451 | * check to see if stream_template has an EKT data structure, in |
| 3452 | * which case we initialize the template using the EKT policy |
| 3453 | * referenced by that data (which consists of decrypting the |
| 3454 | * master key from the EKT field) |
| 3455 | * |
| 3456 | * this function initializes a *provisional* stream, and this |
| 3457 | * stream should not be accepted until and unless the packet |
| 3458 | * passes its authentication check |
| 3459 | */ |
| 3460 | if (stream->ekt != NULL) { |
| 3461 | status = srtp_stream_init_from_ekt(stream, srtcp_hdr, *pkt_octet_len); |
| 3462 | if (status) |
| 3463 | return status; |
| 3464 | } |
| 3465 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3466 | debug_print(mod_srtp, "srtcp using provisional stream (SSRC: 0x%08x)", |
| 3467 | hdr->ssrc); |
| 3468 | } else { |
| 3469 | /* no template stream, so we return an error */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3470 | return srtp_err_status_no_ctx; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | |
Travis Cross | 8ba46eb | 2014-06-29 18:42:29 +0000 | [diff] [blame] | 3474 | /* get tag length from stream context */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 3475 | tag_len = srtp_auth_get_tag_length(stream->rtcp_auth); |
Travis Cross | 8ba46eb | 2014-06-29 18:42:29 +0000 | [diff] [blame] | 3476 | |
| 3477 | /* check the packet length - it must contain at least a full RTCP |
| 3478 | header, an auth tag (if applicable), and the SRTCP encrypted flag |
| 3479 | and 31-bit index value */ |
Joachim Bauch | 25a0e6c | 2015-03-27 16:59:06 +0100 | [diff] [blame] | 3480 | if (*pkt_octet_len < (int) (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t))) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3481 | return srtp_err_status_bad_param; |
jfigus | 73e3093 | 2014-07-07 15:50:32 -0400 | [diff] [blame] | 3482 | } |
Travis Cross | 8ba46eb | 2014-06-29 18:42:29 +0000 | [diff] [blame] | 3483 | |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3484 | /* |
| 3485 | * Check if this is an AEAD stream (GCM mode). If so, then dispatch |
| 3486 | * the request to our AEAD handler. |
| 3487 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 3488 | if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM || |
| 3489 | stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) { |
Travis Cross | 3184400 | 2014-07-02 16:18:57 +0000 | [diff] [blame] | 3490 | return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr, (unsigned int*)pkt_octet_len); |
jfigus | 8c36da2 | 2013-10-01 16:41:19 -0400 | [diff] [blame] | 3491 | } |
| 3492 | |
TV Sriram | 4986a36 | 2013-05-06 11:24:03 -0700 | [diff] [blame] | 3493 | sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf || |
| 3494 | stream->rtcp_services == sec_serv_conf_and_auth; |
| 3495 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3496 | /* |
| 3497 | * set encryption start, encryption length, and trailer |
| 3498 | */ |
| 3499 | enc_octet_len = *pkt_octet_len - |
| 3500 | (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t)); |
| 3501 | /* index & E (encryption) bit follow normal data. hdr->len |
| 3502 | is the number of words (32-bit) in the normal packet minus 1 */ |
| 3503 | /* This should point trailer to the word past the end of the |
| 3504 | normal data. */ |
| 3505 | /* This would need to be modified for optional mikey data */ |
| 3506 | /* |
| 3507 | * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always |
| 3508 | * multiples of 32-bits (RFC 3550 6.1) |
| 3509 | */ |
| 3510 | trailer = (uint32_t *) ((char *) hdr + |
TV Sriram | 4986a36 | 2013-05-06 11:24:03 -0700 | [diff] [blame] | 3511 | *pkt_octet_len -(tag_len + sizeof(srtcp_trailer_t))); |
| 3512 | e_bit_in_packet = |
| 3513 | (*((unsigned char *) trailer) & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT; |
| 3514 | if (e_bit_in_packet != sec_serv_confidentiality) { |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3515 | return srtp_err_status_cant_check; |
TV Sriram | 4986a36 | 2013-05-06 11:24:03 -0700 | [diff] [blame] | 3516 | } |
| 3517 | if (sec_serv_confidentiality) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3518 | enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header; |
| 3519 | } else { |
| 3520 | enc_octet_len = 0; |
| 3521 | enc_start = NULL; /* this indicates that there's no encryption */ |
| 3522 | } |
| 3523 | |
| 3524 | /* |
| 3525 | * set the auth_start and auth_tag pointers to the proper locations |
| 3526 | * (note that srtcp *always* uses authentication, unlike srtp) |
| 3527 | */ |
| 3528 | auth_start = (uint32_t *)hdr; |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3529 | auth_len = *pkt_octet_len - tag_len; |
| 3530 | auth_tag = (uint8_t *)hdr + auth_len; |
| 3531 | |
| 3532 | /* |
| 3533 | * if EKT is in use, then we make a copy of the tag from the packet, |
| 3534 | * and then zeroize the location of the base tag |
| 3535 | * |
| 3536 | * we first re-position the auth_tag pointer so that it points to |
| 3537 | * the base tag |
| 3538 | */ |
| 3539 | if (stream->ekt) { |
jfigus | c5887e7 | 2014-11-06 09:46:18 -0500 | [diff] [blame] | 3540 | auth_tag -= srtp_ekt_octets_after_base_tag(stream->ekt); |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3541 | memcpy(tag_copy, auth_tag, tag_len); |
| 3542 | octet_string_set_to_zero(auth_tag, tag_len); |
| 3543 | auth_tag = tag_copy; |
| 3544 | auth_len += tag_len; |
| 3545 | } |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3546 | |
| 3547 | /* |
| 3548 | * check the sequence number for replays |
| 3549 | */ |
| 3550 | /* this is easier than dealing with bitfield access */ |
| 3551 | seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK; |
David McGrew | 9c70f29 | 2006-05-03 19:38:38 +0000 | [diff] [blame] | 3552 | debug_print(mod_srtp, "srtcp index: %x", seq_num); |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3553 | status = srtp_rdb_check(&stream->rtcp_rdb, seq_num); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3554 | if (status) |
| 3555 | return status; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3556 | |
| 3557 | /* |
| 3558 | * if we're using aes counter mode, set nonce and seq |
| 3559 | */ |
jfigus | 67b9c73 | 2014-11-20 10:17:21 -0500 | [diff] [blame] | 3560 | if (stream->rtcp_cipher->type->id == SRTP_AES_ICM) { |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3561 | v128_t iv; |
| 3562 | |
| 3563 | iv.v32[0] = 0; |
| 3564 | iv.v32[1] = hdr->ssrc; /* still in network order! */ |
| 3565 | iv.v32[2] = htonl(seq_num >> 16); |
| 3566 | iv.v32[3] = htonl(seq_num << 16); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 3567 | status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_decrypt); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3568 | |
| 3569 | } else { |
| 3570 | v128_t iv; |
| 3571 | |
| 3572 | /* otherwise, just set the index to seq_num */ |
| 3573 | iv.v32[0] = 0; |
| 3574 | iv.v32[1] = 0; |
| 3575 | iv.v32[2] = 0; |
| 3576 | iv.v32[3] = htonl(seq_num); |
persmule | bfec1cd | 2015-10-24 02:29:57 +0800 | [diff] [blame] | 3577 | status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_decrypt); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3578 | |
| 3579 | } |
| 3580 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3581 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3582 | |
| 3583 | /* initialize auth func context */ |
| 3584 | auth_start(stream->rtcp_auth); |
| 3585 | |
| 3586 | /* run auth func over packet, put result into tmp_tag */ |
Marcus Sundberg | 410faaa | 2005-09-29 12:36:43 +0000 | [diff] [blame] | 3587 | status = auth_compute(stream->rtcp_auth, (uint8_t *)auth_start, |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3588 | auth_len, tmp_tag); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3589 | debug_print(mod_srtp, "srtcp computed tag: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3590 | srtp_octet_string_hex_string(tmp_tag, tag_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3591 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3592 | return srtp_err_status_auth_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3593 | |
| 3594 | /* compare the tag just computed with the one in the packet */ |
| 3595 | debug_print(mod_srtp, "srtcp tag from packet: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3596 | srtp_octet_string_hex_string(auth_tag, tag_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3597 | if (octet_string_is_eq(tmp_tag, auth_tag, tag_len)) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3598 | return srtp_err_status_auth_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* |
| 3601 | * if we're authenticating using a universal hash, put the keystream |
| 3602 | * prefix into the authentication tag |
| 3603 | */ |
jfigus | 8f66972 | 2014-11-19 15:20:03 -0500 | [diff] [blame] | 3604 | prefix_len = srtp_auth_get_prefix_length(stream->rtcp_auth); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3605 | if (prefix_len) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3606 | status = srtp_cipher_output(stream->rtcp_cipher, auth_tag, &prefix_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3607 | debug_print(mod_srtp, "keystream prefix: %s", |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3608 | srtp_octet_string_hex_string(auth_tag, prefix_len)); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3609 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3610 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | /* if we're decrypting, exor keystream into the message */ |
| 3614 | if (enc_start) { |
jfigus | 2964a15 | 2014-11-25 15:25:37 -0500 | [diff] [blame] | 3615 | status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t *)enc_start, &enc_octet_len); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3616 | if (status) |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3617 | return srtp_err_status_cipher_fail; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3620 | /* decrease the packet length by the length of the auth tag and seq_num */ |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3621 | *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t)); |
| 3622 | |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3623 | /* |
| 3624 | * if EKT is in effect, subtract the EKT data out of the packet |
| 3625 | * length |
| 3626 | */ |
jfigus | c5887e7 | 2014-11-06 09:46:18 -0500 | [diff] [blame] | 3627 | *pkt_octet_len -= srtp_ekt_octets_after_base_tag(stream->ekt); |
David McGrew | 79870d6 | 2007-06-15 18:17:39 +0000 | [diff] [blame] | 3628 | |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3629 | /* |
| 3630 | * verify that stream is for received traffic - this check will |
| 3631 | * detect SSRC collisions, since a stream that appears in both |
| 3632 | * srtp_protect() and srtp_unprotect() will fail this test in one of |
| 3633 | * those functions. |
| 3634 | * |
| 3635 | * we do this check *after* the authentication check, so that the |
| 3636 | * latter check will catch any attempts to fool us into thinking |
| 3637 | * that we've got a collision |
| 3638 | */ |
| 3639 | if (stream->direction != dir_srtp_receiver) { |
| 3640 | if (stream->direction == dir_unknown) { |
| 3641 | stream->direction = dir_srtp_receiver; |
| 3642 | } else { |
| 3643 | srtp_handle_event(ctx, stream, event_ssrc_collision); |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | /* |
| 3648 | * if the stream is a 'provisional' one, in which the template context |
| 3649 | * is used, then we need to allocate a new stream at this point, since |
| 3650 | * the authentication passed |
| 3651 | */ |
| 3652 | if (stream == ctx->stream_template) { |
| 3653 | srtp_stream_ctx_t *new_stream; |
| 3654 | |
| 3655 | /* |
| 3656 | * allocate and initialize a new stream |
| 3657 | * |
| 3658 | * note that we indicate failure if we can't allocate the new |
| 3659 | * stream, and some implementations will want to not return |
| 3660 | * failure here |
| 3661 | */ |
| 3662 | status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream); |
| 3663 | if (status) |
| 3664 | return status; |
| 3665 | |
| 3666 | /* add new stream to the head of the stream_list */ |
| 3667 | new_stream->next = ctx->stream_list; |
| 3668 | ctx->stream_list = new_stream; |
| 3669 | |
| 3670 | /* set stream (the pointer used in this function) */ |
| 3671 | stream = new_stream; |
| 3672 | } |
| 3673 | |
| 3674 | /* we've passed the authentication check, so add seq_num to the rdb */ |
jfigus | de8deb3 | 2014-11-25 12:58:11 -0500 | [diff] [blame] | 3675 | srtp_rdb_add_index(&stream->rtcp_rdb, seq_num); |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3676 | |
| 3677 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3678 | return srtp_err_status_ok; |
Cullen Jennings | 235513a | 2005-09-21 22:51:36 +0000 | [diff] [blame] | 3679 | } |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3680 | |
| 3681 | |
Iñaki Baz Castillo | 241fec3 | 2014-08-21 00:51:00 +0200 | [diff] [blame] | 3682 | /* |
| 3683 | * user data within srtp_t context |
| 3684 | */ |
| 3685 | |
| 3686 | void |
| 3687 | srtp_set_user_data(srtp_t ctx, void *data) { |
| 3688 | ctx->user_data = data; |
| 3689 | } |
| 3690 | |
| 3691 | void* |
| 3692 | srtp_get_user_data(srtp_t ctx) { |
| 3693 | return ctx->user_data; |
| 3694 | } |
| 3695 | |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3696 | |
| 3697 | /* |
| 3698 | * dtls keying for srtp |
| 3699 | */ |
| 3700 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3701 | srtp_err_status_t |
| 3702 | srtp_crypto_policy_set_from_profile_for_rtp(srtp_crypto_policy_t *policy, |
| 3703 | srtp_profile_t profile) { |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3704 | |
| 3705 | /* set SRTP policy from the SRTP profile in the key set */ |
| 3706 | switch(profile) { |
| 3707 | case srtp_profile_aes128_cm_sha1_80: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3708 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | case srtp_profile_aes128_cm_sha1_32: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3711 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3712 | break; |
| 3713 | case srtp_profile_null_sha1_80: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3714 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3715 | break; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3716 | case srtp_profile_aes256_cm_sha1_80: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3717 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3718 | break; |
| 3719 | case srtp_profile_aes256_cm_sha1_32: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3720 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(policy); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3721 | break; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3722 | /* the following profiles are not (yet) supported */ |
| 3723 | case srtp_profile_null_sha1_32: |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3724 | default: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3725 | return srtp_err_status_bad_param; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3728 | return srtp_err_status_ok; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3731 | srtp_err_status_t |
| 3732 | srtp_crypto_policy_set_from_profile_for_rtcp(srtp_crypto_policy_t *policy, |
| 3733 | srtp_profile_t profile) { |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3734 | |
| 3735 | /* set SRTP policy from the SRTP profile in the key set */ |
| 3736 | switch(profile) { |
| 3737 | case srtp_profile_aes128_cm_sha1_80: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3738 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3739 | break; |
| 3740 | case srtp_profile_aes128_cm_sha1_32: |
jfigus | 0acbb03 | 2013-05-30 16:47:02 -0400 | [diff] [blame] | 3741 | /* We do not honor the 32-bit auth tag request since |
| 3742 | * this is not compliant with RFC 3711 */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3743 | srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3744 | break; |
| 3745 | case srtp_profile_null_sha1_80: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3746 | srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3747 | break; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3748 | case srtp_profile_aes256_cm_sha1_80: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3749 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | case srtp_profile_aes256_cm_sha1_32: |
jfigus | 0acbb03 | 2013-05-30 16:47:02 -0400 | [diff] [blame] | 3752 | /* We do not honor the 32-bit auth tag request since |
| 3753 | * this is not compliant with RFC 3711 */ |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3754 | srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy); |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3755 | break; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3756 | /* the following profiles are not (yet) supported */ |
| 3757 | case srtp_profile_null_sha1_32: |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3758 | default: |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3759 | return srtp_err_status_bad_param; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3762 | return srtp_err_status_ok; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
jfigus | 857009c | 2014-11-05 11:17:43 -0500 | [diff] [blame] | 3765 | void srtp_append_salt_to_key(uint8_t *key, unsigned int bytes_in_key, uint8_t *salt, unsigned int bytes_in_salt) { |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3766 | memcpy(key + bytes_in_key, salt, bytes_in_salt); |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | unsigned int |
| 3770 | srtp_profile_get_master_key_length(srtp_profile_t profile) { |
| 3771 | |
| 3772 | switch(profile) { |
| 3773 | case srtp_profile_aes128_cm_sha1_80: |
| 3774 | return 16; |
| 3775 | break; |
| 3776 | case srtp_profile_aes128_cm_sha1_32: |
| 3777 | return 16; |
| 3778 | break; |
| 3779 | case srtp_profile_null_sha1_80: |
| 3780 | return 16; |
| 3781 | break; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3782 | case srtp_profile_aes256_cm_sha1_80: |
| 3783 | return 32; |
| 3784 | break; |
| 3785 | case srtp_profile_aes256_cm_sha1_32: |
| 3786 | return 32; |
| 3787 | break; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3788 | /* the following profiles are not (yet) supported */ |
| 3789 | case srtp_profile_null_sha1_32: |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3790 | default: |
| 3791 | return 0; /* indicate error by returning a zero */ |
| 3792 | } |
| 3793 | } |
| 3794 | |
| 3795 | unsigned int |
| 3796 | srtp_profile_get_master_salt_length(srtp_profile_t profile) { |
| 3797 | |
| 3798 | switch(profile) { |
| 3799 | case srtp_profile_aes128_cm_sha1_80: |
| 3800 | return 14; |
| 3801 | break; |
| 3802 | case srtp_profile_aes128_cm_sha1_32: |
| 3803 | return 14; |
| 3804 | break; |
| 3805 | case srtp_profile_null_sha1_80: |
| 3806 | return 14; |
| 3807 | break; |
Jonathan Lennox | 5df951a | 2010-05-20 20:55:54 +0000 | [diff] [blame] | 3808 | case srtp_profile_aes256_cm_sha1_80: |
| 3809 | return 14; |
| 3810 | break; |
| 3811 | case srtp_profile_aes256_cm_sha1_32: |
| 3812 | return 14; |
| 3813 | break; |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3814 | /* the following profiles are not (yet) supported */ |
| 3815 | case srtp_profile_null_sha1_32: |
David McGrew | 0cb86ee | 2006-07-07 15:46:57 +0000 | [diff] [blame] | 3816 | default: |
| 3817 | return 0; /* indicate error by returning a zero */ |
| 3818 | } |
| 3819 | } |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3820 | |
| 3821 | /* |
| 3822 | * SRTP debug interface |
| 3823 | */ |
| 3824 | srtp_err_status_t srtp_set_debug_module(char *mod_name, int v) |
| 3825 | { |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 3826 | return srtp_crypto_kernel_set_debug_module(mod_name, v); |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | srtp_err_status_t srtp_list_debug_modules(void) |
| 3830 | { |
jfigus | 92736bc | 2014-11-21 10:30:54 -0500 | [diff] [blame] | 3831 | return srtp_crypto_kernel_list_debug_modules(); |
jfigus | 46d6b47 | 2014-11-14 16:42:01 -0500 | [diff] [blame] | 3832 | } |
| 3833 | |