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