blob: ea831680ce84a3e8e9dde937b8c8369c0174f3ab [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
45
David McGrew79870d62007-06-15 18:17:39 +000046#include "srtp.h"
47#include "ekt.h" /* for SRTP Encrypted Key Transport */
Cullen Jennings235513a2005-09-21 22:51:36 +000048#include "alloc.h" /* for crypto_alloc() */
jfigus8719f952014-04-08 09:15:49 -040049#ifdef OPENSSL
50#include "aes_gcm_ossl.h" /* for AES GCM mode */
51#endif
Cullen Jennings235513a2005-09-21 22:51:36 +000052
Marcus Sundberg735eb4f2005-10-03 15:31:07 +000053#ifndef SRTP_KERNEL
54# include <limits.h>
55# ifdef HAVE_NETINET_IN_H
56# include <netinet/in.h>
57# elif defined(HAVE_WINSOCK2_H)
58# include <winsock2.h>
59# endif
60#endif /* ! SRTP_KERNEL */
Marcus Sundberge4e34f92005-10-02 20:19:35 +000061
62
Cullen Jennings235513a2005-09-21 22:51:36 +000063/* the debug module for srtp */
64
65debug_module_t mod_srtp = {
66 0, /* debugging is off by default */
67 "srtp" /* printable name for module */
68};
69
70#define octets_in_rtp_header 12
71#define uint32s_in_rtp_header 3
72#define octets_in_rtcp_header 8
73#define uint32s_in_rtcp_header 2
74
Cullen Jennings235513a2005-09-21 22:51:36 +000075
76err_status_t
77srtp_stream_alloc(srtp_stream_ctx_t **str_ptr,
78 const srtp_policy_t *p) {
79 srtp_stream_ctx_t *str;
80 err_status_t stat;
81
82 /*
83 * This function allocates the stream context, rtp and rtcp ciphers
84 * and auth functions, and key limit structure. If there is a
85 * failure during allocation, we free all previously allocated
86 * memory and return a failure code. The code could probably
87 * be improved, but it works and should be clear.
88 */
89
90 /* allocate srtp stream and set str_ptr */
91 str = (srtp_stream_ctx_t *) crypto_alloc(sizeof(srtp_stream_ctx_t));
92 if (str == NULL)
93 return err_status_alloc_fail;
94 *str_ptr = str;
95
96 /* allocate cipher */
97 stat = crypto_kernel_alloc_cipher(p->rtp.cipher_type,
98 &str->rtp_cipher,
jfigusc13c1002014-05-08 13:34:53 -040099 p->rtp.cipher_key_len,
100 p->rtp.auth_tag_len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000101 if (stat) {
102 crypto_free(str);
103 return stat;
104 }
105
106 /* allocate auth function */
107 stat = crypto_kernel_alloc_auth(p->rtp.auth_type,
108 &str->rtp_auth,
109 p->rtp.auth_key_len,
110 p->rtp.auth_tag_len);
111 if (stat) {
112 cipher_dealloc(str->rtp_cipher);
113 crypto_free(str);
114 return stat;
115 }
116
117 /* allocate key limit structure */
Derek MacDonald17127da2006-07-12 22:22:08 +0000118 str->limit = (key_limit_ctx_t*) crypto_alloc(sizeof(key_limit_ctx_t));
Cullen Jennings235513a2005-09-21 22:51:36 +0000119 if (str->limit == NULL) {
120 auth_dealloc(str->rtp_auth);
121 cipher_dealloc(str->rtp_cipher);
122 crypto_free(str);
123 return err_status_alloc_fail;
124 }
125
126 /*
127 * ...and now the RTCP-specific initialization - first, allocate
128 * the cipher
129 */
130 stat = crypto_kernel_alloc_cipher(p->rtcp.cipher_type,
131 &str->rtcp_cipher,
jfigusc13c1002014-05-08 13:34:53 -0400132 p->rtcp.cipher_key_len,
133 p->rtcp.auth_tag_len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000134 if (stat) {
135 auth_dealloc(str->rtp_auth);
136 cipher_dealloc(str->rtp_cipher);
137 crypto_free(str->limit);
138 crypto_free(str);
139 return stat;
140 }
141
142 /* allocate auth function */
143 stat = crypto_kernel_alloc_auth(p->rtcp.auth_type,
144 &str->rtcp_auth,
145 p->rtcp.auth_key_len,
146 p->rtcp.auth_tag_len);
147 if (stat) {
148 cipher_dealloc(str->rtcp_cipher);
149 auth_dealloc(str->rtp_auth);
150 cipher_dealloc(str->rtp_cipher);
151 crypto_free(str->limit);
152 crypto_free(str);
153 return stat;
154 }
155
David McGrew79870d62007-06-15 18:17:39 +0000156 /* allocate ekt data associated with stream */
157 stat = ekt_alloc(&str->ekt, p->ekt);
158 if (stat) {
159 auth_dealloc(str->rtcp_auth);
160 cipher_dealloc(str->rtcp_cipher);
161 auth_dealloc(str->rtp_auth);
162 cipher_dealloc(str->rtp_cipher);
163 crypto_free(str->limit);
164 crypto_free(str);
165 return stat;
166 }
167
Cullen Jennings235513a2005-09-21 22:51:36 +0000168 return err_status_ok;
169}
170
171err_status_t
172srtp_stream_dealloc(srtp_t session, srtp_stream_ctx_t *stream) {
173 err_status_t status;
174
175 /*
176 * we use a conservative deallocation strategy - if any deallocation
177 * fails, then we report that fact without trying to deallocate
178 * anything else
179 */
180
181 /* deallocate cipher, if it is not the same as that in template */
David McGrewfec49dd2005-09-23 19:34:11 +0000182 if (session->stream_template
Cullen Jennings235513a2005-09-21 22:51:36 +0000183 && stream->rtp_cipher == session->stream_template->rtp_cipher) {
184 /* do nothing */
185 } else {
186 status = cipher_dealloc(stream->rtp_cipher);
187 if (status)
David McGrewfec49dd2005-09-23 19:34:11 +0000188 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000189 }
190
191 /* deallocate auth function, if it is not the same as that in template */
192 if (session->stream_template
193 && stream->rtp_auth == session->stream_template->rtp_auth) {
194 /* do nothing */
195 } else {
196 status = auth_dealloc(stream->rtp_auth);
197 if (status)
198 return status;
199 }
200
David McGrewfec49dd2005-09-23 19:34:11 +0000201 /* deallocate key usage limit, if it is not the same as that in template */
202 if (session->stream_template
203 && stream->limit == session->stream_template->limit) {
204 /* do nothing */
205 } else {
Cullen Jennings235513a2005-09-21 22:51:36 +0000206 crypto_free(stream->limit);
David McGrewfec49dd2005-09-23 19:34:11 +0000207 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000208
209 /*
210 * deallocate rtcp cipher, if it is not the same as that in
211 * template
212 */
213 if (session->stream_template
214 && stream->rtcp_cipher == session->stream_template->rtcp_cipher) {
215 /* do nothing */
216 } else {
217 status = cipher_dealloc(stream->rtcp_cipher);
218 if (status)
219 return status;
220 }
221
222 /*
223 * deallocate rtcp auth function, if it is not the same as that in
224 * template
225 */
226 if (session->stream_template
227 && stream->rtcp_auth == session->stream_template->rtcp_auth) {
228 /* do nothing */
229 } else {
230 status = auth_dealloc(stream->rtcp_auth);
231 if (status)
232 return status;
233 }
David McGrew79870d62007-06-15 18:17:39 +0000234
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000235 status = rdbx_dealloc(&stream->rtp_rdbx);
236 if (status)
237 return status;
238
David McGrew79870d62007-06-15 18:17:39 +0000239 /* DAM - need to deallocate EKT here */
jfigus8c36da22013-10-01 16:41:19 -0400240
241 /*
242 * zeroize the salt value
243 */
244 memset(stream->salt, 0, SRTP_AEAD_SALT_LEN);
245 memset(stream->c_salt, 0, SRTP_AEAD_SALT_LEN);
246
Cullen Jennings235513a2005-09-21 22:51:36 +0000247
248 /* deallocate srtp stream context */
249 crypto_free(stream);
250
251 return err_status_ok;
252}
253
254
255/*
256 * srtp_stream_clone(stream_template, new) allocates a new stream and
257 * initializes it using the cipher and auth of the stream_template
258 *
259 * the only unique data in a cloned stream is the replay database and
260 * the SSRC
261 */
262
263err_status_t
264srtp_stream_clone(const srtp_stream_ctx_t *stream_template,
265 uint32_t ssrc,
266 srtp_stream_ctx_t **str_ptr) {
267 err_status_t status;
268 srtp_stream_ctx_t *str;
269
270 debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ssrc);
271
272 /* allocate srtp stream and set str_ptr */
273 str = (srtp_stream_ctx_t *) crypto_alloc(sizeof(srtp_stream_ctx_t));
274 if (str == NULL)
275 return err_status_alloc_fail;
276 *str_ptr = str;
277
278 /* set cipher and auth pointers to those of the template */
279 str->rtp_cipher = stream_template->rtp_cipher;
280 str->rtp_auth = stream_template->rtp_auth;
281 str->rtcp_cipher = stream_template->rtcp_cipher;
282 str->rtcp_auth = stream_template->rtcp_auth;
283
284 /* set key limit to point to that of the template */
285 status = key_limit_clone(stream_template->limit, &str->limit);
jfigus8c36da22013-10-01 16:41:19 -0400286 if (status) {
287 crypto_free(*str_ptr);
288 *str_ptr = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +0000289 return status;
jfigus8c36da22013-10-01 16:41:19 -0400290 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000291
292 /* initialize replay databases */
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000293 status = rdbx_init(&str->rtp_rdbx,
294 rdbx_get_window_size(&stream_template->rtp_rdbx));
jfigus8c36da22013-10-01 16:41:19 -0400295 if (status) {
296 crypto_free(*str_ptr);
297 *str_ptr = NULL;
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000298 return status;
jfigus8c36da22013-10-01 16:41:19 -0400299 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000300 rdb_init(&str->rtcp_rdb);
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000301 str->allow_repeat_tx = stream_template->allow_repeat_tx;
Cullen Jennings235513a2005-09-21 22:51:36 +0000302
303 /* set ssrc to that provided */
304 str->ssrc = ssrc;
305
306 /* set direction and security services */
307 str->direction = stream_template->direction;
308 str->rtp_services = stream_template->rtp_services;
309 str->rtcp_services = stream_template->rtcp_services;
310
David McGrew79870d62007-06-15 18:17:39 +0000311 /* set pointer to EKT data associated with stream */
312 str->ekt = stream_template->ekt;
313
jfigus8c36da22013-10-01 16:41:19 -0400314 /* Copy the salt values */
315 memcpy(str->salt, stream_template->salt, SRTP_AEAD_SALT_LEN);
316 memcpy(str->c_salt, stream_template->c_salt, SRTP_AEAD_SALT_LEN);
317
Cullen Jennings235513a2005-09-21 22:51:36 +0000318 /* defensive coding */
319 str->next = NULL;
320
321 return err_status_ok;
322}
323
324
325/*
326 * key derivation functions, internal to libSRTP
327 *
328 * srtp_kdf_t is a key derivation context
329 *
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000330 * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher
331 * described by cipher_id, with the master key k with length in octets keylen.
Cullen Jennings235513a2005-09-21 22:51:36 +0000332 *
333 * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key
334 * corresponding to label l and puts it into kl; the length
335 * of the key in octets is provided as keylen. this function
336 * should be called once for each subkey that is derived.
337 *
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000338 * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state
Cullen Jennings235513a2005-09-21 22:51:36 +0000339 */
340
341typedef enum {
342 label_rtp_encryption = 0x00,
343 label_rtp_msg_auth = 0x01,
344 label_rtp_salt = 0x02,
345 label_rtcp_encryption = 0x03,
346 label_rtcp_msg_auth = 0x04,
347 label_rtcp_salt = 0x05
348} srtp_prf_label;
349
350
351/*
352 * srtp_kdf_t represents a key derivation function. The SRTP
353 * default KDF is the only one implemented at present.
354 */
355
356typedef struct {
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000357 cipher_t *cipher; /* cipher used for key derivation */
Cullen Jennings235513a2005-09-21 22:51:36 +0000358} srtp_kdf_t;
359
360err_status_t
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000361srtp_kdf_init(srtp_kdf_t *kdf, cipher_type_id_t cipher_id, const uint8_t *key, int length) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000362
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000363 err_status_t stat;
jfigusc13c1002014-05-08 13:34:53 -0400364 stat = crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, length, 0);
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000365 if (stat)
366 return stat;
367
jfigus7882dd92013-08-02 16:08:23 -0400368 stat = cipher_init(kdf->cipher, key);
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000369 if (stat) {
370 cipher_dealloc(kdf->cipher);
371 return stat;
372 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000373
374 return err_status_ok;
375}
376
377err_status_t
378srtp_kdf_generate(srtp_kdf_t *kdf, srtp_prf_label label,
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000379 uint8_t *key, unsigned length) {
Cullen Jennings235513a2005-09-21 22:51:36 +0000380
381 v128_t nonce;
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000382 err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000383
384 /* set eigth octet of nonce to <label>, set the rest of it to zero */
385 v128_set_to_zero(&nonce);
Marcus Sundberg7627bc52005-10-08 16:38:06 +0000386 nonce.v8[7] = label;
Cullen Jennings235513a2005-09-21 22:51:36 +0000387
jfigus7882dd92013-08-02 16:08:23 -0400388 status = cipher_set_iv(kdf->cipher, &nonce, direction_encrypt);
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000389 if (status)
390 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000391
392 /* generate keystream output */
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000393 octet_string_set_to_zero(key, length);
394 status = cipher_encrypt(kdf->cipher, key, &length);
395 if (status)
396 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000397
398 return err_status_ok;
399}
400
401err_status_t
402srtp_kdf_clear(srtp_kdf_t *kdf) {
Jonathan Lennox953f46f2010-05-18 16:35:09 +0000403 err_status_t status;
404 status = cipher_dealloc(kdf->cipher);
405 if (status)
406 return status;
407 kdf->cipher = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +0000408
409 return err_status_ok;
410}
411
412/*
413 * end of key derivation functions
414 */
415
416#define MAX_SRTP_KEY_LEN 256
417
418
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000419/* Get the base key length corresponding to a given combined key+salt
420 * length for the given cipher.
421 * Assumption is that for AES-ICM a key length < 30 is Ismacryp using
422 * AES-128 and short salts; everything else uses a salt length of 14.
423 * TODO: key and salt lengths should be separate fields in the policy. */
Jonathan Lennoxe2774db2011-10-27 16:06:12 +0000424static inline int base_key_length(const cipher_type_t *cipher, int key_length)
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000425{
jfigus8c36da22013-10-01 16:41:19 -0400426 switch (cipher->id) {
427 case AES_128_ICM:
428 case AES_192_ICM:
429 case AES_256_ICM:
430 /* The legacy modes are derived from
431 * the configured key length on the policy */
432 return key_length - 14;
433 break;
434 case AES_128_GCM:
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000435 return 16;
jfigus8c36da22013-10-01 16:41:19 -0400436 break;
437 case AES_256_GCM:
438 return 32;
439 break;
440 default:
441 return key_length;
442 break;
443 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000444}
445
David McGrew576e1482006-06-09 21:47:44 +0000446err_status_t
447srtp_stream_init_keys(srtp_stream_ctx_t *srtp, const void *key) {
448 err_status_t stat;
449 srtp_kdf_t kdf;
450 uint8_t tmp_key[MAX_SRTP_KEY_LEN];
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000451 int kdf_keylen = 30, rtp_keylen, rtcp_keylen;
452 int rtp_base_key_len, rtp_salt_len;
453 int rtcp_base_key_len, rtcp_salt_len;
454
455 /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */
456 /* TODO: kdf algorithm, master key length, and master salt length should
457 * be part of srtp_policy_t. */
458 rtp_keylen = cipher_get_key_length(srtp->rtp_cipher);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000459 rtcp_keylen = cipher_get_key_length(srtp->rtcp_cipher);
jfigus8719f952014-04-08 09:15:49 -0400460 rtp_base_key_len = base_key_length(srtp->rtp_cipher->type, rtp_keylen);
461 rtp_salt_len = rtp_keylen - rtp_base_key_len;
462
463 if (rtp_keylen > kdf_keylen) {
464 kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
465 }
466
467 if (rtcp_keylen > kdf_keylen) {
468 kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
469 }
470
jfigus8c36da22013-10-01 16:41:19 -0400471 debug_print(mod_srtp, "srtp key len: %d", rtp_keylen);
472 debug_print(mod_srtp, "srtcp key len: %d", rtcp_keylen);
jfigus8719f952014-04-08 09:15:49 -0400473 debug_print(mod_srtp, "base key len: %d", rtp_base_key_len);
474 debug_print(mod_srtp, "kdf key len: %d", kdf_keylen);
475 debug_print(mod_srtp, "rtp salt len: %d", rtp_salt_len);
476
477 /*
478 * Make sure the key given to us is 'zero' appended. GCM
479 * mode uses a shorter master SALT (96 bits), but still relies on
480 * the legacy CTR mode KDF, which uses a 112 bit master SALT.
481 */
482 memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN);
483 memcpy(tmp_key, key, (rtp_base_key_len + rtp_salt_len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000484
David McGrew576e1482006-06-09 21:47:44 +0000485 /* initialize KDF state */
jfigus8719f952014-04-08 09:15:49 -0400486 stat = srtp_kdf_init(&kdf, AES_ICM, (const uint8_t *)tmp_key, kdf_keylen);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000487 if (stat) {
488 return err_status_init_fail;
489 }
David McGrew576e1482006-06-09 21:47:44 +0000490
491 /* generate encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000492 stat = srtp_kdf_generate(&kdf, label_rtp_encryption,
493 tmp_key, rtp_base_key_len);
494 if (stat) {
495 /* zeroize temp buffer */
496 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
497 return err_status_init_fail;
498 }
jfigus8719f952014-04-08 09:15:49 -0400499 debug_print(mod_srtp, "cipher key: %s",
500 octet_string_hex_string(tmp_key, rtp_base_key_len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000501
David McGrew576e1482006-06-09 21:47:44 +0000502 /*
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000503 * if the cipher in the srtp context uses a salt, then we need
David McGrew576e1482006-06-09 21:47:44 +0000504 * to generate the salt value
505 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000506 if (rtp_salt_len > 0) {
507 debug_print(mod_srtp, "found rtp_salt_len > 0, generating salt", NULL);
David McGrew576e1482006-06-09 21:47:44 +0000508
509 /* generate encryption salt, put after encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000510 stat = srtp_kdf_generate(&kdf, label_rtp_salt,
511 tmp_key + rtp_base_key_len, rtp_salt_len);
512 if (stat) {
513 /* zeroize temp buffer */
514 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
515 return err_status_init_fail;
516 }
jfigus8c36da22013-10-01 16:41:19 -0400517 memcpy(srtp->salt, tmp_key + rtp_base_key_len, SRTP_AEAD_SALT_LEN);
David McGrew576e1482006-06-09 21:47:44 +0000518 }
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000519 if (rtp_salt_len > 0) {
520 debug_print(mod_srtp, "cipher salt: %s",
521 octet_string_hex_string(tmp_key + rtp_base_key_len, rtp_salt_len));
522 }
David McGrew576e1482006-06-09 21:47:44 +0000523
524 /* initialize cipher */
jfigus7882dd92013-08-02 16:08:23 -0400525 stat = cipher_init(srtp->rtp_cipher, tmp_key);
David McGrew576e1482006-06-09 21:47:44 +0000526 if (stat) {
527 /* zeroize temp buffer */
528 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
529 return err_status_init_fail;
530 }
531
532 /* generate authentication key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000533 stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth,
534 tmp_key, auth_get_key_length(srtp->rtp_auth));
535 if (stat) {
536 /* zeroize temp buffer */
537 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
538 return err_status_init_fail;
539 }
David McGrew576e1482006-06-09 21:47:44 +0000540 debug_print(mod_srtp, "auth key: %s",
541 octet_string_hex_string(tmp_key,
542 auth_get_key_length(srtp->rtp_auth)));
543
544 /* initialize auth function */
545 stat = auth_init(srtp->rtp_auth, tmp_key);
546 if (stat) {
547 /* zeroize temp buffer */
548 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
549 return err_status_init_fail;
550 }
551
552 /*
553 * ...now initialize SRTCP keys
554 */
555
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000556 rtcp_base_key_len = base_key_length(srtp->rtcp_cipher->type, rtcp_keylen);
557 rtcp_salt_len = rtcp_keylen - rtcp_base_key_len;
jfigus8c36da22013-10-01 16:41:19 -0400558 debug_print(mod_srtp, "rtcp salt len: %d", rtcp_salt_len);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000559
David McGrew576e1482006-06-09 21:47:44 +0000560 /* generate encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000561 stat = srtp_kdf_generate(&kdf, label_rtcp_encryption,
562 tmp_key, rtcp_base_key_len);
563 if (stat) {
564 /* zeroize temp buffer */
565 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
566 return err_status_init_fail;
567 }
568
David McGrew576e1482006-06-09 21:47:44 +0000569 /*
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000570 * if the cipher in the srtp context uses a salt, then we need
David McGrew576e1482006-06-09 21:47:44 +0000571 * to generate the salt value
572 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000573 if (rtcp_salt_len > 0) {
574 debug_print(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt",
575 NULL);
David McGrew576e1482006-06-09 21:47:44 +0000576
577 /* generate encryption salt, put after encryption key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000578 stat = srtp_kdf_generate(&kdf, label_rtcp_salt,
579 tmp_key + rtcp_base_key_len, rtcp_salt_len);
580 if (stat) {
581 /* zeroize temp buffer */
582 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
583 return err_status_init_fail;
584 }
jfigus8c36da22013-10-01 16:41:19 -0400585 memcpy(srtp->c_salt, tmp_key + rtcp_base_key_len, SRTP_AEAD_SALT_LEN);
David McGrew576e1482006-06-09 21:47:44 +0000586 }
587 debug_print(mod_srtp, "rtcp cipher key: %s",
Jonathan Lennoxc0f1f1b2012-04-26 23:16:00 +0000588 octet_string_hex_string(tmp_key, rtcp_base_key_len));
589 if (rtcp_salt_len > 0) {
590 debug_print(mod_srtp, "rtcp cipher salt: %s",
591 octet_string_hex_string(tmp_key + rtcp_base_key_len, rtcp_salt_len));
592 }
David McGrew576e1482006-06-09 21:47:44 +0000593
594 /* initialize cipher */
jfigus7882dd92013-08-02 16:08:23 -0400595 stat = cipher_init(srtp->rtcp_cipher, tmp_key);
David McGrew576e1482006-06-09 21:47:44 +0000596 if (stat) {
597 /* zeroize temp buffer */
598 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
599 return err_status_init_fail;
600 }
601
602 /* generate authentication key */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000603 stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth,
604 tmp_key, auth_get_key_length(srtp->rtcp_auth));
605 if (stat) {
606 /* zeroize temp buffer */
607 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
608 return err_status_init_fail;
609 }
610
David McGrew576e1482006-06-09 21:47:44 +0000611 debug_print(mod_srtp, "rtcp auth key: %s",
612 octet_string_hex_string(tmp_key,
613 auth_get_key_length(srtp->rtcp_auth)));
614
615 /* initialize auth function */
616 stat = auth_init(srtp->rtcp_auth, tmp_key);
617 if (stat) {
618 /* zeroize temp buffer */
619 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
620 return err_status_init_fail;
621 }
622
623 /* clear memory then return */
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000624 stat = srtp_kdf_clear(&kdf);
David McGrew576e1482006-06-09 21:47:44 +0000625 octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000626 if (stat)
627 return err_status_init_fail;
David McGrew576e1482006-06-09 21:47:44 +0000628
629 return err_status_ok;
630}
Cullen Jennings235513a2005-09-21 22:51:36 +0000631
632err_status_t
633srtp_stream_init(srtp_stream_ctx_t *srtp,
634 const srtp_policy_t *p) {
David McGrew576e1482006-06-09 21:47:44 +0000635 err_status_t err;
Cullen Jennings235513a2005-09-21 22:51:36 +0000636
637 debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)",
638 p->ssrc.value);
639
640 /* initialize replay database */
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000641 /* window size MUST be at least 64. MAY be larger. Values more than
642 * 2^15 aren't meaningful due to how extended sequence numbers are
643 * calculated. Let a window size of 0 imply the default value. */
644
645 if (p->window_size != 0 && (p->window_size < 64 || p->window_size >= 0x8000))
646 return err_status_bad_param;
647
648 if (p->window_size != 0)
649 err = rdbx_init(&srtp->rtp_rdbx, p->window_size);
650 else
651 err = rdbx_init(&srtp->rtp_rdbx, 128);
652 if (err) return err;
Cullen Jennings235513a2005-09-21 22:51:36 +0000653
654 /* initialize key limit to maximum value */
Marcus Sundberge4e34f92005-10-02 20:19:35 +0000655#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +0000656{
Marcus Sundberge4e34f92005-10-02 20:19:35 +0000657 uint64_t temp;
658 temp = make64(UINT_MAX,UINT_MAX);
659 key_limit_set(srtp->limit, temp);
Cullen Jennings235513a2005-09-21 22:51:36 +0000660}
661#else
662 key_limit_set(srtp->limit, 0xffffffffffffLL);
663#endif
664
665 /* set the SSRC value */
666 srtp->ssrc = htonl(p->ssrc.value);
667
668 /* set the security service flags */
669 srtp->rtp_services = p->rtp.sec_serv;
670 srtp->rtcp_services = p->rtcp.sec_serv;
671
672 /*
673 * set direction to unknown - this flag gets checked in srtp_protect(),
674 * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and
675 * gets set appropriately if it is set to unknown.
676 */
677 srtp->direction = dir_unknown;
678
David McGrew576e1482006-06-09 21:47:44 +0000679 /* initialize SRTCP replay database */
Cullen Jennings235513a2005-09-21 22:51:36 +0000680 rdb_init(&srtp->rtcp_rdb);
681
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000682 /* initialize allow_repeat_tx */
683 /* guard against uninitialized memory: allow only 0 or 1 here */
684 if (p->allow_repeat_tx != 0 && p->allow_repeat_tx != 1) {
685 rdbx_dealloc(&srtp->rtp_rdbx);
686 return err_status_bad_param;
687 }
688 srtp->allow_repeat_tx = p->allow_repeat_tx;
689
Cullen Jennings235513a2005-09-21 22:51:36 +0000690 /* DAM - no RTCP key limit at present */
691
David McGrew576e1482006-06-09 21:47:44 +0000692 /* initialize keys */
693 err = srtp_stream_init_keys(srtp, p->key);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000694 if (err) {
695 rdbx_dealloc(&srtp->rtp_rdbx);
696 return err;
697 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000698
David McGrew79870d62007-06-15 18:17:39 +0000699 /*
700 * if EKT is in use, then initialize the EKT data associated with
701 * the stream
702 */
703 err = ekt_stream_init_from_policy(srtp->ekt, p->ekt);
Jonathan Lennoxa1242f82010-05-17 21:46:04 +0000704 if (err) {
705 rdbx_dealloc(&srtp->rtp_rdbx);
706 return err;
707 }
David McGrew79870d62007-06-15 18:17:39 +0000708
Cullen Jennings235513a2005-09-21 22:51:36 +0000709 return err_status_ok;
710 }
711
712
713 /*
714 * srtp_event_reporter is an event handler function that merely
715 * reports the events that are reported by the callbacks
716 */
717
718 void
719 srtp_event_reporter(srtp_event_data_t *data) {
720
721 err_report(err_level_warning, "srtp: in stream 0x%x: ",
722 data->stream->ssrc);
723
724 switch(data->event) {
725 case event_ssrc_collision:
726 err_report(err_level_warning, "\tSSRC collision\n");
727 break;
728 case event_key_soft_limit:
729 err_report(err_level_warning, "\tkey usage soft limit reached\n");
730 break;
731 case event_key_hard_limit:
732 err_report(err_level_warning, "\tkey usage hard limit reached\n");
733 break;
734 case event_packet_index_limit:
735 err_report(err_level_warning, "\tpacket index limit reached\n");
736 break;
737 default:
738 err_report(err_level_warning, "\tunknown event reported to handler\n");
739 }
740 }
741
742 /*
743 * srtp_event_handler is a global variable holding a pointer to the
744 * event handler function; this function is called for any unexpected
745 * event that needs to be handled out of the SRTP data path. see
746 * srtp_event_t in srtp.h for more info
747 *
748 * it is okay to set srtp_event_handler to NULL, but we set
749 * it to the srtp_event_reporter.
750 */
751
752 static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter;
753
754 err_status_t
755 srtp_install_event_handler(srtp_event_handler_func_t func) {
756
757 /*
758 * note that we accept NULL arguments intentionally - calling this
759 * function with a NULL arguments removes an event handler that's
760 * been previously installed
761 */
762
763 /* set global event handling function */
764 srtp_event_handler = func;
765 return err_status_ok;
766 }
767
jfigus8c36da22013-10-01 16:41:19 -0400768/*
769 * AEAD uses a new IV formation method. This function implements
770 * section 9.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The
771 * calculation is defined as, where (+) is the xor operation:
772 *
773 *
774 * 0 0 0 0 0 0 0 0 0 0 1 1
775 * 0 1 2 3 4 5 6 7 8 9 0 1
776 * +--+--+--+--+--+--+--+--+--+--+--+--+
777 * |00|00| SSRC | ROC | SEQ |---+
778 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
779 * |
780 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
781 * | Encryption Salt |->(+)
782 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
783 * |
784 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
785 * | Initialization Vector |<--+
786 * +--+--+--+--+--+--+--+--+--+--+--+--+*
787 *
788 * Input: *stream - pointer to SRTP stream context, used to retrieve
789 * the SALT
790 * *iv - Pointer to receive the calculated IV
791 * *seq - The ROC and SEQ value to use for the
792 * IV calculation.
793 * *hdr - The RTP header, used to get the SSRC value
794 *
795 */
796static void srtp_calc_aead_iv(srtp_stream_ctx_t *stream, v128_t *iv,
797 xtd_seq_num_t *seq, srtp_hdr_t *hdr)
798{
799 v128_t in;
800 v128_t salt;
Dmitry Sobinov367d5d32014-03-27 22:36:32 +0700801
802#ifdef NO_64BIT_MATH
803 uint32_t local_roc = ((high32(*seq) << 16) |
804 (low32(*seq) >> 16));
805 uint16_t local_seq = (uint16_t) (low32(*seq));
806#else
807 uint32_t local_roc = (uint32_t)(*seq >> 16);
808 uint16_t local_seq = (uint16_t) *seq;
809#endif
jfigus8c36da22013-10-01 16:41:19 -0400810
811 memset(&in, 0, sizeof(v128_t));
812 memset(&salt, 0, sizeof(v128_t));
813
Dmitry Sobinov367d5d32014-03-27 22:36:32 +0700814 in.v16[5] = htons(local_seq);
815 local_roc = htonl(local_roc);
816 memcpy(&in.v16[3], &local_roc, sizeof(local_roc));
jfigus8c36da22013-10-01 16:41:19 -0400817
818 /*
819 * Copy in the RTP SSRC value
820 */
821 memcpy(&in.v8[2], &hdr->ssrc, 4);
822 debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in));
823
824 /*
825 * Get the SALT value from the context
826 */
827 memcpy(salt.v8, stream->salt, SRTP_AEAD_SALT_LEN);
828 debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt));
829
830 /*
831 * Finally, apply tyhe SALT to the input
832 */
833 v128_xor(iv, &in, &salt);
834}
835
836
837/*
838 * This function handles outgoing SRTP packets while in AEAD mode,
839 * which currently supports AES-GCM encryption. All packets are
840 * encrypted and authenticated.
841 */
842static err_status_t
843srtp_protect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream,
844 void *rtp_hdr, int *pkt_octet_len)
845{
846 srtp_hdr_t *hdr = (srtp_hdr_t*)rtp_hdr;
847 uint32_t *enc_start; /* pointer to start of encrypted portion */
848 unsigned enc_octet_len = 0; /* number of octets in encrypted portion */
849 xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
850 int delta; /* delta of local pkt idx and that in hdr */
851 err_status_t status;
852 int tag_len;
853 v128_t iv;
854 unsigned int aad_len;
855
856 debug_print(mod_srtp, "function srtp_protect_aead", NULL);
857
858 /*
859 * update the key usage limit, and check it to make sure that we
860 * didn't just hit either the soft limit or the hard limit, and call
861 * the event handler if we hit either.
862 */
863 switch (key_limit_update(stream->limit)) {
864 case key_event_normal:
865 break;
866 case key_event_hard_limit:
867 srtp_handle_event(ctx, stream, event_key_hard_limit);
868 return err_status_key_expired;
869 case key_event_soft_limit:
870 default:
871 srtp_handle_event(ctx, stream, event_key_soft_limit);
872 break;
873 }
874
875 /* get tag length from stream */
876 tag_len = auth_get_tag_length(stream->rtp_auth);
877
878 /*
879 * find starting point for encryption and length of data to be
880 * encrypted - the encrypted portion starts after the rtp header
881 * extension, if present; otherwise, it starts after the last csrc,
882 * if any are present
883 *
884 * if we're not providing confidentiality, set enc_start to NULL
885 */
886 if (stream->rtp_services & sec_serv_conf) {
887 enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc;
888 if (hdr->x == 1) {
889 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t*)enc_start;
890 enc_start += (ntohs(xtn_hdr->length) + 1);
891 }
892 enc_octet_len = (unsigned int)(*pkt_octet_len -
893 ((enc_start - (uint32_t*)hdr) << 2));
894 } else {
895 enc_start = NULL;
896 }
897
898 /*
899 * estimate the packet index using the start of the replay window
900 * and the sequence number from the header
901 */
902 delta = rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
903 status = rdbx_check(&stream->rtp_rdbx, delta);
904 if (status) {
905 if (status != err_status_replay_fail || !stream->allow_repeat_tx) {
906 return status; /* we've been asked to reuse an index */
907 }
908 } else {
909 rdbx_add_index(&stream->rtp_rdbx, delta);
910 }
911
912#ifdef NO_64BIT_MATH
913 debug_print2(mod_srtp, "estimated packet index: %08x%08x",
914 high32(est), low32(est));
915#else
916 debug_print(mod_srtp, "estimated packet index: %016llx", est);
917#endif
918
919 /*
920 * AEAD uses a new IV formation method
921 */
922 srtp_calc_aead_iv(stream, &iv, &est, hdr);
923 status = cipher_set_iv(stream->rtp_cipher, &iv, direction_encrypt);
924 if (status) {
925 return err_status_cipher_fail;
926 }
927
928 /* shift est, put into network byte order */
929#ifdef NO_64BIT_MATH
930 est = be64_to_cpu(make64((high32(est) << 16) |
931 (low32(est) >> 16),
932 low32(est) << 16));
933#else
934 est = be64_to_cpu(est << 16);
935#endif
936
937 /*
938 * Set the AAD over the RTP header
939 */
940 aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
941 status = cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
942 if (status) {
943 return ( err_status_cipher_fail);
944 }
945
946 /* Encrypt the payload */
947 status = cipher_encrypt(stream->rtp_cipher,
948 (uint8_t*)enc_start, &enc_octet_len);
949 if (status) {
950 return err_status_cipher_fail;
951 }
952 /*
953 * If we're doing GCM, we need to get the tag
954 * and append that to the output
955 */
956 status = cipher_get_tag(stream->rtp_cipher,
957 (uint8_t*)enc_start+enc_octet_len, &tag_len);
958 if (status) {
959 return ( err_status_cipher_fail);
960 }
961 enc_octet_len += tag_len;
962
963 /* increase the packet length by the length of the auth tag */
964 *pkt_octet_len += tag_len;
965
966 return err_status_ok;
967}
968
969
970/*
971 * This function handles incoming SRTP packets while in AEAD mode,
972 * which currently supports AES-GCM encryption. All packets are
973 * encrypted and authenticated. Note, the auth tag is at the end
974 * of the packet stream and is automatically checked by GCM
975 * when decrypting the payload.
976 */
977static err_status_t
978srtp_unprotect_aead (srtp_ctx_t *ctx, srtp_stream_ctx_t *stream, int delta,
979 xtd_seq_num_t est, void *srtp_hdr, int *pkt_octet_len)
980{
981 srtp_hdr_t *hdr = (srtp_hdr_t*)srtp_hdr;
982 uint32_t *enc_start; /* pointer to start of encrypted portion */
983 unsigned enc_octet_len = 0; /* number of octets in encrypted portion */
984 v128_t iv;
985 err_status_t status;
986 int tag_len;
987 unsigned int aad_len;
988
989 debug_print(mod_srtp, "function srtp_unprotect_aead", NULL);
990
991#ifdef NO_64BIT_MATH
992 debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est), low32(est));
993#else
994 debug_print(mod_srtp, "estimated u_packet index: %016llx", est);
995#endif
996
997 /* get tag length from stream */
998 tag_len = auth_get_tag_length(stream->rtp_auth);
999
1000 /*
1001 * AEAD uses a new IV formation method
1002 */
1003 srtp_calc_aead_iv(stream, &iv, &est, hdr);
1004 status = cipher_set_iv(stream->rtp_cipher, &iv, direction_decrypt);
1005 if (status) {
1006 return err_status_cipher_fail;
1007 }
1008
1009 /*
1010 * find starting point for decryption and length of data to be
1011 * decrypted - the encrypted portion starts after the rtp header
1012 * extension, if present; otherwise, it starts after the last csrc,
1013 * if any are present
1014 */
1015 enc_start = (uint32_t*)hdr + uint32s_in_rtp_header + hdr->cc;
1016 if (hdr->x == 1) {
1017 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t*)enc_start;
1018 enc_start += (ntohs(xtn_hdr->length) + 1);
1019 }
1020 /*
1021 * We pass the tag down to the cipher when doing GCM mode
1022 */
1023 enc_octet_len = (unsigned int) *pkt_octet_len -
1024 ((enc_start - (uint32_t *)hdr) << 2);
1025
1026 /*
jfigusc13c1002014-05-08 13:34:53 -04001027 * Sanity check the encrypted payload length against
1028 * the tag size. It must always be at least as large
1029 * as the tag length.
1030 */
1031 if (enc_octet_len < tag_len) {
1032 return err_status_cipher_fail;
1033 }
1034
1035 /*
jfigus8c36da22013-10-01 16:41:19 -04001036 * update the key usage limit, and check it to make sure that we
1037 * didn't just hit either the soft limit or the hard limit, and call
1038 * the event handler if we hit either.
1039 */
1040 switch (key_limit_update(stream->limit)) {
1041 case key_event_normal:
1042 break;
1043 case key_event_soft_limit:
1044 srtp_handle_event(ctx, stream, event_key_soft_limit);
1045 break;
1046 case key_event_hard_limit:
1047 srtp_handle_event(ctx, stream, event_key_hard_limit);
1048 return err_status_key_expired;
1049 default:
1050 break;
1051 }
1052
1053 /*
1054 * Set the AAD for AES-GCM, which is the RTP header
1055 */
1056 aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
1057 status = cipher_set_aad(stream->rtp_cipher, (uint8_t*)hdr, aad_len);
1058 if (status) {
1059 return ( err_status_cipher_fail);
1060 }
1061
1062 /* Decrypt the ciphertext. This also checks the auth tag based
1063 * on the AAD we just specified above */
1064 status = cipher_decrypt(stream->rtp_cipher,
1065 (uint8_t*)enc_start, &enc_octet_len);
1066 if (status) {
1067 return status;
1068 }
1069
1070 /*
1071 * verify that stream is for received traffic - this check will
1072 * detect SSRC collisions, since a stream that appears in both
1073 * srtp_protect() and srtp_unprotect() will fail this test in one of
1074 * those functions.
1075 *
1076 * we do this check *after* the authentication check, so that the
1077 * latter check will catch any attempts to fool us into thinking
1078 * that we've got a collision
1079 */
1080 if (stream->direction != dir_srtp_receiver) {
1081 if (stream->direction == dir_unknown) {
1082 stream->direction = dir_srtp_receiver;
1083 } else {
1084 srtp_handle_event(ctx, stream, event_ssrc_collision);
1085 }
1086 }
1087
1088 /*
1089 * if the stream is a 'provisional' one, in which the template context
1090 * is used, then we need to allocate a new stream at this point, since
1091 * the authentication passed
1092 */
1093 if (stream == ctx->stream_template) {
1094 srtp_stream_ctx_t *new_stream;
1095
1096 /*
1097 * allocate and initialize a new stream
1098 *
1099 * note that we indicate failure if we can't allocate the new
1100 * stream, and some implementations will want to not return
1101 * failure here
1102 */
1103 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
1104 if (status) {
1105 return status;
1106 }
1107
1108 /* add new stream to the head of the stream_list */
1109 new_stream->next = ctx->stream_list;
1110 ctx->stream_list = new_stream;
1111
1112 /* set stream (the pointer used in this function) */
1113 stream = new_stream;
1114 }
1115
1116 /*
1117 * the message authentication function passed, so add the packet
1118 * index into the replay database
1119 */
1120 rdbx_add_index(&stream->rtp_rdbx, delta);
1121
1122 /* decrease the packet length by the length of the auth tag */
1123 *pkt_octet_len -= tag_len;
1124
1125 return err_status_ok;
1126}
1127
1128
1129
1130
Cullen Jennings235513a2005-09-21 22:51:36 +00001131 err_status_t
1132 srtp_protect(srtp_ctx_t *ctx, void *rtp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00001133 srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00001134 uint32_t *enc_start; /* pointer to start of encrypted portion */
1135 uint32_t *auth_start; /* pointer to start of auth. portion */
1136 unsigned enc_octet_len = 0; /* number of octets in encrypted portion */
1137 xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
1138 int delta; /* delta of local pkt idx and that in hdr */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001139 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
Cullen Jennings235513a2005-09-21 22:51:36 +00001140 err_status_t status;
1141 int tag_len;
1142 srtp_stream_ctx_t *stream;
1143 int prefix_len;
1144
1145 debug_print(mod_srtp, "function srtp_protect", NULL);
1146
1147 /* we assume the hdr is 32-bit aligned to start */
1148
1149 /* check the packet length - it must at least contain a full header */
1150 if (*pkt_octet_len < octets_in_rtp_header)
1151 return err_status_bad_param;
1152
1153 /*
1154 * look up ssrc in srtp_stream list, and process the packet with
1155 * the appropriate stream. if we haven't seen this stream before,
1156 * there's a template key for this srtp_session, and the cipher
1157 * supports key-sharing, then we assume that a new stream using
1158 * that key has just started up
1159 */
1160 stream = srtp_get_stream(ctx, hdr->ssrc);
1161 if (stream == NULL) {
1162 if (ctx->stream_template != NULL) {
1163 srtp_stream_ctx_t *new_stream;
1164
1165 /* allocate and initialize a new stream */
1166 status = srtp_stream_clone(ctx->stream_template,
David McGrewfec49dd2005-09-23 19:34:11 +00001167 hdr->ssrc, &new_stream);
Cullen Jennings235513a2005-09-21 22:51:36 +00001168 if (status)
1169 return status;
1170
1171 /* add new stream to the head of the stream_list */
1172 new_stream->next = ctx->stream_list;
1173 ctx->stream_list = new_stream;
1174
1175 /* set direction to outbound */
1176 new_stream->direction = dir_srtp_sender;
1177
1178 /* set stream (the pointer used in this function) */
1179 stream = new_stream;
1180 } else {
1181 /* no template stream, so we return an error */
1182 return err_status_no_ctx;
1183 }
1184 }
1185
1186 /*
1187 * verify that stream is for sending traffic - this check will
1188 * detect SSRC collisions, since a stream that appears in both
1189 * srtp_protect() and srtp_unprotect() will fail this test in one of
1190 * those functions.
1191 */
jfigus8c36da22013-10-01 16:41:19 -04001192 if (stream->direction != dir_srtp_sender) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001193 if (stream->direction == dir_unknown) {
1194 stream->direction = dir_srtp_sender;
1195 } else {
1196 srtp_handle_event(ctx, stream, event_ssrc_collision);
1197 }
jfigus8c36da22013-10-01 16:41:19 -04001198 }
1199
1200 /*
1201 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
1202 * the request to our AEAD handler.
1203 */
1204 if (stream->rtp_cipher->algorithm == AES_128_GCM ||
1205 stream->rtp_cipher->algorithm == AES_256_GCM) {
1206 return srtp_protect_aead(ctx, stream, rtp_hdr, pkt_octet_len);
1207 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001208
1209 /*
1210 * update the key usage limit, and check it to make sure that we
1211 * didn't just hit either the soft limit or the hard limit, and call
1212 * the event handler if we hit either.
1213 */
1214 switch(key_limit_update(stream->limit)) {
1215 case key_event_normal:
1216 break;
1217 case key_event_soft_limit:
1218 srtp_handle_event(ctx, stream, event_key_soft_limit);
1219 break;
1220 case key_event_hard_limit:
1221 srtp_handle_event(ctx, stream, event_key_hard_limit);
David McGrewfec49dd2005-09-23 19:34:11 +00001222 return err_status_key_expired;
Cullen Jennings235513a2005-09-21 22:51:36 +00001223 default:
1224 break;
1225 }
1226
1227 /* get tag length from stream */
1228 tag_len = auth_get_tag_length(stream->rtp_auth);
1229
1230 /*
1231 * find starting point for encryption and length of data to be
1232 * encrypted - the encrypted portion starts after the rtp header
1233 * extension, if present; otherwise, it starts after the last csrc,
1234 * if any are present
1235 *
1236 * if we're not providing confidentiality, set enc_start to NULL
1237 */
1238 if (stream->rtp_services & sec_serv_conf) {
1239 enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
David McGrew14829302005-10-10 18:53:19 +00001240 if (hdr->x == 1) {
1241 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
1242 enc_start += (ntohs(xtn_hdr->length) + 1);
1243 }
David McGrewc4fc00b2006-06-08 18:51:27 +00001244 enc_octet_len = (unsigned int)(*pkt_octet_len
1245 - ((enc_start - (uint32_t *)hdr) << 2));
Cullen Jennings235513a2005-09-21 22:51:36 +00001246 } else {
1247 enc_start = NULL;
1248 }
1249
1250 /*
1251 * if we're providing authentication, set the auth_start and auth_tag
1252 * pointers to the proper locations; otherwise, set auth_start to NULL
1253 * to indicate that no authentication is needed
1254 */
1255 if (stream->rtp_services & sec_serv_auth) {
1256 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001257 auth_tag = (uint8_t *)hdr + *pkt_octet_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001258 } else {
1259 auth_start = NULL;
1260 auth_tag = NULL;
1261 }
1262
1263 /*
1264 * estimate the packet index using the start of the replay window
1265 * and the sequence number from the header
1266 */
1267 delta = rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
1268 status = rdbx_check(&stream->rtp_rdbx, delta);
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +00001269 if (status) {
1270 if (status != err_status_replay_fail || !stream->allow_repeat_tx)
1271 return status; /* we've been asked to reuse an index */
1272 }
1273 else
1274 rdbx_add_index(&stream->rtp_rdbx, delta);
Cullen Jennings235513a2005-09-21 22:51:36 +00001275
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001276#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001277 debug_print2(mod_srtp, "estimated packet index: %08x%08x",
1278 high32(est),low32(est));
1279#else
1280 debug_print(mod_srtp, "estimated packet index: %016llx", est);
1281#endif
1282
1283 /*
1284 * if we're using rindael counter mode, set nonce and seq
1285 */
jfigus8c36da22013-10-01 16:41:19 -04001286 if (stream->rtp_cipher->type->id == AES_ICM ||
1287 stream->rtp_cipher->type->id == AES_256_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001288 v128_t iv;
1289
1290 iv.v32[0] = 0;
1291 iv.v32[1] = hdr->ssrc;
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001292#ifdef NO_64BIT_MATH
1293 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
David McGrewfec49dd2005-09-23 19:34:11 +00001294 low32(est) << 16));
Cullen Jennings235513a2005-09-21 22:51:36 +00001295#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001296 iv.v64[1] = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001297#endif
jfigus7882dd92013-08-02 16:08:23 -04001298 status = cipher_set_iv(stream->rtp_cipher, &iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001299
1300 } else {
1301 v128_t iv;
1302
1303 /* otherwise, set the index to est */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001304#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001305 iv.v32[0] = 0;
1306 iv.v32[1] = 0;
1307#else
1308 iv.v64[0] = 0;
1309#endif
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001310 iv.v64[1] = be64_to_cpu(est);
jfigus7882dd92013-08-02 16:08:23 -04001311 status = cipher_set_iv(stream->rtp_cipher, &iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001312 }
1313 if (status)
1314 return err_status_cipher_fail;
1315
1316 /* shift est, put into network byte order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001317#ifdef NO_64BIT_MATH
1318 est = be64_to_cpu(make64((high32(est) << 16) |
Cullen Jennings235513a2005-09-21 22:51:36 +00001319 (low32(est) >> 16),
Randell Jesup811e1442005-09-28 22:43:41 +00001320 low32(est) << 16));
Cullen Jennings235513a2005-09-21 22:51:36 +00001321#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001322 est = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001323#endif
1324
1325 /*
1326 * if we're authenticating using a universal hash, put the keystream
1327 * prefix into the authentication tag
1328 */
1329 if (auth_start) {
1330
1331 prefix_len = auth_get_prefix_length(stream->rtp_auth);
1332 if (prefix_len) {
1333 status = cipher_output(stream->rtp_cipher, auth_tag, prefix_len);
1334 if (status)
1335 return err_status_cipher_fail;
1336 debug_print(mod_srtp, "keystream prefix: %s",
1337 octet_string_hex_string(auth_tag, prefix_len));
1338 }
1339 }
1340
1341 /* if we're encrypting, exor keystream into the message */
1342 if (enc_start) {
1343 status = cipher_encrypt(stream->rtp_cipher,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001344 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001345 if (status)
1346 return err_status_cipher_fail;
1347 }
1348
1349 /*
1350 * if we're authenticating, run authentication function and put result
1351 * into the auth_tag
1352 */
1353 if (auth_start) {
1354
1355 /* initialize auth func context */
1356 status = auth_start(stream->rtp_auth);
1357 if (status) return status;
1358
1359 /* run auth func over packet */
1360 status = auth_update(stream->rtp_auth,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001361 (uint8_t *)auth_start, *pkt_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001362 if (status) return status;
1363
1364 /* run auth func over ROC, put result into auth_tag */
David McGrew89fb7ea2005-09-26 19:33:44 +00001365 debug_print(mod_srtp, "estimated packet index: %016llx", est);
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001366 status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, auth_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00001367 debug_print(mod_srtp, "srtp auth tag: %s",
1368 octet_string_hex_string(auth_tag, tag_len));
1369 if (status)
1370 return err_status_auth_fail;
1371
1372 }
1373
1374 if (auth_tag) {
1375
1376 /* increase the packet length by the length of the auth tag */
1377 *pkt_octet_len += tag_len;
1378 }
1379
1380 return err_status_ok;
1381}
1382
1383
1384err_status_t
1385srtp_unprotect(srtp_ctx_t *ctx, void *srtp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00001386 srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00001387 uint32_t *enc_start; /* pointer to start of encrypted portion */
1388 uint32_t *auth_start; /* pointer to start of auth. portion */
David McGrewbb077322006-07-18 19:45:45 +00001389 unsigned enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001390 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
Cullen Jennings235513a2005-09-21 22:51:36 +00001391 xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
1392 int delta; /* delta of local pkt idx and that in hdr */
1393 v128_t iv;
1394 err_status_t status;
1395 srtp_stream_ctx_t *stream;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001396 uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
Cullen Jennings235513a2005-09-21 22:51:36 +00001397 int tag_len, prefix_len;
1398
1399 debug_print(mod_srtp, "function srtp_unprotect", NULL);
1400
1401 /* we assume the hdr is 32-bit aligned to start */
1402
1403 /* check the packet length - it must at least contain a full header */
1404 if (*pkt_octet_len < octets_in_rtp_header)
1405 return err_status_bad_param;
1406
1407 /*
1408 * look up ssrc in srtp_stream list, and process the packet with
1409 * the appropriate stream. if we haven't seen this stream before,
1410 * there's only one key for this srtp_session, and the cipher
1411 * supports key-sharing, then we assume that a new stream using
1412 * that key has just started up
1413 */
1414 stream = srtp_get_stream(ctx, hdr->ssrc);
1415 if (stream == NULL) {
1416 if (ctx->stream_template != NULL) {
1417 stream = ctx->stream_template;
1418 debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)",
1419 hdr->ssrc);
1420
1421 /*
1422 * set estimated packet index to sequence number from header,
1423 * and set delta equal to the same value
1424 */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001425#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001426 est = (xtd_seq_num_t) make64(0,ntohs(hdr->seq));
1427 delta = low32(est);
1428#else
1429 est = (xtd_seq_num_t) ntohs(hdr->seq);
David McGrewc4fc00b2006-06-08 18:51:27 +00001430 delta = (int)est;
Cullen Jennings235513a2005-09-21 22:51:36 +00001431#endif
1432 } else {
1433
1434 /*
1435 * no stream corresponding to SSRC found, and we don't do
1436 * key-sharing, so return an error
1437 */
1438 return err_status_no_ctx;
1439 }
1440 } else {
1441
1442 /* estimate packet index from seq. num. in header */
1443 delta = rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001444
1445 /* check replay database */
1446 status = rdbx_check(&stream->rtp_rdbx, delta);
1447 if (status)
1448 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001449 }
1450
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001451#ifdef NO_64BIT_MATH
David McGrewfec49dd2005-09-23 19:34:11 +00001452 debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est),low32(est));
1453#else
1454 debug_print(mod_srtp, "estimated u_packet index: %016llx", est);
1455#endif
Cullen Jennings235513a2005-09-21 22:51:36 +00001456
jfigus8c36da22013-10-01 16:41:19 -04001457 /*
1458 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
1459 * the request to our AEAD handler.
1460 */
1461 if (stream->rtp_cipher->algorithm == AES_128_GCM ||
1462 stream->rtp_cipher->algorithm == AES_256_GCM) {
1463 return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr, pkt_octet_len);
1464 }
1465
Cullen Jennings235513a2005-09-21 22:51:36 +00001466 /* get tag length from stream */
1467 tag_len = auth_get_tag_length(stream->rtp_auth);
1468
1469 /*
1470 * set the cipher's IV properly, depending on whatever cipher we
1471 * happen to be using
1472 */
jfigus8c36da22013-10-01 16:41:19 -04001473 if (stream->rtp_cipher->type->id == AES_ICM ||
1474 stream->rtp_cipher->type->id == AES_256_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00001475
1476 /* aes counter mode */
1477 iv.v32[0] = 0;
1478 iv.v32[1] = hdr->ssrc; /* still in network order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001479#ifdef NO_64BIT_MATH
1480 iv.v64[1] = be64_to_cpu(make64((high32(est) << 16) | (low32(est) >> 16),
Cullen Jennings235513a2005-09-21 22:51:36 +00001481 low32(est) << 16));
1482#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001483 iv.v64[1] = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001484#endif
jfigus7882dd92013-08-02 16:08:23 -04001485 status = cipher_set_iv(stream->rtp_cipher, &iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001486 } else {
1487
1488 /* no particular format - set the iv to the pakcet index */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001489#ifdef NO_64BIT_MATH
Cullen Jennings235513a2005-09-21 22:51:36 +00001490 iv.v32[0] = 0;
1491 iv.v32[1] = 0;
1492#else
1493 iv.v64[0] = 0;
1494#endif
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001495 iv.v64[1] = be64_to_cpu(est);
jfigus7882dd92013-08-02 16:08:23 -04001496 status = cipher_set_iv(stream->rtp_cipher, &iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00001497 }
1498 if (status)
1499 return err_status_cipher_fail;
1500
1501 /* shift est, put into network byte order */
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001502#ifdef NO_64BIT_MATH
1503 est = be64_to_cpu(make64((high32(est) << 16) |
Cullen Jennings235513a2005-09-21 22:51:36 +00001504 (low32(est) >> 16),
1505 low32(est) << 16));
1506#else
Marcus Sundberge4e34f92005-10-02 20:19:35 +00001507 est = be64_to_cpu(est << 16);
Cullen Jennings235513a2005-09-21 22:51:36 +00001508#endif
1509
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001510 /*
1511 * find starting point for decryption and length of data to be
1512 * decrypted - the encrypted portion starts after the rtp header
1513 * extension, if present; otherwise, it starts after the last csrc,
1514 * if any are present
1515 *
1516 * if we're not providing confidentiality, set enc_start to NULL
1517 */
1518 if (stream->rtp_services & sec_serv_conf) {
1519 enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
David McGrew14829302005-10-10 18:53:19 +00001520 if (hdr->x == 1) {
1521 srtp_hdr_xtnd_t *xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
1522 enc_start += (ntohs(xtn_hdr->length) + 1);
1523 }
David McGrewc4fc00b2006-06-08 18:51:27 +00001524 enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len
1525 - ((enc_start - (uint32_t *)hdr) << 2));
Marcus Sundberg0c324cb2005-10-10 17:23:47 +00001526 } else {
1527 enc_start = NULL;
1528 }
1529
Cullen Jennings235513a2005-09-21 22:51:36 +00001530 /*
1531 * if we're providing authentication, set the auth_start and auth_tag
1532 * pointers to the proper locations; otherwise, set auth_start to NULL
1533 * to indicate that no authentication is needed
1534 */
1535 if (stream->rtp_services & sec_serv_auth) {
1536 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001537 auth_tag = (uint8_t *)hdr + *pkt_octet_len - tag_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00001538 } else {
1539 auth_start = NULL;
1540 auth_tag = NULL;
1541 }
1542
1543 /*
1544 * if we expect message authentication, run the authentication
1545 * function and compare the result with the value of the auth_tag
1546 */
1547 if (auth_start) {
1548
1549 /*
1550 * if we're using a universal hash, then we need to compute the
1551 * keystream prefix for encrypting the universal hash output
1552 *
1553 * if the keystream prefix length is zero, then we know that
1554 * the authenticator isn't using a universal hash function
1555 */
1556 if (stream->rtp_auth->prefix_len != 0) {
1557
1558 prefix_len = auth_get_prefix_length(stream->rtp_auth);
1559 status = cipher_output(stream->rtp_cipher, tmp_tag, prefix_len);
1560 debug_print(mod_srtp, "keystream prefix: %s",
1561 octet_string_hex_string(tmp_tag, prefix_len));
1562 if (status)
1563 return err_status_cipher_fail;
1564 }
1565
1566 /* initialize auth func context */
1567 status = auth_start(stream->rtp_auth);
1568 if (status) return status;
David McGrewfec49dd2005-09-23 19:34:11 +00001569
Cullen Jennings235513a2005-09-21 22:51:36 +00001570 /* now compute auth function over packet */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001571 status = auth_update(stream->rtp_auth, (uint8_t *)auth_start,
Cullen Jennings235513a2005-09-21 22:51:36 +00001572 *pkt_octet_len - tag_len);
1573
1574 /* run auth func over ROC, then write tmp tag */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001575 status = auth_compute(stream->rtp_auth, (uint8_t *)&est, 4, tmp_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00001576
1577 debug_print(mod_srtp, "computed auth tag: %s",
1578 octet_string_hex_string(tmp_tag, tag_len));
1579 debug_print(mod_srtp, "packet auth tag: %s",
1580 octet_string_hex_string(auth_tag, tag_len));
1581 if (status)
1582 return err_status_auth_fail;
1583
1584 if (octet_string_is_eq(tmp_tag, auth_tag, tag_len))
1585 return err_status_auth_fail;
1586 }
1587
Marcus Sundbergfc4d1382005-10-08 18:28:16 +00001588 /*
1589 * update the key usage limit, and check it to make sure that we
1590 * didn't just hit either the soft limit or the hard limit, and call
1591 * the event handler if we hit either.
1592 */
1593 switch(key_limit_update(stream->limit)) {
1594 case key_event_normal:
1595 break;
1596 case key_event_soft_limit:
1597 srtp_handle_event(ctx, stream, event_key_soft_limit);
1598 break;
1599 case key_event_hard_limit:
1600 srtp_handle_event(ctx, stream, event_key_hard_limit);
1601 return err_status_key_expired;
1602 default:
1603 break;
1604 }
1605
Jonathan Lennox23dc1e22010-06-01 18:19:04 +00001606 /* if we're decrypting, add keystream into ciphertext */
Cullen Jennings235513a2005-09-21 22:51:36 +00001607 if (enc_start) {
Jonathan Lennox23dc1e22010-06-01 18:19:04 +00001608 status = cipher_decrypt(stream->rtp_cipher,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00001609 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001610 if (status)
1611 return err_status_cipher_fail;
1612 }
1613
1614 /*
1615 * verify that stream is for received traffic - this check will
1616 * detect SSRC collisions, since a stream that appears in both
1617 * srtp_protect() and srtp_unprotect() will fail this test in one of
1618 * those functions.
1619 *
1620 * we do this check *after* the authentication check, so that the
1621 * latter check will catch any attempts to fool us into thinking
1622 * that we've got a collision
1623 */
1624 if (stream->direction != dir_srtp_receiver) {
1625 if (stream->direction == dir_unknown) {
1626 stream->direction = dir_srtp_receiver;
1627 } else {
1628 srtp_handle_event(ctx, stream, event_ssrc_collision);
1629 }
1630 }
1631
1632 /*
1633 * if the stream is a 'provisional' one, in which the template context
1634 * is used, then we need to allocate a new stream at this point, since
1635 * the authentication passed
1636 */
1637 if (stream == ctx->stream_template) {
1638 srtp_stream_ctx_t *new_stream;
1639
1640 /*
1641 * allocate and initialize a new stream
1642 *
1643 * note that we indicate failure if we can't allocate the new
1644 * stream, and some implementations will want to not return
1645 * failure here
1646 */
1647 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
1648 if (status)
1649 return status;
1650
1651 /* add new stream to the head of the stream_list */
1652 new_stream->next = ctx->stream_list;
1653 ctx->stream_list = new_stream;
1654
1655 /* set stream (the pointer used in this function) */
1656 stream = new_stream;
1657 }
1658
1659 /*
1660 * the message authentication function passed, so add the packet
1661 * index into the replay database
1662 */
1663 rdbx_add_index(&stream->rtp_rdbx, delta);
1664
1665 /* decrease the packet length by the length of the auth tag */
1666 *pkt_octet_len -= tag_len;
1667
1668 return err_status_ok;
1669}
1670
1671err_status_t
1672srtp_init() {
1673 err_status_t status;
1674
1675 /* initialize crypto kernel */
1676 status = crypto_kernel_init();
1677 if (status)
1678 return status;
1679
1680 /* load srtp debug module into the kernel */
1681 status = crypto_kernel_load_debug_module(&mod_srtp);
1682 if (status)
1683 return status;
1684
1685 return err_status_ok;
1686}
1687
Jonathan Lennox5ae76332010-05-15 04:48:59 +00001688err_status_t
1689srtp_shutdown() {
1690 err_status_t status;
1691
1692 /* shut down crypto kernel */
1693 status = crypto_kernel_shutdown();
1694 if (status)
1695 return status;
1696
1697 /* shutting down crypto kernel frees the srtp debug module as well */
1698
1699 return err_status_ok;
1700}
1701
1702
Cullen Jennings235513a2005-09-21 22:51:36 +00001703/*
1704 * The following code is under consideration for removal. See
1705 * SRTP_MAX_TRAILER_LEN
1706 */
1707#if 0
1708
1709/*
1710 * srtp_get_trailer_length(&a) returns the number of octets that will
1711 * be added to an RTP packet by the SRTP processing. This value
1712 * is constant for a given srtp_stream_t (i.e. between initializations).
1713 */
1714
1715int
1716srtp_get_trailer_length(const srtp_stream_t s) {
1717 return auth_get_tag_length(s->rtp_auth);
1718}
1719
1720#endif
1721
1722/*
1723 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
1724 * to ssrc, or NULL if no stream exists for that ssrc
1725 *
1726 * this is an internal function
1727 */
1728
1729srtp_stream_ctx_t *
1730srtp_get_stream(srtp_t srtp, uint32_t ssrc) {
1731 srtp_stream_ctx_t *stream;
1732
1733 /* walk down list until ssrc is found */
1734 stream = srtp->stream_list;
1735 while (stream != NULL) {
1736 if (stream->ssrc == ssrc)
1737 return stream;
1738 stream = stream->next;
1739 }
1740
1741 /* we haven't found our ssrc, so return a null */
1742 return NULL;
1743}
1744
1745err_status_t
1746srtp_dealloc(srtp_t session) {
1747 srtp_stream_ctx_t *stream;
1748 err_status_t status;
1749
1750 /*
1751 * we take a conservative deallocation strategy - if we encounter an
1752 * error deallocating a stream, then we stop trying to deallocate
1753 * memory and just return an error
1754 */
1755
1756 /* walk list of streams, deallocating as we go */
1757 stream = session->stream_list;
1758 while (stream != NULL) {
1759 srtp_stream_t next = stream->next;
1760 status = srtp_stream_dealloc(session, stream);
1761 if (status)
1762 return status;
1763 stream = next;
1764 }
1765
1766 /* deallocate stream template, if there is one */
1767 if (session->stream_template != NULL) {
David McGrewfec49dd2005-09-23 19:34:11 +00001768 status = auth_dealloc(session->stream_template->rtcp_auth);
1769 if (status)
1770 return status;
1771 status = cipher_dealloc(session->stream_template->rtcp_cipher);
1772 if (status)
1773 return status;
1774 crypto_free(session->stream_template->limit);
Cullen Jennings235513a2005-09-21 22:51:36 +00001775 status = cipher_dealloc(session->stream_template->rtp_cipher);
1776 if (status)
1777 return status;
1778 status = auth_dealloc(session->stream_template->rtp_auth);
1779 if (status)
1780 return status;
Jonathan Lennoxa1242f82010-05-17 21:46:04 +00001781 status = rdbx_dealloc(&session->stream_template->rtp_rdbx);
1782 if (status)
1783 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00001784 crypto_free(session->stream_template);
1785 }
1786
1787 /* deallocate session context */
1788 crypto_free(session);
1789
1790 return err_status_ok;
1791}
1792
1793
1794err_status_t
1795srtp_add_stream(srtp_t session,
1796 const srtp_policy_t *policy) {
1797 err_status_t status;
1798 srtp_stream_t tmp;
1799
Marcus Sundberg67398e62005-10-05 12:39:51 +00001800 /* sanity check arguments */
1801 if ((session == NULL) || (policy == NULL) || (policy->key == NULL))
1802 return err_status_bad_param;
1803
Cullen Jennings235513a2005-09-21 22:51:36 +00001804 /* allocate stream */
1805 status = srtp_stream_alloc(&tmp, policy);
1806 if (status) {
1807 return status;
1808 }
1809
1810 /* initialize stream */
1811 status = srtp_stream_init(tmp, policy);
1812 if (status) {
1813 crypto_free(tmp);
1814 return status;
1815 }
1816
1817 /*
1818 * set the head of the stream list or the template to point to the
1819 * stream that we've just alloced and init'ed, depending on whether
1820 * or not it has a wildcard SSRC value or not
1821 *
1822 * if the template stream has already been set, then the policy is
1823 * inconsistent, so we return a bad_param error code
1824 */
1825 switch (policy->ssrc.type) {
1826 case (ssrc_any_outbound):
1827 if (session->stream_template) {
1828 return err_status_bad_param;
1829 }
1830 session->stream_template = tmp;
1831 session->stream_template->direction = dir_srtp_sender;
1832 break;
1833 case (ssrc_any_inbound):
1834 if (session->stream_template) {
1835 return err_status_bad_param;
1836 }
1837 session->stream_template = tmp;
1838 session->stream_template->direction = dir_srtp_receiver;
1839 break;
1840 case (ssrc_specific):
1841 tmp->next = session->stream_list;
1842 session->stream_list = tmp;
1843 break;
1844 case (ssrc_undefined):
1845 default:
1846 crypto_free(tmp);
1847 return err_status_bad_param;
1848 }
1849
1850 return err_status_ok;
1851}
1852
1853
1854err_status_t
1855srtp_create(srtp_t *session, /* handle for session */
1856 const srtp_policy_t *policy) { /* SRTP policy (list) */
1857 err_status_t stat;
1858 srtp_ctx_t *ctx;
1859
1860 /* sanity check arguments */
Marcus Sundberg67398e62005-10-05 12:39:51 +00001861 if (session == NULL)
Cullen Jennings235513a2005-09-21 22:51:36 +00001862 return err_status_bad_param;
1863
1864 /* allocate srtp context and set ctx_ptr */
1865 ctx = (srtp_ctx_t *) crypto_alloc(sizeof(srtp_ctx_t));
1866 if (ctx == NULL)
1867 return err_status_alloc_fail;
1868 *session = ctx;
1869
1870 /*
1871 * loop over elements in the policy list, allocating and
1872 * initializing a stream for each element
1873 */
1874 ctx->stream_template = NULL;
1875 ctx->stream_list = NULL;
1876 while (policy != NULL) {
1877
1878 stat = srtp_add_stream(ctx, policy);
1879 if (stat) {
Marcus Sundberg67398e62005-10-05 12:39:51 +00001880 /* clean up everything */
1881 srtp_dealloc(*session);
Cullen Jennings235513a2005-09-21 22:51:36 +00001882 return stat;
1883 }
1884
1885 /* set policy to next item in list */
1886 policy = policy->next;
1887 }
1888
1889 return err_status_ok;
1890}
1891
1892
1893err_status_t
1894srtp_remove_stream(srtp_t session, uint32_t ssrc) {
1895 srtp_stream_ctx_t *stream, *last_stream;
1896 err_status_t status;
1897
1898 /* sanity check arguments */
1899 if (session == NULL)
1900 return err_status_bad_param;
1901
1902 /* find stream in list; complain if not found */
1903 last_stream = stream = session->stream_list;
1904 while ((stream != NULL) && (ssrc != stream->ssrc)) {
1905 last_stream = stream;
1906 stream = stream->next;
1907 }
1908 if (stream == NULL)
1909 return err_status_no_ctx;
1910
1911 /* remove stream from the list */
Jonathan Lennox20505b32010-05-27 19:22:25 +00001912 if (last_stream == stream)
1913 /* stream was first in list */
1914 session->stream_list = stream->next;
1915 else
1916 last_stream->next = stream->next;
Cullen Jennings235513a2005-09-21 22:51:36 +00001917
1918 /* deallocate the stream */
1919 status = srtp_stream_dealloc(session, stream);
1920 if (status)
1921 return status;
1922
1923 return err_status_ok;
1924}
1925
1926
1927/*
1928 * the default policy - provides a convenient way for callers to use
1929 * the default security policy
1930 *
1931 * this policy is that defined in the current SRTP internet draft.
1932 *
1933 */
1934
1935/*
1936 * NOTE: cipher_key_len is really key len (128 bits) plus salt len
1937 * (112 bits)
1938 */
1939/* There are hard-coded 16's for base_key_len in the key generation code */
1940
1941void
1942crypto_policy_set_rtp_default(crypto_policy_t *p) {
1943
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001944 p->cipher_type = AES_ICM;
Cullen Jennings235513a2005-09-21 22:51:36 +00001945 p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */
1946 p->auth_type = HMAC_SHA1;
1947 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
1948 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
1949 p->sec_serv = sec_serv_conf_and_auth;
1950
1951}
1952
1953void
1954crypto_policy_set_rtcp_default(crypto_policy_t *p) {
1955
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001956 p->cipher_type = AES_ICM;
Cullen Jennings235513a2005-09-21 22:51:36 +00001957 p->cipher_key_len = 30; /* default 128 bits per RFC 3711 */
1958 p->auth_type = HMAC_SHA1;
1959 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
1960 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
1961 p->sec_serv = sec_serv_conf_and_auth;
1962
1963}
1964
David McGrewa8546882006-01-12 17:56:02 +00001965void
1966crypto_policy_set_aes_cm_128_hmac_sha1_32(crypto_policy_t *p) {
1967
1968 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00001969 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00001970 *
1971 * note that this crypto policy is intended for SRTP, but not SRTCP
1972 */
1973
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001974 p->cipher_type = AES_ICM;
David McGrewa8546882006-01-12 17:56:02 +00001975 p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */
1976 p->auth_type = HMAC_SHA1;
1977 p->auth_key_len = 20; /* 160 bit key */
1978 p->auth_tag_len = 4; /* 32 bit tag */
1979 p->sec_serv = sec_serv_conf_and_auth;
1980
1981}
1982
1983
1984void
1985crypto_policy_set_aes_cm_128_null_auth(crypto_policy_t *p) {
1986
1987 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00001988 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00001989 *
1990 * note that this crypto policy is intended for SRTP, but not SRTCP
1991 */
1992
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001993 p->cipher_type = AES_ICM;
David McGrewa8546882006-01-12 17:56:02 +00001994 p->cipher_key_len = 30; /* 128 bit key, 112 bit salt */
1995 p->auth_type = NULL_AUTH;
1996 p->auth_key_len = 0;
1997 p->auth_tag_len = 0;
1998 p->sec_serv = sec_serv_conf;
1999
2000}
2001
2002
2003void
2004crypto_policy_set_null_cipher_hmac_sha1_80(crypto_policy_t *p) {
2005
2006 /*
Jonathan Lennoxd8d5cd02010-05-17 20:08:17 +00002007 * corresponds to RFC 4568
David McGrewa8546882006-01-12 17:56:02 +00002008 */
2009
2010 p->cipher_type = NULL_CIPHER;
2011 p->cipher_key_len = 0;
2012 p->auth_type = HMAC_SHA1;
2013 p->auth_key_len = 20;
2014 p->auth_tag_len = 10;
2015 p->sec_serv = sec_serv_auth;
2016
2017}
2018
2019
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002020void
2021crypto_policy_set_aes_cm_256_hmac_sha1_80(crypto_policy_t *p) {
2022
2023 /*
2024 * corresponds to draft-ietf-avt-big-aes-03.txt
2025 */
2026
2027 p->cipher_type = AES_ICM;
2028 p->cipher_key_len = 46;
2029 p->auth_type = HMAC_SHA1;
2030 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2031 p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
2032 p->sec_serv = sec_serv_conf_and_auth;
2033}
2034
2035
2036void
2037crypto_policy_set_aes_cm_256_hmac_sha1_32(crypto_policy_t *p) {
2038
2039 /*
2040 * corresponds to draft-ietf-avt-big-aes-03.txt
2041 *
2042 * note that this crypto policy is intended for SRTP, but not SRTCP
2043 */
2044
2045 p->cipher_type = AES_ICM;
2046 p->cipher_key_len = 46;
2047 p->auth_type = HMAC_SHA1;
2048 p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
2049 p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */
2050 p->sec_serv = sec_serv_conf_and_auth;
2051}
2052
jfigus8c36da22013-10-01 16:41:19 -04002053/*
2054 * AES-256 with no authentication.
2055 */
2056void
2057crypto_policy_set_aes_cm_256_null_auth (crypto_policy_t *p)
2058{
2059 p->cipher_type = AES_ICM;
2060 p->cipher_key_len = 46;
2061 p->auth_type = NULL_AUTH;
2062 p->auth_key_len = 0;
2063 p->auth_tag_len = 0;
2064 p->sec_serv = sec_serv_conf;
2065}
2066
2067#ifdef OPENSSL
2068/*
2069 * AES-128 GCM mode with 8 octet auth tag.
2070 */
2071void
2072crypto_policy_set_aes_gcm_128_8_auth(crypto_policy_t *p) {
2073 p->cipher_type = AES_128_GCM;
jfigus8719f952014-04-08 09:15:49 -04002074 p->cipher_key_len = AES_128_GCM_KEYSIZE_WSALT;
jfigus8c36da22013-10-01 16:41:19 -04002075 p->auth_type = NULL_AUTH; /* GCM handles the auth for us */
2076 p->auth_key_len = 0;
2077 p->auth_tag_len = 8; /* 8 octet tag length */
2078 p->sec_serv = sec_serv_conf_and_auth;
2079}
2080
2081/*
2082 * AES-256 GCM mode with 8 octet auth tag.
2083 */
2084void
2085crypto_policy_set_aes_gcm_256_8_auth(crypto_policy_t *p) {
2086 p->cipher_type = AES_256_GCM;
jfigus8719f952014-04-08 09:15:49 -04002087 p->cipher_key_len = AES_256_GCM_KEYSIZE_WSALT;
jfigus8c36da22013-10-01 16:41:19 -04002088 p->auth_type = NULL_AUTH; /* GCM handles the auth for us */
2089 p->auth_key_len = 0;
2090 p->auth_tag_len = 8; /* 8 octet tag length */
2091 p->sec_serv = sec_serv_conf_and_auth;
2092}
2093
2094/*
2095 * AES-128 GCM mode with 8 octet auth tag, no RTCP encryption.
2096 */
2097void
2098crypto_policy_set_aes_gcm_128_8_only_auth(crypto_policy_t *p) {
2099 p->cipher_type = AES_128_GCM;
jfigus8719f952014-04-08 09:15:49 -04002100 p->cipher_key_len = AES_128_GCM_KEYSIZE_WSALT;
jfigus8c36da22013-10-01 16:41:19 -04002101 p->auth_type = NULL_AUTH; /* GCM handles the auth for us */
2102 p->auth_key_len = 0;
2103 p->auth_tag_len = 8; /* 8 octet tag length */
2104 p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
2105}
2106
2107/*
2108 * AES-256 GCM mode with 8 octet auth tag, no RTCP encryption.
2109 */
2110void
2111crypto_policy_set_aes_gcm_256_8_only_auth(crypto_policy_t *p) {
2112 p->cipher_type = AES_256_GCM;
jfigus8719f952014-04-08 09:15:49 -04002113 p->cipher_key_len = AES_256_GCM_KEYSIZE_WSALT;
jfigus8c36da22013-10-01 16:41:19 -04002114 p->auth_type = NULL_AUTH; /* GCM handles the auth for us */
2115 p->auth_key_len = 0;
2116 p->auth_tag_len = 8; /* 8 octet tag length */
2117 p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
2118}
jfigusc13c1002014-05-08 13:34:53 -04002119
2120/*
2121 * AES-128 GCM mode with 16 octet auth tag.
2122 */
2123void
2124crypto_policy_set_aes_gcm_128_16_auth(crypto_policy_t *p) {
2125 p->cipher_type = AES_128_GCM;
2126 p->cipher_key_len = AES_128_GCM_KEYSIZE_WSALT;
2127 p->auth_type = NULL_AUTH; /* GCM handles the auth for us */
2128 p->auth_key_len = 0;
2129 p->auth_tag_len = 16; /* 16 octet tag length */
2130 p->sec_serv = sec_serv_conf_and_auth;
2131}
2132
2133/*
2134 * AES-256 GCM mode with 16 octet auth tag.
2135 */
2136void
2137crypto_policy_set_aes_gcm_256_16_auth(crypto_policy_t *p) {
2138 p->cipher_type = AES_256_GCM;
2139 p->cipher_key_len = AES_256_GCM_KEYSIZE_WSALT;
2140 p->auth_type = NULL_AUTH; /* GCM handles the auth for us */
2141 p->auth_key_len = 0;
2142 p->auth_tag_len = 16; /* 16 octet tag length */
2143 p->sec_serv = sec_serv_conf_and_auth;
2144}
2145
jfigus8c36da22013-10-01 16:41:19 -04002146#endif
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002147
Cullen Jennings235513a2005-09-21 22:51:36 +00002148/*
2149 * secure rtcp functions
2150 */
2151
jfigus8c36da22013-10-01 16:41:19 -04002152/*
2153 * AEAD uses a new IV formation method. This function implements
2154 * section 10.1 from draft-ietf-avtcore-srtp-aes-gcm-07.txt. The
2155 * calculation is defined as, where (+) is the xor operation:
2156 *
2157 * 0 1 2 3 4 5 6 7 8 9 10 11
2158 * +--+--+--+--+--+--+--+--+--+--+--+--+
2159 * |00|00| SSRC |00|00|0+SRTCP Idx|---+
2160 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2161 * |
2162 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2163 * | Encryption Salt |->(+)
2164 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2165 * |
2166 * +--+--+--+--+--+--+--+--+--+--+--+--+ |
2167 * | Initialization Vector |<--+
2168 * +--+--+--+--+--+--+--+--+--+--+--+--+*
2169 *
2170 * Input: *stream - pointer to SRTP stream context, used to retrieve
2171 * the SALT
2172 * *iv - Pointer to recieve the calculated IV
2173 * seq_num - The SEQ value to use for the IV calculation.
2174 * *hdr - The RTP header, used to get the SSRC value
2175 *
2176 */
2177static void srtp_calc_aead_iv_srtcp(srtp_stream_ctx_t *stream, v128_t *iv,
2178 uint32_t seq_num, srtcp_hdr_t *hdr)
2179{
2180 v128_t in;
2181 v128_t salt;
2182
2183 memset(&in, 0, sizeof(v128_t));
2184 memset(&salt, 0, sizeof(v128_t));
2185
2186 in.v16[0] = 0;
2187 memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */
2188 in.v16[3] = 0;
2189 in.v32[2] = 0x7FFFFFFF & htonl(seq_num); /* bit 32 is suppose to be zero */
2190
2191 debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in));
2192
2193 /*
2194 * Get the SALT value from the context
2195 */
2196 memcpy(salt.v8, stream->c_salt, 12);
2197 debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt));
2198
2199 /*
2200 * Finally, apply the SALT to the input
2201 */
2202 v128_xor(iv, &in, &salt);
2203}
2204
2205/*
2206 * This code handles AEAD ciphers for outgoing RTCP. We currently support
2207 * AES-GCM mode with 128 or 256 bit keys.
2208 */
2209static err_status_t
2210srtp_protect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,
2211 void *rtcp_hdr, int *pkt_octet_len)
2212{
2213 srtcp_hdr_t *hdr = (srtcp_hdr_t*)rtcp_hdr;
2214 uint32_t *enc_start; /* pointer to start of encrypted portion */
2215 uint32_t *trailer; /* pointer to start of trailer */
2216 unsigned enc_octet_len = 0; /* number of octets in encrypted portion */
2217 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
2218 err_status_t status;
2219 int tag_len;
2220 uint32_t seq_num;
2221 v128_t iv;
2222 uint32_t tseq;
2223
2224 /* get tag length from stream context */
2225 tag_len = auth_get_tag_length(stream->rtcp_auth);
2226
2227 /*
2228 * set encryption start and encryption length - if we're not
2229 * providing confidentiality, set enc_start to NULL
2230 */
2231 enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header;
2232 enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
2233
2234 /* NOTE: hdr->length is not usable - it refers to only the first
2235 RTCP report in the compound packet! */
2236 /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2237 multiples of 32-bits (RFC 3550 6.1) */
2238 trailer = (uint32_t*)((char*)enc_start + enc_octet_len + tag_len);
2239
2240 if (stream->rtcp_services & sec_serv_conf) {
2241 *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
2242 } else {
2243 enc_start = NULL;
2244 enc_octet_len = 0;
2245 /* 0 is network-order independant */
2246 *trailer = 0x00000000; /* set encrypt bit */
2247 }
2248
2249 /*
2250 * set the auth_tag pointer to the proper location, which is after
2251 * the payload, but before the trailer
2252 * (note that srtpc *always* provides authentication, unlike srtp)
2253 */
2254 /* Note: This would need to change for optional mikey data */
2255 auth_tag = (uint8_t*)hdr + *pkt_octet_len;
2256
2257 /*
2258 * check sequence number for overruns, and copy it into the packet
2259 * if its value isn't too big
2260 */
2261 status = rdb_increment(&stream->rtcp_rdb);
2262 if (status) {
2263 return status;
2264 }
2265 seq_num = rdb_get_value(&stream->rtcp_rdb);
2266 *trailer |= htonl(seq_num);
2267 debug_print(mod_srtp, "srtcp index: %x", seq_num);
2268
2269 /*
2270 * Calculating the IV and pass it down to the cipher
2271 */
2272 srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr);
2273 status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_encrypt);
2274 if (status) {
2275 return err_status_cipher_fail;
2276 }
2277
2278 /*
2279 * Set the AAD for GCM mode
2280 */
2281 if (enc_start) {
2282 /*
2283 * If payload encryption is enabled, then the AAD consist of
2284 * the RTCP header and the seq# at the end of the packet
2285 */
2286 status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
2287 octets_in_rtcp_header);
2288 if (status) {
2289 return ( err_status_cipher_fail);
2290 }
2291 } else {
2292 /*
2293 * Since payload encryption is not enabled, we must authenticate
2294 * the entire packet as described in section 10.3 in revision 07
2295 * of the draft.
2296 */
2297 status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
2298 *pkt_octet_len);
2299 if (status) {
2300 return ( err_status_cipher_fail);
2301 }
2302 }
2303 /*
2304 * put the idx# into network byte order and process it as AAD
2305 */
2306 tseq = htonl(*trailer);
2307 status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq,
2308 sizeof(srtcp_trailer_t));
2309 if (status) {
2310 return ( err_status_cipher_fail);
2311 }
2312
2313 /* if we're encrypting, exor keystream into the message */
2314 if (enc_start) {
2315 status = cipher_encrypt(stream->rtcp_cipher,
2316 (uint8_t*)enc_start, &enc_octet_len);
2317 if (status) {
2318 return err_status_cipher_fail;
2319 }
2320 /*
2321 * Get the tag and append that to the output
2322 */
2323 status = cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag,
2324 &tag_len);
2325 if (status) {
2326 return ( err_status_cipher_fail);
2327 }
2328 enc_octet_len += tag_len;
2329 } else {
2330 /*
2331 * Even though we're not encrypting the payload, we need
2332 * to run the cipher to get the auth tag.
2333 */
2334 unsigned nolen = 0;
2335 status = cipher_encrypt(stream->rtcp_cipher, NULL, &nolen);
2336 if (status) {
2337 return err_status_cipher_fail;
2338 }
2339 /*
2340 * Get the tag and append that to the output
2341 */
2342 status = cipher_get_tag(stream->rtcp_cipher, (uint8_t*)auth_tag,
2343 &tag_len);
2344 if (status) {
2345 return ( err_status_cipher_fail);
2346 }
2347 enc_octet_len += tag_len;
2348 }
2349
2350 /* increase the packet length by the length of the auth tag and seq_num*/
2351 *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
2352
2353 return err_status_ok;
2354}
2355
2356/*
2357 * This function handles incoming SRTCP packets while in AEAD mode,
2358 * which currently supports AES-GCM encryption. Note, the auth tag is
2359 * at the end of the packet stream and is automatically checked by GCM
2360 * when decrypting the payload.
2361 */
2362static err_status_t
2363srtp_unprotect_rtcp_aead (srtp_t ctx, srtp_stream_ctx_t *stream,
2364 void *srtcp_hdr, int *pkt_octet_len)
2365{
2366 srtcp_hdr_t *hdr = (srtcp_hdr_t*)srtcp_hdr;
2367 uint32_t *enc_start; /* pointer to start of encrypted portion */
2368 uint32_t *trailer; /* pointer to start of trailer */
2369 unsigned enc_octet_len = 0; /* number of octets in encrypted portion */
2370 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
2371 err_status_t status;
2372 int tag_len;
2373 unsigned int tmp_len;
2374 uint32_t seq_num;
2375 v128_t iv;
2376 uint32_t tseq;
2377
2378 /* get tag length from stream context */
2379 tag_len = auth_get_tag_length(stream->rtcp_auth);
2380
2381 /* Validate packet length */
2382 if (*pkt_octet_len < (octets_in_rtcp_header + tag_len +
2383 sizeof(srtcp_trailer_t))) {
2384 return err_status_bad_param;
2385 }
2386
2387 /*
2388 * set encryption start, encryption length, and trailer
2389 */
2390 /* index & E (encryption) bit follow normal data. hdr->len
2391 is the number of words (32-bit) in the normal packet minus 1 */
2392 /* This should point trailer to the word past the end of the
2393 normal data. */
2394 /* This would need to be modified for optional mikey data */
2395 /*
2396 * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2397 * multiples of 32-bits (RFC 3550 6.1)
2398 */
2399 trailer = (uint32_t*)((char*)hdr + *pkt_octet_len - sizeof(srtcp_trailer_t));
2400 /*
2401 * We pass the tag down to the cipher when doing GCM mode
2402 */
2403 enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header +
2404 sizeof(srtcp_trailer_t));
2405 auth_tag = (uint8_t*)hdr + *pkt_octet_len - tag_len - sizeof(srtcp_trailer_t);
2406
2407 if (*((unsigned char*)trailer) & SRTCP_E_BYTE_BIT) {
2408 enc_start = (uint32_t*)hdr + uint32s_in_rtcp_header;
2409 } else {
2410 enc_octet_len = 0;
2411 enc_start = NULL; /* this indicates that there's no encryption */
2412 }
2413
2414 /*
2415 * check the sequence number for replays
2416 */
2417 /* this is easier than dealing with bitfield access */
2418 seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK;
2419 debug_print(mod_srtp, "srtcp index: %x", seq_num);
2420 status = rdb_check(&stream->rtcp_rdb, seq_num);
2421 if (status) {
2422 return status;
2423 }
2424
2425 /*
2426 * Calculate and set the IV
2427 */
2428 srtp_calc_aead_iv_srtcp(stream, &iv, seq_num, hdr);
2429 status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_decrypt);
2430 if (status) {
2431 return err_status_cipher_fail;
2432 }
2433
2434 /*
2435 * Set the AAD for GCM mode
2436 */
2437 if (enc_start) {
2438 /*
2439 * If payload encryption is enabled, then the AAD consist of
2440 * the RTCP header and the seq# at the end of the packet
2441 */
2442 status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
2443 octets_in_rtcp_header);
2444 if (status) {
2445 return ( err_status_cipher_fail);
2446 }
2447 } else {
2448 /*
2449 * Since payload encryption is not enabled, we must authenticate
2450 * the entire packet as described in section 10.3 in revision 07
2451 * of the draft.
2452 */
2453 status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)hdr,
2454 (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t)));
2455 if (status) {
2456 return ( err_status_cipher_fail);
2457 }
2458 }
2459
2460 /*
2461 * put the idx# into network byte order, and process it as AAD
2462 */
2463 tseq = htonl(*trailer);
2464 status = cipher_set_aad(stream->rtcp_cipher, (uint8_t*)&tseq,
2465 sizeof(srtcp_trailer_t));
2466 if (status) {
2467 return ( err_status_cipher_fail);
2468 }
2469
2470 /* if we're decrypting, exor keystream into the message */
2471 if (enc_start) {
2472 status = cipher_decrypt(stream->rtcp_cipher,
2473 (uint8_t*)enc_start, &enc_octet_len);
2474 if (status) {
2475 return status;
2476 }
2477 } else {
2478 /*
2479 * Still need to run the cipher to check the tag
2480 */
2481 tmp_len = tag_len;
2482 status = cipher_decrypt(stream->rtcp_cipher, (uint8_t*)auth_tag,
2483 &tmp_len);
2484 if (status) {
2485 return status;
2486 }
2487 }
2488
2489 /* decrease the packet length by the length of the auth tag and seq_num*/
2490 *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
2491
2492 /*
2493 * verify that stream is for received traffic - this check will
2494 * detect SSRC collisions, since a stream that appears in both
2495 * srtp_protect() and srtp_unprotect() will fail this test in one of
2496 * those functions.
2497 *
2498 * we do this check *after* the authentication check, so that the
2499 * latter check will catch any attempts to fool us into thinking
2500 * that we've got a collision
2501 */
2502 if (stream->direction != dir_srtp_receiver) {
2503 if (stream->direction == dir_unknown) {
2504 stream->direction = dir_srtp_receiver;
2505 } else {
2506 srtp_handle_event(ctx, stream, event_ssrc_collision);
2507 }
2508 }
2509
2510 /*
2511 * if the stream is a 'provisional' one, in which the template context
2512 * is used, then we need to allocate a new stream at this point, since
2513 * the authentication passed
2514 */
2515 if (stream == ctx->stream_template) {
2516 srtp_stream_ctx_t *new_stream;
2517
2518 /*
2519 * allocate and initialize a new stream
2520 *
2521 * note that we indicate failure if we can't allocate the new
2522 * stream, and some implementations will want to not return
2523 * failure here
2524 */
2525 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
2526 if (status) {
2527 return status;
2528 }
2529
2530 /* add new stream to the head of the stream_list */
2531 new_stream->next = ctx->stream_list;
2532 ctx->stream_list = new_stream;
2533
2534 /* set stream (the pointer used in this function) */
2535 stream = new_stream;
2536 }
2537
2538 /* we've passed the authentication check, so add seq_num to the rdb */
2539 rdb_add_index(&stream->rtcp_rdb, seq_num);
2540
2541 return err_status_ok;
2542}
2543
Cullen Jennings235513a2005-09-21 22:51:36 +00002544err_status_t
2545srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00002546 srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00002547 uint32_t *enc_start; /* pointer to start of encrypted portion */
2548 uint32_t *auth_start; /* pointer to start of auth. portion */
2549 uint32_t *trailer; /* pointer to start of trailer */
David McGrewbb077322006-07-18 19:45:45 +00002550 unsigned enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002551 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
Cullen Jennings235513a2005-09-21 22:51:36 +00002552 err_status_t status;
2553 int tag_len;
2554 srtp_stream_ctx_t *stream;
2555 int prefix_len;
2556 uint32_t seq_num;
2557
2558 /* we assume the hdr is 32-bit aligned to start */
2559 /*
2560 * look up ssrc in srtp_stream list, and process the packet with
2561 * the appropriate stream. if we haven't seen this stream before,
2562 * there's only one key for this srtp_session, and the cipher
2563 * supports key-sharing, then we assume that a new stream using
2564 * that key has just started up
2565 */
2566 stream = srtp_get_stream(ctx, hdr->ssrc);
2567 if (stream == NULL) {
2568 if (ctx->stream_template != NULL) {
2569 srtp_stream_ctx_t *new_stream;
2570
2571 /* allocate and initialize a new stream */
2572 status = srtp_stream_clone(ctx->stream_template,
2573 hdr->ssrc, &new_stream);
2574 if (status)
2575 return status;
2576
2577 /* add new stream to the head of the stream_list */
2578 new_stream->next = ctx->stream_list;
2579 ctx->stream_list = new_stream;
2580
2581 /* set stream (the pointer used in this function) */
2582 stream = new_stream;
2583 } else {
2584 /* no template stream, so we return an error */
2585 return err_status_no_ctx;
2586 }
2587 }
2588
2589 /*
2590 * verify that stream is for sending traffic - this check will
2591 * detect SSRC collisions, since a stream that appears in both
2592 * srtp_protect() and srtp_unprotect() will fail this test in one of
2593 * those functions.
2594 */
2595 if (stream->direction != dir_srtp_sender) {
2596 if (stream->direction == dir_unknown) {
David McGrewc34f7402006-03-09 21:17:00 +00002597 stream->direction = dir_srtp_sender;
Cullen Jennings235513a2005-09-21 22:51:36 +00002598 } else {
2599 srtp_handle_event(ctx, stream, event_ssrc_collision);
2600 }
2601 }
2602
jfigus8c36da22013-10-01 16:41:19 -04002603 /*
2604 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
2605 * the request to our AEAD handler.
2606 */
2607 if (stream->rtp_cipher->algorithm == AES_128_GCM ||
2608 stream->rtp_cipher->algorithm == AES_256_GCM) {
2609 return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr, pkt_octet_len);
2610 }
2611
Cullen Jennings235513a2005-09-21 22:51:36 +00002612 /* get tag length from stream context */
2613 tag_len = auth_get_tag_length(stream->rtcp_auth);
2614
2615 /*
2616 * set encryption start and encryption length - if we're not
2617 * providing confidentiality, set enc_start to NULL
2618 */
2619 enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
2620 enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
2621
2622 /* all of the packet, except the header, gets encrypted */
2623 /* NOTE: hdr->length is not usable - it refers to only the first
2624 RTCP report in the compound packet! */
2625 /* NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2626 multiples of 32-bits (RFC 3550 6.1) */
2627 trailer = (uint32_t *) ((char *)enc_start + enc_octet_len);
2628
2629 if (stream->rtcp_services & sec_serv_conf) {
2630 *trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
2631 } else {
2632 enc_start = NULL;
2633 enc_octet_len = 0;
2634 /* 0 is network-order independant */
2635 *trailer = 0x00000000; /* set encrypt bit */
2636 }
2637
2638 /*
2639 * set the auth_start and auth_tag pointers to the proper locations
2640 * (note that srtpc *always* provides authentication, unlike srtp)
2641 */
2642 /* Note: This would need to change for optional mikey data */
2643 auth_start = (uint32_t *)hdr;
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002644 auth_tag = (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t);
Cullen Jennings235513a2005-09-21 22:51:36 +00002645
David McGrew79870d62007-06-15 18:17:39 +00002646 /* perform EKT processing if needed */
2647 ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len,
2648 rdbx_get_packet_index(&stream->rtp_rdbx));
2649
Cullen Jennings235513a2005-09-21 22:51:36 +00002650 /*
2651 * check sequence number for overruns, and copy it into the packet
2652 * if its value isn't too big
2653 */
2654 status = rdb_increment(&stream->rtcp_rdb);
2655 if (status)
2656 return status;
2657 seq_num = rdb_get_value(&stream->rtcp_rdb);
2658 *trailer |= htonl(seq_num);
2659 debug_print(mod_srtp, "srtcp index: %x", seq_num);
2660
2661 /*
2662 * if we're using rindael counter mode, set nonce and seq
2663 */
Jonathan Lennoxf4332412010-05-20 22:10:20 +00002664 if (stream->rtcp_cipher->type->id == AES_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002665 v128_t iv;
2666
2667 iv.v32[0] = 0;
2668 iv.v32[1] = hdr->ssrc; /* still in network order! */
2669 iv.v32[2] = htonl(seq_num >> 16);
2670 iv.v32[3] = htonl(seq_num << 16);
jfigus7882dd92013-08-02 16:08:23 -04002671 status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00002672
2673 } else {
2674 v128_t iv;
2675
2676 /* otherwise, just set the index to seq_num */
2677 iv.v32[0] = 0;
2678 iv.v32[1] = 0;
2679 iv.v32[2] = 0;
2680 iv.v32[3] = htonl(seq_num);
jfigus7882dd92013-08-02 16:08:23 -04002681 status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_encrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00002682 }
2683 if (status)
2684 return err_status_cipher_fail;
2685
2686 /*
2687 * if we're authenticating using a universal hash, put the keystream
2688 * prefix into the authentication tag
2689 */
2690
2691 /* if auth_start is non-null, then put keystream into tag */
2692 if (auth_start) {
2693
2694 /* put keystream prefix into auth_tag */
2695 prefix_len = auth_get_prefix_length(stream->rtcp_auth);
2696 status = cipher_output(stream->rtcp_cipher, auth_tag, prefix_len);
2697
2698 debug_print(mod_srtp, "keystream prefix: %s",
2699 octet_string_hex_string(auth_tag, prefix_len));
2700
2701 if (status)
2702 return err_status_cipher_fail;
2703 }
2704
2705 /* if we're encrypting, exor keystream into the message */
2706 if (enc_start) {
2707 status = cipher_encrypt(stream->rtcp_cipher,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002708 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00002709 if (status)
2710 return err_status_cipher_fail;
2711 }
2712
2713 /* initialize auth func context */
2714 auth_start(stream->rtcp_auth);
2715
David McGrew9c70f292006-05-03 19:38:38 +00002716 /*
2717 * run auth func over packet (including trailer), and write the
2718 * result at auth_tag
2719 */
Cullen Jennings235513a2005-09-21 22:51:36 +00002720 status = auth_compute(stream->rtcp_auth,
David McGrew9c70f292006-05-03 19:38:38 +00002721 (uint8_t *)auth_start,
2722 (*pkt_octet_len) + sizeof(srtcp_trailer_t),
2723 auth_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00002724 debug_print(mod_srtp, "srtcp auth tag: %s",
2725 octet_string_hex_string(auth_tag, tag_len));
2726 if (status)
2727 return err_status_auth_fail;
2728
2729 /* increase the packet length by the length of the auth tag and seq_num*/
2730 *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
2731
2732 return err_status_ok;
2733}
2734
2735
2736err_status_t
2737srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len) {
Derek MacDonald17127da2006-07-12 22:22:08 +00002738 srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +00002739 uint32_t *enc_start; /* pointer to start of encrypted portion */
2740 uint32_t *auth_start; /* pointer to start of auth. portion */
2741 uint32_t *trailer; /* pointer to start of trailer */
David McGrewbb077322006-07-18 19:45:45 +00002742 unsigned enc_octet_len = 0;/* number of octets in encrypted portion */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002743 uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
2744 uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
David McGrew79870d62007-06-15 18:17:39 +00002745 uint8_t tag_copy[SRTP_MAX_TAG_LEN];
Cullen Jennings235513a2005-09-21 22:51:36 +00002746 err_status_t status;
David McGrew79870d62007-06-15 18:17:39 +00002747 unsigned auth_len;
Cullen Jennings235513a2005-09-21 22:51:36 +00002748 int tag_len;
2749 srtp_stream_ctx_t *stream;
2750 int prefix_len;
2751 uint32_t seq_num;
TV Sriram4986a362013-05-06 11:24:03 -07002752 int e_bit_in_packet; /* whether the E-bit was found in the packet */
2753 int sec_serv_confidentiality; /* whether confidentiality was requested */
Cullen Jennings235513a2005-09-21 22:51:36 +00002754
2755 /* we assume the hdr is 32-bit aligned to start */
2756 /*
2757 * look up ssrc in srtp_stream list, and process the packet with
2758 * the appropriate stream. if we haven't seen this stream before,
2759 * there's only one key for this srtp_session, and the cipher
2760 * supports key-sharing, then we assume that a new stream using
2761 * that key has just started up
2762 */
2763 stream = srtp_get_stream(ctx, hdr->ssrc);
2764 if (stream == NULL) {
2765 if (ctx->stream_template != NULL) {
2766 stream = ctx->stream_template;
David McGrew79870d62007-06-15 18:17:39 +00002767
2768 /*
2769 * check to see if stream_template has an EKT data structure, in
2770 * which case we initialize the template using the EKT policy
2771 * referenced by that data (which consists of decrypting the
2772 * master key from the EKT field)
2773 *
2774 * this function initializes a *provisional* stream, and this
2775 * stream should not be accepted until and unless the packet
2776 * passes its authentication check
2777 */
2778 if (stream->ekt != NULL) {
2779 status = srtp_stream_init_from_ekt(stream, srtcp_hdr, *pkt_octet_len);
2780 if (status)
2781 return status;
2782 }
2783
Cullen Jennings235513a2005-09-21 22:51:36 +00002784 debug_print(mod_srtp, "srtcp using provisional stream (SSRC: 0x%08x)",
2785 hdr->ssrc);
2786 } else {
2787 /* no template stream, so we return an error */
2788 return err_status_no_ctx;
2789 }
2790 }
2791
jfigus8c36da22013-10-01 16:41:19 -04002792 /*
2793 * Check if this is an AEAD stream (GCM mode). If so, then dispatch
2794 * the request to our AEAD handler.
2795 */
2796 if (stream->rtp_cipher->algorithm == AES_128_GCM ||
2797 stream->rtp_cipher->algorithm == AES_256_GCM) {
2798 return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr, pkt_octet_len);
2799 }
2800
TV Sriram4986a362013-05-06 11:24:03 -07002801 sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf ||
2802 stream->rtcp_services == sec_serv_conf_and_auth;
2803
Cullen Jennings235513a2005-09-21 22:51:36 +00002804 /* get tag length from stream context */
2805 tag_len = auth_get_tag_length(stream->rtcp_auth);
2806
2807 /*
2808 * set encryption start, encryption length, and trailer
2809 */
2810 enc_octet_len = *pkt_octet_len -
2811 (octets_in_rtcp_header + tag_len + sizeof(srtcp_trailer_t));
2812 /* index & E (encryption) bit follow normal data. hdr->len
2813 is the number of words (32-bit) in the normal packet minus 1 */
2814 /* This should point trailer to the word past the end of the
2815 normal data. */
2816 /* This would need to be modified for optional mikey data */
2817 /*
2818 * NOTE: trailer is 32-bit aligned because RTCP 'packets' are always
2819 * multiples of 32-bits (RFC 3550 6.1)
2820 */
2821 trailer = (uint32_t *) ((char *) hdr +
TV Sriram4986a362013-05-06 11:24:03 -07002822 *pkt_octet_len -(tag_len + sizeof(srtcp_trailer_t)));
2823 e_bit_in_packet =
2824 (*((unsigned char *) trailer) & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT;
2825 if (e_bit_in_packet != sec_serv_confidentiality) {
2826 return err_status_cant_check;
2827 }
2828 if (sec_serv_confidentiality) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002829 enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
2830 } else {
2831 enc_octet_len = 0;
2832 enc_start = NULL; /* this indicates that there's no encryption */
2833 }
2834
2835 /*
2836 * set the auth_start and auth_tag pointers to the proper locations
2837 * (note that srtcp *always* uses authentication, unlike srtp)
2838 */
2839 auth_start = (uint32_t *)hdr;
David McGrew79870d62007-06-15 18:17:39 +00002840 auth_len = *pkt_octet_len - tag_len;
2841 auth_tag = (uint8_t *)hdr + auth_len;
2842
2843 /*
2844 * if EKT is in use, then we make a copy of the tag from the packet,
2845 * and then zeroize the location of the base tag
2846 *
2847 * we first re-position the auth_tag pointer so that it points to
2848 * the base tag
2849 */
2850 if (stream->ekt) {
2851 auth_tag -= ekt_octets_after_base_tag(stream->ekt);
2852 memcpy(tag_copy, auth_tag, tag_len);
2853 octet_string_set_to_zero(auth_tag, tag_len);
2854 auth_tag = tag_copy;
2855 auth_len += tag_len;
2856 }
Cullen Jennings235513a2005-09-21 22:51:36 +00002857
2858 /*
2859 * check the sequence number for replays
2860 */
2861 /* this is easier than dealing with bitfield access */
2862 seq_num = ntohl(*trailer) & SRTCP_INDEX_MASK;
David McGrew9c70f292006-05-03 19:38:38 +00002863 debug_print(mod_srtp, "srtcp index: %x", seq_num);
Cullen Jennings235513a2005-09-21 22:51:36 +00002864 status = rdb_check(&stream->rtcp_rdb, seq_num);
2865 if (status)
2866 return status;
Cullen Jennings235513a2005-09-21 22:51:36 +00002867
2868 /*
2869 * if we're using aes counter mode, set nonce and seq
2870 */
Jonathan Lennoxf4332412010-05-20 22:10:20 +00002871 if (stream->rtcp_cipher->type->id == AES_ICM) {
Cullen Jennings235513a2005-09-21 22:51:36 +00002872 v128_t iv;
2873
2874 iv.v32[0] = 0;
2875 iv.v32[1] = hdr->ssrc; /* still in network order! */
2876 iv.v32[2] = htonl(seq_num >> 16);
2877 iv.v32[3] = htonl(seq_num << 16);
jfigus7882dd92013-08-02 16:08:23 -04002878 status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00002879
2880 } else {
2881 v128_t iv;
2882
2883 /* otherwise, just set the index to seq_num */
2884 iv.v32[0] = 0;
2885 iv.v32[1] = 0;
2886 iv.v32[2] = 0;
2887 iv.v32[3] = htonl(seq_num);
jfigus7882dd92013-08-02 16:08:23 -04002888 status = cipher_set_iv(stream->rtcp_cipher, &iv, direction_decrypt);
Cullen Jennings235513a2005-09-21 22:51:36 +00002889
2890 }
2891 if (status)
2892 return err_status_cipher_fail;
2893
2894 /* initialize auth func context */
2895 auth_start(stream->rtcp_auth);
2896
2897 /* run auth func over packet, put result into tmp_tag */
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002898 status = auth_compute(stream->rtcp_auth, (uint8_t *)auth_start,
David McGrew79870d62007-06-15 18:17:39 +00002899 auth_len, tmp_tag);
Cullen Jennings235513a2005-09-21 22:51:36 +00002900 debug_print(mod_srtp, "srtcp computed tag: %s",
2901 octet_string_hex_string(tmp_tag, tag_len));
2902 if (status)
2903 return err_status_auth_fail;
2904
2905 /* compare the tag just computed with the one in the packet */
2906 debug_print(mod_srtp, "srtcp tag from packet: %s",
2907 octet_string_hex_string(auth_tag, tag_len));
2908 if (octet_string_is_eq(tmp_tag, auth_tag, tag_len))
2909 return err_status_auth_fail;
2910
2911 /*
2912 * if we're authenticating using a universal hash, put the keystream
2913 * prefix into the authentication tag
2914 */
2915 prefix_len = auth_get_prefix_length(stream->rtcp_auth);
2916 if (prefix_len) {
2917 status = cipher_output(stream->rtcp_cipher, auth_tag, prefix_len);
2918 debug_print(mod_srtp, "keystream prefix: %s",
2919 octet_string_hex_string(auth_tag, prefix_len));
2920 if (status)
2921 return err_status_cipher_fail;
2922 }
2923
2924 /* if we're decrypting, exor keystream into the message */
2925 if (enc_start) {
Jonathan Lennox23dc1e22010-06-01 18:19:04 +00002926 status = cipher_decrypt(stream->rtcp_cipher,
Marcus Sundberg410faaa2005-09-29 12:36:43 +00002927 (uint8_t *)enc_start, &enc_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00002928 if (status)
2929 return err_status_cipher_fail;
2930 }
2931
David McGrew79870d62007-06-15 18:17:39 +00002932 /* decrease the packet length by the length of the auth tag and seq_num */
Cullen Jennings235513a2005-09-21 22:51:36 +00002933 *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
2934
David McGrew79870d62007-06-15 18:17:39 +00002935 /*
2936 * if EKT is in effect, subtract the EKT data out of the packet
2937 * length
2938 */
2939 *pkt_octet_len -= ekt_octets_after_base_tag(stream->ekt);
2940
Cullen Jennings235513a2005-09-21 22:51:36 +00002941 /*
2942 * verify that stream is for received traffic - this check will
2943 * detect SSRC collisions, since a stream that appears in both
2944 * srtp_protect() and srtp_unprotect() will fail this test in one of
2945 * those functions.
2946 *
2947 * we do this check *after* the authentication check, so that the
2948 * latter check will catch any attempts to fool us into thinking
2949 * that we've got a collision
2950 */
2951 if (stream->direction != dir_srtp_receiver) {
2952 if (stream->direction == dir_unknown) {
2953 stream->direction = dir_srtp_receiver;
2954 } else {
2955 srtp_handle_event(ctx, stream, event_ssrc_collision);
2956 }
2957 }
2958
2959 /*
2960 * if the stream is a 'provisional' one, in which the template context
2961 * is used, then we need to allocate a new stream at this point, since
2962 * the authentication passed
2963 */
2964 if (stream == ctx->stream_template) {
2965 srtp_stream_ctx_t *new_stream;
2966
2967 /*
2968 * allocate and initialize a new stream
2969 *
2970 * note that we indicate failure if we can't allocate the new
2971 * stream, and some implementations will want to not return
2972 * failure here
2973 */
2974 status = srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
2975 if (status)
2976 return status;
2977
2978 /* add new stream to the head of the stream_list */
2979 new_stream->next = ctx->stream_list;
2980 ctx->stream_list = new_stream;
2981
2982 /* set stream (the pointer used in this function) */
2983 stream = new_stream;
2984 }
2985
2986 /* we've passed the authentication check, so add seq_num to the rdb */
2987 rdb_add_index(&stream->rtcp_rdb, seq_num);
2988
2989
2990 return err_status_ok;
2991}
David McGrew0cb86ee2006-07-07 15:46:57 +00002992
2993
2994
2995/*
2996 * dtls keying for srtp
2997 */
2998
2999err_status_t
3000crypto_policy_set_from_profile_for_rtp(crypto_policy_t *policy,
3001 srtp_profile_t profile) {
3002
3003 /* set SRTP policy from the SRTP profile in the key set */
3004 switch(profile) {
3005 case srtp_profile_aes128_cm_sha1_80:
3006 crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003007 break;
3008 case srtp_profile_aes128_cm_sha1_32:
3009 crypto_policy_set_aes_cm_128_hmac_sha1_32(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003010 break;
3011 case srtp_profile_null_sha1_80:
3012 crypto_policy_set_null_cipher_hmac_sha1_80(policy);
David McGrew0cb86ee2006-07-07 15:46:57 +00003013 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003014 case srtp_profile_aes256_cm_sha1_80:
3015 crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003016 break;
3017 case srtp_profile_aes256_cm_sha1_32:
3018 crypto_policy_set_aes_cm_256_hmac_sha1_32(policy);
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003019 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003020 /* the following profiles are not (yet) supported */
3021 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003022 default:
3023 return err_status_bad_param;
3024 }
3025
3026 return err_status_ok;
3027}
3028
3029err_status_t
3030crypto_policy_set_from_profile_for_rtcp(crypto_policy_t *policy,
3031 srtp_profile_t profile) {
3032
3033 /* set SRTP policy from the SRTP profile in the key set */
3034 switch(profile) {
3035 case srtp_profile_aes128_cm_sha1_80:
3036 crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
3037 break;
3038 case srtp_profile_aes128_cm_sha1_32:
jfigus0acbb032013-05-30 16:47:02 -04003039 /* We do not honor the 32-bit auth tag request since
3040 * this is not compliant with RFC 3711 */
David McGrew0cb86ee2006-07-07 15:46:57 +00003041 crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
3042 break;
3043 case srtp_profile_null_sha1_80:
3044 crypto_policy_set_null_cipher_hmac_sha1_80(policy);
3045 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003046 case srtp_profile_aes256_cm_sha1_80:
3047 crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
3048 break;
3049 case srtp_profile_aes256_cm_sha1_32:
jfigus0acbb032013-05-30 16:47:02 -04003050 /* We do not honor the 32-bit auth tag request since
3051 * this is not compliant with RFC 3711 */
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003052 crypto_policy_set_aes_cm_256_hmac_sha1_80(policy);
3053 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003054 /* the following profiles are not (yet) supported */
3055 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003056 default:
3057 return err_status_bad_param;
3058 }
3059
3060 return err_status_ok;
3061}
3062
3063void
3064append_salt_to_key(uint8_t *key, unsigned int bytes_in_key,
3065 uint8_t *salt, unsigned int bytes_in_salt) {
3066
3067 memcpy(key + bytes_in_key, salt, bytes_in_salt);
3068
3069}
3070
3071unsigned int
3072srtp_profile_get_master_key_length(srtp_profile_t profile) {
3073
3074 switch(profile) {
3075 case srtp_profile_aes128_cm_sha1_80:
3076 return 16;
3077 break;
3078 case srtp_profile_aes128_cm_sha1_32:
3079 return 16;
3080 break;
3081 case srtp_profile_null_sha1_80:
3082 return 16;
3083 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003084 case srtp_profile_aes256_cm_sha1_80:
3085 return 32;
3086 break;
3087 case srtp_profile_aes256_cm_sha1_32:
3088 return 32;
3089 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003090 /* the following profiles are not (yet) supported */
3091 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003092 default:
3093 return 0; /* indicate error by returning a zero */
3094 }
3095}
3096
3097unsigned int
3098srtp_profile_get_master_salt_length(srtp_profile_t profile) {
3099
3100 switch(profile) {
3101 case srtp_profile_aes128_cm_sha1_80:
3102 return 14;
3103 break;
3104 case srtp_profile_aes128_cm_sha1_32:
3105 return 14;
3106 break;
3107 case srtp_profile_null_sha1_80:
3108 return 14;
3109 break;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00003110 case srtp_profile_aes256_cm_sha1_80:
3111 return 14;
3112 break;
3113 case srtp_profile_aes256_cm_sha1_32:
3114 return 14;
3115 break;
David McGrew0cb86ee2006-07-07 15:46:57 +00003116 /* the following profiles are not (yet) supported */
3117 case srtp_profile_null_sha1_32:
David McGrew0cb86ee2006-07-07 15:46:57 +00003118 default:
3119 return 0; /* indicate error by returning a zero */
3120 }
3121}