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
33
34 #include <openssl/bio.h>
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
37
39
45 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
47 #endif
50
51 #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
52 #include <openssl/crypto.h>
54 static void openssl_lock(
int mode,
int type,
const char *file,
int line)
55 {
56 if (
mode & CRYPTO_LOCK)
58 else
60 }
61 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
62 static unsigned long openssl_thread_id(void)
63 {
64 return (intptr_t) pthread_self();
65 }
66 #endif
67 #endif
68
70 {
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()) {
86 if (!openssl_mutexes) {
89 }
90
91 for (
i = 0;
i < CRYPTO_num_locks();
i++)
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 }
102
103 return 0;
104 }
105
107 {
111 #if HAVE_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L
112 if (CRYPTO_get_locking_callback() == openssl_lock) {
114 CRYPTO_set_locking_callback(
NULL);
115 for (
i = 0;
i < CRYPTO_num_locks();
i++)
118 }
119 #endif
120 }
122 }
123
125 {
129 int err = SSL_get_error(
c->ssl,
ret);
130 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
132 }
133 while ((e = ERR_get_error()) != 0) {
135 printed = 1;
136 }
139 printed = 1;
142 }
143 if (!printed)
146 }
147
149 {
152 SSL_shutdown(
c->ssl);
154 }
156 SSL_CTX_free(
c->ctx);
158 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
159 if (
c->url_bio_method)
160 BIO_meth_free(
c->url_bio_method);
161 #endif
163 return 0;
164 }
165
167 {
168 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
170 BIO_set_data(
b,
NULL);
172 #else
176 #endif
177 return 1;
178 }
179
181 {
182 return 1;
183 }
184
185 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
186 #define GET_BIO_DATA(x) BIO_get_data(x)
187 #else
188 #define GET_BIO_DATA(x) (x)->ptr
189 #endif
190
192 {
197 BIO_clear_retry_flags(
b);
199 return 0;
201 BIO_set_retry_read(
b);
202 else
204 return -1;
205 }
206
208 {
213 BIO_clear_retry_flags(
b);
215 return 0;
217 BIO_set_retry_write(
b);
218 else
220 return -1;
221 }
222
224 {
225 if (cmd == BIO_CTRL_FLUSH) {
226 BIO_clear_retry_flags(
b);
227 return 1;
228 }
229 return 0;
230 }
231
233 {
235 }
236
237 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
239 .type = BIO_TYPE_SOURCE_SINK,
240 .name = "urlprotocol bio",
248 };
249 #endif
250
252 {
255 BIO *bio;
257
260
263
264 // We want to support all versions of TLS >= 1.0, but not the deprecated
265 // and insecure SSLv2 and SSLv3. Despite the name, SSLv23_*_method()
266 // enables support for all versions of SSL and TLS, and we then disable
267 // support for the old protocols immediately after creating the context.
268 p->
ctx = SSL_CTX_new(
c->listen ? SSLv23_server_method() : SSLv23_client_method());
273 }
274 SSL_CTX_set_options(p->
ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
276 if (!SSL_CTX_load_verify_locations(p->
ctx,
c->ca_file,
NULL))
278 }
279 if (
c->cert_file && !SSL_CTX_use_certificate_chain_file(p->
ctx,
c->cert_file)) {
281 c->cert_file, ERR_error_string(ERR_get_error(),
NULL));
284 }
285 if (
c->key_file && !SSL_CTX_use_PrivateKey_file(p->
ctx,
c->key_file, SSL_FILETYPE_PEM)) {
287 c->key_file, ERR_error_string(ERR_get_error(),
NULL));
290 }
291 // Note, this doesn't check that the peer certificate actually matches
292 // the requested hostname.
294 SSL_CTX_set_verify(p->
ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
NULL);
300 }
301 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
302 p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "urlprotocol bio");
309 bio = BIO_new(p->url_bio_method);
310 BIO_set_data(bio, p);
311 #else
313 bio->ptr = p;
314 #endif
315 SSL_set_bio(p->
ssl, bio, bio);
316 if (!
c->listen && !
c->numerichost)
317 SSL_set_tlsext_host_name(p->
ssl,
c->host);
318 ret =
c->listen ? SSL_accept(p->
ssl) : SSL_connect(p->
ssl);
323 }
else if (
ret < 0) {
326 }
327
328 return 0;
332 }
333
335 {
338 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
347 }
348
350 {
353 // Set or clear the AVIO_FLAG_NONBLOCK on c->tls_shared.tcp
362 }
363
365 {
368 }
369
371 {
374 }
375
379 };
380
386 };
387
399 };