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 {
49 #include <openssl/crypto.h>
50 pthread_mutex_t *openssl_mutexes;
51 static void openssl_lock(int mode, int type, const char *file, int line)
53 if (mode & CRYPTO_LOCK)
54 pthread_mutex_lock(&openssl_mutexes[type]);
56 pthread_mutex_unlock(&openssl_mutexes[type]);
58 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
59 static unsigned long openssl_thread_id(void)
61 return (intptr_t) pthread_self();
66 int ff_openssl_init(void)
68 avpriv_lock_avformat();
71 SSL_load_error_strings();
73 if (!CRYPTO_get_locking_callback()) {
75 openssl_mutexes = av_malloc_array(sizeof(pthread_mutex_t), CRYPTO_num_locks());
76 if (!openssl_mutexes) {
77 avpriv_unlock_avformat();
78 return AVERROR(ENOMEM);
81 for (i = 0; i < CRYPTO_num_locks(); i++)
82 pthread_mutex_init(&openssl_mutexes[i], NULL);
83 CRYPTO_set_locking_callback(openssl_lock);
84 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
85 CRYPTO_set_id_callback(openssl_thread_id);
91 avpriv_unlock_avformat();
96 void ff_openssl_deinit(void)
98 avpriv_lock_avformat();
102 if (CRYPTO_get_locking_callback() == openssl_lock) {
104 CRYPTO_set_locking_callback(NULL);
105 for (i = 0; i < CRYPTO_num_locks(); i++)
106 pthread_mutex_destroy(&openssl_mutexes[i]);
107 av_free(openssl_mutexes);
111 avpriv_unlock_avformat();
114 static int print_tls_error(URLContext *h, int ret)
116 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
120 static int tls_close(URLContext *h)
122 TLSContext *c = h->priv_data;
124 SSL_shutdown(c->ssl);
128 SSL_CTX_free(c->ctx);
129 if (c->tls_shared.tcp)
130 ffurl_close(c->tls_shared.tcp);
135 static int url_bio_create(BIO *b)
143 static int url_bio_destroy(BIO *b)
148 static int url_bio_bread(BIO *b, char *buf, int len)
150 URLContext *h = b->ptr;
151 int ret = ffurl_read(h, buf, len);
154 BIO_clear_retry_flags(b);
155 if (ret == AVERROR_EXIT)
160 static int url_bio_bwrite(BIO *b, const char *buf, int len)
162 URLContext *h = b->ptr;
163 int ret = ffurl_write(h, buf, len);
166 BIO_clear_retry_flags(b);
167 if (ret == AVERROR_EXIT)
172 static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
174 if (cmd == BIO_CTRL_FLUSH) {
175 BIO_clear_retry_flags(b);
181 static int url_bio_bputs(BIO *b, const char *str)
183 return url_bio_bwrite(b, str, strlen(str));
186 static BIO_METHOD url_bio_method = {
187 .type = BIO_TYPE_SOURCE_SINK,
188 .name = "urlprotocol bio",
189 .bwrite = url_bio_bwrite,
190 .bread = url_bio_bread,
191 .bputs = url_bio_bputs,
193 .ctrl = url_bio_ctrl,
194 .create = url_bio_create,
195 .destroy = url_bio_destroy,
198 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
200 TLSContext *p = h->priv_data;
201 TLSShared *c = &p->tls_shared;
205 if ((ret = ff_openssl_init()) < 0)
208 if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
211 p->ctx = SSL_CTX_new(c->listen ? TLSv1_server_method() : TLSv1_client_method());
213 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
218 if (!SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL))
219 av_log(h, AV_LOG_ERROR, "SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
221 if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
222 av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
223 c->cert_file, ERR_error_string(ERR_get_error(), NULL));
227 if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
228 av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
229 c->key_file, ERR_error_string(ERR_get_error(), NULL));
233 // Note, this doesn't check that the peer certificate actually matches
234 // the requested hostname.
236 SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
237 p->ssl = SSL_new(p->ctx);
239 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
243 bio = BIO_new(&url_bio_method);
245 SSL_set_bio(p->ssl, bio, bio);
246 if (!c->listen && !c->numerichost)
247 SSL_set_tlsext_host_name(p->ssl, c->host);
248 ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
250 av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
253 } else if (ret < 0) {
254 ret = print_tls_error(h, ret);
264 static int tls_read(URLContext *h, uint8_t *buf, int size)
266 TLSContext *c = h->priv_data;
267 int ret = SSL_read(c->ssl, buf, size);
272 return print_tls_error(h, ret);
275 static int tls_write(URLContext *h, const uint8_t *buf, int size)
277 TLSContext *c = h->priv_data;
278 int ret = SSL_write(c->ssl, buf, size);
283 return print_tls_error(h, ret);
286 static const AVOption options[] = {
287 TLS_COMMON_OPTIONS(TLSContext, tls_shared),
291 static const AVClass tls_class = {
293 .item_name = av_default_item_name,
295 .version = LIBAVUTIL_VERSION_INT,
298 URLProtocol ff_tls_openssl_protocol = {
300 .url_open2 = tls_open,
301 .url_read = tls_read,
302 .url_write = tls_write,
303 .url_close = tls_close,
304 .priv_data_size = sizeof(TLSContext),
305 .flags = URL_PROTOCOL_FLAG_NETWORK,
306 .priv_data_class = &tls_class,