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;
57 unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
60 struct ast_srtp_policy {
64 static int g_initialized = 0;
67 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
68 static void ast_srtp_destroy(struct ast_srtp *srtp);
69 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
70 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
72 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
73 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
74 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
75 static int ast_srtp_get_random(unsigned char *key, size_t len);
77 /* Policy functions */
78 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
79 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
80 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
81 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);
82 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
84 static struct ast_srtp_res srtp_res = {
85 .create = ast_srtp_create,
86 .destroy = ast_srtp_destroy,
87 .add_stream = ast_srtp_add_stream,
88 .change_source = ast_srtp_change_source,
89 .set_cb = ast_srtp_set_cb,
90 .unprotect = ast_srtp_unprotect,
91 .protect = ast_srtp_protect,
92 .get_random = ast_srtp_get_random
95 static struct ast_srtp_policy_res policy_res = {
96 .alloc = ast_srtp_policy_alloc,
97 .destroy = ast_srtp_policy_destroy,
98 .set_suite = ast_srtp_policy_set_suite,
99 .set_master_key = ast_srtp_policy_set_master_key,
100 .set_ssrc = ast_srtp_policy_set_ssrc
103 static const char *srtp_errstr(int err)
107 return "nothing to report";
108 case err_status_fail:
109 return "unspecified failure";
110 case err_status_bad_param:
111 return "unsupported parameter";
112 case err_status_alloc_fail:
113 return "couldn't allocate memory";
114 case err_status_dealloc_fail:
115 return "couldn't deallocate properly";
116 case err_status_init_fail:
117 return "couldn't initialize";
118 case err_status_terminus:
119 return "can't process as much data as requested";
120 case err_status_auth_fail:
121 return "authentication failure";
122 case err_status_cipher_fail:
123 return "cipher failure";
124 case err_status_replay_fail:
125 return "replay check failed (bad index)";
126 case err_status_replay_old:
127 return "replay check failed (index too old)";
128 case err_status_algo_fail:
129 return "algorithm failed test routine";
130 case err_status_no_such_op:
131 return "unsupported operation";
132 case err_status_no_ctx:
133 return "no appropriate context found";
134 case err_status_cant_check:
135 return "unable to perform desired validation";
136 case err_status_key_expired:
137 return "can't use key any more";
143 static int policy_hash_fn(const void *obj, const int flags)
145 const struct ast_srtp_policy *policy = obj;
147 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
150 static int policy_cmp_fn(void *obj, void *arg, int flags)
152 const struct ast_srtp_policy *one = obj, *two = arg;
154 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
157 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
159 struct ast_srtp_policy tmp = {
161 .ssrc.type = policy->ssrc.type,
162 .ssrc.value = policy->ssrc.value,
166 return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
169 static struct ast_srtp *res_srtp_new(void)
171 struct ast_srtp *srtp;
173 if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
174 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
178 if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
187 struct ast_srtp_policy
189 static void srtp_event_cb(srtp_event_data_t *data)
191 switch (data->event) {
192 case event_ssrc_collision:
193 ast_debug(1, "SSRC collision\n");
195 case event_key_soft_limit:
196 ast_debug(1, "event_key_soft_limit\n");
198 case event_key_hard_limit:
199 ast_debug(1, "event_key_hard_limit\n");
201 case event_packet_index_limit:
202 ast_debug(1, "event_packet_index_limit\n");
207 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
208 unsigned long ssrc, int inbound)
211 policy->sp.ssrc.type = ssrc_specific;
212 policy->sp.ssrc.value = ssrc;
214 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
218 static void policy_destructor(void *obj)
220 struct ast_srtp_policy *policy = obj;
222 if (policy->sp.key) {
223 ast_free(policy->sp.key);
224 policy->sp.key = NULL;
228 static struct ast_srtp_policy *ast_srtp_policy_alloc()
230 struct ast_srtp_policy *tmp;
232 if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
233 ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
239 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
241 ao2_t_ref(policy, -1, "Destroying policy");
244 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
247 case AST_AES_CM_128_HMAC_SHA1_80:
248 p->cipher_type = AES_128_ICM;
249 p->cipher_key_len = 30;
250 p->auth_type = HMAC_SHA1;
251 p->auth_key_len = 20;
252 p->auth_tag_len = 10;
253 p->sec_serv = sec_serv_conf_and_auth;
256 case AST_AES_CM_128_HMAC_SHA1_32:
257 p->cipher_type = AES_128_ICM;
258 p->cipher_key_len = 30;
259 p->auth_type = HMAC_SHA1;
260 p->auth_key_len = 20;
262 p->sec_serv = sec_serv_conf_and_auth;
266 ast_log(LOG_ERROR, "Invalid crypto suite: %d\n", suite);
271 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
273 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
276 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)
278 size_t size = key_len + salt_len;
279 unsigned char *master_key;
281 if (policy->sp.key) {
282 ast_free(policy->sp.key);
283 policy->sp.key = NULL;
286 if (!(master_key = ast_calloc(1, size))) {
290 memcpy(master_key, key, key_len);
291 memcpy(master_key + key_len, salt, salt_len);
293 policy->sp.key = master_key;
298 static int ast_srtp_get_random(unsigned char *key, size_t len)
300 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
303 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
313 /* Vtable functions */
314 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
319 struct ast_rtp_instance_stats stats = {0,};
323 for (i = 0; i < 2; i++) {
324 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
325 if (res != err_status_no_ctx) {
329 if (srtp->cb && srtp->cb->no_ctx) {
330 if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
333 if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
341 if (retry == 0 && res == err_status_replay_old) {
342 ast_log(LOG_WARNING, "SRTP unprotect: %s\n", srtp_errstr(res));
345 struct ast_srtp_policy *policy;
346 struct ao2_iterator it;
347 int policies_count = 0;
350 ast_log(LOG_WARNING, "SRTP destroy before re-create\n");
351 srtp_dealloc(srtp->session);
354 policies_count = ao2_container_count(srtp->policies);
356 // get the first to build up
357 it = ao2_iterator_init(srtp->policies, 0);
358 policy = ao2_iterator_next(&it);
360 ast_log(LOG_WARNING, "SRTP try to re-create\n");
361 if (srtp_create(&srtp->session, &policy->sp) == err_status_ok) {
362 ast_log(LOG_WARNING, "SRTP re-created with first policy\n");
364 // unref first element
365 ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
367 // if we have more than one policy, add them afterwards
368 if (policies_count > 1) {
369 ast_log(LOG_WARNING, "Add all the other %d policies\n", policies_count-1);
370 while ((policy = ao2_iterator_next(&it))) {
371 srtp_add_stream(srtp->session, &policy->sp);
372 ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
377 ao2_iterator_destroy(&it);
380 ao2_iterator_destroy(&it);
384 if (res != err_status_ok && res != err_status_replay_fail ) {
385 ast_log(LOG_WARNING, "SRTP unprotect: %s\n", srtp_errstr(res));
393 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
397 if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
401 memcpy(srtp->buf, *buf, *len);
403 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) {
404 ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
412 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
414 struct ast_srtp *temp;
416 if (!(temp = res_srtp_new())) {
420 if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
424 ast_module_ref(ast_module_info->self);
428 ao2_t_link((*srtp)->policies, policy, "Created initial policy");
433 static void ast_srtp_destroy(struct ast_srtp *srtp)
436 srtp_dealloc(srtp->session);
439 ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
440 ao2_t_ref(srtp->policies, -1, "Destroying container");
443 ast_module_unref(ast_module_info->self);
446 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
448 struct ast_srtp_policy *match;
450 if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
451 ast_debug(3, "Policy already exists, not re-adding\n");
452 ao2_t_ref(match, -1, "Unreffing already existing policy");
456 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
460 ao2_t_link(srtp->policies, policy, "Added additional stream");
465 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
467 struct ast_srtp_policy *match;
468 struct srtp_policy_t sp = {
469 .ssrc.type = ssrc_specific,
470 .ssrc.value = from_ssrc,
474 /* If we find a mach, return and unlink it from the container so we
475 * can change the SSRC (which is part of the hash) and then have
476 * ast_srtp_add_stream link it back in if all is well */
477 if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
478 match->sp.ssrc.value = to_ssrc;
479 if (ast_srtp_add_stream(srtp, match)) {
480 ast_log(LOG_WARNING, "Couldn't add stream\n");
481 } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
482 ast_debug(3, "Couldn't remove stream (%d)\n", status);
484 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
490 static int res_srtp_init(void)
496 if (srtp_init() != err_status_ok) {
500 srtp_install_event_handler(srtp_event_cb);
502 return ast_rtp_engine_register_srtp(&srtp_res, &policy_res);
509 static int load_module(void)
511 return res_srtp_init();
514 static int unload_module(void)
516 ast_rtp_engine_unregister_srtp();
520 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
522 .unload = unload_module,
523 .load_pri = AST_MODPRI_CHANNEL_DEPEND,