Asterisk - The Open Source Telephony Project GIT-master-0deac78
res_srtp.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005, Mikael Magnusson
5 *
6 * Mikael Magnusson <mikma@users.sourceforge.net>
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 <mikma@users.sourceforge.net>
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
92struct ast_srtp {
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
104 srtp_policy_t sp;
105};
106
107/*! Tracks whether or not we've initialized the libsrtp library */
108static int g_initialized = 0;
109
110/* SRTP functions */
111static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
112static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
113static void ast_srtp_destroy(struct ast_srtp *srtp);
114static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
115static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
116
117static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
118static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
119static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
120static int ast_srtp_get_random(unsigned char *key, size_t len);
121
122/* Policy functions */
123static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
124static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
125static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
126static 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);
127static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
128
129static struct ast_srtp_res srtp_res = {
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
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
149static 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";
157 return "unsupported parameter";
159 return "couldn't allocate memory";
161 return "couldn't deallocate properly";
163 return "couldn't initialize";
165 return "can't process as much data as requested";
167 return "authentication failure";
169 return "cipher failure";
171 return "replay check failed (bad index)";
173 return "replay check failed (index too old)";
175 return "algorithm failed test routine";
177 return "unsupported operation";
179 return "no appropriate context found";
181 return "unable to perform desired validation";
183 return "can't use key any more";
184 default:
185 return "unknown";
186 }
187}
188
189static 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
196static 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
203static 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
215static 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
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*/
239static 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
257static 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
268static 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
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
289static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
290{
291 ao2_t_ref(policy, -1, "Destroying policy");
292}
293
295{
296 switch (suite) {
299 return 0;
300
303 return 0;
304
305#if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
308 return 0;
309
312 return 0;
313#endif
314#if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
317 return 0;
318
321 return 0;
322#endif
323#if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
324 case AST_AES_GCM_128:
326 return 0;
327
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:
335 return 0;
336
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
348static 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
353static 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
375static 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
384static 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 */
395static 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
403tryagain:
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) {
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++;
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 }
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",
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
514static 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
542static 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 }
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",
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
571static 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
588static 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);
599}
600
601static 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
635static 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 };
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
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];
667};
668
669static 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
677 }
678}
679
680static 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
689
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
707static 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;
716
717 /* default is a key which uses AST_AES_CM_128_HMAC_SHA1_xx */
719 if (!result) {
721 }
722
723 return result;
724}
725
727{
729}
730
731static 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
762static 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
778static 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
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
818err:
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
830static 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
902
903 if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
904 suite_val = AST_AES_CM_128_HMAC_SHA1_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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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)) {
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
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 }
1110 taglen |= 0x0200;
1111 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1112 taglen |= 0x0100;
1113 }
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
1124 return 0;
1125}
1126
1127static 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)
1172#endif
1173#if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
1175#endif
1176#if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
1178#endif
1179#if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
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) {
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 */
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 }
1222 taglen |= 0x0200;
1223 } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1224 taglen |= 0x0100;
1225 }
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
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
1246static void res_srtp_shutdown(void)
1247{
1250 srtp_install_event_handler(NULL);
1251#ifdef HAVE_SRTP_SHUTDOWN
1252 srtp_shutdown();
1253#endif
1254 g_initialized = 0;
1255}
1256
1257static 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
1271 ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
1273 return -1;
1274 }
1275
1277 ast_log(AST_LOG_WARNING, "Failed to register SDP SRTP crypto API\n");
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
1296static int load_module(void)
1297{
1298 return res_srtp_init();
1299}
1300
1301static int unload_module(void)
1302{
1304 return 0;
1305}
1306
1308 .support_level = AST_MODULE_SUPPORT_CORE,
1309 .load = load_module,
1310 .unload = unload_module,
1311 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
jack_status_t status
Definition: app_jack.c:149
const char * str
Definition: app_jack.c:150
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ao2_t_ref(o, delta, tag)
Definition: astobj2.h:460
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
#define OBJ_POINTER
Definition: astobj2.h:1150
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_t_find(container, arg, flags, tag)
Definition: astobj2.h:1734
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_t_link(container, obj, tag)
Definition: astobj2.h:1534
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag)
Definition: astobj2.h:1306
#define ao2_t_unlink(container, obj, tag)
Definition: astobj2.h:1580
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_t_alloc(data_size, destructor_fn, debug_msg)
Definition: astobj2.h:407
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_MULTIPLE
Definition: astobj2.h:1049
@ OBJ_UNLINK
Definition: astobj2.h:1039
#define ao2_t_callback(c, flags, cb_fn, arg, tag)
Definition: astobj2.h:1696
static PGresult * result
Definition: cel_pgsql.c:84
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition: chan_iax2.c:2388
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Asterisk internal frame definitions.
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
Support for logging to various files, console and syslog Configuration in file logger....
#define AST_LOG_WARNING
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define AST_LOG_NOTICE
#define ast_verb(level,...)
#define LOG_NOTICE
#define LOG_WARNING
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
int errno
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:330
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
#define ast_module_ref(mod)
Hold a reference to the module.
Definition: module.h:457
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CHANNEL_DEPEND
Definition: module.h:340
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
def info(msg)
const char * method
Definition: res_pjsip.c:1279
#define SRTP_MASTER_KEY_LEN
static int policy_cmp_fn(void *obj, void *arg, int flags)
Definition: res_srtp.c:196
static struct ast_sdp_crypto * sdp_crypto_alloc(const int key_len)
Definition: res_srtp.c:707
static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
Definition: res_srtp.c:289
static int crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, int key_len, struct ast_rtp_instance *rtp)
Definition: res_srtp.c:778
static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:542
static const char * srtp_errstr(int err)
Definition: res_srtp.c:149
static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
Definition: res_srtp.c:348
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)
Definition: res_srtp.c:353
static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:601
static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
Definition: res_srtp.c:294
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)
Definition: res_srtp.c:762
static struct ast_sdp_crypto * res_sdp_crypto_alloc(void)
Definition: res_srtp.c:726
static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
Definition: res_srtp.c:635
static int res_srtp_init(void)
Definition: res_srtp.c:1257
static struct ast_sdp_crypto * crypto_init_keys(struct ast_sdp_crypto *p, const int key_len)
Definition: res_srtp.c:680
static int res_sdp_crypto_parse_offer(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
Definition: res_srtp.c:830
static struct ast_srtp * res_srtp_new(void)
Definition: res_srtp.c:215
static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.c:571
static struct ast_srtp_res srtp_res
Definition: res_srtp.c:129
static int ast_srtp_get_random(unsigned char *key, size_t len)
Definition: res_srtp.c:375
static void ast_srtp_destroy(struct ast_srtp *srtp)
Definition: res_srtp.c:588
static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
Definition: res_srtp.c:257
static struct ast_srtp_policy * ast_srtp_policy_alloc(void)
Definition: res_srtp.c:278
static void res_sdp_crypto_dtor(struct ast_sdp_crypto *crypto)
Definition: res_srtp.c:669
static int load_module(void)
Definition: res_srtp.c:1296
static int unload_module(void)
Definition: res_srtp.c:1301
static int policy_hash_fn(const void *obj, const int flags)
Definition: res_srtp.c:189
static struct ast_srtp_policy_res policy_res
Definition: res_srtp.c:141
static struct ast_srtp_policy * find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
Definition: res_srtp.c:203
static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
Definition: res_srtp.c:395
static void res_srtp_shutdown(void)
Definition: res_srtp.c:1246
static int res_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
Definition: res_srtp.c:731
static void srtp_event_cb(srtp_event_data_t *data)
Definition: res_srtp.c:239
static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
Definition: res_srtp.c:514
static const char * res_sdp_srtp_get_attr(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
Definition: res_srtp.c:1127
static struct ast_sdp_crypto_api res_sdp_crypto_api
Definition: res_srtp.c:1238
static void policy_destructor(void *obj)
Definition: res_srtp.c:268
static int g_initialized
Definition: res_srtp.c:108
static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
Definition: res_srtp.c:384
SRTP resource.
ast_srtp_suite
Definition: res_srtp.h:56
@ AST_AES_CM_128_HMAC_SHA1_80
Definition: res_srtp.h:58
@ AST_AES_GCM_256
Definition: res_srtp.h:63
@ AST_AES_GCM_128
Definition: res_srtp.h:62
@ AST_AES_CM_128_HMAC_SHA1_32
Definition: res_srtp.h:59
@ AST_AES_CM_256_HMAC_SHA1_32
Definition: res_srtp.h:61
@ AST_AES_CM_192_HMAC_SHA1_80
Definition: res_srtp.h:66
@ AST_AES_CM_256_HMAC_SHA1_80
Definition: res_srtp.h:60
@ AST_AES_GCM_128_8
Definition: res_srtp.h:64
@ AST_AES_CM_192_HMAC_SHA1_32
Definition: res_srtp.h:67
@ AST_AES_GCM_256_8
Definition: res_srtp.h:65
#define NULL
Definition: resample.c:96
Pluggable RTP Architecture.
int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
Definition: rtp_engine.c:2916
void ast_rtp_engine_unregister_srtp(void)
Definition: rtp_engine.c:2931
@ AST_RTP_INSTANCE_STAT_LOCAL_SSRC
Definition: rtp_engine.h:249
@ AST_RTP_INSTANCE_STAT_REMOTE_SSRC
Definition: rtp_engine.h:251
int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
Retrieve statistics about an RTP instance.
Definition: rtp_engine.c:2628
unsigned int ast_rtp_instance_get_ssrc(struct ast_rtp_instance *rtp)
Retrieve the local SSRC value that we will be using.
Definition: rtp_engine.c:4017
int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
Add or replace the SRTP policies for the given RTP instance.
Definition: rtp_engine.c:2942
SRTP and SDP Security descriptions.
int ast_sdp_crypto_register(struct ast_sdp_crypto_api *api)
Register SDP SRTP crypto processing routines.
Definition: sdp_srtp.c:123
struct ast_sdp_srtp * ast_sdp_srtp_alloc(void)
allocate a ast_sdp_srtp structure
Definition: sdp_srtp.c:41
#define AST_SRTP_CRYPTO_TAG_16
Definition: sdp_srtp.h:48
#define AST_SRTP_CRYPTO_TAG_80
Definition: sdp_srtp.h:47
#define AST_SRTP_CRYPTO_AES_256
Definition: sdp_srtp.h:51
#define AST_SRTP_CRYPTO_TAG_32
Definition: sdp_srtp.h:46
void ast_sdp_crypto_unregister(struct ast_sdp_crypto_api *api)
Unregister SDP SRTP crypto processing routines.
Definition: sdp_srtp.c:132
#define AST_SRTP_CRYPTO_TAG_8
Definition: sdp_srtp.h:49
#define AST_SRTP_CRYPTO_AES_192
Definition: sdp_srtp.h:50
#define AST_SRTP_CRYPTO_OLD_NAME
Definition: sdp_srtp.h:52
#define AST_SRTP_CRYPTO_OFFER_OK
Definition: sdp_srtp.h:45
#define err_status_cipher_fail
Definition: srtp_compat.h:40
#define crypto_policy_set_aes_gcm_256_8_auth
Definition: srtp_compat.h:17
#define crypto_policy_set_aes_cm_192_hmac_sha1_80
Definition: srtp_compat.h:10
#define crypto_policy_set_aes_gcm_128_16_auth
Definition: srtp_compat.h:14
#define err_status_ok
Definition: srtp_compat.h:32
#define crypto_policy_set_aes_cm_128_hmac_sha1_32
Definition: srtp_compat.h:9
#define crypto_policy_set_aes_cm_128_hmac_sha1_80
Definition: srtp_compat.h:8
#define err_status_init_fail
Definition: srtp_compat.h:37
#define err_status_replay_old
Definition: srtp_compat.h:42
#define err_status_algo_fail
Definition: srtp_compat.h:43
#define crypto_policy_set_aes_gcm_256_16_auth
Definition: srtp_compat.h:15
#define AES_128_GCM_KEYSIZE_WSALT
Definition: srtp_compat.h:22
#define err_status_no_ctx
Definition: srtp_compat.h:45
#define crypto_policy_set_aes_cm_192_hmac_sha1_32
Definition: srtp_compat.h:11
#define err_status_dealloc_fail
Definition: srtp_compat.h:36
#define crypto_policy_set_aes_cm_256_hmac_sha1_32
Definition: srtp_compat.h:13
#define crypto_policy_set_aes_cm_256_hmac_sha1_80
Definition: srtp_compat.h:12
#define err_status_alloc_fail
Definition: srtp_compat.h:35
#define err_status_no_such_op
Definition: srtp_compat.h:44
#define err_status_key_expired
Definition: srtp_compat.h:47
#define AES_256_GCM_KEYSIZE_WSALT
Definition: srtp_compat.h:28
#define err_status_t
Definition: srtp_compat.h:31
#define crypto_policy_set_aes_gcm_128_8_auth
Definition: srtp_compat.h:16
#define err_status_cant_check
Definition: srtp_compat.h:46
#define err_status_bad_param
Definition: srtp_compat.h:34
#define err_status_auth_fail
Definition: srtp_compat.h:39
#define err_status_terminus
Definition: srtp_compat.h:38
#define err_status_replay_fail
Definition: srtp_compat.h:41
#define err_status_fail
Definition: srtp_compat.h:33
#define crypto_policy_t
Definition: srtp_compat.h:6
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
struct ast_module * self
Definition: module.h:356
unsigned int remote_ssrc
Definition: rtp_engine.h:454
unsigned int local_ssrc
Definition: rtp_engine.h:452
sdp_crypto_destroy_cb dtor
Definition: sdp_srtp.h:128
char * a_crypto
Definition: res_srtp.c:661
unsigned char local_key[SRTP_MAX_KEY_LEN]
Definition: res_srtp.c:662
char local_key64[((SRTP_MAX_KEY_LEN) *8+5)/6+1]
Definition: res_srtp.c:664
unsigned char remote_key[SRTP_MAX_KEY_LEN]
Definition: res_srtp.c:665
structure for secure RTP audio
Definition: sdp_srtp.h:38
struct ast_sdp_srtp::@283 sdp_srtp_list
struct ast_sdp_crypto * crypto
Definition: sdp_srtp.h:40
unsigned int flags
Definition: sdp_srtp.h:39
int(* no_ctx)(struct ast_rtp_instance *rtp, unsigned long ssrc, void *data)
Definition: res_srtp.h:31
void(* destroy)(struct ast_srtp_policy *policy)
Definition: res_srtp.h:72
int(* set_master_key)(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
Definition: res_srtp.h:74
void(* set_ssrc)(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
Definition: res_srtp.h:75
int(* set_suite)(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
Definition: res_srtp.h:73
struct ast_srtp_policy *(* alloc)(void)
Definition: res_srtp.h:71
srtp_policy_t sp
Definition: res_srtp.c:104
int(* get_random)(unsigned char *key, size_t len)
Definition: res_srtp.h:52
int(* create)(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
Definition: res_srtp.h:36
unsigned char buf[8192+AST_FRIENDLY_OFFSET]
Definition: res_srtp.c:99
struct ao2_container * policies
Definition: res_srtp.c:94
srtp_t session
Definition: res_srtp.c:95
void * data
Definition: res_srtp.c:97
const struct ast_srtp_cb * cb
Definition: res_srtp.c:96
int warned
Definition: res_srtp.c:98
struct ast_rtp_instance * rtp
Definition: res_srtp.c:93
unsigned char rtcpbuf[8192+AST_FRIENDLY_OFFSET]
Definition: res_srtp.c:100
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: utils.c:296
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: utils.c:406
#define ast_clear_flag(p, flag)
Definition: utils.h:77
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define ARRAY_LEN(a)
Definition: utils.h:703