Debian Codegraph @

src/openssl.c

raw
1 /* SSL support via OpenSSL library.
2 Copyright (C) 2000-2012, 2015, 2018-2024 Free Software Foundation,
3 Inc.
4 Originally contributed by Christian Fraenkel.
5
6 This file is part of GNU Wget.
7
8 GNU Wget is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Wget is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20
21 Additional permission under GNU GPL version 3 section 7
22
23 If you modify this program, or any covered work, by linking or
24 combining it with the OpenSSL project's OpenSSL library (or a
25 modified version of that library), containing parts covered by the
26 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
27 grants you additional permission to convey the resulting work.
28 Corresponding Source for a non-source form of such a combination
29 shall include the source code for the parts of OpenSSL used as well
30 as that of the covered work. */
31
32 #include "wget.h"
33
34 #include <assert.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <xalloc.h>
39
40 #include <openssl/ssl.h>
41 #include <openssl/x509v3.h>
42 #include <openssl/err.h>
43 #include <openssl/rand.h>
44 #include <openssl/bio.h>
45 #if OPENSSL_VERSION_NUMBER >= 0x00907000
46 #include <openssl/conf.h>
47 #ifndef OPENSSL_NO_ENGINE
48 #include <openssl/engine.h>
49 #endif
50 #endif
51
52 #include <sys/ioctl.h>
53
54 #include "utils.h"
55 #include "connect.h"
56 #include "ptimer.h"
57 #include "url.h"
58 #include "ssl.h"
59 #include "exits.h"
60
61 #include <fcntl.h>
62
63 #ifdef WINDOWS
64 # include <w32sock.h>
65 #endif
66
67 /* Application-wide SSL context. This is common to all SSL
68 connections. */
69 static SSL_CTX *ssl_ctx;
70
71 /* Initialize the SSL's PRNG using various methods. */
72
73 static void
74 init_prng (void)
75 {
76 char namebuf[256];
77 const char *random_file;
78
79 /* Seed from a file specified by the user. This will be the file
80 specified with --random-file, $RANDFILE, if set, or ~/.rnd, if it
81 exists. */
82 if (opt.random_file)
83 random_file = opt.random_file;
84 else
85 {
86 /* Get the random file name using RAND_file_name. */
87 namebuf[0] = '\0';
88 random_file = RAND_file_name (namebuf, sizeof (namebuf));
89 if (!file_exists_p (random_file, NULL))
90 random_file = NULL;
91 }
92
93 if (random_file && *random_file)
94 /* Seed at most 16k (apparently arbitrary value borrowed from
95 curl) from random file. */
96 {
97 int _err = RAND_load_file (random_file, 16384);
98 if(_err == -1)
99 /* later the thread error queue will be cleared */
100 if ( (_err = ERR_peek_last_error ()) )
101 logprintf (LOG_VERBOSE, "WARNING: Could not load random file: %s, %s\n", opt.random_file, ERR_reason_error_string(_err));
102 }
103
104 #ifdef HAVE_RAND_EGD
105 /* Get random data from EGD if opt.egd_file was used. */
106 if (opt.egd_file && *opt.egd_file)
107 RAND_egd (opt.egd_file);
108 #endif
109
110 #ifdef WINDOWS
111 /* Under Windows, we can try to seed the PRNG using screen content.
112 This may or may not work, depending on whether we'll calling Wget
113 interactively. */
114
115 RAND_screen ();
116 if (RAND_status ())
117 return;
118 #endif
119
120 #if 0 /* don't do this by default */
121 {
122 int maxrand = 500;
123
124 /* Still not random enough, presumably because neither /dev/random
125 nor EGD were available. Try to seed OpenSSL's PRNG with libc
126 PRNG. This is cryptographically weak and defeats the purpose
127 of using OpenSSL, which is why it is highly discouraged. */
128
129 logprintf (LOG_NOTQUIET, _("WARNING: using a weak random seed.\n"));
130
131 while (RAND_status () == 0 && maxrand-- > 0)
132 {
133 unsigned char rnd = random_number (256);
134 RAND_seed (&rnd, sizeof (rnd));
135 }
136 }
137 #endif
138 }
139
140 /* Print errors in the OpenSSL error stack. */
141
142 static void
143 print_errors (void)
144 {
145 unsigned long err;
146 while ((err = ERR_get_error ()) != 0)
147 logprintf (LOG_NOTQUIET, "OpenSSL: %s\n", ERR_error_string (err, NULL));
148 }
149
150 /* Convert keyfile type as used by options.h to a type as accepted by
151 SSL_CTX_use_certificate_file and SSL_CTX_use_PrivateKey_file.
152
153 (options.h intentionally doesn't use values from openssl/ssl.h so
154 it doesn't depend specifically on OpenSSL for SSL functionality.) */
155
156 static int
157 key_type_to_ssl_type (enum keyfile_type type)
158 {
159 switch (type)
160 {
161 case keyfile_pem:
162 return SSL_FILETYPE_PEM;
163 case keyfile_asn1:
164 return SSL_FILETYPE_ASN1;
165 default:
166 abort ();
167 }
168 }
169
170 /* SSL has been initialized */
171 static int ssl_true_initialized = 0;
172
173 /* Create an SSL Context and set default paths etc. Called the first
174 time an HTTP download is attempted.
175
176 Returns true on success, false otherwise. */
177
178 bool
179 ssl_init (void)
180 {
181 SSL_METHOD const *meth = NULL;
182 long ssl_options = 0;
183 char *ciphers_string = NULL;
184 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
185 int ssl_proto_version = 0;
186 #endif
187
188 #if OPENSSL_VERSION_NUMBER >= 0x00907000
189 if (ssl_true_initialized == 0)
190 {
191 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
192 OPENSSL_init_ssl (OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_ENGINE_ALL_BUILTIN, NULL);
193 #else
194 OPENSSL_config (NULL);
195 #endif
196 ssl_true_initialized = 1;
197 }
198 #endif
199
200 if (ssl_ctx)
201 /* The SSL has already been initialized. */
202 return true;
203
204 /* Init the PRNG. If that fails, bail out. */
205 init_prng ();
206 if (RAND_status () != 1)
207 {
208 logprintf (LOG_NOTQUIET,
209 _("Could not seed PRNG; consider using --random-file.\n"));
210 goto error;
211 }
212
213 #if defined(LIBRESSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x10100000L)
214 SSL_library_init ();
215 SSL_load_error_strings ();
216 SSLeay_add_all_algorithms ();
217 SSLeay_add_ssl_algorithms ();
218 #endif
219
220 switch (opt.secure_protocol)
221 {
222 case secure_protocol_sslv2:
223 #if !defined OPENSSL_NO_SSL2 && OPENSSL_VERSION_NUMBER < 0x10100000L
224 meth = SSLv2_client_method ();
225 #endif
226 break;
227
228 case secure_protocol_sslv3:
229 #ifndef OPENSSL_NO_SSL3_METHOD
230 meth = SSLv3_client_method ();
231 #endif
232 break;
233
234 case secure_protocol_auto:
235 case secure_protocol_pfs:
236 meth = SSLv23_client_method ();
237 ssl_options |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
238 break;
239 case secure_protocol_tlsv1:
240 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
241 meth = TLS_client_method();
242 ssl_proto_version = TLS1_VERSION;
243 #else
244 meth = TLSv1_client_method ();
245 #endif
246 break;
247
248 #if OPENSSL_VERSION_NUMBER >= 0x10001000
249 case secure_protocol_tlsv1_1:
250 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
251 meth = TLS_client_method();
252 ssl_proto_version = TLS1_1_VERSION;
253 #else
254 meth = TLSv1_1_client_method ();
255 #endif
256 break;
257
258 case secure_protocol_tlsv1_2:
259 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
260 meth = TLS_client_method();
261 ssl_proto_version = TLS1_2_VERSION;
262 #else
263 meth = TLSv1_2_client_method ();
264 #endif
265 break;
266
267 case secure_protocol_tlsv1_3:
268 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L) && defined TLS1_3_VERSION
269 meth = TLS_client_method();
270 ssl_proto_version = TLS1_3_VERSION;
271 #else
272 logprintf (LOG_NOTQUIET, _("Your OpenSSL version is too old to support TLS 1.3\n"));
273 goto error;
274 #endif
275 break;
276 #else
277 case secure_protocol_tlsv1_1:
278 logprintf (LOG_NOTQUIET, _("Your OpenSSL version is too old to support TLSv1.1\n"));
279 goto error;
280
281 case secure_protocol_tlsv1_2:
282 logprintf (LOG_NOTQUIET, _("Your OpenSSL version is too old to support TLSv1.2\n"));
283 goto error;
284
285 #endif
286
287 default:
288 logprintf (LOG_NOTQUIET, _("OpenSSL: unimplemented 'secure-protocol' option value %d\n"), opt.secure_protocol);
289 logprintf (LOG_NOTQUIET, _("Please report this issue to [email protected]\n"));
290 abort ();
291 }
292
293 if (!meth)
294 {
295 logprintf (LOG_NOTQUIET, _("Your OpenSSL version does not support option '%s'.\n"), opt.secure_protocol_name);
296 logprintf (LOG_NOTQUIET, _("Rebuilding Wget and/or OpenSSL may help in this situation.\n"));
297 exit (WGET_EXIT_GENERIC_ERROR);
298 }
299
300 /* The type cast below accommodates older OpenSSL versions (0.9.8)
301 where SSL_CTX_new() is declared without a "const" argument. */
302 ssl_ctx = SSL_CTX_new ((SSL_METHOD *) meth);
303 if (!ssl_ctx)
304 goto error;
305
306 if (ssl_options)
307 SSL_CTX_set_options (ssl_ctx, ssl_options);
308
309 #if ((OPENSSL_VERSION_NUMBER >= 0x10101000L) && \
310 !defined(LIBRESSL_VERSION_NUMBER) && \
311 !defined(OPENSSL_IS_BORINGSSL))
312 SSL_CTX_set_post_handshake_auth (ssl_ctx, 1);
313 #endif
314
315 #if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
316 if (ssl_proto_version)
317 SSL_CTX_set_min_proto_version(ssl_ctx, ssl_proto_version);
318 #endif
319
320 /* OpenSSL ciphers: https://www.openssl.org/docs/apps/ciphers.html
321 *
322 * Rules:
323 * 1. --ciphers overrides everything
324 * 2. We allow RSA key exchange by default (secure_protocol_auto)
325 * 3. We disallow RSA key exchange if PFS was requested (secure_protocol_pfs)
326 */
327 if (!opt.tls_ciphers_string)
328 {
329 if (opt.secure_protocol == secure_protocol_auto)
330 ciphers_string = "HIGH:!aNULL:!RC4:!MD5:!SRP:!PSK";
331 else if (opt.secure_protocol == secure_protocol_pfs)
332 ciphers_string = "HIGH:!aNULL:!RC4:!MD5:!SRP:!PSK:!kRSA";
333 }
334 else
335 {
336 ciphers_string = opt.tls_ciphers_string;
337 }
338
339 if (ciphers_string && !SSL_CTX_set_cipher_list(ssl_ctx, ciphers_string))
340 {
341 logprintf(LOG_NOTQUIET, _("OpenSSL: Invalid cipher list: %s\n"), ciphers_string);
342 goto error;
343 }
344
345 SSL_CTX_set_default_verify_paths (ssl_ctx);
346 SSL_CTX_load_verify_locations (ssl_ctx, opt.ca_cert, opt.ca_directory);
347
348 #ifdef X509_V_FLAG_PARTIAL_CHAIN
349 /* Set X509_V_FLAG_PARTIAL_CHAIN to allow the client to anchor trust in
350 * a non-self-signed certificate. This defies RFC 4158 (Path Building)
351 * which defines a trust anchor in terms of a self-signed certificate.
352 * However, it substantially reduces attack surface by pruning the tree
353 * of unneeded trust points. For example, the cross-certified
354 * Let's Encrypt X3 CA, which protects gnu.org and appears as an
355 * intermediate CA to clients, can be used as a trust anchor without
356 * the entire IdentTrust PKI.
357 */
358 X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
359 if (param)
360 {
361 /* We only want X509_V_FLAG_PARTIAL_CHAIN, but the OpenSSL docs
362 * say to use X509_V_FLAG_TRUSTED_FIRST also. It looks like
363 * X509_V_FLAG_TRUSTED_FIRST applies to a collection of trust
364 * anchors and not a single trust anchor.
365 */
366 (void) X509_VERIFY_PARAM_set_flags (param, X509_V_FLAG_TRUSTED_FIRST | X509_V_FLAG_PARTIAL_CHAIN);
367 if (SSL_CTX_set1_param (ssl_ctx, param) == 0)
368 logprintf(LOG_NOTQUIET, _("OpenSSL: Failed set trust to partial chain\n"));
369 /* We continue on error */
370 X509_VERIFY_PARAM_free (param);
371 }
372 else
373 {
374 logprintf(LOG_NOTQUIET, _("OpenSSL: Failed to allocate verification param\n"));
375 /* We continue on error */
376 }
377 #endif
378
379 if (opt.crl_file)
380 {
381 X509_STORE *store = SSL_CTX_get_cert_store (ssl_ctx);
382 X509_LOOKUP *lookup;
383
384 if (!(lookup = X509_STORE_add_lookup (store, X509_LOOKUP_file ()))
385 || (!X509_load_crl_file (lookup, opt.crl_file, X509_FILETYPE_PEM)))
386 goto error;
387
388 X509_STORE_set_flags (store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
389 }
390
391 /* SSL_VERIFY_NONE instructs OpenSSL not to abort SSL_connect if the
392 certificate is invalid. We verify the certificate separately in
393 ssl_check_certificate, which provides much better diagnostics
394 than examining the error stack after a failed SSL_connect. */
395 SSL_CTX_set_verify (ssl_ctx, SSL_VERIFY_NONE, NULL);
396
397 /* Use the private key from the cert file unless otherwise specified. */
398 if (opt.cert_file && !opt.private_key)
399 {
400 opt.private_key = xstrdup (opt.cert_file);
401 opt.private_key_type = opt.cert_type;
402 }
403
404 /* Use cert from private key file unless otherwise specified. */
405 if (opt.private_key && !opt.cert_file)
406 {
407 opt.cert_file = xstrdup (opt.private_key);
408 opt.cert_type = opt.private_key_type;
409 }
410
411 if (opt.cert_file)
412 if (SSL_CTX_use_certificate_file (ssl_ctx, opt.cert_file,
413 key_type_to_ssl_type (opt.cert_type))
414 != 1)
415 goto error;
416 if (opt.private_key)
417 if (SSL_CTX_use_PrivateKey_file (ssl_ctx, opt.private_key,
418 key_type_to_ssl_type (opt.private_key_type))
419 != 1)
420 goto error;
421
422 /* Since fd_write unconditionally assumes partial writes (and
423 handles them correctly), allow them in OpenSSL. */
424 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
425
426 /* The OpenSSL library can handle renegotiations automatically, so
427 tell it to do so. */
428 SSL_CTX_set_mode (ssl_ctx, SSL_MODE_AUTO_RETRY);
429
430 return true;
431
432 error:
433 if (ssl_ctx)
434 SSL_CTX_free (ssl_ctx);
435 print_errors ();
436 return false;
437 }
438
439 void
440 ssl_cleanup (void)
441 {
442 }
443
444 struct openssl_transport_context
445 {
446 SSL *conn; /* SSL connection handle */
447 SSL_SESSION *sess; /* SSL session info */
448 char *last_error; /* last error printed with openssl_errstr */
449 };
450
451 typedef int (*ssl_fn_t)(SSL *, void *, int);
452
453 #ifdef OPENSSL_RUN_WITHTIMEOUT
454
455 struct scwt_context
456 {
457 SSL *ssl;
458 int result;
459 };
460
461 static void
462 ssl_connect_with_timeout_callback(void *arg)
463 {
464 struct scwt_context *ctx = (struct scwt_context *)arg;
465 ctx->result = SSL_connect(ctx->ssl);
466 }
467
468 static int
469 ssl_connect_with_timeout(int fd _GL_UNUSED, SSL *conn, double timeout)
470 {
471 struct scwt_context scwt_ctx;
472 scwt_ctx.ssl = conn;
473 errno = 0;
474 if (run_with_timeout(timeout, ssl_connect_with_timeout_callback,
475 &scwt_ctx))
476 {
477 errno = ETIMEDOUT;
478 return -1;
479 }
480 return scwt_ctx.result;
481 }
482
483 struct openssl_read_args
484 {
485 int fd;
486 struct openssl_transport_context *ctx;
487 ssl_fn_t fn;
488 char *buf;
489 int bufsize;
490 int retval;
491 };
492
493 static void
494 openssl_read_peek_callback(void *arg)
495 {
496 struct openssl_read_args *args = (struct openssl_read_args *) arg;
497 struct openssl_transport_context *ctx = args->ctx;
498 ssl_fn_t fn = args->fn;
499 SSL *conn = ctx->conn;
500 char *buf = args->buf;
501 int bufsize = args->bufsize;
502 int ret;
503
504 do
505 {
506 ret = fn (conn, buf, bufsize);
507 }
508 while (ret == -1 && SSL_get_error (conn, ret) == SSL_ERROR_SYSCALL && errno == EINTR);
509 args->retval = ret;
510 }
511
512 static int
513 openssl_read_peek (int fd, char *buf, int bufsize, void *arg, double timeout, ssl_fn_t fn)
514 {
515 struct openssl_transport_context *ctx = arg;
516 int ret = SSL_pending (ctx->conn);
517
518 if (ret)
519 ret = fn (ctx->conn, buf, MIN (bufsize, ret));
520 else
521 {
522 struct openssl_read_args args;
523 args.fd = fd;
524 args.buf = buf;
525 args.bufsize = bufsize;
526 args.fn = fn;
527 args.ctx = ctx;
528
529 if (timeout == -1)
530 timeout = opt.read_timeout;
531
532 if (run_with_timeout(timeout, openssl_read_peek_callback, &args))
533 {
534 errno = ETIMEDOUT;
535 ret = -1;
536 }
537 else
538 ret = args.retval;
539 }
540 return ret;
541 }
542
543 #else /* OPENSSL_RUN_WITHTIMEOUT */
544
545 #ifdef F_GETFL
546 #define NONBLOCK_DECL int flags = 0;
547 #define FD_SET_NONBLOCKED(_fd) \
548 flags = fcntl (_fd, F_GETFL, 0); \
549 if (flags < 0) \
550 return flags; \
551 if (fcntl (_fd, F_SETFL, flags | O_NONBLOCK)) \
552 return -1;
553 #define FD_SET_BLOCKED(_fd) \
554 if (fcntl (_fd, F_SETFL, flags) < 0) \
555 return -1;
556 #else
557 #define NONBLOCK_DECL
558 #define FD_SET_NONBLOCKED(_fd) \
559 {\
560 const int one = 1;\
561 if (ioctl (_fd, FIONBIO, &one) < 0)\
562 return -1;\
563 }
564 #define FD_SET_BLOCKED(_fd) \
565 {\
566 const int zero = 0;\
567 if (ioctl (_fd, FIONBIO, &zero) < 0)\
568 return -1;\
569 }
570 #endif /* F_GETFL */
571
572 #define TIMER_INIT(_fd, _ret, _timeout) \
573 { \
574 NONBLOCK_DECL \
575 int timed_out = 0; \
576 FD_SET_NONBLOCKED(_fd) \
577 struct ptimer *timer = ptimer_new (); \
578 if (timer == NULL) \
579 _ret = -1; \
580 else \
581 { \
582 double next_timeout = _timeout;
583
584 #define TIMER_FREE(_fd) \
585 ptimer_destroy (timer); \
586 } \
587 FD_SET_BLOCKED(_fd) \
588 if (timed_out) \
589 { \
590 errno = ETIMEDOUT; \
591 } \
592 }
593
594 #define TIMER_WAIT(_fd, _conn, _ret, _timeout) \
595 { \
596 int wait_for; \
597 int err = SSL_get_error(_conn, _ret); \
598 if (err == SSL_ERROR_WANT_READ) \
599 wait_for = WAIT_FOR_READ; \
600 else if (err == SSL_ERROR_WANT_WRITE) \
601 wait_for = WAIT_FOR_WRITE; \
602 else \
603 break; \
604 err = select_fd_nb (_fd, next_timeout, wait_for); \
605 if (err <= 0) \
606 { \
607 if (err == 0) \
608 timedout: \
609 timed_out = 1; \
610 _ret = -1; \
611 break; \
612 } \
613 next_timeout = _timeout - ptimer_measure (timer); \
614 if (next_timeout <= 0) \
615 goto timedout; \
616 }
617
618 static int
619 ssl_connect_with_timeout(int fd, SSL *conn, double timeout)
620 {
621 int ret;
622
623 errno = 0;
624 if (timeout == 0)
625 ret = SSL_connect(conn);
626 else
627 {
628 TIMER_INIT(fd, ret, timeout)
629 ERR_clear_error();
630 while( (ret = SSL_connect(conn)) < 0 )
631 TIMER_WAIT(fd, conn, ret, timeout)
632 TIMER_FREE(fd)
633 }
634
635 return ret;
636 }
637
638 static int
639 openssl_read_peek (int fd, char *buf, int bufsize, void *arg, double timeout, ssl_fn_t fn)
640 {
641 struct openssl_transport_context *ctx = arg;
642 int ret = SSL_pending (ctx->conn);
643
644 if (timeout == -1)
645 timeout = opt.read_timeout;
646 /* If we have data available for immediate read, simply return that,
647 or do blocked read when timeout == 0 */
648 if (ret || timeout == 0)
649 do
650 {
651 ret = fn (ctx->conn, buf, (ret ? MIN (bufsize, ret) : bufsize));
652 }
653 while (ret == -1 && SSL_get_error (ctx->conn, ret) == SSL_ERROR_SYSCALL && errno == EINTR);
654 else
655 {
656 TIMER_INIT(fd, ret, timeout)
657 while( (ret = fn (ctx->conn, buf, bufsize)) <= 0 )
658 TIMER_WAIT(fd, ctx->conn, ret, timeout)
659 TIMER_FREE(fd)
660 }
661
662 return ret;
663 }
664
665 #endif /* OPENSSL_RUN_WITHTIMEOUT */
666
667 static int
668 openssl_read (int fd, char *buf, int bufsize, void *arg, double timeout)
669 {
670 return openssl_read_peek (fd, buf, bufsize, arg, timeout, SSL_read);
671 }
672
673 static int
674 openssl_write (int fd _GL_UNUSED, char *buf, int bufsize, void *arg)
675 {
676 int ret = 0;
677 struct openssl_transport_context *ctx = arg;
678 SSL *conn = ctx->conn;
679 do
680 ret = SSL_write (conn, buf, bufsize);
681 while (ret == -1 && SSL_get_error (conn, ret) == SSL_ERROR_SYSCALL && errno == EINTR);
682 return ret;
683 }
684
685 static int
686 openssl_poll (int fd, double timeout, int wait_for, void *arg)
687 {
688 struct openssl_transport_context *ctx = arg;
689 SSL *conn = ctx->conn;
690 if ((wait_for & WAIT_FOR_READ) && SSL_pending (conn))
691 return 1;
692 /* if (timeout == 0)
693 return 1; */
694 if (timeout == -1)
695 timeout = opt.read_timeout;
696 return select_fd (fd, timeout, wait_for);
697 }
698
699 static int
700 openssl_peek (int fd, char *buf, int bufsize, void *arg, double timeout)
701 {
702 return openssl_read_peek (fd, buf, bufsize, arg, timeout, SSL_peek);
703 }
704
705 static const char *
706 openssl_errstr (int fd _GL_UNUSED, void *arg)
707 {
708 struct openssl_transport_context *ctx = arg;
709 unsigned long errcode;
710 char *errmsg = NULL;
711 int msglen = 0;
712
713 /* If there are no SSL-specific errors, just return NULL. */
714 if ((errcode = ERR_get_error ()) == 0)
715 return NULL;
716
717 /* Get rid of previous contents of ctx->last_error, if any. */
718 xfree (ctx->last_error);
719
720 /* Iterate over OpenSSL's error stack and accumulate errors in the
721 last_error buffer, separated by "; ". This is better than using
722 a static buffer, which *always* takes up space (and has to be
723 large, to fit more than one error message), whereas these
724 allocations are only performed when there is an actual error. */
725
726 for (;;)
727 {
728 const char *str = ERR_error_string (errcode, NULL);
729 int len = strlen (str);
730
731 /* Allocate space for the existing message, plus two more chars
732 for the "; " separator and one for the terminating \0. */
733 errmsg = xrealloc (errmsg, msglen + len + 2 + 1);
734 memcpy (errmsg + msglen, str, len);
735 msglen += len;
736
737 /* Get next error and bail out if there are no more. */
738 errcode = ERR_get_error ();
739 if (errcode == 0)
740 break;
741
742 errmsg[msglen++] = ';';
743 errmsg[msglen++] = ' ';
744 }
745 errmsg[msglen] = '\0';
746
747 /* Store the error in ctx->last_error where openssl_close will
748 eventually find it and free it. */
749 ctx->last_error = errmsg;
750
751 return errmsg;
752 }
753
754 static void
755 openssl_close (int fd, void *arg)
756 {
757 struct openssl_transport_context *ctx = arg;
758 SSL *conn = ctx->conn;
759
760 SSL_shutdown (conn);
761 SSL_free (conn);
762 xfree (ctx->last_error);
763 xfree (ctx);
764
765 close (fd);
766
767 DEBUGP (("Closed %d/SSL 0x%0*lx\n", fd, PTR_FORMAT (conn)));
768 }
769
770 /* openssl_transport is the singleton that describes the SSL transport
771 methods provided by this file. */
772
773 static struct transport_implementation openssl_transport = {
774 openssl_read, openssl_write, openssl_poll,
775 openssl_peek, openssl_errstr, openssl_close
776 };
777
778 static const char *
779 _sni_hostname(const char *hostname)
780 {
781 size_t len = strlen(hostname);
782
783 char *sni_hostname = xmemdup(hostname, len + 1);
784
785 /* Remove trailing dot(s) to fix #47408.
786 * Regarding RFC 6066 (SNI): The hostname is represented as a byte
787 * string using ASCII encoding without a trailing dot. */
788 while (len && sni_hostname[--len] == '.')
789 sni_hostname[len] = 0;
790
791 return sni_hostname;
792 }
793
794 /* Perform the SSL handshake on file descriptor FD, which is assumed
795 to be connected to an SSL server. The SSL handle provided by
796 OpenSSL is registered with the file descriptor FD using
797 fd_register_transport, so that subsequent calls to fd_read,
798 fd_write, etc., will use the corresponding SSL functions.
799
800 Returns true on success, false on failure. */
801
802 bool
803 ssl_connect_wget (int fd, const char *hostname, int *continue_session)
804 {
805 SSL *conn;
806 struct openssl_transport_context *ctx;
807
808 DEBUGP (("Initiating SSL handshake.\n"));
809
810 assert (ssl_ctx != NULL);
811 conn = SSL_new (ssl_ctx);
812 if (!conn)
813 goto error;
814 #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
815 /* If the SSL library was built with support for ServerNameIndication
816 then use it whenever we have a hostname. If not, don't, ever. */
817 if (! is_valid_ip_address (hostname))
818 {
819 const char *sni_hostname = _sni_hostname(hostname);
820
821 long rc = SSL_set_tlsext_host_name (conn, sni_hostname);
822 xfree(sni_hostname);
823
824 if (rc == 0)
825 {
826 DEBUGP (("Failed to set TLS server-name indication."));
827 goto error;
828 }
829 }
830 #endif
831
832 if (continue_session)
833 {
834 /* attempt to resume a previous SSL session */
835 ctx = (struct openssl_transport_context *) fd_transport_context (*continue_session);
836 if (!ctx || !ctx->sess || !SSL_set_session (conn, ctx->sess))
837 goto error;
838 }
839
840 #ifndef FD_TO_SOCKET
841 # define FD_TO_SOCKET(X) (X)
842 #endif
843 if (!SSL_set_fd (conn, FD_TO_SOCKET (fd)))
844 goto error;
845 SSL_set_connect_state (conn);
846
847 /* Re-seed the PRNG before the SSL handshake */
848 init_prng ();
849 if (RAND_status () != 1)
850 {
851 logprintf(LOG_NOTQUIET,
852 _("WARNING: Could not seed PRNG. Consider using --random-file.\n"));
853 goto error;
854 }
855
856 if (ssl_connect_with_timeout(fd, conn, opt.read_timeout) <= 0
857 || !SSL_is_init_finished(conn))
858 goto timedout;
859
860 ctx = xnew0 (struct openssl_transport_context);
861 ctx->conn = conn;
862 ctx->sess = SSL_get0_session (conn);
863 if (!ctx->sess)
864 logprintf (LOG_NOTQUIET, "WARNING: Could not save SSL session data for socket %d\n", fd);
865
866 /* Register FD with Wget's transport layer, i.e. arrange that our
867 functions are used for reading, writing, and polling. */
868 fd_register_transport (fd, &openssl_transport, ctx);
869 DEBUGP (("Handshake successful; connected socket %d to SSL handle 0x%0*lx\n",
870 fd, PTR_FORMAT (conn)));
871
872 ERR_clear_error ();
873 return true;
874
875 timedout:
876 if (errno == ETIMEDOUT)
877 DEBUGP (("SSL handshake timed out.\n"));
878 else
879 error:
880 DEBUGP (("SSL handshake failed.\n"));
881 print_errors ();
882 if (conn)
883 SSL_free (conn);
884 return false;
885 }
886
887 #define ASTERISK_EXCLUDES_DOT /* mandated by rfc2818 */
888
889 /* Return true is STRING (case-insensitively) matches PATTERN, false
890 otherwise. The recognized wildcard character is "*", which matches
891 any character in STRING except ".". Any number of the "*" wildcard
892 may be present in the pattern.
893
894 This is used to match of hosts as indicated in rfc2818: "Names may
895 contain the wildcard character * which is considered to match any
896 single domain name component or component fragment. E.g., *.a.com
897 matches foo.a.com but not bar.foo.a.com. f*.com matches foo.com but
898 not bar.com [or foo.bar.com]."
899
900 If the pattern contain no wildcards, pattern_match(a, b) is
901 equivalent to !strcasecmp(a, b). */
902
903 static bool
904 pattern_match (const char *pattern, const char *string)
905 {
906 const char *p = pattern, *n = string;
907 char c;
908 for (; (c = c_tolower (*p++)) != '\0'; n++)
909 if (c == '*')
910 {
911 for (c = c_tolower (*p); c == '*'; c = c_tolower (*++p))
912 ;
913 for (; *n != '\0'; n++)
914 if (c_tolower (*n) == c && pattern_match (p, n))
915 return true;
916 #ifdef ASTERISK_EXCLUDES_DOT
917 else if (*n == '.')
918 return false;
919 #endif
920 return c == '\0';
921 }
922 else
923 {
924 if (c != c_tolower (*n))
925 return false;
926 }
927 return *n == '\0';
928 }
929
930 static char *_get_rfc2253_formatted (X509_NAME *name)
931 {
932 int len;
933 char *out = NULL;
934 BIO* b;
935
936 if ((b = BIO_new (BIO_s_mem ())))
937 {
938 if (X509_NAME_print_ex (b, name, 0, XN_FLAG_RFC2253) >= 0
939 && (len = BIO_number_written (b)) > 0)
940 {
941 out = xmalloc (len + 1);
942 BIO_read (b, out, len);
943 out[len] = 0;
944 }
945 BIO_free (b);
946 }
947
948 return out ? out : xstrdup("");
949 }
950
951 /*
952 * Heavily modified from:
953 * https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#OpenSSL
954 */
955 static bool
956 pkp_pin_peer_pubkey (X509* cert, const char *pinnedpubkey)
957 {
958 /* Scratch */
959 int len1 = 0, len2 = 0;
960 char *buff1 = NULL, *temp = NULL;
961
962 /* Result is returned to caller */
963 bool result = false;
964
965 /* if a path wasn't specified, don't pin */
966 if (!pinnedpubkey)
967 return true;
968
969 if (!cert)
970 return result;
971
972 /* Begin Gyrations to get the subjectPublicKeyInfo */
973 /* Thanks to Viktor Dukhovni on the OpenSSL mailing list */
974
975 /* https://groups.google.com/group/mailing.openssl.users/browse_thread
976 /thread/d61858dae102c6c7 */
977 len1 = i2d_X509_PUBKEY (X509_get_X509_PUBKEY (cert), NULL);
978 if (len1 < 1)
979 goto cleanup; /* failed */
980
981 /* https://www.openssl.org/docs/crypto/buffer.html */
982 buff1 = temp = OPENSSL_malloc (len1);
983 if (!buff1)
984 goto cleanup; /* failed */
985
986 /* https://www.openssl.org/docs/crypto/d2i_X509.html */
987 len2 = i2d_X509_PUBKEY (X509_get_X509_PUBKEY (cert), (unsigned char **) &temp);
988
989 /*
990 * These checks are verifying we got back the same values as when we
991 * sized the buffer. It's pretty weak since they should always be the
992 * same. But it gives us something to test.
993 */
994 if ((len1 != len2) || !temp || ((temp - buff1) != len1))
995 goto cleanup; /* failed */
996
997 /* End Gyrations */
998
999 /* The one good exit point */
1000 result = wg_pin_peer_pubkey (pinnedpubkey, buff1, len1);
1001
1002 cleanup:
1003 /* https://www.openssl.org/docs/crypto/buffer.html */
1004 if (NULL != buff1)
1005 OPENSSL_free (buff1);
1006
1007 return result;
1008 }
1009
1010 /* Verify the validity of the certificate presented by the server.
1011 Also check that the "common name" of the server, as presented by
1012 its certificate, corresponds to HOST. (HOST typically comes from
1013 the URL and is what the user thinks he's connecting to.)
1014
1015 This assumes that ssl_connect_wget has successfully finished, i.e. that
1016 the SSL handshake has been performed and that FD is connected to an
1017 SSL handle.
1018
1019 If opt.check_cert is true (the default), this returns 1 if the
1020 certificate is valid, 0 otherwise. If opt.check_cert is 0, the
1021 function always returns 1, but should still be called because it
1022 warns the user about any problems with the certificate. */
1023
1024 bool
1025 ssl_check_certificate (int fd, const char *host)
1026 {
1027 X509 *cert;
1028 GENERAL_NAMES *subjectAltNames;
1029 char common_name[256];
1030 long vresult;
1031 bool success = true;
1032 bool alt_name_checked = false;
1033 bool pinsuccess = opt.pinnedpubkey == NULL;
1034
1035 /* If the user has specified --no-check-cert, we still want to warn
1036 him about problems with the server's certificate. */
1037 const char *severity = opt.check_cert ? _("ERROR") : _("WARNING");
1038
1039 struct openssl_transport_context *ctx = fd_transport_context (fd);
1040 SSL *conn = ctx->conn;
1041 assert (conn != NULL);
1042
1043 /* The user explicitly said to not check for the certificate. */
1044 if (opt.check_cert == CHECK_CERT_QUIET && pinsuccess)
1045 return success;
1046
1047 cert = SSL_get_peer_certificate (conn);
1048 if (!cert)
1049 {
1050 logprintf (LOG_NOTQUIET, _("%s: No certificate presented by %s.\n"),
1051 severity, quotearg_style (escape_quoting_style, host));
1052 success = false;
1053 goto no_cert; /* must bail out since CERT is NULL */
1054 }
1055
1056 IF_DEBUG
1057 {
1058 char *subject = _get_rfc2253_formatted (X509_get_subject_name (cert));
1059 char *issuer = _get_rfc2253_formatted (X509_get_issuer_name (cert));
1060 DEBUGP (("certificate:\n subject: %s\n issuer: %s\n",
1061 quotearg_n_style (0, escape_quoting_style, subject),
1062 quotearg_n_style (1, escape_quoting_style, issuer)));
1063 xfree (subject);
1064 xfree (issuer);
1065 }
1066
1067 vresult = SSL_get_verify_result (conn);
1068 if (vresult != X509_V_OK)
1069 {
1070 char *issuer = _get_rfc2253_formatted (X509_get_issuer_name (cert));
1071 logprintf (LOG_NOTQUIET,
1072 _("%s: cannot verify %s's certificate, issued by %s:\n"),
1073 severity, quotearg_n_style (0, escape_quoting_style, host),
1074 quote_n (1, issuer));
1075 xfree(issuer);
1076
1077 /* Try to print more user-friendly (and translated) messages for
1078 the frequent verification errors. */
1079 switch (vresult)
1080 {
1081 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1082 logprintf (LOG_NOTQUIET,
1083 _(" Unable to locally verify the issuer's authority.\n"));
1084 break;
1085 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1086 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1087 logprintf (LOG_NOTQUIET,
1088 _(" Self-signed certificate encountered.\n"));
1089 break;
1090 case X509_V_ERR_CERT_NOT_YET_VALID:
1091 logprintf (LOG_NOTQUIET, _(" Issued certificate not yet valid.\n"));
1092 break;
1093 case X509_V_ERR_CERT_HAS_EXPIRED:
1094 logprintf (LOG_NOTQUIET, _(" Issued certificate has expired.\n"));
1095 break;
1096 default:
1097 /* For the less frequent error strings, simply provide the
1098 OpenSSL error message. */
1099 logprintf (LOG_NOTQUIET, " %s\n",
1100 X509_verify_cert_error_string (vresult));
1101 }
1102 success = false;
1103 /* Fall through, so that the user is warned about *all* issues
1104 with the cert (important with --no-check-certificate.) */
1105 }
1106
1107 /* Check that HOST matches the common name in the certificate.
1108 #### The following remains to be done:
1109
1110 - When matching against common names, it should loop over all
1111 common names and choose the most specific one, i.e. the last
1112 one, not the first one, which the current code picks.
1113
1114 - Ensure that ASN1 strings from the certificate are encoded as
1115 UTF-8 which can be meaningfully compared to HOST. */
1116
1117 subjectAltNames = X509_get_ext_d2i (cert, NID_subject_alt_name, NULL, NULL);
1118
1119 if (subjectAltNames)
1120 {
1121 /* Test subject alternative names */
1122
1123 /* SNI hostname must not have a trailing dot */
1124 const char *sni_hostname = _sni_hostname(host);
1125
1126 /* Do we want to check for dNSNAmes or ipAddresses (see RFC 2818)?
1127 * Signal it by host_in_octet_string. */
1128 ASN1_OCTET_STRING *host_in_octet_string = a2i_IPADDRESS (sni_hostname);
1129
1130 int numaltnames = sk_GENERAL_NAME_num (subjectAltNames);
1131 int i;
1132 for (i=0; i < numaltnames; i++)
1133 {
1134 const GENERAL_NAME *name =
1135 sk_GENERAL_NAME_value (subjectAltNames, i);
1136 if (name)
1137 {
1138 if (host_in_octet_string)
1139 {
1140 if (name->type == GEN_IPADD)
1141 {
1142 /* Check for ipAddress */
1143 /* TODO: Should we convert between IPv4-mapped IPv6
1144 * addresses and IPv4 addresses? */
1145 alt_name_checked = true;
1146 if (!ASN1_STRING_cmp (host_in_octet_string,
1147 name->d.iPAddress))
1148 break;
1149 }
1150 }
1151 else if (name->type == GEN_DNS)
1152 {
1153 /* dNSName should be IA5String (i.e. ASCII), however who
1154 * does trust CA? Convert it into UTF-8 for sure. */
1155 unsigned char *name_in_utf8 = NULL;
1156
1157 /* Check for dNSName */
1158 alt_name_checked = true;
1159
1160 if (0 <= ASN1_STRING_to_UTF8 (&name_in_utf8, name->d.dNSName))
1161 {
1162 /* Compare and check for NULL attack in ASN1_STRING */
1163 if (pattern_match ((char *)name_in_utf8, sni_hostname) &&
1164 (strlen ((char *)name_in_utf8) ==
1165 (size_t) ASN1_STRING_length (name->d.dNSName)))
1166 {
1167 OPENSSL_free (name_in_utf8);
1168 break;
1169 }
1170 OPENSSL_free (name_in_utf8);
1171 }
1172 }
1173 }
1174 }
1175 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
1176 if (host_in_octet_string)
1177 ASN1_OCTET_STRING_free(host_in_octet_string);
1178
1179 if (alt_name_checked == true && i >= numaltnames)
1180 {
1181 logprintf (LOG_NOTQUIET,
1182 _("%s: no certificate subject alternative name matches\n"
1183 "\trequested host name %s.\n"),
1184 severity, quote_n (1, sni_hostname));
1185 success = false;
1186 }
1187
1188 xfree(sni_hostname);
1189 }
1190
1191 if (alt_name_checked == false)
1192 {
1193 /* Test commomName */
1194 X509_NAME *xname = X509_get_subject_name(cert);
1195 common_name[0] = '\0';
1196 X509_NAME_get_text_by_NID (xname, NID_commonName, common_name,
1197 sizeof (common_name));
1198
1199 if (!pattern_match (common_name, host))
1200 {
1201 logprintf (LOG_NOTQUIET, _("\
1202 %s: certificate common name %s doesn't match requested host name %s.\n"),
1203 severity, quote_n (0, common_name), quote_n (1, host));
1204 success = false;
1205 }
1206 else
1207 {
1208 /* We now determine the length of the ASN1 string. If it
1209 * differs from common_name's length, then there is a \0
1210 * before the string terminates. This can be an instance of a
1211 * null-prefix attack.
1212 *
1213 * https://www.blackhat.com/html/bh-usa-09/bh-usa-09-archives.html#Marlinspike
1214 * */
1215
1216 int i = -1, j;
1217 X509_NAME_ENTRY *xentry;
1218 ASN1_STRING *sdata;
1219
1220 if (xname) {
1221 for (;;)
1222 {
1223 j = X509_NAME_get_index_by_NID (xname, NID_commonName, i);
1224 if (j == -1) break;
1225 i = j;
1226 }
1227 }
1228
1229 xentry = X509_NAME_get_entry(xname,i);
1230 sdata = X509_NAME_ENTRY_get_data(xentry);
1231 if (strlen (common_name) != (size_t) ASN1_STRING_length (sdata))
1232 {
1233 logprintf (LOG_NOTQUIET, _("\
1234 %s: certificate common name is invalid (contains a NUL character).\n\
1235 This may be an indication that the host is not who it claims to be\n\
1236 (that is, it is not the real %s).\n"),
1237 severity, quote (host));
1238 success = false;
1239 }
1240 }
1241 }
1242
1243 pinsuccess = pkp_pin_peer_pubkey (cert, opt.pinnedpubkey);
1244 if (!pinsuccess)
1245 {
1246 logprintf (LOG_ALWAYS, _("The public key does not match pinned public key!\n"));
1247 success = false;
1248 }
1249
1250
1251 if (success)
1252 DEBUGP (("X509 certificate successfully verified and matches host %s\n",
1253 quotearg_style (escape_quoting_style, host)));
1254 X509_free (cert);
1255
1256 no_cert:
1257 if (opt.check_cert == CHECK_CERT_ON && !success)
1258 logprintf (LOG_NOTQUIET, _("\
1259 To connect to %s insecurely, use `--no-check-certificate'.\n"),
1260 quotearg_style (escape_quoting_style, host));
1261
1262 /* never return true if pinsuccess fails */
1263 return !pinsuccess ? false : (opt.check_cert == CHECK_CERT_ON ? success : true);
1264 }
1265
1266 /*
1267 * vim: tabstop=2 shiftwidth=2 softtabstop=2
1268 */