Merged revisions 318917 via svnmerge from
[asterisk/asterisk.git] / res / res_srtp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Mikael Magnusson
5  *
6  * Mikael Magnusson <mikma@users.sourceforge.net>
7  *
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.
13  *
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.
17  *
18  * Builds on libSRTP http://srtp.sourceforge.net
19  */
20
21 /*! \file res_srtp.c
22  *
23  * \brief Secure RTP (SRTP)
24  *
25  * Secure RTP (SRTP)
26  * Specified in RFC 3711.
27  *
28  * \author Mikael Magnusson <mikma@users.sourceforge.net>
29  */
30
31 /*** MODULEINFO
32          <depend>srtp</depend>
33 ***/
34
35 /* See https://wiki.asterisk.org/wiki/display/AST/Secure+Calling */
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include <srtp/srtp.h>
42
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"
49
50 struct ast_srtp {
51         struct ast_rtp_instance *rtp;
52         struct ao2_container *policies;
53         srtp_t session;
54         const struct ast_srtp_cb *cb;
55         void *data;
56         unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
57 };
58
59 struct ast_srtp_policy {
60         srtp_policy_t sp;
61 };
62
63 static int g_initialized = 0;
64
65 /* SRTP functions */
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);
70
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);
75
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);
82
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
92 };
93
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
100 };
101
102 static const char *srtp_errstr(int err)
103 {
104         switch(err) {
105         case err_status_ok:
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";
137         default:
138                 return "unknown";
139         }
140 }
141
142 static int policy_hash_fn(const void *obj, const int flags)
143 {
144         const struct ast_srtp_policy *policy = obj;
145
146         return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
147 }
148
149 static int policy_cmp_fn(void *obj, void *arg, int flags)
150 {
151         const struct ast_srtp_policy *one = obj, *two = arg;
152
153         return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
154 }
155
156 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
157 {
158         struct ast_srtp_policy tmp = {
159                 .sp = {
160                         .ssrc.type = policy->ssrc.type,
161                         .ssrc.value = policy->ssrc.value,
162                 },
163         };
164
165         return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
166 }
167
168 static struct ast_srtp *res_srtp_new(void)
169 {
170         struct ast_srtp *srtp;
171
172         if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
173                 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
174                 return NULL;
175         }
176
177         if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
178                 ast_free(srtp);
179                 return NULL;
180         }
181
182         return srtp;
183 }
184
185 /*
186   struct ast_srtp_policy
187 */
188 static void srtp_event_cb(srtp_event_data_t *data)
189 {
190         switch (data->event) {
191         case event_ssrc_collision:
192                 ast_debug(1, "SSRC collision\n");
193                 break;
194         case event_key_soft_limit:
195                 ast_debug(1, "event_key_soft_limit\n");
196                 break;
197         case event_key_hard_limit:
198                 ast_debug(1, "event_key_hard_limit\n");
199                 break;
200         case event_packet_index_limit:
201                 ast_debug(1, "event_packet_index_limit\n");
202                 break;
203         }
204 }
205
206 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
207                 unsigned long ssrc, int inbound)
208 {
209         if (ssrc) {
210                 policy->sp.ssrc.type = ssrc_specific;
211                 policy->sp.ssrc.value = ssrc;
212         } else {
213                 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
214         }
215 }
216
217 static void policy_destructor(void *obj)
218 {
219         struct ast_srtp_policy *policy = obj;
220
221         if (policy->sp.key) {
222                 ast_free(policy->sp.key);
223                 policy->sp.key = NULL;
224         }
225 }
226
227 static struct ast_srtp_policy *ast_srtp_policy_alloc()
228 {
229         struct ast_srtp_policy *tmp;
230
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");
233         }
234
235         return tmp;
236 }
237
238 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
239 {
240         ao2_t_ref(policy, -1, "Destroying policy");
241 }
242
243 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
244 {
245         switch (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;
253                 return 0;
254
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;
260                 p->auth_tag_len = 4;
261                 p->sec_serv = sec_serv_conf_and_auth;
262                 return 0;
263
264         default:
265                 ast_log(LOG_ERROR, "Invalid crypto suite: %d\n", suite);
266                 return -1;
267         }
268 }
269
270 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
271 {
272         return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
273 }
274
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)
276 {
277         size_t size = key_len + salt_len;
278         unsigned char *master_key;
279
280         if (policy->sp.key) {
281                 ast_free(policy->sp.key);
282                 policy->sp.key = NULL;
283         }
284
285         if (!(master_key = ast_calloc(1, size))) {
286                 return -1;
287         }
288
289         memcpy(master_key, key, key_len);
290         memcpy(master_key + key_len, salt, salt_len);
291
292         policy->sp.key = master_key;
293
294         return 0;
295 }
296
297 static int ast_srtp_get_random(unsigned char *key, size_t len)
298 {
299         return crypto_get_random(key, len) != err_status_ok ? -1: 0;
300 }
301
302 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
303 {
304         if (!srtp) {
305                 return;
306         }
307
308         srtp->cb = cb;
309         srtp->data = data;
310 }
311
312 /* Vtable functions */
313 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
314 {
315         int res = 0;
316         int i;
317         struct ast_rtp_instance_stats stats = {0,};
318
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) {
322                         break;
323                 }
324
325                 if (srtp->cb && srtp->cb->no_ctx) {
326                         if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
327                                 break;
328                         }
329                         if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
330                                 break;
331                         }
332                 } else {
333                         break;
334                 }
335         }
336
337         if (res != err_status_ok && res != err_status_replay_fail ) {
338                 ast_log(LOG_WARNING, "SRTP unprotect: %s\n", srtp_errstr(res));
339                 errno = EAGAIN;
340                 return -1;
341         }
342
343         return *len;
344 }
345
346 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
347 {
348         int res;
349
350         if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
351                 return -1;
352         }
353
354         memcpy(srtp->buf, *buf, *len);
355
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));
358                 return -1;
359         }
360
361         *buf = srtp->buf;
362         return *len;
363 }
364
365 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
366 {
367         struct ast_srtp *temp;
368
369         if (!(temp = res_srtp_new())) {
370                 return -1;
371         }
372
373         if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
374                 return -1;
375         }
376
377         ast_module_ref(ast_module_info->self);
378         temp->rtp = rtp;
379         *srtp = temp;
380
381         ao2_t_link((*srtp)->policies, policy, "Created initial policy");
382
383         return 0;
384 }
385
386 static void ast_srtp_destroy(struct ast_srtp *srtp)
387 {
388         if (srtp->session) {
389                 srtp_dealloc(srtp->session);
390         }
391
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");
394
395         ast_free(srtp);
396         ast_module_unref(ast_module_info->self);
397 }
398
399 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
400 {
401         struct ast_srtp_policy *match;
402
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");
406                 return -1;
407         }
408
409         if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
410                 return -1;
411         }
412
413         ao2_t_link(srtp->policies, policy, "Added additional stream");
414
415         return 0;
416 }
417
418 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
419 {
420         struct ast_srtp_policy *match;
421         struct srtp_policy_t sp = {
422                 .ssrc.type = ssrc_specific,
423                 .ssrc.value = from_ssrc,
424         };
425         err_status_t status;
426
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);
436                 }
437                 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
438         }
439
440         return 0;
441 }
442
443 static int res_srtp_init(void)
444 {
445         if (g_initialized) {
446                 return 0;
447         }
448
449         if (srtp_init() != err_status_ok) {
450                 return -1;
451         }
452
453         srtp_install_event_handler(srtp_event_cb);
454
455         return ast_rtp_engine_register_srtp(&srtp_res, &policy_res);
456 }
457
458 /*
459  * Exported functions
460  */
461
462 static int load_module(void)
463 {
464         return res_srtp_init();
465 }
466
467 static int unload_module(void)
468 {
469         ast_rtp_engine_unregister_srtp();
470         return 0;
471 }
472
473 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
474         .load = load_module,
475         .unload = unload_module,
476         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
477 );