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