3 * Copyright (c) 2011 Martin Storsjo
4 * Copyright (c) 2017 sfan5 <sfan5@live.de>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavcodec/internal.h"
29 #include "libavutil/avutil.h"
30 #include "libavutil/opt.h"
34 typedef struct TLSContext {
40 static int ff_tls_close(URLContext *h)
42 TLSContext *p = h->priv_data;
47 if (p->tls_shared.tcp)
48 ffurl_close(p->tls_shared.tcp);
52 static ssize_t tls_read_callback(struct tls *ctx, void *buf, size_t buflen, void *cb_arg)
54 URLContext *h = (URLContext*) cb_arg;
55 int ret = ffurl_read(h, buf, buflen);
56 if (ret == AVERROR(EAGAIN))
57 return TLS_WANT_POLLIN;
58 else if (ret == AVERROR_EXIT)
60 return ret >= 0 ? ret : -1;
63 static ssize_t tls_write_callback(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg)
65 URLContext *h = (URLContext*) cb_arg;
66 int ret = ffurl_write(h, buf, buflen);
67 if (ret == AVERROR(EAGAIN))
68 return TLS_WANT_POLLOUT;
69 else if (ret == AVERROR_EXIT)
71 return ret >= 0 ? ret : -1;
74 static int ff_tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
76 TLSContext *p = h->priv_data;
77 TLSShared *c = &p->tls_shared;
78 struct tls_config *cfg = NULL;
81 if (tls_init() == -1) {
86 if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
89 p->ctx = !c->listen ? tls_client() : tls_server();
95 cfg = tls_config_new();
100 if (tls_config_set_protocols(cfg, TLS_PROTOCOLS_ALL) == -1)
102 // While TLSv1.0 and TLSv1.1 are already enabled by the above,
103 // we need to be less strict with ciphers so it works in practice.
104 if (tls_config_set_ciphers(cfg, "compat") == -1)
106 if (c->ca_file && tls_config_set_ca_file(cfg, c->ca_file) == -1)
108 if (c->cert_file && tls_config_set_cert_file(cfg, c->cert_file) == -1)
110 if (c->key_file && tls_config_set_key_file(cfg, c->key_file) == -1)
113 tls_config_insecure_noverifycert(cfg);
114 tls_config_insecure_noverifyname(cfg);
115 tls_config_insecure_noverifytime(cfg);
117 if (tls_configure(p->ctx, cfg) == -1)
121 ret = tls_connect_cbs(p->ctx, tls_read_callback, tls_write_callback,
122 c->tcp, !c->numerichost ? c->host : NULL);
125 ret = tls_accept_cbs(p->ctx, &ctx_new, tls_read_callback,
126 tls_write_callback, c->tcp);
128 // free "server" context and replace by "connection" context
136 tls_config_free(cfg);
139 av_log(h, AV_LOG_ERROR, "%s\n", tls_config_error(cfg));
143 av_log(h, AV_LOG_ERROR, "%s\n", tls_error(p->ctx));
148 tls_config_free(cfg);
153 static int ff_tls_read(URLContext *h, uint8_t *buf, int size)
155 TLSContext *p = h->priv_data;
157 ret = tls_read(p->ctx, buf, size);
162 av_log(h, AV_LOG_ERROR, "%s\n", tls_error(p->ctx));
166 static int ff_tls_write(URLContext *h, const uint8_t *buf, int size)
168 TLSContext *p = h->priv_data;
170 ret = tls_write(p->ctx, buf, size);
175 av_log(h, AV_LOG_ERROR, "%s\n", tls_error(p->ctx));
179 static int tls_get_file_handle(URLContext *h)
181 TLSContext *c = h->priv_data;
182 return ffurl_get_file_handle(c->tls_shared.tcp);
185 static const AVOption options[] = {
186 TLS_COMMON_OPTIONS(TLSContext, tls_shared),
190 static const AVClass tls_class = {
192 .item_name = av_default_item_name,
194 .version = LIBAVUTIL_VERSION_INT,
197 const URLProtocol ff_tls_protocol = {
199 .url_open2 = ff_tls_open,
200 .url_read = ff_tls_read,
201 .url_write = ff_tls_write,
202 .url_close = ff_tls_close,
203 .url_get_file_handle = tls_get_file_handle,
204 .priv_data_size = sizeof(TLSContext),
205 .flags = URL_PROTOCOL_FLAG_NETWORK,
206 .priv_data_class = &tls_class,