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