mov: Avoid memcmp of uninitialised data
[libav.git] / libavformat / crypto.c
CommitLineData
6e4f70a8
MS
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
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
daf8cf35 32typedef struct CryptoContext {
6e4f70a8
MS
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 int eof;
40 uint8_t *key;
41 int keylen;
42 uint8_t *iv;
43 int ivlen;
d860a3cc
CS
44 uint8_t *decrypt_key;
45 int decrypt_keylen;
46 uint8_t *decrypt_iv;
47 int decrypt_ivlen;
48 uint8_t *encrypt_key;
49 int encrypt_keylen;
50 uint8_t *encrypt_iv;
51 int encrypt_ivlen;
52 struct AVAES *aes_decrypt;
53 struct AVAES *aes_encrypt;
54 uint8_t *write_buf;
55 unsigned int write_buf_size;
56 uint8_t pad[BLOCKSIZE];
57 int pad_len;
6e4f70a8
MS
58} CryptoContext;
59
60#define OFFSET(x) offsetof(CryptoContext, x)
dc86ca1a 61#define D AV_OPT_FLAG_DECODING_PARAM
d860a3cc 62#define E AV_OPT_FLAG_ENCODING_PARAM
6e4f70a8 63static const AVOption options[] = {
d860a3cc
CS
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 },
6e4f70a8
MS
70 { NULL }
71};
72
73static const AVClass crypto_class = {
f0029cbc
AK
74 .class_name = "crypto",
75 .item_name = av_default_item_name,
76 .option = options,
77 .version = LIBAVUTIL_VERSION_INT,
6e4f70a8
MS
78};
79
d860a3cc
CS
80static int set_aes_arg(URLContext *h, 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(h, AV_LOG_ERROR, "%s not set\n", desc);
87 return AVERROR(EINVAL);
88 } else if (default_buf_len != BLOCKSIZE) {
89 av_log(h, AV_LOG_ERROR,
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_malloc(default_buf_len);
95 if (!*buf)
96 return AVERROR(ENOMEM);
97 memcpy(*buf, default_buf, default_buf_len);
98 *buf_len = default_buf_len;
99 } else if (*buf_len != BLOCKSIZE) {
100 av_log(h, AV_LOG_ERROR,
101 "invalid %s size (%d bytes, block size is %d)\n",
102 desc, *buf_len, BLOCKSIZE);
103 return AVERROR(EINVAL);
104 }
105 return 0;
106}
107
6e4f70a8
MS
108static int crypto_open(URLContext *h, const char *uri, int flags)
109{
110 const char *nested_url;
eaa8c1f9 111 int ret = 0;
6e4f70a8
MS
112 CryptoContext *c = h->priv_data;
113
114 if (!av_strstart(uri, "crypto+", &nested_url) &&
115 !av_strstart(uri, "crypto:", &nested_url)) {
116 av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
117 ret = AVERROR(EINVAL);
118 goto err;
119 }
120
d860a3cc
CS
121 if (flags & AVIO_FLAG_READ) {
122 if ((ret = set_aes_arg(h, &c->decrypt_key, &c->decrypt_keylen,
123 c->key, c->keylen, "decryption key")) < 0)
124 goto err;
125 if ((ret = set_aes_arg(h, &c->decrypt_iv, &c->decrypt_ivlen,
126 c->iv, c->ivlen, "decryption IV")) < 0)
127 goto err;
6e4f70a8 128 }
d860a3cc 129
6e4f70a8 130 if (flags & AVIO_FLAG_WRITE) {
d860a3cc
CS
131 if ((ret = set_aes_arg(h, &c->encrypt_key, &c->encrypt_keylen,
132 c->key, c->keylen, "encryption key")) < 0)
133 if (ret < 0)
134 goto err;
135 if ((ret = set_aes_arg(h, &c->encrypt_iv, &c->encrypt_ivlen,
136 c->iv, c->ivlen, "encryption IV")) < 0)
137 goto err;
6e4f70a8 138 }
d860a3cc
CS
139
140 if ((ret = ffurl_open(&c->hd, nested_url, flags,
fab8156b 141 &h->interrupt_callback, NULL, h->protocols, h)) < 0) {
d860a3cc 142 av_log(h, AV_LOG_ERROR, "Unable to open resource: %s\n", nested_url);
6e4f70a8
MS
143 goto err;
144 }
d860a3cc
CS
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;
6e4f70a8
MS
155 }
156
d860a3cc
CS
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 }
6e4f70a8
MS
167
168 h->is_streamed = 1;
169
6e4f70a8 170err:
6e4f70a8
MS
171 return ret;
172}
173
174static int crypto_read(URLContext *h, uint8_t *buf, int size)
175{
176 CryptoContext *c = h->priv_data;
177 int blocks;
178retry:
179 if (c->outdata > 0) {
180 size = FFMIN(size, c->outdata);
181 memcpy(buf, c->outptr, size);
182 c->outptr += size;
183 c->outdata -= size;
184 return size;
185 }
186 // We avoid using the last block until we've found EOF,
187 // since we'll remove PKCS7 padding at the end. So make
188 // sure we've got at least 2 blocks, so we can decrypt
189 // at least one.
190 while (c->indata - c->indata_used < 2*BLOCKSIZE) {
191 int n = ffurl_read(c->hd, c->inbuffer + c->indata,
192 sizeof(c->inbuffer) - c->indata);
193 if (n <= 0) {
194 c->eof = 1;
195 break;
196 }
197 c->indata += n;
198 }
199 blocks = (c->indata - c->indata_used) / BLOCKSIZE;
200 if (!blocks)
201 return AVERROR_EOF;
202 if (!c->eof)
203 blocks--;
d860a3cc
CS
204 av_aes_crypt(c->aes_decrypt, c->outbuffer, c->inbuffer + c->indata_used,
205 blocks, c->decrypt_iv, 1);
6e4f70a8
MS
206 c->outdata = BLOCKSIZE * blocks;
207 c->outptr = c->outbuffer;
208 c->indata_used += BLOCKSIZE * blocks;
209 if (c->indata_used >= sizeof(c->inbuffer)/2) {
210 memmove(c->inbuffer, c->inbuffer + c->indata_used,
211 c->indata - c->indata_used);
212 c->indata -= c->indata_used;
213 c->indata_used = 0;
214 }
215 if (c->eof) {
216 // Remove PKCS7 padding at the end
217 int padding = c->outbuffer[c->outdata - 1];
218 c->outdata -= padding;
219 }
220 goto retry;
221}
222
d860a3cc
CS
223static int crypto_write(URLContext *h, const uint8_t *buf, int size)
224{
225 CryptoContext *c = h->priv_data;
226 int total_size, blocks, pad_len, out_size;
227 int ret = 0;
228
229 total_size = size + c->pad_len;
230 pad_len = total_size % BLOCKSIZE;
231 out_size = total_size - pad_len;
232 blocks = out_size / BLOCKSIZE;
233
234 if (out_size) {
235 av_fast_malloc(&c->write_buf, &c->write_buf_size, out_size);
236
237 if (!c->write_buf)
238 return AVERROR(ENOMEM);
239
240 if (c->pad_len) {
241 memcpy(&c->pad[c->pad_len], buf, BLOCKSIZE - c->pad_len);
242 av_aes_crypt(c->aes_encrypt, c->write_buf, c->pad, 1, c->encrypt_iv, 0);
243 blocks--;
244 }
245
246 av_aes_crypt(c->aes_encrypt,
247 &c->write_buf[c->pad_len ? BLOCKSIZE : 0],
248 &buf[c->pad_len ? BLOCKSIZE - c->pad_len : 0],
249 blocks, c->encrypt_iv, 0);
250
251 ret = ffurl_write(c->hd, c->write_buf, out_size);
252 if (ret < 0)
253 return ret;
254
255 memcpy(c->pad, &buf[size - pad_len], pad_len);
256 } else
257 memcpy(&c->pad[c->pad_len], buf, size);
258
259 c->pad_len = pad_len;
260
261 return size;
262}
263
6e4f70a8
MS
264static int crypto_close(URLContext *h)
265{
266 CryptoContext *c = h->priv_data;
d860a3cc
CS
267 int ret = 0;
268
269 if (c->aes_encrypt) {
270 uint8_t out_buf[BLOCKSIZE];
271 int pad = BLOCKSIZE - c->pad_len;
272
273 memset(&c->pad[c->pad_len], pad, pad);
274 av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
275 ret = ffurl_write(c->hd, out_buf, BLOCKSIZE);
276 }
277
6e4f70a8
MS
278 if (c->hd)
279 ffurl_close(c->hd);
d860a3cc
CS
280 av_freep(&c->aes_decrypt);
281 av_freep(&c->aes_encrypt);
282 av_freep(&c->write_buf);
283 return ret;
6e4f70a8
MS
284}
285
2758cded 286const URLProtocol ff_crypto_protocol = {
6e4f70a8
MS
287 .name = "crypto",
288 .url_open = crypto_open,
289 .url_read = crypto_read,
d860a3cc 290 .url_write = crypto_write,
6e4f70a8
MS
291 .url_close = crypto_close,
292 .priv_data_size = sizeof(CryptoContext),
293 .priv_data_class = &crypto_class,
294 .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
295};