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