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>
35 /* See doc/tex/secure-calls.tex for SRTP usage information */
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include <srtp/srtp.h>
43 #include "asterisk/lock.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/options.h"
47 #include "asterisk/rtp_engine.h"
48 #include "asterisk/astobj2.h"
51 struct ast_rtp_instance *rtp;
52 struct ao2_container *policies;
54 const struct ast_srtp_cb *cb;
56 unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
59 struct ast_srtp_policy {
63 static int g_initialized = 0;
66 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
67 static void ast_srtp_destroy(struct ast_srtp *srtp);
68 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
69 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
71 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
72 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
73 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
74 static int ast_srtp_get_random(unsigned char *key, size_t len);
76 /* Policy functions */
77 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
78 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
79 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
80 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);
81 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
83 static struct ast_srtp_res srtp_res = {
84 .create = ast_srtp_create,
85 .destroy = ast_srtp_destroy,
86 .add_stream = ast_srtp_add_stream,
87 .change_source = ast_srtp_change_source,
88 .set_cb = ast_srtp_set_cb,
89 .unprotect = ast_srtp_unprotect,
90 .protect = ast_srtp_protect,
91 .get_random = ast_srtp_get_random
94 static struct ast_srtp_policy_res policy_res = {
95 .alloc = ast_srtp_policy_alloc,
96 .destroy = ast_srtp_policy_destroy,
97 .set_suite = ast_srtp_policy_set_suite,
98 .set_master_key = ast_srtp_policy_set_master_key,
99 .set_ssrc = ast_srtp_policy_set_ssrc
102 static const char *srtp_errstr(int err)
106 return "nothing to report";
107 case err_status_fail:
108 return "unspecified failure";
109 case err_status_bad_param:
110 return "unsupported parameter";
111 case err_status_alloc_fail:
112 return "couldn't allocate memory";
113 case err_status_dealloc_fail:
114 return "couldn't deallocate properly";
115 case err_status_init_fail:
116 return "couldn't initialize";
117 case err_status_terminus:
118 return "can't process as much data as requested";
119 case err_status_auth_fail:
120 return "authentication failure";
121 case err_status_cipher_fail:
122 return "cipher failure";
123 case err_status_replay_fail:
124 return "replay check failed (bad index)";
125 case err_status_replay_old:
126 return "replay check failed (index too old)";
127 case err_status_algo_fail:
128 return "algorithm failed test routine";
129 case err_status_no_such_op:
130 return "unsupported operation";
131 case err_status_no_ctx:
132 return "no appropriate context found";
133 case err_status_cant_check:
134 return "unable to perform desired validation";
135 case err_status_key_expired:
136 return "can't use key any more";
142 static int policy_hash_fn(const void *obj, const int flags)
144 const struct ast_srtp_policy *policy = obj;
146 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
149 static int policy_cmp_fn(void *obj, void *arg, int flags)
151 const struct ast_srtp_policy *one = obj, *two = arg;
153 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
156 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
158 struct ast_srtp_policy tmp = {
160 .ssrc.type = policy->ssrc.type,
161 .ssrc.value = policy->ssrc.value,
165 return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
168 static struct ast_srtp *res_srtp_new(void)
170 struct ast_srtp *srtp;
172 if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
173 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
177 if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
186 struct ast_srtp_policy
188 static void srtp_event_cb(srtp_event_data_t *data)
190 switch (data->event) {
191 case event_ssrc_collision:
192 ast_debug(1, "SSRC collision\n");
194 case event_key_soft_limit:
195 ast_debug(1, "event_key_soft_limit\n");
197 case event_key_hard_limit:
198 ast_debug(1, "event_key_hard_limit\n");
200 case event_packet_index_limit:
201 ast_debug(1, "event_packet_index_limit\n");
206 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
207 unsigned long ssrc, int inbound)
210 policy->sp.ssrc.type = ssrc_specific;
211 policy->sp.ssrc.value = ssrc;
213 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
217 static void policy_destructor(void *obj)
219 struct ast_srtp_policy *policy = obj;
221 if (policy->sp.key) {
222 ast_free(policy->sp.key);
223 policy->sp.key = NULL;
227 static struct ast_srtp_policy *ast_srtp_policy_alloc()
229 struct ast_srtp_policy *tmp;
231 if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
232 ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
238 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
240 ao2_t_ref(policy, -1, "Destroying policy");
243 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
246 case AST_AES_CM_128_HMAC_SHA1_80:
247 p->cipher_type = AES_128_ICM;
248 p->cipher_key_len = 30;
249 p->auth_type = HMAC_SHA1;
250 p->auth_key_len = 20;
251 p->auth_tag_len = 10;
252 p->sec_serv = sec_serv_conf_and_auth;
255 case AST_AES_CM_128_HMAC_SHA1_32:
256 p->cipher_type = AES_128_ICM;
257 p->cipher_key_len = 30;
258 p->auth_type = HMAC_SHA1;
259 p->auth_key_len = 20;
261 p->sec_serv = sec_serv_conf_and_auth;
265 ast_log(LOG_ERROR, "Invalid crypto suite: %d\n", suite);
270 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
272 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
275 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)
277 size_t size = key_len + salt_len;
278 unsigned char *master_key;
280 if (policy->sp.key) {
281 ast_free(policy->sp.key);
282 policy->sp.key = NULL;
285 if (!(master_key = ast_calloc(1, size))) {
289 memcpy(master_key, key, key_len);
290 memcpy(master_key + key_len, salt, salt_len);
292 policy->sp.key = master_key;
297 static int ast_srtp_get_random(unsigned char *key, size_t len)
299 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
302 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
312 /* Vtable functions */
313 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
317 struct ast_rtp_instance_stats stats = {0,};
319 for (i = 0; i < 2; i++) {
320 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
321 if (res != err_status_no_ctx) {
325 if (srtp->cb && srtp->cb->no_ctx) {
326 if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
329 if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
337 if (res != err_status_ok && res != err_status_replay_fail ) {
338 ast_log(LOG_WARNING, "SRTP unprotect: %s\n", srtp_errstr(res));
346 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
350 if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
354 memcpy(srtp->buf, *buf, *len);
356 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) {
357 ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
365 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
367 struct ast_srtp *temp;
369 if (!(temp = res_srtp_new())) {
373 if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
377 ast_module_ref(ast_module_info->self);
381 ao2_t_link((*srtp)->policies, policy, "Created initial policy");
386 static void ast_srtp_destroy(struct ast_srtp *srtp)
389 srtp_dealloc(srtp->session);
392 ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
393 ao2_t_ref(srtp->policies, -1, "Destroying container");
396 ast_module_unref(ast_module_info->self);
399 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
401 struct ast_srtp_policy *match;
403 if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
404 ast_debug(3, "Policy already exists, not re-adding\n");
405 ao2_t_ref(match, -1, "Unreffing already existing policy");
409 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
413 ao2_t_link(srtp->policies, policy, "Added additional stream");
418 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
420 struct ast_srtp_policy *match;
421 struct srtp_policy_t sp = {
422 .ssrc.type = ssrc_specific,
423 .ssrc.value = from_ssrc,
427 /* If we find a mach, return and unlink it from the container so we
428 * can change the SSRC (which is part of the hash) and then have
429 * ast_srtp_add_stream link it back in if all is well */
430 if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
431 match->sp.ssrc.value = to_ssrc;
432 if (ast_srtp_add_stream(srtp, match)) {
433 ast_log(LOG_WARNING, "Couldn't add stream\n");
434 } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
435 ast_debug(3, "Couldn't remove stream (%d)\n", status);
437 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
443 static int res_srtp_init(void)
449 if (srtp_init() != err_status_ok) {
453 srtp_install_event_handler(srtp_event_cb);
455 return ast_rtp_engine_register_srtp(&srtp_res, &policy_res);
462 static int load_module(void)
464 return res_srtp_init();
467 static int unload_module(void)
469 ast_rtp_engine_unregister_srtp();
473 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
475 .unload = unload_module,
476 .load_pri = AST_MODPRI_CHANNEL_DEPEND,