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.

294 lines
8.0KB

  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, seq_largest;
  113. uint32_t ssrc, 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. }