res_srtp.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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
  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. /*** MODULEINFO
  31. <depend>srtp</depend>
  32. <use type="external">openssl</use>
  33. <support_level>core</support_level>
  34. ***/
  35. /* See https://docs.asterisk.org/Deployment/Secure-Calling/ */
  36. #include "asterisk.h" /* for NULL, size_t, memcpy, etc */
  37. #include <math.h> /* for pow */
  38. #if HAVE_SRTP_VERSION > 1
  39. # include <srtp2/srtp.h>
  40. # include "srtp/srtp_compat.h"
  41. # include <openssl/rand.h>
  42. #else
  43. # include <srtp/srtp.h>
  44. # ifdef HAVE_OPENSSL
  45. # include <openssl/rand.h>
  46. # else
  47. # include <srtp/crypto_kernel.h>
  48. # endif
  49. #endif
  50. #include "asterisk/astobj2.h" /* for ao2_t_ref, etc */
  51. #include "asterisk/frame.h" /* for AST_FRIENDLY_OFFSET */
  52. #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
  53. #include "asterisk/module.h" /* for ast_module_info, etc */
  54. #include "asterisk/sdp_srtp.h"
  55. #include "asterisk/res_srtp.h" /* for ast_srtp_cb, ast_srtp_suite, etc */
  56. #include "asterisk/rtp_engine.h" /* for ast_rtp_engine_register_srtp, etc */
  57. #include "asterisk/utils.h" /* for ast_free, ast_calloc */
  58. struct ast_srtp {
  59. struct ast_rtp_instance *rtp;
  60. struct ao2_container *policies;
  61. srtp_t session;
  62. const struct ast_srtp_cb *cb;
  63. void *data;
  64. int warned;
  65. unsigned char buf[8192 + AST_FRIENDLY_OFFSET];
  66. unsigned char rtcpbuf[8192 + AST_FRIENDLY_OFFSET];
  67. };
  68. struct ast_srtp_policy {
  69. srtp_policy_t sp;
  70. };
  71. /*! Tracks whether or not we've initialized the libsrtp library */
  72. static int g_initialized = 0;
  73. /* SRTP functions */
  74. static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
  75. static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy);
  76. static void ast_srtp_destroy(struct ast_srtp *srtp);
  77. static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy);
  78. static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc);
  79. static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int rtcp);
  80. static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp);
  81. static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data);
  82. static int ast_srtp_get_random(unsigned char *key, size_t len);
  83. /* Policy functions */
  84. static struct ast_srtp_policy *ast_srtp_policy_alloc(void);
  85. static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy);
  86. static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite);
  87. 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);
  88. static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound);
  89. static struct ast_srtp_res srtp_res = {
  90. .create = ast_srtp_create,
  91. .replace = ast_srtp_replace,
  92. .destroy = ast_srtp_destroy,
  93. .add_stream = ast_srtp_add_stream,
  94. .change_source = ast_srtp_change_source,
  95. .set_cb = ast_srtp_set_cb,
  96. .unprotect = ast_srtp_unprotect,
  97. .protect = ast_srtp_protect,
  98. .get_random = ast_srtp_get_random
  99. };
  100. static struct ast_srtp_policy_res policy_res = {
  101. .alloc = ast_srtp_policy_alloc,
  102. .destroy = ast_srtp_policy_destroy,
  103. .set_suite = ast_srtp_policy_set_suite,
  104. .set_master_key = ast_srtp_policy_set_master_key,
  105. .set_ssrc = ast_srtp_policy_set_ssrc
  106. };
  107. static const char *srtp_errstr(int err)
  108. {
  109. switch(err) {
  110. case err_status_ok:
  111. return "nothing to report";
  112. case err_status_fail:
  113. return "unspecified failure";
  114. case err_status_bad_param:
  115. return "unsupported parameter";
  116. case err_status_alloc_fail:
  117. return "couldn't allocate memory";
  118. case err_status_dealloc_fail:
  119. return "couldn't deallocate properly";
  120. case err_status_init_fail:
  121. return "couldn't initialize";
  122. case err_status_terminus:
  123. return "can't process as much data as requested";
  124. case err_status_auth_fail:
  125. return "authentication failure";
  126. case err_status_cipher_fail:
  127. return "cipher failure";
  128. case err_status_replay_fail:
  129. return "replay check failed (bad index)";
  130. case err_status_replay_old:
  131. return "replay check failed (index too old)";
  132. case err_status_algo_fail:
  133. return "algorithm failed test routine";
  134. case err_status_no_such_op:
  135. return "unsupported operation";
  136. case err_status_no_ctx:
  137. return "no appropriate context found";
  138. case err_status_cant_check:
  139. return "unable to perform desired validation";
  140. case err_status_key_expired:
  141. return "can't use key any more";
  142. default:
  143. return "unknown";
  144. }
  145. }
  146. static int policy_hash_fn(const void *obj, const int flags)
  147. {
  148. const struct ast_srtp_policy *policy = obj;
  149. return policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type;
  150. }
  151. static int policy_cmp_fn(void *obj, void *arg, int flags)
  152. {
  153. const struct ast_srtp_policy *one = obj, *two = arg;
  154. return one->sp.ssrc.type == two->sp.ssrc.type && one->sp.ssrc.value == two->sp.ssrc.value;
  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. return ao2_t_find(srtp->policies, &tmp, flags, "Looking for policy");
  165. }
  166. static struct ast_srtp *res_srtp_new(void)
  167. {
  168. struct ast_srtp *srtp;
  169. if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
  170. ast_log(LOG_ERROR, "Unable to allocate memory for srtp\n");
  171. return NULL;
  172. }
  173. srtp->policies = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 5,
  174. policy_hash_fn, NULL, policy_cmp_fn, "SRTP policy container");
  175. if (!srtp->policies) {
  176. ast_free(srtp);
  177. return NULL;
  178. }
  179. srtp->warned = 1;
  180. return srtp;
  181. }
  182. /*
  183. struct ast_srtp_policy
  184. */
  185. static void srtp_event_cb(srtp_event_data_t *data)
  186. {
  187. switch (data->event) {
  188. case event_ssrc_collision:
  189. ast_debug(1, "SSRC collision\n");
  190. break;
  191. case event_key_soft_limit:
  192. ast_debug(1, "event_key_soft_limit\n");
  193. break;
  194. case event_key_hard_limit:
  195. ast_debug(1, "event_key_hard_limit\n");
  196. break;
  197. case event_packet_index_limit:
  198. ast_debug(1, "event_packet_index_limit\n");
  199. break;
  200. }
  201. }
  202. static void ast_srtp_policy_set_ssrc(struct ast_srtp_policy *policy,
  203. unsigned long ssrc, int inbound)
  204. {
  205. if (ssrc) {
  206. policy->sp.ssrc.type = ssrc_specific;
  207. policy->sp.ssrc.value = ssrc;
  208. } else {
  209. policy->sp.ssrc.type = inbound ? ssrc_any_inbound : ssrc_any_outbound;
  210. }
  211. }
  212. static void policy_destructor(void *obj)
  213. {
  214. struct ast_srtp_policy *policy = obj;
  215. if (policy->sp.key) {
  216. ast_free(policy->sp.key);
  217. policy->sp.key = NULL;
  218. }
  219. }
  220. static struct ast_srtp_policy *ast_srtp_policy_alloc()
  221. {
  222. struct ast_srtp_policy *tmp;
  223. if (!(tmp = ao2_t_alloc(sizeof(*tmp), policy_destructor, "Allocating policy"))) {
  224. ast_log(LOG_ERROR, "Unable to allocate memory for srtp_policy\n");
  225. }
  226. return tmp;
  227. }
  228. static void ast_srtp_policy_destroy(struct ast_srtp_policy *policy)
  229. {
  230. ao2_t_ref(policy, -1, "Destroying policy");
  231. }
  232. static int policy_set_suite(crypto_policy_t *p, enum ast_srtp_suite suite)
  233. {
  234. switch (suite) {
  235. case AST_AES_CM_128_HMAC_SHA1_80:
  236. crypto_policy_set_aes_cm_128_hmac_sha1_80(p);
  237. return 0;
  238. case AST_AES_CM_128_HMAC_SHA1_32:
  239. crypto_policy_set_aes_cm_128_hmac_sha1_32(p);
  240. return 0;
  241. #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
  242. case AST_AES_CM_192_HMAC_SHA1_80:
  243. crypto_policy_set_aes_cm_192_hmac_sha1_80(p);
  244. return 0;
  245. case AST_AES_CM_192_HMAC_SHA1_32:
  246. crypto_policy_set_aes_cm_192_hmac_sha1_32(p);
  247. return 0;
  248. #endif
  249. #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
  250. case AST_AES_CM_256_HMAC_SHA1_80:
  251. crypto_policy_set_aes_cm_256_hmac_sha1_80(p);
  252. return 0;
  253. case AST_AES_CM_256_HMAC_SHA1_32:
  254. crypto_policy_set_aes_cm_256_hmac_sha1_32(p);
  255. return 0;
  256. #endif
  257. #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
  258. case AST_AES_GCM_128:
  259. crypto_policy_set_aes_gcm_128_16_auth(p);
  260. return 0;
  261. case AST_AES_GCM_128_8:
  262. crypto_policy_set_aes_gcm_128_8_auth(p);
  263. return 0;
  264. #endif
  265. #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
  266. case AST_AES_GCM_256:
  267. crypto_policy_set_aes_gcm_256_16_auth(p);
  268. return 0;
  269. case AST_AES_GCM_256_8:
  270. crypto_policy_set_aes_gcm_256_8_auth(p);
  271. return 0;
  272. #endif
  273. default:
  274. ast_log(LOG_ERROR, "Invalid crypto suite: %u\n", suite);
  275. return -1;
  276. }
  277. }
  278. static int ast_srtp_policy_set_suite(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
  279. {
  280. return policy_set_suite(&policy->sp.rtp, suite) | policy_set_suite(&policy->sp.rtcp, suite);
  281. }
  282. 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)
  283. {
  284. size_t size = key_len + salt_len;
  285. unsigned char *master_key;
  286. if (policy->sp.key) {
  287. ast_free(policy->sp.key);
  288. policy->sp.key = NULL;
  289. }
  290. if (!(master_key = ast_calloc(1, size))) {
  291. return -1;
  292. }
  293. memcpy(master_key, key, key_len);
  294. memcpy(master_key + key_len, salt, salt_len);
  295. policy->sp.key = master_key;
  296. return 0;
  297. }
  298. static int ast_srtp_get_random(unsigned char *key, size_t len)
  299. {
  300. #ifdef HAVE_OPENSSL
  301. return RAND_bytes(key, len) > 0 ? 0: -1;
  302. #else
  303. return crypto_get_random(key, len) != err_status_ok ? -1: 0;
  304. #endif
  305. }
  306. static void ast_srtp_set_cb(struct ast_srtp *srtp, const struct ast_srtp_cb *cb, void *data)
  307. {
  308. if (!srtp) {
  309. return;
  310. }
  311. srtp->cb = cb;
  312. srtp->data = data;
  313. }
  314. /* Vtable functions */
  315. static int ast_srtp_unprotect(struct ast_srtp *srtp, void *buf, int *len, int flags)
  316. {
  317. int res = 0;
  318. int i;
  319. int rtcp = (flags & 0x01) >> 0;
  320. int retry = (flags & 0x02) >> 1;
  321. struct ast_rtp_instance_stats stats = {0,};
  322. tryagain:
  323. if (!srtp->session) {
  324. ast_log(LOG_ERROR, "SRTP unprotect %s - missing session\n", rtcp ? "rtcp" : "rtp");
  325. errno = EINVAL;
  326. return -1;
  327. }
  328. for (i = 0; i < 2; i++) {
  329. res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
  330. if (res != err_status_no_ctx) {
  331. break;
  332. }
  333. if (srtp->cb && srtp->cb->no_ctx) {
  334. if (ast_rtp_instance_get_stats(srtp->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC)) {
  335. break;
  336. }
  337. if (srtp->cb->no_ctx(srtp->rtp, stats.remote_ssrc, srtp->data) < 0) {
  338. break;
  339. }
  340. } else {
  341. break;
  342. }
  343. }
  344. if (retry == 0 && res == err_status_replay_old) {
  345. ast_log(AST_LOG_NOTICE, "SRTP unprotect failed with %s, retrying\n", srtp_errstr(res));
  346. if (srtp->session) {
  347. struct ast_srtp_policy *policy;
  348. struct ao2_iterator it;
  349. int policies_count;
  350. /* dealloc first */
  351. ast_debug(5, "SRTP destroy before re-create\n");
  352. srtp_dealloc(srtp->session);
  353. /* get the count */
  354. policies_count = ao2_container_count(srtp->policies);
  355. /* get the first to build up */
  356. it = ao2_iterator_init(srtp->policies, 0);
  357. policy = ao2_iterator_next(&it);
  358. ast_debug(5, "SRTP try to re-create\n");
  359. if (policy) {
  360. int res_srtp_create = srtp_create(&srtp->session, &policy->sp);
  361. if (res_srtp_create == err_status_ok) {
  362. ast_debug(5, "SRTP re-created with first policy\n");
  363. ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
  364. /* if we have more than one policy, add them */
  365. if (policies_count > 1) {
  366. ast_debug(5, "Add all the other %d policies\n",
  367. policies_count - 1);
  368. while ((policy = ao2_iterator_next(&it))) {
  369. srtp_add_stream(srtp->session, &policy->sp);
  370. ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
  371. }
  372. }
  373. retry++;
  374. ao2_iterator_destroy(&it);
  375. goto tryagain;
  376. }
  377. ast_log(LOG_ERROR, "SRTP session could not be re-created after unprotect failure: %s\n", srtp_errstr(res_srtp_create));
  378. /* If srtp_create() fails with a previously alloced session, it will have been dealloced before returning. */
  379. srtp->session = NULL;
  380. ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
  381. }
  382. ao2_iterator_destroy(&it);
  383. }
  384. }
  385. if (!srtp->session) {
  386. errno = EINVAL;
  387. return -1;
  388. }
  389. if (res != err_status_ok && res != err_status_replay_fail ) {
  390. /*
  391. * Authentication failures happen when an active attacker tries to
  392. * insert malicious RTP packets. Furthermore, authentication failures
  393. * happen, when the other party encrypts the sRTP data in an unexpected
  394. * way. This happens quite often with RTCP. Therefore, when you see
  395. * authentication failures, try to identify the implementation
  396. * (author and product name) used by your other party. Try to investigate
  397. * whether they use a custom library or an outdated version of libSRTP.
  398. */
  399. if (rtcp) {
  400. ast_verb(2, "SRTCP unprotect failed on SSRC %u because of %s\n",
  401. ast_rtp_instance_get_ssrc(srtp->rtp), srtp_errstr(res));
  402. } else {
  403. if ((srtp->warned >= 10) && !((srtp->warned - 10) % 150)) {
  404. ast_verb(2, "SRTP unprotect failed on SSRC %u because of %s %d\n",
  405. ast_rtp_instance_get_ssrc(srtp->rtp), srtp_errstr(res), srtp->warned);
  406. srtp->warned = 11;
  407. } else {
  408. srtp->warned++;
  409. }
  410. }
  411. errno = EAGAIN;
  412. return -1;
  413. }
  414. return *len;
  415. }
  416. static int ast_srtp_protect(struct ast_srtp *srtp, void **buf, int *len, int rtcp)
  417. {
  418. int res;
  419. unsigned char *localbuf;
  420. if (!srtp->session) {
  421. ast_log(LOG_ERROR, "SRTP protect %s - missing session\n", rtcp ? "rtcp" : "rtp");
  422. errno = EINVAL;
  423. return -1;
  424. }
  425. if ((*len + SRTP_MAX_TRAILER_LEN) > sizeof(srtp->buf)) {
  426. return -1;
  427. }
  428. localbuf = rtcp ? srtp->rtcpbuf : srtp->buf;
  429. memcpy(localbuf, *buf, *len);
  430. if ((res = rtcp ? srtp_protect_rtcp(srtp->session, localbuf, len) : srtp_protect(srtp->session, localbuf, len)) != err_status_ok && res != err_status_replay_fail) {
  431. ast_log(LOG_WARNING, "SRTP protect: %s\n", srtp_errstr(res));
  432. return -1;
  433. }
  434. *buf = localbuf;
  435. return *len;
  436. }
  437. static int ast_srtp_create(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
  438. {
  439. struct ast_srtp *temp;
  440. int status;
  441. if (!(temp = res_srtp_new())) {
  442. return -1;
  443. }
  444. ast_module_ref(ast_module_info->self);
  445. /* Any failures after this point can use ast_srtp_destroy to destroy the instance */
  446. status = srtp_create(&temp->session, &policy->sp);
  447. if (status != err_status_ok) {
  448. /* Session either wasn't created or was created and dealloced. */
  449. temp->session = NULL;
  450. ast_srtp_destroy(temp);
  451. ast_log(LOG_ERROR, "Failed to create srtp session on rtp instance (%p) - %s\n",
  452. rtp, srtp_errstr(status));
  453. return -1;
  454. }
  455. temp->rtp = rtp;
  456. *srtp = temp;
  457. ao2_t_link((*srtp)->policies, policy, "Created initial policy");
  458. return 0;
  459. }
  460. static int ast_srtp_replace(struct ast_srtp **srtp, struct ast_rtp_instance *rtp, struct ast_srtp_policy *policy)
  461. {
  462. struct ast_srtp *old = *srtp;
  463. int res = ast_srtp_create(srtp, rtp, policy);
  464. if (!res && old) {
  465. ast_srtp_destroy(old);
  466. }
  467. if (res) {
  468. ast_log(LOG_ERROR, "Failed to replace srtp (%p) on rtp instance (%p) "
  469. "- keeping old\n", *srtp, rtp);
  470. }
  471. return res;
  472. }
  473. static void ast_srtp_destroy(struct ast_srtp *srtp)
  474. {
  475. if (srtp->session) {
  476. srtp_dealloc(srtp->session);
  477. }
  478. ao2_t_callback(srtp->policies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Unallocate policy");
  479. ao2_t_ref(srtp->policies, -1, "Destroying container");
  480. ast_free(srtp);
  481. ast_module_unref(ast_module_info->self);
  482. }
  483. static int ast_srtp_add_stream(struct ast_srtp *srtp, struct ast_srtp_policy *policy)
  484. {
  485. struct ast_srtp_policy *match;
  486. /* For existing streams, replace if its an SSRC stream, or bail if its a wildcard */
  487. if ((match = find_policy(srtp, &policy->sp, OBJ_POINTER))) {
  488. if (policy->sp.ssrc.type != ssrc_specific) {
  489. ast_log(AST_LOG_WARNING, "Cannot replace an existing wildcard policy\n");
  490. ao2_t_ref(match, -1, "Unreffing already existing policy");
  491. return -1;
  492. } else {
  493. if (srtp_remove_stream(srtp->session, match->sp.ssrc.value) != err_status_ok) {
  494. ast_log(AST_LOG_WARNING, "Failed to remove SRTP stream for SSRC %u\n", match->sp.ssrc.value);
  495. }
  496. ao2_t_unlink(srtp->policies, match, "Remove existing match policy");
  497. ao2_t_ref(match, -1, "Unreffing already existing policy");
  498. }
  499. }
  500. ast_debug(3, "Adding new policy for %s %u\n",
  501. policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
  502. policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
  503. if (srtp_add_stream(srtp->session, &policy->sp) != err_status_ok) {
  504. ast_log(AST_LOG_WARNING, "Failed to add SRTP stream for %s %u\n",
  505. policy->sp.ssrc.type == ssrc_specific ? "SSRC" : "type",
  506. policy->sp.ssrc.type == ssrc_specific ? policy->sp.ssrc.value : policy->sp.ssrc.type);
  507. return -1;
  508. }
  509. ao2_t_link(srtp->policies, policy, "Added additional stream");
  510. return 0;
  511. }
  512. static int ast_srtp_change_source(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
  513. {
  514. struct ast_srtp_policy *match;
  515. struct srtp_policy_t sp = {
  516. .ssrc.type = ssrc_specific,
  517. .ssrc.value = from_ssrc,
  518. };
  519. err_status_t status;
  520. /* If we find a match, return and unlink it from the container so we
  521. * can change the SSRC (which is part of the hash) and then have
  522. * ast_srtp_add_stream link it back in if all is well */
  523. if ((match = find_policy(srtp, &sp, OBJ_POINTER | OBJ_UNLINK))) {
  524. match->sp.ssrc.value = to_ssrc;
  525. if (ast_srtp_add_stream(srtp, match)) {
  526. ast_log(LOG_WARNING, "Couldn't add stream\n");
  527. } else if ((status = srtp_remove_stream(srtp->session, from_ssrc))) {
  528. ast_debug(3, "Couldn't remove stream (%u)\n", status);
  529. }
  530. ao2_t_ref(match, -1, "Unreffing found policy in change_source");
  531. }
  532. return 0;
  533. }
  534. struct ast_sdp_crypto {
  535. char *a_crypto;
  536. unsigned char local_key[SRTP_MAX_KEY_LEN];
  537. int tag;
  538. char local_key64[((SRTP_MAX_KEY_LEN) * 8 + 5) / 6 + 1];
  539. unsigned char remote_key[SRTP_MAX_KEY_LEN];
  540. int key_len;
  541. };
  542. static void res_sdp_crypto_dtor(struct ast_sdp_crypto *crypto)
  543. {
  544. if (crypto) {
  545. ast_free(crypto->a_crypto);
  546. crypto->a_crypto = NULL;
  547. ast_free(crypto);
  548. ast_module_unref(ast_module_info->self);
  549. }
  550. }
  551. static struct ast_sdp_crypto *crypto_init_keys(struct ast_sdp_crypto *p, const int key_len)
  552. {
  553. unsigned char remote_key[key_len];
  554. if (srtp_res.get_random(p->local_key, key_len) < 0) {
  555. return NULL;
  556. }
  557. ast_base64encode(p->local_key64, p->local_key, key_len, sizeof(p->local_key64));
  558. p->key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
  559. if (p->key_len != key_len) {
  560. ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", p->key_len, key_len);
  561. return NULL;
  562. }
  563. if (memcmp(remote_key, p->local_key, p->key_len)) {
  564. ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
  565. return NULL;
  566. }
  567. ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
  568. return p;
  569. }
  570. static struct ast_sdp_crypto *sdp_crypto_alloc(const int key_len)
  571. {
  572. struct ast_sdp_crypto *p, *result;
  573. if (!(p = ast_calloc(1, sizeof(*p)))) {
  574. return NULL;
  575. }
  576. p->tag = 1;
  577. ast_module_ref(ast_module_info->self);
  578. /* default is a key which uses AST_AES_CM_128_HMAC_SHA1_xx */
  579. result = crypto_init_keys(p, key_len);
  580. if (!result) {
  581. res_sdp_crypto_dtor(p);
  582. }
  583. return result;
  584. }
  585. static struct ast_sdp_crypto *res_sdp_crypto_alloc(void)
  586. {
  587. return sdp_crypto_alloc(SRTP_MASTER_KEY_LEN);
  588. }
  589. static int res_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
  590. {
  591. int res;
  592. /* Rebuild the crypto line */
  593. ast_free(p->a_crypto);
  594. p->a_crypto = NULL;
  595. if ((taglen & 0x007f) == 8) {
  596. res = ast_asprintf(&p->a_crypto, "%d AEAD_AES_%d_GCM_%d inline:%s",
  597. p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
  598. } else if ((taglen & 0x007f) == 16) {
  599. res = ast_asprintf(&p->a_crypto, "%d AEAD_AES_%d_GCM inline:%s",
  600. p->tag, 128 + ((taglen & 0x0300) >> 2), p->local_key64);
  601. } else if ((taglen & 0x0300) && !(taglen & 0x0080)) {
  602. res = ast_asprintf(&p->a_crypto, "%d AES_%d_CM_HMAC_SHA1_%d inline:%s",
  603. p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
  604. } else {
  605. res = ast_asprintf(&p->a_crypto, "%d AES_CM_%d_HMAC_SHA1_%d inline:%s",
  606. p->tag, 128 + ((taglen & 0x0300) >> 2), taglen & 0x007f, p->local_key64);
  607. }
  608. if (res == -1 || !p->a_crypto) {
  609. ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
  610. return -1;
  611. }
  612. ast_debug(1, "Crypto line: a=crypto:%s\n", p->a_crypto);
  613. return 0;
  614. }
  615. static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, int key_len, unsigned long ssrc, int inbound)
  616. {
  617. if (policy_res.set_master_key(policy, master_key, key_len, NULL, 0) < 0) {
  618. return -1;
  619. }
  620. if (policy_res.set_suite(policy, suite_val)) {
  621. ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
  622. return -1;
  623. }
  624. policy_res.set_ssrc(policy, ssrc, inbound);
  625. return 0;
  626. }
  627. static int crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, int key_len, struct ast_rtp_instance *rtp)
  628. {
  629. struct ast_srtp_policy *local_policy = NULL;
  630. struct ast_srtp_policy *remote_policy = NULL;
  631. struct ast_rtp_instance_stats stats = {0,};
  632. int res = -1;
  633. if (!p) {
  634. return -1;
  635. }
  636. if (!(local_policy = policy_res.alloc())) {
  637. return -1;
  638. }
  639. if (!(remote_policy = policy_res.alloc())) {
  640. goto err;
  641. }
  642. if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
  643. goto err;
  644. }
  645. if (set_crypto_policy(local_policy, suite_val, p->local_key, key_len, stats.local_ssrc, 0) < 0) {
  646. goto err;
  647. }
  648. if (set_crypto_policy(remote_policy, suite_val, remote_key, key_len, 0, 1) < 0) {
  649. goto err;
  650. }
  651. /* Add the SRTP policies */
  652. if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy, 0)) {
  653. ast_log(LOG_WARNING, "Could not set SRTP policies\n");
  654. goto err;
  655. }
  656. ast_debug(1 , "SRTP policy activated\n");
  657. res = 0;
  658. err:
  659. if (local_policy) {
  660. policy_res.destroy(local_policy);
  661. }
  662. if (remote_policy) {
  663. policy_res.destroy(remote_policy);
  664. }
  665. return res;
  666. }
  667. static int res_sdp_crypto_parse_offer(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
  668. {
  669. char *str = NULL;
  670. char *tag = NULL;
  671. char *suite = NULL;
  672. char *key_params = NULL;
  673. char *key_param = NULL;
  674. char *session_params = NULL;
  675. char *key_salt = NULL; /* The actual master key and key salt */
  676. char *lifetime = NULL; /* Key lifetime (# of RTP packets) */
  677. char *mki = NULL; /* Master Key Index */
  678. int found = 0;
  679. int key_len_from_sdp;
  680. int key_len_expected;
  681. int tag_from_sdp;
  682. int suite_val = 0;
  683. unsigned char remote_key[SRTP_MAX_KEY_LEN];
  684. int taglen;
  685. double sdes_lifetime;
  686. struct ast_sdp_crypto *crypto;
  687. struct ast_sdp_srtp *tmp;
  688. str = ast_strdupa(attr);
  689. tag = strsep(&str, " ");
  690. suite = strsep(&str, " ");
  691. key_params = strsep(&str, " ");
  692. session_params = strsep(&str, " ");
  693. if (!tag || !suite) {
  694. ast_log(LOG_WARNING, "Unrecognized crypto attribute a=%s\n", attr);
  695. return -1;
  696. }
  697. /* RFC4568 9.1 - tag is 1-9 digits */
  698. if (sscanf(tag, "%30d", &tag_from_sdp) != 1 || tag_from_sdp < 0 || tag_from_sdp > 999999999) {
  699. ast_log(LOG_WARNING, "Unacceptable a=crypto tag: %s\n", tag);
  700. return -1;
  701. }
  702. if (!ast_strlen_zero(session_params)) {
  703. ast_log(LOG_WARNING, "Unsupported crypto parameters: %s\n", session_params);
  704. return -1;
  705. }
  706. /* On egress, Asterisk sent several crypto lines in the SIP/SDP offer
  707. The remote party might have choosen another line than the first */
  708. for (tmp = srtp; tmp && tmp->crypto && tmp->crypto->tag != tag_from_sdp;) {
  709. tmp = AST_LIST_NEXT(tmp, sdp_srtp_list);
  710. }
  711. if (tmp) { /* tag matched an already created crypto line */
  712. unsigned int flags = tmp->flags;
  713. /* Make that crypto line the head of the list, not by changing the
  714. list structure but by exchanging the content of the list members */
  715. crypto = tmp->crypto;
  716. tmp->crypto = srtp->crypto;
  717. tmp->flags = srtp->flags;
  718. srtp->crypto = crypto;
  719. srtp->flags = flags;
  720. } else {
  721. crypto = srtp->crypto;
  722. crypto->tag = tag_from_sdp;
  723. }
  724. ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
  725. ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
  726. ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  727. ast_clear_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  728. ast_clear_flag(srtp, AST_SRTP_CRYPTO_AES_192);
  729. ast_clear_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  730. ast_clear_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
  731. if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
  732. suite_val = AST_AES_CM_128_HMAC_SHA1_80;
  733. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  734. key_len_expected = 30;
  735. } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
  736. suite_val = AST_AES_CM_128_HMAC_SHA1_32;
  737. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  738. key_len_expected = 30;
  739. #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
  740. } else if (!strcmp(suite, "AES_192_CM_HMAC_SHA1_80")) {
  741. suite_val = AST_AES_CM_192_HMAC_SHA1_80;
  742. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  743. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
  744. key_len_expected = 38;
  745. } else if (!strcmp(suite, "AES_192_CM_HMAC_SHA1_32")) {
  746. suite_val = AST_AES_CM_192_HMAC_SHA1_32;
  747. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  748. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
  749. key_len_expected = 38;
  750. /* RFC used a different name while in draft, some still use that */
  751. } else if (!strcmp(suite, "AES_CM_192_HMAC_SHA1_80")) {
  752. suite_val = AST_AES_CM_192_HMAC_SHA1_80;
  753. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  754. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
  755. ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
  756. key_len_expected = 38;
  757. } else if (!strcmp(suite, "AES_CM_192_HMAC_SHA1_32")) {
  758. suite_val = AST_AES_CM_192_HMAC_SHA1_32;
  759. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  760. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_192);
  761. ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
  762. key_len_expected = 38;
  763. #endif
  764. #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
  765. } else if (!strcmp(suite, "AES_256_CM_HMAC_SHA1_80")) {
  766. suite_val = AST_AES_CM_256_HMAC_SHA1_80;
  767. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  768. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  769. key_len_expected = 46;
  770. } else if (!strcmp(suite, "AES_256_CM_HMAC_SHA1_32")) {
  771. suite_val = AST_AES_CM_256_HMAC_SHA1_32;
  772. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  773. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  774. key_len_expected = 46;
  775. /* RFC used a different name while in draft, some still use that */
  776. } else if (!strcmp(suite, "AES_CM_256_HMAC_SHA1_80")) {
  777. suite_val = AST_AES_CM_256_HMAC_SHA1_80;
  778. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  779. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  780. ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
  781. key_len_expected = 46;
  782. } else if (!strcmp(suite, "AES_CM_256_HMAC_SHA1_32")) {
  783. suite_val = AST_AES_CM_256_HMAC_SHA1_32;
  784. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  785. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  786. ast_set_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME);
  787. key_len_expected = 46;
  788. #endif
  789. #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
  790. } else if (!strcmp(suite, "AEAD_AES_128_GCM")) {
  791. suite_val = AST_AES_GCM_128;
  792. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
  793. key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
  794. /* RFC contained a (too) short auth tag for RTP media, some still use that */
  795. } else if (!strcmp(suite, "AEAD_AES_128_GCM_8")) {
  796. suite_val = AST_AES_GCM_128_8;
  797. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
  798. key_len_expected = AES_128_GCM_KEYSIZE_WSALT;
  799. #endif
  800. #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
  801. } else if (!strcmp(suite, "AEAD_AES_256_GCM")) {
  802. suite_val = AST_AES_GCM_256;
  803. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_16);
  804. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  805. key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
  806. /* RFC contained a (too) short auth tag for RTP media, some still use that */
  807. } else if (!strcmp(suite, "AEAD_AES_256_GCM_8")) {
  808. suite_val = AST_AES_GCM_256_8;
  809. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_8);
  810. ast_set_flag(srtp, AST_SRTP_CRYPTO_AES_256);
  811. key_len_expected = AES_256_GCM_KEYSIZE_WSALT;
  812. #endif
  813. } else {
  814. ast_verb(1, "Unsupported crypto suite: %s\n", suite);
  815. return -1;
  816. }
  817. while ((key_param = strsep(&key_params, ";"))) {
  818. unsigned int n_lifetime;
  819. char *method = NULL;
  820. char *info = NULL;
  821. method = strsep(&key_param, ":");
  822. info = strsep(&key_param, ";");
  823. sdes_lifetime = 0;
  824. if (strcmp(method, "inline")) {
  825. continue;
  826. }
  827. key_salt = strsep(&info, "|");
  828. /* The next parameter can be either lifetime or MKI */
  829. lifetime = strsep(&info, "|");
  830. if (!lifetime) {
  831. found = 1;
  832. break;
  833. }
  834. mki = strchr(lifetime, ':');
  835. if (mki) {
  836. mki = lifetime;
  837. lifetime = NULL;
  838. } else {
  839. mki = strsep(&info, "|");
  840. }
  841. if (mki && *mki != '1') {
  842. ast_log(LOG_NOTICE, "Crypto MKI handling is not supported: ignoring attribute %s\n", attr);
  843. continue;
  844. }
  845. if (lifetime) {
  846. if (!strncmp(lifetime, "2^", 2)) {
  847. char *lifetime_val = lifetime + 2;
  848. /* Exponential lifetime */
  849. if (sscanf(lifetime_val, "%30u", &n_lifetime) != 1) {
  850. ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
  851. continue;
  852. }
  853. if (n_lifetime > 48) {
  854. /* Yeah... that's a bit big. */
  855. ast_log(LOG_NOTICE, "Crypto lifetime exponent of '%u' is a bit large; using 48\n", n_lifetime);
  856. n_lifetime = 48;
  857. }
  858. sdes_lifetime = pow(2, n_lifetime);
  859. } else {
  860. /* Decimal lifetime */
  861. if (sscanf(lifetime, "%30u", &n_lifetime) != 1) {
  862. ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
  863. continue;
  864. }
  865. sdes_lifetime = n_lifetime;
  866. }
  867. /* Accept anything above ~5.8 hours. Less than ~5.8; reject. */
  868. if (sdes_lifetime < 1048576) {
  869. ast_log(LOG_NOTICE, "Rejecting crypto attribute '%s': lifetime '%f' too short\n", attr, sdes_lifetime);
  870. continue;
  871. }
  872. }
  873. ast_debug(2, "Crypto attribute '%s' accepted with lifetime '%f', MKI '%s'\n",
  874. attr, sdes_lifetime, mki ? mki : "-");
  875. found = 1;
  876. break;
  877. }
  878. if (!found) {
  879. ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable: '%s'\n", attr);
  880. return -1;
  881. }
  882. key_len_from_sdp = ast_base64decode(remote_key, key_salt, sizeof(remote_key));
  883. if (key_len_from_sdp != key_len_expected) {
  884. ast_log(LOG_WARNING, "SRTP descriptions key length is '%d', not '%d'\n",
  885. key_len_from_sdp, key_len_expected);
  886. return -1;
  887. }
  888. /* on default, the key is 30 (AES-128); throw that away (only) when the suite changed actually */
  889. /* ingress: optional, but saves one expensive call to get_random(.) */
  890. /* egress: required, because the local key was communicated before the remote key is processed */
  891. if (crypto->key_len != key_len_from_sdp) {
  892. if (!crypto_init_keys(crypto, key_len_from_sdp)) {
  893. return -1;
  894. }
  895. } else if (!memcmp(crypto->remote_key, remote_key, key_len_from_sdp)) {
  896. ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
  897. return 0;
  898. }
  899. if (key_len_from_sdp > sizeof(crypto->remote_key)) {
  900. ast_log(LOG_ERROR,
  901. "SRTP key buffer is %zu although it must be at least %d bytes\n",
  902. sizeof(crypto->remote_key), key_len_from_sdp);
  903. return -1;
  904. }
  905. memcpy(crypto->remote_key, remote_key, key_len_from_sdp);
  906. if (crypto_activate(crypto, suite_val, remote_key, key_len_from_sdp, rtp) < 0) {
  907. return -1;
  908. }
  909. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
  910. taglen = 32;
  911. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
  912. taglen = 16;
  913. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
  914. taglen = 8;
  915. } else {
  916. taglen = 80;
  917. }
  918. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_256)) {
  919. taglen |= 0x0200;
  920. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
  921. taglen |= 0x0100;
  922. }
  923. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME)) {
  924. taglen |= 0x0080;
  925. }
  926. /* Finally, rebuild the crypto line */
  927. if (res_sdp_crypto_build_offer(crypto, taglen)) {
  928. return -1;
  929. }
  930. ast_set_flag(srtp, AST_SRTP_CRYPTO_OFFER_OK);
  931. return 0;
  932. }
  933. static const char *res_sdp_srtp_get_attr(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
  934. {
  935. int taglen;
  936. if (!srtp) {
  937. return NULL;
  938. }
  939. /* Set encryption properties */
  940. if (!srtp->crypto) {
  941. if (AST_LIST_NEXT(srtp, sdp_srtp_list)) {
  942. srtp->crypto = res_sdp_crypto_alloc();
  943. ast_log(LOG_ERROR, "SRTP SDP list was not empty\n");
  944. } else {
  945. const int len = default_taglen_32 ? AST_SRTP_CRYPTO_TAG_32 : AST_SRTP_CRYPTO_TAG_80;
  946. const int attr[][3] = {
  947. /* This array creates the following list:
  948. * a=crypto:1 AES_CM_128_HMAC_SHA1_ ...
  949. * a=crypto:2 AEAD_AES_128_GCM ...
  950. * a=crypto:3 AES_256_CM_HMAC_SHA1_ ...
  951. * a=crypto:4 AEAD_AES_256_GCM ...
  952. * a=crypto:5 AES_192_CM_HMAC_SHA1_ ...
  953. * something like 'AEAD_AES_192_GCM' is not specified by the RFCs
  954. *
  955. * If you want to prefer another crypto suite or you want to
  956. * exclude a suite, change this array and recompile Asterisk.
  957. * This list cannot be changed from rtp.conf because you should
  958. * know what you are doing. Especially AES-192 and AES-GCM are
  959. * broken in many VoIP clients, see
  960. * https://github.com/cisco/libsrtp/pull/170
  961. * https://github.com/cisco/libsrtp/pull/184
  962. * Furthermore, AES-GCM uses a shorter crypto-suite string which
  963. * causes Nokia phones based on Symbian/S60 to reject the whole
  964. * INVITE with status 500, even if a matching suite was offered.
  965. * AES-256 might just waste your processor cycles, especially if
  966. * your TLS transport is not secured with equivalent grade, see
  967. * https://security.stackexchange.com/q/61361
  968. * Therefore, AES-128 was preferred here.
  969. *
  970. * If you want to enable one of those defines, please, go for
  971. * CFLAGS='-DENABLE_SRTP_AES_GCM' ./configure && sudo make install
  972. */
  973. { len, 0, 30 },
  974. #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM)
  975. { AST_SRTP_CRYPTO_TAG_16, 0, AES_128_GCM_KEYSIZE_WSALT },
  976. #endif
  977. #if defined(HAVE_SRTP_256) && defined(ENABLE_SRTP_AES_256)
  978. { len, AST_SRTP_CRYPTO_AES_256, 46 },
  979. #endif
  980. #if defined(HAVE_SRTP_GCM) && defined(ENABLE_SRTP_AES_GCM) && defined(ENABLE_SRTP_AES_256)
  981. { AST_SRTP_CRYPTO_TAG_16, AST_SRTP_CRYPTO_AES_256, AES_256_GCM_KEYSIZE_WSALT },
  982. #endif
  983. #if defined(HAVE_SRTP_192) && defined(ENABLE_SRTP_AES_192)
  984. { len, AST_SRTP_CRYPTO_AES_192, 38 },
  985. #endif
  986. };
  987. struct ast_sdp_srtp *tmp = srtp;
  988. int i;
  989. for (i = 0; i < ARRAY_LEN(attr); i++) {
  990. if (attr[i][0]) {
  991. ast_set_flag(tmp, attr[i][0]);
  992. }
  993. if (attr[i][1]) {
  994. ast_set_flag(tmp, attr[i][1]);
  995. }
  996. tmp->crypto = sdp_crypto_alloc(attr[i][2]); /* key_len */
  997. tmp->crypto->tag = (i + 1); /* tag starts at 1 */
  998. if (i < ARRAY_LEN(attr) - 1) {
  999. AST_LIST_NEXT(tmp, sdp_srtp_list) = ast_sdp_srtp_alloc();
  1000. tmp = AST_LIST_NEXT(tmp, sdp_srtp_list);
  1001. }
  1002. }
  1003. }
  1004. }
  1005. if (dtls_enabled) {
  1006. /* If DTLS-SRTP is enabled the key details will be pulled from TLS */
  1007. return NULL;
  1008. }
  1009. /* set the key length based on INVITE or settings */
  1010. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_80)) {
  1011. taglen = 80;
  1012. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
  1013. taglen = 32;
  1014. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_16)) {
  1015. taglen = 16;
  1016. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_8)) {
  1017. taglen = 8;
  1018. } else {
  1019. taglen = default_taglen_32 ? 32 : 80;
  1020. }
  1021. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_256)) {
  1022. taglen |= 0x0200;
  1023. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_AES_192)) {
  1024. taglen |= 0x0100;
  1025. }
  1026. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_OLD_NAME)) {
  1027. taglen |= 0x0080;
  1028. }
  1029. if (srtp->crypto && (res_sdp_crypto_build_offer(srtp->crypto, taglen) >= 0)) {
  1030. return srtp->crypto->a_crypto;
  1031. }
  1032. ast_log(LOG_WARNING, "No SRTP key management enabled\n");
  1033. return NULL;
  1034. }
  1035. static struct ast_sdp_crypto_api res_sdp_crypto_api = {
  1036. .dtor = res_sdp_crypto_dtor,
  1037. .alloc = res_sdp_crypto_alloc,
  1038. .build_offer = res_sdp_crypto_build_offer,
  1039. .parse_offer = res_sdp_crypto_parse_offer,
  1040. .get_attr = res_sdp_srtp_get_attr,
  1041. };
  1042. static void res_srtp_shutdown(void)
  1043. {
  1044. ast_sdp_crypto_unregister(&res_sdp_crypto_api);
  1045. ast_rtp_engine_unregister_srtp();
  1046. srtp_install_event_handler(NULL);
  1047. #ifdef HAVE_SRTP_SHUTDOWN
  1048. srtp_shutdown();
  1049. #endif
  1050. g_initialized = 0;
  1051. }
  1052. static int res_srtp_init(void)
  1053. {
  1054. if (g_initialized) {
  1055. return 0;
  1056. }
  1057. if (srtp_init() != err_status_ok) {
  1058. ast_log(AST_LOG_WARNING, "Failed to initialize libsrtp\n");
  1059. return -1;
  1060. }
  1061. srtp_install_event_handler(srtp_event_cb);
  1062. if (ast_rtp_engine_register_srtp(&srtp_res, &policy_res)) {
  1063. ast_log(AST_LOG_WARNING, "Failed to register SRTP with rtp engine\n");
  1064. res_srtp_shutdown();
  1065. return -1;
  1066. }
  1067. if (ast_sdp_crypto_register(&res_sdp_crypto_api)) {
  1068. ast_log(AST_LOG_WARNING, "Failed to register SDP SRTP crypto API\n");
  1069. res_srtp_shutdown();
  1070. return -1;
  1071. }
  1072. #ifdef HAVE_SRTP_GET_VERSION
  1073. ast_verb(2, "%s initialized\n", srtp_get_version_string());
  1074. #else
  1075. ast_verb(2, "libsrtp initialized\n");
  1076. #endif
  1077. g_initialized = 1;
  1078. return 0;
  1079. }
  1080. /*
  1081. * Exported functions
  1082. */
  1083. static int load_module(void)
  1084. {
  1085. return res_srtp_init();
  1086. }
  1087. static int unload_module(void)
  1088. {
  1089. res_srtp_shutdown();
  1090. return 0;
  1091. }
  1092. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Secure RTP (SRTP)",
  1093. .support_level = AST_MODULE_SUPPORT_CORE,
  1094. .load = load_module,
  1095. .unload = unload_module,
  1096. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  1097. );