3 * Copyright (c) 2011 Martin Storsjo
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "os_support.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"
35 #include <openssl/bio.h>
36 #include <openssl/ssl.h>
37 #include <openssl/err.h>
39 static int openssl_init;
41 typedef struct TLSContext {
46 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
47 BIO_METHOD* url_bio_method;
52 #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
53 #include <openssl/crypto.h>
54 pthread_mutex_t *openssl_mutexes;
55 static void openssl_lock(int mode, int type, const char *file, int line)
57 if (mode & CRYPTO_LOCK)
58 pthread_mutex_lock(&openssl_mutexes[type]);
60 pthread_mutex_unlock(&openssl_mutexes[type]);
62 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
63 static unsigned long openssl_thread_id(void)
65 return (intptr_t) pthread_self();
70 int ff_openssl_init(void)
74 /* OpenSSL 1.0.2 or below, then you would use SSL_library_init. If you are
75 * using OpenSSL 1.1.0 or above, then the library will initialize
76 * itself automatically.
77 * https://wiki.openssl.org/index.php/Library_Initialization
79 #if OPENSSL_VERSION_NUMBER < 0x10100000L
81 SSL_load_error_strings();
83 #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
84 if (!CRYPTO_get_locking_callback()) {
86 openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks());
87 if (!openssl_mutexes) {
89 return AVERROR(ENOMEM);
92 for (i = 0; i < CRYPTO_num_locks(); i++)
93 pthread_mutex_init(&openssl_mutexes[i], NULL);
94 CRYPTO_set_locking_callback(openssl_lock);
95 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
96 CRYPTO_set_id_callback(openssl_thread_id);
102 ff_unlock_avformat();
107 void ff_openssl_deinit(void)
112 #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
113 if (CRYPTO_get_locking_callback() == openssl_lock) {
115 CRYPTO_set_locking_callback(NULL);
116 for (i = 0; i < CRYPTO_num_locks(); i++)
117 pthread_mutex_destroy(&openssl_mutexes[i]);
118 av_free(openssl_mutexes);
122 ff_unlock_avformat();
125 static int print_tls_error(URLContext *h, int ret)
127 TLSContext *c = h->priv_data;
128 int printed = 0, e, averr = AVERROR(EIO);
129 if (h->flags & AVIO_FLAG_NONBLOCK) {
130 int err = SSL_get_error(c->ssl, ret);
131 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
132 return AVERROR(EAGAIN);
134 while ((e = ERR_get_error()) != 0) {
135 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(e, NULL));
139 av_log(h, AV_LOG_ERROR, "IO error: %s\n", av_err2str(c->io_err));
145 av_log(h, AV_LOG_ERROR, "Unknown error\n");
149 static int tls_close(URLContext *h)
151 TLSContext *c = h->priv_data;
153 SSL_shutdown(c->ssl);
157 SSL_CTX_free(c->ctx);
158 ffurl_closep(&c->tls_shared.tcp);
159 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
160 if (c->url_bio_method)
161 BIO_meth_free(c->url_bio_method);
167 static int url_bio_create(BIO *b)
169 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
171 BIO_set_data(b, NULL);
181 static int url_bio_destroy(BIO *b)
186 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
187 #define GET_BIO_DATA(x) BIO_get_data(x)
189 #define GET_BIO_DATA(x) (x)->ptr
192 static int url_bio_bread(BIO *b, char *buf, int len)
194 TLSContext *c = GET_BIO_DATA(b);
195 int ret = ffurl_read(c->tls_shared.tcp, buf, len);
198 BIO_clear_retry_flags(b);
199 if (ret == AVERROR_EXIT)
201 if (ret == AVERROR(EAGAIN))
202 BIO_set_retry_read(b);
208 static int url_bio_bwrite(BIO *b, const char *buf, int len)
210 TLSContext *c = GET_BIO_DATA(b);
211 int ret = ffurl_write(c->tls_shared.tcp, buf, len);
214 BIO_clear_retry_flags(b);
215 if (ret == AVERROR_EXIT)
217 if (ret == AVERROR(EAGAIN))
218 BIO_set_retry_write(b);
224 static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
226 if (cmd == BIO_CTRL_FLUSH) {
227 BIO_clear_retry_flags(b);
233 static int url_bio_bputs(BIO *b, const char *str)
235 return url_bio_bwrite(b, str, strlen(str));
238 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
239 static BIO_METHOD url_bio_method = {
240 .type = BIO_TYPE_SOURCE_SINK,
241 .name = "urlprotocol bio",
242 .bwrite = url_bio_bwrite,
243 .bread = url_bio_bread,
244 .bputs = url_bio_bputs,
246 .ctrl = url_bio_ctrl,
247 .create = url_bio_create,
248 .destroy = url_bio_destroy,
252 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
254 TLSContext *p = h->priv_data;
255 TLSShared *c = &p->tls_shared;
259 if ((ret = ff_openssl_init()) < 0)
262 if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
265 // We want to support all versions of TLS >= 1.0, but not the deprecated
266 // and insecure SSLv2 and SSLv3. Despite the name, SSLv23_*_method()
267 // enables support for all versions of SSL and TLS, and we then disable
268 // support for the old protocols immediately after creating the context.
269 p->ctx = SSL_CTX_new(c->listen ? SSLv23_server_method() : SSLv23_client_method());
271 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
275 SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
277 if (!SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL))
278 av_log(h, AV_LOG_ERROR, "SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
280 if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
281 av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
282 c->cert_file, ERR_error_string(ERR_get_error(), NULL));
286 if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
287 av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
288 c->key_file, ERR_error_string(ERR_get_error(), NULL));
292 // Note, this doesn't check that the peer certificate actually matches
293 // the requested hostname.
295 SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
296 p->ssl = SSL_new(p->ctx);
298 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
302 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
303 p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
304 BIO_meth_set_write(p->url_bio_method, url_bio_bwrite);
305 BIO_meth_set_read(p->url_bio_method, url_bio_bread);
306 BIO_meth_set_puts(p->url_bio_method, url_bio_bputs);
307 BIO_meth_set_ctrl(p->url_bio_method, url_bio_ctrl);
308 BIO_meth_set_create(p->url_bio_method, url_bio_create);
309 BIO_meth_set_destroy(p->url_bio_method, url_bio_destroy);
310 bio = BIO_new(p->url_bio_method);
311 BIO_set_data(bio, p);
313 bio = BIO_new(&url_bio_method);
316 SSL_set_bio(p->ssl, bio, bio);
317 if (!c->listen && !c->numerichost)
318 SSL_set_tlsext_host_name(p->ssl, c->host);
319 ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
321 av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
324 } else if (ret < 0) {
325 ret = print_tls_error(h, ret);
335 static int tls_read(URLContext *h, uint8_t *buf, int size)
337 TLSContext *c = h->priv_data;
339 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
340 c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
341 c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
342 ret = SSL_read(c->ssl, buf, size);
347 return print_tls_error(h, ret);
350 static int tls_write(URLContext *h, const uint8_t *buf, int size)
352 TLSContext *c = h->priv_data;
354 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
355 c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
356 c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
357 ret = SSL_write(c->ssl, buf, size);
362 return print_tls_error(h, ret);
365 static int tls_get_file_handle(URLContext *h)
367 TLSContext *c = h->priv_data;
368 return ffurl_get_file_handle(c->tls_shared.tcp);
371 static int tls_get_short_seek(URLContext *h)
373 TLSContext *s = h->priv_data;
374 return ffurl_get_short_seek(s->tls_shared.tcp);
377 static const AVOption options[] = {
378 TLS_COMMON_OPTIONS(TLSContext, tls_shared),
382 static const AVClass tls_class = {
384 .item_name = av_default_item_name,
386 .version = LIBAVUTIL_VERSION_INT,
389 const URLProtocol ff_tls_protocol = {
391 .url_open2 = tls_open,
392 .url_read = tls_read,
393 .url_write = tls_write,
394 .url_close = tls_close,
395 .url_get_file_handle = tls_get_file_handle,
396 .url_get_short_seek = tls_get_short_seek,
397 .priv_data_size = sizeof(TLSContext),
398 .flags = URL_PROTOCOL_FLAG_NETWORK,
399 .priv_data_class = &tls_class,