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.

326 lines
8.9KB

  1. /*
  2. * SRTP encryption/decryption
  3. * Copyright (c) 2012 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. }