| 1 |
/* SSL support via GnuTLS library. |
| 2 |
Copyright (C) 2005-2012, 2015, 2018-2024 Free Software Foundation, |
| 3 |
Inc. |
| 4 |
|
| 5 |
This file is part of GNU Wget. |
| 6 |
|
| 7 |
GNU Wget is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
GNU Wget 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 |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with Wget. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Additional permission under GNU GPL version 3 section 7 |
| 21 |
|
| 22 |
If you modify this program, or any covered work, by linking or |
| 23 |
combining it with the OpenSSL project's OpenSSL library (or a |
| 24 |
modified version of that library), containing parts covered by the |
| 25 |
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation |
| 26 |
grants you additional permission to convey the resulting work. |
| 27 |
Corresponding Source for a non-source form of such a combination |
| 28 |
shall include the source code for the parts of OpenSSL used as well |
| 29 |
as that of the covered work. */ |
| 30 |
|
| 31 |
#include "wget.h" |
| 32 |
|
| 33 |
#include <errno.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <string.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <dirent.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <xalloc.h> |
| 40 |
|
| 41 |
#include <gnutls/abstract.h> |
| 42 |
#include <gnutls/gnutls.h> |
| 43 |
#include <gnutls/x509.h> |
| 44 |
#include <sys/ioctl.h> |
| 45 |
|
| 46 |
#include "utils.h" |
| 47 |
#include "connect.h" |
| 48 |
#include "url.h" |
| 49 |
#include "ptimer.h" |
| 50 |
#include "hash.h" |
| 51 |
#include "ssl.h" |
| 52 |
|
| 53 |
#include <fcntl.h> |
| 54 |
|
| 55 |
#ifdef WIN32 |
| 56 |
# include "w32sock.h" |
| 57 |
#endif |
| 58 |
|
| 59 |
#include "host.h" |
| 60 |
|
| 61 |
struct st_read_timer |
| 62 |
{ |
| 63 |
double timeout; |
| 64 |
double next_timeout; |
| 65 |
struct ptimer *timer; |
| 66 |
int timed_out; |
| 67 |
}; |
| 68 |
|
| 69 |
static int |
| 70 |
_do_handshake (gnutls_session_t session, int fd, struct st_read_timer *timeout); |
| 71 |
|
| 72 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 73 |
static int |
| 74 |
_do_reauth (gnutls_session_t session, int fd, struct st_read_timer *timeout); |
| 75 |
#endif |
| 76 |
|
| 77 |
static int |
| 78 |
key_type_to_gnutls_type (enum keyfile_type type) |
| 79 |
{ |
| 80 |
switch (type) |
| 81 |
{ |
| 82 |
case keyfile_pem: |
| 83 |
return GNUTLS_X509_FMT_PEM; |
| 84 |
case keyfile_asn1: |
| 85 |
return GNUTLS_X509_FMT_DER; |
| 86 |
default: |
| 87 |
abort (); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* Note: some of the functions private to this file have names that |
| 92 |
begin with "wgnutls_" (e.g. wgnutls_read) so that they wouldn't be |
| 93 |
confused with actual gnutls functions -- such as the gnutls_read |
| 94 |
preprocessor macro. */ |
| 95 |
|
| 96 |
static bool ssl_initialized = false; |
| 97 |
|
| 98 |
static gnutls_certificate_credentials_t credentials; |
| 99 |
bool |
| 100 |
ssl_init (void) |
| 101 |
{ |
| 102 |
/* Becomes true if GnuTLS is initialized. */ |
| 103 |
const char *ca_directory; |
| 104 |
DIR *dir; |
| 105 |
int ncerts = -1; |
| 106 |
int rc; |
| 107 |
|
| 108 |
/* GnuTLS should be initialized only once. */ |
| 109 |
if (ssl_initialized) |
| 110 |
return true; |
| 111 |
|
| 112 |
gnutls_global_init (); |
| 113 |
gnutls_certificate_allocate_credentials (&credentials); |
| 114 |
gnutls_certificate_set_verify_flags (credentials, |
| 115 |
GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT); |
| 116 |
|
| 117 |
#if GNUTLS_VERSION_MAJOR >= 3 |
| 118 |
if (!opt.ca_directory) |
| 119 |
ncerts = gnutls_certificate_set_x509_system_trust (credentials); |
| 120 |
#endif |
| 121 |
|
| 122 |
/* If GnuTLS version is too old or CA loading failed, fallback to old behaviour. |
| 123 |
* Also use old behaviour if the CA directory is user-provided. */ |
| 124 |
if (ncerts <= 0) |
| 125 |
{ |
| 126 |
ncerts = 0; |
| 127 |
|
| 128 |
ca_directory = opt.ca_directory ? opt.ca_directory : "/etc/ssl/certs"; |
| 129 |
|
| 130 |
if ((dir = opendir (ca_directory)) == NULL) |
| 131 |
{ |
| 132 |
if (opt.ca_directory && *opt.ca_directory) |
| 133 |
logprintf (LOG_NOTQUIET, _("ERROR: Cannot open directory %s.\n"), |
| 134 |
opt.ca_directory); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
struct hash_table *inode_map = hash_table_new (196, NULL, NULL); |
| 139 |
struct dirent *dent; |
| 140 |
|
| 141 |
while ((dent = readdir (dir)) != NULL) |
| 142 |
{ |
| 143 |
struct stat st; |
| 144 |
char ca_file[1024]; |
| 145 |
|
| 146 |
if (((unsigned) snprintf (ca_file, sizeof (ca_file), "%s/%s", ca_directory, dent->d_name)) >= sizeof (ca_file)) |
| 147 |
continue; // overflow |
| 148 |
|
| 149 |
if (stat (ca_file, &st) != 0) |
| 150 |
continue; |
| 151 |
|
| 152 |
if (! S_ISREG (st.st_mode)) |
| 153 |
continue; |
| 154 |
|
| 155 |
/* avoid loading the same file twice by checking the inode. */ |
| 156 |
if (hash_table_contains (inode_map, (void *)(intptr_t) st.st_ino)) |
| 157 |
continue; |
| 158 |
|
| 159 |
hash_table_put (inode_map, (void *)(intptr_t) st.st_ino, NULL); |
| 160 |
if ((rc = gnutls_certificate_set_x509_trust_file (credentials, ca_file, |
| 161 |
GNUTLS_X509_FMT_PEM)) <= 0) |
| 162 |
DEBUGP (("WARNING: Failed to open cert %s: (%d).\n", ca_file, rc)); |
| 163 |
else |
| 164 |
ncerts += rc; |
| 165 |
} |
| 166 |
|
| 167 |
hash_table_destroy (inode_map); |
| 168 |
closedir (dir); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (opt.ca_cert) |
| 173 |
{ |
| 174 |
if (ncerts < 0) |
| 175 |
ncerts = 0; |
| 176 |
|
| 177 |
if ((rc = gnutls_certificate_set_x509_trust_file (credentials, opt.ca_cert, |
| 178 |
GNUTLS_X509_FMT_PEM)) <= 0) |
| 179 |
logprintf (LOG_NOTQUIET, _("ERROR: Failed to open cert %s: (%d).\n"), |
| 180 |
opt.ca_cert, rc); |
| 181 |
else |
| 182 |
{ |
| 183 |
ncerts += rc; |
| 184 |
logprintf (LOG_VERBOSE, _("Loaded CA certificate '%s'\n"), opt.ca_cert); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (opt.crl_file) |
| 189 |
{ |
| 190 |
if ((rc = gnutls_certificate_set_x509_crl_file (credentials, opt.crl_file, GNUTLS_X509_FMT_PEM)) <= 0) |
| 191 |
{ |
| 192 |
logprintf (LOG_NOTQUIET, _("ERROR: Failed to load CRL file '%s': (%d)\n"), opt.crl_file, rc); |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
logprintf (LOG_VERBOSE, _("Loaded CRL file '%s'\n"), opt.crl_file); |
| 197 |
} |
| 198 |
|
| 199 |
DEBUGP (("Certificates loaded: %d\n", ncerts)); |
| 200 |
|
| 201 |
/* Use the private key from the cert file unless otherwise specified. */ |
| 202 |
if (opt.cert_file && !opt.private_key) |
| 203 |
{ |
| 204 |
opt.private_key = xstrdup (opt.cert_file); |
| 205 |
opt.private_key_type = opt.cert_type; |
| 206 |
} |
| 207 |
/* Use the cert from the private key file unless otherwise specified. */ |
| 208 |
if (!opt.cert_file && opt.private_key) |
| 209 |
{ |
| 210 |
opt.cert_file = xstrdup (opt.private_key); |
| 211 |
opt.cert_type = opt.private_key_type; |
| 212 |
} |
| 213 |
|
| 214 |
if (opt.cert_file && opt.private_key) |
| 215 |
{ |
| 216 |
int type; |
| 217 |
if (opt.private_key_type != opt.cert_type) |
| 218 |
{ |
| 219 |
/* GnuTLS can't handle this */ |
| 220 |
logprintf (LOG_NOTQUIET, _("ERROR: GnuTLS requires the key and the \ |
| 221 |
cert to be of the same type.\n")); |
| 222 |
} |
| 223 |
|
| 224 |
type = key_type_to_gnutls_type (opt.private_key_type); |
| 225 |
|
| 226 |
gnutls_certificate_set_x509_key_file (credentials, opt.cert_file, |
| 227 |
opt.private_key, |
| 228 |
type); |
| 229 |
} |
| 230 |
|
| 231 |
ssl_initialized = true; |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
void |
| 237 |
ssl_cleanup (void) |
| 238 |
{ |
| 239 |
if (!ssl_initialized) |
| 240 |
return; |
| 241 |
|
| 242 |
if (credentials) |
| 243 |
gnutls_certificate_free_credentials(credentials); |
| 244 |
|
| 245 |
gnutls_global_deinit(); |
| 246 |
|
| 247 |
ssl_initialized = false; |
| 248 |
} |
| 249 |
|
| 250 |
struct wgnutls_transport_context |
| 251 |
{ |
| 252 |
gnutls_session_t session; /* GnuTLS session handle */ |
| 253 |
gnutls_datum_t *session_data; |
| 254 |
int last_error; /* last error returned by read/write/... */ |
| 255 |
|
| 256 |
/* Since GnuTLS doesn't support the equivalent to recv(..., |
| 257 |
MSG_PEEK) or SSL_peek(), we have to do it ourselves. Peeked data |
| 258 |
is stored to PEEKBUF, and wgnutls_read checks that buffer before |
| 259 |
actually reading. */ |
| 260 |
char peekbuf[512]; |
| 261 |
int peeklen; |
| 262 |
}; |
| 263 |
|
| 264 |
static int |
| 265 |
wgnutls_read_timeout (int fd, char *buf, int bufsize, void *arg, double timeout) |
| 266 |
{ |
| 267 |
#ifdef F_GETFL |
| 268 |
int flags = 0; |
| 269 |
#endif |
| 270 |
struct wgnutls_transport_context *ctx = arg; |
| 271 |
int ret = gnutls_record_check_pending (ctx->session); |
| 272 |
struct st_read_timer read_timer = {(timeout == -1 ? opt.read_timeout : timeout), 0, NULL, 0}; |
| 273 |
|
| 274 |
if (ret) |
| 275 |
return gnutls_record_recv (ctx->session, buf, MIN (ret, bufsize)); |
| 276 |
|
| 277 |
if (read_timer.timeout) |
| 278 |
{ |
| 279 |
#ifdef F_GETFL |
| 280 |
flags = fcntl (fd, F_GETFL, 0); |
| 281 |
if (flags < 0) |
| 282 |
return flags; |
| 283 |
if (fcntl (fd, F_SETFL, flags | O_NONBLOCK)) |
| 284 |
return -1; |
| 285 |
#else |
| 286 |
/* XXX: Assume it was blocking before. */ |
| 287 |
const int one = 1; |
| 288 |
if (ioctl (fd, FIONBIO, &one) < 0) |
| 289 |
return -1; |
| 290 |
#endif |
| 291 |
|
| 292 |
read_timer.timer = ptimer_new (); |
| 293 |
if (read_timer.timer == NULL) |
| 294 |
{ |
| 295 |
ret = -1; |
| 296 |
goto timer_err; |
| 297 |
} |
| 298 |
read_timer.next_timeout = read_timer.timeout; |
| 299 |
} |
| 300 |
|
| 301 |
ret = ctx->last_error; |
| 302 |
do |
| 303 |
{ |
| 304 |
if (ret == GNUTLS_E_REHANDSHAKE) |
| 305 |
{ |
| 306 |
int err; |
| 307 |
DEBUGP (("GnuTLS: *** REHANDSHAKE while reading\n")); |
| 308 |
if ((err = _do_handshake (ctx->session, fd, &read_timer)) != 0) |
| 309 |
{ |
| 310 |
ret = err; |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 315 |
else if (ret == GNUTLS_E_REAUTH_REQUEST) |
| 316 |
{ |
| 317 |
int err; |
| 318 |
DEBUGP (("GnuTLS: *** re-authentication while reading\n")); |
| 319 |
if ((err = _do_reauth (ctx->session, fd, &read_timer)) != 0) |
| 320 |
{ |
| 321 |
ret = err; |
| 322 |
break; |
| 323 |
} |
| 324 |
} |
| 325 |
#endif |
| 326 |
do |
| 327 |
{ |
| 328 |
ret = gnutls_record_recv (ctx->session, buf, bufsize); |
| 329 |
if (ret == GNUTLS_E_AGAIN && read_timer.timer) |
| 330 |
{ |
| 331 |
int err = select_fd_nb (fd, read_timer.next_timeout, WAIT_FOR_READ); |
| 332 |
if (err <= 0) |
| 333 |
{ |
| 334 |
if (err == 0) |
| 335 |
read_timer.timed_out = 1; |
| 336 |
goto break_all; |
| 337 |
} |
| 338 |
if ( (read_timer.next_timeout = read_timer.timeout - ptimer_measure (read_timer.timer)) <= 0 ) |
| 339 |
{ |
| 340 |
read_timer.timed_out = 1; |
| 341 |
goto break_all; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED); |
| 346 |
} |
| 347 |
while (ret == GNUTLS_E_REHANDSHAKE |
| 348 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 349 |
|| ret == GNUTLS_E_REAUTH_REQUEST |
| 350 |
#endif |
| 351 |
); |
| 352 |
|
| 353 |
break_all: |
| 354 |
if (read_timer.timer) |
| 355 |
{ |
| 356 |
ptimer_destroy (read_timer.timer); |
| 357 |
timer_err: ; |
| 358 |
#ifdef F_GETFL |
| 359 |
if (fcntl (fd, F_SETFL, flags) < 0) |
| 360 |
return -1; |
| 361 |
#else |
| 362 |
{ |
| 363 |
const int zero = 0; |
| 364 |
if (ioctl (fd, FIONBIO, &zero) < 0) |
| 365 |
return -1; |
| 366 |
} |
| 367 |
#endif |
| 368 |
if (read_timer.timed_out) |
| 369 |
errno = ETIMEDOUT; |
| 370 |
} |
| 371 |
|
| 372 |
return ret; |
| 373 |
} |
| 374 |
|
| 375 |
static int |
| 376 |
wgnutls_read (int fd, char *buf, int bufsize, void *arg, double timeout) |
| 377 |
{ |
| 378 |
int ret; |
| 379 |
struct wgnutls_transport_context *ctx = arg; |
| 380 |
|
| 381 |
if (ctx->peeklen) |
| 382 |
{ |
| 383 |
/* If we have any peek data, simply return that. */ |
| 384 |
int copysize = MIN (bufsize, ctx->peeklen); |
| 385 |
memcpy (buf, ctx->peekbuf, copysize); |
| 386 |
ctx->peeklen -= copysize; |
| 387 |
if (ctx->peeklen != 0) |
| 388 |
memmove (ctx->peekbuf, ctx->peekbuf + copysize, ctx->peeklen); |
| 389 |
|
| 390 |
return copysize; |
| 391 |
} |
| 392 |
|
| 393 |
ret = wgnutls_read_timeout (fd, buf, bufsize, arg, timeout); |
| 394 |
ctx->last_error = ret; |
| 395 |
return ret; |
| 396 |
} |
| 397 |
|
| 398 |
static int |
| 399 |
wgnutls_write (int fd _GL_UNUSED, char *buf, int bufsize, void *arg) |
| 400 |
{ |
| 401 |
struct wgnutls_transport_context *ctx = arg; |
| 402 |
int ret = ctx->last_error; |
| 403 |
|
| 404 |
/* it should never happen, |
| 405 |
placed here only for debug msg. */ |
| 406 |
if (ret == GNUTLS_E_REHANDSHAKE) |
| 407 |
{ |
| 408 |
DEBUGP (("GnuTLS: *** REHANDSHAKE while writing\n")); |
| 409 |
if ((ret = _do_handshake (ctx->session, fd, NULL)) != 0) |
| 410 |
goto ext; |
| 411 |
} |
| 412 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 413 |
else if (ret == GNUTLS_E_REAUTH_REQUEST) |
| 414 |
{ |
| 415 |
DEBUGP (("GnuTLS: *** re-authentication while writing\n")); |
| 416 |
if ((ret = _do_reauth (ctx->session, fd, NULL)) != 0) |
| 417 |
goto ext; |
| 418 |
} |
| 419 |
#endif |
| 420 |
|
| 421 |
do |
| 422 |
ret = gnutls_record_send (ctx->session, buf, bufsize); |
| 423 |
while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN); |
| 424 |
ext: |
| 425 |
ctx->last_error = ret; |
| 426 |
return ret; |
| 427 |
} |
| 428 |
|
| 429 |
static int |
| 430 |
wgnutls_poll (int fd, double timeout, int wait_for, void *arg) |
| 431 |
{ |
| 432 |
struct wgnutls_transport_context *ctx = arg; |
| 433 |
|
| 434 |
if ((wait_for & WAIT_FOR_READ) |
| 435 |
&& (ctx->peeklen || gnutls_record_check_pending (ctx->session))) |
| 436 |
return 1; |
| 437 |
|
| 438 |
if (timeout == -1) |
| 439 |
timeout = opt.read_timeout; |
| 440 |
return select_fd (fd, timeout, wait_for); |
| 441 |
} |
| 442 |
|
| 443 |
static int |
| 444 |
wgnutls_peek (int fd, char *buf, int bufsize, void *arg, double timeout) |
| 445 |
{ |
| 446 |
int read = 0; |
| 447 |
struct wgnutls_transport_context *ctx = arg; |
| 448 |
int offset = MIN (bufsize, ctx->peeklen); |
| 449 |
|
| 450 |
if (ctx->peeklen) |
| 451 |
{ |
| 452 |
memcpy (buf, ctx->peekbuf, offset); |
| 453 |
return offset; |
| 454 |
} |
| 455 |
|
| 456 |
if (bufsize > (int) sizeof ctx->peekbuf) |
| 457 |
bufsize = sizeof ctx->peekbuf; |
| 458 |
|
| 459 |
if (bufsize > offset) |
| 460 |
{ /* let wgnutls_read_timeout() take care about timeout */ |
| 461 |
/*if (timeout && gnutls_record_check_pending (ctx->session) == 0 |
| 462 |
&& select_fd (fd, 0.0, WAIT_FOR_READ) <= 0) |
| 463 |
read = 0; |
| 464 |
else*/ |
| 465 |
read = wgnutls_read_timeout (fd, buf + offset, bufsize - offset, |
| 466 |
ctx, timeout); |
| 467 |
ctx->last_error = read; |
| 468 |
if (read < 0) |
| 469 |
{ |
| 470 |
if (offset) |
| 471 |
read = 0; |
| 472 |
else |
| 473 |
return read; |
| 474 |
} |
| 475 |
|
| 476 |
if (read > 0) |
| 477 |
{ |
| 478 |
memcpy (ctx->peekbuf + offset, buf + offset, |
| 479 |
read); |
| 480 |
ctx->peeklen += read; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
return offset + read; |
| 485 |
} |
| 486 |
|
| 487 |
static const char * |
| 488 |
wgnutls_errstr (int fd _GL_UNUSED, void *arg) |
| 489 |
{ |
| 490 |
struct wgnutls_transport_context *ctx = arg; |
| 491 |
|
| 492 |
if (ctx->last_error > 0 |
| 493 |
|| ((ctx->last_error == GNUTLS_E_AGAIN |
| 494 |
|| ctx->last_error == GNUTLS_E_REHANDSHAKE |
| 495 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 496 |
|| ctx->last_error == GNUTLS_E_REAUTH_REQUEST |
| 497 |
#endif |
| 498 |
) && errno == ETIMEDOUT)) |
| 499 |
return NULL; |
| 500 |
|
| 501 |
return gnutls_strerror (ctx->last_error); |
| 502 |
} |
| 503 |
|
| 504 |
static void |
| 505 |
wgnutls_close (int fd, void *arg) |
| 506 |
{ |
| 507 |
struct wgnutls_transport_context *ctx = arg; |
| 508 |
/*gnutls_bye (ctx->session, GNUTLS_SHUT_RDWR);*/ |
| 509 |
if (ctx->session_data) |
| 510 |
{ |
| 511 |
gnutls_free (ctx->session_data->data); |
| 512 |
gnutls_free (ctx->session_data); |
| 513 |
} |
| 514 |
gnutls_deinit (ctx->session); |
| 515 |
xfree (ctx); |
| 516 |
close (fd); |
| 517 |
} |
| 518 |
|
| 519 |
/* gnutls_transport is the singleton that describes the SSL transport |
| 520 |
methods provided by this file. */ |
| 521 |
|
| 522 |
static struct transport_implementation wgnutls_transport = |
| 523 |
{ |
| 524 |
wgnutls_read, wgnutls_write, wgnutls_poll, |
| 525 |
wgnutls_peek, wgnutls_errstr, wgnutls_close |
| 526 |
}; |
| 527 |
|
| 528 |
static int |
| 529 |
_do_handshake (gnutls_session_t session, int fd, struct st_read_timer *read_timer) |
| 530 |
{ |
| 531 |
#ifdef F_GETFL |
| 532 |
int flags = 0; |
| 533 |
#endif |
| 534 |
int err; |
| 535 |
double next_timeout = (read_timer ? read_timer->next_timeout : opt.read_timeout); |
| 536 |
|
| 537 |
/* if (read_timer != NULL) - fd is already non blocking */ |
| 538 |
if (!read_timer && next_timeout) |
| 539 |
{ |
| 540 |
#ifdef F_GETFL |
| 541 |
flags = fcntl (fd, F_GETFL, 0); |
| 542 |
if (flags < 0) |
| 543 |
return flags; |
| 544 |
if (fcntl (fd, F_SETFL, flags | O_NONBLOCK)) |
| 545 |
return -1; |
| 546 |
#else |
| 547 |
/* XXX: Assume it was blocking before. */ |
| 548 |
const int one = 1; |
| 549 |
if (ioctl (fd, FIONBIO, &one) < 0) |
| 550 |
return -1; |
| 551 |
#endif |
| 552 |
} |
| 553 |
|
| 554 |
/* We don't stop the handshake process for non-fatal errors */ |
| 555 |
do |
| 556 |
{ |
| 557 |
err = gnutls_handshake (session); |
| 558 |
|
| 559 |
if (err == GNUTLS_E_AGAIN && next_timeout) |
| 560 |
{ |
| 561 |
int sel; |
| 562 |
if (gnutls_record_get_direction (session)) |
| 563 |
{ |
| 564 |
/* wait for writeability */ |
| 565 |
sel = WAIT_FOR_WRITE; |
| 566 |
} |
| 567 |
else |
| 568 |
{ |
| 569 |
/* wait for readability */ |
| 570 |
sel = WAIT_FOR_READ; |
| 571 |
} |
| 572 |
sel = select_fd_nb (fd, next_timeout, sel); |
| 573 |
|
| 574 |
if (sel <= 0) |
| 575 |
{ |
| 576 |
if (sel == 0) |
| 577 |
{ |
| 578 |
if (read_timer) |
| 579 |
goto read_timedout; |
| 580 |
else |
| 581 |
{ |
| 582 |
errno = ETIMEDOUT; |
| 583 |
err = -1; |
| 584 |
} |
| 585 |
} |
| 586 |
break; |
| 587 |
} |
| 588 |
if (read_timer) |
| 589 |
{ |
| 590 |
if ( (read_timer->next_timeout = read_timer->timeout - ptimer_measure (read_timer->timer)) <= 0 ) |
| 591 |
{ |
| 592 |
read_timedout: /* return GNUTLS_E_REHANDSHAKE for gnutls_read */ |
| 593 |
err = GNUTLS_E_REHANDSHAKE; |
| 594 |
read_timer->timed_out = 1; |
| 595 |
break; |
| 596 |
} |
| 597 |
next_timeout = read_timer->next_timeout; |
| 598 |
} |
| 599 |
} |
| 600 |
else if (err < 0) |
| 601 |
{ |
| 602 |
logprintf (LOG_NOTQUIET, "GnuTLS: %s\n", gnutls_strerror (err)); |
| 603 |
if (err == GNUTLS_E_WARNING_ALERT_RECEIVED || |
| 604 |
err == GNUTLS_E_FATAL_ALERT_RECEIVED) |
| 605 |
{ |
| 606 |
gnutls_alert_description_t alert = gnutls_alert_get (session); |
| 607 |
const char *str = gnutls_alert_get_name (alert); |
| 608 |
logprintf (LOG_NOTQUIET, "GnuTLS: received alert [%u]: %s\n", |
| 609 |
alert, str ? str : "(unknown)"); |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
while (err && gnutls_error_is_fatal (err) == 0); |
| 614 |
|
| 615 |
if (!read_timer && next_timeout) |
| 616 |
{ |
| 617 |
#ifdef F_GETFL |
| 618 |
if (fcntl (fd, F_SETFL, flags) < 0) |
| 619 |
return -1; |
| 620 |
#else |
| 621 |
const int zero = 0; |
| 622 |
if (ioctl (fd, FIONBIO, &zero) < 0) |
| 623 |
return -1; |
| 624 |
#endif |
| 625 |
} |
| 626 |
|
| 627 |
return err; |
| 628 |
} |
| 629 |
|
| 630 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 631 |
static int |
| 632 |
_do_reauth (gnutls_session_t session, int fd, struct st_read_timer *read_timer) |
| 633 |
{ |
| 634 |
#ifdef F_GETFL |
| 635 |
int flags = 0; |
| 636 |
#endif |
| 637 |
int err; |
| 638 |
double next_timeout = (read_timer ? read_timer->next_timeout : opt.read_timeout); |
| 639 |
|
| 640 |
/* if (read_timer != NULL) - fd is already non blocking */ |
| 641 |
if (!read_timer && next_timeout) |
| 642 |
{ |
| 643 |
#ifdef F_GETFL |
| 644 |
flags = fcntl (fd, F_GETFL, 0); |
| 645 |
if (flags < 0) |
| 646 |
return flags; |
| 647 |
if (fcntl (fd, F_SETFL, flags | O_NONBLOCK)) |
| 648 |
return -1; |
| 649 |
#else |
| 650 |
/* XXX: Assume it was blocking before. */ |
| 651 |
const int one = 1; |
| 652 |
if (ioctl (fd, FIONBIO, &one) < 0) |
| 653 |
return -1; |
| 654 |
#endif |
| 655 |
} |
| 656 |
|
| 657 |
/* We don't stop the handshake process for non-fatal errors */ |
| 658 |
do |
| 659 |
{ |
| 660 |
err = gnutls_reauth (session, 0); |
| 661 |
|
| 662 |
if (err == GNUTLS_E_AGAIN && next_timeout) |
| 663 |
{ |
| 664 |
int sel; |
| 665 |
if (gnutls_record_get_direction (session)) |
| 666 |
{ |
| 667 |
/* wait for writeability */ |
| 668 |
sel = WAIT_FOR_WRITE; |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
/* wait for readability */ |
| 673 |
sel = WAIT_FOR_READ; |
| 674 |
} |
| 675 |
sel = select_fd_nb (fd, next_timeout, sel); |
| 676 |
|
| 677 |
if (sel <= 0) |
| 678 |
{ |
| 679 |
if (sel == 0) |
| 680 |
{ |
| 681 |
if (read_timer) |
| 682 |
goto read_timedout; |
| 683 |
else |
| 684 |
{ |
| 685 |
errno = ETIMEDOUT; |
| 686 |
err = -1; |
| 687 |
} |
| 688 |
} |
| 689 |
break; |
| 690 |
} |
| 691 |
if (read_timer) |
| 692 |
{ |
| 693 |
if ( (read_timer->next_timeout = read_timer->timeout - ptimer_measure (read_timer->timer)) <= 0 ) |
| 694 |
{ |
| 695 |
read_timedout: /* return GNUTLS_E_REAUTH_REQUEST for gnutls_read */ |
| 696 |
err = GNUTLS_E_REAUTH_REQUEST; |
| 697 |
read_timer->timed_out = 1; |
| 698 |
break; |
| 699 |
} |
| 700 |
next_timeout = read_timer->next_timeout; |
| 701 |
} |
| 702 |
} |
| 703 |
else if (err < 0) |
| 704 |
{ |
| 705 |
logprintf (LOG_NOTQUIET, "GnuTLS: %s\n", gnutls_strerror (err)); |
| 706 |
} |
| 707 |
} |
| 708 |
while (err && gnutls_error_is_fatal (err) == 0); |
| 709 |
|
| 710 |
if (!read_timer && next_timeout) |
| 711 |
{ |
| 712 |
#ifdef F_GETFL |
| 713 |
if (fcntl (fd, F_SETFL, flags) < 0) |
| 714 |
return -1; |
| 715 |
#else |
| 716 |
const int zero = 0; |
| 717 |
if (ioctl (fd, FIONBIO, &zero) < 0) |
| 718 |
return -1; |
| 719 |
#endif |
| 720 |
} |
| 721 |
|
| 722 |
return err; |
| 723 |
} |
| 724 |
#endif |
| 725 |
|
| 726 |
static const char * |
| 727 |
_sni_hostname(const char *hostname) |
| 728 |
{ |
| 729 |
size_t len = strlen(hostname); |
| 730 |
|
| 731 |
char *sni_hostname = xmemdup(hostname, len + 1); |
| 732 |
|
| 733 |
/* Remove trailing dot(s) to fix #47408. |
| 734 |
* Regarding RFC 6066 (SNI): The hostname is represented as a byte |
| 735 |
* string using ASCII encoding without a trailing dot. */ |
| 736 |
while (len && sni_hostname[--len] == '.') |
| 737 |
sni_hostname[len] = 0; |
| 738 |
|
| 739 |
return sni_hostname; |
| 740 |
} |
| 741 |
|
| 742 |
static int |
| 743 |
set_prio_default (gnutls_session_t session) |
| 744 |
{ |
| 745 |
int err = -1; |
| 746 |
|
| 747 |
#if HAVE_GNUTLS_PRIORITY_SET_DIRECT |
| 748 |
switch (opt.secure_protocol) |
| 749 |
{ |
| 750 |
case secure_protocol_auto: |
| 751 |
err = gnutls_set_default_priority (session); |
| 752 |
gnutls_session_enable_compatibility_mode(session); |
| 753 |
break; |
| 754 |
|
| 755 |
case secure_protocol_sslv2: |
| 756 |
case secure_protocol_sslv3: |
| 757 |
err = gnutls_priority_set_direct (session, "NORMAL:-VERS-TLS-ALL:+VERS-SSL3.0", NULL); |
| 758 |
break; |
| 759 |
|
| 760 |
case secure_protocol_tlsv1: |
| 761 |
err = gnutls_priority_set_direct (session, "NORMAL:-VERS-SSL3.0", NULL); |
| 762 |
break; |
| 763 |
|
| 764 |
case secure_protocol_tlsv1_1: |
| 765 |
err = gnutls_priority_set_direct (session, "NORMAL:-VERS-SSL3.0:-VERS-TLS1.0", NULL); |
| 766 |
break; |
| 767 |
|
| 768 |
case secure_protocol_tlsv1_2: |
| 769 |
err = gnutls_priority_set_direct (session, "NORMAL:-VERS-SSL3.0:-VERS-TLS1.0:-VERS-TLS1.1", NULL); |
| 770 |
break; |
| 771 |
|
| 772 |
case secure_protocol_tlsv1_3: |
| 773 |
#if GNUTLS_VERSION_NUMBER >= 0x030603 |
| 774 |
err = gnutls_priority_set_direct (session, "NORMAL:-VERS-SSL3.0:+VERS-TLS1.3:-VERS-TLS1.0:-VERS-TLS1.1:-VERS-TLS1.2", NULL); |
| 775 |
break; |
| 776 |
#else |
| 777 |
logprintf (LOG_NOTQUIET, _("Your GnuTLS version is too old to support TLS 1.3\n")); |
| 778 |
return -1; |
| 779 |
#endif |
| 780 |
|
| 781 |
case secure_protocol_pfs: |
| 782 |
err = gnutls_priority_set_direct (session, "PFS:-VERS-SSL3.0", NULL); |
| 783 |
if (err != GNUTLS_E_SUCCESS) |
| 784 |
/* fallback if PFS is not available */ |
| 785 |
err = gnutls_priority_set_direct (session, "NORMAL:-RSA:-VERS-SSL3.0", NULL); |
| 786 |
break; |
| 787 |
|
| 788 |
default: |
| 789 |
logprintf (LOG_NOTQUIET, _("GnuTLS: unimplemented 'secure-protocol' option value %u\n"), |
| 790 |
(unsigned) opt.secure_protocol); |
| 791 |
logprintf (LOG_NOTQUIET, _("Please report this issue to [email protected]\n")); |
| 792 |
abort (); |
| 793 |
} |
| 794 |
#else |
| 795 |
int allowed_protocols[4] = {0, 0, 0, 0}; |
| 796 |
switch (opt.secure_protocol) |
| 797 |
{ |
| 798 |
case secure_protocol_auto: |
| 799 |
err = gnutls_set_default_priority (session); |
| 800 |
break; |
| 801 |
|
| 802 |
case secure_protocol_sslv2: |
| 803 |
case secure_protocol_sslv3: |
| 804 |
allowed_protocols[0] = GNUTLS_SSL3; |
| 805 |
err = gnutls_protocol_set_priority (session, allowed_protocols); |
| 806 |
break; |
| 807 |
|
| 808 |
case secure_protocol_tlsv1: |
| 809 |
allowed_protocols[0] = GNUTLS_TLS1_0; |
| 810 |
allowed_protocols[1] = GNUTLS_TLS1_1; |
| 811 |
allowed_protocols[2] = GNUTLS_TLS1_2; |
| 812 |
#if GNUTLS_VERSION_NUMBER >= 0x030603 |
| 813 |
allowed_protocols[3] = GNUTLS_TLS1_3; |
| 814 |
#endif |
| 815 |
err = gnutls_protocol_set_priority (session, allowed_protocols); |
| 816 |
break; |
| 817 |
|
| 818 |
case secure_protocol_tlsv1_1: |
| 819 |
allowed_protocols[0] = GNUTLS_TLS1_1; |
| 820 |
allowed_protocols[1] = GNUTLS_TLS1_2; |
| 821 |
#if GNUTLS_VERSION_NUMBER >= 0x030603 |
| 822 |
allowed_protocols[2] = GNUTLS_TLS1_3; |
| 823 |
#endif |
| 824 |
err = gnutls_protocol_set_priority (session, allowed_protocols); |
| 825 |
break; |
| 826 |
|
| 827 |
case secure_protocol_tlsv1_2: |
| 828 |
allowed_protocols[0] = GNUTLS_TLS1_2; |
| 829 |
#if GNUTLS_VERSION_NUMBER >= 0x030603 |
| 830 |
allowed_protocols[1] = GNUTLS_TLS1_3; |
| 831 |
#endif |
| 832 |
err = gnutls_protocol_set_priority (session, allowed_protocols); |
| 833 |
break; |
| 834 |
|
| 835 |
case secure_protocol_tlsv1_3: |
| 836 |
#if GNUTLS_VERSION_NUMBER >= 0x030603 |
| 837 |
allowed_protocols[0] = GNUTLS_TLS1_3; |
| 838 |
err = gnutls_protocol_set_priority (session, allowed_protocols); |
| 839 |
break; |
| 840 |
#else |
| 841 |
logprintf (LOG_NOTQUIET, _("Your GnuTLS version is too old to support TLS 1.3\n")); |
| 842 |
return -1; |
| 843 |
#endif |
| 844 |
|
| 845 |
default: |
| 846 |
logprintf (LOG_NOTQUIET, _("GnuTLS: unimplemented 'secure-protocol' option value %d\n"), opt.secure_protocol); |
| 847 |
logprintf (LOG_NOTQUIET, _("Please report this issue to [email protected]\n")); |
| 848 |
abort (); |
| 849 |
} |
| 850 |
#endif |
| 851 |
|
| 852 |
return err; |
| 853 |
} |
| 854 |
|
| 855 |
bool |
| 856 |
ssl_connect_wget (int fd, const char *hostname, int *continue_session) |
| 857 |
{ |
| 858 |
struct wgnutls_transport_context *ctx; |
| 859 |
gnutls_session_t session; |
| 860 |
int err; |
| 861 |
|
| 862 |
#if GNUTLS_VERSION_NUMBER >= 0x030604 |
| 863 |
// enable support of TLS1.3 post-handshake authentication |
| 864 |
gnutls_init (&session, GNUTLS_CLIENT | GNUTLS_POST_HANDSHAKE_AUTH); |
| 865 |
#else |
| 866 |
gnutls_init (&session, GNUTLS_CLIENT); |
| 867 |
#endif |
| 868 |
|
| 869 |
/* We set the server name but only if it's not an IP address. */ |
| 870 |
if (! is_valid_ip_address (hostname)) |
| 871 |
{ |
| 872 |
/* GnuTLS 3.4.x (x<=10) disrespects the length parameter, we have to construct a new string */ |
| 873 |
/* see https://gitlab.com/gnutls/gnutls/issues/78 */ |
| 874 |
const char *sni_hostname = _sni_hostname(hostname); |
| 875 |
|
| 876 |
gnutls_server_name_set (session, GNUTLS_NAME_DNS, sni_hostname, strlen(sni_hostname)); |
| 877 |
xfree(sni_hostname); |
| 878 |
} |
| 879 |
|
| 880 |
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, credentials); |
| 881 |
#ifndef FD_TO_SOCKET |
| 882 |
# define FD_TO_SOCKET(X) (X) |
| 883 |
#endif |
| 884 |
#ifdef HAVE_INTPTR_T |
| 885 |
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) FD_TO_SOCKET (fd)); |
| 886 |
#else |
| 887 |
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) FD_TO_SOCKET (fd)); |
| 888 |
#endif |
| 889 |
|
| 890 |
if (!opt.tls_ciphers_string) |
| 891 |
{ |
| 892 |
err = set_prio_default (session); |
| 893 |
} |
| 894 |
else |
| 895 |
{ |
| 896 |
#if HAVE_GNUTLS_PRIORITY_SET_DIRECT |
| 897 |
err = gnutls_priority_set_direct (session, opt.tls_ciphers_string, NULL); |
| 898 |
#else |
| 899 |
logprintf (LOG_NOTQUIET, _("GnuTLS: Cannot set prio string directly. Falling back to default priority.\n")); |
| 900 |
err = gnutls_set_default_priority (session); |
| 901 |
#endif |
| 902 |
} |
| 903 |
|
| 904 |
if (err < 0) |
| 905 |
{ |
| 906 |
logprintf (LOG_NOTQUIET, "GnuTLS: %s\n", gnutls_strerror (err)); |
| 907 |
gnutls_deinit (session); |
| 908 |
return false; |
| 909 |
} |
| 910 |
|
| 911 |
if (continue_session) |
| 912 |
{ |
| 913 |
ctx = (struct wgnutls_transport_context *) fd_transport_context (*continue_session); |
| 914 |
if (!gnutls_session_is_resumed (session)) |
| 915 |
{ |
| 916 |
if (!ctx || !ctx->session_data || gnutls_session_set_data (session, ctx->session_data->data, ctx->session_data->size)) |
| 917 |
{ |
| 918 |
if (ctx && ctx->session_data) |
| 919 |
{ |
| 920 |
/* server does not want to continue the session */ |
| 921 |
if (ctx->session_data->data) |
| 922 |
gnutls_free (ctx->session_data->data); |
| 923 |
gnutls_free (ctx->session_data); |
| 924 |
} |
| 925 |
gnutls_deinit (session); |
| 926 |
return false; |
| 927 |
} |
| 928 |
} |
| 929 |
else |
| 930 |
{ |
| 931 |
logputs (LOG_ALWAYS, "SSL session has already been resumed. Continuing.\n"); |
| 932 |
continue_session = NULL; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
err = _do_handshake (session, fd, NULL); |
| 937 |
|
| 938 |
if (err < 0) |
| 939 |
{ |
| 940 |
gnutls_deinit (session); |
| 941 |
return false; |
| 942 |
} |
| 943 |
|
| 944 |
ctx = xnew0 (struct wgnutls_transport_context); |
| 945 |
ctx->session_data = xnew0 (gnutls_datum_t); |
| 946 |
ctx->session = session; |
| 947 |
if (gnutls_session_get_data2 (session, ctx->session_data)) |
| 948 |
{ |
| 949 |
xfree (ctx->session_data); |
| 950 |
logprintf (LOG_NOTQUIET, "WARNING: Could not save SSL session data for socket %d\n", fd); |
| 951 |
} |
| 952 |
fd_register_transport (fd, &wgnutls_transport, ctx); |
| 953 |
return true; |
| 954 |
} |
| 955 |
|
| 956 |
static bool |
| 957 |
pkp_pin_peer_pubkey (gnutls_x509_crt_t cert, const char *pinnedpubkey) |
| 958 |
{ |
| 959 |
/* Scratch */ |
| 960 |
size_t len1 = 0, len2 = 0; |
| 961 |
char *buff1 = NULL; |
| 962 |
|
| 963 |
gnutls_pubkey_t key = NULL; |
| 964 |
|
| 965 |
/* Result is returned to caller */ |
| 966 |
int ret = 0; |
| 967 |
bool result = false; |
| 968 |
|
| 969 |
/* if a path wasn't specified, don't pin */ |
| 970 |
if (NULL == pinnedpubkey) |
| 971 |
return true; |
| 972 |
|
| 973 |
if (NULL == cert) |
| 974 |
return result; |
| 975 |
|
| 976 |
/* Begin Gyrations to get the public key */ |
| 977 |
gnutls_pubkey_init (&key); |
| 978 |
|
| 979 |
ret = gnutls_pubkey_import_x509 (key, cert, 0); |
| 980 |
if (ret < 0) |
| 981 |
goto cleanup; /* failed */ |
| 982 |
|
| 983 |
ret = gnutls_pubkey_export (key, GNUTLS_X509_FMT_DER, NULL, &len1); |
| 984 |
if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER || len1 == 0) |
| 985 |
goto cleanup; /* failed */ |
| 986 |
|
| 987 |
buff1 = xmalloc (len1); |
| 988 |
|
| 989 |
len2 = len1; |
| 990 |
|
| 991 |
ret = gnutls_pubkey_export (key, GNUTLS_X509_FMT_DER, buff1, &len2); |
| 992 |
if (ret < 0 || len1 != len2) |
| 993 |
goto cleanup; /* failed */ |
| 994 |
|
| 995 |
/* End Gyrations */ |
| 996 |
|
| 997 |
/* The one good exit point */ |
| 998 |
result = wg_pin_peer_pubkey (pinnedpubkey, buff1, len1); |
| 999 |
|
| 1000 |
cleanup: |
| 1001 |
if (NULL != key) |
| 1002 |
gnutls_pubkey_deinit (key); |
| 1003 |
|
| 1004 |
xfree (buff1); |
| 1005 |
|
| 1006 |
return result; |
| 1007 |
} |
| 1008 |
|
| 1009 |
#define _CHECK_CERT(flag,msg) \ |
| 1010 |
if (status & (flag))\ |
| 1011 |
{\ |
| 1012 |
logprintf (LOG_NOTQUIET, (msg),\ |
| 1013 |
severity, quote (host));\ |
| 1014 |
success = false;\ |
| 1015 |
} |
| 1016 |
|
| 1017 |
bool |
| 1018 |
ssl_check_certificate (int fd, const char *host) |
| 1019 |
{ |
| 1020 |
struct wgnutls_transport_context *ctx = fd_transport_context (fd); |
| 1021 |
|
| 1022 |
unsigned int status; |
| 1023 |
int err; |
| 1024 |
|
| 1025 |
/* If the user has specified --no-check-cert, we still want to warn |
| 1026 |
him about problems with the server's certificate. */ |
| 1027 |
const char *severity = opt.check_cert ? _("ERROR") : _("WARNING"); |
| 1028 |
bool success = true; |
| 1029 |
bool pinsuccess = opt.pinnedpubkey == NULL; |
| 1030 |
|
| 1031 |
/* The user explicitly said to not check for the certificate. */ |
| 1032 |
if (opt.check_cert == CHECK_CERT_QUIET && pinsuccess) |
| 1033 |
return success; |
| 1034 |
|
| 1035 |
err = gnutls_certificate_verify_peers2 (ctx->session, &status); |
| 1036 |
if (err < 0) |
| 1037 |
{ |
| 1038 |
logprintf (LOG_NOTQUIET, _("%s: No certificate presented by %s.\n"), |
| 1039 |
severity, quotearg_style (escape_quoting_style, host)); |
| 1040 |
success = false; |
| 1041 |
goto out; |
| 1042 |
} |
| 1043 |
|
| 1044 |
_CHECK_CERT (GNUTLS_CERT_INVALID, _("%s: The certificate of %s is not trusted.\n")); |
| 1045 |
_CHECK_CERT (GNUTLS_CERT_SIGNER_NOT_FOUND, _("%s: The certificate of %s doesn't have a known issuer.\n")); |
| 1046 |
_CHECK_CERT (GNUTLS_CERT_REVOKED, _("%s: The certificate of %s has been revoked.\n")); |
| 1047 |
_CHECK_CERT (GNUTLS_CERT_SIGNER_NOT_CA, _("%s: The certificate signer of %s was not a CA.\n")); |
| 1048 |
_CHECK_CERT (GNUTLS_CERT_INSECURE_ALGORITHM, _("%s: The certificate of %s was signed using an insecure algorithm.\n")); |
| 1049 |
_CHECK_CERT (GNUTLS_CERT_NOT_ACTIVATED, _("%s: The certificate of %s is not yet activated.\n")); |
| 1050 |
_CHECK_CERT (GNUTLS_CERT_EXPIRED, _("%s: The certificate of %s has expired.\n")); |
| 1051 |
|
| 1052 |
if (gnutls_certificate_type_get (ctx->session) == GNUTLS_CRT_X509) |
| 1053 |
{ |
| 1054 |
time_t now = time (NULL); |
| 1055 |
gnutls_x509_crt_t cert; |
| 1056 |
const gnutls_datum_t *cert_list; |
| 1057 |
unsigned int cert_list_size; |
| 1058 |
const char *sni_hostname; |
| 1059 |
|
| 1060 |
if ((err = gnutls_x509_crt_init (&cert)) < 0) |
| 1061 |
{ |
| 1062 |
logprintf (LOG_NOTQUIET, _("Error initializing X509 certificate: %s\n"), |
| 1063 |
gnutls_strerror (err)); |
| 1064 |
success = false; |
| 1065 |
goto out; |
| 1066 |
} |
| 1067 |
|
| 1068 |
cert_list = gnutls_certificate_get_peers (ctx->session, &cert_list_size); |
| 1069 |
if (!cert_list) |
| 1070 |
{ |
| 1071 |
logprintf (LOG_NOTQUIET, _("No certificate found\n")); |
| 1072 |
success = false; |
| 1073 |
goto crt_deinit; |
| 1074 |
} |
| 1075 |
err = gnutls_x509_crt_import (cert, cert_list, GNUTLS_X509_FMT_DER); |
| 1076 |
if (err < 0) |
| 1077 |
{ |
| 1078 |
logprintf (LOG_NOTQUIET, _("Error parsing certificate: %s\n"), |
| 1079 |
gnutls_strerror (err)); |
| 1080 |
success = false; |
| 1081 |
goto crt_deinit; |
| 1082 |
} |
| 1083 |
if (now < gnutls_x509_crt_get_activation_time (cert)) |
| 1084 |
{ |
| 1085 |
logprintf (LOG_NOTQUIET, _("The certificate has not yet been activated\n")); |
| 1086 |
success = false; |
| 1087 |
} |
| 1088 |
if (now >= gnutls_x509_crt_get_expiration_time (cert)) |
| 1089 |
{ |
| 1090 |
logprintf (LOG_NOTQUIET, _("The certificate has expired\n")); |
| 1091 |
success = false; |
| 1092 |
} |
| 1093 |
sni_hostname = _sni_hostname(host); |
| 1094 |
if (!gnutls_x509_crt_check_hostname (cert, sni_hostname)) |
| 1095 |
{ |
| 1096 |
logprintf (LOG_NOTQUIET, |
| 1097 |
_("The certificate's owner does not match hostname %s\n"), |
| 1098 |
quote (sni_hostname)); |
| 1099 |
success = false; |
| 1100 |
} |
| 1101 |
xfree(sni_hostname); |
| 1102 |
|
| 1103 |
pinsuccess = pkp_pin_peer_pubkey (cert, opt.pinnedpubkey); |
| 1104 |
if (!pinsuccess) |
| 1105 |
{ |
| 1106 |
logprintf (LOG_ALWAYS, _("The public key does not match pinned public key!\n")); |
| 1107 |
success = false; |
| 1108 |
} |
| 1109 |
|
| 1110 |
crt_deinit: |
| 1111 |
gnutls_x509_crt_deinit (cert); |
| 1112 |
} |
| 1113 |
else |
| 1114 |
{ |
| 1115 |
logprintf (LOG_NOTQUIET, _("Certificate must be X.509\n")); |
| 1116 |
success = false; |
| 1117 |
} |
| 1118 |
|
| 1119 |
out: |
| 1120 |
/* never return true if pinsuccess fails */ |
| 1121 |
return !pinsuccess ? false : (opt.check_cert == CHECK_CERT_ON ? success : true); |
| 1122 |
} |