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