3 * Copyright (c) 2011 Martin Storsjo
5 * This file is part of Libav.
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.
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.
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
25 #include "os_support.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/parseutils.h"
32 #include <openssl/bio.h>
33 #include <openssl/ssl.h>
34 #include <openssl/err.h>
36 typedef struct TLSContext {
43 static int print_tls_error(URLContext *h, int ret)
45 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
49 static int tls_close(URLContext *h)
51 TLSContext *c = h->priv_data;
58 if (c->tls_shared.tcp)
59 ffurl_close(c->tls_shared.tcp);
64 static int url_bio_create(BIO *b)
72 static int url_bio_destroy(BIO *b)
77 static int url_bio_bread(BIO *b, char *buf, int len)
79 URLContext *h = b->ptr;
80 int ret = ffurl_read(h, buf, len);
83 BIO_clear_retry_flags(b);
84 if (ret == AVERROR_EXIT)
89 static int url_bio_bwrite(BIO *b, const char *buf, int len)
91 URLContext *h = b->ptr;
92 int ret = ffurl_write(h, buf, len);
95 BIO_clear_retry_flags(b);
96 if (ret == AVERROR_EXIT)
101 static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
103 if (cmd == BIO_CTRL_FLUSH) {
104 BIO_clear_retry_flags(b);
110 static int url_bio_bputs(BIO *b, const char *str)
112 return url_bio_bwrite(b, str, strlen(str));
115 static BIO_METHOD url_bio_method = {
116 .type = BIO_TYPE_SOURCE_SINK,
117 .name = "urlprotocol bio",
118 .bwrite = url_bio_bwrite,
119 .bread = url_bio_bread,
120 .bputs = url_bio_bputs,
122 .ctrl = url_bio_ctrl,
123 .create = url_bio_create,
124 .destroy = url_bio_destroy,
127 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
129 TLSContext *p = h->priv_data;
130 TLSShared *c = &p->tls_shared;
136 if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
139 p->ctx = SSL_CTX_new(c->listen ? TLSv1_server_method() : TLSv1_client_method());
141 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
146 SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL);
147 if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
148 av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
149 c->cert_file, ERR_error_string(ERR_get_error(), NULL));
153 if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
154 av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
155 c->key_file, ERR_error_string(ERR_get_error(), NULL));
159 // Note, this doesn't check that the peer certificate actually matches
160 // the requested hostname.
162 SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER, NULL);
163 p->ssl = SSL_new(p->ctx);
165 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
169 bio = BIO_new(&url_bio_method);
171 SSL_set_bio(p->ssl, bio, bio);
172 if (!c->listen && !c->numerichost)
173 SSL_set_tlsext_host_name(p->ssl, c->host);
174 ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
176 av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
179 } else if (ret < 0) {
180 ret = print_tls_error(h, ret);
190 static int tls_read(URLContext *h, uint8_t *buf, int size)
192 TLSContext *c = h->priv_data;
193 int ret = SSL_read(c->ssl, buf, size);
198 return print_tls_error(h, ret);
201 static int tls_write(URLContext *h, const uint8_t *buf, int size)
203 TLSContext *c = h->priv_data;
204 int ret = SSL_write(c->ssl, buf, size);
209 return print_tls_error(h, ret);
212 static const AVOption options[] = {
213 TLS_COMMON_OPTIONS(TLSContext, tls_shared),
217 static const AVClass tls_class = {
219 .item_name = av_default_item_name,
221 .version = LIBAVUTIL_VERSION_INT,
224 URLProtocol ff_tls_openssl_protocol = {
226 .url_open2 = tls_open,
227 .url_read = tls_read,
228 .url_write = tls_write,
229 .url_close = tls_close,
230 .priv_data_size = sizeof(TLSContext),
231 .flags = URL_PROTOCOL_FLAG_NETWORK,
232 .priv_data_class = &tls_class,