tls_openssl: Readd support for nonblocking operation
[libav.git] / libavformat / tls_openssl.c
CommitLineData
d8ffb205 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 "internal.h"
24#include "network.h"
25#include "os_support.h"
26#include "url.h"
27#include "tls.h"
57cde2b1 28#include "libavcodec/internal.h"
d8ffb205 29#include "libavutil/avstring.h"
57cde2b1 30#include "libavutil/avutil.h"
d8ffb205 31#include "libavutil/opt.h"
32#include "libavutil/parseutils.h"
57cde2b1 33#include "libavutil/thread.h"
d8ffb205 34
35#include <openssl/bio.h>
36#include <openssl/ssl.h>
37#include <openssl/err.h>
38
57cde2b1 39static int openssl_init;
40
d8ffb205 41typedef struct TLSContext {
42 const AVClass *class;
43 TLSShared tls_shared;
44 SSL_CTX *ctx;
45 SSL *ssl;
ee050797
MO
46#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
47 BIO_METHOD* url_bio_method;
48#endif
d8ffb205 49} TLSContext;
50
57cde2b1 51#if HAVE_THREADS
52#include <openssl/crypto.h>
53pthread_mutex_t *openssl_mutexes;
54static void openssl_lock(int mode, int type, const char *file, int line)
55{
56 if (mode & CRYPTO_LOCK)
57 pthread_mutex_lock(&openssl_mutexes[type]);
58 else
59 pthread_mutex_unlock(&openssl_mutexes[type]);
60}
61#if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
62static unsigned long openssl_thread_id(void)
63{
64 return (intptr_t) pthread_self();
65}
66#endif
67#endif
68
69void ff_openssl_init(void)
70{
71 avpriv_lock_avformat();
72 if (!openssl_init) {
73 SSL_library_init();
74 SSL_load_error_strings();
75#if HAVE_THREADS
76 if (!CRYPTO_get_locking_callback()) {
77 int i;
78 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
79 for (i = 0; i < CRYPTO_num_locks(); i++)
80 pthread_mutex_init(&openssl_mutexes[i], NULL);
81 CRYPTO_set_locking_callback(openssl_lock);
82#if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
83 CRYPTO_set_id_callback(openssl_thread_id);
84#endif
85 }
86#endif
87 }
88 openssl_init++;
89 avpriv_unlock_avformat();
90}
91
92void ff_openssl_deinit(void)
93{
94 avpriv_lock_avformat();
95 openssl_init--;
96 if (!openssl_init) {
97#if HAVE_THREADS
98 if (CRYPTO_get_locking_callback() == openssl_lock) {
99 int i;
100 CRYPTO_set_locking_callback(NULL);
101 for (i = 0; i < CRYPTO_num_locks(); i++)
102 pthread_mutex_destroy(&openssl_mutexes[i]);
103 av_free(openssl_mutexes);
104 }
105#endif
106 }
107 avpriv_unlock_avformat();
108}
109
d8ffb205 110static int print_tls_error(URLContext *h, int ret)
111{
0671eb23
MS
112 TLSContext *c = h->priv_data;
113 if (h->flags & AVIO_FLAG_NONBLOCK) {
114 int err = SSL_get_error(c->ssl, ret);
115 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_READ)
116 return AVERROR(EAGAIN);
117 }
d8ffb205 118 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
119 return AVERROR(EIO);
120}
121
122static int tls_close(URLContext *h)
123{
124 TLSContext *c = h->priv_data;
125 if (c->ssl) {
126 SSL_shutdown(c->ssl);
127 SSL_free(c->ssl);
128 }
129 if (c->ctx)
130 SSL_CTX_free(c->ctx);
131 if (c->tls_shared.tcp)
132 ffurl_close(c->tls_shared.tcp);
ee050797
MO
133#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
134 if (c->url_bio_method)
135 BIO_meth_free(c->url_bio_method);
136#endif
57cde2b1 137 ff_openssl_deinit();
d8ffb205 138 return 0;
139}
140
141static int url_bio_create(BIO *b)
142{
ee050797
MO
143#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
144 BIO_set_init(b, 1);
145 BIO_set_data(b, NULL);
146 BIO_set_flags(b, 0);
147#else
d8ffb205 148 b->init = 1;
149 b->ptr = NULL;
150 b->flags = 0;
ee050797 151#endif
d8ffb205 152 return 1;
153}
154
155static int url_bio_destroy(BIO *b)
156{
157 return 1;
158}
159
ee050797 160#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
f22363c7 161#define GET_BIO_DATA(x) BIO_get_data(x)
ee050797 162#else
f22363c7 163#define GET_BIO_DATA(x) (x)->ptr
ee050797
MO
164#endif
165
d8ffb205 166static int url_bio_bread(BIO *b, char *buf, int len)
167{
ee050797 168 URLContext *h = GET_BIO_DATA(b);
d8ffb205 169 int ret = ffurl_read(h, buf, len);
170 if (ret >= 0)
171 return ret;
172 BIO_clear_retry_flags(b);
0671eb23
MS
173 if (ret == AVERROR(EAGAIN))
174 BIO_set_retry_read(b);
d8ffb205 175 if (ret == AVERROR_EXIT)
176 return 0;
177 return -1;
178}
179
180static int url_bio_bwrite(BIO *b, const char *buf, int len)
181{
ee050797 182 URLContext *h = GET_BIO_DATA(b);
d8ffb205 183 int ret = ffurl_write(h, buf, len);
184 if (ret >= 0)
185 return ret;
186 BIO_clear_retry_flags(b);
0671eb23
MS
187 if (ret == AVERROR(EAGAIN))
188 BIO_set_retry_write(b);
d8ffb205 189 if (ret == AVERROR_EXIT)
190 return 0;
191 return -1;
192}
193
194static long url_bio_ctrl(BIO *b, int cmd, long num, void *ptr)
195{
196 if (cmd == BIO_CTRL_FLUSH) {
197 BIO_clear_retry_flags(b);
198 return 1;
199 }
200 return 0;
201}
202
203static int url_bio_bputs(BIO *b, const char *str)
204{
205 return url_bio_bwrite(b, str, strlen(str));
206}
207
ee050797 208#if OPENSSL_VERSION_NUMBER < 0x1010000fL
d8ffb205 209static BIO_METHOD url_bio_method = {
210 .type = BIO_TYPE_SOURCE_SINK,
211 .name = "urlprotocol bio",
212 .bwrite = url_bio_bwrite,
213 .bread = url_bio_bread,
214 .bputs = url_bio_bputs,
215 .bgets = NULL,
216 .ctrl = url_bio_ctrl,
217 .create = url_bio_create,
218 .destroy = url_bio_destroy,
219};
ee050797 220#endif
d8ffb205 221
222static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
223{
224 TLSContext *p = h->priv_data;
225 TLSShared *c = &p->tls_shared;
226 BIO *bio;
227 int ret;
228
57cde2b1 229 ff_openssl_init();
d8ffb205 230
231 if ((ret = ff_tls_open_underlying(c, h, uri, options)) < 0)
232 goto fail;
233
218ed725
MT
234 // We want to support all versions of TLS >= 1.0, but not the deprecated
235 // and insecure SSLv2 and SSLv3. Despite the name, SSLv23_*_method()
236 // enables support for all versions of SSL and TLS, and we then disable
237 // support for the old protocols immediately after creating the context.
238 p->ctx = SSL_CTX_new(c->listen ? SSLv23_server_method() : SSLv23_client_method());
d8ffb205 239 if (!p->ctx) {
240 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
241 ret = AVERROR(EIO);
242 goto fail;
243 }
218ed725 244 SSL_CTX_set_options(p->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
d8ffb205 245 if (c->ca_file)
246 SSL_CTX_load_verify_locations(p->ctx, c->ca_file, NULL);
247 if (c->cert_file && !SSL_CTX_use_certificate_chain_file(p->ctx, c->cert_file)) {
248 av_log(h, AV_LOG_ERROR, "Unable to load cert file %s: %s\n",
249 c->cert_file, ERR_error_string(ERR_get_error(), NULL));
250 ret = AVERROR(EIO);
251 goto fail;
252 }
253 if (c->key_file && !SSL_CTX_use_PrivateKey_file(p->ctx, c->key_file, SSL_FILETYPE_PEM)) {
254 av_log(h, AV_LOG_ERROR, "Unable to load key file %s: %s\n",
255 c->key_file, ERR_error_string(ERR_get_error(), NULL));
256 ret = AVERROR(EIO);
257 goto fail;
258 }
259 // Note, this doesn't check that the peer certificate actually matches
260 // the requested hostname.
261 if (c->verify)
262 SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER, NULL);
263 p->ssl = SSL_new(p->ctx);
264 if (!p->ssl) {
265 av_log(h, AV_LOG_ERROR, "%s\n", ERR_error_string(ERR_get_error(), NULL));
266 ret = AVERROR(EIO);
267 goto fail;
268 }
ee050797
MO
269#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
270 p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
271 BIO_meth_set_write(p->url_bio_method, url_bio_bwrite);
272 BIO_meth_set_read(p->url_bio_method, url_bio_bread);
273 BIO_meth_set_puts(p->url_bio_method, url_bio_bputs);
274 BIO_meth_set_ctrl(p->url_bio_method, url_bio_ctrl);
275 BIO_meth_set_create(p->url_bio_method, url_bio_create);
276 BIO_meth_set_destroy(p->url_bio_method, url_bio_destroy);
277 bio = BIO_new(p->url_bio_method);
278 BIO_set_data(bio, c->tcp);
279#else
d8ffb205 280 bio = BIO_new(&url_bio_method);
281 bio->ptr = c->tcp;
ee050797 282#endif
d8ffb205 283 SSL_set_bio(p->ssl, bio, bio);
284 if (!c->listen && !c->numerichost)
285 SSL_set_tlsext_host_name(p->ssl, c->host);
286 ret = c->listen ? SSL_accept(p->ssl) : SSL_connect(p->ssl);
287 if (ret == 0) {
288 av_log(h, AV_LOG_ERROR, "Unable to negotiate TLS/SSL session\n");
289 ret = AVERROR(EIO);
290 goto fail;
291 } else if (ret < 0) {
292 ret = print_tls_error(h, ret);
293 goto fail;
294 }
295
296 return 0;
297fail:
298 tls_close(h);
299 return ret;
300}
301
302static int tls_read(URLContext *h, uint8_t *buf, int size)
303{
304 TLSContext *c = h->priv_data;
0671eb23
MS
305 int ret;
306 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
307 c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
308 c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
309 ret = SSL_read(c->ssl, buf, size);
d8ffb205 310 if (ret > 0)
311 return ret;
312 if (ret == 0)
313 return AVERROR_EOF;
314 return print_tls_error(h, ret);
315}
316
317static int tls_write(URLContext *h, const uint8_t *buf, int size)
318{
319 TLSContext *c = h->priv_data;
0671eb23
MS
320 int ret;
321 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
322 c->tls_shared.tcp->flags &= ~AVIO_FLAG_NONBLOCK;
323 c->tls_shared.tcp->flags |= h->flags & AVIO_FLAG_NONBLOCK;
324 ret = SSL_write(c->ssl, buf, size);
d8ffb205 325 if (ret > 0)
326 return ret;
327 if (ret == 0)
328 return AVERROR_EOF;
329 return print_tls_error(h, ret);
330}
331
332static const AVOption options[] = {
333 TLS_COMMON_OPTIONS(TLSContext, tls_shared),
334 { NULL }
335};
336
337static const AVClass tls_class = {
338 .class_name = "tls",
339 .item_name = av_default_item_name,
340 .option = options,
341 .version = LIBAVUTIL_VERSION_INT,
342};
343
61cec5ad 344const URLProtocol ff_tls_protocol = {
d8ffb205 345 .name = "tls",
346 .url_open2 = tls_open,
347 .url_read = tls_read,
348 .url_write = tls_write,
349 .url_close = tls_close,
350 .priv_data_size = sizeof(TLSContext),
351 .flags = URL_PROTOCOL_FLAG_NETWORK,
352 .priv_data_class = &tls_class,
353};