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"
26 #include <gnutls/gnutls.h>
27 #define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size)
28 #define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size)
29 #define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR)
30 #define TLS_free(c) do { \
32 gnutls_deinit(c->session); \
34 gnutls_certificate_free_credentials(c->cred); \
37 #include <openssl/bio.h>
38 #include <openssl/ssl.h>
39 #include <openssl/err.h>
40 #define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size)
41 #define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size)
42 #define TLS_shutdown(c) SSL_shutdown(c->ssl)
43 #define TLS_free(c) do { \
47 SSL_CTX_free(c->ctx); \
51 #include "os_support.h"
61 gnutls_session_t session;
62 gnutls_certificate_credentials_t cred;
70 static int do_tls_poll(URLContext *h, int ret)
72 TLSContext *c = h->priv_data;
73 struct pollfd p = { c->fd, 0, 0 };
75 if (ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED) {
76 av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
79 if (gnutls_record_get_direction(c->session))
84 ret = SSL_get_error(c->ssl, ret);
85 if (ret == SSL_ERROR_WANT_READ) {
87 } else if (ret == SSL_ERROR_WANT_WRITE) {
90 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
94 if (h->flags & AVIO_FLAG_NONBLOCK)
95 return AVERROR(EAGAIN);
97 int n = poll(&p, 1, 100);
100 if (ff_check_interrupt(&h->interrupt_callback))
101 return AVERROR(EINTR);
106 static int tls_open(URLContext *h, const char *uri, int flags)
108 TLSContext *c = h->priv_data;
111 char buf[200], host[200];
113 struct addrinfo hints = { 0 }, *ai = NULL;
114 const char *proxy_path;
119 proxy_path = getenv("http_proxy");
120 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
121 av_strstart(proxy_path, "http://", NULL);
123 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
124 ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL);
126 hints.ai_flags = AI_NUMERICHOST;
127 if (!getaddrinfo(host, NULL, &hints, &ai)) {
133 char proxy_host[200], proxy_auth[200], dest[200];
135 av_url_split(NULL, 0, proxy_auth, sizeof(proxy_auth),
136 proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
138 ff_url_join(dest, sizeof(dest), NULL, NULL, host, port, NULL);
139 ff_url_join(buf, sizeof(buf), "httpproxy", proxy_auth, proxy_host,
140 proxy_port, "/%s", dest);
143 ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE,
144 &h->interrupt_callback, NULL);
147 c->fd = ffurl_get_file_handle(c->tcp);
150 gnutls_init(&c->session, GNUTLS_CLIENT);
152 gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
153 gnutls_certificate_allocate_credentials(&c->cred);
154 gnutls_certificate_set_verify_flags(c->cred, 0);
155 gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
156 gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
158 gnutls_priority_set_direct(c->session, "NORMAL", NULL);
160 ret = gnutls_handshake(c->session);
163 if ((ret = do_tls_poll(h, ret)) < 0)
167 c->ctx = SSL_CTX_new(TLSv1_client_method());
169 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
173 c->ssl = SSL_new(c->ctx);
175 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
179 SSL_set_fd(c->ssl, c->fd);
181 SSL_set_tlsext_host_name(c->ssl, host);
183 ret = SSL_connect(c->ssl);
187 av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
191 if ((ret = do_tls_poll(h, ret)) < 0)
204 static int tls_read(URLContext *h, uint8_t *buf, int size)
206 TLSContext *c = h->priv_data;
208 int ret = TLS_read(c, buf, size);
213 if ((ret = do_tls_poll(h, ret)) < 0)
219 static int tls_write(URLContext *h, const uint8_t *buf, int size)
221 TLSContext *c = h->priv_data;
223 int ret = TLS_write(c, buf, size);
228 if ((ret = do_tls_poll(h, ret)) < 0)
234 static int tls_close(URLContext *h)
236 TLSContext *c = h->priv_data;
244 URLProtocol ff_tls_protocol = {
246 .url_open = tls_open,
247 .url_read = tls_read,
248 .url_write = tls_write,
249 .url_close = tls_close,
250 .priv_data_size = sizeof(TLSContext),
251 .flags = URL_PROTOCOL_FLAG_NETWORK,