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 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 "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 *write_buf;
  52. unsigned int write_buf_size;
  53. uint8_t pad[BLOCKSIZE];
  54. int pad_len;
  55. } CryptoContext;
  56. #define OFFSET(x) offsetof(CryptoContext, x)
  57. #define D AV_OPT_FLAG_DECODING_PARAM
  58. #define E AV_OPT_FLAG_ENCODING_PARAM
  59. static const AVOption options[] = {
  60. {"key", "AES encryption/decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, .flags = D|E },
  61. {"iv", "AES encryption/decryption initialization vector", OFFSET(iv), AV_OPT_TYPE_BINARY, .flags = D|E },
  62. {"decryption_key", "AES decryption key", OFFSET(decrypt_key), AV_OPT_TYPE_BINARY, .flags = D },
  63. {"decryption_iv", "AES decryption initialization vector", OFFSET(decrypt_iv), AV_OPT_TYPE_BINARY, .flags = D },
  64. {"encryption_key", "AES encryption key", OFFSET(encrypt_key), AV_OPT_TYPE_BINARY, .flags = E },
  65. {"encryption_iv", "AES encryption initialization vector", OFFSET(encrypt_iv), AV_OPT_TYPE_BINARY, .flags = E },
  66. { NULL }
  67. };
  68. static const AVClass crypto_class = {
  69. .class_name = "crypto",
  70. .item_name = av_default_item_name,
  71. .option = options,
  72. .version = LIBAVUTIL_VERSION_INT,
  73. };
  74. static int set_aes_arg(URLContext *h, uint8_t **buf, int *buf_len,
  75. uint8_t *default_buf, int default_buf_len,
  76. const char *desc)
  77. {
  78. if (!*buf_len) {
  79. if (!default_buf_len) {
  80. av_log(h, AV_LOG_ERROR, "%s not set\n", desc);
  81. return AVERROR(EINVAL);
  82. } else if (default_buf_len != BLOCKSIZE) {
  83. av_log(h, AV_LOG_ERROR,
  84. "invalid %s size (%d bytes, block size is %d)\n",
  85. desc, default_buf_len, BLOCKSIZE);
  86. return AVERROR(EINVAL);
  87. }
  88. *buf = av_malloc(default_buf_len);
  89. if (!*buf)
  90. return AVERROR(ENOMEM);
  91. memcpy(*buf, default_buf, default_buf_len);
  92. *buf_len = default_buf_len;
  93. } else if (*buf_len != BLOCKSIZE) {
  94. av_log(h, AV_LOG_ERROR,
  95. "invalid %s size (%d bytes, block size is %d)\n",
  96. desc, *buf_len, BLOCKSIZE);
  97. return AVERROR(EINVAL);
  98. }
  99. return 0;
  100. }
  101. static int crypto_open(URLContext *h, const char *uri, int flags)
  102. {
  103. const char *nested_url;
  104. int ret = 0;
  105. CryptoContext *c = h->priv_data;
  106. if (!av_strstart(uri, "crypto+", &nested_url) &&
  107. !av_strstart(uri, "crypto:", &nested_url)) {
  108. av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
  109. ret = AVERROR(EINVAL);
  110. goto err;
  111. }
  112. if (flags & AVIO_FLAG_READ) {
  113. if ((ret = set_aes_arg(h, &c->decrypt_key, &c->decrypt_keylen,
  114. c->key, c->keylen, "decryption key")) < 0)
  115. goto err;
  116. if ((ret = set_aes_arg(h, &c->decrypt_iv, &c->decrypt_ivlen,
  117. c->iv, c->ivlen, "decryption IV")) < 0)
  118. goto err;
  119. }
  120. if (flags & AVIO_FLAG_WRITE) {
  121. if ((ret = set_aes_arg(h, &c->encrypt_key, &c->encrypt_keylen,
  122. c->key, c->keylen, "encryption key")) < 0)
  123. if (ret < 0)
  124. goto err;
  125. if ((ret = set_aes_arg(h, &c->encrypt_iv, &c->encrypt_ivlen,
  126. c->iv, c->ivlen, "encryption IV")) < 0)
  127. goto err;
  128. }
  129. if ((ret = ffurl_open(&c->hd, nested_url, flags,
  130. &h->interrupt_callback, NULL, h->protocols, h)) < 0) {
  131. av_log(h, AV_LOG_ERROR, "Unable to open resource: %s\n", nested_url);
  132. goto err;
  133. }
  134. if (flags & AVIO_FLAG_READ) {
  135. c->aes_decrypt = av_aes_alloc();
  136. if (!c->aes_decrypt) {
  137. ret = AVERROR(ENOMEM);
  138. goto err;
  139. }
  140. ret = av_aes_init(c->aes_decrypt, c->decrypt_key, BLOCKSIZE * 8, 1);
  141. if (ret < 0)
  142. goto err;
  143. }
  144. if (flags & AVIO_FLAG_WRITE) {
  145. c->aes_encrypt = av_aes_alloc();
  146. if (!c->aes_encrypt) {
  147. ret = AVERROR(ENOMEM);
  148. goto err;
  149. }
  150. ret = av_aes_init(c->aes_encrypt, c->encrypt_key, BLOCKSIZE * 8, 0);
  151. if (ret < 0)
  152. goto err;
  153. }
  154. h->is_streamed = 1;
  155. err:
  156. return ret;
  157. }
  158. static int crypto_read(URLContext *h, uint8_t *buf, int size)
  159. {
  160. CryptoContext *c = h->priv_data;
  161. int blocks;
  162. retry:
  163. if (c->outdata > 0) {
  164. size = FFMIN(size, c->outdata);
  165. memcpy(buf, c->outptr, size);
  166. c->outptr += size;
  167. c->outdata -= size;
  168. return size;
  169. }
  170. // We avoid using the last block until we've found EOF,
  171. // since we'll remove PKCS7 padding at the end. So make
  172. // sure we've got at least 2 blocks, so we can decrypt
  173. // at least one.
  174. while (c->indata - c->indata_used < 2*BLOCKSIZE) {
  175. int n = ffurl_read(c->hd, c->inbuffer + c->indata,
  176. sizeof(c->inbuffer) - c->indata);
  177. if (n <= 0) {
  178. c->eof = 1;
  179. break;
  180. }
  181. c->indata += n;
  182. }
  183. blocks = (c->indata - c->indata_used) / BLOCKSIZE;
  184. if (!blocks)
  185. return AVERROR_EOF;
  186. if (!c->eof)
  187. blocks--;
  188. av_aes_crypt(c->aes_decrypt, c->outbuffer, c->inbuffer + c->indata_used,
  189. blocks, c->decrypt_iv, 1);
  190. c->outdata = BLOCKSIZE * blocks;
  191. c->outptr = c->outbuffer;
  192. c->indata_used += BLOCKSIZE * blocks;
  193. if (c->indata_used >= sizeof(c->inbuffer)/2) {
  194. memmove(c->inbuffer, c->inbuffer + c->indata_used,
  195. c->indata - c->indata_used);
  196. c->indata -= c->indata_used;
  197. c->indata_used = 0;
  198. }
  199. if (c->eof) {
  200. // Remove PKCS7 padding at the end
  201. int padding = c->outbuffer[c->outdata - 1];
  202. c->outdata -= padding;
  203. }
  204. goto retry;
  205. }
  206. static int crypto_write(URLContext *h, const uint8_t *buf, int size)
  207. {
  208. CryptoContext *c = h->priv_data;
  209. int total_size, blocks, pad_len, out_size;
  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. av_fast_malloc(&c->write_buf, &c->write_buf_size, out_size);
  217. if (!c->write_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, c->write_buf, c->pad, 1, c->encrypt_iv, 0);
  222. blocks--;
  223. }
  224. av_aes_crypt(c->aes_encrypt,
  225. &c->write_buf[c->pad_len ? BLOCKSIZE : 0],
  226. &buf[c->pad_len ? BLOCKSIZE - c->pad_len : 0],
  227. blocks, c->encrypt_iv, 0);
  228. ret = ffurl_write(c->hd, c->write_buf, out_size);
  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. int ret = 0;
  241. if (c->aes_encrypt) {
  242. uint8_t out_buf[BLOCKSIZE];
  243. int 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. ret = ffurl_write(c->hd, out_buf, BLOCKSIZE);
  247. }
  248. if (c->hd)
  249. ffurl_close(c->hd);
  250. av_freep(&c->aes_decrypt);
  251. av_freep(&c->aes_encrypt);
  252. av_freep(&c->write_buf);
  253. return ret;
  254. }
  255. const URLProtocol ff_crypto_protocol = {
  256. .name = "crypto",
  257. .url_open = crypto_open,
  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. };