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.

472 lines
14KB

  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. !strcmp(suite, "SRTP_AES128_CM_HMAC_SHA1_80")) {
  67. s->rtp_hmac_size = s->rtcp_hmac_size = 10;
  68. } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
  69. s->rtp_hmac_size = s->rtcp_hmac_size = 4;
  70. } else if (!strcmp(suite, "SRTP_AES128_CM_HMAC_SHA1_32")) {
  71. // RFC 5764 section 4.1.2
  72. s->rtp_hmac_size = 4;
  73. s->rtcp_hmac_size = 10;
  74. } else {
  75. av_log(NULL, AV_LOG_WARNING, "SRTP Crypto suite %s not supported\n",
  76. suite);
  77. return AVERROR(EINVAL);
  78. }
  79. if (av_base64_decode(buf, params, sizeof(buf)) != sizeof(buf)) {
  80. av_log(NULL, AV_LOG_WARNING, "Incorrect amount of SRTP params\n");
  81. return AVERROR(EINVAL);
  82. }
  83. // MKI and lifetime not handled yet
  84. s->aes = av_aes_alloc();
  85. s->hmac = av_hmac_alloc(AV_HMAC_SHA1);
  86. if (!s->aes || !s->hmac)
  87. return AVERROR(ENOMEM);
  88. memcpy(s->master_key, buf, 16);
  89. memcpy(s->master_salt, buf + 16, 14);
  90. // RFC 3711
  91. av_aes_init(s->aes, s->master_key, 128, 0);
  92. derive_key(s->aes, s->master_salt, 0x00, s->rtp_key, sizeof(s->rtp_key));
  93. derive_key(s->aes, s->master_salt, 0x02, s->rtp_salt, sizeof(s->rtp_salt));
  94. derive_key(s->aes, s->master_salt, 0x01, s->rtp_auth, sizeof(s->rtp_auth));
  95. derive_key(s->aes, s->master_salt, 0x03, s->rtcp_key, sizeof(s->rtcp_key));
  96. derive_key(s->aes, s->master_salt, 0x05, s->rtcp_salt, sizeof(s->rtcp_salt));
  97. derive_key(s->aes, s->master_salt, 0x04, s->rtcp_auth, sizeof(s->rtcp_auth));
  98. return 0;
  99. }
  100. static void create_iv(uint8_t *iv, const uint8_t *salt, uint64_t index,
  101. uint32_t ssrc)
  102. {
  103. uint8_t indexbuf[8];
  104. int i;
  105. memset(iv, 0, 16);
  106. AV_WB32(&iv[4], ssrc);
  107. AV_WB64(indexbuf, index);
  108. for (i = 0; i < 8; i++) // index << 16
  109. iv[6 + i] ^= indexbuf[i];
  110. for (i = 0; i < 14; i++)
  111. iv[i] ^= salt[i];
  112. }
  113. int ff_srtp_decrypt(struct SRTPContext *s, uint8_t *buf, int *lenptr)
  114. {
  115. uint8_t iv[16] = { 0 }, hmac[20];
  116. int len = *lenptr;
  117. int av_uninit(seq_largest);
  118. uint32_t ssrc, av_uninit(roc);
  119. uint64_t index;
  120. int rtcp, hmac_size;
  121. // TODO: Missing replay protection
  122. if (len < 2)
  123. return AVERROR_INVALIDDATA;
  124. rtcp = RTP_PT_IS_RTCP(buf[1]);
  125. hmac_size = rtcp ? s->rtcp_hmac_size : s->rtp_hmac_size;
  126. if (len < hmac_size)
  127. return AVERROR_INVALIDDATA;
  128. // Authentication HMAC
  129. av_hmac_init(s->hmac, rtcp ? s->rtcp_auth : s->rtp_auth, sizeof(s->rtp_auth));
  130. // If MKI is used, this should exclude the MKI as well
  131. av_hmac_update(s->hmac, buf, len - hmac_size);
  132. if (!rtcp) {
  133. int seq = AV_RB16(buf + 2);
  134. uint32_t v;
  135. uint8_t rocbuf[4];
  136. // RFC 3711 section 3.3.1, appendix A
  137. seq_largest = s->seq_initialized ? s->seq_largest : seq;
  138. v = roc = s->roc;
  139. if (seq_largest < 32768) {
  140. if (seq - seq_largest > 32768)
  141. v = roc - 1;
  142. } else {
  143. if (seq_largest - 32768 > seq)
  144. v = roc + 1;
  145. }
  146. if (v == roc) {
  147. seq_largest = FFMAX(seq_largest, seq);
  148. } else if (v == roc + 1) {
  149. seq_largest = seq;
  150. roc = v;
  151. }
  152. index = seq + (((uint64_t)v) << 16);
  153. AV_WB32(rocbuf, roc);
  154. av_hmac_update(s->hmac, rocbuf, 4);
  155. }
  156. av_hmac_final(s->hmac, hmac, sizeof(hmac));
  157. if (memcmp(hmac, buf + len - hmac_size, hmac_size)) {
  158. av_log(NULL, AV_LOG_WARNING, "HMAC mismatch\n");
  159. return AVERROR_INVALIDDATA;
  160. }
  161. len -= hmac_size;
  162. *lenptr = len;
  163. if (len < 12)
  164. return AVERROR_INVALIDDATA;
  165. if (rtcp) {
  166. uint32_t srtcp_index = AV_RB32(buf + len - 4);
  167. len -= 4;
  168. *lenptr = len;
  169. ssrc = AV_RB32(buf + 4);
  170. index = srtcp_index & 0x7fffffff;
  171. buf += 8;
  172. len -= 8;
  173. if (!(srtcp_index & 0x80000000))
  174. return 0;
  175. } else {
  176. int ext, csrc;
  177. s->seq_initialized = 1;
  178. s->seq_largest = seq_largest;
  179. s->roc = roc;
  180. csrc = buf[0] & 0x0f;
  181. ext = buf[0] & 0x10;
  182. ssrc = AV_RB32(buf + 8);
  183. buf += 12;
  184. len -= 12;
  185. buf += 4 * csrc;
  186. len -= 4 * csrc;
  187. if (len < 0)
  188. return AVERROR_INVALIDDATA;
  189. if (ext) {
  190. if (len < 4)
  191. return AVERROR_INVALIDDATA;
  192. ext = (AV_RB16(buf + 2) + 1) * 4;
  193. if (len < ext)
  194. return AVERROR_INVALIDDATA;
  195. len -= ext;
  196. buf += ext;
  197. }
  198. }
  199. create_iv(iv, rtcp ? s->rtcp_salt : s->rtp_salt, index, ssrc);
  200. av_aes_init(s->aes, rtcp ? s->rtcp_key : s->rtp_key, 128, 0);
  201. encrypt_counter(s->aes, iv, buf, len);
  202. return 0;
  203. }
  204. int ff_srtp_encrypt(struct SRTPContext *s, const uint8_t *in, int len,
  205. uint8_t *out, int outlen)
  206. {
  207. uint8_t iv[16] = { 0 }, hmac[20];
  208. uint64_t index;
  209. uint32_t ssrc;
  210. int rtcp, hmac_size, padding;
  211. uint8_t *buf;
  212. if (len < 8)
  213. return AVERROR_INVALIDDATA;
  214. rtcp = RTP_PT_IS_RTCP(in[1]);
  215. hmac_size = rtcp ? s->rtcp_hmac_size : s->rtp_hmac_size;
  216. padding = hmac_size;
  217. if (rtcp)
  218. padding += 4; // For the RTCP index
  219. if (len + padding > outlen)
  220. return 0;
  221. memcpy(out, in, len);
  222. buf = out;
  223. if (rtcp) {
  224. ssrc = AV_RB32(buf + 4);
  225. index = s->rtcp_index++;
  226. buf += 8;
  227. len -= 8;
  228. } else {
  229. int ext, csrc;
  230. int seq = AV_RB16(buf + 2);
  231. if (len < 12)
  232. return AVERROR_INVALIDDATA;
  233. ssrc = AV_RB32(buf + 8);
  234. if (seq < s->seq_largest)
  235. s->roc++;
  236. s->seq_largest = seq;
  237. index = seq + (((uint64_t)s->roc) << 16);
  238. csrc = buf[0] & 0x0f;
  239. ext = buf[0] & 0x10;
  240. buf += 12;
  241. len -= 12;
  242. buf += 4 * csrc;
  243. len -= 4 * csrc;
  244. if (len < 0)
  245. return AVERROR_INVALIDDATA;
  246. if (ext) {
  247. if (len < 4)
  248. return AVERROR_INVALIDDATA;
  249. ext = (AV_RB16(buf + 2) + 1) * 4;
  250. if (len < ext)
  251. return AVERROR_INVALIDDATA;
  252. len -= ext;
  253. buf += ext;
  254. }
  255. }
  256. create_iv(iv, rtcp ? s->rtcp_salt : s->rtp_salt, index, ssrc);
  257. av_aes_init(s->aes, rtcp ? s->rtcp_key : s->rtp_key, 128, 0);
  258. encrypt_counter(s->aes, iv, buf, len);
  259. if (rtcp) {
  260. AV_WB32(buf + len, 0x80000000 | index);
  261. len += 4;
  262. }
  263. av_hmac_init(s->hmac, rtcp ? s->rtcp_auth : s->rtp_auth, sizeof(s->rtp_auth));
  264. av_hmac_update(s->hmac, out, buf + len - out);
  265. if (!rtcp) {
  266. uint8_t rocbuf[4];
  267. AV_WB32(rocbuf, s->roc);
  268. av_hmac_update(s->hmac, rocbuf, 4);
  269. }
  270. av_hmac_final(s->hmac, hmac, sizeof(hmac));
  271. memcpy(buf + len, hmac, hmac_size);
  272. len += hmac_size;
  273. return buf + len - out;
  274. }
  275. #ifdef TEST
  276. #include <stdio.h>
  277. static const char *aes128_80_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn";
  278. static const uint8_t rtp_aes128_80[] = {
  279. // RTP header
  280. 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
  281. // encrypted payload
  282. 0x62, 0x69, 0x76, 0xca, 0xc5,
  283. // HMAC
  284. 0xa1, 0xac, 0x1b, 0xb4, 0xa0, 0x1c, 0xd5, 0x49, 0x28, 0x99,
  285. };
  286. static const uint8_t rtcp_aes128_80[] = {
  287. // RTCP header
  288. 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78,
  289. // encrypted payload
  290. 0x8a, 0xac, 0xdc, 0xa5, 0x4c, 0xf6, 0x78, 0xa6, 0x62, 0x8f, 0x24, 0xda,
  291. 0x6c, 0x09, 0x3f, 0xa9, 0x28, 0x7a, 0xb5, 0x7f, 0x1f, 0x0f, 0xc9, 0x35,
  292. // RTCP index
  293. 0x80, 0x00, 0x00, 0x03,
  294. // HMAC
  295. 0xe9, 0x3b, 0xc0, 0x5c, 0x0c, 0x06, 0x9f, 0xab, 0xc0, 0xde,
  296. };
  297. static const char *aes128_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn";
  298. static const uint8_t rtp_aes128_32[] = {
  299. // RTP header
  300. 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
  301. // encrypted payload
  302. 0x62, 0x69, 0x76, 0xca, 0xc5,
  303. // HMAC
  304. 0xa1, 0xac, 0x1b, 0xb4,
  305. };
  306. static const uint8_t rtcp_aes128_32[] = {
  307. // RTCP header
  308. 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78,
  309. // encrypted payload
  310. 0x35, 0xe9, 0xb5, 0xff, 0x0d, 0xd1, 0xde, 0x70, 0x74, 0x10, 0xaa, 0x1b,
  311. 0xb2, 0x8d, 0xf0, 0x20, 0x02, 0x99, 0x6b, 0x1b, 0x0b, 0xd0, 0x47, 0x34,
  312. // RTCP index
  313. 0x80, 0x00, 0x00, 0x04,
  314. // HMAC
  315. 0x5b, 0xd2, 0xa9, 0x9d,
  316. };
  317. static const char *aes128_80_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn";
  318. static const uint8_t rtp_aes128_80_32[] = {
  319. // RTP header
  320. 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
  321. // encrypted payload
  322. 0x62, 0x69, 0x76, 0xca, 0xc5,
  323. // HMAC
  324. 0xa1, 0xac, 0x1b, 0xb4,
  325. };
  326. static const uint8_t rtcp_aes128_80_32[] = {
  327. // RTCP header
  328. 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78,
  329. // encrypted payload
  330. 0xd6, 0xae, 0xc1, 0x58, 0x63, 0x70, 0xc9, 0x88, 0x66, 0x26, 0x1c, 0x53,
  331. 0xff, 0x5d, 0x5d, 0x2b, 0x0f, 0x8c, 0x72, 0x3e, 0xc9, 0x1d, 0x43, 0xf9,
  332. // RTCP index
  333. 0x80, 0x00, 0x00, 0x05,
  334. // HMAC
  335. 0x09, 0x16, 0xb4, 0x27, 0x9a, 0xe9, 0x92, 0x26, 0x4e, 0x10,
  336. };
  337. static void print_data(const uint8_t *buf, int len)
  338. {
  339. int i;
  340. for (i = 0; i < len; i++)
  341. printf("%02x", buf[i]);
  342. printf("\n");
  343. }
  344. static int test_decrypt(struct SRTPContext *srtp, const uint8_t *in, int len,
  345. uint8_t *out)
  346. {
  347. memcpy(out, in, len);
  348. if (!ff_srtp_decrypt(srtp, out, &len)) {
  349. print_data(out, len);
  350. return len;
  351. } else
  352. return -1;
  353. }
  354. static void test_encrypt(const uint8_t *data, int in_len, const char *suite,
  355. const char *key)
  356. {
  357. struct SRTPContext enc = { 0 }, dec = { 0 };
  358. int len;
  359. char buf[1500];
  360. ff_srtp_set_crypto(&enc, suite, key);
  361. ff_srtp_set_crypto(&dec, suite, key);
  362. len = ff_srtp_encrypt(&enc, data, in_len, buf, sizeof(buf));
  363. if (!ff_srtp_decrypt(&dec, buf, &len)) {
  364. if (len == in_len && !memcmp(buf, data, len))
  365. printf("Decrypted content matches input\n");
  366. else
  367. printf("Decrypted content doesn't match input\n");
  368. } else {
  369. printf("Decryption failed\n");
  370. }
  371. ff_srtp_free(&enc);
  372. ff_srtp_free(&dec);
  373. }
  374. int main(void)
  375. {
  376. static const char *aes128_80_suite = "AES_CM_128_HMAC_SHA1_80";
  377. static const char *aes128_32_suite = "AES_CM_128_HMAC_SHA1_32";
  378. static const char *aes128_80_32_suite = "SRTP_AES128_CM_HMAC_SHA1_32";
  379. static const char *test_key = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
  380. uint8_t buf[1500];
  381. struct SRTPContext srtp = { 0 };
  382. int len;
  383. ff_srtp_set_crypto(&srtp, aes128_80_suite, aes128_80_key);
  384. len = test_decrypt(&srtp, rtp_aes128_80, sizeof(rtp_aes128_80), buf);
  385. test_encrypt(buf, len, aes128_80_suite, test_key);
  386. test_encrypt(buf, len, aes128_32_suite, test_key);
  387. test_encrypt(buf, len, aes128_80_32_suite, test_key);
  388. test_decrypt(&srtp, rtcp_aes128_80, sizeof(rtcp_aes128_80), buf);
  389. test_encrypt(buf, len, aes128_80_suite, test_key);
  390. test_encrypt(buf, len, aes128_32_suite, test_key);
  391. test_encrypt(buf, len, aes128_80_32_suite, test_key);
  392. ff_srtp_free(&srtp);
  393. memset(&srtp, 0, sizeof(srtp)); // Clear the context
  394. ff_srtp_set_crypto(&srtp, aes128_32_suite, aes128_32_key);
  395. test_decrypt(&srtp, rtp_aes128_32, sizeof(rtp_aes128_32), buf);
  396. test_decrypt(&srtp, rtcp_aes128_32, sizeof(rtcp_aes128_32), buf);
  397. ff_srtp_free(&srtp);
  398. memset(&srtp, 0, sizeof(srtp)); // Clear the context
  399. ff_srtp_set_crypto(&srtp, aes128_80_32_suite, aes128_80_32_key);
  400. test_decrypt(&srtp, rtp_aes128_80_32, sizeof(rtp_aes128_80_32), buf);
  401. test_decrypt(&srtp, rtcp_aes128_80_32, sizeof(rtcp_aes128_80_32), buf);
  402. ff_srtp_free(&srtp);
  403. return 0;
  404. }
  405. #endif /* TEST */