You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
12KB

  1. /*
  2. * SRTP encryption/decryption
  3. * Copyright (c) 2012 Martin Storsjo
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/base64.h"
  22. #include "libavutil/aes.h"
  23. #include "libavutil/hmac.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/log.h"
  26. #include "rtp.h"
  27. #include "srtp.h"
  28. void ff_srtp_free(struct SRTPContext *s)
  29. {
  30. if (!s)
  31. return;
  32. av_freep(&s->aes);
  33. if (s->hmac)
  34. av_hmac_free(s->hmac);
  35. s->hmac = NULL;
  36. }
  37. static void encrypt_counter(struct AVAES *aes, uint8_t *iv, uint8_t *outbuf,
  38. int outlen)
  39. {
  40. int i, j, outpos;
  41. for (i = 0, outpos = 0; outpos < outlen; i++) {
  42. uint8_t keystream[16];
  43. AV_WB16(&iv[14], i);
  44. av_aes_crypt(aes, keystream, iv, 1, NULL, 0);
  45. for (j = 0; j < 16 && outpos < outlen; j++, outpos++)
  46. outbuf[outpos] ^= keystream[j];
  47. }
  48. }
  49. static void derive_key(struct AVAES *aes, const uint8_t *salt, int label,
  50. uint8_t *out, int outlen)
  51. {
  52. uint8_t input[16] = { 0 };
  53. memcpy(input, salt, 14);
  54. // Key derivation rate assumed to be zero
  55. input[14 - 7] ^= label;
  56. memset(out, 0, outlen);
  57. encrypt_counter(aes, input, out, outlen);
  58. }
  59. int ff_srtp_set_crypto(struct SRTPContext *s, const char *suite,
  60. const char *params)
  61. {
  62. uint8_t buf[30];
  63. ff_srtp_free(s);
  64. // RFC 4568
  65. if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
  66. s->hmac_size = 10;
  67. } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
  68. s->hmac_size = 4;
  69. } else {
  70. av_log(NULL, AV_LOG_WARNING, "SRTP Crypto suite %s not supported\n",
  71. suite);
  72. return AVERROR(EINVAL);
  73. }
  74. if (av_base64_decode(buf, params, sizeof(buf)) != sizeof(buf)) {
  75. av_log(NULL, AV_LOG_WARNING, "Incorrect amount of SRTP params\n");
  76. return AVERROR(EINVAL);
  77. }
  78. // MKI and lifetime not handled yet
  79. s->aes = av_aes_alloc();
  80. s->hmac = av_hmac_alloc(AV_HMAC_SHA1);
  81. if (!s->aes || !s->hmac)
  82. return AVERROR(ENOMEM);
  83. memcpy(s->master_key, buf, 16);
  84. memcpy(s->master_salt, buf + 16, 14);
  85. // RFC 3711
  86. av_aes_init(s->aes, s->master_key, 128, 0);
  87. derive_key(s->aes, s->master_salt, 0x00, s->rtp_key, sizeof(s->rtp_key));
  88. derive_key(s->aes, s->master_salt, 0x02, s->rtp_salt, sizeof(s->rtp_salt));
  89. derive_key(s->aes, s->master_salt, 0x01, s->rtp_auth, sizeof(s->rtp_auth));
  90. derive_key(s->aes, s->master_salt, 0x03, s->rtcp_key, sizeof(s->rtcp_key));
  91. derive_key(s->aes, s->master_salt, 0x05, s->rtcp_salt, sizeof(s->rtcp_salt));
  92. derive_key(s->aes, s->master_salt, 0x04, s->rtcp_auth, sizeof(s->rtcp_auth));
  93. return 0;
  94. }
  95. static void create_iv(uint8_t *iv, const uint8_t *salt, uint64_t index,
  96. uint32_t ssrc)
  97. {
  98. uint8_t indexbuf[8];
  99. int i;
  100. memset(iv, 0, 16);
  101. AV_WB32(&iv[4], ssrc);
  102. AV_WB64(indexbuf, index);
  103. for (i = 0; i < 8; i++) // index << 16
  104. iv[6 + i] ^= indexbuf[i];
  105. for (i = 0; i < 14; i++)
  106. iv[i] ^= salt[i];
  107. }
  108. int ff_srtp_decrypt(struct SRTPContext *s, uint8_t *buf, int *lenptr)
  109. {
  110. uint8_t iv[16] = { 0 }, hmac[20];
  111. int len = *lenptr;
  112. int ext, av_uninit(seq_largest);
  113. uint32_t ssrc, av_uninit(roc);
  114. uint64_t index;
  115. int rtcp;
  116. // TODO: Missing replay protection
  117. if (len < s->hmac_size)
  118. return AVERROR_INVALIDDATA;
  119. rtcp = RTP_PT_IS_RTCP(buf[1]);
  120. // Authentication HMAC
  121. av_hmac_init(s->hmac, rtcp ? s->rtcp_auth : s->rtp_auth, sizeof(s->rtp_auth));
  122. // If MKI is used, this should exclude the MKI as well
  123. av_hmac_update(s->hmac, buf, len - s->hmac_size);
  124. if (!rtcp) {
  125. int seq = AV_RB16(buf + 2);
  126. uint32_t v;
  127. uint8_t rocbuf[4];
  128. // RFC 3711 section 3.3.1, appendix A
  129. seq_largest = s->seq_initialized ? s->seq_largest : seq;
  130. v = roc = s->roc;
  131. if (seq_largest < 32768) {
  132. if (seq - seq_largest > 32768)
  133. v = roc - 1;
  134. } else {
  135. if (seq_largest - 32768 > seq)
  136. v = roc + 1;
  137. }
  138. if (v == roc) {
  139. seq_largest = FFMAX(seq_largest, seq);
  140. } else if (v == roc + 1) {
  141. seq_largest = seq;
  142. roc = v;
  143. }
  144. index = seq + (((uint64_t)v) << 16);
  145. AV_WB32(rocbuf, roc);
  146. av_hmac_update(s->hmac, rocbuf, 4);
  147. }
  148. av_hmac_final(s->hmac, hmac, sizeof(hmac));
  149. if (memcmp(hmac, buf + len - s->hmac_size, s->hmac_size)) {
  150. av_log(NULL, AV_LOG_WARNING, "HMAC mismatch\n");
  151. return AVERROR_INVALIDDATA;
  152. }
  153. len -= s->hmac_size;
  154. *lenptr = len;
  155. if (len < 12)
  156. return AVERROR_INVALIDDATA;
  157. if (rtcp) {
  158. uint32_t srtcp_index = AV_RB32(buf + len - 4);
  159. len -= 4;
  160. *lenptr = len;
  161. ssrc = AV_RB32(buf + 4);
  162. index = srtcp_index & 0x7fffffff;
  163. buf += 8;
  164. len -= 8;
  165. if (!(srtcp_index & 0x80000000))
  166. return 0;
  167. } else {
  168. s->seq_initialized = 1;
  169. s->seq_largest = seq_largest;
  170. s->roc = roc;
  171. ext = buf[0] & 0x10;
  172. ssrc = AV_RB32(buf + 8);
  173. buf += 12;
  174. len -= 12;
  175. if (ext) {
  176. if (len < 4)
  177. return AVERROR_INVALIDDATA;
  178. ext = (AV_RB16(buf + 2) + 1) * 4;
  179. if (len < ext)
  180. return AVERROR_INVALIDDATA;
  181. len -= ext;
  182. buf += ext;
  183. }
  184. }
  185. create_iv(iv, rtcp ? s->rtcp_salt : s->rtp_salt, index, ssrc);
  186. av_aes_init(s->aes, rtcp ? s->rtcp_key : s->rtp_key, 128, 0);
  187. encrypt_counter(s->aes, iv, buf, len);
  188. return 0;
  189. }
  190. int ff_srtp_encrypt(struct SRTPContext *s, const uint8_t *in, int len,
  191. uint8_t *out, int outlen)
  192. {
  193. uint8_t iv[16] = { 0 }, hmac[20];
  194. uint64_t index;
  195. uint32_t ssrc;
  196. int rtcp;
  197. uint8_t *buf;
  198. if (len + 14 > outlen)
  199. return 0;
  200. if (len < 12)
  201. return 0;
  202. memcpy(out, in, len);
  203. buf = out;
  204. rtcp = RTP_PT_IS_RTCP(buf[1]);
  205. if (rtcp) {
  206. ssrc = AV_RB32(buf + 4);
  207. index = s->rtcp_index++;
  208. buf += 8;
  209. len -= 8;
  210. } else {
  211. int ext;
  212. int seq = AV_RB16(buf + 2);
  213. ssrc = AV_RB32(buf + 8);
  214. if (seq < s->seq_largest)
  215. s->roc++;
  216. s->seq_largest = seq;
  217. index = seq + (((uint64_t)s->roc) << 16);
  218. ext = buf[0] & 0x10;
  219. buf += 12;
  220. len -= 12;
  221. if (ext) {
  222. if (len < 4)
  223. return AVERROR_INVALIDDATA;
  224. ext = (AV_RB16(buf + 2) + 1) * 4;
  225. if (len < ext)
  226. return AVERROR_INVALIDDATA;
  227. len -= ext;
  228. buf += ext;
  229. }
  230. }
  231. create_iv(iv, rtcp ? s->rtcp_salt : s->rtp_salt, index, ssrc);
  232. av_aes_init(s->aes, rtcp ? s->rtcp_key : s->rtp_key, 128, 0);
  233. encrypt_counter(s->aes, iv, buf, len);
  234. if (rtcp) {
  235. AV_WB32(buf + len, 0x80000000 | index);
  236. len += 4;
  237. }
  238. av_hmac_init(s->hmac, rtcp ? s->rtcp_auth : s->rtp_auth, sizeof(s->rtp_auth));
  239. av_hmac_update(s->hmac, out, buf + len - out);
  240. if (!rtcp) {
  241. uint8_t rocbuf[4];
  242. AV_WB32(rocbuf, s->roc);
  243. av_hmac_update(s->hmac, rocbuf, 4);
  244. }
  245. av_hmac_final(s->hmac, hmac, sizeof(hmac));
  246. memcpy(buf + len, hmac, s->hmac_size);
  247. len += s->hmac_size;
  248. return buf + len - out;
  249. }
  250. #ifdef TEST
  251. #include <stdio.h>
  252. static const char *aes128_80_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn";
  253. static const uint8_t rtp_aes128_80[] = {
  254. // RTP header
  255. 0x80, 0xe0, 0x12, 0x34,
  256. 0x12, 0x34, 0x56, 0x78,
  257. 0x12, 0x34, 0x56, 0x78,
  258. // encrypted payload
  259. 0x62, 0x69, 0x76, 0xca, 0xc5,
  260. // HMAC
  261. 0xa1, 0xac, 0x1b, 0xb4, 0xa0, 0x1c, 0xd5, 0x49, 0x28, 0x99,
  262. };
  263. static const uint8_t rtcp_aes128_80[] = {
  264. // RTCP header
  265. 0x81, 0xc9, 0x00, 0x07,
  266. 0x12, 0x34, 0x56, 0x78,
  267. // encrypted payload
  268. 0x8a, 0xac, 0xdc, 0xa5,
  269. 0x4c, 0xf6, 0x78, 0xa6,
  270. 0x62, 0x8f, 0x24, 0xda,
  271. 0x6c, 0x09, 0x3f, 0xa9,
  272. 0x28, 0x7a, 0xb5, 0x7f,
  273. 0x1f, 0x0f, 0xc9, 0x35,
  274. // RTCP index
  275. 0x80, 0x00, 0x00, 0x03,
  276. // HMAC
  277. 0xe9, 0x3b, 0xc0, 0x5c, 0x0c, 0x06, 0x9f, 0xab, 0xc0, 0xde,
  278. };
  279. static const char *aes128_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn";
  280. static const uint8_t rtp_aes128_32[] = {
  281. // RTP header
  282. 0x80, 0xe0, 0x12, 0x34,
  283. 0x12, 0x34, 0x56, 0x78,
  284. 0x12, 0x34, 0x56, 0x78,
  285. // encrypted payload
  286. 0x62, 0x69, 0x76, 0xca, 0xc5,
  287. // HMAC
  288. 0xa1, 0xac, 0x1b, 0xb4,
  289. };
  290. static const uint8_t rtcp_aes128_32[] = {
  291. // RTCP header
  292. 0x81, 0xc9, 0x00, 0x07,
  293. 0x12, 0x34, 0x56, 0x78,
  294. // encrypted payload
  295. 0x35, 0xe9, 0xb5, 0xff,
  296. 0x0d, 0xd1, 0xde, 0x70,
  297. 0x74, 0x10, 0xaa, 0x1b,
  298. 0xb2, 0x8d, 0xf0, 0x20,
  299. 0x02, 0x99, 0x6b, 0x1b,
  300. 0x0b, 0xd0, 0x47, 0x34,
  301. // RTCP index
  302. 0x80, 0x00, 0x00, 0x04,
  303. // HMAC
  304. 0x5b, 0xd2, 0xa9, 0x9d,
  305. };
  306. static void print_data(const uint8_t *buf, int len)
  307. {
  308. int i;
  309. for (i = 0; i < len; i++)
  310. printf("%02x", buf[i]);
  311. printf("\n");
  312. }
  313. static int test_decrypt(struct SRTPContext *srtp, const uint8_t *in, int len,
  314. uint8_t *out)
  315. {
  316. memcpy(out, in, len);
  317. if (!ff_srtp_decrypt(srtp, out, &len)) {
  318. print_data(out, len);
  319. return len;
  320. } else
  321. return -1;
  322. }
  323. static void test_encrypt(const uint8_t *data, int in_len, const char *suite,
  324. const char *key)
  325. {
  326. struct SRTPContext enc = { 0 }, dec = { 0 };
  327. int len;
  328. char buf[1500];
  329. ff_srtp_set_crypto(&enc, suite, key);
  330. ff_srtp_set_crypto(&dec, suite, key);
  331. len = ff_srtp_encrypt(&enc, data, in_len, buf, sizeof(buf));
  332. if (!ff_srtp_decrypt(&dec, buf, &len)) {
  333. if (len == in_len && !memcmp(buf, data, len))
  334. printf("Decrypted content matches input\n");
  335. else
  336. printf("Decrypted content doesn't match input\n");
  337. } else {
  338. printf("Decryption failed\n");
  339. }
  340. ff_srtp_free(&enc);
  341. ff_srtp_free(&dec);
  342. }
  343. int main(void)
  344. {
  345. static const char *aes128_80_suite = "AES_CM_128_HMAC_SHA1_80";
  346. static const char *aes128_32_suite = "AES_CM_128_HMAC_SHA1_32";
  347. static const char *test_key = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
  348. uint8_t buf[1500];
  349. struct SRTPContext srtp = { 0 };
  350. int len;
  351. ff_srtp_set_crypto(&srtp, aes128_80_suite, aes128_80_key);
  352. len = test_decrypt(&srtp, rtp_aes128_80, sizeof(rtp_aes128_80), buf);
  353. test_encrypt(buf, len, aes128_80_suite, test_key);
  354. test_encrypt(buf, len, aes128_32_suite, test_key);
  355. test_decrypt(&srtp, rtcp_aes128_80, sizeof(rtcp_aes128_80), buf);
  356. test_encrypt(buf, len, aes128_80_suite, test_key);
  357. test_encrypt(buf, len, aes128_32_suite, test_key);
  358. ff_srtp_free(&srtp);
  359. memset(&srtp, 0, sizeof(srtp)); // Clear the context
  360. ff_srtp_set_crypto(&srtp, aes128_32_suite, aes128_32_key);
  361. test_decrypt(&srtp, rtp_aes128_32, sizeof(rtp_aes128_32), buf);
  362. test_decrypt(&srtp, rtcp_aes128_32, sizeof(rtcp_aes128_32), buf);
  363. ff_srtp_free(&srtp);
  364. return 0;
  365. }
  366. #endif /* TEST */