2 * Copyright 1995-2017 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
16 #include <openssl/e_os2.h>
18 #ifndef OPENSSL_NO_SOCK
21 * With IPv6, it looks like Digital has mixed up the proper order of
22 * recursive header file inclusion, resulting in the compiler complaining
23 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
24 * needed to have fileno() declared correctly... So let's define u_int
26 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
28 typedef unsigned int u_int;
33 #include <openssl/x509.h>
34 #include <openssl/ssl.h>
35 #include <openssl/err.h>
36 #include <openssl/pem.h>
37 #include <openssl/rand.h>
38 #include <openssl/ocsp.h>
39 #include <openssl/bn.h>
40 #include <openssl/async.h>
41 #ifndef OPENSSL_NO_SRP
42 # include <openssl/srp.h>
45 # include <openssl/ct.h>
50 #if defined(__has_feature)
51 # if __has_feature(memory_sanitizer)
52 # include <sanitizer/msan_interface.h>
57 #define BUFSIZZ 1024*8
58 #define S_CLIENT_IRC_READ_TIMEOUT 8
61 static int c_debug = 0;
62 static int c_showcerts = 0;
63 static char *keymatexportlabel = NULL;
64 static int keymatexportlen = 20;
65 static BIO *bio_c_out = NULL;
66 static int c_quiet = 0;
67 static char *sess_out = NULL;
69 static void print_stuff(BIO *berr, SSL *con, int full);
70 #ifndef OPENSSL_NO_OCSP
71 static int ocsp_resp_cb(SSL *s, void *arg);
73 static int ldap_ExtendedResponse_parse(const char *buf, long rem);
75 static int saved_errno;
77 static void save_errno(void)
83 static int restore_errno(void)
90 static void do_ssl_shutdown(SSL *ssl)
95 /* We only do unidirectional shutdown */
96 ret = SSL_shutdown(ssl);
98 switch (SSL_get_error(ssl, ret)) {
99 case SSL_ERROR_WANT_READ:
100 case SSL_ERROR_WANT_WRITE:
101 case SSL_ERROR_WANT_ASYNC:
102 case SSL_ERROR_WANT_ASYNC_JOB:
103 /* We just do busy waiting. Nothing clever */
111 #ifndef OPENSSL_NO_PSK
112 /* Default PSK identity and key */
113 static char *psk_identity = "Client_identity";
115 static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
116 unsigned int max_identity_len,
118 unsigned int max_psk_len)
125 BIO_printf(bio_c_out, "psk_client_cb\n");
127 /* no ServerKeyExchange message */
129 BIO_printf(bio_c_out,
130 "NULL received PSK identity hint, continuing anyway\n");
131 } else if (c_debug) {
132 BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
136 * lookup PSK identity and PSK key based on the given identity hint here
138 ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
139 if (ret < 0 || (unsigned int)ret > max_identity_len)
142 BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity,
145 /* convert the PSK key to binary */
146 key = OPENSSL_hexstr2buf(psk_key, &key_len);
148 BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
152 if (max_psk_len > INT_MAX || key_len > (long)max_psk_len) {
154 "psk buffer of callback is too small (%d) for key (%ld)\n",
155 max_psk_len, key_len);
160 memcpy(psk, key, key_len);
164 BIO_printf(bio_c_out, "created PSK len=%ld\n", key_len);
169 BIO_printf(bio_err, "Error in PSK client callback\n");
174 /* This is a context that we pass to callbacks */
175 typedef struct tlsextctx_st {
180 static int ssl_servername_cb(SSL *s, int *ad, void *arg)
182 tlsextctx *p = (tlsextctx *) arg;
183 const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
184 if (SSL_get_servername_type(s) != -1)
185 p->ack = !SSL_session_reused(s) && hn != NULL;
187 BIO_printf(bio_err, "Can't use SSL_get_servername\n");
189 return SSL_TLSEXT_ERR_OK;
192 #ifndef OPENSSL_NO_SRP
194 /* This is a context that we pass to all callbacks */
195 typedef struct srp_arg_st {
198 int msg; /* copy from c_msg */
199 int debug; /* copy from c_debug */
200 int amp; /* allow more groups */
201 int strength; /* minimal size for N */
204 # define SRP_NUMBER_ITERATIONS_FOR_PRIME 64
206 static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)
208 BN_CTX *bn_ctx = BN_CTX_new();
209 BIGNUM *p = BN_new();
210 BIGNUM *r = BN_new();
212 g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&
213 BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&
214 p != NULL && BN_rshift1(p, N) &&
216 BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&
218 /* verify g^((N-1)/2) == -1 (mod N) */
219 BN_mod_exp(r, g, p, N, bn_ctx) &&
220 BN_add_word(r, 1) && BN_cmp(r, N) == 0;
229 * This callback is used here for two purposes:
230 * - extended debugging
231 * - making some primality tests for unknown groups
232 * The callback is only called for a non default group.
234 * An application does not need the call back at all if
235 * only the standard groups are used. In real life situations,
236 * client and server already share well known groups,
237 * thus there is no need to verify them.
238 * Furthermore, in case that a server actually proposes a group that
239 * is not one of those defined in RFC 5054, it is more appropriate
240 * to add the group to a static list and then compare since
241 * primality tests are rather cpu consuming.
244 static int ssl_srp_verify_param_cb(SSL *s, void *arg)
246 SRP_ARG *srp_arg = (SRP_ARG *)arg;
247 BIGNUM *N = NULL, *g = NULL;
249 if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL))
251 if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) {
252 BIO_printf(bio_err, "SRP parameters:\n");
253 BIO_printf(bio_err, "\tN=");
254 BN_print(bio_err, N);
255 BIO_printf(bio_err, "\n\tg=");
256 BN_print(bio_err, g);
257 BIO_printf(bio_err, "\n");
260 if (SRP_check_known_gN_param(g, N))
263 if (srp_arg->amp == 1) {
266 "SRP param N and g are not known params, going to check deeper.\n");
269 * The srp_moregroups is a real debugging feature. Implementors
270 * should rather add the value to the known ones. The minimal size
271 * has already been tested.
273 if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g))
276 BIO_printf(bio_err, "SRP param N and g rejected.\n");
280 # define PWD_STRLEN 1024
282 static char *ssl_give_srp_client_pwd_cb(SSL *s, void *arg)
284 SRP_ARG *srp_arg = (SRP_ARG *)arg;
285 char *pass = app_malloc(PWD_STRLEN + 1, "SRP password buffer");
289 cb_tmp.password = (char *)srp_arg->srppassin;
290 cb_tmp.prompt_info = "SRP user";
291 if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) {
292 BIO_printf(bio_err, "Can't read Password\n");
303 static char *srtp_profiles = NULL;
305 #ifndef OPENSSL_NO_NEXTPROTONEG
306 /* This the context that we pass to next_proto_cb */
307 typedef struct tlsextnextprotoctx_st {
311 } tlsextnextprotoctx;
313 static tlsextnextprotoctx next_proto;
315 static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen,
316 const unsigned char *in, unsigned int inlen,
319 tlsextnextprotoctx *ctx = arg;
322 /* We can assume that |in| is syntactically valid. */
324 BIO_printf(bio_c_out, "Protocols advertised by server: ");
325 for (i = 0; i < inlen;) {
327 BIO_write(bio_c_out, ", ", 2);
328 BIO_write(bio_c_out, &in[i + 1], in[i]);
331 BIO_write(bio_c_out, "\n", 1);
335 SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
336 return SSL_TLSEXT_ERR_OK;
338 #endif /* ndef OPENSSL_NO_NEXTPROTONEG */
340 static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
341 const unsigned char *in, size_t inlen,
345 unsigned char ext_buf[4 + 65536];
347 /* Reconstruct the type/len fields prior to extension data */
348 ext_buf[0] = ext_type >> 8;
349 ext_buf[1] = ext_type & 0xFF;
350 ext_buf[2] = inlen >> 8;
351 ext_buf[3] = inlen & 0xFF;
352 memcpy(ext_buf + 4, in, inlen);
354 BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d",
356 PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen);
361 * Hex decoder that tolerates optional whitespace. Returns number of bytes
362 * produced, advances inptr to end of input string.
364 static ossl_ssize_t hexdecode(const char **inptr, void *result)
366 unsigned char **out = (unsigned char **)result;
367 const char *in = *inptr;
368 unsigned char *ret = app_malloc(strlen(in) / 2, "hexdecode");
369 unsigned char *cp = ret;
376 for (byte = 0; *in; ++in) {
379 if (isspace(_UC(*in)))
381 x = OPENSSL_hexchar2int(*in);
387 if ((nibble ^= 1) == 0) {
400 return cp - (*out = ret);
404 * Decode unsigned 0..255, returns 1 on success, <= 0 on failure. Advances
405 * inptr to next field skipping leading whitespace.
407 static ossl_ssize_t checked_uint8(const char **inptr, void *out)
409 uint8_t *result = (uint8_t *)out;
410 const char *in = *inptr;
416 v = strtol(in, &endp, 10);
419 if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
420 endp == in || !isspace(_UC(*endp)) ||
421 v != (*result = (uint8_t) v)) {
424 for (in = endp; isspace(_UC(*in)); ++in)
434 ossl_ssize_t (*parser)(const char **, void *);
437 static int tlsa_import_rr(SSL *con, const char *rrdata)
439 /* Not necessary to re-init these values; the "parsers" do that. */
440 static uint8_t usage;
441 static uint8_t selector;
442 static uint8_t mtype;
443 static unsigned char *data;
444 static struct tlsa_field tlsa_fields[] = {
445 { &usage, "usage", checked_uint8 },
446 { &selector, "selector", checked_uint8 },
447 { &mtype, "mtype", checked_uint8 },
448 { &data, "data", hexdecode },
451 struct tlsa_field *f;
453 const char *cp = rrdata;
454 ossl_ssize_t len = 0;
456 for (f = tlsa_fields; f->var; ++f) {
457 /* Returns number of bytes produced, advances cp to next field */
458 if ((len = f->parser(&cp, f->var)) <= 0) {
459 BIO_printf(bio_err, "%s: warning: bad TLSA %s field in: %s\n",
460 prog, f->name, rrdata);
464 /* The data field is last, so len is its length */
465 ret = SSL_dane_tlsa_add(con, usage, selector, mtype, data, len);
469 ERR_print_errors(bio_err);
470 BIO_printf(bio_err, "%s: warning: unusable TLSA rrdata: %s\n",
475 ERR_print_errors(bio_err);
476 BIO_printf(bio_err, "%s: warning: error loading TLSA rrdata: %s\n",
483 static int tlsa_import_rrset(SSL *con, STACK_OF(OPENSSL_STRING) *rrset)
485 int num = sk_OPENSSL_STRING_num(rrset);
489 for (i = 0; i < num; ++i) {
490 char *rrdata = sk_OPENSSL_STRING_value(rrset, i);
491 if (tlsa_import_rr(con, rrdata) > 0)
497 typedef enum OPTION_choice {
498 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
499 OPT_4, OPT_6, OPT_HOST, OPT_PORT, OPT_CONNECT, OPT_UNIX,
500 OPT_XMPPHOST, OPT_VERIFY, OPT_NAMEOPT,
501 OPT_CERT, OPT_CRL, OPT_CRL_DOWNLOAD, OPT_SESS_OUT, OPT_SESS_IN,
502 OPT_CERTFORM, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
503 OPT_BRIEF, OPT_PREXIT, OPT_CRLF, OPT_QUIET, OPT_NBIO,
504 OPT_SSL_CLIENT_ENGINE, OPT_RAND, OPT_IGN_EOF, OPT_NO_IGN_EOF,
505 OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_WDEBUG,
506 OPT_MSG, OPT_MSGFILE, OPT_ENGINE, OPT_TRACE, OPT_SECURITY_DEBUG,
507 OPT_SECURITY_DEBUG_VERBOSE, OPT_SHOWCERTS, OPT_NBIO_TEST, OPT_STATE,
508 #ifndef OPENSSL_NO_PSK
509 OPT_PSK_IDENTITY, OPT_PSK,
511 #ifndef OPENSSL_NO_SRP
512 OPT_SRPUSER, OPT_SRPPASS, OPT_SRP_STRENGTH, OPT_SRP_LATEUSER,
515 OPT_SSL3, OPT_SSL_CONFIG,
516 OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
517 OPT_DTLS1_2, OPT_SCTP, OPT_TIMEOUT, OPT_MTU, OPT_KEYFORM, OPT_PASS,
518 OPT_CERT_CHAIN, OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH,
519 OPT_KEY, OPT_RECONNECT, OPT_BUILD_CHAIN, OPT_CAFILE, OPT_NOCAFILE,
520 OPT_CHAINCAFILE, OPT_VERIFYCAFILE, OPT_NEXTPROTONEG, OPT_ALPN,
521 OPT_SERVERINFO, OPT_STARTTLS, OPT_SERVERNAME, OPT_NOSERVERNAME, OPT_ASYNC,
522 OPT_USE_SRTP, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN, OPT_SMTPHOST,
523 OPT_MAX_SEND_FRAG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES, OPT_READ_BUF,
524 OPT_KEYLOG_FILE, OPT_EARLY_DATA, OPT_REQCAFILE,
528 OPT_FALLBACKSCSV, OPT_NOCMDS, OPT_PROXY, OPT_DANE_TLSA_DOMAIN,
529 #ifndef OPENSSL_NO_CT
530 OPT_CT, OPT_NOCT, OPT_CTLOG_FILE,
532 OPT_DANE_TLSA_RRDATA, OPT_DANE_EE_NO_NAME
535 const OPTIONS s_client_options[] = {
536 {"help", OPT_HELP, '-', "Display this summary"},
537 {"host", OPT_HOST, 's', "Use -connect instead"},
538 {"port", OPT_PORT, 'p', "Use -connect instead"},
539 {"connect", OPT_CONNECT, 's',
540 "TCP/IP where to connect (default is :" PORT ")"},
541 {"proxy", OPT_PROXY, 's',
542 "Connect to via specified proxy to the real server"},
544 {"unix", OPT_UNIX, 's', "Connect over the specified Unix-domain socket"},
546 {"4", OPT_4, '-', "Use IPv4 only"},
548 {"6", OPT_6, '-', "Use IPv6 only"},
550 {"verify", OPT_VERIFY, 'p', "Turn on peer certificate verification"},
551 {"cert", OPT_CERT, '<', "Certificate file to use, PEM format assumed"},
552 {"certform", OPT_CERTFORM, 'F',
553 "Certificate format (PEM or DER) PEM default"},
554 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
555 {"key", OPT_KEY, 's', "Private key file to use, if not in -cert file"},
556 {"keyform", OPT_KEYFORM, 'E', "Key format (PEM, DER or engine) PEM default"},
557 {"pass", OPT_PASS, 's', "Private key file pass phrase source"},
558 {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
559 {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
560 {"no-CAfile", OPT_NOCAFILE, '-',
561 "Do not load the default certificates file"},
562 {"no-CApath", OPT_NOCAPATH, '-',
563 "Do not load certificates from the default certificates directory"},
564 {"requestCAfile", OPT_REQCAFILE, '<',
565 "PEM format file of CA names to send to the server"},
566 {"dane_tlsa_domain", OPT_DANE_TLSA_DOMAIN, 's', "DANE TLSA base domain"},
567 {"dane_tlsa_rrdata", OPT_DANE_TLSA_RRDATA, 's',
568 "DANE TLSA rrdata presentation form"},
569 {"dane_ee_no_namechecks", OPT_DANE_EE_NO_NAME, '-',
570 "Disable name checks when matching DANE-EE(3) TLSA records"},
571 {"reconnect", OPT_RECONNECT, '-',
572 "Drop and re-make the connection with the same Session-ID"},
573 {"showcerts", OPT_SHOWCERTS, '-', "Show all certificates in the chain"},
574 {"debug", OPT_DEBUG, '-', "Extra output"},
575 {"msg", OPT_MSG, '-', "Show protocol messages"},
576 {"msgfile", OPT_MSGFILE, '>',
577 "File to send output of -msg or -trace, instead of stdout"},
578 {"nbio_test", OPT_NBIO_TEST, '-', "More ssl protocol testing"},
579 {"state", OPT_STATE, '-', "Print the ssl states"},
580 {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
581 {"quiet", OPT_QUIET, '-', "No s_client output"},
582 {"ign_eof", OPT_IGN_EOF, '-', "Ignore input eof (default when -quiet)"},
583 {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Don't ignore input eof"},
584 {"starttls", OPT_STARTTLS, 's',
585 "Use the appropriate STARTTLS command before starting TLS"},
586 {"xmpphost", OPT_XMPPHOST, 's',
587 "Host to use with \"-starttls xmpp[-server]\""},
588 {"rand", OPT_RAND, 's',
589 "Load the file(s) into the random number generator"},
590 {"sess_out", OPT_SESS_OUT, '>', "File to write SSL session to"},
591 {"sess_in", OPT_SESS_IN, '<', "File to read SSL session from"},
592 {"use_srtp", OPT_USE_SRTP, 's',
593 "Offer SRTP key management with a colon-separated profile list"},
594 {"keymatexport", OPT_KEYMATEXPORT, 's',
595 "Export keying material using label"},
596 {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
597 "Export len bytes of keying material (default 20)"},
598 {"fallback_scsv", OPT_FALLBACKSCSV, '-', "Send the fallback SCSV"},
599 {"name", OPT_SMTPHOST, 's',
600 "Hostname to use for \"-starttls lmtp\" or \"-starttls smtp\""},
601 {"CRL", OPT_CRL, '<', "CRL file to use"},
602 {"crl_download", OPT_CRL_DOWNLOAD, '-', "Download CRL from distribution points"},
603 {"CRLform", OPT_CRLFORM, 'F', "CRL format (PEM or DER) PEM is default"},
604 {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
605 "Close connection on verification error"},
606 {"verify_quiet", OPT_VERIFY_QUIET, '-', "Restrict verify output to errors"},
607 {"brief", OPT_BRIEF, '-',
608 "Restrict output to brief summary of connection parameters"},
609 {"prexit", OPT_PREXIT, '-',
610 "Print session information when the program exits"},
611 {"security_debug", OPT_SECURITY_DEBUG, '-',
612 "Enable security debug messages"},
613 {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
614 "Output more security debug output"},
615 {"cert_chain", OPT_CERT_CHAIN, '<',
616 "Certificate chain file (in PEM format)"},
617 {"chainCApath", OPT_CHAINCAPATH, '/',
618 "Use dir as certificate store path to build CA certificate chain"},
619 {"verifyCApath", OPT_VERIFYCAPATH, '/',
620 "Use dir as certificate store path to verify CA certificate"},
621 {"build_chain", OPT_BUILD_CHAIN, '-', "Build certificate chain"},
622 {"chainCAfile", OPT_CHAINCAFILE, '<',
623 "CA file for certificate chain (PEM format)"},
624 {"verifyCAfile", OPT_VERIFYCAFILE, '<',
625 "CA file for certificate verification (PEM format)"},
626 {"nocommands", OPT_NOCMDS, '-', "Do not use interactive command letters"},
627 {"servername", OPT_SERVERNAME, 's',
628 "Set TLS extension servername (SNI) in ClientHello (default)"},
629 {"noservername", OPT_NOSERVERNAME, '-',
630 "Do not send the server name (SNI) extension in the ClientHello"},
631 {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
632 "Hex dump of all TLS extensions received"},
633 #ifndef OPENSSL_NO_OCSP
634 {"status", OPT_STATUS, '-', "Request certificate status from server"},
636 {"serverinfo", OPT_SERVERINFO, 's',
637 "types Send empty ClientHello extensions (comma-separated numbers)"},
638 {"alpn", OPT_ALPN, 's',
639 "Enable ALPN extension, considering named protocols supported (comma-separated list)"},
640 {"async", OPT_ASYNC, '-', "Support asynchronous operation"},
641 {"ssl_config", OPT_SSL_CONFIG, 's', "Use specified configuration file"},
642 {"max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames "},
643 {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'p',
644 "Size used to split data for encrypt pipelines"},
645 {"max_pipelines", OPT_MAX_PIPELINES, 'p',
646 "Maximum number of encrypt/decrypt pipelines to be used"},
647 {"read_buf", OPT_READ_BUF, 'p',
648 "Default read buffer size to be used for connections"},
652 #ifndef OPENSSL_NO_SSL3
653 {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
655 #ifndef OPENSSL_NO_TLS1
656 {"tls1", OPT_TLS1, '-', "Just use TLSv1"},
658 #ifndef OPENSSL_NO_TLS1_1
659 {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"},
661 #ifndef OPENSSL_NO_TLS1_2
662 {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"},
664 #ifndef OPENSSL_NO_TLS1_3
665 {"tls1_3", OPT_TLS1_3, '-', "Just use TLSv1.3"},
667 #ifndef OPENSSL_NO_DTLS
668 {"dtls", OPT_DTLS, '-', "Use any version of DTLS"},
669 {"timeout", OPT_TIMEOUT, '-',
670 "Enable send/receive timeout on DTLS connections"},
671 {"mtu", OPT_MTU, 'p', "Set the link layer MTU"},
673 #ifndef OPENSSL_NO_DTLS1
674 {"dtls1", OPT_DTLS1, '-', "Just use DTLSv1"},
676 #ifndef OPENSSL_NO_DTLS1_2
677 {"dtls1_2", OPT_DTLS1_2, '-', "Just use DTLSv1.2"},
679 #ifndef OPENSSL_NO_SCTP
680 {"sctp", OPT_SCTP, '-', "Use SCTP"},
682 #ifndef OPENSSL_NO_SSL_TRACE
683 {"trace", OPT_TRACE, '-', "Show trace output of protocol messages"},
686 {"wdebug", OPT_WDEBUG, '-', "WATT-32 tcp debugging"},
688 {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
689 #ifndef OPENSSL_NO_PSK
690 {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity"},
691 {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
693 #ifndef OPENSSL_NO_SRP
694 {"srpuser", OPT_SRPUSER, 's', "SRP authentication for 'user'"},
695 {"srppass", OPT_SRPPASS, 's', "Password for 'user'"},
696 {"srp_lateuser", OPT_SRP_LATEUSER, '-',
697 "SRP username into second ClientHello message"},
698 {"srp_moregroups", OPT_SRP_MOREGROUPS, '-',
699 "Tolerate other than the known g N values."},
700 {"srp_strength", OPT_SRP_STRENGTH, 'p', "Minimal length in bits for N"},
702 #ifndef OPENSSL_NO_NEXTPROTONEG
703 {"nextprotoneg", OPT_NEXTPROTONEG, 's',
704 "Enable NPN extension, considering named protocols supported (comma-separated list)"},
706 #ifndef OPENSSL_NO_ENGINE
707 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
708 {"ssl_client_engine", OPT_SSL_CLIENT_ENGINE, 's',
709 "Specify engine to be used for client certificate operations"},
711 #ifndef OPENSSL_NO_CT
712 {"ct", OPT_CT, '-', "Request and parse SCTs (also enables OCSP stapling)"},
713 {"noct", OPT_NOCT, '-', "Do not request or parse SCTs (default)"},
714 {"ctlogfile", OPT_CTLOG_FILE, '<', "CT log list CONF file"},
716 {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"},
717 {"early_data", OPT_EARLY_DATA, '<', "File to send as early data"},
718 {NULL, OPT_EOF, 0x00, NULL}
721 typedef enum PROTOCOL_choice {
740 static const OPT_PAIR services[] = {
741 {"smtp", PROTO_SMTP},
742 {"pop3", PROTO_POP3},
743 {"imap", PROTO_IMAP},
745 {"xmpp", PROTO_XMPP},
746 {"xmpp-server", PROTO_XMPP_SERVER},
747 {"telnet", PROTO_TELNET},
749 {"mysql", PROTO_MYSQL},
750 {"postgres", PROTO_POSTGRES},
751 {"lmtp", PROTO_LMTP},
752 {"nntp", PROTO_NNTP},
753 {"sieve", PROTO_SIEVE},
754 {"ldap", PROTO_LDAP},
758 #define IS_INET_FLAG(o) \
759 (o == OPT_4 || o == OPT_6 || o == OPT_HOST || o == OPT_PORT || o == OPT_CONNECT)
760 #define IS_UNIX_FLAG(o) (o == OPT_UNIX)
762 #define IS_PROT_FLAG(o) \
763 (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
764 || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
766 /* Free |*dest| and optionally set it to a copy of |source|. */
767 static void freeandcopy(char **dest, const char *source)
772 *dest = OPENSSL_strdup(source);
775 static int new_session_cb(SSL *S, SSL_SESSION *sess)
777 BIO *stmp = BIO_new_file(sess_out, "w");
780 BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
782 PEM_write_bio_SSL_SESSION(stmp, sess);
787 * We always return a "fail" response so that the session gets freed again
788 * because we haven't used the reference.
793 int s_client_main(int argc, char **argv)
796 EVP_PKEY *key = NULL;
799 STACK_OF(X509) *chain = NULL;
801 X509_VERIFY_PARAM *vpm = NULL;
802 SSL_EXCERT *exc = NULL;
803 SSL_CONF_CTX *cctx = NULL;
804 STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
805 char *dane_tlsa_domain = NULL;
806 STACK_OF(OPENSSL_STRING) *dane_tlsa_rrset = NULL;
807 int dane_ee_no_name = 0;
808 STACK_OF(X509_CRL) *crls = NULL;
809 const SSL_METHOD *meth = TLS_client_method();
810 const char *CApath = NULL, *CAfile = NULL;
811 char *cbuf = NULL, *sbuf = NULL;
812 char *mbuf = NULL, *proxystr = NULL, *connectstr = NULL;
813 char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
814 char *chCApath = NULL, *chCAfile = NULL, *host = NULL;
815 char *port = OPENSSL_strdup(PORT);
817 char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;
818 char *ReqCAfile = NULL;
819 char *sess_in = NULL, *crl_file = NULL, *p;
820 char *xmpphost = NULL;
821 const char *ehlo = "mail.example.com";
822 struct timeval timeout, *timeoutp;
823 fd_set readfds, writefds;
824 int noCApath = 0, noCAfile = 0;
825 int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_PEM;
826 int key_format = FORMAT_PEM, crlf = 0, full_log = 1, mbuf_len = 0;
829 int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0;
830 int ret = 1, in_init = 1, i, nbio_test = 0, s = -1, k, width, state = 0;
831 int sbuf_len, sbuf_off, cmdletters = 1;
832 int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0;
833 int starttls_proto = PROTO_OFF, crl_format = FORMAT_PEM, crl_download = 0;
834 int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
835 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
838 int read_buf_len = 0;
839 int fallback_scsv = 0;
842 #ifndef OPENSSL_NO_DTLS
843 int enable_timeouts = 0;
846 #ifndef OPENSSL_NO_ENGINE
847 ENGINE *ssl_client_engine = NULL;
850 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
853 char *servername = NULL;
854 int noservername = 0;
855 const char *alpn_in = NULL;
856 tlsextctx tlsextcbp = { NULL, 0 };
857 const char *ssl_config = NULL;
858 #define MAX_SI_TYPES 100
859 unsigned short serverinfo_types[MAX_SI_TYPES];
860 int serverinfo_count = 0, start = 0, len;
861 #ifndef OPENSSL_NO_NEXTPROTONEG
862 const char *next_proto_neg_in = NULL;
864 #ifndef OPENSSL_NO_SRP
865 char *srppass = NULL;
866 int srp_lateuser = 0;
867 SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };
869 #ifndef OPENSSL_NO_CT
870 char *ctlog_file = NULL;
871 int ct_validation = 0;
873 int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0;
875 unsigned int max_send_fragment = 0;
876 unsigned int split_send_fragment = 0, max_pipelines = 0;
877 enum { use_inet, use_unix, use_unknown } connect_type = use_unknown;
879 int c_nbio = 0, c_msg = 0, c_ign_eof = 0, c_brief = 0;
880 int c_tlsextdebug = 0;
881 #ifndef OPENSSL_NO_OCSP
882 int c_status_req = 0;
884 BIO *bio_c_msg = NULL;
885 const char *keylog_file = NULL, *early_data_file = NULL;
886 #ifndef OPENSSL_NO_DTLS
892 /* Known false-positive of MemorySanitizer. */
893 #if defined(__has_feature)
894 # if __has_feature(memory_sanitizer)
895 __msan_unpoison(&readfds, sizeof(readfds));
896 __msan_unpoison(&writefds, sizeof(writefds));
900 prog = opt_progname(argv[0]);
905 vpm = X509_VERIFY_PARAM_new();
906 cctx = SSL_CONF_CTX_new();
908 if (vpm == NULL || cctx == NULL) {
909 BIO_printf(bio_err, "%s: out of memory\n", prog);
913 cbuf = app_malloc(BUFSIZZ, "cbuf");
914 sbuf = app_malloc(BUFSIZZ, "sbuf");
915 mbuf = app_malloc(BUFSIZZ, "mbuf");
917 SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE);
919 prog = opt_init(argc, argv, s_client_options);
920 while ((o = opt_next()) != OPT_EOF) {
921 /* Check for intermixing flags. */
922 if (connect_type == use_unix && IS_INET_FLAG(o)) {
924 "%s: Intermixed protocol flags (unix and internet domains)\n",
928 if (connect_type == use_inet && IS_UNIX_FLAG(o)) {
930 "%s: Intermixed protocol flags (internet and unix domains)\n",
935 if (IS_PROT_FLAG(o) && ++prot_opt > 1) {
936 BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
939 if (IS_NO_PROT_FLAG(o))
941 if (prot_opt == 1 && no_prot_opt) {
943 "Cannot supply both a protocol flag and '-no_<prot>'\n");
951 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
954 opt_help(s_client_options);
958 connect_type = use_inet;
959 socket_family = AF_INET;
964 connect_type = use_inet;
965 socket_family = AF_INET6;
970 connect_type = use_inet;
971 freeandcopy(&host, opt_arg());
974 connect_type = use_inet;
975 freeandcopy(&port, opt_arg());
978 connect_type = use_inet;
979 freeandcopy(&connectstr, opt_arg());
982 proxystr = opt_arg();
983 starttls_proto = PROTO_CONNECT;
987 connect_type = use_unix;
988 socket_family = AF_UNIX;
989 freeandcopy(&host, opt_arg());
993 xmpphost = opt_arg();
999 verify = SSL_VERIFY_PEER;
1000 verify_args.depth = atoi(opt_arg());
1002 BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth);
1005 cert_file = opt_arg();
1008 if (!set_nameopt(opt_arg()))
1012 crl_file = opt_arg();
1014 case OPT_CRL_DOWNLOAD:
1018 sess_out = opt_arg();
1021 sess_in = opt_arg();
1024 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &cert_format))
1028 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
1031 case OPT_VERIFY_RET_ERROR:
1032 verify_args.return_error = 1;
1034 case OPT_VERIFY_QUIET:
1035 verify_args.quiet = 1;
1038 c_brief = verify_args.quiet = c_quiet = 1;
1041 if (ssl_args == NULL)
1042 ssl_args = sk_OPENSSL_STRING_new_null();
1043 if (ssl_args == NULL
1044 || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
1045 || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
1046 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1051 if (!opt_verify(o, vpm))
1056 if (!args_excert(o, &exc))
1066 c_quiet = c_ign_eof = 1;
1075 e = setup_engine(opt_arg(), 1);
1077 case OPT_SSL_CLIENT_ENGINE:
1078 #ifndef OPENSSL_NO_ENGINE
1079 ssl_client_engine = ENGINE_by_id(opt_arg());
1080 if (ssl_client_engine == NULL) {
1081 BIO_printf(bio_err, "Error getting client auth engine\n");
1092 case OPT_NO_IGN_EOF:
1098 case OPT_TLSEXTDEBUG:
1102 #ifndef OPENSSL_NO_OCSP
1115 bio_c_msg = BIO_new_file(opt_arg(), "w");
1118 #ifndef OPENSSL_NO_SSL_TRACE
1122 case OPT_SECURITY_DEBUG:
1125 case OPT_SECURITY_DEBUG_VERBOSE:
1137 #ifndef OPENSSL_NO_PSK
1138 case OPT_PSK_IDENTITY:
1139 psk_identity = opt_arg();
1142 for (p = psk_key = opt_arg(); *p; p++) {
1143 if (isxdigit(_UC(*p)))
1145 BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
1150 #ifndef OPENSSL_NO_SRP
1152 srp_arg.srplogin = opt_arg();
1153 if (min_version < TLS1_VERSION)
1154 min_version = TLS1_VERSION;
1157 srppass = opt_arg();
1158 if (min_version < TLS1_VERSION)
1159 min_version = TLS1_VERSION;
1161 case OPT_SRP_STRENGTH:
1162 srp_arg.strength = atoi(opt_arg());
1163 BIO_printf(bio_err, "SRP minimal length for N is %d\n",
1165 if (min_version < TLS1_VERSION)
1166 min_version = TLS1_VERSION;
1168 case OPT_SRP_LATEUSER:
1170 if (min_version < TLS1_VERSION)
1171 min_version = TLS1_VERSION;
1173 case OPT_SRP_MOREGROUPS:
1175 if (min_version < TLS1_VERSION)
1176 min_version = TLS1_VERSION;
1179 case OPT_SSL_CONFIG:
1180 ssl_config = opt_arg();
1183 min_version = SSL3_VERSION;
1184 max_version = SSL3_VERSION;
1187 min_version = TLS1_3_VERSION;
1188 max_version = TLS1_3_VERSION;
1191 min_version = TLS1_2_VERSION;
1192 max_version = TLS1_2_VERSION;
1195 min_version = TLS1_1_VERSION;
1196 max_version = TLS1_1_VERSION;
1199 min_version = TLS1_VERSION;
1200 max_version = TLS1_VERSION;
1203 #ifndef OPENSSL_NO_DTLS
1204 meth = DTLS_client_method();
1205 socket_type = SOCK_DGRAM;
1210 #ifndef OPENSSL_NO_DTLS1
1211 meth = DTLS_client_method();
1212 min_version = DTLS1_VERSION;
1213 max_version = DTLS1_VERSION;
1214 socket_type = SOCK_DGRAM;
1219 #ifndef OPENSSL_NO_DTLS1_2
1220 meth = DTLS_client_method();
1221 min_version = DTLS1_2_VERSION;
1222 max_version = DTLS1_2_VERSION;
1223 socket_type = SOCK_DGRAM;
1228 #ifndef OPENSSL_NO_SCTP
1229 protocol = IPPROTO_SCTP;
1233 #ifndef OPENSSL_NO_DTLS
1234 enable_timeouts = 1;
1238 #ifndef OPENSSL_NO_DTLS
1239 socket_mtu = atol(opt_arg());
1242 case OPT_FALLBACKSCSV:
1246 if (!opt_format(opt_arg(), OPT_FMT_PDE, &key_format))
1250 passarg = opt_arg();
1252 case OPT_CERT_CHAIN:
1253 chain_file = opt_arg();
1256 key_file = opt_arg();
1267 case OPT_CHAINCAPATH:
1268 chCApath = opt_arg();
1270 case OPT_VERIFYCAPATH:
1271 vfyCApath = opt_arg();
1273 case OPT_BUILD_CHAIN:
1277 ReqCAfile = opt_arg();
1285 #ifndef OPENSSL_NO_CT
1292 case OPT_CTLOG_FILE:
1293 ctlog_file = opt_arg();
1296 case OPT_CHAINCAFILE:
1297 chCAfile = opt_arg();
1299 case OPT_VERIFYCAFILE:
1300 vfyCAfile = opt_arg();
1302 case OPT_DANE_TLSA_DOMAIN:
1303 dane_tlsa_domain = opt_arg();
1305 case OPT_DANE_TLSA_RRDATA:
1306 if (dane_tlsa_rrset == NULL)
1307 dane_tlsa_rrset = sk_OPENSSL_STRING_new_null();
1308 if (dane_tlsa_rrset == NULL ||
1309 !sk_OPENSSL_STRING_push(dane_tlsa_rrset, opt_arg())) {
1310 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1314 case OPT_DANE_EE_NO_NAME:
1315 dane_ee_no_name = 1;
1317 case OPT_NEXTPROTONEG:
1318 #ifndef OPENSSL_NO_NEXTPROTONEG
1319 next_proto_neg_in = opt_arg();
1323 alpn_in = opt_arg();
1325 case OPT_SERVERINFO:
1328 for (start = 0, i = 0; i <= len; ++i) {
1329 if (i == len || p[i] == ',') {
1330 serverinfo_types[serverinfo_count] = atoi(p + start);
1331 if (++serverinfo_count == MAX_SI_TYPES)
1338 if (!opt_pair(opt_arg(), services, &starttls_proto))
1341 case OPT_SERVERNAME:
1342 servername = opt_arg();
1344 case OPT_NOSERVERNAME:
1348 srtp_profiles = opt_arg();
1350 case OPT_KEYMATEXPORT:
1351 keymatexportlabel = opt_arg();
1353 case OPT_KEYMATEXPORTLEN:
1354 keymatexportlen = atoi(opt_arg());
1359 case OPT_MAX_SEND_FRAG:
1360 max_send_fragment = atoi(opt_arg());
1362 case OPT_SPLIT_SEND_FRAG:
1363 split_send_fragment = atoi(opt_arg());
1365 case OPT_MAX_PIPELINES:
1366 max_pipelines = atoi(opt_arg());
1369 read_buf_len = atoi(opt_arg());
1371 case OPT_KEYLOG_FILE:
1372 keylog_file = opt_arg();
1374 case OPT_EARLY_DATA:
1375 early_data_file = opt_arg();
1379 if (count4or6 >= 2) {
1380 BIO_printf(bio_err, "%s: Can't use both -4 and -6\n", prog);
1384 if (servername != NULL) {
1386 "%s: Can't use -servername and -noservername together\n",
1390 if (dane_tlsa_domain != NULL) {
1392 "%s: Can't use -dane_tlsa_domain and -noservername together\n",
1397 argc = opt_num_rest();
1399 /* If there's a positional argument, it's the equivalent of
1401 * Don't allow -connect and a separate argument.
1403 if (connectstr != NULL) {
1405 "%s: must not provide both -connect option and target parameter\n",
1409 connect_type = use_inet;
1410 connectstr = *opt_rest();
1411 } else if (argc != 0) {
1415 #ifndef OPENSSL_NO_NEXTPROTONEG
1416 if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) {
1417 BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n");
1421 if (proxystr != NULL) {
1423 char *tmp_host = host, *tmp_port = port;
1424 if (connectstr == NULL) {
1425 BIO_printf(bio_err, "%s: -proxy requires use of -connect or target parameter\n", prog);
1428 res = BIO_parse_hostserv(proxystr, &host, &port, BIO_PARSE_PRIO_HOST);
1429 if (tmp_host != host)
1430 OPENSSL_free(tmp_host);
1431 if (tmp_port != port)
1432 OPENSSL_free(tmp_port);
1435 "%s: -proxy argument malformed or ambiguous\n", prog);
1440 char *tmp_host = host, *tmp_port = port;
1441 if (connectstr != NULL)
1442 res = BIO_parse_hostserv(connectstr, &host, &port,
1443 BIO_PARSE_PRIO_HOST);
1444 if (tmp_host != host)
1445 OPENSSL_free(tmp_host);
1446 if (tmp_port != port)
1447 OPENSSL_free(tmp_port);
1450 "%s: -connect argument or target parameter malformed or ambiguous\n",
1456 if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
1458 "Can't use unix sockets and datagrams together\n");
1462 #ifndef OPENSSL_NO_SCTP
1463 if (protocol == IPPROTO_SCTP) {
1464 if (socket_type != SOCK_DGRAM) {
1465 BIO_printf(bio_err, "Can't use -sctp without DTLS\n");
1468 /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1469 socket_type = SOCK_STREAM;
1473 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1474 next_proto.status = -1;
1475 if (next_proto_neg_in) {
1477 next_protos_parse(&next_proto.len, next_proto_neg_in);
1478 if (next_proto.data == NULL) {
1479 BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
1483 next_proto.data = NULL;
1486 if (!app_passwd(passarg, NULL, &pass, NULL)) {
1487 BIO_printf(bio_err, "Error getting password\n");
1491 if (key_file == NULL)
1492 key_file = cert_file;
1494 if (key_file != NULL) {
1495 key = load_key(key_file, key_format, 0, pass, e,
1496 "client certificate private key file");
1498 ERR_print_errors(bio_err);
1503 if (cert_file != NULL) {
1504 cert = load_cert(cert_file, cert_format, "client certificate file");
1506 ERR_print_errors(bio_err);
1511 if (chain_file != NULL) {
1512 if (!load_certs(chain_file, &chain, FORMAT_PEM, NULL,
1513 "client certificate chain"))
1517 if (crl_file != NULL) {
1519 crl = load_crl(crl_file, crl_format);
1521 BIO_puts(bio_err, "Error loading CRL\n");
1522 ERR_print_errors(bio_err);
1525 crls = sk_X509_CRL_new_null();
1526 if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {
1527 BIO_puts(bio_err, "Error adding CRL\n");
1528 ERR_print_errors(bio_err);
1534 if (!load_excert(&exc))
1537 if (!app_RAND_load_file(NULL, 1) && inrand == NULL
1538 && !RAND_status()) {
1540 "warning, not much extra random data, consider using the -rand option\n");
1542 if (inrand != NULL) {
1543 randamt = app_RAND_load_files(inrand);
1544 BIO_printf(bio_err, "%ld semi-random bytes loaded\n", randamt);
1547 if (bio_c_out == NULL) {
1548 if (c_quiet && !c_debug) {
1549 bio_c_out = BIO_new(BIO_s_null());
1550 if (c_msg && bio_c_msg == NULL)
1551 bio_c_msg = dup_bio_out(FORMAT_TEXT);
1552 } else if (bio_c_out == NULL)
1553 bio_c_out = dup_bio_out(FORMAT_TEXT);
1555 #ifndef OPENSSL_NO_SRP
1556 if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) {
1557 BIO_printf(bio_err, "Error getting password\n");
1562 ctx = SSL_CTX_new(meth);
1564 ERR_print_errors(bio_err);
1569 ssl_ctx_security_debug(ctx, sdebug);
1571 if (ssl_config != NULL) {
1572 if (SSL_CTX_config(ctx, ssl_config) == 0) {
1573 BIO_printf(bio_err, "Error using configuration \"%s\"\n",
1575 ERR_print_errors(bio_err);
1580 if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
1582 if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
1585 if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
1586 BIO_printf(bio_err, "Error setting verify params\n");
1587 ERR_print_errors(bio_err);
1592 SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
1595 if (max_send_fragment > 0
1596 && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) {
1597 BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n",
1598 prog, max_send_fragment);
1602 if (split_send_fragment > 0
1603 && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) {
1604 BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n",
1605 prog, split_send_fragment);
1609 if (max_pipelines > 0
1610 && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) {
1611 BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n",
1612 prog, max_pipelines);
1616 if (read_buf_len > 0) {
1617 SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);
1620 if (!config_ctx(cctx, ssl_args, ctx))
1623 if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,
1624 crls, crl_download)) {
1625 BIO_printf(bio_err, "Error loading store locations\n");
1626 ERR_print_errors(bio_err);
1629 if (ReqCAfile != NULL) {
1630 STACK_OF(X509_NAME) *nm = sk_X509_NAME_new_null();
1632 if (nm == NULL || !SSL_add_file_cert_subjects_to_stack(nm, ReqCAfile)) {
1633 sk_X509_NAME_pop_free(nm, X509_NAME_free);
1634 BIO_printf(bio_err, "Error loading CA names\n");
1635 ERR_print_errors(bio_err);
1638 SSL_CTX_set0_CA_list(ctx, nm);
1640 #ifndef OPENSSL_NO_ENGINE
1641 if (ssl_client_engine) {
1642 if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
1643 BIO_puts(bio_err, "Error setting client auth engine\n");
1644 ERR_print_errors(bio_err);
1645 ENGINE_free(ssl_client_engine);
1648 ENGINE_free(ssl_client_engine);
1652 #ifndef OPENSSL_NO_PSK
1653 if (psk_key != NULL) {
1655 BIO_printf(bio_c_out, "PSK key given, setting client callback\n");
1656 SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
1659 #ifndef OPENSSL_NO_SRTP
1660 if (srtp_profiles != NULL) {
1661 /* Returns 0 on success! */
1662 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
1663 BIO_printf(bio_err, "Error setting SRTP profile\n");
1664 ERR_print_errors(bio_err);
1671 ssl_ctx_set_excert(ctx, exc);
1673 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1674 if (next_proto.data != NULL)
1675 SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
1679 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
1682 BIO_printf(bio_err, "Error parsing -alpn argument\n");
1685 /* Returns 0 on success! */
1686 if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) {
1687 BIO_printf(bio_err, "Error setting ALPN\n");
1693 for (i = 0; i < serverinfo_count; i++) {
1694 if (!SSL_CTX_add_client_custom_ext(ctx,
1695 serverinfo_types[i],
1697 serverinfo_cli_parse_cb, NULL)) {
1699 "Warning: Unable to add custom extension %u, skipping\n",
1700 serverinfo_types[i]);
1705 SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1707 #ifndef OPENSSL_NO_CT
1708 /* Enable SCT processing, without early connection termination */
1709 if (ct_validation &&
1710 !SSL_CTX_enable_ct(ctx, SSL_CT_VALIDATION_PERMISSIVE)) {
1711 ERR_print_errors(bio_err);
1715 if (!ctx_set_ctlog_list_file(ctx, ctlog_file)) {
1716 if (ct_validation) {
1717 ERR_print_errors(bio_err);
1722 * If CT validation is not enabled, the log list isn't needed so don't
1723 * show errors or abort. We try to load it regardless because then we
1724 * can show the names of the logs any SCTs came from (SCTs may be seen
1725 * even with validation disabled).
1731 SSL_CTX_set_verify(ctx, verify, verify_callback);
1733 if (!ctx_set_verify_locations(ctx, CAfile, CApath, noCAfile, noCApath)) {
1734 ERR_print_errors(bio_err);
1738 ssl_ctx_add_crls(ctx, crls, crl_download);
1740 if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))
1743 if (!noservername) {
1744 tlsextcbp.biodebug = bio_err;
1745 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1746 SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1748 # ifndef OPENSSL_NO_SRP
1749 if (srp_arg.srplogin) {
1750 if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) {
1751 BIO_printf(bio_err, "Unable to set SRP username\n");
1754 srp_arg.msg = c_msg;
1755 srp_arg.debug = c_debug;
1756 SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);
1757 SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);
1758 SSL_CTX_set_srp_strength(ctx, srp_arg.strength);
1759 if (c_msg || c_debug || srp_arg.amp == 0)
1760 SSL_CTX_set_srp_verify_param_callback(ctx,
1761 ssl_srp_verify_param_cb);
1765 if (dane_tlsa_domain != NULL) {
1766 if (SSL_CTX_dane_enable(ctx) <= 0) {
1768 "%s: Error enabling DANE TLSA authentication.\n",
1770 ERR_print_errors(bio_err);
1776 * In TLSv1.3 NewSessionTicket messages arrive after the handshake and can
1777 * come at any time. Therefore we use a callback to write out the session
1778 * when we know about it. This approach works for < TLSv1.3 as well.
1780 if (sess_out != NULL) {
1781 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT
1782 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1783 SSL_CTX_sess_set_new_cb(ctx, new_session_cb);
1786 if (set_keylog_file(ctx, keylog_file))
1790 if (sess_in != NULL) {
1792 BIO *stmp = BIO_new_file(sess_in, "r");
1794 BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
1795 ERR_print_errors(bio_err);
1798 sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
1801 BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
1802 ERR_print_errors(bio_err);
1805 if (!SSL_set_session(con, sess)) {
1806 BIO_printf(bio_err, "Can't set session\n");
1807 ERR_print_errors(bio_err);
1810 SSL_SESSION_free(sess);
1814 SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
1816 if (!noservername && (servername != NULL || dane_tlsa_domain == NULL)) {
1817 if (servername == NULL)
1818 servername = (host == NULL) ? "localhost" : host;
1819 if (!SSL_set_tlsext_host_name(con, servername)) {
1820 BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
1821 ERR_print_errors(bio_err);
1826 if (dane_tlsa_domain != NULL) {
1827 if (SSL_dane_enable(con, dane_tlsa_domain) <= 0) {
1828 BIO_printf(bio_err, "%s: Error enabling DANE TLSA "
1829 "authentication.\n", prog);
1830 ERR_print_errors(bio_err);
1833 if (dane_tlsa_rrset == NULL) {
1834 BIO_printf(bio_err, "%s: DANE TLSA authentication requires at "
1835 "least one -dane_tlsa_rrdata option.\n", prog);
1838 if (tlsa_import_rrset(con, dane_tlsa_rrset) <= 0) {
1839 BIO_printf(bio_err, "%s: Failed to import any TLSA "
1840 "records.\n", prog);
1843 if (dane_ee_no_name)
1844 SSL_dane_set_flags(con, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
1845 } else if (dane_tlsa_rrset != NULL) {
1846 BIO_printf(bio_err, "%s: DANE TLSA authentication requires the "
1847 "-dane_tlsa_domain option.\n", prog);
1852 if (init_client(&s, host, port, socket_family, socket_type, protocol)
1854 BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
1858 BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s);
1861 if (!BIO_socket_nbio(s, 1)) {
1862 ERR_print_errors(bio_err);
1865 BIO_printf(bio_c_out, "Turned on non blocking io\n");
1867 #ifndef OPENSSL_NO_DTLS
1869 union BIO_sock_info_u peer_info;
1871 #ifndef OPENSSL_NO_SCTP
1872 if (protocol == IPPROTO_SCTP)
1873 sbio = BIO_new_dgram_sctp(s, BIO_NOCLOSE);
1876 sbio = BIO_new_dgram(s, BIO_NOCLOSE);
1878 if ((peer_info.addr = BIO_ADDR_new()) == NULL) {
1879 BIO_printf(bio_err, "memory allocation failure\n");
1883 if (!BIO_sock_info(s, BIO_SOCK_INFO_ADDRESS, &peer_info)) {
1884 BIO_printf(bio_err, "getsockname:errno=%d\n",
1885 get_last_socket_error());
1886 BIO_ADDR_free(peer_info.addr);
1891 (void)BIO_ctrl_set_connected(sbio, peer_info.addr);
1892 BIO_ADDR_free(peer_info.addr);
1893 peer_info.addr = NULL;
1895 if (enable_timeouts) {
1897 timeout.tv_usec = DGRAM_RCV_TIMEOUT;
1898 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
1901 timeout.tv_usec = DGRAM_SND_TIMEOUT;
1902 BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
1906 if (socket_mtu < DTLS_get_link_min_mtu(con)) {
1907 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
1908 DTLS_get_link_min_mtu(con));
1912 SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
1913 if (!DTLS_set_link_mtu(con, socket_mtu)) {
1914 BIO_printf(bio_err, "Failed to set MTU\n");
1919 /* want to do MTU discovery */
1920 BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
1923 #endif /* OPENSSL_NO_DTLS */
1924 sbio = BIO_new_socket(s, BIO_NOCLOSE);
1929 test = BIO_new(BIO_f_nbio_test());
1930 sbio = BIO_push(test, sbio);
1934 BIO_set_callback(sbio, bio_dump_callback);
1935 BIO_set_callback_arg(sbio, (char *)bio_c_out);
1938 #ifndef OPENSSL_NO_SSL_TRACE
1940 SSL_set_msg_callback(con, SSL_trace);
1943 SSL_set_msg_callback(con, msg_cb);
1944 SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);
1947 if (c_tlsextdebug) {
1948 SSL_set_tlsext_debug_callback(con, tlsext_cb);
1949 SSL_set_tlsext_debug_arg(con, bio_c_out);
1951 #ifndef OPENSSL_NO_OCSP
1953 SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
1954 SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
1955 SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
1959 SSL_set_bio(con, sbio, sbio);
1960 SSL_set_connect_state(con);
1962 /* ok, lets connect */
1963 if (fileno_stdin() > SSL_get_fd(con))
1964 width = fileno_stdin() + 1;
1966 width = SSL_get_fd(con) + 1;
1979 switch ((PROTOCOL_CHOICE) starttls_proto) {
1986 * This is an ugly hack that does a lot of assumptions. We do
1987 * have to handle multi-line responses which may come in a single
1988 * packet or not. We therefore have to use BIO_gets() which does
1989 * need a buffering BIO. So during the initial chitchat we do
1990 * push a buffering BIO into the chain that is removed again
1991 * later on to not disturb the rest of the s_client operation.
1994 BIO *fbio = BIO_new(BIO_f_buffer());
1996 BIO_push(fbio, sbio);
1997 /* Wait for multi-line response to end from LMTP or SMTP */
1999 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2000 } while (mbuf_len > 3 && mbuf[3] == '-');
2001 if (starttls_proto == (int)PROTO_LMTP)
2002 BIO_printf(fbio, "LHLO %s\r\n", ehlo);
2004 BIO_printf(fbio, "EHLO %s\r\n", ehlo);
2005 (void)BIO_flush(fbio);
2007 * Wait for multi-line response to end LHLO LMTP or EHLO SMTP
2011 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2012 if (strstr(mbuf, "STARTTLS"))
2014 } while (mbuf_len > 3 && mbuf[3] == '-');
2015 (void)BIO_flush(fbio);
2020 "Didn't find STARTTLS in server response,"
2021 " trying anyway...\n");
2022 BIO_printf(sbio, "STARTTLS\r\n");
2023 BIO_read(sbio, sbuf, BUFSIZZ);
2028 BIO_read(sbio, mbuf, BUFSIZZ);
2029 BIO_printf(sbio, "STLS\r\n");
2030 mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);
2032 BIO_printf(bio_err, "BIO_read failed\n");
2040 BIO *fbio = BIO_new(BIO_f_buffer());
2042 BIO_push(fbio, sbio);
2043 BIO_gets(fbio, mbuf, BUFSIZZ);
2044 /* STARTTLS command requires CAPABILITY... */
2045 BIO_printf(fbio, ". CAPABILITY\r\n");
2046 (void)BIO_flush(fbio);
2047 /* wait for multi-line CAPABILITY response */
2049 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2050 if (strstr(mbuf, "STARTTLS"))
2053 while (mbuf_len > 3 && mbuf[0] != '.');
2054 (void)BIO_flush(fbio);
2059 "Didn't find STARTTLS in server response,"
2060 " trying anyway...\n");
2061 BIO_printf(sbio, ". STARTTLS\r\n");
2062 BIO_read(sbio, sbuf, BUFSIZZ);
2067 BIO *fbio = BIO_new(BIO_f_buffer());
2069 BIO_push(fbio, sbio);
2070 /* wait for multi-line response to end from FTP */
2072 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2074 while (mbuf_len > 3 && mbuf[3] == '-');
2075 (void)BIO_flush(fbio);
2078 BIO_printf(sbio, "AUTH TLS\r\n");
2079 BIO_read(sbio, sbuf, BUFSIZZ);
2083 case PROTO_XMPP_SERVER:
2086 BIO_printf(sbio, "<stream:stream "
2087 "xmlns:stream='http://etherx.jabber.org/streams' "
2088 "xmlns='jabber:%s' to='%s' version='1.0'>",
2089 starttls_proto == PROTO_XMPP ? "client" : "server",
2090 xmpphost ? xmpphost : host);
2091 seen = BIO_read(sbio, mbuf, BUFSIZZ);
2093 BIO_printf(bio_err, "BIO_read failed\n");
2098 (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")
2100 "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\""))
2102 seen = BIO_read(sbio, mbuf, BUFSIZZ);
2110 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
2111 seen = BIO_read(sbio, sbuf, BUFSIZZ);
2113 BIO_printf(bio_err, "BIO_read failed\n");
2117 if (!strstr(sbuf, "<proceed"))
2124 static const unsigned char tls_do[] = {
2125 /* IAC DO START_TLS */
2128 static const unsigned char tls_will[] = {
2129 /* IAC WILL START_TLS */
2132 static const unsigned char tls_follows[] = {
2133 /* IAC SB START_TLS FOLLOWS IAC SE */
2134 255, 250, 46, 1, 255, 240
2138 /* Telnet server should demand we issue START_TLS */
2139 bytes = BIO_read(sbio, mbuf, BUFSIZZ);
2140 if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)
2142 /* Agree to issue START_TLS and send the FOLLOWS sub-command */
2143 BIO_write(sbio, tls_will, 3);
2144 BIO_write(sbio, tls_follows, 6);
2145 (void)BIO_flush(sbio);
2146 /* Telnet server also sent the FOLLOWS sub-command */
2147 bytes = BIO_read(sbio, mbuf, BUFSIZZ);
2148 if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)
2155 error_proto, /* Wrong protocol, not even HTTP */
2156 error_connect, /* CONNECT failed */
2158 } foundit = error_connect;
2159 BIO *fbio = BIO_new(BIO_f_buffer());
2161 BIO_push(fbio, sbio);
2162 BIO_printf(fbio, "CONNECT %s HTTP/1.0\r\n\r\n", connectstr);
2163 (void)BIO_flush(fbio);
2165 * The first line is the HTTP response. According to RFC 7230,
2166 * it's formated exactly like this:
2168 * HTTP/d.d ddd Reason text\r\n
2170 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2171 if (mbuf_len < (int)strlen("HTTP/1.0 200")) {
2173 "%s: HTTP CONNECT failed, insufficient response "
2174 "from proxy (got %d octets)\n", prog, mbuf_len);
2175 (void)BIO_flush(fbio);
2180 if (mbuf[8] != ' ') {
2182 "%s: HTTP CONNECT failed, incorrect response "
2183 "from proxy\n", prog);
2184 foundit = error_proto;
2185 } else if (mbuf[9] != '2') {
2186 BIO_printf(bio_err, "%s: HTTP CONNECT failed: %s ", prog,
2191 if (foundit != error_proto) {
2192 /* Read past all following headers */
2194 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2195 } while (mbuf_len > 2);
2197 (void)BIO_flush(fbio);
2200 if (foundit != success) {
2208 BIO *fbio = BIO_new(BIO_f_buffer());
2210 BIO_push(fbio, sbio);
2211 BIO_printf(fbio, "STARTTLS\r\n");
2212 (void)BIO_flush(fbio);
2213 width = SSL_get_fd(con) + 1;
2219 openssl_fdset(SSL_get_fd(con), &readfds);
2220 timeout.tv_sec = S_CLIENT_IRC_READ_TIMEOUT;
2221 timeout.tv_usec = 0;
2223 * If the IRCd doesn't respond within
2224 * S_CLIENT_IRC_READ_TIMEOUT seconds, assume
2225 * it doesn't support STARTTLS. Many IRCds
2226 * will not give _any_ sort of response to a
2227 * STARTTLS command when it's not supported.
2229 if (!BIO_get_buffer_num_lines(fbio)
2230 && !BIO_pending(fbio)
2231 && !BIO_pending(sbio)
2232 && select(width, (void *)&readfds, NULL, NULL,
2235 "Timeout waiting for response (%d seconds).\n",
2236 S_CLIENT_IRC_READ_TIMEOUT);
2240 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2241 if (mbuf_len < 1 || sscanf(mbuf, "%*s %d", &numeric) != 1)
2243 /* :example.net 451 STARTTLS :You have not registered */
2244 /* :example.net 421 STARTTLS :Unknown command */
2245 if ((numeric == 451 || numeric == 421)
2246 && strstr(mbuf, "STARTTLS") != NULL) {
2247 BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
2250 if (numeric == 691) {
2251 BIO_printf(bio_err, "STARTTLS negotiation failed: ");
2252 ERR_print_errors(bio_err);
2255 } while (numeric != 670);
2257 (void)BIO_flush(fbio);
2260 if (numeric != 670) {
2261 BIO_printf(bio_err, "Server does not support STARTTLS.\n");
2269 /* SSL request packet */
2270 static const unsigned char ssl_req[] = {
2271 /* payload_length, sequence_id */
2272 0x20, 0x00, 0x00, 0x01,
2274 /* capability flags, CLIENT_SSL always set */
2275 0x85, 0xae, 0x7f, 0x00,
2276 /* max-packet size */
2277 0x00, 0x00, 0x00, 0x01,
2280 /* string[23] reserved (all [0]) */
2281 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2283 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2286 int ssl_flg = 0x800;
2288 const unsigned char *packet = (const unsigned char *)sbuf;
2290 /* Receiving Initial Handshake packet. */
2291 bytes = BIO_read(sbio, (void *)packet, BUFSIZZ);
2293 BIO_printf(bio_err, "BIO_read failed\n");
2295 /* Packet length[3], Packet number[1] + minimum payload[17] */
2296 } else if (bytes < 21) {
2297 BIO_printf(bio_err, "MySQL packet too short.\n");
2299 } else if (bytes != (4 + packet[0] +
2301 (packet[2] << 16))) {
2302 BIO_printf(bio_err, "MySQL packet length does not match.\n");
2304 /* protocol version[1] */
2305 } else if (packet[4] != 0xA) {
2307 "Only MySQL protocol version 10 is supported.\n");
2312 /* server version[string+NULL] */
2315 BIO_printf(bio_err, "Cannot confirm server version. ");
2317 } else if (packet[pos++] == '\0') {
2323 /* make sure we have more 15 bytes left in the packet */
2324 if (pos + 15 > bytes) {
2326 "MySQL server handshake packet is broken.\n");
2330 pos += 12; /* skip over conn id[4] + SALT[8] */
2331 if (packet[pos++] != '\0') { /* verify filler */
2333 "MySQL packet is broken.\n");
2337 /* capability flags[2] */
2338 if (!((packet[pos] + (packet[pos + 1] << 8)) & ssl_flg)) {
2339 BIO_printf(bio_err, "MySQL server does not support SSL.\n");
2343 /* Sending SSL Handshake packet. */
2344 BIO_write(sbio, ssl_req, sizeof(ssl_req));
2345 (void)BIO_flush(sbio);
2348 case PROTO_POSTGRES:
2350 static const unsigned char ssl_request[] = {
2351 /* Length SSLRequest */
2352 0, 0, 0, 8, 4, 210, 22, 47
2356 /* Send SSLRequest packet */
2357 BIO_write(sbio, ssl_request, 8);
2358 (void)BIO_flush(sbio);
2360 /* Reply will be a single S if SSL is enabled */
2361 bytes = BIO_read(sbio, sbuf, BUFSIZZ);
2362 if (bytes != 1 || sbuf[0] != 'S')
2369 BIO *fbio = BIO_new(BIO_f_buffer());
2371 BIO_push(fbio, sbio);
2372 BIO_gets(fbio, mbuf, BUFSIZZ);
2373 /* STARTTLS command requires CAPABILITIES... */
2374 BIO_printf(fbio, "CAPABILITIES\r\n");
2375 (void)BIO_flush(fbio);
2376 /* wait for multi-line CAPABILITIES response */
2378 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2379 if (strstr(mbuf, "STARTTLS"))
2381 } while (mbuf_len > 1 && mbuf[0] != '.');
2382 (void)BIO_flush(fbio);
2387 "Didn't find STARTTLS in server response,"
2388 " trying anyway...\n");
2389 BIO_printf(sbio, "STARTTLS\r\n");
2390 mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
2392 BIO_printf(bio_err, "BIO_read failed\n");
2395 mbuf[mbuf_len] = '\0';
2396 if (strstr(mbuf, "382") == NULL) {
2397 BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
2405 BIO *fbio = BIO_new(BIO_f_buffer());
2407 BIO_push(fbio, sbio);
2408 /* wait for multi-line response to end from Sieve */
2410 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2412 * According to RFC 5804 § 1.7, capability
2413 * is case-insensitive, make it uppercase
2415 if (mbuf_len > 1 && mbuf[0] == '"') {
2416 make_uppercase(mbuf);
2417 if (strncmp(mbuf, "\"STARTTLS\"", 10) == 0)
2420 } while (mbuf_len > 1 && mbuf[0] == '"');
2421 (void)BIO_flush(fbio);
2426 "Didn't find STARTTLS in server response,"
2427 " trying anyway...\n");
2428 BIO_printf(sbio, "STARTTLS\r\n");
2429 mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
2431 BIO_printf(bio_err, "BIO_read failed\n");
2434 mbuf[mbuf_len] = '\0';
2436 BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
2440 * According to RFC 5804 § 2.2, response codes are case-
2441 * insensitive, make it uppercase but preserve the response.
2443 strncpy(sbuf, mbuf, 2);
2444 make_uppercase(sbuf);
2445 if (strncmp(sbuf, "OK", 2) != 0) {
2446 BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
2453 /* StartTLS Operation according to RFC 4511 */
2454 static char ldap_tls_genconf[] = "asn1=SEQUENCE:LDAPMessage\n"
2456 "messageID=INTEGER:1\n"
2457 "extendedReq=EXPLICIT:23A,IMPLICIT:0C,"
2458 "FORMAT:ASCII,OCT:1.3.6.1.4.1.1466.20037\n";
2460 char *genstr = NULL;
2462 ASN1_TYPE *atyp = NULL;
2463 BIO *ldapbio = BIO_new(BIO_s_mem());
2464 CONF *cnf = NCONF_new(NULL);
2470 BIO_puts(ldapbio, ldap_tls_genconf);
2471 if (NCONF_load_bio(cnf, ldapbio, &errline) <= 0) {
2475 BIO_printf(bio_err, "NCONF_load_bio failed\n");
2478 BIO_printf(bio_err, "Error on line %ld\n", errline);
2483 genstr = NCONF_get_string(cnf, "default", "asn1");
2484 if (genstr == NULL) {
2486 BIO_printf(bio_err, "NCONF_get_string failed\n");
2489 atyp = ASN1_generate_nconf(genstr, cnf);
2492 BIO_printf(bio_err, "ASN1_generate_nconf failed\n");
2497 /* Send SSLRequest packet */
2498 BIO_write(sbio, atyp->value.sequence->data,
2499 atyp->value.sequence->length);
2500 (void)BIO_flush(sbio);
2501 ASN1_TYPE_free(atyp);
2503 mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
2505 BIO_printf(bio_err, "BIO_read failed\n");
2508 result = ldap_ExtendedResponse_parse(mbuf, mbuf_len);
2510 BIO_printf(bio_err, "ldap_ExtendedResponse_parse failed\n");
2512 } else if (result > 0) {
2513 BIO_printf(bio_err, "STARTTLS failed, LDAP Result Code: %i\n",
2522 if (early_data_file != NULL
2523 && SSL_get0_session(con) != NULL
2524 && SSL_SESSION_get_max_early_data(SSL_get0_session(con)) > 0) {
2525 BIO *edfile = BIO_new_file(early_data_file, "r");
2526 size_t readbytes, writtenbytes;
2529 if (edfile == NULL) {
2530 BIO_printf(bio_err, "Cannot open early data file\n");
2535 if (!BIO_read_ex(edfile, cbuf, BUFSIZZ, &readbytes))
2538 while (!SSL_write_early_data(con, cbuf, readbytes, &writtenbytes)) {
2539 switch (SSL_get_error(con, 0)) {
2540 case SSL_ERROR_WANT_WRITE:
2541 case SSL_ERROR_WANT_ASYNC:
2542 case SSL_ERROR_WANT_READ:
2543 /* Just keep trying - busy waiting */
2546 BIO_printf(bio_err, "Error writing early data\n");
2560 if ((SSL_version(con) == DTLS1_VERSION) &&
2561 DTLSv1_get_timeout(con, &timeout))
2562 timeoutp = &timeout;
2566 if (!SSL_is_init_finished(con) && SSL_total_renegotiations(con) == 0
2567 && SSL_get_key_update_type(con) == SSL_KEY_UPDATE_NONE) {
2576 BIO_puts(bio_err, "CONNECTION ESTABLISHED\n");
2577 print_ssl_summary(con);
2580 print_stuff(bio_c_out, con, full_log);
2584 if (starttls_proto) {
2585 BIO_write(bio_err, mbuf, mbuf_len);
2586 /* We don't need to know any more */
2588 starttls_proto = PROTO_OFF;
2593 BIO_printf(bio_c_out,
2594 "drop connection and then reconnect\n");
2595 do_ssl_shutdown(con);
2596 SSL_set_connect_state(con);
2597 BIO_closesocket(SSL_get_fd(con));
2603 ssl_pending = read_ssl && SSL_has_pending(con);
2606 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2609 * Note that select() returns when read _would not block_,
2610 * and EOF satisfies that. To avoid a CPU-hogging loop,
2611 * set the flag so we exit.
2613 if (read_tty && !at_eof)
2614 openssl_fdset(fileno_stdin(), &readfds);
2615 #if !defined(OPENSSL_SYS_VMS)
2617 openssl_fdset(fileno_stdout(), &writefds);
2621 openssl_fdset(SSL_get_fd(con), &readfds);
2623 openssl_fdset(SSL_get_fd(con), &writefds);
2625 if (!tty_on || !write_tty) {
2627 openssl_fdset(SSL_get_fd(con), &readfds);
2629 openssl_fdset(SSL_get_fd(con), &writefds);
2634 * Note: under VMS with SOCKETSHR the second parameter is
2635 * currently of type (int *) whereas under other systems it is
2636 * (void *) if you don't have a cast it will choke the compiler:
2637 * if you do have a cast then you can either go for (int *) or
2640 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
2642 * Under Windows/DOS we make the assumption that we can always
2643 * write to the tty: therefore if we need to write to the tty we
2644 * just fall through. Otherwise we timeout the select every
2645 * second and see if there are any keypresses. Note: this is a
2646 * hack, in a proper Windows application we wouldn't do this.
2653 i = select(width, (void *)&readfds, (void *)&writefds,
2655 if (!i && (!has_stdin_waiting() || !read_tty))
2658 i = select(width, (void *)&readfds, (void *)&writefds,
2662 i = select(width, (void *)&readfds, (void *)&writefds,
2666 BIO_printf(bio_err, "bad select %d\n",
2667 get_last_socket_error());
2672 if ((SSL_version(con) == DTLS1_VERSION)
2673 && DTLSv1_handle_timeout(con) > 0) {
2674 BIO_printf(bio_err, "TIMEOUT occurred\n");
2677 if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
2678 k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);
2679 switch (SSL_get_error(con, k)) {
2680 case SSL_ERROR_NONE:
2685 /* we have done a write(con,NULL,0); */
2686 if (cbuf_len <= 0) {
2689 } else { /* if (cbuf_len > 0) */
2695 case SSL_ERROR_WANT_WRITE:
2696 BIO_printf(bio_c_out, "write W BLOCK\n");
2700 case SSL_ERROR_WANT_ASYNC:
2701 BIO_printf(bio_c_out, "write A BLOCK\n");
2702 wait_for_async(con);
2706 case SSL_ERROR_WANT_READ:
2707 BIO_printf(bio_c_out, "write R BLOCK\n");
2712 case SSL_ERROR_WANT_X509_LOOKUP:
2713 BIO_printf(bio_c_out, "write X BLOCK\n");
2715 case SSL_ERROR_ZERO_RETURN:
2716 if (cbuf_len != 0) {
2717 BIO_printf(bio_c_out, "shutdown\n");
2726 case SSL_ERROR_SYSCALL:
2727 if ((k != 0) || (cbuf_len != 0)) {
2728 BIO_printf(bio_err, "write:errno=%d\n",
2729 get_last_socket_error());
2736 case SSL_ERROR_WANT_ASYNC_JOB:
2737 /* This shouldn't ever happen in s_client - treat as an error */
2739 ERR_print_errors(bio_err);
2743 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VMS)
2744 /* Assume Windows/DOS/BeOS can always write */
2745 else if (!ssl_pending && write_tty)
2747 else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds))
2750 #ifdef CHARSET_EBCDIC
2751 ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);
2753 i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);
2756 BIO_printf(bio_c_out, "DONE\n");
2763 if (sbuf_len <= 0) {
2767 } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
2772 SSL_renegotiate(con);
2777 k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
2779 switch (SSL_get_error(con, k)) {
2780 case SSL_ERROR_NONE:
2789 case SSL_ERROR_WANT_ASYNC:
2790 BIO_printf(bio_c_out, "read A BLOCK\n");
2791 wait_for_async(con);
2794 if ((read_tty == 0) && (write_ssl == 0))
2797 case SSL_ERROR_WANT_WRITE:
2798 BIO_printf(bio_c_out, "read W BLOCK\n");
2802 case SSL_ERROR_WANT_READ:
2803 BIO_printf(bio_c_out, "read R BLOCK\n");
2806 if ((read_tty == 0) && (write_ssl == 0))
2809 case SSL_ERROR_WANT_X509_LOOKUP:
2810 BIO_printf(bio_c_out, "read X BLOCK\n");
2812 case SSL_ERROR_SYSCALL:
2813 ret = get_last_socket_error();
2815 BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
2817 BIO_printf(bio_err, "read:errno=%d\n", ret);
2819 case SSL_ERROR_ZERO_RETURN:
2820 BIO_printf(bio_c_out, "closed\n");
2823 case SSL_ERROR_WANT_ASYNC_JOB:
2824 /* This shouldn't ever happen in s_client. Treat as an error */
2826 ERR_print_errors(bio_err);
2830 /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */
2831 #if defined(OPENSSL_SYS_MSDOS)
2832 else if (has_stdin_waiting())
2834 else if (FD_ISSET(fileno_stdin(), &readfds))
2840 i = raw_read_stdin(cbuf, BUFSIZZ / 2);
2842 /* both loops are skipped when i <= 0 */
2843 for (j = 0; j < i; j++)
2844 if (cbuf[j] == '\n')
2846 for (j = i - 1; j >= 0; j--) {
2847 cbuf[j + lf_num] = cbuf[j];
2848 if (cbuf[j] == '\n') {
2851 cbuf[j + lf_num] = '\r';
2854 assert(lf_num == 0);
2856 i = raw_read_stdin(cbuf, BUFSIZZ);
2857 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
2862 if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q' && cmdletters))) {
2863 BIO_printf(bio_err, "DONE\n");
2868 if ((!c_ign_eof) && (cbuf[0] == 'R' && cmdletters)) {
2869 BIO_printf(bio_err, "RENEGOTIATING\n");
2870 SSL_renegotiate(con);
2874 if (!c_ign_eof && (cbuf[0] == 'K' || cbuf[0] == 'k' )
2876 BIO_printf(bio_err, "KEYUPDATE\n");
2878 cbuf[0] == 'K' ? SSL_KEY_UPDATE_REQUESTED
2879 : SSL_KEY_UPDATE_NOT_REQUESTED);
2882 #ifndef OPENSSL_NO_HEARTBEATS
2883 else if ((!c_ign_eof) && (cbuf[0] == 'B' && cmdletters)) {
2884 BIO_printf(bio_err, "HEARTBEATING\n");
2892 #ifdef CHARSET_EBCDIC
2893 ebcdic2ascii(cbuf, cbuf, i);
2905 print_stuff(bio_c_out, con, full_log);
2906 do_ssl_shutdown(con);
2907 #if defined(OPENSSL_SYS_WINDOWS)
2909 * Give the socket time to send its last data before we close it.
2910 * No amount of setting SO_LINGER etc on the socket seems to persuade
2911 * Windows to send the data before closing the socket...but sleeping
2912 * for a short time seems to do it (units in ms)
2913 * TODO: Find a better way to do this
2917 BIO_closesocket(SSL_get_fd(con));
2921 print_stuff(bio_c_out, con, 1);
2924 #if !defined(OPENSSL_NO_NEXTPROTONEG)
2925 OPENSSL_free(next_proto.data);
2928 set_keylog_file(NULL, NULL);
2930 sk_X509_CRL_pop_free(crls, X509_CRL_free);
2932 sk_X509_pop_free(chain, X509_free);
2934 #ifndef OPENSSL_NO_SRP
2935 OPENSSL_free(srp_arg.srppassin);
2937 OPENSSL_free(connectstr);
2940 X509_VERIFY_PARAM_free(vpm);
2941 ssl_excert_free(exc);
2942 sk_OPENSSL_STRING_free(ssl_args);
2943 sk_OPENSSL_STRING_free(dane_tlsa_rrset);
2944 SSL_CONF_CTX_free(cctx);
2945 OPENSSL_clear_free(cbuf, BUFSIZZ);
2946 OPENSSL_clear_free(sbuf, BUFSIZZ);
2947 OPENSSL_clear_free(mbuf, BUFSIZZ);
2949 BIO_free(bio_c_out);
2951 BIO_free(bio_c_msg);
2956 static void print_stuff(BIO *bio, SSL *s, int full)
2960 const SSL_CIPHER *c;
2962 #ifndef OPENSSL_NO_COMP
2963 const COMP_METHOD *comp, *expansion;
2965 unsigned char *exportedkeymat;
2966 #ifndef OPENSSL_NO_CT
2967 const SSL_CTX *ctx = SSL_get_SSL_CTX(s);
2971 int got_a_chain = 0;
2973 sk = SSL_get_peer_cert_chain(s);
2977 BIO_printf(bio, "---\nCertificate chain\n");
2978 for (i = 0; i < sk_X509_num(sk); i++) {
2979 BIO_printf(bio, "%2d s:", i);
2980 X509_NAME_print_ex(bio, X509_get_subject_name(sk_X509_value(sk, i)), 0, get_nameopt());
2981 BIO_puts(bio, "\n");
2982 BIO_printf(bio, " i:");
2983 X509_NAME_print_ex(bio, X509_get_issuer_name(sk_X509_value(sk, i)), 0, get_nameopt());
2984 BIO_puts(bio, "\n");
2986 PEM_write_bio_X509(bio, sk_X509_value(sk, i));
2990 BIO_printf(bio, "---\n");
2991 peer = SSL_get_peer_certificate(s);
2993 BIO_printf(bio, "Server certificate\n");
2995 /* Redundant if we showed the whole chain */
2996 if (!(c_showcerts && got_a_chain))
2997 PEM_write_bio_X509(bio, peer);
2998 dump_cert_text(bio, peer);
3000 BIO_printf(bio, "no peer certificate available\n");
3002 print_ca_names(bio, s);
3004 ssl_print_sigalgs(bio, s);
3005 ssl_print_tmp_key(bio, s);
3007 #ifndef OPENSSL_NO_CT
3009 * When the SSL session is anonymous, or resumed via an abbreviated
3010 * handshake, no SCTs are provided as part of the handshake. While in
3011 * a resumed session SCTs may be present in the session's certificate,
3012 * no callbacks are invoked to revalidate these, and in any case that
3013 * set of SCTs may be incomplete. Thus it makes little sense to
3014 * attempt to display SCTs from a resumed session's certificate, and of
3015 * course none are associated with an anonymous peer.
3017 if (peer != NULL && !SSL_session_reused(s) && SSL_ct_is_enabled(s)) {
3018 const STACK_OF(SCT) *scts = SSL_get0_peer_scts(s);
3019 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
3021 BIO_printf(bio, "---\nSCTs present (%i)\n", sct_count);
3022 if (sct_count > 0) {
3023 const CTLOG_STORE *log_store = SSL_CTX_get0_ctlog_store(ctx);
3025 BIO_printf(bio, "---\n");
3026 for (i = 0; i < sct_count; ++i) {
3027 SCT *sct = sk_SCT_value(scts, i);
3029 BIO_printf(bio, "SCT validation status: %s\n",
3030 SCT_validation_status_string(sct));
3031 SCT_print(sct, bio, 0, log_store);
3032 if (i < sct_count - 1)
3033 BIO_printf(bio, "\n---\n");
3035 BIO_printf(bio, "\n");
3041 "---\nSSL handshake has read %ju bytes "
3042 "and written %ju bytes\n",
3043 BIO_number_read(SSL_get_rbio(s)),
3044 BIO_number_written(SSL_get_wbio(s)));
3046 print_verify_detail(s, bio);
3047 BIO_printf(bio, (SSL_session_reused(s) ? "---\nReused, " : "---\nNew, "));
3048 c = SSL_get_current_cipher(s);
3049 BIO_printf(bio, "%s, Cipher is %s\n",
3050 SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
3054 pktmp = X509_get0_pubkey(peer);
3055 BIO_printf(bio, "Server public key is %d bit\n",
3056 EVP_PKEY_bits(pktmp));
3058 BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
3059 SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
3060 #ifndef OPENSSL_NO_COMP
3061 comp = SSL_get_current_compression(s);
3062 expansion = SSL_get_current_expansion(s);
3063 BIO_printf(bio, "Compression: %s\n",
3064 comp ? SSL_COMP_get_name(comp) : "NONE");
3065 BIO_printf(bio, "Expansion: %s\n",
3066 expansion ? SSL_COMP_get_name(expansion) : "NONE");
3071 /* Print out local port of connection: useful for debugging */
3073 union BIO_sock_info_u info;
3075 sock = SSL_get_fd(s);
3076 if ((info.addr = BIO_ADDR_new()) != NULL
3077 && BIO_sock_info(sock, BIO_SOCK_INFO_ADDRESS, &info)) {
3078 BIO_printf(bio_c_out, "LOCAL PORT is %u\n",
3079 ntohs(BIO_ADDR_rawport(info.addr)));
3081 BIO_ADDR_free(info.addr);
3085 #if !defined(OPENSSL_NO_NEXTPROTONEG)
3086 if (next_proto.status != -1) {
3087 const unsigned char *proto;
3088 unsigned int proto_len;
3089 SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
3090 BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
3091 BIO_write(bio, proto, proto_len);
3092 BIO_write(bio, "\n", 1);
3096 const unsigned char *proto;
3097 unsigned int proto_len;
3098 SSL_get0_alpn_selected(s, &proto, &proto_len);
3099 if (proto_len > 0) {
3100 BIO_printf(bio, "ALPN protocol: ");
3101 BIO_write(bio, proto, proto_len);
3102 BIO_write(bio, "\n", 1);
3104 BIO_printf(bio, "No ALPN negotiated\n");
3107 #ifndef OPENSSL_NO_SRTP
3109 SRTP_PROTECTION_PROFILE *srtp_profile =
3110 SSL_get_selected_srtp_profile(s);
3113 BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
3114 srtp_profile->name);
3118 if (SSL_version(s) == TLS1_3_VERSION) {
3119 switch (SSL_get_early_data_status(s)) {
3120 case SSL_EARLY_DATA_NOT_SENT:
3121 BIO_printf(bio, "Early data was not sent\n");
3124 case SSL_EARLY_DATA_REJECTED:
3125 BIO_printf(bio, "Early data was rejected\n");
3128 case SSL_EARLY_DATA_ACCEPTED:
3129 BIO_printf(bio, "Early data was accepted\n");
3135 SSL_SESSION_print(bio, SSL_get_session(s));
3136 if (SSL_get_session(s) != NULL && keymatexportlabel != NULL) {
3137 BIO_printf(bio, "Keying material exporter:\n");
3138 BIO_printf(bio, " Label: '%s'\n", keymatexportlabel);
3139 BIO_printf(bio, " Length: %i bytes\n", keymatexportlen);
3140 exportedkeymat = app_malloc(keymatexportlen, "export key");
3141 if (!SSL_export_keying_material(s, exportedkeymat,
3144 strlen(keymatexportlabel),
3146 BIO_printf(bio, " Error\n");
3148 BIO_printf(bio, " Keying material: ");
3149 for (i = 0; i < keymatexportlen; i++)
3150 BIO_printf(bio, "%02X", exportedkeymat[i]);
3151 BIO_printf(bio, "\n");
3153 OPENSSL_free(exportedkeymat);
3155 BIO_printf(bio, "---\n");
3157 /* flush, or debugging output gets mixed with http response */
3158 (void)BIO_flush(bio);
3161 # ifndef OPENSSL_NO_OCSP
3162 static int ocsp_resp_cb(SSL *s, void *arg)
3164 const unsigned char *p;
3167 len = SSL_get_tlsext_status_ocsp_resp(s, &p);
3168 BIO_puts(arg, "OCSP response: ");
3170 BIO_puts(arg, "no response sent\n");
3173 rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
3175 BIO_puts(arg, "response parse error\n");
3176 BIO_dump_indent(arg, (char *)p, len, 4);
3179 BIO_puts(arg, "\n======================================\n");
3180 OCSP_RESPONSE_print(arg, rsp, 0);
3181 BIO_puts(arg, "======================================\n");
3182 OCSP_RESPONSE_free(rsp);
3187 static int ldap_ExtendedResponse_parse(const char *buf, long rem)
3189 const unsigned char *cur, *end;
3191 int tag, xclass, inf, ret = -1;
3193 cur = (const unsigned char *)buf;
3199 * LDAPMessage ::= SEQUENCE {
3200 * messageID MessageID,
3201 * protocolOp CHOICE {
3203 * extendedResp ExtendedResponse,
3205 * controls [0] Controls OPTIONAL }
3207 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3208 * COMPONENTS OF LDAPResult,
3209 * responseName [10] LDAPOID OPTIONAL,
3210 * responseValue [11] OCTET STRING OPTIONAL }
3212 * LDAPResult ::= SEQUENCE {
3213 * resultCode ENUMERATED {
3219 * diagnosticMessage LDAPString,
3220 * referral [3] Referral OPTIONAL }
3224 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3225 if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE ||
3226 (rem = end - cur, len > rem)) {
3227 BIO_printf(bio_err, "Unexpected LDAP response\n");
3231 rem = len; /* ensure that we don't overstep the SEQUENCE */
3233 /* pull MessageID */
3234 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3235 if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_INTEGER ||
3236 (rem = end - cur, len > rem)) {
3237 BIO_printf(bio_err, "No MessageID\n");
3241 cur += len; /* shall we check for MessageId match or just skip? */
3243 /* pull [APPLICATION 24] */
3245 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3246 if (inf != V_ASN1_CONSTRUCTED || xclass != V_ASN1_APPLICATION ||
3248 BIO_printf(bio_err, "Not ExtendedResponse\n");
3252 /* pull resultCode */
3254 inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3255 if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_ENUMERATED || len == 0 ||
3256 (rem = end - cur, len > rem)) {
3257 BIO_printf(bio_err, "Not LDAPResult\n");
3261 /* len should always be one, but just in case... */
3262 for (ret = 0, inf = 0; inf < len; inf++) {
3266 /* There is more data, but we don't care... */
3271 #endif /* OPENSSL_NO_SOCK */