| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 22 | #include "libavutil/aes.h" | ||
| 23 | #include "libavutil/avstring.h" | ||
| 24 | #include "libavutil/mem.h" | ||
| 25 | #include "libavutil/opt.h" | ||
| 26 | #include "url.h" | ||
| 27 | |||
| 28 | // encourage reads of 4096 bytes - 1 block is always retained. | ||
| 29 | #define MAX_BUFFER_BLOCKS 257 | ||
| 30 | #define BLOCKSIZE 16 | ||
| 31 | |||
| 32 | typedef struct CryptoContext { | ||
| 33 | const AVClass *class; | ||
| 34 | URLContext *hd; | ||
| 35 | uint8_t inbuffer [BLOCKSIZE*MAX_BUFFER_BLOCKS], | ||
| 36 | outbuffer[BLOCKSIZE*MAX_BUFFER_BLOCKS]; | ||
| 37 | uint8_t *outptr; | ||
| 38 | int indata, indata_used, outdata; | ||
| 39 | int64_t position; // position in file - used in seek | ||
| 40 | int flags; | ||
| 41 | int eof; | ||
| 42 | uint8_t *key; | ||
| 43 | int keylen; | ||
| 44 | uint8_t *iv; | ||
| 45 | int ivlen; | ||
| 46 | uint8_t *decrypt_key; | ||
| 47 | int decrypt_keylen; | ||
| 48 | uint8_t *decrypt_iv; | ||
| 49 | int decrypt_ivlen; | ||
| 50 | uint8_t *encrypt_key; | ||
| 51 | int encrypt_keylen; | ||
| 52 | uint8_t *encrypt_iv; | ||
| 53 | int encrypt_ivlen; | ||
| 54 | struct AVAES *aes_decrypt; | ||
| 55 | struct AVAES *aes_encrypt; | ||
| 56 | uint8_t *write_buf; | ||
| 57 | unsigned int write_buf_size; | ||
| 58 | uint8_t pad[BLOCKSIZE]; | ||
| 59 | int pad_len; | ||
| 60 | } CryptoContext; | ||
| 61 | |||
| 62 | #define OFFSET(x) offsetof(CryptoContext, x) | ||
| 63 | #define D AV_OPT_FLAG_DECODING_PARAM | ||
| 64 | #define E AV_OPT_FLAG_ENCODING_PARAM | ||
| 65 | static const AVOption crypto_options[] = { | ||
| 66 | {"key", "AES encryption/decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, .flags = D|E }, | ||
| 67 | {"iv", "AES encryption/decryption initialization vector", OFFSET(iv), AV_OPT_TYPE_BINARY, .flags = D|E }, | ||
| 68 | {"decryption_key", "AES decryption key", OFFSET(decrypt_key), AV_OPT_TYPE_BINARY, .flags = D }, | ||
| 69 | {"decryption_iv", "AES decryption initialization vector", OFFSET(decrypt_iv), AV_OPT_TYPE_BINARY, .flags = D }, | ||
| 70 | {"encryption_key", "AES encryption key", OFFSET(encrypt_key), AV_OPT_TYPE_BINARY, .flags = E }, | ||
| 71 | {"encryption_iv", "AES encryption initialization vector", OFFSET(encrypt_iv), AV_OPT_TYPE_BINARY, .flags = E }, | ||
| 72 | { NULL } | ||
| 73 | }; | ||
| 74 | |||
| 75 | static const AVClass crypto_class = { | ||
| 76 | .class_name = "crypto", | ||
| 77 | .item_name = av_default_item_name, | ||
| 78 | .option = crypto_options, | ||
| 79 | .version = LIBAVUTIL_VERSION_INT, | ||
| 80 | }; | ||
| 81 | |||
| 82 | ✗ | static int set_aes_arg(URLContext *h, uint8_t **buf, int *buf_len, | |
| 83 | uint8_t *default_buf, int default_buf_len, | ||
| 84 | const char *desc) | ||
| 85 | { | ||
| 86 | ✗ | if (!*buf_len) { | |
| 87 | ✗ | if (!default_buf_len) { | |
| 88 | ✗ | av_log(h, AV_LOG_ERROR, "%s not set\n", desc); | |
| 89 | ✗ | return AVERROR(EINVAL); | |
| 90 | ✗ | } else if (default_buf_len != BLOCKSIZE) { | |
| 91 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 92 | "invalid %s size (%d bytes, block size is %d)\n", | ||
| 93 | desc, default_buf_len, BLOCKSIZE); | ||
| 94 | ✗ | return AVERROR(EINVAL); | |
| 95 | } | ||
| 96 | ✗ | *buf = av_memdup(default_buf, default_buf_len); | |
| 97 | ✗ | if (!*buf) | |
| 98 | ✗ | return AVERROR(ENOMEM); | |
| 99 | ✗ | *buf_len = default_buf_len; | |
| 100 | ✗ | } else if (*buf_len != BLOCKSIZE) { | |
| 101 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 102 | "invalid %s size (%d bytes, block size is %d)\n", | ||
| 103 | desc, *buf_len, BLOCKSIZE); | ||
| 104 | ✗ | return AVERROR(EINVAL); | |
| 105 | } | ||
| 106 | ✗ | return 0; | |
| 107 | } | ||
| 108 | |||
| 109 | ✗ | static int crypto_open2(URLContext *h, const char *uri, int flags, AVDictionary **options) | |
| 110 | { | ||
| 111 | const char *nested_url; | ||
| 112 | ✗ | int ret = 0; | |
| 113 | ✗ | CryptoContext *c = h->priv_data; | |
| 114 | ✗ | c->flags = flags; | |
| 115 | |||
| 116 | ✗ | if (!av_strstart(uri, "crypto+", &nested_url) && | |
| 117 | ✗ | !av_strstart(uri, "crypto:", &nested_url)) { | |
| 118 | ✗ | av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri); | |
| 119 | ✗ | ret = AVERROR(EINVAL); | |
| 120 | ✗ | goto err; | |
| 121 | } | ||
| 122 | |||
| 123 | ✗ | if (flags & AVIO_FLAG_READ) { | |
| 124 | ✗ | if ((ret = set_aes_arg(h, &c->decrypt_key, &c->decrypt_keylen, | |
| 125 | c->key, c->keylen, "decryption key")) < 0) | ||
| 126 | ✗ | goto err; | |
| 127 | ✗ | if ((ret = set_aes_arg(h, &c->decrypt_iv, &c->decrypt_ivlen, | |
| 128 | c->iv, c->ivlen, "decryption IV")) < 0) | ||
| 129 | ✗ | goto err; | |
| 130 | } | ||
| 131 | |||
| 132 | ✗ | if (flags & AVIO_FLAG_WRITE) { | |
| 133 | ✗ | if ((ret = set_aes_arg(h, &c->encrypt_key, &c->encrypt_keylen, | |
| 134 | c->key, c->keylen, "encryption key")) < 0) | ||
| 135 | ✗ | if (ret < 0) | |
| 136 | ✗ | goto err; | |
| 137 | ✗ | if ((ret = set_aes_arg(h, &c->encrypt_iv, &c->encrypt_ivlen, | |
| 138 | c->iv, c->ivlen, "encryption IV")) < 0) | ||
| 139 | ✗ | goto err; | |
| 140 | } | ||
| 141 | |||
| 142 | ✗ | if ((ret = ffurl_open_whitelist(&c->hd, nested_url, flags, | |
| 143 | ✗ | &h->interrupt_callback, options, | |
| 144 | h->protocol_whitelist, h->protocol_blacklist, h)) < 0) { | ||
| 145 | ✗ | av_log(h, AV_LOG_ERROR, "Unable to open resource: %s\n", nested_url); | |
| 146 | ✗ | goto err; | |
| 147 | } | ||
| 148 | |||
| 149 | ✗ | if (flags & AVIO_FLAG_READ) { | |
| 150 | ✗ | c->aes_decrypt = av_aes_alloc(); | |
| 151 | ✗ | if (!c->aes_decrypt) { | |
| 152 | ✗ | ret = AVERROR(ENOMEM); | |
| 153 | ✗ | goto err; | |
| 154 | } | ||
| 155 | ✗ | ret = av_aes_init(c->aes_decrypt, c->decrypt_key, BLOCKSIZE * 8, 1); | |
| 156 | ✗ | if (ret < 0) | |
| 157 | ✗ | goto err; | |
| 158 | |||
| 159 | // pass back information about the context we opened | ||
| 160 | ✗ | if (c->hd->is_streamed) | |
| 161 | ✗ | h->is_streamed = c->hd->is_streamed; | |
| 162 | } | ||
| 163 | |||
| 164 | ✗ | if (flags & AVIO_FLAG_WRITE) { | |
| 165 | ✗ | c->aes_encrypt = av_aes_alloc(); | |
| 166 | ✗ | if (!c->aes_encrypt) { | |
| 167 | ✗ | ret = AVERROR(ENOMEM); | |
| 168 | ✗ | goto err; | |
| 169 | } | ||
| 170 | ✗ | ret = av_aes_init(c->aes_encrypt, c->encrypt_key, BLOCKSIZE * 8, 0); | |
| 171 | ✗ | if (ret < 0) | |
| 172 | ✗ | goto err; | |
| 173 | // for write, we must be streamed | ||
| 174 | // - linear write only for crypto aes-128-cbc | ||
| 175 | ✗ | h->is_streamed = 1; | |
| 176 | } | ||
| 177 | |||
| 178 | ✗ | err: | |
| 179 | ✗ | return ret; | |
| 180 | } | ||
| 181 | |||
| 182 | ✗ | static int crypto_read(URLContext *h, uint8_t *buf, int size) | |
| 183 | { | ||
| 184 | ✗ | CryptoContext *c = h->priv_data; | |
| 185 | int blocks; | ||
| 186 | ✗ | retry: | |
| 187 | ✗ | if (c->outdata > 0) { | |
| 188 | ✗ | size = FFMIN(size, c->outdata); | |
| 189 | ✗ | memcpy(buf, c->outptr, size); | |
| 190 | ✗ | c->outptr += size; | |
| 191 | ✗ | c->outdata -= size; | |
| 192 | ✗ | c->position = c->position + size; | |
| 193 | ✗ | return size; | |
| 194 | } | ||
| 195 | // We avoid using the last block until we've found EOF, | ||
| 196 | // since we'll remove PKCS7 padding at the end. So make | ||
| 197 | // sure we've got at least 2 blocks, so we can decrypt | ||
| 198 | // at least one. | ||
| 199 | ✗ | while (c->indata - c->indata_used < 2*BLOCKSIZE) { | |
| 200 | ✗ | int n = ffurl_read(c->hd, c->inbuffer + c->indata, | |
| 201 | ✗ | sizeof(c->inbuffer) - c->indata); | |
| 202 | ✗ | if (n <= 0) { | |
| 203 | ✗ | c->eof = 1; | |
| 204 | ✗ | break; | |
| 205 | } | ||
| 206 | ✗ | c->indata += n; | |
| 207 | } | ||
| 208 | ✗ | blocks = (c->indata - c->indata_used) / BLOCKSIZE; | |
| 209 | ✗ | if (!blocks) | |
| 210 | ✗ | return AVERROR_EOF; | |
| 211 | ✗ | if (!c->eof) | |
| 212 | ✗ | blocks--; | |
| 213 | ✗ | av_aes_crypt(c->aes_decrypt, c->outbuffer, c->inbuffer + c->indata_used, | |
| 214 | blocks, c->decrypt_iv, 1); | ||
| 215 | ✗ | c->outdata = BLOCKSIZE * blocks; | |
| 216 | ✗ | c->outptr = c->outbuffer; | |
| 217 | ✗ | c->indata_used += BLOCKSIZE * blocks; | |
| 218 | ✗ | if (c->indata_used >= sizeof(c->inbuffer)/2) { | |
| 219 | ✗ | memmove(c->inbuffer, c->inbuffer + c->indata_used, | |
| 220 | ✗ | c->indata - c->indata_used); | |
| 221 | ✗ | c->indata -= c->indata_used; | |
| 222 | ✗ | c->indata_used = 0; | |
| 223 | } | ||
| 224 | ✗ | if (c->eof) { | |
| 225 | // Remove PKCS7 padding at the end | ||
| 226 | ✗ | int padding = c->outbuffer[c->outdata - 1]; | |
| 227 | ✗ | c->outdata -= padding; | |
| 228 | } | ||
| 229 | ✗ | goto retry; | |
| 230 | } | ||
| 231 | |||
| 232 | ✗ | static int64_t crypto_seek(URLContext *h, int64_t pos, int whence) | |
| 233 | { | ||
| 234 | ✗ | CryptoContext *c = h->priv_data; | |
| 235 | int64_t block; | ||
| 236 | int64_t newpos; | ||
| 237 | |||
| 238 | ✗ | if (c->flags & AVIO_FLAG_WRITE) { | |
| 239 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 240 | "Crypto: seek not supported for write\r\n"); | ||
| 241 | /* seems the most appropriate error to return */ | ||
| 242 | ✗ | return AVERROR(ESPIPE); | |
| 243 | } | ||
| 244 | |||
| 245 | // reset eof, else we won't read it correctly if we already hit eof. | ||
| 246 | ✗ | c->eof = 0; | |
| 247 | |||
| 248 | ✗ | switch (whence) { | |
| 249 | ✗ | case SEEK_SET: | |
| 250 | ✗ | break; | |
| 251 | ✗ | case SEEK_CUR: | |
| 252 | ✗ | pos = pos + c->position; | |
| 253 | ✗ | break; | |
| 254 | ✗ | case SEEK_END: | |
| 255 | ✗ | newpos = ffurl_seek( c->hd, pos, AVSEEK_SIZE ); | |
| 256 | ✗ | if (newpos < 0) { | |
| 257 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 258 | "Crypto: seek_end - can't get file size (pos=%"PRId64")\r\n", pos); | ||
| 259 | ✗ | return newpos; | |
| 260 | } | ||
| 261 | ✗ | pos = newpos - pos; | |
| 262 | ✗ | break; | |
| 263 | ✗ | case AVSEEK_SIZE: | |
| 264 | ✗ | return ffurl_seek( c->hd, pos, AVSEEK_SIZE ); | |
| 265 | ✗ | default: | |
| 266 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 267 | "Crypto: no support for seek where 'whence' is %d\r\n", whence); | ||
| 268 | ✗ | return AVERROR(EINVAL); | |
| 269 | } | ||
| 270 | |||
| 271 | ✗ | c->outdata = 0; | |
| 272 | ✗ | c->indata = 0; | |
| 273 | ✗ | c->indata_used = 0; | |
| 274 | ✗ | c->outptr = c->outbuffer; | |
| 275 | |||
| 276 | // identify the block containing the IV for the | ||
| 277 | // next block we will decrypt | ||
| 278 | ✗ | block = pos/BLOCKSIZE; | |
| 279 | ✗ | if (block == 0) { | |
| 280 | // restore the iv to the seed one - this is the iv for the FIRST block | ||
| 281 | ✗ | memcpy( c->decrypt_iv, c->iv, c->ivlen ); | |
| 282 | ✗ | c->position = 0; | |
| 283 | } else { | ||
| 284 | // else, go back one block - we will get av_cyrpt to read this block | ||
| 285 | // which it will then store use as the iv. | ||
| 286 | // note that the DECRYPTED result will not be correct, | ||
| 287 | // but will be discarded | ||
| 288 | ✗ | block--; | |
| 289 | ✗ | c->position = (block * BLOCKSIZE); | |
| 290 | } | ||
| 291 | |||
| 292 | ✗ | newpos = ffurl_seek( c->hd, c->position, SEEK_SET ); | |
| 293 | ✗ | if (newpos < 0) { | |
| 294 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 295 | "Crypto: nested protocol no support for seek or seek failed\n"); | ||
| 296 | ✗ | return newpos; | |
| 297 | } | ||
| 298 | |||
| 299 | // read and discard from here up to required position | ||
| 300 | // (which will set the iv correctly to it). | ||
| 301 | ✗ | if (pos - c->position) { | |
| 302 | uint8_t buff[BLOCKSIZE*2]; // maximum size of pos-c->position | ||
| 303 | ✗ | int len = pos - c->position; | |
| 304 | int res; | ||
| 305 | |||
| 306 | ✗ | while (len > 0) { | |
| 307 | // note: this may not return all the bytes first time | ||
| 308 | ✗ | res = crypto_read(h, buff, len); | |
| 309 | ✗ | if (res < 0) | |
| 310 | ✗ | break; | |
| 311 | ✗ | len -= res; | |
| 312 | } | ||
| 313 | |||
| 314 | // if we did not get all the bytes | ||
| 315 | ✗ | if (len != 0) { | |
| 316 | ✗ | av_log(h, AV_LOG_ERROR, | |
| 317 | "Crypto: discard read did not get all the bytes (%d remain) - read returned (%d)-%s\n", | ||
| 318 | ✗ | len, res, av_err2str(res)); | |
| 319 | ✗ | return AVERROR(EINVAL); | |
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | ✗ | return c->position; | |
| 324 | } | ||
| 325 | |||
| 326 | ✗ | static int crypto_write(URLContext *h, const unsigned char *buf, int size) | |
| 327 | { | ||
| 328 | ✗ | CryptoContext *c = h->priv_data; | |
| 329 | int total_size, blocks, pad_len, out_size; | ||
| 330 | ✗ | int ret = 0; | |
| 331 | |||
| 332 | ✗ | total_size = size + c->pad_len; | |
| 333 | ✗ | pad_len = total_size % BLOCKSIZE; | |
| 334 | ✗ | out_size = total_size - pad_len; | |
| 335 | ✗ | blocks = out_size / BLOCKSIZE; | |
| 336 | |||
| 337 | ✗ | if (out_size) { | |
| 338 | ✗ | av_fast_malloc(&c->write_buf, &c->write_buf_size, out_size); | |
| 339 | |||
| 340 | ✗ | if (!c->write_buf) | |
| 341 | ✗ | return AVERROR(ENOMEM); | |
| 342 | |||
| 343 | ✗ | if (c->pad_len) { | |
| 344 | ✗ | memcpy(&c->pad[c->pad_len], buf, BLOCKSIZE - c->pad_len); | |
| 345 | ✗ | av_aes_crypt(c->aes_encrypt, c->write_buf, c->pad, 1, c->encrypt_iv, 0); | |
| 346 | ✗ | blocks--; | |
| 347 | } | ||
| 348 | |||
| 349 | ✗ | av_aes_crypt(c->aes_encrypt, | |
| 350 | ✗ | &c->write_buf[c->pad_len ? BLOCKSIZE : 0], | |
| 351 | ✗ | &buf[c->pad_len ? BLOCKSIZE - c->pad_len : 0], | |
| 352 | blocks, c->encrypt_iv, 0); | ||
| 353 | |||
| 354 | ✗ | ret = ffurl_write(c->hd, c->write_buf, out_size); | |
| 355 | ✗ | if (ret < 0) | |
| 356 | ✗ | return ret; | |
| 357 | |||
| 358 | ✗ | memcpy(c->pad, &buf[size - pad_len], pad_len); | |
| 359 | } else | ||
| 360 | ✗ | memcpy(&c->pad[c->pad_len], buf, size); | |
| 361 | |||
| 362 | ✗ | c->pad_len = pad_len; | |
| 363 | |||
| 364 | ✗ | return size; | |
| 365 | } | ||
| 366 | |||
| 367 | ✗ | static int crypto_close(URLContext *h) | |
| 368 | { | ||
| 369 | ✗ | CryptoContext *c = h->priv_data; | |
| 370 | ✗ | int ret = 0; | |
| 371 | |||
| 372 | ✗ | if (c->aes_encrypt) { | |
| 373 | uint8_t out_buf[BLOCKSIZE]; | ||
| 374 | ✗ | int pad = BLOCKSIZE - c->pad_len; | |
| 375 | |||
| 376 | ✗ | memset(&c->pad[c->pad_len], pad, pad); | |
| 377 | ✗ | av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0); | |
| 378 | ✗ | ret = ffurl_write(c->hd, out_buf, BLOCKSIZE); | |
| 379 | } | ||
| 380 | |||
| 381 | ✗ | ffurl_closep(&c->hd); | |
| 382 | ✗ | av_freep(&c->aes_decrypt); | |
| 383 | ✗ | av_freep(&c->aes_encrypt); | |
| 384 | ✗ | av_freep(&c->write_buf); | |
| 385 | ✗ | return ret; | |
| 386 | } | ||
| 387 | |||
| 388 | const URLProtocol ff_crypto_protocol = { | ||
| 389 | .name = "crypto", | ||
| 390 | .url_open2 = crypto_open2, | ||
| 391 | .url_seek = crypto_seek, | ||
| 392 | .url_read = crypto_read, | ||
| 393 | .url_write = crypto_write, | ||
| 394 | .url_close = crypto_close, | ||
| 395 | .priv_data_size = sizeof(CryptoContext), | ||
| 396 | .priv_data_class = &crypto_class, | ||
| 397 | .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME, | ||
| 398 | }; | ||
| 399 |