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