]> git.sesse.net Git - ffmpeg/blob - libavformat/tls_gnutls.c
Merge commit '681a86aba6cb09b98ad716d986182060c7795d20'
[ffmpeg] / libavformat / tls_gnutls.c
1 /*
2  * TLS/SSL Protocol
3  * Copyright (c) 2011 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <errno.h>
23
24 #include <gnutls/gnutls.h>
25 #include <gnutls/x509.h>
26
27 #include "avformat.h"
28 #include "internal.h"
29 #include "network.h"
30 #include "os_support.h"
31 #include "url.h"
32 #include "tls.h"
33 #include "libavcodec/internal.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/parseutils.h"
37
38 #ifndef GNUTLS_VERSION_NUMBER
39 #define GNUTLS_VERSION_NUMBER LIBGNUTLS_VERSION_NUMBER
40 #endif
41
42 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
43 #include <gcrypt.h>
44 #include "libavutil/thread.h"
45 GCRY_THREAD_OPTION_PTHREAD_IMPL;
46 #endif
47
48 typedef struct TLSContext {
49     const AVClass *class;
50     TLSShared tls_shared;
51     gnutls_session_t session;
52     gnutls_certificate_credentials_t cred;
53     int need_shutdown;
54 } TLSContext;
55
56 void ff_gnutls_init(void)
57 {
58     avpriv_lock_avformat();
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);
62 #endif
63     gnutls_global_init();
64     avpriv_unlock_avformat();
65 }
66
67 void ff_gnutls_deinit(void)
68 {
69     avpriv_lock_avformat();
70     gnutls_global_deinit();
71     avpriv_unlock_avformat();
72 }
73
74 static int print_tls_error(URLContext *h, int ret)
75 {
76     switch (ret) {
77     case GNUTLS_E_AGAIN:
78     case GNUTLS_E_INTERRUPTED:
79 #ifdef GNUTLS_E_PREMATURE_TERMINATION
80     case GNUTLS_E_PREMATURE_TERMINATION:
81 #endif
82         break;
83     case GNUTLS_E_WARNING_ALERT_RECEIVED:
84         av_log(h, AV_LOG_WARNING, "%s\n", gnutls_strerror(ret));
85         break;
86     default:
87         av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
88         break;
89     }
90     return AVERROR(EIO);
91 }
92
93 static int tls_close(URLContext *h)
94 {
95     TLSContext *c = h->priv_data;
96     if (c->need_shutdown)
97         gnutls_bye(c->session, GNUTLS_SHUT_WR);
98     if (c->session)
99         gnutls_deinit(c->session);
100     if (c->cred)
101         gnutls_certificate_free_credentials(c->cred);
102     if (c->tls_shared.tcp)
103         ffurl_close(c->tls_shared.tcp);
104     ff_gnutls_deinit();
105     return 0;
106 }
107
108 static ssize_t gnutls_url_pull(gnutls_transport_ptr_t transport,
109                                void *buf, size_t len)
110 {
111     URLContext *h = (URLContext*) transport;
112     int ret = ffurl_read(h, buf, len);
113     if (ret >= 0)
114         return ret;
115     if (ret == AVERROR_EXIT)
116         return 0;
117     errno = EIO;
118     return -1;
119 }
120
121 static ssize_t gnutls_url_push(gnutls_transport_ptr_t transport,
122                                const void *buf, size_t len)
123 {
124     URLContext *h = (URLContext*) transport;
125     int ret = ffurl_write(h, buf, len);
126     if (ret >= 0)
127         return ret;
128     if (ret == AVERROR_EXIT)
129         return 0;
130     errno = EIO;
131     return -1;
132 }
133
134 static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
135 {
136     TLSContext *p = h->priv_data;
137     TLSShared *c = &p->tls_shared;
138     int ret;
139
140     ff_gnutls_init();
141
142     if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
143         goto fail;
144
145     gnutls_init(&p->session, c->listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
146     if (!c->listen && !c->numerichost)
147         gnutls_server_name_set(p->session, GNUTLS_NAME_DNS, c->host, strlen(c->host));
148     gnutls_certificate_allocate_credentials(&p->cred);
149     if (c->ca_file) {
150         ret = gnutls_certificate_set_x509_trust_file(p->cred, c->ca_file, GNUTLS_X509_FMT_PEM);
151         if (ret < 0)
152             av_log(h, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
153     }
154 #if GNUTLS_VERSION_NUMBER >= 0x030020
155     else
156         gnutls_certificate_set_x509_system_trust(p->cred);
157 #endif
158     gnutls_certificate_set_verify_flags(p->cred, c->verify ?
159                                         GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
160     if (c->cert_file && c->key_file) {
161         ret = gnutls_certificate_set_x509_key_file(p->cred,
162                                                    c->cert_file, c->key_file,
163                                                    GNUTLS_X509_FMT_PEM);
164         if (ret < 0) {
165             av_log(h, AV_LOG_ERROR,
166                    "Unable to set cert/key files %s and %s: %s\n",
167                    c->cert_file, c->key_file, gnutls_strerror(ret));
168             ret = AVERROR(EIO);
169             goto fail;
170         }
171     } else if (c->cert_file || c->key_file)
172         av_log(h, AV_LOG_ERROR, "cert and key required\n");
173     gnutls_credentials_set(p->session, GNUTLS_CRD_CERTIFICATE, p->cred);
174     gnutls_transport_set_pull_function(p->session, gnutls_url_pull);
175     gnutls_transport_set_push_function(p->session, gnutls_url_push);
176     gnutls_transport_set_ptr(p->session, c->tcp);
177     gnutls_priority_set_direct(p->session, "NORMAL", NULL);
178     ret = gnutls_handshake(p->session);
179     if (ret) {
180         ret = print_tls_error(h, ret);
181         goto fail;
182     }
183     p->need_shutdown = 1;
184     if (c->verify) {
185         unsigned int status, cert_list_size;
186         gnutls_x509_crt_t cert;
187         const gnutls_datum_t *cert_list;
188         if ((ret = gnutls_certificate_verify_peers2(p->session, &status)) < 0) {
189             av_log(h, AV_LOG_ERROR, "Unable to verify peer certificate: %s\n",
190                                     gnutls_strerror(ret));
191             ret = AVERROR(EIO);
192             goto fail;
193         }
194         if (status & GNUTLS_CERT_INVALID) {
195             av_log(h, AV_LOG_ERROR, "Peer certificate failed verification\n");
196             ret = AVERROR(EIO);
197             goto fail;
198         }
199         if (gnutls_certificate_type_get(p->session) != GNUTLS_CRT_X509) {
200             av_log(h, AV_LOG_ERROR, "Unsupported certificate type\n");
201             ret = AVERROR(EIO);
202             goto fail;
203         }
204         gnutls_x509_crt_init(&cert);
205         cert_list = gnutls_certificate_get_peers(p->session, &cert_list_size);
206         gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
207         ret = gnutls_x509_crt_check_hostname(cert, c->host);
208         gnutls_x509_crt_deinit(cert);
209         if (!ret) {
210             av_log(h, AV_LOG_ERROR,
211                    "The certificate's owner does not match hostname %s\n", c->host);
212             ret = AVERROR(EIO);
213             goto fail;
214         }
215     }
216
217     return 0;
218 fail:
219     tls_close(h);
220     return ret;
221 }
222
223 static int tls_read(URLContext *h, uint8_t *buf, int size)
224 {
225     TLSContext *c = h->priv_data;
226     int ret = gnutls_record_recv(c->session, buf, size);
227     if (ret > 0)
228         return ret;
229     if (ret == 0)
230         return AVERROR_EOF;
231     return print_tls_error(h, ret);
232 }
233
234 static int tls_write(URLContext *h, const uint8_t *buf, int size)
235 {
236     TLSContext *c = h->priv_data;
237     int ret = gnutls_record_send(c->session, buf, size);
238     if (ret > 0)
239         return ret;
240     if (ret == 0)
241         return AVERROR_EOF;
242     return print_tls_error(h, ret);
243 }
244
245 static int tls_get_file_handle(URLContext *h)
246 {
247     TLSContext *c = h->priv_data;
248     return ffurl_get_file_handle(c->tls_shared.tcp);
249 }
250
251 static const AVOption options[] = {
252     TLS_COMMON_OPTIONS(TLSContext, tls_shared),
253     { NULL }
254 };
255
256 static const AVClass tls_class = {
257     .class_name = "tls",
258     .item_name  = av_default_item_name,
259     .option     = options,
260     .version    = LIBAVUTIL_VERSION_INT,
261 };
262
263 const URLProtocol ff_tls_gnutls_protocol = {
264     .name           = "tls",
265     .url_open2      = tls_open,
266     .url_read       = tls_read,
267     .url_write      = tls_write,
268     .url_close      = tls_close,
269     .url_get_file_handle = tls_get_file_handle,
270     .priv_data_size = sizeof(TLSContext),
271     .flags          = URL_PROTOCOL_FLAG_NETWORK,
272     .priv_data_class = &tls_class,
273 };