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.

297 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 {
  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_malloc(default_buf_len);
  87. if (!*buf)
  88. return AVERROR(ENOMEM);
  89. memcpy(*buf, default_buf, default_buf_len);
  90. *buf_len = default_buf_len;
  91. } else if (*buf_len != BLOCKSIZE) {
  92. av_log(c, AV_LOG_ERROR,
  93. "invalid %s size (%d bytes, block size is %d)\n",
  94. desc, *buf_len, BLOCKSIZE);
  95. return AVERROR(EINVAL);
  96. }
  97. return 0;
  98. }
  99. static int crypto_open2(URLContext *h, const char *uri, int flags, AVDictionary **options)
  100. {
  101. const char *nested_url;
  102. int ret = 0;
  103. CryptoContext *c = h->priv_data;
  104. if (!av_strstart(uri, "crypto+", &nested_url) &&
  105. !av_strstart(uri, "crypto:", &nested_url)) {
  106. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  107. ret = AVERROR(EINVAL);
  108. goto err;
  109. }
  110. if (flags & AVIO_FLAG_READ) {
  111. if ((ret = set_aes_arg(c, &c->decrypt_key, &c->decrypt_keylen,
  112. c->key, c->keylen, "decryption key")) < 0)
  113. goto err;
  114. if ((ret = set_aes_arg(c, &c->decrypt_iv, &c->decrypt_ivlen,
  115. c->key, c->keylen, "decryption IV")) < 0)
  116. goto err;
  117. }
  118. if (flags & AVIO_FLAG_WRITE) {
  119. if ((ret = set_aes_arg(c, &c->encrypt_key, &c->encrypt_keylen,
  120. c->key, c->keylen, "encryption key")) < 0)
  121. if (ret < 0)
  122. goto err;
  123. if ((ret = set_aes_arg(c, &c->encrypt_iv, &c->encrypt_ivlen,
  124. c->key, c->keylen, "encryption IV")) < 0)
  125. goto err;
  126. }
  127. if ((ret = ffurl_open(&c->hd, nested_url, flags,
  128. &h->interrupt_callback, options)) < 0) {
  129. av_log(h, AV_LOG_ERROR, "Unable to open resource: %s\n", nested_url);
  130. goto err;
  131. }
  132. if (flags & AVIO_FLAG_READ) {
  133. c->aes_decrypt = av_aes_alloc();
  134. if (!c->aes_decrypt) {
  135. ret = AVERROR(ENOMEM);
  136. goto err;
  137. }
  138. ret = av_aes_init(c->aes_decrypt, c->decrypt_key, BLOCKSIZE*8, 1);
  139. if (ret < 0)
  140. goto err;
  141. }
  142. if (flags & AVIO_FLAG_WRITE) {
  143. c->aes_encrypt = av_aes_alloc();
  144. if (!c->aes_encrypt) {
  145. ret = AVERROR(ENOMEM);
  146. goto err;
  147. }
  148. ret = av_aes_init(c->aes_encrypt, c->encrypt_key, BLOCKSIZE*8, 0);
  149. if (ret < 0)
  150. goto err;
  151. }
  152. c->pad_len = 0;
  153. h->is_streamed = 1;
  154. err:
  155. return ret;
  156. }
  157. static int crypto_read(URLContext *h, uint8_t *buf, int size)
  158. {
  159. CryptoContext *c = h->priv_data;
  160. int blocks;
  161. retry:
  162. if (c->outdata > 0) {
  163. size = FFMIN(size, c->outdata);
  164. memcpy(buf, c->outptr, size);
  165. c->outptr += size;
  166. c->outdata -= size;
  167. return size;
  168. }
  169. // We avoid using the last block until we've found EOF,
  170. // since we'll remove PKCS7 padding at the end. So make
  171. // sure we've got at least 2 blocks, so we can decrypt
  172. // at least one.
  173. while (c->indata - c->indata_used < 2*BLOCKSIZE) {
  174. int n = ffurl_read(c->hd, c->inbuffer + c->indata,
  175. sizeof(c->inbuffer) - c->indata);
  176. if (n <= 0) {
  177. c->eof = 1;
  178. break;
  179. }
  180. c->indata += n;
  181. }
  182. blocks = (c->indata - c->indata_used) / BLOCKSIZE;
  183. if (!blocks)
  184. return AVERROR_EOF;
  185. if (!c->eof)
  186. blocks--;
  187. av_aes_crypt(c->aes_decrypt, c->outbuffer, c->inbuffer + c->indata_used,
  188. blocks, c->decrypt_iv, 1);
  189. c->outdata = BLOCKSIZE * blocks;
  190. c->outptr = c->outbuffer;
  191. c->indata_used += BLOCKSIZE * blocks;
  192. if (c->indata_used >= sizeof(c->inbuffer)/2) {
  193. memmove(c->inbuffer, c->inbuffer + c->indata_used,
  194. c->indata - c->indata_used);
  195. c->indata -= c->indata_used;
  196. c->indata_used = 0;
  197. }
  198. if (c->eof) {
  199. // Remove PKCS7 padding at the end
  200. int padding = c->outbuffer[c->outdata - 1];
  201. c->outdata -= padding;
  202. }
  203. goto retry;
  204. }
  205. static int crypto_write(URLContext *h, const unsigned char *buf, int size)
  206. {
  207. CryptoContext *c = h->priv_data;
  208. int total_size, blocks, pad_len, out_size;
  209. uint8_t *out_buf;
  210. int ret = 0;
  211. total_size = size + c->pad_len;
  212. pad_len = total_size % BLOCKSIZE;
  213. out_size = total_size - pad_len;
  214. blocks = out_size / BLOCKSIZE;
  215. if (out_size) {
  216. out_buf = av_malloc(out_size);
  217. if (!out_buf)
  218. return AVERROR(ENOMEM);
  219. if (c->pad_len) {
  220. memcpy(&c->pad[c->pad_len], buf, BLOCKSIZE - c->pad_len);
  221. av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
  222. blocks--;
  223. }
  224. av_aes_crypt(c->aes_encrypt, &out_buf[c->pad_len ? BLOCKSIZE : 0],
  225. &buf[c->pad_len ? BLOCKSIZE - c->pad_len: 0],
  226. blocks, c->encrypt_iv, 0);
  227. ret = ffurl_write(c->hd, out_buf, out_size);
  228. av_free(out_buf);
  229. if (ret < 0)
  230. return ret;
  231. memcpy(c->pad, &buf[size - pad_len], pad_len);
  232. } else
  233. memcpy(&c->pad[c->pad_len], buf, size);
  234. c->pad_len = pad_len;
  235. return size;
  236. }
  237. static int crypto_close(URLContext *h)
  238. {
  239. CryptoContext *c = h->priv_data;
  240. uint8_t out_buf[BLOCKSIZE];
  241. int ret, pad;
  242. if (c->aes_encrypt) {
  243. pad = BLOCKSIZE - c->pad_len;
  244. memset(&c->pad[c->pad_len], pad, pad);
  245. av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
  246. if ((ret = ffurl_write(c->hd, out_buf, BLOCKSIZE)) < 0)
  247. return ret;
  248. }
  249. if (c->hd)
  250. ffurl_close(c->hd);
  251. av_freep(&c->aes_decrypt);
  252. av_freep(&c->aes_encrypt);
  253. return 0;
  254. }
  255. URLProtocol ff_crypto_protocol = {
  256. .name = "crypto",
  257. .url_open2 = crypto_open2,
  258. .url_read = crypto_read,
  259. .url_write = crypto_write,
  260. .url_close = crypto_close,
  261. .priv_data_size = sizeof(CryptoContext),
  262. .priv_data_class = &crypto_class,
  263. .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
  264. };