FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crypto.c
Go to the documentation of this file.
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 "avformat.h"
23 #include "libavutil/aes.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/opt.h"
26 #include "internal.h"
27 #include "url.h"
28 
29 #define MAX_BUFFER_BLOCKS 150
30 #define BLOCKSIZE 16
31 
32 typedef struct CryptoContext {
33  const AVClass *class;
39  int eof;
41  int keylen;
43  int ivlen;
52  struct AVAES *aes_decrypt;
53  struct AVAES *aes_encrypt;
54 
56  int pad_len;
57 
59 
60 #define OFFSET(x) offsetof(CryptoContext, x)
61 #define D AV_OPT_FLAG_DECODING_PARAM
62 #define E AV_OPT_FLAG_ENCODING_PARAM
63 static const AVOption options[] = {
64  {"key", "AES encryption/decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, .flags = D|E },
65  {"iv", "AES encryption/decryption initialization vector", OFFSET(iv), AV_OPT_TYPE_BINARY, .flags = D|E },
66  {"decryption_key", "AES decryption key", OFFSET(decrypt_key), AV_OPT_TYPE_BINARY, .flags = D },
67  {"decryption_iv", "AES decryption initialization vector", OFFSET(decrypt_iv), AV_OPT_TYPE_BINARY, .flags = D },
68  {"encryption_key", "AES encryption key", OFFSET(encrypt_key), AV_OPT_TYPE_BINARY, .flags = E },
69  {"encryption_iv", "AES encryption initialization vector", OFFSET(encrypt_iv), AV_OPT_TYPE_BINARY, .flags = E },
70  { NULL }
71 };
72 
73 static const AVClass crypto_class = {
74  .class_name = "crypto",
75  .item_name = av_default_item_name,
76  .option = options,
77  .version = LIBAVUTIL_VERSION_INT,
78 };
79 
80 static int set_aes_arg(CryptoContext *c, uint8_t **buf, int *buf_len,
81  uint8_t *default_buf, int default_buf_len,
82  const char *desc)
83 {
84  if (!*buf_len) {
85  if (!default_buf_len) {
86  av_log(c, AV_LOG_ERROR, "%s not set\n", desc);
87  return AVERROR(EINVAL);
88  } else if (default_buf_len != BLOCKSIZE) {
90  "invalid %s size (%d bytes, block size is %d)\n",
91  desc, default_buf_len, BLOCKSIZE);
92  return AVERROR(EINVAL);
93  }
94  *buf = av_memdup(default_buf, default_buf_len);
95  if (!*buf)
96  return AVERROR(ENOMEM);
97  *buf_len = default_buf_len;
98  } else if (*buf_len != BLOCKSIZE) {
100  "invalid %s size (%d bytes, block size is %d)\n",
101  desc, *buf_len, BLOCKSIZE);
102  return AVERROR(EINVAL);
103  }
104  return 0;
105 }
106 
107 static int crypto_open2(URLContext *h, const char *uri, int flags, AVDictionary **options)
108 {
109  const char *nested_url;
110  int ret = 0;
111  CryptoContext *c = h->priv_data;
112 
113  if (!av_strstart(uri, "crypto+", &nested_url) &&
114  !av_strstart(uri, "crypto:", &nested_url)) {
115  av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
116  ret = AVERROR(EINVAL);
117  goto err;
118  }
119 
120  if (flags & AVIO_FLAG_READ) {
121  if ((ret = set_aes_arg(c, &c->decrypt_key, &c->decrypt_keylen,
122  c->key, c->keylen, "decryption key")) < 0)
123  goto err;
124  if ((ret = set_aes_arg(c, &c->decrypt_iv, &c->decrypt_ivlen,
125  c->iv, c->ivlen, "decryption IV")) < 0)
126  goto err;
127  }
128 
129  if (flags & AVIO_FLAG_WRITE) {
130  if ((ret = set_aes_arg(c, &c->encrypt_key, &c->encrypt_keylen,
131  c->key, c->keylen, "encryption key")) < 0)
132  if (ret < 0)
133  goto err;
134  if ((ret = set_aes_arg(c, &c->encrypt_iv, &c->encrypt_ivlen,
135  c->iv, c->ivlen, "encryption IV")) < 0)
136  goto err;
137  }
138 
139  if ((ret = ffurl_open_whitelist(&c->hd, nested_url, flags,
140  &h->interrupt_callback, options,
141  h->protocol_whitelist, h->protocol_blacklist, h)) < 0) {
142  av_log(h, AV_LOG_ERROR, "Unable to open resource: %s\n", nested_url);
143  goto err;
144  }
145 
146  if (flags & AVIO_FLAG_READ) {
147  c->aes_decrypt = av_aes_alloc();
148  if (!c->aes_decrypt) {
149  ret = AVERROR(ENOMEM);
150  goto err;
151  }
152  ret = av_aes_init(c->aes_decrypt, c->decrypt_key, BLOCKSIZE*8, 1);
153  if (ret < 0)
154  goto err;
155  }
156 
157  if (flags & AVIO_FLAG_WRITE) {
158  c->aes_encrypt = av_aes_alloc();
159  if (!c->aes_encrypt) {
160  ret = AVERROR(ENOMEM);
161  goto err;
162  }
163  ret = av_aes_init(c->aes_encrypt, c->encrypt_key, BLOCKSIZE*8, 0);
164  if (ret < 0)
165  goto err;
166  }
167 
168  c->pad_len = 0;
169 
170  h->is_streamed = 1;
171 
172 err:
173  return ret;
174 }
175 
176 static int crypto_read(URLContext *h, uint8_t *buf, int size)
177 {
178  CryptoContext *c = h->priv_data;
179  int blocks;
180 retry:
181  if (c->outdata > 0) {
182  size = FFMIN(size, c->outdata);
183  memcpy(buf, c->outptr, size);
184  c->outptr += size;
185  c->outdata -= size;
186  return size;
187  }
188  // We avoid using the last block until we've found EOF,
189  // since we'll remove PKCS7 padding at the end. So make
190  // sure we've got at least 2 blocks, so we can decrypt
191  // at least one.
192  while (c->indata - c->indata_used < 2*BLOCKSIZE) {
193  int n = ffurl_read(c->hd, c->inbuffer + c->indata,
194  sizeof(c->inbuffer) - c->indata);
195  if (n <= 0) {
196  c->eof = 1;
197  break;
198  }
199  c->indata += n;
200  }
201  blocks = (c->indata - c->indata_used) / BLOCKSIZE;
202  if (!blocks)
203  return AVERROR_EOF;
204  if (!c->eof)
205  blocks--;
207  blocks, c->decrypt_iv, 1);
208  c->outdata = BLOCKSIZE * blocks;
209  c->outptr = c->outbuffer;
210  c->indata_used += BLOCKSIZE * blocks;
211  if (c->indata_used >= sizeof(c->inbuffer)/2) {
212  memmove(c->inbuffer, c->inbuffer + c->indata_used,
213  c->indata - c->indata_used);
214  c->indata -= c->indata_used;
215  c->indata_used = 0;
216  }
217  if (c->eof) {
218  // Remove PKCS7 padding at the end
219  int padding = c->outbuffer[c->outdata - 1];
220  c->outdata -= padding;
221  }
222  goto retry;
223 }
224 
225 static int crypto_write(URLContext *h, const unsigned char *buf, int size)
226 {
227  CryptoContext *c = h->priv_data;
228  int total_size, blocks, pad_len, out_size;
229  uint8_t *out_buf;
230  int ret = 0;
231 
232  total_size = size + c->pad_len;
233  pad_len = total_size % BLOCKSIZE;
234  out_size = total_size - pad_len;
235  blocks = out_size / BLOCKSIZE;
236 
237  if (out_size) {
238  out_buf = av_malloc(out_size);
239  if (!out_buf)
240  return AVERROR(ENOMEM);
241 
242  if (c->pad_len) {
243  memcpy(&c->pad[c->pad_len], buf, BLOCKSIZE - c->pad_len);
244  av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
245  blocks--;
246  }
247 
248  av_aes_crypt(c->aes_encrypt, &out_buf[c->pad_len ? BLOCKSIZE : 0],
249  &buf[c->pad_len ? BLOCKSIZE - c->pad_len: 0],
250  blocks, c->encrypt_iv, 0);
251 
252  ret = ffurl_write(c->hd, out_buf, out_size);
253  av_free(out_buf);
254  if (ret < 0)
255  return ret;
256 
257  memcpy(c->pad, &buf[size - pad_len], pad_len);
258  } else
259  memcpy(&c->pad[c->pad_len], buf, size);
260 
261  c->pad_len = pad_len;
262 
263  return size;
264 }
265 
267 {
268  CryptoContext *c = h->priv_data;
269  uint8_t out_buf[BLOCKSIZE];
270  int ret, pad;
271 
272  if (c->aes_encrypt) {
273  pad = BLOCKSIZE - c->pad_len;
274  memset(&c->pad[c->pad_len], pad, pad);
275  av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
276  if ((ret = ffurl_write(c->hd, out_buf, BLOCKSIZE)) < 0)
277  return ret;
278  }
279 
280  if (c->hd)
281  ffurl_close(c->hd);
282  av_freep(&c->aes_decrypt);
283  av_freep(&c->aes_encrypt);
284  return 0;
285 }
286 
288  .name = "crypto",
289  .url_open2 = crypto_open2,
290  .url_read = crypto_read,
291  .url_write = crypto_write,
292  .url_close = crypto_close,
293  .priv_data_size = sizeof(CryptoContext),
294  .priv_data_class = &crypto_class,
296 };
static int crypto_close(URLContext *h)
Definition: crypto.c:266
#define NULL
Definition: coverity.c:32
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:309
uint8_t * iv
Definition: crypto.c:42
AVOption.
Definition: opt.h:245
int ivlen
Definition: crypto.c:43
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:421
const char * desc
Definition: nvenc.c:89
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:45
int decrypt_ivlen
Definition: crypto.c:47
AVIOInterruptCB interrupt_callback
Definition: url.h:47
static int crypto_read(URLContext *h, uint8_t *buf, int size)
Definition: crypto.c:176
#define AVIO_FLAG_READ
read-only
Definition: avio.h:606
void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: aes.c:163
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:607
int decrypt_keylen
Definition: crypto.c:45
int out_size
Definition: movenc.c:55
int encrypt_ivlen
Definition: crypto.c:51
static const AVOption options[]
Definition: crypto.c:63
uint8_t inbuffer[BLOCKSIZE *MAX_BUFFER_BLOCKS]
Definition: crypto.c:35
uint8_t * outptr
Definition: crypto.c:37
uint8_t * encrypt_key
Definition: crypto.c:48
#define E
Definition: crypto.c:62
uint8_t outbuffer[BLOCKSIZE *MAX_BUFFER_BLOCKS]
Definition: crypto.c:35
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int keylen
Definition: crypto.c:41
uint8_t
#define av_malloc(s)
AVOptions.
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
static int crypto_open2(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: crypto.c:107
#define MAX_BUFFER_BLOCKS
Definition: crypto.c:29
#define BLOCKSIZE
Definition: crypto.c:30
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
struct AVAES * aes_decrypt
Definition: crypto.c:52
uint8_t * decrypt_key
Definition: crypto.c:44
int indata_used
Definition: crypto.c:38
const char * protocol_whitelist
Definition: url.h:49
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
uint8_t pad[BLOCKSIZE]
Definition: crypto.c:55
#define URL_PROTOCOL_FLAG_NESTED_SCHEME
Definition: url.h:33
int pad_len
Definition: crypto.c:56
int outdata
Definition: crypto.c:38
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:31
struct AVAES * aes_encrypt
Definition: crypto.c:53
uint8_t * encrypt_iv
Definition: crypto.c:50
#define D
Definition: crypto.c:61
uint8_t * key
Definition: crypto.c:40
#define FFMIN(a, b)
Definition: common.h:96
static const AVClass crypto_class
Definition: crypto.c:73
URLContext * hd
Definition: crypto.c:34
int n
Definition: avisynth_c.h:547
int indata
Definition: crypto.c:38
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
void * av_memdup(const void *p, size_t size)
Duplicate the buffer p.
Definition: mem.c:299
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:195
const char * protocol_blacklist
Definition: url.h:50
const URLProtocol ff_crypto_protocol
Definition: crypto.c:287
uint8_t * decrypt_iv
Definition: crypto.c:46
void * buf
Definition: avisynth_c.h:553
Definition: url.h:38
Describe the class of an AVClass context structure.
Definition: log.h:67
void * priv_data
Definition: url.h:41
static int crypto_write(URLContext *h, const unsigned char *buf, int size)
Definition: crypto.c:225
const char * name
Definition: url.h:54
static int flags
Definition: cpu.c:47
int ffurl_close(URLContext *h)
Definition: avio.c:467
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
Main libavformat public API header.
static int set_aes_arg(CryptoContext *c, uint8_t **buf, int *buf_len, uint8_t *default_buf, int default_buf_len, const char *desc)
Definition: crypto.c:80
static double c[64]
#define av_free(p)
#define OFFSET(x)
Definition: crypto.c:60
#define av_freep(p)
unbuffered private I/O API
int encrypt_keylen
Definition: crypto.c:49
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:407