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.

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