00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "asterisk.h"
00039
00040 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 347995 $")
00041
00042 #include <srtp/srtp.h>
00043
00044 #include "asterisk/lock.h"
00045 #include "asterisk/sched.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/options.h"
00048 #include "asterisk/rtp_engine.h"
00049 #include "asterisk/astobj2.h"
00050
00051 struct ast_srtp {
00052 struct ast_rtp_instance *rtp;
00053 struct ao2_container *policies;
00054 srtp_t session;
00055 const struct ast_srtp_cb *cb;
00056 void *data;
00057 int warned;
00058 unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
00059 unsigned char rtcpbuf[8192 + AST_FRIENDLY_OFFSET];
00060 };
00061
00062 struct ast_srtp_policy {
00063 srtp_policy_t sp;
00064 };
00065
00066 static int g_initialized = 0;
00067
00068
00069 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
00070 static void ast_srtp_destroy(struct ast_srtp *srtp);
00071 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
00072 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
00073
00074 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
00075 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
00076 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
00077 static int ast_srtp_get_random(unsigned char *key, size_t len);
00078
00079
00080 static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
00081 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
00082 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
00083 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);
00084 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
00085
00086 static struct ast_srtp_res srtp_res = {
00087 .create = ast_srtp_create,
00088 .destroy = ast_srtp_destroy,
00089 .add_stream = ast_srtp_add_stream,
00090 .change_source = ast_srtp_change_source,
00091 .set_cb = ast_srtp_set_cb,
00092 .unprotect = ast_srtp_unprotect,
00093 .protect = ast_srtp_protect,
00094 .get_random = ast_srtp_get_random
00095 };
00096
00097 static struct ast_srtp_policy_res policy_res = {
00098 .alloc = ast_srtp_policy_alloc,
00099 .destroy = ast_srtp_policy_destroy,
00100 .set_suite = ast_srtp_policy_set_suite,
00101 .set_master_key = ast_srtp_policy_set_master_key,
00102 .set_ssrc = ast_srtp_policy_set_ssrc
00103 };
00104
00105 static const char *srtp_errstr(int err)
00106 {
00107 switch(err) {
00108 case err_status_ok:
00109 return "nothing to report";
00110 case err_status_fail:
00111 return "unspecified failure";
00112 case err_status_bad_param:
00113 return "unsupported parameter";
00114 case err_status_alloc_fail:
00115 return "couldn't allocate memory";
00116 case err_status_dealloc_fail:
00117 return "couldn't deallocate properly";
00118 case err_status_init_fail:
00119 return "couldn't initialize";
00120 case err_status_terminus:
00121 return "can't process as much data as requested";
00122 case err_status_auth_fail:
00123 return "authentication failure";
00124 case err_status_cipher_fail:
00125 return "cipher failure";
00126 case err_status_replay_fail:
00127 return "replay check failed (bad index)";
00128 case err_status_replay_old:
00129 return "replay check failed (index too old)";
00130 case err_status_algo_fail:
00131 return "algorithm failed test routine";
00132 case err_status_no_such_op:
00133 return "unsupported operation";
00134 case err_status_no_ctx:
00135 return "no appropriate context found";
00136 case err_status_cant_check:
00137 return "unable to perform desired validation";
00138 case err_status_key_expired:
00139 return "can't use key any more";
00140 default:
00141 return "unknown";
00142 }
00143 }
00144
00145 static int policy_hash_fn(const void *obj, const int flags)
00146 {
00147 const struct ast_srtp_policy *policy = obj;
00148
00149 return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
00150 }
00151
00152 static int policy_cmp_fn(void *obj, void *arg, int flags)
00153 {
00154 const struct ast_srtp_policy *one = obj, *two = arg;
00155
00156 return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
00157 }
00158
00159 static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
00160 {
00161 struct ast_srtp_policy tmp = {
00162 .sp = {
00163 .ssrc.type = policy->ssrc.type,
00164 .ssrc.value = policy->ssrc.value,
00165 },
00166 };
00167
00168 return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
00169 }
00170
00171 static struct ast_srtp *res_srtp_new(void)
00172 {
00173 struct ast_srtp *srtp;
00174
00175 if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
00176 ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
00177 return NULL;
00178 }
00179
00180 if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
00181 ast_free(srtp);
00182 return NULL;
00183 }
00184
00185 srtp->warned = 1;
00186
00187 return srtp;
00188 }
00189
00190
00191
00192
00193 static void srtp_event_cb(srtp_event_data_t *data)
00194 {
00195 switch (data->event) {
00196 case event_ssrc_collision:
00197 ast_debug(1, "SSRC collision\n");
00198 break;
00199 case event_key_soft_limit:
00200 ast_debug(1, "event_key_soft_limit\n");
00201 break;
00202 case event_key_hard_limit:
00203 ast_debug(1, "event_key_hard_limit\n");
00204 break;
00205 case event_packet_index_limit:
00206 ast_debug(1, "event_packet_index_limit\n");
00207 break;
00208 }
00209 }
00210
00211 static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
00212 unsigned long ssrc, int inbound)
00213 {
00214 if (ssrc) {
00215 policy->sp.ssrc.type = ssrc_specific;
00216 policy->sp.ssrc.value = ssrc;
00217 } else {
00218 policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
00219 }
00220 }
00221
00222 static void policy_destructor(void *obj)
00223 {
00224 struct ast_srtp_policy *policy = obj;
00225
00226 if (policy->sp.key) {
00227 ast_free(policy->sp.key);
00228 policy->sp.key = NULL;
00229 }
00230 }
00231
00232 static struct ast_srtp_policy *ast_srtp_policy_alloc()
00233 {
00234 struct ast_srtp_policy *tmp;
00235
00236 if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
00237 ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
00238 }
00239
00240 return tmp;
00241 }
00242
00243 static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
00244 {
00245 ao2_t_ref(policy, -1, "Destroying policy");
00246 }
00247
00248 static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
00249 {
00250 switch (suite) {
00251 case AST_AES_CM_128_HMAC_SHA1_80:
00252 p->cipher_type = AES_128_ICM;
00253 p->cipher_key_len = 30;
00254 p->auth_type = HMAC_SHA1;
00255 p->auth_key_len = 20;
00256 p->auth_tag_len = 10;
00257 p->sec_serv = sec_serv_conf_and_auth;
00258 return 0;
00259
00260 case AST_AES_CM_128_HMAC_SHA1_32:
00261 p->cipher_type = AES_128_ICM;
00262 p->cipher_key_len = 30;
00263 p->auth_type = HMAC_SHA1;
00264 p->auth_key_len = 20;
00265 p->auth_tag_len = 4;
00266 p->sec_serv = sec_serv_conf_and_auth;
00267 return 0;
00268
00269 default:
00270 ast_log(LOG_ERROR, "Invalid crypto suite: %d\n", suite);
00271 return -1;
00272 }
00273 }
00274
00275 static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
00276 {
00277 return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
00278 }
00279
00280 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)
00281 {
00282 size_t size = key_len + salt_len;
00283 unsigned char *master_key;
00284
00285 if (policy->sp.key) {
00286 ast_free(policy->sp.key);
00287 policy->sp.key = NULL;
00288 }
00289
00290 if (!(master_key = ast_calloc(1, size))) {
00291 return -1;
00292 }
00293
00294 memcpy(master_key, key, key_len);
00295 memcpy(master_key + key_len, salt, salt_len);
00296
00297 policy->sp.key = master_key;
00298
00299 return 0;
00300 }
00301
00302 static int ast_srtp_get_random(unsigned char *key, size_t len)
00303 {
00304 return crypto_get_random(key, len) != err_status_ok ? -1: 0;
00305 }
00306
00307 static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
00308 {
00309 if (!srtp) {
00310 return;
00311 }
00312
00313 srtp->cb = cb;
00314 srtp->data = data;
00315 }
00316
00317
00318 static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
00319 {
00320 int res = 0;
00321 int i;
00322 int retry = 0;
00323 struct ast_rtp_instance_stats stats = {0,};
00324
00325 tryagain:
00326
00327 for (i = 0; i < 2; i++) {
00328 res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
00329 if (res != err_status_no_ctx) {
00330 break;
00331 }
00332
00333 if (srtp->cb && srtp->cb->no_ctx) {
00334 if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
00335 break;
00336 }
00337 if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
00338 break;
00339 }
00340 } else {
00341 break;
00342 }
00343 }
00344
00345 if (retry == 0 && res == err_status_replay_old) {
00346 ast_log(LOG_WARNING, "SRTP unprotect: %s\n", srtp_errstr(res));
00347
00348 if (srtp->session) {
00349 struct ast_srtp_policy *policy;
00350 struct ao2_iterator it;
00351 int policies_count = 0;
00352
00353
00354 ast_log(LOG_WARNING, "SRTP destroy before re-create\n");
00355 srtp_dealloc(srtp->session);
00356
00357
00358 policies_count = ao2_container_count(srtp->policies);
00359
00360
00361 it = ao2_iterator_init(srtp->policies, 0);
00362 policy = ao2_iterator_next(&it);
00363
00364 ast_log(LOG_WARNING, "SRTP try to re-create\n");
00365 if (srtp_create(&srtp->session, &policy->sp) == err_status_ok) {
00366 ast_log(LOG_WARNING, "SRTP re-created with first policy\n");
00367
00368
00369 ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
00370
00371
00372 if (policies_count > 1) {
00373 ast_log(LOG_WARNING, "Add all the other %d policies\n", policies_count-1);
00374 while ((policy = ao2_iterator_next(&it))) {
00375 srtp_add_stream(srtp->session, &policy->sp);
00376 ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
00377 }
00378 }
00379
00380 retry++;
00381 ao2_iterator_destroy(&it);
00382 goto tryagain;
00383 }
00384 ao2_iterator_destroy(&it);
00385 }
00386 }
00387
00388 if (res != err_status_ok && res != err_status_replay_fail ) {
00389 if ((srtp->warned >= 10) && !((srtp->warned - 10) % 100)) {
00390 ast_log(LOG_WARNING, "SRTP unprotect: %s %d\n", srtp_errstr(res), srtp->warned);
00391 srtp->warned = 11;
00392 } else {
00393 srtp->warned++;
00394 }
00395 errno = EAGAIN;
00396 return -1;
00397 }
00398
00399 return *len;
00400 }
00401
00402 static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
00403 {
00404 int res;
00405 unsigned char *localbuf;
00406
00407 if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
00408 return -1;
00409 }
00410
00411 localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
00412
00413 memcpy(localbuf, *buf, *len);
00414
00415 if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
00416 ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
00417 return -1;
00418 }
00419
00420 *buf = localbuf;
00421 return *len;
00422 }
00423
00424 static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
00425 {
00426 struct ast_srtp *temp;
00427
00428 if (!(temp = res_srtp_new())) {
00429 return -1;
00430 }
00431
00432 if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
00433 return -1;
00434 }
00435
00436 ast_module_ref(ast_module_info->self);
00437 temp->rtp = rtp;
00438 *srtp = temp;
00439
00440 ao2_t_link((*srtp)->policies, policy, "Created initial policy");
00441
00442 return 0;
00443 }
00444
00445 static void ast_srtp_destroy(struct ast_srtp *srtp)
00446 {
00447 if (srtp->session) {
00448 srtp_dealloc(srtp->session);
00449 }
00450
00451 ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
00452 ao2_t_ref(srtp->policies, -1, "Destroying container");
00453
00454 ast_free(srtp);
00455 ast_module_unref(ast_module_info->self);
00456 }
00457
00458 static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
00459 {
00460 struct ast_srtp_policy *match;
00461
00462 if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
00463 ast_debug(3, "Policy already exists, not re-adding\n");
00464 ao2_t_ref(match, -1, "Unreffing already existing policy");
00465 return -1;
00466 }
00467
00468 if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
00469 return -1;
00470 }
00471
00472 ao2_t_link(srtp->policies, policy, "Added additional stream");
00473
00474 return 0;
00475 }
00476
00477 static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
00478 {
00479 struct ast_srtp_policy *match;
00480 struct srtp_policy_t sp = {
00481 .ssrc.type = ssrc_specific,
00482 .ssrc.value = from_ssrc,
00483 };
00484 err_status_t status;
00485
00486
00487
00488
00489 if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
00490 match->sp.ssrc.value = to_ssrc;
00491 if (ast_srtp_add_stream(srtp, match)) {
00492 ast_log(LOG_WARNING, "Couldn't add stream\n");
00493 } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
00494 ast_debug(3, "Couldn't remove stream (%d)\n", status);
00495 }
00496 ao2_t_ref(match, -1, "Unreffing found policy in change_source");
00497 }
00498
00499 return 0;
00500 }
00501
00502 static int res_srtp_init(void)
00503 {
00504 if (g_initialized) {
00505 return 0;
00506 }
00507
00508 if (srtp_init() != err_status_ok) {
00509 return -1;
00510 }
00511
00512 srtp_install_event_handler(srtp_event_cb);
00513
00514 return ast_rtp_engine_register_srtp(&srtp_res, &policy_res);
00515 }
00516
00517
00518
00519
00520
00521 static int load_module(void)
00522 {
00523 return res_srtp_init();
00524 }
00525
00526 static int unload_module(void)
00527 {
00528 ast_rtp_engine_unregister_srtp();
00529 return 0;
00530 }
00531
00532 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
00533 .load = load_module,
00534 .unload = unload_module,
00535 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
00536 );