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.

296 lines
9.2KB

  1. /*
  2. * Decryption protocol handler
  3. * Copyright (c) 2011 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 "avformat.h"
  22. #include "libavutil/aes.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/opt.h"
  25. #include "internal.h"
  26. #include "url.h"
  27. #define MAX_BUFFER_BLOCKS 150
  28. #define BLOCKSIZE 16
  29. typedef struct CryptoContext {
  30. const AVClass *class;
  31. URLContext *hd;
  32. uint8_t inbuffer [BLOCKSIZE*MAX_BUFFER_BLOCKS],
  33. outbuffer[BLOCKSIZE*MAX_BUFFER_BLOCKS];
  34. uint8_t *outptr;
  35. int indata, indata_used, outdata;
  36. int eof;
  37. uint8_t *key;
  38. int keylen;
  39. uint8_t *iv;
  40. int ivlen;
  41. uint8_t *decrypt_key;
  42. int decrypt_keylen;
  43. uint8_t *decrypt_iv;
  44. int decrypt_ivlen;
  45. uint8_t *encrypt_key;
  46. int encrypt_keylen;
  47. uint8_t *encrypt_iv;
  48. int encrypt_ivlen;
  49. struct AVAES *aes_decrypt;
  50. struct AVAES *aes_encrypt;
  51. uint8_t pad[BLOCKSIZE];
  52. int pad_len;
  53. } CryptoContext;
  54. #define OFFSET(x) offsetof(CryptoContext, x)
  55. #define D AV_OPT_FLAG_DECODING_PARAM
  56. #define E AV_OPT_FLAG_ENCODING_PARAM
  57. static const AVOption options[] = {
  58. {"key", "AES encryption/decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, .flags = D|E },
  59. {"iv", "AES encryption/decryption initialization vector", OFFSET(iv), AV_OPT_TYPE_BINARY, .flags = D|E },
  60. {"decryption_key", "AES decryption key", OFFSET(decrypt_key), AV_OPT_TYPE_BINARY, .flags = D },
  61. {"decryption_iv", "AES decryption initialization vector", OFFSET(decrypt_iv), AV_OPT_TYPE_BINARY, .flags = D },
  62. {"encryption_key", "AES encryption key", OFFSET(encrypt_key), AV_OPT_TYPE_BINARY, .flags = E },
  63. {"encryption_iv", "AES encryption initialization vector", OFFSET(encrypt_iv), AV_OPT_TYPE_BINARY, .flags = E },
  64. { NULL }
  65. };
  66. static const AVClass crypto_class = {
  67. .class_name = "crypto",
  68. .item_name = av_default_item_name,
  69. .option = options,
  70. .version = LIBAVUTIL_VERSION_INT,
  71. };
  72. static int set_aes_arg(CryptoContext *c, uint8_t **buf, int *buf_len,
  73. uint8_t *default_buf, int default_buf_len,
  74. const char *desc)
  75. {
  76. if (!*buf_len) {
  77. if (!default_buf_len) {
  78. av_log(c, AV_LOG_ERROR, "%s not set\n", desc);
  79. return AVERROR(EINVAL);
  80. } else if (default_buf_len != BLOCKSIZE) {
  81. av_log(c, AV_LOG_ERROR,
  82. "invalid %s size (%d bytes, block size is %d)\n",
  83. desc, default_buf_len, BLOCKSIZE);
  84. return AVERROR(EINVAL);
  85. }
  86. *buf = av_memdup(default_buf, default_buf_len);
  87. if (!*buf)
  88. return AVERROR(ENOMEM);
  89. *buf_len = default_buf_len;
  90. } else if (*buf_len != BLOCKSIZE) {
  91. av_log(c, AV_LOG_ERROR,
  92. "invalid %s size (%d bytes, block size is %d)\n",
  93. desc, *buf_len, BLOCKSIZE);
  94. return AVERROR(EINVAL);
  95. }
  96. return 0;
  97. }
  98. static int crypto_open2(URLContext *h, const char *uri, int flags, AVDictionary **options)
  99. {
  100. const char *nested_url;
  101. int ret = 0;
  102. CryptoContext *c = h->priv_data;
  103. if (!av_strstart(uri, "crypto+", &nested_url) &&
  104. !av_strstart(uri, "crypto:", &nested_url)) {
  105. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  106. ret = AVERROR(EINVAL);
  107. goto err;
  108. }
  109. if (flags & AVIO_FLAG_READ) {
  110. if ((ret = set_aes_arg(c, &c->decrypt_key, &c->decrypt_keylen,
  111. c->key, c->keylen, "decryption key")) < 0)
  112. goto err;
  113. if ((ret = set_aes_arg(c, &c->decrypt_iv, &c->decrypt_ivlen,
  114. c->iv, c->ivlen, "decryption IV")) < 0)
  115. goto err;
  116. }
  117. if (flags & AVIO_FLAG_WRITE) {
  118. if ((ret = set_aes_arg(c, &c->encrypt_key, &c->encrypt_keylen,
  119. c->key, c->keylen, "encryption key")) < 0)
  120. if (ret < 0)
  121. goto err;
  122. if ((ret = set_aes_arg(c, &c->encrypt_iv, &c->encrypt_ivlen,
  123. c->iv, c->ivlen, "encryption IV")) < 0)
  124. goto err;
  125. }
  126. if ((ret = ffurl_open(&c->hd, nested_url, flags,
  127. &h->interrupt_callback, options)) < 0) {
  128. av_log(h, AV_LOG_ERROR, "Unable to open resource: %s\n", nested_url);
  129. goto err;
  130. }
  131. if (flags & AVIO_FLAG_READ) {
  132. c->aes_decrypt = av_aes_alloc();
  133. if (!c->aes_decrypt) {
  134. ret = AVERROR(ENOMEM);
  135. goto err;
  136. }
  137. ret = av_aes_init(c->aes_decrypt, c->decrypt_key, BLOCKSIZE*8, 1);
  138. if (ret < 0)
  139. goto err;
  140. }
  141. if (flags & AVIO_FLAG_WRITE) {
  142. c->aes_encrypt = av_aes_alloc();
  143. if (!c->aes_encrypt) {
  144. ret = AVERROR(ENOMEM);
  145. goto err;
  146. }
  147. ret = av_aes_init(c->aes_encrypt, c->encrypt_key, BLOCKSIZE*8, 0);
  148. if (ret < 0)
  149. goto err;
  150. }
  151. c->pad_len = 0;
  152. h->is_streamed = 1;
  153. err:
  154. return ret;
  155. }
  156. static int crypto_read(URLContext *h, uint8_t *buf, int size)
  157. {
  158. CryptoContext *c = h->priv_data;
  159. int blocks;
  160. retry:
  161. if (c->outdata > 0) {
  162. size = FFMIN(size, c->outdata);
  163. memcpy(buf, c->outptr, size);
  164. c->outptr += size;
  165. c->outdata -= size;
  166. return size;
  167. }
  168. // We avoid using the last block until we've found EOF,
  169. // since we'll remove PKCS7 padding at the end. So make
  170. // sure we've got at least 2 blocks, so we can decrypt
  171. // at least one.
  172. while (c->indata - c->indata_used < 2*BLOCKSIZE) {
  173. int n = ffurl_read(c->hd, c->inbuffer + c->indata,
  174. sizeof(c->inbuffer) - c->indata);
  175. if (n <= 0) {
  176. c->eof = 1;
  177. break;
  178. }
  179. c->indata += n;
  180. }
  181. blocks = (c->indata - c->indata_used) / BLOCKSIZE;
  182. if (!blocks)
  183. return AVERROR_EOF;
  184. if (!c->eof)
  185. blocks--;
  186. av_aes_crypt(c->aes_decrypt, c->outbuffer, c->inbuffer + c->indata_used,
  187. blocks, c->decrypt_iv, 1);
  188. c->outdata = BLOCKSIZE * blocks;
  189. c->outptr = c->outbuffer;
  190. c->indata_used += BLOCKSIZE * blocks;
  191. if (c->indata_used >= sizeof(c->inbuffer)/2) {
  192. memmove(c->inbuffer, c->inbuffer + c->indata_used,
  193. c->indata - c->indata_used);
  194. c->indata -= c->indata_used;
  195. c->indata_used = 0;
  196. }
  197. if (c->eof) {
  198. // Remove PKCS7 padding at the end
  199. int padding = c->outbuffer[c->outdata - 1];
  200. c->outdata -= padding;
  201. }
  202. goto retry;
  203. }
  204. static int crypto_write(URLContext *h, const unsigned char *buf, int size)
  205. {
  206. CryptoContext *c = h->priv_data;
  207. int total_size, blocks, pad_len, out_size;
  208. uint8_t *out_buf;
  209. int ret = 0;
  210. total_size = size + c->pad_len;
  211. pad_len = total_size % BLOCKSIZE;
  212. out_size = total_size - pad_len;
  213. blocks = out_size / BLOCKSIZE;
  214. if (out_size) {
  215. out_buf = av_malloc(out_size);
  216. if (!out_buf)
  217. return AVERROR(ENOMEM);
  218. if (c->pad_len) {
  219. memcpy(&c->pad[c->pad_len], buf, BLOCKSIZE - c->pad_len);
  220. av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
  221. blocks--;
  222. }
  223. av_aes_crypt(c->aes_encrypt, &out_buf[c->pad_len ? BLOCKSIZE : 0],
  224. &buf[c->pad_len ? BLOCKSIZE - c->pad_len: 0],
  225. blocks, c->encrypt_iv, 0);
  226. ret = ffurl_write(c->hd, out_buf, out_size);
  227. av_free(out_buf);
  228. if (ret < 0)
  229. return ret;
  230. memcpy(c->pad, &buf[size - pad_len], pad_len);
  231. } else
  232. memcpy(&c->pad[c->pad_len], buf, size);
  233. c->pad_len = pad_len;
  234. return size;
  235. }
  236. static int crypto_close(URLContext *h)
  237. {
  238. CryptoContext *c = h->priv_data;
  239. uint8_t out_buf[BLOCKSIZE];
  240. int ret, pad;
  241. if (c->aes_encrypt) {
  242. pad = BLOCKSIZE - c->pad_len;
  243. memset(&c->pad[c->pad_len], pad, pad);
  244. av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
  245. if ((ret = ffurl_write(c->hd, out_buf, BLOCKSIZE)) < 0)
  246. return ret;
  247. }
  248. if (c->hd)
  249. ffurl_close(c->hd);
  250. av_freep(&c->aes_decrypt);
  251. av_freep(&c->aes_encrypt);
  252. return 0;
  253. }
  254. URLProtocol ff_crypto_protocol = {
  255. .name = "crypto",
  256. .url_open2 = crypto_open2,
  257. .url_read = crypto_read,
  258. .url_write = crypto_write,
  259. .url_close = crypto_close,
  260. .priv_data_size = sizeof(CryptoContext),
  261. .priv_data_class = &crypto_class,
  262. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  263. };