res_srtp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /*! \file res_srtp.c
  21. *
  22. * \brief Secure RTP (SRTP)
  23. *
  24. * Secure RTP (SRTP)
  25. * Specified in RFC 3711.
  26. *
  27. * \author Mikael Magnusson <mikma@users.sourceforge.net>
  28. */
  29. /*** MODULEINFO
  30. <depend>srtp</depend>
  31. <support_level>core</support_level>
  32. ***/
  33. /* See https://wiki.asterisk.org/wiki/display/AST/Secure+Calling */
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include <srtp/srtp.h>
  37. #include "asterisk/lock.h"
  38. #include "asterisk/sched.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/options.h"
  41. #include "asterisk/rtp_engine.h"
  42. #include "asterisk/astobj2.h"
  43. struct ast_srtp {
  44. struct ast_rtp_instance *rtp;
  45. struct ao2_container *policies;
  46. srtp_t session;
  47. const struct ast_srtp_cb *cb;
  48. void *data;
  49. int warned;
  50. unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
  51. unsigned char rtcpbuf[8192 + AST_FRIENDLY_OFFSET];
  52. };
  53. struct ast_srtp_policy {
  54. srtp_policy_t sp;
  55. };
  56. /*! Tracks whether or not we've initialized the libsrtp library */
  57. static int g_initialized = 0;
  58. /* SRTP functions */
  59. static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
  60. static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
  61. static void ast_srtp_destroy(struct ast_srtp *srtp);
  62. static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
  63. static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
  64. static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
  65. static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
  66. static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
  67. static int ast_srtp_get_random(unsigned char *key, size_t len);
  68. /* Policy functions */
  69. static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
  70. static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
  71. static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
  72. 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);
  73. static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
  74. static struct ast_srtp_res srtp_res = {
  75. .create = ast_srtp_create,
  76. .replace = ast_srtp_replace,
  77. .destroy = ast_srtp_destroy,
  78. .add_stream = ast_srtp_add_stream,
  79. .change_source = ast_srtp_change_source,
  80. .set_cb = ast_srtp_set_cb,
  81. .unprotect = ast_srtp_unprotect,
  82. .protect = ast_srtp_protect,
  83. .get_random = ast_srtp_get_random
  84. };
  85. static struct ast_srtp_policy_res policy_res = {
  86. .alloc = ast_srtp_policy_alloc,
  87. .destroy = ast_srtp_policy_destroy,
  88. .set_suite = ast_srtp_policy_set_suite,
  89. .set_master_key = ast_srtp_policy_set_master_key,
  90. .set_ssrc = ast_srtp_policy_set_ssrc
  91. };
  92. static const char *srtp_errstr(int err)
  93. {
  94. switch(err) {
  95. case err_status_ok:
  96. return "nothing to report";
  97. case err_status_fail:
  98. return "unspecified failure";
  99. case err_status_bad_param:
  100. return "unsupported parameter";
  101. case err_status_alloc_fail:
  102. return "couldn't allocate memory";
  103. case err_status_dealloc_fail:
  104. return "couldn't deallocate properly";
  105. case err_status_init_fail:
  106. return "couldn't initialize";
  107. case err_status_terminus:
  108. return "can't process as much data as requested";
  109. case err_status_auth_fail:
  110. return "authentication failure";
  111. case err_status_cipher_fail:
  112. return "cipher failure";
  113. case err_status_replay_fail:
  114. return "replay check failed (bad index)";
  115. case err_status_replay_old:
  116. return "replay check failed (index too old)";
  117. case err_status_algo_fail:
  118. return "algorithm failed test routine";
  119. case err_status_no_such_op:
  120. return "unsupported operation";
  121. case err_status_no_ctx:
  122. return "no appropriate context found";
  123. case err_status_cant_check:
  124. return "unable to perform desired validation";
  125. case err_status_key_expired:
  126. return "can't use key any more";
  127. default:
  128. return "unknown";
  129. }
  130. }
  131. static int policy_hash_fn(const void *obj, const int flags)
  132. {
  133. const struct ast_srtp_policy *policy = obj;
  134. return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
  135. }
  136. static int policy_cmp_fn(void *obj, void *arg, int flags)
  137. {
  138. const struct ast_srtp_policy *one = obj, *two = arg;
  139. return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
  140. }
  141. static struct ast_srtp_policy *find_policy(struct ast_srtp *srtp, const srtp_policy_t *policy, int flags)
  142. {
  143. struct ast_srtp_policy tmp = {
  144. .sp = {
  145. .ssrc.type = policy->ssrc.type,
  146. .ssrc.value = policy->ssrc.value,
  147. },
  148. };
  149. return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
  150. }
  151. static struct ast_srtp *res_srtp_new(void)
  152. {
  153. struct ast_srtp *srtp;
  154. if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
  155. ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
  156. return NULL;
  157. }
  158. if (!(srtp->policies = ao2_t_container_alloc(5, policy_hash_fn, policy_cmp_fn, "SRTP policy container"))) {
  159. ast_free(srtp);
  160. return NULL;
  161. }
  162. srtp->warned = 1;
  163. return srtp;
  164. }
  165. /*
  166. struct ast_srtp_policy
  167. */
  168. static void srtp_event_cb(srtp_event_data_t *data)
  169. {
  170. switch (data->event) {
  171. case event_ssrc_collision:
  172. ast_debug(1, "SSRC collision\n");
  173. break;
  174. case event_key_soft_limit:
  175. ast_debug(1, "event_key_soft_limit\n");
  176. break;
  177. case event_key_hard_limit:
  178. ast_debug(1, "event_key_hard_limit\n");
  179. break;
  180. case event_packet_index_limit:
  181. ast_debug(1, "event_packet_index_limit\n");
  182. break;
  183. }
  184. }
  185. static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
  186. unsigned long ssrc, int inbound)
  187. {
  188. if (ssrc) {
  189. policy->sp.ssrc.type = ssrc_specific;
  190. policy->sp.ssrc.value = ssrc;
  191. } else {
  192. policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
  193. }
  194. }
  195. static void policy_destructor(void *obj)
  196. {
  197. struct ast_srtp_policy *policy = obj;
  198. if (policy->sp.key) {
  199. ast_free(policy->sp.key);
  200. policy->sp.key = NULL;
  201. }
  202. }
  203. static struct ast_srtp_policy *ast_srtp_policy_alloc()
  204. {
  205. struct ast_srtp_policy *tmp;
  206. if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
  207. ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
  208. }
  209. return tmp;
  210. }
  211. static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
  212. {
  213. ao2_t_ref(policy, -1, "Destroying policy");
  214. }
  215. static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
  216. {
  217. switch (suite) {
  218. case AST_AES_CM_128_HMAC_SHA1_80:
  219. p->cipher_type = AES_128_ICM;
  220. p->cipher_key_len = 30;
  221. p->auth_type = HMAC_SHA1;
  222. p->auth_key_len = 20;
  223. p->auth_tag_len = 10;
  224. p->sec_serv = sec_serv_conf_and_auth;
  225. return 0;
  226. case AST_AES_CM_128_HMAC_SHA1_32:
  227. p->cipher_type = AES_128_ICM;
  228. p->cipher_key_len = 30;
  229. p->auth_type = HMAC_SHA1;
  230. p->auth_key_len = 20;
  231. p->auth_tag_len = 4;
  232. p->sec_serv = sec_serv_conf_and_auth;
  233. return 0;
  234. default:
  235. ast_log(LOG_ERROR, "Invalid crypto suite: %u\n", suite);
  236. return -1;
  237. }
  238. }
  239. static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
  240. {
  241. return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
  242. }
  243. 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)
  244. {
  245. size_t size = key_len + salt_len;
  246. unsigned char *master_key;
  247. if (policy->sp.key) {
  248. ast_free(policy->sp.key);
  249. policy->sp.key = NULL;
  250. }
  251. if (!(master_key = ast_calloc(1, size))) {
  252. return -1;
  253. }
  254. memcpy(master_key, key, key_len);
  255. memcpy(master_key + key_len, salt, salt_len);
  256. policy->sp.key = master_key;
  257. return 0;
  258. }
  259. static int ast_srtp_get_random(unsigned char *key, size_t len)
  260. {
  261. return crypto_get_random(key, len) != err_status_ok ? -1: 0;
  262. }
  263. static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
  264. {
  265. if (!srtp) {
  266. return;
  267. }
  268. srtp->cb = cb;
  269. srtp->data = data;
  270. }
  271. /* Vtable functions */
  272. static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp)
  273. {
  274. int res = 0;
  275. int i;
  276. int retry = 0;
  277. struct ast_rtp_instance_stats stats = {0,};
  278. tryagain:
  279. for (i = 0; i < 2; i++) {
  280. res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
  281. if (res != err_status_no_ctx) {
  282. break;
  283. }
  284. if (srtp->cb && srtp->cb->no_ctx) {
  285. if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
  286. break;
  287. }
  288. if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
  289. break;
  290. }
  291. } else {
  292. break;
  293. }
  294. }
  295. if (retry == 0 && res == err_status_replay_old) {
  296. ast_log(AST_LOG_NOTICE, "SRTP unprotect failed with %s, retrying\n", srtp_errstr(res));
  297. if (srtp->session) {
  298. struct ast_srtp_policy *policy;
  299. struct ao2_iterator it;
  300. int policies_count;
  301. /* dealloc first */
  302. ast_debug(5, "SRTP destroy before re-create\n");
  303. srtp_dealloc(srtp->session);
  304. /* get the count */
  305. policies_count = ao2_container_count(srtp->policies);
  306. /* get the first to build up */
  307. it = ao2_iterator_init(srtp->policies, 0);
  308. policy = ao2_iterator_next(&it);
  309. ast_debug(5, "SRTP try to re-create\n");
  310. if (policy) {
  311. int res_srtp_create = srtp_create(&srtp->session, &policy->sp);
  312. if (res_srtp_create == err_status_ok) {
  313. ast_debug(5, "SRTP re-created with first policy\n");
  314. ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
  315. /* if we have more than one policy, add them */
  316. if (policies_count > 1) {
  317. ast_debug(5, "Add all the other %d policies\n",
  318. policies_count - 1);
  319. while ((policy = ao2_iterator_next(&it))) {
  320. srtp_add_stream(srtp->session, &policy->sp);
  321. ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
  322. }
  323. }
  324. retry++;
  325. ao2_iterator_destroy(&it);
  326. goto tryagain;
  327. }
  328. ast_log(LOG_ERROR, "SRTP session could not be re-created after unprotect failure: %s\n", srtp_errstr(res_srtp_create));
  329. /* If srtp_create() fails with a previously alloced session, it will have been dealloced before returning. */
  330. srtp->session = NULL;
  331. ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
  332. }
  333. ao2_iterator_destroy(&it);
  334. }
  335. }
  336. if (!srtp->session) {
  337. errno = EINVAL;
  338. return -1;
  339. }
  340. if (res != err_status_ok && res != err_status_replay_fail ) {
  341. if ((srtp->warned >= 10) && !((srtp->warned - 10) % 100)) {
  342. ast_log(AST_LOG_WARNING, "SRTP unprotect failed with: %s %d\n", srtp_errstr(res), srtp->warned);
  343. srtp->warned = 11;
  344. } else {
  345. srtp->warned++;
  346. }
  347. errno = EAGAIN;
  348. return -1;
  349. }
  350. return *len;
  351. }
  352. static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
  353. {
  354. int res;
  355. unsigned char *localbuf;
  356. if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
  357. return -1;
  358. }
  359. localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
  360. memcpy(localbuf, *buf, *len);
  361. if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
  362. ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
  363. return -1;
  364. }
  365. *buf = localbuf;
  366. return *len;
  367. }
  368. static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
  369. {
  370. struct ast_srtp *temp;
  371. if (!(temp = res_srtp_new())) {
  372. return -1;
  373. }
  374. ast_module_ref(ast_module_info->self);
  375. /* Any failures after this point can use ast_srtp_destroy to destroy the instance */
  376. if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
  377. /* Session either wasn't created or was created and dealloced. */
  378. temp->session = NULL;
  379. ast_srtp_destroy(temp);
  380. return -1;
  381. }
  382. temp->rtp = rtp;
  383. *srtp = temp;
  384. ao2_t_link((*srtp)->policies, policy, "Created initial policy");
  385. return 0;
  386. }
  387. static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
  388. {
  389. if ((*srtp) != NULL) {
  390. ast_srtp_destroy(*srtp);
  391. }
  392. return ast_srtp_create(srtp, rtp, policy);
  393. }
  394. static void ast_srtp_destroy(struct ast_srtp *srtp)
  395. {
  396. if (srtp->session) {
  397. srtp_dealloc(srtp->session);
  398. }
  399. ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
  400. ao2_t_ref(srtp->policies, -1, "Destroying container");
  401. ast_free(srtp);
  402. ast_module_unref(ast_module_info->self);
  403. }
  404. static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
  405. {
  406. struct ast_srtp_policy *match;
  407. /* For existing streams, replace if its an SSRC stream, or bail if its a wildcard */
  408. if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
  409. if (policy->sp.ssrc.type != ssrc_specific) {
  410. ast_log(AST_LOG_WARNING, "Cannot replace an existing wildcard policy\n");
  411. ao2_t_ref(match, -1, "Unreffing already existing policy");
  412. return -1;
  413. } else {
  414. if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
  415. ast_log(AST_LOG_WARNING, "Failed to remove SRTP stream for SSRC %u\n", match->sp.ssrc.value);
  416. }
  417. ao2_t_unlink(srtp->policies, match, "Remove existing match policy");
  418. ao2_t_ref(match, -1, "Unreffing already existing policy");
  419. }
  420. }
  421. ast_debug(3, "Adding new policy for %s %u\n",
  422. policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
  423. policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
  424. if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
  425. ast_log(AST_LOG_WARNING, "Failed to add SRTP stream for %s %u\n",
  426. policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
  427. policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
  428. return -1;
  429. }
  430. ao2_t_link(srtp->policies, policy, "Added additional stream");
  431. return 0;
  432. }
  433. static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
  434. {
  435. struct ast_srtp_policy *match;
  436. struct srtp_policy_t sp = {
  437. .ssrc.type = ssrc_specific,
  438. .ssrc.value = from_ssrc,
  439. };
  440. err_status_t status;
  441. /* If we find a match, return and unlink it from the container so we
  442. * can change the SSRC (which is part of the hash) and then have
  443. * ast_srtp_add_stream link it back in if all is well */
  444. if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
  445. match->sp.ssrc.value = to_ssrc;
  446. if (ast_srtp_add_stream(srtp, match)) {
  447. ast_log(LOG_WARNING, "Couldn't add stream\n");
  448. } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
  449. ast_debug(3, "Couldn't remove stream (%u)\n", status);
  450. }
  451. ao2_t_ref(match, -1, "Unreffing found policy in change_source");
  452. }
  453. return 0;
  454. }
  455. static void res_srtp_shutdown(void)
  456. {
  457. srtp_install_event_handler(NULL);
  458. ast_rtp_engine_unregister_srtp();
  459. #ifdef HAVE_SRTP_SHUTDOWN
  460. srtp_shutdown();
  461. #endif
  462. g_initialized = 0;
  463. }
  464. static int res_srtp_init(void)
  465. {
  466. if (g_initialized) {
  467. return 0;
  468. }
  469. if (srtp_init() != err_status_ok) {
  470. ast_log(AST_LOG_WARNING, "Failed to initialize libsrtp\n");
  471. return -1;
  472. }
  473. srtp_install_event_handler(srtp_event_cb);
  474. if (ast_rtp_engine_register_srtp(&srtp_res, &policy_res)) {
  475. ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
  476. res_srtp_shutdown();
  477. return -1;
  478. }
  479. g_initialized = 1;
  480. return 0;
  481. }
  482. /*
  483. * Exported functions
  484. */
  485. static int load_module(void)
  486. {
  487. return res_srtp_init();
  488. }
  489. static int unload_module(void)
  490. {
  491. res_srtp_shutdown();
  492. return 0;
  493. }
  494. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
  495. .support_level = AST_MODULE_SUPPORT_CORE,
  496. .load = load_module,
  497. .unload = unload_module,
  498. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  499. );