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