Debian Codegraph @

res/res_srtp.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005, Mikael Magnusson
5 *
6 * Mikael Magnusson <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 *
18 * Builds on libSRTP http://srtp.sourceforge.net
19 */
20
21 /*!
22 * \file
23 *
24 * \brief Secure RTP (SRTP)
25 *
26 * Secure RTP (SRTP)
27 * Specified in RFC 3711.
28 *
29 * \author Mikael Magnusson <[email protected]>
30 */
31
32 /*** MODULEINFO
33 <depend>srtp</depend>
34 <use type="external">openssl</use>
35 <support_level>core</support_level>
36 ***/
37
38 /*** MAKEOPTS
39 <category name="MENUSELECT_RES" displayname="Resource Modules">
40 <member name="ENABLE_SRTP_AES_192" displayname="Enable AES 192 cipher suite in res_srtp" touch_on_change="res/res_srtp.c">
41 <defaultenabled>no</defaultenabled>
42 <support_level>option</support_level>
43 <depend>srtp</depend>
44 <depend>res_srtp</depend>
45 <depend>HAVE_SRTP_192</depend>
46 </member>
47 <member name="ENABLE_SRTP_AES_256" displayname="Enable AES 256 cipher suite in res_srtp" touch_on_change="res/res_srtp.c">
48 <defaultenabled>no</defaultenabled>
49 <support_level>option</support_level>
50 <depend>srtp</depend>
51 <depend>res_srtp</depend>
52 <depend>HAVE_SRTP_256</depend>
53 </member>
54 <member name="ENABLE_SRTP_AES_GCM" displayname="Enable AES GCM cipher suite in res_srtp" touch_on_change="res/res_srtp.c">
55 <defaultenabled>no</defaultenabled>
56 <support_level>option</support_level>
57 <depend>srtp</depend>
58 <depend>res_srtp</depend>
59 <depend>HAVE_SRTP_GCM</depend>
60 </member>
61 </category>
62 ***/
63
64 /* See https://docs.asterisk.org/Deployment/Secure-Calling/ */
65
66 #include "asterisk.h" /* for NULL, size_t, memcpy, etc */
67
68 #include <math.h> /* for pow */
69
70 #if HAVE_SRTP_VERSION > 1
71 # include <srtp2/srtp.h>
72 # include "srtp/srtp_compat.h"
73 # include <openssl/rand.h>
74 #else
75 # include <srtp/srtp.h>
76 # ifdef HAVE_OPENSSL
77 # include <openssl/rand.h>
78 # else
79 # include <srtp/crypto_kernel.h>
80 # endif
81 #endif
82
83 #include "asterisk/astobj2.h" /* for ao2_t_ref, etc */
84 #include "asterisk/frame.h" /* for AST_FRIENDLY_OFFSET */
85 #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
86 #include "asterisk/module.h" /* for ast_module_info, etc */
87 #include "asterisk/sdp_srtp.h"
88 #include "asterisk/res_srtp.h" /* for ast_srtp_cb, ast_srtp_suite, etc */
89 #include "asterisk/rtp_engine.h" /* for ast_rtp_engine_register_srtp, etc */
90 #include "asterisk/utils.h" /* for ast_free, ast_calloc */
91
92 struct ast_srtp {
93 struct ast_rtp_instance *rtp;
94 struct ao2_container *policies;
95 srtp_t session;
96 const struct ast_srtp_cb *cb;
97 void *data;
98 int warned;
99 unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
100 unsigned char rtcpbuf[8192 + AST_FRIENDLY_OFFSET];
101 };
102
103 struct ast_srtp_policy {
104 srtp_policy_t sp;
105 };
106
107 /*! Tracks whether or not we've initialized the libsrtp library */
108 static int g_initialized = 0;
109
110 /* SRTP functions */
111 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
112 static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
113 static void ast_srtp_destroy(struct ast_srtp *srtp);
114 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
115 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
116
117 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
118 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
119 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
120 static int ast_srtp_get_random(unsigned char *key, size_t len);
121
122 /* Policy functions */
123 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
124 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
125 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
126 static int ast_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len);
127 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
128
129 static struct ast_srtp_res srtp_res = {
130 .create = ast_srtp_create,
131 .replace = ast_srtp_replace,
132 .destroy = ast_srtp_destroy,
133 .add_stream = ast_srtp_add_stream,
134 .change_source = ast_srtp_change_source,
135 .set_cb = ast_srtp_set_cb,
136 .unprotect = ast_srtp_unprotect,
137 .protect = ast_srtp_protect,
138 .get_random = ast_srtp_get_random
139 };
140
141 static struct ast_srtp_policy_res policy_res = {
142 .alloc = ast_srtp_policy_alloc,
143 .destroy = ast_srtp_policy_destroy,
144 .set_suite = ast_srtp_policy_set_suite,
145 .set_master_key = ast_srtp_policy_set_master_key,
146 .set_ssrc = ast_srtp_policy_set_ssrc
147 };
148
149 static const char *srtp_errstr(int err)
150 {
151 switch(err) {
152 case err_status_ok:
153 return "nothing to report";
154 case err_status_fail:
155 return "unspecified failure";
156 case err_status_bad_param:
157 return "unsupported parameter";
158 case err_status_alloc_fail:
159 return "couldn't allocate memory";
160 case err_status_dealloc_fail:
161 return "couldn't deallocate properly";
162 case err_status_init_fail:
163 return "couldn't initialize";
164 case err_status_terminus:
165 return "can't process as much data as requested";
166 case err_status_auth_fail:
167 return "authentication failure";
168 case err_status_cipher_fail:
169 return "cipher failure";
170 case err_status_replay_fail:
171 return "replay check failed (bad index)";
172 case err_status_replay_old:
173 return "replay check failed (index too old)";
174 case err_status_algo_fail:
175 return "algorithm failed test routine";
176 case err_status_no_such_op:
177 return "unsupported operation";
178 case err_status_no_ctx:
179 return "no appropriate context found";
180 case err_status_cant_check:
181 return "unable to perform desired validation";
182 case err_status_key_expired:
183 return "can't use key any more";
184 default:
185 return "unknown";
186 }
187 }
188
189 static int policy_hash_fn(const void *obj, const int flags)
190 {
191 const struct ast_srtp_policy *policy = obj;
192
193 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
194 }
195
196 static int policy_cmp_fn(void *obj, void *arg, int flags)
197 {
198 const struct ast_srtp_policy *one = obj, *two = arg;
199
200 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
201 }
202
203 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
204 {
205 struct ast_srtp_policy tmp = {
206 .sp = {
207 .ssrc.type = policy->ssrc.type,
208 .ssrc.value = policy->ssrc.value,
209 },
210 };
211
212 return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
213 }
214
215 static struct ast_srtp *res_srtp_new(void)
216 {
217 struct ast_srtp *srtp;
218
219 if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
220 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
221 return NULL;
222 }
223
224 srtp->policies = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 5,
225 policy_hash_fn, NULL, policy_cmp_fn, "SRTP policy container");
226 if (!srtp->policies) {
227 ast_free(srtp);
228 return NULL;
229 }
230
231 srtp->warned = 1;
232
233 return srtp;
234 }
235
236 /*
237 struct ast_srtp_policy
238 */
239 static void srtp_event_cb(srtp_event_data_t *data)
240 {
241 switch (data->event) {
242 case event_ssrc_collision:
243 ast_debug(1, "SSRC collision\n");
244 break;
245 case event_key_soft_limit:
246 ast_debug(1, "event_key_soft_limit\n");
247 break;
248 case event_key_hard_limit:
249 ast_debug(1, "event_key_hard_limit\n");
250 break;
251 case event_packet_index_limit:
252 ast_debug(1, "event_packet_index_limit\n");
253 break;
254 }
255 }
256
257 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
258 unsigned long ssrc, int inbound)
259 {
260 if (ssrc) {
261 policy->sp.ssrc.type = ssrc_specific;
262 policy->sp.ssrc.value = ssrc;
263 } else {
264 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
265 }
266 }
267
268 static void policy_destructor(void *obj)
269 {
270 struct ast_srtp_policy *policy = obj;
271
272 if (policy->sp.key) {
273 ast_free(policy->sp.key);
274 policy->sp.key = NULL;
275 }
276 }
277
278 static struct ast_srtp_policy *ast_srtp_policy_alloc()
279 {
280 struct ast_srtp_policy *tmp;
281
282 if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
283 ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
284 }
285
286 return tmp;
287 }
288
289 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
290 {
291 ao2_t_ref(policy, -1, "Destroying policy");
292 }
293
294 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
295 {
296 switch (suite) {
297 case AST_AES_CM_128_HMAC_SHA1_80:
298 crypto_policy_set_aes_cm_128_hmac_sha1_80(p);
299 return 0;
300
301 case AST_AES_CM_128_HMAC_SHA1_32:
302 crypto_policy_set_aes_cm_128_hmac_sha1_32(p);
303 return 0;
304
305 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
306 case AST_AES_CM_192_HMAC_SHA1_80:
307 crypto_policy_set_aes_cm_192_hmac_sha1_80(p);
308 return 0;
309
310 case AST_AES_CM_192_HMAC_SHA1_32:
311 crypto_policy_set_aes_cm_192_hmac_sha1_32(p);
312 return 0;
313 #endif
314 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
315 case AST_AES_CM_256_HMAC_SHA1_80:
316 crypto_policy_set_aes_cm_256_hmac_sha1_80(p);
317 return 0;
318
319 case AST_AES_CM_256_HMAC_SHA1_32:
320 crypto_policy_set_aes_cm_256_hmac_sha1_32(p);
321 return 0;
322 #endif
323 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
324 case AST_AES_GCM_128:
325 crypto_policy_set_aes_gcm_128_16_auth(p);
326 return 0;
327
328 case AST_AES_GCM_128_8:
329 crypto_policy_set_aes_gcm_128_8_auth(p);
330 return 0;
331 #endif
332 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
333 case AST_AES_GCM_256:
334 crypto_policy_set_aes_gcm_256_16_auth(p);
335 return 0;
336
337 case AST_AES_GCM_256_8:
338 crypto_policy_set_aes_gcm_256_8_auth(p);
339 return 0;
340 #endif
341
342 default:
343 ast_log(LOG_ERROR, "Invalid crypto suite: %u\n", suite);
344 return -1;
345 }
346 }
347
348 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
349 {
350 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
351 }
352
353 static int ast_srtp_policy_set_master_key(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
354 {
355 size_t size = key_len + salt_len;
356 unsigned char *master_key;
357
358 if (policy->sp.key) {
359 ast_free(policy->sp.key);
360 policy->sp.key = NULL;
361 }
362
363 if (!(master_key = ast_calloc(1, size))) {
364 return -1;
365 }
366
367 memcpy(master_key, key, key_len);
368 memcpy(master_key + key_len, salt, salt_len);
369
370 policy->sp.key = master_key;
371
372 return 0;
373 }
374
375 static int ast_srtp_get_random(unsigned char *key, size_t len)
376 {
377 #ifdef HAVE_OPENSSL
378 return RAND_bytes(key, len) > 0 ? 0: -1;
379 #else
380 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
381 #endif
382 }
383
384 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
385 {
386 if (!srtp) {
387 return;
388 }
389
390 srtp->cb = cb;
391 srtp->data = data;
392 }
393
394 /* Vtable functions */
395 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int flags)
396 {
397 int res = 0;
398 int i;
399 int rtcp = (flags & 0x01) >> 0;
400 int retry = (flags & 0x02) >> 1;
401 struct ast_rtp_instance_stats stats = {0,};
402
403 tryagain:
404
405 if (!srtp->session) {
406 ast_log(LOG_ERROR, "SRTP unprotect %s - missing session\n", rtcp ? "rtcp" : "rtp");
407 errno = EINVAL;
408 return -1;
409 }
410
411 for (i = 0; i < 2; i++) {
412 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
413 if (res != err_status_no_ctx) {
414 break;
415 }
416
417 if (srtp->cb && srtp->cb->no_ctx) {
418 if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
419 break;
420 }
421 if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
422 break;
423 }
424 } else {
425 break;
426 }
427 }
428
429 if (retry == 0 && res == err_status_replay_old) {
430 ast_log(AST_LOG_NOTICE, "SRTP unprotect failed with %s, retrying\n", srtp_errstr(res));
431
432 if (srtp->session) {
433 struct ast_srtp_policy *policy;
434 struct ao2_iterator it;
435 int policies_count;
436
437 /* dealloc first */
438 ast_debug(5, "SRTP destroy before re-create\n");
439 srtp_dealloc(srtp->session);
440
441 /* get the count */
442 policies_count = ao2_container_count(srtp->policies);
443
444 /* get the first to build up */
445 it = ao2_iterator_init(srtp->policies, 0);
446 policy = ao2_iterator_next(&it);
447
448 ast_debug(5, "SRTP try to re-create\n");
449 if (policy) {
450 int res_srtp_create = srtp_create(&srtp->session, &policy->sp);
451 if (res_srtp_create == err_status_ok) {
452 ast_debug(5, "SRTP re-created with first policy\n");
453 ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
454
455 /* if we have more than one policy, add them */
456 if (policies_count > 1) {
457 ast_debug(5, "Add all the other %d policies\n",
458 policies_count - 1);
459 while ((policy = ao2_iterator_next(&it))) {
460 srtp_add_stream(srtp->session, &policy->sp);
461 ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
462 }
463 }
464
465 retry++;
466 ao2_iterator_destroy(&it);
467 goto tryagain;
468 }
469 ast_log(LOG_ERROR, "SRTP session could not be re-created after unprotect failure: %s\n", srtp_errstr(res_srtp_create));
470
471 /* If srtp_create() fails with a previously alloced session, it will have been dealloced before returning. */
472 srtp->session = NULL;
473
474 ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
475 }
476 ao2_iterator_destroy(&it);
477 }
478 }
479
480 if (!srtp->session) {
481 errno = EINVAL;
482 return -1;
483 }
484
485 if (res != err_status_ok && res != err_status_replay_fail ) {
486 /*
487 * Authentication failures happen when an active attacker tries to
488 * insert malicious RTP packets. Furthermore, authentication failures
489 * happen, when the other party encrypts the sRTP data in an unexpected
490 * way. This happens quite often with RTCP. Therefore, when you see
491 * authentication failures, try to identify the implementation
492 * (author and product name) used by your other party. Try to investigate
493 * whether they use a custom library or an outdated version of libSRTP.
494 */
495 if (rtcp) {
496 ast_verb(2, "SRTCP unprotect failed on SSRC %u because of %s\n",
497 ast_rtp_instance_get_ssrc(srtp->rtp), srtp_errstr(res));
498 } else {
499 if ((srtp->warned >= 10) && !((srtp->warned - 10) % 150)) {
500 ast_verb(2, "SRTP unprotect failed on SSRC %u because of %s %d\n",
501 ast_rtp_instance_get_ssrc(srtp->rtp), srtp_errstr(res), srtp->warned);
502 srtp->warned = 11;
503 } else {
504 srtp->warned++;
505 }
506 }
507 errno = EAGAIN;
508 return -1;
509 }
510
511 return *len;
512 }
513
514 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
515 {
516 int res;
517 unsigned char *localbuf;
518
519 if (!srtp->session) {
520 ast_log(LOG_ERROR, "SRTP protect %s - missing session\n", rtcp ? "rtcp" : "rtp");
521 errno = EINVAL;
522 return -1;
523 }
524
525 if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
526 return -1;
527 }
528
529 localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
530
531 memcpy(localbuf, *buf, *len);
532
533 if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
534 ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
535 return -1;
536 }
537
538 *buf = localbuf;
539 return *len;
540 }
541
542 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
543 {
544 struct ast_srtp *temp;
545 int status;
546
547 if (!(temp = res_srtp_new())) {
548 return -1;
549 }
550 ast_module_ref(ast_module_info->self);
551
552 /* Any failures after this point can use ast_srtp_destroy to destroy the instance */
553 status = srtp_create(&temp->session, &policy->sp);
554 if (status != err_status_ok) {
555 /* Session either wasn't created or was created and dealloced. */
556 temp->session = NULL;
557 ast_srtp_destroy(temp);
558 ast_log(LOG_ERROR, "Failed to create srtp session on rtp instance (%p) - %s\n",
559 rtp, srtp_errstr(status));
560 return -1;
561 }
562
563 temp->rtp = rtp;
564 *srtp = temp;
565
566 ao2_t_link((*srtp)->policies, policy, "Created initial policy");
567
568 return 0;
569 }
570
571 static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
572 {
573 struct ast_srtp *old = *srtp;
574 int res = ast_srtp_create(srtp, rtp, policy);
575
576 if (!res && old) {
577 ast_srtp_destroy(old);
578 }
579
580 if (res) {
581 ast_log(LOG_ERROR, "Failed to replace srtp (%p) on rtp instance (%p) "
582 "- keeping old\n", *srtp, rtp);
583 }
584
585 return res;
586 }
587
588 static void ast_srtp_destroy(struct ast_srtp *srtp)
589 {
590 if (srtp->session) {
591 srtp_dealloc(srtp->session);
592 }
593
594 ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
595 ao2_t_ref(srtp->policies, -1, "Destroying container");
596
597 ast_free(srtp);
598 ast_module_unref(ast_module_info->self);
599 }
600
601 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
602 {
603 struct ast_srtp_policy *match;
604
605 /* For existing streams, replace if its an SSRC stream, or bail if its a wildcard */
606 if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
607 if (policy->sp.ssrc.type != ssrc_specific) {
608 ast_log(AST_LOG_WARNING, "Cannot replace an existing wildcard policy\n");
609 ao2_t_ref(match, -1, "Unreffing already existing policy");
610 return -1;
611 } else {
612 if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
613 ast_log(AST_LOG_WARNING, "Failed to remove SRTP stream for SSRC %u\n", match->sp.ssrc.value);
614 }
615 ao2_t_unlink(srtp->policies, match, "Remove existing match policy");
616 ao2_t_ref(match, -1, "Unreffing already existing policy");
617 }
618 }
619
620 ast_debug(3, "Adding new policy for %s %u\n",
621 policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
622 policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
623 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
624 ast_log(AST_LOG_WARNING, "Failed to add SRTP stream for %s %u\n",
625 policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
626 policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
627 return -1;
628 }
629
630 ao2_t_link(srtp->policies, policy, "Added additional stream");
631
632 return 0;
633 }
634
635 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
636 {
637 struct ast_srtp_policy *match;
638 struct srtp_policy_t sp = {
639 .ssrc.type = ssrc_specific,
640 .ssrc.value = from_ssrc,
641 };
642 err_status_t status;
643
644 /* If we find a match, return and unlink it from the container so we
645 * can change the SSRC (which is part of the hash) and then have
646 * ast_srtp_add_stream link it back in if all is well */
647 if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
648 match->sp.ssrc.value = to_ssrc;
649 if (ast_srtp_add_stream(srtp, match)) {
650 ast_log(LOG_WARNING, "Couldn't add stream\n");
651 } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
652 ast_debug(3, "Couldn't remove stream (%u)\n", status);
653 }
654 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
655 }
656
657 return 0;
658 }
659
660 struct ast_sdp_crypto {
661 char *a_crypto;
662 unsigned char local_key[SRTP_MAX_KEY_LEN];
663 int tag;
664 char local_key64[((SRTP_MAX_KEY_LEN) * 8 + 5) / 6 + 1];
665 unsigned char remote_key[SRTP_MAX_KEY_LEN];
666 int key_len;
667 };
668
669 static void res_sdp_crypto_dtor(struct ast_sdp_crypto *crypto)
670 {
671 if (crypto) {
672 ast_free(crypto->a_crypto);
673 crypto->a_crypto = NULL;
674 ast_free(crypto);
675
676 ast_module_unref(ast_module_info->self);
677 }
678 }
679
680 static struct ast_sdp_crypto *crypto_init_keys(struct ast_sdp_crypto *p, const int key_len)
681 {
682 unsigned char remote_key[key_len];
683
684 if (srtp_res.get_random(p->local_key, key_len) < 0) {
685 return NULL;
686 }
687
688 ast_base64encode(p->local_key64, p->local_key, key_len, sizeof(p->local_key64));
689
690 p->key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
691
692 if (p->key_len != key_len) {
693 ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", p->key_len, key_len);
694 return NULL;
695 }
696
697 if (memcmp(remote_key, p->local_key, p->key_len)) {
698 ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
699 return NULL;
700 }
701
702 ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
703
704 return p;
705 }
706
707 static struct ast_sdp_crypto *sdp_crypto_alloc(const int key_len)
708 {
709 struct ast_sdp_crypto *p, *result;
710
711 if (!(p = ast_calloc(1, sizeof(*p)))) {
712 return NULL;
713 }
714 p->tag = 1;
715 ast_module_ref(ast_module_info->self);
716
717 /* default is a key which uses AST_AES_CM_128_HMAC_SHA1_xx */
718 result = crypto_init_keys(p, key_len);
719 if (!result) {
720 res_sdp_crypto_dtor(p);
721 }
722
723 return result;
724 }
725
726 static struct ast_sdp_crypto *res_sdp_crypto_alloc(void)
727 {
728 return sdp_crypto_alloc(SRTP_MASTER_KEY_LEN);
729 }
730
731 static int res_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
732 {
733 int res;
734
735 /* Rebuild the crypto line */
736 ast_free(p->a_crypto);
737 p->a_crypto = NULL;
738
739 if ((taglen & 0x007f) == 8) {
740 res = ast_asprintf(&p->a_crypto, "%d AEAD_AES_%d_GCM_%d inline:%s",
741 p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
742 } else if ((taglen & 0x007f) == 16) {
743 res = ast_asprintf(&p->a_crypto, "%d AEAD_AES_%d_GCM inline:%s",
744 p->tag, 128 + ((taglen & 0x0300) >> 2), p->local_key64);
745 } else if ((taglen & 0x0300) && !(taglen & 0x0080)) {
746 res = ast_asprintf(&p->a_crypto, "%d AES_%d_CM_HMAC_SHA1_%d inline:%s",
747 p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
748 } else {
749 res = ast_asprintf(&p->a_crypto, "%d AES_CM_%d_HMAC_SHA1_%d inline:%s",
750 p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
751 }
752 if (res == -1 || !p->a_crypto) {
753 ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
754 return -1;
755 }
756
757 ast_debug(1, "Crypto line: a=crypto:%s\n", p->a_crypto);
758
759 return 0;
760 }
761
762 static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, int key_len, unsigned long ssrc, int inbound)
763 {
764 if (policy_res.set_master_key(policy, master_key, key_len, NULL, 0) < 0) {
765 return -1;
766 }
767
768 if (policy_res.set_suite(policy, suite_val)) {
769 ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
770 return -1;
771 }
772
773 policy_res.set_ssrc(policy, ssrc, inbound);
774
775 return 0;
776 }
777
778 static int crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, int key_len, struct ast_rtp_instance *rtp)
779 {
780 struct ast_srtp_policy *local_policy = NULL;
781 struct ast_srtp_policy *remote_policy = NULL;
782 struct ast_rtp_instance_stats stats = {0,};
783 int res = -1;
784
785 if (!p) {
786 return -1;
787 }
788
789 if (!(local_policy = policy_res.alloc())) {
790 return -1;
791 }
792
793 if (!(remote_policy = policy_res.alloc())) {
794 goto err;
795 }
796
797 if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
798 goto err;
799 }
800
801 if (set_crypto_policy(local_policy, suite_val, p->local_key, key_len, stats.local_ssrc, 0) < 0) {
802 goto err;
803 }
804
805 if (set_crypto_policy(remote_policy, suite_val, remote_key, key_len, 0, 1) < 0) {
806 goto err;
807 }
808
809 /* Add the SRTP policies */
810 if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy, 0)) {
811 ast_log(LOG_WARNING, "Could not set SRTP policies\n");
812 goto err;
813 }
814
815 ast_debug(1 , "SRTP policy activated\n");
816 res = 0;
817
818 err:
819 if (local_policy) {
820 policy_res.destroy(local_policy);
821 }
822
823 if (remote_policy) {
824 policy_res.destroy(remote_policy);
825 }
826
827 return res;
828 }
829
830 static int res_sdp_crypto_parse_offer(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
831 {
832 char *str = NULL;
833 char *tag = NULL;
834 char *suite = NULL;
835 char *key_params = NULL;
836 char *key_param = NULL;
837 char *session_params = NULL;
838 char *key_salt = NULL; /* The actual master key and key salt */
839 char *lifetime = NULL; /* Key lifetime (# of RTP packets) */
840 char *mki = NULL; /* Master Key Index */
841 int found = 0;
842 int key_len_from_sdp;
843 int key_len_expected;
844 int tag_from_sdp;
845 int suite_val = 0;
846 unsigned char remote_key[SRTP_MAX_KEY_LEN];
847 int taglen;
848 double sdes_lifetime;
849 struct ast_sdp_crypto *crypto;
850 struct ast_sdp_srtp *tmp;
851
852 str = ast_strdupa(attr);
853
854 tag = strsep(&str, " ");
855 suite = strsep(&str, " ");
856 key_params = strsep(&str, " ");
857 session_params = strsep(&str, " ");
858
859 if (!tag || !suite) {
860 ast_log(LOG_WARNING, "Unrecognized crypto attribute a=%s\n", attr);
861 return -1;
862 }
863
864 /* RFC4568 9.1 - tag is 1-9 digits */
865 if (sscanf(tag, "%30d", &tag_from_sdp) != 1 || tag_from_sdp < 0 || tag_from_sdp > 999999999) {
866 ast_log(LOG_WARNING, "Unacceptable a=crypto tag: %s\n", tag);
867 return -1;
868 }
869
870 if (!ast_strlen_zero(session_params)) {
871 ast_log(LOG_WARNING, "Unsupported crypto parameters: %s\n", session_params);
872 return -1;
873 }
874
875 /* On egress, Asterisk sent several crypto lines in the SIP/SDP offer
876 The remote party might have choosen another line than the first */
877 for (tmp = srtp; tmp && tmp->crypto && tmp->crypto->tag != tag_from_sdp;) {
878 tmp = AST_LIST_NEXT(tmp, sdp_srtp_list);
879 }
880 if (tmp) { /* tag matched an already created crypto line */
881 unsigned int flags = tmp->flags;
882
883 /* Make that crypto line the head of the list, not by changing the
884 list structure but by exchanging the content of the list members */
885 crypto = tmp->crypto;
886 tmp->crypto = srtp->crypto;
887 tmp->flags = srtp->flags;
888 srtp->crypto = crypto;
889 srtp->flags = flags;
890 } else {
891 crypto = srtp->crypto;
892 crypto->tag = tag_from_sdp;
893 }
894
895 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
896 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
897 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
898 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
899 ast_clear_flag(srtp, AST_SRTP_CRYPTO_AES_192);
900 ast_clear_flag(srtp, AST_SRTP_CRYPTO_AES_256);
901 ast_clear_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
902
903 if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
904 suite_val = AST_AES_CM_128_HMAC_SHA1_80;
905 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
906 key_len_expected = 30;
907 } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
908 suite_val = AST_AES_CM_128_HMAC_SHA1_32;
909 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
910 key_len_expected = 30;
911 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
912 } else if (!strcmp(suite, "AES_192_CM_HMAC_SHA1_80")) {
913 suite_val = AST_AES_CM_192_HMAC_SHA1_80;
914 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
915 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
916 key_len_expected = 38;
917 } else if (!strcmp(suite, "AES_192_CM_HMAC_SHA1_32")) {
918 suite_val = AST_AES_CM_192_HMAC_SHA1_32;
919 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
920 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
921 key_len_expected = 38;
922 /* RFC used a different name while in draft, some still use that */
923 } else if (!strcmp(suite, "AES_CM_192_HMAC_SHA1_80")) {
924 suite_val = AST_AES_CM_192_HMAC_SHA1_80;
925 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
926 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
927 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
928 key_len_expected = 38;
929 } else if (!strcmp(suite, "AES_CM_192_HMAC_SHA1_32")) {
930 suite_val = AST_AES_CM_192_HMAC_SHA1_32;
931 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
932 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
933 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
934 key_len_expected = 38;
935 #endif
936 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
937 } else if (!strcmp(suite, "AES_256_CM_HMAC_SHA1_80")) {
938 suite_val = AST_AES_CM_256_HMAC_SHA1_80;
939 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
940 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
941 key_len_expected = 46;
942 } else if (!strcmp(suite, "AES_256_CM_HMAC_SHA1_32")) {
943 suite_val = AST_AES_CM_256_HMAC_SHA1_32;
944 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
945 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
946 key_len_expected = 46;
947 /* RFC used a different name while in draft, some still use that */
948 } else if (!strcmp(suite, "AES_CM_256_HMAC_SHA1_80")) {
949 suite_val = AST_AES_CM_256_HMAC_SHA1_80;
950 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
951 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
952 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
953 key_len_expected = 46;
954 } else if (!strcmp(suite, "AES_CM_256_HMAC_SHA1_32")) {
955 suite_val = AST_AES_CM_256_HMAC_SHA1_32;
956 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
957 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
958 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
959 key_len_expected = 46;
960 #endif
961 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
962 } else if (!strcmp(suite, "AEAD_AES_128_GCM")) {
963 suite_val = AST_AES_GCM_128;
964 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
965 key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
966 /* RFC contained a (too) short auth tag for RTP media, some still use that */
967 } else if (!strcmp(suite, "AEAD_AES_128_GCM_8")) {
968 suite_val = AST_AES_GCM_128_8;
969 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
970 key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
971 #endif
972 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
973 } else if (!strcmp(suite, "AEAD_AES_256_GCM")) {
974 suite_val = AST_AES_GCM_256;
975 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
976 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
977 key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
978 /* RFC contained a (too) short auth tag for RTP media, some still use that */
979 } else if (!strcmp(suite, "AEAD_AES_256_GCM_8")) {
980 suite_val = AST_AES_GCM_256_8;
981 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
982 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
983 key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
984 #endif
985 } else {
986 ast_debug(3, "Unsupported crypto suite: %s\n", suite);
987 return -1;
988 }
989
990 while ((key_param = strsep(&key_params, ";"))) {
991 unsigned int n_lifetime;
992 char *method = NULL;
993 char *info = NULL;
994
995 method = strsep(&key_param, ":");
996 info = strsep(&key_param, ";");
997 sdes_lifetime = 0;
998
999 if (strcmp(method, "inline")) {
1000 continue;
1001 }
1002
1003 key_salt = strsep(&info, "|");
1004
1005 /* The next parameter can be either lifetime or MKI */
1006 lifetime = strsep(&info, "|");
1007 if (!lifetime) {
1008 found = 1;
1009 break;
1010 }
1011
1012 mki = strchr(lifetime, ':');
1013 if (mki) {
1014 mki = lifetime;
1015 lifetime = NULL;
1016 } else {
1017 mki = strsep(&info, "|");
1018 }
1019
1020 if (mki && *mki != '1') {
1021 ast_log(LOG_NOTICE, "Crypto MKI handling is not supported: ignoring attribute %s\n", attr);
1022 continue;
1023 }
1024
1025 if (lifetime) {
1026 if (!strncmp(lifetime, "2^", 2)) {
1027 char *lifetime_val = lifetime + 2;
1028
1029 /* Exponential lifetime */
1030 if (sscanf(lifetime_val, "%30u", &n_lifetime) != 1) {
1031 ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
1032 continue;
1033 }
1034
1035 if (n_lifetime > 48) {
1036 /* Yeah... that's a bit big. */
1037 ast_log(LOG_NOTICE, "Crypto lifetime exponent of '%u' is a bit large; using 48\n", n_lifetime);
1038 n_lifetime = 48;
1039 }
1040 sdes_lifetime = pow(2, n_lifetime);
1041 } else {
1042 /* Decimal lifetime */
1043 if (sscanf(lifetime, "%30u", &n_lifetime) != 1) {
1044 ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
1045 continue;
1046 }
1047 sdes_lifetime = n_lifetime;
1048 }
1049
1050 /* Accept anything above ~5.8 hours. Less than ~5.8; reject. */
1051 if (sdes_lifetime < 1048576) {
1052 ast_log(LOG_NOTICE, "Rejecting crypto attribute '%s': lifetime '%f' too short\n", attr, sdes_lifetime);
1053 continue;
1054 }
1055 }
1056
1057 ast_debug(2, "Crypto attribute '%s' accepted with lifetime '%f', MKI '%s'\n",
1058 attr, sdes_lifetime, mki ? mki : "-");
1059
1060 found = 1;
1061 break;
1062 }
1063
1064 if (!found) {
1065 ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable: '%s'\n", attr);
1066 return -1;
1067 }
1068
1069 key_len_from_sdp = ast_base64decode(remote_key, key_salt, sizeof(remote_key));
1070 if (key_len_from_sdp != key_len_expected) {
1071 ast_log(LOG_WARNING, "SRTP descriptions key length is '%d', not '%d'\n",
1072 key_len_from_sdp, key_len_expected);
1073 return -1;
1074 }
1075
1076 /* on default, the key is 30 (AES-128); throw that away (only) when the suite changed actually */
1077 /* ingress: optional, but saves one expensive call to get_random(.) */
1078 /* egress: required, because the local key was communicated before the remote key is processed */
1079 if (crypto->key_len != key_len_from_sdp) {
1080 if (!crypto_init_keys(crypto, key_len_from_sdp)) {
1081 return -1;
1082 }
1083 } else if (!memcmp(crypto->remote_key, remote_key, key_len_from_sdp)) {
1084 ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
1085 return 0;
1086 }
1087
1088 if (key_len_from_sdp > sizeof(crypto->remote_key)) {
1089 ast_log(LOG_ERROR,
1090 "SRTP key buffer is %zu although it must be at least %d bytes\n",
1091 sizeof(crypto->remote_key), key_len_from_sdp);
1092 return -1;
1093 }
1094 memcpy(crypto->remote_key, remote_key, key_len_from_sdp);
1095
1096 if (crypto_activate(crypto, suite_val, remote_key, key_len_from_sdp, rtp) < 0) {
1097 return -1;
1098 }
1099
1100 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
1101 taglen = 32;
1102 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
1103 taglen = 16;
1104 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
1105 taglen = 8;
1106 } else {
1107 taglen = 80;
1108 }
1109 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_256)) {
1110 taglen |= 0x0200;
1111 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1112 taglen |= 0x0100;
1113 }
1114 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME)) {
1115 taglen |= 0x0080;
1116 }
1117
1118 /* Finally, rebuild the crypto line */
1119 if (res_sdp_crypto_build_offer(crypto, taglen)) {
1120 return -1;
1121 }
1122
1123 ast_set_flag(srtp, AST_SRTP_CRYPTO_OFFER_OK);
1124 return 0;
1125 }
1126
1127 static const char *res_sdp_srtp_get_attr(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
1128 {
1129 int taglen;
1130
1131 if (!srtp) {
1132 return NULL;
1133 }
1134
1135 /* Set encryption properties */
1136 if (!srtp->crypto) {
1137 if (AST_LIST_NEXT(srtp, sdp_srtp_list)) {
1138 srtp->crypto = res_sdp_crypto_alloc();
1139 ast_log(LOG_ERROR, "SRTP SDP list was not empty\n");
1140 } else {
1141 const int len = default_taglen_32 ? AST_SRTP_CRYPTO_TAG_32 : AST_SRTP_CRYPTO_TAG_80;
1142 const int attr[][3] = {
1143 /* This array creates the following list:
1144 * a=crypto:1 AES_CM_128_HMAC_SHA1_ ...
1145 * a=crypto:2 AEAD_AES_128_GCM ...
1146 * a=crypto:3 AES_256_CM_HMAC_SHA1_ ...
1147 * a=crypto:4 AEAD_AES_256_GCM ...
1148 * a=crypto:5 AES_192_CM_HMAC_SHA1_ ...
1149 * something like 'AEAD_AES_192_GCM' is not specified by the RFCs
1150 *
1151 * If you want to prefer another crypto suite or you want to
1152 * exclude a suite, change this array and recompile Asterisk.
1153 * This list cannot be changed from rtp.conf because you should
1154 * know what you are doing. Especially AES-192 and AES-GCM are
1155 * broken in many VoIP clients, see
1156 * https://github.com/cisco/libsrtp/pull/170
1157 * https://github.com/cisco/libsrtp/pull/184
1158 * Furthermore, AES-GCM uses a shorter crypto-suite string which
1159 * causes Nokia phones based on Symbian/S60 to reject the whole
1160 * INVITE with status 500, even if a matching suite was offered.
1161 * AES-256 might just waste your processor cycles, especially if
1162 * your TLS transport is not secured with equivalent grade, see
1163 * https://security.stackexchange.com/q/61361
1164 * Therefore, AES-128 was preferred here.
1165 *
1166 * If you want to enable one of those defines, please, go for
1167 * CFLAGS='-DENABLE_SRTP_AES_GCM' ./configure && sudo make install
1168 */
1169 { len, 0, 30 },
1170 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
1171 { AST_SRTP_CRYPTO_TAG_16, 0, AES_128_GCM_KEYSIZE_WSALT },
1172 #endif
1173 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
1174 { len, AST_SRTP_CRYPTO_AES_256, 46 },
1175 #endif
1176 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
1177 { AST_SRTP_CRYPTO_TAG_16, AST_SRTP_CRYPTO_AES_256, AES_256_GCM_KEYSIZE_WSALT },
1178 #endif
1179 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
1180 { len, AST_SRTP_CRYPTO_AES_192, 38 },
1181 #endif
1182 };
1183 struct ast_sdp_srtp *tmp = srtp;
1184 int i;
1185
1186 for (i = 0; i < ARRAY_LEN(attr); i++) {
1187 if (attr[i][0]) {
1188 ast_set_flag(tmp, attr[i][0]);
1189 }
1190 if (attr[i][1]) {
1191 ast_set_flag(tmp, attr[i][1]);
1192 }
1193 tmp->crypto = sdp_crypto_alloc(attr[i][2]); /* key_len */
1194 tmp->crypto->tag = (i + 1); /* tag starts at 1 */
1195
1196 if (i < ARRAY_LEN(attr) - 1) {
1197 AST_LIST_NEXT(tmp, sdp_srtp_list) = ast_sdp_srtp_alloc();
1198 tmp = AST_LIST_NEXT(tmp, sdp_srtp_list);
1199 }
1200 }
1201 }
1202 }
1203
1204 if (dtls_enabled) {
1205 /* If DTLS-SRTP is enabled the key details will be pulled from TLS */
1206 return NULL;
1207 }
1208
1209 /* set the key length based on INVITE or settings */
1210 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_80)) {
1211 taglen = 80;
1212 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
1213 taglen = 32;
1214 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
1215 taglen = 16;
1216 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
1217 taglen = 8;
1218 } else {
1219 taglen = default_taglen_32 ? 32 : 80;
1220 }
1221 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_256)) {
1222 taglen |= 0x0200;
1223 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1224 taglen |= 0x0100;
1225 }
1226 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME)) {
1227 taglen |= 0x0080;
1228 }
1229
1230 if (srtp->crypto && (res_sdp_crypto_build_offer(srtp->crypto, taglen) >= 0)) {
1231 return srtp->crypto->a_crypto;
1232 }
1233
1234 ast_log(LOG_WARNING, "No SRTP key management enabled\n");
1235 return NULL;
1236 }
1237
1238 static struct ast_sdp_crypto_api res_sdp_crypto_api = {
1239 .dtor = res_sdp_crypto_dtor,
1240 .alloc = res_sdp_crypto_alloc,
1241 .build_offer = res_sdp_crypto_build_offer,
1242 .parse_offer = res_sdp_crypto_parse_offer,
1243 .get_attr = res_sdp_srtp_get_attr,
1244 };
1245
1246 static void res_srtp_shutdown(void)
1247 {
1248 ast_sdp_crypto_unregister(&res_sdp_crypto_api);
1249 ast_rtp_engine_unregister_srtp();
1250 srtp_install_event_handler(NULL);
1251 #ifdef HAVE_SRTP_SHUTDOWN
1252 srtp_shutdown();
1253 #endif
1254 g_initialized = 0;
1255 }
1256
1257 static int res_srtp_init(void)
1258 {
1259 if (g_initialized) {
1260 return 0;
1261 }
1262
1263 if (srtp_init() != err_status_ok) {
1264 ast_log(AST_LOG_WARNING, "Failed to initialize libsrtp\n");
1265 return -1;
1266 }
1267
1268 srtp_install_event_handler(srtp_event_cb);
1269
1270 if (ast_rtp_engine_register_srtp(&srtp_res, &policy_res)) {
1271 ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
1272 res_srtp_shutdown();
1273 return -1;
1274 }
1275
1276 if (ast_sdp_crypto_register(&res_sdp_crypto_api)) {
1277 ast_log(AST_LOG_WARNING, "Failed to register SDP SRTP crypto API\n");
1278 res_srtp_shutdown();
1279 return -1;
1280 }
1281
1282 #ifdef HAVE_SRTP_GET_VERSION
1283 ast_verb(2, "%s initialized\n", srtp_get_version_string());
1284 #else
1285 ast_verb(2, "libsrtp initialized\n");
1286 #endif
1287
1288 g_initialized = 1;
1289 return 0;
1290 }
1291
1292 /*
1293 * Exported functions
1294 */
1295
1296 static int load_module(void)
1297 {
1298 return res_srtp_init();
1299 }
1300
1301 static int unload_module(void)
1302 {
1303 res_srtp_shutdown();
1304 return 0;
1305 }
1306
1307 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
1308 .support_level = AST_MODULE_SUPPORT_CORE,
1309 .load = load_module,
1310 .unload = unload_module,
1311 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
1312 );