]> git.sesse.net Git - ffmpeg/blob - libavformat/tls_openssl.c
avcodec/vp6: clear dimensions on failed resolution change in vp6_parse_header()
[ffmpeg] / libavformat / tls_openssl.c
1 /*
2  * TLS/SSL Protocol
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 "internal.h"
24 #include "network.h"
25 #include "os_support.h"
26 #include "url.h"
27 #include "tls.h"
28 #include "libavcodec/internal.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/avutil.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/thread.h"
34
35 #include <openssl/bio.h>
36 #include <openssl/ssl.h>
37 #include <openssl/err.h>
38
39 static int openssl_init;
40
41 typedef struct TLSContext {
42     const AVClass *class;
43     TLSShared tls_shared;
44     SSL_CTX *ctx;
45     SSL *ssl;
46 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
47     BIO_METHOD* url_bio_method;
48 #endif
49 } TLSContext;
50
51 #if HAVE_THREADS
52 #include <openssl/crypto.h>
53 pthread_mutex_t *openssl_mutexes;
54 static void openssl_lock(int mode, int type, const char *file, int line)
55 {
56     if (mode & CRYPTO_LOCK)
57         pthread_mutex_lock(&openssl_mutexes[type]);
58     else
59         pthread_mutex_unlock(&openssl_mutexes[type]);
60 }
61 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
62 static unsigned long openssl_thread_id(void)
63 {
64     return (intptr_t) pthread_self();
65 }
66 #endif
67 #endif
68
69 static int url_bio_create(BIO *b)
70 {
71 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
72     BIO_set_init(b, 1);
73     BIO_set_data(b, NULL);
74     BIO_set_flags(b, 0);
75 #else
76     b->init = 1;
77     b->ptr = NULL;
78     b->flags = 0;
79 #endif
80     return 1;
81 }
82
83 static int url_bio_destroy(BIO *b)
84 {
85     return 1;
86 }
87
88 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
89 #define GET_BIO_DATA(x) BIO_get_data(x);
90 #else
91 #define GET_BIO_DATA(x) (x)->ptr;
92 #endif
93
94 static int url_bio_bread(BIO *b, char *buf, int len)
95 {
96     URLContext *h;
97     int ret;
98     h = GET_BIO_DATA(b);
99     ret = ffurl_read(h, buf, len);
100     if (ret >= 0)
101         return ret;
102     BIO_clear_retry_flags(b);
103     if (ret == AVERROR_EXIT)
104         return 0;
105     return -1;
106 }
107
108 static int url_bio_bwrite(BIO *b, const char *buf, int len)
109 {
110     URLContext *h;
111     int ret;
112     h = GET_BIO_DATA(b);
113     ret = ffurl_write(h, buf, len);
114     if (ret >= 0)
115         return ret;
116     BIO_clear_retry_flags(b);
117     if (ret == AVERROR_EXIT)
118         return 0;
119     return -1;
120 }
121
122 static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
123 {
124     if (cmd == BIO_CTRL_FLUSH) {
125         BIO_clear_retry_flags(b);
126         return 1;
127     }
128     return 0;
129 }
130
131 static int url_bio_bputs(BIO *b, const char *str)
132 {
133     return url_bio_bwrite(b, str, strlen(str));
134 }
135
136 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
137 static BIO_METHOD url_bio_method = {
138     .type = BIO_TYPE_SOURCE_SINK,
139     .name = "urlprotocol bio",
140     .bwrite = url_bio_bwrite,
141     .bread = url_bio_bread,
142     .bputs = url_bio_bputs,
143     .bgets = NULL,
144     .ctrl = url_bio_ctrl,
145     .create = url_bio_create,
146     .destroy = url_bio_destroy,
147 };
148 #endif
149
150 int ff_openssl_init(void)
151 {
152     avpriv_lock_avformat();
153     if (!openssl_init) {
154         SSL_library_init();
155         SSL_load_error_strings();
156 #if HAVE_THREADS
157         if (!CRYPTO_get_locking_callback()) {
158             int i;
159             openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks());
160             if (!openssl_mutexes) {
161                 avpriv_unlock_avformat();
162                 return AVERROR(ENOMEM);
163             }
164
165             for (i = 0; i < CRYPTO_num_locks(); i++)
166                 pthread_mutex_init(&openssl_mutexes[i], NULL);
167             CRYPTO_set_locking_callback(openssl_lock);
168 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
169             CRYPTO_set_id_callback(openssl_thread_id);
170 #endif
171         }
172 #endif
173     }
174     openssl_init++;
175     avpriv_unlock_avformat();
176
177     return 0;
178 }
179
180 void ff_openssl_deinit(void)
181 {
182     avpriv_lock_avformat();
183     openssl_init--;
184     if (!openssl_init) {
185 #if HAVE_THREADS
186         if (CRYPTO_get_locking_callback() == openssl_lock) {
187             int i;
188             CRYPTO_set_locking_callback(NULL);
189             for (i = 0; i < CRYPTO_num_locks(); i++)
190                 pthread_mutex_destroy(&openssl_mutexes[i]);
191             av_free(openssl_mutexes);
192         }
193 #endif
194     }
195     avpriv_unlock_avformat();
196 }
197
198 static int print_tls_error(URLContext *h, int ret)
199 {
200     av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
201     return AVERROR(EIO);
202 }
203
204 static int tls_close(URLContext *h)
205 {
206     TLSContext *c = h->priv_data;
207     if (c->ssl) {
208         SSL_shutdown(c->ssl);
209         SSL_free(c->ssl);
210     }
211     if (c->ctx)
212         SSL_CTX_free(c->ctx);
213     if (c->tls_shared.tcp)
214         ffurl_close(c->tls_shared.tcp);
215 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
216     if (c->url_bio_method)
217         BIO_meth_free(c->url_bio_method);
218 #endif
219     ff_openssl_deinit();
220     return 0;
221 }
222
223 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
224 {
225     TLSContext *p = h->priv_data;
226     TLSShared *c = &p->tls_shared;
227     BIO *bio;
228     int ret;
229
230     if ((ret = ff_openssl_init()) < 0)
231         return ret;
232
233     if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
234         goto fail;
235
236     // We want to support all versions of TLS >= 1.0, but not the deprecated
237     // and insecure SSLv2 and SSLv3.  Despite the name, SSLv23_*_method()
238     // enables support for all versions of SSL and TLS, and we then disable
239     // support for the old protocols immediately after creating the context.
240     p->ctx = SSL_CTX_new(c->listen ? SSLv23_server_method() : SSLv23_client_method());
241     if (!p->ctx) {
242         av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
243         ret = AVERROR(EIO);
244         goto fail;
245     }
246     SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
247     if (c->ca_file) {
248         if (!SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL))
249             av_log(h, AV_LOG_ERROR, "SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
250     }
251     if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
252         av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
253                c->cert_file, ERR_error_string(ERR_get_error(), NULL));
254         ret = AVERROR(EIO);
255         goto fail;
256     }
257     if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
258         av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
259                c->key_file, ERR_error_string(ERR_get_error(), NULL));
260         ret = AVERROR(EIO);
261         goto fail;
262     }
263     // Note, this doesn't check that the peer certificate actually matches
264     // the requested hostname.
265     if (c->verify)
266         SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
267     p->ssl = SSL_new(p->ctx);
268     if (!p->ssl) {
269         av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
270         ret = AVERROR(EIO);
271         goto fail;
272     }
273 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
274     p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
275     BIO_meth_set_write(p->url_bio_method, url_bio_bwrite);
276     BIO_meth_set_read(p->url_bio_method, url_bio_bread);
277     BIO_meth_set_puts(p->url_bio_method, url_bio_bputs);
278     BIO_meth_set_ctrl(p->url_bio_method, url_bio_ctrl);
279     BIO_meth_set_create(p->url_bio_method, url_bio_create);
280     BIO_meth_set_destroy(p->url_bio_method, url_bio_destroy);
281     bio = BIO_new(p->url_bio_method);
282     BIO_set_data(bio, c->tcp);
283 #else
284     bio = BIO_new(&url_bio_method);
285     bio->ptr = c->tcp;
286 #endif
287     SSL_set_bio(p->ssl, bio, bio);
288     if (!c->listen && !c->numerichost)
289         SSL_set_tlsext_host_name(p->ssl, c->host);
290     ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
291     if (ret == 0) {
292         av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
293         ret = AVERROR(EIO);
294         goto fail;
295     } else if (ret < 0) {
296         ret = print_tls_error(h, ret);
297         goto fail;
298     }
299
300     return 0;
301 fail:
302     tls_close(h);
303     return ret;
304 }
305
306 static int tls_read(URLContext *h, uint8_t *buf, int size)
307 {
308     TLSContext *c = h->priv_data;
309     int ret = SSL_read(c->ssl, buf, size);
310     if (ret > 0)
311         return ret;
312     if (ret == 0)
313         return AVERROR_EOF;
314     return print_tls_error(h, ret);
315 }
316
317 static int tls_write(URLContext *h, const uint8_t *buf, int size)
318 {
319     TLSContext *c = h->priv_data;
320     int ret = SSL_write(c->ssl, buf, size);
321     if (ret > 0)
322         return ret;
323     if (ret == 0)
324         return AVERROR_EOF;
325     return print_tls_error(h, ret);
326 }
327
328 static int tls_get_file_handle(URLContext *h)
329 {
330     TLSContext *c = h->priv_data;
331     return ffurl_get_file_handle(c->tls_shared.tcp);
332 }
333
334 static const AVOption options[] = {
335     TLS_COMMON_OPTIONS(TLSContext, tls_shared),
336     { NULL }
337 };
338
339 static const AVClass tls_class = {
340     .class_name = "tls",
341     .item_name  = av_default_item_name,
342     .option     = options,
343     .version    = LIBAVUTIL_VERSION_INT,
344 };
345
346 const URLProtocol ff_tls_openssl_protocol = {
347     .name           = "tls",
348     .url_open2      = tls_open,
349     .url_read       = tls_read,
350     .url_write      = tls_write,
351     .url_close      = tls_close,
352     .url_get_file_handle = tls_get_file_handle,
353     .priv_data_size = sizeof(TLSContext),
354     .flags          = URL_PROTOCOL_FLAG_NETWORK,
355     .priv_data_class = &tls_class,
356 };