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