b9d3e0326f037d3acb850e126da0ad96541286d5
[libav.git] / libavformat / crypto.c
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
32 typedef struct {
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;
44 struct AVAES *aes;
45 } CryptoContext;
46
47 #define OFFSET(x) offsetof(CryptoContext, x)
48 static const AVOption options[] = {
49 {"key", "AES decryption key", OFFSET(key), AV_OPT_TYPE_BINARY },
50 {"iv", "AES decryption initialization vector", OFFSET(iv), AV_OPT_TYPE_BINARY },
51 { NULL }
52 };
53
54 static const AVClass crypto_class = {
55 .class_name = "crypto",
56 .item_name = av_default_item_name,
57 .option = options,
58 .version = LIBAVUTIL_VERSION_INT,
59 };
60
61 static int crypto_open(URLContext *h, const char *uri, int flags)
62 {
63 const char *nested_url;
64 int ret;
65 CryptoContext *c = h->priv_data;
66
67 if (!av_strstart(uri, "crypto+", &nested_url) &&
68 !av_strstart(uri, "crypto:", &nested_url)) {
69 av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
70 ret = AVERROR(EINVAL);
71 goto err;
72 }
73
74 if (c->keylen < BLOCKSIZE || c->ivlen < BLOCKSIZE) {
75 av_log(h, AV_LOG_ERROR, "Key or IV not set\n");
76 ret = AVERROR(EINVAL);
77 goto err;
78 }
79 if (flags & AVIO_FLAG_WRITE) {
80 av_log(h, AV_LOG_ERROR, "Only decryption is supported currently\n");
81 ret = AVERROR(ENOSYS);
82 goto err;
83 }
84 if ((ret = ffurl_open(&c->hd, nested_url, AVIO_FLAG_READ)) < 0) {
85 av_log(h, AV_LOG_ERROR, "Unable to open input\n");
86 goto err;
87 }
88 c->aes = av_mallocz(av_aes_size);
89 if (!c->aes) {
90 ret = AVERROR(ENOMEM);
91 goto err;
92 }
93
94 av_aes_init(c->aes, c->key, 128, 1);
95
96 h->is_streamed = 1;
97
98 return 0;
99 err:
100 av_freep(&c->key);
101 av_freep(&c->iv);
102 return ret;
103 }
104
105 static int crypto_read(URLContext *h, uint8_t *buf, int size)
106 {
107 CryptoContext *c = h->priv_data;
108 int blocks;
109 retry:
110 if (c->outdata > 0) {
111 size = FFMIN(size, c->outdata);
112 memcpy(buf, c->outptr, size);
113 c->outptr += size;
114 c->outdata -= size;
115 return size;
116 }
117 // We avoid using the last block until we've found EOF,
118 // since we'll remove PKCS7 padding at the end. So make
119 // sure we've got at least 2 blocks, so we can decrypt
120 // at least one.
121 while (c->indata - c->indata_used < 2*BLOCKSIZE) {
122 int n = ffurl_read(c->hd, c->inbuffer + c->indata,
123 sizeof(c->inbuffer) - c->indata);
124 if (n <= 0) {
125 c->eof = 1;
126 break;
127 }
128 c->indata += n;
129 }
130 blocks = (c->indata - c->indata_used) / BLOCKSIZE;
131 if (!blocks)
132 return AVERROR_EOF;
133 if (!c->eof)
134 blocks--;
135 av_aes_crypt(c->aes, c->outbuffer, c->inbuffer + c->indata_used, blocks,
136 c->iv, 1);
137 c->outdata = BLOCKSIZE * blocks;
138 c->outptr = c->outbuffer;
139 c->indata_used += BLOCKSIZE * blocks;
140 if (c->indata_used >= sizeof(c->inbuffer)/2) {
141 memmove(c->inbuffer, c->inbuffer + c->indata_used,
142 c->indata - c->indata_used);
143 c->indata -= c->indata_used;
144 c->indata_used = 0;
145 }
146 if (c->eof) {
147 // Remove PKCS7 padding at the end
148 int padding = c->outbuffer[c->outdata - 1];
149 c->outdata -= padding;
150 }
151 goto retry;
152 }
153
154 static int crypto_close(URLContext *h)
155 {
156 CryptoContext *c = h->priv_data;
157 if (c->hd)
158 ffurl_close(c->hd);
159 av_freep(&c->aes);
160 av_freep(&c->key);
161 av_freep(&c->iv);
162 return 0;
163 }
164
165 URLProtocol ff_crypto_protocol = {
166 .name = "crypto",
167 .url_open = crypto_open,
168 .url_read = crypto_read,
169 .url_close = crypto_close,
170 .priv_data_size = sizeof(CryptoContext),
171 .priv_data_class = &crypto_class,
172 .flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
173 };