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