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