2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
17 #include <openssl/e_os2.h>
19 #ifndef OPENSSL_NO_SOCK
22 * With IPv6, it looks like Digital has mixed up the proper order of
23 * recursive header file inclusion, resulting in the compiler complaining
24 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
25 * needed to have fileno() declared correctly... So let's define u_int
27 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
29 typedef unsigned int u_int
;
34 #include <openssl/x509.h>
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
37 #include <openssl/pem.h>
38 #include <openssl/rand.h>
39 #include <openssl/ocsp.h>
40 #include <openssl/bn.h>
41 #include <openssl/async.h>
42 #ifndef OPENSSL_NO_SRP
43 # include <openssl/srp.h>
46 # include <openssl/ct.h>
50 #include "internal/sockets.h"
52 #if defined(__has_feature)
53 # if __has_feature(memory_sanitizer)
54 # include <sanitizer/msan_interface.h>
59 #define BUFSIZZ 1024*8
60 #define S_CLIENT_IRC_READ_TIMEOUT 8
63 static int c_debug
= 0;
64 static int c_showcerts
= 0;
65 static char *keymatexportlabel
= NULL
;
66 static int keymatexportlen
= 20;
67 static BIO
*bio_c_out
= NULL
;
68 static int c_quiet
= 0;
69 static char *sess_out
= NULL
;
70 static SSL_SESSION
*psksess
= NULL
;
72 static void print_stuff(BIO
*berr
, SSL
*con
, int full
);
73 #ifndef OPENSSL_NO_OCSP
74 static int ocsp_resp_cb(SSL
*s
, void *arg
);
76 static int ldap_ExtendedResponse_parse(const char *buf
, long rem
);
78 static int saved_errno
;
80 static void save_errno(void)
86 static int restore_errno(void)
93 static void do_ssl_shutdown(SSL
*ssl
)
98 /* We only do unidirectional shutdown */
99 ret
= SSL_shutdown(ssl
);
101 switch (SSL_get_error(ssl
, ret
)) {
102 case SSL_ERROR_WANT_READ
:
103 case SSL_ERROR_WANT_WRITE
:
104 case SSL_ERROR_WANT_ASYNC
:
105 case SSL_ERROR_WANT_ASYNC_JOB
:
106 /* We just do busy waiting. Nothing clever */
114 /* Default PSK identity and key */
115 static char *psk_identity
= "Client_identity";
117 #ifndef OPENSSL_NO_PSK
118 static unsigned int psk_client_cb(SSL
*ssl
, const char *hint
, char *identity
,
119 unsigned int max_identity_len
,
121 unsigned int max_psk_len
)
128 BIO_printf(bio_c_out
, "psk_client_cb\n");
130 /* no ServerKeyExchange message */
132 BIO_printf(bio_c_out
,
133 "NULL received PSK identity hint, continuing anyway\n");
134 } else if (c_debug
) {
135 BIO_printf(bio_c_out
, "Received PSK identity hint '%s'\n", hint
);
139 * lookup PSK identity and PSK key based on the given identity hint here
141 ret
= BIO_snprintf(identity
, max_identity_len
, "%s", psk_identity
);
142 if (ret
< 0 || (unsigned int)ret
> max_identity_len
)
145 BIO_printf(bio_c_out
, "created identity '%s' len=%d\n", identity
,
148 /* convert the PSK key to binary */
149 key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
151 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
155 if (max_psk_len
> INT_MAX
|| key_len
> (long)max_psk_len
) {
157 "psk buffer of callback is too small (%d) for key (%ld)\n",
158 max_psk_len
, key_len
);
163 memcpy(psk
, key
, key_len
);
167 BIO_printf(bio_c_out
, "created PSK len=%ld\n", key_len
);
172 BIO_printf(bio_err
, "Error in PSK client callback\n");
177 const unsigned char tls13_aes128gcmsha256_id
[] = { 0x13, 0x01 };
178 const unsigned char tls13_aes256gcmsha384_id
[] = { 0x13, 0x02 };
180 static int psk_use_session_cb(SSL
*s
, const EVP_MD
*md
,
181 const unsigned char **id
, size_t *idlen
,
184 SSL_SESSION
*usesess
= NULL
;
185 const SSL_CIPHER
*cipher
= NULL
;
187 if (psksess
!= NULL
) {
188 SSL_SESSION_up_ref(psksess
);
192 unsigned char *key
= OPENSSL_hexstr2buf(psk_key
, &key_len
);
195 BIO_printf(bio_err
, "Could not convert PSK key '%s' to buffer\n",
200 /* We default to SHA-256 */
201 cipher
= SSL_CIPHER_find(s
, tls13_aes128gcmsha256_id
);
202 if (cipher
== NULL
) {
203 BIO_printf(bio_err
, "Error finding suitable ciphersuite\n");
208 usesess
= SSL_SESSION_new();
210 || !SSL_SESSION_set1_master_key(usesess
, key
, key_len
)
211 || !SSL_SESSION_set_cipher(usesess
, cipher
)
212 || !SSL_SESSION_set_protocol_version(usesess
, TLS1_3_VERSION
)) {
219 cipher
= SSL_SESSION_get0_cipher(usesess
);
223 if (md
!= NULL
&& SSL_CIPHER_get_handshake_digest(cipher
) != md
) {
224 /* PSK not usable, ignore it */
228 SSL_SESSION_free(usesess
);
231 *id
= (unsigned char *)psk_identity
;
232 *idlen
= strlen(psk_identity
);
238 SSL_SESSION_free(usesess
);
242 /* This is a context that we pass to callbacks */
243 typedef struct tlsextctx_st
{
248 static int ssl_servername_cb(SSL
*s
, int *ad
, void *arg
)
250 tlsextctx
*p
= (tlsextctx
*) arg
;
251 const char *hn
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
252 if (SSL_get_servername_type(s
) != -1)
253 p
->ack
= !SSL_session_reused(s
) && hn
!= NULL
;
255 BIO_printf(bio_err
, "Can't use SSL_get_servername\n");
257 return SSL_TLSEXT_ERR_OK
;
260 #ifndef OPENSSL_NO_SRP
262 /* This is a context that we pass to all callbacks */
263 typedef struct srp_arg_st
{
266 int msg
; /* copy from c_msg */
267 int debug
; /* copy from c_debug */
268 int amp
; /* allow more groups */
269 int strength
; /* minimal size for N */
272 # define SRP_NUMBER_ITERATIONS_FOR_PRIME 64
274 static int srp_Verify_N_and_g(const BIGNUM
*N
, const BIGNUM
*g
)
276 BN_CTX
*bn_ctx
= BN_CTX_new();
277 BIGNUM
*p
= BN_new();
278 BIGNUM
*r
= BN_new();
280 g
!= NULL
&& N
!= NULL
&& bn_ctx
!= NULL
&& BN_is_odd(N
) &&
281 BN_is_prime_ex(N
, SRP_NUMBER_ITERATIONS_FOR_PRIME
, bn_ctx
, NULL
) == 1 &&
282 p
!= NULL
&& BN_rshift1(p
, N
) &&
284 BN_is_prime_ex(p
, SRP_NUMBER_ITERATIONS_FOR_PRIME
, bn_ctx
, NULL
) == 1 &&
286 /* verify g^((N-1)/2) == -1 (mod N) */
287 BN_mod_exp(r
, g
, p
, N
, bn_ctx
) &&
288 BN_add_word(r
, 1) && BN_cmp(r
, N
) == 0;
297 * This callback is used here for two purposes:
298 * - extended debugging
299 * - making some primality tests for unknown groups
300 * The callback is only called for a non default group.
302 * An application does not need the call back at all if
303 * only the standard groups are used. In real life situations,
304 * client and server already share well known groups,
305 * thus there is no need to verify them.
306 * Furthermore, in case that a server actually proposes a group that
307 * is not one of those defined in RFC 5054, it is more appropriate
308 * to add the group to a static list and then compare since
309 * primality tests are rather cpu consuming.
312 static int ssl_srp_verify_param_cb(SSL
*s
, void *arg
)
314 SRP_ARG
*srp_arg
= (SRP_ARG
*)arg
;
315 BIGNUM
*N
= NULL
, *g
= NULL
;
317 if (((N
= SSL_get_srp_N(s
)) == NULL
) || ((g
= SSL_get_srp_g(s
)) == NULL
))
319 if (srp_arg
->debug
|| srp_arg
->msg
|| srp_arg
->amp
== 1) {
320 BIO_printf(bio_err
, "SRP parameters:\n");
321 BIO_printf(bio_err
, "\tN=");
322 BN_print(bio_err
, N
);
323 BIO_printf(bio_err
, "\n\tg=");
324 BN_print(bio_err
, g
);
325 BIO_printf(bio_err
, "\n");
328 if (SRP_check_known_gN_param(g
, N
))
331 if (srp_arg
->amp
== 1) {
334 "SRP param N and g are not known params, going to check deeper.\n");
337 * The srp_moregroups is a real debugging feature. Implementors
338 * should rather add the value to the known ones. The minimal size
339 * has already been tested.
341 if (BN_num_bits(g
) <= BN_BITS
&& srp_Verify_N_and_g(N
, g
))
344 BIO_printf(bio_err
, "SRP param N and g rejected.\n");
348 # define PWD_STRLEN 1024
350 static char *ssl_give_srp_client_pwd_cb(SSL
*s
, void *arg
)
352 SRP_ARG
*srp_arg
= (SRP_ARG
*)arg
;
353 char *pass
= app_malloc(PWD_STRLEN
+ 1, "SRP password buffer");
357 cb_tmp
.password
= (char *)srp_arg
->srppassin
;
358 cb_tmp
.prompt_info
= "SRP user";
359 if ((l
= password_callback(pass
, PWD_STRLEN
, 0, &cb_tmp
)) < 0) {
360 BIO_printf(bio_err
, "Can't read Password\n");
371 #ifndef OPENSSL_NO_NEXTPROTONEG
372 /* This the context that we pass to next_proto_cb */
373 typedef struct tlsextnextprotoctx_st
{
377 } tlsextnextprotoctx
;
379 static tlsextnextprotoctx next_proto
;
381 static int next_proto_cb(SSL
*s
, unsigned char **out
, unsigned char *outlen
,
382 const unsigned char *in
, unsigned int inlen
,
385 tlsextnextprotoctx
*ctx
= arg
;
388 /* We can assume that |in| is syntactically valid. */
390 BIO_printf(bio_c_out
, "Protocols advertised by server: ");
391 for (i
= 0; i
< inlen
;) {
393 BIO_write(bio_c_out
, ", ", 2);
394 BIO_write(bio_c_out
, &in
[i
+ 1], in
[i
]);
397 BIO_write(bio_c_out
, "\n", 1);
401 SSL_select_next_proto(out
, outlen
, in
, inlen
, ctx
->data
, ctx
->len
);
402 return SSL_TLSEXT_ERR_OK
;
404 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
406 static int serverinfo_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
407 const unsigned char *in
, size_t inlen
,
411 unsigned char ext_buf
[4 + 65536];
413 /* Reconstruct the type/len fields prior to extension data */
414 inlen
&= 0xffff; /* for formal memcmpy correctness */
415 ext_buf
[0] = (unsigned char)(ext_type
>> 8);
416 ext_buf
[1] = (unsigned char)(ext_type
);
417 ext_buf
[2] = (unsigned char)(inlen
>> 8);
418 ext_buf
[3] = (unsigned char)(inlen
);
419 memcpy(ext_buf
+ 4, in
, inlen
);
421 BIO_snprintf(pem_name
, sizeof(pem_name
), "SERVERINFO FOR EXTENSION %d",
423 PEM_write_bio(bio_c_out
, pem_name
, "", ext_buf
, 4 + inlen
);
428 * Hex decoder that tolerates optional whitespace. Returns number of bytes
429 * produced, advances inptr to end of input string.
431 static ossl_ssize_t
hexdecode(const char **inptr
, void *result
)
433 unsigned char **out
= (unsigned char **)result
;
434 const char *in
= *inptr
;
435 unsigned char *ret
= app_malloc(strlen(in
) / 2, "hexdecode");
436 unsigned char *cp
= ret
;
443 for (byte
= 0; *in
; ++in
) {
446 if (isspace(_UC(*in
)))
448 x
= OPENSSL_hexchar2int(*in
);
454 if ((nibble
^= 1) == 0) {
467 return cp
- (*out
= ret
);
471 * Decode unsigned 0..255, returns 1 on success, <= 0 on failure. Advances
472 * inptr to next field skipping leading whitespace.
474 static ossl_ssize_t
checked_uint8(const char **inptr
, void *out
)
476 uint8_t *result
= (uint8_t *)out
;
477 const char *in
= *inptr
;
483 v
= strtol(in
, &endp
, 10);
486 if (((v
== LONG_MIN
|| v
== LONG_MAX
) && e
== ERANGE
) ||
487 endp
== in
|| !isspace(_UC(*endp
)) ||
488 v
!= (*result
= (uint8_t) v
)) {
491 for (in
= endp
; isspace(_UC(*in
)); ++in
)
501 ossl_ssize_t (*parser
)(const char **, void *);
504 static int tlsa_import_rr(SSL
*con
, const char *rrdata
)
506 /* Not necessary to re-init these values; the "parsers" do that. */
507 static uint8_t usage
;
508 static uint8_t selector
;
509 static uint8_t mtype
;
510 static unsigned char *data
;
511 static struct tlsa_field tlsa_fields
[] = {
512 { &usage
, "usage", checked_uint8
},
513 { &selector
, "selector", checked_uint8
},
514 { &mtype
, "mtype", checked_uint8
},
515 { &data
, "data", hexdecode
},
518 struct tlsa_field
*f
;
520 const char *cp
= rrdata
;
521 ossl_ssize_t len
= 0;
523 for (f
= tlsa_fields
; f
->var
; ++f
) {
524 /* Returns number of bytes produced, advances cp to next field */
525 if ((len
= f
->parser(&cp
, f
->var
)) <= 0) {
526 BIO_printf(bio_err
, "%s: warning: bad TLSA %s field in: %s\n",
527 prog
, f
->name
, rrdata
);
531 /* The data field is last, so len is its length */
532 ret
= SSL_dane_tlsa_add(con
, usage
, selector
, mtype
, data
, len
);
536 ERR_print_errors(bio_err
);
537 BIO_printf(bio_err
, "%s: warning: unusable TLSA rrdata: %s\n",
542 ERR_print_errors(bio_err
);
543 BIO_printf(bio_err
, "%s: warning: error loading TLSA rrdata: %s\n",
550 static int tlsa_import_rrset(SSL
*con
, STACK_OF(OPENSSL_STRING
) *rrset
)
552 int num
= sk_OPENSSL_STRING_num(rrset
);
556 for (i
= 0; i
< num
; ++i
) {
557 char *rrdata
= sk_OPENSSL_STRING_value(rrset
, i
);
558 if (tlsa_import_rr(con
, rrdata
) > 0)
564 typedef enum OPTION_choice
{
565 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
566 OPT_4
, OPT_6
, OPT_HOST
, OPT_PORT
, OPT_CONNECT
, OPT_BIND
, OPT_UNIX
,
567 OPT_XMPPHOST
, OPT_VERIFY
, OPT_NAMEOPT
,
568 OPT_CERT
, OPT_CRL
, OPT_CRL_DOWNLOAD
, OPT_SESS_OUT
, OPT_SESS_IN
,
569 OPT_CERTFORM
, OPT_CRLFORM
, OPT_VERIFY_RET_ERROR
, OPT_VERIFY_QUIET
,
570 OPT_BRIEF
, OPT_PREXIT
, OPT_CRLF
, OPT_QUIET
, OPT_NBIO
,
571 OPT_SSL_CLIENT_ENGINE
, OPT_IGN_EOF
, OPT_NO_IGN_EOF
,
572 OPT_DEBUG
, OPT_TLSEXTDEBUG
, OPT_STATUS
, OPT_WDEBUG
,
573 OPT_MSG
, OPT_MSGFILE
, OPT_ENGINE
, OPT_TRACE
, OPT_SECURITY_DEBUG
,
574 OPT_SECURITY_DEBUG_VERBOSE
, OPT_SHOWCERTS
, OPT_NBIO_TEST
, OPT_STATE
,
575 OPT_PSK_IDENTITY
, OPT_PSK
, OPT_PSK_SESS
,
576 #ifndef OPENSSL_NO_SRP
577 OPT_SRPUSER
, OPT_SRPPASS
, OPT_SRP_STRENGTH
, OPT_SRP_LATEUSER
,
580 OPT_SSL3
, OPT_SSL_CONFIG
,
581 OPT_TLS1_3
, OPT_TLS1_2
, OPT_TLS1_1
, OPT_TLS1
, OPT_DTLS
, OPT_DTLS1
,
582 OPT_DTLS1_2
, OPT_SCTP
, OPT_TIMEOUT
, OPT_MTU
, OPT_KEYFORM
, OPT_PASS
,
583 OPT_CERT_CHAIN
, OPT_CAPATH
, OPT_NOCAPATH
, OPT_CHAINCAPATH
, OPT_VERIFYCAPATH
,
584 OPT_KEY
, OPT_RECONNECT
, OPT_BUILD_CHAIN
, OPT_CAFILE
, OPT_NOCAFILE
,
585 OPT_CHAINCAFILE
, OPT_VERIFYCAFILE
, OPT_NEXTPROTONEG
, OPT_ALPN
,
586 OPT_SERVERINFO
, OPT_STARTTLS
, OPT_SERVERNAME
, OPT_NOSERVERNAME
, OPT_ASYNC
,
587 OPT_USE_SRTP
, OPT_KEYMATEXPORT
, OPT_KEYMATEXPORTLEN
, OPT_PROTOHOST
,
588 OPT_MAXFRAGLEN
, OPT_MAX_SEND_FRAG
, OPT_SPLIT_SEND_FRAG
, OPT_MAX_PIPELINES
,
589 OPT_READ_BUF
, OPT_KEYLOG_FILE
, OPT_EARLY_DATA
, OPT_REQCAFILE
,
593 OPT_FALLBACKSCSV
, OPT_NOCMDS
, OPT_PROXY
, OPT_DANE_TLSA_DOMAIN
,
594 #ifndef OPENSSL_NO_CT
595 OPT_CT
, OPT_NOCT
, OPT_CTLOG_FILE
,
597 OPT_DANE_TLSA_RRDATA
, OPT_DANE_EE_NO_NAME
,
602 const OPTIONS s_client_options
[] = {
603 {"help", OPT_HELP
, '-', "Display this summary"},
604 {"host", OPT_HOST
, 's', "Use -connect instead"},
605 {"port", OPT_PORT
, 'p', "Use -connect instead"},
606 {"connect", OPT_CONNECT
, 's',
607 "TCP/IP where to connect (default is :" PORT
")"},
608 {"bind", OPT_BIND
, 's', "bind local address for connection"},
609 {"proxy", OPT_PROXY
, 's',
610 "Connect to via specified proxy to the real server"},
612 {"unix", OPT_UNIX
, 's', "Connect over the specified Unix-domain socket"},
614 {"4", OPT_4
, '-', "Use IPv4 only"},
616 {"6", OPT_6
, '-', "Use IPv6 only"},
618 {"verify", OPT_VERIFY
, 'p', "Turn on peer certificate verification"},
619 {"cert", OPT_CERT
, '<', "Certificate file to use, PEM format assumed"},
620 {"certform", OPT_CERTFORM
, 'F',
621 "Certificate format (PEM or DER) PEM default"},
622 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
623 {"key", OPT_KEY
, 's', "Private key file to use, if not in -cert file"},
624 {"keyform", OPT_KEYFORM
, 'E', "Key format (PEM, DER or engine) PEM default"},
625 {"pass", OPT_PASS
, 's', "Private key file pass phrase source"},
626 {"CApath", OPT_CAPATH
, '/', "PEM format directory of CA's"},
627 {"CAfile", OPT_CAFILE
, '<', "PEM format file of CA's"},
628 {"no-CAfile", OPT_NOCAFILE
, '-',
629 "Do not load the default certificates file"},
630 {"no-CApath", OPT_NOCAPATH
, '-',
631 "Do not load certificates from the default certificates directory"},
632 {"requestCAfile", OPT_REQCAFILE
, '<',
633 "PEM format file of CA names to send to the server"},
634 {"dane_tlsa_domain", OPT_DANE_TLSA_DOMAIN
, 's', "DANE TLSA base domain"},
635 {"dane_tlsa_rrdata", OPT_DANE_TLSA_RRDATA
, 's',
636 "DANE TLSA rrdata presentation form"},
637 {"dane_ee_no_namechecks", OPT_DANE_EE_NO_NAME
, '-',
638 "Disable name checks when matching DANE-EE(3) TLSA records"},
639 {"reconnect", OPT_RECONNECT
, '-',
640 "Drop and re-make the connection with the same Session-ID"},
641 {"showcerts", OPT_SHOWCERTS
, '-',
642 "Show all certificates sent by the server"},
643 {"debug", OPT_DEBUG
, '-', "Extra output"},
644 {"msg", OPT_MSG
, '-', "Show protocol messages"},
645 {"msgfile", OPT_MSGFILE
, '>',
646 "File to send output of -msg or -trace, instead of stdout"},
647 {"nbio_test", OPT_NBIO_TEST
, '-', "More ssl protocol testing"},
648 {"state", OPT_STATE
, '-', "Print the ssl states"},
649 {"crlf", OPT_CRLF
, '-', "Convert LF from terminal into CRLF"},
650 {"quiet", OPT_QUIET
, '-', "No s_client output"},
651 {"ign_eof", OPT_IGN_EOF
, '-', "Ignore input eof (default when -quiet)"},
652 {"no_ign_eof", OPT_NO_IGN_EOF
, '-', "Don't ignore input eof"},
653 {"starttls", OPT_STARTTLS
, 's',
654 "Use the appropriate STARTTLS command before starting TLS"},
655 {"xmpphost", OPT_XMPPHOST
, 's',
656 "Alias of -name option for \"-starttls xmpp[-server]\""},
658 {"sess_out", OPT_SESS_OUT
, '>', "File to write SSL session to"},
659 {"sess_in", OPT_SESS_IN
, '<', "File to read SSL session from"},
660 #ifndef OPENSSL_NO_SRTP
661 {"use_srtp", OPT_USE_SRTP
, 's',
662 "Offer SRTP key management with a colon-separated profile list"},
664 {"keymatexport", OPT_KEYMATEXPORT
, 's',
665 "Export keying material using label"},
666 {"keymatexportlen", OPT_KEYMATEXPORTLEN
, 'p',
667 "Export len bytes of keying material (default 20)"},
668 {"maxfraglen", OPT_MAXFRAGLEN
, 'p',
669 "Enable Maximum Fragment Length Negotiation (len values: 512, 1024, 2048 and 4096)"},
670 {"fallback_scsv", OPT_FALLBACKSCSV
, '-', "Send the fallback SCSV"},
671 {"name", OPT_PROTOHOST
, 's',
672 "Hostname to use for \"-starttls lmtp\", \"-starttls smtp\" or \"-starttls xmpp[-server]\""},
673 {"CRL", OPT_CRL
, '<', "CRL file to use"},
674 {"crl_download", OPT_CRL_DOWNLOAD
, '-', "Download CRL from distribution points"},
675 {"CRLform", OPT_CRLFORM
, 'F', "CRL format (PEM or DER) PEM is default"},
676 {"verify_return_error", OPT_VERIFY_RET_ERROR
, '-',
677 "Close connection on verification error"},
678 {"verify_quiet", OPT_VERIFY_QUIET
, '-', "Restrict verify output to errors"},
679 {"brief", OPT_BRIEF
, '-',
680 "Restrict output to brief summary of connection parameters"},
681 {"prexit", OPT_PREXIT
, '-',
682 "Print session information when the program exits"},
683 {"security_debug", OPT_SECURITY_DEBUG
, '-',
684 "Enable security debug messages"},
685 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE
, '-',
686 "Output more security debug output"},
687 {"cert_chain", OPT_CERT_CHAIN
, '<',
688 "Certificate chain file (in PEM format)"},
689 {"chainCApath", OPT_CHAINCAPATH
, '/',
690 "Use dir as certificate store path to build CA certificate chain"},
691 {"verifyCApath", OPT_VERIFYCAPATH
, '/',
692 "Use dir as certificate store path to verify CA certificate"},
693 {"build_chain", OPT_BUILD_CHAIN
, '-', "Build certificate chain"},
694 {"chainCAfile", OPT_CHAINCAFILE
, '<',
695 "CA file for certificate chain (PEM format)"},
696 {"verifyCAfile", OPT_VERIFYCAFILE
, '<',
697 "CA file for certificate verification (PEM format)"},
698 {"nocommands", OPT_NOCMDS
, '-', "Do not use interactive command letters"},
699 {"servername", OPT_SERVERNAME
, 's',
700 "Set TLS extension servername (SNI) in ClientHello (default)"},
701 {"noservername", OPT_NOSERVERNAME
, '-',
702 "Do not send the server name (SNI) extension in the ClientHello"},
703 {"tlsextdebug", OPT_TLSEXTDEBUG
, '-',
704 "Hex dump of all TLS extensions received"},
705 #ifndef OPENSSL_NO_OCSP
706 {"status", OPT_STATUS
, '-', "Request certificate status from server"},
708 {"serverinfo", OPT_SERVERINFO
, 's',
709 "types Send empty ClientHello extensions (comma-separated numbers)"},
710 {"alpn", OPT_ALPN
, 's',
711 "Enable ALPN extension, considering named protocols supported (comma-separated list)"},
712 {"async", OPT_ASYNC
, '-', "Support asynchronous operation"},
713 {"ssl_config", OPT_SSL_CONFIG
, 's', "Use specified configuration file"},
714 {"max_send_frag", OPT_MAX_SEND_FRAG
, 'p', "Maximum Size of send frames "},
715 {"split_send_frag", OPT_SPLIT_SEND_FRAG
, 'p',
716 "Size used to split data for encrypt pipelines"},
717 {"max_pipelines", OPT_MAX_PIPELINES
, 'p',
718 "Maximum number of encrypt/decrypt pipelines to be used"},
719 {"read_buf", OPT_READ_BUF
, 'p',
720 "Default read buffer size to be used for connections"},
724 #ifndef OPENSSL_NO_SSL3
725 {"ssl3", OPT_SSL3
, '-', "Just use SSLv3"},
727 #ifndef OPENSSL_NO_TLS1
728 {"tls1", OPT_TLS1
, '-', "Just use TLSv1"},
730 #ifndef OPENSSL_NO_TLS1_1
731 {"tls1_1", OPT_TLS1_1
, '-', "Just use TLSv1.1"},
733 #ifndef OPENSSL_NO_TLS1_2
734 {"tls1_2", OPT_TLS1_2
, '-', "Just use TLSv1.2"},
736 #ifndef OPENSSL_NO_TLS1_3
737 {"tls1_3", OPT_TLS1_3
, '-', "Just use TLSv1.3"},
739 #ifndef OPENSSL_NO_DTLS
740 {"dtls", OPT_DTLS
, '-', "Use any version of DTLS"},
741 {"timeout", OPT_TIMEOUT
, '-',
742 "Enable send/receive timeout on DTLS connections"},
743 {"mtu", OPT_MTU
, 'p', "Set the link layer MTU"},
745 #ifndef OPENSSL_NO_DTLS1
746 {"dtls1", OPT_DTLS1
, '-', "Just use DTLSv1"},
748 #ifndef OPENSSL_NO_DTLS1_2
749 {"dtls1_2", OPT_DTLS1_2
, '-', "Just use DTLSv1.2"},
751 #ifndef OPENSSL_NO_SCTP
752 {"sctp", OPT_SCTP
, '-', "Use SCTP"},
754 #ifndef OPENSSL_NO_SSL_TRACE
755 {"trace", OPT_TRACE
, '-', "Show trace output of protocol messages"},
758 {"wdebug", OPT_WDEBUG
, '-', "WATT-32 tcp debugging"},
760 {"nbio", OPT_NBIO
, '-', "Use non-blocking IO"},
761 {"psk_identity", OPT_PSK_IDENTITY
, 's', "PSK identity"},
762 {"psk", OPT_PSK
, 's', "PSK in hex (without 0x)"},
763 {"psk_session", OPT_PSK_SESS
, '<', "File to read PSK SSL session from"},
764 #ifndef OPENSSL_NO_SRP
765 {"srpuser", OPT_SRPUSER
, 's', "SRP authentication for 'user'"},
766 {"srppass", OPT_SRPPASS
, 's', "Password for 'user'"},
767 {"srp_lateuser", OPT_SRP_LATEUSER
, '-',
768 "SRP username into second ClientHello message"},
769 {"srp_moregroups", OPT_SRP_MOREGROUPS
, '-',
770 "Tolerate other than the known g N values."},
771 {"srp_strength", OPT_SRP_STRENGTH
, 'p', "Minimal length in bits for N"},
773 #ifndef OPENSSL_NO_NEXTPROTONEG
774 {"nextprotoneg", OPT_NEXTPROTONEG
, 's',
775 "Enable NPN extension, considering named protocols supported (comma-separated list)"},
777 #ifndef OPENSSL_NO_ENGINE
778 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
779 {"ssl_client_engine", OPT_SSL_CLIENT_ENGINE
, 's',
780 "Specify engine to be used for client certificate operations"},
782 #ifndef OPENSSL_NO_CT
783 {"ct", OPT_CT
, '-', "Request and parse SCTs (also enables OCSP stapling)"},
784 {"noct", OPT_NOCT
, '-', "Do not request or parse SCTs (default)"},
785 {"ctlogfile", OPT_CTLOG_FILE
, '<', "CT log list CONF file"},
787 {"keylogfile", OPT_KEYLOG_FILE
, '>', "Write TLS secrets to file"},
788 {"early_data", OPT_EARLY_DATA
, '<', "File to send as early data"},
789 {"force_pha", OPT_FORCE_PHA
, '-', "Force-enable post-handshake-authentication"},
790 {NULL
, OPT_EOF
, 0x00, NULL
}
793 typedef enum PROTOCOL_choice
{
812 static const OPT_PAIR services
[] = {
813 {"smtp", PROTO_SMTP
},
814 {"pop3", PROTO_POP3
},
815 {"imap", PROTO_IMAP
},
817 {"xmpp", PROTO_XMPP
},
818 {"xmpp-server", PROTO_XMPP_SERVER
},
819 {"telnet", PROTO_TELNET
},
821 {"mysql", PROTO_MYSQL
},
822 {"postgres", PROTO_POSTGRES
},
823 {"lmtp", PROTO_LMTP
},
824 {"nntp", PROTO_NNTP
},
825 {"sieve", PROTO_SIEVE
},
826 {"ldap", PROTO_LDAP
},
830 #define IS_INET_FLAG(o) \
831 (o == OPT_4 || o == OPT_6 || o == OPT_HOST || o == OPT_PORT || o == OPT_CONNECT)
832 #define IS_UNIX_FLAG(o) (o == OPT_UNIX)
834 #define IS_PROT_FLAG(o) \
835 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
836 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
838 /* Free |*dest| and optionally set it to a copy of |source|. */
839 static void freeandcopy(char **dest
, const char *source
)
844 *dest
= OPENSSL_strdup(source
);
847 static int new_session_cb(SSL
*s
, SSL_SESSION
*sess
)
850 if (sess_out
!= NULL
) {
851 BIO
*stmp
= BIO_new_file(sess_out
, "w");
854 BIO_printf(bio_err
, "Error writing session file %s\n", sess_out
);
856 PEM_write_bio_SSL_SESSION(stmp
, sess
);
862 * Session data gets dumped on connection for TLSv1.2 and below, and on
863 * arrival of the NewSessionTicket for TLSv1.3.
865 if (SSL_version(s
) == TLS1_3_VERSION
) {
866 BIO_printf(bio_c_out
,
867 "---\nPost-Handshake New Session Ticket arrived:\n");
868 SSL_SESSION_print(bio_c_out
, sess
);
869 BIO_printf(bio_c_out
, "---\n");
873 * We always return a "fail" response so that the session gets freed again
874 * because we haven't used the reference.
879 int s_client_main(int argc
, char **argv
)
882 EVP_PKEY
*key
= NULL
;
885 STACK_OF(X509
) *chain
= NULL
;
887 X509_VERIFY_PARAM
*vpm
= NULL
;
888 SSL_EXCERT
*exc
= NULL
;
889 SSL_CONF_CTX
*cctx
= NULL
;
890 STACK_OF(OPENSSL_STRING
) *ssl_args
= NULL
;
891 char *dane_tlsa_domain
= NULL
;
892 STACK_OF(OPENSSL_STRING
) *dane_tlsa_rrset
= NULL
;
893 int dane_ee_no_name
= 0;
894 STACK_OF(X509_CRL
) *crls
= NULL
;
895 const SSL_METHOD
*meth
= TLS_client_method();
896 const char *CApath
= NULL
, *CAfile
= NULL
;
897 char *cbuf
= NULL
, *sbuf
= NULL
;
898 char *mbuf
= NULL
, *proxystr
= NULL
, *connectstr
= NULL
, *bindstr
= NULL
;
899 char *cert_file
= NULL
, *key_file
= NULL
, *chain_file
= NULL
;
900 char *chCApath
= NULL
, *chCAfile
= NULL
, *host
= NULL
;
901 char *port
= OPENSSL_strdup(PORT
);
902 char *bindhost
= NULL
, *bindport
= NULL
;
903 char *passarg
= NULL
, *pass
= NULL
, *vfyCApath
= NULL
, *vfyCAfile
= NULL
;
904 char *ReqCAfile
= NULL
;
905 char *sess_in
= NULL
, *crl_file
= NULL
, *p
;
906 const char *protohost
= NULL
;
907 struct timeval timeout
, *timeoutp
;
908 fd_set readfds
, writefds
;
909 int noCApath
= 0, noCAfile
= 0;
910 int build_chain
= 0, cbuf_len
, cbuf_off
, cert_format
= FORMAT_PEM
;
911 int key_format
= FORMAT_PEM
, crlf
= 0, full_log
= 1, mbuf_len
= 0;
914 int reconnect
= 0, verify
= SSL_VERIFY_NONE
, vpmtouched
= 0;
915 int ret
= 1, in_init
= 1, i
, nbio_test
= 0, s
= -1, k
, width
, state
= 0;
916 int sbuf_len
, sbuf_off
, cmdletters
= 1;
917 int socket_family
= AF_UNSPEC
, socket_type
= SOCK_STREAM
, protocol
= 0;
918 int starttls_proto
= PROTO_OFF
, crl_format
= FORMAT_PEM
, crl_download
= 0;
919 int write_tty
, read_tty
, write_ssl
, read_ssl
, tty_on
, ssl_pending
;
920 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
923 int read_buf_len
= 0;
924 int fallback_scsv
= 0;
926 #ifndef OPENSSL_NO_DTLS
927 int enable_timeouts
= 0;
930 #ifndef OPENSSL_NO_ENGINE
931 ENGINE
*ssl_client_engine
= NULL
;
934 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
937 const char *servername
= NULL
;
938 int noservername
= 0;
939 const char *alpn_in
= NULL
;
940 tlsextctx tlsextcbp
= { NULL
, 0 };
941 const char *ssl_config
= NULL
;
942 #define MAX_SI_TYPES 100
943 unsigned short serverinfo_types
[MAX_SI_TYPES
];
944 int serverinfo_count
= 0, start
= 0, len
;
945 #ifndef OPENSSL_NO_NEXTPROTONEG
946 const char *next_proto_neg_in
= NULL
;
948 #ifndef OPENSSL_NO_SRP
949 char *srppass
= NULL
;
950 int srp_lateuser
= 0;
951 SRP_ARG srp_arg
= { NULL
, NULL
, 0, 0, 0, 1024 };
953 #ifndef OPENSSL_NO_SRTP
954 char *srtp_profiles
= NULL
;
956 #ifndef OPENSSL_NO_CT
957 char *ctlog_file
= NULL
;
958 int ct_validation
= 0;
960 int min_version
= 0, max_version
= 0, prot_opt
= 0, no_prot_opt
= 0;
962 unsigned int max_send_fragment
= 0;
963 unsigned int split_send_fragment
= 0, max_pipelines
= 0;
964 enum { use_inet
, use_unix
, use_unknown
} connect_type
= use_unknown
;
966 uint8_t maxfraglen
= 0;
967 int c_nbio
= 0, c_msg
= 0, c_ign_eof
= 0, c_brief
= 0;
968 int c_tlsextdebug
= 0;
969 #ifndef OPENSSL_NO_OCSP
970 int c_status_req
= 0;
972 BIO
*bio_c_msg
= NULL
;
973 const char *keylog_file
= NULL
, *early_data_file
= NULL
;
974 #ifndef OPENSSL_NO_DTLS
977 char *psksessf
= NULL
;
982 /* Known false-positive of MemorySanitizer. */
983 #if defined(__has_feature)
984 # if __has_feature(memory_sanitizer)
985 __msan_unpoison(&readfds
, sizeof(readfds
));
986 __msan_unpoison(&writefds
, sizeof(writefds
));
990 prog
= opt_progname(argv
[0]);
995 vpm
= X509_VERIFY_PARAM_new();
996 cctx
= SSL_CONF_CTX_new();
998 if (vpm
== NULL
|| cctx
== NULL
) {
999 BIO_printf(bio_err
, "%s: out of memory\n", prog
);
1003 cbuf
= app_malloc(BUFSIZZ
, "cbuf");
1004 sbuf
= app_malloc(BUFSIZZ
, "sbuf");
1005 mbuf
= app_malloc(BUFSIZZ
, "mbuf");
1007 SSL_CONF_CTX_set_flags(cctx
, SSL_CONF_FLAG_CLIENT
| SSL_CONF_FLAG_CMDLINE
);
1009 prog
= opt_init(argc
, argv
, s_client_options
);
1010 while ((o
= opt_next()) != OPT_EOF
) {
1011 /* Check for intermixing flags. */
1012 if (connect_type
== use_unix
&& IS_INET_FLAG(o
)) {
1014 "%s: Intermixed protocol flags (unix and internet domains)\n",
1018 if (connect_type
== use_inet
&& IS_UNIX_FLAG(o
)) {
1020 "%s: Intermixed protocol flags (internet and unix domains)\n",
1025 if (IS_PROT_FLAG(o
) && ++prot_opt
> 1) {
1026 BIO_printf(bio_err
, "Cannot supply multiple protocol flags\n");
1029 if (IS_NO_PROT_FLAG(o
))
1031 if (prot_opt
== 1 && no_prot_opt
) {
1033 "Cannot supply both a protocol flag and '-no_<prot>'\n");
1041 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
1044 opt_help(s_client_options
);
1048 connect_type
= use_inet
;
1049 socket_family
= AF_INET
;
1054 connect_type
= use_inet
;
1055 socket_family
= AF_INET6
;
1060 connect_type
= use_inet
;
1061 freeandcopy(&host
, opt_arg());
1064 connect_type
= use_inet
;
1065 freeandcopy(&port
, opt_arg());
1068 connect_type
= use_inet
;
1069 freeandcopy(&connectstr
, opt_arg());
1072 freeandcopy(&bindstr
, opt_arg());
1075 proxystr
= opt_arg();
1076 starttls_proto
= PROTO_CONNECT
;
1080 connect_type
= use_unix
;
1081 socket_family
= AF_UNIX
;
1082 freeandcopy(&host
, opt_arg());
1086 /* fall through, since this is an alias */
1088 protohost
= opt_arg();
1091 verify
= SSL_VERIFY_PEER
;
1092 verify_args
.depth
= atoi(opt_arg());
1094 BIO_printf(bio_err
, "verify depth is %d\n", verify_args
.depth
);
1097 cert_file
= opt_arg();
1100 if (!set_nameopt(opt_arg()))
1104 crl_file
= opt_arg();
1106 case OPT_CRL_DOWNLOAD
:
1110 sess_out
= opt_arg();
1113 sess_in
= opt_arg();
1116 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &cert_format
))
1120 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &crl_format
))
1123 case OPT_VERIFY_RET_ERROR
:
1124 verify_args
.return_error
= 1;
1126 case OPT_VERIFY_QUIET
:
1127 verify_args
.quiet
= 1;
1130 c_brief
= verify_args
.quiet
= c_quiet
= 1;
1133 if (ssl_args
== NULL
)
1134 ssl_args
= sk_OPENSSL_STRING_new_null();
1135 if (ssl_args
== NULL
1136 || !sk_OPENSSL_STRING_push(ssl_args
, opt_flag())
1137 || !sk_OPENSSL_STRING_push(ssl_args
, opt_arg())) {
1138 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1143 if (!opt_verify(o
, vpm
))
1148 if (!args_excert(o
, &exc
))
1158 c_quiet
= c_ign_eof
= 1;
1167 e
= setup_engine(opt_arg(), 1);
1169 case OPT_SSL_CLIENT_ENGINE
:
1170 #ifndef OPENSSL_NO_ENGINE
1171 ssl_client_engine
= ENGINE_by_id(opt_arg());
1172 if (ssl_client_engine
== NULL
) {
1173 BIO_printf(bio_err
, "Error getting client auth engine\n");
1185 case OPT_NO_IGN_EOF
:
1191 case OPT_TLSEXTDEBUG
:
1195 #ifndef OPENSSL_NO_OCSP
1208 bio_c_msg
= BIO_new_file(opt_arg(), "w");
1211 #ifndef OPENSSL_NO_SSL_TRACE
1215 case OPT_SECURITY_DEBUG
:
1218 case OPT_SECURITY_DEBUG_VERBOSE
:
1230 case OPT_PSK_IDENTITY
:
1231 psk_identity
= opt_arg();
1234 for (p
= psk_key
= opt_arg(); *p
; p
++) {
1235 if (isxdigit(_UC(*p
)))
1237 BIO_printf(bio_err
, "Not a hex number '%s'\n", psk_key
);
1242 psksessf
= opt_arg();
1244 #ifndef OPENSSL_NO_SRP
1246 srp_arg
.srplogin
= opt_arg();
1247 if (min_version
< TLS1_VERSION
)
1248 min_version
= TLS1_VERSION
;
1251 srppass
= opt_arg();
1252 if (min_version
< TLS1_VERSION
)
1253 min_version
= TLS1_VERSION
;
1255 case OPT_SRP_STRENGTH
:
1256 srp_arg
.strength
= atoi(opt_arg());
1257 BIO_printf(bio_err
, "SRP minimal length for N is %d\n",
1259 if (min_version
< TLS1_VERSION
)
1260 min_version
= TLS1_VERSION
;
1262 case OPT_SRP_LATEUSER
:
1264 if (min_version
< TLS1_VERSION
)
1265 min_version
= TLS1_VERSION
;
1267 case OPT_SRP_MOREGROUPS
:
1269 if (min_version
< TLS1_VERSION
)
1270 min_version
= TLS1_VERSION
;
1273 case OPT_SSL_CONFIG
:
1274 ssl_config
= opt_arg();
1277 min_version
= SSL3_VERSION
;
1278 max_version
= SSL3_VERSION
;
1281 min_version
= TLS1_3_VERSION
;
1282 max_version
= TLS1_3_VERSION
;
1285 min_version
= TLS1_2_VERSION
;
1286 max_version
= TLS1_2_VERSION
;
1289 min_version
= TLS1_1_VERSION
;
1290 max_version
= TLS1_1_VERSION
;
1293 min_version
= TLS1_VERSION
;
1294 max_version
= TLS1_VERSION
;
1297 #ifndef OPENSSL_NO_DTLS
1298 meth
= DTLS_client_method();
1299 socket_type
= SOCK_DGRAM
;
1304 #ifndef OPENSSL_NO_DTLS1
1305 meth
= DTLS_client_method();
1306 min_version
= DTLS1_VERSION
;
1307 max_version
= DTLS1_VERSION
;
1308 socket_type
= SOCK_DGRAM
;
1313 #ifndef OPENSSL_NO_DTLS1_2
1314 meth
= DTLS_client_method();
1315 min_version
= DTLS1_2_VERSION
;
1316 max_version
= DTLS1_2_VERSION
;
1317 socket_type
= SOCK_DGRAM
;
1322 #ifndef OPENSSL_NO_SCTP
1323 protocol
= IPPROTO_SCTP
;
1327 #ifndef OPENSSL_NO_DTLS
1328 enable_timeouts
= 1;
1332 #ifndef OPENSSL_NO_DTLS
1333 socket_mtu
= atol(opt_arg());
1336 case OPT_FALLBACKSCSV
:
1340 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &key_format
))
1344 passarg
= opt_arg();
1346 case OPT_CERT_CHAIN
:
1347 chain_file
= opt_arg();
1350 key_file
= opt_arg();
1361 case OPT_CHAINCAPATH
:
1362 chCApath
= opt_arg();
1364 case OPT_VERIFYCAPATH
:
1365 vfyCApath
= opt_arg();
1367 case OPT_BUILD_CHAIN
:
1371 ReqCAfile
= opt_arg();
1379 #ifndef OPENSSL_NO_CT
1386 case OPT_CTLOG_FILE
:
1387 ctlog_file
= opt_arg();
1390 case OPT_CHAINCAFILE
:
1391 chCAfile
= opt_arg();
1393 case OPT_VERIFYCAFILE
:
1394 vfyCAfile
= opt_arg();
1396 case OPT_DANE_TLSA_DOMAIN
:
1397 dane_tlsa_domain
= opt_arg();
1399 case OPT_DANE_TLSA_RRDATA
:
1400 if (dane_tlsa_rrset
== NULL
)
1401 dane_tlsa_rrset
= sk_OPENSSL_STRING_new_null();
1402 if (dane_tlsa_rrset
== NULL
||
1403 !sk_OPENSSL_STRING_push(dane_tlsa_rrset
, opt_arg())) {
1404 BIO_printf(bio_err
, "%s: Memory allocation failure\n", prog
);
1408 case OPT_DANE_EE_NO_NAME
:
1409 dane_ee_no_name
= 1;
1411 case OPT_NEXTPROTONEG
:
1412 #ifndef OPENSSL_NO_NEXTPROTONEG
1413 next_proto_neg_in
= opt_arg();
1417 alpn_in
= opt_arg();
1419 case OPT_SERVERINFO
:
1422 for (start
= 0, i
= 0; i
<= len
; ++i
) {
1423 if (i
== len
|| p
[i
] == ',') {
1424 serverinfo_types
[serverinfo_count
] = atoi(p
+ start
);
1425 if (++serverinfo_count
== MAX_SI_TYPES
)
1432 if (!opt_pair(opt_arg(), services
, &starttls_proto
))
1435 case OPT_SERVERNAME
:
1436 servername
= opt_arg();
1438 case OPT_NOSERVERNAME
:
1442 #ifndef OPENSSL_NO_SRTP
1443 srtp_profiles
= opt_arg();
1446 case OPT_KEYMATEXPORT
:
1447 keymatexportlabel
= opt_arg();
1449 case OPT_KEYMATEXPORTLEN
:
1450 keymatexportlen
= atoi(opt_arg());
1455 case OPT_MAXFRAGLEN
:
1456 len
= atoi(opt_arg());
1459 maxfraglen
= TLSEXT_max_fragment_length_512
;
1462 maxfraglen
= TLSEXT_max_fragment_length_1024
;
1465 maxfraglen
= TLSEXT_max_fragment_length_2048
;
1468 maxfraglen
= TLSEXT_max_fragment_length_4096
;
1472 "%s: Max Fragment Len %u is out of permitted values",
1477 case OPT_MAX_SEND_FRAG
:
1478 max_send_fragment
= atoi(opt_arg());
1480 case OPT_SPLIT_SEND_FRAG
:
1481 split_send_fragment
= atoi(opt_arg());
1483 case OPT_MAX_PIPELINES
:
1484 max_pipelines
= atoi(opt_arg());
1487 read_buf_len
= atoi(opt_arg());
1489 case OPT_KEYLOG_FILE
:
1490 keylog_file
= opt_arg();
1492 case OPT_EARLY_DATA
:
1493 early_data_file
= opt_arg();
1500 if (count4or6
>= 2) {
1501 BIO_printf(bio_err
, "%s: Can't use both -4 and -6\n", prog
);
1505 if (servername
!= NULL
) {
1507 "%s: Can't use -servername and -noservername together\n",
1511 if (dane_tlsa_domain
!= NULL
) {
1513 "%s: Can't use -dane_tlsa_domain and -noservername together\n",
1518 argc
= opt_num_rest();
1520 /* If there's a positional argument, it's the equivalent of
1522 * Don't allow -connect and a separate argument.
1524 if (connectstr
!= NULL
) {
1526 "%s: must not provide both -connect option and target parameter\n",
1530 connect_type
= use_inet
;
1531 freeandcopy(&connectstr
, *opt_rest());
1532 } else if (argc
!= 0) {
1536 #ifndef OPENSSL_NO_NEXTPROTONEG
1537 if (min_version
== TLS1_3_VERSION
&& next_proto_neg_in
!= NULL
) {
1538 BIO_printf(bio_err
, "Cannot supply -nextprotoneg with TLSv1.3\n");
1542 if (proxystr
!= NULL
) {
1544 char *tmp_host
= host
, *tmp_port
= port
;
1545 if (connectstr
== NULL
) {
1546 BIO_printf(bio_err
, "%s: -proxy requires use of -connect or target parameter\n", prog
);
1549 res
= BIO_parse_hostserv(proxystr
, &host
, &port
, BIO_PARSE_PRIO_HOST
);
1550 if (tmp_host
!= host
)
1551 OPENSSL_free(tmp_host
);
1552 if (tmp_port
!= port
)
1553 OPENSSL_free(tmp_port
);
1556 "%s: -proxy argument malformed or ambiguous\n", prog
);
1561 char *tmp_host
= host
, *tmp_port
= port
;
1562 if (connectstr
!= NULL
)
1563 res
= BIO_parse_hostserv(connectstr
, &host
, &port
,
1564 BIO_PARSE_PRIO_HOST
);
1565 if (tmp_host
!= host
)
1566 OPENSSL_free(tmp_host
);
1567 if (tmp_port
!= port
)
1568 OPENSSL_free(tmp_port
);
1571 "%s: -connect argument or target parameter malformed or ambiguous\n",
1577 if (bindstr
!= NULL
) {
1579 res
= BIO_parse_hostserv(bindstr
, &bindhost
, &bindport
,
1580 BIO_PARSE_PRIO_HOST
);
1583 "%s: -bind argument parameter malformed or ambiguous\n",
1590 if (socket_family
== AF_UNIX
&& socket_type
!= SOCK_STREAM
) {
1592 "Can't use unix sockets and datagrams together\n");
1597 #ifndef OPENSSL_NO_SCTP
1598 if (protocol
== IPPROTO_SCTP
) {
1599 if (socket_type
!= SOCK_DGRAM
) {
1600 BIO_printf(bio_err
, "Can't use -sctp without DTLS\n");
1603 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1604 socket_type
= SOCK_STREAM
;
1608 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1609 next_proto
.status
= -1;
1610 if (next_proto_neg_in
) {
1612 next_protos_parse(&next_proto
.len
, next_proto_neg_in
);
1613 if (next_proto
.data
== NULL
) {
1614 BIO_printf(bio_err
, "Error parsing -nextprotoneg argument\n");
1618 next_proto
.data
= NULL
;
1621 if (!app_passwd(passarg
, NULL
, &pass
, NULL
)) {
1622 BIO_printf(bio_err
, "Error getting password\n");
1626 if (key_file
== NULL
)
1627 key_file
= cert_file
;
1629 if (key_file
!= NULL
) {
1630 key
= load_key(key_file
, key_format
, 0, pass
, e
,
1631 "client certificate private key file");
1633 ERR_print_errors(bio_err
);
1638 if (cert_file
!= NULL
) {
1639 cert
= load_cert(cert_file
, cert_format
, "client certificate file");
1641 ERR_print_errors(bio_err
);
1646 if (chain_file
!= NULL
) {
1647 if (!load_certs(chain_file
, &chain
, FORMAT_PEM
, NULL
,
1648 "client certificate chain"))
1652 if (crl_file
!= NULL
) {
1654 crl
= load_crl(crl_file
, crl_format
);
1656 BIO_puts(bio_err
, "Error loading CRL\n");
1657 ERR_print_errors(bio_err
);
1660 crls
= sk_X509_CRL_new_null();
1661 if (crls
== NULL
|| !sk_X509_CRL_push(crls
, crl
)) {
1662 BIO_puts(bio_err
, "Error adding CRL\n");
1663 ERR_print_errors(bio_err
);
1669 if (!load_excert(&exc
))
1672 if (bio_c_out
== NULL
) {
1673 if (c_quiet
&& !c_debug
) {
1674 bio_c_out
= BIO_new(BIO_s_null());
1675 if (c_msg
&& bio_c_msg
== NULL
)
1676 bio_c_msg
= dup_bio_out(FORMAT_TEXT
);
1677 } else if (bio_c_out
== NULL
)
1678 bio_c_out
= dup_bio_out(FORMAT_TEXT
);
1680 #ifndef OPENSSL_NO_SRP
1681 if (!app_passwd(srppass
, NULL
, &srp_arg
.srppassin
, NULL
)) {
1682 BIO_printf(bio_err
, "Error getting password\n");
1687 ctx
= SSL_CTX_new(meth
);
1689 ERR_print_errors(bio_err
);
1693 SSL_CTX_clear_mode(ctx
, SSL_MODE_AUTO_RETRY
);
1696 ssl_ctx_security_debug(ctx
, sdebug
);
1698 if (!config_ctx(cctx
, ssl_args
, ctx
))
1701 if (ssl_config
!= NULL
) {
1702 if (SSL_CTX_config(ctx
, ssl_config
) == 0) {
1703 BIO_printf(bio_err
, "Error using configuration \"%s\"\n",
1705 ERR_print_errors(bio_err
);
1710 if (min_version
!= 0
1711 && SSL_CTX_set_min_proto_version(ctx
, min_version
) == 0)
1713 if (max_version
!= 0
1714 && SSL_CTX_set_max_proto_version(ctx
, max_version
) == 0)
1717 if (vpmtouched
&& !SSL_CTX_set1_param(ctx
, vpm
)) {
1718 BIO_printf(bio_err
, "Error setting verify params\n");
1719 ERR_print_errors(bio_err
);
1724 SSL_CTX_set_mode(ctx
, SSL_MODE_ASYNC
);
1727 if (max_send_fragment
> 0
1728 && !SSL_CTX_set_max_send_fragment(ctx
, max_send_fragment
)) {
1729 BIO_printf(bio_err
, "%s: Max send fragment size %u is out of permitted range\n",
1730 prog
, max_send_fragment
);
1734 if (split_send_fragment
> 0
1735 && !SSL_CTX_set_split_send_fragment(ctx
, split_send_fragment
)) {
1736 BIO_printf(bio_err
, "%s: Split send fragment size %u is out of permitted range\n",
1737 prog
, split_send_fragment
);
1741 if (max_pipelines
> 0
1742 && !SSL_CTX_set_max_pipelines(ctx
, max_pipelines
)) {
1743 BIO_printf(bio_err
, "%s: Max pipelines %u is out of permitted range\n",
1744 prog
, max_pipelines
);
1748 if (read_buf_len
> 0) {
1749 SSL_CTX_set_default_read_buffer_len(ctx
, read_buf_len
);
1753 && !SSL_CTX_set_tlsext_max_fragment_length(ctx
, maxfraglen
)) {
1755 "%s: Max Fragment Length code %u is out of permitted values"
1756 "\n", prog
, maxfraglen
);
1760 if (!ssl_load_stores(ctx
, vfyCApath
, vfyCAfile
, chCApath
, chCAfile
,
1761 crls
, crl_download
)) {
1762 BIO_printf(bio_err
, "Error loading store locations\n");
1763 ERR_print_errors(bio_err
);
1766 if (ReqCAfile
!= NULL
) {
1767 STACK_OF(X509_NAME
) *nm
= sk_X509_NAME_new_null();
1769 if (nm
== NULL
|| !SSL_add_file_cert_subjects_to_stack(nm
, ReqCAfile
)) {
1770 sk_X509_NAME_pop_free(nm
, X509_NAME_free
);
1771 BIO_printf(bio_err
, "Error loading CA names\n");
1772 ERR_print_errors(bio_err
);
1775 SSL_CTX_set0_CA_list(ctx
, nm
);
1777 #ifndef OPENSSL_NO_ENGINE
1778 if (ssl_client_engine
) {
1779 if (!SSL_CTX_set_client_cert_engine(ctx
, ssl_client_engine
)) {
1780 BIO_puts(bio_err
, "Error setting client auth engine\n");
1781 ERR_print_errors(bio_err
);
1782 ENGINE_free(ssl_client_engine
);
1785 ENGINE_free(ssl_client_engine
);
1789 #ifndef OPENSSL_NO_PSK
1790 if (psk_key
!= NULL
) {
1792 BIO_printf(bio_c_out
, "PSK key given, setting client callback\n");
1793 SSL_CTX_set_psk_client_callback(ctx
, psk_client_cb
);
1796 if (psksessf
!= NULL
) {
1797 BIO
*stmp
= BIO_new_file(psksessf
, "r");
1800 BIO_printf(bio_err
, "Can't open PSK session file %s\n", psksessf
);
1801 ERR_print_errors(bio_err
);
1804 psksess
= PEM_read_bio_SSL_SESSION(stmp
, NULL
, 0, NULL
);
1806 if (psksess
== NULL
) {
1807 BIO_printf(bio_err
, "Can't read PSK session file %s\n", psksessf
);
1808 ERR_print_errors(bio_err
);
1812 if (psk_key
!= NULL
|| psksess
!= NULL
)
1813 SSL_CTX_set_psk_use_session_callback(ctx
, psk_use_session_cb
);
1815 #ifndef OPENSSL_NO_SRTP
1816 if (srtp_profiles
!= NULL
) {
1817 /* Returns 0 on success! */
1818 if (SSL_CTX_set_tlsext_use_srtp(ctx
, srtp_profiles
) != 0) {
1819 BIO_printf(bio_err
, "Error setting SRTP profile\n");
1820 ERR_print_errors(bio_err
);
1827 ssl_ctx_set_excert(ctx
, exc
);
1829 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1830 if (next_proto
.data
!= NULL
)
1831 SSL_CTX_set_next_proto_select_cb(ctx
, next_proto_cb
, &next_proto
);
1835 unsigned char *alpn
= next_protos_parse(&alpn_len
, alpn_in
);
1838 BIO_printf(bio_err
, "Error parsing -alpn argument\n");
1841 /* Returns 0 on success! */
1842 if (SSL_CTX_set_alpn_protos(ctx
, alpn
, alpn_len
) != 0) {
1843 BIO_printf(bio_err
, "Error setting ALPN\n");
1849 for (i
= 0; i
< serverinfo_count
; i
++) {
1850 if (!SSL_CTX_add_client_custom_ext(ctx
,
1851 serverinfo_types
[i
],
1853 serverinfo_cli_parse_cb
, NULL
)) {
1855 "Warning: Unable to add custom extension %u, skipping\n",
1856 serverinfo_types
[i
]);
1861 SSL_CTX_set_info_callback(ctx
, apps_ssl_info_callback
);
1863 #ifndef OPENSSL_NO_CT
1864 /* Enable SCT processing, without early connection termination */
1865 if (ct_validation
&&
1866 !SSL_CTX_enable_ct(ctx
, SSL_CT_VALIDATION_PERMISSIVE
)) {
1867 ERR_print_errors(bio_err
);
1871 if (!ctx_set_ctlog_list_file(ctx
, ctlog_file
)) {
1872 if (ct_validation
) {
1873 ERR_print_errors(bio_err
);
1878 * If CT validation is not enabled, the log list isn't needed so don't
1879 * show errors or abort. We try to load it regardless because then we
1880 * can show the names of the logs any SCTs came from (SCTs may be seen
1881 * even with validation disabled).
1887 SSL_CTX_set_verify(ctx
, verify
, verify_callback
);
1889 if (!ctx_set_verify_locations(ctx
, CAfile
, CApath
, noCAfile
, noCApath
)) {
1890 ERR_print_errors(bio_err
);
1894 ssl_ctx_add_crls(ctx
, crls
, crl_download
);
1896 if (!set_cert_key_stuff(ctx
, cert
, key
, chain
, build_chain
))
1899 if (!noservername
) {
1900 tlsextcbp
.biodebug
= bio_err
;
1901 SSL_CTX_set_tlsext_servername_callback(ctx
, ssl_servername_cb
);
1902 SSL_CTX_set_tlsext_servername_arg(ctx
, &tlsextcbp
);
1904 # ifndef OPENSSL_NO_SRP
1905 if (srp_arg
.srplogin
) {
1906 if (!srp_lateuser
&& !SSL_CTX_set_srp_username(ctx
, srp_arg
.srplogin
)) {
1907 BIO_printf(bio_err
, "Unable to set SRP username\n");
1910 srp_arg
.msg
= c_msg
;
1911 srp_arg
.debug
= c_debug
;
1912 SSL_CTX_set_srp_cb_arg(ctx
, &srp_arg
);
1913 SSL_CTX_set_srp_client_pwd_callback(ctx
, ssl_give_srp_client_pwd_cb
);
1914 SSL_CTX_set_srp_strength(ctx
, srp_arg
.strength
);
1915 if (c_msg
|| c_debug
|| srp_arg
.amp
== 0)
1916 SSL_CTX_set_srp_verify_param_callback(ctx
,
1917 ssl_srp_verify_param_cb
);
1921 if (dane_tlsa_domain
!= NULL
) {
1922 if (SSL_CTX_dane_enable(ctx
) <= 0) {
1924 "%s: Error enabling DANE TLSA authentication.\n",
1926 ERR_print_errors(bio_err
);
1932 * In TLSv1.3 NewSessionTicket messages arrive after the handshake and can
1933 * come at any time. Therefore we use a callback to write out the session
1934 * when we know about it. This approach works for < TLSv1.3 as well.
1936 SSL_CTX_set_session_cache_mode(ctx
, SSL_SESS_CACHE_CLIENT
1937 | SSL_SESS_CACHE_NO_INTERNAL_STORE
);
1938 SSL_CTX_sess_set_new_cb(ctx
, new_session_cb
);
1940 if (set_keylog_file(ctx
, keylog_file
))
1948 SSL_force_post_handshake_auth(con
);
1950 if (sess_in
!= NULL
) {
1952 BIO
*stmp
= BIO_new_file(sess_in
, "r");
1954 BIO_printf(bio_err
, "Can't open session file %s\n", sess_in
);
1955 ERR_print_errors(bio_err
);
1958 sess
= PEM_read_bio_SSL_SESSION(stmp
, NULL
, 0, NULL
);
1961 BIO_printf(bio_err
, "Can't open session file %s\n", sess_in
);
1962 ERR_print_errors(bio_err
);
1965 if (!SSL_set_session(con
, sess
)) {
1966 BIO_printf(bio_err
, "Can't set session\n");
1967 ERR_print_errors(bio_err
);
1971 SSL_SESSION_free(sess
);
1975 SSL_set_mode(con
, SSL_MODE_SEND_FALLBACK_SCSV
);
1977 if (!noservername
&& (servername
!= NULL
|| dane_tlsa_domain
== NULL
)) {
1978 if (servername
== NULL
)
1979 servername
= (host
== NULL
) ? "localhost" : host
;
1980 if (!SSL_set_tlsext_host_name(con
, servername
)) {
1981 BIO_printf(bio_err
, "Unable to set TLS servername extension.\n");
1982 ERR_print_errors(bio_err
);
1987 if (dane_tlsa_domain
!= NULL
) {
1988 if (SSL_dane_enable(con
, dane_tlsa_domain
) <= 0) {
1989 BIO_printf(bio_err
, "%s: Error enabling DANE TLSA "
1990 "authentication.\n", prog
);
1991 ERR_print_errors(bio_err
);
1994 if (dane_tlsa_rrset
== NULL
) {
1995 BIO_printf(bio_err
, "%s: DANE TLSA authentication requires at "
1996 "least one -dane_tlsa_rrdata option.\n", prog
);
1999 if (tlsa_import_rrset(con
, dane_tlsa_rrset
) <= 0) {
2000 BIO_printf(bio_err
, "%s: Failed to import any TLSA "
2001 "records.\n", prog
);
2004 if (dane_ee_no_name
)
2005 SSL_dane_set_flags(con
, DANE_FLAG_NO_DANE_EE_NAMECHECKS
);
2006 } else if (dane_tlsa_rrset
!= NULL
) {
2007 BIO_printf(bio_err
, "%s: DANE TLSA authentication requires the "
2008 "-dane_tlsa_domain option.\n", prog
);
2013 if (init_client(&s
, host
, port
, bindhost
, bindport
, socket_family
,
2014 socket_type
, protocol
) == 0) {
2015 BIO_printf(bio_err
, "connect:errno=%d\n", get_last_socket_error());
2019 BIO_printf(bio_c_out
, "CONNECTED(%08X)\n", s
);
2022 if (!BIO_socket_nbio(s
, 1)) {
2023 ERR_print_errors(bio_err
);
2026 BIO_printf(bio_c_out
, "Turned on non blocking io\n");
2028 #ifndef OPENSSL_NO_DTLS
2030 union BIO_sock_info_u peer_info
;
2032 #ifndef OPENSSL_NO_SCTP
2033 if (protocol
== IPPROTO_SCTP
)
2034 sbio
= BIO_new_dgram_sctp(s
, BIO_NOCLOSE
);
2037 sbio
= BIO_new_dgram(s
, BIO_NOCLOSE
);
2039 if ((peer_info
.addr
= BIO_ADDR_new()) == NULL
) {
2040 BIO_printf(bio_err
, "memory allocation failure\n");
2044 if (!BIO_sock_info(s
, BIO_SOCK_INFO_ADDRESS
, &peer_info
)) {
2045 BIO_printf(bio_err
, "getsockname:errno=%d\n",
2046 get_last_socket_error());
2047 BIO_ADDR_free(peer_info
.addr
);
2052 (void)BIO_ctrl_set_connected(sbio
, peer_info
.addr
);
2053 BIO_ADDR_free(peer_info
.addr
);
2054 peer_info
.addr
= NULL
;
2056 if (enable_timeouts
) {
2058 timeout
.tv_usec
= DGRAM_RCV_TIMEOUT
;
2059 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT
, 0, &timeout
);
2062 timeout
.tv_usec
= DGRAM_SND_TIMEOUT
;
2063 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT
, 0, &timeout
);
2067 if (socket_mtu
< DTLS_get_link_min_mtu(con
)) {
2068 BIO_printf(bio_err
, "MTU too small. Must be at least %ld\n",
2069 DTLS_get_link_min_mtu(con
));
2073 SSL_set_options(con
, SSL_OP_NO_QUERY_MTU
);
2074 if (!DTLS_set_link_mtu(con
, socket_mtu
)) {
2075 BIO_printf(bio_err
, "Failed to set MTU\n");
2080 /* want to do MTU discovery */
2081 BIO_ctrl(sbio
, BIO_CTRL_DGRAM_MTU_DISCOVER
, 0, NULL
);
2084 #endif /* OPENSSL_NO_DTLS */
2085 sbio
= BIO_new_socket(s
, BIO_NOCLOSE
);
2090 test
= BIO_new(BIO_f_nbio_test());
2091 sbio
= BIO_push(test
, sbio
);
2095 BIO_set_callback(sbio
, bio_dump_callback
);
2096 BIO_set_callback_arg(sbio
, (char *)bio_c_out
);
2099 #ifndef OPENSSL_NO_SSL_TRACE
2101 SSL_set_msg_callback(con
, SSL_trace
);
2104 SSL_set_msg_callback(con
, msg_cb
);
2105 SSL_set_msg_callback_arg(con
, bio_c_msg
? bio_c_msg
: bio_c_out
);
2108 if (c_tlsextdebug
) {
2109 SSL_set_tlsext_debug_callback(con
, tlsext_cb
);
2110 SSL_set_tlsext_debug_arg(con
, bio_c_out
);
2112 #ifndef OPENSSL_NO_OCSP
2114 SSL_set_tlsext_status_type(con
, TLSEXT_STATUSTYPE_ocsp
);
2115 SSL_CTX_set_tlsext_status_cb(ctx
, ocsp_resp_cb
);
2116 SSL_CTX_set_tlsext_status_arg(ctx
, bio_c_out
);
2120 SSL_set_bio(con
, sbio
, sbio
);
2121 SSL_set_connect_state(con
);
2123 /* ok, lets connect */
2124 if (fileno_stdin() > SSL_get_fd(con
))
2125 width
= fileno_stdin() + 1;
2127 width
= SSL_get_fd(con
) + 1;
2140 switch ((PROTOCOL_CHOICE
) starttls_proto
) {
2147 * This is an ugly hack that does a lot of assumptions. We do
2148 * have to handle multi-line responses which may come in a single
2149 * packet or not. We therefore have to use BIO_gets() which does
2150 * need a buffering BIO. So during the initial chitchat we do
2151 * push a buffering BIO into the chain that is removed again
2152 * later on to not disturb the rest of the s_client operation.
2155 BIO
*fbio
= BIO_new(BIO_f_buffer());
2157 BIO_push(fbio
, sbio
);
2158 /* Wait for multi-line response to end from LMTP or SMTP */
2160 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2161 } while (mbuf_len
> 3 && mbuf
[3] == '-');
2162 if (protohost
== NULL
)
2163 protohost
= "mail.example.com";
2164 if (starttls_proto
== (int)PROTO_LMTP
)
2165 BIO_printf(fbio
, "LHLO %s\r\n", protohost
);
2167 BIO_printf(fbio
, "EHLO %s\r\n", protohost
);
2168 (void)BIO_flush(fbio
);
2170 * Wait for multi-line response to end LHLO LMTP or EHLO SMTP
2174 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2175 if (strstr(mbuf
, "STARTTLS"))
2177 } while (mbuf_len
> 3 && mbuf
[3] == '-');
2178 (void)BIO_flush(fbio
);
2183 "Didn't find STARTTLS in server response,"
2184 " trying anyway...\n");
2185 BIO_printf(sbio
, "STARTTLS\r\n");
2186 BIO_read(sbio
, sbuf
, BUFSIZZ
);
2191 BIO_read(sbio
, mbuf
, BUFSIZZ
);
2192 BIO_printf(sbio
, "STLS\r\n");
2193 mbuf_len
= BIO_read(sbio
, sbuf
, BUFSIZZ
);
2195 BIO_printf(bio_err
, "BIO_read failed\n");
2203 BIO
*fbio
= BIO_new(BIO_f_buffer());
2205 BIO_push(fbio
, sbio
);
2206 BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2207 /* STARTTLS command requires CAPABILITY... */
2208 BIO_printf(fbio
, ". CAPABILITY\r\n");
2209 (void)BIO_flush(fbio
);
2210 /* wait for multi-line CAPABILITY response */
2212 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2213 if (strstr(mbuf
, "STARTTLS"))
2216 while (mbuf_len
> 3 && mbuf
[0] != '.');
2217 (void)BIO_flush(fbio
);
2222 "Didn't find STARTTLS in server response,"
2223 " trying anyway...\n");
2224 BIO_printf(sbio
, ". STARTTLS\r\n");
2225 BIO_read(sbio
, sbuf
, BUFSIZZ
);
2230 BIO
*fbio
= BIO_new(BIO_f_buffer());
2232 BIO_push(fbio
, sbio
);
2233 /* wait for multi-line response to end from FTP */
2235 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2237 while (mbuf_len
> 3 && mbuf
[3] == '-');
2238 (void)BIO_flush(fbio
);
2241 BIO_printf(sbio
, "AUTH TLS\r\n");
2242 BIO_read(sbio
, sbuf
, BUFSIZZ
);
2246 case PROTO_XMPP_SERVER
:
2249 BIO_printf(sbio
, "<stream:stream "
2250 "xmlns:stream='http://etherx.jabber.org/streams' "
2251 "xmlns='jabber:%s' to='%s' version='1.0'>",
2252 starttls_proto
== PROTO_XMPP
? "client" : "server",
2253 protohost
? protohost
: host
);
2254 seen
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2256 BIO_printf(bio_err
, "BIO_read failed\n");
2261 (mbuf
, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")
2263 "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\""))
2265 seen
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2273 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
2274 seen
= BIO_read(sbio
, sbuf
, BUFSIZZ
);
2276 BIO_printf(bio_err
, "BIO_read failed\n");
2280 if (!strstr(sbuf
, "<proceed"))
2287 static const unsigned char tls_do
[] = {
2288 /* IAC DO START_TLS */
2291 static const unsigned char tls_will
[] = {
2292 /* IAC WILL START_TLS */
2295 static const unsigned char tls_follows
[] = {
2296 /* IAC SB START_TLS FOLLOWS IAC SE */
2297 255, 250, 46, 1, 255, 240
2301 /* Telnet server should demand we issue START_TLS */
2302 bytes
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2303 if (bytes
!= 3 || memcmp(mbuf
, tls_do
, 3) != 0)
2305 /* Agree to issue START_TLS and send the FOLLOWS sub-command */
2306 BIO_write(sbio
, tls_will
, 3);
2307 BIO_write(sbio
, tls_follows
, 6);
2308 (void)BIO_flush(sbio
);
2309 /* Telnet server also sent the FOLLOWS sub-command */
2310 bytes
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2311 if (bytes
!= 6 || memcmp(mbuf
, tls_follows
, 6) != 0)
2318 error_proto
, /* Wrong protocol, not even HTTP */
2319 error_connect
, /* CONNECT failed */
2321 } foundit
= error_connect
;
2322 BIO
*fbio
= BIO_new(BIO_f_buffer());
2324 BIO_push(fbio
, sbio
);
2325 BIO_printf(fbio
, "CONNECT %s HTTP/1.0\r\n\r\n", connectstr
);
2326 (void)BIO_flush(fbio
);
2328 * The first line is the HTTP response. According to RFC 7230,
2329 * it's formated exactly like this:
2331 * HTTP/d.d ddd Reason text\r\n
2333 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2334 if (mbuf_len
< (int)strlen("HTTP/1.0 200")) {
2336 "%s: HTTP CONNECT failed, insufficient response "
2337 "from proxy (got %d octets)\n", prog
, mbuf_len
);
2338 (void)BIO_flush(fbio
);
2343 if (mbuf
[8] != ' ') {
2345 "%s: HTTP CONNECT failed, incorrect response "
2346 "from proxy\n", prog
);
2347 foundit
= error_proto
;
2348 } else if (mbuf
[9] != '2') {
2349 BIO_printf(bio_err
, "%s: HTTP CONNECT failed: %s ", prog
,
2354 if (foundit
!= error_proto
) {
2355 /* Read past all following headers */
2357 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2358 } while (mbuf_len
> 2);
2360 (void)BIO_flush(fbio
);
2363 if (foundit
!= success
) {
2371 BIO
*fbio
= BIO_new(BIO_f_buffer());
2373 BIO_push(fbio
, sbio
);
2374 BIO_printf(fbio
, "STARTTLS\r\n");
2375 (void)BIO_flush(fbio
);
2376 width
= SSL_get_fd(con
) + 1;
2382 openssl_fdset(SSL_get_fd(con
), &readfds
);
2383 timeout
.tv_sec
= S_CLIENT_IRC_READ_TIMEOUT
;
2384 timeout
.tv_usec
= 0;
2386 * If the IRCd doesn't respond within
2387 * S_CLIENT_IRC_READ_TIMEOUT seconds, assume
2388 * it doesn't support STARTTLS. Many IRCds
2389 * will not give _any_ sort of response to a
2390 * STARTTLS command when it's not supported.
2392 if (!BIO_get_buffer_num_lines(fbio
)
2393 && !BIO_pending(fbio
)
2394 && !BIO_pending(sbio
)
2395 && select(width
, (void *)&readfds
, NULL
, NULL
,
2398 "Timeout waiting for response (%d seconds).\n",
2399 S_CLIENT_IRC_READ_TIMEOUT
);
2403 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2404 if (mbuf_len
< 1 || sscanf(mbuf
, "%*s %d", &numeric
) != 1)
2406 /* :example.net 451 STARTTLS :You have not registered */
2407 /* :example.net 421 STARTTLS :Unknown command */
2408 if ((numeric
== 451 || numeric
== 421)
2409 && strstr(mbuf
, "STARTTLS") != NULL
) {
2410 BIO_printf(bio_err
, "STARTTLS not supported: %s", mbuf
);
2413 if (numeric
== 691) {
2414 BIO_printf(bio_err
, "STARTTLS negotiation failed: ");
2415 ERR_print_errors(bio_err
);
2418 } while (numeric
!= 670);
2420 (void)BIO_flush(fbio
);
2423 if (numeric
!= 670) {
2424 BIO_printf(bio_err
, "Server does not support STARTTLS.\n");
2432 /* SSL request packet */
2433 static const unsigned char ssl_req
[] = {
2434 /* payload_length, sequence_id */
2435 0x20, 0x00, 0x00, 0x01,
2437 /* capability flags, CLIENT_SSL always set */
2438 0x85, 0xae, 0x7f, 0x00,
2439 /* max-packet size */
2440 0x00, 0x00, 0x00, 0x01,
2443 /* string[23] reserved (all [0]) */
2444 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2445 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2446 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2449 int ssl_flg
= 0x800;
2451 const unsigned char *packet
= (const unsigned char *)sbuf
;
2453 /* Receiving Initial Handshake packet. */
2454 bytes
= BIO_read(sbio
, (void *)packet
, BUFSIZZ
);
2456 BIO_printf(bio_err
, "BIO_read failed\n");
2458 /* Packet length[3], Packet number[1] + minimum payload[17] */
2459 } else if (bytes
< 21) {
2460 BIO_printf(bio_err
, "MySQL packet too short.\n");
2462 } else if (bytes
!= (4 + packet
[0] +
2464 (packet
[2] << 16))) {
2465 BIO_printf(bio_err
, "MySQL packet length does not match.\n");
2467 /* protocol version[1] */
2468 } else if (packet
[4] != 0xA) {
2470 "Only MySQL protocol version 10 is supported.\n");
2475 /* server version[string+NULL] */
2478 BIO_printf(bio_err
, "Cannot confirm server version. ");
2480 } else if (packet
[pos
++] == '\0') {
2485 /* make sure we have at least 15 bytes left in the packet */
2486 if (pos
+ 15 > bytes
) {
2488 "MySQL server handshake packet is broken.\n");
2492 pos
+= 12; /* skip over conn id[4] + SALT[8] */
2493 if (packet
[pos
++] != '\0') { /* verify filler */
2495 "MySQL packet is broken.\n");
2499 /* capability flags[2] */
2500 if (!((packet
[pos
] + (packet
[pos
+ 1] << 8)) & ssl_flg
)) {
2501 BIO_printf(bio_err
, "MySQL server does not support SSL.\n");
2505 /* Sending SSL Handshake packet. */
2506 BIO_write(sbio
, ssl_req
, sizeof(ssl_req
));
2507 (void)BIO_flush(sbio
);
2510 case PROTO_POSTGRES
:
2512 static const unsigned char ssl_request
[] = {
2513 /* Length SSLRequest */
2514 0, 0, 0, 8, 4, 210, 22, 47
2518 /* Send SSLRequest packet */
2519 BIO_write(sbio
, ssl_request
, 8);
2520 (void)BIO_flush(sbio
);
2522 /* Reply will be a single S if SSL is enabled */
2523 bytes
= BIO_read(sbio
, sbuf
, BUFSIZZ
);
2524 if (bytes
!= 1 || sbuf
[0] != 'S')
2531 BIO
*fbio
= BIO_new(BIO_f_buffer());
2533 BIO_push(fbio
, sbio
);
2534 BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2535 /* STARTTLS command requires CAPABILITIES... */
2536 BIO_printf(fbio
, "CAPABILITIES\r\n");
2537 (void)BIO_flush(fbio
);
2538 /* wait for multi-line CAPABILITIES response */
2540 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2541 if (strstr(mbuf
, "STARTTLS"))
2543 } while (mbuf_len
> 1 && mbuf
[0] != '.');
2544 (void)BIO_flush(fbio
);
2549 "Didn't find STARTTLS in server response,"
2550 " trying anyway...\n");
2551 BIO_printf(sbio
, "STARTTLS\r\n");
2552 mbuf_len
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2554 BIO_printf(bio_err
, "BIO_read failed\n");
2557 mbuf
[mbuf_len
] = '\0';
2558 if (strstr(mbuf
, "382") == NULL
) {
2559 BIO_printf(bio_err
, "STARTTLS failed: %s", mbuf
);
2567 BIO
*fbio
= BIO_new(BIO_f_buffer());
2569 BIO_push(fbio
, sbio
);
2570 /* wait for multi-line response to end from Sieve */
2572 mbuf_len
= BIO_gets(fbio
, mbuf
, BUFSIZZ
);
2574 * According to RFC 5804 § 1.7, capability
2575 * is case-insensitive, make it uppercase
2577 if (mbuf_len
> 1 && mbuf
[0] == '"') {
2578 make_uppercase(mbuf
);
2579 if (strncmp(mbuf
, "\"STARTTLS\"", 10) == 0)
2582 } while (mbuf_len
> 1 && mbuf
[0] == '"');
2583 (void)BIO_flush(fbio
);
2588 "Didn't find STARTTLS in server response,"
2589 " trying anyway...\n");
2590 BIO_printf(sbio
, "STARTTLS\r\n");
2591 mbuf_len
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2593 BIO_printf(bio_err
, "BIO_read failed\n");
2596 mbuf
[mbuf_len
] = '\0';
2598 BIO_printf(bio_err
, "STARTTLS failed: %s", mbuf
);
2602 * According to RFC 5804 § 2.2, response codes are case-
2603 * insensitive, make it uppercase but preserve the response.
2605 strncpy(sbuf
, mbuf
, 2);
2606 make_uppercase(sbuf
);
2607 if (strncmp(sbuf
, "OK", 2) != 0) {
2608 BIO_printf(bio_err
, "STARTTLS not supported: %s", mbuf
);
2615 /* StartTLS Operation according to RFC 4511 */
2616 static char ldap_tls_genconf
[] = "asn1=SEQUENCE:LDAPMessage\n"
2618 "messageID=INTEGER:1\n"
2619 "extendedReq=EXPLICIT:23A,IMPLICIT:0C,"
2620 "FORMAT:ASCII,OCT:1.3.6.1.4.1.1466.20037\n";
2622 char *genstr
= NULL
;
2624 ASN1_TYPE
*atyp
= NULL
;
2625 BIO
*ldapbio
= BIO_new(BIO_s_mem());
2626 CONF
*cnf
= NCONF_new(NULL
);
2632 BIO_puts(ldapbio
, ldap_tls_genconf
);
2633 if (NCONF_load_bio(cnf
, ldapbio
, &errline
) <= 0) {
2637 BIO_printf(bio_err
, "NCONF_load_bio failed\n");
2640 BIO_printf(bio_err
, "Error on line %ld\n", errline
);
2645 genstr
= NCONF_get_string(cnf
, "default", "asn1");
2646 if (genstr
== NULL
) {
2648 BIO_printf(bio_err
, "NCONF_get_string failed\n");
2651 atyp
= ASN1_generate_nconf(genstr
, cnf
);
2654 BIO_printf(bio_err
, "ASN1_generate_nconf failed\n");
2659 /* Send SSLRequest packet */
2660 BIO_write(sbio
, atyp
->value
.sequence
->data
,
2661 atyp
->value
.sequence
->length
);
2662 (void)BIO_flush(sbio
);
2663 ASN1_TYPE_free(atyp
);
2665 mbuf_len
= BIO_read(sbio
, mbuf
, BUFSIZZ
);
2667 BIO_printf(bio_err
, "BIO_read failed\n");
2670 result
= ldap_ExtendedResponse_parse(mbuf
, mbuf_len
);
2672 BIO_printf(bio_err
, "ldap_ExtendedResponse_parse failed\n");
2674 } else if (result
> 0) {
2675 BIO_printf(bio_err
, "STARTTLS failed, LDAP Result Code: %i\n",
2684 if (early_data_file
!= NULL
2685 && ((SSL_get0_session(con
) != NULL
2686 && SSL_SESSION_get_max_early_data(SSL_get0_session(con
)) > 0)
2688 && SSL_SESSION_get_max_early_data(psksess
) > 0))) {
2689 BIO
*edfile
= BIO_new_file(early_data_file
, "r");
2690 size_t readbytes
, writtenbytes
;
2693 if (edfile
== NULL
) {
2694 BIO_printf(bio_err
, "Cannot open early data file\n");
2699 if (!BIO_read_ex(edfile
, cbuf
, BUFSIZZ
, &readbytes
))
2702 while (!SSL_write_early_data(con
, cbuf
, readbytes
, &writtenbytes
)) {
2703 switch (SSL_get_error(con
, 0)) {
2704 case SSL_ERROR_WANT_WRITE
:
2705 case SSL_ERROR_WANT_ASYNC
:
2706 case SSL_ERROR_WANT_READ
:
2707 /* Just keep trying - busy waiting */
2710 BIO_printf(bio_err
, "Error writing early data\n");
2712 ERR_print_errors(bio_err
);
2725 if (SSL_is_dtls(con
) && DTLSv1_get_timeout(con
, &timeout
))
2726 timeoutp
= &timeout
;
2730 if (!SSL_is_init_finished(con
) && SSL_total_renegotiations(con
) == 0
2731 && SSL_get_key_update_type(con
) == SSL_KEY_UPDATE_NONE
) {
2740 BIO_puts(bio_err
, "CONNECTION ESTABLISHED\n");
2741 print_ssl_summary(con
);
2744 print_stuff(bio_c_out
, con
, full_log
);
2748 if (starttls_proto
) {
2749 BIO_write(bio_err
, mbuf
, mbuf_len
);
2750 /* We don't need to know any more */
2752 starttls_proto
= PROTO_OFF
;
2757 BIO_printf(bio_c_out
,
2758 "drop connection and then reconnect\n");
2759 do_ssl_shutdown(con
);
2760 SSL_set_connect_state(con
);
2761 BIO_closesocket(SSL_get_fd(con
));
2767 ssl_pending
= read_ssl
&& SSL_has_pending(con
);
2770 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2773 * Note that select() returns when read _would not block_,
2774 * and EOF satisfies that. To avoid a CPU-hogging loop,
2775 * set the flag so we exit.
2777 if (read_tty
&& !at_eof
)
2778 openssl_fdset(fileno_stdin(), &readfds
);
2779 #if !defined(OPENSSL_SYS_VMS)
2781 openssl_fdset(fileno_stdout(), &writefds
);
2785 openssl_fdset(SSL_get_fd(con
), &readfds
);
2787 openssl_fdset(SSL_get_fd(con
), &writefds
);
2789 if (!tty_on
|| !write_tty
) {
2791 openssl_fdset(SSL_get_fd(con
), &readfds
);
2793 openssl_fdset(SSL_get_fd(con
), &writefds
);
2798 * Note: under VMS with SOCKETSHR the second parameter is
2799 * currently of type (int *) whereas under other systems it is
2800 * (void *) if you don't have a cast it will choke the compiler:
2801 * if you do have a cast then you can either go for (int *) or
2804 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2806 * Under Windows/DOS we make the assumption that we can always
2807 * write to the tty: therefore if we need to write to the tty we
2808 * just fall through. Otherwise we timeout the select every
2809 * second and see if there are any keypresses. Note: this is a
2810 * hack, in a proper Windows application we wouldn't do this.
2817 i
= select(width
, (void *)&readfds
, (void *)&writefds
,
2819 if (!i
&& (!has_stdin_waiting() || !read_tty
))
2822 i
= select(width
, (void *)&readfds
, (void *)&writefds
,
2826 i
= select(width
, (void *)&readfds
, (void *)&writefds
,
2830 BIO_printf(bio_err
, "bad select %d\n",
2831 get_last_socket_error());
2836 if (SSL_is_dtls(con
) && DTLSv1_handle_timeout(con
) > 0)
2837 BIO_printf(bio_err
, "TIMEOUT occurred\n");
2839 if (!ssl_pending
&& FD_ISSET(SSL_get_fd(con
), &writefds
)) {
2840 k
= SSL_write(con
, &(cbuf
[cbuf_off
]), (unsigned int)cbuf_len
);
2841 switch (SSL_get_error(con
, k
)) {
2842 case SSL_ERROR_NONE
:
2847 /* we have done a write(con,NULL,0); */
2848 if (cbuf_len
<= 0) {
2851 } else { /* if (cbuf_len > 0) */
2857 case SSL_ERROR_WANT_WRITE
:
2858 BIO_printf(bio_c_out
, "write W BLOCK\n");
2862 case SSL_ERROR_WANT_ASYNC
:
2863 BIO_printf(bio_c_out
, "write A BLOCK\n");
2864 wait_for_async(con
);
2868 case SSL_ERROR_WANT_READ
:
2869 BIO_printf(bio_c_out
, "write R BLOCK\n");
2874 case SSL_ERROR_WANT_X509_LOOKUP
:
2875 BIO_printf(bio_c_out
, "write X BLOCK\n");
2877 case SSL_ERROR_ZERO_RETURN
:
2878 if (cbuf_len
!= 0) {
2879 BIO_printf(bio_c_out
, "shutdown\n");
2888 case SSL_ERROR_SYSCALL
:
2889 if ((k
!= 0) || (cbuf_len
!= 0)) {
2890 BIO_printf(bio_err
, "write:errno=%d\n",
2891 get_last_socket_error());
2898 case SSL_ERROR_WANT_ASYNC_JOB
:
2899 /* This shouldn't ever happen in s_client - treat as an error */
2901 ERR_print_errors(bio_err
);
2905 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VMS)
2906 /* Assume Windows/DOS/BeOS can always write */
2907 else if (!ssl_pending
&& write_tty
)
2909 else if (!ssl_pending
&& FD_ISSET(fileno_stdout(), &writefds
))
2912 #ifdef CHARSET_EBCDIC
2913 ascii2ebcdic(&(sbuf
[sbuf_off
]), &(sbuf
[sbuf_off
]), sbuf_len
);
2915 i
= raw_write_stdout(&(sbuf
[sbuf_off
]), sbuf_len
);
2918 BIO_printf(bio_c_out
, "DONE\n");
2925 if (sbuf_len
<= 0) {
2929 } else if (ssl_pending
|| FD_ISSET(SSL_get_fd(con
), &readfds
)) {
2934 SSL_renegotiate(con
);
2939 k
= SSL_read(con
, sbuf
, 1024 /* BUFSIZZ */ );
2941 switch (SSL_get_error(con
, k
)) {
2942 case SSL_ERROR_NONE
:
2951 case SSL_ERROR_WANT_ASYNC
:
2952 BIO_printf(bio_c_out
, "read A BLOCK\n");
2953 wait_for_async(con
);
2956 if ((read_tty
== 0) && (write_ssl
== 0))
2959 case SSL_ERROR_WANT_WRITE
:
2960 BIO_printf(bio_c_out
, "read W BLOCK\n");
2964 case SSL_ERROR_WANT_READ
:
2965 BIO_printf(bio_c_out
, "read R BLOCK\n");
2968 if ((read_tty
== 0) && (write_ssl
== 0))
2971 case SSL_ERROR_WANT_X509_LOOKUP
:
2972 BIO_printf(bio_c_out
, "read X BLOCK\n");
2974 case SSL_ERROR_SYSCALL
:
2975 ret
= get_last_socket_error();
2977 BIO_puts(bio_err
, "CONNECTION CLOSED BY SERVER\n");
2979 BIO_printf(bio_err
, "read:errno=%d\n", ret
);
2981 case SSL_ERROR_ZERO_RETURN
:
2982 BIO_printf(bio_c_out
, "closed\n");
2985 case SSL_ERROR_WANT_ASYNC_JOB
:
2986 /* This shouldn't ever happen in s_client. Treat as an error */
2988 ERR_print_errors(bio_err
);
2992 /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */
2993 #if defined(OPENSSL_SYS_MSDOS)
2994 else if (has_stdin_waiting())
2996 else if (FD_ISSET(fileno_stdin(), &readfds
))
3002 i
= raw_read_stdin(cbuf
, BUFSIZZ
/ 2);
3004 /* both loops are skipped when i <= 0 */
3005 for (j
= 0; j
< i
; j
++)
3006 if (cbuf
[j
] == '\n')
3008 for (j
= i
- 1; j
>= 0; j
--) {
3009 cbuf
[j
+ lf_num
] = cbuf
[j
];
3010 if (cbuf
[j
] == '\n') {
3013 cbuf
[j
+ lf_num
] = '\r';
3016 assert(lf_num
== 0);
3018 i
= raw_read_stdin(cbuf
, BUFSIZZ
);
3019 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
3024 if ((!c_ign_eof
) && ((i
<= 0) || (cbuf
[0] == 'Q' && cmdletters
))) {
3025 BIO_printf(bio_err
, "DONE\n");
3030 if ((!c_ign_eof
) && (cbuf
[0] == 'R' && cmdletters
)) {
3031 BIO_printf(bio_err
, "RENEGOTIATING\n");
3032 SSL_renegotiate(con
);
3036 if (!c_ign_eof
&& (cbuf
[0] == 'K' || cbuf
[0] == 'k' )
3038 BIO_printf(bio_err
, "KEYUPDATE\n");
3040 cbuf
[0] == 'K' ? SSL_KEY_UPDATE_REQUESTED
3041 : SSL_KEY_UPDATE_NOT_REQUESTED
);
3044 #ifndef OPENSSL_NO_HEARTBEATS
3045 else if ((!c_ign_eof
) && (cbuf
[0] == 'B' && cmdletters
)) {
3046 BIO_printf(bio_err
, "HEARTBEATING\n");
3054 #ifdef CHARSET_EBCDIC
3055 ebcdic2ascii(cbuf
, cbuf
, i
);
3067 print_stuff(bio_c_out
, con
, full_log
);
3068 do_ssl_shutdown(con
);
3071 * If we ended with an alert being sent, but still with data in the
3072 * network buffer to be read, then calling BIO_closesocket() will
3073 * result in a TCP-RST being sent. On some platforms (notably
3074 * Windows) then this will result in the peer immediately abandoning
3075 * the connection including any buffered alert data before it has
3076 * had a chance to be read. Shutting down the sending side first,
3077 * and then closing the socket sends TCP-FIN first followed by
3078 * TCP-RST. This seems to allow the peer to read the alert data.
3080 shutdown(SSL_get_fd(con
), 1); /* SHUT_WR */
3082 * We just said we have nothing else to say, but it doesn't mean that
3083 * the other side has nothing. It's even recommended to consume incoming
3084 * data. [In testing context this ensures that alerts are passed on...]
3087 timeout
.tv_usec
= 500000; /* some extreme round-trip */
3090 openssl_fdset(s
, &readfds
);
3091 } while (select(s
+ 1, &readfds
, NULL
, NULL
, &timeout
) > 0
3092 && BIO_read(sbio
, sbuf
, BUFSIZZ
) > 0);
3094 BIO_closesocket(SSL_get_fd(con
));
3098 print_stuff(bio_c_out
, con
, 1);
3101 SSL_SESSION_free(psksess
);
3102 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3103 OPENSSL_free(next_proto
.data
);
3106 set_keylog_file(NULL
, NULL
);
3108 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
3110 sk_X509_pop_free(chain
, X509_free
);
3112 #ifndef OPENSSL_NO_SRP
3113 OPENSSL_free(srp_arg
.srppassin
);
3115 OPENSSL_free(connectstr
);
3116 OPENSSL_free(bindstr
);
3119 X509_VERIFY_PARAM_free(vpm
);
3120 ssl_excert_free(exc
);
3121 sk_OPENSSL_STRING_free(ssl_args
);
3122 sk_OPENSSL_STRING_free(dane_tlsa_rrset
);
3123 SSL_CONF_CTX_free(cctx
);
3124 OPENSSL_clear_free(cbuf
, BUFSIZZ
);
3125 OPENSSL_clear_free(sbuf
, BUFSIZZ
);
3126 OPENSSL_clear_free(mbuf
, BUFSIZZ
);
3128 BIO_free(bio_c_out
);
3130 BIO_free(bio_c_msg
);
3135 static void print_stuff(BIO
*bio
, SSL
*s
, int full
)
3139 const SSL_CIPHER
*c
;
3140 int i
, istls13
= (SSL_version(s
) == TLS1_3_VERSION
);
3142 #ifndef OPENSSL_NO_COMP
3143 const COMP_METHOD
*comp
, *expansion
;
3145 unsigned char *exportedkeymat
;
3146 #ifndef OPENSSL_NO_CT
3147 const SSL_CTX
*ctx
= SSL_get_SSL_CTX(s
);
3151 int got_a_chain
= 0;
3153 sk
= SSL_get_peer_cert_chain(s
);
3157 BIO_printf(bio
, "---\nCertificate chain\n");
3158 for (i
= 0; i
< sk_X509_num(sk
); i
++) {
3159 BIO_printf(bio
, "%2d s:", i
);
3160 X509_NAME_print_ex(bio
, X509_get_subject_name(sk_X509_value(sk
, i
)), 0, get_nameopt());
3161 BIO_puts(bio
, "\n");
3162 BIO_printf(bio
, " i:");
3163 X509_NAME_print_ex(bio
, X509_get_issuer_name(sk_X509_value(sk
, i
)), 0, get_nameopt());
3164 BIO_puts(bio
, "\n");
3166 PEM_write_bio_X509(bio
, sk_X509_value(sk
, i
));
3170 BIO_printf(bio
, "---\n");
3171 peer
= SSL_get_peer_certificate(s
);
3173 BIO_printf(bio
, "Server certificate\n");
3175 /* Redundant if we showed the whole chain */
3176 if (!(c_showcerts
&& got_a_chain
))
3177 PEM_write_bio_X509(bio
, peer
);
3178 dump_cert_text(bio
, peer
);
3180 BIO_printf(bio
, "no peer certificate available\n");
3182 print_ca_names(bio
, s
);
3184 ssl_print_sigalgs(bio
, s
);
3185 ssl_print_tmp_key(bio
, s
);
3187 #ifndef OPENSSL_NO_CT
3189 * When the SSL session is anonymous, or resumed via an abbreviated
3190 * handshake, no SCTs are provided as part of the handshake. While in
3191 * a resumed session SCTs may be present in the session's certificate,
3192 * no callbacks are invoked to revalidate these, and in any case that
3193 * set of SCTs may be incomplete. Thus it makes little sense to
3194 * attempt to display SCTs from a resumed session's certificate, and of
3195 * course none are associated with an anonymous peer.
3197 if (peer
!= NULL
&& !SSL_session_reused(s
) && SSL_ct_is_enabled(s
)) {
3198 const STACK_OF(SCT
) *scts
= SSL_get0_peer_scts(s
);
3199 int sct_count
= scts
!= NULL
? sk_SCT_num(scts
) : 0;
3201 BIO_printf(bio
, "---\nSCTs present (%i)\n", sct_count
);
3202 if (sct_count
> 0) {
3203 const CTLOG_STORE
*log_store
= SSL_CTX_get0_ctlog_store(ctx
);
3205 BIO_printf(bio
, "---\n");
3206 for (i
= 0; i
< sct_count
; ++i
) {
3207 SCT
*sct
= sk_SCT_value(scts
, i
);
3209 BIO_printf(bio
, "SCT validation status: %s\n",
3210 SCT_validation_status_string(sct
));
3211 SCT_print(sct
, bio
, 0, log_store
);
3212 if (i
< sct_count
- 1)
3213 BIO_printf(bio
, "\n---\n");
3215 BIO_printf(bio
, "\n");
3221 "---\nSSL handshake has read %ju bytes "
3222 "and written %ju bytes\n",
3223 BIO_number_read(SSL_get_rbio(s
)),
3224 BIO_number_written(SSL_get_wbio(s
)));
3226 print_verify_detail(s
, bio
);
3227 BIO_printf(bio
, (SSL_session_reused(s
) ? "---\nReused, " : "---\nNew, "));
3228 c
= SSL_get_current_cipher(s
);
3229 BIO_printf(bio
, "%s, Cipher is %s\n",
3230 SSL_CIPHER_get_version(c
), SSL_CIPHER_get_name(c
));
3234 pktmp
= X509_get0_pubkey(peer
);
3235 BIO_printf(bio
, "Server public key is %d bit\n",
3236 EVP_PKEY_bits(pktmp
));
3238 BIO_printf(bio
, "Secure Renegotiation IS%s supported\n",
3239 SSL_get_secure_renegotiation_support(s
) ? "" : " NOT");
3240 #ifndef OPENSSL_NO_COMP
3241 comp
= SSL_get_current_compression(s
);
3242 expansion
= SSL_get_current_expansion(s
);
3243 BIO_printf(bio
, "Compression: %s\n",
3244 comp
? SSL_COMP_get_name(comp
) : "NONE");
3245 BIO_printf(bio
, "Expansion: %s\n",
3246 expansion
? SSL_COMP_get_name(expansion
) : "NONE");
3251 /* Print out local port of connection: useful for debugging */
3253 union BIO_sock_info_u info
;
3255 sock
= SSL_get_fd(s
);
3256 if ((info
.addr
= BIO_ADDR_new()) != NULL
3257 && BIO_sock_info(sock
, BIO_SOCK_INFO_ADDRESS
, &info
)) {
3258 BIO_printf(bio_c_out
, "LOCAL PORT is %u\n",
3259 ntohs(BIO_ADDR_rawport(info
.addr
)));
3261 BIO_ADDR_free(info
.addr
);
3265 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3266 if (next_proto
.status
!= -1) {
3267 const unsigned char *proto
;
3268 unsigned int proto_len
;
3269 SSL_get0_next_proto_negotiated(s
, &proto
, &proto_len
);
3270 BIO_printf(bio
, "Next protocol: (%d) ", next_proto
.status
);
3271 BIO_write(bio
, proto
, proto_len
);
3272 BIO_write(bio
, "\n", 1);
3276 const unsigned char *proto
;
3277 unsigned int proto_len
;
3278 SSL_get0_alpn_selected(s
, &proto
, &proto_len
);
3279 if (proto_len
> 0) {
3280 BIO_printf(bio
, "ALPN protocol: ");
3281 BIO_write(bio
, proto
, proto_len
);
3282 BIO_write(bio
, "\n", 1);
3284 BIO_printf(bio
, "No ALPN negotiated\n");
3287 #ifndef OPENSSL_NO_SRTP
3289 SRTP_PROTECTION_PROFILE
*srtp_profile
=
3290 SSL_get_selected_srtp_profile(s
);
3293 BIO_printf(bio
, "SRTP Extension negotiated, profile=%s\n",
3294 srtp_profile
->name
);
3299 switch (SSL_get_early_data_status(s
)) {
3300 case SSL_EARLY_DATA_NOT_SENT
:
3301 BIO_printf(bio
, "Early data was not sent\n");
3304 case SSL_EARLY_DATA_REJECTED
:
3305 BIO_printf(bio
, "Early data was rejected\n");
3308 case SSL_EARLY_DATA_ACCEPTED
:
3309 BIO_printf(bio
, "Early data was accepted\n");
3315 * We also print the verify results when we dump session information,
3316 * but in TLSv1.3 we may not get that right away (or at all) depending
3317 * on when we get a NewSessionTicket. Therefore we print it now as well.
3319 verify_result
= SSL_get_verify_result(s
);
3320 BIO_printf(bio
, "Verify return code: %ld (%s)\n", verify_result
,
3321 X509_verify_cert_error_string(verify_result
));
3323 /* In TLSv1.3 we do this on arrival of a NewSessionTicket */
3324 SSL_SESSION_print(bio
, SSL_get_session(s
));
3327 if (SSL_get_session(s
) != NULL
&& keymatexportlabel
!= NULL
) {
3328 BIO_printf(bio
, "Keying material exporter:\n");
3329 BIO_printf(bio
, " Label: '%s'\n", keymatexportlabel
);
3330 BIO_printf(bio
, " Length: %i bytes\n", keymatexportlen
);
3331 exportedkeymat
= app_malloc(keymatexportlen
, "export key");
3332 if (!SSL_export_keying_material(s
, exportedkeymat
,
3335 strlen(keymatexportlabel
),
3337 BIO_printf(bio
, " Error\n");
3339 BIO_printf(bio
, " Keying material: ");
3340 for (i
= 0; i
< keymatexportlen
; i
++)
3341 BIO_printf(bio
, "%02X", exportedkeymat
[i
]);
3342 BIO_printf(bio
, "\n");
3344 OPENSSL_free(exportedkeymat
);
3346 BIO_printf(bio
, "---\n");
3348 /* flush, or debugging output gets mixed with http response */
3349 (void)BIO_flush(bio
);
3352 # ifndef OPENSSL_NO_OCSP
3353 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3355 const unsigned char *p
;
3358 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3359 BIO_puts(arg
, "OCSP response: ");
3361 BIO_puts(arg
, "no response sent\n");
3364 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3366 BIO_puts(arg
, "response parse error\n");
3367 BIO_dump_indent(arg
, (char *)p
, len
, 4);
3370 BIO_puts(arg
, "\n======================================\n");
3371 OCSP_RESPONSE_print(arg
, rsp
, 0);
3372 BIO_puts(arg
, "======================================\n");
3373 OCSP_RESPONSE_free(rsp
);
3378 static int ldap_ExtendedResponse_parse(const char *buf
, long rem
)
3380 const unsigned char *cur
, *end
;
3382 int tag
, xclass
, inf
, ret
= -1;
3384 cur
= (const unsigned char *)buf
;
3390 * LDAPMessage ::= SEQUENCE {
3391 * messageID MessageID,
3392 * protocolOp CHOICE {
3394 * extendedResp ExtendedResponse,
3396 * controls [0] Controls OPTIONAL }
3398 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3399 * COMPONENTS OF LDAPResult,
3400 * responseName [10] LDAPOID OPTIONAL,
3401 * responseValue [11] OCTET STRING OPTIONAL }
3403 * LDAPResult ::= SEQUENCE {
3404 * resultCode ENUMERATED {
3410 * diagnosticMessage LDAPString,
3411 * referral [3] Referral OPTIONAL }
3415 inf
= ASN1_get_object(&cur
, &len
, &tag
, &xclass
, rem
);
3416 if (inf
!= V_ASN1_CONSTRUCTED
|| tag
!= V_ASN1_SEQUENCE
||
3417 (rem
= end
- cur
, len
> rem
)) {
3418 BIO_printf(bio_err
, "Unexpected LDAP response\n");
3422 rem
= len
; /* ensure that we don't overstep the SEQUENCE */
3424 /* pull MessageID */
3425 inf
= ASN1_get_object(&cur
, &len
, &tag
, &xclass
, rem
);
3426 if (inf
!= V_ASN1_UNIVERSAL
|| tag
!= V_ASN1_INTEGER
||
3427 (rem
= end
- cur
, len
> rem
)) {
3428 BIO_printf(bio_err
, "No MessageID\n");
3432 cur
+= len
; /* shall we check for MessageId match or just skip? */
3434 /* pull [APPLICATION 24] */
3436 inf
= ASN1_get_object(&cur
, &len
, &tag
, &xclass
, rem
);
3437 if (inf
!= V_ASN1_CONSTRUCTED
|| xclass
!= V_ASN1_APPLICATION
||
3439 BIO_printf(bio_err
, "Not ExtendedResponse\n");
3443 /* pull resultCode */
3445 inf
= ASN1_get_object(&cur
, &len
, &tag
, &xclass
, rem
);
3446 if (inf
!= V_ASN1_UNIVERSAL
|| tag
!= V_ASN1_ENUMERATED
|| len
== 0 ||
3447 (rem
= end
- cur
, len
> rem
)) {
3448 BIO_printf(bio_err
, "Not LDAPResult\n");
3452 /* len should always be one, but just in case... */
3453 for (ret
= 0, inf
= 0; inf
< len
; inf
++) {
3457 /* There is more data, but we don't care... */
3462 #endif /* OPENSSL_NO_SOCK */