blob: 0d7d98810a603ade2638a3a85d93d68fac0d8ea3 [file] [log] [blame]
Cullen Jennings235513a2005-09-21 22:51:36 +00001/*
2 * srtp.c
3 *
4 * the secure real-time transport protocol
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9/*
10 *
David McGrew7629bf22006-06-08 17:00:25 +000011 * Copyright (c) 2001-2006, Cisco Systems, Inc.
Cullen Jennings235513a2005-09-21 22:51:36 +000012 * 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
jfigusa9ac8982014-10-31 14:49:31 -040045#include "srtp.h"
Teerapap Changwichukarn6cffe242014-09-24 11:24:07 +080046#include "srtp_priv.h"
jfigusa9ac8982014-10-31 14:49:31 -040047#include "crypto_types.h"
48#include "err.h"
David McGrew79870d62007-06-15 18:17:39 +000049#include "ekt.h" /* for SRTP Encrypted Key Transport */
jfigused755f52014-11-19 14:57:19 -050050#include "alloc.h" /* for srtp_crypto_alloc() */
jfigus8719f952014-04-08 09:15:49 -040051#ifdef OPENSSL
52#include "aes_gcm_ossl.h" /* for AES GCM mode */
jfigus038d2cf2015-05-11 14:10:11 -040053# ifdef OPENSSL_KDF
54# include <openssl/kdf.h>
55# include "aes_icm_ossl.h" /* for AES GCM mode */
56# endif
jfigus8719f952014-04-08 09:15:49 -040057#endif
Cullen Jennings235513a2005-09-21 22:51:36 +000058
jfigusa6cf2082014-11-21 10:04:03 -050059#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 Sundberge4e34f92005-10-02 20:19:35 +000065
66
Cullen Jennings235513a2005-09-21 22:51:36 +000067/* the debug module for srtp */
68
jfigus02d6f032014-11-21 10:56:42 -050069srtp_debug_module_t mod_srtp = {
Cullen Jennings235513a2005-09-21 22:51:36 +000070 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 Bauch557a7872015-02-19 01:36:48 +010078#define octets_in_rtp_extn_hdr 4
79
jfigus724bb292015-02-19 06:43:53 -050080static srtp_err_status_t
Joachim Bauch557a7872015-02-19 01:36:48 +010081srtp_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)
jfigus724bb292015-02-19 06:43:53 -050090 return srtp_err_status_bad_param;
Joachim Bauch557a7872015-02-19 01:36:48 +010091
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)
jfigus724bb292015-02-19 06:43:53 -0500100 return srtp_err_status_bad_param;
Joachim Bauch557a7872015-02-19 01:36:48 +0100101 }
jfigus724bb292015-02-19 06:43:53 -0500102 return srtp_err_status_ok;
Joachim Bauch557a7872015-02-19 01:36:48 +0100103}
Cullen Jennings235513a2005-09-21 22:51:36 +0000104
Christian Oiend4e3eec2014-10-24 10:14:08 +0200105const char *srtp_get_version_string ()
jfigusf62b64d2014-10-08 13:53:57 -0400106{
107 /*
108 * Simply return the autotools generated string
109 */
110 return SRTP_VER_STRING;
111}
112
113unsigned int srtp_get_version ()
114{
115 unsigned int major = 0, minor = 0, micro = 0;
116 unsigned int rv = 0;
jfigusb2edbef2014-10-13 10:15:15 -0400117 int parse_rv;
jfigusf62b64d2014-10-08 13:53:57 -0400118
119 /*
120 * Parse the autotools generated version
121 */
jfigusb2edbef2014-10-13 10:15:15 -0400122 parse_rv = sscanf(SRTP_VERSION, "%u.%u.%u", &major, &minor, &micro);
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 }
jfigusf62b64d2014-10-08 13:53:57 -0400131
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 Jennings235513a2005-09-21 22:51:36 +0000143
Joachim Bauchf3965b42015-12-14 21:30:11 +0100144/* Release (maybe partially allocated) stream. */
jfigus84944902015-12-15 07:41:16 -0500145static void
Joachim Bauchf3965b42015-12-14 21:30:11 +0100146srtp_stream_free(srtp_stream_ctx_t *str) {
147 if (str->rtp_xtn_hdr_cipher) {
148 srtp_cipher_dealloc(str->rtp_xtn_hdr_cipher);
149 }
150 if (str->enc_xtn_hdr) {
151 srtp_crypto_free(str->enc_xtn_hdr);
152 }
153 if (str->rtcp_auth) {
154 auth_dealloc(str->rtcp_auth);
155 }
156 if (str->rtcp_cipher) {
157 srtp_cipher_dealloc(str->rtcp_cipher);
158 }
159 if (str->limit) {
160 srtp_crypto_free(str->limit);
161 }
162 if (str->rtp_auth) {
163 auth_dealloc(str->rtp_auth);
164 }
165 if (str->rtp_cipher) {
166 srtp_cipher_dealloc(str->rtp_cipher);
167 }
168 srtp_crypto_free(str);
169}
170
jfigus857009c2014-11-05 11:17:43 -0500171srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000172srtp_stream_alloc(srtp_stream_ctx_t **str_ptr,
173 const srtp_policy_t *p) {
174 srtp_stream_ctx_t *str;
jfigus857009c2014-11-05 11:17:43 -0500175 srtp_err_status_t stat;
Cullen Jennings235513a2005-09-21 22:51:36 +0000176
177 /*
178 * This function allocates the stream context, rtp and rtcp ciphers
179 * and auth functions, and key limit structure. If there is a
180 * failure during allocation, we free all previously allocated
181 * memory and return a failure code. The code could probably
182 * be improved, but it works and should be clear.
183 */
184
185 /* allocate srtp stream and set str_ptr */
jfigused755f52014-11-19 14:57:19 -0500186 str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000187 if (str == NULL)
jfigus857009c2014-11-05 11:17:43 -0500188 return srtp_err_status_alloc_fail;
Joachim Bauchf3965b42015-12-14 21:30:11 +0100189
190 memset(str, 0, sizeof(srtp_stream_ctx_t));
191 *str_ptr = str;
192
Cullen Jennings235513a2005-09-21 22:51:36 +0000193 /* allocate cipher */
jfigus92736bc2014-11-21 10:30:54 -0500194 stat = srtp_crypto_kernel_alloc_cipher(p->rtp.cipher_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000195 &str->rtp_cipher,
jfigusc13c1002014-05-08 13:34:53 -0400196 p->rtp.cipher_key_len,
197 p->rtp.auth_tag_len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000198 if (stat) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100199 srtp_stream_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000200 return stat;
201 }
202
203 /* allocate auth function */
jfigus92736bc2014-11-21 10:30:54 -0500204 stat = srtp_crypto_kernel_alloc_auth(p->rtp.auth_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000205 &str->rtp_auth,
206 p->rtp.auth_key_len,
207 p->rtp.auth_tag_len);
208 if (stat) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100209 srtp_stream_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000210 return stat;
211 }
212
213 /* allocate key limit structure */
jfigusc7cdc9a2014-11-19 16:19:08 -0500214 str->limit = (srtp_key_limit_ctx_t*) srtp_crypto_alloc(sizeof(srtp_key_limit_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000215 if (str->limit == NULL) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100216 srtp_stream_free(str);
jfigus857009c2014-11-05 11:17:43 -0500217 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000218 }
219
220 /*
221 * ...and now the RTCP-specific initialization - first, allocate
222 * the cipher
223 */
jfigus92736bc2014-11-21 10:30:54 -0500224 stat = srtp_crypto_kernel_alloc_cipher(p->rtcp.cipher_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000225 &str->rtcp_cipher,
jfigusc13c1002014-05-08 13:34:53 -0400226 p->rtcp.cipher_key_len,
227 p->rtcp.auth_tag_len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000228 if (stat) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100229 srtp_stream_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000230 return stat;
231 }
232
233 /* allocate auth function */
jfigus92736bc2014-11-21 10:30:54 -0500234 stat = srtp_crypto_kernel_alloc_auth(p->rtcp.auth_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000235 &str->rtcp_auth,
236 p->rtcp.auth_key_len,
237 p->rtcp.auth_tag_len);
238 if (stat) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100239 srtp_stream_free(str);
240 return stat;
Cullen Jennings235513a2005-09-21 22:51:36 +0000241 }
242
David McGrew79870d62007-06-15 18:17:39 +0000243 /* allocate ekt data associated with stream */
jfigusc5887e72014-11-06 09:46:18 -0500244 stat = srtp_ekt_alloc(&str->ekt, p->ekt);
David McGrew79870d62007-06-15 18:17:39 +0000245 if (stat) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100246 srtp_stream_free(str);
247 return stat;
David McGrew79870d62007-06-15 18:17:39 +0000248 }
249
Joachim Bauch99a74822015-11-17 00:08:19 +0100250 if (p->enc_xtn_hdr && p->enc_xtn_hdr_count > 0) {
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100251 srtp_cipher_type_id_t enc_xtn_hdr_cipher_type;
252 int enc_xtn_hdr_cipher_key_len;
253
Joachim Bauch99a74822015-11-17 00:08:19 +0100254 str->enc_xtn_hdr = (int*) srtp_crypto_alloc(p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0]));
255 if (!str->enc_xtn_hdr) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100256 srtp_stream_free(str);
Joachim Bauch99a74822015-11-17 00:08:19 +0100257 return srtp_err_status_alloc_fail;
258 }
259 memcpy(str->enc_xtn_hdr, p->enc_xtn_hdr, p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0]));
260 str->enc_xtn_hdr_count = p->enc_xtn_hdr_count;
261
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100262 /* For GCM ciphers, the corresponding ICM cipher is used for header extensions encryption. */
263 switch (p->rtp.cipher_type) {
264 case SRTP_AES_128_GCM:
265 enc_xtn_hdr_cipher_type = SRTP_AES_128_ICM;
266 enc_xtn_hdr_cipher_key_len = 30;
267 break;
268 case SRTP_AES_256_GCM:
269 enc_xtn_hdr_cipher_type = SRTP_AES_256_ICM;
270 enc_xtn_hdr_cipher_key_len = 46;
271 break;
272 default:
273 enc_xtn_hdr_cipher_type = p->rtp.cipher_type;
274 enc_xtn_hdr_cipher_key_len = p->rtp.cipher_key_len;
275 break;
276 }
277
Joachim Bauch99a74822015-11-17 00:08:19 +0100278 /* allocate cipher for extensions header encryption */
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100279 stat = srtp_crypto_kernel_alloc_cipher(enc_xtn_hdr_cipher_type,
Joachim Bauch99a74822015-11-17 00:08:19 +0100280 &str->rtp_xtn_hdr_cipher,
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100281 enc_xtn_hdr_cipher_key_len,
282 0);
Joachim Bauch99a74822015-11-17 00:08:19 +0100283 if (stat) {
Joachim Bauchf3965b42015-12-14 21:30:11 +0100284 srtp_stream_free(str);
Joachim Bauch99a74822015-11-17 00:08:19 +0100285 return stat;
286 }
287 } else {
288 str->rtp_xtn_hdr_cipher = NULL;
289 str->enc_xtn_hdr = NULL;
290 str->enc_xtn_hdr_count = 0;
291 }
292
jfigus857009c2014-11-05 11:17:43 -0500293 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000294}
295
jfigus857009c2014-11-05 11:17:43 -0500296srtp_err_status_t
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100297srtp_stream_dealloc(srtp_stream_ctx_t *stream, srtp_stream_ctx_t *stream_template) {
jfigus857009c2014-11-05 11:17:43 -0500298 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000299
300 /*
301 * we use a conservative deallocation strategy - if any deallocation
302 * fails, then we report that fact without trying to deallocate
303 * anything else
304 */
305
306 /* deallocate cipher, if it is not the same as that in template */
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100307 if (stream_template
308 && stream->rtp_cipher == stream_template->rtp_cipher) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000309 /* do nothing */
310 } else {
jfigus3f93c3c2014-12-01 15:38:09 -0500311 status = srtp_cipher_dealloc(stream->rtp_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +0000312 if (status)
David McGrewfec49dd2005-09-23 19:34:11 +0000313 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000314 }
315
316 /* deallocate auth function, if it is not the same as that in template */
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100317 if (stream_template
318 && stream->rtp_auth == stream_template->rtp_auth) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000319 /* do nothing */
320 } else {
321 status = auth_dealloc(stream->rtp_auth);
322 if (status)
323 return status;
324 }
325
David McGrewfec49dd2005-09-23 19:34:11 +0000326 /* deallocate key usage limit, if it is not the same as that in template */
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100327 if (stream_template
328 && stream->limit == stream_template->limit) {
David McGrewfec49dd2005-09-23 19:34:11 +0000329 /* do nothing */
330 } else {
jfigused755f52014-11-19 14:57:19 -0500331 srtp_crypto_free(stream->limit);
David McGrewfec49dd2005-09-23 19:34:11 +0000332 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000333
jfigusfb0bd872015-12-14 09:25:42 -0500334 if (stream_template
335 && stream->rtp_xtn_hdr_cipher == stream_template->rtp_xtn_hdr_cipher) {
Joachim Bauch99a74822015-11-17 00:08:19 +0100336 /* do nothing */
337 } else if (stream->rtp_xtn_hdr_cipher) {
338 status = srtp_cipher_dealloc(stream->rtp_xtn_hdr_cipher);
339 if (status)
340 return status;
341 }
342
Cullen Jennings235513a2005-09-21 22:51:36 +0000343 /*
344 * deallocate rtcp cipher, if it is not the same as that in
345 * template
346 */
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100347 if (stream_template
348 && stream->rtcp_cipher == stream_template->rtcp_cipher) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000349 /* do nothing */
350 } else {
jfigus3f93c3c2014-12-01 15:38:09 -0500351 status = srtp_cipher_dealloc(stream->rtcp_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +0000352 if (status)
353 return status;
354 }
355
356 /*
357 * deallocate rtcp auth function, if it is not the same as that in
358 * template
359 */
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100360 if (stream_template
361 && stream->rtcp_auth == stream_template->rtcp_auth) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000362 /* do nothing */
363 } else {
364 status = auth_dealloc(stream->rtcp_auth);
365 if (status)
366 return status;
367 }
David McGrew79870d62007-06-15 18:17:39 +0000368
jfigusde8deb32014-11-25 12:58:11 -0500369 status = srtp_rdbx_dealloc(&stream->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000370 if (status)
371 return status;
372
David McGrew79870d62007-06-15 18:17:39 +0000373 /* DAM - need to deallocate EKT here */
jfigus8c36da22013-10-01 16:41:19 -0400374
jfigusfb0bd872015-12-14 09:25:42 -0500375 if (stream_template
376 && stream->enc_xtn_hdr == stream_template->enc_xtn_hdr) {
Joachim Bauch99a74822015-11-17 00:08:19 +0100377 /* do nothing */
378 } else if (stream->enc_xtn_hdr) {
379 srtp_crypto_free(stream->enc_xtn_hdr);
380 }
381
jfigus8c36da22013-10-01 16:41:19 -0400382 /*
383 * zeroize the salt value
384 */
385 memset(stream->salt, 0, SRTP_AEAD_SALT_LEN);
386 memset(stream->c_salt, 0, SRTP_AEAD_SALT_LEN);
387
Cullen Jennings235513a2005-09-21 22:51:36 +0000388
389 /* deallocate srtp stream context */
jfigused755f52014-11-19 14:57:19 -0500390 srtp_crypto_free(stream);
Cullen Jennings235513a2005-09-21 22:51:36 +0000391
jfigus857009c2014-11-05 11:17:43 -0500392 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000393}
394
395
396/*
397 * srtp_stream_clone(stream_template, new) allocates a new stream and
398 * initializes it using the cipher and auth of the stream_template
399 *
400 * the only unique data in a cloned stream is the replay database and
401 * the SSRC
402 */
403
jfigus857009c2014-11-05 11:17:43 -0500404srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000405srtp_stream_clone(const srtp_stream_ctx_t *stream_template,
406 uint32_t ssrc,
407 srtp_stream_ctx_t **str_ptr) {
jfigus857009c2014-11-05 11:17:43 -0500408 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000409 srtp_stream_ctx_t *str;
410
411 debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ssrc);
412
413 /* allocate srtp stream and set str_ptr */
jfigused755f52014-11-19 14:57:19 -0500414 str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000415 if (str == NULL)
jfigus857009c2014-11-05 11:17:43 -0500416 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000417 *str_ptr = str;
418
419 /* set cipher and auth pointers to those of the template */
420 str->rtp_cipher = stream_template->rtp_cipher;
421 str->rtp_auth = stream_template->rtp_auth;
Joachim Bauch99a74822015-11-17 00:08:19 +0100422 str->rtp_xtn_hdr_cipher = stream_template->rtp_xtn_hdr_cipher;
Cullen Jennings235513a2005-09-21 22:51:36 +0000423 str->rtcp_cipher = stream_template->rtcp_cipher;
424 str->rtcp_auth = stream_template->rtcp_auth;
425
426 /* set key limit to point to that of the template */
jfigusc7cdc9a2014-11-19 16:19:08 -0500427 status = srtp_key_limit_clone(stream_template->limit, &str->limit);
jfigus8c36da22013-10-01 16:41:19 -0400428 if (status) {
jfigused755f52014-11-19 14:57:19 -0500429 srtp_crypto_free(*str_ptr);
jfigus8c36da22013-10-01 16:41:19 -0400430 *str_ptr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +0000431 return status;
jfigus8c36da22013-10-01 16:41:19 -0400432 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000433
434 /* initialize replay databases */
jfigusde8deb32014-11-25 12:58:11 -0500435 status = srtp_rdbx_init(&str->rtp_rdbx,
436 srtp_rdbx_get_window_size(&stream_template->rtp_rdbx));
jfigus8c36da22013-10-01 16:41:19 -0400437 if (status) {
jfigused755f52014-11-19 14:57:19 -0500438 srtp_crypto_free(*str_ptr);
jfigus8c36da22013-10-01 16:41:19 -0400439 *str_ptr = NULL;
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000440 return status;
jfigus8c36da22013-10-01 16:41:19 -0400441 }
jfigusde8deb32014-11-25 12:58:11 -0500442 srtp_rdb_init(&str->rtcp_rdb);
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000443 str->allow_repeat_tx = stream_template->allow_repeat_tx;
Cullen Jennings235513a2005-09-21 22:51:36 +0000444
445 /* set ssrc to that provided */
446 str->ssrc = ssrc;
447
448 /* set direction and security services */
449 str->direction = stream_template->direction;
450 str->rtp_services = stream_template->rtp_services;
451 str->rtcp_services = stream_template->rtcp_services;
452
David McGrew79870d62007-06-15 18:17:39 +0000453 /* set pointer to EKT data associated with stream */
454 str->ekt = stream_template->ekt;
455
jfigus8c36da22013-10-01 16:41:19 -0400456 /* Copy the salt values */
457 memcpy(str->salt, stream_template->salt, SRTP_AEAD_SALT_LEN);
458 memcpy(str->c_salt, stream_template->c_salt, SRTP_AEAD_SALT_LEN);
459
Joachim Bauch99a74822015-11-17 00:08:19 +0100460 /* copy information about extensions header encryption */
461 str->enc_xtn_hdr = stream_template->enc_xtn_hdr;
462 str->enc_xtn_hdr_count = stream_template->enc_xtn_hdr_count;
463
Cullen Jennings235513a2005-09-21 22:51:36 +0000464 /* defensive coding */
465 str->next = NULL;
466
jfigus857009c2014-11-05 11:17:43 -0500467 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000468}
469
470
471/*
472 * key derivation functions, internal to libSRTP
473 *
474 * srtp_kdf_t is a key derivation context
475 *
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000476 * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher
477 * described by cipher_id, with the master key k with length in octets keylen.
Cullen Jennings235513a2005-09-21 22:51:36 +0000478 *
479 * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key
480 * corresponding to label l and puts it into kl; the length
481 * of the key in octets is provided as keylen. this function
482 * should be called once for each subkey that is derived.
483 *
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000484 * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state
Cullen Jennings235513a2005-09-21 22:51:36 +0000485 */
486
487typedef enum {
488 label_rtp_encryption = 0x00,
489 label_rtp_msg_auth = 0x01,
490 label_rtp_salt = 0x02,
491 label_rtcp_encryption = 0x03,
492 label_rtcp_msg_auth = 0x04,
Joachim Bauch99a74822015-11-17 00:08:19 +0100493 label_rtcp_salt = 0x05,
494 label_rtp_header_encryption = 0x06,
495 label_rtp_header_salt = 0x07
Cullen Jennings235513a2005-09-21 22:51:36 +0000496} srtp_prf_label;
497
jfigus038d2cf2015-05-11 14:10:11 -0400498#define MAX_SRTP_KEY_LEN 256
499
500#if defined(OPENSSL) && defined(OPENSSL_KDF)
501#define MAX_SRTP_AESKEY_LEN 32
502#define MAX_SRTP_SALT_LEN 14
Cullen Jennings235513a2005-09-21 22:51:36 +0000503
504/*
505 * srtp_kdf_t represents a key derivation function. The SRTP
506 * default KDF is the only one implemented at present.
507 */
Cullen Jennings235513a2005-09-21 22:51:36 +0000508typedef struct {
jfigus038d2cf2015-05-11 14:10:11 -0400509 uint8_t master_key[MAX_SRTP_AESKEY_LEN];
510 uint8_t master_salt[MAX_SRTP_SALT_LEN];
511 const EVP_CIPHER *evp;
Cullen Jennings235513a2005-09-21 22:51:36 +0000512} srtp_kdf_t;
513
Cullen Jennings235513a2005-09-21 22:51:36 +0000514
jfigus038d2cf2015-05-11 14:10:11 -0400515static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, const uint8_t *key, int key_len, int salt_len)
516{
517 memset(kdf, 0x0, sizeof(srtp_kdf_t));
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000518
jfigus038d2cf2015-05-11 14:10:11 -0400519 /* The NULL cipher has zero key length */
520 if (key_len == 0) return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000521
jfigus038d2cf2015-05-11 14:10:11 -0400522 if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) {
523 return srtp_err_status_bad_param;
524 }
525 switch (key_len) {
526 case SRTP_AES_256_KEYSIZE:
527 kdf->evp = EVP_aes_256_ctr();
528 break;
529 case SRTP_AES_192_KEYSIZE:
530 kdf->evp = EVP_aes_192_ctr();
531 break;
532 case SRTP_AES_128_KEYSIZE:
533 kdf->evp = EVP_aes_128_ctr();
534 break;
535 default:
536 return srtp_err_status_bad_param;
537 break;
538 }
539 memcpy(kdf->master_key, key, key_len);
540 memcpy(kdf->master_salt, key+key_len, salt_len);
541 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000542}
543
jfigus038d2cf2015-05-11 14:10:11 -0400544static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length)
545{
546 int ret;
Cullen Jennings235513a2005-09-21 22:51:36 +0000547
jfigus038d2cf2015-05-11 14:10:11 -0400548 /* The NULL cipher will not have an EVP */
549 if (!kdf->evp) return srtp_err_status_ok;
550
551 octet_string_set_to_zero(key, length);
552
553 /*
554 * Invoke the OpenSSL SRTP KDF function
555 * This is useful if OpenSSL is in FIPS mode and FIP
556 * compliance is required for SRTP.
557 */
558 ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key, (char *)&kdf->master_salt, NULL, NULL, label, (char *)key);
559 if (ret == -1) {
560 return (srtp_err_status_algo_fail);
561 }
562
563 return srtp_err_status_ok;
564}
565
566static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) {
Pascal Bühler9bdcfc72015-10-07 16:55:52 +0200567 memset(kdf->master_key, 0x0, MAX_SRTP_AESKEY_LEN);
568 memset(kdf->master_salt, 0x0, MAX_SRTP_SALT_LEN);
jfigus038d2cf2015-05-11 14:10:11 -0400569 kdf->evp = NULL;
570
571 return srtp_err_status_ok;
572}
573
574#else /* if OPENSSL_KDF */
575
576/*
577 * srtp_kdf_t represents a key derivation function. The SRTP
578 * default KDF is the only one implemented at present.
579 */
580typedef struct {
581 srtp_cipher_t *cipher; /* cipher used for key derivation */
582} srtp_kdf_t;
583
584static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, srtp_cipher_type_id_t cipher_id, const uint8_t *key, int length)
585{
586 srtp_err_status_t stat;
587 stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, length, 0);
588 if (stat) return stat;
589
590 stat = srtp_cipher_init(kdf->cipher, key);
591 if (stat) {
592 srtp_cipher_dealloc(kdf->cipher);
593 return stat;
594 }
595 return srtp_err_status_ok;
596}
597
598static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length)
599{
600 srtp_err_status_t status;
601 v128_t nonce;
Cullen Jennings235513a2005-09-21 22:51:36 +0000602
jfigus038d2cf2015-05-11 14:10:11 -0400603 /* set eigth octet of nonce to <label>, set the rest of it to zero */
604 v128_set_to_zero(&nonce);
605 nonce.v8[7] = label;
Cullen Jennings235513a2005-09-21 22:51:36 +0000606
persmulebfec1cd2015-10-24 02:29:57 +0800607 status = srtp_cipher_set_iv(kdf->cipher, (uint8_t*)&nonce, direction_encrypt);
jfigus038d2cf2015-05-11 14:10:11 -0400608 if (status) return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000609
jfigus038d2cf2015-05-11 14:10:11 -0400610 /* generate keystream output */
611 octet_string_set_to_zero(key, length);
612 status = srtp_cipher_encrypt(kdf->cipher, key, &length);
613 if (status) return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000614
jfigus038d2cf2015-05-11 14:10:11 -0400615 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000616}
617
jfigus038d2cf2015-05-11 14:10:11 -0400618static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) {
619 srtp_err_status_t status;
620 status = srtp_cipher_dealloc(kdf->cipher);
621 if (status) return status;
622 kdf->cipher = NULL;
623 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000624}
jfigus038d2cf2015-05-11 14:10:11 -0400625#endif /* else OPENSSL_KDF */
Cullen Jennings235513a2005-09-21 22:51:36 +0000626
627/*
628 * end of key derivation functions
629 */
630
Cullen Jennings235513a2005-09-21 22:51:36 +0000631
632
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000633/* Get the base key length corresponding to a given combined key+salt
634 * length for the given cipher.
635 * Assumption is that for AES-ICM a key length < 30 is Ismacryp using
636 * AES-128 and short salts; everything else uses a salt length of 14.
637 * TODO: key and salt lengths should be separate fields in the policy. */
jfigus9a840432014-11-19 15:48:21 -0500638static inline int base_key_length(const srtp_cipher_type_t *cipher, int key_length)
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000639{
jfigus8c36da22013-10-01 16:41:19 -0400640 switch (cipher->id) {
jfigus67b9c732014-11-20 10:17:21 -0500641 case SRTP_AES_128_ICM:
642 case SRTP_AES_192_ICM:
643 case SRTP_AES_256_ICM:
jfigus8c36da22013-10-01 16:41:19 -0400644 /* The legacy modes are derived from
645 * the configured key length on the policy */
646 return key_length - 14;
647 break;
jfigus67b9c732014-11-20 10:17:21 -0500648 case SRTP_AES_128_GCM:
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000649 return 16;
jfigus8c36da22013-10-01 16:41:19 -0400650 break;
jfigus67b9c732014-11-20 10:17:21 -0500651 case SRTP_AES_256_GCM:
jfigus8c36da22013-10-01 16:41:19 -0400652 return 32;
653 break;
654 default:
655 return key_length;
656 break;
657 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000658}
659
jfigus857009c2014-11-05 11:17:43 -0500660srtp_err_status_t
David McGrew576e1482006-06-09 21:47:44 +0000661srtp_stream_init_keys(srtp_stream_ctx_t *srtp, const void *key) {
jfigus857009c2014-11-05 11:17:43 -0500662 srtp_err_status_t stat;
David McGrew576e1482006-06-09 21:47:44 +0000663 srtp_kdf_t kdf;
664 uint8_t tmp_key[MAX_SRTP_KEY_LEN];
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000665 int kdf_keylen = 30, rtp_keylen, rtcp_keylen;
666 int rtp_base_key_len, rtp_salt_len;
667 int rtcp_base_key_len, rtcp_salt_len;
668
669 /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */
670 /* TODO: kdf algorithm, master key length, and master salt length should
671 * be part of srtp_policy_t. */
jfigus9a840432014-11-19 15:48:21 -0500672 rtp_keylen = srtp_cipher_get_key_length(srtp->rtp_cipher);
673 rtcp_keylen = srtp_cipher_get_key_length(srtp->rtcp_cipher);
jfigus8719f952014-04-08 09:15:49 -0400674 rtp_base_key_len = base_key_length(srtp->rtp_cipher->type, rtp_keylen);
675 rtp_salt_len = rtp_keylen - rtp_base_key_len;
676
677 if (rtp_keylen > kdf_keylen) {
678 kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
679 }
680
681 if (rtcp_keylen > kdf_keylen) {
682 kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
683 }
684
jfigus8c36da22013-10-01 16:41:19 -0400685 debug_print(mod_srtp, "srtp key len: %d", rtp_keylen);
686 debug_print(mod_srtp, "srtcp key len: %d", rtcp_keylen);
jfigus8719f952014-04-08 09:15:49 -0400687 debug_print(mod_srtp, "base key len: %d", rtp_base_key_len);
688 debug_print(mod_srtp, "kdf key len: %d", kdf_keylen);
689 debug_print(mod_srtp, "rtp salt len: %d", rtp_salt_len);
690
691 /*
692 * Make sure the key given to us is 'zero' appended. GCM
693 * mode uses a shorter master SALT (96 bits), but still relies on
694 * the legacy CTR mode KDF, which uses a 112 bit master SALT.
695 */
696 memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN);
697 memcpy(tmp_key, key, (rtp_base_key_len + rtp_salt_len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000698
David McGrew576e1482006-06-09 21:47:44 +0000699 /* initialize KDF state */
jfigus038d2cf2015-05-11 14:10:11 -0400700#if defined(OPENSSL) && defined(OPENSSL_KDF)
701 stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, rtp_base_key_len, rtp_salt_len);
702#else
jfigus67b9c732014-11-20 10:17:21 -0500703 stat = srtp_kdf_init(&kdf, SRTP_AES_ICM, (const uint8_t *)tmp_key, kdf_keylen);
jfigus038d2cf2015-05-11 14:10:11 -0400704#endif
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000705 if (stat) {
jfigus857009c2014-11-05 11:17:43 -0500706 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000707 }
David McGrew576e1482006-06-09 21:47:44 +0000708
709 /* generate encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000710 stat = srtp_kdf_generate(&kdf, label_rtp_encryption,
711 tmp_key, rtp_base_key_len);
712 if (stat) {
713 /* zeroize temp buffer */
714 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500715 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000716 }
jfigus8719f952014-04-08 09:15:49 -0400717 debug_print(mod_srtp, "cipher key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500718 srtp_octet_string_hex_string(tmp_key, rtp_base_key_len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000719
David McGrew576e1482006-06-09 21:47:44 +0000720 /*
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000721 * if the cipher in the srtp context uses a salt, then we need
David McGrew576e1482006-06-09 21:47:44 +0000722 * to generate the salt value
723 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000724 if (rtp_salt_len > 0) {
725 debug_print(mod_srtp, "found rtp_salt_len > 0, generating salt", NULL);
David McGrew576e1482006-06-09 21:47:44 +0000726
727 /* generate encryption salt, put after encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000728 stat = srtp_kdf_generate(&kdf, label_rtp_salt,
729 tmp_key + rtp_base_key_len, rtp_salt_len);
730 if (stat) {
731 /* zeroize temp buffer */
732 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500733 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000734 }
jfigus8c36da22013-10-01 16:41:19 -0400735 memcpy(srtp->salt, tmp_key + rtp_base_key_len, SRTP_AEAD_SALT_LEN);
David McGrew576e1482006-06-09 21:47:44 +0000736 }
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000737 if (rtp_salt_len > 0) {
738 debug_print(mod_srtp, "cipher salt: %s",
jfigus46d6b472014-11-14 16:42:01 -0500739 srtp_octet_string_hex_string(tmp_key + rtp_base_key_len, rtp_salt_len));
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000740 }
David McGrew576e1482006-06-09 21:47:44 +0000741
742 /* initialize cipher */
jfigus3f93c3c2014-12-01 15:38:09 -0500743 stat = srtp_cipher_init(srtp->rtp_cipher, tmp_key);
David McGrew576e1482006-06-09 21:47:44 +0000744 if (stat) {
745 /* zeroize temp buffer */
746 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500747 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000748 }
749
Joachim Bauch99a74822015-11-17 00:08:19 +0100750 if (srtp->rtp_xtn_hdr_cipher) {
751 /* generate extensions header encryption key */
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100752 int rtp_xtn_hdr_keylen;
753 int rtp_xtn_hdr_base_key_len;
754 int rtp_xtn_hdr_salt_len;
755 srtp_kdf_t tmp_kdf;
756 srtp_kdf_t *xtn_hdr_kdf;
757
758 if (srtp->rtp_xtn_hdr_cipher->type != srtp->rtp_cipher->type) {
759 /* With GCM ciphers, the header extensions are still encrypted using the corresponding ICM cipher. */
760 /* See https://tools.ietf.org/html/draft-ietf-avtcore-srtp-aes-gcm-17#section-8.3 */
761 uint8_t tmp_xtn_hdr_key[MAX_SRTP_KEY_LEN];
762 rtp_xtn_hdr_keylen = srtp_cipher_get_key_length(srtp->rtp_xtn_hdr_cipher);
763 rtp_xtn_hdr_base_key_len = base_key_length(srtp->rtp_xtn_hdr_cipher->type, rtp_xtn_hdr_keylen);
764 rtp_xtn_hdr_salt_len = rtp_xtn_hdr_keylen - rtp_xtn_hdr_base_key_len;
765 memset(tmp_xtn_hdr_key, 0x0, MAX_SRTP_KEY_LEN);
766 memcpy(tmp_xtn_hdr_key, key, (rtp_xtn_hdr_base_key_len + rtp_xtn_hdr_salt_len));
767 xtn_hdr_kdf = &tmp_kdf;
768
769 /* initialize KDF state */
770#if defined(OPENSSL) && defined(OPENSSL_KDF)
771 stat = srtp_kdf_init(xtn_hdr_kdf, (const uint8_t *)tmp_xtn_hdr_key, rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len);
772#else
773 stat = srtp_kdf_init(xtn_hdr_kdf, SRTP_AES_ICM, (const uint8_t *)tmp_xtn_hdr_key, kdf_keylen);
774#endif
775 octet_string_set_to_zero(tmp_xtn_hdr_key, MAX_SRTP_KEY_LEN);
776 if (stat) {
777 return srtp_err_status_init_fail;
778 }
779 } else {
780 /* Reuse main KDF. */
781 rtp_xtn_hdr_keylen = rtp_keylen;
782 rtp_xtn_hdr_base_key_len = rtp_base_key_len;
783 rtp_xtn_hdr_salt_len = rtp_salt_len;
784 xtn_hdr_kdf = &kdf;
785 }
786
787 stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_encryption,
788 tmp_key, rtp_xtn_hdr_base_key_len);
Joachim Bauch99a74822015-11-17 00:08:19 +0100789 if (stat) {
790 /* zeroize temp buffer */
791 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
792 return srtp_err_status_init_fail;
793 }
794 debug_print(mod_srtp, "extensions cipher key: %s",
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100795 srtp_octet_string_hex_string(tmp_key, rtp_xtn_hdr_base_key_len));
Joachim Bauch99a74822015-11-17 00:08:19 +0100796
797 /*
798 * if the cipher in the srtp context uses a salt, then we need
799 * to generate the salt value
800 */
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100801 if (rtp_xtn_hdr_salt_len > 0) {
802 debug_print(mod_srtp, "found rtp_xtn_hdr_salt_len > 0, generating salt", NULL);
Joachim Bauch99a74822015-11-17 00:08:19 +0100803
804 /* generate encryption salt, put after encryption key */
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100805 stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_salt,
806 tmp_key + rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len);
Joachim Bauch99a74822015-11-17 00:08:19 +0100807 if (stat) {
808 /* zeroize temp buffer */
809 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
810 return srtp_err_status_init_fail;
811 }
Joachim Bauch99a74822015-11-17 00:08:19 +0100812 }
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100813 if (rtp_xtn_hdr_salt_len > 0) {
Joachim Bauch99a74822015-11-17 00:08:19 +0100814 debug_print(mod_srtp, "extensions cipher salt: %s",
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100815 srtp_octet_string_hex_string(tmp_key + rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len));
Joachim Bauch99a74822015-11-17 00:08:19 +0100816 }
817
818 /* initialize extensions header cipher */
819 stat = srtp_cipher_init(srtp->rtp_xtn_hdr_cipher, tmp_key);
820 if (stat) {
821 /* zeroize temp buffer */
822 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
823 return srtp_err_status_init_fail;
824 }
Joachim Bauch985769a2015-12-14 21:47:12 +0100825
826 if (xtn_hdr_kdf != &kdf) {
827 /* release memory for custom header extension encryption kdf */
828 stat = srtp_kdf_clear(xtn_hdr_kdf);
829 if (stat) {
830 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
831 return srtp_err_status_init_fail;
832 }
833 }
Joachim Bauch99a74822015-11-17 00:08:19 +0100834 }
835
David McGrew576e1482006-06-09 21:47:44 +0000836 /* generate authentication key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000837 stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth,
jfigus8f669722014-11-19 15:20:03 -0500838 tmp_key, srtp_auth_get_key_length(srtp->rtp_auth));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000839 if (stat) {
840 /* zeroize temp buffer */
841 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500842 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000843 }
David McGrew576e1482006-06-09 21:47:44 +0000844 debug_print(mod_srtp, "auth key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500845 srtp_octet_string_hex_string(tmp_key,
jfigus8f669722014-11-19 15:20:03 -0500846 srtp_auth_get_key_length(srtp->rtp_auth)));
David McGrew576e1482006-06-09 21:47:44 +0000847
848 /* initialize auth function */
849 stat = auth_init(srtp->rtp_auth, tmp_key);
850 if (stat) {
851 /* zeroize temp buffer */
852 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500853 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000854 }
855
856 /*
857 * ...now initialize SRTCP keys
858 */
859
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000860 rtcp_base_key_len = base_key_length(srtp->rtcp_cipher->type, rtcp_keylen);
861 rtcp_salt_len = rtcp_keylen - rtcp_base_key_len;
jfigus8c36da22013-10-01 16:41:19 -0400862 debug_print(mod_srtp, "rtcp salt len: %d", rtcp_salt_len);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000863
David McGrew576e1482006-06-09 21:47:44 +0000864 /* generate encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000865 stat = srtp_kdf_generate(&kdf, label_rtcp_encryption,
866 tmp_key, rtcp_base_key_len);
867 if (stat) {
868 /* zeroize temp buffer */
869 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500870 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000871 }
872
David McGrew576e1482006-06-09 21:47:44 +0000873 /*
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000874 * if the cipher in the srtp context uses a salt, then we need
David McGrew576e1482006-06-09 21:47:44 +0000875 * to generate the salt value
876 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000877 if (rtcp_salt_len > 0) {
878 debug_print(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt",
879 NULL);
David McGrew576e1482006-06-09 21:47:44 +0000880
881 /* generate encryption salt, put after encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000882 stat = srtp_kdf_generate(&kdf, label_rtcp_salt,
883 tmp_key + rtcp_base_key_len, rtcp_salt_len);
884 if (stat) {
885 /* zeroize temp buffer */
886 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500887 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000888 }
jfigus8c36da22013-10-01 16:41:19 -0400889 memcpy(srtp->c_salt, tmp_key + rtcp_base_key_len, SRTP_AEAD_SALT_LEN);
David McGrew576e1482006-06-09 21:47:44 +0000890 }
891 debug_print(mod_srtp, "rtcp cipher key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500892 srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len));
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000893 if (rtcp_salt_len > 0) {
894 debug_print(mod_srtp, "rtcp cipher salt: %s",
jfigus46d6b472014-11-14 16:42:01 -0500895 srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len, rtcp_salt_len));
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000896 }
David McGrew576e1482006-06-09 21:47:44 +0000897
898 /* initialize cipher */
jfigus3f93c3c2014-12-01 15:38:09 -0500899 stat = srtp_cipher_init(srtp->rtcp_cipher, tmp_key);
David McGrew576e1482006-06-09 21:47:44 +0000900 if (stat) {
901 /* zeroize temp buffer */
902 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500903 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000904 }
905
906 /* generate authentication key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000907 stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth,
jfigus8f669722014-11-19 15:20:03 -0500908 tmp_key, srtp_auth_get_key_length(srtp->rtcp_auth));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000909 if (stat) {
910 /* zeroize temp buffer */
911 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500912 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000913 }
914
David McGrew576e1482006-06-09 21:47:44 +0000915 debug_print(mod_srtp, "rtcp auth key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500916 srtp_octet_string_hex_string(tmp_key,
jfigus8f669722014-11-19 15:20:03 -0500917 srtp_auth_get_key_length(srtp->rtcp_auth)));
David McGrew576e1482006-06-09 21:47:44 +0000918
919 /* initialize auth function */
920 stat = auth_init(srtp->rtcp_auth, tmp_key);
921 if (stat) {
922 /* zeroize temp buffer */
923 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500924 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000925 }
926
927 /* clear memory then return */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000928 stat = srtp_kdf_clear(&kdf);
David McGrew576e1482006-06-09 21:47:44 +0000929 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000930 if (stat)
jfigus857009c2014-11-05 11:17:43 -0500931 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000932
jfigus857009c2014-11-05 11:17:43 -0500933 return srtp_err_status_ok;
David McGrew576e1482006-06-09 21:47:44 +0000934}
Cullen Jennings235513a2005-09-21 22:51:36 +0000935
jfigus857009c2014-11-05 11:17:43 -0500936srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000937srtp_stream_init(srtp_stream_ctx_t *srtp,
938 const srtp_policy_t *p) {
jfigus857009c2014-11-05 11:17:43 -0500939 srtp_err_status_t err;
Cullen Jennings235513a2005-09-21 22:51:36 +0000940
941 debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)",
942 p->ssrc.value);
943
944 /* initialize replay database */
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000945 /* window size MUST be at least 64. MAY be larger. Values more than
946 * 2^15 aren't meaningful due to how extended sequence numbers are
947 * calculated. Let a window size of 0 imply the default value. */
948
949 if (p->window_size != 0 && (p->window_size < 64 || p->window_size >= 0x8000))
jfigus857009c2014-11-05 11:17:43 -0500950 return srtp_err_status_bad_param;
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000951
952 if (p->window_size != 0)
jfigusde8deb32014-11-25 12:58:11 -0500953 err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000954 else
jfigusde8deb32014-11-25 12:58:11 -0500955 err = srtp_rdbx_init(&srtp->rtp_rdbx, 128);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000956 if (err) return err;
Cullen Jennings235513a2005-09-21 22:51:36 +0000957
958 /* initialize key limit to maximum value */
Marcus Sundberge4e34f92005-10-02 20:19:35 +0000959#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +0000960{
Marcus Sundberge4e34f92005-10-02 20:19:35 +0000961 uint64_t temp;
962 temp = make64(UINT_MAX,UINT_MAX);
jfigusc7cdc9a2014-11-19 16:19:08 -0500963 srtp_key_limit_set(srtp->limit, temp);
Cullen Jennings235513a2005-09-21 22:51:36 +0000964}
965#else
jfigusc7cdc9a2014-11-19 16:19:08 -0500966 srtp_key_limit_set(srtp->limit, 0xffffffffffffLL);
Cullen Jennings235513a2005-09-21 22:51:36 +0000967#endif
968
969 /* set the SSRC value */
970 srtp->ssrc = htonl(p->ssrc.value);
971
972 /* set the security service flags */
973 srtp->rtp_services = p->rtp.sec_serv;
974 srtp->rtcp_services = p->rtcp.sec_serv;
975
976 /*
977 * set direction to unknown - this flag gets checked in srtp_protect(),
978 * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and
979 * gets set appropriately if it is set to unknown.
980 */
981 srtp->direction = dir_unknown;
982
David McGrew576e1482006-06-09 21:47:44 +0000983 /* initialize SRTCP replay database */
jfigusde8deb32014-11-25 12:58:11 -0500984 srtp_rdb_init(&srtp->rtcp_rdb);
Cullen Jennings235513a2005-09-21 22:51:36 +0000985
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000986 /* initialize allow_repeat_tx */
987 /* guard against uninitialized memory: allow only 0 or 1 here */
988 if (p->allow_repeat_tx != 0 && p->allow_repeat_tx != 1) {
jfigusde8deb32014-11-25 12:58:11 -0500989 srtp_rdbx_dealloc(&srtp->rtp_rdbx);
jfigus857009c2014-11-05 11:17:43 -0500990 return srtp_err_status_bad_param;
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000991 }
992 srtp->allow_repeat_tx = p->allow_repeat_tx;
993
Cullen Jennings235513a2005-09-21 22:51:36 +0000994 /* DAM - no RTCP key limit at present */
995
David McGrew576e1482006-06-09 21:47:44 +0000996 /* initialize keys */
997 err = srtp_stream_init_keys(srtp, p->key);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000998 if (err) {
jfigusde8deb32014-11-25 12:58:11 -0500999 srtp_rdbx_dealloc(&srtp->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +00001000 return err;
1001 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001002
David McGrew79870d62007-06-15 18:17:39 +00001003 /*
1004 * if EKT is in use, then initialize the EKT data associated with
1005 * the stream
1006 */
jfigusc5887e72014-11-06 09:46:18 -05001007 err = srtp_ekt_stream_init_from_policy(srtp->ekt, p->ekt);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +00001008 if (err) {
jfigusde8deb32014-11-25 12:58:11 -05001009 srtp_rdbx_dealloc(&srtp->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +00001010 return err;
1011 }
David McGrew79870d62007-06-15 18:17:39 +00001012
jfigus857009c2014-11-05 11:17:43 -05001013 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001014 }
1015
1016
1017 /*
1018 * srtp_event_reporter is an event handler function that merely
1019 * reports the events that are reported by the callbacks
1020 */
1021
1022 void
1023 srtp_event_reporter(srtp_event_data_t *data) {
1024
jfigus02d6f032014-11-21 10:56:42 -05001025 srtp_err_report(srtp_err_level_warning, "srtp: in stream 0x%x: ",
Cullen Jennings235513a2005-09-21 22:51:36 +00001026 data->stream->ssrc);
1027
1028 switch(data->event) {
1029 case event_ssrc_collision:
jfigus02d6f032014-11-21 10:56:42 -05001030 srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n");
Cullen Jennings235513a2005-09-21 22:51:36 +00001031 break;
1032 case event_key_soft_limit:
jfigus02d6f032014-11-21 10:56:42 -05001033 srtp_err_report(srtp_err_level_warning, "\tkey usage soft limit reached\n");
Cullen Jennings235513a2005-09-21 22:51:36 +00001034 break;
1035 case event_key_hard_limit:
jfigus02d6f032014-11-21 10:56:42 -05001036 srtp_err_report(srtp_err_level_warning, "\tkey usage hard limit reached\n");
Cullen Jennings235513a2005-09-21 22:51:36 +00001037 break;
1038 case event_packet_index_limit:
jfigus02d6f032014-11-21 10:56:42 -05001039 srtp_err_report(srtp_err_level_warning, "\tpacket index limit reached\n");
Cullen Jennings235513a2005-09-21 22:51:36 +00001040 break;
1041 default:
jfigus02d6f032014-11-21 10:56:42 -05001042 srtp_err_report(srtp_err_level_warning, "\tunknown event reported to handler\n");
Cullen Jennings235513a2005-09-21 22:51:36 +00001043 }
1044 }
1045
1046 /*
1047 * srtp_event_handler is a global variable holding a pointer to the
1048 * event handler function; this function is called for any unexpected
1049 * event that needs to be handled out of the SRTP data path. see
1050 * srtp_event_t in srtp.h for more info
1051 *
1052 * it is okay to set srtp_event_handler to NULL, but we set
1053 * it to the srtp_event_reporter.
1054 */
1055
1056 static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter;
1057
jfigus857009c2014-11-05 11:17:43 -05001058 srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001059 srtp_install_event_handler(srtp_event_handler_func_t func) {
1060
1061 /*
1062 * note that we accept NULL arguments intentionally - calling this
1063 * function with a NULL arguments removes an event handler that's
1064 * been previously installed
1065 */
1066
1067 /* set global event handling function */
1068 srtp_event_handler = func;
jfigus857009c2014-11-05 11:17:43 -05001069 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001070 }
1071
jfigus8c36da22013-10-01 16:41:19 -04001072
Joachim Bauch99a74822015-11-17 00:08:19 +01001073/*
1074 * Check if the given extension header id is / should be encrypted.
1075 * Returns 1 if yes, otherwise 0.
1076 */
1077static int
1078srtp_protect_extension_header(srtp_stream_ctx_t *stream, int id) {
1079 int* enc_xtn_hdr = stream->enc_xtn_hdr;
1080 int count = stream->enc_xtn_hdr_count;
1081
1082 if (!enc_xtn_hdr || count <= 0) {
1083 return 0;
1084 }
1085
1086 while (count > 0) {
1087 if (*enc_xtn_hdr == id) {
1088 return 1;
1089 }
1090
1091 enc_xtn_hdr++;
1092 count--;
1093 }
1094 return 0;
1095}
1096
1097
1098/*
1099 * extensions header encryption RFC 6904
1100 */
1101static srtp_err_status_t
1102srtp_process_header_encryption(srtp_stream_ctx_t *stream, srtp_hdr_xtnd_t *xtn_hdr) {
1103 srtp_err_status_t status;
1104 uint8_t keystream[257]; /* Maximum 2 bytes header + 255 bytes data. */
1105 int keystream_pos;
1106 uint8_t* xtn_hdr_data = ((uint8_t *)xtn_hdr) + octets_in_rtp_extn_hdr;
1107 uint8_t* xtn_hdr_end = xtn_hdr_data + (ntohs(xtn_hdr->length) * sizeof(uint32_t));
1108
1109 if (ntohs(xtn_hdr->profile_specific) == 0xbede) {
1110 /* RFC 5285, section 4.2. One-Byte Header */
1111 while (xtn_hdr_data < xtn_hdr_end) {
1112 uint8_t xid = (*xtn_hdr_data & 0xf0) >> 4;
1113 unsigned int xlen = (*xtn_hdr_data & 0x0f) + 1;
1114 uint32_t xlen_with_header = 1+xlen;
1115 xtn_hdr_data++;
1116
Joachim Bauchc1fc2ba2015-12-11 17:21:41 +01001117 if (xtn_hdr_data + xlen > xtn_hdr_end)
1118 return srtp_err_status_parse_err;
1119
Joachim Bauch99a74822015-11-17 00:08:19 +01001120 if (xid == 15) {
1121 /* found header 15, stop further processing. */
1122 break;
1123 }
1124
1125 status = srtp_cipher_output(stream->rtp_xtn_hdr_cipher, keystream, &xlen_with_header);
1126 if (status)
1127 return srtp_err_status_cipher_fail;
1128
1129 if (srtp_protect_extension_header(stream, xid)) {
1130 keystream_pos = 1;
1131 while (xlen > 0) {
1132 *xtn_hdr_data ^= keystream[keystream_pos++];
1133 xtn_hdr_data++;
1134 xlen--;
1135 }
1136 } else {
1137 xtn_hdr_data += xlen;
1138 }
1139
1140 /* skip padding bytes. */
1141 while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) {
1142 xtn_hdr_data++;
1143 }
1144 }
1145 } else if ((ntohs(xtn_hdr->profile_specific) & 0x1fff) == 0x100) {
1146 /* RFC 5285, section 4.3. Two-Byte Header */
1147 while (xtn_hdr_data + 1 < xtn_hdr_end) {
1148 uint8_t xid = *xtn_hdr_data;
1149 unsigned int xlen = *(xtn_hdr_data+1);
1150 uint32_t xlen_with_header = 2+xlen;
1151 xtn_hdr_data += 2;
1152
Joachim Bauchc1fc2ba2015-12-11 17:21:41 +01001153 if (xtn_hdr_data + xlen > xtn_hdr_end)
1154 return srtp_err_status_parse_err;
1155
Joachim Bauch99a74822015-11-17 00:08:19 +01001156 status = srtp_cipher_output(stream->rtp_xtn_hdr_cipher, keystream, &xlen_with_header);
1157 if (status)
1158 return srtp_err_status_cipher_fail;
1159
1160 if (xlen > 0 && srtp_protect_extension_header(stream, xid)) {
1161 keystream_pos = 2;
1162 while (xlen > 0) {
1163 *xtn_hdr_data ^= keystream[keystream_pos++];
1164 xtn_hdr_data++;
1165 xlen--;
1166 }
1167 } else {
1168 xtn_hdr_data += xlen;
1169 }
1170
1171 /* skip padding bytes. */
1172 while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) {
1173 xtn_hdr_data++;
1174 }
1175 }
1176 } else {
1177 /* unsupported extension header format. */
1178 return srtp_err_status_parse_err;
1179 }
1180
1181 return srtp_err_status_ok;
1182}
jfigus8c36da22013-10-01 16:41:19 -04001183
1184
Cullen Jennings235513a2005-09-21 22:51:36 +00001185/*
1186 * AEAD uses a new IV formation method. This function implements
1187 * section 9.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The
1188 * calculation is defined as, where (+) is the xor operation:
1189 *
1190 *
1191 * 0 0 0 0 0 0 0 0 0 0 1 1
1192 * 0 1 2 3 4 5 6 7 8 9 0 1
1193 * +--+--+--+--+--+--+--+--+--+--+--+--+
1194 * |00|00| SSRC | ROC | SEQ |---+
1195 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
1196 * |
1197 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
1198 * | Encryption Salt |->(+)
1199 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
1200 * |
1201 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
1202 * | Initialization Vector |<--+
1203 * +--+--+--+--+--+--+--+--+--+--+--+--+*
1204 *
1205 * Input: *stream - pointer to SRTP stream context, used to retrieve
1206 * the SALT
1207 * *iv - Pointer to receive the calculated IV
1208 * *seq - The ROC and SEQ value to use for the
1209 * IV calculation.
1210 * *hdr - The RTP header, used to get the SSRC value
1211 *
1212 */
1213static void srtp_calc_aead_iv(srtp_stream_ctx_t *stream, v128_t *iv,
1214 srtp_xtd_seq_num_t *seq, srtp_hdr_t *hdr)
1215{
1216 v128_t in;
1217 v128_t salt;
1218
1219#ifdef NO_64BIT_MATH
1220 uint32_t local_roc = ((high32(*seq) << 16) |
1221 (low32(*seq) >> 16));
1222 uint16_t local_seq = (uint16_t) (low32(*seq));
1223#else
1224 uint32_t local_roc = (uint32_t)(*seq >> 16);
1225 uint16_t local_seq = (uint16_t) *seq;
1226#endif
1227
1228 memset(&in, 0, sizeof(v128_t));
1229 memset(&salt, 0, sizeof(v128_t));
1230
1231 in.v16[5] = htons(local_seq);
1232 local_roc = htonl(local_roc);
1233 memcpy(&in.v16[3], &local_roc, sizeof(local_roc));
1234
1235 /*
1236 * Copy in the RTP SSRC value
1237 */
1238 memcpy(&in.v8[2], &hdr->ssrc, 4);
1239 debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in));
1240
1241 /*
1242 * Get the SALT value from the context
1243 */
1244 memcpy(salt.v8, stream->salt, SRTP_AEAD_SALT_LEN);
1245 debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt));
1246
1247 /*
1248 * Finally, apply tyhe SALT to the input
1249 */
1250 v128_xor(iv, &in, &salt);
1251}
1252
1253
1254/*
1255 * This function handles outgoing SRTP packets while in AEAD mode,
1256 * which currently supports AES-GCM encryption. All packets are
1257 * encrypted and authenticated.
1258 */
1259static srtp_err_status_t
1260srtp_protect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream,
1261 void *rtp_hdr, unsigned int *pkt_octet_len)
1262{
1263 srtp_hdr_t *hdr = (srtp_hdr_t*)rtp_hdr;
1264 uint32_t *enc_start; /* pointer to start of encrypted portion */
jfigus0380bf42015-11-18 10:58:10 -05001265 int enc_octet_len = 0; /* number of octets in encrypted portion */
Cullen Jennings235513a2005-09-21 22:51:36 +00001266 srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
1267 int delta; /* delta of local pkt idx and that in hdr */
1268 srtp_err_status_t status;
1269 uint32_t tag_len;
1270 v128_t iv;
1271 unsigned int aad_len;
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001272 srtp_hdr_xtnd_t *xtn_hdr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00001273
1274 debug_print(mod_srtp, "function srtp_protect_aead", NULL);
1275
1276 /*
1277 * update the key usage limit, and check it to make sure that we
1278 * didn't just hit either the soft limit or the hard limit, and call
1279 * the event handler if we hit either.
1280 */
1281 switch (srtp_key_limit_update(stream->limit)) {
1282 case srtp_key_event_normal:
1283 break;
1284 case srtp_key_event_hard_limit:
1285 srtp_handle_event(ctx, stream, event_key_hard_limit);
1286 return srtp_err_status_key_expired;
1287 case srtp_key_event_soft_limit:
1288 default:
1289 srtp_handle_event(ctx, stream, event_key_soft_limit);
1290 break;
1291 }
1292
1293 /* get tag length from stream */
1294 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
1295
1296 /*
1297 * find starting point for encryption and length of data to be
1298 * encrypted - the encrypted portion starts after the rtp header
1299 * extension, if present; otherwise, it starts after the last csrc,
1300 * if any are present
1301 */
1302 enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc;
1303 if (hdr->x == 1) {
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001304 xtn_hdr = (srtp_hdr_xtnd_t*)enc_start;
Cullen Jennings235513a2005-09-21 22:51:36 +00001305 enc_start += (ntohs(xtn_hdr->length) + 1);
1306 }
1307 /* note: the passed size is without the auth tag */
jfigus2e955302015-11-20 10:24:39 -05001308 if (!((uint8_t*)enc_start <= (uint8_t*)hdr + *pkt_octet_len))
Cullen Jennings235513a2005-09-21 22:51:36 +00001309 return srtp_err_status_parse_err;
jfigus0380bf42015-11-18 10:58:10 -05001310 enc_octet_len = (int)(*pkt_octet_len -
Cullen Jennings235513a2005-09-21 22:51:36 +00001311 ((uint8_t*)enc_start - (uint8_t*)hdr));
jfigus0380bf42015-11-18 10:58:10 -05001312 if (enc_octet_len < 0) return srtp_err_status_parse_err;
Cullen Jennings235513a2005-09-21 22:51:36 +00001313
1314 /*
1315 * estimate the packet index using the start of the replay window
1316 * and the sequence number from the header
1317 */
1318 delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
1319 status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
1320 if (status) {
1321 if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) {
1322 return status; /* we've been asked to reuse an index */
1323 }
1324 } else {
1325 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
1326 }
1327
1328#ifdef NO_64BIT_MATH
1329 debug_print2(mod_srtp, "estimated packet index: %08x%08x",
1330 high32(est), low32(est));
1331#else
1332 debug_print(mod_srtp, "estimated packet index: %016llx", est);
1333#endif
1334
1335 /*
1336 * AEAD uses a new IV formation method
1337 */
1338 srtp_calc_aead_iv(stream, &iv, &est, hdr);
Cullen Jennings235513a2005-09-21 22:51:36 +00001339 /* shift est, put into network byte order */
1340#ifdef NO_64BIT_MATH
1341 est = be64_to_cpu(make64((high32(est) << 16) |
1342 (low32(est) >> 16),
1343 low32(est) << 16));
1344#else
1345 est = be64_to_cpu(est << 16);
1346#endif
1347
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001348 status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_encrypt);
1349 if (!status && stream->rtp_xtn_hdr_cipher) {
1350 iv.v32[0] = 0;
1351 iv.v32[1] = hdr->ssrc;
1352 iv.v64[1] = est;
1353 status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt);
1354 }
1355 if (status) {
1356 return srtp_err_status_cipher_fail;
1357 }
1358
1359 if (xtn_hdr && stream->rtp_xtn_hdr_cipher) {
1360 /*
1361 * extensions header encryption RFC 6904
1362 */
1363 status = srtp_process_header_encryption(stream, xtn_hdr);
1364 if (status) {
1365 return status;
1366 }
1367 }
1368
Cullen Jennings235513a2005-09-21 22:51:36 +00001369 /*
1370 * Set the AAD over the RTP header
1371 */
1372 aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
1373 status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
1374 if (status) {
1375 return ( srtp_err_status_cipher_fail);
1376 }
1377
1378 /* Encrypt the payload */
1379 status = srtp_cipher_encrypt(stream->rtp_cipher,
jfigus0380bf42015-11-18 10:58:10 -05001380 (uint8_t*)enc_start, (unsigned int *)&enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001381 if (status) {
1382 return srtp_err_status_cipher_fail;
1383 }
1384 /*
1385 * If we're doing GCM, we need to get the tag
1386 * and append that to the output
1387 */
1388 status = srtp_cipher_get_tag(stream->rtp_cipher,
1389 (uint8_t*)enc_start+enc_octet_len, &tag_len);
1390 if (status) {
1391 return ( srtp_err_status_cipher_fail);
1392 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001393
1394 /* increase the packet length by the length of the auth tag */
1395 *pkt_octet_len += tag_len;
1396
1397 return srtp_err_status_ok;
1398}
1399
1400
1401/*
1402 * This function handles incoming SRTP packets while in AEAD mode,
1403 * which currently supports AES-GCM encryption. All packets are
1404 * encrypted and authenticated. Note, the auth tag is at the end
1405 * of the packet stream and is automatically checked by GCM
1406 * when decrypting the payload.
1407 */
1408static srtp_err_status_t
1409srtp_unprotect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, int delta,
1410 srtp_xtd_seq_num_t est, void *srtp_hdr, unsigned int *pkt_octet_len)
1411{
1412 srtp_hdr_t *hdr = (srtp_hdr_t*)srtp_hdr;
1413 uint32_t *enc_start; /* pointer to start of encrypted portion */
1414 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
1415 v128_t iv;
1416 srtp_err_status_t status;
1417 int tag_len;
1418 unsigned int aad_len;
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001419 srtp_hdr_xtnd_t *xtn_hdr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00001420
1421 debug_print(mod_srtp, "function srtp_unprotect_aead", NULL);
1422
1423#ifdef NO_64BIT_MATH
1424 debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est), low32(est));
1425#else
1426 debug_print(mod_srtp, "estimated u_packet index: %016llx", est);
1427#endif
1428
1429 /* get tag length from stream */
1430 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
1431
1432 /*
1433 * AEAD uses a new IV formation method
1434 */
1435 srtp_calc_aead_iv(stream, &iv, &est, hdr);
1436 status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_decrypt);
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001437 if (!status && stream->rtp_xtn_hdr_cipher) {
1438 iv.v32[0] = 0;
1439 iv.v32[1] = hdr->ssrc;
1440#ifdef NO_64BIT_MATH
1441 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
1442 low32(est) << 16));
1443#else
1444 iv.v64[1] = be64_to_cpu(est << 16);
1445#endif
1446 status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt);
1447 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001448 if (status) {
1449 return srtp_err_status_cipher_fail;
1450 }
1451
1452 /*
1453 * find starting point for decryption and length of data to be
1454 * decrypted - the encrypted portion starts after the rtp header
1455 * extension, if present; otherwise, it starts after the last csrc,
1456 * if any are present
1457 */
1458 enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc;
1459 if (hdr->x == 1) {
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001460 xtn_hdr = (srtp_hdr_xtnd_t*)enc_start;
Cullen Jennings235513a2005-09-21 22:51:36 +00001461 enc_start += (ntohs(xtn_hdr->length) + 1);
1462 }
Joachim Bauchdae53252015-12-14 22:50:10 +01001463 if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len)))
Cullen Jennings235513a2005-09-21 22:51:36 +00001464 return srtp_err_status_parse_err;
1465 /*
1466 * We pass the tag down to the cipher when doing GCM mode
1467 */
1468 enc_octet_len = (unsigned int)(*pkt_octet_len -
1469 ((uint8_t*)enc_start - (uint8_t*)hdr));
1470
1471 /*
1472 * Sanity check the encrypted payload length against
1473 * the tag size. It must always be at least as large
1474 * as the tag length.
1475 */
1476 if (enc_octet_len < (unsigned int) tag_len) {
1477 return srtp_err_status_cipher_fail;
1478 }
1479
1480 /*
1481 * update the key usage limit, and check it to make sure that we
1482 * didn't just hit either the soft limit or the hard limit, and call
1483 * the event handler if we hit either.
1484 */
1485 switch (srtp_key_limit_update(stream->limit)) {
1486 case srtp_key_event_normal:
1487 break;
1488 case srtp_key_event_soft_limit:
1489 srtp_handle_event(ctx, stream, event_key_soft_limit);
1490 break;
1491 case srtp_key_event_hard_limit:
1492 srtp_handle_event(ctx, stream, event_key_hard_limit);
1493 return srtp_err_status_key_expired;
1494 default:
1495 break;
1496 }
1497
1498 /*
1499 * Set the AAD for AES-GCM, which is the RTP header
1500 */
1501 aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
1502 status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
1503 if (status) {
1504 return ( srtp_err_status_cipher_fail);
1505 }
1506
Joachim Bauchda339872015-12-11 18:09:01 +01001507 /* Decrypt the ciphertext. This also checks the auth tag based
Cullen Jennings235513a2005-09-21 22:51:36 +00001508 * on the AAD we just specified above */
1509 status = srtp_cipher_decrypt(stream->rtp_cipher, (uint8_t*)enc_start, &enc_octet_len);
1510 if (status) {
1511 return status;
1512 }
1513
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001514 if (xtn_hdr && stream->rtp_xtn_hdr_cipher) {
1515 /*
1516 * extensions header encryption RFC 6904
1517 */
1518 status = srtp_process_header_encryption(stream, xtn_hdr);
1519 if (status) {
1520 return status;
1521 }
1522 }
1523
Cullen Jennings235513a2005-09-21 22:51:36 +00001524 /*
1525 * verify that stream is for received traffic - this check will
1526 * detect SSRC collisions, since a stream that appears in both
1527 * srtp_protect() and srtp_unprotect() will fail this test in one of
1528 * those functions.
1529 *
1530 * we do this check *after* the authentication check, so that the
1531 * latter check will catch any attempts to fool us into thinking
1532 * that we've got a collision
1533 */
1534 if (stream->direction != dir_srtp_receiver) {
1535 if (stream->direction == dir_unknown) {
1536 stream->direction = dir_srtp_receiver;
1537 } else {
1538 srtp_handle_event(ctx, stream, event_ssrc_collision);
1539 }
1540 }
1541
1542 /*
1543 * if the stream is a 'provisional' one, in which the template context
1544 * is used, then we need to allocate a new stream at this point, since
1545 * the authentication passed
1546 */
1547 if (stream == ctx->stream_template) {
1548 srtp_stream_ctx_t *new_stream;
1549
1550 /*
1551 * allocate and initialize a new stream
1552 *
1553 * note that we indicate failure if we can't allocate the new
1554 * stream, and some implementations will want to not return
1555 * failure here
1556 */
1557 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
1558 if (status) {
1559 return status;
1560 }
1561
1562 /* add new stream to the head of the stream_list */
1563 new_stream->next = ctx->stream_list;
1564 ctx->stream_list = new_stream;
1565
1566 /* set stream (the pointer used in this function) */
1567 stream = new_stream;
1568 }
1569
1570 /*
1571 * the message authentication function passed, so add the packet
1572 * index into the replay database
1573 */
1574 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
1575
1576 /* decrease the packet length by the length of the auth tag */
1577 *pkt_octet_len -= tag_len;
1578
1579 return srtp_err_status_ok;
1580}
1581
1582
jfigus857009c2014-11-05 11:17:43 -05001583 srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001584 srtp_protect(srtp_ctx_t *ctx, void *rtp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00001585 srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00001586 uint32_t *enc_start; /* pointer to start of encrypted portion */
1587 uint32_t *auth_start; /* pointer to start of auth. portion */
jfigus0380bf42015-11-18 10:58:10 -05001588 int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigusde8deb32014-11-25 12:58:11 -05001589 srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
Cullen Jennings235513a2005-09-21 22:51:36 +00001590 int delta; /* delta of local pkt idx and that in hdr */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001591 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05001592 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001593 int tag_len;
1594 srtp_stream_ctx_t *stream;
jfigus2964a152014-11-25 15:25:37 -05001595 uint32_t prefix_len;
Joachim Bauch99a74822015-11-17 00:08:19 +01001596 srtp_hdr_xtnd_t *xtn_hdr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00001597
1598 debug_print(mod_srtp, "function srtp_protect", NULL);
1599
1600 /* we assume the hdr is 32-bit aligned to start */
1601
Joachim Bauch557a7872015-02-19 01:36:48 +01001602 /* Verify RTP header */
1603 status = srtp_validate_rtp_header(rtp_hdr, pkt_octet_len);
1604 if (status)
1605 return status;
1606
Cullen Jennings235513a2005-09-21 22:51:36 +00001607 /* check the packet length - it must at least contain a full header */
1608 if (*pkt_octet_len < octets_in_rtp_header)
jfigus857009c2014-11-05 11:17:43 -05001609 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001610
1611 /*
1612 * look up ssrc in srtp_stream list, and process the packet with
1613 * the appropriate stream. if we haven't seen this stream before,
1614 * there's a template key for this srtp_session, and the cipher
1615 * supports key-sharing, then we assume that a new stream using
1616 * that key has just started up
1617 */
1618 stream = srtp_get_stream(ctx, hdr->ssrc);
1619 if (stream == NULL) {
1620 if (ctx->stream_template != NULL) {
1621 srtp_stream_ctx_t *new_stream;
1622
1623 /* allocate and initialize a new stream */
1624 status = srtp_stream_clone(ctx->stream_template,
David McGrewfec49dd2005-09-23 19:34:11 +00001625 hdr->ssrc, &new_stream);
Cullen Jennings235513a2005-09-21 22:51:36 +00001626 if (status)
1627 return status;
1628
1629 /* add new stream to the head of the stream_list */
1630 new_stream->next = ctx->stream_list;
1631 ctx->stream_list = new_stream;
1632
1633 /* set direction to outbound */
1634 new_stream->direction = dir_srtp_sender;
1635
1636 /* set stream (the pointer used in this function) */
1637 stream = new_stream;
1638 } else {
1639 /* no template stream, so we return an error */
jfigus857009c2014-11-05 11:17:43 -05001640 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00001641 }
1642 }
1643
1644 /*
1645 * verify that stream is for sending traffic - this check will
1646 * detect SSRC collisions, since a stream that appears in both
1647 * srtp_protect() and srtp_unprotect() will fail this test in one of
1648 * those functions.
1649 */
jfigus8c36da22013-10-01 16:41:19 -04001650 if (stream->direction != dir_srtp_sender) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001651 if (stream->direction == dir_unknown) {
1652 stream->direction = dir_srtp_sender;
1653 } else {
1654 srtp_handle_event(ctx, stream, event_ssrc_collision);
1655 }
jfigus8c36da22013-10-01 16:41:19 -04001656 }
1657
1658 /*
1659 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
1660 * the request to our AEAD handler.
1661 */
jfigus67b9c732014-11-20 10:17:21 -05001662 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
1663 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00001664 return srtp_protect_aead(ctx, stream, rtp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04001665 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001666
1667 /*
1668 * update the key usage limit, and check it to make sure that we
1669 * didn't just hit either the soft limit or the hard limit, and call
1670 * the event handler if we hit either.
1671 */
jfigusc7cdc9a2014-11-19 16:19:08 -05001672 switch(srtp_key_limit_update(stream->limit)) {
1673 case srtp_key_event_normal:
Cullen Jennings235513a2005-09-21 22:51:36 +00001674 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001675 case srtp_key_event_soft_limit:
Cullen Jennings235513a2005-09-21 22:51:36 +00001676 srtp_handle_event(ctx, stream, event_key_soft_limit);
1677 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001678 case srtp_key_event_hard_limit:
Cullen Jennings235513a2005-09-21 22:51:36 +00001679 srtp_handle_event(ctx, stream, event_key_hard_limit);
jfigus857009c2014-11-05 11:17:43 -05001680 return srtp_err_status_key_expired;
Cullen Jennings235513a2005-09-21 22:51:36 +00001681 default:
1682 break;
1683 }
1684
1685 /* get tag length from stream */
jfigus8f669722014-11-19 15:20:03 -05001686 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001687
1688 /*
1689 * find starting point for encryption and length of data to be
1690 * encrypted - the encrypted portion starts after the rtp header
1691 * extension, if present; otherwise, it starts after the last csrc,
1692 * if any are present
1693 *
1694 * if we're not providing confidentiality, set enc_start to NULL
1695 */
1696 if (stream->rtp_services & sec_serv_conf) {
1697 enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
David McGrew14829302005-10-10 18:53:19 +00001698 if (hdr->x == 1) {
Joachim Bauch99a74822015-11-17 00:08:19 +01001699 xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
David McGrew14829302005-10-10 18:53:19 +00001700 enc_start += (ntohs(xtn_hdr->length) + 1);
1701 }
Joachim Bauch3444c4b2015-11-13 00:03:19 +01001702 /* note: the passed size is without the auth tag */
jfigus2e955302015-11-20 10:24:39 -05001703 if (!((uint8_t*)enc_start <= (uint8_t*)hdr + *pkt_octet_len))
jfigus25831502015-11-02 10:09:43 -05001704 return srtp_err_status_parse_err;
jfigus0380bf42015-11-18 10:58:10 -05001705 enc_octet_len = (int)(*pkt_octet_len -
Travis Crossdee3ee82014-07-02 15:20:12 +00001706 ((uint8_t*)enc_start - (uint8_t*)hdr));
jfigus0380bf42015-11-18 10:58:10 -05001707 if (enc_octet_len < 0) return srtp_err_status_parse_err;
Cullen Jennings235513a2005-09-21 22:51:36 +00001708 } else {
1709 enc_start = NULL;
1710 }
1711
1712 /*
1713 * if we're providing authentication, set the auth_start and auth_tag
1714 * pointers to the proper locations; otherwise, set auth_start to NULL
1715 * to indicate that no authentication is needed
1716 */
1717 if (stream->rtp_services & sec_serv_auth) {
1718 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001719 auth_tag = (uint8_t *)hdr + *pkt_octet_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001720 } else {
1721 auth_start = NULL;
1722 auth_tag = NULL;
1723 }
1724
1725 /*
1726 * estimate the packet index using the start of the replay window
1727 * and the sequence number from the header
1728 */
jfigusde8deb32014-11-25 12:58:11 -05001729 delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
1730 status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +00001731 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001732 if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx)
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +00001733 return status; /* we've been asked to reuse an index */
1734 }
1735 else
jfigusde8deb32014-11-25 12:58:11 -05001736 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
Cullen Jennings235513a2005-09-21 22:51:36 +00001737
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001738#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001739 debug_print2(mod_srtp, "estimated packet index: %08x%08x",
1740 high32(est),low32(est));
1741#else
1742 debug_print(mod_srtp, "estimated packet index: %016llx", est);
1743#endif
1744
1745 /*
1746 * if we're using rindael counter mode, set nonce and seq
1747 */
jfigus67b9c732014-11-20 10:17:21 -05001748 if (stream->rtp_cipher->type->id == SRTP_AES_ICM ||
1749 stream->rtp_cipher->type->id == SRTP_AES_256_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001750 v128_t iv;
1751
1752 iv.v32[0] = 0;
1753 iv.v32[1] = hdr->ssrc;
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001754#ifdef NO_64BIT_MATH
1755 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
David McGrewfec49dd2005-09-23 19:34:11 +00001756 low32(est) << 16));
Cullen Jennings235513a2005-09-21 22:51:36 +00001757#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001758 iv.v64[1] = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001759#endif
persmulebfec1cd2015-10-24 02:29:57 +08001760 status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_encrypt);
Joachim Bauch99a74822015-11-17 00:08:19 +01001761 if (!status && stream->rtp_xtn_hdr_cipher) {
1762 status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt);
1763 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001764 } else {
1765 v128_t iv;
1766
1767 /* otherwise, set the index to est */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001768#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001769 iv.v32[0] = 0;
1770 iv.v32[1] = 0;
1771#else
1772 iv.v64[0] = 0;
1773#endif
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001774 iv.v64[1] = be64_to_cpu(est);
persmulebfec1cd2015-10-24 02:29:57 +08001775 status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_encrypt);
Joachim Bauch99a74822015-11-17 00:08:19 +01001776 if (!status && stream->rtp_xtn_hdr_cipher) {
1777 status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_encrypt);
1778 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001779 }
1780 if (status)
jfigus857009c2014-11-05 11:17:43 -05001781 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001782
1783 /* shift est, put into network byte order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001784#ifdef NO_64BIT_MATH
1785 est = be64_to_cpu(make64((high32(est) << 16) |
Cullen Jennings235513a2005-09-21 22:51:36 +00001786 (low32(est) >> 16),
Randell Jesup811e1442005-09-28 22:43:41 +00001787 low32(est) << 16));
Cullen Jennings235513a2005-09-21 22:51:36 +00001788#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001789 est = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001790#endif
1791
1792 /*
1793 * if we're authenticating using a universal hash, put the keystream
1794 * prefix into the authentication tag
1795 */
1796 if (auth_start) {
1797
jfigus8f669722014-11-19 15:20:03 -05001798 prefix_len = srtp_auth_get_prefix_length(stream->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001799 if (prefix_len) {
jfigus2964a152014-11-25 15:25:37 -05001800 status = srtp_cipher_output(stream->rtp_cipher, auth_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001801 if (status)
jfigus857009c2014-11-05 11:17:43 -05001802 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001803 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05001804 srtp_octet_string_hex_string(auth_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001805 }
1806 }
1807
Joachim Bauch99a74822015-11-17 00:08:19 +01001808 if (xtn_hdr && stream->rtp_xtn_hdr_cipher) {
1809 /*
1810 * extensions header encryption RFC 6904
1811 */
1812 status = srtp_process_header_encryption(stream, xtn_hdr);
1813 if (status) {
1814 return status;
1815 }
1816 }
1817
Cullen Jennings235513a2005-09-21 22:51:36 +00001818 /* if we're encrypting, exor keystream into the message */
1819 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05001820 status = srtp_cipher_encrypt(stream->rtp_cipher,
jfigus0380bf42015-11-18 10:58:10 -05001821 (uint8_t *)enc_start, (unsigned int *)&enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001822 if (status)
jfigus857009c2014-11-05 11:17:43 -05001823 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001824 }
1825
1826 /*
1827 * if we're authenticating, run authentication function and put result
1828 * into the auth_tag
1829 */
1830 if (auth_start) {
1831
1832 /* initialize auth func context */
1833 status = auth_start(stream->rtp_auth);
1834 if (status) return status;
1835
1836 /* run auth func over packet */
1837 status = auth_update(stream->rtp_auth,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001838 (uint8_t *)auth_start, *pkt_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001839 if (status) return status;
1840
1841 /* run auth func over ROC, put result into auth_tag */
David McGrew89fb7ea2005-09-26 19:33:44 +00001842 debug_print(mod_srtp, "estimated packet index: %016llx", est);
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001843 status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, auth_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00001844 debug_print(mod_srtp, "srtp auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05001845 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001846 if (status)
jfigus857009c2014-11-05 11:17:43 -05001847 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001848
1849 }
1850
1851 if (auth_tag) {
1852
1853 /* increase the packet length by the length of the auth tag */
1854 *pkt_octet_len += tag_len;
1855 }
1856
jfigus857009c2014-11-05 11:17:43 -05001857 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001858}
1859
1860
jfigus857009c2014-11-05 11:17:43 -05001861srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001862srtp_unprotect(srtp_ctx_t *ctx, void *srtp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00001863 srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00001864 uint32_t *enc_start; /* pointer to start of encrypted portion */
1865 uint32_t *auth_start; /* pointer to start of auth. portion */
Travis Cross1b8b1e72014-07-02 15:32:36 +00001866 unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001867 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigusde8deb32014-11-25 12:58:11 -05001868 srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
Cullen Jennings235513a2005-09-21 22:51:36 +00001869 int delta; /* delta of local pkt idx and that in hdr */
1870 v128_t iv;
jfigus857009c2014-11-05 11:17:43 -05001871 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001872 srtp_stream_ctx_t *stream;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001873 uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
jfigus2964a152014-11-25 15:25:37 -05001874 uint32_t tag_len, prefix_len;
Joachim Bauch99a74822015-11-17 00:08:19 +01001875 srtp_hdr_xtnd_t *xtn_hdr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00001876
1877 debug_print(mod_srtp, "function srtp_unprotect", NULL);
1878
1879 /* we assume the hdr is 32-bit aligned to start */
1880
Joachim Bauch557a7872015-02-19 01:36:48 +01001881 /* Verify RTP header */
1882 status = srtp_validate_rtp_header(srtp_hdr, pkt_octet_len);
1883 if (status)
1884 return status;
1885
Cullen Jennings235513a2005-09-21 22:51:36 +00001886 /* check the packet length - it must at least contain a full header */
1887 if (*pkt_octet_len < octets_in_rtp_header)
jfigus857009c2014-11-05 11:17:43 -05001888 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001889
1890 /*
1891 * look up ssrc in srtp_stream list, and process the packet with
1892 * the appropriate stream. if we haven't seen this stream before,
1893 * there's only one key for this srtp_session, and the cipher
1894 * supports key-sharing, then we assume that a new stream using
1895 * that key has just started up
1896 */
1897 stream = srtp_get_stream(ctx, hdr->ssrc);
1898 if (stream == NULL) {
1899 if (ctx->stream_template != NULL) {
1900 stream = ctx->stream_template;
1901 debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)",
1902 hdr->ssrc);
1903
1904 /*
1905 * set estimated packet index to sequence number from header,
1906 * and set delta equal to the same value
1907 */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001908#ifdef NO_64BIT_MATH
jfigusde8deb32014-11-25 12:58:11 -05001909 est = (srtp_xtd_seq_num_t) make64(0,ntohs(hdr->seq));
Cullen Jennings235513a2005-09-21 22:51:36 +00001910 delta = low32(est);
1911#else
jfigusde8deb32014-11-25 12:58:11 -05001912 est = (srtp_xtd_seq_num_t) ntohs(hdr->seq);
David McGrewc4fc00b2006-06-08 18:51:27 +00001913 delta = (int)est;
Cullen Jennings235513a2005-09-21 22:51:36 +00001914#endif
1915 } else {
1916
1917 /*
1918 * no stream corresponding to SSRC found, and we don't do
1919 * key-sharing, so return an error
1920 */
jfigus857009c2014-11-05 11:17:43 -05001921 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00001922 }
1923 } else {
1924
1925 /* estimate packet index from seq. num. in header */
jfigusde8deb32014-11-25 12:58:11 -05001926 delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001927
1928 /* check replay database */
jfigusde8deb32014-11-25 12:58:11 -05001929 status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001930 if (status)
1931 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001932 }
1933
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001934#ifdef NO_64BIT_MATH
David McGrewfec49dd2005-09-23 19:34:11 +00001935 debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est),low32(est));
1936#else
1937 debug_print(mod_srtp, "estimated u_packet index: %016llx", est);
1938#endif
Cullen Jennings235513a2005-09-21 22:51:36 +00001939
jfigus8c36da22013-10-01 16:41:19 -04001940 /*
1941 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
1942 * the request to our AEAD handler.
1943 */
jfigus67b9c732014-11-20 10:17:21 -05001944 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
1945 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00001946 return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04001947 }
1948
Cullen Jennings235513a2005-09-21 22:51:36 +00001949 /* get tag length from stream */
jfigus8f669722014-11-19 15:20:03 -05001950 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001951
1952 /*
1953 * set the cipher's IV properly, depending on whatever cipher we
1954 * happen to be using
1955 */
jfigus67b9c732014-11-20 10:17:21 -05001956 if (stream->rtp_cipher->type->id == SRTP_AES_ICM ||
1957 stream->rtp_cipher->type->id == SRTP_AES_256_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001958
1959 /* aes counter mode */
1960 iv.v32[0] = 0;
1961 iv.v32[1] = hdr->ssrc; /* still in network order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001962#ifdef NO_64BIT_MATH
1963 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
Cullen Jennings235513a2005-09-21 22:51:36 +00001964 low32(est) << 16));
1965#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001966 iv.v64[1] = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001967#endif
persmulebfec1cd2015-10-24 02:29:57 +08001968 status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_decrypt);
Joachim Bauch99a74822015-11-17 00:08:19 +01001969 if (!status && stream->rtp_xtn_hdr_cipher) {
1970 status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_decrypt);
1971 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001972 } else {
1973
1974 /* no particular format - set the iv to the pakcet index */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001975#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001976 iv.v32[0] = 0;
1977 iv.v32[1] = 0;
1978#else
1979 iv.v64[0] = 0;
1980#endif
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001981 iv.v64[1] = be64_to_cpu(est);
persmulebfec1cd2015-10-24 02:29:57 +08001982 status = srtp_cipher_set_iv(stream->rtp_cipher, (uint8_t*)&iv, direction_decrypt);
Joachim Bauch99a74822015-11-17 00:08:19 +01001983 if (!status && stream->rtp_xtn_hdr_cipher) {
1984 status = srtp_cipher_set_iv(stream->rtp_xtn_hdr_cipher, (uint8_t*)&iv, direction_decrypt);
1985 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001986 }
1987 if (status)
jfigus857009c2014-11-05 11:17:43 -05001988 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001989
1990 /* shift est, put into network byte order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001991#ifdef NO_64BIT_MATH
1992 est = be64_to_cpu(make64((high32(est) << 16) |
Cullen Jennings235513a2005-09-21 22:51:36 +00001993 (low32(est) >> 16),
1994 low32(est) << 16));
1995#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001996 est = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001997#endif
1998
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001999 /*
2000 * find starting point for decryption and length of data to be
2001 * decrypted - the encrypted portion starts after the rtp header
2002 * extension, if present; otherwise, it starts after the last csrc,
2003 * if any are present
2004 *
2005 * if we're not providing confidentiality, set enc_start to NULL
2006 */
2007 if (stream->rtp_services & sec_serv_conf) {
2008 enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
David McGrew14829302005-10-10 18:53:19 +00002009 if (hdr->x == 1) {
Joachim Bauch99a74822015-11-17 00:08:19 +01002010 xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
David McGrew14829302005-10-10 18:53:19 +00002011 enc_start += (ntohs(xtn_hdr->length) + 1);
2012 }
Joachim Bauchdae53252015-12-14 22:50:10 +01002013 if (!((uint8_t*)enc_start <= (uint8_t*)hdr + (*pkt_octet_len - tag_len)))
jfigus857009c2014-11-05 11:17:43 -05002014 return srtp_err_status_parse_err;
Travis Crossdee3ee82014-07-02 15:20:12 +00002015 enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len -
2016 ((uint8_t*)enc_start - (uint8_t*)hdr));
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00002017 } else {
2018 enc_start = NULL;
2019 }
2020
Cullen Jennings235513a2005-09-21 22:51:36 +00002021 /*
2022 * if we're providing authentication, set the auth_start and auth_tag
2023 * pointers to the proper locations; otherwise, set auth_start to NULL
2024 * to indicate that no authentication is needed
2025 */
2026 if (stream->rtp_services & sec_serv_auth) {
2027 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002028 auth_tag = (uint8_t *)hdr + *pkt_octet_len - tag_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00002029 } else {
2030 auth_start = NULL;
2031 auth_tag = NULL;
2032 }
2033
2034 /*
2035 * if we expect message authentication, run the authentication
2036 * function and compare the result with the value of the auth_tag
2037 */
2038 if (auth_start) {
2039
2040 /*
2041 * if we're using a universal hash, then we need to compute the
2042 * keystream prefix for encrypting the universal hash output
2043 *
2044 * if the keystream prefix length is zero, then we know that
2045 * the authenticator isn't using a universal hash function
2046 */
2047 if (stream->rtp_auth->prefix_len != 0) {
2048
jfigus8f669722014-11-19 15:20:03 -05002049 prefix_len = srtp_auth_get_prefix_length(stream->rtp_auth);
jfigus2964a152014-11-25 15:25:37 -05002050 status = srtp_cipher_output(stream->rtp_cipher, tmp_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00002051 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05002052 srtp_octet_string_hex_string(tmp_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00002053 if (status)
jfigus857009c2014-11-05 11:17:43 -05002054 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002055 }
2056
2057 /* initialize auth func context */
2058 status = auth_start(stream->rtp_auth);
2059 if (status) return status;
David McGrewfec49dd2005-09-23 19:34:11 +00002060
Cullen Jennings235513a2005-09-21 22:51:36 +00002061 /* now compute auth function over packet */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002062 status = auth_update(stream->rtp_auth, (uint8_t *)auth_start,
Cullen Jennings235513a2005-09-21 22:51:36 +00002063 *pkt_octet_len - tag_len);
2064
2065 /* run auth func over ROC, then write tmp tag */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002066 status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, tmp_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00002067
2068 debug_print(mod_srtp, "computed auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05002069 srtp_octet_string_hex_string(tmp_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00002070 debug_print(mod_srtp, "packet auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05002071 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00002072 if (status)
jfigus857009c2014-11-05 11:17:43 -05002073 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002074
2075 if (octet_string_is_eq(tmp_tag, auth_tag, tag_len))
jfigus857009c2014-11-05 11:17:43 -05002076 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002077 }
2078
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00002079 /*
2080 * update the key usage limit, and check it to make sure that we
2081 * didn't just hit either the soft limit or the hard limit, and call
2082 * the event handler if we hit either.
2083 */
jfigusc7cdc9a2014-11-19 16:19:08 -05002084 switch(srtp_key_limit_update(stream->limit)) {
2085 case srtp_key_event_normal:
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00002086 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05002087 case srtp_key_event_soft_limit:
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00002088 srtp_handle_event(ctx, stream, event_key_soft_limit);
2089 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05002090 case srtp_key_event_hard_limit:
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00002091 srtp_handle_event(ctx, stream, event_key_hard_limit);
jfigus857009c2014-11-05 11:17:43 -05002092 return srtp_err_status_key_expired;
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00002093 default:
2094 break;
2095 }
2096
Joachim Bauch99a74822015-11-17 00:08:19 +01002097 if (xtn_hdr && stream->rtp_xtn_hdr_cipher) {
2098 /*
2099 * extensions header encryption RFC 6904
2100 */
2101 status = srtp_process_header_encryption(stream, xtn_hdr);
2102 if (status) {
2103 return status;
2104 }
2105 }
2106
Jonathan Lennox23dc1e22010-06-01 18:19:04 +00002107 /* if we're decrypting, add keystream into ciphertext */
Cullen Jennings235513a2005-09-21 22:51:36 +00002108 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05002109 status = srtp_cipher_decrypt(stream->rtp_cipher, (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00002110 if (status)
jfigus857009c2014-11-05 11:17:43 -05002111 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002112 }
2113
2114 /*
2115 * verify that stream is for received traffic - this check will
2116 * detect SSRC collisions, since a stream that appears in both
2117 * srtp_protect() and srtp_unprotect() will fail this test in one of
2118 * those functions.
2119 *
2120 * we do this check *after* the authentication check, so that the
2121 * latter check will catch any attempts to fool us into thinking
2122 * that we've got a collision
2123 */
2124 if (stream->direction != dir_srtp_receiver) {
2125 if (stream->direction == dir_unknown) {
2126 stream->direction = dir_srtp_receiver;
2127 } else {
2128 srtp_handle_event(ctx, stream, event_ssrc_collision);
2129 }
2130 }
2131
2132 /*
2133 * if the stream is a 'provisional' one, in which the template context
2134 * is used, then we need to allocate a new stream at this point, since
2135 * the authentication passed
2136 */
2137 if (stream == ctx->stream_template) {
2138 srtp_stream_ctx_t *new_stream;
2139
2140 /*
2141 * allocate and initialize a new stream
2142 *
2143 * note that we indicate failure if we can't allocate the new
2144 * stream, and some implementations will want to not return
2145 * failure here
2146 */
2147 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
2148 if (status)
2149 return status;
2150
2151 /* add new stream to the head of the stream_list */
2152 new_stream->next = ctx->stream_list;
2153 ctx->stream_list = new_stream;
2154
2155 /* set stream (the pointer used in this function) */
2156 stream = new_stream;
2157 }
2158
2159 /*
2160 * the message authentication function passed, so add the packet
2161 * index into the replay database
2162 */
jfigusde8deb32014-11-25 12:58:11 -05002163 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
Cullen Jennings235513a2005-09-21 22:51:36 +00002164
2165 /* decrease the packet length by the length of the auth tag */
2166 *pkt_octet_len -= tag_len;
2167
jfigus857009c2014-11-05 11:17:43 -05002168 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002169}
2170
jfigus857009c2014-11-05 11:17:43 -05002171srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002172srtp_init() {
jfigus857009c2014-11-05 11:17:43 -05002173 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002174
2175 /* initialize crypto kernel */
jfigus92736bc2014-11-21 10:30:54 -05002176 status = srtp_crypto_kernel_init();
Cullen Jennings235513a2005-09-21 22:51:36 +00002177 if (status)
2178 return status;
2179
2180 /* load srtp debug module into the kernel */
jfigus92736bc2014-11-21 10:30:54 -05002181 status = srtp_crypto_kernel_load_debug_module(&mod_srtp);
Cullen Jennings235513a2005-09-21 22:51:36 +00002182 if (status)
2183 return status;
2184
jfigus857009c2014-11-05 11:17:43 -05002185 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002186}
2187
jfigus857009c2014-11-05 11:17:43 -05002188srtp_err_status_t
Jonathan Lennox5ae76332010-05-15 04:48:59 +00002189srtp_shutdown() {
jfigus857009c2014-11-05 11:17:43 -05002190 srtp_err_status_t status;
Jonathan Lennox5ae76332010-05-15 04:48:59 +00002191
2192 /* shut down crypto kernel */
jfigus92736bc2014-11-21 10:30:54 -05002193 status = srtp_crypto_kernel_shutdown();
Jonathan Lennox5ae76332010-05-15 04:48:59 +00002194 if (status)
2195 return status;
2196
2197 /* shutting down crypto kernel frees the srtp debug module as well */
2198
jfigus857009c2014-11-05 11:17:43 -05002199 return srtp_err_status_ok;
Jonathan Lennox5ae76332010-05-15 04:48:59 +00002200}
2201
2202
Cullen Jennings235513a2005-09-21 22:51:36 +00002203/*
2204 * The following code is under consideration for removal. See
2205 * SRTP_MAX_TRAILER_LEN
2206 */
2207#if 0
2208
2209/*
2210 * srtp_get_trailer_length(&a) returns the number of octets that will
2211 * be added to an RTP packet by the SRTP processing. This value
2212 * is constant for a given srtp_stream_t (i.e. between initializations).
2213 */
2214
2215int
2216srtp_get_trailer_length(const srtp_stream_t s) {
jfigus8f669722014-11-19 15:20:03 -05002217 return srtp_auth_get_tag_length(s->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00002218}
2219
2220#endif
2221
2222/*
2223 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
2224 * to ssrc, or NULL if no stream exists for that ssrc
2225 *
2226 * this is an internal function
2227 */
2228
2229srtp_stream_ctx_t *
2230srtp_get_stream(srtp_t srtp, uint32_t ssrc) {
2231 srtp_stream_ctx_t *stream;
2232
2233 /* walk down list until ssrc is found */
2234 stream = srtp->stream_list;
2235 while (stream != NULL) {
2236 if (stream->ssrc == ssrc)
2237 return stream;
2238 stream = stream->next;
2239 }
2240
2241 /* we haven't found our ssrc, so return a null */
2242 return NULL;
2243}
2244
jfigus857009c2014-11-05 11:17:43 -05002245srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002246srtp_dealloc(srtp_t session) {
2247 srtp_stream_ctx_t *stream;
jfigus857009c2014-11-05 11:17:43 -05002248 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002249
2250 /*
2251 * we take a conservative deallocation strategy - if we encounter an
2252 * error deallocating a stream, then we stop trying to deallocate
2253 * memory and just return an error
2254 */
2255
2256 /* walk list of streams, deallocating as we go */
2257 stream = session->stream_list;
2258 while (stream != NULL) {
2259 srtp_stream_t next = stream->next;
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002260 status = srtp_stream_dealloc(stream, session->stream_template);
Cullen Jennings235513a2005-09-21 22:51:36 +00002261 if (status)
2262 return status;
2263 stream = next;
2264 }
2265
2266 /* deallocate stream template, if there is one */
2267 if (session->stream_template != NULL) {
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002268 status = srtp_stream_dealloc(session->stream_template, NULL);
Cullen Jennings235513a2005-09-21 22:51:36 +00002269 if (status)
2270 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002271 }
2272
2273 /* deallocate session context */
jfigused755f52014-11-19 14:57:19 -05002274 srtp_crypto_free(session);
Cullen Jennings235513a2005-09-21 22:51:36 +00002275
jfigus857009c2014-11-05 11:17:43 -05002276 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002277}
2278
2279
jfigus857009c2014-11-05 11:17:43 -05002280srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002281srtp_add_stream(srtp_t session,
2282 const srtp_policy_t *policy) {
jfigus857009c2014-11-05 11:17:43 -05002283 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002284 srtp_stream_t tmp;
2285
Marcus Sundberg67398e62005-10-05 12:39:51 +00002286 /* sanity check arguments */
2287 if ((session == NULL) || (policy == NULL) || (policy->key == NULL))
jfigus857009c2014-11-05 11:17:43 -05002288 return srtp_err_status_bad_param;
Marcus Sundberg67398e62005-10-05 12:39:51 +00002289
Cullen Jennings235513a2005-09-21 22:51:36 +00002290 /* allocate stream */
2291 status = srtp_stream_alloc(&tmp, policy);
2292 if (status) {
2293 return status;
2294 }
2295
2296 /* initialize stream */
2297 status = srtp_stream_init(tmp, policy);
2298 if (status) {
jfigused755f52014-11-19 14:57:19 -05002299 srtp_crypto_free(tmp);
Cullen Jennings235513a2005-09-21 22:51:36 +00002300 return status;
2301 }
2302
2303 /*
2304 * set the head of the stream list or the template to point to the
2305 * stream that we've just alloced and init'ed, depending on whether
2306 * or not it has a wildcard SSRC value or not
2307 *
2308 * if the template stream has already been set, then the policy is
2309 * inconsistent, so we return a bad_param error code
2310 */
2311 switch (policy->ssrc.type) {
2312 case (ssrc_any_outbound):
2313 if (session->stream_template) {
jfigus857009c2014-11-05 11:17:43 -05002314 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002315 }
2316 session->stream_template = tmp;
2317 session->stream_template->direction = dir_srtp_sender;
2318 break;
2319 case (ssrc_any_inbound):
2320 if (session->stream_template) {
jfigus857009c2014-11-05 11:17:43 -05002321 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002322 }
2323 session->stream_template = tmp;
2324 session->stream_template->direction = dir_srtp_receiver;
2325 break;
2326 case (ssrc_specific):
2327 tmp->next = session->stream_list;
2328 session->stream_list = tmp;
2329 break;
2330 case (ssrc_undefined):
2331 default:
jfigused755f52014-11-19 14:57:19 -05002332 srtp_crypto_free(tmp);
jfigus857009c2014-11-05 11:17:43 -05002333 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002334 }
2335
jfigus857009c2014-11-05 11:17:43 -05002336 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002337}
2338
2339
jfigus857009c2014-11-05 11:17:43 -05002340srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002341srtp_create(srtp_t *session, /* handle for session */
2342 const srtp_policy_t *policy) { /* SRTP policy (list) */
jfigus857009c2014-11-05 11:17:43 -05002343 srtp_err_status_t stat;
Cullen Jennings235513a2005-09-21 22:51:36 +00002344 srtp_ctx_t *ctx;
2345
2346 /* sanity check arguments */
Marcus Sundberg67398e62005-10-05 12:39:51 +00002347 if (session == NULL)
jfigus857009c2014-11-05 11:17:43 -05002348 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002349
2350 /* allocate srtp context and set ctx_ptr */
jfigused755f52014-11-19 14:57:19 -05002351 ctx = (srtp_ctx_t *) srtp_crypto_alloc(sizeof(srtp_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +00002352 if (ctx == NULL)
jfigus857009c2014-11-05 11:17:43 -05002353 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002354 *session = ctx;
2355
2356 /*
2357 * loop over elements in the policy list, allocating and
2358 * initializing a stream for each element
2359 */
2360 ctx->stream_template = NULL;
2361 ctx->stream_list = NULL;
Iñaki Baz Castillo241fec32014-08-21 00:51:00 +02002362 ctx->user_data = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00002363 while (policy != NULL) {
2364
2365 stat = srtp_add_stream(ctx, policy);
2366 if (stat) {
Marcus Sundberg67398e62005-10-05 12:39:51 +00002367 /* clean up everything */
2368 srtp_dealloc(*session);
Joachim Bauch8301c362015-06-07 23:33:29 +02002369 *session = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00002370 return stat;
2371 }
2372
2373 /* set policy to next item in list */
2374 policy = policy->next;
2375 }
2376
jfigus857009c2014-11-05 11:17:43 -05002377 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002378}
2379
2380
jfigus857009c2014-11-05 11:17:43 -05002381srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002382srtp_remove_stream(srtp_t session, uint32_t ssrc) {
2383 srtp_stream_ctx_t *stream, *last_stream;
jfigus857009c2014-11-05 11:17:43 -05002384 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002385
2386 /* sanity check arguments */
2387 if (session == NULL)
jfigus857009c2014-11-05 11:17:43 -05002388 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002389
2390 /* find stream in list; complain if not found */
2391 last_stream = stream = session->stream_list;
2392 while ((stream != NULL) && (ssrc != stream->ssrc)) {
2393 last_stream = stream;
2394 stream = stream->next;
2395 }
2396 if (stream == NULL)
jfigus857009c2014-11-05 11:17:43 -05002397 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00002398
2399 /* remove stream from the list */
Jonathan Lennox20505b32010-05-27 19:22:25 +00002400 if (last_stream == stream)
2401 /* stream was first in list */
2402 session->stream_list = stream->next;
2403 else
2404 last_stream->next = stream->next;
Cullen Jennings235513a2005-09-21 22:51:36 +00002405
2406 /* deallocate the stream */
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002407 status = srtp_stream_dealloc(stream, session->stream_template);
Cullen Jennings235513a2005-09-21 22:51:36 +00002408 if (status)
2409 return status;
2410
jfigus857009c2014-11-05 11:17:43 -05002411 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002412}
2413
2414
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002415srtp_err_status_t
2416srtp_update(srtp_t session, const srtp_policy_t *policy) {
2417 srtp_err_status_t stat;
2418
2419 /* sanity check arguments */
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002420 if ((session == NULL) || (policy == NULL) || (policy->key == NULL)) {
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002421 return srtp_err_status_bad_param;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002422 }
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002423
2424 while (policy != NULL) {
2425 stat = srtp_update_stream(session, policy);
2426 if (stat) {
2427 return stat;
2428 }
2429
2430 /* set policy to next item in list */
2431 policy = policy->next;
2432 }
2433 return srtp_err_status_ok;
2434}
2435
2436
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002437static srtp_err_status_t
2438update_template_streams(srtp_t session, const srtp_policy_t *policy) {
2439 srtp_err_status_t status;
2440 srtp_stream_t new_stream_template;
2441 srtp_stream_t new_stream_list = NULL;
2442
2443 if (session->stream_template == NULL) {
2444 return srtp_err_status_bad_param;
2445 }
2446
2447 /* allocate new template stream */
2448 status = srtp_stream_alloc(&new_stream_template, policy);
2449 if (status) {
2450 return status;
2451 }
2452
2453 /* initialize new template stream */
2454 status = srtp_stream_init(new_stream_template, policy);
2455 if (status) {
2456 srtp_crypto_free(new_stream_template);
2457 return status;
2458 }
2459
2460 /* for all old templated streams */
2461 for (;;) {
2462 srtp_stream_t stream;
2463 uint32_t ssrc;
2464 srtp_xtd_seq_num_t old_index;
Pascal Bühlere0fb3df2015-11-25 09:33:00 +01002465 srtp_rdb_t old_rtcp_rdb;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002466
2467 stream = session->stream_list;
2468 while ((stream != NULL) && (stream->rtp_auth != session->stream_template->rtp_auth)) {
2469 stream = stream->next;
2470 }
2471 if (stream == NULL) {
2472 /* no more templated streams */
2473 break;
2474 }
2475
2476 /* save old extendard seq */
2477 ssrc = stream->ssrc;
2478 old_index = stream->rtp_rdbx.index;
Pascal Bühlere0fb3df2015-11-25 09:33:00 +01002479 old_rtcp_rdb = stream->rtcp_rdb;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002480
2481 /* remove stream */
2482 status = srtp_remove_stream(session, ssrc);
2483 if (status) {
2484 /* free new allocations */
2485 while (new_stream_list != NULL) {
2486 srtp_stream_t next = new_stream_list->next;
2487 srtp_stream_dealloc(new_stream_list, new_stream_template);
2488 new_stream_list = next;
2489 }
2490 srtp_stream_dealloc(new_stream_template, NULL);
2491 return status;
2492 }
2493
2494 /* allocate and initialize a new stream */
2495 status = srtp_stream_clone(new_stream_template, ssrc, &stream);
2496 if (status) {
2497 /* free new allocations */
2498 while (new_stream_list != NULL) {
2499 srtp_stream_t next = new_stream_list->next;
2500 srtp_stream_dealloc(new_stream_list, new_stream_template);
2501 new_stream_list = next;
2502 }
2503 srtp_stream_dealloc(new_stream_template, NULL);
2504 return status;
2505 }
2506
2507 /* add new stream to the head of the new_stream_list */
2508 stream->next = new_stream_list;
2509 new_stream_list = stream;
2510
2511 /* restore old extended seq */
2512 stream->rtp_rdbx.index = old_index;
Pascal Bühlere0fb3df2015-11-25 09:33:00 +01002513 stream->rtcp_rdb = old_rtcp_rdb;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002514 }
2515 /* dealloc old template */
2516 srtp_stream_dealloc(session->stream_template, NULL);
2517 /* set new template */
2518 session->stream_template = new_stream_template;
2519 /* add new list */
2520 if (new_stream_list) {
Pascal Bühler102da172015-11-23 12:49:52 +01002521 srtp_stream_t tail = new_stream_list;
2522 while (tail->next) {
2523 tail = tail->next;
2524 }
2525 tail->next = session->stream_list;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002526 session->stream_list = new_stream_list;
2527 }
2528 return status;
2529}
2530
2531
2532static srtp_err_status_t
2533update_stream(srtp_t session, const srtp_policy_t *policy) {
2534 srtp_err_status_t status;
2535 srtp_xtd_seq_num_t old_index;
Pascal Bühlere0fb3df2015-11-25 09:33:00 +01002536 srtp_rdb_t old_rtcp_rdb;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002537 srtp_stream_t stream;
2538
2539 stream = srtp_get_stream(session, policy->ssrc.value);
2540 if (stream == NULL) {
2541 return srtp_err_status_bad_param;
2542 }
2543
2544 /* save old extendard seq */
2545 old_index = stream->rtp_rdbx.index;
Pascal Bühlere0fb3df2015-11-25 09:33:00 +01002546 old_rtcp_rdb = stream->rtcp_rdb;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002547
2548 status = srtp_remove_stream(session, policy->ssrc.value);
2549 if (status) {
2550 return status;
2551 }
2552
2553 status = srtp_add_stream(session, policy);
2554 if (status) {
2555 return status;
2556 }
2557
2558 stream = srtp_get_stream(session, policy->ssrc.value);
2559 if (stream == NULL) {
2560 return srtp_err_status_fail;
2561 }
2562
2563 /* restore old extended seq */
2564 stream->rtp_rdbx.index = old_index;
Pascal Bühlere0fb3df2015-11-25 09:33:00 +01002565 stream->rtcp_rdb = old_rtcp_rdb;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002566
jfigus49848d12015-11-19 14:54:56 -05002567 return srtp_err_status_ok;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002568}
2569
2570
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002571srtp_err_status_t
2572srtp_update_stream(srtp_t session, const srtp_policy_t *policy) {
2573 srtp_err_status_t status;
2574
2575 /* sanity check arguments */
2576 if ((session == NULL) || (policy == NULL) || (policy->key == NULL))
2577 return srtp_err_status_bad_param;
2578
2579 switch (policy->ssrc.type) {
2580 case (ssrc_any_outbound):
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002581 case (ssrc_any_inbound):
2582 status = update_template_streams(session, policy);
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002583 break;
Pascal Bühler356f1cd2015-11-10 09:05:49 +01002584 case (ssrc_specific):
2585 status = update_stream(session, policy);
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002586 break;
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002587 case (ssrc_undefined):
2588 default:
2589 return srtp_err_status_bad_param;
2590 }
2591
jfigus49848d12015-11-19 14:54:56 -05002592 return status;
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002593}
2594
2595
Cullen Jennings235513a2005-09-21 22:51:36 +00002596/*
2597 * the default policy - provides a convenient way for callers to use
2598 * the default security policy
2599 *
2600 * this policy is that defined in the current SRTP internet draft.
2601 *
2602 */
2603
2604/*
2605 * NOTE: cipher_key_len is really key len (128 bits) plus salt len
2606 * (112 bits)
2607 */
2608/* There are hard-coded 16's for base_key_len in the key generation code */
2609
2610void
jfigus857009c2014-11-05 11:17:43 -05002611srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002612
jfigus67b9c732014-11-20 10:17:21 -05002613 p->cipher_type = SRTP_AES_ICM;
Cullen Jennings235513a2005-09-21 22:51:36 +00002614 p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */
jfigus67b9c732014-11-20 10:17:21 -05002615 p->auth_type = SRTP_HMAC_SHA1;
Cullen Jennings235513a2005-09-21 22:51:36 +00002616 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2617 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2618 p->sec_serv = sec_serv_conf_and_auth;
2619
2620}
2621
2622void
jfigus857009c2014-11-05 11:17:43 -05002623srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002624
jfigus67b9c732014-11-20 10:17:21 -05002625 p->cipher_type = SRTP_AES_ICM;
Cullen Jennings235513a2005-09-21 22:51:36 +00002626 p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */
jfigus67b9c732014-11-20 10:17:21 -05002627 p->auth_type = SRTP_HMAC_SHA1;
Cullen Jennings235513a2005-09-21 22:51:36 +00002628 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2629 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2630 p->sec_serv = sec_serv_conf_and_auth;
2631
2632}
2633
David McGrewa8546882006-01-12 17:56:02 +00002634void
jfigus857009c2014-11-05 11:17:43 -05002635srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p) {
David McGrewa8546882006-01-12 17:56:02 +00002636
2637 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002638 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002639 *
2640 * note that this crypto policy is intended for SRTP, but not SRTCP
2641 */
2642
jfigus67b9c732014-11-20 10:17:21 -05002643 p->cipher_type = SRTP_AES_ICM;
David McGrewa8546882006-01-12 17:56:02 +00002644 p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */
jfigus67b9c732014-11-20 10:17:21 -05002645 p->auth_type = SRTP_HMAC_SHA1;
David McGrewa8546882006-01-12 17:56:02 +00002646 p->auth_key_len = 20; /* 160 bit key */
2647 p->auth_tag_len = 4; /* 32 bit tag */
2648 p->sec_serv = sec_serv_conf_and_auth;
2649
2650}
2651
2652
2653void
jfigus857009c2014-11-05 11:17:43 -05002654srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p) {
David McGrewa8546882006-01-12 17:56:02 +00002655
2656 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002657 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002658 *
2659 * note that this crypto policy is intended for SRTP, but not SRTCP
2660 */
2661
jfigus67b9c732014-11-20 10:17:21 -05002662 p->cipher_type = SRTP_AES_ICM;
David McGrewa8546882006-01-12 17:56:02 +00002663 p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */
jfigus67b9c732014-11-20 10:17:21 -05002664 p->auth_type = SRTP_NULL_AUTH;
David McGrewa8546882006-01-12 17:56:02 +00002665 p->auth_key_len = 0;
2666 p->auth_tag_len = 0;
2667 p->sec_serv = sec_serv_conf;
2668
2669}
2670
2671
2672void
jfigus857009c2014-11-05 11:17:43 -05002673srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p) {
David McGrewa8546882006-01-12 17:56:02 +00002674
2675 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002676 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002677 */
2678
jfigus67b9c732014-11-20 10:17:21 -05002679 p->cipher_type = SRTP_NULL_CIPHER;
David McGrewa8546882006-01-12 17:56:02 +00002680 p->cipher_key_len = 0;
jfigus67b9c732014-11-20 10:17:21 -05002681 p->auth_type = SRTP_HMAC_SHA1;
David McGrewa8546882006-01-12 17:56:02 +00002682 p->auth_key_len = 20;
2683 p->auth_tag_len = 10;
2684 p->sec_serv = sec_serv_auth;
2685
2686}
2687
jfigus267956d2014-11-06 10:49:21 -05002688void
2689srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p) {
2690
2691 /*
2692 * Should only be used for testing
2693 */
2694
jfigus67b9c732014-11-20 10:17:21 -05002695 p->cipher_type = SRTP_NULL_CIPHER;
jfigus267956d2014-11-06 10:49:21 -05002696 p->cipher_key_len = 0;
jfigus67b9c732014-11-20 10:17:21 -05002697 p->auth_type = SRTP_NULL_AUTH;
jfigus267956d2014-11-06 10:49:21 -05002698 p->auth_key_len = 0;
2699 p->auth_tag_len = 0;
2700 p->sec_serv = sec_serv_none;
2701
2702}
2703
David McGrewa8546882006-01-12 17:56:02 +00002704
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002705void
jfigus857009c2014-11-05 11:17:43 -05002706srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p) {
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002707
2708 /*
2709 * corresponds to draft-ietf-avt-big-aes-03.txt
2710 */
2711
jfigus67b9c732014-11-20 10:17:21 -05002712 p->cipher_type = SRTP_AES_ICM;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002713 p->cipher_key_len = 46;
jfigus67b9c732014-11-20 10:17:21 -05002714 p->auth_type = SRTP_HMAC_SHA1;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002715 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2716 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2717 p->sec_serv = sec_serv_conf_and_auth;
2718}
2719
2720
2721void
jfigus857009c2014-11-05 11:17:43 -05002722srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p) {
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002723
2724 /*
2725 * corresponds to draft-ietf-avt-big-aes-03.txt
2726 *
2727 * note that this crypto policy is intended for SRTP, but not SRTCP
2728 */
2729
jfigus67b9c732014-11-20 10:17:21 -05002730 p->cipher_type = SRTP_AES_ICM;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002731 p->cipher_key_len = 46;
jfigus67b9c732014-11-20 10:17:21 -05002732 p->auth_type = SRTP_HMAC_SHA1;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002733 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2734 p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */
2735 p->sec_serv = sec_serv_conf_and_auth;
2736}
2737
jfigus8c36da22013-10-01 16:41:19 -04002738/*
2739 * AES-256 with no authentication.
2740 */
2741void
jfigus857009c2014-11-05 11:17:43 -05002742srtp_crypto_policy_set_aes_cm_256_null_auth (srtp_crypto_policy_t *p)
jfigus8c36da22013-10-01 16:41:19 -04002743{
jfigus67b9c732014-11-20 10:17:21 -05002744 p->cipher_type = SRTP_AES_ICM;
jfigus8c36da22013-10-01 16:41:19 -04002745 p->cipher_key_len = 46;
jfigus67b9c732014-11-20 10:17:21 -05002746 p->auth_type = SRTP_NULL_AUTH;
jfigus8c36da22013-10-01 16:41:19 -04002747 p->auth_key_len = 0;
2748 p->auth_tag_len = 0;
2749 p->sec_serv = sec_serv_conf;
2750}
2751
2752#ifdef OPENSSL
2753/*
2754 * AES-128 GCM mode with 8 octet auth tag.
2755 */
2756void
jfigus857009c2014-11-05 11:17:43 -05002757srtp_crypto_policy_set_aes_gcm_128_8_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002758 p->cipher_type = SRTP_AES_128_GCM;
jfigus857009c2014-11-05 11:17:43 -05002759 p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002760 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002761 p->auth_key_len = 0;
2762 p->auth_tag_len = 8; /* 8 octet tag length */
2763 p->sec_serv = sec_serv_conf_and_auth;
2764}
2765
2766/*
2767 * AES-256 GCM mode with 8 octet auth tag.
2768 */
2769void
jfigus857009c2014-11-05 11:17:43 -05002770srtp_crypto_policy_set_aes_gcm_256_8_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002771 p->cipher_type = SRTP_AES_256_GCM;
jfigus857009c2014-11-05 11:17:43 -05002772 p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002773 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002774 p->auth_key_len = 0;
2775 p->auth_tag_len = 8; /* 8 octet tag length */
2776 p->sec_serv = sec_serv_conf_and_auth;
2777}
2778
2779/*
2780 * AES-128 GCM mode with 8 octet auth tag, no RTCP encryption.
2781 */
2782void
jfigus857009c2014-11-05 11:17:43 -05002783srtp_crypto_policy_set_aes_gcm_128_8_only_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002784 p->cipher_type = SRTP_AES_128_GCM;
jfigus857009c2014-11-05 11:17:43 -05002785 p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002786 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002787 p->auth_key_len = 0;
2788 p->auth_tag_len = 8; /* 8 octet tag length */
2789 p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
2790}
2791
2792/*
2793 * AES-256 GCM mode with 8 octet auth tag, no RTCP encryption.
2794 */
2795void
jfigus857009c2014-11-05 11:17:43 -05002796srtp_crypto_policy_set_aes_gcm_256_8_only_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002797 p->cipher_type = SRTP_AES_256_GCM;
jfigus857009c2014-11-05 11:17:43 -05002798 p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002799 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002800 p->auth_key_len = 0;
2801 p->auth_tag_len = 8; /* 8 octet tag length */
2802 p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
2803}
jfigusc13c1002014-05-08 13:34:53 -04002804
2805/*
2806 * AES-128 GCM mode with 16 octet auth tag.
2807 */
2808void
jfigus857009c2014-11-05 11:17:43 -05002809srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002810 p->cipher_type = SRTP_AES_128_GCM;
jfigus857009c2014-11-05 11:17:43 -05002811 p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002812 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigusc13c1002014-05-08 13:34:53 -04002813 p->auth_key_len = 0;
2814 p->auth_tag_len = 16; /* 16 octet tag length */
2815 p->sec_serv = sec_serv_conf_and_auth;
2816}
2817
2818/*
2819 * AES-256 GCM mode with 16 octet auth tag.
2820 */
2821void
jfigus857009c2014-11-05 11:17:43 -05002822srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002823 p->cipher_type = SRTP_AES_256_GCM;
jfigus857009c2014-11-05 11:17:43 -05002824 p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002825 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigusc13c1002014-05-08 13:34:53 -04002826 p->auth_key_len = 0;
2827 p->auth_tag_len = 16; /* 16 octet tag length */
2828 p->sec_serv = sec_serv_conf_and_auth;
2829}
2830
jfigus8c36da22013-10-01 16:41:19 -04002831#endif
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002832
Cullen Jennings235513a2005-09-21 22:51:36 +00002833/*
2834 * secure rtcp functions
2835 */
2836
jfigus8c36da22013-10-01 16:41:19 -04002837/*
2838 * AEAD uses a new IV formation method. This function implements
2839 * section 10.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The
2840 * calculation is defined as, where (+) is the xor operation:
2841 *
2842 * 0 1 2 3 4 5 6 7 8 9 10 11
2843 * +--+--+--+--+--+--+--+--+--+--+--+--+
2844 * |00|00| SSRC |00|00|0+SRTCP Idx|---+
2845 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2846 * |
2847 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2848 * | Encryption Salt |->(+)
2849 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2850 * |
2851 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2852 * | Initialization Vector |<--+
2853 * +--+--+--+--+--+--+--+--+--+--+--+--+*
2854 *
2855 * Input: *stream - pointer to SRTP stream context, used to retrieve
2856 * the SALT
2857 * *iv - Pointer to recieve the calculated IV
2858 * seq_num - The SEQ value to use for the IV calculation.
2859 * *hdr - The RTP header, used to get the SSRC value
2860 *
2861 */
2862static void srtp_calc_aead_iv_srtcp(srtp_stream_ctx_t *stream, v128_t *iv,
2863 uint32_t seq_num, srtcp_hdr_t *hdr)
2864{
2865 v128_t in;
2866 v128_t salt;
2867
2868 memset(&in, 0, sizeof(v128_t));
2869 memset(&salt, 0, sizeof(v128_t));
2870
2871 in.v16[0] = 0;
2872 memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */
2873 in.v16[3] = 0;
2874 in.v32[2] = 0x7FFFFFFF & htonl(seq_num); /* bit 32 is suppose to be zero */
2875
2876 debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in));
2877
2878 /*
2879 * Get the SALT value from the context
2880 */
2881 memcpy(salt.v8, stream->c_salt, 12);
2882 debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt));
2883
2884 /*
2885 * Finally, apply the SALT to the input
2886 */
2887 v128_xor(iv, &in, &salt);
2888}
2889
2890/*
2891 * This code handles AEAD ciphers for outgoing RTCP. We currently support
2892 * AES-GCM mode with 128 or 256 bit keys.
2893 */
jfigus857009c2014-11-05 11:17:43 -05002894static srtp_err_status_t
jfigus8c36da22013-10-01 16:41:19 -04002895srtp_protect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,
Travis Cross31844002014-07-02 16:18:57 +00002896 void *rtcp_hdr, unsigned int *pkt_octet_len)
jfigus8c36da22013-10-01 16:41:19 -04002897{
2898 srtcp_hdr_t *hdr = (srtcp_hdr_t*)rtcp_hdr;
2899 uint32_t *enc_start; /* pointer to start of encrypted portion */
2900 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00002901 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigus8c36da22013-10-01 16:41:19 -04002902 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05002903 srtp_err_status_t status;
jfigus226ec562014-12-02 15:11:03 -05002904 uint32_t tag_len;
jfigus8c36da22013-10-01 16:41:19 -04002905 uint32_t seq_num;
2906 v128_t iv;
2907 uint32_t tseq;
2908
2909 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05002910 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
jfigus8c36da22013-10-01 16:41:19 -04002911
2912 /*
2913 * set encryption start and encryption length - if we're not
2914 * providing confidentiality, set enc_start to NULL
2915 */
2916 enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header;
2917 enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
2918
2919 /* NOTE: hdr->length is not usable - it refers to only the first
2920 RTCP report in the compound packet! */
2921 /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2922 multiples of 32-bits (RFC 3550 6.1) */
2923 trailer = (uint32_t*)((char*)enc_start + enc_octet_len + tag_len);
2924
2925 if (stream->rtcp_services & sec_serv_conf) {
2926 *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
2927 } else {
2928 enc_start = NULL;
2929 enc_octet_len = 0;
2930 /* 0 is network-order independant */
2931 *trailer = 0x00000000; /* set encrypt bit */
2932 }
2933
2934 /*
2935 * set the auth_tag pointer to the proper location, which is after
2936 * the payload, but before the trailer
2937 * (note that srtpc *always* provides authentication, unlike srtp)
2938 */
2939 /* Note: This would need to change for optional mikey data */
2940 auth_tag = (uint8_t*)hdr + *pkt_octet_len;
2941
2942 /*
2943 * check sequence number for overruns, and copy it into the packet
2944 * if its value isn't too big
2945 */
jfigusde8deb32014-11-25 12:58:11 -05002946 status = srtp_rdb_increment(&stream->rtcp_rdb);
jfigus8c36da22013-10-01 16:41:19 -04002947 if (status) {
2948 return status;
2949 }
jfigusde8deb32014-11-25 12:58:11 -05002950 seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
jfigus8c36da22013-10-01 16:41:19 -04002951 *trailer |= htonl(seq_num);
2952 debug_print(mod_srtp, "srtcp index: %x", seq_num);
2953
2954 /*
2955 * Calculating the IV and pass it down to the cipher
2956 */
2957 srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr);
persmulebfec1cd2015-10-24 02:29:57 +08002958 status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_encrypt);
jfigus8c36da22013-10-01 16:41:19 -04002959 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002960 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04002961 }
2962
2963 /*
2964 * Set the AAD for GCM mode
2965 */
2966 if (enc_start) {
2967 /*
2968 * If payload encryption is enabled, then the AAD consist of
2969 * the RTCP header and the seq# at the end of the packet
2970 */
jfigus226ec562014-12-02 15:11:03 -05002971 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header);
jfigus8c36da22013-10-01 16:41:19 -04002972 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002973 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002974 }
2975 } else {
2976 /*
2977 * Since payload encryption is not enabled, we must authenticate
2978 * the entire packet as described in section 10.3 in revision 07
2979 * of the draft.
2980 */
jfigus226ec562014-12-02 15:11:03 -05002981 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, *pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002982 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002983 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002984 }
2985 }
2986 /*
jfigusda852b02016-01-12 09:14:46 -05002987 * Process the sequence# as AAD
jfigus8c36da22013-10-01 16:41:19 -04002988 */
jfigusda852b02016-01-12 09:14:46 -05002989 tseq = *trailer;
jfigus226ec562014-12-02 15:11:03 -05002990 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t));
jfigus8c36da22013-10-01 16:41:19 -04002991 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002992 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002993 }
2994
2995 /* if we're encrypting, exor keystream into the message */
2996 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05002997 status = srtp_cipher_encrypt(stream->rtcp_cipher,
2998 (uint8_t*)enc_start, &enc_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002999 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003000 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04003001 }
3002 /*
3003 * Get the tag and append that to the output
3004 */
jfigus226ec562014-12-02 15:11:03 -05003005 status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len);
jfigus8c36da22013-10-01 16:41:19 -04003006 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003007 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04003008 }
3009 enc_octet_len += tag_len;
3010 } else {
3011 /*
3012 * Even though we're not encrypting the payload, we need
3013 * to run the cipher to get the auth tag.
3014 */
Travis Cross1b8b1e72014-07-02 15:32:36 +00003015 unsigned int nolen = 0;
jfigus2964a152014-11-25 15:25:37 -05003016 status = srtp_cipher_encrypt(stream->rtcp_cipher, NULL, &nolen);
jfigus8c36da22013-10-01 16:41:19 -04003017 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003018 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04003019 }
3020 /*
3021 * Get the tag and append that to the output
3022 */
jfigus226ec562014-12-02 15:11:03 -05003023 status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len);
jfigus8c36da22013-10-01 16:41:19 -04003024 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003025 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04003026 }
3027 enc_octet_len += tag_len;
3028 }
3029
3030 /* increase the packet length by the length of the auth tag and seq_num*/
3031 *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
3032
jfigus857009c2014-11-05 11:17:43 -05003033 return srtp_err_status_ok;
jfigus8c36da22013-10-01 16:41:19 -04003034}
3035
3036/*
3037 * This function handles incoming SRTCP packets while in AEAD mode,
3038 * which currently supports AES-GCM encryption. Note, the auth tag is
3039 * at the end of the packet stream and is automatically checked by GCM
3040 * when decrypting the payload.
3041 */
jfigus857009c2014-11-05 11:17:43 -05003042static srtp_err_status_t
jfigus8c36da22013-10-01 16:41:19 -04003043srtp_unprotect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,
Travis Cross31844002014-07-02 16:18:57 +00003044 void *srtcp_hdr, unsigned int *pkt_octet_len)
jfigus8c36da22013-10-01 16:41:19 -04003045{
3046 srtcp_hdr_t *hdr = (srtcp_hdr_t*)srtcp_hdr;
3047 uint32_t *enc_start; /* pointer to start of encrypted portion */
3048 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00003049 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigus8c36da22013-10-01 16:41:19 -04003050 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05003051 srtp_err_status_t status;
jfigus8c36da22013-10-01 16:41:19 -04003052 int tag_len;
3053 unsigned int tmp_len;
3054 uint32_t seq_num;
3055 v128_t iv;
3056 uint32_t tseq;
3057
3058 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05003059 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
jfigus8c36da22013-10-01 16:41:19 -04003060
jfigus8c36da22013-10-01 16:41:19 -04003061 /*
3062 * set encryption start, encryption length, and trailer
3063 */
3064 /* index & E (encryption) bit follow normal data. hdr->len
3065 is the number of words (32-bit) in the normal packet minus 1 */
3066 /* This should point trailer to the word past the end of the
3067 normal data. */
3068 /* This would need to be modified for optional mikey data */
3069 /*
3070 * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
3071 * multiples of 32-bits (RFC 3550 6.1)
3072 */
3073 trailer = (uint32_t*)((char*)hdr + *pkt_octet_len - sizeof(srtcp_trailer_t));
3074 /*
3075 * We pass the tag down to the cipher when doing GCM mode
3076 */
3077 enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header +
3078 sizeof(srtcp_trailer_t));
3079 auth_tag = (uint8_t*)hdr + *pkt_octet_len - tag_len - sizeof(srtcp_trailer_t);
3080
3081 if (*((unsigned char*)trailer) & SRTCP_E_BYTE_BIT) {
3082 enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header;
3083 } else {
3084 enc_octet_len = 0;
3085 enc_start = NULL; /* this indicates that there's no encryption */
3086 }
3087
3088 /*
3089 * check the sequence number for replays
3090 */
3091 /* this is easier than dealing with bitfield access */
3092 seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK;
3093 debug_print(mod_srtp, "srtcp index: %x", seq_num);
jfigusde8deb32014-11-25 12:58:11 -05003094 status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
jfigus8c36da22013-10-01 16:41:19 -04003095 if (status) {
3096 return status;
3097 }
3098
3099 /*
3100 * Calculate and set the IV
3101 */
3102 srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr);
persmulebfec1cd2015-10-24 02:29:57 +08003103 status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_decrypt);
jfigus8c36da22013-10-01 16:41:19 -04003104 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003105 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04003106 }
3107
3108 /*
3109 * Set the AAD for GCM mode
3110 */
3111 if (enc_start) {
3112 /*
3113 * If payload encryption is enabled, then the AAD consist of
3114 * the RTCP header and the seq# at the end of the packet
3115 */
jfigus226ec562014-12-02 15:11:03 -05003116 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header);
jfigus8c36da22013-10-01 16:41:19 -04003117 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003118 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04003119 }
3120 } else {
3121 /*
3122 * Since payload encryption is not enabled, we must authenticate
3123 * the entire packet as described in section 10.3 in revision 07
3124 * of the draft.
3125 */
jfigus226ec562014-12-02 15:11:03 -05003126 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
3127 (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t)));
jfigus8c36da22013-10-01 16:41:19 -04003128 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003129 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04003130 }
3131 }
3132
3133 /*
jfigusda852b02016-01-12 09:14:46 -05003134 * Process the sequence# as AAD
jfigus8c36da22013-10-01 16:41:19 -04003135 */
jfigusda852b02016-01-12 09:14:46 -05003136 tseq = *trailer;
jfigus226ec562014-12-02 15:11:03 -05003137 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t));
jfigus8c36da22013-10-01 16:41:19 -04003138 if (status) {
jfigus857009c2014-11-05 11:17:43 -05003139 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04003140 }
3141
3142 /* if we're decrypting, exor keystream into the message */
3143 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05003144 status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t*)enc_start, &enc_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04003145 if (status) {
3146 return status;
3147 }
3148 } else {
3149 /*
3150 * Still need to run the cipher to check the tag
3151 */
3152 tmp_len = tag_len;
jfigus2964a152014-11-25 15:25:37 -05003153 status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t*)auth_tag, &tmp_len);
jfigus8c36da22013-10-01 16:41:19 -04003154 if (status) {
3155 return status;
3156 }
3157 }
3158
3159 /* decrease the packet length by the length of the auth tag and seq_num*/
3160 *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
3161
3162 /*
3163 * verify that stream is for received traffic - this check will
3164 * detect SSRC collisions, since a stream that appears in both
3165 * srtp_protect() and srtp_unprotect() will fail this test in one of
3166 * those functions.
3167 *
3168 * we do this check *after* the authentication check, so that the
3169 * latter check will catch any attempts to fool us into thinking
3170 * that we've got a collision
3171 */
3172 if (stream->direction != dir_srtp_receiver) {
3173 if (stream->direction == dir_unknown) {
3174 stream->direction = dir_srtp_receiver;
3175 } else {
3176 srtp_handle_event(ctx, stream, event_ssrc_collision);
3177 }
3178 }
3179
3180 /*
3181 * if the stream is a 'provisional' one, in which the template context
3182 * is used, then we need to allocate a new stream at this point, since
3183 * the authentication passed
3184 */
3185 if (stream == ctx->stream_template) {
3186 srtp_stream_ctx_t *new_stream;
3187
3188 /*
3189 * allocate and initialize a new stream
3190 *
3191 * note that we indicate failure if we can't allocate the new
3192 * stream, and some implementations will want to not return
3193 * failure here
3194 */
3195 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
3196 if (status) {
3197 return status;
3198 }
3199
3200 /* add new stream to the head of the stream_list */
3201 new_stream->next = ctx->stream_list;
3202 ctx->stream_list = new_stream;
3203
3204 /* set stream (the pointer used in this function) */
3205 stream = new_stream;
3206 }
3207
3208 /* we've passed the authentication check, so add seq_num to the rdb */
jfigusde8deb32014-11-25 12:58:11 -05003209 srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
jfigus8c36da22013-10-01 16:41:19 -04003210
jfigus857009c2014-11-05 11:17:43 -05003211 return srtp_err_status_ok;
jfigus8c36da22013-10-01 16:41:19 -04003212}
3213
jfigus857009c2014-11-05 11:17:43 -05003214srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00003215srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00003216 srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00003217 uint32_t *enc_start; /* pointer to start of encrypted portion */
3218 uint32_t *auth_start; /* pointer to start of auth. portion */
3219 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00003220 unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00003221 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05003222 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00003223 int tag_len;
3224 srtp_stream_ctx_t *stream;
jfigus2964a152014-11-25 15:25:37 -05003225 uint32_t prefix_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00003226 uint32_t seq_num;
3227
3228 /* we assume the hdr is 32-bit aligned to start */
Travis Cross8ba46eb2014-06-29 18:42:29 +00003229
3230 /* check the packet length - it must at least contain a full header */
3231 if (*pkt_octet_len < octets_in_rtcp_header)
jfigus857009c2014-11-05 11:17:43 -05003232 return srtp_err_status_bad_param;
Travis Cross8ba46eb2014-06-29 18:42:29 +00003233
Cullen Jennings235513a2005-09-21 22:51:36 +00003234 /*
3235 * look up ssrc in srtp_stream list, and process the packet with
3236 * the appropriate stream. if we haven't seen this stream before,
3237 * there's only one key for this srtp_session, and the cipher
3238 * supports key-sharing, then we assume that a new stream using
3239 * that key has just started up
3240 */
3241 stream = srtp_get_stream(ctx, hdr->ssrc);
3242 if (stream == NULL) {
3243 if (ctx->stream_template != NULL) {
3244 srtp_stream_ctx_t *new_stream;
3245
3246 /* allocate and initialize a new stream */
3247 status = srtp_stream_clone(ctx->stream_template,
3248 hdr->ssrc, &new_stream);
3249 if (status)
3250 return status;
3251
3252 /* add new stream to the head of the stream_list */
3253 new_stream->next = ctx->stream_list;
3254 ctx->stream_list = new_stream;
3255
3256 /* set stream (the pointer used in this function) */
3257 stream = new_stream;
3258 } else {
3259 /* no template stream, so we return an error */
jfigus857009c2014-11-05 11:17:43 -05003260 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00003261 }
3262 }
3263
3264 /*
3265 * verify that stream is for sending traffic - this check will
3266 * detect SSRC collisions, since a stream that appears in both
3267 * srtp_protect() and srtp_unprotect() will fail this test in one of
3268 * those functions.
3269 */
3270 if (stream->direction != dir_srtp_sender) {
3271 if (stream->direction == dir_unknown) {
David McGrewc34f7402006-03-09 21:17:00 +00003272 stream->direction = dir_srtp_sender;
Cullen Jennings235513a2005-09-21 22:51:36 +00003273 } else {
3274 srtp_handle_event(ctx, stream, event_ssrc_collision);
3275 }
3276 }
3277
jfigus8c36da22013-10-01 16:41:19 -04003278 /*
3279 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
3280 * the request to our AEAD handler.
3281 */
jfigus67b9c732014-11-20 10:17:21 -05003282 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
3283 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00003284 return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04003285 }
3286
Cullen Jennings235513a2005-09-21 22:51:36 +00003287 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05003288 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00003289
3290 /*
3291 * set encryption start and encryption length - if we're not
3292 * providing confidentiality, set enc_start to NULL
3293 */
3294 enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
3295 enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
3296
3297 /* all of the packet, except the header, gets encrypted */
3298 /* NOTE: hdr->length is not usable - it refers to only the first
3299 RTCP report in the compound packet! */
3300 /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
3301 multiples of 32-bits (RFC 3550 6.1) */
3302 trailer = (uint32_t *) ((char *)enc_start + enc_octet_len);
3303
3304 if (stream->rtcp_services & sec_serv_conf) {
3305 *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
3306 } else {
3307 enc_start = NULL;
3308 enc_octet_len = 0;
3309 /* 0 is network-order independant */
3310 *trailer = 0x00000000; /* set encrypt bit */
3311 }
3312
3313 /*
3314 * set the auth_start and auth_tag pointers to the proper locations
3315 * (note that srtpc *always* provides authentication, unlike srtp)
3316 */
3317 /* Note: This would need to change for optional mikey data */
3318 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00003319 auth_tag = (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t);
Cullen Jennings235513a2005-09-21 22:51:36 +00003320
David McGrew79870d62007-06-15 18:17:39 +00003321 /* perform EKT processing if needed */
jfigusc5887e72014-11-06 09:46:18 -05003322 srtp_ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len,
jfigusde8deb32014-11-25 12:58:11 -05003323 srtp_rdbx_get_packet_index(&stream->rtp_rdbx));
David McGrew79870d62007-06-15 18:17:39 +00003324
Cullen Jennings235513a2005-09-21 22:51:36 +00003325 /*
3326 * check sequence number for overruns, and copy it into the packet
3327 * if its value isn't too big
3328 */
jfigusde8deb32014-11-25 12:58:11 -05003329 status = srtp_rdb_increment(&stream->rtcp_rdb);
Cullen Jennings235513a2005-09-21 22:51:36 +00003330 if (status)
3331 return status;
jfigusde8deb32014-11-25 12:58:11 -05003332 seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
Cullen Jennings235513a2005-09-21 22:51:36 +00003333 *trailer |= htonl(seq_num);
3334 debug_print(mod_srtp, "srtcp index: %x", seq_num);
3335
3336 /*
3337 * if we're using rindael counter mode, set nonce and seq
3338 */
jfigus67b9c732014-11-20 10:17:21 -05003339 if (stream->rtcp_cipher->type->id == SRTP_AES_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00003340 v128_t iv;
3341
3342 iv.v32[0] = 0;
3343 iv.v32[1] = hdr->ssrc; /* still in network order! */
3344 iv.v32[2] = htonl(seq_num >> 16);
3345 iv.v32[3] = htonl(seq_num << 16);
persmulebfec1cd2015-10-24 02:29:57 +08003346 status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00003347
3348 } else {
3349 v128_t iv;
3350
3351 /* otherwise, just set the index to seq_num */
3352 iv.v32[0] = 0;
3353 iv.v32[1] = 0;
3354 iv.v32[2] = 0;
3355 iv.v32[3] = htonl(seq_num);
persmulebfec1cd2015-10-24 02:29:57 +08003356 status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00003357 }
3358 if (status)
jfigus857009c2014-11-05 11:17:43 -05003359 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003360
3361 /*
3362 * if we're authenticating using a universal hash, put the keystream
3363 * prefix into the authentication tag
3364 */
3365
3366 /* if auth_start is non-null, then put keystream into tag */
3367 if (auth_start) {
3368
3369 /* put keystream prefix into auth_tag */
jfigus8f669722014-11-19 15:20:03 -05003370 prefix_len = srtp_auth_get_prefix_length(stream->rtcp_auth);
jfigus2964a152014-11-25 15:25:37 -05003371 status = srtp_cipher_output(stream->rtcp_cipher, auth_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00003372
3373 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05003374 srtp_octet_string_hex_string(auth_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003375
3376 if (status)
jfigus857009c2014-11-05 11:17:43 -05003377 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003378 }
3379
3380 /* if we're encrypting, exor keystream into the message */
3381 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05003382 status = srtp_cipher_encrypt(stream->rtcp_cipher,
3383 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00003384 if (status)
jfigus857009c2014-11-05 11:17:43 -05003385 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003386 }
3387
3388 /* initialize auth func context */
3389 auth_start(stream->rtcp_auth);
3390
David McGrew9c70f292006-05-03 19:38:38 +00003391 /*
3392 * run auth func over packet (including trailer), and write the
3393 * result at auth_tag
3394 */
Cullen Jennings235513a2005-09-21 22:51:36 +00003395 status = auth_compute(stream->rtcp_auth,
David McGrew9c70f292006-05-03 19:38:38 +00003396 (uint8_t *)auth_start,
3397 (*pkt_octet_len) + sizeof(srtcp_trailer_t),
3398 auth_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00003399 debug_print(mod_srtp, "srtcp auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05003400 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003401 if (status)
jfigus857009c2014-11-05 11:17:43 -05003402 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003403
3404 /* increase the packet length by the length of the auth tag and seq_num*/
3405 *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
3406
jfigus857009c2014-11-05 11:17:43 -05003407 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00003408}
3409
3410
jfigus857009c2014-11-05 11:17:43 -05003411srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00003412srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00003413 srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00003414 uint32_t *enc_start; /* pointer to start of encrypted portion */
3415 uint32_t *auth_start; /* pointer to start of auth. portion */
3416 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00003417 unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00003418 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
3419 uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
David McGrew79870d62007-06-15 18:17:39 +00003420 uint8_t tag_copy[SRTP_MAX_TAG_LEN];
jfigus857009c2014-11-05 11:17:43 -05003421 srtp_err_status_t status;
Travis Cross1b8b1e72014-07-02 15:32:36 +00003422 unsigned int auth_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00003423 int tag_len;
3424 srtp_stream_ctx_t *stream;
jfigus2964a152014-11-25 15:25:37 -05003425 uint32_t prefix_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00003426 uint32_t seq_num;
TV Sriram4986a362013-05-06 11:24:03 -07003427 int e_bit_in_packet; /* whether the E-bit was found in the packet */
3428 int sec_serv_confidentiality; /* whether confidentiality was requested */
Cullen Jennings235513a2005-09-21 22:51:36 +00003429
3430 /* we assume the hdr is 32-bit aligned to start */
Travis Cross444a5442014-07-02 16:01:01 +00003431
Travis Crosse896bf72014-07-07 19:59:33 +00003432 /* check that the length value is sane; we'll check again once we
3433 know the tag length, but we at least want to know that it is
3434 a positive value */
3435 if (*pkt_octet_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t))
jfigus857009c2014-11-05 11:17:43 -05003436 return srtp_err_status_bad_param;
Travis Crosse896bf72014-07-07 19:59:33 +00003437
Cullen Jennings235513a2005-09-21 22:51:36 +00003438 /*
3439 * look up ssrc in srtp_stream list, and process the packet with
3440 * the appropriate stream. if we haven't seen this stream before,
3441 * there's only one key for this srtp_session, and the cipher
3442 * supports key-sharing, then we assume that a new stream using
3443 * that key has just started up
3444 */
3445 stream = srtp_get_stream(ctx, hdr->ssrc);
3446 if (stream == NULL) {
3447 if (ctx->stream_template != NULL) {
3448 stream = ctx->stream_template;
David McGrew79870d62007-06-15 18:17:39 +00003449
3450 /*
3451 * check to see if stream_template has an EKT data structure, in
3452 * which case we initialize the template using the EKT policy
3453 * referenced by that data (which consists of decrypting the
3454 * master key from the EKT field)
3455 *
3456 * this function initializes a *provisional* stream, and this
3457 * stream should not be accepted until and unless the packet
3458 * passes its authentication check
3459 */
3460 if (stream->ekt != NULL) {
3461 status = srtp_stream_init_from_ekt(stream, srtcp_hdr, *pkt_octet_len);
3462 if (status)
3463 return status;
3464 }
3465
Cullen Jennings235513a2005-09-21 22:51:36 +00003466 debug_print(mod_srtp, "srtcp using provisional stream (SSRC: 0x%08x)",
3467 hdr->ssrc);
3468 } else {
3469 /* no template stream, so we return an error */
jfigus857009c2014-11-05 11:17:43 -05003470 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00003471 }
3472 }
3473
Travis Cross8ba46eb2014-06-29 18:42:29 +00003474 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05003475 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
Travis Cross8ba46eb2014-06-29 18:42:29 +00003476
3477 /* check the packet length - it must contain at least a full RTCP
3478 header, an auth tag (if applicable), and the SRTCP encrypted flag
3479 and 31-bit index value */
Joachim Bauch25a0e6c2015-03-27 16:59:06 +01003480 if (*pkt_octet_len < (int) (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t))) {
jfigus857009c2014-11-05 11:17:43 -05003481 return srtp_err_status_bad_param;
jfigus73e30932014-07-07 15:50:32 -04003482 }
Travis Cross8ba46eb2014-06-29 18:42:29 +00003483
jfigus8c36da22013-10-01 16:41:19 -04003484 /*
3485 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
3486 * the request to our AEAD handler.
3487 */
jfigus67b9c732014-11-20 10:17:21 -05003488 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
3489 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00003490 return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04003491 }
3492
TV Sriram4986a362013-05-06 11:24:03 -07003493 sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf ||
3494 stream->rtcp_services == sec_serv_conf_and_auth;
3495
Cullen Jennings235513a2005-09-21 22:51:36 +00003496 /*
3497 * set encryption start, encryption length, and trailer
3498 */
3499 enc_octet_len = *pkt_octet_len -
3500 (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t));
3501 /* index & E (encryption) bit follow normal data. hdr->len
3502 is the number of words (32-bit) in the normal packet minus 1 */
3503 /* This should point trailer to the word past the end of the
3504 normal data. */
3505 /* This would need to be modified for optional mikey data */
3506 /*
3507 * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
3508 * multiples of 32-bits (RFC 3550 6.1)
3509 */
3510 trailer = (uint32_t *) ((char *) hdr +
TV Sriram4986a362013-05-06 11:24:03 -07003511 *pkt_octet_len -(tag_len + sizeof(srtcp_trailer_t)));
3512 e_bit_in_packet =
3513 (*((unsigned char *) trailer) & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT;
3514 if (e_bit_in_packet != sec_serv_confidentiality) {
jfigus857009c2014-11-05 11:17:43 -05003515 return srtp_err_status_cant_check;
TV Sriram4986a362013-05-06 11:24:03 -07003516 }
3517 if (sec_serv_confidentiality) {
Cullen Jennings235513a2005-09-21 22:51:36 +00003518 enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
3519 } else {
3520 enc_octet_len = 0;
3521 enc_start = NULL; /* this indicates that there's no encryption */
3522 }
3523
3524 /*
3525 * set the auth_start and auth_tag pointers to the proper locations
3526 * (note that srtcp *always* uses authentication, unlike srtp)
3527 */
3528 auth_start = (uint32_t *)hdr;
David McGrew79870d62007-06-15 18:17:39 +00003529 auth_len = *pkt_octet_len - tag_len;
3530 auth_tag = (uint8_t *)hdr + auth_len;
3531
3532 /*
3533 * if EKT is in use, then we make a copy of the tag from the packet,
3534 * and then zeroize the location of the base tag
3535 *
3536 * we first re-position the auth_tag pointer so that it points to
3537 * the base tag
3538 */
3539 if (stream->ekt) {
jfigusc5887e72014-11-06 09:46:18 -05003540 auth_tag -= srtp_ekt_octets_after_base_tag(stream->ekt);
David McGrew79870d62007-06-15 18:17:39 +00003541 memcpy(tag_copy, auth_tag, tag_len);
3542 octet_string_set_to_zero(auth_tag, tag_len);
3543 auth_tag = tag_copy;
3544 auth_len += tag_len;
3545 }
Cullen Jennings235513a2005-09-21 22:51:36 +00003546
3547 /*
3548 * check the sequence number for replays
3549 */
3550 /* this is easier than dealing with bitfield access */
3551 seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK;
David McGrew9c70f292006-05-03 19:38:38 +00003552 debug_print(mod_srtp, "srtcp index: %x", seq_num);
jfigusde8deb32014-11-25 12:58:11 -05003553 status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
Cullen Jennings235513a2005-09-21 22:51:36 +00003554 if (status)
3555 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00003556
3557 /*
3558 * if we're using aes counter mode, set nonce and seq
3559 */
jfigus67b9c732014-11-20 10:17:21 -05003560 if (stream->rtcp_cipher->type->id == SRTP_AES_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00003561 v128_t iv;
3562
3563 iv.v32[0] = 0;
3564 iv.v32[1] = hdr->ssrc; /* still in network order! */
3565 iv.v32[2] = htonl(seq_num >> 16);
3566 iv.v32[3] = htonl(seq_num << 16);
persmulebfec1cd2015-10-24 02:29:57 +08003567 status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00003568
3569 } else {
3570 v128_t iv;
3571
3572 /* otherwise, just set the index to seq_num */
3573 iv.v32[0] = 0;
3574 iv.v32[1] = 0;
3575 iv.v32[2] = 0;
3576 iv.v32[3] = htonl(seq_num);
persmulebfec1cd2015-10-24 02:29:57 +08003577 status = srtp_cipher_set_iv(stream->rtcp_cipher, (uint8_t*)&iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00003578
3579 }
3580 if (status)
jfigus857009c2014-11-05 11:17:43 -05003581 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003582
3583 /* initialize auth func context */
3584 auth_start(stream->rtcp_auth);
3585
3586 /* run auth func over packet, put result into tmp_tag */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00003587 status = auth_compute(stream->rtcp_auth, (uint8_t *)auth_start,
David McGrew79870d62007-06-15 18:17:39 +00003588 auth_len, tmp_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00003589 debug_print(mod_srtp, "srtcp computed tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05003590 srtp_octet_string_hex_string(tmp_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003591 if (status)
jfigus857009c2014-11-05 11:17:43 -05003592 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003593
3594 /* compare the tag just computed with the one in the packet */
3595 debug_print(mod_srtp, "srtcp tag from packet: %s",
jfigus46d6b472014-11-14 16:42:01 -05003596 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003597 if (octet_string_is_eq(tmp_tag, auth_tag, tag_len))
jfigus857009c2014-11-05 11:17:43 -05003598 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003599
3600 /*
3601 * if we're authenticating using a universal hash, put the keystream
3602 * prefix into the authentication tag
3603 */
jfigus8f669722014-11-19 15:20:03 -05003604 prefix_len = srtp_auth_get_prefix_length(stream->rtcp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00003605 if (prefix_len) {
jfigus2964a152014-11-25 15:25:37 -05003606 status = srtp_cipher_output(stream->rtcp_cipher, auth_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00003607 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05003608 srtp_octet_string_hex_string(auth_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003609 if (status)
jfigus857009c2014-11-05 11:17:43 -05003610 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003611 }
3612
3613 /* if we're decrypting, exor keystream into the message */
3614 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05003615 status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00003616 if (status)
jfigus857009c2014-11-05 11:17:43 -05003617 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003618 }
3619
David McGrew79870d62007-06-15 18:17:39 +00003620 /* decrease the packet length by the length of the auth tag and seq_num */
Cullen Jennings235513a2005-09-21 22:51:36 +00003621 *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
3622
David McGrew79870d62007-06-15 18:17:39 +00003623 /*
3624 * if EKT is in effect, subtract the EKT data out of the packet
3625 * length
3626 */
jfigusc5887e72014-11-06 09:46:18 -05003627 *pkt_octet_len -= srtp_ekt_octets_after_base_tag(stream->ekt);
David McGrew79870d62007-06-15 18:17:39 +00003628
Cullen Jennings235513a2005-09-21 22:51:36 +00003629 /*
3630 * verify that stream is for received traffic - this check will
3631 * detect SSRC collisions, since a stream that appears in both
3632 * srtp_protect() and srtp_unprotect() will fail this test in one of
3633 * those functions.
3634 *
3635 * we do this check *after* the authentication check, so that the
3636 * latter check will catch any attempts to fool us into thinking
3637 * that we've got a collision
3638 */
3639 if (stream->direction != dir_srtp_receiver) {
3640 if (stream->direction == dir_unknown) {
3641 stream->direction = dir_srtp_receiver;
3642 } else {
3643 srtp_handle_event(ctx, stream, event_ssrc_collision);
3644 }
3645 }
3646
3647 /*
3648 * if the stream is a 'provisional' one, in which the template context
3649 * is used, then we need to allocate a new stream at this point, since
3650 * the authentication passed
3651 */
3652 if (stream == ctx->stream_template) {
3653 srtp_stream_ctx_t *new_stream;
3654
3655 /*
3656 * allocate and initialize a new stream
3657 *
3658 * note that we indicate failure if we can't allocate the new
3659 * stream, and some implementations will want to not return
3660 * failure here
3661 */
3662 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
3663 if (status)
3664 return status;
3665
3666 /* add new stream to the head of the stream_list */
3667 new_stream->next = ctx->stream_list;
3668 ctx->stream_list = new_stream;
3669
3670 /* set stream (the pointer used in this function) */
3671 stream = new_stream;
3672 }
3673
3674 /* we've passed the authentication check, so add seq_num to the rdb */
jfigusde8deb32014-11-25 12:58:11 -05003675 srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
Cullen Jennings235513a2005-09-21 22:51:36 +00003676
3677
jfigus857009c2014-11-05 11:17:43 -05003678 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00003679}
David McGrew0cb86ee2006-07-07 15:46:57 +00003680
3681
Iñaki Baz Castillo241fec32014-08-21 00:51:00 +02003682/*
3683 * user data within srtp_t context
3684 */
3685
3686void
3687srtp_set_user_data(srtp_t ctx, void *data) {
3688 ctx->user_data = data;
3689}
3690
3691void*
3692srtp_get_user_data(srtp_t ctx) {
3693 return ctx->user_data;
3694}
3695
David McGrew0cb86ee2006-07-07 15:46:57 +00003696
3697/*
3698 * dtls keying for srtp
3699 */
3700
jfigus857009c2014-11-05 11:17:43 -05003701srtp_err_status_t
3702srtp_crypto_policy_set_from_profile_for_rtp(srtp_crypto_policy_t *policy,
3703 srtp_profile_t profile) {
David McGrew0cb86ee2006-07-07 15:46:57 +00003704
3705 /* set SRTP policy from the SRTP profile in the key set */
3706 switch(profile) {
3707 case srtp_profile_aes128_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003708 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003709 break;
3710 case srtp_profile_aes128_cm_sha1_32:
jfigus857009c2014-11-05 11:17:43 -05003711 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003712 break;
3713 case srtp_profile_null_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003714 srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003715 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003716 case srtp_profile_aes256_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003717 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003718 break;
3719 case srtp_profile_aes256_cm_sha1_32:
jfigus857009c2014-11-05 11:17:43 -05003720 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003721 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003722 /* the following profiles are not (yet) supported */
3723 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003724 default:
jfigus857009c2014-11-05 11:17:43 -05003725 return srtp_err_status_bad_param;
David McGrew0cb86ee2006-07-07 15:46:57 +00003726 }
3727
jfigus857009c2014-11-05 11:17:43 -05003728 return srtp_err_status_ok;
David McGrew0cb86ee2006-07-07 15:46:57 +00003729}
3730
jfigus857009c2014-11-05 11:17:43 -05003731srtp_err_status_t
3732srtp_crypto_policy_set_from_profile_for_rtcp(srtp_crypto_policy_t *policy,
3733 srtp_profile_t profile) {
David McGrew0cb86ee2006-07-07 15:46:57 +00003734
3735 /* set SRTP policy from the SRTP profile in the key set */
3736 switch(profile) {
3737 case srtp_profile_aes128_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003738 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003739 break;
3740 case srtp_profile_aes128_cm_sha1_32:
jfigus0acbb032013-05-30 16:47:02 -04003741 /* We do not honor the 32-bit auth tag request since
3742 * this is not compliant with RFC 3711 */
jfigus857009c2014-11-05 11:17:43 -05003743 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003744 break;
3745 case srtp_profile_null_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003746 srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003747 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003748 case srtp_profile_aes256_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003749 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003750 break;
3751 case srtp_profile_aes256_cm_sha1_32:
jfigus0acbb032013-05-30 16:47:02 -04003752 /* We do not honor the 32-bit auth tag request since
3753 * this is not compliant with RFC 3711 */
jfigus857009c2014-11-05 11:17:43 -05003754 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003755 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003756 /* the following profiles are not (yet) supported */
3757 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003758 default:
jfigus857009c2014-11-05 11:17:43 -05003759 return srtp_err_status_bad_param;
David McGrew0cb86ee2006-07-07 15:46:57 +00003760 }
3761
jfigus857009c2014-11-05 11:17:43 -05003762 return srtp_err_status_ok;
David McGrew0cb86ee2006-07-07 15:46:57 +00003763}
3764
jfigus857009c2014-11-05 11:17:43 -05003765void srtp_append_salt_to_key(uint8_t *key, unsigned int bytes_in_key, uint8_t *salt, unsigned int bytes_in_salt) {
David McGrew0cb86ee2006-07-07 15:46:57 +00003766 memcpy(key + bytes_in_key, salt, bytes_in_salt);
David McGrew0cb86ee2006-07-07 15:46:57 +00003767}
3768
3769unsigned int
3770srtp_profile_get_master_key_length(srtp_profile_t profile) {
3771
3772 switch(profile) {
3773 case srtp_profile_aes128_cm_sha1_80:
3774 return 16;
3775 break;
3776 case srtp_profile_aes128_cm_sha1_32:
3777 return 16;
3778 break;
3779 case srtp_profile_null_sha1_80:
3780 return 16;
3781 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003782 case srtp_profile_aes256_cm_sha1_80:
3783 return 32;
3784 break;
3785 case srtp_profile_aes256_cm_sha1_32:
3786 return 32;
3787 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003788 /* the following profiles are not (yet) supported */
3789 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003790 default:
3791 return 0; /* indicate error by returning a zero */
3792 }
3793}
3794
3795unsigned int
3796srtp_profile_get_master_salt_length(srtp_profile_t profile) {
3797
3798 switch(profile) {
3799 case srtp_profile_aes128_cm_sha1_80:
3800 return 14;
3801 break;
3802 case srtp_profile_aes128_cm_sha1_32:
3803 return 14;
3804 break;
3805 case srtp_profile_null_sha1_80:
3806 return 14;
3807 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003808 case srtp_profile_aes256_cm_sha1_80:
3809 return 14;
3810 break;
3811 case srtp_profile_aes256_cm_sha1_32:
3812 return 14;
3813 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003814 /* the following profiles are not (yet) supported */
3815 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003816 default:
3817 return 0; /* indicate error by returning a zero */
3818 }
3819}
jfigus46d6b472014-11-14 16:42:01 -05003820
3821/*
3822 * SRTP debug interface
3823 */
3824srtp_err_status_t srtp_set_debug_module(char *mod_name, int v)
3825{
jfigus92736bc2014-11-21 10:30:54 -05003826 return srtp_crypto_kernel_set_debug_module(mod_name, v);
jfigus46d6b472014-11-14 16:42:01 -05003827}
3828
3829srtp_err_status_t srtp_list_debug_modules(void)
3830{
jfigus92736bc2014-11-21 10:30:54 -05003831 return srtp_crypto_kernel_list_debug_modules();
jfigus46d6b472014-11-14 16:42:01 -05003832}
3833