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