blob: 62681c5968622176bd4e79ca62bed8f2b38dce07 [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
jfigus857009c2014-11-05 11:17:43 -0500144srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000145srtp_stream_alloc(srtp_stream_ctx_t **str_ptr,
146 const srtp_policy_t *p) {
147 srtp_stream_ctx_t *str;
jfigus857009c2014-11-05 11:17:43 -0500148 srtp_err_status_t stat;
Cullen Jennings235513a2005-09-21 22:51:36 +0000149
150 /*
151 * This function allocates the stream context, rtp and rtcp ciphers
152 * and auth functions, and key limit structure. If there is a
153 * failure during allocation, we free all previously allocated
154 * memory and return a failure code. The code could probably
155 * be improved, but it works and should be clear.
156 */
157
158 /* allocate srtp stream and set str_ptr */
jfigused755f52014-11-19 14:57:19 -0500159 str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000160 if (str == NULL)
jfigus857009c2014-11-05 11:17:43 -0500161 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000162 *str_ptr = str;
163
164 /* allocate cipher */
jfigus92736bc2014-11-21 10:30:54 -0500165 stat = srtp_crypto_kernel_alloc_cipher(p->rtp.cipher_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000166 &str->rtp_cipher,
jfigusc13c1002014-05-08 13:34:53 -0400167 p->rtp.cipher_key_len,
168 p->rtp.auth_tag_len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000169 if (stat) {
jfigused755f52014-11-19 14:57:19 -0500170 srtp_crypto_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000171 return stat;
172 }
173
174 /* allocate auth function */
jfigus92736bc2014-11-21 10:30:54 -0500175 stat = srtp_crypto_kernel_alloc_auth(p->rtp.auth_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000176 &str->rtp_auth,
177 p->rtp.auth_key_len,
178 p->rtp.auth_tag_len);
179 if (stat) {
jfigus3f93c3c2014-12-01 15:38:09 -0500180 srtp_cipher_dealloc(str->rtp_cipher);
jfigused755f52014-11-19 14:57:19 -0500181 srtp_crypto_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000182 return stat;
183 }
184
185 /* allocate key limit structure */
jfigusc7cdc9a2014-11-19 16:19:08 -0500186 str->limit = (srtp_key_limit_ctx_t*) srtp_crypto_alloc(sizeof(srtp_key_limit_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000187 if (str->limit == NULL) {
188 auth_dealloc(str->rtp_auth);
jfigus3f93c3c2014-12-01 15:38:09 -0500189 srtp_cipher_dealloc(str->rtp_cipher);
jfigused755f52014-11-19 14:57:19 -0500190 srtp_crypto_free(str);
jfigus857009c2014-11-05 11:17:43 -0500191 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000192 }
193
194 /*
195 * ...and now the RTCP-specific initialization - first, allocate
196 * the cipher
197 */
jfigus92736bc2014-11-21 10:30:54 -0500198 stat = srtp_crypto_kernel_alloc_cipher(p->rtcp.cipher_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000199 &str->rtcp_cipher,
jfigusc13c1002014-05-08 13:34:53 -0400200 p->rtcp.cipher_key_len,
201 p->rtcp.auth_tag_len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000202 if (stat) {
203 auth_dealloc(str->rtp_auth);
jfigus3f93c3c2014-12-01 15:38:09 -0500204 srtp_cipher_dealloc(str->rtp_cipher);
jfigused755f52014-11-19 14:57:19 -0500205 srtp_crypto_free(str->limit);
206 srtp_crypto_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000207 return stat;
208 }
209
210 /* allocate auth function */
jfigus92736bc2014-11-21 10:30:54 -0500211 stat = srtp_crypto_kernel_alloc_auth(p->rtcp.auth_type,
Cullen Jennings235513a2005-09-21 22:51:36 +0000212 &str->rtcp_auth,
213 p->rtcp.auth_key_len,
214 p->rtcp.auth_tag_len);
215 if (stat) {
jfigus3f93c3c2014-12-01 15:38:09 -0500216 srtp_cipher_dealloc(str->rtcp_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +0000217 auth_dealloc(str->rtp_auth);
jfigus3f93c3c2014-12-01 15:38:09 -0500218 srtp_cipher_dealloc(str->rtp_cipher);
jfigused755f52014-11-19 14:57:19 -0500219 srtp_crypto_free(str->limit);
220 srtp_crypto_free(str);
Cullen Jennings235513a2005-09-21 22:51:36 +0000221 return stat;
222 }
223
David McGrew79870d62007-06-15 18:17:39 +0000224 /* allocate ekt data associated with stream */
jfigusc5887e72014-11-06 09:46:18 -0500225 stat = srtp_ekt_alloc(&str->ekt, p->ekt);
David McGrew79870d62007-06-15 18:17:39 +0000226 if (stat) {
227 auth_dealloc(str->rtcp_auth);
jfigus3f93c3c2014-12-01 15:38:09 -0500228 srtp_cipher_dealloc(str->rtcp_cipher);
David McGrew79870d62007-06-15 18:17:39 +0000229 auth_dealloc(str->rtp_auth);
jfigus3f93c3c2014-12-01 15:38:09 -0500230 srtp_cipher_dealloc(str->rtp_cipher);
jfigused755f52014-11-19 14:57:19 -0500231 srtp_crypto_free(str->limit);
232 srtp_crypto_free(str);
David McGrew79870d62007-06-15 18:17:39 +0000233 return stat;
234 }
235
jfigus857009c2014-11-05 11:17:43 -0500236 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000237}
238
jfigus857009c2014-11-05 11:17:43 -0500239srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000240srtp_stream_dealloc(srtp_t session, srtp_stream_ctx_t *stream) {
jfigus857009c2014-11-05 11:17:43 -0500241 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000242
243 /*
244 * we use a conservative deallocation strategy - if any deallocation
245 * fails, then we report that fact without trying to deallocate
246 * anything else
247 */
248
249 /* deallocate cipher, if it is not the same as that in template */
David McGrewfec49dd2005-09-23 19:34:11 +0000250 if (session->stream_template
Cullen Jennings235513a2005-09-21 22:51:36 +0000251 && stream->rtp_cipher == session->stream_template->rtp_cipher) {
252 /* do nothing */
253 } else {
jfigus3f93c3c2014-12-01 15:38:09 -0500254 status = srtp_cipher_dealloc(stream->rtp_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +0000255 if (status)
David McGrewfec49dd2005-09-23 19:34:11 +0000256 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000257 }
258
259 /* deallocate auth function, if it is not the same as that in template */
260 if (session->stream_template
261 && stream->rtp_auth == session->stream_template->rtp_auth) {
262 /* do nothing */
263 } else {
264 status = auth_dealloc(stream->rtp_auth);
265 if (status)
266 return status;
267 }
268
David McGrewfec49dd2005-09-23 19:34:11 +0000269 /* deallocate key usage limit, if it is not the same as that in template */
270 if (session->stream_template
271 && stream->limit == session->stream_template->limit) {
272 /* do nothing */
273 } else {
jfigused755f52014-11-19 14:57:19 -0500274 srtp_crypto_free(stream->limit);
David McGrewfec49dd2005-09-23 19:34:11 +0000275 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000276
277 /*
278 * deallocate rtcp cipher, if it is not the same as that in
279 * template
280 */
281 if (session->stream_template
282 && stream->rtcp_cipher == session->stream_template->rtcp_cipher) {
283 /* do nothing */
284 } else {
jfigus3f93c3c2014-12-01 15:38:09 -0500285 status = srtp_cipher_dealloc(stream->rtcp_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +0000286 if (status)
287 return status;
288 }
289
290 /*
291 * deallocate rtcp auth function, if it is not the same as that in
292 * template
293 */
294 if (session->stream_template
295 && stream->rtcp_auth == session->stream_template->rtcp_auth) {
296 /* do nothing */
297 } else {
298 status = auth_dealloc(stream->rtcp_auth);
299 if (status)
300 return status;
301 }
David McGrew79870d62007-06-15 18:17:39 +0000302
jfigusde8deb32014-11-25 12:58:11 -0500303 status = srtp_rdbx_dealloc(&stream->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000304 if (status)
305 return status;
306
David McGrew79870d62007-06-15 18:17:39 +0000307 /* DAM - need to deallocate EKT here */
jfigus8c36da22013-10-01 16:41:19 -0400308
309 /*
310 * zeroize the salt value
311 */
312 memset(stream->salt, 0, SRTP_AEAD_SALT_LEN);
313 memset(stream->c_salt, 0, SRTP_AEAD_SALT_LEN);
314
Cullen Jennings235513a2005-09-21 22:51:36 +0000315
316 /* deallocate srtp stream context */
jfigused755f52014-11-19 14:57:19 -0500317 srtp_crypto_free(stream);
Cullen Jennings235513a2005-09-21 22:51:36 +0000318
jfigus857009c2014-11-05 11:17:43 -0500319 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000320}
321
322
323/*
324 * srtp_stream_clone(stream_template, new) allocates a new stream and
325 * initializes it using the cipher and auth of the stream_template
326 *
327 * the only unique data in a cloned stream is the replay database and
328 * the SSRC
329 */
330
jfigus857009c2014-11-05 11:17:43 -0500331srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000332srtp_stream_clone(const srtp_stream_ctx_t *stream_template,
333 uint32_t ssrc,
334 srtp_stream_ctx_t **str_ptr) {
jfigus857009c2014-11-05 11:17:43 -0500335 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000336 srtp_stream_ctx_t *str;
337
338 debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ssrc);
339
340 /* allocate srtp stream and set str_ptr */
jfigused755f52014-11-19 14:57:19 -0500341 str = (srtp_stream_ctx_t *) srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000342 if (str == NULL)
jfigus857009c2014-11-05 11:17:43 -0500343 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000344 *str_ptr = str;
345
346 /* set cipher and auth pointers to those of the template */
347 str->rtp_cipher = stream_template->rtp_cipher;
348 str->rtp_auth = stream_template->rtp_auth;
349 str->rtcp_cipher = stream_template->rtcp_cipher;
350 str->rtcp_auth = stream_template->rtcp_auth;
351
352 /* set key limit to point to that of the template */
jfigusc7cdc9a2014-11-19 16:19:08 -0500353 status = srtp_key_limit_clone(stream_template->limit, &str->limit);
jfigus8c36da22013-10-01 16:41:19 -0400354 if (status) {
jfigused755f52014-11-19 14:57:19 -0500355 srtp_crypto_free(*str_ptr);
jfigus8c36da22013-10-01 16:41:19 -0400356 *str_ptr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +0000357 return status;
jfigus8c36da22013-10-01 16:41:19 -0400358 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000359
360 /* initialize replay databases */
jfigusde8deb32014-11-25 12:58:11 -0500361 status = srtp_rdbx_init(&str->rtp_rdbx,
362 srtp_rdbx_get_window_size(&stream_template->rtp_rdbx));
jfigus8c36da22013-10-01 16:41:19 -0400363 if (status) {
jfigused755f52014-11-19 14:57:19 -0500364 srtp_crypto_free(*str_ptr);
jfigus8c36da22013-10-01 16:41:19 -0400365 *str_ptr = NULL;
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000366 return status;
jfigus8c36da22013-10-01 16:41:19 -0400367 }
jfigusde8deb32014-11-25 12:58:11 -0500368 srtp_rdb_init(&str->rtcp_rdb);
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000369 str->allow_repeat_tx = stream_template->allow_repeat_tx;
Cullen Jennings235513a2005-09-21 22:51:36 +0000370
371 /* set ssrc to that provided */
372 str->ssrc = ssrc;
373
374 /* set direction and security services */
375 str->direction = stream_template->direction;
376 str->rtp_services = stream_template->rtp_services;
377 str->rtcp_services = stream_template->rtcp_services;
378
David McGrew79870d62007-06-15 18:17:39 +0000379 /* set pointer to EKT data associated with stream */
380 str->ekt = stream_template->ekt;
381
jfigus8c36da22013-10-01 16:41:19 -0400382 /* Copy the salt values */
383 memcpy(str->salt, stream_template->salt, SRTP_AEAD_SALT_LEN);
384 memcpy(str->c_salt, stream_template->c_salt, SRTP_AEAD_SALT_LEN);
385
Cullen Jennings235513a2005-09-21 22:51:36 +0000386 /* defensive coding */
387 str->next = NULL;
388
jfigus857009c2014-11-05 11:17:43 -0500389 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000390}
391
392
393/*
394 * key derivation functions, internal to libSRTP
395 *
396 * srtp_kdf_t is a key derivation context
397 *
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000398 * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher
399 * described by cipher_id, with the master key k with length in octets keylen.
Cullen Jennings235513a2005-09-21 22:51:36 +0000400 *
401 * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key
402 * corresponding to label l and puts it into kl; the length
403 * of the key in octets is provided as keylen. this function
404 * should be called once for each subkey that is derived.
405 *
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000406 * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state
Cullen Jennings235513a2005-09-21 22:51:36 +0000407 */
408
409typedef enum {
410 label_rtp_encryption = 0x00,
411 label_rtp_msg_auth = 0x01,
412 label_rtp_salt = 0x02,
413 label_rtcp_encryption = 0x03,
414 label_rtcp_msg_auth = 0x04,
415 label_rtcp_salt = 0x05
416} srtp_prf_label;
417
jfigus038d2cf2015-05-11 14:10:11 -0400418#define MAX_SRTP_KEY_LEN 256
419
420#if defined(OPENSSL) && defined(OPENSSL_KDF)
421#define MAX_SRTP_AESKEY_LEN 32
422#define MAX_SRTP_SALT_LEN 14
Cullen Jennings235513a2005-09-21 22:51:36 +0000423
424/*
425 * srtp_kdf_t represents a key derivation function. The SRTP
426 * default KDF is the only one implemented at present.
427 */
Cullen Jennings235513a2005-09-21 22:51:36 +0000428typedef struct {
jfigus038d2cf2015-05-11 14:10:11 -0400429 uint8_t master_key[MAX_SRTP_AESKEY_LEN];
430 uint8_t master_salt[MAX_SRTP_SALT_LEN];
431 const EVP_CIPHER *evp;
Cullen Jennings235513a2005-09-21 22:51:36 +0000432} srtp_kdf_t;
433
Cullen Jennings235513a2005-09-21 22:51:36 +0000434
jfigus038d2cf2015-05-11 14:10:11 -0400435static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, const uint8_t *key, int key_len, int salt_len)
436{
437 memset(kdf, 0x0, sizeof(srtp_kdf_t));
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000438
jfigus038d2cf2015-05-11 14:10:11 -0400439 /* The NULL cipher has zero key length */
440 if (key_len == 0) return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000441
jfigus038d2cf2015-05-11 14:10:11 -0400442 if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) {
443 return srtp_err_status_bad_param;
444 }
445 switch (key_len) {
446 case SRTP_AES_256_KEYSIZE:
447 kdf->evp = EVP_aes_256_ctr();
448 break;
449 case SRTP_AES_192_KEYSIZE:
450 kdf->evp = EVP_aes_192_ctr();
451 break;
452 case SRTP_AES_128_KEYSIZE:
453 kdf->evp = EVP_aes_128_ctr();
454 break;
455 default:
456 return srtp_err_status_bad_param;
457 break;
458 }
459 memcpy(kdf->master_key, key, key_len);
460 memcpy(kdf->master_salt, key+key_len, salt_len);
461 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000462}
463
jfigus038d2cf2015-05-11 14:10:11 -0400464static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length)
465{
466 int ret;
Cullen Jennings235513a2005-09-21 22:51:36 +0000467
jfigus038d2cf2015-05-11 14:10:11 -0400468 /* The NULL cipher will not have an EVP */
469 if (!kdf->evp) return srtp_err_status_ok;
470
471 octet_string_set_to_zero(key, length);
472
473 /*
474 * Invoke the OpenSSL SRTP KDF function
475 * This is useful if OpenSSL is in FIPS mode and FIP
476 * compliance is required for SRTP.
477 */
478 ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key, (char *)&kdf->master_salt, NULL, NULL, label, (char *)key);
479 if (ret == -1) {
480 return (srtp_err_status_algo_fail);
481 }
482
483 return srtp_err_status_ok;
484}
485
486static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) {
487 memset(kdf->master_key, 0x0, MAX_SRTP_KEY_LEN);
488 memset(kdf->master_salt, 0x0, MAX_SRTP_KEY_LEN);
489 kdf->evp = NULL;
490
491 return srtp_err_status_ok;
492}
493
494#else /* if OPENSSL_KDF */
495
496/*
497 * srtp_kdf_t represents a key derivation function. The SRTP
498 * default KDF is the only one implemented at present.
499 */
500typedef struct {
501 srtp_cipher_t *cipher; /* cipher used for key derivation */
502} srtp_kdf_t;
503
504static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf, srtp_cipher_type_id_t cipher_id, const uint8_t *key, int length)
505{
506 srtp_err_status_t stat;
507 stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, length, 0);
508 if (stat) return stat;
509
510 stat = srtp_cipher_init(kdf->cipher, key);
511 if (stat) {
512 srtp_cipher_dealloc(kdf->cipher);
513 return stat;
514 }
515 return srtp_err_status_ok;
516}
517
518static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label, uint8_t *key, unsigned int length)
519{
520 srtp_err_status_t status;
521 v128_t nonce;
Cullen Jennings235513a2005-09-21 22:51:36 +0000522
jfigus038d2cf2015-05-11 14:10:11 -0400523 /* set eigth octet of nonce to <label>, set the rest of it to zero */
524 v128_set_to_zero(&nonce);
525 nonce.v8[7] = label;
Cullen Jennings235513a2005-09-21 22:51:36 +0000526
jfigus038d2cf2015-05-11 14:10:11 -0400527 status = srtp_cipher_set_iv(kdf->cipher, (const uint8_t*)&nonce, direction_encrypt);
528 if (status) return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000529
jfigus038d2cf2015-05-11 14:10:11 -0400530 /* generate keystream output */
531 octet_string_set_to_zero(key, length);
532 status = srtp_cipher_encrypt(kdf->cipher, key, &length);
533 if (status) return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000534
jfigus038d2cf2015-05-11 14:10:11 -0400535 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000536}
537
jfigus038d2cf2015-05-11 14:10:11 -0400538static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf) {
539 srtp_err_status_t status;
540 status = srtp_cipher_dealloc(kdf->cipher);
541 if (status) return status;
542 kdf->cipher = NULL;
543 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000544}
jfigus038d2cf2015-05-11 14:10:11 -0400545#endif /* else OPENSSL_KDF */
Cullen Jennings235513a2005-09-21 22:51:36 +0000546
547/*
548 * end of key derivation functions
549 */
550
Cullen Jennings235513a2005-09-21 22:51:36 +0000551
552
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000553/* Get the base key length corresponding to a given combined key+salt
554 * length for the given cipher.
555 * Assumption is that for AES-ICM a key length < 30 is Ismacryp using
556 * AES-128 and short salts; everything else uses a salt length of 14.
557 * TODO: key and salt lengths should be separate fields in the policy. */
jfigus9a840432014-11-19 15:48:21 -0500558static inline int base_key_length(const srtp_cipher_type_t *cipher, int key_length)
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000559{
jfigus8c36da22013-10-01 16:41:19 -0400560 switch (cipher->id) {
jfigus67b9c732014-11-20 10:17:21 -0500561 case SRTP_AES_128_ICM:
562 case SRTP_AES_192_ICM:
563 case SRTP_AES_256_ICM:
jfigus8c36da22013-10-01 16:41:19 -0400564 /* The legacy modes are derived from
565 * the configured key length on the policy */
566 return key_length - 14;
567 break;
jfigus67b9c732014-11-20 10:17:21 -0500568 case SRTP_AES_128_GCM:
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000569 return 16;
jfigus8c36da22013-10-01 16:41:19 -0400570 break;
jfigus67b9c732014-11-20 10:17:21 -0500571 case SRTP_AES_256_GCM:
jfigus8c36da22013-10-01 16:41:19 -0400572 return 32;
573 break;
574 default:
575 return key_length;
576 break;
577 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000578}
579
jfigus857009c2014-11-05 11:17:43 -0500580srtp_err_status_t
David McGrew576e1482006-06-09 21:47:44 +0000581srtp_stream_init_keys(srtp_stream_ctx_t *srtp, const void *key) {
jfigus857009c2014-11-05 11:17:43 -0500582 srtp_err_status_t stat;
David McGrew576e1482006-06-09 21:47:44 +0000583 srtp_kdf_t kdf;
584 uint8_t tmp_key[MAX_SRTP_KEY_LEN];
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000585 int kdf_keylen = 30, rtp_keylen, rtcp_keylen;
586 int rtp_base_key_len, rtp_salt_len;
587 int rtcp_base_key_len, rtcp_salt_len;
588
589 /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */
590 /* TODO: kdf algorithm, master key length, and master salt length should
591 * be part of srtp_policy_t. */
jfigus9a840432014-11-19 15:48:21 -0500592 rtp_keylen = srtp_cipher_get_key_length(srtp->rtp_cipher);
593 rtcp_keylen = srtp_cipher_get_key_length(srtp->rtcp_cipher);
jfigus8719f952014-04-08 09:15:49 -0400594 rtp_base_key_len = base_key_length(srtp->rtp_cipher->type, rtp_keylen);
595 rtp_salt_len = rtp_keylen - rtp_base_key_len;
596
597 if (rtp_keylen > kdf_keylen) {
598 kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
599 }
600
601 if (rtcp_keylen > kdf_keylen) {
602 kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
603 }
604
jfigus8c36da22013-10-01 16:41:19 -0400605 debug_print(mod_srtp, "srtp key len: %d", rtp_keylen);
606 debug_print(mod_srtp, "srtcp key len: %d", rtcp_keylen);
jfigus8719f952014-04-08 09:15:49 -0400607 debug_print(mod_srtp, "base key len: %d", rtp_base_key_len);
608 debug_print(mod_srtp, "kdf key len: %d", kdf_keylen);
609 debug_print(mod_srtp, "rtp salt len: %d", rtp_salt_len);
610
611 /*
612 * Make sure the key given to us is 'zero' appended. GCM
613 * mode uses a shorter master SALT (96 bits), but still relies on
614 * the legacy CTR mode KDF, which uses a 112 bit master SALT.
615 */
616 memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN);
617 memcpy(tmp_key, key, (rtp_base_key_len + rtp_salt_len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000618
David McGrew576e1482006-06-09 21:47:44 +0000619 /* initialize KDF state */
jfigus038d2cf2015-05-11 14:10:11 -0400620#if defined(OPENSSL) && defined(OPENSSL_KDF)
621 stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, rtp_base_key_len, rtp_salt_len);
622#else
jfigus67b9c732014-11-20 10:17:21 -0500623 stat = srtp_kdf_init(&kdf, SRTP_AES_ICM, (const uint8_t *)tmp_key, kdf_keylen);
jfigus038d2cf2015-05-11 14:10:11 -0400624#endif
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000625 if (stat) {
jfigus857009c2014-11-05 11:17:43 -0500626 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000627 }
David McGrew576e1482006-06-09 21:47:44 +0000628
629 /* generate encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000630 stat = srtp_kdf_generate(&kdf, label_rtp_encryption,
631 tmp_key, rtp_base_key_len);
632 if (stat) {
633 /* zeroize temp buffer */
634 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500635 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000636 }
jfigus8719f952014-04-08 09:15:49 -0400637 debug_print(mod_srtp, "cipher key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500638 srtp_octet_string_hex_string(tmp_key, rtp_base_key_len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000639
David McGrew576e1482006-06-09 21:47:44 +0000640 /*
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000641 * if the cipher in the srtp context uses a salt, then we need
David McGrew576e1482006-06-09 21:47:44 +0000642 * to generate the salt value
643 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000644 if (rtp_salt_len > 0) {
645 debug_print(mod_srtp, "found rtp_salt_len > 0, generating salt", NULL);
David McGrew576e1482006-06-09 21:47:44 +0000646
647 /* generate encryption salt, put after encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000648 stat = srtp_kdf_generate(&kdf, label_rtp_salt,
649 tmp_key + rtp_base_key_len, rtp_salt_len);
650 if (stat) {
651 /* zeroize temp buffer */
652 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500653 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000654 }
jfigus8c36da22013-10-01 16:41:19 -0400655 memcpy(srtp->salt, tmp_key + rtp_base_key_len, SRTP_AEAD_SALT_LEN);
David McGrew576e1482006-06-09 21:47:44 +0000656 }
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000657 if (rtp_salt_len > 0) {
658 debug_print(mod_srtp, "cipher salt: %s",
jfigus46d6b472014-11-14 16:42:01 -0500659 srtp_octet_string_hex_string(tmp_key + rtp_base_key_len, rtp_salt_len));
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000660 }
David McGrew576e1482006-06-09 21:47:44 +0000661
662 /* initialize cipher */
jfigus3f93c3c2014-12-01 15:38:09 -0500663 stat = srtp_cipher_init(srtp->rtp_cipher, tmp_key);
David McGrew576e1482006-06-09 21:47:44 +0000664 if (stat) {
665 /* zeroize temp buffer */
666 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500667 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000668 }
669
670 /* generate authentication key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000671 stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth,
jfigus8f669722014-11-19 15:20:03 -0500672 tmp_key, srtp_auth_get_key_length(srtp->rtp_auth));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000673 if (stat) {
674 /* zeroize temp buffer */
675 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500676 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000677 }
David McGrew576e1482006-06-09 21:47:44 +0000678 debug_print(mod_srtp, "auth key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500679 srtp_octet_string_hex_string(tmp_key,
jfigus8f669722014-11-19 15:20:03 -0500680 srtp_auth_get_key_length(srtp->rtp_auth)));
David McGrew576e1482006-06-09 21:47:44 +0000681
682 /* initialize auth function */
683 stat = auth_init(srtp->rtp_auth, tmp_key);
684 if (stat) {
685 /* zeroize temp buffer */
686 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500687 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000688 }
689
690 /*
691 * ...now initialize SRTCP keys
692 */
693
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000694 rtcp_base_key_len = base_key_length(srtp->rtcp_cipher->type, rtcp_keylen);
695 rtcp_salt_len = rtcp_keylen - rtcp_base_key_len;
jfigus8c36da22013-10-01 16:41:19 -0400696 debug_print(mod_srtp, "rtcp salt len: %d", rtcp_salt_len);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000697
David McGrew576e1482006-06-09 21:47:44 +0000698 /* generate encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000699 stat = srtp_kdf_generate(&kdf, label_rtcp_encryption,
700 tmp_key, rtcp_base_key_len);
701 if (stat) {
702 /* zeroize temp buffer */
703 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500704 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000705 }
706
David McGrew576e1482006-06-09 21:47:44 +0000707 /*
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000708 * if the cipher in the srtp context uses a salt, then we need
David McGrew576e1482006-06-09 21:47:44 +0000709 * to generate the salt value
710 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000711 if (rtcp_salt_len > 0) {
712 debug_print(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt",
713 NULL);
David McGrew576e1482006-06-09 21:47:44 +0000714
715 /* generate encryption salt, put after encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000716 stat = srtp_kdf_generate(&kdf, label_rtcp_salt,
717 tmp_key + rtcp_base_key_len, rtcp_salt_len);
718 if (stat) {
719 /* zeroize temp buffer */
720 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500721 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000722 }
jfigus8c36da22013-10-01 16:41:19 -0400723 memcpy(srtp->c_salt, tmp_key + rtcp_base_key_len, SRTP_AEAD_SALT_LEN);
David McGrew576e1482006-06-09 21:47:44 +0000724 }
725 debug_print(mod_srtp, "rtcp cipher key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500726 srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len));
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000727 if (rtcp_salt_len > 0) {
728 debug_print(mod_srtp, "rtcp cipher salt: %s",
jfigus46d6b472014-11-14 16:42:01 -0500729 srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len, rtcp_salt_len));
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000730 }
David McGrew576e1482006-06-09 21:47:44 +0000731
732 /* initialize cipher */
jfigus3f93c3c2014-12-01 15:38:09 -0500733 stat = srtp_cipher_init(srtp->rtcp_cipher, tmp_key);
David McGrew576e1482006-06-09 21:47:44 +0000734 if (stat) {
735 /* zeroize temp buffer */
736 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500737 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000738 }
739
740 /* generate authentication key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000741 stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth,
jfigus8f669722014-11-19 15:20:03 -0500742 tmp_key, srtp_auth_get_key_length(srtp->rtcp_auth));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000743 if (stat) {
744 /* zeroize temp buffer */
745 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500746 return srtp_err_status_init_fail;
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000747 }
748
David McGrew576e1482006-06-09 21:47:44 +0000749 debug_print(mod_srtp, "rtcp auth key: %s",
jfigus46d6b472014-11-14 16:42:01 -0500750 srtp_octet_string_hex_string(tmp_key,
jfigus8f669722014-11-19 15:20:03 -0500751 srtp_auth_get_key_length(srtp->rtcp_auth)));
David McGrew576e1482006-06-09 21:47:44 +0000752
753 /* initialize auth function */
754 stat = auth_init(srtp->rtcp_auth, tmp_key);
755 if (stat) {
756 /* zeroize temp buffer */
757 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
jfigus857009c2014-11-05 11:17:43 -0500758 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000759 }
760
761 /* clear memory then return */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000762 stat = srtp_kdf_clear(&kdf);
David McGrew576e1482006-06-09 21:47:44 +0000763 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000764 if (stat)
jfigus857009c2014-11-05 11:17:43 -0500765 return srtp_err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000766
jfigus857009c2014-11-05 11:17:43 -0500767 return srtp_err_status_ok;
David McGrew576e1482006-06-09 21:47:44 +0000768}
Cullen Jennings235513a2005-09-21 22:51:36 +0000769
jfigus857009c2014-11-05 11:17:43 -0500770srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000771srtp_stream_init(srtp_stream_ctx_t *srtp,
772 const srtp_policy_t *p) {
jfigus857009c2014-11-05 11:17:43 -0500773 srtp_err_status_t err;
Cullen Jennings235513a2005-09-21 22:51:36 +0000774
775 debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)",
776 p->ssrc.value);
777
778 /* initialize replay database */
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000779 /* window size MUST be at least 64. MAY be larger. Values more than
780 * 2^15 aren't meaningful due to how extended sequence numbers are
781 * calculated. Let a window size of 0 imply the default value. */
782
783 if (p->window_size != 0 && (p->window_size < 64 || p->window_size >= 0x8000))
jfigus857009c2014-11-05 11:17:43 -0500784 return srtp_err_status_bad_param;
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000785
786 if (p->window_size != 0)
jfigusde8deb32014-11-25 12:58:11 -0500787 err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000788 else
jfigusde8deb32014-11-25 12:58:11 -0500789 err = srtp_rdbx_init(&srtp->rtp_rdbx, 128);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000790 if (err) return err;
Cullen Jennings235513a2005-09-21 22:51:36 +0000791
792 /* initialize key limit to maximum value */
Marcus Sundberge4e34f92005-10-02 20:19:35 +0000793#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +0000794{
Marcus Sundberge4e34f92005-10-02 20:19:35 +0000795 uint64_t temp;
796 temp = make64(UINT_MAX,UINT_MAX);
jfigusc7cdc9a2014-11-19 16:19:08 -0500797 srtp_key_limit_set(srtp->limit, temp);
Cullen Jennings235513a2005-09-21 22:51:36 +0000798}
799#else
jfigusc7cdc9a2014-11-19 16:19:08 -0500800 srtp_key_limit_set(srtp->limit, 0xffffffffffffLL);
Cullen Jennings235513a2005-09-21 22:51:36 +0000801#endif
802
803 /* set the SSRC value */
804 srtp->ssrc = htonl(p->ssrc.value);
805
806 /* set the security service flags */
807 srtp->rtp_services = p->rtp.sec_serv;
808 srtp->rtcp_services = p->rtcp.sec_serv;
809
810 /*
811 * set direction to unknown - this flag gets checked in srtp_protect(),
812 * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and
813 * gets set appropriately if it is set to unknown.
814 */
815 srtp->direction = dir_unknown;
816
David McGrew576e1482006-06-09 21:47:44 +0000817 /* initialize SRTCP replay database */
jfigusde8deb32014-11-25 12:58:11 -0500818 srtp_rdb_init(&srtp->rtcp_rdb);
Cullen Jennings235513a2005-09-21 22:51:36 +0000819
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000820 /* initialize allow_repeat_tx */
821 /* guard against uninitialized memory: allow only 0 or 1 here */
822 if (p->allow_repeat_tx != 0 && p->allow_repeat_tx != 1) {
jfigusde8deb32014-11-25 12:58:11 -0500823 srtp_rdbx_dealloc(&srtp->rtp_rdbx);
jfigus857009c2014-11-05 11:17:43 -0500824 return srtp_err_status_bad_param;
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000825 }
826 srtp->allow_repeat_tx = p->allow_repeat_tx;
827
Cullen Jennings235513a2005-09-21 22:51:36 +0000828 /* DAM - no RTCP key limit at present */
829
David McGrew576e1482006-06-09 21:47:44 +0000830 /* initialize keys */
831 err = srtp_stream_init_keys(srtp, p->key);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000832 if (err) {
jfigusde8deb32014-11-25 12:58:11 -0500833 srtp_rdbx_dealloc(&srtp->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000834 return err;
835 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000836
David McGrew79870d62007-06-15 18:17:39 +0000837 /*
838 * if EKT is in use, then initialize the EKT data associated with
839 * the stream
840 */
jfigusc5887e72014-11-06 09:46:18 -0500841 err = srtp_ekt_stream_init_from_policy(srtp->ekt, p->ekt);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000842 if (err) {
jfigusde8deb32014-11-25 12:58:11 -0500843 srtp_rdbx_dealloc(&srtp->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000844 return err;
845 }
David McGrew79870d62007-06-15 18:17:39 +0000846
jfigus857009c2014-11-05 11:17:43 -0500847 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000848 }
849
850
851 /*
852 * srtp_event_reporter is an event handler function that merely
853 * reports the events that are reported by the callbacks
854 */
855
856 void
857 srtp_event_reporter(srtp_event_data_t *data) {
858
jfigus02d6f032014-11-21 10:56:42 -0500859 srtp_err_report(srtp_err_level_warning, "srtp: in stream 0x%x: ",
Cullen Jennings235513a2005-09-21 22:51:36 +0000860 data->stream->ssrc);
861
862 switch(data->event) {
863 case event_ssrc_collision:
jfigus02d6f032014-11-21 10:56:42 -0500864 srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000865 break;
866 case event_key_soft_limit:
jfigus02d6f032014-11-21 10:56:42 -0500867 srtp_err_report(srtp_err_level_warning, "\tkey usage soft limit reached\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000868 break;
869 case event_key_hard_limit:
jfigus02d6f032014-11-21 10:56:42 -0500870 srtp_err_report(srtp_err_level_warning, "\tkey usage hard limit reached\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000871 break;
872 case event_packet_index_limit:
jfigus02d6f032014-11-21 10:56:42 -0500873 srtp_err_report(srtp_err_level_warning, "\tpacket index limit reached\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000874 break;
875 default:
jfigus02d6f032014-11-21 10:56:42 -0500876 srtp_err_report(srtp_err_level_warning, "\tunknown event reported to handler\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000877 }
878 }
879
880 /*
881 * srtp_event_handler is a global variable holding a pointer to the
882 * event handler function; this function is called for any unexpected
883 * event that needs to be handled out of the SRTP data path. see
884 * srtp_event_t in srtp.h for more info
885 *
886 * it is okay to set srtp_event_handler to NULL, but we set
887 * it to the srtp_event_reporter.
888 */
889
890 static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter;
891
jfigus857009c2014-11-05 11:17:43 -0500892 srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000893 srtp_install_event_handler(srtp_event_handler_func_t func) {
894
895 /*
896 * note that we accept NULL arguments intentionally - calling this
897 * function with a NULL arguments removes an event handler that's
898 * been previously installed
899 */
900
901 /* set global event handling function */
902 srtp_event_handler = func;
jfigus857009c2014-11-05 11:17:43 -0500903 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000904 }
905
jfigus8c36da22013-10-01 16:41:19 -0400906/*
907 * AEAD uses a new IV formation method. This function implements
908 * section 9.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The
909 * calculation is defined as, where (+) is the xor operation:
910 *
911 *
912 * 0 0 0 0 0 0 0 0 0 0 1 1
913 * 0 1 2 3 4 5 6 7 8 9 0 1
914 * +--+--+--+--+--+--+--+--+--+--+--+--+
915 * |00|00| SSRC | ROC | SEQ |---+
916 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
917 * |
918 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
919 * | Encryption Salt |->(+)
920 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
921 * |
922 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
923 * | Initialization Vector |<--+
924 * +--+--+--+--+--+--+--+--+--+--+--+--+*
925 *
926 * Input: *stream - pointer to SRTP stream context, used to retrieve
927 * the SALT
928 * *iv - Pointer to receive the calculated IV
929 * *seq - The ROC and SEQ value to use for the
930 * IV calculation.
931 * *hdr - The RTP header, used to get the SSRC value
932 *
933 */
934static void srtp_calc_aead_iv(srtp_stream_ctx_t *stream, v128_t *iv,
jfigusde8deb32014-11-25 12:58:11 -0500935 srtp_xtd_seq_num_t *seq, srtp_hdr_t *hdr)
jfigus8c36da22013-10-01 16:41:19 -0400936{
937 v128_t in;
938 v128_t salt;
Dmitry Sobinov367d5d32014-03-27 22:36:32 +0700939
940#ifdef NO_64BIT_MATH
941 uint32_t local_roc = ((high32(*seq) << 16) |
942 (low32(*seq) >> 16));
943 uint16_t local_seq = (uint16_t) (low32(*seq));
944#else
945 uint32_t local_roc = (uint32_t)(*seq >> 16);
946 uint16_t local_seq = (uint16_t) *seq;
947#endif
jfigus8c36da22013-10-01 16:41:19 -0400948
949 memset(&in, 0, sizeof(v128_t));
950 memset(&salt, 0, sizeof(v128_t));
951
Dmitry Sobinov367d5d32014-03-27 22:36:32 +0700952 in.v16[5] = htons(local_seq);
953 local_roc = htonl(local_roc);
954 memcpy(&in.v16[3], &local_roc, sizeof(local_roc));
jfigus8c36da22013-10-01 16:41:19 -0400955
956 /*
957 * Copy in the RTP SSRC value
958 */
959 memcpy(&in.v8[2], &hdr->ssrc, 4);
960 debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in));
961
962 /*
963 * Get the SALT value from the context
964 */
965 memcpy(salt.v8, stream->salt, SRTP_AEAD_SALT_LEN);
966 debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt));
967
968 /*
969 * Finally, apply tyhe SALT to the input
970 */
971 v128_xor(iv, &in, &salt);
972}
973
974
975/*
976 * This function handles outgoing SRTP packets while in AEAD mode,
977 * which currently supports AES-GCM encryption. All packets are
978 * encrypted and authenticated.
979 */
jfigus857009c2014-11-05 11:17:43 -0500980static srtp_err_status_t
jfigus8c36da22013-10-01 16:41:19 -0400981srtp_protect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream,
Travis Cross31844002014-07-02 16:18:57 +0000982 void *rtp_hdr, unsigned int *pkt_octet_len)
jfigus8c36da22013-10-01 16:41:19 -0400983{
984 srtp_hdr_t *hdr = (srtp_hdr_t*)rtp_hdr;
985 uint32_t *enc_start; /* pointer to start of encrypted portion */
Travis Cross1b8b1e72014-07-02 15:32:36 +0000986 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigusde8deb32014-11-25 12:58:11 -0500987 srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
jfigus8c36da22013-10-01 16:41:19 -0400988 int delta; /* delta of local pkt idx and that in hdr */
jfigus857009c2014-11-05 11:17:43 -0500989 srtp_err_status_t status;
jfigus226ec562014-12-02 15:11:03 -0500990 uint32_t tag_len;
jfigus8c36da22013-10-01 16:41:19 -0400991 v128_t iv;
992 unsigned int aad_len;
993
994 debug_print(mod_srtp, "function srtp_protect_aead", NULL);
995
996 /*
997 * update the key usage limit, and check it to make sure that we
998 * didn't just hit either the soft limit or the hard limit, and call
999 * the event handler if we hit either.
1000 */
jfigusc7cdc9a2014-11-19 16:19:08 -05001001 switch (srtp_key_limit_update(stream->limit)) {
1002 case srtp_key_event_normal:
jfigus8c36da22013-10-01 16:41:19 -04001003 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001004 case srtp_key_event_hard_limit:
jfigus8c36da22013-10-01 16:41:19 -04001005 srtp_handle_event(ctx, stream, event_key_hard_limit);
jfigus857009c2014-11-05 11:17:43 -05001006 return srtp_err_status_key_expired;
jfigusc7cdc9a2014-11-19 16:19:08 -05001007 case srtp_key_event_soft_limit:
jfigus8c36da22013-10-01 16:41:19 -04001008 default:
1009 srtp_handle_event(ctx, stream, event_key_soft_limit);
1010 break;
1011 }
1012
1013 /* get tag length from stream */
jfigus8f669722014-11-19 15:20:03 -05001014 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
jfigus8c36da22013-10-01 16:41:19 -04001015
1016 /*
1017 * find starting point for encryption and length of data to be
1018 * encrypted - the encrypted portion starts after the rtp header
1019 * extension, if present; otherwise, it starts after the last csrc,
1020 * if any are present
jfigus8c36da22013-10-01 16:41:19 -04001021 */
Travis Cross3600c272014-06-29 17:32:33 +00001022 enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc;
1023 if (hdr->x == 1) {
1024 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t*)enc_start;
1025 enc_start += (ntohs(xtn_hdr->length) + 1);
1026 }
Travis Cross83439f72014-07-02 14:18:46 +00001027 if (!((uint8_t*)enc_start < (uint8_t*)hdr + *pkt_octet_len))
jfigus857009c2014-11-05 11:17:43 -05001028 return srtp_err_status_parse_err;
Travis Cross3600c272014-06-29 17:32:33 +00001029 enc_octet_len = (unsigned int)(*pkt_octet_len -
Travis Crossdee3ee82014-07-02 15:20:12 +00001030 ((uint8_t*)enc_start - (uint8_t*)hdr));
jfigus8c36da22013-10-01 16:41:19 -04001031
1032 /*
1033 * estimate the packet index using the start of the replay window
1034 * and the sequence number from the header
1035 */
jfigusde8deb32014-11-25 12:58:11 -05001036 delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
1037 status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
jfigus8c36da22013-10-01 16:41:19 -04001038 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001039 if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) {
jfigus8c36da22013-10-01 16:41:19 -04001040 return status; /* we've been asked to reuse an index */
1041 }
1042 } else {
jfigusde8deb32014-11-25 12:58:11 -05001043 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
jfigus8c36da22013-10-01 16:41:19 -04001044 }
1045
1046#ifdef NO_64BIT_MATH
1047 debug_print2(mod_srtp, "estimated packet index: %08x%08x",
1048 high32(est), low32(est));
1049#else
1050 debug_print(mod_srtp, "estimated packet index: %016llx", est);
1051#endif
1052
1053 /*
1054 * AEAD uses a new IV formation method
1055 */
1056 srtp_calc_aead_iv(stream, &iv, &est, hdr);
jfigus0d99b712014-12-01 16:08:19 -05001057 status = srtp_cipher_set_iv(stream->rtp_cipher, (const uint8_t*)&iv, direction_encrypt);
jfigus8c36da22013-10-01 16:41:19 -04001058 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001059 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04001060 }
1061
1062 /* shift est, put into network byte order */
1063#ifdef NO_64BIT_MATH
1064 est = be64_to_cpu(make64((high32(est) << 16) |
1065 (low32(est) >> 16),
1066 low32(est) << 16));
1067#else
1068 est = be64_to_cpu(est << 16);
1069#endif
1070
1071 /*
1072 * Set the AAD over the RTP header
1073 */
1074 aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
jfigus226ec562014-12-02 15:11:03 -05001075 status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
jfigus8c36da22013-10-01 16:41:19 -04001076 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001077 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04001078 }
1079
1080 /* Encrypt the payload */
jfigus2964a152014-11-25 15:25:37 -05001081 status = srtp_cipher_encrypt(stream->rtp_cipher,
jfigus8c36da22013-10-01 16:41:19 -04001082 (uint8_t*)enc_start, &enc_octet_len);
1083 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001084 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04001085 }
1086 /*
1087 * If we're doing GCM, we need to get the tag
1088 * and append that to the output
1089 */
jfigus226ec562014-12-02 15:11:03 -05001090 status = srtp_cipher_get_tag(stream->rtp_cipher,
jfigus8c36da22013-10-01 16:41:19 -04001091 (uint8_t*)enc_start+enc_octet_len, &tag_len);
1092 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001093 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04001094 }
1095 enc_octet_len += tag_len;
1096
1097 /* increase the packet length by the length of the auth tag */
1098 *pkt_octet_len += tag_len;
1099
jfigus857009c2014-11-05 11:17:43 -05001100 return srtp_err_status_ok;
jfigus8c36da22013-10-01 16:41:19 -04001101}
1102
1103
1104/*
1105 * This function handles incoming SRTP packets while in AEAD mode,
1106 * which currently supports AES-GCM encryption. All packets are
1107 * encrypted and authenticated. Note, the auth tag is at the end
1108 * of the packet stream and is automatically checked by GCM
1109 * when decrypting the payload.
1110 */
jfigus857009c2014-11-05 11:17:43 -05001111static srtp_err_status_t
jfigus8c36da22013-10-01 16:41:19 -04001112srtp_unprotect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, int delta,
jfigusde8deb32014-11-25 12:58:11 -05001113 srtp_xtd_seq_num_t est, void *srtp_hdr, unsigned int *pkt_octet_len)
jfigus8c36da22013-10-01 16:41:19 -04001114{
1115 srtp_hdr_t *hdr = (srtp_hdr_t*)srtp_hdr;
1116 uint32_t *enc_start; /* pointer to start of encrypted portion */
Travis Cross1b8b1e72014-07-02 15:32:36 +00001117 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigus8c36da22013-10-01 16:41:19 -04001118 v128_t iv;
jfigus857009c2014-11-05 11:17:43 -05001119 srtp_err_status_t status;
jfigus8c36da22013-10-01 16:41:19 -04001120 int tag_len;
1121 unsigned int aad_len;
1122
1123 debug_print(mod_srtp, "function srtp_unprotect_aead", NULL);
1124
1125#ifdef NO_64BIT_MATH
1126 debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est), low32(est));
1127#else
1128 debug_print(mod_srtp, "estimated u_packet index: %016llx", est);
1129#endif
1130
1131 /* get tag length from stream */
jfigus8f669722014-11-19 15:20:03 -05001132 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
jfigus8c36da22013-10-01 16:41:19 -04001133
1134 /*
1135 * AEAD uses a new IV formation method
1136 */
1137 srtp_calc_aead_iv(stream, &iv, &est, hdr);
jfigus0d99b712014-12-01 16:08:19 -05001138 status = srtp_cipher_set_iv(stream->rtp_cipher, (const uint8_t*)&iv, direction_decrypt);
jfigus8c36da22013-10-01 16:41:19 -04001139 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001140 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04001141 }
1142
1143 /*
1144 * find starting point for decryption and length of data to be
1145 * decrypted - the encrypted portion starts after the rtp header
1146 * extension, if present; otherwise, it starts after the last csrc,
1147 * if any are present
1148 */
1149 enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc;
1150 if (hdr->x == 1) {
1151 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t*)enc_start;
1152 enc_start += (ntohs(xtn_hdr->length) + 1);
1153 }
Travis Cross83439f72014-07-02 14:18:46 +00001154 if (!((uint8_t*)enc_start < (uint8_t*)hdr + *pkt_octet_len))
jfigus857009c2014-11-05 11:17:43 -05001155 return srtp_err_status_parse_err;
jfigus8c36da22013-10-01 16:41:19 -04001156 /*
1157 * We pass the tag down to the cipher when doing GCM mode
1158 */
Travis Cross7d4c1032014-07-02 14:46:53 +00001159 enc_octet_len = (unsigned int)(*pkt_octet_len -
Travis Crossdee3ee82014-07-02 15:20:12 +00001160 ((uint8_t*)enc_start - (uint8_t*)hdr));
jfigus8c36da22013-10-01 16:41:19 -04001161
1162 /*
jfigusc13c1002014-05-08 13:34:53 -04001163 * Sanity check the encrypted payload length against
1164 * the tag size. It must always be at least as large
1165 * as the tag length.
1166 */
Joachim Bauch25a0e6c2015-03-27 16:59:06 +01001167 if (enc_octet_len < (unsigned int) tag_len) {
jfigus857009c2014-11-05 11:17:43 -05001168 return srtp_err_status_cipher_fail;
jfigusc13c1002014-05-08 13:34:53 -04001169 }
1170
1171 /*
jfigus8c36da22013-10-01 16:41:19 -04001172 * update the key usage limit, and check it to make sure that we
1173 * didn't just hit either the soft limit or the hard limit, and call
1174 * the event handler if we hit either.
1175 */
jfigusc7cdc9a2014-11-19 16:19:08 -05001176 switch (srtp_key_limit_update(stream->limit)) {
1177 case srtp_key_event_normal:
jfigus8c36da22013-10-01 16:41:19 -04001178 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001179 case srtp_key_event_soft_limit:
jfigus8c36da22013-10-01 16:41:19 -04001180 srtp_handle_event(ctx, stream, event_key_soft_limit);
1181 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001182 case srtp_key_event_hard_limit:
jfigus8c36da22013-10-01 16:41:19 -04001183 srtp_handle_event(ctx, stream, event_key_hard_limit);
jfigus857009c2014-11-05 11:17:43 -05001184 return srtp_err_status_key_expired;
jfigus8c36da22013-10-01 16:41:19 -04001185 default:
1186 break;
1187 }
1188
1189 /*
1190 * Set the AAD for AES-GCM, which is the RTP header
1191 */
1192 aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
jfigus226ec562014-12-02 15:11:03 -05001193 status = srtp_cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
jfigus8c36da22013-10-01 16:41:19 -04001194 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001195 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04001196 }
1197
1198 /* Decrypt the ciphertext. This also checks the auth tag based
1199 * on the AAD we just specified above */
jfigus2964a152014-11-25 15:25:37 -05001200 status = srtp_cipher_decrypt(stream->rtp_cipher, (uint8_t*)enc_start, &enc_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04001201 if (status) {
1202 return status;
1203 }
1204
1205 /*
1206 * verify that stream is for received traffic - this check will
1207 * detect SSRC collisions, since a stream that appears in both
1208 * srtp_protect() and srtp_unprotect() will fail this test in one of
1209 * those functions.
1210 *
1211 * we do this check *after* the authentication check, so that the
1212 * latter check will catch any attempts to fool us into thinking
1213 * that we've got a collision
1214 */
1215 if (stream->direction != dir_srtp_receiver) {
1216 if (stream->direction == dir_unknown) {
1217 stream->direction = dir_srtp_receiver;
1218 } else {
1219 srtp_handle_event(ctx, stream, event_ssrc_collision);
1220 }
1221 }
1222
1223 /*
1224 * if the stream is a 'provisional' one, in which the template context
1225 * is used, then we need to allocate a new stream at this point, since
1226 * the authentication passed
1227 */
1228 if (stream == ctx->stream_template) {
1229 srtp_stream_ctx_t *new_stream;
1230
1231 /*
1232 * allocate and initialize a new stream
1233 *
1234 * note that we indicate failure if we can't allocate the new
1235 * stream, and some implementations will want to not return
1236 * failure here
1237 */
1238 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
1239 if (status) {
1240 return status;
1241 }
1242
1243 /* add new stream to the head of the stream_list */
1244 new_stream->next = ctx->stream_list;
1245 ctx->stream_list = new_stream;
1246
1247 /* set stream (the pointer used in this function) */
1248 stream = new_stream;
1249 }
1250
1251 /*
1252 * the message authentication function passed, so add the packet
1253 * index into the replay database
1254 */
jfigusde8deb32014-11-25 12:58:11 -05001255 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
jfigus8c36da22013-10-01 16:41:19 -04001256
1257 /* decrease the packet length by the length of the auth tag */
1258 *pkt_octet_len -= tag_len;
1259
jfigus857009c2014-11-05 11:17:43 -05001260 return srtp_err_status_ok;
jfigus8c36da22013-10-01 16:41:19 -04001261}
1262
1263
1264
1265
jfigus857009c2014-11-05 11:17:43 -05001266 srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001267 srtp_protect(srtp_ctx_t *ctx, void *rtp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00001268 srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00001269 uint32_t *enc_start; /* pointer to start of encrypted portion */
1270 uint32_t *auth_start; /* pointer to start of auth. portion */
Travis Cross1b8b1e72014-07-02 15:32:36 +00001271 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigusde8deb32014-11-25 12:58:11 -05001272 srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
Cullen Jennings235513a2005-09-21 22:51:36 +00001273 int delta; /* delta of local pkt idx and that in hdr */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001274 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05001275 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001276 int tag_len;
1277 srtp_stream_ctx_t *stream;
jfigus2964a152014-11-25 15:25:37 -05001278 uint32_t prefix_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001279
1280 debug_print(mod_srtp, "function srtp_protect", NULL);
1281
1282 /* we assume the hdr is 32-bit aligned to start */
1283
Joachim Bauch557a7872015-02-19 01:36:48 +01001284 /* Verify RTP header */
1285 status = srtp_validate_rtp_header(rtp_hdr, pkt_octet_len);
1286 if (status)
1287 return status;
1288
Cullen Jennings235513a2005-09-21 22:51:36 +00001289 /* check the packet length - it must at least contain a full header */
1290 if (*pkt_octet_len < octets_in_rtp_header)
jfigus857009c2014-11-05 11:17:43 -05001291 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001292
1293 /*
1294 * look up ssrc in srtp_stream list, and process the packet with
1295 * the appropriate stream. if we haven't seen this stream before,
1296 * there's a template key for this srtp_session, and the cipher
1297 * supports key-sharing, then we assume that a new stream using
1298 * that key has just started up
1299 */
1300 stream = srtp_get_stream(ctx, hdr->ssrc);
1301 if (stream == NULL) {
1302 if (ctx->stream_template != NULL) {
1303 srtp_stream_ctx_t *new_stream;
1304
1305 /* allocate and initialize a new stream */
1306 status = srtp_stream_clone(ctx->stream_template,
David McGrewfec49dd2005-09-23 19:34:11 +00001307 hdr->ssrc, &new_stream);
Cullen Jennings235513a2005-09-21 22:51:36 +00001308 if (status)
1309 return status;
1310
1311 /* add new stream to the head of the stream_list */
1312 new_stream->next = ctx->stream_list;
1313 ctx->stream_list = new_stream;
1314
1315 /* set direction to outbound */
1316 new_stream->direction = dir_srtp_sender;
1317
1318 /* set stream (the pointer used in this function) */
1319 stream = new_stream;
1320 } else {
1321 /* no template stream, so we return an error */
jfigus857009c2014-11-05 11:17:43 -05001322 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00001323 }
1324 }
1325
1326 /*
1327 * verify that stream is for sending traffic - this check will
1328 * detect SSRC collisions, since a stream that appears in both
1329 * srtp_protect() and srtp_unprotect() will fail this test in one of
1330 * those functions.
1331 */
jfigus8c36da22013-10-01 16:41:19 -04001332 if (stream->direction != dir_srtp_sender) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001333 if (stream->direction == dir_unknown) {
1334 stream->direction = dir_srtp_sender;
1335 } else {
1336 srtp_handle_event(ctx, stream, event_ssrc_collision);
1337 }
jfigus8c36da22013-10-01 16:41:19 -04001338 }
1339
1340 /*
1341 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
1342 * the request to our AEAD handler.
1343 */
jfigus67b9c732014-11-20 10:17:21 -05001344 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
1345 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00001346 return srtp_protect_aead(ctx, stream, rtp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04001347 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001348
1349 /*
1350 * update the key usage limit, and check it to make sure that we
1351 * didn't just hit either the soft limit or the hard limit, and call
1352 * the event handler if we hit either.
1353 */
jfigusc7cdc9a2014-11-19 16:19:08 -05001354 switch(srtp_key_limit_update(stream->limit)) {
1355 case srtp_key_event_normal:
Cullen Jennings235513a2005-09-21 22:51:36 +00001356 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001357 case srtp_key_event_soft_limit:
Cullen Jennings235513a2005-09-21 22:51:36 +00001358 srtp_handle_event(ctx, stream, event_key_soft_limit);
1359 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001360 case srtp_key_event_hard_limit:
Cullen Jennings235513a2005-09-21 22:51:36 +00001361 srtp_handle_event(ctx, stream, event_key_hard_limit);
jfigus857009c2014-11-05 11:17:43 -05001362 return srtp_err_status_key_expired;
Cullen Jennings235513a2005-09-21 22:51:36 +00001363 default:
1364 break;
1365 }
1366
1367 /* get tag length from stream */
jfigus8f669722014-11-19 15:20:03 -05001368 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001369
1370 /*
1371 * find starting point for encryption and length of data to be
1372 * encrypted - the encrypted portion starts after the rtp header
1373 * extension, if present; otherwise, it starts after the last csrc,
1374 * if any are present
1375 *
1376 * if we're not providing confidentiality, set enc_start to NULL
1377 */
1378 if (stream->rtp_services & sec_serv_conf) {
1379 enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
David McGrew14829302005-10-10 18:53:19 +00001380 if (hdr->x == 1) {
1381 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
1382 enc_start += (ntohs(xtn_hdr->length) + 1);
Travis Cross83439f72014-07-02 14:18:46 +00001383 if (!((uint8_t*)enc_start < (uint8_t*)hdr + *pkt_octet_len))
jfigus857009c2014-11-05 11:17:43 -05001384 return srtp_err_status_parse_err;
David McGrew14829302005-10-10 18:53:19 +00001385 }
Travis Crossdee3ee82014-07-02 15:20:12 +00001386 enc_octet_len = (unsigned int)(*pkt_octet_len -
1387 ((uint8_t*)enc_start - (uint8_t*)hdr));
Cullen Jennings235513a2005-09-21 22:51:36 +00001388 } else {
1389 enc_start = NULL;
1390 }
1391
1392 /*
1393 * if we're providing authentication, set the auth_start and auth_tag
1394 * pointers to the proper locations; otherwise, set auth_start to NULL
1395 * to indicate that no authentication is needed
1396 */
1397 if (stream->rtp_services & sec_serv_auth) {
1398 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001399 auth_tag = (uint8_t *)hdr + *pkt_octet_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001400 } else {
1401 auth_start = NULL;
1402 auth_tag = NULL;
1403 }
1404
1405 /*
1406 * estimate the packet index using the start of the replay window
1407 * and the sequence number from the header
1408 */
jfigusde8deb32014-11-25 12:58:11 -05001409 delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
1410 status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +00001411 if (status) {
jfigus857009c2014-11-05 11:17:43 -05001412 if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx)
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +00001413 return status; /* we've been asked to reuse an index */
1414 }
1415 else
jfigusde8deb32014-11-25 12:58:11 -05001416 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
Cullen Jennings235513a2005-09-21 22:51:36 +00001417
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001418#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001419 debug_print2(mod_srtp, "estimated packet index: %08x%08x",
1420 high32(est),low32(est));
1421#else
1422 debug_print(mod_srtp, "estimated packet index: %016llx", est);
1423#endif
1424
1425 /*
1426 * if we're using rindael counter mode, set nonce and seq
1427 */
jfigus67b9c732014-11-20 10:17:21 -05001428 if (stream->rtp_cipher->type->id == SRTP_AES_ICM ||
1429 stream->rtp_cipher->type->id == SRTP_AES_256_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001430 v128_t iv;
1431
1432 iv.v32[0] = 0;
1433 iv.v32[1] = hdr->ssrc;
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001434#ifdef NO_64BIT_MATH
1435 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
David McGrewfec49dd2005-09-23 19:34:11 +00001436 low32(est) << 16));
Cullen Jennings235513a2005-09-21 22:51:36 +00001437#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001438 iv.v64[1] = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001439#endif
jfigus0d99b712014-12-01 16:08:19 -05001440 status = srtp_cipher_set_iv(stream->rtp_cipher, (const uint8_t*)&iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001441
1442 } else {
1443 v128_t iv;
1444
1445 /* otherwise, set the index to est */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001446#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001447 iv.v32[0] = 0;
1448 iv.v32[1] = 0;
1449#else
1450 iv.v64[0] = 0;
1451#endif
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001452 iv.v64[1] = be64_to_cpu(est);
jfigus0d99b712014-12-01 16:08:19 -05001453 status = srtp_cipher_set_iv(stream->rtp_cipher, (const uint8_t*)&iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001454 }
1455 if (status)
jfigus857009c2014-11-05 11:17:43 -05001456 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001457
1458 /* shift est, put into network byte order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001459#ifdef NO_64BIT_MATH
1460 est = be64_to_cpu(make64((high32(est) << 16) |
Cullen Jennings235513a2005-09-21 22:51:36 +00001461 (low32(est) >> 16),
Randell Jesup811e1442005-09-28 22:43:41 +00001462 low32(est) << 16));
Cullen Jennings235513a2005-09-21 22:51:36 +00001463#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001464 est = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001465#endif
1466
1467 /*
1468 * if we're authenticating using a universal hash, put the keystream
1469 * prefix into the authentication tag
1470 */
1471 if (auth_start) {
1472
jfigus8f669722014-11-19 15:20:03 -05001473 prefix_len = srtp_auth_get_prefix_length(stream->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001474 if (prefix_len) {
jfigus2964a152014-11-25 15:25:37 -05001475 status = srtp_cipher_output(stream->rtp_cipher, auth_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001476 if (status)
jfigus857009c2014-11-05 11:17:43 -05001477 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001478 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05001479 srtp_octet_string_hex_string(auth_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001480 }
1481 }
1482
1483 /* if we're encrypting, exor keystream into the message */
1484 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05001485 status = srtp_cipher_encrypt(stream->rtp_cipher,
1486 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001487 if (status)
jfigus857009c2014-11-05 11:17:43 -05001488 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001489 }
1490
1491 /*
1492 * if we're authenticating, run authentication function and put result
1493 * into the auth_tag
1494 */
1495 if (auth_start) {
1496
1497 /* initialize auth func context */
1498 status = auth_start(stream->rtp_auth);
1499 if (status) return status;
1500
1501 /* run auth func over packet */
1502 status = auth_update(stream->rtp_auth,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001503 (uint8_t *)auth_start, *pkt_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001504 if (status) return status;
1505
1506 /* run auth func over ROC, put result into auth_tag */
David McGrew89fb7ea2005-09-26 19:33:44 +00001507 debug_print(mod_srtp, "estimated packet index: %016llx", est);
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001508 status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, auth_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00001509 debug_print(mod_srtp, "srtp auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05001510 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001511 if (status)
jfigus857009c2014-11-05 11:17:43 -05001512 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001513
1514 }
1515
1516 if (auth_tag) {
1517
1518 /* increase the packet length by the length of the auth tag */
1519 *pkt_octet_len += tag_len;
1520 }
1521
jfigus857009c2014-11-05 11:17:43 -05001522 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001523}
1524
1525
jfigus857009c2014-11-05 11:17:43 -05001526srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001527srtp_unprotect(srtp_ctx_t *ctx, void *srtp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00001528 srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00001529 uint32_t *enc_start; /* pointer to start of encrypted portion */
1530 uint32_t *auth_start; /* pointer to start of auth. portion */
Travis Cross1b8b1e72014-07-02 15:32:36 +00001531 unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001532 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigusde8deb32014-11-25 12:58:11 -05001533 srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
Cullen Jennings235513a2005-09-21 22:51:36 +00001534 int delta; /* delta of local pkt idx and that in hdr */
1535 v128_t iv;
jfigus857009c2014-11-05 11:17:43 -05001536 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001537 srtp_stream_ctx_t *stream;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001538 uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
jfigus2964a152014-11-25 15:25:37 -05001539 uint32_t tag_len, prefix_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001540
1541 debug_print(mod_srtp, "function srtp_unprotect", NULL);
1542
1543 /* we assume the hdr is 32-bit aligned to start */
1544
Joachim Bauch557a7872015-02-19 01:36:48 +01001545 /* Verify RTP header */
1546 status = srtp_validate_rtp_header(srtp_hdr, pkt_octet_len);
1547 if (status)
1548 return status;
1549
Cullen Jennings235513a2005-09-21 22:51:36 +00001550 /* check the packet length - it must at least contain a full header */
1551 if (*pkt_octet_len < octets_in_rtp_header)
jfigus857009c2014-11-05 11:17:43 -05001552 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001553
1554 /*
1555 * look up ssrc in srtp_stream list, and process the packet with
1556 * the appropriate stream. if we haven't seen this stream before,
1557 * there's only one key for this srtp_session, and the cipher
1558 * supports key-sharing, then we assume that a new stream using
1559 * that key has just started up
1560 */
1561 stream = srtp_get_stream(ctx, hdr->ssrc);
1562 if (stream == NULL) {
1563 if (ctx->stream_template != NULL) {
1564 stream = ctx->stream_template;
1565 debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)",
1566 hdr->ssrc);
1567
1568 /*
1569 * set estimated packet index to sequence number from header,
1570 * and set delta equal to the same value
1571 */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001572#ifdef NO_64BIT_MATH
jfigusde8deb32014-11-25 12:58:11 -05001573 est = (srtp_xtd_seq_num_t) make64(0,ntohs(hdr->seq));
Cullen Jennings235513a2005-09-21 22:51:36 +00001574 delta = low32(est);
1575#else
jfigusde8deb32014-11-25 12:58:11 -05001576 est = (srtp_xtd_seq_num_t) ntohs(hdr->seq);
David McGrewc4fc00b2006-06-08 18:51:27 +00001577 delta = (int)est;
Cullen Jennings235513a2005-09-21 22:51:36 +00001578#endif
1579 } else {
1580
1581 /*
1582 * no stream corresponding to SSRC found, and we don't do
1583 * key-sharing, so return an error
1584 */
jfigus857009c2014-11-05 11:17:43 -05001585 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00001586 }
1587 } else {
1588
1589 /* estimate packet index from seq. num. in header */
jfigusde8deb32014-11-25 12:58:11 -05001590 delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001591
1592 /* check replay database */
jfigusde8deb32014-11-25 12:58:11 -05001593 status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001594 if (status)
1595 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001596 }
1597
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001598#ifdef NO_64BIT_MATH
David McGrewfec49dd2005-09-23 19:34:11 +00001599 debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est),low32(est));
1600#else
1601 debug_print(mod_srtp, "estimated u_packet index: %016llx", est);
1602#endif
Cullen Jennings235513a2005-09-21 22:51:36 +00001603
jfigus8c36da22013-10-01 16:41:19 -04001604 /*
1605 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
1606 * the request to our AEAD handler.
1607 */
jfigus67b9c732014-11-20 10:17:21 -05001608 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
1609 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00001610 return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04001611 }
1612
Cullen Jennings235513a2005-09-21 22:51:36 +00001613 /* get tag length from stream */
jfigus8f669722014-11-19 15:20:03 -05001614 tag_len = srtp_auth_get_tag_length(stream->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001615
1616 /*
1617 * set the cipher's IV properly, depending on whatever cipher we
1618 * happen to be using
1619 */
jfigus67b9c732014-11-20 10:17:21 -05001620 if (stream->rtp_cipher->type->id == SRTP_AES_ICM ||
1621 stream->rtp_cipher->type->id == SRTP_AES_256_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001622
1623 /* aes counter mode */
1624 iv.v32[0] = 0;
1625 iv.v32[1] = hdr->ssrc; /* still in network order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001626#ifdef NO_64BIT_MATH
1627 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
Cullen Jennings235513a2005-09-21 22:51:36 +00001628 low32(est) << 16));
1629#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001630 iv.v64[1] = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001631#endif
jfigus0d99b712014-12-01 16:08:19 -05001632 status = srtp_cipher_set_iv(stream->rtp_cipher, (const uint8_t*)&iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001633 } else {
1634
1635 /* no particular format - set the iv to the pakcet index */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001636#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001637 iv.v32[0] = 0;
1638 iv.v32[1] = 0;
1639#else
1640 iv.v64[0] = 0;
1641#endif
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001642 iv.v64[1] = be64_to_cpu(est);
jfigus0d99b712014-12-01 16:08:19 -05001643 status = srtp_cipher_set_iv(stream->rtp_cipher, (const uint8_t*)&iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001644 }
1645 if (status)
jfigus857009c2014-11-05 11:17:43 -05001646 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001647
1648 /* shift est, put into network byte order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001649#ifdef NO_64BIT_MATH
1650 est = be64_to_cpu(make64((high32(est) << 16) |
Cullen Jennings235513a2005-09-21 22:51:36 +00001651 (low32(est) >> 16),
1652 low32(est) << 16));
1653#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001654 est = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001655#endif
1656
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001657 /*
1658 * find starting point for decryption and length of data to be
1659 * decrypted - the encrypted portion starts after the rtp header
1660 * extension, if present; otherwise, it starts after the last csrc,
1661 * if any are present
1662 *
1663 * if we're not providing confidentiality, set enc_start to NULL
1664 */
1665 if (stream->rtp_services & sec_serv_conf) {
1666 enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
David McGrew14829302005-10-10 18:53:19 +00001667 if (hdr->x == 1) {
1668 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
1669 enc_start += (ntohs(xtn_hdr->length) + 1);
1670 }
Travis Cross83439f72014-07-02 14:18:46 +00001671 if (!((uint8_t*)enc_start < (uint8_t*)hdr + *pkt_octet_len))
jfigus857009c2014-11-05 11:17:43 -05001672 return srtp_err_status_parse_err;
Travis Crossdee3ee82014-07-02 15:20:12 +00001673 enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len -
1674 ((uint8_t*)enc_start - (uint8_t*)hdr));
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001675 } else {
1676 enc_start = NULL;
1677 }
1678
Cullen Jennings235513a2005-09-21 22:51:36 +00001679 /*
1680 * if we're providing authentication, set the auth_start and auth_tag
1681 * pointers to the proper locations; otherwise, set auth_start to NULL
1682 * to indicate that no authentication is needed
1683 */
1684 if (stream->rtp_services & sec_serv_auth) {
1685 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001686 auth_tag = (uint8_t *)hdr + *pkt_octet_len - tag_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001687 } else {
1688 auth_start = NULL;
1689 auth_tag = NULL;
1690 }
1691
1692 /*
1693 * if we expect message authentication, run the authentication
1694 * function and compare the result with the value of the auth_tag
1695 */
1696 if (auth_start) {
1697
1698 /*
1699 * if we're using a universal hash, then we need to compute the
1700 * keystream prefix for encrypting the universal hash output
1701 *
1702 * if the keystream prefix length is zero, then we know that
1703 * the authenticator isn't using a universal hash function
1704 */
1705 if (stream->rtp_auth->prefix_len != 0) {
1706
jfigus8f669722014-11-19 15:20:03 -05001707 prefix_len = srtp_auth_get_prefix_length(stream->rtp_auth);
jfigus2964a152014-11-25 15:25:37 -05001708 status = srtp_cipher_output(stream->rtp_cipher, tmp_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001709 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05001710 srtp_octet_string_hex_string(tmp_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001711 if (status)
jfigus857009c2014-11-05 11:17:43 -05001712 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001713 }
1714
1715 /* initialize auth func context */
1716 status = auth_start(stream->rtp_auth);
1717 if (status) return status;
David McGrewfec49dd2005-09-23 19:34:11 +00001718
Cullen Jennings235513a2005-09-21 22:51:36 +00001719 /* now compute auth function over packet */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001720 status = auth_update(stream->rtp_auth, (uint8_t *)auth_start,
Cullen Jennings235513a2005-09-21 22:51:36 +00001721 *pkt_octet_len - tag_len);
1722
1723 /* run auth func over ROC, then write tmp tag */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001724 status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, tmp_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00001725
1726 debug_print(mod_srtp, "computed auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05001727 srtp_octet_string_hex_string(tmp_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001728 debug_print(mod_srtp, "packet auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05001729 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001730 if (status)
jfigus857009c2014-11-05 11:17:43 -05001731 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001732
1733 if (octet_string_is_eq(tmp_tag, auth_tag, tag_len))
jfigus857009c2014-11-05 11:17:43 -05001734 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001735 }
1736
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00001737 /*
1738 * update the key usage limit, and check it to make sure that we
1739 * didn't just hit either the soft limit or the hard limit, and call
1740 * the event handler if we hit either.
1741 */
jfigusc7cdc9a2014-11-19 16:19:08 -05001742 switch(srtp_key_limit_update(stream->limit)) {
1743 case srtp_key_event_normal:
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00001744 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001745 case srtp_key_event_soft_limit:
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00001746 srtp_handle_event(ctx, stream, event_key_soft_limit);
1747 break;
jfigusc7cdc9a2014-11-19 16:19:08 -05001748 case srtp_key_event_hard_limit:
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00001749 srtp_handle_event(ctx, stream, event_key_hard_limit);
jfigus857009c2014-11-05 11:17:43 -05001750 return srtp_err_status_key_expired;
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00001751 default:
1752 break;
1753 }
1754
Jonathan Lennox23dc1e22010-06-01 18:19:04 +00001755 /* if we're decrypting, add keystream into ciphertext */
Cullen Jennings235513a2005-09-21 22:51:36 +00001756 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05001757 status = srtp_cipher_decrypt(stream->rtp_cipher, (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001758 if (status)
jfigus857009c2014-11-05 11:17:43 -05001759 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00001760 }
1761
1762 /*
1763 * verify that stream is for received traffic - this check will
1764 * detect SSRC collisions, since a stream that appears in both
1765 * srtp_protect() and srtp_unprotect() will fail this test in one of
1766 * those functions.
1767 *
1768 * we do this check *after* the authentication check, so that the
1769 * latter check will catch any attempts to fool us into thinking
1770 * that we've got a collision
1771 */
1772 if (stream->direction != dir_srtp_receiver) {
1773 if (stream->direction == dir_unknown) {
1774 stream->direction = dir_srtp_receiver;
1775 } else {
1776 srtp_handle_event(ctx, stream, event_ssrc_collision);
1777 }
1778 }
1779
1780 /*
1781 * if the stream is a 'provisional' one, in which the template context
1782 * is used, then we need to allocate a new stream at this point, since
1783 * the authentication passed
1784 */
1785 if (stream == ctx->stream_template) {
1786 srtp_stream_ctx_t *new_stream;
1787
1788 /*
1789 * allocate and initialize a new stream
1790 *
1791 * note that we indicate failure if we can't allocate the new
1792 * stream, and some implementations will want to not return
1793 * failure here
1794 */
1795 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
1796 if (status)
1797 return status;
1798
1799 /* add new stream to the head of the stream_list */
1800 new_stream->next = ctx->stream_list;
1801 ctx->stream_list = new_stream;
1802
1803 /* set stream (the pointer used in this function) */
1804 stream = new_stream;
1805 }
1806
1807 /*
1808 * the message authentication function passed, so add the packet
1809 * index into the replay database
1810 */
jfigusde8deb32014-11-25 12:58:11 -05001811 srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
Cullen Jennings235513a2005-09-21 22:51:36 +00001812
1813 /* decrease the packet length by the length of the auth tag */
1814 *pkt_octet_len -= tag_len;
1815
jfigus857009c2014-11-05 11:17:43 -05001816 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001817}
1818
jfigus857009c2014-11-05 11:17:43 -05001819srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001820srtp_init() {
jfigus857009c2014-11-05 11:17:43 -05001821 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001822
1823 /* initialize crypto kernel */
jfigus92736bc2014-11-21 10:30:54 -05001824 status = srtp_crypto_kernel_init();
Cullen Jennings235513a2005-09-21 22:51:36 +00001825 if (status)
1826 return status;
1827
1828 /* load srtp debug module into the kernel */
jfigus92736bc2014-11-21 10:30:54 -05001829 status = srtp_crypto_kernel_load_debug_module(&mod_srtp);
Cullen Jennings235513a2005-09-21 22:51:36 +00001830 if (status)
1831 return status;
1832
jfigus857009c2014-11-05 11:17:43 -05001833 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001834}
1835
jfigus857009c2014-11-05 11:17:43 -05001836srtp_err_status_t
Jonathan Lennox5ae76332010-05-15 04:48:59 +00001837srtp_shutdown() {
jfigus857009c2014-11-05 11:17:43 -05001838 srtp_err_status_t status;
Jonathan Lennox5ae76332010-05-15 04:48:59 +00001839
1840 /* shut down crypto kernel */
jfigus92736bc2014-11-21 10:30:54 -05001841 status = srtp_crypto_kernel_shutdown();
Jonathan Lennox5ae76332010-05-15 04:48:59 +00001842 if (status)
1843 return status;
1844
1845 /* shutting down crypto kernel frees the srtp debug module as well */
1846
jfigus857009c2014-11-05 11:17:43 -05001847 return srtp_err_status_ok;
Jonathan Lennox5ae76332010-05-15 04:48:59 +00001848}
1849
1850
Cullen Jennings235513a2005-09-21 22:51:36 +00001851/*
1852 * The following code is under consideration for removal. See
1853 * SRTP_MAX_TRAILER_LEN
1854 */
1855#if 0
1856
1857/*
1858 * srtp_get_trailer_length(&a) returns the number of octets that will
1859 * be added to an RTP packet by the SRTP processing. This value
1860 * is constant for a given srtp_stream_t (i.e. between initializations).
1861 */
1862
1863int
1864srtp_get_trailer_length(const srtp_stream_t s) {
jfigus8f669722014-11-19 15:20:03 -05001865 return srtp_auth_get_tag_length(s->rtp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00001866}
1867
1868#endif
1869
1870/*
1871 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
1872 * to ssrc, or NULL if no stream exists for that ssrc
1873 *
1874 * this is an internal function
1875 */
1876
1877srtp_stream_ctx_t *
1878srtp_get_stream(srtp_t srtp, uint32_t ssrc) {
1879 srtp_stream_ctx_t *stream;
1880
1881 /* walk down list until ssrc is found */
1882 stream = srtp->stream_list;
1883 while (stream != NULL) {
1884 if (stream->ssrc == ssrc)
1885 return stream;
1886 stream = stream->next;
1887 }
1888
1889 /* we haven't found our ssrc, so return a null */
1890 return NULL;
1891}
1892
jfigus857009c2014-11-05 11:17:43 -05001893srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001894srtp_dealloc(srtp_t session) {
1895 srtp_stream_ctx_t *stream;
jfigus857009c2014-11-05 11:17:43 -05001896 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001897
1898 /*
1899 * we take a conservative deallocation strategy - if we encounter an
1900 * error deallocating a stream, then we stop trying to deallocate
1901 * memory and just return an error
1902 */
1903
1904 /* walk list of streams, deallocating as we go */
1905 stream = session->stream_list;
1906 while (stream != NULL) {
1907 srtp_stream_t next = stream->next;
1908 status = srtp_stream_dealloc(session, stream);
1909 if (status)
1910 return status;
1911 stream = next;
1912 }
1913
1914 /* deallocate stream template, if there is one */
1915 if (session->stream_template != NULL) {
David McGrewfec49dd2005-09-23 19:34:11 +00001916 status = auth_dealloc(session->stream_template->rtcp_auth);
1917 if (status)
1918 return status;
jfigus3f93c3c2014-12-01 15:38:09 -05001919 status = srtp_cipher_dealloc(session->stream_template->rtcp_cipher);
David McGrewfec49dd2005-09-23 19:34:11 +00001920 if (status)
1921 return status;
jfigused755f52014-11-19 14:57:19 -05001922 srtp_crypto_free(session->stream_template->limit);
jfigus3f93c3c2014-12-01 15:38:09 -05001923 status = srtp_cipher_dealloc(session->stream_template->rtp_cipher);
Cullen Jennings235513a2005-09-21 22:51:36 +00001924 if (status)
1925 return status;
1926 status = auth_dealloc(session->stream_template->rtp_auth);
1927 if (status)
1928 return status;
jfigusde8deb32014-11-25 12:58:11 -05001929 status = srtp_rdbx_dealloc(&session->stream_template->rtp_rdbx);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +00001930 if (status)
1931 return status;
jfigused755f52014-11-19 14:57:19 -05001932 srtp_crypto_free(session->stream_template);
Cullen Jennings235513a2005-09-21 22:51:36 +00001933 }
1934
1935 /* deallocate session context */
jfigused755f52014-11-19 14:57:19 -05001936 srtp_crypto_free(session);
Cullen Jennings235513a2005-09-21 22:51:36 +00001937
jfigus857009c2014-11-05 11:17:43 -05001938 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001939}
1940
1941
jfigus857009c2014-11-05 11:17:43 -05001942srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00001943srtp_add_stream(srtp_t session,
1944 const srtp_policy_t *policy) {
jfigus857009c2014-11-05 11:17:43 -05001945 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001946 srtp_stream_t tmp;
1947
Marcus Sundberg67398e62005-10-05 12:39:51 +00001948 /* sanity check arguments */
1949 if ((session == NULL) || (policy == NULL) || (policy->key == NULL))
jfigus857009c2014-11-05 11:17:43 -05001950 return srtp_err_status_bad_param;
Marcus Sundberg67398e62005-10-05 12:39:51 +00001951
Cullen Jennings235513a2005-09-21 22:51:36 +00001952 /* allocate stream */
1953 status = srtp_stream_alloc(&tmp, policy);
1954 if (status) {
1955 return status;
1956 }
1957
1958 /* initialize stream */
1959 status = srtp_stream_init(tmp, policy);
1960 if (status) {
jfigused755f52014-11-19 14:57:19 -05001961 srtp_crypto_free(tmp);
Cullen Jennings235513a2005-09-21 22:51:36 +00001962 return status;
1963 }
1964
1965 /*
1966 * set the head of the stream list or the template to point to the
1967 * stream that we've just alloced and init'ed, depending on whether
1968 * or not it has a wildcard SSRC value or not
1969 *
1970 * if the template stream has already been set, then the policy is
1971 * inconsistent, so we return a bad_param error code
1972 */
1973 switch (policy->ssrc.type) {
1974 case (ssrc_any_outbound):
1975 if (session->stream_template) {
jfigus857009c2014-11-05 11:17:43 -05001976 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001977 }
1978 session->stream_template = tmp;
1979 session->stream_template->direction = dir_srtp_sender;
1980 break;
1981 case (ssrc_any_inbound):
1982 if (session->stream_template) {
jfigus857009c2014-11-05 11:17:43 -05001983 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001984 }
1985 session->stream_template = tmp;
1986 session->stream_template->direction = dir_srtp_receiver;
1987 break;
1988 case (ssrc_specific):
1989 tmp->next = session->stream_list;
1990 session->stream_list = tmp;
1991 break;
1992 case (ssrc_undefined):
1993 default:
jfigused755f52014-11-19 14:57:19 -05001994 srtp_crypto_free(tmp);
jfigus857009c2014-11-05 11:17:43 -05001995 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00001996 }
1997
jfigus857009c2014-11-05 11:17:43 -05001998 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001999}
2000
2001
jfigus857009c2014-11-05 11:17:43 -05002002srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002003srtp_create(srtp_t *session, /* handle for session */
2004 const srtp_policy_t *policy) { /* SRTP policy (list) */
jfigus857009c2014-11-05 11:17:43 -05002005 srtp_err_status_t stat;
Cullen Jennings235513a2005-09-21 22:51:36 +00002006 srtp_ctx_t *ctx;
2007
2008 /* sanity check arguments */
Marcus Sundberg67398e62005-10-05 12:39:51 +00002009 if (session == NULL)
jfigus857009c2014-11-05 11:17:43 -05002010 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002011
2012 /* allocate srtp context and set ctx_ptr */
jfigused755f52014-11-19 14:57:19 -05002013 ctx = (srtp_ctx_t *) srtp_crypto_alloc(sizeof(srtp_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +00002014 if (ctx == NULL)
jfigus857009c2014-11-05 11:17:43 -05002015 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002016 *session = ctx;
2017
2018 /*
2019 * loop over elements in the policy list, allocating and
2020 * initializing a stream for each element
2021 */
2022 ctx->stream_template = NULL;
2023 ctx->stream_list = NULL;
Iñaki Baz Castillo241fec32014-08-21 00:51:00 +02002024 ctx->user_data = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00002025 while (policy != NULL) {
2026
2027 stat = srtp_add_stream(ctx, policy);
2028 if (stat) {
Marcus Sundberg67398e62005-10-05 12:39:51 +00002029 /* clean up everything */
2030 srtp_dealloc(*session);
Joachim Bauch8301c362015-06-07 23:33:29 +02002031 *session = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00002032 return stat;
2033 }
2034
2035 /* set policy to next item in list */
2036 policy = policy->next;
2037 }
2038
jfigus857009c2014-11-05 11:17:43 -05002039 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002040}
2041
2042
jfigus857009c2014-11-05 11:17:43 -05002043srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002044srtp_remove_stream(srtp_t session, uint32_t ssrc) {
2045 srtp_stream_ctx_t *stream, *last_stream;
jfigus857009c2014-11-05 11:17:43 -05002046 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002047
2048 /* sanity check arguments */
2049 if (session == NULL)
jfigus857009c2014-11-05 11:17:43 -05002050 return srtp_err_status_bad_param;
Cullen Jennings235513a2005-09-21 22:51:36 +00002051
2052 /* find stream in list; complain if not found */
2053 last_stream = stream = session->stream_list;
2054 while ((stream != NULL) && (ssrc != stream->ssrc)) {
2055 last_stream = stream;
2056 stream = stream->next;
2057 }
2058 if (stream == NULL)
jfigus857009c2014-11-05 11:17:43 -05002059 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00002060
2061 /* remove stream from the list */
Jonathan Lennox20505b32010-05-27 19:22:25 +00002062 if (last_stream == stream)
2063 /* stream was first in list */
2064 session->stream_list = stream->next;
2065 else
2066 last_stream->next = stream->next;
Cullen Jennings235513a2005-09-21 22:51:36 +00002067
2068 /* deallocate the stream */
2069 status = srtp_stream_dealloc(session, stream);
2070 if (status)
2071 return status;
2072
jfigus857009c2014-11-05 11:17:43 -05002073 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002074}
2075
2076
2077/*
2078 * the default policy - provides a convenient way for callers to use
2079 * the default security policy
2080 *
2081 * this policy is that defined in the current SRTP internet draft.
2082 *
2083 */
2084
2085/*
2086 * NOTE: cipher_key_len is really key len (128 bits) plus salt len
2087 * (112 bits)
2088 */
2089/* There are hard-coded 16's for base_key_len in the key generation code */
2090
2091void
jfigus857009c2014-11-05 11:17:43 -05002092srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002093
jfigus67b9c732014-11-20 10:17:21 -05002094 p->cipher_type = SRTP_AES_ICM;
Cullen Jennings235513a2005-09-21 22:51:36 +00002095 p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */
jfigus67b9c732014-11-20 10:17:21 -05002096 p->auth_type = SRTP_HMAC_SHA1;
Cullen Jennings235513a2005-09-21 22:51:36 +00002097 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2098 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2099 p->sec_serv = sec_serv_conf_and_auth;
2100
2101}
2102
2103void
jfigus857009c2014-11-05 11:17:43 -05002104srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002105
jfigus67b9c732014-11-20 10:17:21 -05002106 p->cipher_type = SRTP_AES_ICM;
Cullen Jennings235513a2005-09-21 22:51:36 +00002107 p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */
jfigus67b9c732014-11-20 10:17:21 -05002108 p->auth_type = SRTP_HMAC_SHA1;
Cullen Jennings235513a2005-09-21 22:51:36 +00002109 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2110 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2111 p->sec_serv = sec_serv_conf_and_auth;
2112
2113}
2114
David McGrewa8546882006-01-12 17:56:02 +00002115void
jfigus857009c2014-11-05 11:17:43 -05002116srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p) {
David McGrewa8546882006-01-12 17:56:02 +00002117
2118 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002119 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002120 *
2121 * note that this crypto policy is intended for SRTP, but not SRTCP
2122 */
2123
jfigus67b9c732014-11-20 10:17:21 -05002124 p->cipher_type = SRTP_AES_ICM;
David McGrewa8546882006-01-12 17:56:02 +00002125 p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */
jfigus67b9c732014-11-20 10:17:21 -05002126 p->auth_type = SRTP_HMAC_SHA1;
David McGrewa8546882006-01-12 17:56:02 +00002127 p->auth_key_len = 20; /* 160 bit key */
2128 p->auth_tag_len = 4; /* 32 bit tag */
2129 p->sec_serv = sec_serv_conf_and_auth;
2130
2131}
2132
2133
2134void
jfigus857009c2014-11-05 11:17:43 -05002135srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p) {
David McGrewa8546882006-01-12 17:56:02 +00002136
2137 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002138 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002139 *
2140 * note that this crypto policy is intended for SRTP, but not SRTCP
2141 */
2142
jfigus67b9c732014-11-20 10:17:21 -05002143 p->cipher_type = SRTP_AES_ICM;
David McGrewa8546882006-01-12 17:56:02 +00002144 p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */
jfigus67b9c732014-11-20 10:17:21 -05002145 p->auth_type = SRTP_NULL_AUTH;
David McGrewa8546882006-01-12 17:56:02 +00002146 p->auth_key_len = 0;
2147 p->auth_tag_len = 0;
2148 p->sec_serv = sec_serv_conf;
2149
2150}
2151
2152
2153void
jfigus857009c2014-11-05 11:17:43 -05002154srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p) {
David McGrewa8546882006-01-12 17:56:02 +00002155
2156 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002157 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002158 */
2159
jfigus67b9c732014-11-20 10:17:21 -05002160 p->cipher_type = SRTP_NULL_CIPHER;
David McGrewa8546882006-01-12 17:56:02 +00002161 p->cipher_key_len = 0;
jfigus67b9c732014-11-20 10:17:21 -05002162 p->auth_type = SRTP_HMAC_SHA1;
David McGrewa8546882006-01-12 17:56:02 +00002163 p->auth_key_len = 20;
2164 p->auth_tag_len = 10;
2165 p->sec_serv = sec_serv_auth;
2166
2167}
2168
jfigus267956d2014-11-06 10:49:21 -05002169void
2170srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p) {
2171
2172 /*
2173 * Should only be used for testing
2174 */
2175
jfigus67b9c732014-11-20 10:17:21 -05002176 p->cipher_type = SRTP_NULL_CIPHER;
jfigus267956d2014-11-06 10:49:21 -05002177 p->cipher_key_len = 0;
jfigus67b9c732014-11-20 10:17:21 -05002178 p->auth_type = SRTP_NULL_AUTH;
jfigus267956d2014-11-06 10:49:21 -05002179 p->auth_key_len = 0;
2180 p->auth_tag_len = 0;
2181 p->sec_serv = sec_serv_none;
2182
2183}
2184
David McGrewa8546882006-01-12 17:56:02 +00002185
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002186void
jfigus857009c2014-11-05 11:17:43 -05002187srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p) {
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002188
2189 /*
2190 * corresponds to draft-ietf-avt-big-aes-03.txt
2191 */
2192
jfigus67b9c732014-11-20 10:17:21 -05002193 p->cipher_type = SRTP_AES_ICM;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002194 p->cipher_key_len = 46;
jfigus67b9c732014-11-20 10:17:21 -05002195 p->auth_type = SRTP_HMAC_SHA1;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002196 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2197 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2198 p->sec_serv = sec_serv_conf_and_auth;
2199}
2200
2201
2202void
jfigus857009c2014-11-05 11:17:43 -05002203srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p) {
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002204
2205 /*
2206 * corresponds to draft-ietf-avt-big-aes-03.txt
2207 *
2208 * note that this crypto policy is intended for SRTP, but not SRTCP
2209 */
2210
jfigus67b9c732014-11-20 10:17:21 -05002211 p->cipher_type = SRTP_AES_ICM;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002212 p->cipher_key_len = 46;
jfigus67b9c732014-11-20 10:17:21 -05002213 p->auth_type = SRTP_HMAC_SHA1;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002214 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2215 p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */
2216 p->sec_serv = sec_serv_conf_and_auth;
2217}
2218
jfigus8c36da22013-10-01 16:41:19 -04002219/*
2220 * AES-256 with no authentication.
2221 */
2222void
jfigus857009c2014-11-05 11:17:43 -05002223srtp_crypto_policy_set_aes_cm_256_null_auth (srtp_crypto_policy_t *p)
jfigus8c36da22013-10-01 16:41:19 -04002224{
jfigus67b9c732014-11-20 10:17:21 -05002225 p->cipher_type = SRTP_AES_ICM;
jfigus8c36da22013-10-01 16:41:19 -04002226 p->cipher_key_len = 46;
jfigus67b9c732014-11-20 10:17:21 -05002227 p->auth_type = SRTP_NULL_AUTH;
jfigus8c36da22013-10-01 16:41:19 -04002228 p->auth_key_len = 0;
2229 p->auth_tag_len = 0;
2230 p->sec_serv = sec_serv_conf;
2231}
2232
2233#ifdef OPENSSL
2234/*
2235 * AES-128 GCM mode with 8 octet auth tag.
2236 */
2237void
jfigus857009c2014-11-05 11:17:43 -05002238srtp_crypto_policy_set_aes_gcm_128_8_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002239 p->cipher_type = SRTP_AES_128_GCM;
jfigus857009c2014-11-05 11:17:43 -05002240 p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002241 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002242 p->auth_key_len = 0;
2243 p->auth_tag_len = 8; /* 8 octet tag length */
2244 p->sec_serv = sec_serv_conf_and_auth;
2245}
2246
2247/*
2248 * AES-256 GCM mode with 8 octet auth tag.
2249 */
2250void
jfigus857009c2014-11-05 11:17:43 -05002251srtp_crypto_policy_set_aes_gcm_256_8_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002252 p->cipher_type = SRTP_AES_256_GCM;
jfigus857009c2014-11-05 11:17:43 -05002253 p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002254 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002255 p->auth_key_len = 0;
2256 p->auth_tag_len = 8; /* 8 octet tag length */
2257 p->sec_serv = sec_serv_conf_and_auth;
2258}
2259
2260/*
2261 * AES-128 GCM mode with 8 octet auth tag, no RTCP encryption.
2262 */
2263void
jfigus857009c2014-11-05 11:17:43 -05002264srtp_crypto_policy_set_aes_gcm_128_8_only_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002265 p->cipher_type = SRTP_AES_128_GCM;
jfigus857009c2014-11-05 11:17:43 -05002266 p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002267 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002268 p->auth_key_len = 0;
2269 p->auth_tag_len = 8; /* 8 octet tag length */
2270 p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
2271}
2272
2273/*
2274 * AES-256 GCM mode with 8 octet auth tag, no RTCP encryption.
2275 */
2276void
jfigus857009c2014-11-05 11:17:43 -05002277srtp_crypto_policy_set_aes_gcm_256_8_only_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002278 p->cipher_type = SRTP_AES_256_GCM;
jfigus857009c2014-11-05 11:17:43 -05002279 p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002280 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigus8c36da22013-10-01 16:41:19 -04002281 p->auth_key_len = 0;
2282 p->auth_tag_len = 8; /* 8 octet tag length */
2283 p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
2284}
jfigusc13c1002014-05-08 13:34:53 -04002285
2286/*
2287 * AES-128 GCM mode with 16 octet auth tag.
2288 */
2289void
jfigus857009c2014-11-05 11:17:43 -05002290srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002291 p->cipher_type = SRTP_AES_128_GCM;
jfigus857009c2014-11-05 11:17:43 -05002292 p->cipher_key_len = SRTP_AES_128_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002293 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigusc13c1002014-05-08 13:34:53 -04002294 p->auth_key_len = 0;
2295 p->auth_tag_len = 16; /* 16 octet tag length */
2296 p->sec_serv = sec_serv_conf_and_auth;
2297}
2298
2299/*
2300 * AES-256 GCM mode with 16 octet auth tag.
2301 */
2302void
jfigus857009c2014-11-05 11:17:43 -05002303srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p) {
jfigus67b9c732014-11-20 10:17:21 -05002304 p->cipher_type = SRTP_AES_256_GCM;
jfigus857009c2014-11-05 11:17:43 -05002305 p->cipher_key_len = SRTP_AES_256_GCM_KEYSIZE_WSALT;
jfigus67b9c732014-11-20 10:17:21 -05002306 p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
jfigusc13c1002014-05-08 13:34:53 -04002307 p->auth_key_len = 0;
2308 p->auth_tag_len = 16; /* 16 octet tag length */
2309 p->sec_serv = sec_serv_conf_and_auth;
2310}
2311
jfigus8c36da22013-10-01 16:41:19 -04002312#endif
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002313
Cullen Jennings235513a2005-09-21 22:51:36 +00002314/*
2315 * secure rtcp functions
2316 */
2317
jfigus8c36da22013-10-01 16:41:19 -04002318/*
2319 * AEAD uses a new IV formation method. This function implements
2320 * section 10.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The
2321 * calculation is defined as, where (+) is the xor operation:
2322 *
2323 * 0 1 2 3 4 5 6 7 8 9 10 11
2324 * +--+--+--+--+--+--+--+--+--+--+--+--+
2325 * |00|00| SSRC |00|00|0+SRTCP Idx|---+
2326 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2327 * |
2328 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2329 * | Encryption Salt |->(+)
2330 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2331 * |
2332 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2333 * | Initialization Vector |<--+
2334 * +--+--+--+--+--+--+--+--+--+--+--+--+*
2335 *
2336 * Input: *stream - pointer to SRTP stream context, used to retrieve
2337 * the SALT
2338 * *iv - Pointer to recieve the calculated IV
2339 * seq_num - The SEQ value to use for the IV calculation.
2340 * *hdr - The RTP header, used to get the SSRC value
2341 *
2342 */
2343static void srtp_calc_aead_iv_srtcp(srtp_stream_ctx_t *stream, v128_t *iv,
2344 uint32_t seq_num, srtcp_hdr_t *hdr)
2345{
2346 v128_t in;
2347 v128_t salt;
2348
2349 memset(&in, 0, sizeof(v128_t));
2350 memset(&salt, 0, sizeof(v128_t));
2351
2352 in.v16[0] = 0;
2353 memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */
2354 in.v16[3] = 0;
2355 in.v32[2] = 0x7FFFFFFF & htonl(seq_num); /* bit 32 is suppose to be zero */
2356
2357 debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in));
2358
2359 /*
2360 * Get the SALT value from the context
2361 */
2362 memcpy(salt.v8, stream->c_salt, 12);
2363 debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt));
2364
2365 /*
2366 * Finally, apply the SALT to the input
2367 */
2368 v128_xor(iv, &in, &salt);
2369}
2370
2371/*
2372 * This code handles AEAD ciphers for outgoing RTCP. We currently support
2373 * AES-GCM mode with 128 or 256 bit keys.
2374 */
jfigus857009c2014-11-05 11:17:43 -05002375static srtp_err_status_t
jfigus8c36da22013-10-01 16:41:19 -04002376srtp_protect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,
Travis Cross31844002014-07-02 16:18:57 +00002377 void *rtcp_hdr, unsigned int *pkt_octet_len)
jfigus8c36da22013-10-01 16:41:19 -04002378{
2379 srtcp_hdr_t *hdr = (srtcp_hdr_t*)rtcp_hdr;
2380 uint32_t *enc_start; /* pointer to start of encrypted portion */
2381 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00002382 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigus8c36da22013-10-01 16:41:19 -04002383 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05002384 srtp_err_status_t status;
jfigus226ec562014-12-02 15:11:03 -05002385 uint32_t tag_len;
jfigus8c36da22013-10-01 16:41:19 -04002386 uint32_t seq_num;
2387 v128_t iv;
2388 uint32_t tseq;
2389
2390 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05002391 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
jfigus8c36da22013-10-01 16:41:19 -04002392
2393 /*
2394 * set encryption start and encryption length - if we're not
2395 * providing confidentiality, set enc_start to NULL
2396 */
2397 enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header;
2398 enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
2399
2400 /* NOTE: hdr->length is not usable - it refers to only the first
2401 RTCP report in the compound packet! */
2402 /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2403 multiples of 32-bits (RFC 3550 6.1) */
2404 trailer = (uint32_t*)((char*)enc_start + enc_octet_len + tag_len);
2405
2406 if (stream->rtcp_services & sec_serv_conf) {
2407 *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
2408 } else {
2409 enc_start = NULL;
2410 enc_octet_len = 0;
2411 /* 0 is network-order independant */
2412 *trailer = 0x00000000; /* set encrypt bit */
2413 }
2414
2415 /*
2416 * set the auth_tag pointer to the proper location, which is after
2417 * the payload, but before the trailer
2418 * (note that srtpc *always* provides authentication, unlike srtp)
2419 */
2420 /* Note: This would need to change for optional mikey data */
2421 auth_tag = (uint8_t*)hdr + *pkt_octet_len;
2422
2423 /*
2424 * check sequence number for overruns, and copy it into the packet
2425 * if its value isn't too big
2426 */
jfigusde8deb32014-11-25 12:58:11 -05002427 status = srtp_rdb_increment(&stream->rtcp_rdb);
jfigus8c36da22013-10-01 16:41:19 -04002428 if (status) {
2429 return status;
2430 }
jfigusde8deb32014-11-25 12:58:11 -05002431 seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
jfigus8c36da22013-10-01 16:41:19 -04002432 *trailer |= htonl(seq_num);
2433 debug_print(mod_srtp, "srtcp index: %x", seq_num);
2434
2435 /*
2436 * Calculating the IV and pass it down to the cipher
2437 */
2438 srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr);
jfigus0d99b712014-12-01 16:08:19 -05002439 status = srtp_cipher_set_iv(stream->rtcp_cipher, (const uint8_t*)&iv, direction_encrypt);
jfigus8c36da22013-10-01 16:41:19 -04002440 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002441 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04002442 }
2443
2444 /*
2445 * Set the AAD for GCM mode
2446 */
2447 if (enc_start) {
2448 /*
2449 * If payload encryption is enabled, then the AAD consist of
2450 * the RTCP header and the seq# at the end of the packet
2451 */
jfigus226ec562014-12-02 15:11:03 -05002452 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header);
jfigus8c36da22013-10-01 16:41:19 -04002453 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002454 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002455 }
2456 } else {
2457 /*
2458 * Since payload encryption is not enabled, we must authenticate
2459 * the entire packet as described in section 10.3 in revision 07
2460 * of the draft.
2461 */
jfigus226ec562014-12-02 15:11:03 -05002462 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, *pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002463 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002464 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002465 }
2466 }
2467 /*
2468 * put the idx# into network byte order and process it as AAD
2469 */
2470 tseq = htonl(*trailer);
jfigus226ec562014-12-02 15:11:03 -05002471 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t));
jfigus8c36da22013-10-01 16:41:19 -04002472 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002473 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002474 }
2475
2476 /* if we're encrypting, exor keystream into the message */
2477 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05002478 status = srtp_cipher_encrypt(stream->rtcp_cipher,
2479 (uint8_t*)enc_start, &enc_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002480 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002481 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04002482 }
2483 /*
2484 * Get the tag and append that to the output
2485 */
jfigus226ec562014-12-02 15:11:03 -05002486 status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len);
jfigus8c36da22013-10-01 16:41:19 -04002487 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002488 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002489 }
2490 enc_octet_len += tag_len;
2491 } else {
2492 /*
2493 * Even though we're not encrypting the payload, we need
2494 * to run the cipher to get the auth tag.
2495 */
Travis Cross1b8b1e72014-07-02 15:32:36 +00002496 unsigned int nolen = 0;
jfigus2964a152014-11-25 15:25:37 -05002497 status = srtp_cipher_encrypt(stream->rtcp_cipher, NULL, &nolen);
jfigus8c36da22013-10-01 16:41:19 -04002498 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002499 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04002500 }
2501 /*
2502 * Get the tag and append that to the output
2503 */
jfigus226ec562014-12-02 15:11:03 -05002504 status = srtp_cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag, &tag_len);
jfigus8c36da22013-10-01 16:41:19 -04002505 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002506 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002507 }
2508 enc_octet_len += tag_len;
2509 }
2510
2511 /* increase the packet length by the length of the auth tag and seq_num*/
2512 *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
2513
jfigus857009c2014-11-05 11:17:43 -05002514 return srtp_err_status_ok;
jfigus8c36da22013-10-01 16:41:19 -04002515}
2516
2517/*
2518 * This function handles incoming SRTCP packets while in AEAD mode,
2519 * which currently supports AES-GCM encryption. Note, the auth tag is
2520 * at the end of the packet stream and is automatically checked by GCM
2521 * when decrypting the payload.
2522 */
jfigus857009c2014-11-05 11:17:43 -05002523static srtp_err_status_t
jfigus8c36da22013-10-01 16:41:19 -04002524srtp_unprotect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,
Travis Cross31844002014-07-02 16:18:57 +00002525 void *srtcp_hdr, unsigned int *pkt_octet_len)
jfigus8c36da22013-10-01 16:41:19 -04002526{
2527 srtcp_hdr_t *hdr = (srtcp_hdr_t*)srtcp_hdr;
2528 uint32_t *enc_start; /* pointer to start of encrypted portion */
2529 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00002530 unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
jfigus8c36da22013-10-01 16:41:19 -04002531 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05002532 srtp_err_status_t status;
jfigus8c36da22013-10-01 16:41:19 -04002533 int tag_len;
2534 unsigned int tmp_len;
2535 uint32_t seq_num;
2536 v128_t iv;
2537 uint32_t tseq;
2538
2539 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05002540 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
jfigus8c36da22013-10-01 16:41:19 -04002541
jfigus8c36da22013-10-01 16:41:19 -04002542 /*
2543 * set encryption start, encryption length, and trailer
2544 */
2545 /* index & E (encryption) bit follow normal data. hdr->len
2546 is the number of words (32-bit) in the normal packet minus 1 */
2547 /* This should point trailer to the word past the end of the
2548 normal data. */
2549 /* This would need to be modified for optional mikey data */
2550 /*
2551 * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2552 * multiples of 32-bits (RFC 3550 6.1)
2553 */
2554 trailer = (uint32_t*)((char*)hdr + *pkt_octet_len - sizeof(srtcp_trailer_t));
2555 /*
2556 * We pass the tag down to the cipher when doing GCM mode
2557 */
2558 enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header +
2559 sizeof(srtcp_trailer_t));
2560 auth_tag = (uint8_t*)hdr + *pkt_octet_len - tag_len - sizeof(srtcp_trailer_t);
2561
2562 if (*((unsigned char*)trailer) & SRTCP_E_BYTE_BIT) {
2563 enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header;
2564 } else {
2565 enc_octet_len = 0;
2566 enc_start = NULL; /* this indicates that there's no encryption */
2567 }
2568
2569 /*
2570 * check the sequence number for replays
2571 */
2572 /* this is easier than dealing with bitfield access */
2573 seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK;
2574 debug_print(mod_srtp, "srtcp index: %x", seq_num);
jfigusde8deb32014-11-25 12:58:11 -05002575 status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
jfigus8c36da22013-10-01 16:41:19 -04002576 if (status) {
2577 return status;
2578 }
2579
2580 /*
2581 * Calculate and set the IV
2582 */
2583 srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr);
jfigus0d99b712014-12-01 16:08:19 -05002584 status = srtp_cipher_set_iv(stream->rtcp_cipher, (const uint8_t*)&iv, direction_decrypt);
jfigus8c36da22013-10-01 16:41:19 -04002585 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002586 return srtp_err_status_cipher_fail;
jfigus8c36da22013-10-01 16:41:19 -04002587 }
2588
2589 /*
2590 * Set the AAD for GCM mode
2591 */
2592 if (enc_start) {
2593 /*
2594 * If payload encryption is enabled, then the AAD consist of
2595 * the RTCP header and the seq# at the end of the packet
2596 */
jfigus226ec562014-12-02 15:11:03 -05002597 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr, octets_in_rtcp_header);
jfigus8c36da22013-10-01 16:41:19 -04002598 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002599 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002600 }
2601 } else {
2602 /*
2603 * Since payload encryption is not enabled, we must authenticate
2604 * the entire packet as described in section 10.3 in revision 07
2605 * of the draft.
2606 */
jfigus226ec562014-12-02 15:11:03 -05002607 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
2608 (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t)));
jfigus8c36da22013-10-01 16:41:19 -04002609 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002610 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002611 }
2612 }
2613
2614 /*
2615 * put the idx# into network byte order, and process it as AAD
2616 */
2617 tseq = htonl(*trailer);
jfigus226ec562014-12-02 15:11:03 -05002618 status = srtp_cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq, sizeof(srtcp_trailer_t));
jfigus8c36da22013-10-01 16:41:19 -04002619 if (status) {
jfigus857009c2014-11-05 11:17:43 -05002620 return ( srtp_err_status_cipher_fail);
jfigus8c36da22013-10-01 16:41:19 -04002621 }
2622
2623 /* if we're decrypting, exor keystream into the message */
2624 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05002625 status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t*)enc_start, &enc_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002626 if (status) {
2627 return status;
2628 }
2629 } else {
2630 /*
2631 * Still need to run the cipher to check the tag
2632 */
2633 tmp_len = tag_len;
jfigus2964a152014-11-25 15:25:37 -05002634 status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t*)auth_tag, &tmp_len);
jfigus8c36da22013-10-01 16:41:19 -04002635 if (status) {
2636 return status;
2637 }
2638 }
2639
2640 /* decrease the packet length by the length of the auth tag and seq_num*/
2641 *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
2642
2643 /*
2644 * verify that stream is for received traffic - this check will
2645 * detect SSRC collisions, since a stream that appears in both
2646 * srtp_protect() and srtp_unprotect() will fail this test in one of
2647 * those functions.
2648 *
2649 * we do this check *after* the authentication check, so that the
2650 * latter check will catch any attempts to fool us into thinking
2651 * that we've got a collision
2652 */
2653 if (stream->direction != dir_srtp_receiver) {
2654 if (stream->direction == dir_unknown) {
2655 stream->direction = dir_srtp_receiver;
2656 } else {
2657 srtp_handle_event(ctx, stream, event_ssrc_collision);
2658 }
2659 }
2660
2661 /*
2662 * if the stream is a 'provisional' one, in which the template context
2663 * is used, then we need to allocate a new stream at this point, since
2664 * the authentication passed
2665 */
2666 if (stream == ctx->stream_template) {
2667 srtp_stream_ctx_t *new_stream;
2668
2669 /*
2670 * allocate and initialize a new stream
2671 *
2672 * note that we indicate failure if we can't allocate the new
2673 * stream, and some implementations will want to not return
2674 * failure here
2675 */
2676 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
2677 if (status) {
2678 return status;
2679 }
2680
2681 /* add new stream to the head of the stream_list */
2682 new_stream->next = ctx->stream_list;
2683 ctx->stream_list = new_stream;
2684
2685 /* set stream (the pointer used in this function) */
2686 stream = new_stream;
2687 }
2688
2689 /* we've passed the authentication check, so add seq_num to the rdb */
jfigusde8deb32014-11-25 12:58:11 -05002690 srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
jfigus8c36da22013-10-01 16:41:19 -04002691
jfigus857009c2014-11-05 11:17:43 -05002692 return srtp_err_status_ok;
jfigus8c36da22013-10-01 16:41:19 -04002693}
2694
jfigus857009c2014-11-05 11:17:43 -05002695srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002696srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00002697 srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00002698 uint32_t *enc_start; /* pointer to start of encrypted portion */
2699 uint32_t *auth_start; /* pointer to start of auth. portion */
2700 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00002701 unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002702 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
jfigus857009c2014-11-05 11:17:43 -05002703 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002704 int tag_len;
2705 srtp_stream_ctx_t *stream;
jfigus2964a152014-11-25 15:25:37 -05002706 uint32_t prefix_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00002707 uint32_t seq_num;
2708
2709 /* we assume the hdr is 32-bit aligned to start */
Travis Cross8ba46eb2014-06-29 18:42:29 +00002710
2711 /* check the packet length - it must at least contain a full header */
2712 if (*pkt_octet_len < octets_in_rtcp_header)
jfigus857009c2014-11-05 11:17:43 -05002713 return srtp_err_status_bad_param;
Travis Cross8ba46eb2014-06-29 18:42:29 +00002714
Cullen Jennings235513a2005-09-21 22:51:36 +00002715 /*
2716 * look up ssrc in srtp_stream list, and process the packet with
2717 * the appropriate stream. if we haven't seen this stream before,
2718 * there's only one key for this srtp_session, and the cipher
2719 * supports key-sharing, then we assume that a new stream using
2720 * that key has just started up
2721 */
2722 stream = srtp_get_stream(ctx, hdr->ssrc);
2723 if (stream == NULL) {
2724 if (ctx->stream_template != NULL) {
2725 srtp_stream_ctx_t *new_stream;
2726
2727 /* allocate and initialize a new stream */
2728 status = srtp_stream_clone(ctx->stream_template,
2729 hdr->ssrc, &new_stream);
2730 if (status)
2731 return status;
2732
2733 /* add new stream to the head of the stream_list */
2734 new_stream->next = ctx->stream_list;
2735 ctx->stream_list = new_stream;
2736
2737 /* set stream (the pointer used in this function) */
2738 stream = new_stream;
2739 } else {
2740 /* no template stream, so we return an error */
jfigus857009c2014-11-05 11:17:43 -05002741 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00002742 }
2743 }
2744
2745 /*
2746 * verify that stream is for sending traffic - this check will
2747 * detect SSRC collisions, since a stream that appears in both
2748 * srtp_protect() and srtp_unprotect() will fail this test in one of
2749 * those functions.
2750 */
2751 if (stream->direction != dir_srtp_sender) {
2752 if (stream->direction == dir_unknown) {
David McGrewc34f7402006-03-09 21:17:00 +00002753 stream->direction = dir_srtp_sender;
Cullen Jennings235513a2005-09-21 22:51:36 +00002754 } else {
2755 srtp_handle_event(ctx, stream, event_ssrc_collision);
2756 }
2757 }
2758
jfigus8c36da22013-10-01 16:41:19 -04002759 /*
2760 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
2761 * the request to our AEAD handler.
2762 */
jfigus67b9c732014-11-20 10:17:21 -05002763 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
2764 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00002765 return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002766 }
2767
Cullen Jennings235513a2005-09-21 22:51:36 +00002768 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05002769 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00002770
2771 /*
2772 * set encryption start and encryption length - if we're not
2773 * providing confidentiality, set enc_start to NULL
2774 */
2775 enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
2776 enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
2777
2778 /* all of the packet, except the header, gets encrypted */
2779 /* NOTE: hdr->length is not usable - it refers to only the first
2780 RTCP report in the compound packet! */
2781 /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2782 multiples of 32-bits (RFC 3550 6.1) */
2783 trailer = (uint32_t *) ((char *)enc_start + enc_octet_len);
2784
2785 if (stream->rtcp_services & sec_serv_conf) {
2786 *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
2787 } else {
2788 enc_start = NULL;
2789 enc_octet_len = 0;
2790 /* 0 is network-order independant */
2791 *trailer = 0x00000000; /* set encrypt bit */
2792 }
2793
2794 /*
2795 * set the auth_start and auth_tag pointers to the proper locations
2796 * (note that srtpc *always* provides authentication, unlike srtp)
2797 */
2798 /* Note: This would need to change for optional mikey data */
2799 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002800 auth_tag = (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t);
Cullen Jennings235513a2005-09-21 22:51:36 +00002801
David McGrew79870d62007-06-15 18:17:39 +00002802 /* perform EKT processing if needed */
jfigusc5887e72014-11-06 09:46:18 -05002803 srtp_ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len,
jfigusde8deb32014-11-25 12:58:11 -05002804 srtp_rdbx_get_packet_index(&stream->rtp_rdbx));
David McGrew79870d62007-06-15 18:17:39 +00002805
Cullen Jennings235513a2005-09-21 22:51:36 +00002806 /*
2807 * check sequence number for overruns, and copy it into the packet
2808 * if its value isn't too big
2809 */
jfigusde8deb32014-11-25 12:58:11 -05002810 status = srtp_rdb_increment(&stream->rtcp_rdb);
Cullen Jennings235513a2005-09-21 22:51:36 +00002811 if (status)
2812 return status;
jfigusde8deb32014-11-25 12:58:11 -05002813 seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
Cullen Jennings235513a2005-09-21 22:51:36 +00002814 *trailer |= htonl(seq_num);
2815 debug_print(mod_srtp, "srtcp index: %x", seq_num);
2816
2817 /*
2818 * if we're using rindael counter mode, set nonce and seq
2819 */
jfigus67b9c732014-11-20 10:17:21 -05002820 if (stream->rtcp_cipher->type->id == SRTP_AES_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002821 v128_t iv;
2822
2823 iv.v32[0] = 0;
2824 iv.v32[1] = hdr->ssrc; /* still in network order! */
2825 iv.v32[2] = htonl(seq_num >> 16);
2826 iv.v32[3] = htonl(seq_num << 16);
jfigus0d99b712014-12-01 16:08:19 -05002827 status = srtp_cipher_set_iv(stream->rtcp_cipher, (const uint8_t*)&iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00002828
2829 } else {
2830 v128_t iv;
2831
2832 /* otherwise, just set the index to seq_num */
2833 iv.v32[0] = 0;
2834 iv.v32[1] = 0;
2835 iv.v32[2] = 0;
2836 iv.v32[3] = htonl(seq_num);
jfigus0d99b712014-12-01 16:08:19 -05002837 status = srtp_cipher_set_iv(stream->rtcp_cipher, (const uint8_t*)&iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00002838 }
2839 if (status)
jfigus857009c2014-11-05 11:17:43 -05002840 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002841
2842 /*
2843 * if we're authenticating using a universal hash, put the keystream
2844 * prefix into the authentication tag
2845 */
2846
2847 /* if auth_start is non-null, then put keystream into tag */
2848 if (auth_start) {
2849
2850 /* put keystream prefix into auth_tag */
jfigus8f669722014-11-19 15:20:03 -05002851 prefix_len = srtp_auth_get_prefix_length(stream->rtcp_auth);
jfigus2964a152014-11-25 15:25:37 -05002852 status = srtp_cipher_output(stream->rtcp_cipher, auth_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00002853
2854 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05002855 srtp_octet_string_hex_string(auth_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00002856
2857 if (status)
jfigus857009c2014-11-05 11:17:43 -05002858 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002859 }
2860
2861 /* if we're encrypting, exor keystream into the message */
2862 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05002863 status = srtp_cipher_encrypt(stream->rtcp_cipher,
2864 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00002865 if (status)
jfigus857009c2014-11-05 11:17:43 -05002866 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002867 }
2868
2869 /* initialize auth func context */
2870 auth_start(stream->rtcp_auth);
2871
David McGrew9c70f292006-05-03 19:38:38 +00002872 /*
2873 * run auth func over packet (including trailer), and write the
2874 * result at auth_tag
2875 */
Cullen Jennings235513a2005-09-21 22:51:36 +00002876 status = auth_compute(stream->rtcp_auth,
David McGrew9c70f292006-05-03 19:38:38 +00002877 (uint8_t *)auth_start,
2878 (*pkt_octet_len) + sizeof(srtcp_trailer_t),
2879 auth_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00002880 debug_print(mod_srtp, "srtcp auth tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05002881 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00002882 if (status)
jfigus857009c2014-11-05 11:17:43 -05002883 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00002884
2885 /* increase the packet length by the length of the auth tag and seq_num*/
2886 *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
2887
jfigus857009c2014-11-05 11:17:43 -05002888 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002889}
2890
2891
jfigus857009c2014-11-05 11:17:43 -05002892srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +00002893srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00002894 srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00002895 uint32_t *enc_start; /* pointer to start of encrypted portion */
2896 uint32_t *auth_start; /* pointer to start of auth. portion */
2897 uint32_t *trailer; /* pointer to start of trailer */
Travis Cross1b8b1e72014-07-02 15:32:36 +00002898 unsigned int enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002899 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
2900 uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
David McGrew79870d62007-06-15 18:17:39 +00002901 uint8_t tag_copy[SRTP_MAX_TAG_LEN];
jfigus857009c2014-11-05 11:17:43 -05002902 srtp_err_status_t status;
Travis Cross1b8b1e72014-07-02 15:32:36 +00002903 unsigned int auth_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00002904 int tag_len;
2905 srtp_stream_ctx_t *stream;
jfigus2964a152014-11-25 15:25:37 -05002906 uint32_t prefix_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00002907 uint32_t seq_num;
TV Sriram4986a362013-05-06 11:24:03 -07002908 int e_bit_in_packet; /* whether the E-bit was found in the packet */
2909 int sec_serv_confidentiality; /* whether confidentiality was requested */
Cullen Jennings235513a2005-09-21 22:51:36 +00002910
2911 /* we assume the hdr is 32-bit aligned to start */
Travis Cross444a5442014-07-02 16:01:01 +00002912
Travis Crosse896bf72014-07-07 19:59:33 +00002913 /* check that the length value is sane; we'll check again once we
2914 know the tag length, but we at least want to know that it is
2915 a positive value */
2916 if (*pkt_octet_len < octets_in_rtcp_header + sizeof(srtcp_trailer_t))
jfigus857009c2014-11-05 11:17:43 -05002917 return srtp_err_status_bad_param;
Travis Crosse896bf72014-07-07 19:59:33 +00002918
Cullen Jennings235513a2005-09-21 22:51:36 +00002919 /*
2920 * look up ssrc in srtp_stream list, and process the packet with
2921 * the appropriate stream. if we haven't seen this stream before,
2922 * there's only one key for this srtp_session, and the cipher
2923 * supports key-sharing, then we assume that a new stream using
2924 * that key has just started up
2925 */
2926 stream = srtp_get_stream(ctx, hdr->ssrc);
2927 if (stream == NULL) {
2928 if (ctx->stream_template != NULL) {
2929 stream = ctx->stream_template;
David McGrew79870d62007-06-15 18:17:39 +00002930
2931 /*
2932 * check to see if stream_template has an EKT data structure, in
2933 * which case we initialize the template using the EKT policy
2934 * referenced by that data (which consists of decrypting the
2935 * master key from the EKT field)
2936 *
2937 * this function initializes a *provisional* stream, and this
2938 * stream should not be accepted until and unless the packet
2939 * passes its authentication check
2940 */
2941 if (stream->ekt != NULL) {
2942 status = srtp_stream_init_from_ekt(stream, srtcp_hdr, *pkt_octet_len);
2943 if (status)
2944 return status;
2945 }
2946
Cullen Jennings235513a2005-09-21 22:51:36 +00002947 debug_print(mod_srtp, "srtcp using provisional stream (SSRC: 0x%08x)",
2948 hdr->ssrc);
2949 } else {
2950 /* no template stream, so we return an error */
jfigus857009c2014-11-05 11:17:43 -05002951 return srtp_err_status_no_ctx;
Cullen Jennings235513a2005-09-21 22:51:36 +00002952 }
2953 }
2954
Travis Cross8ba46eb2014-06-29 18:42:29 +00002955 /* get tag length from stream context */
jfigus8f669722014-11-19 15:20:03 -05002956 tag_len = srtp_auth_get_tag_length(stream->rtcp_auth);
Travis Cross8ba46eb2014-06-29 18:42:29 +00002957
2958 /* check the packet length - it must contain at least a full RTCP
2959 header, an auth tag (if applicable), and the SRTCP encrypted flag
2960 and 31-bit index value */
Joachim Bauch25a0e6c2015-03-27 16:59:06 +01002961 if (*pkt_octet_len < (int) (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t))) {
jfigus857009c2014-11-05 11:17:43 -05002962 return srtp_err_status_bad_param;
jfigus73e30932014-07-07 15:50:32 -04002963 }
Travis Cross8ba46eb2014-06-29 18:42:29 +00002964
jfigus8c36da22013-10-01 16:41:19 -04002965 /*
2966 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
2967 * the request to our AEAD handler.
2968 */
jfigus67b9c732014-11-20 10:17:21 -05002969 if (stream->rtp_cipher->algorithm == SRTP_AES_128_GCM ||
2970 stream->rtp_cipher->algorithm == SRTP_AES_256_GCM) {
Travis Cross31844002014-07-02 16:18:57 +00002971 return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr, (unsigned int*)pkt_octet_len);
jfigus8c36da22013-10-01 16:41:19 -04002972 }
2973
TV Sriram4986a362013-05-06 11:24:03 -07002974 sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf ||
2975 stream->rtcp_services == sec_serv_conf_and_auth;
2976
Cullen Jennings235513a2005-09-21 22:51:36 +00002977 /*
2978 * set encryption start, encryption length, and trailer
2979 */
2980 enc_octet_len = *pkt_octet_len -
2981 (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t));
2982 /* index & E (encryption) bit follow normal data. hdr->len
2983 is the number of words (32-bit) in the normal packet minus 1 */
2984 /* This should point trailer to the word past the end of the
2985 normal data. */
2986 /* This would need to be modified for optional mikey data */
2987 /*
2988 * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2989 * multiples of 32-bits (RFC 3550 6.1)
2990 */
2991 trailer = (uint32_t *) ((char *) hdr +
TV Sriram4986a362013-05-06 11:24:03 -07002992 *pkt_octet_len -(tag_len + sizeof(srtcp_trailer_t)));
2993 e_bit_in_packet =
2994 (*((unsigned char *) trailer) & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT;
2995 if (e_bit_in_packet != sec_serv_confidentiality) {
jfigus857009c2014-11-05 11:17:43 -05002996 return srtp_err_status_cant_check;
TV Sriram4986a362013-05-06 11:24:03 -07002997 }
2998 if (sec_serv_confidentiality) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002999 enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
3000 } else {
3001 enc_octet_len = 0;
3002 enc_start = NULL; /* this indicates that there's no encryption */
3003 }
3004
3005 /*
3006 * set the auth_start and auth_tag pointers to the proper locations
3007 * (note that srtcp *always* uses authentication, unlike srtp)
3008 */
3009 auth_start = (uint32_t *)hdr;
David McGrew79870d62007-06-15 18:17:39 +00003010 auth_len = *pkt_octet_len - tag_len;
3011 auth_tag = (uint8_t *)hdr + auth_len;
3012
3013 /*
3014 * if EKT is in use, then we make a copy of the tag from the packet,
3015 * and then zeroize the location of the base tag
3016 *
3017 * we first re-position the auth_tag pointer so that it points to
3018 * the base tag
3019 */
3020 if (stream->ekt) {
jfigusc5887e72014-11-06 09:46:18 -05003021 auth_tag -= srtp_ekt_octets_after_base_tag(stream->ekt);
David McGrew79870d62007-06-15 18:17:39 +00003022 memcpy(tag_copy, auth_tag, tag_len);
3023 octet_string_set_to_zero(auth_tag, tag_len);
3024 auth_tag = tag_copy;
3025 auth_len += tag_len;
3026 }
Cullen Jennings235513a2005-09-21 22:51:36 +00003027
3028 /*
3029 * check the sequence number for replays
3030 */
3031 /* this is easier than dealing with bitfield access */
3032 seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK;
David McGrew9c70f292006-05-03 19:38:38 +00003033 debug_print(mod_srtp, "srtcp index: %x", seq_num);
jfigusde8deb32014-11-25 12:58:11 -05003034 status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
Cullen Jennings235513a2005-09-21 22:51:36 +00003035 if (status)
3036 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00003037
3038 /*
3039 * if we're using aes counter mode, set nonce and seq
3040 */
jfigus67b9c732014-11-20 10:17:21 -05003041 if (stream->rtcp_cipher->type->id == SRTP_AES_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00003042 v128_t iv;
3043
3044 iv.v32[0] = 0;
3045 iv.v32[1] = hdr->ssrc; /* still in network order! */
3046 iv.v32[2] = htonl(seq_num >> 16);
3047 iv.v32[3] = htonl(seq_num << 16);
jfigus0d99b712014-12-01 16:08:19 -05003048 status = srtp_cipher_set_iv(stream->rtcp_cipher, (const uint8_t*)&iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00003049
3050 } else {
3051 v128_t iv;
3052
3053 /* otherwise, just set the index to seq_num */
3054 iv.v32[0] = 0;
3055 iv.v32[1] = 0;
3056 iv.v32[2] = 0;
3057 iv.v32[3] = htonl(seq_num);
jfigus0d99b712014-12-01 16:08:19 -05003058 status = srtp_cipher_set_iv(stream->rtcp_cipher, (const uint8_t*)&iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00003059
3060 }
3061 if (status)
jfigus857009c2014-11-05 11:17:43 -05003062 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003063
3064 /* initialize auth func context */
3065 auth_start(stream->rtcp_auth);
3066
3067 /* run auth func over packet, put result into tmp_tag */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00003068 status = auth_compute(stream->rtcp_auth, (uint8_t *)auth_start,
David McGrew79870d62007-06-15 18:17:39 +00003069 auth_len, tmp_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00003070 debug_print(mod_srtp, "srtcp computed tag: %s",
jfigus46d6b472014-11-14 16:42:01 -05003071 srtp_octet_string_hex_string(tmp_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003072 if (status)
jfigus857009c2014-11-05 11:17:43 -05003073 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003074
3075 /* compare the tag just computed with the one in the packet */
3076 debug_print(mod_srtp, "srtcp tag from packet: %s",
jfigus46d6b472014-11-14 16:42:01 -05003077 srtp_octet_string_hex_string(auth_tag, tag_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003078 if (octet_string_is_eq(tmp_tag, auth_tag, tag_len))
jfigus857009c2014-11-05 11:17:43 -05003079 return srtp_err_status_auth_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003080
3081 /*
3082 * if we're authenticating using a universal hash, put the keystream
3083 * prefix into the authentication tag
3084 */
jfigus8f669722014-11-19 15:20:03 -05003085 prefix_len = srtp_auth_get_prefix_length(stream->rtcp_auth);
Cullen Jennings235513a2005-09-21 22:51:36 +00003086 if (prefix_len) {
jfigus2964a152014-11-25 15:25:37 -05003087 status = srtp_cipher_output(stream->rtcp_cipher, auth_tag, &prefix_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00003088 debug_print(mod_srtp, "keystream prefix: %s",
jfigus46d6b472014-11-14 16:42:01 -05003089 srtp_octet_string_hex_string(auth_tag, prefix_len));
Cullen Jennings235513a2005-09-21 22:51:36 +00003090 if (status)
jfigus857009c2014-11-05 11:17:43 -05003091 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003092 }
3093
3094 /* if we're decrypting, exor keystream into the message */
3095 if (enc_start) {
jfigus2964a152014-11-25 15:25:37 -05003096 status = srtp_cipher_decrypt(stream->rtcp_cipher, (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00003097 if (status)
jfigus857009c2014-11-05 11:17:43 -05003098 return srtp_err_status_cipher_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +00003099 }
3100
David McGrew79870d62007-06-15 18:17:39 +00003101 /* decrease the packet length by the length of the auth tag and seq_num */
Cullen Jennings235513a2005-09-21 22:51:36 +00003102 *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
3103
David McGrew79870d62007-06-15 18:17:39 +00003104 /*
3105 * if EKT is in effect, subtract the EKT data out of the packet
3106 * length
3107 */
jfigusc5887e72014-11-06 09:46:18 -05003108 *pkt_octet_len -= srtp_ekt_octets_after_base_tag(stream->ekt);
David McGrew79870d62007-06-15 18:17:39 +00003109
Cullen Jennings235513a2005-09-21 22:51:36 +00003110 /*
3111 * verify that stream is for received traffic - this check will
3112 * detect SSRC collisions, since a stream that appears in both
3113 * srtp_protect() and srtp_unprotect() will fail this test in one of
3114 * those functions.
3115 *
3116 * we do this check *after* the authentication check, so that the
3117 * latter check will catch any attempts to fool us into thinking
3118 * that we've got a collision
3119 */
3120 if (stream->direction != dir_srtp_receiver) {
3121 if (stream->direction == dir_unknown) {
3122 stream->direction = dir_srtp_receiver;
3123 } else {
3124 srtp_handle_event(ctx, stream, event_ssrc_collision);
3125 }
3126 }
3127
3128 /*
3129 * if the stream is a 'provisional' one, in which the template context
3130 * is used, then we need to allocate a new stream at this point, since
3131 * the authentication passed
3132 */
3133 if (stream == ctx->stream_template) {
3134 srtp_stream_ctx_t *new_stream;
3135
3136 /*
3137 * allocate and initialize a new stream
3138 *
3139 * note that we indicate failure if we can't allocate the new
3140 * stream, and some implementations will want to not return
3141 * failure here
3142 */
3143 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
3144 if (status)
3145 return status;
3146
3147 /* add new stream to the head of the stream_list */
3148 new_stream->next = ctx->stream_list;
3149 ctx->stream_list = new_stream;
3150
3151 /* set stream (the pointer used in this function) */
3152 stream = new_stream;
3153 }
3154
3155 /* we've passed the authentication check, so add seq_num to the rdb */
jfigusde8deb32014-11-25 12:58:11 -05003156 srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
Cullen Jennings235513a2005-09-21 22:51:36 +00003157
3158
jfigus857009c2014-11-05 11:17:43 -05003159 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00003160}
David McGrew0cb86ee2006-07-07 15:46:57 +00003161
3162
Iñaki Baz Castillo241fec32014-08-21 00:51:00 +02003163/*
3164 * user data within srtp_t context
3165 */
3166
3167void
3168srtp_set_user_data(srtp_t ctx, void *data) {
3169 ctx->user_data = data;
3170}
3171
3172void*
3173srtp_get_user_data(srtp_t ctx) {
3174 return ctx->user_data;
3175}
3176
David McGrew0cb86ee2006-07-07 15:46:57 +00003177
3178/*
3179 * dtls keying for srtp
3180 */
3181
jfigus857009c2014-11-05 11:17:43 -05003182srtp_err_status_t
3183srtp_crypto_policy_set_from_profile_for_rtp(srtp_crypto_policy_t *policy,
3184 srtp_profile_t profile) {
David McGrew0cb86ee2006-07-07 15:46:57 +00003185
3186 /* set SRTP policy from the SRTP profile in the key set */
3187 switch(profile) {
3188 case srtp_profile_aes128_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003189 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003190 break;
3191 case srtp_profile_aes128_cm_sha1_32:
jfigus857009c2014-11-05 11:17:43 -05003192 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003193 break;
3194 case srtp_profile_null_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003195 srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003196 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003197 case srtp_profile_aes256_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003198 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003199 break;
3200 case srtp_profile_aes256_cm_sha1_32:
jfigus857009c2014-11-05 11:17:43 -05003201 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003202 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003203 /* the following profiles are not (yet) supported */
3204 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003205 default:
jfigus857009c2014-11-05 11:17:43 -05003206 return srtp_err_status_bad_param;
David McGrew0cb86ee2006-07-07 15:46:57 +00003207 }
3208
jfigus857009c2014-11-05 11:17:43 -05003209 return srtp_err_status_ok;
David McGrew0cb86ee2006-07-07 15:46:57 +00003210}
3211
jfigus857009c2014-11-05 11:17:43 -05003212srtp_err_status_t
3213srtp_crypto_policy_set_from_profile_for_rtcp(srtp_crypto_policy_t *policy,
3214 srtp_profile_t profile) {
David McGrew0cb86ee2006-07-07 15:46:57 +00003215
3216 /* set SRTP policy from the SRTP profile in the key set */
3217 switch(profile) {
3218 case srtp_profile_aes128_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003219 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003220 break;
3221 case srtp_profile_aes128_cm_sha1_32:
jfigus0acbb032013-05-30 16:47:02 -04003222 /* We do not honor the 32-bit auth tag request since
3223 * this is not compliant with RFC 3711 */
jfigus857009c2014-11-05 11:17:43 -05003224 srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003225 break;
3226 case srtp_profile_null_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003227 srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003228 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003229 case srtp_profile_aes256_cm_sha1_80:
jfigus857009c2014-11-05 11:17:43 -05003230 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003231 break;
3232 case srtp_profile_aes256_cm_sha1_32:
jfigus0acbb032013-05-30 16:47:02 -04003233 /* We do not honor the 32-bit auth tag request since
3234 * this is not compliant with RFC 3711 */
jfigus857009c2014-11-05 11:17:43 -05003235 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003236 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003237 /* the following profiles are not (yet) supported */
3238 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003239 default:
jfigus857009c2014-11-05 11:17:43 -05003240 return srtp_err_status_bad_param;
David McGrew0cb86ee2006-07-07 15:46:57 +00003241 }
3242
jfigus857009c2014-11-05 11:17:43 -05003243 return srtp_err_status_ok;
David McGrew0cb86ee2006-07-07 15:46:57 +00003244}
3245
jfigus857009c2014-11-05 11:17:43 -05003246void 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 +00003247 memcpy(key + bytes_in_key, salt, bytes_in_salt);
David McGrew0cb86ee2006-07-07 15:46:57 +00003248}
3249
3250unsigned int
3251srtp_profile_get_master_key_length(srtp_profile_t profile) {
3252
3253 switch(profile) {
3254 case srtp_profile_aes128_cm_sha1_80:
3255 return 16;
3256 break;
3257 case srtp_profile_aes128_cm_sha1_32:
3258 return 16;
3259 break;
3260 case srtp_profile_null_sha1_80:
3261 return 16;
3262 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003263 case srtp_profile_aes256_cm_sha1_80:
3264 return 32;
3265 break;
3266 case srtp_profile_aes256_cm_sha1_32:
3267 return 32;
3268 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003269 /* the following profiles are not (yet) supported */
3270 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003271 default:
3272 return 0; /* indicate error by returning a zero */
3273 }
3274}
3275
3276unsigned int
3277srtp_profile_get_master_salt_length(srtp_profile_t profile) {
3278
3279 switch(profile) {
3280 case srtp_profile_aes128_cm_sha1_80:
3281 return 14;
3282 break;
3283 case srtp_profile_aes128_cm_sha1_32:
3284 return 14;
3285 break;
3286 case srtp_profile_null_sha1_80:
3287 return 14;
3288 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003289 case srtp_profile_aes256_cm_sha1_80:
3290 return 14;
3291 break;
3292 case srtp_profile_aes256_cm_sha1_32:
3293 return 14;
3294 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003295 /* the following profiles are not (yet) supported */
3296 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003297 default:
3298 return 0; /* indicate error by returning a zero */
3299 }
3300}
jfigus46d6b472014-11-14 16:42:01 -05003301
3302/*
3303 * SRTP debug interface
3304 */
3305srtp_err_status_t srtp_set_debug_module(char *mod_name, int v)
3306{
jfigus92736bc2014-11-21 10:30:54 -05003307 return srtp_crypto_kernel_set_debug_module(mod_name, v);
jfigus46d6b472014-11-14 16:42:01 -05003308}
3309
3310srtp_err_status_t srtp_list_debug_modules(void)
3311{
jfigus92736bc2014-11-21 10:30:54 -05003312 return srtp_crypto_kernel_list_debug_modules();
jfigus46d6b472014-11-14 16:42:01 -05003313}
3314