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 /* The SIP channel will automatically use sdescriptions if received in a SDP offer,
36 and res_srtp is loaded. SRTP with sdescriptions key exchange can be activated
37 in outgoing offers by setting _SIPSRTP_CRYPTO=enable in extension.conf before executing Dial
39 The dial fails if the callee doesn't support SRTP and sdescriptions.
41 exten => 2345,1,Set(_SIPSRTP_CRYPTO=enable)
42 exten => 2345,2,Dial(SIP/1001)
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include <srtp/srtp.h>
51 #include "asterisk/lock.h"
52 #include "asterisk/sched.h"
53 #include "asterisk/module.h"
54 #include "asterisk/options.h"
55 #include "asterisk/rtp_engine.h"
56 #include "asterisk/astobj2.h"
59 struct ast_rtp_instance *rtp;
60 struct ao2_container *policies;
62 const struct ast_srtp_cb *cb;
64 unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
67 struct ast_srtp_policy {
71 static int g_initialized = 0;
74 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
75 static void ast_srtp_destroy(struct ast_srtp *srtp);
76 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
77 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
79 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
80 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
81 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
82 static int ast_srtp_get_random(unsigned char *key, size_t len);
84 /* Policy functions */
85 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
86 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
87 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
88 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);
89 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
91 static struct ast_srtp_res srtp_res = {
92 .create = ast_srtp_create,
93 .destroy = ast_srtp_destroy,
94 .add_stream = ast_srtp_add_stream,
95 .change_source = ast_srtp_change_source,
96 .set_cb = ast_srtp_set_cb,
97 .unprotect = ast_srtp_unprotect,
98 .protect = ast_srtp_protect,
99 .get_random = ast_srtp_get_random
102 static struct ast_srtp_policy_res policy_res = {
103 .alloc = ast_srtp_policy_alloc,
104 .destroy = ast_srtp_policy_destroy,
105 .set_suite = ast_srtp_policy_set_suite,
106 .set_master_key = ast_srtp_policy_set_master_key,
107 .set_ssrc = ast_srtp_policy_set_ssrc
110 static const char *srtp_errstr(int err)
114 return "nothing to report";
115 case err_status_fail:
116 return "unspecified failure";
117 case err_status_bad_param:
118 return "unsupported parameter";
119 case err_status_alloc_fail:
120 return "couldn't allocate memory";
121 case err_status_dealloc_fail:
122 return "couldn't deallocate properly";
123 case err_status_init_fail:
124 return "couldn't initialize";
125 case err_status_terminus:
126 return "can't process as much data as requested";
127 case err_status_auth_fail:
128 return "authentication failure";
129 case err_status_cipher_fail:
130 return "cipher failure";
131 case err_status_replay_fail:
132 return "replay check failed (bad index)";
133 case err_status_replay_old:
134 return "replay check failed (index too old)";
135 case err_status_algo_fail:
136 return "algorithm failed test routine";
137 case err_status_no_such_op:
138 return "unsupported operation";
139 case err_status_no_ctx:
140 return "no appropriate context found";
141 case err_status_cant_check:
142 return "unable to perform desired validation";
143 case err_status_key_expired:
144 return "can't use key any more";
150 static int policy_hash_fn(const void *obj, const int flags)
152 const struct ast_srtp_policy *policy = obj;
154 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
157 static int policy_cmp_fn(void *obj, void *arg, int flags)
159 const struct ast_srtp_policy *one = obj, *two = arg;
161 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
164 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
166 struct ast_srtp_policy tmp = {
168 .ssrc.type = policy->ssrc.type,
169 .ssrc.value = policy->ssrc.value,
173 return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
176 static struct ast_srtp *res_srtp_new(void)
178 struct ast_srtp *srtp;
180 if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
181 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
185 if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
194 struct ast_srtp_policy
196 static void srtp_event_cb(srtp_event_data_t *data)
198 switch (data->event) {
199 case event_ssrc_collision:
200 ast_debug(1, "SSRC collision\n");
202 case event_key_soft_limit:
203 ast_debug(1, "event_key_soft_limit\n");
205 case event_key_hard_limit:
206 ast_debug(1, "event_key_hard_limit\n");
208 case event_packet_index_limit:
209 ast_debug(1, "event_packet_index_limit\n");
214 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
215 unsigned long ssrc, int inbound)
218 policy->sp.ssrc.type = ssrc_specific;
219 policy->sp.ssrc.value = ssrc;
221 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
225 static void policy_destructor(void *obj)
227 struct ast_srtp_policy *policy = obj;
229 if (policy->sp.key) {
230 ast_free(policy->sp.key);
231 policy->sp.key = NULL;
235 static struct ast_srtp_policy *ast_srtp_policy_alloc()
237 struct ast_srtp_policy *tmp;
239 if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
240 ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
246 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
248 ao2_t_ref(policy, -1, "Destroying policy");
251 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
254 case AST_AES_CM_128_HMAC_SHA1_80:
255 p->cipher_type = AES_128_ICM;
256 p->cipher_key_len = 30;
257 p->auth_type = HMAC_SHA1;
258 p->auth_key_len = 20;
259 p->auth_tag_len = 10;
260 p->sec_serv = sec_serv_conf_and_auth;
263 case AST_AES_CM_128_HMAC_SHA1_32:
264 p->cipher_type = AES_128_ICM;
265 p->cipher_key_len = 30;
266 p->auth_type = HMAC_SHA1;
267 p->auth_key_len = 20;
269 p->sec_serv = sec_serv_conf_and_auth;
273 ast_log(LOG_ERROR, "Invalid crypto suite: %d\n", suite);
278 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
280 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
283 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)
285 size_t size = key_len + salt_len;
286 unsigned char *master_key;
288 if (policy->sp.key) {
289 ast_free(policy->sp.key);
290 policy->sp.key = NULL;
293 if (!(master_key = ast_calloc(1, size))) {
297 memcpy(master_key, key, key_len);
298 memcpy(master_key + key_len, salt, salt_len);
300 policy->sp.key = master_key;
305 static int ast_srtp_get_random(unsigned char *key, size_t len)
307 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
310 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
320 /* Vtable functions */
321 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
325 struct ast_rtp_instance_stats stats = {0,};
327 for (i = 0; i < 2; i++) {
328 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
329 if (res != err_status_no_ctx) {
333 if (srtp->cb && srtp->cb->no_ctx) {
334 if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
337 if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
345 if (res != err_status_ok && res != err_status_replay_fail ) {
346 ast_debug(1, "SRTP unprotect: %s\n", srtp_errstr(res));
353 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
357 if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
361 memcpy(srtp->buf, *buf, *len);
363 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) {
364 ast_debug(1, "SRTP protect: %s\n", srtp_errstr(res));
372 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
374 struct ast_srtp *temp;
376 if (!(temp = res_srtp_new())) {
380 if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
387 ao2_t_link((*srtp)->policies, policy, "Created initial policy");
392 static void ast_srtp_destroy(struct ast_srtp *srtp)
395 srtp_dealloc(srtp->session);
398 ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
399 ao2_t_ref(srtp->policies, -1, "Destroying container");
404 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
406 struct ast_srtp_policy *match;
408 if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
409 ast_debug(3, "Policy already exists, not re-adding\n");
410 ao2_t_ref(match, -1, "Unreffing already existing policy");
414 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
418 ao2_t_link(srtp->policies, policy, "Added additional stream");
423 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
425 struct ast_srtp_policy *match;
426 struct srtp_policy_t sp = {
427 .ssrc.type = ssrc_specific,
428 .ssrc.value = from_ssrc,
432 /* If we find a mach, return and unlink it from the container so we
433 * can change the SSRC (which is part of the hash) and then have
434 * ast_srtp_add_stream link it back in if all is well */
435 if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
436 match->sp.ssrc.value = to_ssrc;
437 if (ast_srtp_add_stream(srtp, match)) {
438 ast_log(LOG_WARNING, "Couldn't add stream\n");
439 } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
440 ast_debug(3, "Couldn't remove stream (%d)\n", status);
442 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
448 static int res_srtp_init(void)
454 if (srtp_init() != err_status_ok) {
458 srtp_install_event_handler(srtp_event_cb);
460 return ast_rtp_engine_register_srtp(&srtp_res, &policy_res);
467 static int load_module(void)
469 return res_srtp_init();
472 static int unload_module(void)
474 ast_rtp_engine_unregister_srtp();
478 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
480 .unload = unload_module,
481 .load_pri = AST_MODPRI_CHANNEL_DEPEND,