2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Mikael Magnusson
6 * Mikael Magnusson <mikma@users.sourceforge.net>
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.
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.
18 * Builds on libSRTP http://srtp.sourceforge.net
23 * \brief Secure RTP (SRTP)
26 * Specified in RFC 3711.
28 * \author Mikael Magnusson <mikma@users.sourceforge.net>
33 <support_level>core</support_level>
36 /* See https://wiki.asterisk.org/wiki/display/AST/Secure+Calling */
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include <srtp/srtp.h>
44 #include "asterisk/lock.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/module.h"
47 #include "asterisk/options.h"
48 #include "asterisk/rtp_engine.h"
49 #include "asterisk/astobj2.h"
52 struct ast_rtp_instance *rtp;
53 struct ao2_container *policies;
55 const struct ast_srtp_cb *cb;
58 unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
61 struct ast_srtp_policy {
65 static int g_initialized = 0;
68 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
69 static void ast_srtp_destroy(struct ast_srtp *srtp);
70 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
71 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
73 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
74 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
75 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
76 static int ast_srtp_get_random(unsigned char *key, size_t len);
78 /* Policy functions */
79 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
80 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
81 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
82 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);
83 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
85 static struct ast_srtp_res srtp_res = {
86 .create = ast_srtp_create,
87 .destroy = ast_srtp_destroy,
88 .add_stream = ast_srtp_add_stream,
89 .change_source = ast_srtp_change_source,
90 .set_cb = ast_srtp_set_cb,
91 .unprotect = ast_srtp_unprotect,
92 .protect = ast_srtp_protect,
93 .get_random = ast_srtp_get_random
96 static struct ast_srtp_policy_res policy_res = {
97 .alloc = ast_srtp_policy_alloc,
98 .destroy = ast_srtp_policy_destroy,
99 .set_suite = ast_srtp_policy_set_suite,
100 .set_master_key = ast_srtp_policy_set_master_key,
101 .set_ssrc = ast_srtp_policy_set_ssrc
104 static const char *srtp_errstr(int err)
108 return "nothing to report";
109 case err_status_fail:
110 return "unspecified failure";
111 case err_status_bad_param:
112 return "unsupported parameter";
113 case err_status_alloc_fail:
114 return "couldn't allocate memory";
115 case err_status_dealloc_fail:
116 return "couldn't deallocate properly";
117 case err_status_init_fail:
118 return "couldn't initialize";
119 case err_status_terminus:
120 return "can't process as much data as requested";
121 case err_status_auth_fail:
122 return "authentication failure";
123 case err_status_cipher_fail:
124 return "cipher failure";
125 case err_status_replay_fail:
126 return "replay check failed (bad index)";
127 case err_status_replay_old:
128 return "replay check failed (index too old)";
129 case err_status_algo_fail:
130 return "algorithm failed test routine";
131 case err_status_no_such_op:
132 return "unsupported operation";
133 case err_status_no_ctx:
134 return "no appropriate context found";
135 case err_status_cant_check:
136 return "unable to perform desired validation";
137 case err_status_key_expired:
138 return "can't use key any more";
144 static int policy_hash_fn(const void *obj, const int flags)
146 const struct ast_srtp_policy *policy = obj;
148 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
151 static int policy_cmp_fn(void *obj, void *arg, int flags)
153 const struct ast_srtp_policy *one = obj, *two = arg;
155 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
158 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
160 struct ast_srtp_policy tmp = {
162 .ssrc.type = policy->ssrc.type,
163 .ssrc.value = policy->ssrc.value,
167 return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
170 static struct ast_srtp *res_srtp_new(void)
172 struct ast_srtp *srtp;
174 if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
175 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
179 if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
190 struct ast_srtp_policy
192 static void srtp_event_cb(srtp_event_data_t *data)
194 switch (data->event) {
195 case event_ssrc_collision:
196 ast_debug(1, "SSRC collision\n");
198 case event_key_soft_limit:
199 ast_debug(1, "event_key_soft_limit\n");
201 case event_key_hard_limit:
202 ast_debug(1, "event_key_hard_limit\n");
204 case event_packet_index_limit:
205 ast_debug(1, "event_packet_index_limit\n");
210 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
211 unsigned long ssrc, int inbound)
214 policy->sp.ssrc.type = ssrc_specific;
215 policy->sp.ssrc.value = ssrc;
217 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
221 static void policy_destructor(void *obj)
223 struct ast_srtp_policy *policy = obj;
225 if (policy->sp.key) {
226 ast_free(policy->sp.key);
227 policy->sp.key = NULL;
231 static struct ast_srtp_policy *ast_srtp_policy_alloc()
233 struct ast_srtp_policy *tmp;
235 if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
236 ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
242 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
244 ao2_t_ref(policy, -1, "Destroying policy");
247 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
250 case AST_AES_CM_128_HMAC_SHA1_80:
251 p->cipher_type = AES_128_ICM;
252 p->cipher_key_len = 30;
253 p->auth_type = HMAC_SHA1;
254 p->auth_key_len = 20;
255 p->auth_tag_len = 10;
256 p->sec_serv = sec_serv_conf_and_auth;
259 case AST_AES_CM_128_HMAC_SHA1_32:
260 p->cipher_type = AES_128_ICM;
261 p->cipher_key_len = 30;
262 p->auth_type = HMAC_SHA1;
263 p->auth_key_len = 20;
265 p->sec_serv = sec_serv_conf_and_auth;
269 ast_log(LOG_ERROR, "Invalid crypto suite: %d\n", suite);
274 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
276 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
279 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)
281 size_t size = key_len + salt_len;
282 unsigned char *master_key;
284 if (policy->sp.key) {
285 ast_free(policy->sp.key);
286 policy->sp.key = NULL;
289 if (!(master_key = ast_calloc(1, size))) {
293 memcpy(master_key, key, key_len);
294 memcpy(master_key + key_len, salt, salt_len);
296 policy->sp.key = master_key;
301 static int ast_srtp_get_random(unsigned char *key, size_t len)
303 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
306 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
316 /* Vtable functions */
317 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
322 struct ast_rtp_instance_stats stats = {0,};
326 for (i = 0; i < 2; i++) {
327 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
328 if (res != err_status_no_ctx) {
332 if (srtp->cb && srtp->cb->no_ctx) {
333 if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
336 if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
344 if (retry == 0 && res == err_status_replay_old) {
345 ast_log(LOG_WARNING, "SRTP unprotect: %s\n", srtp_errstr(res));
348 struct ast_srtp_policy *policy;
349 struct ao2_iterator it;
350 int policies_count = 0;
353 ast_log(LOG_WARNING, "SRTP destroy before re-create\n");
354 srtp_dealloc(srtp->session);
357 policies_count = ao2_container_count(srtp->policies);
359 // get the first to build up
360 it = ao2_iterator_init(srtp->policies, 0);
361 policy = ao2_iterator_next(&it);
363 ast_log(LOG_WARNING, "SRTP try to re-create\n");
364 if (srtp_create(&srtp->session, &policy->sp) == err_status_ok) {
365 ast_log(LOG_WARNING, "SRTP re-created with first policy\n");
367 // unref first element
368 ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
370 // if we have more than one policy, add them afterwards
371 if (policies_count > 1) {
372 ast_log(LOG_WARNING, "Add all the other %d policies\n", policies_count-1);
373 while ((policy = ao2_iterator_next(&it))) {
374 srtp_add_stream(srtp->session, &policy->sp);
375 ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
380 ao2_iterator_destroy(&it);
383 ao2_iterator_destroy(&it);
387 if (res != err_status_ok && res != err_status_replay_fail ) {
388 if ((srtp->warned >= 10) && !((srtp->warned - 10) % 100)) {
389 ast_log(LOG_WARNING, "SRTP unprotect: %s %d\n", srtp_errstr(res), srtp->warned);
401 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
405 if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
409 memcpy(srtp->buf, *buf, *len);
411 if ((res = rtcp ? srtp_protect_rtcp(srtp->session, srtp->buf, len) : srtp_protect(srtp->session, srtp->buf, len)) != err_status_ok && res != err_status_replay_fail) {
412 ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
420 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
422 struct ast_srtp *temp;
424 if (!(temp = res_srtp_new())) {
428 if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
432 ast_module_ref(ast_module_info->self);
436 ao2_t_link((*srtp)->policies, policy, "Created initial policy");
441 static void ast_srtp_destroy(struct ast_srtp *srtp)
444 srtp_dealloc(srtp->session);
447 ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
448 ao2_t_ref(srtp->policies, -1, "Destroying container");
451 ast_module_unref(ast_module_info->self);
454 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
456 struct ast_srtp_policy *match;
458 if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
459 ast_debug(3, "Policy already exists, not re-adding\n");
460 ao2_t_ref(match, -1, "Unreffing already existing policy");
464 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
468 ao2_t_link(srtp->policies, policy, "Added additional stream");
473 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
475 struct ast_srtp_policy *match;
476 struct srtp_policy_t sp = {
477 .ssrc.type = ssrc_specific,
478 .ssrc.value = from_ssrc,
482 /* If we find a mach, return and unlink it from the container so we
483 * can change the SSRC (which is part of the hash) and then have
484 * ast_srtp_add_stream link it back in if all is well */
485 if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
486 match->sp.ssrc.value = to_ssrc;
487 if (ast_srtp_add_stream(srtp, match)) {
488 ast_log(LOG_WARNING, "Couldn't add stream\n");
489 } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
490 ast_debug(3, "Couldn't remove stream (%d)\n", status);
492 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
498 static int res_srtp_init(void)
504 if (srtp_init() != err_status_ok) {
508 srtp_install_event_handler(srtp_event_cb);
510 return ast_rtp_engine_register_srtp(&srtp_res, &policy_res);
517 static int load_module(void)
519 return res_srtp_init();
522 static int unload_module(void)
524 ast_rtp_engine_unregister_srtp();
528 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
530 .unload = unload_module,
531 .load_pri = AST_MODPRI_CHANNEL_DEPEND,