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