res_srtp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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: %d\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. if (srtp_create(&srtp->session, &policy->sp) == err_status_ok) {
  312. ast_debug(5, "SRTP re-created with first policy\n");
  313. ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
  314. /* if we have more than one policy, add them */
  315. if (policies_count > 1) {
  316. ast_debug(5, "Add all the other %d policies\n",
  317. policies_count - 1);
  318. while ((policy = ao2_iterator_next(&it))) {
  319. srtp_add_stream(srtp->session, &policy->sp);
  320. ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
  321. }
  322. }
  323. retry++;
  324. ao2_iterator_destroy(&it);
  325. goto tryagain;
  326. } else {
  327. srtp->session = NULL;
  328. }
  329. ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
  330. }
  331. ao2_iterator_destroy(&it);
  332. }
  333. }
  334. if (res != err_status_ok && res != err_status_replay_fail ) {
  335. if ((srtp->warned >= 10) && !((srtp->warned - 10) % 100)) {
  336. ast_log(AST_LOG_WARNING, "SRTP unprotect failed with: %s %d\n", srtp_errstr(res), srtp->warned);
  337. srtp->warned = 11;
  338. } else {
  339. srtp->warned++;
  340. }
  341. errno = EAGAIN;
  342. return -1;
  343. }
  344. return *len;
  345. }
  346. static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
  347. {
  348. int res;
  349. unsigned char *localbuf;
  350. if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
  351. return -1;
  352. }
  353. localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
  354. memcpy(localbuf, *buf, *len);
  355. if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
  356. ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
  357. return -1;
  358. }
  359. *buf = localbuf;
  360. return *len;
  361. }
  362. static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
  363. {
  364. struct ast_srtp *temp;
  365. if (!(temp = res_srtp_new())) {
  366. return -1;
  367. }
  368. ast_module_ref(ast_module_info->self);
  369. /* Any failures after this point can use ast_srtp_destroy to destroy the instance */
  370. if (srtp_create(&temp->session, &policy->sp) != err_status_ok) {
  371. /* Session either wasn't created or was created and dealloced. */
  372. temp->session = NULL;
  373. ast_srtp_destroy(temp);
  374. return -1;
  375. }
  376. temp->rtp = rtp;
  377. *srtp = temp;
  378. ao2_t_link((*srtp)->policies, policy, "Created initial policy");
  379. return 0;
  380. }
  381. static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
  382. {
  383. if ((*srtp) != NULL) {
  384. ast_srtp_destroy(*srtp);
  385. }
  386. return ast_srtp_create(srtp, rtp, policy);
  387. }
  388. static void ast_srtp_destroy(struct ast_srtp *srtp)
  389. {
  390. if (srtp->session) {
  391. srtp_dealloc(srtp->session);
  392. }
  393. ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
  394. ao2_t_ref(srtp->policies, -1, "Destroying container");
  395. ast_free(srtp);
  396. ast_module_unref(ast_module_info->self);
  397. }
  398. static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
  399. {
  400. struct ast_srtp_policy *match;
  401. /* For existing streams, replace if its an SSRC stream, or bail if its a wildcard */
  402. if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
  403. if (policy->sp.ssrc.type != ssrc_specific) {
  404. ast_log(AST_LOG_WARNING, "Cannot replace an existing wildcard policy\n");
  405. ao2_t_ref(match, -1, "Unreffing already existing policy");
  406. return -1;
  407. } else {
  408. if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
  409. ast_log(AST_LOG_WARNING, "Failed to remove SRTP stream for SSRC %d\n", match->sp.ssrc.value);
  410. }
  411. ao2_t_unlink(srtp->policies, match, "Remove existing match policy");
  412. ao2_t_ref(match, -1, "Unreffing already existing policy");
  413. }
  414. }
  415. ast_debug(3, "Adding new policy for %s %d\n",
  416. policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
  417. policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
  418. if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
  419. ast_log(AST_LOG_WARNING, "Failed to add SRTP stream for %s %d\n",
  420. policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
  421. policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
  422. return -1;
  423. }
  424. ao2_t_link(srtp->policies, policy, "Added additional stream");
  425. return 0;
  426. }
  427. static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
  428. {
  429. struct ast_srtp_policy *match;
  430. struct srtp_policy_t sp = {
  431. .ssrc.type = ssrc_specific,
  432. .ssrc.value = from_ssrc,
  433. };
  434. err_status_t status;
  435. /* If we find a match, return and unlink it from the container so we
  436. * can change the SSRC (which is part of the hash) and then have
  437. * ast_srtp_add_stream link it back in if all is well */
  438. if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
  439. match->sp.ssrc.value = to_ssrc;
  440. if (ast_srtp_add_stream(srtp, match)) {
  441. ast_log(LOG_WARNING, "Couldn't add stream\n");
  442. } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
  443. ast_debug(3, "Couldn't remove stream (%d)\n", status);
  444. }
  445. ao2_t_ref(match, -1, "Unreffing found policy in change_source");
  446. }
  447. return 0;
  448. }
  449. static void res_srtp_shutdown(void)
  450. {
  451. srtp_install_event_handler(NULL);
  452. ast_rtp_engine_unregister_srtp();
  453. g_initialized = 0;
  454. }
  455. static int res_srtp_init(void)
  456. {
  457. if (g_initialized) {
  458. return 0;
  459. }
  460. if (srtp_init() != err_status_ok) {
  461. ast_log(AST_LOG_WARNING, "Failed to initialize libsrtp\n");
  462. return -1;
  463. }
  464. srtp_install_event_handler(srtp_event_cb);
  465. if (ast_rtp_engine_register_srtp(&srtp_res, &policy_res)) {
  466. ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
  467. res_srtp_shutdown();
  468. return -1;
  469. }
  470. g_initialized = 1;
  471. return 0;
  472. }
  473. /*
  474. * Exported functions
  475. */
  476. static int load_module(void)
  477. {
  478. return res_srtp_init();
  479. }
  480. static int unload_module(void)
  481. {
  482. res_srtp_shutdown();
  483. return 0;
  484. }
  485. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
  486. .load = load_module,
  487. .unload = unload_module,
  488. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  489. );