sdp_srtp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "asterisk/options.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/sdp_srtp.h"
  35. #define SRTP_MASTER_LEN 30
  36. #define SRTP_MASTERKEY_LEN 16
  37. #define SRTP_MASTERSALT_LEN ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))
  38. #define SRTP_MASTER_LEN64 (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)
  39. extern struct ast_srtp_res *res_srtp;
  40. extern struct ast_srtp_policy_res *res_srtp_policy;
  41. struct ast_sdp_srtp *ast_sdp_srtp_alloc(void)
  42. {
  43. if (!ast_rtp_engine_srtp_is_registered()) {
  44. ast_debug(1, "No SRTP module loaded, can't setup SRTP session.\n");
  45. return NULL;
  46. }
  47. return ast_calloc(1, sizeof(struct ast_sdp_srtp));
  48. }
  49. void ast_sdp_srtp_destroy(struct ast_sdp_srtp *srtp)
  50. {
  51. if (srtp->crypto) {
  52. ast_sdp_crypto_destroy(srtp->crypto);
  53. }
  54. srtp->crypto = NULL;
  55. ast_free(srtp);
  56. }
  57. struct ast_sdp_crypto {
  58. char *a_crypto;
  59. unsigned char local_key[SRTP_MASTER_LEN];
  60. char *tag;
  61. char local_key64[SRTP_MASTER_LEN64];
  62. unsigned char remote_key[SRTP_MASTER_LEN];
  63. };
  64. static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound);
  65. void ast_sdp_crypto_destroy(struct ast_sdp_crypto *crypto)
  66. {
  67. ast_free(crypto->a_crypto);
  68. crypto->a_crypto = NULL;
  69. ast_free(crypto->tag);
  70. crypto->tag = NULL;
  71. ast_free(crypto);
  72. }
  73. struct ast_sdp_crypto *ast_sdp_crypto_alloc(void)
  74. {
  75. struct ast_sdp_crypto *p;
  76. int key_len;
  77. unsigned char remote_key[SRTP_MASTER_LEN];
  78. if (!ast_rtp_engine_srtp_is_registered()) {
  79. return NULL;
  80. }
  81. if (!(p = ast_calloc(1, sizeof(*p)))) {
  82. return NULL;
  83. }
  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_free(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_free(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)) {
  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;
  172. char *lifetime = NULL;
  173. int found = 0;
  174. int key_len = 0;
  175. int suite_val = 0;
  176. unsigned char remote_key[SRTP_MASTER_LEN];
  177. int taglen = 0;
  178. struct ast_sdp_crypto *crypto = srtp->crypto;
  179. if (!ast_rtp_engine_srtp_is_registered()) {
  180. return -1;
  181. }
  182. str = ast_strdupa(attr);
  183. tag = strsep(&str, " ");
  184. suite = strsep(&str, " ");
  185. key_params = strsep(&str, " ");
  186. session_params = strsep(&str, " ");
  187. if (!tag || !suite) {
  188. ast_log(LOG_WARNING, "Unrecognized a=%s", attr);
  189. return -1;
  190. }
  191. if (!ast_strlen_zero(session_params)) {
  192. ast_log(LOG_WARNING, "Unsupported crypto parameters: %s", session_params);
  193. return -1;
  194. }
  195. if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
  196. suite_val = AST_AES_CM_128_HMAC_SHA1_80;
  197. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
  198. taglen = 80;
  199. } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
  200. suite_val = AST_AES_CM_128_HMAC_SHA1_32;
  201. ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
  202. taglen = 32;
  203. } else {
  204. ast_log(LOG_WARNING, "Unsupported crypto suite: %s\n", suite);
  205. return -1;
  206. }
  207. while ((key_param = strsep(&key_params, ";"))) {
  208. char *method = NULL;
  209. char *info = NULL;
  210. method = strsep(&key_param, ":");
  211. info = strsep(&key_param, ";");
  212. if (!strcmp(method, "inline")) {
  213. key_salt = strsep(&info, "|");
  214. lifetime = strsep(&info, "|");
  215. if (lifetime) {
  216. ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n", attr);
  217. continue;
  218. }
  219. found = 1;
  220. break;
  221. }
  222. }
  223. if (!found) {
  224. ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable\n");
  225. return -1;
  226. }
  227. if ((key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key))) != SRTP_MASTER_LEN) {
  228. ast_log(LOG_WARNING, "SRTP descriptions key %d != %d\n", key_len, SRTP_MASTER_LEN);
  229. return -1;
  230. }
  231. if (!memcmp(crypto->remote_key, remote_key, sizeof(crypto->remote_key))) {
  232. ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
  233. ast_set_flag(srtp, AST_SRTP_CRYPTO_OFFER_OK);
  234. return 0;
  235. }
  236. memcpy(crypto->remote_key, remote_key, sizeof(crypto->remote_key));
  237. if (crypto_activate(crypto, suite_val, remote_key, rtp) < 0) {
  238. return -1;
  239. }
  240. if (!crypto->tag) {
  241. ast_debug(1, "Accepting crypto tag %s\n", tag);
  242. crypto->tag = ast_strdup(tag);
  243. if (!crypto->tag) {
  244. ast_log(LOG_ERROR, "Could not allocate memory for tag\n");
  245. return -1;
  246. }
  247. }
  248. /* Finally, rebuild the crypto line */
  249. if (ast_sdp_crypto_build_offer(crypto, taglen)) {
  250. return -1;
  251. }
  252. ast_set_flag(srtp, AST_SRTP_CRYPTO_OFFER_OK);
  253. return 0;
  254. }
  255. int ast_sdp_crypto_build_offer(struct ast_sdp_crypto *p, int taglen)
  256. {
  257. /* Rebuild the crypto line */
  258. if (p->a_crypto) {
  259. ast_free(p->a_crypto);
  260. }
  261. if (ast_asprintf(&p->a_crypto, "%s AES_CM_128_HMAC_SHA1_%i inline:%s",
  262. p->tag ? p->tag : "1", taglen, p->local_key64) == -1) {
  263. ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
  264. return -1;
  265. }
  266. ast_debug(1, "Crypto line: a=crypto:%s\n", p->a_crypto);
  267. return 0;
  268. }
  269. const char *ast_sdp_srtp_get_attrib(struct ast_sdp_srtp *srtp, int dtls_enabled, int default_taglen_32)
  270. {
  271. int taglen = default_taglen_32 ? 32 : 80;
  272. if (!srtp) {
  273. return NULL;
  274. }
  275. /* Set encryption properties */
  276. if (!srtp->crypto) {
  277. srtp->crypto = ast_sdp_crypto_alloc();
  278. }
  279. if (dtls_enabled) {
  280. /* If DTLS-SRTP is enabled the key details will be pulled from TLS */
  281. return NULL;
  282. }
  283. /* set the key length based on INVITE or settings */
  284. if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_80)) {
  285. taglen = 80;
  286. } else if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
  287. taglen = 32;
  288. }
  289. if (srtp->crypto && (ast_sdp_crypto_build_offer(srtp->crypto, taglen) >= 0)) {
  290. return srtp->crypto->a_crypto;
  291. }
  292. ast_log(LOG_WARNING, "No SRTP key management enabled\n");
  293. return NULL;
  294. }
  295. char *ast_sdp_get_rtp_profile(unsigned int sdes_active, struct ast_rtp_instance *instance, unsigned int using_avpf)
  296. {
  297. struct ast_rtp_engine_dtls *dtls;
  298. if ((dtls = ast_rtp_instance_get_dtls(instance)) && dtls->active(instance)) {
  299. return using_avpf ? "UDP/TLS/RTP/SAVPF" : "UDP/TLS/RTP/SAVP";
  300. } else {
  301. if (using_avpf) {
  302. return sdes_active ? "RTP/SAVPF" : "RTP/AVPF";
  303. } else {
  304. return sdes_active ? "RTP/SAVP" : "RTP/AVP";
  305. }
  306. }
  307. }