sdp_srtp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006 - 2007, 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. /*! \file ast_sdp_crypto.c
  19. *
  20. * \brief SRTP and SDP Security descriptions
  21. *
  22. * Specified in RFC 3711
  23. * Specified in RFC 4568
  24. *
  25. * \author Mikael Magnusson <mikma@users.sourceforge.net>
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <math.h>
  33. #include "asterisk/options.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/sdp_srtp.h"
  36. #define SRTP_MASTER_LEN 30
  37. #define SRTP_MASTERKEY_LEN 16
  38. #define SRTP_MASTERSALT_LEN ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))
  39. #define SRTP_MASTER_LEN64 (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)
  40. extern struct ast_srtp_res *res_srtp;
  41. extern struct ast_srtp_policy_res *res_srtp_policy;
  42. struct ast_sdp_srtp *ast_sdp_srtp_alloc(void)
  43. {
  44. if (!ast_rtp_engine_srtp_is_registered()) {
  45. ast_debug(1, "No SRTP module loaded, can't setup SRTP session.\n");
  46. return NULL;
  47. }
  48. return ast_calloc(1, sizeof(struct ast_sdp_srtp));
  49. }
  50. void ast_sdp_srtp_destroy(struct ast_sdp_srtp *srtp)
  51. {
  52. if (srtp->crypto) {
  53. ast_sdp_crypto_destroy(srtp->crypto);
  54. }
  55. srtp->crypto = NULL;
  56. ast_free(srtp);
  57. }
  58. struct ast_sdp_crypto {
  59. char *a_crypto;
  60. unsigned char local_key[SRTP_MASTER_LEN];
  61. int tag;
  62. char local_key64[SRTP_MASTER_LEN64];
  63. unsigned char remote_key[SRTP_MASTER_LEN];
  64. };
  65. static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound);
  66. void ast_sdp_crypto_destroy(struct ast_sdp_crypto *crypto)
  67. {
  68. ast_free(crypto->a_crypto);
  69. crypto->a_crypto = NULL;
  70. ast_free(crypto);
  71. }
  72. struct ast_sdp_crypto *ast_sdp_crypto_alloc(void)
  73. {
  74. struct ast_sdp_crypto *p;
  75. int key_len;
  76. unsigned char remote_key[SRTP_MASTER_LEN];
  77. if (!ast_rtp_engine_srtp_is_registered()) {
  78. return NULL;
  79. }
  80. if (!(p = ast_calloc(1, sizeof(*p)))) {
  81. return NULL;
  82. }
  83. p->tag = 1;
  84. if (res_srtp->get_random(p->local_key, sizeof(p->local_key)) < 0) {
  85. ast_sdp_crypto_destroy(p);
  86. return NULL;
  87. }
  88. ast_base64encode(p->local_key64, p->local_key, SRTP_MASTER_LEN, sizeof(p->local_key64));
  89. key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
  90. if (key_len != SRTP_MASTER_LEN) {
  91. ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN);
  92. ast_sdp_crypto_destroy(p);
  93. return NULL;
  94. }
  95. if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) {
  96. ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
  97. ast_sdp_crypto_destroy(p);
  98. return NULL;
  99. }
  100. ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
  101. return p;
  102. }
  103. static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
  104. {
  105. const unsigned char *master_salt = NULL;
  106. if (!ast_rtp_engine_srtp_is_registered()) {
  107. return -1;
  108. }
  109. master_salt = master_key + SRTP_MASTERKEY_LEN;
  110. if (res_srtp_policy->set_master_key(policy, master_key, SRTP_MASTERKEY_LEN, master_salt, SRTP_MASTERSALT_LEN) < 0) {
  111. return -1;
  112. }
  113. if (res_srtp_policy->set_suite(policy, suite_val)) {
  114. ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
  115. return -1;
  116. }
  117. res_srtp_policy->set_ssrc(policy, ssrc, inbound);
  118. return 0;
  119. }
  120. static int crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
  121. {
  122. struct ast_srtp_policy *local_policy = NULL;
  123. struct ast_srtp_policy *remote_policy = NULL;
  124. struct ast_rtp_instance_stats stats = {0,};
  125. int res = -1;
  126. if (!ast_rtp_engine_srtp_is_registered()) {
  127. return -1;
  128. }
  129. if (!p) {
  130. return -1;
  131. }
  132. if (!(local_policy = res_srtp_policy->alloc())) {
  133. return -1;
  134. }
  135. if (!(remote_policy = res_srtp_policy->alloc())) {
  136. goto err;
  137. }
  138. if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
  139. goto err;
  140. }
  141. if (set_crypto_policy(local_policy, suite_val, p->local_key, stats.local_ssrc, 0) < 0) {
  142. goto err;
  143. }
  144. if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0) {
  145. goto err;
  146. }
  147. /* Add the SRTP policies */
  148. if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy, 0)) {
  149. ast_log(LOG_WARNING, "Could not set SRTP policies\n");
  150. goto err;
  151. }
  152. ast_debug(1 , "SRTP policy activated\n");
  153. res = 0;
  154. err:
  155. if (local_policy) {
  156. res_srtp_policy->destroy(local_policy);
  157. }
  158. if (remote_policy) {
  159. res_srtp_policy->destroy(remote_policy);
  160. }
  161. return res;
  162. }
  163. int ast_sdp_crypto_process(struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp, const char *attr)
  164. {
  165. char *str = NULL;
  166. char *tag = NULL;
  167. char *suite = NULL;
  168. char *key_params = NULL;
  169. char *key_param = NULL;
  170. char *session_params = NULL;
  171. char *key_salt = NULL; /* The actual master key and key salt */
  172. char *lifetime = NULL; /* Key lifetime (# of RTP packets) */
  173. char *mki = NULL; /* Master Key Index */
  174. int found = 0;
  175. int key_len = 0;
  176. int suite_val = 0;
  177. unsigned char remote_key[SRTP_MASTER_LEN];
  178. int taglen = 0;
  179. double sdes_lifetime;
  180. struct ast_sdp_crypto *crypto = srtp->crypto;
  181. if (!ast_rtp_engine_srtp_is_registered()) {
  182. return -1;
  183. }
  184. str = ast_strdupa(attr);
  185. tag = strsep(&str, " ");
  186. suite = strsep(&str, " ");
  187. key_params = strsep(&str, " ");
  188. session_params = strsep(&str, " ");
  189. if (!tag || !suite) {
  190. ast_log(LOG_WARNING, "Unrecognized crypto attribute a=%s\n", attr);
  191. return -1;
  192. }
  193. /* RFC4568 9.1 - tag is 1-9 digits */
  194. if (sscanf(tag, "%30d", &crypto->tag) != 1 || crypto->tag < 0 || crypto->tag > 999999999) {
  195. ast_log(LOG_WARNING, "Unacceptable a=crypto tag: %s\n", tag);
  196. return -1;
  197. }
  198. if (!ast_strlen_zero(session_params)) {
  199. ast_log(LOG_WARNING, "Unsupported crypto parameters: %s\n", session_params);
  200. return -1;
  201. }
  202. if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
  203. suite_val = AST_AES_CM_128_HMAC_SHA1_80;
  204. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  205. taglen = 80;
  206. } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
  207. suite_val = AST_AES_CM_128_HMAC_SHA1_32;
  208. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  209. taglen = 32;
  210. } else {
  211. ast_log(LOG_WARNING, "Unsupported crypto suite: %s\n", suite);
  212. return -1;
  213. }
  214. while ((key_param = strsep(&key_params, ";"))) {
  215. unsigned int n_lifetime;
  216. char *method = NULL;
  217. char *info = NULL;
  218. method = strsep(&key_param, ":");
  219. info = strsep(&key_param, ";");
  220. sdes_lifetime = 0;
  221. if (strcmp(method, "inline")) {
  222. continue;
  223. }
  224. key_salt = strsep(&info, "|");
  225. /* The next parameter can be either lifetime or MKI */
  226. lifetime = strsep(&info, "|");
  227. if (!lifetime) {
  228. found = 1;
  229. break;
  230. }
  231. mki = strchr(lifetime, ':');
  232. if (mki) {
  233. mki = lifetime;
  234. lifetime = NULL;
  235. } else {
  236. mki = strsep(&info, "|");
  237. }
  238. if (mki && *mki != '1') {
  239. ast_log(LOG_NOTICE, "Crypto MKI handling is not supported: ignoring attribute %s\n", attr);
  240. continue;
  241. }
  242. if (lifetime) {
  243. if (!strncmp(lifetime, "2^", 2)) {
  244. char *lifetime_val = lifetime + 2;
  245. /* Exponential lifetime */
  246. if (sscanf(lifetime_val, "%30u", &n_lifetime) != 1) {
  247. ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
  248. continue;
  249. }
  250. if (n_lifetime > 48) {
  251. /* Yeah... that's a bit big. */
  252. ast_log(LOG_NOTICE, "Crypto lifetime exponent of '%u' is a bit large; using 48\n", n_lifetime);
  253. n_lifetime = 48;
  254. }
  255. sdes_lifetime = pow(2, n_lifetime);
  256. } else {
  257. /* Decimal lifetime */
  258. if (sscanf(lifetime, "%30u", &n_lifetime) != 1) {
  259. ast_log(LOG_NOTICE, "Failed to parse lifetime value in crypto attribute: %s\n", attr);
  260. continue;
  261. }
  262. sdes_lifetime = n_lifetime;
  263. }
  264. /* Accept anything above ~5.8 hours. Less than ~5.8; reject. */
  265. if (sdes_lifetime < 1048576) {
  266. ast_log(LOG_NOTICE, "Rejecting crypto attribute '%s': lifetime '%f' too short\n", attr, sdes_lifetime);
  267. continue;
  268. }
  269. }
  270. ast_debug(2, "Crypto attribute '%s' accepted with lifetime '%f', MKI '%s'\n",
  271. attr, sdes_lifetime, mki ? mki : "-");
  272. found = 1;
  273. break;
  274. }
  275. if (!found) {
  276. ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable: '%s'\n", attr);
  277. return -1;
  278. }
  279. key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key));
  280. if (key_len != SRTP_MASTER_LEN) {
  281. ast_log(LOG_WARNING, "SRTP descriptions key length '%d' != master length '%d'\n",
  282. key_len, SRTP_MASTER_LEN);
  283. return -1;
  284. }
  285. if (!memcmp(crypto->remote_key, remote_key, sizeof(crypto->remote_key))) {
  286. ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
  287. return 0;
  288. }
  289. memcpy(crypto->remote_key, remote_key, sizeof(crypto->remote_key));
  290. if (crypto_activate(crypto, suite_val, remote_key, rtp) < 0) {
  291. return -1;
  292. }
  293. /* Finally, rebuild the crypto line */
  294. if (ast_sdp_crypto_build_offer(crypto, taglen)) {
  295. return -1;
  296. }
  297. ast_set_flag(srtp, AST_SRTP_CRYPTO_OFFER_OK);
  298. return 0;
  299. }
  300. int ast_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
  301. {
  302. /* Rebuild the crypto line */
  303. if (p->a_crypto) {
  304. ast_free(p->a_crypto);
  305. }
  306. if (ast_asprintf(&p->a_crypto, "%d AES_CM_128_HMAC_SHA1_%i inline:%s",
  307. p->tag, taglen, p->local_key64) == -1) {
  308. ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
  309. return -1;
  310. }
  311. ast_debug(1, "Crypto line: a=crypto:%s\n", p->a_crypto);
  312. return 0;
  313. }
  314. const char *ast_sdp_srtp_get_attrib(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
  315. {
  316. int taglen = default_taglen_32 ? 32 : 80;
  317. if (!srtp) {
  318. return NULL;
  319. }
  320. /* Set encryption properties */
  321. if (!srtp->crypto) {
  322. srtp->crypto = ast_sdp_crypto_alloc();
  323. }
  324. if (dtls_enabled) {
  325. /* If DTLS-SRTP is enabled the key details will be pulled from TLS */
  326. return NULL;
  327. }
  328. /* set the key length based on INVITE or settings */
  329. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_80)) {
  330. taglen = 80;
  331. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
  332. taglen = 32;
  333. }
  334. if (srtp->crypto && (ast_sdp_crypto_build_offer(srtp->crypto, taglen) >= 0)) {
  335. return srtp->crypto->a_crypto;
  336. }
  337. ast_log(LOG_WARNING, "No SRTP key management enabled\n");
  338. return NULL;
  339. }
  340. char *ast_sdp_get_rtp_profile(unsigned int sdes_active, struct ast_rtp_instance *instance, unsigned int using_avpf,
  341. unsigned int force_avp)
  342. {
  343. struct ast_rtp_engine_dtls *dtls;
  344. if ((dtls = ast_rtp_instance_get_dtls(instance)) && dtls->active(instance)) {
  345. if (force_avp) {
  346. return using_avpf ? "RTP/SAVPF" : "RTP/SAVP";
  347. } else {
  348. return using_avpf ? "UDP/TLS/RTP/SAVPF" : "UDP/TLS/RTP/SAVP";
  349. }
  350. } else {
  351. if (using_avpf) {
  352. return sdes_active ? "RTP/SAVPF" : "RTP/AVPF";
  353. } else {
  354. return sdes_active ? "RTP/SAVP" : "RTP/AVP";
  355. }
  356. }
  357. }