44 #if HAVE_SRTP_VERSION > 1
45 # include <srtp2/srtp.h>
46 # include "srtp/srtp_compat.h"
47 # include <openssl/rand.h>
49 # include <srtp/srtp.h>
51 # include <openssl/rand.h>
53 # include <srtp/crypto_kernel.h>
87 static void ast_srtp_destroy(
struct ast_srtp *srtp);
89 static int ast_srtp_change_source(
struct ast_srtp *srtp,
unsigned int from_ssrc,
unsigned int to_ssrc);
91 static int ast_srtp_unprotect(
struct ast_srtp *srtp,
void *buf,
int *len,
int rtcp);
92 static int ast_srtp_protect(
struct ast_srtp *srtp,
void **buf,
int *len,
int rtcp);
93 static void ast_srtp_set_cb(
struct ast_srtp *srtp,
const struct ast_srtp_cb *cb,
void *data);
94 static int ast_srtp_get_random(
unsigned char *key,
size_t len);
99 static int ast_srtp_policy_set_suite(
struct ast_srtp_policy *policy,
enum ast_srtp_suite suite);
100 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);
101 static void ast_srtp_policy_set_ssrc(
struct ast_srtp_policy *policy,
unsigned long ssrc,
int inbound);
104 .
create = ast_srtp_create,
105 .replace = ast_srtp_replace,
106 .destroy = ast_srtp_destroy,
107 .add_stream = ast_srtp_add_stream,
108 .change_source = ast_srtp_change_source,
109 .set_cb = ast_srtp_set_cb,
110 .unprotect = ast_srtp_unprotect,
111 .protect = ast_srtp_protect,
112 .get_random = ast_srtp_get_random
116 .alloc = ast_srtp_policy_alloc,
117 .destroy = ast_srtp_policy_destroy,
118 .set_suite = ast_srtp_policy_set_suite,
119 .set_master_key = ast_srtp_policy_set_master_key,
120 .set_ssrc = ast_srtp_policy_set_ssrc
123 static const char *srtp_errstr(
int err)
127 return "nothing to report";
128 case err_status_fail:
129 return "unspecified failure";
130 case err_status_bad_param:
131 return "unsupported parameter";
132 case err_status_alloc_fail:
133 return "couldn't allocate memory";
134 case err_status_dealloc_fail:
135 return "couldn't deallocate properly";
136 case err_status_init_fail:
137 return "couldn't initialize";
138 case err_status_terminus:
139 return "can't process as much data as requested";
140 case err_status_auth_fail:
141 return "authentication failure";
142 case err_status_cipher_fail:
143 return "cipher failure";
144 case err_status_replay_fail:
145 return "replay check failed (bad index)";
146 case err_status_replay_old:
147 return "replay check failed (index too old)";
148 case err_status_algo_fail:
149 return "algorithm failed test routine";
150 case err_status_no_such_op:
151 return "unsupported operation";
152 case err_status_no_ctx:
153 return "no appropriate context found";
154 case err_status_cant_check:
155 return "unable to perform desired validation";
156 case err_status_key_expired:
157 return "can't use key any more";
163 static int policy_hash_fn(
const void *obj,
const int flags)
167 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
170 static int policy_cmp_fn(
void *obj,
void *arg,
int flags)
174 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
181 .ssrc.type = policy->ssrc.type,
182 .ssrc.value = policy->ssrc.value,
186 return ao2_t_find(srtp->policies, &tmp, flags,
"Looking for policy");
189 static struct ast_srtp *res_srtp_new(
void)
194 ast_log(LOG_ERROR,
"Unable to allocate memory for srtp\n");
199 policy_hash_fn, NULL, policy_cmp_fn,
"SRTP policy container");
200 if (!srtp->policies) {
213 static void srtp_event_cb(srtp_event_data_t *data)
215 switch (data->event) {
216 case event_ssrc_collision:
219 case event_key_soft_limit:
222 case event_key_hard_limit:
225 case event_packet_index_limit:
226 ast_debug(1,
"event_packet_index_limit\n");
232 unsigned long ssrc,
int inbound)
235 policy->sp.ssrc.type = ssrc_specific;
236 policy->sp.ssrc.value = ssrc;
238 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
242 static void policy_destructor(
void *obj)
246 if (policy->sp.key) {
247 ast_free(policy->sp.key);
248 policy->sp.key = NULL;
256 if (!(tmp = ao2_t_alloc(
sizeof(*tmp), policy_destructor,
"Allocating policy"))) {
257 ast_log(LOG_ERROR,
"Unable to allocate memory for srtp_policy\n");
265 ao2_t_ref(policy, -1,
"Destroying policy");
268 static int policy_set_suite(crypto_policy_t *p,
enum ast_srtp_suite suite)
271 case AST_AES_CM_128_HMAC_SHA1_80:
272 crypto_policy_set_aes_cm_128_hmac_sha1_80(p);
275 case AST_AES_CM_128_HMAC_SHA1_32:
276 crypto_policy_set_aes_cm_128_hmac_sha1_32(p);
279 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
280 case AST_AES_CM_192_HMAC_SHA1_80:
281 crypto_policy_set_aes_cm_192_hmac_sha1_80(p);
284 case AST_AES_CM_192_HMAC_SHA1_32:
285 crypto_policy_set_aes_cm_192_hmac_sha1_32(p);
288 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
289 case AST_AES_CM_256_HMAC_SHA1_80:
290 crypto_policy_set_aes_cm_256_hmac_sha1_80(p);
293 case AST_AES_CM_256_HMAC_SHA1_32:
294 crypto_policy_set_aes_cm_256_hmac_sha1_32(p);
297 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
298 case AST_AES_GCM_128:
299 crypto_policy_set_aes_gcm_128_16_auth(p);
302 case AST_AES_GCM_128_8:
303 crypto_policy_set_aes_gcm_128_8_auth(p);
306 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
307 case AST_AES_GCM_256:
308 crypto_policy_set_aes_gcm_256_16_auth(p);
311 case AST_AES_GCM_256_8:
312 crypto_policy_set_aes_gcm_256_8_auth(p);
317 ast_log(LOG_ERROR,
"Invalid crypto suite: %u\n", suite);
322 static int ast_srtp_policy_set_suite(
struct ast_srtp_policy *policy,
enum ast_srtp_suite suite)
324 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
327 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)
329 size_t size = key_len + salt_len;
330 unsigned char *master_key;
332 if (policy->sp.key) {
333 ast_free(policy->sp.key);
334 policy->sp.key = NULL;
341 memcpy(master_key, key, key_len);
342 memcpy(master_key + key_len, salt, salt_len);
344 policy->sp.key = master_key;
349 static int ast_srtp_get_random(
unsigned char *key,
size_t len)
352 return RAND_bytes(key, len) > 0 ? 0: -1;
354 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
358 static void ast_srtp_set_cb(
struct ast_srtp *srtp,
const struct ast_srtp_cb *cb,
void *data)
369 static int ast_srtp_unprotect(
struct ast_srtp *srtp,
void *buf,
int *len,
int flags)
373 int rtcp = (flags & 0x01) >> 0;
374 int retry = (flags & 0x02) >> 1;
379 if (!srtp->session) {
380 ast_log(LOG_ERROR,
"SRTP unprotect %s - missing session\n", rtcp ?
"rtcp" :
"rtp");
385 for (i = 0; i < 2; i++) {
386 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
387 if (res != err_status_no_ctx) {
391 if (srtp->cb && srtp->cb->no_ctx) {
395 if (srtp->cb->no_ctx(srtp->rtp, stats.
remote_ssrc, srtp->data) < 0) {
403 if (retry == 0 && res == err_status_replay_old) {
404 ast_log(AST_LOG_NOTICE,
"SRTP unprotect failed with %s, retrying\n", srtp_errstr(res));
412 ast_debug(5,
"SRTP destroy before re-create\n");
413 srtp_dealloc(srtp->session);
420 policy = ao2_iterator_next(&it);
424 int res_srtp_create = srtp_create(&srtp->session, &policy->sp);
425 if (res_srtp_create == err_status_ok) {
426 ast_debug(5,
"SRTP re-created with first policy\n");
427 ao2_t_ref(policy, -1,
"Unreffing first policy for re-creating srtp session");
430 if (policies_count > 1) {
431 ast_debug(5,
"Add all the other %d policies\n",
433 while ((policy = ao2_iterator_next(&it))) {
434 srtp_add_stream(srtp->session, &policy->sp);
435 ao2_t_ref(policy, -1,
"Unreffing n-th policy for re-creating srtp session");
443 ast_log(LOG_ERROR,
"SRTP session could not be re-created after unprotect failure: %s\n", srtp_errstr(res_srtp_create));
446 srtp->session = NULL;
448 ao2_t_ref(policy, -1,
"Unreffing first policy after srtp_create failed");
454 if (!srtp->session) {
459 if (res != err_status_ok && res != err_status_replay_fail ) {
470 ast_verb(2,
"SRTCP unprotect failed on SSRC %u because of %s\n",
473 if ((srtp->warned >= 10) && !((srtp->warned - 10) % 150)) {
474 ast_verb(2,
"SRTP unprotect failed on SSRC %u because of %s %d\n",
488 static int ast_srtp_protect(
struct ast_srtp *srtp,
void **buf,
int *len,
int rtcp)
491 unsigned char *localbuf;
493 if (!srtp->session) {
494 ast_log(LOG_ERROR,
"SRTP protect %s - missing session\n", rtcp ?
"rtcp" :
"rtp");
499 if ((*len + SRTP_MAX_TRAILER_LEN) >
sizeof(srtp->buf)) {
503 localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
505 memcpy(localbuf, *buf, *len);
507 if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
508 ast_log(LOG_WARNING,
"SRTP protect: %s\n", srtp_errstr(res));
521 if (!(temp = res_srtp_new())) {
527 status = srtp_create(&temp->session, &policy->sp);
528 if (status != err_status_ok) {
530 temp->session = NULL;
531 ast_srtp_destroy(temp);
532 ast_log(LOG_ERROR,
"Failed to create srtp session on rtp instance (%p) - %s\n",
533 rtp, srtp_errstr(status));
540 ao2_t_link((*srtp)->policies, policy,
"Created initial policy");
548 int res = ast_srtp_create(srtp, rtp, policy);
551 ast_srtp_destroy(old);
555 ast_log(LOG_ERROR,
"Failed to replace srtp (%p) on rtp instance (%p) "
556 "- keeping old\n", *srtp, rtp);
562 static void ast_srtp_destroy(
struct ast_srtp *srtp)
565 srtp_dealloc(srtp->session);
569 ao2_t_ref(srtp->policies, -1,
"Destroying container");
580 if ((match = find_policy(srtp, &policy->sp,
OBJ_POINTER))) {
581 if (policy->sp.ssrc.type != ssrc_specific) {
582 ast_log(AST_LOG_WARNING,
"Cannot replace an existing wildcard policy\n");
583 ao2_t_ref(match, -1,
"Unreffing already existing policy");
586 if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
587 ast_log(AST_LOG_WARNING,
"Failed to remove SRTP stream for SSRC %u\n", match->sp.ssrc.value);
589 ao2_t_unlink(srtp->policies, match,
"Remove existing match policy");
590 ao2_t_ref(match, -1,
"Unreffing already existing policy");
594 ast_debug(3,
"Adding new policy for %s %u\n",
595 policy->sp.ssrc.type == ssrc_specific ?
"SSRC" :
"type",
596 policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
597 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
598 ast_log(AST_LOG_WARNING,
"Failed to add SRTP stream for %s %u\n",
599 policy->sp.ssrc.type == ssrc_specific ?
"SSRC" :
"type",
600 policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
604 ao2_t_link(srtp->policies, policy,
"Added additional stream");
609 static int ast_srtp_change_source(
struct ast_srtp *srtp,
unsigned int from_ssrc,
unsigned int to_ssrc)
612 struct srtp_policy_t sp = {
613 .ssrc.type = ssrc_specific,
614 .ssrc.value = from_ssrc,
622 match->sp.ssrc.value = to_ssrc;
623 if (ast_srtp_add_stream(srtp, match)) {
624 ast_log(LOG_WARNING,
"Couldn't add stream\n");
625 }
else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
626 ast_debug(3,
"Couldn't remove stream (%u)\n", status);
628 ao2_t_ref(match, -1,
"Unreffing found policy in change_source");
636 unsigned char local_key[SRTP_MAX_KEY_LEN];
638 char local_key64[((SRTP_MAX_KEY_LEN) * 8 + 5) / 6 + 1];
639 unsigned char remote_key[SRTP_MAX_KEY_LEN];
646 ast_free(crypto->a_crypto);
647 crypto->a_crypto = NULL;
656 unsigned char remote_key[key_len];
658 if (srtp_res.get_random(p->local_key, key_len) < 0) {
662 ast_base64encode(p->local_key64, p->local_key, key_len,
sizeof(p->local_key64));
664 p->key_len =
ast_base64decode(remote_key, p->local_key64,
sizeof(remote_key));
666 if (p->key_len != key_len) {
667 ast_log(LOG_ERROR,
"base64 encode/decode bad len %d != %d\n", p->key_len, key_len);
671 if (memcmp(remote_key, p->local_key, p->key_len)) {
672 ast_log(LOG_ERROR,
"base64 encode/decode bad key\n");
676 ast_debug(1 ,
"local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
692 result = crypto_init_keys(p, key_len);
694 res_sdp_crypto_dtor(p);
702 return sdp_crypto_alloc(SRTP_MASTER_KEY_LEN);
705 static int res_sdp_crypto_build_offer(
struct ast_sdp_crypto *p,
int taglen)
710 ast_free(p->a_crypto);
713 if ((taglen & 0x007f) == 8) {
714 res =
ast_asprintf(&p->a_crypto,
"%d AEAD_AES_%d_GCM_%d inline:%s",
715 p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
716 }
else if ((taglen & 0x007f) == 16) {
717 res =
ast_asprintf(&p->a_crypto,
"%d AEAD_AES_%d_GCM inline:%s",
718 p->tag, 128 + ((taglen & 0x0300) >> 2), p->local_key64);
719 }
else if ((taglen & 0x0300) && !(taglen & 0x0080)) {
720 res =
ast_asprintf(&p->a_crypto,
"%d AES_%d_CM_HMAC_SHA1_%d inline:%s",
721 p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
723 res =
ast_asprintf(&p->a_crypto,
"%d AES_CM_%d_HMAC_SHA1_%d inline:%s",
724 p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
726 if (res == -1 || !p->a_crypto) {
727 ast_log(LOG_ERROR,
"Could not allocate memory for crypto line\n");
731 ast_debug(1,
"Crypto line: a=crypto:%s\n", p->a_crypto);
736 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)
738 if (policy_res.set_master_key(policy, master_key, key_len, NULL, 0) < 0) {
742 if (policy_res.set_suite(policy, suite_val)) {
743 ast_log(LOG_WARNING,
"Could not set remote SRTP suite\n");
747 policy_res.set_ssrc(policy, ssrc, inbound);
763 if (!(local_policy = policy_res.alloc())) {
767 if (!(remote_policy = policy_res.alloc())) {
775 if (set_crypto_policy(local_policy, suite_val, p->local_key, key_len, stats.
local_ssrc, 0) < 0) {
779 if (set_crypto_policy(remote_policy, suite_val, remote_key, key_len, 0, 1) < 0) {
785 ast_log(LOG_WARNING,
"Could not set SRTP policies\n");
789 ast_debug(1 ,
"SRTP policy activated\n");
794 policy_res.destroy(local_policy);
798 policy_res.destroy(remote_policy);
809 char *key_params = NULL;
810 char *key_param = NULL;
811 char *session_params = NULL;
812 char *key_salt = NULL;
813 char *lifetime = NULL;
816 int key_len_from_sdp;
817 int key_len_expected;
820 unsigned char remote_key[SRTP_MAX_KEY_LEN];
822 double sdes_lifetime;
828 tag = strsep(&str,
" ");
829 suite = strsep(&str,
" ");
830 key_params = strsep(&str,
" ");
831 session_params = strsep(&str,
" ");
833 if (!tag || !suite) {
834 ast_log(LOG_WARNING,
"Unrecognized crypto attribute a=%s\n", attr);
839 if (sscanf(tag,
"%30d", &tag_from_sdp) != 1 || tag_from_sdp < 0 || tag_from_sdp > 999999999) {
840 ast_log(LOG_WARNING,
"Unacceptable a=crypto tag: %s\n", tag);
844 if (!ast_strlen_zero(session_params)) {
845 ast_log(LOG_WARNING,
"Unsupported crypto parameters: %s\n", session_params);
851 for (tmp = srtp; tmp && tmp->crypto && tmp->crypto->tag != tag_from_sdp;) {
855 unsigned int flags = tmp->flags;
859 crypto = tmp->crypto;
860 tmp->crypto = srtp->crypto;
861 tmp->flags = srtp->flags;
862 srtp->crypto = crypto;
865 crypto = srtp->crypto;
866 crypto->tag = tag_from_sdp;
869 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
870 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
871 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
872 ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
873 ast_clear_flag(srtp, AST_SRTP_CRYPTO_AES_192);
874 ast_clear_flag(srtp, AST_SRTP_CRYPTO_AES_256);
875 ast_clear_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
877 if (!strcmp(suite,
"AES_CM_128_HMAC_SHA1_80")) {
878 suite_val = AST_AES_CM_128_HMAC_SHA1_80;
879 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
880 key_len_expected = 30;
881 }
else if (!strcmp(suite,
"AES_CM_128_HMAC_SHA1_32")) {
882 suite_val = AST_AES_CM_128_HMAC_SHA1_32;
883 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
884 key_len_expected = 30;
885 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
886 }
else if (!strcmp(suite,
"AES_192_CM_HMAC_SHA1_80")) {
887 suite_val = AST_AES_CM_192_HMAC_SHA1_80;
888 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
889 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
890 key_len_expected = 38;
891 }
else if (!strcmp(suite,
"AES_192_CM_HMAC_SHA1_32")) {
892 suite_val = AST_AES_CM_192_HMAC_SHA1_32;
893 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
894 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
895 key_len_expected = 38;
897 }
else if (!strcmp(suite,
"AES_CM_192_HMAC_SHA1_80")) {
898 suite_val = AST_AES_CM_192_HMAC_SHA1_80;
899 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
900 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
901 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
902 key_len_expected = 38;
903 }
else if (!strcmp(suite,
"AES_CM_192_HMAC_SHA1_32")) {
904 suite_val = AST_AES_CM_192_HMAC_SHA1_32;
905 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
906 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
907 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
908 key_len_expected = 38;
910 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
911 }
else if (!strcmp(suite,
"AES_256_CM_HMAC_SHA1_80")) {
912 suite_val = AST_AES_CM_256_HMAC_SHA1_80;
913 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
914 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
915 key_len_expected = 46;
916 }
else if (!strcmp(suite,
"AES_256_CM_HMAC_SHA1_32")) {
917 suite_val = AST_AES_CM_256_HMAC_SHA1_32;
918 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
919 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
920 key_len_expected = 46;
922 }
else if (!strcmp(suite,
"AES_CM_256_HMAC_SHA1_80")) {
923 suite_val = AST_AES_CM_256_HMAC_SHA1_80;
924 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
925 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
926 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
927 key_len_expected = 46;
928 }
else if (!strcmp(suite,
"AES_CM_256_HMAC_SHA1_32")) {
929 suite_val = AST_AES_CM_256_HMAC_SHA1_32;
930 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
931 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
932 ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
933 key_len_expected = 46;
935 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
936 }
else if (!strcmp(suite,
"AEAD_AES_128_GCM")) {
937 suite_val = AST_AES_GCM_128;
938 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
939 key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
941 }
else if (!strcmp(suite,
"AEAD_AES_128_GCM_8")) {
942 suite_val = AST_AES_GCM_128_8;
943 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
944 key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
946 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
947 }
else if (!strcmp(suite,
"AEAD_AES_256_GCM")) {
948 suite_val = AST_AES_GCM_256;
949 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
950 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
951 key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
953 }
else if (!strcmp(suite,
"AEAD_AES_256_GCM_8")) {
954 suite_val = AST_AES_GCM_256_8;
955 ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
956 ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
957 key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
960 ast_verb(1,
"Unsupported crypto suite: %s\n", suite);
964 while ((key_param = strsep(&key_params,
";"))) {
965 unsigned int n_lifetime;
969 method = strsep(&key_param,
":");
970 info = strsep(&key_param,
";");
973 if (strcmp(method,
"inline")) {
977 key_salt = strsep(&info,
"|");
980 lifetime = strsep(&info,
"|");
986 mki = strchr(lifetime,
':');
991 mki = strsep(&info,
"|");
994 if (mki && *mki !=
'1') {
995 ast_log(LOG_NOTICE,
"Crypto MKI handling is not supported: ignoring attribute %s\n", attr);
1000 if (!strncmp(lifetime,
"2^", 2)) {
1001 char *lifetime_val = lifetime + 2;
1004 if (sscanf(lifetime_val,
"%30u", &n_lifetime) != 1) {
1005 ast_log(LOG_NOTICE,
"Failed to parse lifetime value in crypto attribute: %s\n", attr);
1009 if (n_lifetime > 48) {
1011 ast_log(LOG_NOTICE,
"Crypto lifetime exponent of '%u' is a bit large; using 48\n", n_lifetime);
1014 sdes_lifetime = pow(2, n_lifetime);
1017 if (sscanf(lifetime,
"%30u", &n_lifetime) != 1) {
1018 ast_log(LOG_NOTICE,
"Failed to parse lifetime value in crypto attribute: %s\n", attr);
1021 sdes_lifetime = n_lifetime;
1025 if (sdes_lifetime < 1048576) {
1026 ast_log(LOG_NOTICE,
"Rejecting crypto attribute '%s': lifetime '%f' too short\n", attr, sdes_lifetime);
1031 ast_debug(2,
"Crypto attribute '%s' accepted with lifetime '%f', MKI '%s'\n",
1032 attr, sdes_lifetime, mki ? mki :
"-");
1039 ast_log(LOG_NOTICE,
"SRTP crypto offer not acceptable: '%s'\n", attr);
1043 key_len_from_sdp =
ast_base64decode(remote_key, key_salt,
sizeof(remote_key));
1044 if (key_len_from_sdp != key_len_expected) {
1045 ast_log(LOG_WARNING,
"SRTP descriptions key length is '%d', not '%d'\n",
1046 key_len_from_sdp, key_len_expected);
1053 if (crypto->key_len != key_len_from_sdp) {
1054 if (!crypto_init_keys(crypto, key_len_from_sdp)) {
1057 }
else if (!memcmp(crypto->remote_key, remote_key, key_len_from_sdp)) {
1058 ast_debug(1,
"SRTP remote key unchanged; maintaining current policy\n");
1062 if (key_len_from_sdp >
sizeof(crypto->remote_key)) {
1064 "SRTP key buffer is %zu although it must be at least %d bytes\n",
1065 sizeof(crypto->remote_key), key_len_from_sdp);
1068 memcpy(crypto->remote_key, remote_key, key_len_from_sdp);
1070 if (crypto_activate(crypto, suite_val, remote_key, key_len_from_sdp, rtp) < 0) {
1074 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
1076 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
1078 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
1083 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_256)) {
1085 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1088 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME)) {
1093 if (res_sdp_crypto_build_offer(crypto, taglen)) {
1097 ast_set_flag(srtp, AST_SRTP_CRYPTO_OFFER_OK);
1101 static const char *res_sdp_srtp_get_attr(
struct ast_sdp_srtp *srtp,
int dtls_enabled,
int default_taglen_32)
1110 if (!srtp->crypto) {
1112 srtp->crypto = res_sdp_crypto_alloc();
1113 ast_log(LOG_ERROR,
"SRTP SDP list was not empty\n");
1115 const int len = default_taglen_32 ? AST_SRTP_CRYPTO_TAG_32 : AST_SRTP_CRYPTO_TAG_80;
1116 const int attr[][3] = {
1144 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
1145 { AST_SRTP_CRYPTO_TAG_16, 0, AES_128_GCM_KEYSIZE_WSALT },
1147 #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
1148 { len, AST_SRTP_CRYPTO_AES_256, 46 },
1150 #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
1151 { AST_SRTP_CRYPTO_TAG_16, AST_SRTP_CRYPTO_AES_256, AES_256_GCM_KEYSIZE_WSALT },
1153 #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
1154 { len, AST_SRTP_CRYPTO_AES_192, 38 },
1160 for (i = 0; i < ARRAY_LEN(attr); i++) {
1162 ast_set_flag(tmp, attr[i][0]);
1165 ast_set_flag(tmp, attr[i][1]);
1167 tmp->crypto = sdp_crypto_alloc(attr[i][2]);
1168 tmp->crypto->tag = (i + 1);
1170 if (i < ARRAY_LEN(attr) - 1) {
1184 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_80)) {
1186 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
1188 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
1190 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
1193 taglen = default_taglen_32 ? 32 : 80;
1195 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_256)) {
1197 }
else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
1200 if (ast_test_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME)) {
1204 if (srtp->crypto && (res_sdp_crypto_build_offer(srtp->crypto, taglen) >= 0)) {
1205 return srtp->crypto->a_crypto;
1208 ast_log(LOG_WARNING,
"No SRTP key management enabled\n");
1213 .
dtor = res_sdp_crypto_dtor,
1214 .alloc = res_sdp_crypto_alloc,
1215 .build_offer = res_sdp_crypto_build_offer,
1216 .parse_offer = res_sdp_crypto_parse_offer,
1217 .get_attr = res_sdp_srtp_get_attr,
1220 static void res_srtp_shutdown(
void)
1223 ast_rtp_engine_unregister_srtp();
1224 srtp_install_event_handler(NULL);
1225 #ifdef HAVE_SRTP_SHUTDOWN
1231 static int res_srtp_init(
void)
1237 if (srtp_init() != err_status_ok) {
1238 ast_log(AST_LOG_WARNING,
"Failed to initialize libsrtp\n");
1242 srtp_install_event_handler(srtp_event_cb);
1244 if (ast_rtp_engine_register_srtp(&srtp_res, &policy_res)) {
1245 ast_log(AST_LOG_WARNING,
"Failed to register SRTP with rtp engine\n");
1246 res_srtp_shutdown();
1251 ast_log(AST_LOG_WARNING,
"Failed to register SDP SRTP crypto API\n");
1252 res_srtp_shutdown();
1256 #ifdef HAVE_SRTP_GET_VERSION
1257 ast_verb(2,
"%s initialized\n", srtp_get_version_string());
1259 ast_verb(2,
"libsrtp initialized\n");
1270 static int load_module(
void)
1272 return res_srtp_init();
1275 static int unload_module(
void)
1277 res_srtp_shutdown();
1281 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
"Secure RTP (SRTP)",
1282 .support_level = AST_MODULE_SUPPORT_CORE,
1283 .load = load_module,
1284 .unload = unload_module,
structure for secure RTP audio
Asterisk main include file. File version handling, generic pbx functions.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
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.
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
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.
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
sdp_crypto_destroy_cb dtor
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
SRTP and SDP Security descriptions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Asterisk internal frame definitions.
#define ast_debug(level,...)
Log a DEBUG message.
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
unsigned int ast_rtp_instance_get_ssrc(struct ast_rtp_instance *rtp)
Retrieve the local SSRC value that we will be using.
#define ast_module_ref(mod)
Hold a reference to the module.
#define ast_calloc(num, len)
A wrapper for calloc()
struct ast_sdp_srtp * ast_sdp_srtp_alloc(void)
allocate a ast_sdp_srtp structure
Support for logging to various files, console and syslog Configuration in file logger.conf.
#define ast_module_unref(mod)
Release a reference to the module.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
void ast_sdp_crypto_unregister(struct ast_sdp_crypto_api *api)
Unregister SDP SRTP crypto processing routines.
#define ao2_t_find(container, arg, flags, tag)
int ast_sdp_crypto_register(struct ast_sdp_crypto_api *api)
Register SDP SRTP crypto processing routines.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Pluggable RTP Architecture.
Asterisk module definitions.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
int(* create)(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)