1 /* 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright 2005 Nokia. All rights reserved. 4 * 5 * Licensed under the OpenSSL license (the "License"). You may not use 6 * this file except in compliance with the License. You can obtain a copy 7 * in the file LICENSE in the source distribution or at 8 * https://www.openssl.org/source/license.html 9 */ 10 11 #include "e_os.h" 12 #include <ctype.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <errno.h> 17 #include <openssl/e_os2.h> 18 19 #ifndef OPENSSL_NO_SOCK 20 21 /* 22 * With IPv6, it looks like Digital has mixed up the proper order of 23 * recursive header file inclusion, resulting in the compiler complaining 24 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is 25 * needed to have fileno() declared correctly... So let's define u_int 26 */ 27 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) 28 # define __U_INT 29 typedef unsigned int u_int; 30 #endif 31 32 #include "apps.h" 33 #include "progs.h" 34 #include <openssl/x509.h> 35 #include <openssl/ssl.h> 36 #include <openssl/err.h> 37 #include <openssl/pem.h> 38 #include <openssl/rand.h> 39 #include <openssl/ocsp.h> 40 #include <openssl/bn.h> 41 #include <openssl/async.h> 42 #ifndef OPENSSL_NO_SRP 43 # include <openssl/srp.h> 44 #endif 45 #ifndef OPENSSL_NO_CT 46 # include <openssl/ct.h> 47 #endif 48 #include "s_apps.h" 49 #include "timeouts.h" 50 #include "internal/sockets.h" 51 52 #if defined(__has_feature) 53 # if __has_feature(memory_sanitizer) 54 # include <sanitizer/msan_interface.h> 55 # endif 56 #endif 57 58 #undef BUFSIZZ 59 #define BUFSIZZ 1024*8 60 #define S_CLIENT_IRC_READ_TIMEOUT 8 61 62 static char *prog; 63 static int c_debug = 0; 64 static int c_showcerts = 0; 65 static char *keymatexportlabel = NULL; 66 static int keymatexportlen = 20; 67 static BIO *bio_c_out = NULL; 68 static int c_quiet = 0; 69 static char *sess_out = NULL; 70 static SSL_SESSION *psksess = NULL; 71 72 static void print_stuff(BIO *berr, SSL *con, int full); 73 #ifndef OPENSSL_NO_OCSP 74 static int ocsp_resp_cb(SSL *s, void *arg); 75 #endif 76 static int ldap_ExtendedResponse_parse(const char *buf, long rem); 77 78 static int saved_errno; 79 80 static void save_errno(void) 81 { 82 saved_errno = errno; 83 errno = 0; 84 } 85 86 static int restore_errno(void) 87 { 88 int ret = errno; 89 errno = saved_errno; 90 return ret; 91 } 92 93 static void do_ssl_shutdown(SSL *ssl) 94 { 95 int ret; 96 97 do { 98 /* We only do unidirectional shutdown */ 99 ret = SSL_shutdown(ssl); 100 if (ret < 0) { 101 switch (SSL_get_error(ssl, ret)) { 102 case SSL_ERROR_WANT_READ: 103 case SSL_ERROR_WANT_WRITE: 104 case SSL_ERROR_WANT_ASYNC: 105 case SSL_ERROR_WANT_ASYNC_JOB: 106 /* We just do busy waiting. Nothing clever */ 107 continue; 108 } 109 ret = 0; 110 } 111 } while (ret < 0); 112 } 113 114 /* Default PSK identity and key */ 115 static char *psk_identity = "Client_identity"; 116 117 #ifndef OPENSSL_NO_PSK 118 static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity, 119 unsigned int max_identity_len, 120 unsigned char *psk, 121 unsigned int max_psk_len) 122 { 123 int ret; 124 long key_len; 125 unsigned char *key; 126 127 if (c_debug) 128 BIO_printf(bio_c_out, "psk_client_cb\n"); 129 if (!hint) { 130 /* no ServerKeyExchange message */ 131 if (c_debug) 132 BIO_printf(bio_c_out, 133 "NULL received PSK identity hint, continuing anyway\n"); 134 } else if (c_debug) { 135 BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint); 136 } 137 138 /* 139 * lookup PSK identity and PSK key based on the given identity hint here 140 */ 141 ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity); 142 if (ret < 0 || (unsigned int)ret > max_identity_len) 143 goto out_err; 144 if (c_debug) 145 BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, 146 ret); 147 148 /* convert the PSK key to binary */ 149 key = OPENSSL_hexstr2buf(psk_key, &key_len); 150 if (key == NULL) { 151 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n", 152 psk_key); 153 return 0; 154 } 155 if (max_psk_len > INT_MAX || key_len > (long)max_psk_len) { 156 BIO_printf(bio_err, 157 "psk buffer of callback is too small (%d) for key (%ld)\n", 158 max_psk_len, key_len); 159 OPENSSL_free(key); 160 return 0; 161 } 162 163 memcpy(psk, key, key_len); 164 OPENSSL_free(key); 165 166 if (c_debug) 167 BIO_printf(bio_c_out, "created PSK len=%ld\n", key_len); 168 169 return key_len; 170 out_err: 171 if (c_debug) 172 BIO_printf(bio_err, "Error in PSK client callback\n"); 173 return 0; 174 } 175 #endif 176 177 const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; 178 const unsigned char tls13_aes256gcmsha384_id[] = { 0x13, 0x02 }; 179 180 static int psk_use_session_cb(SSL *s, const EVP_MD *md, 181 const unsigned char **id, size_t *idlen, 182 SSL_SESSION **sess) 183 { 184 SSL_SESSION *usesess = NULL; 185 const SSL_CIPHER *cipher = NULL; 186 187 if (psksess != NULL) { 188 SSL_SESSION_up_ref(psksess); 189 usesess = psksess; 190 } else { 191 long key_len; 192 unsigned char *key = OPENSSL_hexstr2buf(psk_key, &key_len); 193 194 if (key == NULL) { 195 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n", 196 psk_key); 197 return 0; 198 } 199 200 /* We default to SHA-256 */ 201 cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id); 202 if (cipher == NULL) { 203 BIO_printf(bio_err, "Error finding suitable ciphersuite\n"); 204 OPENSSL_free(key); 205 return 0; 206 } 207 208 usesess = SSL_SESSION_new(); 209 if (usesess == NULL 210 || !SSL_SESSION_set1_master_key(usesess, key, key_len) 211 || !SSL_SESSION_set_cipher(usesess, cipher) 212 || !SSL_SESSION_set_protocol_version(usesess, TLS1_3_VERSION)) { 213 OPENSSL_free(key); 214 goto err; 215 } 216 OPENSSL_free(key); 217 } 218 219 cipher = SSL_SESSION_get0_cipher(usesess); 220 if (cipher == NULL) 221 goto err; 222 223 if (md != NULL && SSL_CIPHER_get_handshake_digest(cipher) != md) { 224 /* PSK not usable, ignore it */ 225 *id = NULL; 226 *idlen = 0; 227 *sess = NULL; 228 SSL_SESSION_free(usesess); 229 } else { 230 *sess = usesess; 231 *id = (unsigned char *)psk_identity; 232 *idlen = strlen(psk_identity); 233 } 234 235 return 1; 236 237 err: 238 SSL_SESSION_free(usesess); 239 return 0; 240 } 241 242 /* This is a context that we pass to callbacks */ 243 typedef struct tlsextctx_st { 244 BIO *biodebug; 245 int ack; 246 } tlsextctx; 247 248 static int ssl_servername_cb(SSL *s, int *ad, void *arg) 249 { 250 tlsextctx *p = (tlsextctx *) arg; 251 const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); 252 if (SSL_get_servername_type(s) != -1) 253 p->ack = !SSL_session_reused(s) && hn != NULL; 254 else 255 BIO_printf(bio_err, "Can't use SSL_get_servername\n"); 256 257 return SSL_TLSEXT_ERR_OK; 258 } 259 260 #ifndef OPENSSL_NO_SRP 261 262 /* This is a context that we pass to all callbacks */ 263 typedef struct srp_arg_st { 264 char *srppassin; 265 char *srplogin; 266 int msg; /* copy from c_msg */ 267 int debug; /* copy from c_debug */ 268 int amp; /* allow more groups */ 269 int strength; /* minimal size for N */ 270 } SRP_ARG; 271 272 # define SRP_NUMBER_ITERATIONS_FOR_PRIME 64 273 274 static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g) 275 { 276 BN_CTX *bn_ctx = BN_CTX_new(); 277 BIGNUM *p = BN_new(); 278 BIGNUM *r = BN_new(); 279 int ret = 280 g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) && 281 BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 && 282 p != NULL && BN_rshift1(p, N) && 283 /* p = (N-1)/2 */ 284 BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 && 285 r != NULL && 286 /* verify g^((N-1)/2) == -1 (mod N) */ 287 BN_mod_exp(r, g, p, N, bn_ctx) && 288 BN_add_word(r, 1) && BN_cmp(r, N) == 0; 289 290 BN_free(r); 291 BN_free(p); 292 BN_CTX_free(bn_ctx); 293 return ret; 294 } 295 296 /*- 297 * This callback is used here for two purposes: 298 * - extended debugging 299 * - making some primality tests for unknown groups 300 * The callback is only called for a non default group. 301 * 302 * An application does not need the call back at all if 303 * only the standard groups are used. In real life situations, 304 * client and server already share well known groups, 305 * thus there is no need to verify them. 306 * Furthermore, in case that a server actually proposes a group that 307 * is not one of those defined in RFC 5054, it is more appropriate 308 * to add the group to a static list and then compare since 309 * primality tests are rather cpu consuming. 310 */ 311 312 static int ssl_srp_verify_param_cb(SSL *s, void *arg) 313 { 314 SRP_ARG *srp_arg = (SRP_ARG *)arg; 315 BIGNUM *N = NULL, *g = NULL; 316 317 if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL)) 318 return 0; 319 if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) { 320 BIO_printf(bio_err, "SRP parameters:\n"); 321 BIO_printf(bio_err, "\tN="); 322 BN_print(bio_err, N); 323 BIO_printf(bio_err, "\n\tg="); 324 BN_print(bio_err, g); 325 BIO_printf(bio_err, "\n"); 326 } 327 328 if (SRP_check_known_gN_param(g, N)) 329 return 1; 330 331 if (srp_arg->amp == 1) { 332 if (srp_arg->debug) 333 BIO_printf(bio_err, 334 "SRP param N and g are not known params, going to check deeper.\n"); 335 336 /* 337 * The srp_moregroups is a real debugging feature. Implementors 338 * should rather add the value to the known ones. The minimal size 339 * has already been tested. 340 */ 341 if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g)) 342 return 1; 343 } 344 BIO_printf(bio_err, "SRP param N and g rejected.\n"); 345 return 0; 346 } 347 348 # define PWD_STRLEN 1024 349 350 static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg) 351 { 352 SRP_ARG *srp_arg = (SRP_ARG *)arg; 353 char *pass = app_malloc(PWD_STRLEN + 1, "SRP password buffer"); 354 PW_CB_DATA cb_tmp; 355 int l; 356 357 cb_tmp.password = (char *)srp_arg->srppassin; 358 cb_tmp.prompt_info = "SRP user"; 359 if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) { 360 BIO_printf(bio_err, "Can't read Password\n"); 361 OPENSSL_free(pass); 362 return NULL; 363 } 364 *(pass + l) = '\0'; 365 366 return pass; 367 } 368 369 #endif 370 371 #ifndef OPENSSL_NO_NEXTPROTONEG 372 /* This the context that we pass to next_proto_cb */ 373 typedef struct tlsextnextprotoctx_st { 374 unsigned char *data; 375 size_t len; 376 int status; 377 } tlsextnextprotoctx; 378 379 static tlsextnextprotoctx next_proto; 380 381 static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen, 382 const unsigned char *in, unsigned int inlen, 383 void *arg) 384 { 385 tlsextnextprotoctx *ctx = arg; 386 387 if (!c_quiet) { 388 /* We can assume that |in| is syntactically valid. */ 389 unsigned i; 390 BIO_printf(bio_c_out, "Protocols advertised by server: "); 391 for (i = 0; i < inlen;) { 392 if (i) 393 BIO_write(bio_c_out, ", ", 2); 394 BIO_write(bio_c_out, &in[i + 1], in[i]); 395 i += in[i] + 1; 396 } 397 BIO_write(bio_c_out, "\n", 1); 398 } 399 400 ctx->status = 401 SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len); 402 return SSL_TLSEXT_ERR_OK; 403 } 404 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */ 405 406 static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type, 407 const unsigned char *in, size_t inlen, 408 int *al, void *arg) 409 { 410 char pem_name[100]; 411 unsigned char ext_buf[4 + 65536]; 412 413 /* Reconstruct the type/len fields prior to extension data */ 414 inlen &= 0xffff; /* for formal memcmpy correctness */ 415 ext_buf[0] = (unsigned char)(ext_type >> 8); 416 ext_buf[1] = (unsigned char)(ext_type); 417 ext_buf[2] = (unsigned char)(inlen >> 8); 418 ext_buf[3] = (unsigned char)(inlen); 419 memcpy(ext_buf + 4, in, inlen); 420 421 BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d", 422 ext_type); 423 PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen); 424 return 1; 425 } 426 427 /* 428 * Hex decoder that tolerates optional whitespace. Returns number of bytes 429 * produced, advances inptr to end of input string. 430 */ 431 static ossl_ssize_t hexdecode(const char **inptr, void *result) 432 { 433 unsigned char **out = (unsigned char **)result; 434 const char *in = *inptr; 435 unsigned char *ret = app_malloc(strlen(in) / 2, "hexdecode"); 436 unsigned char *cp = ret; 437 uint8_t byte; 438 int nibble = 0; 439 440 if (ret == NULL) 441 return -1; 442 443 for (byte = 0; *in; ++in) { 444 int x; 445 446 if (isspace(_UC(*in))) 447 continue; 448 x = OPENSSL_hexchar2int(*in); 449 if (x < 0) { 450 OPENSSL_free(ret); 451 return 0; 452 } 453 byte |= (char)x; 454 if ((nibble ^= 1) == 0) { 455 *cp++ = byte; 456 byte = 0; 457 } else { 458 byte <<= 4; 459 } 460 } 461 if (nibble != 0) { 462 OPENSSL_free(ret); 463 return 0; 464 } 465 *inptr = in; 466 467 return cp - (*out = ret); 468 } 469 470 /* 471 * Decode unsigned 0..255, returns 1 on success, <= 0 on failure. Advances 472 * inptr to next field skipping leading whitespace. 473 */ 474 static ossl_ssize_t checked_uint8(const char **inptr, void *out) 475 { 476 uint8_t *result = (uint8_t *)out; 477 const char *in = *inptr; 478 char *endp; 479 long v; 480 int e; 481 482 save_errno(); 483 v = strtol(in, &endp, 10); 484 e = restore_errno(); 485 486 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) || 487 endp == in || !isspace(_UC(*endp)) || 488 v != (*result = (uint8_t) v)) { 489 return -1; 490 } 491 for (in = endp; isspace(_UC(*in)); ++in) 492 continue; 493 494 *inptr = in; 495 return 1; 496 } 497 498 struct tlsa_field { 499 void *var; 500 const char *name; 501 ossl_ssize_t (*parser)(const char **, void *); 502 }; 503 504 static int tlsa_import_rr(SSL *con, const char *rrdata) 505 { 506 /* Not necessary to re-init these values; the "parsers" do that. */ 507 static uint8_t usage; 508 static uint8_t selector; 509 static uint8_t mtype; 510 static unsigned char *data; 511 static struct tlsa_field tlsa_fields[] = { 512 { &usage, "usage", checked_uint8 }, 513 { &selector, "selector", checked_uint8 }, 514 { &mtype, "mtype", checked_uint8 }, 515 { &data, "data", hexdecode }, 516 { NULL, } 517 }; 518 struct tlsa_field *f; 519 int ret; 520 const char *cp = rrdata; 521 ossl_ssize_t len = 0; 522 523 for (f = tlsa_fields; f->var; ++f) { 524 /* Returns number of bytes produced, advances cp to next field */ 525 if ((len = f->parser(&cp, f->var)) <= 0) { 526 BIO_printf(bio_err, "%s: warning: bad TLSA %s field in: %s\n", 527 prog, f->name, rrdata); 528 return 0; 529 } 530 } 531 /* The data field is last, so len is its length */ 532 ret = SSL_dane_tlsa_add(con, usage, selector, mtype, data, len); 533 OPENSSL_free(data); 534 535 if (ret == 0) { 536 ERR_print_errors(bio_err); 537 BIO_printf(bio_err, "%s: warning: unusable TLSA rrdata: %s\n", 538 prog, rrdata); 539 return 0; 540 } 541 if (ret < 0) { 542 ERR_print_errors(bio_err); 543 BIO_printf(bio_err, "%s: warning: error loading TLSA rrdata: %s\n", 544 prog, rrdata); 545 return 0; 546 } 547 return ret; 548 } 549 550 static int tlsa_import_rrset(SSL *con, STACK_OF(OPENSSL_STRING) *rrset) 551 { 552 int num = sk_OPENSSL_STRING_num(rrset); 553 int count = 0; 554 int i; 555 556 for (i = 0; i < num; ++i) { 557 char *rrdata = sk_OPENSSL_STRING_value(rrset, i); 558 if (tlsa_import_rr(con, rrdata) > 0) 559 ++count; 560 } 561 return count > 0; 562 } 563 564 typedef enum OPTION_choice { 565 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, 566 OPT_4, OPT_6, OPT_HOST, OPT_PORT, OPT_CONNECT, OPT_BIND, OPT_UNIX, 567 OPT_XMPPHOST, OPT_VERIFY, OPT_NAMEOPT, 568 OPT_CERT, OPT_CRL, OPT_CRL_DOWNLOAD, OPT_SESS_OUT, OPT_SESS_IN, 569 OPT_CERTFORM, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET, 570 OPT_BRIEF, OPT_PREXIT, OPT_CRLF, OPT_QUIET, OPT_NBIO, 571 OPT_SSL_CLIENT_ENGINE, OPT_IGN_EOF, OPT_NO_IGN_EOF, 572 OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_WDEBUG, 573 OPT_MSG, OPT_MSGFILE, OPT_ENGINE, OPT_TRACE, OPT_SECURITY_DEBUG, 574 OPT_SECURITY_DEBUG_VERBOSE, OPT_SHOWCERTS, OPT_NBIO_TEST, OPT_STATE, 575 OPT_PSK_IDENTITY, OPT_PSK, OPT_PSK_SESS, 576 #ifndef OPENSSL_NO_SRP 577 OPT_SRPUSER, OPT_SRPPASS, OPT_SRP_STRENGTH, OPT_SRP_LATEUSER, 578 OPT_SRP_MOREGROUPS, 579 #endif 580 OPT_SSL3, OPT_SSL_CONFIG, 581 OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1, 582 OPT_DTLS1_2, OPT_SCTP, OPT_TIMEOUT, OPT_MTU, OPT_KEYFORM, OPT_PASS, 583 OPT_CERT_CHAIN, OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH, 584 OPT_KEY, OPT_RECONNECT, OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE, 585 OPT_CHAINCAFILE, OPT_VERIFYCAFILE, OPT_NEXTPROTONEG, OPT_ALPN, 586 OPT_SERVERINFO, OPT_STARTTLS, OPT_SERVERNAME, OPT_NOSERVERNAME, OPT_ASYNC, 587 OPT_USE_SRTP, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN, OPT_PROTOHOST, 588 OPT_MAXFRAGLEN, OPT_MAX_SEND_FRAG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES, 589 OPT_READ_BUF, OPT_KEYLOG_FILE, OPT_EARLY_DATA, OPT_REQCAFILE, 590 OPT_V_ENUM, 591 OPT_X_ENUM, 592 OPT_S_ENUM, 593 OPT_FALLBACKSCSV, OPT_NOCMDS, OPT_PROXY, OPT_DANE_TLSA_DOMAIN, 594 #ifndef OPENSSL_NO_CT 595 OPT_CT, OPT_NOCT, OPT_CTLOG_FILE, 596 #endif 597 OPT_DANE_TLSA_RRDATA, OPT_DANE_EE_NO_NAME, 598 OPT_ENABLE_PHA, 599 OPT_R_ENUM 600 } OPTION_CHOICE; 601 602 const OPTIONS s_client_options[] = { 603 {"help", OPT_HELP, '-', "Display this summary"}, 604 {"host", OPT_HOST, 's', "Use -connect instead"}, 605 {"port", OPT_PORT, 'p', "Use -connect instead"}, 606 {"connect", OPT_CONNECT, 's', 607 "TCP/IP where to connect (default is :" PORT ")"}, 608 {"bind", OPT_BIND, 's', "bind local address for connection"}, 609 {"proxy", OPT_PROXY, 's', 610 "Connect to via specified proxy to the real server"}, 611 #ifdef AF_UNIX 612 {"unix", OPT_UNIX, 's', "Connect over the specified Unix-domain socket"}, 613 #endif 614 {"4", OPT_4, '-', "Use IPv4 only"}, 615 #ifdef AF_INET6 616 {"6", OPT_6, '-', "Use IPv6 only"}, 617 #endif 618 {"verify", OPT_VERIFY, 'p', "Turn on peer certificate verification"}, 619 {"cert", OPT_CERT, '<', "Certificate file to use, PEM format assumed"}, 620 {"certform", OPT_CERTFORM, 'F', 621 "Certificate format (PEM or DER) PEM default"}, 622 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"}, 623 {"key", OPT_KEY, 's', "Private key file to use, if not in -cert file"}, 624 {"keyform", OPT_KEYFORM, 'E', "Key format (PEM, DER or engine) PEM default"}, 625 {"pass", OPT_PASS, 's', "Private key file pass phrase source"}, 626 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"}, 627 {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"}, 628 {"no-CAfile", OPT_NOCAFILE, '-', 629 "Do not load the default certificates file"}, 630 {"no-CApath", OPT_NOCAPATH, '-', 631 "Do not load certificates from the default certificates directory"}, 632 {"requestCAfile", OPT_REQCAFILE, '<', 633 "PEM format file of CA names to send to the server"}, 634 {"dane_tlsa_domain", OPT_DANE_TLSA_DOMAIN, 's', "DANE TLSA base domain"}, 635 {"dane_tlsa_rrdata", OPT_DANE_TLSA_RRDATA, 's', 636 "DANE TLSA rrdata presentation form"}, 637 {"dane_ee_no_namechecks", OPT_DANE_EE_NO_NAME, '-', 638 "Disable name checks when matching DANE-EE(3) TLSA records"}, 639 {"reconnect", OPT_RECONNECT, '-', 640 "Drop and re-make the connection with the same Session-ID"}, 641 {"showcerts", OPT_SHOWCERTS, '-', 642 "Show all certificates sent by the server"}, 643 {"debug", OPT_DEBUG, '-', "Extra output"}, 644 {"msg", OPT_MSG, '-', "Show protocol messages"}, 645 {"msgfile", OPT_MSGFILE, '>', 646 "File to send output of -msg or -trace, instead of stdout"}, 647 {"nbio_test", OPT_NBIO_TEST, '-', "More ssl protocol testing"}, 648 {"state", OPT_STATE, '-', "Print the ssl states"}, 649 {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"}, 650 {"quiet", OPT_QUIET, '-', "No s_client output"}, 651 {"ign_eof", OPT_IGN_EOF, '-', "Ignore input eof (default when -quiet)"}, 652 {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Don't ignore input eof"}, 653 {"starttls", OPT_STARTTLS, 's', 654 "Use the appropriate STARTTLS command before starting TLS"}, 655 {"xmpphost", OPT_XMPPHOST, 's', 656 "Alias of -name option for \"-starttls xmpp[-server]\""}, 657 OPT_R_OPTIONS, 658 {"sess_out", OPT_SESS_OUT, '>', "File to write SSL session to"}, 659 {"sess_in", OPT_SESS_IN, '<', "File to read SSL session from"}, 660 #ifndef OPENSSL_NO_SRTP 661 {"use_srtp", OPT_USE_SRTP, 's', 662 "Offer SRTP key management with a colon-separated profile list"}, 663 #endif 664 {"keymatexport", OPT_KEYMATEXPORT, 's', 665 "Export keying material using label"}, 666 {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p', 667 "Export len bytes of keying material (default 20)"}, 668 {"maxfraglen", OPT_MAXFRAGLEN, 'p', 669 "Enable Maximum Fragment Length Negotiation (len values: 512, 1024, 2048 and 4096)"}, 670 {"fallback_scsv", OPT_FALLBACKSCSV, '-', "Send the fallback SCSV"}, 671 {"name", OPT_PROTOHOST, 's', 672 "Hostname to use for \"-starttls lmtp\", \"-starttls smtp\" or \"-starttls xmpp[-server]\""}, 673 {"CRL", OPT_CRL, '<', "CRL file to use"}, 674 {"crl_download", OPT_CRL_DOWNLOAD, '-', "Download CRL from distribution points"}, 675 {"CRLform", OPT_CRLFORM, 'F', "CRL format (PEM or DER) PEM is default"}, 676 {"verify_return_error", OPT_VERIFY_RET_ERROR, '-', 677 "Close connection on verification error"}, 678 {"verify_quiet", OPT_VERIFY_QUIET, '-', "Restrict verify output to errors"}, 679 {"brief", OPT_BRIEF, '-', 680 "Restrict output to brief summary of connection parameters"}, 681 {"prexit", OPT_PREXIT, '-', 682 "Print session information when the program exits"}, 683 {"security_debug", OPT_SECURITY_DEBUG, '-', 684 "Enable security debug messages"}, 685 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-', 686 "Output more security debug output"}, 687 {"cert_chain", OPT_CERT_CHAIN, '<', 688 "Certificate chain file (in PEM format)"}, 689 {"chainCApath", OPT_CHAINCAPATH, '/', 690 "Use dir as certificate store path to build CA certificate chain"}, 691 {"verifyCApath", OPT_VERIFYCAPATH, '/', 692 "Use dir as certificate store path to verify CA certificate"}, 693 {"build_chain", OPT_BUILD_CHAIN, '-', "Build certificate chain"}, 694 {"chainCAfile", OPT_CHAINCAFILE, '<', 695 "CA file for certificate chain (PEM format)"}, 696 {"verifyCAfile", OPT_VERIFYCAFILE, '<', 697 "CA file for certificate verification (PEM format)"}, 698 {"nocommands", OPT_NOCMDS, '-', "Do not use interactive command letters"}, 699 {"servername", OPT_SERVERNAME, 's', 700 "Set TLS extension servername (SNI) in ClientHello (default)"}, 701 {"noservername", OPT_NOSERVERNAME, '-', 702 "Do not send the server name (SNI) extension in the ClientHello"}, 703 {"tlsextdebug", OPT_TLSEXTDEBUG, '-', 704 "Hex dump of all TLS extensions received"}, 705 #ifndef OPENSSL_NO_OCSP 706 {"status", OPT_STATUS, '-', "Request certificate status from server"}, 707 #endif 708 {"serverinfo", OPT_SERVERINFO, 's', 709 "types Send empty ClientHello extensions (comma-separated numbers)"}, 710 {"alpn", OPT_ALPN, 's', 711 "Enable ALPN extension, considering named protocols supported (comma-separated list)"}, 712 {"async", OPT_ASYNC, '-', "Support asynchronous operation"}, 713 {"ssl_config", OPT_SSL_CONFIG, 's', "Use specified configuration file"}, 714 {"max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames "}, 715 {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'p', 716 "Size used to split data for encrypt pipelines"}, 717 {"max_pipelines", OPT_MAX_PIPELINES, 'p', 718 "Maximum number of encrypt/decrypt pipelines to be used"}, 719 {"read_buf", OPT_READ_BUF, 'p', 720 "Default read buffer size to be used for connections"}, 721 OPT_S_OPTIONS, 722 OPT_V_OPTIONS, 723 OPT_X_OPTIONS, 724 #ifndef OPENSSL_NO_SSL3 725 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"}, 726 #endif 727 #ifndef OPENSSL_NO_TLS1 728 {"tls1", OPT_TLS1, '-', "Just use TLSv1"}, 729 #endif 730 #ifndef OPENSSL_NO_TLS1_1 731 {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"}, 732 #endif 733 #ifndef OPENSSL_NO_TLS1_2 734 {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"}, 735 #endif 736 #ifndef OPENSSL_NO_TLS1_3 737 {"tls1_3", OPT_TLS1_3, '-', "Just use TLSv1.3"}, 738 #endif 739 #ifndef OPENSSL_NO_DTLS 740 {"dtls", OPT_DTLS, '-', "Use any version of DTLS"}, 741 {"timeout", OPT_TIMEOUT, '-', 742 "Enable send/receive timeout on DTLS connections"}, 743 {"mtu", OPT_MTU, 'p', "Set the link layer MTU"}, 744 #endif 745 #ifndef OPENSSL_NO_DTLS1 746 {"dtls1", OPT_DTLS1, '-', "Just use DTLSv1"}, 747 #endif 748 #ifndef OPENSSL_NO_DTLS1_2 749 {"dtls1_2", OPT_DTLS1_2, '-', "Just use DTLSv1.2"}, 750 #endif 751 #ifndef OPENSSL_NO_SCTP 752 {"sctp", OPT_SCTP, '-', "Use SCTP"}, 753 #endif 754 #ifndef OPENSSL_NO_SSL_TRACE 755 {"trace", OPT_TRACE, '-', "Show trace output of protocol messages"}, 756 #endif 757 #ifdef WATT32 758 {"wdebug", OPT_WDEBUG, '-', "WATT-32 tcp debugging"}, 759 #endif 760 {"nbio", OPT_NBIO, '-', "Use non-blocking IO"}, 761 {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity"}, 762 {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"}, 763 {"psk_session", OPT_PSK_SESS, '<', "File to read PSK SSL session from"}, 764 #ifndef OPENSSL_NO_SRP 765 {"srpuser", OPT_SRPUSER, 's', "SRP authentication for 'user'"}, 766 {"srppass", OPT_SRPPASS, 's', "Password for 'user'"}, 767 {"srp_lateuser", OPT_SRP_LATEUSER, '-', 768 "SRP username into second ClientHello message"}, 769 {"srp_moregroups", OPT_SRP_MOREGROUPS, '-', 770 "Tolerate other than the known g N values."}, 771 {"srp_strength", OPT_SRP_STRENGTH, 'p', "Minimal length in bits for N"}, 772 #endif 773 #ifndef OPENSSL_NO_NEXTPROTONEG 774 {"nextprotoneg", OPT_NEXTPROTONEG, 's', 775 "Enable NPN extension, considering named protocols supported (comma-separated list)"}, 776 #endif 777 #ifndef OPENSSL_NO_ENGINE 778 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, 779 {"ssl_client_engine", OPT_SSL_CLIENT_ENGINE, 's', 780 "Specify engine to be used for client certificate operations"}, 781 #endif 782 #ifndef OPENSSL_NO_CT 783 {"ct", OPT_CT, '-', "Request and parse SCTs (also enables OCSP stapling)"}, 784 {"noct", OPT_NOCT, '-', "Do not request or parse SCTs (default)"}, 785 {"ctlogfile", OPT_CTLOG_FILE, '<', "CT log list CONF file"}, 786 #endif 787 {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"}, 788 {"early_data", OPT_EARLY_DATA, '<', "File to send as early data"}, 789 {"enable_pha", OPT_ENABLE_PHA, '-', "Enable post-handshake-authentication"}, 790 {NULL, OPT_EOF, 0x00, NULL} 791 }; 792 793 typedef enum PROTOCOL_choice { 794 PROTO_OFF, 795 PROTO_SMTP, 796 PROTO_POP3, 797 PROTO_IMAP, 798 PROTO_FTP, 799 PROTO_TELNET, 800 PROTO_XMPP, 801 PROTO_XMPP_SERVER, 802 PROTO_CONNECT, 803 PROTO_IRC, 804 PROTO_MYSQL, 805 PROTO_POSTGRES, 806 PROTO_LMTP, 807 PROTO_NNTP, 808 PROTO_SIEVE, 809 PROTO_LDAP 810 } PROTOCOL_CHOICE; 811 812 static const OPT_PAIR services[] = { 813 {"smtp", PROTO_SMTP}, 814 {"pop3", PROTO_POP3}, 815 {"imap", PROTO_IMAP}, 816 {"ftp", PROTO_FTP}, 817 {"xmpp", PROTO_XMPP}, 818 {"xmpp-server", PROTO_XMPP_SERVER}, 819 {"telnet", PROTO_TELNET}, 820 {"irc", PROTO_IRC}, 821 {"mysql", PROTO_MYSQL}, 822 {"postgres", PROTO_POSTGRES}, 823 {"lmtp", PROTO_LMTP}, 824 {"nntp", PROTO_NNTP}, 825 {"sieve", PROTO_SIEVE}, 826 {"ldap", PROTO_LDAP}, 827 {NULL, 0} 828 }; 829 830 #define IS_INET_FLAG(o) \ 831 (o == OPT_4 || o == OPT_6 || o == OPT_HOST || o == OPT_PORT || o == OPT_CONNECT) 832 #define IS_UNIX_FLAG(o) (o == OPT_UNIX) 833 834 #define IS_PROT_FLAG(o) \ 835 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \ 836 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2) 837 838 /* Free |*dest| and optionally set it to a copy of |source|. */ 839 static void freeandcopy(char **dest, const char *source) 840 { 841 OPENSSL_free(*dest); 842 *dest = NULL; 843 if (source != NULL) 844 *dest = OPENSSL_strdup(source); 845 } 846 847 static int new_session_cb(SSL *s, SSL_SESSION *sess) 848 { 849 850 if (sess_out != NULL) { 851 BIO *stmp = BIO_new_file(sess_out, "w"); 852 853 if (stmp == NULL) { 854 BIO_printf(bio_err, "Error writing session file %s\n", sess_out); 855 } else { 856 PEM_write_bio_SSL_SESSION(stmp, sess); 857 BIO_free(stmp); 858 } 859 } 860 861 /* 862 * Session data gets dumped on connection for TLSv1.2 and below, and on 863 * arrival of the NewSessionTicket for TLSv1.3. 864 */ 865 if (SSL_version(s) == TLS1_3_VERSION) { 866 BIO_printf(bio_c_out, 867 "---\nPost-Handshake New Session Ticket arrived:\n"); 868 SSL_SESSION_print(bio_c_out, sess); 869 BIO_printf(bio_c_out, "---\n"); 870 } 871 872 /* 873 * We always return a "fail" response so that the session gets freed again 874 * because we haven't used the reference. 875 */ 876 return 0; 877 } 878 879 int s_client_main(int argc, char **argv) 880 { 881 BIO *sbio; 882 EVP_PKEY *key = NULL; 883 SSL *con = NULL; 884 SSL_CTX *ctx = NULL; 885 STACK_OF(X509) *chain = NULL; 886 X509 *cert = NULL; 887 X509_VERIFY_PARAM *vpm = NULL; 888 SSL_EXCERT *exc = NULL; 889 SSL_CONF_CTX *cctx = NULL; 890 STACK_OF(OPENSSL_STRING) *ssl_args = NULL; 891 char *dane_tlsa_domain = NULL; 892 STACK_OF(OPENSSL_STRING) *dane_tlsa_rrset = NULL; 893 int dane_ee_no_name = 0; 894 STACK_OF(X509_CRL) *crls = NULL; 895 const SSL_METHOD *meth = TLS_client_method(); 896 const char *CApath = NULL, *CAfile = NULL; 897 char *cbuf = NULL, *sbuf = NULL; 898 char *mbuf = NULL, *proxystr = NULL, *connectstr = NULL, *bindstr = NULL; 899 char *cert_file = NULL, *key_file = NULL, *chain_file = NULL; 900 char *chCApath = NULL, *chCAfile = NULL, *host = NULL; 901 char *port = OPENSSL_strdup(PORT); 902 char *bindhost = NULL, *bindport = NULL; 903 char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL; 904 char *ReqCAfile = NULL; 905 char *sess_in = NULL, *crl_file = NULL, *p; 906 const char *protohost = NULL; 907 struct timeval timeout, *timeoutp; 908 fd_set readfds, writefds; 909 int noCApath = 0, noCAfile = 0; 910 int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_PEM; 911 int key_format = FORMAT_PEM, crlf = 0, full_log = 1, mbuf_len = 0; 912 int prexit = 0; 913 int sdebug = 0; 914 int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0; 915 int ret = 1, in_init = 1, i, nbio_test = 0, s = -1, k, width, state = 0; 916 int sbuf_len, sbuf_off, cmdletters = 1; 917 int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0; 918 int starttls_proto = PROTO_OFF, crl_format = FORMAT_PEM, crl_download = 0; 919 int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending; 920 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) 921 int at_eof = 0; 922 #endif 923 int read_buf_len = 0; 924 int fallback_scsv = 0; 925 OPTION_CHOICE o; 926 #ifndef OPENSSL_NO_DTLS 927 int enable_timeouts = 0; 928 long socket_mtu = 0; 929 #endif 930 #ifndef OPENSSL_NO_ENGINE 931 ENGINE *ssl_client_engine = NULL; 932 #endif 933 ENGINE *e = NULL; 934 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) 935 struct timeval tv; 936 #endif 937 const char *servername = NULL; 938 int noservername = 0; 939 const char *alpn_in = NULL; 940 tlsextctx tlsextcbp = { NULL, 0 }; 941 const char *ssl_config = NULL; 942 #define MAX_SI_TYPES 100 943 unsigned short serverinfo_types[MAX_SI_TYPES]; 944 int serverinfo_count = 0, start = 0, len; 945 #ifndef OPENSSL_NO_NEXTPROTONEG 946 const char *next_proto_neg_in = NULL; 947 #endif 948 #ifndef OPENSSL_NO_SRP 949 char *srppass = NULL; 950 int srp_lateuser = 0; 951 SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 }; 952 #endif 953 #ifndef OPENSSL_NO_SRTP 954 char *srtp_profiles = NULL; 955 #endif 956 #ifndef OPENSSL_NO_CT 957 char *ctlog_file = NULL; 958 int ct_validation = 0; 959 #endif 960 int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0; 961 int async = 0; 962 unsigned int max_send_fragment = 0; 963 unsigned int split_send_fragment = 0, max_pipelines = 0; 964 enum { use_inet, use_unix, use_unknown } connect_type = use_unknown; 965 int count4or6 = 0; 966 uint8_t maxfraglen = 0; 967 int c_nbio = 0, c_msg = 0, c_ign_eof = 0, c_brief = 0; 968 int c_tlsextdebug = 0; 969 #ifndef OPENSSL_NO_OCSP 970 int c_status_req = 0; 971 #endif 972 BIO *bio_c_msg = NULL; 973 const char *keylog_file = NULL, *early_data_file = NULL; 974 #ifndef OPENSSL_NO_DTLS 975 int isdtls = 0; 976 #endif 977 char *psksessf = NULL; 978 int enable_pha = 0; 979 980 FD_ZERO(&readfds); 981 FD_ZERO(&writefds); 982 /* Known false-positive of MemorySanitizer. */ 983 #if defined(__has_feature) 984 # if __has_feature(memory_sanitizer) 985 __msan_unpoison(&readfds, sizeof(readfds)); 986 __msan_unpoison(&writefds, sizeof(writefds)); 987 # endif 988 #endif 989 990 prog = opt_progname(argv[0]); 991 c_quiet = 0; 992 c_debug = 0; 993 c_showcerts = 0; 994 c_nbio = 0; 995 vpm = X509_VERIFY_PARAM_new(); 996 cctx = SSL_CONF_CTX_new(); 997 998 if (vpm == NULL || cctx == NULL) { 999 BIO_printf(bio_err, "%s: out of memory\n", prog); 1000 goto end; 1001 } 1002 1003 cbuf = app_malloc(BUFSIZZ, "cbuf"); 1004 sbuf = app_malloc(BUFSIZZ, "sbuf"); 1005 mbuf = app_malloc(BUFSIZZ, "mbuf"); 1006 1007 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE); 1008 1009 prog = opt_init(argc, argv, s_client_options); 1010 while ((o = opt_next()) != OPT_EOF) { 1011 /* Check for intermixing flags. */ 1012 if (connect_type == use_unix && IS_INET_FLAG(o)) { 1013 BIO_printf(bio_err, 1014 "%s: Intermixed protocol flags (unix and internet domains)\n", 1015 prog); 1016 goto end; 1017 } 1018 if (connect_type == use_inet && IS_UNIX_FLAG(o)) { 1019 BIO_printf(bio_err, 1020 "%s: Intermixed protocol flags (internet and unix domains)\n", 1021 prog); 1022 goto end; 1023 } 1024 1025 if (IS_PROT_FLAG(o) && ++prot_opt > 1) { 1026 BIO_printf(bio_err, "Cannot supply multiple protocol flags\n"); 1027 goto end; 1028 } 1029 if (IS_NO_PROT_FLAG(o)) 1030 no_prot_opt++; 1031 if (prot_opt == 1 && no_prot_opt) { 1032 BIO_printf(bio_err, 1033 "Cannot supply both a protocol flag and '-no_<prot>'\n"); 1034 goto end; 1035 } 1036 1037 switch (o) { 1038 case OPT_EOF: 1039 case OPT_ERR: 1040 opthelp: 1041 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 1042 goto end; 1043 case OPT_HELP: 1044 opt_help(s_client_options); 1045 ret = 0; 1046 goto end; 1047 case OPT_4: 1048 connect_type = use_inet; 1049 socket_family = AF_INET; 1050 count4or6++; 1051 break; 1052 #ifdef AF_INET6 1053 case OPT_6: 1054 connect_type = use_inet; 1055 socket_family = AF_INET6; 1056 count4or6++; 1057 break; 1058 #endif 1059 case OPT_HOST: 1060 connect_type = use_inet; 1061 freeandcopy(&host, opt_arg()); 1062 break; 1063 case OPT_PORT: 1064 connect_type = use_inet; 1065 freeandcopy(&port, opt_arg()); 1066 break; 1067 case OPT_CONNECT: 1068 connect_type = use_inet; 1069 freeandcopy(&connectstr, opt_arg()); 1070 break; 1071 case OPT_BIND: 1072 freeandcopy(&bindstr, opt_arg()); 1073 break; 1074 case OPT_PROXY: 1075 proxystr = opt_arg(); 1076 starttls_proto = PROTO_CONNECT; 1077 break; 1078 #ifdef AF_UNIX 1079 case OPT_UNIX: 1080 connect_type = use_unix; 1081 socket_family = AF_UNIX; 1082 freeandcopy(&host, opt_arg()); 1083 break; 1084 #endif 1085 case OPT_XMPPHOST: 1086 /* fall through, since this is an alias */ 1087 case OPT_PROTOHOST: 1088 protohost = opt_arg(); 1089 break; 1090 case OPT_VERIFY: 1091 verify = SSL_VERIFY_PEER; 1092 verify_args.depth = atoi(opt_arg()); 1093 if (!c_quiet) 1094 BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth); 1095 break; 1096 case OPT_CERT: 1097 cert_file = opt_arg(); 1098 break; 1099 case OPT_NAMEOPT: 1100 if (!set_nameopt(opt_arg())) 1101 goto end; 1102 break; 1103 case OPT_CRL: 1104 crl_file = opt_arg(); 1105 break; 1106 case OPT_CRL_DOWNLOAD: 1107 crl_download = 1; 1108 break; 1109 case OPT_SESS_OUT: 1110 sess_out = opt_arg(); 1111 break; 1112 case OPT_SESS_IN: 1113 sess_in = opt_arg(); 1114 break; 1115 case OPT_CERTFORM: 1116 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &cert_format)) 1117 goto opthelp; 1118 break; 1119 case OPT_CRLFORM: 1120 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format)) 1121 goto opthelp; 1122 break; 1123 case OPT_VERIFY_RET_ERROR: 1124 verify_args.return_error = 1; 1125 break; 1126 case OPT_VERIFY_QUIET: 1127 verify_args.quiet = 1; 1128 break; 1129 case OPT_BRIEF: 1130 c_brief = verify_args.quiet = c_quiet = 1; 1131 break; 1132 case OPT_S_CASES: 1133 if (ssl_args == NULL) 1134 ssl_args = sk_OPENSSL_STRING_new_null(); 1135 if (ssl_args == NULL 1136 || !sk_OPENSSL_STRING_push(ssl_args, opt_flag()) 1137 || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) { 1138 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog); 1139 goto end; 1140 } 1141 break; 1142 case OPT_V_CASES: 1143 if (!opt_verify(o, vpm)) 1144 goto end; 1145 vpmtouched++; 1146 break; 1147 case OPT_X_CASES: 1148 if (!args_excert(o, &exc)) 1149 goto end; 1150 break; 1151 case OPT_PREXIT: 1152 prexit = 1; 1153 break; 1154 case OPT_CRLF: 1155 crlf = 1; 1156 break; 1157 case OPT_QUIET: 1158 c_quiet = c_ign_eof = 1; 1159 break; 1160 case OPT_NBIO: 1161 c_nbio = 1; 1162 break; 1163 case OPT_NOCMDS: 1164 cmdletters = 0; 1165 break; 1166 case OPT_ENGINE: 1167 e = setup_engine(opt_arg(), 1); 1168 break; 1169 case OPT_SSL_CLIENT_ENGINE: 1170 #ifndef OPENSSL_NO_ENGINE 1171 ssl_client_engine = ENGINE_by_id(opt_arg()); 1172 if (ssl_client_engine == NULL) { 1173 BIO_printf(bio_err, "Error getting client auth engine\n"); 1174 goto opthelp; 1175 } 1176 #endif 1177 break; 1178 case OPT_R_CASES: 1179 if (!opt_rand(o)) 1180 goto end; 1181 break; 1182 case OPT_IGN_EOF: 1183 c_ign_eof = 1; 1184 break; 1185 case OPT_NO_IGN_EOF: 1186 c_ign_eof = 0; 1187 break; 1188 case OPT_DEBUG: 1189 c_debug = 1; 1190 break; 1191 case OPT_TLSEXTDEBUG: 1192 c_tlsextdebug = 1; 1193 break; 1194 case OPT_STATUS: 1195 #ifndef OPENSSL_NO_OCSP 1196 c_status_req = 1; 1197 #endif 1198 break; 1199 case OPT_WDEBUG: 1200 #ifdef WATT32 1201 dbug_init(); 1202 #endif 1203 break; 1204 case OPT_MSG: 1205 c_msg = 1; 1206 break; 1207 case OPT_MSGFILE: 1208 bio_c_msg = BIO_new_file(opt_arg(), "w"); 1209 break; 1210 case OPT_TRACE: 1211 #ifndef OPENSSL_NO_SSL_TRACE 1212 c_msg = 2; 1213 #endif 1214 break; 1215 case OPT_SECURITY_DEBUG: 1216 sdebug = 1; 1217 break; 1218 case OPT_SECURITY_DEBUG_VERBOSE: 1219 sdebug = 2; 1220 break; 1221 case OPT_SHOWCERTS: 1222 c_showcerts = 1; 1223 break; 1224 case OPT_NBIO_TEST: 1225 nbio_test = 1; 1226 break; 1227 case OPT_STATE: 1228 state = 1; 1229 break; 1230 case OPT_PSK_IDENTITY: 1231 psk_identity = opt_arg(); 1232 break; 1233 case OPT_PSK: 1234 for (p = psk_key = opt_arg(); *p; p++) { 1235 if (isxdigit(_UC(*p))) 1236 continue; 1237 BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key); 1238 goto end; 1239 } 1240 break; 1241 case OPT_PSK_SESS: 1242 psksessf = opt_arg(); 1243 break; 1244 #ifndef OPENSSL_NO_SRP 1245 case OPT_SRPUSER: 1246 srp_arg.srplogin = opt_arg(); 1247 if (min_version < TLS1_VERSION) 1248 min_version = TLS1_VERSION; 1249 break; 1250 case OPT_SRPPASS: 1251 srppass = opt_arg(); 1252 if (min_version < TLS1_VERSION) 1253 min_version = TLS1_VERSION; 1254 break; 1255 case OPT_SRP_STRENGTH: 1256 srp_arg.strength = atoi(opt_arg()); 1257 BIO_printf(bio_err, "SRP minimal length for N is %d\n", 1258 srp_arg.strength); 1259 if (min_version < TLS1_VERSION) 1260 min_version = TLS1_VERSION; 1261 break; 1262 case OPT_SRP_LATEUSER: 1263 srp_lateuser = 1; 1264 if (min_version < TLS1_VERSION) 1265 min_version = TLS1_VERSION; 1266 break; 1267 case OPT_SRP_MOREGROUPS: 1268 srp_arg.amp = 1; 1269 if (min_version < TLS1_VERSION) 1270 min_version = TLS1_VERSION; 1271 break; 1272 #endif 1273 case OPT_SSL_CONFIG: 1274 ssl_config = opt_arg(); 1275 break; 1276 case OPT_SSL3: 1277 min_version = SSL3_VERSION; 1278 max_version = SSL3_VERSION; 1279 break; 1280 case OPT_TLS1_3: 1281 min_version = TLS1_3_VERSION; 1282 max_version = TLS1_3_VERSION; 1283 break; 1284 case OPT_TLS1_2: 1285 min_version = TLS1_2_VERSION; 1286 max_version = TLS1_2_VERSION; 1287 break; 1288 case OPT_TLS1_1: 1289 min_version = TLS1_1_VERSION; 1290 max_version = TLS1_1_VERSION; 1291 break; 1292 case OPT_TLS1: 1293 min_version = TLS1_VERSION; 1294 max_version = TLS1_VERSION; 1295 break; 1296 case OPT_DTLS: 1297 #ifndef OPENSSL_NO_DTLS 1298 meth = DTLS_client_method(); 1299 socket_type = SOCK_DGRAM; 1300 isdtls = 1; 1301 #endif 1302 break; 1303 case OPT_DTLS1: 1304 #ifndef OPENSSL_NO_DTLS1 1305 meth = DTLS_client_method(); 1306 min_version = DTLS1_VERSION; 1307 max_version = DTLS1_VERSION; 1308 socket_type = SOCK_DGRAM; 1309 isdtls = 1; 1310 #endif 1311 break; 1312 case OPT_DTLS1_2: 1313 #ifndef OPENSSL_NO_DTLS1_2 1314 meth = DTLS_client_method(); 1315 min_version = DTLS1_2_VERSION; 1316 max_version = DTLS1_2_VERSION; 1317 socket_type = SOCK_DGRAM; 1318 isdtls = 1; 1319 #endif 1320 break; 1321 case OPT_SCTP: 1322 #ifndef OPENSSL_NO_SCTP 1323 protocol = IPPROTO_SCTP; 1324 #endif 1325 break; 1326 case OPT_TIMEOUT: 1327 #ifndef OPENSSL_NO_DTLS 1328 enable_timeouts = 1; 1329 #endif 1330 break; 1331 case OPT_MTU: 1332 #ifndef OPENSSL_NO_DTLS 1333 socket_mtu = atol(opt_arg()); 1334 #endif 1335 break; 1336 case OPT_FALLBACKSCSV: 1337 fallback_scsv = 1; 1338 break; 1339 case OPT_KEYFORM: 1340 if (!opt_format(opt_arg(), OPT_FMT_PDE, &key_format)) 1341 goto opthelp; 1342 break; 1343 case OPT_PASS: 1344 passarg = opt_arg(); 1345 break; 1346 case OPT_CERT_CHAIN: 1347 chain_file = opt_arg(); 1348 break; 1349 case OPT_KEY: 1350 key_file = opt_arg(); 1351 break; 1352 case OPT_RECONNECT: 1353 reconnect = 5; 1354 break; 1355 case OPT_CAPATH: 1356 CApath = opt_arg(); 1357 break; 1358 case OPT_NOCAPATH: 1359 noCApath = 1; 1360 break; 1361 case OPT_CHAINCAPATH: 1362 chCApath = opt_arg(); 1363 break; 1364 case OPT_VERIFYCAPATH: 1365 vfyCApath = opt_arg(); 1366 break; 1367 case OPT_BUILD_CHAIN: 1368 build_chain = 1; 1369 break; 1370 case OPT_REQCAFILE: 1371 ReqCAfile = opt_arg(); 1372 break; 1373 case OPT_CAFILE: 1374 CAfile = opt_arg(); 1375 break; 1376 case OPT_NOCAFILE: 1377 noCAfile = 1; 1378 break; 1379 #ifndef OPENSSL_NO_CT 1380 case OPT_NOCT: 1381 ct_validation = 0; 1382 break; 1383 case OPT_CT: 1384 ct_validation = 1; 1385 break; 1386 case OPT_CTLOG_FILE: 1387 ctlog_file = opt_arg(); 1388 break; 1389 #endif 1390 case OPT_CHAINCAFILE: 1391 chCAfile = opt_arg(); 1392 break; 1393 case OPT_VERIFYCAFILE: 1394 vfyCAfile = opt_arg(); 1395 break; 1396 case OPT_DANE_TLSA_DOMAIN: 1397 dane_tlsa_domain = opt_arg(); 1398 break; 1399 case OPT_DANE_TLSA_RRDATA: 1400 if (dane_tlsa_rrset == NULL) 1401 dane_tlsa_rrset = sk_OPENSSL_STRING_new_null(); 1402 if (dane_tlsa_rrset == NULL || 1403 !sk_OPENSSL_STRING_push(dane_tlsa_rrset, opt_arg())) { 1404 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog); 1405 goto end; 1406 } 1407 break; 1408 case OPT_DANE_EE_NO_NAME: 1409 dane_ee_no_name = 1; 1410 break; 1411 case OPT_NEXTPROTONEG: 1412 #ifndef OPENSSL_NO_NEXTPROTONEG 1413 next_proto_neg_in = opt_arg(); 1414 #endif 1415 break; 1416 case OPT_ALPN: 1417 alpn_in = opt_arg(); 1418 break; 1419 case OPT_SERVERINFO: 1420 p = opt_arg(); 1421 len = strlen(p); 1422 for (start = 0, i = 0; i <= len; ++i) { 1423 if (i == len || p[i] == ',') { 1424 serverinfo_types[serverinfo_count] = atoi(p + start); 1425 if (++serverinfo_count == MAX_SI_TYPES) 1426 break; 1427 start = i + 1; 1428 } 1429 } 1430 break; 1431 case OPT_STARTTLS: 1432 if (!opt_pair(opt_arg(), services, &starttls_proto)) 1433 goto end; 1434 break; 1435 case OPT_SERVERNAME: 1436 servername = opt_arg(); 1437 break; 1438 case OPT_NOSERVERNAME: 1439 noservername = 1; 1440 break; 1441 case OPT_USE_SRTP: 1442 #ifndef OPENSSL_NO_SRTP 1443 srtp_profiles = opt_arg(); 1444 #endif 1445 break; 1446 case OPT_KEYMATEXPORT: 1447 keymatexportlabel = opt_arg(); 1448 break; 1449 case OPT_KEYMATEXPORTLEN: 1450 keymatexportlen = atoi(opt_arg()); 1451 break; 1452 case OPT_ASYNC: 1453 async = 1; 1454 break; 1455 case OPT_MAXFRAGLEN: 1456 len = atoi(opt_arg()); 1457 switch (len) { 1458 case 512: 1459 maxfraglen = TLSEXT_max_fragment_length_512; 1460 break; 1461 case 1024: 1462 maxfraglen = TLSEXT_max_fragment_length_1024; 1463 break; 1464 case 2048: 1465 maxfraglen = TLSEXT_max_fragment_length_2048; 1466 break; 1467 case 4096: 1468 maxfraglen = TLSEXT_max_fragment_length_4096; 1469 break; 1470 default: 1471 BIO_printf(bio_err, 1472 "%s: Max Fragment Len %u is out of permitted values", 1473 prog, len); 1474 goto opthelp; 1475 } 1476 break; 1477 case OPT_MAX_SEND_FRAG: 1478 max_send_fragment = atoi(opt_arg()); 1479 break; 1480 case OPT_SPLIT_SEND_FRAG: 1481 split_send_fragment = atoi(opt_arg()); 1482 break; 1483 case OPT_MAX_PIPELINES: 1484 max_pipelines = atoi(opt_arg()); 1485 break; 1486 case OPT_READ_BUF: 1487 read_buf_len = atoi(opt_arg()); 1488 break; 1489 case OPT_KEYLOG_FILE: 1490 keylog_file = opt_arg(); 1491 break; 1492 case OPT_EARLY_DATA: 1493 early_data_file = opt_arg(); 1494 break; 1495 case OPT_ENABLE_PHA: 1496 enable_pha = 1; 1497 break; 1498 } 1499 } 1500 if (count4or6 >= 2) { 1501 BIO_printf(bio_err, "%s: Can't use both -4 and -6\n", prog); 1502 goto opthelp; 1503 } 1504 if (noservername) { 1505 if (servername != NULL) { 1506 BIO_printf(bio_err, 1507 "%s: Can't use -servername and -noservername together\n", 1508 prog); 1509 goto opthelp; 1510 } 1511 if (dane_tlsa_domain != NULL) { 1512 BIO_printf(bio_err, 1513 "%s: Can't use -dane_tlsa_domain and -noservername together\n", 1514 prog); 1515 goto opthelp; 1516 } 1517 } 1518 argc = opt_num_rest(); 1519 if (argc == 1) { 1520 /* If there's a positional argument, it's the equivalent of 1521 * OPT_CONNECT. 1522 * Don't allow -connect and a separate argument. 1523 */ 1524 if (connectstr != NULL) { 1525 BIO_printf(bio_err, 1526 "%s: must not provide both -connect option and target parameter\n", 1527 prog); 1528 goto opthelp; 1529 } 1530 connect_type = use_inet; 1531 freeandcopy(&connectstr, *opt_rest()); 1532 } else if (argc != 0) { 1533 goto opthelp; 1534 } 1535 1536 #ifndef OPENSSL_NO_NEXTPROTONEG 1537 if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) { 1538 BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n"); 1539 goto opthelp; 1540 } 1541 #endif 1542 if (proxystr != NULL) { 1543 int res; 1544 char *tmp_host = host, *tmp_port = port; 1545 if (connectstr == NULL) { 1546 BIO_printf(bio_err, "%s: -proxy requires use of -connect or target parameter\n", prog); 1547 goto opthelp; 1548 } 1549 res = BIO_parse_hostserv(proxystr, &host, &port, BIO_PARSE_PRIO_HOST); 1550 if (tmp_host != host) 1551 OPENSSL_free(tmp_host); 1552 if (tmp_port != port) 1553 OPENSSL_free(tmp_port); 1554 if (!res) { 1555 BIO_printf(bio_err, 1556 "%s: -proxy argument malformed or ambiguous\n", prog); 1557 goto end; 1558 } 1559 } else { 1560 int res = 1; 1561 char *tmp_host = host, *tmp_port = port; 1562 if (connectstr != NULL) 1563 res = BIO_parse_hostserv(connectstr, &host, &port, 1564 BIO_PARSE_PRIO_HOST); 1565 if (tmp_host != host) 1566 OPENSSL_free(tmp_host); 1567 if (tmp_port != port) 1568 OPENSSL_free(tmp_port); 1569 if (!res) { 1570 BIO_printf(bio_err, 1571 "%s: -connect argument or target parameter malformed or ambiguous\n", 1572 prog); 1573 goto end; 1574 } 1575 } 1576 1577 if (bindstr != NULL) { 1578 int res; 1579 res = BIO_parse_hostserv(bindstr, &bindhost, &bindport, 1580 BIO_PARSE_PRIO_HOST); 1581 if (!res) { 1582 BIO_printf(bio_err, 1583 "%s: -bind argument parameter malformed or ambiguous\n", 1584 prog); 1585 goto end; 1586 } 1587 } 1588 1589 #ifdef AF_UNIX 1590 if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) { 1591 BIO_printf(bio_err, 1592 "Can't use unix sockets and datagrams together\n"); 1593 goto end; 1594 } 1595 #endif 1596 1597 #ifndef OPENSSL_NO_SCTP 1598 if (protocol == IPPROTO_SCTP) { 1599 if (socket_type != SOCK_DGRAM) { 1600 BIO_printf(bio_err, "Can't use -sctp without DTLS\n"); 1601 goto end; 1602 } 1603 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */ 1604 socket_type = SOCK_STREAM; 1605 } 1606 #endif 1607 1608 #if !defined(OPENSSL_NO_NEXTPROTONEG) 1609 next_proto.status = -1; 1610 if (next_proto_neg_in) { 1611 next_proto.data = 1612 next_protos_parse(&next_proto.len, next_proto_neg_in); 1613 if (next_proto.data == NULL) { 1614 BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n"); 1615 goto end; 1616 } 1617 } else 1618 next_proto.data = NULL; 1619 #endif 1620 1621 if (!app_passwd(passarg, NULL, &pass, NULL)) { 1622 BIO_printf(bio_err, "Error getting password\n"); 1623 goto end; 1624 } 1625 1626 if (key_file == NULL) 1627 key_file = cert_file; 1628 1629 if (key_file != NULL) { 1630 key = load_key(key_file, key_format, 0, pass, e, 1631 "client certificate private key file"); 1632 if (key == NULL) { 1633 ERR_print_errors(bio_err); 1634 goto end; 1635 } 1636 } 1637 1638 if (cert_file != NULL) { 1639 cert = load_cert(cert_file, cert_format, "client certificate file"); 1640 if (cert == NULL) { 1641 ERR_print_errors(bio_err); 1642 goto end; 1643 } 1644 } 1645 1646 if (chain_file != NULL) { 1647 if (!load_certs(chain_file, &chain, FORMAT_PEM, NULL, 1648 "client certificate chain")) 1649 goto end; 1650 } 1651 1652 if (crl_file != NULL) { 1653 X509_CRL *crl; 1654 crl = load_crl(crl_file, crl_format); 1655 if (crl == NULL) { 1656 BIO_puts(bio_err, "Error loading CRL\n"); 1657 ERR_print_errors(bio_err); 1658 goto end; 1659 } 1660 crls = sk_X509_CRL_new_null(); 1661 if (crls == NULL || !sk_X509_CRL_push(crls, crl)) { 1662 BIO_puts(bio_err, "Error adding CRL\n"); 1663 ERR_print_errors(bio_err); 1664 X509_CRL_free(crl); 1665 goto end; 1666 } 1667 } 1668 1669 if (!load_excert(&exc)) 1670 goto end; 1671 1672 if (bio_c_out == NULL) { 1673 if (c_quiet && !c_debug) { 1674 bio_c_out = BIO_new(BIO_s_null()); 1675 if (c_msg && bio_c_msg == NULL) 1676 bio_c_msg = dup_bio_out(FORMAT_TEXT); 1677 } else if (bio_c_out == NULL) 1678 bio_c_out = dup_bio_out(FORMAT_TEXT); 1679 } 1680 #ifndef OPENSSL_NO_SRP 1681 if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) { 1682 BIO_printf(bio_err, "Error getting password\n"); 1683 goto end; 1684 } 1685 #endif 1686 1687 ctx = SSL_CTX_new(meth); 1688 if (ctx == NULL) { 1689 ERR_print_errors(bio_err); 1690 goto end; 1691 } 1692 1693 SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY); 1694 1695 if (sdebug) 1696 ssl_ctx_security_debug(ctx, sdebug); 1697 1698 if (!config_ctx(cctx, ssl_args, ctx)) 1699 goto end; 1700 1701 if (ssl_config != NULL) { 1702 if (SSL_CTX_config(ctx, ssl_config) == 0) { 1703 BIO_printf(bio_err, "Error using configuration \"%s\"\n", 1704 ssl_config); 1705 ERR_print_errors(bio_err); 1706 goto end; 1707 } 1708 } 1709 1710 if (min_version != 0 1711 && SSL_CTX_set_min_proto_version(ctx, min_version) == 0) 1712 goto end; 1713 if (max_version != 0 1714 && SSL_CTX_set_max_proto_version(ctx, max_version) == 0) 1715 goto end; 1716 1717 if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) { 1718 BIO_printf(bio_err, "Error setting verify params\n"); 1719 ERR_print_errors(bio_err); 1720 goto end; 1721 } 1722 1723 if (async) { 1724 SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC); 1725 } 1726 1727 if (max_send_fragment > 0 1728 && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) { 1729 BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n", 1730 prog, max_send_fragment); 1731 goto end; 1732 } 1733 1734 if (split_send_fragment > 0 1735 && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) { 1736 BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n", 1737 prog, split_send_fragment); 1738 goto end; 1739 } 1740 1741 if (max_pipelines > 0 1742 && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) { 1743 BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n", 1744 prog, max_pipelines); 1745 goto end; 1746 } 1747 1748 if (read_buf_len > 0) { 1749 SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len); 1750 } 1751 1752 if (maxfraglen > 0 1753 && !SSL_CTX_set_tlsext_max_fragment_length(ctx, maxfraglen)) { 1754 BIO_printf(bio_err, 1755 "%s: Max Fragment Length code %u is out of permitted values" 1756 "\n", prog, maxfraglen); 1757 goto end; 1758 } 1759 1760 if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile, 1761 crls, crl_download)) { 1762 BIO_printf(bio_err, "Error loading store locations\n"); 1763 ERR_print_errors(bio_err); 1764 goto end; 1765 } 1766 if (ReqCAfile != NULL) { 1767 STACK_OF(X509_NAME) *nm = sk_X509_NAME_new_null(); 1768 1769 if (nm == NULL || !SSL_add_file_cert_subjects_to_stack(nm, ReqCAfile)) { 1770 sk_X509_NAME_pop_free(nm, X509_NAME_free); 1771 BIO_printf(bio_err, "Error loading CA names\n"); 1772 ERR_print_errors(bio_err); 1773 goto end; 1774 } 1775 SSL_CTX_set0_CA_list(ctx, nm); 1776 } 1777 #ifndef OPENSSL_NO_ENGINE 1778 if (ssl_client_engine) { 1779 if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) { 1780 BIO_puts(bio_err, "Error setting client auth engine\n"); 1781 ERR_print_errors(bio_err); 1782 ENGINE_free(ssl_client_engine); 1783 goto end; 1784 } 1785 ENGINE_free(ssl_client_engine); 1786 } 1787 #endif 1788 1789 #ifndef OPENSSL_NO_PSK 1790 if (psk_key != NULL) { 1791 if (c_debug) 1792 BIO_printf(bio_c_out, "PSK key given, setting client callback\n"); 1793 SSL_CTX_set_psk_client_callback(ctx, psk_client_cb); 1794 } 1795 #endif 1796 if (psksessf != NULL) { 1797 BIO *stmp = BIO_new_file(psksessf, "r"); 1798 1799 if (stmp == NULL) { 1800 BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf); 1801 ERR_print_errors(bio_err); 1802 goto end; 1803 } 1804 psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL); 1805 BIO_free(stmp); 1806 if (psksess == NULL) { 1807 BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf); 1808 ERR_print_errors(bio_err); 1809 goto end; 1810 } 1811 } 1812 if (psk_key != NULL || psksess != NULL) 1813 SSL_CTX_set_psk_use_session_callback(ctx, psk_use_session_cb); 1814 1815 #ifndef OPENSSL_NO_SRTP 1816 if (srtp_profiles != NULL) { 1817 /* Returns 0 on success! */ 1818 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) { 1819 BIO_printf(bio_err, "Error setting SRTP profile\n"); 1820 ERR_print_errors(bio_err); 1821 goto end; 1822 } 1823 } 1824 #endif 1825 1826 if (exc != NULL) 1827 ssl_ctx_set_excert(ctx, exc); 1828 1829 #if !defined(OPENSSL_NO_NEXTPROTONEG) 1830 if (next_proto.data != NULL) 1831 SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto); 1832 #endif 1833 if (alpn_in) { 1834 size_t alpn_len; 1835 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in); 1836 1837 if (alpn == NULL) { 1838 BIO_printf(bio_err, "Error parsing -alpn argument\n"); 1839 goto end; 1840 } 1841 /* Returns 0 on success! */ 1842 if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) { 1843 BIO_printf(bio_err, "Error setting ALPN\n"); 1844 goto end; 1845 } 1846 OPENSSL_free(alpn); 1847 } 1848 1849 for (i = 0; i < serverinfo_count; i++) { 1850 if (!SSL_CTX_add_client_custom_ext(ctx, 1851 serverinfo_types[i], 1852 NULL, NULL, NULL, 1853 serverinfo_cli_parse_cb, NULL)) { 1854 BIO_printf(bio_err, 1855 "Warning: Unable to add custom extension %u, skipping\n", 1856 serverinfo_types[i]); 1857 } 1858 } 1859 1860 if (state) 1861 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback); 1862 1863 #ifndef OPENSSL_NO_CT 1864 /* Enable SCT processing, without early connection termination */ 1865 if (ct_validation && 1866 !SSL_CTX_enable_ct(ctx, SSL_CT_VALIDATION_PERMISSIVE)) { 1867 ERR_print_errors(bio_err); 1868 goto end; 1869 } 1870 1871 if (!ctx_set_ctlog_list_file(ctx, ctlog_file)) { 1872 if (ct_validation) { 1873 ERR_print_errors(bio_err); 1874 goto end; 1875 } 1876 1877 /* 1878 * If CT validation is not enabled, the log list isn't needed so don't 1879 * show errors or abort. We try to load it regardless because then we 1880 * can show the names of the logs any SCTs came from (SCTs may be seen 1881 * even with validation disabled). 1882 */ 1883 ERR_clear_error(); 1884 } 1885 #endif 1886 1887 SSL_CTX_set_verify(ctx, verify, verify_callback); 1888 1889 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) { 1890 ERR_print_errors(bio_err); 1891 goto end; 1892 } 1893 1894 ssl_ctx_add_crls(ctx, crls, crl_download); 1895 1896 if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain)) 1897 goto end; 1898 1899 if (!noservername) { 1900 tlsextcbp.biodebug = bio_err; 1901 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); 1902 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp); 1903 } 1904 # ifndef OPENSSL_NO_SRP 1905 if (srp_arg.srplogin) { 1906 if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) { 1907 BIO_printf(bio_err, "Unable to set SRP username\n"); 1908 goto end; 1909 } 1910 srp_arg.msg = c_msg; 1911 srp_arg.debug = c_debug; 1912 SSL_CTX_set_srp_cb_arg(ctx, &srp_arg); 1913 SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb); 1914 SSL_CTX_set_srp_strength(ctx, srp_arg.strength); 1915 if (c_msg || c_debug || srp_arg.amp == 0) 1916 SSL_CTX_set_srp_verify_param_callback(ctx, 1917 ssl_srp_verify_param_cb); 1918 } 1919 # endif 1920 1921 if (dane_tlsa_domain != NULL) { 1922 if (SSL_CTX_dane_enable(ctx) <= 0) { 1923 BIO_printf(bio_err, 1924 "%s: Error enabling DANE TLSA authentication.\n", 1925 prog); 1926 ERR_print_errors(bio_err); 1927 goto end; 1928 } 1929 } 1930 1931 /* 1932 * In TLSv1.3 NewSessionTicket messages arrive after the handshake and can 1933 * come at any time. Therefore we use a callback to write out the session 1934 * when we know about it. This approach works for < TLSv1.3 as well. 1935 */ 1936 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT 1937 | SSL_SESS_CACHE_NO_INTERNAL_STORE); 1938 SSL_CTX_sess_set_new_cb(ctx, new_session_cb); 1939 1940 if (set_keylog_file(ctx, keylog_file)) 1941 goto end; 1942 1943 con = SSL_new(ctx); 1944 if (con == NULL) 1945 goto end; 1946 1947 if (enable_pha) 1948 SSL_set_post_handshake_auth(con, 1); 1949 1950 if (sess_in != NULL) { 1951 SSL_SESSION *sess; 1952 BIO *stmp = BIO_new_file(sess_in, "r"); 1953 if (stmp == NULL) { 1954 BIO_printf(bio_err, "Can't open session file %s\n", sess_in); 1955 ERR_print_errors(bio_err); 1956 goto end; 1957 } 1958 sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL); 1959 BIO_free(stmp); 1960 if (sess == NULL) { 1961 BIO_printf(bio_err, "Can't open session file %s\n", sess_in); 1962 ERR_print_errors(bio_err); 1963 goto end; 1964 } 1965 if (!SSL_set_session(con, sess)) { 1966 BIO_printf(bio_err, "Can't set session\n"); 1967 ERR_print_errors(bio_err); 1968 goto end; 1969 } 1970 1971 SSL_SESSION_free(sess); 1972 } 1973 1974 if (fallback_scsv) 1975 SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV); 1976 1977 if (!noservername && (servername != NULL || dane_tlsa_domain == NULL)) { 1978 if (servername == NULL) 1979 servername = (host == NULL) ? "localhost" : host; 1980 if (!SSL_set_tlsext_host_name(con, servername)) { 1981 BIO_printf(bio_err, "Unable to set TLS servername extension.\n"); 1982 ERR_print_errors(bio_err); 1983 goto end; 1984 } 1985 } 1986 1987 if (dane_tlsa_domain != NULL) { 1988 if (SSL_dane_enable(con, dane_tlsa_domain) <= 0) { 1989 BIO_printf(bio_err, "%s: Error enabling DANE TLSA " 1990 "authentication.\n", prog); 1991 ERR_print_errors(bio_err); 1992 goto end; 1993 } 1994 if (dane_tlsa_rrset == NULL) { 1995 BIO_printf(bio_err, "%s: DANE TLSA authentication requires at " 1996 "least one -dane_tlsa_rrdata option.\n", prog); 1997 goto end; 1998 } 1999 if (tlsa_import_rrset(con, dane_tlsa_rrset) <= 0) { 2000 BIO_printf(bio_err, "%s: Failed to import any TLSA " 2001 "records.\n", prog); 2002 goto end; 2003 } 2004 if (dane_ee_no_name) 2005 SSL_dane_set_flags(con, DANE_FLAG_NO_DANE_EE_NAMECHECKS); 2006 } else if (dane_tlsa_rrset != NULL) { 2007 BIO_printf(bio_err, "%s: DANE TLSA authentication requires the " 2008 "-dane_tlsa_domain option.\n", prog); 2009 goto end; 2010 } 2011 2012 re_start: 2013 if (init_client(&s, host, port, bindhost, bindport, socket_family, 2014 socket_type, protocol) == 0) { 2015 BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error()); 2016 BIO_closesocket(s); 2017 goto end; 2018 } 2019 BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s); 2020 2021 if (c_nbio) { 2022 if (!BIO_socket_nbio(s, 1)) { 2023 ERR_print_errors(bio_err); 2024 goto end; 2025 } 2026 BIO_printf(bio_c_out, "Turned on non blocking io\n"); 2027 } 2028 #ifndef OPENSSL_NO_DTLS 2029 if (isdtls) { 2030 union BIO_sock_info_u peer_info; 2031 2032 #ifndef OPENSSL_NO_SCTP 2033 if (protocol == IPPROTO_SCTP) 2034 sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE); 2035 else 2036 #endif 2037 sbio = BIO_new_dgram(s, BIO_NOCLOSE); 2038 2039 if ((peer_info.addr = BIO_ADDR_new()) == NULL) { 2040 BIO_printf(bio_err, "memory allocation failure\n"); 2041 BIO_closesocket(s); 2042 goto end; 2043 } 2044 if (!BIO_sock_info(s, BIO_SOCK_INFO_ADDRESS, &peer_info)) { 2045 BIO_printf(bio_err, "getsockname:errno=%d\n", 2046 get_last_socket_error()); 2047 BIO_ADDR_free(peer_info.addr); 2048 BIO_closesocket(s); 2049 goto end; 2050 } 2051 2052 (void)BIO_ctrl_set_connected(sbio, peer_info.addr); 2053 BIO_ADDR_free(peer_info.addr); 2054 peer_info.addr = NULL; 2055 2056 if (enable_timeouts) { 2057 timeout.tv_sec = 0; 2058 timeout.tv_usec = DGRAM_RCV_TIMEOUT; 2059 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); 2060 2061 timeout.tv_sec = 0; 2062 timeout.tv_usec = DGRAM_SND_TIMEOUT; 2063 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); 2064 } 2065 2066 if (socket_mtu) { 2067 if (socket_mtu < DTLS_get_link_min_mtu(con)) { 2068 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n", 2069 DTLS_get_link_min_mtu(con)); 2070 BIO_free(sbio); 2071 goto shut; 2072 } 2073 SSL_set_options(con, SSL_OP_NO_QUERY_MTU); 2074 if (!DTLS_set_link_mtu(con, socket_mtu)) { 2075 BIO_printf(bio_err, "Failed to set MTU\n"); 2076 BIO_free(sbio); 2077 goto shut; 2078 } 2079 } else { 2080 /* want to do MTU discovery */ 2081 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); 2082 } 2083 } else 2084 #endif /* OPENSSL_NO_DTLS */ 2085 sbio = BIO_new_socket(s, BIO_NOCLOSE); 2086 2087 if (nbio_test) { 2088 BIO *test; 2089 2090 test = BIO_new(BIO_f_nbio_test()); 2091 sbio = BIO_push(test, sbio); 2092 } 2093 2094 if (c_debug) { 2095 BIO_set_callback(sbio, bio_dump_callback); 2096 BIO_set_callback_arg(sbio, (char *)bio_c_out); 2097 } 2098 if (c_msg) { 2099 #ifndef OPENSSL_NO_SSL_TRACE 2100 if (c_msg == 2) 2101 SSL_set_msg_callback(con, SSL_trace); 2102 else 2103 #endif 2104 SSL_set_msg_callback(con, msg_cb); 2105 SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out); 2106 } 2107 2108 if (c_tlsextdebug) { 2109 SSL_set_tlsext_debug_callback(con, tlsext_cb); 2110 SSL_set_tlsext_debug_arg(con, bio_c_out); 2111 } 2112 #ifndef OPENSSL_NO_OCSP 2113 if (c_status_req) { 2114 SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp); 2115 SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb); 2116 SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out); 2117 } 2118 #endif 2119 2120 SSL_set_bio(con, sbio, sbio); 2121 SSL_set_connect_state(con); 2122 2123 /* ok, lets connect */ 2124 if (fileno_stdin() > SSL_get_fd(con)) 2125 width = fileno_stdin() + 1; 2126 else 2127 width = SSL_get_fd(con) + 1; 2128 2129 read_tty = 1; 2130 write_tty = 0; 2131 tty_on = 0; 2132 read_ssl = 1; 2133 write_ssl = 1; 2134 2135 cbuf_len = 0; 2136 cbuf_off = 0; 2137 sbuf_len = 0; 2138 sbuf_off = 0; 2139 2140 switch ((PROTOCOL_CHOICE) starttls_proto) { 2141 case PROTO_OFF: 2142 break; 2143 case PROTO_LMTP: 2144 case PROTO_SMTP: 2145 { 2146 /* 2147 * This is an ugly hack that does a lot of assumptions. We do 2148 * have to handle multi-line responses which may come in a single 2149 * packet or not. We therefore have to use BIO_gets() which does 2150 * need a buffering BIO. So during the initial chitchat we do 2151 * push a buffering BIO into the chain that is removed again 2152 * later on to not disturb the rest of the s_client operation. 2153 */ 2154 int foundit = 0; 2155 BIO *fbio = BIO_new(BIO_f_buffer()); 2156 2157 BIO_push(fbio, sbio); 2158 /* Wait for multi-line response to end from LMTP or SMTP */ 2159 do { 2160 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2161 } while (mbuf_len > 3 && mbuf[3] == '-'); 2162 if (protohost == NULL) 2163 protohost = "mail.example.com"; 2164 if (starttls_proto == (int)PROTO_LMTP) 2165 BIO_printf(fbio, "LHLO %s\r\n", protohost); 2166 else 2167 BIO_printf(fbio, "EHLO %s\r\n", protohost); 2168 (void)BIO_flush(fbio); 2169 /* 2170 * Wait for multi-line response to end LHLO LMTP or EHLO SMTP 2171 * response. 2172 */ 2173 do { 2174 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2175 if (strstr(mbuf, "STARTTLS")) 2176 foundit = 1; 2177 } while (mbuf_len > 3 && mbuf[3] == '-'); 2178 (void)BIO_flush(fbio); 2179 BIO_pop(fbio); 2180 BIO_free(fbio); 2181 if (!foundit) 2182 BIO_printf(bio_err, 2183 "Didn't find STARTTLS in server response," 2184 " trying anyway...\n"); 2185 BIO_printf(sbio, "STARTTLS\r\n"); 2186 BIO_read(sbio, sbuf, BUFSIZZ); 2187 } 2188 break; 2189 case PROTO_POP3: 2190 { 2191 BIO_read(sbio, mbuf, BUFSIZZ); 2192 BIO_printf(sbio, "STLS\r\n"); 2193 mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ); 2194 if (mbuf_len < 0) { 2195 BIO_printf(bio_err, "BIO_read failed\n"); 2196 goto end; 2197 } 2198 } 2199 break; 2200 case PROTO_IMAP: 2201 { 2202 int foundit = 0; 2203 BIO *fbio = BIO_new(BIO_f_buffer()); 2204 2205 BIO_push(fbio, sbio); 2206 BIO_gets(fbio, mbuf, BUFSIZZ); 2207 /* STARTTLS command requires CAPABILITY... */ 2208 BIO_printf(fbio, ". CAPABILITY\r\n"); 2209 (void)BIO_flush(fbio); 2210 /* wait for multi-line CAPABILITY response */ 2211 do { 2212 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2213 if (strstr(mbuf, "STARTTLS")) 2214 foundit = 1; 2215 } 2216 while (mbuf_len > 3 && mbuf[0] != '.'); 2217 (void)BIO_flush(fbio); 2218 BIO_pop(fbio); 2219 BIO_free(fbio); 2220 if (!foundit) 2221 BIO_printf(bio_err, 2222 "Didn't find STARTTLS in server response," 2223 " trying anyway...\n"); 2224 BIO_printf(sbio, ". STARTTLS\r\n"); 2225 BIO_read(sbio, sbuf, BUFSIZZ); 2226 } 2227 break; 2228 case PROTO_FTP: 2229 { 2230 BIO *fbio = BIO_new(BIO_f_buffer()); 2231 2232 BIO_push(fbio, sbio); 2233 /* wait for multi-line response to end from FTP */ 2234 do { 2235 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2236 } 2237 while (mbuf_len > 3 && mbuf[3] == '-'); 2238 (void)BIO_flush(fbio); 2239 BIO_pop(fbio); 2240 BIO_free(fbio); 2241 BIO_printf(sbio, "AUTH TLS\r\n"); 2242 BIO_read(sbio, sbuf, BUFSIZZ); 2243 } 2244 break; 2245 case PROTO_XMPP: 2246 case PROTO_XMPP_SERVER: 2247 { 2248 int seen = 0; 2249 BIO_printf(sbio, "<stream:stream " 2250 "xmlns:stream='http://etherx.jabber.org/streams' " 2251 "xmlns='jabber:%s' to='%s' version='1.0'>", 2252 starttls_proto == PROTO_XMPP ? "client" : "server", 2253 protohost ? protohost : host); 2254 seen = BIO_read(sbio, mbuf, BUFSIZZ); 2255 if (seen < 0) { 2256 BIO_printf(bio_err, "BIO_read failed\n"); 2257 goto end; 2258 } 2259 mbuf[seen] = '\0'; 2260 while (!strstr 2261 (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'") 2262 && !strstr(mbuf, 2263 "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"")) 2264 { 2265 seen = BIO_read(sbio, mbuf, BUFSIZZ); 2266 2267 if (seen <= 0) 2268 goto shut; 2269 2270 mbuf[seen] = '\0'; 2271 } 2272 BIO_printf(sbio, 2273 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"); 2274 seen = BIO_read(sbio, sbuf, BUFSIZZ); 2275 if (seen < 0) { 2276 BIO_printf(bio_err, "BIO_read failed\n"); 2277 goto shut; 2278 } 2279 sbuf[seen] = '\0'; 2280 if (!strstr(sbuf, "<proceed")) 2281 goto shut; 2282 mbuf[0] = '\0'; 2283 } 2284 break; 2285 case PROTO_TELNET: 2286 { 2287 static const unsigned char tls_do[] = { 2288 /* IAC DO START_TLS */ 2289 255, 253, 46 2290 }; 2291 static const unsigned char tls_will[] = { 2292 /* IAC WILL START_TLS */ 2293 255, 251, 46 2294 }; 2295 static const unsigned char tls_follows[] = { 2296 /* IAC SB START_TLS FOLLOWS IAC SE */ 2297 255, 250, 46, 1, 255, 240 2298 }; 2299 int bytes; 2300 2301 /* Telnet server should demand we issue START_TLS */ 2302 bytes = BIO_read(sbio, mbuf, BUFSIZZ); 2303 if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0) 2304 goto shut; 2305 /* Agree to issue START_TLS and send the FOLLOWS sub-command */ 2306 BIO_write(sbio, tls_will, 3); 2307 BIO_write(sbio, tls_follows, 6); 2308 (void)BIO_flush(sbio); 2309 /* Telnet server also sent the FOLLOWS sub-command */ 2310 bytes = BIO_read(sbio, mbuf, BUFSIZZ); 2311 if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0) 2312 goto shut; 2313 } 2314 break; 2315 case PROTO_CONNECT: 2316 { 2317 enum { 2318 error_proto, /* Wrong protocol, not even HTTP */ 2319 error_connect, /* CONNECT failed */ 2320 success 2321 } foundit = error_connect; 2322 BIO *fbio = BIO_new(BIO_f_buffer()); 2323 2324 BIO_push(fbio, sbio); 2325 BIO_printf(fbio, "CONNECT %s HTTP/1.0\r\n\r\n", connectstr); 2326 (void)BIO_flush(fbio); 2327 /* 2328 * The first line is the HTTP response. According to RFC 7230, 2329 * it's formated exactly like this: 2330 * 2331 * HTTP/d.d ddd Reason text\r\n 2332 */ 2333 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2334 if (mbuf_len < (int)strlen("HTTP/1.0 200")) { 2335 BIO_printf(bio_err, 2336 "%s: HTTP CONNECT failed, insufficient response " 2337 "from proxy (got %d octets)\n", prog, mbuf_len); 2338 (void)BIO_flush(fbio); 2339 BIO_pop(fbio); 2340 BIO_free(fbio); 2341 goto shut; 2342 } 2343 if (mbuf[8] != ' ') { 2344 BIO_printf(bio_err, 2345 "%s: HTTP CONNECT failed, incorrect response " 2346 "from proxy\n", prog); 2347 foundit = error_proto; 2348 } else if (mbuf[9] != '2') { 2349 BIO_printf(bio_err, "%s: HTTP CONNECT failed: %s ", prog, 2350 &mbuf[9]); 2351 } else { 2352 foundit = success; 2353 } 2354 if (foundit != error_proto) { 2355 /* Read past all following headers */ 2356 do { 2357 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2358 } while (mbuf_len > 2); 2359 } 2360 (void)BIO_flush(fbio); 2361 BIO_pop(fbio); 2362 BIO_free(fbio); 2363 if (foundit != success) { 2364 goto shut; 2365 } 2366 } 2367 break; 2368 case PROTO_IRC: 2369 { 2370 int numeric; 2371 BIO *fbio = BIO_new(BIO_f_buffer()); 2372 2373 BIO_push(fbio, sbio); 2374 BIO_printf(fbio, "STARTTLS\r\n"); 2375 (void)BIO_flush(fbio); 2376 width = SSL_get_fd(con) + 1; 2377 2378 do { 2379 numeric = 0; 2380 2381 FD_ZERO(&readfds); 2382 openssl_fdset(SSL_get_fd(con), &readfds); 2383 timeout.tv_sec = S_CLIENT_IRC_READ_TIMEOUT; 2384 timeout.tv_usec = 0; 2385 /* 2386 * If the IRCd doesn't respond within 2387 * S_CLIENT_IRC_READ_TIMEOUT seconds, assume 2388 * it doesn't support STARTTLS. Many IRCds 2389 * will not give _any_ sort of response to a 2390 * STARTTLS command when it's not supported. 2391 */ 2392 if (!BIO_get_buffer_num_lines(fbio) 2393 && !BIO_pending(fbio) 2394 && !BIO_pending(sbio) 2395 && select(width, (void *)&readfds, NULL, NULL, 2396 &timeout) < 1) { 2397 BIO_printf(bio_err, 2398 "Timeout waiting for response (%d seconds).\n", 2399 S_CLIENT_IRC_READ_TIMEOUT); 2400 break; 2401 } 2402 2403 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2404 if (mbuf_len < 1 || sscanf(mbuf, "%*s %d", &numeric) != 1) 2405 break; 2406 /* :example.net 451 STARTTLS :You have not registered */ 2407 /* :example.net 421 STARTTLS :Unknown command */ 2408 if ((numeric == 451 || numeric == 421) 2409 && strstr(mbuf, "STARTTLS") != NULL) { 2410 BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf); 2411 break; 2412 } 2413 if (numeric == 691) { 2414 BIO_printf(bio_err, "STARTTLS negotiation failed: "); 2415 ERR_print_errors(bio_err); 2416 break; 2417 } 2418 } while (numeric != 670); 2419 2420 (void)BIO_flush(fbio); 2421 BIO_pop(fbio); 2422 BIO_free(fbio); 2423 if (numeric != 670) { 2424 BIO_printf(bio_err, "Server does not support STARTTLS.\n"); 2425 ret = 1; 2426 goto shut; 2427 } 2428 } 2429 break; 2430 case PROTO_MYSQL: 2431 { 2432 /* SSL request packet */ 2433 static const unsigned char ssl_req[] = { 2434 /* payload_length, sequence_id */ 2435 0x20, 0x00, 0x00, 0x01, 2436 /* payload */ 2437 /* capability flags, CLIENT_SSL always set */ 2438 0x85, 0xae, 0x7f, 0x00, 2439 /* max-packet size */ 2440 0x00, 0x00, 0x00, 0x01, 2441 /* character set */ 2442 0x21, 2443 /* string[23] reserved (all [0]) */ 2444 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2445 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2446 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 2447 }; 2448 int bytes = 0; 2449 int ssl_flg = 0x800; 2450 int pos; 2451 const unsigned char *packet = (const unsigned char *)sbuf; 2452 2453 /* Receiving Initial Handshake packet. */ 2454 bytes = BIO_read(sbio, (void *)packet, BUFSIZZ); 2455 if (bytes < 0) { 2456 BIO_printf(bio_err, "BIO_read failed\n"); 2457 goto shut; 2458 /* Packet length[3], Packet number[1] + minimum payload[17] */ 2459 } else if (bytes < 21) { 2460 BIO_printf(bio_err, "MySQL packet too short.\n"); 2461 goto shut; 2462 } else if (bytes != (4 + packet[0] + 2463 (packet[1] << 8) + 2464 (packet[2] << 16))) { 2465 BIO_printf(bio_err, "MySQL packet length does not match.\n"); 2466 goto shut; 2467 /* protocol version[1] */ 2468 } else if (packet[4] != 0xA) { 2469 BIO_printf(bio_err, 2470 "Only MySQL protocol version 10 is supported.\n"); 2471 goto shut; 2472 } 2473 2474 pos = 5; 2475 /* server version[string+NULL] */ 2476 for (;;) { 2477 if (pos >= bytes) { 2478 BIO_printf(bio_err, "Cannot confirm server version. "); 2479 goto shut; 2480 } else if (packet[pos++] == '\0') { 2481 break; 2482 } 2483 } 2484 2485 /* make sure we have at least 15 bytes left in the packet */ 2486 if (pos + 15 > bytes) { 2487 BIO_printf(bio_err, 2488 "MySQL server handshake packet is broken.\n"); 2489 goto shut; 2490 } 2491 2492 pos += 12; /* skip over conn id[4] + SALT[8] */ 2493 if (packet[pos++] != '\0') { /* verify filler */ 2494 BIO_printf(bio_err, 2495 "MySQL packet is broken.\n"); 2496 goto shut; 2497 } 2498 2499 /* capability flags[2] */ 2500 if (!((packet[pos] + (packet[pos + 1] << 8)) & ssl_flg)) { 2501 BIO_printf(bio_err, "MySQL server does not support SSL.\n"); 2502 goto shut; 2503 } 2504 2505 /* Sending SSL Handshake packet. */ 2506 BIO_write(sbio, ssl_req, sizeof(ssl_req)); 2507 (void)BIO_flush(sbio); 2508 } 2509 break; 2510 case PROTO_POSTGRES: 2511 { 2512 static const unsigned char ssl_request[] = { 2513 /* Length SSLRequest */ 2514 0, 0, 0, 8, 4, 210, 22, 47 2515 }; 2516 int bytes; 2517 2518 /* Send SSLRequest packet */ 2519 BIO_write(sbio, ssl_request, 8); 2520 (void)BIO_flush(sbio); 2521 2522 /* Reply will be a single S if SSL is enabled */ 2523 bytes = BIO_read(sbio, sbuf, BUFSIZZ); 2524 if (bytes != 1 || sbuf[0] != 'S') 2525 goto shut; 2526 } 2527 break; 2528 case PROTO_NNTP: 2529 { 2530 int foundit = 0; 2531 BIO *fbio = BIO_new(BIO_f_buffer()); 2532 2533 BIO_push(fbio, sbio); 2534 BIO_gets(fbio, mbuf, BUFSIZZ); 2535 /* STARTTLS command requires CAPABILITIES... */ 2536 BIO_printf(fbio, "CAPABILITIES\r\n"); 2537 (void)BIO_flush(fbio); 2538 /* wait for multi-line CAPABILITIES response */ 2539 do { 2540 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2541 if (strstr(mbuf, "STARTTLS")) 2542 foundit = 1; 2543 } while (mbuf_len > 1 && mbuf[0] != '.'); 2544 (void)BIO_flush(fbio); 2545 BIO_pop(fbio); 2546 BIO_free(fbio); 2547 if (!foundit) 2548 BIO_printf(bio_err, 2549 "Didn't find STARTTLS in server response," 2550 " trying anyway...\n"); 2551 BIO_printf(sbio, "STARTTLS\r\n"); 2552 mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ); 2553 if (mbuf_len < 0) { 2554 BIO_printf(bio_err, "BIO_read failed\n"); 2555 goto end; 2556 } 2557 mbuf[mbuf_len] = '\0'; 2558 if (strstr(mbuf, "382") == NULL) { 2559 BIO_printf(bio_err, "STARTTLS failed: %s", mbuf); 2560 goto shut; 2561 } 2562 } 2563 break; 2564 case PROTO_SIEVE: 2565 { 2566 int foundit = 0; 2567 BIO *fbio = BIO_new(BIO_f_buffer()); 2568 2569 BIO_push(fbio, sbio); 2570 /* wait for multi-line response to end from Sieve */ 2571 do { 2572 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); 2573 /* 2574 * According to RFC 5804 § 1.7, capability 2575 * is case-insensitive, make it uppercase 2576 */ 2577 if (mbuf_len > 1 && mbuf[0] == '"') { 2578 make_uppercase(mbuf); 2579 if (strncmp(mbuf, "\"STARTTLS\"", 10) == 0) 2580 foundit = 1; 2581 } 2582 } while (mbuf_len > 1 && mbuf[0] == '"'); 2583 (void)BIO_flush(fbio); 2584 BIO_pop(fbio); 2585 BIO_free(fbio); 2586 if (!foundit) 2587 BIO_printf(bio_err, 2588 "Didn't find STARTTLS in server response," 2589 " trying anyway...\n"); 2590 BIO_printf(sbio, "STARTTLS\r\n"); 2591 mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ); 2592 if (mbuf_len < 0) { 2593 BIO_printf(bio_err, "BIO_read failed\n"); 2594 goto end; 2595 } 2596 mbuf[mbuf_len] = '\0'; 2597 if (mbuf_len < 2) { 2598 BIO_printf(bio_err, "STARTTLS failed: %s", mbuf); 2599 goto shut; 2600 } 2601 /* 2602 * According to RFC 5804 § 2.2, response codes are case- 2603 * insensitive, make it uppercase but preserve the response. 2604 */ 2605 strncpy(sbuf, mbuf, 2); 2606 make_uppercase(sbuf); 2607 if (strncmp(sbuf, "OK", 2) != 0) { 2608 BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf); 2609 goto shut; 2610 } 2611 } 2612 break; 2613 case PROTO_LDAP: 2614 { 2615 /* StartTLS Operation according to RFC 4511 */ 2616 static char ldap_tls_genconf[] = "asn1=SEQUENCE:LDAPMessage\n" 2617 "[LDAPMessage]\n" 2618 "messageID=INTEGER:1\n" 2619 "extendedReq=EXPLICIT:23A,IMPLICIT:0C," 2620 "FORMAT:ASCII,OCT:1.3.6.1.4.1.1466.20037\n"; 2621 long errline = -1; 2622 char *genstr = NULL; 2623 int result = -1; 2624 ASN1_TYPE *atyp = NULL; 2625 BIO *ldapbio = BIO_new(BIO_s_mem()); 2626 CONF *cnf = NCONF_new(NULL); 2627 2628 if (cnf == NULL) { 2629 BIO_free(ldapbio); 2630 goto end; 2631 } 2632 BIO_puts(ldapbio, ldap_tls_genconf); 2633 if (NCONF_load_bio(cnf, ldapbio, &errline) <= 0) { 2634 BIO_free(ldapbio); 2635 NCONF_free(cnf); 2636 if (errline <= 0) { 2637 BIO_printf(bio_err, "NCONF_load_bio failed\n"); 2638 goto end; 2639 } else { 2640 BIO_printf(bio_err, "Error on line %ld\n", errline); 2641 goto end; 2642 } 2643 } 2644 BIO_free(ldapbio); 2645 genstr = NCONF_get_string(cnf, "default", "asn1"); 2646 if (genstr == NULL) { 2647 NCONF_free(cnf); 2648 BIO_printf(bio_err, "NCONF_get_string failed\n"); 2649 goto end; 2650 } 2651 atyp = ASN1_generate_nconf(genstr, cnf); 2652 if (atyp == NULL) { 2653 NCONF_free(cnf); 2654 BIO_printf(bio_err, "ASN1_generate_nconf failed\n"); 2655 goto end; 2656 } 2657 NCONF_free(cnf); 2658 2659 /* Send SSLRequest packet */ 2660 BIO_write(sbio, atyp->value.sequence->data, 2661 atyp->value.sequence->length); 2662 (void)BIO_flush(sbio); 2663 ASN1_TYPE_free(atyp); 2664 2665 mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ); 2666 if (mbuf_len < 0) { 2667 BIO_printf(bio_err, "BIO_read failed\n"); 2668 goto end; 2669 } 2670 result = ldap_ExtendedResponse_parse(mbuf, mbuf_len); 2671 if (result < 0) { 2672 BIO_printf(bio_err, "ldap_ExtendedResponse_parse failed\n"); 2673 goto shut; 2674 } else if (result > 0) { 2675 BIO_printf(bio_err, "STARTTLS failed, LDAP Result Code: %i\n", 2676 result); 2677 goto shut; 2678 } 2679 mbuf_len = 0; 2680 } 2681 break; 2682 } 2683 2684 if (early_data_file != NULL 2685 && ((SSL_get0_session(con) != NULL 2686 && SSL_SESSION_get_max_early_data(SSL_get0_session(con)) > 0) 2687 || (psksess != NULL 2688 && SSL_SESSION_get_max_early_data(psksess) > 0))) { 2689 BIO *edfile = BIO_new_file(early_data_file, "r"); 2690 size_t readbytes, writtenbytes; 2691 int finish = 0; 2692 2693 if (edfile == NULL) { 2694 BIO_printf(bio_err, "Cannot open early data file\n"); 2695 goto shut; 2696 } 2697 2698 while (!finish) { 2699 if (!BIO_read_ex(edfile, cbuf, BUFSIZZ, &readbytes)) 2700 finish = 1; 2701 2702 while (!SSL_write_early_data(con, cbuf, readbytes, &writtenbytes)) { 2703 switch (SSL_get_error(con, 0)) { 2704 case SSL_ERROR_WANT_WRITE: 2705 case SSL_ERROR_WANT_ASYNC: 2706 case SSL_ERROR_WANT_READ: 2707 /* Just keep trying - busy waiting */ 2708 continue; 2709 default: 2710 BIO_printf(bio_err, "Error writing early data\n"); 2711 BIO_free(edfile); 2712 ERR_print_errors(bio_err); 2713 goto shut; 2714 } 2715 } 2716 } 2717 2718 BIO_free(edfile); 2719 } 2720 2721 for (;;) { 2722 FD_ZERO(&readfds); 2723 FD_ZERO(&writefds); 2724 2725 if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout)) 2726 timeoutp = &timeout; 2727 else 2728 timeoutp = NULL; 2729 2730 if (!SSL_is_init_finished(con) && SSL_total_renegotiations(con) == 0 2731 && SSL_get_key_update_type(con) == SSL_KEY_UPDATE_NONE) { 2732 in_init = 1; 2733 tty_on = 0; 2734 } else { 2735 tty_on = 1; 2736 if (in_init) { 2737 in_init = 0; 2738 2739 if (c_brief) { 2740 BIO_puts(bio_err, "CONNECTION ESTABLISHED\n"); 2741 print_ssl_summary(con); 2742 } 2743 2744 print_stuff(bio_c_out, con, full_log); 2745 if (full_log > 0) 2746 full_log--; 2747 2748 if (starttls_proto) { 2749 BIO_write(bio_err, mbuf, mbuf_len); 2750 /* We don't need to know any more */ 2751 if (!reconnect) 2752 starttls_proto = PROTO_OFF; 2753 } 2754 2755 if (reconnect) { 2756 reconnect--; 2757 BIO_printf(bio_c_out, 2758 "drop connection and then reconnect\n"); 2759 do_ssl_shutdown(con); 2760 SSL_set_connect_state(con); 2761 BIO_closesocket(SSL_get_fd(con)); 2762 goto re_start; 2763 } 2764 } 2765 } 2766 2767 ssl_pending = read_ssl && SSL_has_pending(con); 2768 2769 if (!ssl_pending) { 2770 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) 2771 if (tty_on) { 2772 /* 2773 * Note that select() returns when read _would not block_, 2774 * and EOF satisfies that. To avoid a CPU-hogging loop, 2775 * set the flag so we exit. 2776 */ 2777 if (read_tty && !at_eof) 2778 openssl_fdset(fileno_stdin(), &readfds); 2779 #if !defined(OPENSSL_SYS_VMS) 2780 if (write_tty) 2781 openssl_fdset(fileno_stdout(), &writefds); 2782 #endif 2783 } 2784 if (read_ssl) 2785 openssl_fdset(SSL_get_fd(con), &readfds); 2786 if (write_ssl) 2787 openssl_fdset(SSL_get_fd(con), &writefds); 2788 #else 2789 if (!tty_on || !write_tty) { 2790 if (read_ssl) 2791 openssl_fdset(SSL_get_fd(con), &readfds); 2792 if (write_ssl) 2793 openssl_fdset(SSL_get_fd(con), &writefds); 2794 } 2795 #endif 2796 2797 /* 2798 * Note: under VMS with SOCKETSHR the second parameter is 2799 * currently of type (int *) whereas under other systems it is 2800 * (void *) if you don't have a cast it will choke the compiler: 2801 * if you do have a cast then you can either go for (int *) or 2802 * (void *). 2803 */ 2804 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) 2805 /* 2806 * Under Windows/DOS we make the assumption that we can always 2807 * write to the tty: therefore if we need to write to the tty we 2808 * just fall through. Otherwise we timeout the select every 2809 * second and see if there are any keypresses. Note: this is a 2810 * hack, in a proper Windows application we wouldn't do this. 2811 */ 2812 i = 0; 2813 if (!write_tty) { 2814 if (read_tty) { 2815 tv.tv_sec = 1; 2816 tv.tv_usec = 0; 2817 i = select(width, (void *)&readfds, (void *)&writefds, 2818 NULL, &tv); 2819 if (!i && (!has_stdin_waiting() || !read_tty)) 2820 continue; 2821 } else 2822 i = select(width, (void *)&readfds, (void *)&writefds, 2823 NULL, timeoutp); 2824 } 2825 #else 2826 i = select(width, (void *)&readfds, (void *)&writefds, 2827 NULL, timeoutp); 2828 #endif 2829 if (i < 0) { 2830 BIO_printf(bio_err, "bad select %d\n", 2831 get_last_socket_error()); 2832 goto shut; 2833 } 2834 } 2835 2836 if (SSL_is_dtls(con) && DTLSv1_handle_timeout(con) > 0) 2837 BIO_printf(bio_err, "TIMEOUT occurred\n"); 2838 2839 if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) { 2840 k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len); 2841 switch (SSL_get_error(con, k)) { 2842 case SSL_ERROR_NONE: 2843 cbuf_off += k; 2844 cbuf_len -= k; 2845 if (k <= 0) 2846 goto end; 2847 /* we have done a write(con,NULL,0); */ 2848 if (cbuf_len <= 0) { 2849 read_tty = 1; 2850 write_ssl = 0; 2851 } else { /* if (cbuf_len > 0) */ 2852 2853 read_tty = 0; 2854 write_ssl = 1; 2855 } 2856 break; 2857 case SSL_ERROR_WANT_WRITE: 2858 BIO_printf(bio_c_out, "write W BLOCK\n"); 2859 write_ssl = 1; 2860 read_tty = 0; 2861 break; 2862 case SSL_ERROR_WANT_ASYNC: 2863 BIO_printf(bio_c_out, "write A BLOCK\n"); 2864 wait_for_async(con); 2865 write_ssl = 1; 2866 read_tty = 0; 2867 break; 2868 case SSL_ERROR_WANT_READ: 2869 BIO_printf(bio_c_out, "write R BLOCK\n"); 2870 write_tty = 0; 2871 read_ssl = 1; 2872 write_ssl = 0; 2873 break; 2874 case SSL_ERROR_WANT_X509_LOOKUP: 2875 BIO_printf(bio_c_out, "write X BLOCK\n"); 2876 break; 2877 case SSL_ERROR_ZERO_RETURN: 2878 if (cbuf_len != 0) { 2879 BIO_printf(bio_c_out, "shutdown\n"); 2880 ret = 0; 2881 goto shut; 2882 } else { 2883 read_tty = 1; 2884 write_ssl = 0; 2885 break; 2886 } 2887 2888 case SSL_ERROR_SYSCALL: 2889 if ((k != 0) || (cbuf_len != 0)) { 2890 BIO_printf(bio_err, "write:errno=%d\n", 2891 get_last_socket_error()); 2892 goto shut; 2893 } else { 2894 read_tty = 1; 2895 write_ssl = 0; 2896 } 2897 break; 2898 case SSL_ERROR_WANT_ASYNC_JOB: 2899 /* This shouldn't ever happen in s_client - treat as an error */ 2900 case SSL_ERROR_SSL: 2901 ERR_print_errors(bio_err); 2902 goto shut; 2903 } 2904 } 2905 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VMS) 2906 /* Assume Windows/DOS/BeOS can always write */ 2907 else if (!ssl_pending && write_tty) 2908 #else 2909 else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds)) 2910 #endif 2911 { 2912 #ifdef CHARSET_EBCDIC 2913 ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len); 2914 #endif 2915 i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len); 2916 2917 if (i <= 0) { 2918 BIO_printf(bio_c_out, "DONE\n"); 2919 ret = 0; 2920 goto shut; 2921 } 2922 2923 sbuf_len -= i; 2924 sbuf_off += i; 2925 if (sbuf_len <= 0) { 2926 read_ssl = 1; 2927 write_tty = 0; 2928 } 2929 } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) { 2930 #ifdef RENEG 2931 { 2932 static int iiii; 2933 if (++iiii == 52) { 2934 SSL_renegotiate(con); 2935 iiii = 0; 2936 } 2937 } 2938 #endif 2939 k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ ); 2940 2941 switch (SSL_get_error(con, k)) { 2942 case SSL_ERROR_NONE: 2943 if (k <= 0) 2944 goto end; 2945 sbuf_off = 0; 2946 sbuf_len = k; 2947 2948 read_ssl = 0; 2949 write_tty = 1; 2950 break; 2951 case SSL_ERROR_WANT_ASYNC: 2952 BIO_printf(bio_c_out, "read A BLOCK\n"); 2953 wait_for_async(con); 2954 write_tty = 0; 2955 read_ssl = 1; 2956 if ((read_tty == 0) && (write_ssl == 0)) 2957 write_ssl = 1; 2958 break; 2959 case SSL_ERROR_WANT_WRITE: 2960 BIO_printf(bio_c_out, "read W BLOCK\n"); 2961 write_ssl = 1; 2962 read_tty = 0; 2963 break; 2964 case SSL_ERROR_WANT_READ: 2965 BIO_printf(bio_c_out, "read R BLOCK\n"); 2966 write_tty = 0; 2967 read_ssl = 1; 2968 if ((read_tty == 0) && (write_ssl == 0)) 2969 write_ssl = 1; 2970 break; 2971 case SSL_ERROR_WANT_X509_LOOKUP: 2972 BIO_printf(bio_c_out, "read X BLOCK\n"); 2973 break; 2974 case SSL_ERROR_SYSCALL: 2975 ret = get_last_socket_error(); 2976 if (c_brief) 2977 BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n"); 2978 else 2979 BIO_printf(bio_err, "read:errno=%d\n", ret); 2980 goto shut; 2981 case SSL_ERROR_ZERO_RETURN: 2982 BIO_printf(bio_c_out, "closed\n"); 2983 ret = 0; 2984 goto shut; 2985 case SSL_ERROR_WANT_ASYNC_JOB: 2986 /* This shouldn't ever happen in s_client. Treat as an error */ 2987 case SSL_ERROR_SSL: 2988 ERR_print_errors(bio_err); 2989 goto shut; 2990 } 2991 } 2992 /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */ 2993 #if defined(OPENSSL_SYS_MSDOS) 2994 else if (has_stdin_waiting()) 2995 #else 2996 else if (FD_ISSET(fileno_stdin(), &readfds)) 2997 #endif 2998 { 2999 if (crlf) { 3000 int j, lf_num; 3001 3002 i = raw_read_stdin(cbuf, BUFSIZZ / 2); 3003 lf_num = 0; 3004 /* both loops are skipped when i <= 0 */ 3005 for (j = 0; j < i; j++) 3006 if (cbuf[j] == '\n') 3007 lf_num++; 3008 for (j = i - 1; j >= 0; j--) { 3009 cbuf[j + lf_num] = cbuf[j]; 3010 if (cbuf[j] == '\n') { 3011 lf_num--; 3012 i++; 3013 cbuf[j + lf_num] = '\r'; 3014 } 3015 } 3016 assert(lf_num == 0); 3017 } else 3018 i = raw_read_stdin(cbuf, BUFSIZZ); 3019 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) 3020 if (i == 0) 3021 at_eof = 1; 3022 #endif 3023 3024 if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q' && cmdletters))) { 3025 BIO_printf(bio_err, "DONE\n"); 3026 ret = 0; 3027 goto shut; 3028 } 3029 3030 if ((!c_ign_eof) && (cbuf[0] == 'R' && cmdletters)) { 3031 BIO_printf(bio_err, "RENEGOTIATING\n"); 3032 SSL_renegotiate(con); 3033 cbuf_len = 0; 3034 } 3035 3036 if (!c_ign_eof && (cbuf[0] == 'K' || cbuf[0] == 'k' ) 3037 && cmdletters) { 3038 BIO_printf(bio_err, "KEYUPDATE\n"); 3039 SSL_key_update(con, 3040 cbuf[0] == 'K' ? SSL_KEY_UPDATE_REQUESTED 3041 : SSL_KEY_UPDATE_NOT_REQUESTED); 3042 cbuf_len = 0; 3043 } 3044 #ifndef OPENSSL_NO_HEARTBEATS 3045 else if ((!c_ign_eof) && (cbuf[0] == 'B' && cmdletters)) { 3046 BIO_printf(bio_err, "HEARTBEATING\n"); 3047 SSL_heartbeat(con); 3048 cbuf_len = 0; 3049 } 3050 #endif 3051 else { 3052 cbuf_len = i; 3053 cbuf_off = 0; 3054 #ifdef CHARSET_EBCDIC 3055 ebcdic2ascii(cbuf, cbuf, i); 3056 #endif 3057 } 3058 3059 write_ssl = 1; 3060 read_tty = 0; 3061 } 3062 } 3063 3064 ret = 0; 3065 shut: 3066 if (in_init) 3067 print_stuff(bio_c_out, con, full_log); 3068 do_ssl_shutdown(con); 3069 3070 /* 3071 * If we ended with an alert being sent, but still with data in the 3072 * network buffer to be read, then calling BIO_closesocket() will 3073 * result in a TCP-RST being sent. On some platforms (notably 3074 * Windows) then this will result in the peer immediately abandoning 3075 * the connection including any buffered alert data before it has 3076 * had a chance to be read. Shutting down the sending side first, 3077 * and then closing the socket sends TCP-FIN first followed by 3078 * TCP-RST. This seems to allow the peer to read the alert data. 3079 */ 3080 shutdown(SSL_get_fd(con), 1); /* SHUT_WR */ 3081 /* 3082 * We just said we have nothing else to say, but it doesn't mean that 3083 * the other side has nothing. It's even recommended to consume incoming 3084 * data. [In testing context this ensures that alerts are passed on...] 3085 */ 3086 timeout.tv_sec = 0; 3087 timeout.tv_usec = 500000; /* some extreme round-trip */ 3088 do { 3089 FD_ZERO(&readfds); 3090 openssl_fdset(s, &readfds); 3091 } while (select(s + 1, &readfds, NULL, NULL, &timeout) > 0 3092 && BIO_read(sbio, sbuf, BUFSIZZ) > 0); 3093 3094 BIO_closesocket(SSL_get_fd(con)); 3095 end: 3096 if (con != NULL) { 3097 if (prexit != 0) 3098 print_stuff(bio_c_out, con, 1); 3099 SSL_free(con); 3100 } 3101 SSL_SESSION_free(psksess); 3102 #if !defined(OPENSSL_NO_NEXTPROTONEG) 3103 OPENSSL_free(next_proto.data); 3104 #endif 3105 SSL_CTX_free(ctx); 3106 set_keylog_file(NULL, NULL); 3107 X509_free(cert); 3108 sk_X509_CRL_pop_free(crls, X509_CRL_free); 3109 EVP_PKEY_free(key); 3110 sk_X509_pop_free(chain, X509_free); 3111 OPENSSL_free(pass); 3112 #ifndef OPENSSL_NO_SRP 3113 OPENSSL_free(srp_arg.srppassin); 3114 #endif 3115 OPENSSL_free(connectstr); 3116 OPENSSL_free(bindstr); 3117 OPENSSL_free(host); 3118 OPENSSL_free(port); 3119 X509_VERIFY_PARAM_free(vpm); 3120 ssl_excert_free(exc); 3121 sk_OPENSSL_STRING_free(ssl_args); 3122 sk_OPENSSL_STRING_free(dane_tlsa_rrset); 3123 SSL_CONF_CTX_free(cctx); 3124 OPENSSL_clear_free(cbuf, BUFSIZZ); 3125 OPENSSL_clear_free(sbuf, BUFSIZZ); 3126 OPENSSL_clear_free(mbuf, BUFSIZZ); 3127 release_engine(e); 3128 BIO_free(bio_c_out); 3129 bio_c_out = NULL; 3130 BIO_free(bio_c_msg); 3131 bio_c_msg = NULL; 3132 return ret; 3133 } 3134 3135 static void print_stuff(BIO *bio, SSL *s, int full) 3136 { 3137 X509 *peer = NULL; 3138 STACK_OF(X509) *sk; 3139 const SSL_CIPHER *c; 3140 int i, istls13 = (SSL_version(s) == TLS1_3_VERSION); 3141 long verify_result; 3142 #ifndef OPENSSL_NO_COMP 3143 const COMP_METHOD *comp, *expansion; 3144 #endif 3145 unsigned char *exportedkeymat; 3146 #ifndef OPENSSL_NO_CT 3147 const SSL_CTX *ctx = SSL_get_SSL_CTX(s); 3148 #endif 3149 3150 if (full) { 3151 int got_a_chain = 0; 3152 3153 sk = SSL_get_peer_cert_chain(s); 3154 if (sk != NULL) { 3155 got_a_chain = 1; 3156 3157 BIO_printf(bio, "---\nCertificate chain\n"); 3158 for (i = 0; i < sk_X509_num(sk); i++) { 3159 BIO_printf(bio, "%2d s:", i); 3160 X509_NAME_print_ex(bio, X509_get_subject_name(sk_X509_value(sk, i)), 0, get_nameopt()); 3161 BIO_puts(bio, "\n"); 3162 BIO_printf(bio, " i:"); 3163 X509_NAME_print_ex(bio, X509_get_issuer_name(sk_X509_value(sk, i)), 0, get_nameopt()); 3164 BIO_puts(bio, "\n"); 3165 if (c_showcerts) 3166 PEM_write_bio_X509(bio, sk_X509_value(sk, i)); 3167 } 3168 } 3169 3170 BIO_printf(bio, "---\n"); 3171 peer = SSL_get_peer_certificate(s); 3172 if (peer != NULL) { 3173 BIO_printf(bio, "Server certificate\n"); 3174 3175 /* Redundant if we showed the whole chain */ 3176 if (!(c_showcerts && got_a_chain)) 3177 PEM_write_bio_X509(bio, peer); 3178 dump_cert_text(bio, peer); 3179 } else { 3180 BIO_printf(bio, "no peer certificate available\n"); 3181 } 3182 print_ca_names(bio, s); 3183 3184 ssl_print_sigalgs(bio, s); 3185 ssl_print_tmp_key(bio, s); 3186 3187 #ifndef OPENSSL_NO_CT 3188 /* 3189 * When the SSL session is anonymous, or resumed via an abbreviated 3190 * handshake, no SCTs are provided as part of the handshake. While in 3191 * a resumed session SCTs may be present in the session's certificate, 3192 * no callbacks are invoked to revalidate these, and in any case that 3193 * set of SCTs may be incomplete. Thus it makes little sense to 3194 * attempt to display SCTs from a resumed session's certificate, and of 3195 * course none are associated with an anonymous peer. 3196 */ 3197 if (peer != NULL && !SSL_session_reused(s) && SSL_ct_is_enabled(s)) { 3198 const STACK_OF(SCT) *scts = SSL_get0_peer_scts(s); 3199 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0; 3200 3201 BIO_printf(bio, "---\nSCTs present (%i)\n", sct_count); 3202 if (sct_count > 0) { 3203 const CTLOG_STORE *log_store = SSL_CTX_get0_ctlog_store(ctx); 3204 3205 BIO_printf(bio, "---\n"); 3206 for (i = 0; i < sct_count; ++i) { 3207 SCT *sct = sk_SCT_value(scts, i); 3208 3209 BIO_printf(bio, "SCT validation status: %s\n", 3210 SCT_validation_status_string(sct)); 3211 SCT_print(sct, bio, 0, log_store); 3212 if (i < sct_count - 1) 3213 BIO_printf(bio, "\n---\n"); 3214 } 3215 BIO_printf(bio, "\n"); 3216 } 3217 } 3218 #endif 3219 3220 BIO_printf(bio, 3221 "---\nSSL handshake has read %ju bytes " 3222 "and written %ju bytes\n", 3223 BIO_number_read(SSL_get_rbio(s)), 3224 BIO_number_written(SSL_get_wbio(s))); 3225 } 3226 print_verify_detail(s, bio); 3227 BIO_printf(bio, (SSL_session_reused(s) ? "---\nReused, " : "---\nNew, ")); 3228 c = SSL_get_current_cipher(s); 3229 BIO_printf(bio, "%s, Cipher is %s\n", 3230 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); 3231 if (peer != NULL) { 3232 EVP_PKEY *pktmp; 3233 3234 pktmp = X509_get0_pubkey(peer); 3235 BIO_printf(bio, "Server public key is %d bit\n", 3236 EVP_PKEY_bits(pktmp)); 3237 } 3238 BIO_printf(bio, "Secure Renegotiation IS%s supported\n", 3239 SSL_get_secure_renegotiation_support(s) ? "" : " NOT"); 3240 #ifndef OPENSSL_NO_COMP 3241 comp = SSL_get_current_compression(s); 3242 expansion = SSL_get_current_expansion(s); 3243 BIO_printf(bio, "Compression: %s\n", 3244 comp ? SSL_COMP_get_name(comp) : "NONE"); 3245 BIO_printf(bio, "Expansion: %s\n", 3246 expansion ? SSL_COMP_get_name(expansion) : "NONE"); 3247 #endif 3248 3249 #ifdef SSL_DEBUG 3250 { 3251 /* Print out local port of connection: useful for debugging */ 3252 int sock; 3253 union BIO_sock_info_u info; 3254 3255 sock = SSL_get_fd(s); 3256 if ((info.addr = BIO_ADDR_new()) != NULL 3257 && BIO_sock_info(sock, BIO_SOCK_INFO_ADDRESS, &info)) { 3258 BIO_printf(bio_c_out, "LOCAL PORT is %u\n", 3259 ntohs(BIO_ADDR_rawport(info.addr))); 3260 } 3261 BIO_ADDR_free(info.addr); 3262 } 3263 #endif 3264 3265 #if !defined(OPENSSL_NO_NEXTPROTONEG) 3266 if (next_proto.status != -1) { 3267 const unsigned char *proto; 3268 unsigned int proto_len; 3269 SSL_get0_next_proto_negotiated(s, &proto, &proto_len); 3270 BIO_printf(bio, "Next protocol: (%d) ", next_proto.status); 3271 BIO_write(bio, proto, proto_len); 3272 BIO_write(bio, "\n", 1); 3273 } 3274 #endif 3275 { 3276 const unsigned char *proto; 3277 unsigned int proto_len; 3278 SSL_get0_alpn_selected(s, &proto, &proto_len); 3279 if (proto_len > 0) { 3280 BIO_printf(bio, "ALPN protocol: "); 3281 BIO_write(bio, proto, proto_len); 3282 BIO_write(bio, "\n", 1); 3283 } else 3284 BIO_printf(bio, "No ALPN negotiated\n"); 3285 } 3286 3287 #ifndef OPENSSL_NO_SRTP 3288 { 3289 SRTP_PROTECTION_PROFILE *srtp_profile = 3290 SSL_get_selected_srtp_profile(s); 3291 3292 if (srtp_profile) 3293 BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n", 3294 srtp_profile->name); 3295 } 3296 #endif 3297 3298 if (istls13) { 3299 switch (SSL_get_early_data_status(s)) { 3300 case SSL_EARLY_DATA_NOT_SENT: 3301 BIO_printf(bio, "Early data was not sent\n"); 3302 break; 3303 3304 case SSL_EARLY_DATA_REJECTED: 3305 BIO_printf(bio, "Early data was rejected\n"); 3306 break; 3307 3308 case SSL_EARLY_DATA_ACCEPTED: 3309 BIO_printf(bio, "Early data was accepted\n"); 3310 break; 3311 3312 } 3313 3314 /* 3315 * We also print the verify results when we dump session information, 3316 * but in TLSv1.3 we may not get that right away (or at all) depending 3317 * on when we get a NewSessionTicket. Therefore we print it now as well. 3318 */ 3319 verify_result = SSL_get_verify_result(s); 3320 BIO_printf(bio, "Verify return code: %ld (%s)\n", verify_result, 3321 X509_verify_cert_error_string(verify_result)); 3322 } else { 3323 /* In TLSv1.3 we do this on arrival of a NewSessionTicket */ 3324 SSL_SESSION_print(bio, SSL_get_session(s)); 3325 } 3326 3327 if (SSL_get_session(s) != NULL && keymatexportlabel != NULL) { 3328 BIO_printf(bio, "Keying material exporter:\n"); 3329 BIO_printf(bio, " Label: '%s'\n", keymatexportlabel); 3330 BIO_printf(bio, " Length: %i bytes\n", keymatexportlen); 3331 exportedkeymat = app_malloc(keymatexportlen, "export key"); 3332 if (!SSL_export_keying_material(s, exportedkeymat, 3333 keymatexportlen, 3334 keymatexportlabel, 3335 strlen(keymatexportlabel), 3336 NULL, 0, 0)) { 3337 BIO_printf(bio, " Error\n"); 3338 } else { 3339 BIO_printf(bio, " Keying material: "); 3340 for (i = 0; i < keymatexportlen; i++) 3341 BIO_printf(bio, "%02X", exportedkeymat[i]); 3342 BIO_printf(bio, "\n"); 3343 } 3344 OPENSSL_free(exportedkeymat); 3345 } 3346 BIO_printf(bio, "---\n"); 3347 X509_free(peer); 3348 /* flush, or debugging output gets mixed with http response */ 3349 (void)BIO_flush(bio); 3350 } 3351 3352 # ifndef OPENSSL_NO_OCSP 3353 static int ocsp_resp_cb(SSL *s, void *arg) 3354 { 3355 const unsigned char *p; 3356 int len; 3357 OCSP_RESPONSE *rsp; 3358 len = SSL_get_tlsext_status_ocsp_resp(s, &p); 3359 BIO_puts(arg, "OCSP response: "); 3360 if (p == NULL) { 3361 BIO_puts(arg, "no response sent\n"); 3362 return 1; 3363 } 3364 rsp = d2i_OCSP_RESPONSE(NULL, &p, len); 3365 if (rsp == NULL) { 3366 BIO_puts(arg, "response parse error\n"); 3367 BIO_dump_indent(arg, (char *)p, len, 4); 3368 return 0; 3369 } 3370 BIO_puts(arg, "\n======================================\n"); 3371 OCSP_RESPONSE_print(arg, rsp, 0); 3372 BIO_puts(arg, "======================================\n"); 3373 OCSP_RESPONSE_free(rsp); 3374 return 1; 3375 } 3376 # endif 3377 3378 static int ldap_ExtendedResponse_parse(const char *buf, long rem) 3379 { 3380 const unsigned char *cur, *end; 3381 long len; 3382 int tag, xclass, inf, ret = -1; 3383 3384 cur = (const unsigned char *)buf; 3385 end = cur + rem; 3386 3387 /* 3388 * From RFC 4511: 3389 * 3390 * LDAPMessage ::= SEQUENCE { 3391 * messageID MessageID, 3392 * protocolOp CHOICE { 3393 * ... 3394 * extendedResp ExtendedResponse, 3395 * ... }, 3396 * controls [0] Controls OPTIONAL } 3397 * 3398 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE { 3399 * COMPONENTS OF LDAPResult, 3400 * responseName [10] LDAPOID OPTIONAL, 3401 * responseValue [11] OCTET STRING OPTIONAL } 3402 * 3403 * LDAPResult ::= SEQUENCE { 3404 * resultCode ENUMERATED { 3405 * success (0), 3406 * ... 3407 * other (80), 3408 * ... }, 3409 * matchedDN LDAPDN, 3410 * diagnosticMessage LDAPString, 3411 * referral [3] Referral OPTIONAL } 3412 */ 3413 3414 /* pull SEQUENCE */ 3415 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem); 3416 if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE || 3417 (rem = end - cur, len > rem)) { 3418 BIO_printf(bio_err, "Unexpected LDAP response\n"); 3419 goto end; 3420 } 3421 3422 rem = len; /* ensure that we don't overstep the SEQUENCE */ 3423 3424 /* pull MessageID */ 3425 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem); 3426 if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_INTEGER || 3427 (rem = end - cur, len > rem)) { 3428 BIO_printf(bio_err, "No MessageID\n"); 3429 goto end; 3430 } 3431 3432 cur += len; /* shall we check for MessageId match or just skip? */ 3433 3434 /* pull [APPLICATION 24] */ 3435 rem = end - cur; 3436 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem); 3437 if (inf != V_ASN1_CONSTRUCTED || xclass != V_ASN1_APPLICATION || 3438 tag != 24) { 3439 BIO_printf(bio_err, "Not ExtendedResponse\n"); 3440 goto end; 3441 } 3442 3443 /* pull resultCode */ 3444 rem = end - cur; 3445 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem); 3446 if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_ENUMERATED || len == 0 || 3447 (rem = end - cur, len > rem)) { 3448 BIO_printf(bio_err, "Not LDAPResult\n"); 3449 goto end; 3450 } 3451 3452 /* len should always be one, but just in case... */ 3453 for (ret = 0, inf = 0; inf < len; inf++) { 3454 ret <<= 8; 3455 ret |= cur[inf]; 3456 } 3457 /* There is more data, but we don't care... */ 3458 end: 3459 return ret; 3460 } 3461 3462 #endif /* OPENSSL_NO_SOCK */ 3463