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
27 #if CONFIG_GNUTLS
28 #include <gnutls/gnutls.h>
29 #include <gnutls/x509.h>
30 #define TLS_read(c, buf, size) gnutls_record_recv(c->session, buf, size)
31 #define TLS_write(c, buf, size) gnutls_record_send(c->session, buf, size)
32 #define TLS_shutdown(c) gnutls_bye(c->session, GNUTLS_SHUT_RDWR)
33 #define TLS_free(c) do { \
34 if (c->session) \
35 gnutls_deinit(c->session); \
36 if (c->cred) \
37 gnutls_certificate_free_credentials(c->cred); \
38 } while (0)
39 #elif CONFIG_OPENSSL
40 #include <openssl/bio.h>
41 #include <openssl/ssl.h>
42 #include <openssl/err.h>
43 #define TLS_read(c, buf, size) SSL_read(c->ssl, buf, size)
44 #define TLS_write(c, buf, size) SSL_write(c->ssl, buf, size)
45 #define TLS_shutdown(c) SSL_shutdown(c->ssl)
46 #define TLS_free(c) do { \
47 if (c->ssl) \
48 SSL_free(c->ssl); \
49 if (c->ctx) \
50 SSL_CTX_free(c->ctx); \
51 } while (0)
52 #endif
56 #if HAVE_POLL_H
57 #include <poll.h>
58 #endif
59
63 #if CONFIG_GNUTLS
64 gnutls_session_t session;
65 gnutls_certificate_credentials_t cred;
66 #elif CONFIG_OPENSSL
67 SSL_CTX *ctx;
68 SSL *ssl;
69 #endif
77
78 #define OFFSET(x) offsetof(TLSContext, x)
79 #define D AV_OPT_FLAG_DECODING_PARAM
80 #define E AV_OPT_FLAG_ENCODING_PARAM
84 {
"tls_verify",
"Verify the peer certificate",
OFFSET(verify),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags =
D|
E },
87 {
"listen",
"Listen for incoming connections",
OFFSET(listen),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags =
D|
E },
88 { NULL }
89 };
90
96 };
97
99 {
101 struct pollfd p = { c->
fd, 0, 0 };
102 #if CONFIG_GNUTLS
103 switch (ret) {
104 case GNUTLS_E_AGAIN:
105 case GNUTLS_E_INTERRUPTED:
106 break;
107 case GNUTLS_E_WARNING_ALERT_RECEIVED:
109 break;
110 default:
113 }
114 if (gnutls_record_get_direction(c->session))
115 p.events = POLLOUT;
116 else
117 p.events = POLLIN;
118 #elif CONFIG_OPENSSL
119 ret = SSL_get_error(c->ssl, ret);
120 if (ret == SSL_ERROR_WANT_READ) {
121 p.events = POLLIN;
122 } else if (ret == SSL_ERROR_WANT_WRITE) {
123 p.events = POLLOUT;
124 } else {
127 }
128 #endif
131 while (1) {
132 int n = poll(&p, 1, 100);
133 if (n > 0)
134 break;
137 }
138 return 0;
139 }
140
142 {
145 const char *p = strchr(uri, '?');
146 if (!p)
147 return;
148
151
153 char *endptr = NULL;
154 c->
verify = strtol(buf, &endptr, 10);
155 if (buf == endptr)
157 }
158
161
164 }
165
167 {
170 int port;
171 char buf[200], host[200], opts[50] =
"";
172 int numerichost = 0;
173 struct addrinfo hints = { 0 }, *ai = NULL;
174 const char *proxy_path;
175 int use_proxy;
176 const char *p = strchr(uri, '?');
177
179
183 snprintf(opts,
sizeof(opts),
"?listen=1");
184
185 av_url_split(NULL, 0, NULL, 0, host,
sizeof(host), &port, NULL, 0, uri);
186 ff_url_join(buf,
sizeof(buf),
"tcp", NULL, host, port,
"%s", opts);
187
190 numerichost = 1;
192 }
193
194 proxy_path = getenv("http_proxy");
196 proxy_path != NULL &&
av_strstart(proxy_path,
"http://", NULL);
197
198 if (use_proxy) {
199 char proxy_host[200], proxy_auth[200], dest[200];
200 int proxy_port;
202 proxy_host, sizeof(proxy_host), &proxy_port, NULL, 0,
203 proxy_path);
204 ff_url_join(dest,
sizeof(dest), NULL, NULL, host, port, NULL);
205 ff_url_join(buf,
sizeof(buf),
"httpproxy", proxy_auth, proxy_host,
206 proxy_port, "/%s", dest);
207 }
208
211 if (ret)
212 goto fail;
214
215 #if CONFIG_GNUTLS
216 gnutls_init(&c->session, c->
listen ? GNUTLS_SERVER : GNUTLS_CLIENT);
217 if (!c->
listen && !numerichost)
218 gnutls_server_name_set(c->session, GNUTLS_NAME_DNS, host, strlen(host));
219 gnutls_certificate_allocate_credentials(&c->cred);
222 ret = gnutls_certificate_set_x509_trust_file(c->cred, c->
ca_file, GNUTLS_X509_FMT_PEM);
223 if (ret < 0)
225 }
226 #if GNUTLS_VERSION_MAJOR >= 3
227 else
228 gnutls_certificate_set_x509_system_trust(c->cred);
229 #endif
230 gnutls_certificate_set_verify_flags(c->cred, c->
verify ?
231 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT : 0);
233 ret = gnutls_certificate_set_x509_key_file(c->cred,
235 GNUTLS_X509_FMT_PEM);
236 if (ret < 0) {
238 "Unable to set cert/key files %s and %s: %s\n",
241 goto fail;
242 }
245 gnutls_credentials_set(c->session, GNUTLS_CRD_CERTIFICATE, c->cred);
246 gnutls_transport_set_ptr(c->session, (gnutls_transport_ptr_t)
248 gnutls_priority_set_direct(c->session, "NORMAL", NULL);
249 while (1) {
250 ret = gnutls_handshake(c->session);
251 if (ret == 0)
252 break;
254 goto fail;
255 }
257 unsigned int status, cert_list_size;
258 gnutls_x509_crt_t cert;
259 const gnutls_datum_t *cert_list;
260 if ((ret = gnutls_certificate_verify_peers2(c->session, &status)) < 0) {
262 gnutls_strerror(ret));
264 goto fail;
265 }
266 if (status & GNUTLS_CERT_INVALID) {
269 goto fail;
270 }
271 if (gnutls_certificate_type_get(c->session) != GNUTLS_CRT_X509) {
274 goto fail;
275 }
276 gnutls_x509_crt_init(&cert);
277 cert_list = gnutls_certificate_get_peers(c->session, &cert_list_size);
278 gnutls_x509_crt_import(cert, cert_list, GNUTLS_X509_FMT_DER);
279 ret = gnutls_x509_crt_check_hostname(cert, host);
280 gnutls_x509_crt_deinit(cert);
281 if (!ret) {
283 "The certificate's owner does not match hostname %s\n", host);
285 goto fail;
286 }
287 }
288 #elif CONFIG_OPENSSL
289 c->ctx = SSL_CTX_new(c->
listen ? TLSv1_server_method() : TLSv1_client_method());
290 if (!c->ctx) {
293 goto fail;
294 }
297 if (!SSL_CTX_load_verify_locations(c->ctx, c->
ca_file, NULL))
298 av_log(h,
AV_LOG_ERROR,
"SSL_CTX_load_verify_locations %s\n", ERR_error_string(ERR_get_error(), NULL));
299 }
302 c->
cert_file, ERR_error_string(ERR_get_error(), NULL));
304 goto fail;
305 }
306 if (c->
key_file && !SSL_CTX_use_PrivateKey_file(c->ctx, c->
key_file, SSL_FILETYPE_PEM)) {
308 c->
key_file, ERR_error_string(ERR_get_error(), NULL));
310 goto fail;
311 }
312 // Note, this doesn't check that the peer certificate actually matches
313 // the requested hostname.
315 SSL_CTX_set_verify(c->ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
316 c->ssl = SSL_new(c->ctx);
317 if (!c->ssl) {
320 goto fail;
321 }
322 SSL_set_fd(c->ssl, c->
fd);
323 if (!c->
listen && !numerichost)
324 SSL_set_tlsext_host_name(c->ssl, host);
325 while (1) {
326 ret = c->
listen ? SSL_accept(c->ssl) : SSL_connect(c->ssl);
327 if (ret > 0)
328 break;
329 if (ret == 0) {
332 goto fail;
333 }
335 goto fail;
336 }
337 #endif
338 return 0;
339 fail:
340 TLS_free(c);
345 }
346
348 {
350 while (1) {
351 int ret = TLS_read(c, buf, size);
352 if (ret > 0)
354 if (ret == 0)
358 }
359 return 0;
360 }
361
363 {
365 while (1) {
366 int ret = TLS_write(c, buf, size);
367 if (ret > 0)
369 if (ret == 0)
373 }
374 return 0;
375 }
376
378 {
380 TLS_shutdown(c);
381 TLS_free(c);
384 return 0;
385 }
386
395 .priv_data_class = &tls_class,
396 };