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
24 #include "libavutil/avstring.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
28 #include <gnutls/gnutls.h>
29 #include <gnutls/x509.h>
30 #define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size)
31 #define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size)
32 #define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR)
33 #define TLS_free(c) do { \
35 gnutls_deinit(c->session); \
37 gnutls_certificate_free_credentials(c->cred); \
40 static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
41 void *buf, size_t len)
43 URLContext *h = (URLContext*) transport;
44 int ret = ffurl_read(h, buf, len);
47 if (ret == AVERROR_EXIT)
52 static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
53 const void *buf, size_t len)
55 URLContext *h = (URLContext*) transport;
56 int ret = ffurl_write(h, buf, len);
59 if (ret == AVERROR_EXIT)
65 #include <openssl/bio.h>
66 #include <openssl/ssl.h>
67 #include <openssl/err.h>
68 #define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size)
69 #define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size)
70 #define TLS_shutdown(c) SSL_shutdown(c->ssl)
71 #define TLS_free(c) do { \
75 SSL_CTX_free(c->ctx); \
78 static int url_bio_create(BIO *b)
86 static int url_bio_destroy(BIO *b)
91 static int url_bio_bread(BIO *b, char *buf, int len)
93 URLContext *h = b->ptr;
94 int ret = ffurl_read(h, buf, len);
97 BIO_clear_retry_flags(b);
98 if (ret == AVERROR_EXIT)
103 static int url_bio_bwrite(BIO *b, const char *buf, int len)
105 URLContext *h = b->ptr;
106 int ret = ffurl_write(h, buf, len);
109 BIO_clear_retry_flags(b);
110 if (ret == AVERROR_EXIT)
115 static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
117 if (cmd == BIO_CTRL_FLUSH) {
118 BIO_clear_retry_flags(b);
124 static int url_bio_bputs(BIO *b, const char *str)
126 return url_bio_bwrite(b, str, strlen(str));
129 static BIO_METHOD url_bio_method = {
130 .type = BIO_TYPE_SOURCE_SINK,
131 .name = "urlprotocol bio",
132 .bwrite = url_bio_bwrite,
133 .bread = url_bio_bread,
134 .bputs = url_bio_bputs,
136 .ctrl = url_bio_ctrl,
137 .create = url_bio_create,
138 .destroy = url_bio_destroy,
143 #include "os_support.h"
144 #include "internal.h"
149 typedef struct TLSContext {
150 const AVClass *class;
153 gnutls_session_t session;
154 gnutls_certificate_credentials_t cred;
166 #define OFFSET(x) offsetof(TLSContext, x)
167 #define D AV_OPT_FLAG_DECODING_PARAM
168 #define E AV_OPT_FLAG_ENCODING_PARAM
169 static const AVOption options[] = {
170 {"ca_file", "Certificate Authority database file", OFFSET(ca_file), AV_OPT_TYPE_STRING, .flags = D|E },
171 {"tls_verify", "Verify the peer certificate", OFFSET(verify), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
172 {"cert_file", "Certificate file", OFFSET(cert_file), AV_OPT_TYPE_STRING, .flags = D|E },
173 {"key_file", "Private key file", OFFSET(key_file), AV_OPT_TYPE_STRING, .flags = D|E },
174 {"listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
178 static const AVClass tls_class = {
180 .item_name = av_default_item_name,
182 .version = LIBAVUTIL_VERSION_INT,
185 static int print_tls_error(URLContext *h, int ret)
190 case GNUTLS_E_INTERRUPTED:
192 case GNUTLS_E_WARNING_ALERT_RECEIVED:
193 av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
196 av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
200 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
205 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
207 TLSContext *c = h->priv_data;
211 char buf[200], host[200], opts[50] = "";
213 struct addrinfo hints = { 0 }, *ai = NULL;
214 const char *proxy_path;
216 #if CONFIG_OPENSSL && !CONFIG_GNUTLS
223 snprintf(opts, sizeof(opts), "?listen=1");
225 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
227 p = strchr(uri, '?');
232 if (av_find_info_tag(opts, sizeof(opts), "listen", p))
236 ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, "%s", p);
238 hints.ai_flags = AI_NUMERICHOST;
239 if (!getaddrinfo(host, NULL, &hints, &ai)) {
244 proxy_path = getenv("http_proxy");
245 use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), host) &&
246 proxy_path && av_strstart(proxy_path, "http://", NULL);
249 char proxy_host[200], proxy_auth[200], dest[200];
251 av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
252 proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
254 ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
255 ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
256 proxy_port, "/%s", dest);
259 ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
260 &h->interrupt_callback, options);
265 gnutls_init(&c->session, c->listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
266 if (!c->listen && !numerichost)
267 gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
268 gnutls_certificate_allocate_credentials(&c->cred);
270 gnutls_certificate_set_x509_trust_file(c->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
271 #if GNUTLS_VERSION_MAJOR >= 3
273 gnutls_certificate_set_x509_system_trust(c->cred);
275 gnutls_certificate_set_verify_flags(c->cred, c->verify ?
276 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
277 if (c->cert_file && c->key_file) {
278 ret = gnutls_certificate_set_x509_key_file(c->cred,
279 c->cert_file, c->key_file,
280 GNUTLS_X509_FMT_PEM);
282 av_log(h, AV_LOG_ERROR,
283 "Unable to set cert/key files %s and %s: %s\n",
284 c->cert_file, c->key_file, gnutls_strerror(ret));
289 gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
290 gnutls_transport_set_pull_function(c->session, gnutls_url_pull);
291 gnutls_transport_set_push_function(c->session, gnutls_url_push);
292 gnutls_transport_set_ptr(c->session, c->tcp);
293 gnutls_priority_set_direct(c->session, "NORMAL", NULL);
294 ret = gnutls_handshake(c->session);
296 ret = print_tls_error(h, ret);
300 unsigned int status, cert_list_size;
301 gnutls_x509_crt_t cert;
302 const gnutls_datum_t *cert_list;
303 if ((ret = gnutls_certificate_verify_peers2(c->session, &status)) < 0) {
304 av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
305 gnutls_strerror(ret));
309 if (status & GNUTLS_CERT_INVALID) {
310 av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
314 if (gnutls_certificate_type_get(c->session) != GNUTLS_CRT_X509) {
315 av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
319 gnutls_x509_crt_init(&cert);
320 cert_list = gnutls_certificate_get_peers(c->session, &cert_list_size);
321 gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
322 ret = gnutls_x509_crt_check_hostname(cert, host);
323 gnutls_x509_crt_deinit(cert);
325 av_log(h, AV_LOG_ERROR,
326 "The certificate's owner does not match hostname %s\n", host);
332 c->ctx = SSL_CTX_new(c->listen ? TLSv1_server_method() : TLSv1_client_method());
334 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
339 SSL_CTX_load_verify_locations(c->ctx, c->ca_file, NULL);
340 if (c->cert_file && !SSL_CTX_use_certificate_chain_file(c->ctx, c->cert_file)) {
341 av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
342 c->cert_file, ERR_error_string(ERR_get_error(), NULL));
346 if (c->key_file && !SSL_CTX_use_PrivateKey_file(c->ctx, c->key_file, SSL_FILETYPE_PEM)) {
347 av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
348 c->key_file, ERR_error_string(ERR_get_error(), NULL));
352 // Note, this doesn't check that the peer certificate actually matches
353 // the requested hostname.
355 SSL_CTX_set_verify(c->ctx, SSL_VERIFY_PEER, NULL);
356 c->ssl = SSL_new(c->ctx);
358 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
362 bio = BIO_new(&url_bio_method);
364 SSL_set_bio(c->ssl, bio, bio);
365 if (!c->listen && !numerichost)
366 SSL_set_tlsext_host_name(c->ssl, host);
367 ret = c->listen ? SSL_accept(c->ssl) : SSL_connect(c->ssl);
369 av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
372 } else if (ret < 0) {
373 ret = print_tls_error(h, ret);
386 static int tls_read(URLContext *h, uint8_t *buf, int size)
388 TLSContext *c = h->priv_data;
389 int ret = TLS_read(c, buf, size);
394 return print_tls_error(h, ret);
397 static int tls_write(URLContext *h, const uint8_t *buf, int size)
399 TLSContext *c = h->priv_data;
400 int ret = TLS_write(c, buf, size);
405 return print_tls_error(h, ret);
408 static int tls_close(URLContext *h)
410 TLSContext *c = h->priv_data;
418 URLProtocol ff_tls_protocol = {
420 .url_open2 = tls_open,
421 .url_read = tls_read,
422 .url_write = tls_write,
423 .url_close = tls_close,
424 .priv_data_size = sizeof(TLSContext),
425 .flags = URL_PROTOCOL_FLAG_NETWORK,
426 .priv_data_class = &tls_class,