sdp_crypto.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 sdp_crypto.c
  19. *
  20. * \brief SDP Security descriptions
  21. *
  22. * Specified in RFC 4568
  23. *
  24. * \author Mikael Magnusson <mikma@users.sourceforge.net>
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/options.h"
  32. #include "asterisk/utils.h"
  33. #include "include/sdp_crypto.h"
  34. #include "include/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 sdp_crypto {
  42. char *a_crypto;
  43. unsigned char local_key[SRTP_MASTER_LEN];
  44. char local_key64[SRTP_MASTER_LEN64];
  45. unsigned char remote_key[SRTP_MASTER_LEN];
  46. };
  47. static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound);
  48. static struct sdp_crypto *sdp_crypto_alloc(void)
  49. {
  50. return ast_calloc(1, sizeof(struct sdp_crypto));
  51. }
  52. void sdp_crypto_destroy(struct sdp_crypto *crypto)
  53. {
  54. ast_free(crypto->a_crypto);
  55. crypto->a_crypto = NULL;
  56. ast_free(crypto);
  57. }
  58. struct sdp_crypto *sdp_crypto_setup(void)
  59. {
  60. struct sdp_crypto *p;
  61. int key_len;
  62. unsigned char remote_key[SRTP_MASTER_LEN];
  63. if (!ast_rtp_engine_srtp_is_registered()) {
  64. return NULL;
  65. }
  66. if (!(p = sdp_crypto_alloc())) {
  67. return NULL;
  68. }
  69. if (res_srtp->get_random(p->local_key, sizeof(p->local_key)) < 0) {
  70. sdp_crypto_destroy(p);
  71. return NULL;
  72. }
  73. ast_base64encode(p->local_key64, p->local_key, SRTP_MASTER_LEN, sizeof(p->local_key64));
  74. key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
  75. if (key_len != SRTP_MASTER_LEN) {
  76. ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN);
  77. ast_free(p);
  78. return NULL;
  79. }
  80. if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) {
  81. ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
  82. ast_free(p);
  83. return NULL;
  84. }
  85. ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
  86. return p;
  87. }
  88. static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
  89. {
  90. const unsigned char *master_salt = NULL;
  91. if (!ast_rtp_engine_srtp_is_registered()) {
  92. return -1;
  93. }
  94. master_salt = master_key + SRTP_MASTERKEY_LEN;
  95. if (res_srtp_policy->set_master_key(policy, master_key, SRTP_MASTERKEY_LEN, master_salt, SRTP_MASTERSALT_LEN) < 0) {
  96. return -1;
  97. }
  98. if (res_srtp_policy->set_suite(policy, suite_val)) {
  99. ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
  100. return -1;
  101. }
  102. res_srtp_policy->set_ssrc(policy, ssrc, inbound);
  103. return 0;
  104. }
  105. static int sdp_crypto_activate(struct sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
  106. {
  107. struct ast_srtp_policy *local_policy = NULL;
  108. struct ast_srtp_policy *remote_policy = NULL;
  109. struct ast_rtp_instance_stats stats = {0,};
  110. int res = -1;
  111. if (!ast_rtp_engine_srtp_is_registered()) {
  112. return -1;
  113. }
  114. if (!p) {
  115. return -1;
  116. }
  117. if (!(local_policy = res_srtp_policy->alloc())) {
  118. return -1;
  119. }
  120. if (!(remote_policy = res_srtp_policy->alloc())) {
  121. goto err;
  122. }
  123. if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
  124. goto err;
  125. }
  126. if (set_crypto_policy(local_policy, suite_val, p->local_key, stats.local_ssrc, 0) < 0) {
  127. goto err;
  128. }
  129. if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0) {
  130. goto err;
  131. }
  132. /* Add the SRTP policies */
  133. if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy)) {
  134. ast_log(LOG_WARNING, "Could not set SRTP policies\n");
  135. goto err;
  136. }
  137. ast_debug(1 , "SRTP policy activated\n");
  138. res = 0;
  139. err:
  140. if (local_policy) {
  141. res_srtp_policy->destroy(local_policy);
  142. }
  143. if (remote_policy) {
  144. res_srtp_policy->destroy(remote_policy);
  145. }
  146. return res;
  147. }
  148. int sdp_crypto_process(struct sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp, struct sip_srtp *srtp)
  149. {
  150. char *str = NULL;
  151. char *tag = NULL;
  152. char *suite = NULL;
  153. char *key_params = NULL;
  154. char *key_param = NULL;
  155. char *session_params = NULL;
  156. char *key_salt = NULL;
  157. char *lifetime = NULL;
  158. int found = 0;
  159. int attr_len = strlen(attr);
  160. int key_len = 0;
  161. int suite_val = 0;
  162. unsigned char remote_key[SRTP_MASTER_LEN];
  163. if (!ast_rtp_engine_srtp_is_registered()) {
  164. return -1;
  165. }
  166. str = ast_strdupa(attr);
  167. strsep(&str, ":");
  168. tag = strsep(&str, " ");
  169. suite = strsep(&str, " ");
  170. key_params = strsep(&str, " ");
  171. session_params = strsep(&str, " ");
  172. if (!tag || !suite) {
  173. ast_log(LOG_WARNING, "Unrecognized a=%s", attr);
  174. return -1;
  175. }
  176. if (session_params) {
  177. ast_log(LOG_WARNING, "Unsupported crypto parameters: %s", session_params);
  178. return -1;
  179. }
  180. if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
  181. suite_val = AST_AES_CM_128_HMAC_SHA1_80;
  182. ast_set_flag(srtp, SRTP_CRYPTO_TAG_80);
  183. } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
  184. suite_val = AST_AES_CM_128_HMAC_SHA1_32;
  185. ast_set_flag(srtp, SRTP_CRYPTO_TAG_32);
  186. } else {
  187. ast_log(LOG_WARNING, "Unsupported crypto suite: %s\n", suite);
  188. return -1;
  189. }
  190. while ((key_param = strsep(&key_params, ";"))) {
  191. char *method = NULL;
  192. char *info = NULL;
  193. method = strsep(&key_param, ":");
  194. info = strsep(&key_param, ";");
  195. if (!strcmp(method, "inline")) {
  196. key_salt = strsep(&info, "|");
  197. lifetime = strsep(&info, "|");
  198. if (lifetime) {
  199. ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n", attr);
  200. continue;
  201. }
  202. found = 1;
  203. break;
  204. }
  205. }
  206. if (!found) {
  207. ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable\n");
  208. return -1;
  209. }
  210. if ((key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key))) != SRTP_MASTER_LEN) {
  211. ast_log(LOG_WARNING, "SRTP descriptions key %d != %d\n", key_len, SRTP_MASTER_LEN);
  212. return -1;
  213. }
  214. if (!memcmp(p->remote_key, remote_key, sizeof(p->remote_key))) {
  215. ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
  216. return 0;
  217. }
  218. memcpy(p->remote_key, remote_key, sizeof(p->remote_key));
  219. if (sdp_crypto_activate(p, suite_val, remote_key, rtp) < 0) {
  220. return -1;
  221. }
  222. if (!p->a_crypto) {
  223. if (!(p->a_crypto = ast_calloc(1, attr_len + 11))) {
  224. ast_log(LOG_ERROR, "Could not allocate memory for a_crypto\n");
  225. return -1;
  226. }
  227. snprintf(p->a_crypto, attr_len + 10, "a=crypto:%s %s inline:%s\r\n", tag, suite, p->local_key64);
  228. }
  229. return 0;
  230. }
  231. int sdp_crypto_offer(struct sdp_crypto *p, int taglen)
  232. {
  233. char crypto_buf[128];
  234. if (p->a_crypto) {
  235. ast_free(p->a_crypto);
  236. }
  237. if (snprintf(crypto_buf, sizeof(crypto_buf), "a=crypto:1 AES_CM_128_HMAC_SHA1_%i inline:%s\r\n",
  238. taglen, p->local_key64) < 1) {
  239. return -1;
  240. }
  241. if (!(p->a_crypto = ast_strdup(crypto_buf))) {
  242. return -1;
  243. }
  244. return 0;
  245. }
  246. const char *sdp_crypto_attrib(struct sdp_crypto *p)
  247. {
  248. return p->a_crypto;
  249. }