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
24 #include <gnutls/gnutls.h>
25 #include <gnutls/x509.h>
30 #include "os_support.h"
33 #include "libavcodec/internal.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/parseutils.h"
38 #ifndef GNUTLS_VERSION_NUMBER
39 #define GNUTLS_VERSION_NUMBER LIBGNUTLS_VERSION_NUMBER
42 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
44 #include "libavutil/thread.h"
45 GCRY_THREAD_OPTION_PTHREAD_IMPL;
48 typedef struct TLSContext {
51 gnutls_session_t session;
52 gnutls_certificate_credentials_t cred;
56 void ff_gnutls_init(void)
59 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
60 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
61 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
67 void ff_gnutls_deinit(void)
70 gnutls_global_deinit();
74 static int print_tls_error(URLContext *h, int ret)
78 return AVERROR(EAGAIN);
79 case GNUTLS_E_INTERRUPTED:
80 #ifdef GNUTLS_E_PREMATURE_TERMINATION
81 case GNUTLS_E_PREMATURE_TERMINATION:
84 case GNUTLS_E_WARNING_ALERT_RECEIVED:
85 av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
88 av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
94 static int tls_close(URLContext *h)
96 TLSContext *c = h->priv_data;
98 gnutls_bye(c->session, GNUTLS_SHUT_WR);
100 gnutls_deinit(c->session);
102 gnutls_certificate_free_credentials(c->cred);
103 ffurl_closep(&c->tls_shared.tcp);
108 static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
109 void *buf, size_t len)
111 URLContext *h = (URLContext*) transport;
112 int ret = ffurl_read(h, buf, len);
115 if (ret == AVERROR_EXIT)
117 if (ret == AVERROR(EAGAIN))
124 static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
125 const void *buf, size_t len)
127 URLContext *h = (URLContext*) transport;
128 int ret = ffurl_write(h, buf, len);
131 if (ret == AVERROR_EXIT)
133 if (ret == AVERROR(EAGAIN))
140 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
142 TLSContext *p = h->priv_data;
143 TLSShared *c = &p->tls_shared;
148 if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
151 gnutls_init(&p->session, c->listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
152 if (!c->listen && !c->numerichost)
153 gnutls_server_name_set(p->session, GNUTLS_NAME_DNS, c->host, strlen(c->host));
154 gnutls_certificate_allocate_credentials(&p->cred);
156 ret = gnutls_certificate_set_x509_trust_file(p->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
158 av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
160 #if GNUTLS_VERSION_NUMBER >= 0x030020
162 gnutls_certificate_set_x509_system_trust(p->cred);
164 gnutls_certificate_set_verify_flags(p->cred, c->verify ?
165 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
166 if (c->cert_file && c->key_file) {
167 ret = gnutls_certificate_set_x509_key_file(p->cred,
168 c->cert_file, c->key_file,
169 GNUTLS_X509_FMT_PEM);
171 av_log(h, AV_LOG_ERROR,
172 "Unable to set cert/key files %s and %s: %s\n",
173 c->cert_file, c->key_file, gnutls_strerror(ret));
177 } else if (c->cert_file || c->key_file)
178 av_log(h, AV_LOG_ERROR, "cert and key required\n");
179 gnutls_credentials_set(p->session, GNUTLS_CRD_CERTIFICATE, p->cred);
180 gnutls_transport_set_pull_function(p->session, gnutls_url_pull);
181 gnutls_transport_set_push_function(p->session, gnutls_url_push);
182 gnutls_transport_set_ptr(p->session, c->tcp);
183 gnutls_priority_set_direct(p->session, "NORMAL", NULL);
185 if (ff_check_interrupt(&h->interrupt_callback)) {
190 ret = gnutls_handshake(p->session);
191 if (gnutls_error_is_fatal(ret)) {
192 ret = print_tls_error(h, ret);
196 p->need_shutdown = 1;
198 unsigned int status, cert_list_size;
199 gnutls_x509_crt_t cert;
200 const gnutls_datum_t *cert_list;
201 if ((ret = gnutls_certificate_verify_peers2(p->session, &status)) < 0) {
202 av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
203 gnutls_strerror(ret));
207 if (status & GNUTLS_CERT_INVALID) {
208 av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
212 if (gnutls_certificate_type_get(p->session) != GNUTLS_CRT_X509) {
213 av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
217 gnutls_x509_crt_init(&cert);
218 cert_list = gnutls_certificate_get_peers(p->session, &cert_list_size);
219 gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
220 ret = gnutls_x509_crt_check_hostname(cert, c->host);
221 gnutls_x509_crt_deinit(cert);
223 av_log(h, AV_LOG_ERROR,
224 "The certificate's owner does not match hostname %s\n", c->host);
236 static int tls_read(URLContext *h, uint8_t *buf, int size)
238 TLSContext *c = h->priv_data;
240 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
241 c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
242 c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
243 ret = gnutls_record_recv(c->session, buf, size);
248 return print_tls_error(h, ret);
251 static int tls_write(URLContext *h, const uint8_t *buf, int size)
253 TLSContext *c = h->priv_data;
255 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
256 c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
257 c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
258 ret = gnutls_record_send(c->session, buf, size);
263 return print_tls_error(h, ret);
266 static int tls_get_file_handle(URLContext *h)
268 TLSContext *c = h->priv_data;
269 return ffurl_get_file_handle(c->tls_shared.tcp);
272 static int tls_get_short_seek(URLContext *h)
274 TLSContext *s = h->priv_data;
275 return ffurl_get_short_seek(s->tls_shared.tcp);
278 static const AVOption options[] = {
279 TLS_COMMON_OPTIONS(TLSContext, tls_shared),
283 static const AVClass tls_class = {
285 .item_name = av_default_item_name,
287 .version = LIBAVUTIL_VERSION_INT,
290 const URLProtocol ff_tls_protocol = {
292 .url_open2 = tls_open,
293 .url_read = tls_read,
294 .url_write = tls_write,
295 .url_close = tls_close,
296 .url_get_file_handle = tls_get_file_handle,
297 .url_get_short_seek = tls_get_short_seek,
298 .priv_data_size = sizeof(TLSContext),
299 .flags = URL_PROTOCOL_FLAG_NETWORK,
300 .priv_data_class = &tls_class,