]> git.sesse.net Git - ffmpeg/blob - libavformat/tls.c
tls: Use ERR_get_error() in do_tls_poll
[ffmpeg] / libavformat / tls.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 "avformat.h"
23 #include "url.h"
24 #include "libavutil/avstring.h"
25 #if CONFIG_GNUTLS
26 #include <gnutls/gnutls.h>
27 #define TLS_read(c, buf, size)  gnutls_record_recv(c->session, buf, size)
28 #define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size)
29 #define TLS_shutdown(c)         gnutls_bye(c->session, GNUTLS_SHUT_RDWR)
30 #define TLS_free(c) do { \
31         if (c->session) \
32             gnutls_deinit(c->session); \
33         if (c->cred) \
34             gnutls_certificate_free_credentials(c->cred); \
35     } while (0)
36 #elif CONFIG_OPENSSL
37 #include <openssl/bio.h>
38 #include <openssl/ssl.h>
39 #include <openssl/err.h>
40 #define TLS_read(c, buf, size)  SSL_read(c->ssl,  buf, size)
41 #define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size)
42 #define TLS_shutdown(c)         SSL_shutdown(c->ssl)
43 #define TLS_free(c) do { \
44         if (c->ssl) \
45             SSL_free(c->ssl); \
46         if (c->ctx) \
47             SSL_CTX_free(c->ctx); \
48     } while (0)
49 #endif
50 #include "network.h"
51 #include "os_support.h"
52 #include "internal.h"
53 #if HAVE_POLL_H
54 #include <poll.h>
55 #endif
56
57 typedef struct {
58     const AVClass *class;
59     URLContext *tcp;
60 #if CONFIG_GNUTLS
61     gnutls_session_t session;
62     gnutls_certificate_credentials_t cred;
63 #elif CONFIG_OPENSSL
64     SSL_CTX *ctx;
65     SSL *ssl;
66 #endif
67     int fd;
68 } TLSContext;
69
70 static int do_tls_poll(URLContext *h, int ret)
71 {
72     TLSContext *c = h->priv_data;
73     struct pollfd p = { c->fd, 0, 0 };
74 #if CONFIG_GNUTLS
75     if (ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED) {
76         av_log(NULL, AV_LOG_ERROR, "%s\n", gnutls_strerror(ret));
77         return AVERROR(EIO);
78     }
79     if (gnutls_record_get_direction(c->session))
80         p.events = POLLOUT;
81     else
82         p.events = POLLIN;
83 #elif CONFIG_OPENSSL
84     ret = SSL_get_error(c->ssl, ret);
85     if (ret == SSL_ERROR_WANT_READ) {
86         p.events = POLLIN;
87     } else if (ret == SSL_ERROR_WANT_WRITE) {
88         p.events = POLLOUT;
89     } else {
90         av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
91         return AVERROR(EIO);
92     }
93 #endif
94     if (h->flags & URL_FLAG_NONBLOCK)
95         return AVERROR(EAGAIN);
96     while (1) {
97         int n = poll(&p, 1, 100);
98         if (n > 0)
99             break;
100         if (url_interrupt_cb())
101             return AVERROR(EINTR);
102     }
103     return 0;
104 }
105
106 static int tls_open(URLContext *h, const char *uri, int flags)
107 {
108     TLSContext *c = h->priv_data;
109     int ret;
110     int port;
111     char buf[200], host[200];
112     int numerichost = 0;
113     struct addrinfo hints = { 0 }, *ai = NULL;
114
115     ff_tls_init();
116
117     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0, uri);
118     ff_url_join(buf, sizeof(buf), "tcp", NULL, host, port, NULL);
119
120     hints.ai_flags = AI_NUMERICHOST;
121     if (!getaddrinfo(host, NULL, &hints, &ai)) {
122         numerichost = 1;
123         freeaddrinfo(ai);
124     }
125
126     ret = ffurl_open(&c->tcp, buf, AVIO_FLAG_READ_WRITE);
127     if (ret)
128         goto fail;
129     c->fd = ffurl_get_file_handle(c->tcp);
130
131 #if CONFIG_GNUTLS
132     gnutls_init(&c->session, GNUTLS_CLIENT);
133     if (!numerichost)
134         gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
135     gnutls_certificate_allocate_credentials(&c->cred);
136     gnutls_certificate_set_verify_flags(c->cred, 0);
137     gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
138     gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
139                                          (intptr_t) c->fd);
140     gnutls_priority_set_direct(c->session, "NORMAL", NULL);
141     while (1) {
142         ret = gnutls_handshake(c->session);
143         if (ret == 0)
144             break;
145         if ((ret = do_tls_poll(h, ret)) < 0)
146             goto fail;
147     }
148 #elif CONFIG_OPENSSL
149     c->ctx = SSL_CTX_new(SSLv3_client_method());
150     if (!c->ctx) {
151         av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
152         ret = AVERROR(EIO);
153         goto fail;
154     }
155     c->ssl = SSL_new(c->ctx);
156     if (!c->ssl) {
157         av_log(NULL, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
158         ret = AVERROR(EIO);
159         goto fail;
160     }
161     SSL_set_fd(c->ssl, c->fd);
162     if (!numerichost)
163         SSL_set_tlsext_host_name(c->ssl, host);
164     while (1) {
165         ret = SSL_connect(c->ssl);
166         if (ret > 0)
167             break;
168         if (ret == 0) {
169             av_log(NULL, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
170             ret = AVERROR(EIO);
171             goto fail;
172         }
173         if ((ret = do_tls_poll(h, ret)) < 0)
174             goto fail;
175     }
176 #endif
177     return 0;
178 fail:
179     TLS_free(c);
180     if (c->tcp)
181         ffurl_close(c->tcp);
182     ff_tls_deinit();
183     return ret;
184 }
185
186 static int tls_read(URLContext *h, uint8_t *buf, int size)
187 {
188     TLSContext *c = h->priv_data;
189     while (1) {
190         int ret = TLS_read(c, buf, size);
191         if (ret > 0)
192             return ret;
193         if (ret == 0)
194             return AVERROR(EIO);
195         if ((ret = do_tls_poll(h, ret)) < 0)
196             return ret;
197     }
198     return 0;
199 }
200
201 static int tls_write(URLContext *h, const uint8_t *buf, int size)
202 {
203     TLSContext *c = h->priv_data;
204     while (1) {
205         int ret = TLS_write(c, buf, size);
206         if (ret > 0)
207             return ret;
208         if (ret == 0)
209             return AVERROR(EIO);
210         if ((ret = do_tls_poll(h, ret)) < 0)
211             return ret;
212     }
213     return 0;
214 }
215
216 static int tls_close(URLContext *h)
217 {
218     TLSContext *c = h->priv_data;
219     TLS_shutdown(c);
220     TLS_free(c);
221     ffurl_close(c->tcp);
222     ff_tls_deinit();
223     return 0;
224 }
225
226 URLProtocol ff_tls_protocol = {
227     .name           = "tls",
228     .url_open       = tls_open,
229     .url_read       = tls_read,
230     .url_write      = tls_write,
231     .url_seek       = NULL,
232     .url_close      = tls_close,
233     .priv_data_size = sizeof(TLSContext),
234 };