xref: /freebsd/crypto/openssl/apps/s_client.c (revision aa7957345732816fb0ba8308798d2f79f45597f9)
1e71b7053SJung-uk Kim /*
2*aa795734SPierre Pronchery  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
31f13597dSJung-uk Kim  * Copyright 2005 Nokia. All rights reserved.
41f13597dSJung-uk Kim  *
5b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
6e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
7e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
8e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
91f13597dSJung-uk Kim  */
1074664626SKris Kennaway 
11e71b7053SJung-uk Kim #include "e_os.h"
121f13597dSJung-uk Kim #include <ctype.h>
1374664626SKris Kennaway #include <stdio.h>
1474664626SKris Kennaway #include <stdlib.h>
1574664626SKris Kennaway #include <string.h>
16e71b7053SJung-uk Kim #include <errno.h>
175c87c606SMark Murray #include <openssl/e_os2.h>
18e71b7053SJung-uk Kim 
19e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SOCK
2074664626SKris Kennaway 
216f9291ceSJung-uk Kim /*
226f9291ceSJung-uk Kim  * With IPv6, it looks like Digital has mixed up the proper order of
236f9291ceSJung-uk Kim  * recursive header file inclusion, resulting in the compiler complaining
246f9291ceSJung-uk Kim  * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
256f9291ceSJung-uk Kim  * needed to have fileno() declared correctly...  So let's define u_int
266f9291ceSJung-uk Kim  */
275c87c606SMark Murray #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
2874664626SKris Kennaway # define __U_INT
2974664626SKris Kennaway typedef unsigned int u_int;
3074664626SKris Kennaway #endif
3174664626SKris Kennaway 
3274664626SKris Kennaway #include "apps.h"
33e71b7053SJung-uk Kim #include "progs.h"
3474664626SKris Kennaway #include <openssl/x509.h>
3574664626SKris Kennaway #include <openssl/ssl.h>
3674664626SKris Kennaway #include <openssl/err.h>
3774664626SKris Kennaway #include <openssl/pem.h>
385740a5e3SKris Kennaway #include <openssl/rand.h>
39db522d3aSSimon L. B. Nielsen #include <openssl/ocsp.h>
401f13597dSJung-uk Kim #include <openssl/bn.h>
41b077aed3SPierre Pronchery #include <openssl/trace.h>
42e71b7053SJung-uk Kim #include <openssl/async.h>
43e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
44e71b7053SJung-uk Kim # include <openssl/ct.h>
45e71b7053SJung-uk Kim #endif
4674664626SKris Kennaway #include "s_apps.h"
473b4e3dcbSSimon L. B. Nielsen #include "timeouts.h"
48e71b7053SJung-uk Kim #include "internal/sockets.h"
4974664626SKris Kennaway 
50e71b7053SJung-uk Kim #if defined(__has_feature)
51e71b7053SJung-uk Kim # if __has_feature(memory_sanitizer)
52e71b7053SJung-uk Kim #  include <sanitizer/msan_interface.h>
5374664626SKris Kennaway # endif
541f13597dSJung-uk Kim #endif
551f13597dSJung-uk Kim 
5674664626SKris Kennaway #undef BUFSIZZ
5774664626SKris Kennaway #define BUFSIZZ 1024*8
58e71b7053SJung-uk Kim #define S_CLIENT_IRC_READ_TIMEOUT 8
5974664626SKris Kennaway 
60e71b7053SJung-uk Kim static char *prog;
6174664626SKris Kennaway static int c_debug = 0;
6274664626SKris Kennaway static int c_showcerts = 0;
631f13597dSJung-uk Kim static char *keymatexportlabel = NULL;
641f13597dSJung-uk Kim static int keymatexportlen = 20;
65e71b7053SJung-uk Kim static BIO *bio_c_out = NULL;
66e71b7053SJung-uk Kim static int c_quiet = 0;
67e71b7053SJung-uk Kim static char *sess_out = NULL;
68e71b7053SJung-uk Kim static SSL_SESSION *psksess = NULL;
691f13597dSJung-uk Kim 
7074664626SKris Kennaway static void print_stuff(BIO *berr, SSL *con, int full);
71e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
72db522d3aSSimon L. B. Nielsen static int ocsp_resp_cb(SSL *s, void *arg);
73db522d3aSSimon L. B. Nielsen #endif
74e71b7053SJung-uk Kim static int ldap_ExtendedResponse_parse(const char *buf, long rem);
756935a639SJung-uk Kim static int is_dNS_name(const char *host);
7674664626SKris Kennaway 
77e71b7053SJung-uk Kim static int saved_errno;
78e71b7053SJung-uk Kim 
save_errno(void)79e71b7053SJung-uk Kim static void save_errno(void)
80e71b7053SJung-uk Kim {
81e71b7053SJung-uk Kim     saved_errno = errno;
82e71b7053SJung-uk Kim     errno = 0;
83e71b7053SJung-uk Kim }
84e71b7053SJung-uk Kim 
restore_errno(void)85e71b7053SJung-uk Kim static int restore_errno(void)
86e71b7053SJung-uk Kim {
87e71b7053SJung-uk Kim     int ret = errno;
88e71b7053SJung-uk Kim     errno = saved_errno;
89e71b7053SJung-uk Kim     return ret;
90e71b7053SJung-uk Kim }
91e71b7053SJung-uk Kim 
921f13597dSJung-uk Kim /* Default PSK identity and key */
931f13597dSJung-uk Kim static char *psk_identity = "Client_identity";
941f13597dSJung-uk Kim 
95e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
psk_client_cb(SSL * ssl,const char * hint,char * identity,unsigned int max_identity_len,unsigned char * psk,unsigned int max_psk_len)961f13597dSJung-uk Kim static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
976f9291ceSJung-uk Kim                                   unsigned int max_identity_len,
986f9291ceSJung-uk Kim                                   unsigned char *psk,
991f13597dSJung-uk Kim                                   unsigned int max_psk_len)
1001f13597dSJung-uk Kim {
1011f13597dSJung-uk Kim     int ret;
102aeb5019cSJung-uk Kim     long key_len;
103aeb5019cSJung-uk Kim     unsigned char *key;
1041f13597dSJung-uk Kim 
1051f13597dSJung-uk Kim     if (c_debug)
1061f13597dSJung-uk Kim         BIO_printf(bio_c_out, "psk_client_cb\n");
1076f9291ceSJung-uk Kim     if (!hint) {
1081f13597dSJung-uk Kim         /* no ServerKeyExchange message */
1091f13597dSJung-uk Kim         if (c_debug)
1106f9291ceSJung-uk Kim             BIO_printf(bio_c_out,
1116f9291ceSJung-uk Kim                        "NULL received PSK identity hint, continuing anyway\n");
112e71b7053SJung-uk Kim     } else if (c_debug) {
1131f13597dSJung-uk Kim         BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
114e71b7053SJung-uk Kim     }
1151f13597dSJung-uk Kim 
1166f9291ceSJung-uk Kim     /*
1176f9291ceSJung-uk Kim      * lookup PSK identity and PSK key based on the given identity hint here
1186f9291ceSJung-uk Kim      */
1191f13597dSJung-uk Kim     ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
1201f13597dSJung-uk Kim     if (ret < 0 || (unsigned int)ret > max_identity_len)
1211f13597dSJung-uk Kim         goto out_err;
1221f13597dSJung-uk Kim     if (c_debug)
1236f9291ceSJung-uk Kim         BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity,
1246f9291ceSJung-uk Kim                    ret);
125aeb5019cSJung-uk Kim 
126aeb5019cSJung-uk Kim     /* convert the PSK key to binary */
127e71b7053SJung-uk Kim     key = OPENSSL_hexstr2buf(psk_key, &key_len);
128aeb5019cSJung-uk Kim     if (key == NULL) {
129aeb5019cSJung-uk Kim         BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
1306f9291ceSJung-uk Kim                    psk_key);
1311f13597dSJung-uk Kim         return 0;
1321f13597dSJung-uk Kim     }
133e71b7053SJung-uk Kim     if (max_psk_len > INT_MAX || key_len > (long)max_psk_len) {
1346f9291ceSJung-uk Kim         BIO_printf(bio_err,
135aeb5019cSJung-uk Kim                    "psk buffer of callback is too small (%d) for key (%ld)\n",
136aeb5019cSJung-uk Kim                    max_psk_len, key_len);
137aeb5019cSJung-uk Kim         OPENSSL_free(key);
1381f13597dSJung-uk Kim         return 0;
1391f13597dSJung-uk Kim     }
1401f13597dSJung-uk Kim 
141aeb5019cSJung-uk Kim     memcpy(psk, key, key_len);
142aeb5019cSJung-uk Kim     OPENSSL_free(key);
1431f13597dSJung-uk Kim 
1441f13597dSJung-uk Kim     if (c_debug)
145aeb5019cSJung-uk Kim         BIO_printf(bio_c_out, "created PSK len=%ld\n", key_len);
1461f13597dSJung-uk Kim 
147aeb5019cSJung-uk Kim     return key_len;
1481f13597dSJung-uk Kim  out_err:
1491f13597dSJung-uk Kim     if (c_debug)
1501f13597dSJung-uk Kim         BIO_printf(bio_err, "Error in PSK client callback\n");
1511f13597dSJung-uk Kim     return 0;
1521f13597dSJung-uk Kim }
1531f13597dSJung-uk Kim #endif
1541f13597dSJung-uk Kim 
155e71b7053SJung-uk Kim const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
156e71b7053SJung-uk Kim const unsigned char tls13_aes256gcmsha384_id[] = { 0x13, 0x02 };
15774664626SKris Kennaway 
psk_use_session_cb(SSL * s,const EVP_MD * md,const unsigned char ** id,size_t * idlen,SSL_SESSION ** sess)158e71b7053SJung-uk Kim static int psk_use_session_cb(SSL *s, const EVP_MD *md,
159e71b7053SJung-uk Kim                               const unsigned char **id, size_t *idlen,
160e71b7053SJung-uk Kim                               SSL_SESSION **sess)
161e71b7053SJung-uk Kim {
162e71b7053SJung-uk Kim     SSL_SESSION *usesess = NULL;
163e71b7053SJung-uk Kim     const SSL_CIPHER *cipher = NULL;
164e71b7053SJung-uk Kim 
165e71b7053SJung-uk Kim     if (psksess != NULL) {
166e71b7053SJung-uk Kim         SSL_SESSION_up_ref(psksess);
167e71b7053SJung-uk Kim         usesess = psksess;
168e71b7053SJung-uk Kim     } else {
169e71b7053SJung-uk Kim         long key_len;
170e71b7053SJung-uk Kim         unsigned char *key = OPENSSL_hexstr2buf(psk_key, &key_len);
171e71b7053SJung-uk Kim 
172e71b7053SJung-uk Kim         if (key == NULL) {
173e71b7053SJung-uk Kim             BIO_printf(bio_err, "Could not convert PSK key '%s' to buffer\n",
174e71b7053SJung-uk Kim                        psk_key);
175e71b7053SJung-uk Kim             return 0;
17674664626SKris Kennaway         }
17774664626SKris Kennaway 
178e71b7053SJung-uk Kim         /* We default to SHA-256 */
179e71b7053SJung-uk Kim         cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
180e71b7053SJung-uk Kim         if (cipher == NULL) {
181e71b7053SJung-uk Kim             BIO_printf(bio_err, "Error finding suitable ciphersuite\n");
182e71b7053SJung-uk Kim             OPENSSL_free(key);
183e71b7053SJung-uk Kim             return 0;
184e71b7053SJung-uk Kim         }
185e71b7053SJung-uk Kim 
186e71b7053SJung-uk Kim         usesess = SSL_SESSION_new();
187e71b7053SJung-uk Kim         if (usesess == NULL
188e71b7053SJung-uk Kim                 || !SSL_SESSION_set1_master_key(usesess, key, key_len)
189e71b7053SJung-uk Kim                 || !SSL_SESSION_set_cipher(usesess, cipher)
190e71b7053SJung-uk Kim                 || !SSL_SESSION_set_protocol_version(usesess, TLS1_3_VERSION)) {
191e71b7053SJung-uk Kim             OPENSSL_free(key);
192e71b7053SJung-uk Kim             goto err;
193e71b7053SJung-uk Kim         }
194e71b7053SJung-uk Kim         OPENSSL_free(key);
195e71b7053SJung-uk Kim     }
196e71b7053SJung-uk Kim 
197e71b7053SJung-uk Kim     cipher = SSL_SESSION_get0_cipher(usesess);
198e71b7053SJung-uk Kim     if (cipher == NULL)
199e71b7053SJung-uk Kim         goto err;
200e71b7053SJung-uk Kim 
201e71b7053SJung-uk Kim     if (md != NULL && SSL_CIPHER_get_handshake_digest(cipher) != md) {
202e71b7053SJung-uk Kim         /* PSK not usable, ignore it */
203e71b7053SJung-uk Kim         *id = NULL;
204e71b7053SJung-uk Kim         *idlen = 0;
205e71b7053SJung-uk Kim         *sess = NULL;
206e71b7053SJung-uk Kim         SSL_SESSION_free(usesess);
207e71b7053SJung-uk Kim     } else {
208e71b7053SJung-uk Kim         *sess = usesess;
209e71b7053SJung-uk Kim         *id = (unsigned char *)psk_identity;
210e71b7053SJung-uk Kim         *idlen = strlen(psk_identity);
211e71b7053SJung-uk Kim     }
212e71b7053SJung-uk Kim 
213e71b7053SJung-uk Kim     return 1;
214e71b7053SJung-uk Kim 
215e71b7053SJung-uk Kim  err:
216e71b7053SJung-uk Kim     SSL_SESSION_free(usesess);
217e71b7053SJung-uk Kim     return 0;
218e71b7053SJung-uk Kim }
219db522d3aSSimon L. B. Nielsen 
220db522d3aSSimon L. B. Nielsen /* This is a context that we pass to callbacks */
221db522d3aSSimon L. B. Nielsen typedef struct tlsextctx_st {
222db522d3aSSimon L. B. Nielsen     BIO *biodebug;
223db522d3aSSimon L. B. Nielsen     int ack;
224db522d3aSSimon L. B. Nielsen } tlsextctx;
225db522d3aSSimon L. B. Nielsen 
ssl_servername_cb(SSL * s,int * ad,void * arg)226e71b7053SJung-uk Kim static int ssl_servername_cb(SSL *s, int *ad, void *arg)
227db522d3aSSimon L. B. Nielsen {
228db522d3aSSimon L. B. Nielsen     tlsextctx *p = (tlsextctx *) arg;
229db522d3aSSimon L. B. Nielsen     const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
230db522d3aSSimon L. B. Nielsen     if (SSL_get_servername_type(s) != -1)
231db522d3aSSimon L. B. Nielsen         p->ack = !SSL_session_reused(s) && hn != NULL;
232db522d3aSSimon L. B. Nielsen     else
233db522d3aSSimon L. B. Nielsen         BIO_printf(bio_err, "Can't use SSL_get_servername\n");
234db522d3aSSimon L. B. Nielsen 
235db522d3aSSimon L. B. Nielsen     return SSL_TLSEXT_ERR_OK;
236db522d3aSSimon L. B. Nielsen }
2371f13597dSJung-uk Kim 
2381f13597dSJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
2391f13597dSJung-uk Kim /* This the context that we pass to next_proto_cb */
2401f13597dSJung-uk Kim typedef struct tlsextnextprotoctx_st {
2411f13597dSJung-uk Kim     unsigned char *data;
242e71b7053SJung-uk Kim     size_t len;
2431f13597dSJung-uk Kim     int status;
2441f13597dSJung-uk Kim } tlsextnextprotoctx;
2451f13597dSJung-uk Kim 
2461f13597dSJung-uk Kim static tlsextnextprotoctx next_proto;
2471f13597dSJung-uk Kim 
next_proto_cb(SSL * s,unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)2486f9291ceSJung-uk Kim static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen,
2496f9291ceSJung-uk Kim                          const unsigned char *in, unsigned int inlen,
2506f9291ceSJung-uk Kim                          void *arg)
2511f13597dSJung-uk Kim {
2521f13597dSJung-uk Kim     tlsextnextprotoctx *ctx = arg;
2531f13597dSJung-uk Kim 
2546f9291ceSJung-uk Kim     if (!c_quiet) {
2551f13597dSJung-uk Kim         /* We can assume that |in| is syntactically valid. */
2561f13597dSJung-uk Kim         unsigned i;
2571f13597dSJung-uk Kim         BIO_printf(bio_c_out, "Protocols advertised by server: ");
2586f9291ceSJung-uk Kim         for (i = 0; i < inlen;) {
2591f13597dSJung-uk Kim             if (i)
2601f13597dSJung-uk Kim                 BIO_write(bio_c_out, ", ", 2);
2611f13597dSJung-uk Kim             BIO_write(bio_c_out, &in[i + 1], in[i]);
2621f13597dSJung-uk Kim             i += in[i] + 1;
2631f13597dSJung-uk Kim         }
2641f13597dSJung-uk Kim         BIO_write(bio_c_out, "\n", 1);
2651f13597dSJung-uk Kim     }
2661f13597dSJung-uk Kim 
2676f9291ceSJung-uk Kim     ctx->status =
2686f9291ceSJung-uk Kim         SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len);
2691f13597dSJung-uk Kim     return SSL_TLSEXT_ERR_OK;
2701f13597dSJung-uk Kim }
27109286989SJung-uk Kim #endif                         /* ndef OPENSSL_NO_NEXTPROTONEG */
2727bded2dbSJung-uk Kim 
serverinfo_cli_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)2737bded2dbSJung-uk Kim static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
2747bded2dbSJung-uk Kim                                    const unsigned char *in, size_t inlen,
2757bded2dbSJung-uk Kim                                    int *al, void *arg)
2767bded2dbSJung-uk Kim {
2777bded2dbSJung-uk Kim     char pem_name[100];
2787bded2dbSJung-uk Kim     unsigned char ext_buf[4 + 65536];
2797bded2dbSJung-uk Kim 
2807bded2dbSJung-uk Kim     /* Reconstruct the type/len fields prior to extension data */
281e71b7053SJung-uk Kim     inlen &= 0xffff; /* for formal memcmpy correctness */
282c4ad4dffSJung-uk Kim     ext_buf[0] = (unsigned char)(ext_type >> 8);
283c4ad4dffSJung-uk Kim     ext_buf[1] = (unsigned char)(ext_type);
284c4ad4dffSJung-uk Kim     ext_buf[2] = (unsigned char)(inlen >> 8);
285c4ad4dffSJung-uk Kim     ext_buf[3] = (unsigned char)(inlen);
2867bded2dbSJung-uk Kim     memcpy(ext_buf + 4, in, inlen);
2877bded2dbSJung-uk Kim 
2887bded2dbSJung-uk Kim     BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d",
2897bded2dbSJung-uk Kim                  ext_type);
2907bded2dbSJung-uk Kim     PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen);
2917bded2dbSJung-uk Kim     return 1;
2927bded2dbSJung-uk Kim }
2937bded2dbSJung-uk Kim 
294e71b7053SJung-uk Kim /*
295e71b7053SJung-uk Kim  * Hex decoder that tolerates optional whitespace.  Returns number of bytes
296e71b7053SJung-uk Kim  * produced, advances inptr to end of input string.
297e71b7053SJung-uk Kim  */
hexdecode(const char ** inptr,void * result)298e71b7053SJung-uk Kim static ossl_ssize_t hexdecode(const char **inptr, void *result)
299e71b7053SJung-uk Kim {
300e71b7053SJung-uk Kim     unsigned char **out = (unsigned char **)result;
301e71b7053SJung-uk Kim     const char *in = *inptr;
302e71b7053SJung-uk Kim     unsigned char *ret = app_malloc(strlen(in) / 2, "hexdecode");
303e71b7053SJung-uk Kim     unsigned char *cp = ret;
304e71b7053SJung-uk Kim     uint8_t byte;
305e71b7053SJung-uk Kim     int nibble = 0;
3061f13597dSJung-uk Kim 
307e71b7053SJung-uk Kim     if (ret == NULL)
308e71b7053SJung-uk Kim         return -1;
309e71b7053SJung-uk Kim 
310e71b7053SJung-uk Kim     for (byte = 0; *in; ++in) {
311e71b7053SJung-uk Kim         int x;
312e71b7053SJung-uk Kim 
313e71b7053SJung-uk Kim         if (isspace(_UC(*in)))
314e71b7053SJung-uk Kim             continue;
315e71b7053SJung-uk Kim         x = OPENSSL_hexchar2int(*in);
316e71b7053SJung-uk Kim         if (x < 0) {
317e71b7053SJung-uk Kim             OPENSSL_free(ret);
318e71b7053SJung-uk Kim             return 0;
319e71b7053SJung-uk Kim         }
320e71b7053SJung-uk Kim         byte |= (char)x;
321e71b7053SJung-uk Kim         if ((nibble ^= 1) == 0) {
322e71b7053SJung-uk Kim             *cp++ = byte;
323e71b7053SJung-uk Kim             byte = 0;
324e71b7053SJung-uk Kim         } else {
325e71b7053SJung-uk Kim             byte <<= 4;
326e71b7053SJung-uk Kim         }
327e71b7053SJung-uk Kim     }
328e71b7053SJung-uk Kim     if (nibble != 0) {
329e71b7053SJung-uk Kim         OPENSSL_free(ret);
330e71b7053SJung-uk Kim         return 0;
331e71b7053SJung-uk Kim     }
332e71b7053SJung-uk Kim     *inptr = in;
333e71b7053SJung-uk Kim 
334e71b7053SJung-uk Kim     return cp - (*out = ret);
335e71b7053SJung-uk Kim }
336e71b7053SJung-uk Kim 
337e71b7053SJung-uk Kim /*
338e71b7053SJung-uk Kim  * Decode unsigned 0..255, returns 1 on success, <= 0 on failure. Advances
339e71b7053SJung-uk Kim  * inptr to next field skipping leading whitespace.
340e71b7053SJung-uk Kim  */
checked_uint8(const char ** inptr,void * out)341e71b7053SJung-uk Kim static ossl_ssize_t checked_uint8(const char **inptr, void *out)
342e71b7053SJung-uk Kim {
343e71b7053SJung-uk Kim     uint8_t *result = (uint8_t *)out;
344e71b7053SJung-uk Kim     const char *in = *inptr;
345e71b7053SJung-uk Kim     char *endp;
346e71b7053SJung-uk Kim     long v;
347e71b7053SJung-uk Kim     int e;
348e71b7053SJung-uk Kim 
349e71b7053SJung-uk Kim     save_errno();
350e71b7053SJung-uk Kim     v = strtol(in, &endp, 10);
351e71b7053SJung-uk Kim     e = restore_errno();
352e71b7053SJung-uk Kim 
353e71b7053SJung-uk Kim     if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
354e71b7053SJung-uk Kim         endp == in || !isspace(_UC(*endp)) ||
355e71b7053SJung-uk Kim         v != (*result = (uint8_t) v)) {
356e71b7053SJung-uk Kim         return -1;
357e71b7053SJung-uk Kim     }
358e71b7053SJung-uk Kim     for (in = endp; isspace(_UC(*in)); ++in)
359e71b7053SJung-uk Kim         continue;
360e71b7053SJung-uk Kim 
361e71b7053SJung-uk Kim     *inptr = in;
362e71b7053SJung-uk Kim     return 1;
363e71b7053SJung-uk Kim }
364e71b7053SJung-uk Kim 
365e71b7053SJung-uk Kim struct tlsa_field {
366e71b7053SJung-uk Kim     void *var;
367e71b7053SJung-uk Kim     const char *name;
368e71b7053SJung-uk Kim     ossl_ssize_t (*parser)(const char **, void *);
369e71b7053SJung-uk Kim };
370e71b7053SJung-uk Kim 
tlsa_import_rr(SSL * con,const char * rrdata)371e71b7053SJung-uk Kim static int tlsa_import_rr(SSL *con, const char *rrdata)
372e71b7053SJung-uk Kim {
373e71b7053SJung-uk Kim     /* Not necessary to re-init these values; the "parsers" do that. */
374e71b7053SJung-uk Kim     static uint8_t usage;
375e71b7053SJung-uk Kim     static uint8_t selector;
376e71b7053SJung-uk Kim     static uint8_t mtype;
377e71b7053SJung-uk Kim     static unsigned char *data;
378e71b7053SJung-uk Kim     static struct tlsa_field tlsa_fields[] = {
379e71b7053SJung-uk Kim         { &usage, "usage", checked_uint8 },
380e71b7053SJung-uk Kim         { &selector, "selector", checked_uint8 },
381e71b7053SJung-uk Kim         { &mtype, "mtype", checked_uint8 },
382e71b7053SJung-uk Kim         { &data, "data", hexdecode },
383e71b7053SJung-uk Kim         { NULL, }
384e71b7053SJung-uk Kim     };
385e71b7053SJung-uk Kim     struct tlsa_field *f;
386e71b7053SJung-uk Kim     int ret;
387e71b7053SJung-uk Kim     const char *cp = rrdata;
388e71b7053SJung-uk Kim     ossl_ssize_t len = 0;
389e71b7053SJung-uk Kim 
390e71b7053SJung-uk Kim     for (f = tlsa_fields; f->var; ++f) {
391e71b7053SJung-uk Kim         /* Returns number of bytes produced, advances cp to next field */
392e71b7053SJung-uk Kim         if ((len = f->parser(&cp, f->var)) <= 0) {
393e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: warning: bad TLSA %s field in: %s\n",
394e71b7053SJung-uk Kim                        prog, f->name, rrdata);
395e71b7053SJung-uk Kim             return 0;
396e71b7053SJung-uk Kim         }
397e71b7053SJung-uk Kim     }
398e71b7053SJung-uk Kim     /* The data field is last, so len is its length */
399e71b7053SJung-uk Kim     ret = SSL_dane_tlsa_add(con, usage, selector, mtype, data, len);
400e71b7053SJung-uk Kim     OPENSSL_free(data);
401e71b7053SJung-uk Kim 
402e71b7053SJung-uk Kim     if (ret == 0) {
403e71b7053SJung-uk Kim         ERR_print_errors(bio_err);
404e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: warning: unusable TLSA rrdata: %s\n",
405e71b7053SJung-uk Kim                    prog, rrdata);
406e71b7053SJung-uk Kim         return 0;
407e71b7053SJung-uk Kim     }
408e71b7053SJung-uk Kim     if (ret < 0) {
409e71b7053SJung-uk Kim         ERR_print_errors(bio_err);
410e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: warning: error loading TLSA rrdata: %s\n",
411e71b7053SJung-uk Kim                    prog, rrdata);
412e71b7053SJung-uk Kim         return 0;
413e71b7053SJung-uk Kim     }
414e71b7053SJung-uk Kim     return ret;
415e71b7053SJung-uk Kim }
416e71b7053SJung-uk Kim 
tlsa_import_rrset(SSL * con,STACK_OF (OPENSSL_STRING)* rrset)417e71b7053SJung-uk Kim static int tlsa_import_rrset(SSL *con, STACK_OF(OPENSSL_STRING) *rrset)
418e71b7053SJung-uk Kim {
419e71b7053SJung-uk Kim     int num = sk_OPENSSL_STRING_num(rrset);
420e71b7053SJung-uk Kim     int count = 0;
421e71b7053SJung-uk Kim     int i;
422e71b7053SJung-uk Kim 
423e71b7053SJung-uk Kim     for (i = 0; i < num; ++i) {
424e71b7053SJung-uk Kim         char *rrdata = sk_OPENSSL_STRING_value(rrset, i);
425e71b7053SJung-uk Kim         if (tlsa_import_rr(con, rrdata) > 0)
426e71b7053SJung-uk Kim             ++count;
427e71b7053SJung-uk Kim     }
428e71b7053SJung-uk Kim     return count > 0;
429e71b7053SJung-uk Kim }
430e71b7053SJung-uk Kim 
431e71b7053SJung-uk Kim typedef enum OPTION_choice {
432b077aed3SPierre Pronchery     OPT_COMMON,
433e71b7053SJung-uk Kim     OPT_4, OPT_6, OPT_HOST, OPT_PORT, OPT_CONNECT, OPT_BIND, OPT_UNIX,
434e71b7053SJung-uk Kim     OPT_XMPPHOST, OPT_VERIFY, OPT_NAMEOPT,
435e71b7053SJung-uk Kim     OPT_CERT, OPT_CRL, OPT_CRL_DOWNLOAD, OPT_SESS_OUT, OPT_SESS_IN,
436e71b7053SJung-uk Kim     OPT_CERTFORM, OPT_CRLFORM, OPT_VERIFY_RET_ERROR, OPT_VERIFY_QUIET,
437e71b7053SJung-uk Kim     OPT_BRIEF, OPT_PREXIT, OPT_CRLF, OPT_QUIET, OPT_NBIO,
438e71b7053SJung-uk Kim     OPT_SSL_CLIENT_ENGINE, OPT_IGN_EOF, OPT_NO_IGN_EOF,
439e71b7053SJung-uk Kim     OPT_DEBUG, OPT_TLSEXTDEBUG, OPT_STATUS, OPT_WDEBUG,
440e71b7053SJung-uk Kim     OPT_MSG, OPT_MSGFILE, OPT_ENGINE, OPT_TRACE, OPT_SECURITY_DEBUG,
441e71b7053SJung-uk Kim     OPT_SECURITY_DEBUG_VERBOSE, OPT_SHOWCERTS, OPT_NBIO_TEST, OPT_STATE,
442e71b7053SJung-uk Kim     OPT_PSK_IDENTITY, OPT_PSK, OPT_PSK_SESS,
443e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
444e71b7053SJung-uk Kim     OPT_SRPUSER, OPT_SRPPASS, OPT_SRP_STRENGTH, OPT_SRP_LATEUSER,
445e71b7053SJung-uk Kim     OPT_SRP_MOREGROUPS,
446e71b7053SJung-uk Kim #endif
447e71b7053SJung-uk Kim     OPT_SSL3, OPT_SSL_CONFIG,
448e71b7053SJung-uk Kim     OPT_TLS1_3, OPT_TLS1_2, OPT_TLS1_1, OPT_TLS1, OPT_DTLS, OPT_DTLS1,
449e71b7053SJung-uk Kim     OPT_DTLS1_2, OPT_SCTP, OPT_TIMEOUT, OPT_MTU, OPT_KEYFORM, OPT_PASS,
450b077aed3SPierre Pronchery     OPT_CERT_CHAIN, OPT_KEY, OPT_RECONNECT, OPT_BUILD_CHAIN,
451b077aed3SPierre Pronchery     OPT_NEXTPROTONEG, OPT_ALPN,
452b077aed3SPierre Pronchery     OPT_CAPATH, OPT_NOCAPATH, OPT_CHAINCAPATH, OPT_VERIFYCAPATH,
453b077aed3SPierre Pronchery     OPT_CAFILE, OPT_NOCAFILE, OPT_CHAINCAFILE, OPT_VERIFYCAFILE,
454b077aed3SPierre Pronchery     OPT_CASTORE, OPT_NOCASTORE, OPT_CHAINCASTORE, OPT_VERIFYCASTORE,
455e71b7053SJung-uk Kim     OPT_SERVERINFO, OPT_STARTTLS, OPT_SERVERNAME, OPT_NOSERVERNAME, OPT_ASYNC,
456e71b7053SJung-uk Kim     OPT_USE_SRTP, OPT_KEYMATEXPORT, OPT_KEYMATEXPORTLEN, OPT_PROTOHOST,
457e71b7053SJung-uk Kim     OPT_MAXFRAGLEN, OPT_MAX_SEND_FRAG, OPT_SPLIT_SEND_FRAG, OPT_MAX_PIPELINES,
458e71b7053SJung-uk Kim     OPT_READ_BUF, OPT_KEYLOG_FILE, OPT_EARLY_DATA, OPT_REQCAFILE,
459e71b7053SJung-uk Kim     OPT_V_ENUM,
460e71b7053SJung-uk Kim     OPT_X_ENUM,
461b077aed3SPierre Pronchery     OPT_S_ENUM, OPT_IGNORE_UNEXPECTED_EOF,
462b077aed3SPierre Pronchery     OPT_FALLBACKSCSV, OPT_NOCMDS, OPT_PROXY, OPT_PROXY_USER, OPT_PROXY_PASS,
463b077aed3SPierre Pronchery     OPT_DANE_TLSA_DOMAIN,
464e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
465e71b7053SJung-uk Kim     OPT_CT, OPT_NOCT, OPT_CTLOG_FILE,
466e71b7053SJung-uk Kim #endif
467e71b7053SJung-uk Kim     OPT_DANE_TLSA_RRDATA, OPT_DANE_EE_NO_NAME,
468e71b7053SJung-uk Kim     OPT_ENABLE_PHA,
4696935a639SJung-uk Kim     OPT_SCTP_LABEL_BUG,
470b077aed3SPierre Pronchery     OPT_R_ENUM, OPT_PROV_ENUM
471e71b7053SJung-uk Kim } OPTION_CHOICE;
472e71b7053SJung-uk Kim 
473e71b7053SJung-uk Kim const OPTIONS s_client_options[] = {
474b077aed3SPierre Pronchery     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [host:port]\n"},
475b077aed3SPierre Pronchery 
476b077aed3SPierre Pronchery     OPT_SECTION("General"),
477e71b7053SJung-uk Kim     {"help", OPT_HELP, '-', "Display this summary"},
478b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
479b077aed3SPierre Pronchery     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
480b077aed3SPierre Pronchery     {"ssl_client_engine", OPT_SSL_CLIENT_ENGINE, 's',
481b077aed3SPierre Pronchery      "Specify engine to be used for client certificate operations"},
482b077aed3SPierre Pronchery #endif
483b077aed3SPierre Pronchery     {"ssl_config", OPT_SSL_CONFIG, 's', "Use specified section for SSL_CTX configuration"},
484b077aed3SPierre Pronchery #ifndef OPENSSL_NO_CT
485b077aed3SPierre Pronchery     {"ct", OPT_CT, '-', "Request and parse SCTs (also enables OCSP stapling)"},
486b077aed3SPierre Pronchery     {"noct", OPT_NOCT, '-', "Do not request or parse SCTs (default)"},
487b077aed3SPierre Pronchery     {"ctlogfile", OPT_CTLOG_FILE, '<', "CT log list CONF file"},
488b077aed3SPierre Pronchery #endif
489b077aed3SPierre Pronchery 
490b077aed3SPierre Pronchery     OPT_SECTION("Network"),
491e71b7053SJung-uk Kim     {"host", OPT_HOST, 's', "Use -connect instead"},
492e71b7053SJung-uk Kim     {"port", OPT_PORT, 'p', "Use -connect instead"},
493e71b7053SJung-uk Kim     {"connect", OPT_CONNECT, 's',
494b077aed3SPierre Pronchery      "TCP/IP where to connect; default: " PORT ")"},
495e71b7053SJung-uk Kim     {"bind", OPT_BIND, 's', "bind local address for connection"},
496e71b7053SJung-uk Kim     {"proxy", OPT_PROXY, 's',
497e71b7053SJung-uk Kim      "Connect to via specified proxy to the real server"},
498b077aed3SPierre Pronchery     {"proxy_user", OPT_PROXY_USER, 's', "UserID for proxy authentication"},
499b077aed3SPierre Pronchery     {"proxy_pass", OPT_PROXY_PASS, 's', "Proxy authentication password source"},
500e71b7053SJung-uk Kim #ifdef AF_UNIX
501e71b7053SJung-uk Kim     {"unix", OPT_UNIX, 's', "Connect over the specified Unix-domain socket"},
502e71b7053SJung-uk Kim #endif
503e71b7053SJung-uk Kim     {"4", OPT_4, '-', "Use IPv4 only"},
504e71b7053SJung-uk Kim #ifdef AF_INET6
505e71b7053SJung-uk Kim     {"6", OPT_6, '-', "Use IPv6 only"},
506e71b7053SJung-uk Kim #endif
507b077aed3SPierre Pronchery     {"maxfraglen", OPT_MAXFRAGLEN, 'p',
508b077aed3SPierre Pronchery      "Enable Maximum Fragment Length Negotiation (len values: 512, 1024, 2048 and 4096)"},
509b077aed3SPierre Pronchery     {"max_send_frag", OPT_MAX_SEND_FRAG, 'p', "Maximum Size of send frames "},
510b077aed3SPierre Pronchery     {"split_send_frag", OPT_SPLIT_SEND_FRAG, 'p',
511b077aed3SPierre Pronchery      "Size used to split data for encrypt pipelines"},
512b077aed3SPierre Pronchery     {"max_pipelines", OPT_MAX_PIPELINES, 'p',
513b077aed3SPierre Pronchery      "Maximum number of encrypt/decrypt pipelines to be used"},
514b077aed3SPierre Pronchery     {"read_buf", OPT_READ_BUF, 'p',
515b077aed3SPierre Pronchery      "Default read buffer size to be used for connections"},
516b077aed3SPierre Pronchery     {"fallback_scsv", OPT_FALLBACKSCSV, '-', "Send the fallback SCSV"},
517b077aed3SPierre Pronchery 
518b077aed3SPierre Pronchery     OPT_SECTION("Identity"),
519b077aed3SPierre Pronchery     {"cert", OPT_CERT, '<', "Client certificate file to use"},
520e71b7053SJung-uk Kim     {"certform", OPT_CERTFORM, 'F',
521b077aed3SPierre Pronchery      "Client certificate file format (PEM/DER/P12); has no effect"},
522b077aed3SPierre Pronchery     {"cert_chain", OPT_CERT_CHAIN, '<',
523b077aed3SPierre Pronchery      "Client certificate chain file (in PEM format)"},
524b077aed3SPierre Pronchery     {"build_chain", OPT_BUILD_CHAIN, '-', "Build client certificate chain"},
525b077aed3SPierre Pronchery     {"key", OPT_KEY, 's', "Private key file to use; default: -cert file"},
526b077aed3SPierre Pronchery     {"keyform", OPT_KEYFORM, 'E', "Key format (ENGINE, other values ignored)"},
527b077aed3SPierre Pronchery     {"pass", OPT_PASS, 's', "Private key and cert file pass phrase source"},
528b077aed3SPierre Pronchery     {"verify", OPT_VERIFY, 'p', "Turn on peer certificate verification"},
529b077aed3SPierre Pronchery     {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
530e71b7053SJung-uk Kim     {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
531e71b7053SJung-uk Kim     {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
532b077aed3SPierre Pronchery     {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
533e71b7053SJung-uk Kim     {"no-CAfile", OPT_NOCAFILE, '-',
534e71b7053SJung-uk Kim      "Do not load the default certificates file"},
535e71b7053SJung-uk Kim     {"no-CApath", OPT_NOCAPATH, '-',
536e71b7053SJung-uk Kim      "Do not load certificates from the default certificates directory"},
537b077aed3SPierre Pronchery     {"no-CAstore", OPT_NOCASTORE, '-',
538b077aed3SPierre Pronchery      "Do not load certificates from the default certificates store"},
539e71b7053SJung-uk Kim     {"requestCAfile", OPT_REQCAFILE, '<',
540e71b7053SJung-uk Kim       "PEM format file of CA names to send to the server"},
541e71b7053SJung-uk Kim     {"dane_tlsa_domain", OPT_DANE_TLSA_DOMAIN, 's', "DANE TLSA base domain"},
542e71b7053SJung-uk Kim     {"dane_tlsa_rrdata", OPT_DANE_TLSA_RRDATA, 's',
543e71b7053SJung-uk Kim      "DANE TLSA rrdata presentation form"},
544e71b7053SJung-uk Kim     {"dane_ee_no_namechecks", OPT_DANE_EE_NO_NAME, '-',
545e71b7053SJung-uk Kim      "Disable name checks when matching DANE-EE(3) TLSA records"},
546b077aed3SPierre Pronchery     {"psk_identity", OPT_PSK_IDENTITY, 's', "PSK identity"},
547b077aed3SPierre Pronchery     {"psk", OPT_PSK, 's', "PSK in hex (without 0x)"},
548b077aed3SPierre Pronchery     {"psk_session", OPT_PSK_SESS, '<', "File to read PSK SSL session from"},
549b077aed3SPierre Pronchery     {"name", OPT_PROTOHOST, 's',
550b077aed3SPierre Pronchery      "Hostname to use for \"-starttls lmtp\", \"-starttls smtp\" or \"-starttls xmpp[-server]\""},
551b077aed3SPierre Pronchery 
552b077aed3SPierre Pronchery     OPT_SECTION("Session"),
553e71b7053SJung-uk Kim     {"reconnect", OPT_RECONNECT, '-',
554e71b7053SJung-uk Kim      "Drop and re-make the connection with the same Session-ID"},
555b077aed3SPierre Pronchery     {"sess_out", OPT_SESS_OUT, '>', "File to write SSL session to"},
556b077aed3SPierre Pronchery     {"sess_in", OPT_SESS_IN, '<', "File to read SSL session from"},
557b077aed3SPierre Pronchery 
558b077aed3SPierre Pronchery     OPT_SECTION("Input/Output"),
559e71b7053SJung-uk Kim     {"crlf", OPT_CRLF, '-', "Convert LF from terminal into CRLF"},
560e71b7053SJung-uk Kim     {"quiet", OPT_QUIET, '-', "No s_client output"},
561e71b7053SJung-uk Kim     {"ign_eof", OPT_IGN_EOF, '-', "Ignore input eof (default when -quiet)"},
562e71b7053SJung-uk Kim     {"no_ign_eof", OPT_NO_IGN_EOF, '-', "Don't ignore input eof"},
563e71b7053SJung-uk Kim     {"starttls", OPT_STARTTLS, 's',
564e71b7053SJung-uk Kim      "Use the appropriate STARTTLS command before starting TLS"},
565e71b7053SJung-uk Kim     {"xmpphost", OPT_XMPPHOST, 's',
566e71b7053SJung-uk Kim      "Alias of -name option for \"-starttls xmpp[-server]\""},
567e71b7053SJung-uk Kim     {"brief", OPT_BRIEF, '-',
568e71b7053SJung-uk Kim      "Restrict output to brief summary of connection parameters"},
569e71b7053SJung-uk Kim     {"prexit", OPT_PREXIT, '-',
570e71b7053SJung-uk Kim      "Print session information when the program exits"},
571b077aed3SPierre Pronchery 
572b077aed3SPierre Pronchery     OPT_SECTION("Debug"),
573b077aed3SPierre Pronchery     {"showcerts", OPT_SHOWCERTS, '-',
574b077aed3SPierre Pronchery      "Show all certificates sent by the server"},
575b077aed3SPierre Pronchery     {"debug", OPT_DEBUG, '-', "Extra output"},
576b077aed3SPierre Pronchery     {"msg", OPT_MSG, '-', "Show protocol messages"},
577b077aed3SPierre Pronchery     {"msgfile", OPT_MSGFILE, '>',
578b077aed3SPierre Pronchery      "File to send output of -msg or -trace, instead of stdout"},
579b077aed3SPierre Pronchery     {"nbio_test", OPT_NBIO_TEST, '-', "More ssl protocol testing"},
580b077aed3SPierre Pronchery     {"state", OPT_STATE, '-', "Print the ssl states"},
581b077aed3SPierre Pronchery     {"keymatexport", OPT_KEYMATEXPORT, 's',
582b077aed3SPierre Pronchery      "Export keying material using label"},
583b077aed3SPierre Pronchery     {"keymatexportlen", OPT_KEYMATEXPORTLEN, 'p',
584b077aed3SPierre Pronchery      "Export len bytes of keying material; default 20"},
585e71b7053SJung-uk Kim     {"security_debug", OPT_SECURITY_DEBUG, '-',
586e71b7053SJung-uk Kim      "Enable security debug messages"},
587e71b7053SJung-uk Kim     {"security_debug_verbose", OPT_SECURITY_DEBUG_VERBOSE, '-',
588e71b7053SJung-uk Kim      "Output more security debug output"},
589b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SSL_TRACE
590b077aed3SPierre Pronchery     {"trace", OPT_TRACE, '-', "Show trace output of protocol messages"},
591b077aed3SPierre Pronchery #endif
592b077aed3SPierre Pronchery #ifdef WATT32
593b077aed3SPierre Pronchery     {"wdebug", OPT_WDEBUG, '-', "WATT-32 tcp debugging"},
594b077aed3SPierre Pronchery #endif
595b077aed3SPierre Pronchery     {"keylogfile", OPT_KEYLOG_FILE, '>', "Write TLS secrets to file"},
596e71b7053SJung-uk Kim     {"nocommands", OPT_NOCMDS, '-', "Do not use interactive command letters"},
597e71b7053SJung-uk Kim     {"servername", OPT_SERVERNAME, 's',
598e71b7053SJung-uk Kim      "Set TLS extension servername (SNI) in ClientHello (default)"},
599e71b7053SJung-uk Kim     {"noservername", OPT_NOSERVERNAME, '-',
600e71b7053SJung-uk Kim      "Do not send the server name (SNI) extension in the ClientHello"},
601e71b7053SJung-uk Kim     {"tlsextdebug", OPT_TLSEXTDEBUG, '-',
602e71b7053SJung-uk Kim      "Hex dump of all TLS extensions received"},
603b077aed3SPierre Pronchery     {"ignore_unexpected_eof", OPT_IGNORE_UNEXPECTED_EOF, '-',
604b077aed3SPierre Pronchery      "Do not treat lack of close_notify from a peer as an error"},
605e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
606e71b7053SJung-uk Kim     {"status", OPT_STATUS, '-', "Request certificate status from server"},
607e71b7053SJung-uk Kim #endif
608e71b7053SJung-uk Kim     {"serverinfo", OPT_SERVERINFO, 's',
609e71b7053SJung-uk Kim      "types  Send empty ClientHello extensions (comma-separated numbers)"},
610e71b7053SJung-uk Kim     {"alpn", OPT_ALPN, 's',
611e71b7053SJung-uk Kim      "Enable ALPN extension, considering named protocols supported (comma-separated list)"},
612e71b7053SJung-uk Kim     {"async", OPT_ASYNC, '-', "Support asynchronous operation"},
613b077aed3SPierre Pronchery     {"nbio", OPT_NBIO, '-', "Use non-blocking IO"},
614b077aed3SPierre Pronchery 
615b077aed3SPierre Pronchery     OPT_SECTION("Protocol and version"),
616e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SSL3
617e71b7053SJung-uk Kim     {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
618e71b7053SJung-uk Kim #endif
619e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1
620e71b7053SJung-uk Kim     {"tls1", OPT_TLS1, '-', "Just use TLSv1"},
621e71b7053SJung-uk Kim #endif
622e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_1
623e71b7053SJung-uk Kim     {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"},
624e71b7053SJung-uk Kim #endif
625e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_2
626e71b7053SJung-uk Kim     {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"},
627e71b7053SJung-uk Kim #endif
628e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_3
629e71b7053SJung-uk Kim     {"tls1_3", OPT_TLS1_3, '-', "Just use TLSv1.3"},
630e71b7053SJung-uk Kim #endif
631e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
632e71b7053SJung-uk Kim     {"dtls", OPT_DTLS, '-', "Use any version of DTLS"},
633e71b7053SJung-uk Kim     {"timeout", OPT_TIMEOUT, '-',
634e71b7053SJung-uk Kim      "Enable send/receive timeout on DTLS connections"},
635e71b7053SJung-uk Kim     {"mtu", OPT_MTU, 'p', "Set the link layer MTU"},
636e71b7053SJung-uk Kim #endif
637e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS1
638e71b7053SJung-uk Kim     {"dtls1", OPT_DTLS1, '-', "Just use DTLSv1"},
639e71b7053SJung-uk Kim #endif
640e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS1_2
641e71b7053SJung-uk Kim     {"dtls1_2", OPT_DTLS1_2, '-', "Just use DTLSv1.2"},
642e71b7053SJung-uk Kim #endif
643e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
644e71b7053SJung-uk Kim     {"sctp", OPT_SCTP, '-', "Use SCTP"},
6456935a639SJung-uk Kim     {"sctp_label_bug", OPT_SCTP_LABEL_BUG, '-', "Enable SCTP label length bug"},
646e71b7053SJung-uk Kim #endif
647e71b7053SJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
648e71b7053SJung-uk Kim     {"nextprotoneg", OPT_NEXTPROTONEG, 's',
649e71b7053SJung-uk Kim      "Enable NPN extension, considering named protocols supported (comma-separated list)"},
650e71b7053SJung-uk Kim #endif
651e71b7053SJung-uk Kim     {"early_data", OPT_EARLY_DATA, '<', "File to send as early data"},
652e71b7053SJung-uk Kim     {"enable_pha", OPT_ENABLE_PHA, '-', "Enable post-handshake-authentication"},
653b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SRTP
654b077aed3SPierre Pronchery     {"use_srtp", OPT_USE_SRTP, 's',
655b077aed3SPierre Pronchery      "Offer SRTP key management with a colon-separated profile list"},
656b077aed3SPierre Pronchery #endif
657b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SRP
658b077aed3SPierre Pronchery     {"srpuser", OPT_SRPUSER, 's', "(deprecated) SRP authentication for 'user'"},
659b077aed3SPierre Pronchery     {"srppass", OPT_SRPPASS, 's', "(deprecated) Password for 'user'"},
660b077aed3SPierre Pronchery     {"srp_lateuser", OPT_SRP_LATEUSER, '-',
661b077aed3SPierre Pronchery      "(deprecated) SRP username into second ClientHello message"},
662b077aed3SPierre Pronchery     {"srp_moregroups", OPT_SRP_MOREGROUPS, '-',
663b077aed3SPierre Pronchery      "(deprecated) Tolerate other than the known g N values."},
664b077aed3SPierre Pronchery     {"srp_strength", OPT_SRP_STRENGTH, 'p',
665b077aed3SPierre Pronchery      "(deprecated) Minimal length in bits for N"},
666b077aed3SPierre Pronchery #endif
667b077aed3SPierre Pronchery 
668b077aed3SPierre Pronchery     OPT_R_OPTIONS,
669b077aed3SPierre Pronchery     OPT_S_OPTIONS,
670b077aed3SPierre Pronchery     OPT_V_OPTIONS,
671b077aed3SPierre Pronchery     {"CRL", OPT_CRL, '<', "CRL file to use"},
672b077aed3SPierre Pronchery     {"crl_download", OPT_CRL_DOWNLOAD, '-', "Download CRL from distribution points"},
673b077aed3SPierre Pronchery     {"CRLform", OPT_CRLFORM, 'F', "CRL format (PEM or DER); default PEM"},
674b077aed3SPierre Pronchery     {"verify_return_error", OPT_VERIFY_RET_ERROR, '-',
675b077aed3SPierre Pronchery      "Close connection on verification error"},
676b077aed3SPierre Pronchery     {"verify_quiet", OPT_VERIFY_QUIET, '-', "Restrict verify output to errors"},
677b077aed3SPierre Pronchery     {"chainCAfile", OPT_CHAINCAFILE, '<',
678b077aed3SPierre Pronchery      "CA file for certificate chain (PEM format)"},
679b077aed3SPierre Pronchery     {"chainCApath", OPT_CHAINCAPATH, '/',
680b077aed3SPierre Pronchery      "Use dir as certificate store path to build CA certificate chain"},
681b077aed3SPierre Pronchery     {"chainCAstore", OPT_CHAINCASTORE, ':',
682b077aed3SPierre Pronchery      "CA store URI for certificate chain"},
683b077aed3SPierre Pronchery     {"verifyCAfile", OPT_VERIFYCAFILE, '<',
684b077aed3SPierre Pronchery      "CA file for certificate verification (PEM format)"},
685b077aed3SPierre Pronchery     {"verifyCApath", OPT_VERIFYCAPATH, '/',
686b077aed3SPierre Pronchery      "Use dir as certificate store path to verify CA certificate"},
687b077aed3SPierre Pronchery     {"verifyCAstore", OPT_VERIFYCASTORE, ':',
688b077aed3SPierre Pronchery      "CA store URI for certificate verification"},
689b077aed3SPierre Pronchery     OPT_X_OPTIONS,
690b077aed3SPierre Pronchery     OPT_PROV_OPTIONS,
691b077aed3SPierre Pronchery 
692b077aed3SPierre Pronchery     OPT_PARAMETERS(),
693b077aed3SPierre Pronchery     {"host:port", 0, 0, "Where to connect; same as -connect option"},
694b077aed3SPierre Pronchery     {NULL}
695e71b7053SJung-uk Kim };
696e71b7053SJung-uk Kim 
697e71b7053SJung-uk Kim typedef enum PROTOCOL_choice {
698e71b7053SJung-uk Kim     PROTO_OFF,
6995471f83eSSimon L. B. Nielsen     PROTO_SMTP,
7005471f83eSSimon L. B. Nielsen     PROTO_POP3,
7015471f83eSSimon L. B. Nielsen     PROTO_IMAP,
702db522d3aSSimon L. B. Nielsen     PROTO_FTP,
703e71b7053SJung-uk Kim     PROTO_TELNET,
704e71b7053SJung-uk Kim     PROTO_XMPP,
705e71b7053SJung-uk Kim     PROTO_XMPP_SERVER,
706e71b7053SJung-uk Kim     PROTO_IRC,
707e71b7053SJung-uk Kim     PROTO_MYSQL,
708e71b7053SJung-uk Kim     PROTO_POSTGRES,
709e71b7053SJung-uk Kim     PROTO_LMTP,
710e71b7053SJung-uk Kim     PROTO_NNTP,
711e71b7053SJung-uk Kim     PROTO_SIEVE,
712e71b7053SJung-uk Kim     PROTO_LDAP
713e71b7053SJung-uk Kim } PROTOCOL_CHOICE;
714e71b7053SJung-uk Kim 
715e71b7053SJung-uk Kim static const OPT_PAIR services[] = {
716e71b7053SJung-uk Kim     {"smtp", PROTO_SMTP},
717e71b7053SJung-uk Kim     {"pop3", PROTO_POP3},
718e71b7053SJung-uk Kim     {"imap", PROTO_IMAP},
719e71b7053SJung-uk Kim     {"ftp", PROTO_FTP},
720e71b7053SJung-uk Kim     {"xmpp", PROTO_XMPP},
721e71b7053SJung-uk Kim     {"xmpp-server", PROTO_XMPP_SERVER},
722e71b7053SJung-uk Kim     {"telnet", PROTO_TELNET},
723e71b7053SJung-uk Kim     {"irc", PROTO_IRC},
724e71b7053SJung-uk Kim     {"mysql", PROTO_MYSQL},
725e71b7053SJung-uk Kim     {"postgres", PROTO_POSTGRES},
726e71b7053SJung-uk Kim     {"lmtp", PROTO_LMTP},
727e71b7053SJung-uk Kim     {"nntp", PROTO_NNTP},
728e71b7053SJung-uk Kim     {"sieve", PROTO_SIEVE},
729e71b7053SJung-uk Kim     {"ldap", PROTO_LDAP},
730e71b7053SJung-uk Kim     {NULL, 0}
7315471f83eSSimon L. B. Nielsen };
7325471f83eSSimon L. B. Nielsen 
733e71b7053SJung-uk Kim #define IS_INET_FLAG(o) \
734e71b7053SJung-uk Kim  (o == OPT_4 || o == OPT_6 || o == OPT_HOST || o == OPT_PORT || o == OPT_CONNECT)
735e71b7053SJung-uk Kim #define IS_UNIX_FLAG(o) (o == OPT_UNIX)
736f579bf8eSKris Kennaway 
737e71b7053SJung-uk Kim #define IS_PROT_FLAG(o) \
738e71b7053SJung-uk Kim  (o == OPT_SSL3 || o == OPT_TLS1 || o == OPT_TLS1_1 || o == OPT_TLS1_2 \
739e71b7053SJung-uk Kim   || o == OPT_TLS1_3 || o == OPT_DTLS || o == OPT_DTLS1 || o == OPT_DTLS1_2)
740e71b7053SJung-uk Kim 
741e71b7053SJung-uk Kim /* Free |*dest| and optionally set it to a copy of |source|. */
freeandcopy(char ** dest,const char * source)742e71b7053SJung-uk Kim static void freeandcopy(char **dest, const char *source)
74374664626SKris Kennaway {
744e71b7053SJung-uk Kim     OPENSSL_free(*dest);
745e71b7053SJung-uk Kim     *dest = NULL;
746e71b7053SJung-uk Kim     if (source != NULL)
747e71b7053SJung-uk Kim         *dest = OPENSSL_strdup(source);
748e71b7053SJung-uk Kim }
749e71b7053SJung-uk Kim 
new_session_cb(SSL * s,SSL_SESSION * sess)750e71b7053SJung-uk Kim static int new_session_cb(SSL *s, SSL_SESSION *sess)
751e71b7053SJung-uk Kim {
752e71b7053SJung-uk Kim 
753e71b7053SJung-uk Kim     if (sess_out != NULL) {
754e71b7053SJung-uk Kim         BIO *stmp = BIO_new_file(sess_out, "w");
755e71b7053SJung-uk Kim 
756e71b7053SJung-uk Kim         if (stmp == NULL) {
757e71b7053SJung-uk Kim             BIO_printf(bio_err, "Error writing session file %s\n", sess_out);
758e71b7053SJung-uk Kim         } else {
759e71b7053SJung-uk Kim             PEM_write_bio_SSL_SESSION(stmp, sess);
760e71b7053SJung-uk Kim             BIO_free(stmp);
761e71b7053SJung-uk Kim         }
762e71b7053SJung-uk Kim     }
763e71b7053SJung-uk Kim 
764e71b7053SJung-uk Kim     /*
765e71b7053SJung-uk Kim      * Session data gets dumped on connection for TLSv1.2 and below, and on
766e71b7053SJung-uk Kim      * arrival of the NewSessionTicket for TLSv1.3.
767e71b7053SJung-uk Kim      */
768e71b7053SJung-uk Kim     if (SSL_version(s) == TLS1_3_VERSION) {
769e71b7053SJung-uk Kim         BIO_printf(bio_c_out,
770e71b7053SJung-uk Kim                    "---\nPost-Handshake New Session Ticket arrived:\n");
771e71b7053SJung-uk Kim         SSL_SESSION_print(bio_c_out, sess);
772e71b7053SJung-uk Kim         BIO_printf(bio_c_out, "---\n");
773e71b7053SJung-uk Kim     }
774e71b7053SJung-uk Kim 
775e71b7053SJung-uk Kim     /*
776e71b7053SJung-uk Kim      * We always return a "fail" response so that the session gets freed again
777e71b7053SJung-uk Kim      * because we haven't used the reference.
778e71b7053SJung-uk Kim      */
779e71b7053SJung-uk Kim     return 0;
780e71b7053SJung-uk Kim }
781e71b7053SJung-uk Kim 
s_client_main(int argc,char ** argv)782e71b7053SJung-uk Kim int s_client_main(int argc, char **argv)
783e71b7053SJung-uk Kim {
78474664626SKris Kennaway     BIO *sbio;
785e71b7053SJung-uk Kim     EVP_PKEY *key = NULL;
786e71b7053SJung-uk Kim     SSL *con = NULL;
787e71b7053SJung-uk Kim     SSL_CTX *ctx = NULL;
788e71b7053SJung-uk Kim     STACK_OF(X509) *chain = NULL;
789e71b7053SJung-uk Kim     X509 *cert = NULL;
790e71b7053SJung-uk Kim     X509_VERIFY_PARAM *vpm = NULL;
791e71b7053SJung-uk Kim     SSL_EXCERT *exc = NULL;
792e71b7053SJung-uk Kim     SSL_CONF_CTX *cctx = NULL;
793e71b7053SJung-uk Kim     STACK_OF(OPENSSL_STRING) *ssl_args = NULL;
794e71b7053SJung-uk Kim     char *dane_tlsa_domain = NULL;
795e71b7053SJung-uk Kim     STACK_OF(OPENSSL_STRING) *dane_tlsa_rrset = NULL;
796e71b7053SJung-uk Kim     int dane_ee_no_name = 0;
797e71b7053SJung-uk Kim     STACK_OF(X509_CRL) *crls = NULL;
798e71b7053SJung-uk Kim     const SSL_METHOD *meth = TLS_client_method();
799b077aed3SPierre Pronchery     const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
800b077aed3SPierre Pronchery     char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL;
801b077aed3SPierre Pronchery     char *proxystr = NULL, *proxyuser = NULL;
802b077aed3SPierre Pronchery     char *proxypassarg = NULL, *proxypass = NULL;
803b077aed3SPierre Pronchery     char *connectstr = NULL, *bindstr = NULL;
804e71b7053SJung-uk Kim     char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
805b077aed3SPierre Pronchery     char *chCApath = NULL, *chCAfile = NULL, *chCAstore = NULL, *host = NULL;
806b077aed3SPierre Pronchery     char *thost = NULL, *tport = NULL;
807b077aed3SPierre Pronchery     char *port = NULL;
808e71b7053SJung-uk Kim     char *bindhost = NULL, *bindport = NULL;
809b077aed3SPierre Pronchery     char *passarg = NULL, *pass = NULL;
810b077aed3SPierre Pronchery     char *vfyCApath = NULL, *vfyCAfile = NULL, *vfyCAstore = NULL;
811e71b7053SJung-uk Kim     char *ReqCAfile = NULL;
812e71b7053SJung-uk Kim     char *sess_in = NULL, *crl_file = NULL, *p;
813e71b7053SJung-uk Kim     const char *protohost = NULL;
8146a599222SSimon L. B. Nielsen     struct timeval timeout, *timeoutp;
815e71b7053SJung-uk Kim     fd_set readfds, writefds;
816b077aed3SPierre Pronchery     int noCApath = 0, noCAfile = 0, noCAstore = 0;
817b077aed3SPierre Pronchery     int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_UNDEF;
818b077aed3SPierre Pronchery     int key_format = FORMAT_UNDEF, crlf = 0, full_log = 1, mbuf_len = 0;
819e71b7053SJung-uk Kim     int prexit = 0;
820e71b7053SJung-uk Kim     int sdebug = 0;
821e71b7053SJung-uk Kim     int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0;
822b077aed3SPierre Pronchery     int ret = 1, in_init = 1, i, nbio_test = 0, sock = -1, k, width, state = 0;
823e71b7053SJung-uk Kim     int sbuf_len, sbuf_off, cmdletters = 1;
824e71b7053SJung-uk Kim     int socket_family = AF_UNSPEC, socket_type = SOCK_STREAM, protocol = 0;
825b077aed3SPierre Pronchery     int starttls_proto = PROTO_OFF, crl_format = FORMAT_UNDEF, crl_download = 0;
826e71b7053SJung-uk Kim     int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;
827e71b7053SJung-uk Kim #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
828e71b7053SJung-uk Kim     int at_eof = 0;
829fceca8a3SJacques Vidrine #endif
830e71b7053SJung-uk Kim     int read_buf_len = 0;
831fa5fddf1SJung-uk Kim     int fallback_scsv = 0;
832e71b7053SJung-uk Kim     OPTION_CHOICE o;
833e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
8343b4e3dcbSSimon L. B. Nielsen     int enable_timeouts = 0;
8356a599222SSimon L. B. Nielsen     long socket_mtu = 0;
836e71b7053SJung-uk Kim #endif
837e71b7053SJung-uk Kim #ifndef OPENSSL_NO_ENGINE
838e71b7053SJung-uk Kim     ENGINE *ssl_client_engine = NULL;
839e71b7053SJung-uk Kim #endif
840e71b7053SJung-uk Kim     ENGINE *e = NULL;
841e71b7053SJung-uk Kim #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
842e71b7053SJung-uk Kim     struct timeval tv;
843e71b7053SJung-uk Kim #endif
844e71b7053SJung-uk Kim     const char *servername = NULL;
8455ac766abSJung-uk Kim     char *sname_alloc = NULL;
846e71b7053SJung-uk Kim     int noservername = 0;
847e71b7053SJung-uk Kim     const char *alpn_in = NULL;
848e71b7053SJung-uk Kim     tlsextctx tlsextcbp = { NULL, 0 };
849e71b7053SJung-uk Kim     const char *ssl_config = NULL;
850e71b7053SJung-uk Kim #define MAX_SI_TYPES 100
851e71b7053SJung-uk Kim     unsigned short serverinfo_types[MAX_SI_TYPES];
852e71b7053SJung-uk Kim     int serverinfo_count = 0, start = 0, len;
853e71b7053SJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
854e71b7053SJung-uk Kim     const char *next_proto_neg_in = NULL;
855db522d3aSSimon L. B. Nielsen #endif
8561f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
8571f13597dSJung-uk Kim     char *srppass = NULL;
8581f13597dSJung-uk Kim     int srp_lateuser = 0;
8591f13597dSJung-uk Kim     SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };
8601f13597dSJung-uk Kim #endif
86109286989SJung-uk Kim #ifndef OPENSSL_NO_SRTP
862e71b7053SJung-uk Kim     char *srtp_profiles = NULL;
86309286989SJung-uk Kim #endif
864e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
865e71b7053SJung-uk Kim     char *ctlog_file = NULL;
866e71b7053SJung-uk Kim     int ct_validation = 0;
867e71b7053SJung-uk Kim #endif
868e71b7053SJung-uk Kim     int min_version = 0, max_version = 0, prot_opt = 0, no_prot_opt = 0;
869e71b7053SJung-uk Kim     int async = 0;
870e71b7053SJung-uk Kim     unsigned int max_send_fragment = 0;
871e71b7053SJung-uk Kim     unsigned int split_send_fragment = 0, max_pipelines = 0;
872e71b7053SJung-uk Kim     enum { use_inet, use_unix, use_unknown } connect_type = use_unknown;
873e71b7053SJung-uk Kim     int count4or6 = 0;
874e71b7053SJung-uk Kim     uint8_t maxfraglen = 0;
875e71b7053SJung-uk Kim     int c_nbio = 0, c_msg = 0, c_ign_eof = 0, c_brief = 0;
876e71b7053SJung-uk Kim     int c_tlsextdebug = 0;
877e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
878e71b7053SJung-uk Kim     int c_status_req = 0;
879e71b7053SJung-uk Kim #endif
880e71b7053SJung-uk Kim     BIO *bio_c_msg = NULL;
881e71b7053SJung-uk Kim     const char *keylog_file = NULL, *early_data_file = NULL;
882e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
883e71b7053SJung-uk Kim     int isdtls = 0;
884e71b7053SJung-uk Kim #endif
885e71b7053SJung-uk Kim     char *psksessf = NULL;
886e71b7053SJung-uk Kim     int enable_pha = 0;
8876935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP
8886935a639SJung-uk Kim     int sctp_label_bug = 0;
8896935a639SJung-uk Kim #endif
890b077aed3SPierre Pronchery     int ignore_unexpected_eof = 0;
891e71b7053SJung-uk Kim 
892e71b7053SJung-uk Kim     FD_ZERO(&readfds);
893e71b7053SJung-uk Kim     FD_ZERO(&writefds);
894e71b7053SJung-uk Kim /* Known false-positive of MemorySanitizer. */
895e71b7053SJung-uk Kim #if defined(__has_feature)
896e71b7053SJung-uk Kim # if __has_feature(memory_sanitizer)
897e71b7053SJung-uk Kim     __msan_unpoison(&readfds, sizeof(readfds));
898e71b7053SJung-uk Kim     __msan_unpoison(&writefds, sizeof(writefds));
899e71b7053SJung-uk Kim # endif
9001f13597dSJung-uk Kim #endif
9011f13597dSJung-uk Kim 
902e71b7053SJung-uk Kim     c_quiet = 0;
903e71b7053SJung-uk Kim     c_debug = 0;
904e71b7053SJung-uk Kim     c_showcerts = 0;
905e71b7053SJung-uk Kim     c_nbio = 0;
906b077aed3SPierre Pronchery     port = OPENSSL_strdup(PORT);
907e71b7053SJung-uk Kim     vpm = X509_VERIFY_PARAM_new();
908e71b7053SJung-uk Kim     cctx = SSL_CONF_CTX_new();
909e71b7053SJung-uk Kim 
910b077aed3SPierre Pronchery     if (port == NULL || vpm == NULL || cctx == NULL) {
911b077aed3SPierre Pronchery         BIO_printf(bio_err, "%s: out of memory\n", opt_getprog());
912e71b7053SJung-uk Kim         goto end;
913e71b7053SJung-uk Kim     }
914e71b7053SJung-uk Kim 
915e71b7053SJung-uk Kim     cbuf = app_malloc(BUFSIZZ, "cbuf");
916e71b7053SJung-uk Kim     sbuf = app_malloc(BUFSIZZ, "sbuf");
917e71b7053SJung-uk Kim     mbuf = app_malloc(BUFSIZZ, "mbuf");
918e71b7053SJung-uk Kim 
919e71b7053SJung-uk Kim     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE);
920e71b7053SJung-uk Kim 
921e71b7053SJung-uk Kim     prog = opt_init(argc, argv, s_client_options);
922e71b7053SJung-uk Kim     while ((o = opt_next()) != OPT_EOF) {
923e71b7053SJung-uk Kim         /* Check for intermixing flags. */
924e71b7053SJung-uk Kim         if (connect_type == use_unix && IS_INET_FLAG(o)) {
925e71b7053SJung-uk Kim             BIO_printf(bio_err,
926e71b7053SJung-uk Kim                        "%s: Intermixed protocol flags (unix and internet domains)\n",
927e71b7053SJung-uk Kim                        prog);
928e71b7053SJung-uk Kim             goto end;
929e71b7053SJung-uk Kim         }
930e71b7053SJung-uk Kim         if (connect_type == use_inet && IS_UNIX_FLAG(o)) {
931e71b7053SJung-uk Kim             BIO_printf(bio_err,
932e71b7053SJung-uk Kim                        "%s: Intermixed protocol flags (internet and unix domains)\n",
933e71b7053SJung-uk Kim                        prog);
934e71b7053SJung-uk Kim             goto end;
935e71b7053SJung-uk Kim         }
936e71b7053SJung-uk Kim 
937e71b7053SJung-uk Kim         if (IS_PROT_FLAG(o) && ++prot_opt > 1) {
938aeb5019cSJung-uk Kim             BIO_printf(bio_err, "Cannot supply multiple protocol flags\n");
939aeb5019cSJung-uk Kim             goto end;
940aeb5019cSJung-uk Kim         }
941e71b7053SJung-uk Kim         if (IS_NO_PROT_FLAG(o))
942e71b7053SJung-uk Kim             no_prot_opt++;
943aeb5019cSJung-uk Kim         if (prot_opt == 1 && no_prot_opt) {
944e71b7053SJung-uk Kim             BIO_printf(bio_err,
945e71b7053SJung-uk Kim                        "Cannot supply both a protocol flag and '-no_<prot>'\n");
946aeb5019cSJung-uk Kim             goto end;
947aeb5019cSJung-uk Kim         }
948aeb5019cSJung-uk Kim 
949e71b7053SJung-uk Kim         switch (o) {
950e71b7053SJung-uk Kim         case OPT_EOF:
951e71b7053SJung-uk Kim         case OPT_ERR:
952e71b7053SJung-uk Kim  opthelp:
953e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
954e71b7053SJung-uk Kim             goto end;
955e71b7053SJung-uk Kim         case OPT_HELP:
956e71b7053SJung-uk Kim             opt_help(s_client_options);
957e71b7053SJung-uk Kim             ret = 0;
958e71b7053SJung-uk Kim             goto end;
959e71b7053SJung-uk Kim         case OPT_4:
960e71b7053SJung-uk Kim             connect_type = use_inet;
961e71b7053SJung-uk Kim             socket_family = AF_INET;
962e71b7053SJung-uk Kim             count4or6++;
963e71b7053SJung-uk Kim             break;
964e71b7053SJung-uk Kim #ifdef AF_INET6
965e71b7053SJung-uk Kim         case OPT_6:
966e71b7053SJung-uk Kim             connect_type = use_inet;
967e71b7053SJung-uk Kim             socket_family = AF_INET6;
968e71b7053SJung-uk Kim             count4or6++;
969e71b7053SJung-uk Kim             break;
970e71b7053SJung-uk Kim #endif
971e71b7053SJung-uk Kim         case OPT_HOST:
972e71b7053SJung-uk Kim             connect_type = use_inet;
973e71b7053SJung-uk Kim             freeandcopy(&host, opt_arg());
974e71b7053SJung-uk Kim             break;
975e71b7053SJung-uk Kim         case OPT_PORT:
976e71b7053SJung-uk Kim             connect_type = use_inet;
977e71b7053SJung-uk Kim             freeandcopy(&port, opt_arg());
978e71b7053SJung-uk Kim             break;
979e71b7053SJung-uk Kim         case OPT_CONNECT:
980e71b7053SJung-uk Kim             connect_type = use_inet;
981e71b7053SJung-uk Kim             freeandcopy(&connectstr, opt_arg());
982e71b7053SJung-uk Kim             break;
983e71b7053SJung-uk Kim         case OPT_BIND:
984e71b7053SJung-uk Kim             freeandcopy(&bindstr, opt_arg());
985e71b7053SJung-uk Kim             break;
986e71b7053SJung-uk Kim         case OPT_PROXY:
987e71b7053SJung-uk Kim             proxystr = opt_arg();
988b077aed3SPierre Pronchery             break;
989b077aed3SPierre Pronchery         case OPT_PROXY_USER:
990b077aed3SPierre Pronchery             proxyuser = opt_arg();
991b077aed3SPierre Pronchery             break;
992b077aed3SPierre Pronchery         case OPT_PROXY_PASS:
993b077aed3SPierre Pronchery             proxypassarg = opt_arg();
994e71b7053SJung-uk Kim             break;
995e71b7053SJung-uk Kim #ifdef AF_UNIX
996e71b7053SJung-uk Kim         case OPT_UNIX:
997e71b7053SJung-uk Kim             connect_type = use_unix;
998e71b7053SJung-uk Kim             socket_family = AF_UNIX;
999e71b7053SJung-uk Kim             freeandcopy(&host, opt_arg());
1000e71b7053SJung-uk Kim             break;
1001e71b7053SJung-uk Kim #endif
1002e71b7053SJung-uk Kim         case OPT_XMPPHOST:
1003e71b7053SJung-uk Kim             /* fall through, since this is an alias */
1004e71b7053SJung-uk Kim         case OPT_PROTOHOST:
1005e71b7053SJung-uk Kim             protohost = opt_arg();
1006e71b7053SJung-uk Kim             break;
1007e71b7053SJung-uk Kim         case OPT_VERIFY:
1008e71b7053SJung-uk Kim             verify = SSL_VERIFY_PEER;
1009e71b7053SJung-uk Kim             verify_args.depth = atoi(opt_arg());
1010e71b7053SJung-uk Kim             if (!c_quiet)
1011e71b7053SJung-uk Kim                 BIO_printf(bio_err, "verify depth is %d\n", verify_args.depth);
1012e71b7053SJung-uk Kim             break;
1013e71b7053SJung-uk Kim         case OPT_CERT:
1014e71b7053SJung-uk Kim             cert_file = opt_arg();
1015e71b7053SJung-uk Kim             break;
1016e71b7053SJung-uk Kim         case OPT_NAMEOPT:
1017e71b7053SJung-uk Kim             if (!set_nameopt(opt_arg()))
1018e71b7053SJung-uk Kim                 goto end;
1019e71b7053SJung-uk Kim             break;
1020e71b7053SJung-uk Kim         case OPT_CRL:
1021e71b7053SJung-uk Kim             crl_file = opt_arg();
1022e71b7053SJung-uk Kim             break;
1023e71b7053SJung-uk Kim         case OPT_CRL_DOWNLOAD:
1024e71b7053SJung-uk Kim             crl_download = 1;
1025e71b7053SJung-uk Kim             break;
1026e71b7053SJung-uk Kim         case OPT_SESS_OUT:
1027e71b7053SJung-uk Kim             sess_out = opt_arg();
1028e71b7053SJung-uk Kim             break;
1029e71b7053SJung-uk Kim         case OPT_SESS_IN:
1030e71b7053SJung-uk Kim             sess_in = opt_arg();
1031e71b7053SJung-uk Kim             break;
1032e71b7053SJung-uk Kim         case OPT_CERTFORM:
1033b077aed3SPierre Pronchery             if (!opt_format(opt_arg(), OPT_FMT_ANY, &cert_format))
1034e71b7053SJung-uk Kim                 goto opthelp;
1035e71b7053SJung-uk Kim             break;
1036e71b7053SJung-uk Kim         case OPT_CRLFORM:
1037e71b7053SJung-uk Kim             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))
1038e71b7053SJung-uk Kim                 goto opthelp;
1039e71b7053SJung-uk Kim             break;
1040e71b7053SJung-uk Kim         case OPT_VERIFY_RET_ERROR:
10416935a639SJung-uk Kim             verify = SSL_VERIFY_PEER;
1042e71b7053SJung-uk Kim             verify_args.return_error = 1;
1043e71b7053SJung-uk Kim             break;
1044e71b7053SJung-uk Kim         case OPT_VERIFY_QUIET:
1045e71b7053SJung-uk Kim             verify_args.quiet = 1;
1046e71b7053SJung-uk Kim             break;
1047e71b7053SJung-uk Kim         case OPT_BRIEF:
1048e71b7053SJung-uk Kim             c_brief = verify_args.quiet = c_quiet = 1;
1049e71b7053SJung-uk Kim             break;
1050e71b7053SJung-uk Kim         case OPT_S_CASES:
1051e71b7053SJung-uk Kim             if (ssl_args == NULL)
1052e71b7053SJung-uk Kim                 ssl_args = sk_OPENSSL_STRING_new_null();
1053e71b7053SJung-uk Kim             if (ssl_args == NULL
1054e71b7053SJung-uk Kim                 || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())
1055e71b7053SJung-uk Kim                 || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {
1056e71b7053SJung-uk Kim                 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1057e71b7053SJung-uk Kim                 goto end;
1058e71b7053SJung-uk Kim             }
1059e71b7053SJung-uk Kim             break;
1060e71b7053SJung-uk Kim         case OPT_V_CASES:
1061e71b7053SJung-uk Kim             if (!opt_verify(o, vpm))
1062e71b7053SJung-uk Kim                 goto end;
1063e71b7053SJung-uk Kim             vpmtouched++;
1064e71b7053SJung-uk Kim             break;
1065e71b7053SJung-uk Kim         case OPT_X_CASES:
1066e71b7053SJung-uk Kim             if (!args_excert(o, &exc))
1067e71b7053SJung-uk Kim                 goto end;
1068e71b7053SJung-uk Kim             break;
1069b077aed3SPierre Pronchery         case OPT_IGNORE_UNEXPECTED_EOF:
1070b077aed3SPierre Pronchery             ignore_unexpected_eof = 1;
1071b077aed3SPierre Pronchery             break;
1072e71b7053SJung-uk Kim         case OPT_PREXIT:
1073e71b7053SJung-uk Kim             prexit = 1;
1074e71b7053SJung-uk Kim             break;
1075e71b7053SJung-uk Kim         case OPT_CRLF:
1076e71b7053SJung-uk Kim             crlf = 1;
1077e71b7053SJung-uk Kim             break;
1078e71b7053SJung-uk Kim         case OPT_QUIET:
1079e71b7053SJung-uk Kim             c_quiet = c_ign_eof = 1;
1080e71b7053SJung-uk Kim             break;
1081e71b7053SJung-uk Kim         case OPT_NBIO:
1082e71b7053SJung-uk Kim             c_nbio = 1;
1083e71b7053SJung-uk Kim             break;
1084e71b7053SJung-uk Kim         case OPT_NOCMDS:
1085e71b7053SJung-uk Kim             cmdletters = 0;
1086e71b7053SJung-uk Kim             break;
1087e71b7053SJung-uk Kim         case OPT_ENGINE:
1088e71b7053SJung-uk Kim             e = setup_engine(opt_arg(), 1);
1089e71b7053SJung-uk Kim             break;
1090e71b7053SJung-uk Kim         case OPT_SSL_CLIENT_ENGINE:
1091e71b7053SJung-uk Kim #ifndef OPENSSL_NO_ENGINE
1092b077aed3SPierre Pronchery             ssl_client_engine = setup_engine(opt_arg(), 0);
1093e71b7053SJung-uk Kim             if (ssl_client_engine == NULL) {
1094e71b7053SJung-uk Kim                 BIO_printf(bio_err, "Error getting client auth engine\n");
1095e71b7053SJung-uk Kim                 goto opthelp;
1096e71b7053SJung-uk Kim             }
1097e71b7053SJung-uk Kim #endif
1098e71b7053SJung-uk Kim             break;
1099e71b7053SJung-uk Kim         case OPT_R_CASES:
1100e71b7053SJung-uk Kim             if (!opt_rand(o))
1101e71b7053SJung-uk Kim                 goto end;
1102e71b7053SJung-uk Kim             break;
1103b077aed3SPierre Pronchery         case OPT_PROV_CASES:
1104b077aed3SPierre Pronchery             if (!opt_provider(o))
1105b077aed3SPierre Pronchery                 goto end;
1106b077aed3SPierre Pronchery             break;
1107e71b7053SJung-uk Kim         case OPT_IGN_EOF:
1108e71b7053SJung-uk Kim             c_ign_eof = 1;
1109e71b7053SJung-uk Kim             break;
1110e71b7053SJung-uk Kim         case OPT_NO_IGN_EOF:
1111e71b7053SJung-uk Kim             c_ign_eof = 0;
1112e71b7053SJung-uk Kim             break;
1113e71b7053SJung-uk Kim         case OPT_DEBUG:
1114e71b7053SJung-uk Kim             c_debug = 1;
1115e71b7053SJung-uk Kim             break;
1116e71b7053SJung-uk Kim         case OPT_TLSEXTDEBUG:
1117e71b7053SJung-uk Kim             c_tlsextdebug = 1;
1118e71b7053SJung-uk Kim             break;
1119e71b7053SJung-uk Kim         case OPT_STATUS:
1120e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
1121e71b7053SJung-uk Kim             c_status_req = 1;
1122e71b7053SJung-uk Kim #endif
1123e71b7053SJung-uk Kim             break;
1124e71b7053SJung-uk Kim         case OPT_WDEBUG:
1125e71b7053SJung-uk Kim #ifdef WATT32
1126e71b7053SJung-uk Kim             dbug_init();
1127e71b7053SJung-uk Kim #endif
1128e71b7053SJung-uk Kim             break;
1129e71b7053SJung-uk Kim         case OPT_MSG:
1130e71b7053SJung-uk Kim             c_msg = 1;
1131e71b7053SJung-uk Kim             break;
1132e71b7053SJung-uk Kim         case OPT_MSGFILE:
1133e71b7053SJung-uk Kim             bio_c_msg = BIO_new_file(opt_arg(), "w");
1134b077aed3SPierre Pronchery             if (bio_c_msg == NULL) {
1135b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Error writing file %s\n", opt_arg());
1136b077aed3SPierre Pronchery                 goto end;
1137b077aed3SPierre Pronchery             }
1138e71b7053SJung-uk Kim             break;
1139e71b7053SJung-uk Kim         case OPT_TRACE:
1140e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE
1141e71b7053SJung-uk Kim             c_msg = 2;
1142e71b7053SJung-uk Kim #endif
1143e71b7053SJung-uk Kim             break;
1144e71b7053SJung-uk Kim         case OPT_SECURITY_DEBUG:
1145e71b7053SJung-uk Kim             sdebug = 1;
1146e71b7053SJung-uk Kim             break;
1147e71b7053SJung-uk Kim         case OPT_SECURITY_DEBUG_VERBOSE:
1148e71b7053SJung-uk Kim             sdebug = 2;
1149e71b7053SJung-uk Kim             break;
1150e71b7053SJung-uk Kim         case OPT_SHOWCERTS:
1151e71b7053SJung-uk Kim             c_showcerts = 1;
1152e71b7053SJung-uk Kim             break;
1153e71b7053SJung-uk Kim         case OPT_NBIO_TEST:
1154e71b7053SJung-uk Kim             nbio_test = 1;
1155e71b7053SJung-uk Kim             break;
1156e71b7053SJung-uk Kim         case OPT_STATE:
1157e71b7053SJung-uk Kim             state = 1;
1158e71b7053SJung-uk Kim             break;
1159e71b7053SJung-uk Kim         case OPT_PSK_IDENTITY:
1160e71b7053SJung-uk Kim             psk_identity = opt_arg();
1161e71b7053SJung-uk Kim             break;
1162e71b7053SJung-uk Kim         case OPT_PSK:
1163e71b7053SJung-uk Kim             for (p = psk_key = opt_arg(); *p; p++) {
1164e71b7053SJung-uk Kim                 if (isxdigit(_UC(*p)))
1165e71b7053SJung-uk Kim                     continue;
1166e71b7053SJung-uk Kim                 BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
1167e71b7053SJung-uk Kim                 goto end;
1168e71b7053SJung-uk Kim             }
1169e71b7053SJung-uk Kim             break;
1170e71b7053SJung-uk Kim         case OPT_PSK_SESS:
1171e71b7053SJung-uk Kim             psksessf = opt_arg();
1172e71b7053SJung-uk Kim             break;
1173e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
1174e71b7053SJung-uk Kim         case OPT_SRPUSER:
1175e71b7053SJung-uk Kim             srp_arg.srplogin = opt_arg();
1176e71b7053SJung-uk Kim             if (min_version < TLS1_VERSION)
1177e71b7053SJung-uk Kim                 min_version = TLS1_VERSION;
1178e71b7053SJung-uk Kim             break;
1179e71b7053SJung-uk Kim         case OPT_SRPPASS:
1180e71b7053SJung-uk Kim             srppass = opt_arg();
1181e71b7053SJung-uk Kim             if (min_version < TLS1_VERSION)
1182e71b7053SJung-uk Kim                 min_version = TLS1_VERSION;
1183e71b7053SJung-uk Kim             break;
1184e71b7053SJung-uk Kim         case OPT_SRP_STRENGTH:
1185e71b7053SJung-uk Kim             srp_arg.strength = atoi(opt_arg());
1186e71b7053SJung-uk Kim             BIO_printf(bio_err, "SRP minimal length for N is %d\n",
1187e71b7053SJung-uk Kim                        srp_arg.strength);
1188e71b7053SJung-uk Kim             if (min_version < TLS1_VERSION)
1189e71b7053SJung-uk Kim                 min_version = TLS1_VERSION;
1190e71b7053SJung-uk Kim             break;
1191e71b7053SJung-uk Kim         case OPT_SRP_LATEUSER:
1192e71b7053SJung-uk Kim             srp_lateuser = 1;
1193e71b7053SJung-uk Kim             if (min_version < TLS1_VERSION)
1194e71b7053SJung-uk Kim                 min_version = TLS1_VERSION;
1195e71b7053SJung-uk Kim             break;
1196e71b7053SJung-uk Kim         case OPT_SRP_MOREGROUPS:
1197e71b7053SJung-uk Kim             srp_arg.amp = 1;
1198e71b7053SJung-uk Kim             if (min_version < TLS1_VERSION)
1199e71b7053SJung-uk Kim                 min_version = TLS1_VERSION;
1200e71b7053SJung-uk Kim             break;
1201e71b7053SJung-uk Kim #endif
1202e71b7053SJung-uk Kim         case OPT_SSL_CONFIG:
1203e71b7053SJung-uk Kim             ssl_config = opt_arg();
1204e71b7053SJung-uk Kim             break;
1205e71b7053SJung-uk Kim         case OPT_SSL3:
1206e71b7053SJung-uk Kim             min_version = SSL3_VERSION;
1207e71b7053SJung-uk Kim             max_version = SSL3_VERSION;
120858f35182SJung-uk Kim             socket_type = SOCK_STREAM;
120958f35182SJung-uk Kim #ifndef OPENSSL_NO_DTLS
121058f35182SJung-uk Kim             isdtls = 0;
121158f35182SJung-uk Kim #endif
1212e71b7053SJung-uk Kim             break;
1213e71b7053SJung-uk Kim         case OPT_TLS1_3:
1214e71b7053SJung-uk Kim             min_version = TLS1_3_VERSION;
1215e71b7053SJung-uk Kim             max_version = TLS1_3_VERSION;
121658f35182SJung-uk Kim             socket_type = SOCK_STREAM;
121758f35182SJung-uk Kim #ifndef OPENSSL_NO_DTLS
121858f35182SJung-uk Kim             isdtls = 0;
121958f35182SJung-uk Kim #endif
1220e71b7053SJung-uk Kim             break;
1221e71b7053SJung-uk Kim         case OPT_TLS1_2:
1222e71b7053SJung-uk Kim             min_version = TLS1_2_VERSION;
1223e71b7053SJung-uk Kim             max_version = TLS1_2_VERSION;
122458f35182SJung-uk Kim             socket_type = SOCK_STREAM;
122558f35182SJung-uk Kim #ifndef OPENSSL_NO_DTLS
122658f35182SJung-uk Kim             isdtls = 0;
122758f35182SJung-uk Kim #endif
1228e71b7053SJung-uk Kim             break;
1229e71b7053SJung-uk Kim         case OPT_TLS1_1:
1230e71b7053SJung-uk Kim             min_version = TLS1_1_VERSION;
1231e71b7053SJung-uk Kim             max_version = TLS1_1_VERSION;
123258f35182SJung-uk Kim             socket_type = SOCK_STREAM;
123358f35182SJung-uk Kim #ifndef OPENSSL_NO_DTLS
123458f35182SJung-uk Kim             isdtls = 0;
123558f35182SJung-uk Kim #endif
1236e71b7053SJung-uk Kim             break;
1237e71b7053SJung-uk Kim         case OPT_TLS1:
1238e71b7053SJung-uk Kim             min_version = TLS1_VERSION;
1239e71b7053SJung-uk Kim             max_version = TLS1_VERSION;
124058f35182SJung-uk Kim             socket_type = SOCK_STREAM;
124158f35182SJung-uk Kim #ifndef OPENSSL_NO_DTLS
124258f35182SJung-uk Kim             isdtls = 0;
124358f35182SJung-uk Kim #endif
1244e71b7053SJung-uk Kim             break;
1245e71b7053SJung-uk Kim         case OPT_DTLS:
1246e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1247e71b7053SJung-uk Kim             meth = DTLS_client_method();
1248e71b7053SJung-uk Kim             socket_type = SOCK_DGRAM;
1249e71b7053SJung-uk Kim             isdtls = 1;
1250e71b7053SJung-uk Kim #endif
1251e71b7053SJung-uk Kim             break;
1252e71b7053SJung-uk Kim         case OPT_DTLS1:
1253e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS1
1254e71b7053SJung-uk Kim             meth = DTLS_client_method();
1255e71b7053SJung-uk Kim             min_version = DTLS1_VERSION;
1256e71b7053SJung-uk Kim             max_version = DTLS1_VERSION;
1257e71b7053SJung-uk Kim             socket_type = SOCK_DGRAM;
1258e71b7053SJung-uk Kim             isdtls = 1;
1259e71b7053SJung-uk Kim #endif
1260e71b7053SJung-uk Kim             break;
1261e71b7053SJung-uk Kim         case OPT_DTLS1_2:
1262e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS1_2
1263e71b7053SJung-uk Kim             meth = DTLS_client_method();
1264e71b7053SJung-uk Kim             min_version = DTLS1_2_VERSION;
1265e71b7053SJung-uk Kim             max_version = DTLS1_2_VERSION;
1266e71b7053SJung-uk Kim             socket_type = SOCK_DGRAM;
1267e71b7053SJung-uk Kim             isdtls = 1;
1268e71b7053SJung-uk Kim #endif
1269e71b7053SJung-uk Kim             break;
1270e71b7053SJung-uk Kim         case OPT_SCTP:
1271e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
1272e71b7053SJung-uk Kim             protocol = IPPROTO_SCTP;
1273e71b7053SJung-uk Kim #endif
1274e71b7053SJung-uk Kim             break;
12756935a639SJung-uk Kim         case OPT_SCTP_LABEL_BUG:
12766935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP
12776935a639SJung-uk Kim             sctp_label_bug = 1;
12786935a639SJung-uk Kim #endif
12796935a639SJung-uk Kim             break;
1280e71b7053SJung-uk Kim         case OPT_TIMEOUT:
1281e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1282e71b7053SJung-uk Kim             enable_timeouts = 1;
1283e71b7053SJung-uk Kim #endif
1284e71b7053SJung-uk Kim             break;
1285e71b7053SJung-uk Kim         case OPT_MTU:
1286e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
1287e71b7053SJung-uk Kim             socket_mtu = atol(opt_arg());
1288e71b7053SJung-uk Kim #endif
1289e71b7053SJung-uk Kim             break;
1290e71b7053SJung-uk Kim         case OPT_FALLBACKSCSV:
1291e71b7053SJung-uk Kim             fallback_scsv = 1;
1292e71b7053SJung-uk Kim             break;
1293e71b7053SJung-uk Kim         case OPT_KEYFORM:
1294b077aed3SPierre Pronchery             if (!opt_format(opt_arg(), OPT_FMT_ANY, &key_format))
1295e71b7053SJung-uk Kim                 goto opthelp;
1296e71b7053SJung-uk Kim             break;
1297e71b7053SJung-uk Kim         case OPT_PASS:
1298e71b7053SJung-uk Kim             passarg = opt_arg();
1299e71b7053SJung-uk Kim             break;
1300e71b7053SJung-uk Kim         case OPT_CERT_CHAIN:
1301e71b7053SJung-uk Kim             chain_file = opt_arg();
1302e71b7053SJung-uk Kim             break;
1303e71b7053SJung-uk Kim         case OPT_KEY:
1304e71b7053SJung-uk Kim             key_file = opt_arg();
1305e71b7053SJung-uk Kim             break;
1306e71b7053SJung-uk Kim         case OPT_RECONNECT:
1307e71b7053SJung-uk Kim             reconnect = 5;
1308e71b7053SJung-uk Kim             break;
1309e71b7053SJung-uk Kim         case OPT_CAPATH:
1310e71b7053SJung-uk Kim             CApath = opt_arg();
1311e71b7053SJung-uk Kim             break;
1312e71b7053SJung-uk Kim         case OPT_NOCAPATH:
1313e71b7053SJung-uk Kim             noCApath = 1;
1314e71b7053SJung-uk Kim             break;
1315e71b7053SJung-uk Kim         case OPT_CHAINCAPATH:
1316e71b7053SJung-uk Kim             chCApath = opt_arg();
1317e71b7053SJung-uk Kim             break;
1318e71b7053SJung-uk Kim         case OPT_VERIFYCAPATH:
1319e71b7053SJung-uk Kim             vfyCApath = opt_arg();
1320e71b7053SJung-uk Kim             break;
1321e71b7053SJung-uk Kim         case OPT_BUILD_CHAIN:
1322e71b7053SJung-uk Kim             build_chain = 1;
1323e71b7053SJung-uk Kim             break;
1324e71b7053SJung-uk Kim         case OPT_REQCAFILE:
1325e71b7053SJung-uk Kim             ReqCAfile = opt_arg();
1326e71b7053SJung-uk Kim             break;
1327e71b7053SJung-uk Kim         case OPT_CAFILE:
1328e71b7053SJung-uk Kim             CAfile = opt_arg();
1329e71b7053SJung-uk Kim             break;
1330e71b7053SJung-uk Kim         case OPT_NOCAFILE:
1331e71b7053SJung-uk Kim             noCAfile = 1;
1332e71b7053SJung-uk Kim             break;
1333e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
1334e71b7053SJung-uk Kim         case OPT_NOCT:
1335e71b7053SJung-uk Kim             ct_validation = 0;
1336e71b7053SJung-uk Kim             break;
1337e71b7053SJung-uk Kim         case OPT_CT:
1338e71b7053SJung-uk Kim             ct_validation = 1;
1339e71b7053SJung-uk Kim             break;
1340e71b7053SJung-uk Kim         case OPT_CTLOG_FILE:
1341e71b7053SJung-uk Kim             ctlog_file = opt_arg();
1342e71b7053SJung-uk Kim             break;
1343e71b7053SJung-uk Kim #endif
1344e71b7053SJung-uk Kim         case OPT_CHAINCAFILE:
1345e71b7053SJung-uk Kim             chCAfile = opt_arg();
1346e71b7053SJung-uk Kim             break;
1347e71b7053SJung-uk Kim         case OPT_VERIFYCAFILE:
1348e71b7053SJung-uk Kim             vfyCAfile = opt_arg();
1349e71b7053SJung-uk Kim             break;
1350b077aed3SPierre Pronchery         case OPT_CASTORE:
1351b077aed3SPierre Pronchery             CAstore = opt_arg();
1352b077aed3SPierre Pronchery             break;
1353b077aed3SPierre Pronchery         case OPT_NOCASTORE:
1354b077aed3SPierre Pronchery             noCAstore = 1;
1355b077aed3SPierre Pronchery             break;
1356b077aed3SPierre Pronchery         case OPT_CHAINCASTORE:
1357b077aed3SPierre Pronchery             chCAstore = opt_arg();
1358b077aed3SPierre Pronchery             break;
1359b077aed3SPierre Pronchery         case OPT_VERIFYCASTORE:
1360b077aed3SPierre Pronchery             vfyCAstore = opt_arg();
1361b077aed3SPierre Pronchery             break;
1362e71b7053SJung-uk Kim         case OPT_DANE_TLSA_DOMAIN:
1363e71b7053SJung-uk Kim             dane_tlsa_domain = opt_arg();
1364e71b7053SJung-uk Kim             break;
1365e71b7053SJung-uk Kim         case OPT_DANE_TLSA_RRDATA:
1366e71b7053SJung-uk Kim             if (dane_tlsa_rrset == NULL)
1367e71b7053SJung-uk Kim                 dane_tlsa_rrset = sk_OPENSSL_STRING_new_null();
1368e71b7053SJung-uk Kim             if (dane_tlsa_rrset == NULL ||
1369e71b7053SJung-uk Kim                 !sk_OPENSSL_STRING_push(dane_tlsa_rrset, opt_arg())) {
1370e71b7053SJung-uk Kim                 BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
1371e71b7053SJung-uk Kim                 goto end;
1372e71b7053SJung-uk Kim             }
1373e71b7053SJung-uk Kim             break;
1374e71b7053SJung-uk Kim         case OPT_DANE_EE_NO_NAME:
1375e71b7053SJung-uk Kim             dane_ee_no_name = 1;
1376e71b7053SJung-uk Kim             break;
1377e71b7053SJung-uk Kim         case OPT_NEXTPROTONEG:
1378e71b7053SJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
1379e71b7053SJung-uk Kim             next_proto_neg_in = opt_arg();
1380e71b7053SJung-uk Kim #endif
1381e71b7053SJung-uk Kim             break;
1382e71b7053SJung-uk Kim         case OPT_ALPN:
1383e71b7053SJung-uk Kim             alpn_in = opt_arg();
1384e71b7053SJung-uk Kim             break;
1385e71b7053SJung-uk Kim         case OPT_SERVERINFO:
1386e71b7053SJung-uk Kim             p = opt_arg();
1387e71b7053SJung-uk Kim             len = strlen(p);
1388e71b7053SJung-uk Kim             for (start = 0, i = 0; i <= len; ++i) {
1389e71b7053SJung-uk Kim                 if (i == len || p[i] == ',') {
1390e71b7053SJung-uk Kim                     serverinfo_types[serverinfo_count] = atoi(p + start);
1391e71b7053SJung-uk Kim                     if (++serverinfo_count == MAX_SI_TYPES)
1392e71b7053SJung-uk Kim                         break;
1393e71b7053SJung-uk Kim                     start = i + 1;
1394e71b7053SJung-uk Kim                 }
1395e71b7053SJung-uk Kim             }
1396e71b7053SJung-uk Kim             break;
1397e71b7053SJung-uk Kim         case OPT_STARTTLS:
1398e71b7053SJung-uk Kim             if (!opt_pair(opt_arg(), services, &starttls_proto))
1399e71b7053SJung-uk Kim                 goto end;
1400e71b7053SJung-uk Kim             break;
1401e71b7053SJung-uk Kim         case OPT_SERVERNAME:
1402e71b7053SJung-uk Kim             servername = opt_arg();
1403e71b7053SJung-uk Kim             break;
1404e71b7053SJung-uk Kim         case OPT_NOSERVERNAME:
1405e71b7053SJung-uk Kim             noservername = 1;
1406e71b7053SJung-uk Kim             break;
1407e71b7053SJung-uk Kim         case OPT_USE_SRTP:
1408e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRTP
1409e71b7053SJung-uk Kim             srtp_profiles = opt_arg();
1410e71b7053SJung-uk Kim #endif
1411e71b7053SJung-uk Kim             break;
1412e71b7053SJung-uk Kim         case OPT_KEYMATEXPORT:
1413e71b7053SJung-uk Kim             keymatexportlabel = opt_arg();
1414e71b7053SJung-uk Kim             break;
1415e71b7053SJung-uk Kim         case OPT_KEYMATEXPORTLEN:
1416e71b7053SJung-uk Kim             keymatexportlen = atoi(opt_arg());
1417e71b7053SJung-uk Kim             break;
1418e71b7053SJung-uk Kim         case OPT_ASYNC:
1419e71b7053SJung-uk Kim             async = 1;
1420e71b7053SJung-uk Kim             break;
1421e71b7053SJung-uk Kim         case OPT_MAXFRAGLEN:
1422e71b7053SJung-uk Kim             len = atoi(opt_arg());
1423e71b7053SJung-uk Kim             switch (len) {
1424e71b7053SJung-uk Kim             case 512:
1425e71b7053SJung-uk Kim                 maxfraglen = TLSEXT_max_fragment_length_512;
1426e71b7053SJung-uk Kim                 break;
1427e71b7053SJung-uk Kim             case 1024:
1428e71b7053SJung-uk Kim                 maxfraglen = TLSEXT_max_fragment_length_1024;
1429e71b7053SJung-uk Kim                 break;
1430e71b7053SJung-uk Kim             case 2048:
1431e71b7053SJung-uk Kim                 maxfraglen = TLSEXT_max_fragment_length_2048;
1432e71b7053SJung-uk Kim                 break;
1433e71b7053SJung-uk Kim             case 4096:
1434e71b7053SJung-uk Kim                 maxfraglen = TLSEXT_max_fragment_length_4096;
1435e71b7053SJung-uk Kim                 break;
1436e71b7053SJung-uk Kim             default:
1437e71b7053SJung-uk Kim                 BIO_printf(bio_err,
1438e71b7053SJung-uk Kim                            "%s: Max Fragment Len %u is out of permitted values",
1439e71b7053SJung-uk Kim                            prog, len);
1440e71b7053SJung-uk Kim                 goto opthelp;
1441e71b7053SJung-uk Kim             }
1442e71b7053SJung-uk Kim             break;
1443e71b7053SJung-uk Kim         case OPT_MAX_SEND_FRAG:
1444e71b7053SJung-uk Kim             max_send_fragment = atoi(opt_arg());
1445e71b7053SJung-uk Kim             break;
1446e71b7053SJung-uk Kim         case OPT_SPLIT_SEND_FRAG:
1447e71b7053SJung-uk Kim             split_send_fragment = atoi(opt_arg());
1448e71b7053SJung-uk Kim             break;
1449e71b7053SJung-uk Kim         case OPT_MAX_PIPELINES:
1450e71b7053SJung-uk Kim             max_pipelines = atoi(opt_arg());
1451e71b7053SJung-uk Kim             break;
1452e71b7053SJung-uk Kim         case OPT_READ_BUF:
1453e71b7053SJung-uk Kim             read_buf_len = atoi(opt_arg());
1454e71b7053SJung-uk Kim             break;
1455e71b7053SJung-uk Kim         case OPT_KEYLOG_FILE:
1456e71b7053SJung-uk Kim             keylog_file = opt_arg();
1457e71b7053SJung-uk Kim             break;
1458e71b7053SJung-uk Kim         case OPT_EARLY_DATA:
1459e71b7053SJung-uk Kim             early_data_file = opt_arg();
1460e71b7053SJung-uk Kim             break;
1461e71b7053SJung-uk Kim         case OPT_ENABLE_PHA:
1462e71b7053SJung-uk Kim             enable_pha = 1;
1463e71b7053SJung-uk Kim             break;
1464e71b7053SJung-uk Kim         }
1465e71b7053SJung-uk Kim     }
1466b077aed3SPierre Pronchery 
1467b077aed3SPierre Pronchery     /* Optional argument is connect string if -connect not used. */
1468b077aed3SPierre Pronchery     argc = opt_num_rest();
1469b077aed3SPierre Pronchery     if (argc == 1) {
1470b077aed3SPierre Pronchery         /* Don't allow -connect and a separate argument. */
1471b077aed3SPierre Pronchery         if (connectstr != NULL) {
1472b077aed3SPierre Pronchery             BIO_printf(bio_err,
1473b077aed3SPierre Pronchery                        "%s: cannot provide both -connect option and target parameter\n",
1474b077aed3SPierre Pronchery                        prog);
1475b077aed3SPierre Pronchery             goto opthelp;
1476b077aed3SPierre Pronchery         }
1477b077aed3SPierre Pronchery         connect_type = use_inet;
1478b077aed3SPierre Pronchery         freeandcopy(&connectstr, *opt_rest());
1479b077aed3SPierre Pronchery     } else if (argc != 0) {
1480b077aed3SPierre Pronchery         goto opthelp;
1481b077aed3SPierre Pronchery     }
1482b077aed3SPierre Pronchery     if (!app_RAND_load())
1483b077aed3SPierre Pronchery         goto end;
1484b077aed3SPierre Pronchery 
1485e71b7053SJung-uk Kim     if (count4or6 >= 2) {
1486e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: Can't use both -4 and -6\n", prog);
1487e71b7053SJung-uk Kim         goto opthelp;
1488e71b7053SJung-uk Kim     }
1489e71b7053SJung-uk Kim     if (noservername) {
1490e71b7053SJung-uk Kim         if (servername != NULL) {
1491e71b7053SJung-uk Kim             BIO_printf(bio_err,
1492e71b7053SJung-uk Kim                        "%s: Can't use -servername and -noservername together\n",
1493e71b7053SJung-uk Kim                        prog);
1494e71b7053SJung-uk Kim             goto opthelp;
1495e71b7053SJung-uk Kim         }
1496e71b7053SJung-uk Kim         if (dane_tlsa_domain != NULL) {
1497e71b7053SJung-uk Kim             BIO_printf(bio_err,
1498e71b7053SJung-uk Kim                "%s: Can't use -dane_tlsa_domain and -noservername together\n",
1499e71b7053SJung-uk Kim                prog);
1500e71b7053SJung-uk Kim             goto opthelp;
1501e71b7053SJung-uk Kim         }
1502e71b7053SJung-uk Kim     }
15035c87c606SMark Murray 
1504e71b7053SJung-uk Kim #ifndef OPENSSL_NO_NEXTPROTONEG
1505e71b7053SJung-uk Kim     if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) {
1506e71b7053SJung-uk Kim         BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n");
1507e71b7053SJung-uk Kim         goto opthelp;
1508e71b7053SJung-uk Kim     }
1509e71b7053SJung-uk Kim #endif
1510b077aed3SPierre Pronchery 
1511b077aed3SPierre Pronchery     if (connectstr != NULL) {
1512e71b7053SJung-uk Kim         int res;
1513e71b7053SJung-uk Kim         char *tmp_host = host, *tmp_port = port;
1514b077aed3SPierre Pronchery 
1515b077aed3SPierre Pronchery         res = BIO_parse_hostserv(connectstr, &host, &port, BIO_PARSE_PRIO_HOST);
1516e71b7053SJung-uk Kim         if (tmp_host != host)
1517e71b7053SJung-uk Kim             OPENSSL_free(tmp_host);
1518e71b7053SJung-uk Kim         if (tmp_port != port)
1519e71b7053SJung-uk Kim             OPENSSL_free(tmp_port);
1520e71b7053SJung-uk Kim         if (!res) {
1521e71b7053SJung-uk Kim             BIO_printf(bio_err,
1522e71b7053SJung-uk Kim                        "%s: -connect argument or target parameter malformed or ambiguous\n",
1523e71b7053SJung-uk Kim                        prog);
1524e71b7053SJung-uk Kim             goto end;
1525e71b7053SJung-uk Kim         }
1526e71b7053SJung-uk Kim     }
1527e71b7053SJung-uk Kim 
1528b077aed3SPierre Pronchery     if (proxystr != NULL) {
1529b077aed3SPierre Pronchery         int res;
1530b077aed3SPierre Pronchery         char *tmp_host = host, *tmp_port = port;
1531b077aed3SPierre Pronchery 
1532b077aed3SPierre Pronchery         if (host == NULL || port == NULL) {
1533b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s: -proxy requires use of -connect or target parameter\n", prog);
1534b077aed3SPierre Pronchery             goto opthelp;
1535b077aed3SPierre Pronchery         }
1536b077aed3SPierre Pronchery 
1537b077aed3SPierre Pronchery         if (servername == NULL && !noservername) {
1538b077aed3SPierre Pronchery             servername = sname_alloc = OPENSSL_strdup(host);
1539b077aed3SPierre Pronchery             if (sname_alloc == NULL) {
1540b077aed3SPierre Pronchery                 BIO_printf(bio_err, "%s: out of memory\n", prog);
1541b077aed3SPierre Pronchery                 goto end;
1542b077aed3SPierre Pronchery             }
1543b077aed3SPierre Pronchery         }
1544b077aed3SPierre Pronchery 
1545b077aed3SPierre Pronchery         /* Retain the original target host:port for use in the HTTP proxy connect string */
1546b077aed3SPierre Pronchery         thost = OPENSSL_strdup(host);
1547b077aed3SPierre Pronchery         tport = OPENSSL_strdup(port);
1548b077aed3SPierre Pronchery         if (thost == NULL || tport == NULL) {
1549b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s: out of memory\n", prog);
1550b077aed3SPierre Pronchery             goto end;
1551b077aed3SPierre Pronchery         }
1552b077aed3SPierre Pronchery 
1553b077aed3SPierre Pronchery         res = BIO_parse_hostserv(proxystr, &host, &port, BIO_PARSE_PRIO_HOST);
1554b077aed3SPierre Pronchery         if (tmp_host != host)
1555b077aed3SPierre Pronchery             OPENSSL_free(tmp_host);
1556b077aed3SPierre Pronchery         if (tmp_port != port)
1557b077aed3SPierre Pronchery             OPENSSL_free(tmp_port);
1558b077aed3SPierre Pronchery         if (!res) {
1559b077aed3SPierre Pronchery             BIO_printf(bio_err,
1560b077aed3SPierre Pronchery                        "%s: -proxy argument malformed or ambiguous\n", prog);
1561b077aed3SPierre Pronchery             goto end;
1562b077aed3SPierre Pronchery         }
1563b077aed3SPierre Pronchery     }
1564b077aed3SPierre Pronchery 
1565e71b7053SJung-uk Kim     if (bindstr != NULL) {
1566e71b7053SJung-uk Kim         int res;
1567e71b7053SJung-uk Kim         res = BIO_parse_hostserv(bindstr, &bindhost, &bindport,
1568e71b7053SJung-uk Kim                                  BIO_PARSE_PRIO_HOST);
1569e71b7053SJung-uk Kim         if (!res) {
1570e71b7053SJung-uk Kim             BIO_printf(bio_err,
1571e71b7053SJung-uk Kim                        "%s: -bind argument parameter malformed or ambiguous\n",
1572e71b7053SJung-uk Kim                        prog);
1573e71b7053SJung-uk Kim             goto end;
1574e71b7053SJung-uk Kim         }
1575e71b7053SJung-uk Kim     }
1576e71b7053SJung-uk Kim 
1577e71b7053SJung-uk Kim #ifdef AF_UNIX
1578e71b7053SJung-uk Kim     if (socket_family == AF_UNIX && socket_type != SOCK_STREAM) {
1579e71b7053SJung-uk Kim         BIO_printf(bio_err,
1580e71b7053SJung-uk Kim                    "Can't use unix sockets and datagrams together\n");
1581e71b7053SJung-uk Kim         goto end;
1582e71b7053SJung-uk Kim     }
1583e71b7053SJung-uk Kim #endif
1584e71b7053SJung-uk Kim 
1585e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
1586e71b7053SJung-uk Kim     if (protocol == IPPROTO_SCTP) {
1587e71b7053SJung-uk Kim         if (socket_type != SOCK_DGRAM) {
1588e71b7053SJung-uk Kim             BIO_printf(bio_err, "Can't use -sctp without DTLS\n");
1589e71b7053SJung-uk Kim             goto end;
1590e71b7053SJung-uk Kim         }
1591e71b7053SJung-uk Kim         /* SCTP is unusual. It uses DTLS over a SOCK_STREAM protocol */
1592e71b7053SJung-uk Kim         socket_type = SOCK_STREAM;
1593e71b7053SJung-uk Kim     }
1594e71b7053SJung-uk Kim #endif
1595e71b7053SJung-uk Kim 
1596e71b7053SJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
15971f13597dSJung-uk Kim     next_proto.status = -1;
15986f9291ceSJung-uk Kim     if (next_proto_neg_in) {
15996f9291ceSJung-uk Kim         next_proto.data =
16006f9291ceSJung-uk Kim             next_protos_parse(&next_proto.len, next_proto_neg_in);
16016f9291ceSJung-uk Kim         if (next_proto.data == NULL) {
16021f13597dSJung-uk Kim             BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n");
16031f13597dSJung-uk Kim             goto end;
16041f13597dSJung-uk Kim         }
16056f9291ceSJung-uk Kim     } else
16061f13597dSJung-uk Kim         next_proto.data = NULL;
16071f13597dSJung-uk Kim #endif
16081f13597dSJung-uk Kim 
1609e71b7053SJung-uk Kim     if (!app_passwd(passarg, NULL, &pass, NULL)) {
1610b077aed3SPierre Pronchery         BIO_printf(bio_err, "Error getting private key password\n");
1611b077aed3SPierre Pronchery         goto end;
1612b077aed3SPierre Pronchery     }
1613b077aed3SPierre Pronchery 
1614b077aed3SPierre Pronchery     if (!app_passwd(proxypassarg, NULL, &proxypass, NULL)) {
1615b077aed3SPierre Pronchery         BIO_printf(bio_err, "Error getting proxy password\n");
1616b077aed3SPierre Pronchery         goto end;
1617b077aed3SPierre Pronchery     }
1618b077aed3SPierre Pronchery 
1619b077aed3SPierre Pronchery     if (proxypass != NULL && proxyuser == NULL) {
1620b077aed3SPierre Pronchery         BIO_printf(bio_err, "Error: Must specify proxy_user with proxy_pass\n");
16213b4e3dcbSSimon L. B. Nielsen         goto end;
16223b4e3dcbSSimon L. B. Nielsen     }
16233b4e3dcbSSimon L. B. Nielsen 
16243b4e3dcbSSimon L. B. Nielsen     if (key_file == NULL)
16253b4e3dcbSSimon L. B. Nielsen         key_file = cert_file;
16263b4e3dcbSSimon L. B. Nielsen 
1627e71b7053SJung-uk Kim     if (key_file != NULL) {
1628e71b7053SJung-uk Kim         key = load_key(key_file, key_format, 0, pass, e,
1629b077aed3SPierre Pronchery                        "client certificate private key");
1630b077aed3SPierre Pronchery         if (key == NULL)
16313b4e3dcbSSimon L. B. Nielsen             goto end;
16323b4e3dcbSSimon L. B. Nielsen     }
16335c87c606SMark Murray 
1634e71b7053SJung-uk Kim     if (cert_file != NULL) {
1635b077aed3SPierre Pronchery         cert = load_cert_pass(cert_file, cert_format, 1, pass,
1636b077aed3SPierre Pronchery                               "client certificate");
1637b077aed3SPierre Pronchery         if (cert == NULL)
1638e71b7053SJung-uk Kim             goto end;
1639e71b7053SJung-uk Kim     }
1640e71b7053SJung-uk Kim 
1641e71b7053SJung-uk Kim     if (chain_file != NULL) {
1642b077aed3SPierre Pronchery         if (!load_certs(chain_file, 0, &chain, pass, "client certificate chain"))
16437bded2dbSJung-uk Kim             goto end;
16447bded2dbSJung-uk Kim     }
16457bded2dbSJung-uk Kim 
1646e71b7053SJung-uk Kim     if (crl_file != NULL) {
16477bded2dbSJung-uk Kim         X509_CRL *crl;
1648b077aed3SPierre Pronchery         crl = load_crl(crl_file, crl_format, 0, "CRL");
1649b077aed3SPierre Pronchery         if (crl == NULL)
16507bded2dbSJung-uk Kim             goto end;
16517bded2dbSJung-uk Kim         crls = sk_X509_CRL_new_null();
1652e71b7053SJung-uk Kim         if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {
16537bded2dbSJung-uk Kim             BIO_puts(bio_err, "Error adding CRL\n");
16547bded2dbSJung-uk Kim             ERR_print_errors(bio_err);
16557bded2dbSJung-uk Kim             X509_CRL_free(crl);
16567bded2dbSJung-uk Kim             goto end;
16577bded2dbSJung-uk Kim         }
16587bded2dbSJung-uk Kim     }
16597bded2dbSJung-uk Kim 
1660e71b7053SJung-uk Kim     if (!load_excert(&exc))
16617bded2dbSJung-uk Kim         goto end;
16627bded2dbSJung-uk Kim 
16636f9291ceSJung-uk Kim     if (bio_c_out == NULL) {
16647bded2dbSJung-uk Kim         if (c_quiet && !c_debug) {
166574664626SKris Kennaway             bio_c_out = BIO_new(BIO_s_null());
1666b077aed3SPierre Pronchery             if (c_msg && bio_c_msg == NULL) {
1667e71b7053SJung-uk Kim                 bio_c_msg = dup_bio_out(FORMAT_TEXT);
1668b077aed3SPierre Pronchery                 if (bio_c_msg == NULL) {
1669b077aed3SPierre Pronchery                     BIO_printf(bio_err, "Out of memory\n");
1670b077aed3SPierre Pronchery                     goto end;
1671b077aed3SPierre Pronchery                 }
1672b077aed3SPierre Pronchery             }
1673b077aed3SPierre Pronchery         } else {
1674e71b7053SJung-uk Kim             bio_c_out = dup_bio_out(FORMAT_TEXT);
167574664626SKris Kennaway         }
1676b077aed3SPierre Pronchery 
1677b077aed3SPierre Pronchery         if (bio_c_out == NULL) {
1678b077aed3SPierre Pronchery             BIO_printf(bio_err, "Unable to create BIO\n");
1679b077aed3SPierre Pronchery             goto end;
1680b077aed3SPierre Pronchery         }
1681b077aed3SPierre Pronchery     }
16821f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
1683e71b7053SJung-uk Kim     if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) {
16841f13597dSJung-uk Kim         BIO_printf(bio_err, "Error getting password\n");
16851f13597dSJung-uk Kim         goto end;
16861f13597dSJung-uk Kim     }
16871f13597dSJung-uk Kim #endif
16881f13597dSJung-uk Kim 
1689b077aed3SPierre Pronchery     ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
16906f9291ceSJung-uk Kim     if (ctx == NULL) {
169174664626SKris Kennaway         ERR_print_errors(bio_err);
169274664626SKris Kennaway         goto end;
169374664626SKris Kennaway     }
169474664626SKris Kennaway 
1695e71b7053SJung-uk Kim     SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
16961f13597dSJung-uk Kim 
1697e71b7053SJung-uk Kim     if (sdebug)
1698e71b7053SJung-uk Kim         ssl_ctx_security_debug(ctx, sdebug);
1699e71b7053SJung-uk Kim 
1700e71b7053SJung-uk Kim     if (!config_ctx(cctx, ssl_args, ctx))
1701e71b7053SJung-uk Kim         goto end;
1702e71b7053SJung-uk Kim 
1703e71b7053SJung-uk Kim     if (ssl_config != NULL) {
1704e71b7053SJung-uk Kim         if (SSL_CTX_config(ctx, ssl_config) == 0) {
1705e71b7053SJung-uk Kim             BIO_printf(bio_err, "Error using configuration \"%s\"\n",
1706e71b7053SJung-uk Kim                        ssl_config);
17077bded2dbSJung-uk Kim             ERR_print_errors(bio_err);
17087bded2dbSJung-uk Kim             goto end;
17097bded2dbSJung-uk Kim         }
1710e71b7053SJung-uk Kim     }
1711e71b7053SJung-uk Kim 
17126935a639SJung-uk Kim #ifndef OPENSSL_NO_SCTP
17136935a639SJung-uk Kim     if (protocol == IPPROTO_SCTP && sctp_label_bug == 1)
17146935a639SJung-uk Kim         SSL_CTX_set_mode(ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG);
17156935a639SJung-uk Kim #endif
17166935a639SJung-uk Kim 
1717e71b7053SJung-uk Kim     if (min_version != 0
1718e71b7053SJung-uk Kim         && SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
1719e71b7053SJung-uk Kim         goto end;
1720e71b7053SJung-uk Kim     if (max_version != 0
1721e71b7053SJung-uk Kim         && SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
1722e71b7053SJung-uk Kim         goto end;
1723e71b7053SJung-uk Kim 
1724b077aed3SPierre Pronchery     if (ignore_unexpected_eof)
1725b077aed3SPierre Pronchery         SSL_CTX_set_options(ctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
1726b077aed3SPierre Pronchery 
1727e71b7053SJung-uk Kim     if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {
1728e71b7053SJung-uk Kim         BIO_printf(bio_err, "Error setting verify params\n");
1729e71b7053SJung-uk Kim         ERR_print_errors(bio_err);
1730e71b7053SJung-uk Kim         goto end;
1731e71b7053SJung-uk Kim     }
1732e71b7053SJung-uk Kim 
1733e71b7053SJung-uk Kim     if (async) {
1734e71b7053SJung-uk Kim         SSL_CTX_set_mode(ctx, SSL_MODE_ASYNC);
1735e71b7053SJung-uk Kim     }
1736e71b7053SJung-uk Kim 
1737e71b7053SJung-uk Kim     if (max_send_fragment > 0
1738e71b7053SJung-uk Kim         && !SSL_CTX_set_max_send_fragment(ctx, max_send_fragment)) {
1739e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: Max send fragment size %u is out of permitted range\n",
1740e71b7053SJung-uk Kim                    prog, max_send_fragment);
1741e71b7053SJung-uk Kim         goto end;
1742e71b7053SJung-uk Kim     }
1743e71b7053SJung-uk Kim 
1744e71b7053SJung-uk Kim     if (split_send_fragment > 0
1745e71b7053SJung-uk Kim         && !SSL_CTX_set_split_send_fragment(ctx, split_send_fragment)) {
1746e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: Split send fragment size %u is out of permitted range\n",
1747e71b7053SJung-uk Kim                    prog, split_send_fragment);
1748e71b7053SJung-uk Kim         goto end;
1749e71b7053SJung-uk Kim     }
1750e71b7053SJung-uk Kim 
1751e71b7053SJung-uk Kim     if (max_pipelines > 0
1752e71b7053SJung-uk Kim         && !SSL_CTX_set_max_pipelines(ctx, max_pipelines)) {
1753e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: Max pipelines %u is out of permitted range\n",
1754e71b7053SJung-uk Kim                    prog, max_pipelines);
1755e71b7053SJung-uk Kim         goto end;
1756e71b7053SJung-uk Kim     }
1757e71b7053SJung-uk Kim 
1758e71b7053SJung-uk Kim     if (read_buf_len > 0) {
1759e71b7053SJung-uk Kim         SSL_CTX_set_default_read_buffer_len(ctx, read_buf_len);
1760e71b7053SJung-uk Kim     }
1761e71b7053SJung-uk Kim 
1762e71b7053SJung-uk Kim     if (maxfraglen > 0
1763e71b7053SJung-uk Kim             && !SSL_CTX_set_tlsext_max_fragment_length(ctx, maxfraglen)) {
1764e71b7053SJung-uk Kim         BIO_printf(bio_err,
1765e71b7053SJung-uk Kim                    "%s: Max Fragment Length code %u is out of permitted values"
1766e71b7053SJung-uk Kim                    "\n", prog, maxfraglen);
1767e71b7053SJung-uk Kim         goto end;
1768e71b7053SJung-uk Kim     }
17697bded2dbSJung-uk Kim 
1770b077aed3SPierre Pronchery     if (!ssl_load_stores(ctx,
1771b077aed3SPierre Pronchery                          vfyCApath, vfyCAfile, vfyCAstore,
1772b077aed3SPierre Pronchery                          chCApath, chCAfile, chCAstore,
17737bded2dbSJung-uk Kim                          crls, crl_download)) {
17747bded2dbSJung-uk Kim         BIO_printf(bio_err, "Error loading store locations\n");
17757bded2dbSJung-uk Kim         ERR_print_errors(bio_err);
17767bded2dbSJung-uk Kim         goto end;
17777bded2dbSJung-uk Kim     }
1778e71b7053SJung-uk Kim     if (ReqCAfile != NULL) {
1779e71b7053SJung-uk Kim         STACK_OF(X509_NAME) *nm = sk_X509_NAME_new_null();
1780e71b7053SJung-uk Kim 
1781e71b7053SJung-uk Kim         if (nm == NULL || !SSL_add_file_cert_subjects_to_stack(nm, ReqCAfile)) {
1782e71b7053SJung-uk Kim             sk_X509_NAME_pop_free(nm, X509_NAME_free);
1783e71b7053SJung-uk Kim             BIO_printf(bio_err, "Error loading CA names\n");
1784e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1785e71b7053SJung-uk Kim             goto end;
1786e71b7053SJung-uk Kim         }
1787e71b7053SJung-uk Kim         SSL_CTX_set0_CA_list(ctx, nm);
1788e71b7053SJung-uk Kim     }
1789db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_ENGINE
17906f9291ceSJung-uk Kim     if (ssl_client_engine) {
17916f9291ceSJung-uk Kim         if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {
1792db522d3aSSimon L. B. Nielsen             BIO_puts(bio_err, "Error setting client auth engine\n");
1793db522d3aSSimon L. B. Nielsen             ERR_print_errors(bio_err);
1794b077aed3SPierre Pronchery             release_engine(ssl_client_engine);
1795db522d3aSSimon L. B. Nielsen             goto end;
1796db522d3aSSimon L. B. Nielsen         }
1797b077aed3SPierre Pronchery         release_engine(ssl_client_engine);
1798db522d3aSSimon L. B. Nielsen     }
1799db522d3aSSimon L. B. Nielsen #endif
1800db522d3aSSimon L. B. Nielsen 
18011f13597dSJung-uk Kim #ifndef OPENSSL_NO_PSK
1802e71b7053SJung-uk Kim     if (psk_key != NULL) {
18031f13597dSJung-uk Kim         if (c_debug)
1804e71b7053SJung-uk Kim             BIO_printf(bio_c_out, "PSK key given, setting client callback\n");
18051f13597dSJung-uk Kim         SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
18061f13597dSJung-uk Kim     }
180709286989SJung-uk Kim #endif
1808e71b7053SJung-uk Kim     if (psksessf != NULL) {
1809e71b7053SJung-uk Kim         BIO *stmp = BIO_new_file(psksessf, "r");
1810e71b7053SJung-uk Kim 
1811e71b7053SJung-uk Kim         if (stmp == NULL) {
1812e71b7053SJung-uk Kim             BIO_printf(bio_err, "Can't open PSK session file %s\n", psksessf);
1813e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1814e71b7053SJung-uk Kim             goto end;
1815e71b7053SJung-uk Kim         }
1816e71b7053SJung-uk Kim         psksess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
1817e71b7053SJung-uk Kim         BIO_free(stmp);
1818e71b7053SJung-uk Kim         if (psksess == NULL) {
1819e71b7053SJung-uk Kim             BIO_printf(bio_err, "Can't read PSK session file %s\n", psksessf);
1820e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1821e71b7053SJung-uk Kim             goto end;
1822e71b7053SJung-uk Kim         }
1823e71b7053SJung-uk Kim     }
1824e71b7053SJung-uk Kim     if (psk_key != NULL || psksess != NULL)
1825e71b7053SJung-uk Kim         SSL_CTX_set_psk_use_session_callback(ctx, psk_use_session_cb);
1826e71b7053SJung-uk Kim 
182709286989SJung-uk Kim #ifndef OPENSSL_NO_SRTP
1828e71b7053SJung-uk Kim     if (srtp_profiles != NULL) {
1829e71b7053SJung-uk Kim         /* Returns 0 on success! */
1830e71b7053SJung-uk Kim         if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {
1831e71b7053SJung-uk Kim             BIO_printf(bio_err, "Error setting SRTP profile\n");
1832e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1833e71b7053SJung-uk Kim             goto end;
1834e71b7053SJung-uk Kim         }
1835e71b7053SJung-uk Kim     }
18361f13597dSJung-uk Kim #endif
1837e71b7053SJung-uk Kim 
1838e71b7053SJung-uk Kim     if (exc != NULL)
18397bded2dbSJung-uk Kim         ssl_ctx_set_excert(ctx, exc);
18406a599222SSimon L. B. Nielsen 
18417bded2dbSJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
1842e71b7053SJung-uk Kim     if (next_proto.data != NULL)
18431f13597dSJung-uk Kim         SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);
18441f13597dSJung-uk Kim #endif
18457bded2dbSJung-uk Kim     if (alpn_in) {
1846e71b7053SJung-uk Kim         size_t alpn_len;
18477bded2dbSJung-uk Kim         unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);
18487bded2dbSJung-uk Kim 
18497bded2dbSJung-uk Kim         if (alpn == NULL) {
18507bded2dbSJung-uk Kim             BIO_printf(bio_err, "Error parsing -alpn argument\n");
18517bded2dbSJung-uk Kim             goto end;
18527bded2dbSJung-uk Kim         }
1853e71b7053SJung-uk Kim         /* Returns 0 on success! */
1854e71b7053SJung-uk Kim         if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) {
1855e71b7053SJung-uk Kim             BIO_printf(bio_err, "Error setting ALPN\n");
1856e71b7053SJung-uk Kim             goto end;
1857e71b7053SJung-uk Kim         }
18587bded2dbSJung-uk Kim         OPENSSL_free(alpn);
18597bded2dbSJung-uk Kim     }
1860e71b7053SJung-uk Kim 
1861e71b7053SJung-uk Kim     for (i = 0; i < serverinfo_count; i++) {
1862e71b7053SJung-uk Kim         if (!SSL_CTX_add_client_custom_ext(ctx,
18637bded2dbSJung-uk Kim                                            serverinfo_types[i],
18647bded2dbSJung-uk Kim                                            NULL, NULL, NULL,
1865e71b7053SJung-uk Kim                                            serverinfo_cli_parse_cb, NULL)) {
1866e71b7053SJung-uk Kim             BIO_printf(bio_err,
1867e71b7053SJung-uk Kim                        "Warning: Unable to add custom extension %u, skipping\n",
1868e71b7053SJung-uk Kim                        serverinfo_types[i]);
18697bded2dbSJung-uk Kim         }
1870e71b7053SJung-uk Kim     }
187174664626SKris Kennaway 
18726f9291ceSJung-uk Kim     if (state)
18736f9291ceSJung-uk Kim         SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);
1874e71b7053SJung-uk Kim 
1875e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
1876e71b7053SJung-uk Kim     /* Enable SCT processing, without early connection termination */
1877e71b7053SJung-uk Kim     if (ct_validation &&
1878e71b7053SJung-uk Kim         !SSL_CTX_enable_ct(ctx, SSL_CT_VALIDATION_PERMISSIVE)) {
1879e71b7053SJung-uk Kim         ERR_print_errors(bio_err);
1880e71b7053SJung-uk Kim         goto end;
1881e71b7053SJung-uk Kim     }
1882e71b7053SJung-uk Kim 
1883e71b7053SJung-uk Kim     if (!ctx_set_ctlog_list_file(ctx, ctlog_file)) {
1884e71b7053SJung-uk Kim         if (ct_validation) {
1885e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1886e71b7053SJung-uk Kim             goto end;
1887e71b7053SJung-uk Kim         }
1888e71b7053SJung-uk Kim 
1889e71b7053SJung-uk Kim         /*
1890e71b7053SJung-uk Kim          * If CT validation is not enabled, the log list isn't needed so don't
1891e71b7053SJung-uk Kim          * show errors or abort. We try to load it regardless because then we
1892e71b7053SJung-uk Kim          * can show the names of the logs any SCTs came from (SCTs may be seen
1893e71b7053SJung-uk Kim          * even with validation disabled).
1894e71b7053SJung-uk Kim          */
1895e71b7053SJung-uk Kim         ERR_clear_error();
1896e71b7053SJung-uk Kim     }
189774664626SKris Kennaway #endif
189874664626SKris Kennaway 
189974664626SKris Kennaway     SSL_CTX_set_verify(ctx, verify, verify_callback);
190074664626SKris Kennaway 
1901b077aed3SPierre Pronchery     if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
1902b077aed3SPierre Pronchery                                   CAstore, noCAstore)) {
190374664626SKris Kennaway         ERR_print_errors(bio_err);
1904e71b7053SJung-uk Kim         goto end;
190574664626SKris Kennaway     }
19067bded2dbSJung-uk Kim 
19077bded2dbSJung-uk Kim     ssl_ctx_add_crls(ctx, crls, crl_download);
1908e71b7053SJung-uk Kim 
19097bded2dbSJung-uk Kim     if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))
19107bded2dbSJung-uk Kim         goto end;
19117bded2dbSJung-uk Kim 
1912e71b7053SJung-uk Kim     if (!noservername) {
1913db522d3aSSimon L. B. Nielsen         tlsextcbp.biodebug = bio_err;
1914db522d3aSSimon L. B. Nielsen         SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
1915db522d3aSSimon L. B. Nielsen         SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);
1916db522d3aSSimon L. B. Nielsen     }
19171f13597dSJung-uk Kim #ifndef OPENSSL_NO_SRP
1918b077aed3SPierre Pronchery     if (srp_arg.srplogin != NULL
1919b077aed3SPierre Pronchery             && !set_up_srp_arg(ctx, &srp_arg, srp_lateuser, c_msg, c_debug))
19201f13597dSJung-uk Kim         goto end;
19211f13597dSJung-uk Kim # endif
1922e71b7053SJung-uk Kim 
1923e71b7053SJung-uk Kim     if (dane_tlsa_domain != NULL) {
1924e71b7053SJung-uk Kim         if (SSL_CTX_dane_enable(ctx) <= 0) {
1925e71b7053SJung-uk Kim             BIO_printf(bio_err,
1926e71b7053SJung-uk Kim                        "%s: Error enabling DANE TLSA authentication.\n",
1927e71b7053SJung-uk Kim                        prog);
1928e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1929e71b7053SJung-uk Kim             goto end;
1930e71b7053SJung-uk Kim         }
1931e71b7053SJung-uk Kim     }
1932e71b7053SJung-uk Kim 
1933e71b7053SJung-uk Kim     /*
1934e71b7053SJung-uk Kim      * In TLSv1.3 NewSessionTicket messages arrive after the handshake and can
1935e71b7053SJung-uk Kim      * come at any time. Therefore we use a callback to write out the session
1936e71b7053SJung-uk Kim      * when we know about it. This approach works for < TLSv1.3 as well.
1937e71b7053SJung-uk Kim      */
1938e71b7053SJung-uk Kim     SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT
1939e71b7053SJung-uk Kim                                         | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1940e71b7053SJung-uk Kim     SSL_CTX_sess_set_new_cb(ctx, new_session_cb);
1941e71b7053SJung-uk Kim 
1942e71b7053SJung-uk Kim     if (set_keylog_file(ctx, keylog_file))
1943e71b7053SJung-uk Kim         goto end;
194474664626SKris Kennaway 
1945f579bf8eSKris Kennaway     con = SSL_new(ctx);
1946e71b7053SJung-uk Kim     if (con == NULL)
1947e71b7053SJung-uk Kim         goto end;
1948e71b7053SJung-uk Kim 
1949e71b7053SJung-uk Kim     if (enable_pha)
1950e71b7053SJung-uk Kim         SSL_set_post_handshake_auth(con, 1);
1951e71b7053SJung-uk Kim 
1952e71b7053SJung-uk Kim     if (sess_in != NULL) {
1953db522d3aSSimon L. B. Nielsen         SSL_SESSION *sess;
1954db522d3aSSimon L. B. Nielsen         BIO *stmp = BIO_new_file(sess_in, "r");
1955e71b7053SJung-uk Kim         if (stmp == NULL) {
19566f9291ceSJung-uk Kim             BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
1957db522d3aSSimon L. B. Nielsen             ERR_print_errors(bio_err);
1958db522d3aSSimon L. B. Nielsen             goto end;
1959db522d3aSSimon L. B. Nielsen         }
1960db522d3aSSimon L. B. Nielsen         sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);
1961db522d3aSSimon L. B. Nielsen         BIO_free(stmp);
1962e71b7053SJung-uk Kim         if (sess == NULL) {
19636f9291ceSJung-uk Kim             BIO_printf(bio_err, "Can't open session file %s\n", sess_in);
1964db522d3aSSimon L. B. Nielsen             ERR_print_errors(bio_err);
1965db522d3aSSimon L. B. Nielsen             goto end;
1966db522d3aSSimon L. B. Nielsen         }
1967e71b7053SJung-uk Kim         if (!SSL_set_session(con, sess)) {
1968e71b7053SJung-uk Kim             BIO_printf(bio_err, "Can't set session\n");
1969e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1970e71b7053SJung-uk Kim             goto end;
1971e71b7053SJung-uk Kim         }
1972e71b7053SJung-uk Kim 
1973db522d3aSSimon L. B. Nielsen         SSL_SESSION_free(sess);
1974db522d3aSSimon L. B. Nielsen     }
1975fa5fddf1SJung-uk Kim 
1976fa5fddf1SJung-uk Kim     if (fallback_scsv)
1977fa5fddf1SJung-uk Kim         SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
1978fa5fddf1SJung-uk Kim 
1979e71b7053SJung-uk Kim     if (!noservername && (servername != NULL || dane_tlsa_domain == NULL)) {
19806935a639SJung-uk Kim         if (servername == NULL) {
19816935a639SJung-uk Kim             if(host == NULL || is_dNS_name(host))
1982e71b7053SJung-uk Kim                 servername = (host == NULL) ? "localhost" : host;
19836935a639SJung-uk Kim         }
19846935a639SJung-uk Kim         if (servername != NULL && !SSL_set_tlsext_host_name(con, servername)) {
1985db522d3aSSimon L. B. Nielsen             BIO_printf(bio_err, "Unable to set TLS servername extension.\n");
1986db522d3aSSimon L. B. Nielsen             ERR_print_errors(bio_err);
1987db522d3aSSimon L. B. Nielsen             goto end;
1988db522d3aSSimon L. B. Nielsen         }
1989db522d3aSSimon L. B. Nielsen     }
1990e71b7053SJung-uk Kim 
1991e71b7053SJung-uk Kim     if (dane_tlsa_domain != NULL) {
1992e71b7053SJung-uk Kim         if (SSL_dane_enable(con, dane_tlsa_domain) <= 0) {
1993e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Error enabling DANE TLSA "
1994e71b7053SJung-uk Kim                        "authentication.\n", prog);
1995e71b7053SJung-uk Kim             ERR_print_errors(bio_err);
1996e71b7053SJung-uk Kim             goto end;
19975c87c606SMark Murray         }
1998e71b7053SJung-uk Kim         if (dane_tlsa_rrset == NULL) {
1999e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: DANE TLSA authentication requires at "
2000e71b7053SJung-uk Kim                        "least one -dane_tlsa_rrdata option.\n", prog);
2001e71b7053SJung-uk Kim             goto end;
2002e71b7053SJung-uk Kim         }
2003e71b7053SJung-uk Kim         if (tlsa_import_rrset(con, dane_tlsa_rrset) <= 0) {
2004e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Failed to import any TLSA "
2005e71b7053SJung-uk Kim                        "records.\n", prog);
2006e71b7053SJung-uk Kim             goto end;
2007e71b7053SJung-uk Kim         }
2008e71b7053SJung-uk Kim         if (dane_ee_no_name)
2009e71b7053SJung-uk Kim             SSL_dane_set_flags(con, DANE_FLAG_NO_DANE_EE_NAMECHECKS);
2010e71b7053SJung-uk Kim     } else if (dane_tlsa_rrset != NULL) {
2011e71b7053SJung-uk Kim         BIO_printf(bio_err, "%s: DANE TLSA authentication requires the "
2012e71b7053SJung-uk Kim                    "-dane_tlsa_domain option.\n", prog);
2013e71b7053SJung-uk Kim         goto end;
2014e71b7053SJung-uk Kim     }
201574664626SKris Kennaway 
201674664626SKris Kennaway  re_start:
2017b077aed3SPierre Pronchery     if (init_client(&sock, host, port, bindhost, bindport, socket_family,
2018e71b7053SJung-uk Kim                     socket_type, protocol) == 0) {
201974664626SKris Kennaway         BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
2020b077aed3SPierre Pronchery         BIO_closesocket(sock);
202174664626SKris Kennaway         goto end;
202274664626SKris Kennaway     }
2023b077aed3SPierre Pronchery     BIO_printf(bio_c_out, "CONNECTED(%08X)\n", sock);
202474664626SKris Kennaway 
20256f9291ceSJung-uk Kim     if (c_nbio) {
2026b077aed3SPierre Pronchery         if (!BIO_socket_nbio(sock, 1)) {
202774664626SKris Kennaway             ERR_print_errors(bio_err);
202874664626SKris Kennaway             goto end;
202974664626SKris Kennaway         }
2030e71b7053SJung-uk Kim         BIO_printf(bio_c_out, "Turned on non blocking io\n");
203174664626SKris Kennaway     }
2032e71b7053SJung-uk Kim #ifndef OPENSSL_NO_DTLS
2033e71b7053SJung-uk Kim     if (isdtls) {
2034e71b7053SJung-uk Kim         union BIO_sock_info_u peer_info;
2035e71b7053SJung-uk Kim 
2036e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
2037e71b7053SJung-uk Kim         if (protocol == IPPROTO_SCTP)
2038b077aed3SPierre Pronchery             sbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE);
2039e71b7053SJung-uk Kim         else
204074664626SKris Kennaway #endif
2041b077aed3SPierre Pronchery             sbio = BIO_new_dgram(sock, BIO_NOCLOSE);
2042e71b7053SJung-uk Kim 
2043b077aed3SPierre Pronchery         if (sbio == NULL || (peer_info.addr = BIO_ADDR_new()) == NULL) {
2044e71b7053SJung-uk Kim             BIO_printf(bio_err, "memory allocation failure\n");
2045b077aed3SPierre Pronchery             BIO_free(sbio);
2046b077aed3SPierre Pronchery             BIO_closesocket(sock);
2047e71b7053SJung-uk Kim             goto end;
2048e71b7053SJung-uk Kim         }
2049b077aed3SPierre Pronchery         if (!BIO_sock_info(sock, BIO_SOCK_INFO_ADDRESS, &peer_info)) {
20503b4e3dcbSSimon L. B. Nielsen             BIO_printf(bio_err, "getsockname:errno=%d\n",
20513b4e3dcbSSimon L. B. Nielsen                        get_last_socket_error());
2052b077aed3SPierre Pronchery             BIO_free(sbio);
2053e71b7053SJung-uk Kim             BIO_ADDR_free(peer_info.addr);
2054b077aed3SPierre Pronchery             BIO_closesocket(sock);
20553b4e3dcbSSimon L. B. Nielsen             goto end;
20563b4e3dcbSSimon L. B. Nielsen         }
20573b4e3dcbSSimon L. B. Nielsen 
2058e71b7053SJung-uk Kim         (void)BIO_ctrl_set_connected(sbio, peer_info.addr);
2059e71b7053SJung-uk Kim         BIO_ADDR_free(peer_info.addr);
2060e71b7053SJung-uk Kim         peer_info.addr = NULL;
20613b4e3dcbSSimon L. B. Nielsen 
20626f9291ceSJung-uk Kim         if (enable_timeouts) {
20633b4e3dcbSSimon L. B. Nielsen             timeout.tv_sec = 0;
20643b4e3dcbSSimon L. B. Nielsen             timeout.tv_usec = DGRAM_RCV_TIMEOUT;
20653b4e3dcbSSimon L. B. Nielsen             BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);
20663b4e3dcbSSimon L. B. Nielsen 
20673b4e3dcbSSimon L. B. Nielsen             timeout.tv_sec = 0;
20683b4e3dcbSSimon L. B. Nielsen             timeout.tv_usec = DGRAM_SND_TIMEOUT;
20693b4e3dcbSSimon L. B. Nielsen             BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
20703b4e3dcbSSimon L. B. Nielsen         }
20713b4e3dcbSSimon L. B. Nielsen 
20726f9291ceSJung-uk Kim         if (socket_mtu) {
20736f9291ceSJung-uk Kim             if (socket_mtu < DTLS_get_link_min_mtu(con)) {
2074751d2991SJung-uk Kim                 BIO_printf(bio_err, "MTU too small. Must be at least %ld\n",
2075751d2991SJung-uk Kim                            DTLS_get_link_min_mtu(con));
2076751d2991SJung-uk Kim                 BIO_free(sbio);
2077751d2991SJung-uk Kim                 goto shut;
2078751d2991SJung-uk Kim             }
20793b4e3dcbSSimon L. B. Nielsen             SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
20806f9291ceSJung-uk Kim             if (!DTLS_set_link_mtu(con, socket_mtu)) {
2081751d2991SJung-uk Kim                 BIO_printf(bio_err, "Failed to set MTU\n");
2082751d2991SJung-uk Kim                 BIO_free(sbio);
2083751d2991SJung-uk Kim                 goto shut;
2084751d2991SJung-uk Kim             }
2085e71b7053SJung-uk Kim         } else {
20863b4e3dcbSSimon L. B. Nielsen             /* want to do MTU discovery */
20873b4e3dcbSSimon L. B. Nielsen             BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);
2088e71b7053SJung-uk Kim         }
20896f9291ceSJung-uk Kim     } else
2090e71b7053SJung-uk Kim #endif /* OPENSSL_NO_DTLS */
2091b077aed3SPierre Pronchery         sbio = BIO_new_socket(sock, BIO_NOCLOSE);
2092b077aed3SPierre Pronchery 
2093b077aed3SPierre Pronchery     if (sbio == NULL) {
2094b077aed3SPierre Pronchery         BIO_printf(bio_err, "Unable to create BIO\n");
2095b077aed3SPierre Pronchery         ERR_print_errors(bio_err);
2096b077aed3SPierre Pronchery         BIO_closesocket(sock);
2097b077aed3SPierre Pronchery         goto end;
2098b077aed3SPierre Pronchery     }
209974664626SKris Kennaway 
21006f9291ceSJung-uk Kim     if (nbio_test) {
210174664626SKris Kennaway         BIO *test;
210274664626SKris Kennaway 
210374664626SKris Kennaway         test = BIO_new(BIO_f_nbio_test());
2104b077aed3SPierre Pronchery         if (test == NULL) {
2105b077aed3SPierre Pronchery             BIO_printf(bio_err, "Unable to create BIO\n");
2106b077aed3SPierre Pronchery             BIO_free(sbio);
2107b077aed3SPierre Pronchery             goto shut;
2108b077aed3SPierre Pronchery         }
210974664626SKris Kennaway         sbio = BIO_push(test, sbio);
211074664626SKris Kennaway     }
211174664626SKris Kennaway 
21126f9291ceSJung-uk Kim     if (c_debug) {
2113b077aed3SPierre Pronchery         BIO_set_callback_ex(sbio, bio_dump_callback);
21145471f83eSSimon L. B. Nielsen         BIO_set_callback_arg(sbio, (char *)bio_c_out);
211574664626SKris Kennaway     }
21166f9291ceSJung-uk Kim     if (c_msg) {
21177bded2dbSJung-uk Kim #ifndef OPENSSL_NO_SSL_TRACE
21187bded2dbSJung-uk Kim         if (c_msg == 2)
21197bded2dbSJung-uk Kim             SSL_set_msg_callback(con, SSL_trace);
21207bded2dbSJung-uk Kim         else
21217bded2dbSJung-uk Kim #endif
21225c87c606SMark Murray             SSL_set_msg_callback(con, msg_cb);
21237bded2dbSJung-uk Kim         SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);
21245c87c606SMark Murray     }
2125e71b7053SJung-uk Kim 
21266f9291ceSJung-uk Kim     if (c_tlsextdebug) {
2127db522d3aSSimon L. B. Nielsen         SSL_set_tlsext_debug_callback(con, tlsext_cb);
2128db522d3aSSimon L. B. Nielsen         SSL_set_tlsext_debug_arg(con, bio_c_out);
2129db522d3aSSimon L. B. Nielsen     }
2130e71b7053SJung-uk Kim #ifndef OPENSSL_NO_OCSP
21316f9291ceSJung-uk Kim     if (c_status_req) {
2132db522d3aSSimon L. B. Nielsen         SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);
2133db522d3aSSimon L. B. Nielsen         SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);
2134db522d3aSSimon L. B. Nielsen         SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);
2135db522d3aSSimon L. B. Nielsen     }
2136db522d3aSSimon L. B. Nielsen #endif
213774664626SKris Kennaway 
213874664626SKris Kennaway     SSL_set_bio(con, sbio, sbio);
213974664626SKris Kennaway     SSL_set_connect_state(con);
214074664626SKris Kennaway 
214174664626SKris Kennaway     /* ok, lets connect */
2142aeb5019cSJung-uk Kim     if (fileno_stdin() > SSL_get_fd(con))
2143aeb5019cSJung-uk Kim         width = fileno_stdin() + 1;
2144aeb5019cSJung-uk Kim     else
214574664626SKris Kennaway         width = SSL_get_fd(con) + 1;
214674664626SKris Kennaway 
214774664626SKris Kennaway     read_tty = 1;
214874664626SKris Kennaway     write_tty = 0;
214974664626SKris Kennaway     tty_on = 0;
215074664626SKris Kennaway     read_ssl = 1;
215174664626SKris Kennaway     write_ssl = 1;
215274664626SKris Kennaway 
215374664626SKris Kennaway     cbuf_len = 0;
215474664626SKris Kennaway     cbuf_off = 0;
215574664626SKris Kennaway     sbuf_len = 0;
215674664626SKris Kennaway     sbuf_off = 0;
215774664626SKris Kennaway 
2158b077aed3SPierre Pronchery     if (proxystr != NULL) {
2159b077aed3SPierre Pronchery         /* Here we must use the connect string target host & port */
2160b077aed3SPierre Pronchery         if (!OSSL_HTTP_proxy_connect(sbio, thost, tport, proxyuser, proxypass,
2161b077aed3SPierre Pronchery                                      0 /* no timeout */, bio_err, prog))
2162b077aed3SPierre Pronchery             goto shut;
2163b077aed3SPierre Pronchery     }
2164b077aed3SPierre Pronchery 
2165e71b7053SJung-uk Kim     switch ((PROTOCOL_CHOICE) starttls_proto) {
2166e71b7053SJung-uk Kim     case PROTO_OFF:
2167e71b7053SJung-uk Kim         break;
2168e71b7053SJung-uk Kim     case PROTO_LMTP:
2169e71b7053SJung-uk Kim     case PROTO_SMTP:
2170e71b7053SJung-uk Kim         {
21716f9291ceSJung-uk Kim             /*
2172e71b7053SJung-uk Kim              * This is an ugly hack that does a lot of assumptions. We do
2173e71b7053SJung-uk Kim              * have to handle multi-line responses which may come in a single
2174e71b7053SJung-uk Kim              * packet or not. We therefore have to use BIO_gets() which does
2175e71b7053SJung-uk Kim              * need a buffering BIO. So during the initial chitchat we do
2176e71b7053SJung-uk Kim              * push a buffering BIO into the chain that is removed again
2177e71b7053SJung-uk Kim              * later on to not disturb the rest of the s_client operation.
21786f9291ceSJung-uk Kim              */
21795471f83eSSimon L. B. Nielsen             int foundit = 0;
21805471f83eSSimon L. B. Nielsen             BIO *fbio = BIO_new(BIO_f_buffer());
2181e71b7053SJung-uk Kim 
2182b077aed3SPierre Pronchery             if (fbio == NULL) {
2183b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unable to create BIO\n");
2184b077aed3SPierre Pronchery                 goto shut;
2185b077aed3SPierre Pronchery             }
21865471f83eSSimon L. B. Nielsen             BIO_push(fbio, sbio);
2187e71b7053SJung-uk Kim             /* Wait for multi-line response to end from LMTP or SMTP */
21886f9291ceSJung-uk Kim             do {
21895471f83eSSimon L. B. Nielsen                 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2190e71b7053SJung-uk Kim             } while (mbuf_len > 3 && mbuf[3] == '-');
2191e71b7053SJung-uk Kim             if (protohost == NULL)
2192e71b7053SJung-uk Kim                 protohost = "mail.example.com";
2193e71b7053SJung-uk Kim             if (starttls_proto == (int)PROTO_LMTP)
2194e71b7053SJung-uk Kim                 BIO_printf(fbio, "LHLO %s\r\n", protohost);
2195e71b7053SJung-uk Kim             else
2196e71b7053SJung-uk Kim                 BIO_printf(fbio, "EHLO %s\r\n", protohost);
2197db522d3aSSimon L. B. Nielsen             (void)BIO_flush(fbio);
2198e71b7053SJung-uk Kim             /*
2199e71b7053SJung-uk Kim              * Wait for multi-line response to end LHLO LMTP or EHLO SMTP
2200e71b7053SJung-uk Kim              * response.
2201e71b7053SJung-uk Kim              */
22026f9291ceSJung-uk Kim             do {
22035471f83eSSimon L. B. Nielsen                 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
22045471f83eSSimon L. B. Nielsen                 if (strstr(mbuf, "STARTTLS"))
22055471f83eSSimon L. B. Nielsen                     foundit = 1;
2206e71b7053SJung-uk Kim             } while (mbuf_len > 3 && mbuf[3] == '-');
2207db522d3aSSimon L. B. Nielsen             (void)BIO_flush(fbio);
22085471f83eSSimon L. B. Nielsen             BIO_pop(fbio);
22095471f83eSSimon L. B. Nielsen             BIO_free(fbio);
22105471f83eSSimon L. B. Nielsen             if (!foundit)
22115471f83eSSimon L. B. Nielsen                 BIO_printf(bio_err,
2212e71b7053SJung-uk Kim                            "Didn't find STARTTLS in server response,"
2213e71b7053SJung-uk Kim                            " trying anyway...\n");
22145c87c606SMark Murray             BIO_printf(sbio, "STARTTLS\r\n");
22155c87c606SMark Murray             BIO_read(sbio, sbuf, BUFSIZZ);
2216e71b7053SJung-uk Kim         }
2217e71b7053SJung-uk Kim         break;
2218e71b7053SJung-uk Kim     case PROTO_POP3:
2219e71b7053SJung-uk Kim         {
222050ef0093SJacques Vidrine             BIO_read(sbio, mbuf, BUFSIZZ);
222150ef0093SJacques Vidrine             BIO_printf(sbio, "STLS\r\n");
2222e71b7053SJung-uk Kim             mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);
2223e71b7053SJung-uk Kim             if (mbuf_len < 0) {
2224e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2225e71b7053SJung-uk Kim                 goto end;
2226e71b7053SJung-uk Kim             }
2227e71b7053SJung-uk Kim         }
2228e71b7053SJung-uk Kim         break;
2229e71b7053SJung-uk Kim     case PROTO_IMAP:
2230e71b7053SJung-uk Kim         {
22315471f83eSSimon L. B. Nielsen             int foundit = 0;
22325471f83eSSimon L. B. Nielsen             BIO *fbio = BIO_new(BIO_f_buffer());
2233e71b7053SJung-uk Kim 
2234b077aed3SPierre Pronchery             if (fbio == NULL) {
2235b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unable to create BIO\n");
2236b077aed3SPierre Pronchery                 goto shut;
2237b077aed3SPierre Pronchery             }
22385471f83eSSimon L. B. Nielsen             BIO_push(fbio, sbio);
22395471f83eSSimon L. B. Nielsen             BIO_gets(fbio, mbuf, BUFSIZZ);
22405471f83eSSimon L. B. Nielsen             /* STARTTLS command requires CAPABILITY... */
22415471f83eSSimon L. B. Nielsen             BIO_printf(fbio, ". CAPABILITY\r\n");
2242db522d3aSSimon L. B. Nielsen             (void)BIO_flush(fbio);
22435471f83eSSimon L. B. Nielsen             /* wait for multi-line CAPABILITY response */
22446f9291ceSJung-uk Kim             do {
22455471f83eSSimon L. B. Nielsen                 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
22465471f83eSSimon L. B. Nielsen                 if (strstr(mbuf, "STARTTLS"))
22475471f83eSSimon L. B. Nielsen                     foundit = 1;
22485471f83eSSimon L. B. Nielsen             }
22495471f83eSSimon L. B. Nielsen             while (mbuf_len > 3 && mbuf[0] != '.');
2250db522d3aSSimon L. B. Nielsen             (void)BIO_flush(fbio);
22515471f83eSSimon L. B. Nielsen             BIO_pop(fbio);
22525471f83eSSimon L. B. Nielsen             BIO_free(fbio);
22535471f83eSSimon L. B. Nielsen             if (!foundit)
22545471f83eSSimon L. B. Nielsen                 BIO_printf(bio_err,
2255e71b7053SJung-uk Kim                            "Didn't find STARTTLS in server response,"
2256e71b7053SJung-uk Kim                            " trying anyway...\n");
22575471f83eSSimon L. B. Nielsen             BIO_printf(sbio, ". STARTTLS\r\n");
22585471f83eSSimon L. B. Nielsen             BIO_read(sbio, sbuf, BUFSIZZ);
2259e71b7053SJung-uk Kim         }
2260e71b7053SJung-uk Kim         break;
2261e71b7053SJung-uk Kim     case PROTO_FTP:
2262e71b7053SJung-uk Kim         {
22635471f83eSSimon L. B. Nielsen             BIO *fbio = BIO_new(BIO_f_buffer());
2264e71b7053SJung-uk Kim 
2265b077aed3SPierre Pronchery             if (fbio == NULL) {
2266b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unable to create BIO\n");
2267b077aed3SPierre Pronchery                 goto shut;
2268b077aed3SPierre Pronchery             }
22695471f83eSSimon L. B. Nielsen             BIO_push(fbio, sbio);
22705471f83eSSimon L. B. Nielsen             /* wait for multi-line response to end from FTP */
22716f9291ceSJung-uk Kim             do {
22725471f83eSSimon L. B. Nielsen                 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
22735471f83eSSimon L. B. Nielsen             }
2274*aa795734SPierre Pronchery             while (mbuf_len > 3 && (!isdigit((unsigned char)mbuf[0]) || !isdigit((unsigned char)mbuf[1]) || !isdigit((unsigned char)mbuf[2]) || mbuf[3] != ' '));
2275db522d3aSSimon L. B. Nielsen             (void)BIO_flush(fbio);
22765471f83eSSimon L. B. Nielsen             BIO_pop(fbio);
22775471f83eSSimon L. B. Nielsen             BIO_free(fbio);
22785471f83eSSimon L. B. Nielsen             BIO_printf(sbio, "AUTH TLS\r\n");
22795471f83eSSimon L. B. Nielsen             BIO_read(sbio, sbuf, BUFSIZZ);
22805471f83eSSimon L. B. Nielsen         }
2281e71b7053SJung-uk Kim         break;
2282e71b7053SJung-uk Kim     case PROTO_XMPP:
2283e71b7053SJung-uk Kim     case PROTO_XMPP_SERVER:
2284e71b7053SJung-uk Kim         {
2285db522d3aSSimon L. B. Nielsen             int seen = 0;
2286db522d3aSSimon L. B. Nielsen             BIO_printf(sbio, "<stream:stream "
2287db522d3aSSimon L. B. Nielsen                        "xmlns:stream='http://etherx.jabber.org/streams' "
2288e71b7053SJung-uk Kim                        "xmlns='jabber:%s' to='%s' version='1.0'>",
2289e71b7053SJung-uk Kim                        starttls_proto == PROTO_XMPP ? "client" : "server",
2290e71b7053SJung-uk Kim                        protohost ? protohost : host);
2291db522d3aSSimon L. B. Nielsen             seen = BIO_read(sbio, mbuf, BUFSIZZ);
2292e71b7053SJung-uk Kim             if (seen < 0) {
2293e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2294e71b7053SJung-uk Kim                 goto end;
2295e71b7053SJung-uk Kim             }
2296e71b7053SJung-uk Kim             mbuf[seen] = '\0';
22976f9291ceSJung-uk Kim             while (!strstr
2298e71b7053SJung-uk Kim                    (mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'")
2299e71b7053SJung-uk Kim                    && !strstr(mbuf,
2300e71b7053SJung-uk Kim                               "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\""))
2301e71b7053SJung-uk Kim             {
2302db522d3aSSimon L. B. Nielsen                 seen = BIO_read(sbio, mbuf, BUFSIZZ);
2303e71b7053SJung-uk Kim 
230447902a71SJung-uk Kim                 if (seen <= 0)
230547902a71SJung-uk Kim                     goto shut;
2306e71b7053SJung-uk Kim 
2307e71b7053SJung-uk Kim                 mbuf[seen] = '\0';
2308db522d3aSSimon L. B. Nielsen             }
23096f9291ceSJung-uk Kim             BIO_printf(sbio,
23106f9291ceSJung-uk Kim                        "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
2311db522d3aSSimon L. B. Nielsen             seen = BIO_read(sbio, sbuf, BUFSIZZ);
2312e71b7053SJung-uk Kim             if (seen < 0) {
2313e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2314e71b7053SJung-uk Kim                 goto shut;
2315e71b7053SJung-uk Kim             }
2316e71b7053SJung-uk Kim             sbuf[seen] = '\0';
2317db522d3aSSimon L. B. Nielsen             if (!strstr(sbuf, "<proceed"))
2318db522d3aSSimon L. B. Nielsen                 goto shut;
2319e71b7053SJung-uk Kim             mbuf[0] = '\0';
2320e71b7053SJung-uk Kim         }
2321e71b7053SJung-uk Kim         break;
2322e71b7053SJung-uk Kim     case PROTO_TELNET:
2323e71b7053SJung-uk Kim         {
2324e71b7053SJung-uk Kim             static const unsigned char tls_do[] = {
2325e71b7053SJung-uk Kim                 /* IAC    DO   START_TLS */
2326e71b7053SJung-uk Kim                    255,   253, 46
2327e71b7053SJung-uk Kim             };
2328e71b7053SJung-uk Kim             static const unsigned char tls_will[] = {
2329e71b7053SJung-uk Kim                 /* IAC  WILL START_TLS */
2330e71b7053SJung-uk Kim                    255, 251, 46
2331e71b7053SJung-uk Kim             };
2332e71b7053SJung-uk Kim             static const unsigned char tls_follows[] = {
2333e71b7053SJung-uk Kim                 /* IAC  SB   START_TLS FOLLOWS IAC  SE */
2334e71b7053SJung-uk Kim                    255, 250, 46,       1,      255, 240
2335e71b7053SJung-uk Kim             };
2336e71b7053SJung-uk Kim             int bytes;
2337e71b7053SJung-uk Kim 
2338e71b7053SJung-uk Kim             /* Telnet server should demand we issue START_TLS */
2339e71b7053SJung-uk Kim             bytes = BIO_read(sbio, mbuf, BUFSIZZ);
2340e71b7053SJung-uk Kim             if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)
2341e71b7053SJung-uk Kim                 goto shut;
2342e71b7053SJung-uk Kim             /* Agree to issue START_TLS and send the FOLLOWS sub-command */
2343e71b7053SJung-uk Kim             BIO_write(sbio, tls_will, 3);
2344e71b7053SJung-uk Kim             BIO_write(sbio, tls_follows, 6);
2345e71b7053SJung-uk Kim             (void)BIO_flush(sbio);
2346e71b7053SJung-uk Kim             /* Telnet server also sent the FOLLOWS sub-command */
2347e71b7053SJung-uk Kim             bytes = BIO_read(sbio, mbuf, BUFSIZZ);
2348e71b7053SJung-uk Kim             if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)
2349e71b7053SJung-uk Kim                 goto shut;
2350e71b7053SJung-uk Kim         }
2351e71b7053SJung-uk Kim         break;
2352e71b7053SJung-uk Kim     case PROTO_IRC:
2353e71b7053SJung-uk Kim         {
2354e71b7053SJung-uk Kim             int numeric;
2355e71b7053SJung-uk Kim             BIO *fbio = BIO_new(BIO_f_buffer());
2356e71b7053SJung-uk Kim 
2357b077aed3SPierre Pronchery             if (fbio == NULL) {
2358b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unable to create BIO\n");
2359b077aed3SPierre Pronchery                 goto end;
2360b077aed3SPierre Pronchery             }
2361e71b7053SJung-uk Kim             BIO_push(fbio, sbio);
2362e71b7053SJung-uk Kim             BIO_printf(fbio, "STARTTLS\r\n");
2363e71b7053SJung-uk Kim             (void)BIO_flush(fbio);
2364e71b7053SJung-uk Kim             width = SSL_get_fd(con) + 1;
2365e71b7053SJung-uk Kim 
2366e71b7053SJung-uk Kim             do {
2367e71b7053SJung-uk Kim                 numeric = 0;
2368e71b7053SJung-uk Kim 
2369e71b7053SJung-uk Kim                 FD_ZERO(&readfds);
2370e71b7053SJung-uk Kim                 openssl_fdset(SSL_get_fd(con), &readfds);
2371e71b7053SJung-uk Kim                 timeout.tv_sec = S_CLIENT_IRC_READ_TIMEOUT;
2372e71b7053SJung-uk Kim                 timeout.tv_usec = 0;
2373e71b7053SJung-uk Kim                 /*
2374e71b7053SJung-uk Kim                  * If the IRCd doesn't respond within
2375e71b7053SJung-uk Kim                  * S_CLIENT_IRC_READ_TIMEOUT seconds, assume
2376e71b7053SJung-uk Kim                  * it doesn't support STARTTLS. Many IRCds
2377e71b7053SJung-uk Kim                  * will not give _any_ sort of response to a
2378e71b7053SJung-uk Kim                  * STARTTLS command when it's not supported.
2379e71b7053SJung-uk Kim                  */
2380e71b7053SJung-uk Kim                 if (!BIO_get_buffer_num_lines(fbio)
2381e71b7053SJung-uk Kim                     && !BIO_pending(fbio)
2382e71b7053SJung-uk Kim                     && !BIO_pending(sbio)
2383e71b7053SJung-uk Kim                     && select(width, (void *)&readfds, NULL, NULL,
2384e71b7053SJung-uk Kim                               &timeout) < 1) {
2385e71b7053SJung-uk Kim                     BIO_printf(bio_err,
2386e71b7053SJung-uk Kim                                "Timeout waiting for response (%d seconds).\n",
2387e71b7053SJung-uk Kim                                S_CLIENT_IRC_READ_TIMEOUT);
2388e71b7053SJung-uk Kim                     break;
2389e71b7053SJung-uk Kim                 }
2390e71b7053SJung-uk Kim 
2391e71b7053SJung-uk Kim                 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2392e71b7053SJung-uk Kim                 if (mbuf_len < 1 || sscanf(mbuf, "%*s %d", &numeric) != 1)
2393e71b7053SJung-uk Kim                     break;
2394e71b7053SJung-uk Kim                 /* :example.net 451 STARTTLS :You have not registered */
2395e71b7053SJung-uk Kim                 /* :example.net 421 STARTTLS :Unknown command */
2396e71b7053SJung-uk Kim                 if ((numeric == 451 || numeric == 421)
2397e71b7053SJung-uk Kim                     && strstr(mbuf, "STARTTLS") != NULL) {
2398e71b7053SJung-uk Kim                     BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
2399e71b7053SJung-uk Kim                     break;
2400e71b7053SJung-uk Kim                 }
2401e71b7053SJung-uk Kim                 if (numeric == 691) {
2402e71b7053SJung-uk Kim                     BIO_printf(bio_err, "STARTTLS negotiation failed: ");
2403e71b7053SJung-uk Kim                     ERR_print_errors(bio_err);
2404e71b7053SJung-uk Kim                     break;
2405e71b7053SJung-uk Kim                 }
2406e71b7053SJung-uk Kim             } while (numeric != 670);
2407e71b7053SJung-uk Kim 
2408e71b7053SJung-uk Kim             (void)BIO_flush(fbio);
2409e71b7053SJung-uk Kim             BIO_pop(fbio);
2410e71b7053SJung-uk Kim             BIO_free(fbio);
2411e71b7053SJung-uk Kim             if (numeric != 670) {
2412e71b7053SJung-uk Kim                 BIO_printf(bio_err, "Server does not support STARTTLS.\n");
2413e71b7053SJung-uk Kim                 ret = 1;
2414e71b7053SJung-uk Kim                 goto shut;
2415e71b7053SJung-uk Kim             }
2416e71b7053SJung-uk Kim         }
2417e71b7053SJung-uk Kim         break;
2418e71b7053SJung-uk Kim     case PROTO_MYSQL:
2419e71b7053SJung-uk Kim         {
2420e71b7053SJung-uk Kim             /* SSL request packet */
2421e71b7053SJung-uk Kim             static const unsigned char ssl_req[] = {
2422e71b7053SJung-uk Kim                 /* payload_length,   sequence_id */
2423e71b7053SJung-uk Kim                    0x20, 0x00, 0x00, 0x01,
2424e71b7053SJung-uk Kim                 /* payload */
2425e71b7053SJung-uk Kim                 /* capability flags, CLIENT_SSL always set */
2426e71b7053SJung-uk Kim                    0x85, 0xae, 0x7f, 0x00,
2427e71b7053SJung-uk Kim                 /* max-packet size */
2428e71b7053SJung-uk Kim                    0x00, 0x00, 0x00, 0x01,
2429e71b7053SJung-uk Kim                 /* character set */
2430e71b7053SJung-uk Kim                    0x21,
2431e71b7053SJung-uk Kim                 /* string[23] reserved (all [0]) */
2432e71b7053SJung-uk Kim                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2433e71b7053SJung-uk Kim                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2434e71b7053SJung-uk Kim                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2435e71b7053SJung-uk Kim             };
2436e71b7053SJung-uk Kim             int bytes = 0;
2437e71b7053SJung-uk Kim             int ssl_flg = 0x800;
2438e71b7053SJung-uk Kim             int pos;
2439e71b7053SJung-uk Kim             const unsigned char *packet = (const unsigned char *)sbuf;
2440e71b7053SJung-uk Kim 
2441e71b7053SJung-uk Kim             /* Receiving Initial Handshake packet. */
2442e71b7053SJung-uk Kim             bytes = BIO_read(sbio, (void *)packet, BUFSIZZ);
2443e71b7053SJung-uk Kim             if (bytes < 0) {
2444e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2445e71b7053SJung-uk Kim                 goto shut;
2446e71b7053SJung-uk Kim             /* Packet length[3], Packet number[1] + minimum payload[17] */
2447e71b7053SJung-uk Kim             } else if (bytes < 21) {
2448e71b7053SJung-uk Kim                 BIO_printf(bio_err, "MySQL packet too short.\n");
2449e71b7053SJung-uk Kim                 goto shut;
2450e71b7053SJung-uk Kim             } else if (bytes != (4 + packet[0] +
2451e71b7053SJung-uk Kim                                  (packet[1] << 8) +
2452e71b7053SJung-uk Kim                                  (packet[2] << 16))) {
2453e71b7053SJung-uk Kim                 BIO_printf(bio_err, "MySQL packet length does not match.\n");
2454e71b7053SJung-uk Kim                 goto shut;
2455e71b7053SJung-uk Kim             /* protocol version[1] */
2456e71b7053SJung-uk Kim             } else if (packet[4] != 0xA) {
2457e71b7053SJung-uk Kim                 BIO_printf(bio_err,
2458e71b7053SJung-uk Kim                            "Only MySQL protocol version 10 is supported.\n");
2459e71b7053SJung-uk Kim                 goto shut;
2460e71b7053SJung-uk Kim             }
2461e71b7053SJung-uk Kim 
2462e71b7053SJung-uk Kim             pos = 5;
2463e71b7053SJung-uk Kim             /* server version[string+NULL] */
2464e71b7053SJung-uk Kim             for (;;) {
2465e71b7053SJung-uk Kim                 if (pos >= bytes) {
2466e71b7053SJung-uk Kim                     BIO_printf(bio_err, "Cannot confirm server version. ");
2467e71b7053SJung-uk Kim                     goto shut;
2468e71b7053SJung-uk Kim                 } else if (packet[pos++] == '\0') {
2469e71b7053SJung-uk Kim                     break;
2470e71b7053SJung-uk Kim                 }
2471e71b7053SJung-uk Kim             }
2472e71b7053SJung-uk Kim 
2473e71b7053SJung-uk Kim             /* make sure we have at least 15 bytes left in the packet */
2474e71b7053SJung-uk Kim             if (pos + 15 > bytes) {
2475e71b7053SJung-uk Kim                 BIO_printf(bio_err,
2476e71b7053SJung-uk Kim                            "MySQL server handshake packet is broken.\n");
2477e71b7053SJung-uk Kim                 goto shut;
2478e71b7053SJung-uk Kim             }
2479e71b7053SJung-uk Kim 
2480e71b7053SJung-uk Kim             pos += 12; /* skip over conn id[4] + SALT[8] */
2481e71b7053SJung-uk Kim             if (packet[pos++] != '\0') { /* verify filler */
2482e71b7053SJung-uk Kim                 BIO_printf(bio_err,
2483e71b7053SJung-uk Kim                            "MySQL packet is broken.\n");
2484e71b7053SJung-uk Kim                 goto shut;
2485e71b7053SJung-uk Kim             }
2486e71b7053SJung-uk Kim 
2487e71b7053SJung-uk Kim             /* capability flags[2] */
2488e71b7053SJung-uk Kim             if (!((packet[pos] + (packet[pos + 1] << 8)) & ssl_flg)) {
2489e71b7053SJung-uk Kim                 BIO_printf(bio_err, "MySQL server does not support SSL.\n");
2490e71b7053SJung-uk Kim                 goto shut;
2491e71b7053SJung-uk Kim             }
2492e71b7053SJung-uk Kim 
2493e71b7053SJung-uk Kim             /* Sending SSL Handshake packet. */
2494e71b7053SJung-uk Kim             BIO_write(sbio, ssl_req, sizeof(ssl_req));
2495e71b7053SJung-uk Kim             (void)BIO_flush(sbio);
2496e71b7053SJung-uk Kim         }
2497e71b7053SJung-uk Kim         break;
2498e71b7053SJung-uk Kim     case PROTO_POSTGRES:
2499e71b7053SJung-uk Kim         {
2500e71b7053SJung-uk Kim             static const unsigned char ssl_request[] = {
2501e71b7053SJung-uk Kim                 /* Length        SSLRequest */
2502e71b7053SJung-uk Kim                    0, 0, 0, 8,   4, 210, 22, 47
2503e71b7053SJung-uk Kim             };
2504e71b7053SJung-uk Kim             int bytes;
2505e71b7053SJung-uk Kim 
2506e71b7053SJung-uk Kim             /* Send SSLRequest packet */
2507e71b7053SJung-uk Kim             BIO_write(sbio, ssl_request, 8);
2508e71b7053SJung-uk Kim             (void)BIO_flush(sbio);
2509e71b7053SJung-uk Kim 
2510e71b7053SJung-uk Kim             /* Reply will be a single S if SSL is enabled */
2511e71b7053SJung-uk Kim             bytes = BIO_read(sbio, sbuf, BUFSIZZ);
2512e71b7053SJung-uk Kim             if (bytes != 1 || sbuf[0] != 'S')
2513e71b7053SJung-uk Kim                 goto shut;
2514e71b7053SJung-uk Kim         }
2515e71b7053SJung-uk Kim         break;
2516e71b7053SJung-uk Kim     case PROTO_NNTP:
2517e71b7053SJung-uk Kim         {
2518e71b7053SJung-uk Kim             int foundit = 0;
2519e71b7053SJung-uk Kim             BIO *fbio = BIO_new(BIO_f_buffer());
2520e71b7053SJung-uk Kim 
2521b077aed3SPierre Pronchery             if (fbio == NULL) {
2522b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unable to create BIO\n");
2523b077aed3SPierre Pronchery                 goto end;
2524b077aed3SPierre Pronchery             }
2525e71b7053SJung-uk Kim             BIO_push(fbio, sbio);
2526e71b7053SJung-uk Kim             BIO_gets(fbio, mbuf, BUFSIZZ);
2527e71b7053SJung-uk Kim             /* STARTTLS command requires CAPABILITIES... */
2528e71b7053SJung-uk Kim             BIO_printf(fbio, "CAPABILITIES\r\n");
2529e71b7053SJung-uk Kim             (void)BIO_flush(fbio);
2530b077aed3SPierre Pronchery             BIO_gets(fbio, mbuf, BUFSIZZ);
2531b077aed3SPierre Pronchery             /* no point in trying to parse the CAPABILITIES response if there is none */
2532b077aed3SPierre Pronchery             if (strstr(mbuf, "101") != NULL) {
2533e71b7053SJung-uk Kim                 /* wait for multi-line CAPABILITIES response */
2534e71b7053SJung-uk Kim                 do {
2535e71b7053SJung-uk Kim                     mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2536e71b7053SJung-uk Kim                     if (strstr(mbuf, "STARTTLS"))
2537e71b7053SJung-uk Kim                         foundit = 1;
2538e71b7053SJung-uk Kim                 } while (mbuf_len > 1 && mbuf[0] != '.');
2539b077aed3SPierre Pronchery             }
2540e71b7053SJung-uk Kim             (void)BIO_flush(fbio);
2541e71b7053SJung-uk Kim             BIO_pop(fbio);
2542e71b7053SJung-uk Kim             BIO_free(fbio);
2543e71b7053SJung-uk Kim             if (!foundit)
2544e71b7053SJung-uk Kim                 BIO_printf(bio_err,
2545e71b7053SJung-uk Kim                            "Didn't find STARTTLS in server response,"
2546e71b7053SJung-uk Kim                            " trying anyway...\n");
2547e71b7053SJung-uk Kim             BIO_printf(sbio, "STARTTLS\r\n");
2548e71b7053SJung-uk Kim             mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
2549e71b7053SJung-uk Kim             if (mbuf_len < 0) {
2550e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2551e71b7053SJung-uk Kim                 goto end;
2552e71b7053SJung-uk Kim             }
2553e71b7053SJung-uk Kim             mbuf[mbuf_len] = '\0';
2554e71b7053SJung-uk Kim             if (strstr(mbuf, "382") == NULL) {
2555e71b7053SJung-uk Kim                 BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
2556e71b7053SJung-uk Kim                 goto shut;
2557e71b7053SJung-uk Kim             }
2558e71b7053SJung-uk Kim         }
2559e71b7053SJung-uk Kim         break;
2560e71b7053SJung-uk Kim     case PROTO_SIEVE:
2561e71b7053SJung-uk Kim         {
2562e71b7053SJung-uk Kim             int foundit = 0;
2563e71b7053SJung-uk Kim             BIO *fbio = BIO_new(BIO_f_buffer());
2564e71b7053SJung-uk Kim 
2565b077aed3SPierre Pronchery             if (fbio == NULL) {
2566b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unable to create BIO\n");
2567b077aed3SPierre Pronchery                 goto end;
2568b077aed3SPierre Pronchery             }
2569e71b7053SJung-uk Kim             BIO_push(fbio, sbio);
2570e71b7053SJung-uk Kim             /* wait for multi-line response to end from Sieve */
2571e71b7053SJung-uk Kim             do {
2572e71b7053SJung-uk Kim                 mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
2573e71b7053SJung-uk Kim                 /*
2574e71b7053SJung-uk Kim                  * According to RFC 5804 § 1.7, capability
2575e71b7053SJung-uk Kim                  * is case-insensitive, make it uppercase
2576e71b7053SJung-uk Kim                  */
2577e71b7053SJung-uk Kim                 if (mbuf_len > 1 && mbuf[0] == '"') {
2578e71b7053SJung-uk Kim                     make_uppercase(mbuf);
2579e71b7053SJung-uk Kim                     if (strncmp(mbuf, "\"STARTTLS\"", 10) == 0)
2580e71b7053SJung-uk Kim                         foundit = 1;
2581e71b7053SJung-uk Kim                 }
2582e71b7053SJung-uk Kim             } while (mbuf_len > 1 && mbuf[0] == '"');
2583e71b7053SJung-uk Kim             (void)BIO_flush(fbio);
2584e71b7053SJung-uk Kim             BIO_pop(fbio);
2585e71b7053SJung-uk Kim             BIO_free(fbio);
2586e71b7053SJung-uk Kim             if (!foundit)
2587e71b7053SJung-uk Kim                 BIO_printf(bio_err,
2588e71b7053SJung-uk Kim                            "Didn't find STARTTLS in server response,"
2589e71b7053SJung-uk Kim                            " trying anyway...\n");
2590e71b7053SJung-uk Kim             BIO_printf(sbio, "STARTTLS\r\n");
2591e71b7053SJung-uk Kim             mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
2592e71b7053SJung-uk Kim             if (mbuf_len < 0) {
2593e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2594e71b7053SJung-uk Kim                 goto end;
2595e71b7053SJung-uk Kim             }
2596e71b7053SJung-uk Kim             mbuf[mbuf_len] = '\0';
2597e71b7053SJung-uk Kim             if (mbuf_len < 2) {
2598e71b7053SJung-uk Kim                 BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
2599e71b7053SJung-uk Kim                 goto shut;
2600e71b7053SJung-uk Kim             }
2601e71b7053SJung-uk Kim             /*
2602e71b7053SJung-uk Kim              * According to RFC 5804 § 2.2, response codes are case-
2603e71b7053SJung-uk Kim              * insensitive, make it uppercase but preserve the response.
2604e71b7053SJung-uk Kim              */
2605e71b7053SJung-uk Kim             strncpy(sbuf, mbuf, 2);
2606e71b7053SJung-uk Kim             make_uppercase(sbuf);
2607e71b7053SJung-uk Kim             if (strncmp(sbuf, "OK", 2) != 0) {
2608e71b7053SJung-uk Kim                 BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
2609e71b7053SJung-uk Kim                 goto shut;
2610e71b7053SJung-uk Kim             }
2611e71b7053SJung-uk Kim         }
2612e71b7053SJung-uk Kim         break;
2613e71b7053SJung-uk Kim     case PROTO_LDAP:
2614e71b7053SJung-uk Kim         {
2615e71b7053SJung-uk Kim             /* StartTLS Operation according to RFC 4511 */
2616e71b7053SJung-uk Kim             static char ldap_tls_genconf[] = "asn1=SEQUENCE:LDAPMessage\n"
2617e71b7053SJung-uk Kim                 "[LDAPMessage]\n"
2618e71b7053SJung-uk Kim                 "messageID=INTEGER:1\n"
2619e71b7053SJung-uk Kim                 "extendedReq=EXPLICIT:23A,IMPLICIT:0C,"
2620e71b7053SJung-uk Kim                 "FORMAT:ASCII,OCT:1.3.6.1.4.1.1466.20037\n";
2621e71b7053SJung-uk Kim             long errline = -1;
2622e71b7053SJung-uk Kim             char *genstr = NULL;
2623e71b7053SJung-uk Kim             int result = -1;
2624e71b7053SJung-uk Kim             ASN1_TYPE *atyp = NULL;
2625e71b7053SJung-uk Kim             BIO *ldapbio = BIO_new(BIO_s_mem());
2626e71b7053SJung-uk Kim             CONF *cnf = NCONF_new(NULL);
2627e71b7053SJung-uk Kim 
2628b077aed3SPierre Pronchery             if (ldapbio == NULL || cnf == NULL) {
2629e71b7053SJung-uk Kim                 BIO_free(ldapbio);
2630b077aed3SPierre Pronchery                 NCONF_free(cnf);
2631e71b7053SJung-uk Kim                 goto end;
2632e71b7053SJung-uk Kim             }
2633e71b7053SJung-uk Kim             BIO_puts(ldapbio, ldap_tls_genconf);
2634e71b7053SJung-uk Kim             if (NCONF_load_bio(cnf, ldapbio, &errline) <= 0) {
2635e71b7053SJung-uk Kim                 BIO_free(ldapbio);
2636e71b7053SJung-uk Kim                 NCONF_free(cnf);
2637e71b7053SJung-uk Kim                 if (errline <= 0) {
2638e71b7053SJung-uk Kim                     BIO_printf(bio_err, "NCONF_load_bio failed\n");
2639e71b7053SJung-uk Kim                     goto end;
2640e71b7053SJung-uk Kim                 } else {
2641e71b7053SJung-uk Kim                     BIO_printf(bio_err, "Error on line %ld\n", errline);
2642e71b7053SJung-uk Kim                     goto end;
2643e71b7053SJung-uk Kim                 }
2644e71b7053SJung-uk Kim             }
2645e71b7053SJung-uk Kim             BIO_free(ldapbio);
2646e71b7053SJung-uk Kim             genstr = NCONF_get_string(cnf, "default", "asn1");
2647e71b7053SJung-uk Kim             if (genstr == NULL) {
2648e71b7053SJung-uk Kim                 NCONF_free(cnf);
2649e71b7053SJung-uk Kim                 BIO_printf(bio_err, "NCONF_get_string failed\n");
2650e71b7053SJung-uk Kim                 goto end;
2651e71b7053SJung-uk Kim             }
2652e71b7053SJung-uk Kim             atyp = ASN1_generate_nconf(genstr, cnf);
2653e71b7053SJung-uk Kim             if (atyp == NULL) {
2654e71b7053SJung-uk Kim                 NCONF_free(cnf);
2655e71b7053SJung-uk Kim                 BIO_printf(bio_err, "ASN1_generate_nconf failed\n");
2656e71b7053SJung-uk Kim                 goto end;
2657e71b7053SJung-uk Kim             }
2658e71b7053SJung-uk Kim             NCONF_free(cnf);
2659e71b7053SJung-uk Kim 
2660e71b7053SJung-uk Kim             /* Send SSLRequest packet */
2661e71b7053SJung-uk Kim             BIO_write(sbio, atyp->value.sequence->data,
2662e71b7053SJung-uk Kim                       atyp->value.sequence->length);
2663e71b7053SJung-uk Kim             (void)BIO_flush(sbio);
2664e71b7053SJung-uk Kim             ASN1_TYPE_free(atyp);
2665e71b7053SJung-uk Kim 
2666e71b7053SJung-uk Kim             mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
2667e71b7053SJung-uk Kim             if (mbuf_len < 0) {
2668e71b7053SJung-uk Kim                 BIO_printf(bio_err, "BIO_read failed\n");
2669e71b7053SJung-uk Kim                 goto end;
2670e71b7053SJung-uk Kim             }
2671e71b7053SJung-uk Kim             result = ldap_ExtendedResponse_parse(mbuf, mbuf_len);
2672e71b7053SJung-uk Kim             if (result < 0) {
2673e71b7053SJung-uk Kim                 BIO_printf(bio_err, "ldap_ExtendedResponse_parse failed\n");
2674e71b7053SJung-uk Kim                 goto shut;
2675e71b7053SJung-uk Kim             } else if (result > 0) {
2676e71b7053SJung-uk Kim                 BIO_printf(bio_err, "STARTTLS failed, LDAP Result Code: %i\n",
2677e71b7053SJung-uk Kim                            result);
2678e71b7053SJung-uk Kim                 goto shut;
2679e71b7053SJung-uk Kim             }
2680e71b7053SJung-uk Kim             mbuf_len = 0;
2681e71b7053SJung-uk Kim         }
2682e71b7053SJung-uk Kim         break;
2683e71b7053SJung-uk Kim     }
2684e71b7053SJung-uk Kim 
2685e71b7053SJung-uk Kim     if (early_data_file != NULL
2686e71b7053SJung-uk Kim             && ((SSL_get0_session(con) != NULL
2687e71b7053SJung-uk Kim                  && SSL_SESSION_get_max_early_data(SSL_get0_session(con)) > 0)
2688e71b7053SJung-uk Kim                 || (psksess != NULL
2689e71b7053SJung-uk Kim                     && SSL_SESSION_get_max_early_data(psksess) > 0))) {
2690e71b7053SJung-uk Kim         BIO *edfile = BIO_new_file(early_data_file, "r");
2691e71b7053SJung-uk Kim         size_t readbytes, writtenbytes;
2692e71b7053SJung-uk Kim         int finish = 0;
2693e71b7053SJung-uk Kim 
2694e71b7053SJung-uk Kim         if (edfile == NULL) {
2695e71b7053SJung-uk Kim             BIO_printf(bio_err, "Cannot open early data file\n");
2696e71b7053SJung-uk Kim             goto shut;
2697e71b7053SJung-uk Kim         }
2698e71b7053SJung-uk Kim 
2699e71b7053SJung-uk Kim         while (!finish) {
2700e71b7053SJung-uk Kim             if (!BIO_read_ex(edfile, cbuf, BUFSIZZ, &readbytes))
2701e71b7053SJung-uk Kim                 finish = 1;
2702e71b7053SJung-uk Kim 
2703e71b7053SJung-uk Kim             while (!SSL_write_early_data(con, cbuf, readbytes, &writtenbytes)) {
2704e71b7053SJung-uk Kim                 switch (SSL_get_error(con, 0)) {
2705e71b7053SJung-uk Kim                 case SSL_ERROR_WANT_WRITE:
2706e71b7053SJung-uk Kim                 case SSL_ERROR_WANT_ASYNC:
2707e71b7053SJung-uk Kim                 case SSL_ERROR_WANT_READ:
2708e71b7053SJung-uk Kim                     /* Just keep trying - busy waiting */
2709e71b7053SJung-uk Kim                     continue;
2710e71b7053SJung-uk Kim                 default:
2711e71b7053SJung-uk Kim                     BIO_printf(bio_err, "Error writing early data\n");
2712e71b7053SJung-uk Kim                     BIO_free(edfile);
2713e71b7053SJung-uk Kim                     ERR_print_errors(bio_err);
2714e71b7053SJung-uk Kim                     goto shut;
2715e71b7053SJung-uk Kim                 }
2716e71b7053SJung-uk Kim             }
2717e71b7053SJung-uk Kim         }
2718e71b7053SJung-uk Kim 
2719e71b7053SJung-uk Kim         BIO_free(edfile);
2720db522d3aSSimon L. B. Nielsen     }
27215c87c606SMark Murray 
27226f9291ceSJung-uk Kim     for (;;) {
272374664626SKris Kennaway         FD_ZERO(&readfds);
272474664626SKris Kennaway         FD_ZERO(&writefds);
272574664626SKris Kennaway 
2726e71b7053SJung-uk Kim         if (SSL_is_dtls(con) && DTLSv1_get_timeout(con, &timeout))
27276a599222SSimon L. B. Nielsen             timeoutp = &timeout;
27286a599222SSimon L. B. Nielsen         else
27296a599222SSimon L. B. Nielsen             timeoutp = NULL;
27306a599222SSimon L. B. Nielsen 
2731e71b7053SJung-uk Kim         if (!SSL_is_init_finished(con) && SSL_total_renegotiations(con) == 0
2732e71b7053SJung-uk Kim                 && SSL_get_key_update_type(con) == SSL_KEY_UPDATE_NONE) {
273374664626SKris Kennaway             in_init = 1;
273474664626SKris Kennaway             tty_on = 0;
27356f9291ceSJung-uk Kim         } else {
273674664626SKris Kennaway             tty_on = 1;
27376f9291ceSJung-uk Kim             if (in_init) {
273874664626SKris Kennaway                 in_init = 0;
27397bded2dbSJung-uk Kim                 if (c_brief) {
27407bded2dbSJung-uk Kim                     BIO_puts(bio_err, "CONNECTION ESTABLISHED\n");
2741e71b7053SJung-uk Kim                     print_ssl_summary(con);
27427bded2dbSJung-uk Kim                 }
27437bded2dbSJung-uk Kim 
274474664626SKris Kennaway                 print_stuff(bio_c_out, con, full_log);
27456f9291ceSJung-uk Kim                 if (full_log > 0)
27466f9291ceSJung-uk Kim                     full_log--;
274774664626SKris Kennaway 
27486f9291ceSJung-uk Kim                 if (starttls_proto) {
2749e71b7053SJung-uk Kim                     BIO_write(bio_err, mbuf, mbuf_len);
27505c87c606SMark Murray                     /* We don't need to know any more */
2751e71b7053SJung-uk Kim                     if (!reconnect)
27525471f83eSSimon L. B. Nielsen                         starttls_proto = PROTO_OFF;
27535c87c606SMark Murray                 }
27545c87c606SMark Murray 
27556f9291ceSJung-uk Kim                 if (reconnect) {
275674664626SKris Kennaway                     reconnect--;
27576f9291ceSJung-uk Kim                     BIO_printf(bio_c_out,
27586f9291ceSJung-uk Kim                                "drop connection and then reconnect\n");
2759e71b7053SJung-uk Kim                     do_ssl_shutdown(con);
276074664626SKris Kennaway                     SSL_set_connect_state(con);
2761e71b7053SJung-uk Kim                     BIO_closesocket(SSL_get_fd(con));
276274664626SKris Kennaway                     goto re_start;
276374664626SKris Kennaway                 }
276474664626SKris Kennaway             }
276574664626SKris Kennaway         }
276674664626SKris Kennaway 
2767e71b7053SJung-uk Kim         ssl_pending = read_ssl && SSL_has_pending(con);
276874664626SKris Kennaway 
27696f9291ceSJung-uk Kim         if (!ssl_pending) {
2770e71b7053SJung-uk Kim #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
27716f9291ceSJung-uk Kim             if (tty_on) {
2772e71b7053SJung-uk Kim                 /*
2773e71b7053SJung-uk Kim                  * Note that select() returns when read _would not block_,
2774e71b7053SJung-uk Kim                  * and EOF satisfies that.  To avoid a CPU-hogging loop,
2775e71b7053SJung-uk Kim                  * set the flag so we exit.
2776e71b7053SJung-uk Kim                  */
2777e71b7053SJung-uk Kim                 if (read_tty && !at_eof)
2778aeb5019cSJung-uk Kim                     openssl_fdset(fileno_stdin(), &readfds);
2779aeb5019cSJung-uk Kim #if !defined(OPENSSL_SYS_VMS)
27806f9291ceSJung-uk Kim                 if (write_tty)
2781aeb5019cSJung-uk Kim                     openssl_fdset(fileno_stdout(), &writefds);
2782aeb5019cSJung-uk Kim #endif
278374664626SKris Kennaway             }
278474664626SKris Kennaway             if (read_ssl)
27851f13597dSJung-uk Kim                 openssl_fdset(SSL_get_fd(con), &readfds);
278674664626SKris Kennaway             if (write_ssl)
27871f13597dSJung-uk Kim                 openssl_fdset(SSL_get_fd(con), &writefds);
2788f579bf8eSKris Kennaway #else
2789f579bf8eSKris Kennaway             if (!tty_on || !write_tty) {
2790f579bf8eSKris Kennaway                 if (read_ssl)
27911f13597dSJung-uk Kim                     openssl_fdset(SSL_get_fd(con), &readfds);
2792f579bf8eSKris Kennaway                 if (write_ssl)
27931f13597dSJung-uk Kim                     openssl_fdset(SSL_get_fd(con), &writefds);
2794f579bf8eSKris Kennaway             }
2795f579bf8eSKris Kennaway #endif
279674664626SKris Kennaway 
27976f9291ceSJung-uk Kim             /*
27986f9291ceSJung-uk Kim              * Note: under VMS with SOCKETSHR the second parameter is
27996f9291ceSJung-uk Kim              * currently of type (int *) whereas under other systems it is
28006f9291ceSJung-uk Kim              * (void *) if you don't have a cast it will choke the compiler:
28016f9291ceSJung-uk Kim              * if you do have a cast then you can either go for (int *) or
28026f9291ceSJung-uk Kim              * (void *).
280374664626SKris Kennaway              */
280450ef0093SJacques Vidrine #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
28056f9291ceSJung-uk Kim             /*
28066f9291ceSJung-uk Kim              * Under Windows/DOS we make the assumption that we can always
28076f9291ceSJung-uk Kim              * write to the tty: therefore if we need to write to the tty we
28086f9291ceSJung-uk Kim              * just fall through. Otherwise we timeout the select every
28096f9291ceSJung-uk Kim              * second and see if there are any keypresses. Note: this is a
28106f9291ceSJung-uk Kim              * hack, in a proper Windows application we wouldn't do this.
2811f579bf8eSKris Kennaway              */
2812f579bf8eSKris Kennaway             i = 0;
2813f579bf8eSKris Kennaway             if (!write_tty) {
2814f579bf8eSKris Kennaway                 if (read_tty) {
2815f579bf8eSKris Kennaway                     tv.tv_sec = 1;
2816f579bf8eSKris Kennaway                     tv.tv_usec = 0;
2817f579bf8eSKris Kennaway                     i = select(width, (void *)&readfds, (void *)&writefds,
2818f579bf8eSKris Kennaway                                NULL, &tv);
2819e71b7053SJung-uk Kim                     if (!i && (!has_stdin_waiting() || !read_tty))
28201f13597dSJung-uk Kim                         continue;
28216f9291ceSJung-uk Kim                 } else
28226f9291ceSJung-uk Kim                     i = select(width, (void *)&readfds, (void *)&writefds,
28231f13597dSJung-uk Kim                                NULL, timeoutp);
28241f13597dSJung-uk Kim             }
2825f579bf8eSKris Kennaway #else
282674664626SKris Kennaway             i = select(width, (void *)&readfds, (void *)&writefds,
28276a599222SSimon L. B. Nielsen                        NULL, timeoutp);
2828f579bf8eSKris Kennaway #endif
28296f9291ceSJung-uk Kim             if (i < 0) {
283074664626SKris Kennaway                 BIO_printf(bio_err, "bad select %d\n",
283174664626SKris Kennaway                            get_last_socket_error());
283274664626SKris Kennaway                 goto shut;
283374664626SKris Kennaway             }
283474664626SKris Kennaway         }
283574664626SKris Kennaway 
2836e71b7053SJung-uk Kim         if (SSL_is_dtls(con) && DTLSv1_handle_timeout(con) > 0)
2837e71b7053SJung-uk Kim             BIO_printf(bio_err, "TIMEOUT occurred\n");
28386a599222SSimon L. B. Nielsen 
28396f9291ceSJung-uk Kim         if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {
28406f9291ceSJung-uk Kim             k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);
28416f9291ceSJung-uk Kim             switch (SSL_get_error(con, k)) {
284274664626SKris Kennaway             case SSL_ERROR_NONE:
284374664626SKris Kennaway                 cbuf_off += k;
284474664626SKris Kennaway                 cbuf_len -= k;
28456f9291ceSJung-uk Kim                 if (k <= 0)
28466f9291ceSJung-uk Kim                     goto end;
284774664626SKris Kennaway                 /* we have done a  write(con,NULL,0); */
28486f9291ceSJung-uk Kim                 if (cbuf_len <= 0) {
284974664626SKris Kennaway                     read_tty = 1;
285074664626SKris Kennaway                     write_ssl = 0;
28516f9291ceSJung-uk Kim                 } else {        /* if (cbuf_len > 0) */
28526f9291ceSJung-uk Kim 
285374664626SKris Kennaway                     read_tty = 0;
285474664626SKris Kennaway                     write_ssl = 1;
285574664626SKris Kennaway                 }
285674664626SKris Kennaway                 break;
285774664626SKris Kennaway             case SSL_ERROR_WANT_WRITE:
285874664626SKris Kennaway                 BIO_printf(bio_c_out, "write W BLOCK\n");
285974664626SKris Kennaway                 write_ssl = 1;
286074664626SKris Kennaway                 read_tty = 0;
286174664626SKris Kennaway                 break;
2862e71b7053SJung-uk Kim             case SSL_ERROR_WANT_ASYNC:
2863e71b7053SJung-uk Kim                 BIO_printf(bio_c_out, "write A BLOCK\n");
2864e71b7053SJung-uk Kim                 wait_for_async(con);
2865e71b7053SJung-uk Kim                 write_ssl = 1;
2866e71b7053SJung-uk Kim                 read_tty = 0;
2867e71b7053SJung-uk Kim                 break;
286874664626SKris Kennaway             case SSL_ERROR_WANT_READ:
286974664626SKris Kennaway                 BIO_printf(bio_c_out, "write R BLOCK\n");
287074664626SKris Kennaway                 write_tty = 0;
287174664626SKris Kennaway                 read_ssl = 1;
287274664626SKris Kennaway                 write_ssl = 0;
287374664626SKris Kennaway                 break;
287474664626SKris Kennaway             case SSL_ERROR_WANT_X509_LOOKUP:
287574664626SKris Kennaway                 BIO_printf(bio_c_out, "write X BLOCK\n");
287674664626SKris Kennaway                 break;
287774664626SKris Kennaway             case SSL_ERROR_ZERO_RETURN:
28786f9291ceSJung-uk Kim                 if (cbuf_len != 0) {
287974664626SKris Kennaway                     BIO_printf(bio_c_out, "shutdown\n");
28801f13597dSJung-uk Kim                     ret = 0;
288174664626SKris Kennaway                     goto shut;
28826f9291ceSJung-uk Kim                 } else {
288374664626SKris Kennaway                     read_tty = 1;
288474664626SKris Kennaway                     write_ssl = 0;
288574664626SKris Kennaway                     break;
288674664626SKris Kennaway                 }
288774664626SKris Kennaway 
288874664626SKris Kennaway             case SSL_ERROR_SYSCALL:
28896f9291ceSJung-uk Kim                 if ((k != 0) || (cbuf_len != 0)) {
289074664626SKris Kennaway                     BIO_printf(bio_err, "write:errno=%d\n",
289174664626SKris Kennaway                                get_last_socket_error());
289274664626SKris Kennaway                     goto shut;
28936f9291ceSJung-uk Kim                 } else {
289474664626SKris Kennaway                     read_tty = 1;
289574664626SKris Kennaway                     write_ssl = 0;
289674664626SKris Kennaway                 }
289774664626SKris Kennaway                 break;
2898e71b7053SJung-uk Kim             case SSL_ERROR_WANT_ASYNC_JOB:
2899e71b7053SJung-uk Kim                 /* This shouldn't ever happen in s_client - treat as an error */
290074664626SKris Kennaway             case SSL_ERROR_SSL:
290174664626SKris Kennaway                 ERR_print_errors(bio_err);
290274664626SKris Kennaway                 goto shut;
290374664626SKris Kennaway             }
290474664626SKris Kennaway         }
2905e71b7053SJung-uk Kim #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VMS)
29061f13597dSJung-uk Kim         /* Assume Windows/DOS/BeOS can always write */
2907f579bf8eSKris Kennaway         else if (!ssl_pending && write_tty)
2908f579bf8eSKris Kennaway #else
2909aeb5019cSJung-uk Kim         else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds))
2910f579bf8eSKris Kennaway #endif
291174664626SKris Kennaway         {
291274664626SKris Kennaway #ifdef CHARSET_EBCDIC
291374664626SKris Kennaway             ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);
291474664626SKris Kennaway #endif
29151f13597dSJung-uk Kim             i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);
291674664626SKris Kennaway 
29176f9291ceSJung-uk Kim             if (i <= 0) {
291874664626SKris Kennaway                 BIO_printf(bio_c_out, "DONE\n");
29191f13597dSJung-uk Kim                 ret = 0;
292074664626SKris Kennaway                 goto shut;
292174664626SKris Kennaway             }
292274664626SKris Kennaway 
2923e71b7053SJung-uk Kim             sbuf_len -= i;
292474664626SKris Kennaway             sbuf_off += i;
29256f9291ceSJung-uk Kim             if (sbuf_len <= 0) {
292674664626SKris Kennaway                 read_ssl = 1;
292774664626SKris Kennaway                 write_tty = 0;
292874664626SKris Kennaway             }
29296f9291ceSJung-uk Kim         } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {
293074664626SKris Kennaway #ifdef RENEG
29316f9291ceSJung-uk Kim             {
29326f9291ceSJung-uk Kim                 static int iiii;
29336f9291ceSJung-uk Kim                 if (++iiii == 52) {
29346f9291ceSJung-uk Kim                     SSL_renegotiate(con);
29356f9291ceSJung-uk Kim                     iiii = 0;
29366f9291ceSJung-uk Kim                 }
29376f9291ceSJung-uk Kim             }
293874664626SKris Kennaway #endif
293974664626SKris Kennaway             k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ );
294074664626SKris Kennaway 
29416f9291ceSJung-uk Kim             switch (SSL_get_error(con, k)) {
294274664626SKris Kennaway             case SSL_ERROR_NONE:
294374664626SKris Kennaway                 if (k <= 0)
294474664626SKris Kennaway                     goto end;
294574664626SKris Kennaway                 sbuf_off = 0;
294674664626SKris Kennaway                 sbuf_len = k;
294774664626SKris Kennaway 
294874664626SKris Kennaway                 read_ssl = 0;
294974664626SKris Kennaway                 write_tty = 1;
295074664626SKris Kennaway                 break;
2951e71b7053SJung-uk Kim             case SSL_ERROR_WANT_ASYNC:
2952e71b7053SJung-uk Kim                 BIO_printf(bio_c_out, "read A BLOCK\n");
2953e71b7053SJung-uk Kim                 wait_for_async(con);
2954e71b7053SJung-uk Kim                 write_tty = 0;
2955e71b7053SJung-uk Kim                 read_ssl = 1;
2956e71b7053SJung-uk Kim                 if ((read_tty == 0) && (write_ssl == 0))
2957e71b7053SJung-uk Kim                     write_ssl = 1;
2958e71b7053SJung-uk Kim                 break;
295974664626SKris Kennaway             case SSL_ERROR_WANT_WRITE:
296074664626SKris Kennaway                 BIO_printf(bio_c_out, "read W BLOCK\n");
296174664626SKris Kennaway                 write_ssl = 1;
296274664626SKris Kennaway                 read_tty = 0;
296374664626SKris Kennaway                 break;
296474664626SKris Kennaway             case SSL_ERROR_WANT_READ:
296574664626SKris Kennaway                 BIO_printf(bio_c_out, "read R BLOCK\n");
296674664626SKris Kennaway                 write_tty = 0;
296774664626SKris Kennaway                 read_ssl = 1;
296874664626SKris Kennaway                 if ((read_tty == 0) && (write_ssl == 0))
296974664626SKris Kennaway                     write_ssl = 1;
297074664626SKris Kennaway                 break;
297174664626SKris Kennaway             case SSL_ERROR_WANT_X509_LOOKUP:
297274664626SKris Kennaway                 BIO_printf(bio_c_out, "read X BLOCK\n");
297374664626SKris Kennaway                 break;
297474664626SKris Kennaway             case SSL_ERROR_SYSCALL:
29751f13597dSJung-uk Kim                 ret = get_last_socket_error();
29767bded2dbSJung-uk Kim                 if (c_brief)
29777bded2dbSJung-uk Kim                     BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n");
29787bded2dbSJung-uk Kim                 else
29791f13597dSJung-uk Kim                     BIO_printf(bio_err, "read:errno=%d\n", ret);
298074664626SKris Kennaway                 goto shut;
298174664626SKris Kennaway             case SSL_ERROR_ZERO_RETURN:
298274664626SKris Kennaway                 BIO_printf(bio_c_out, "closed\n");
29831f13597dSJung-uk Kim                 ret = 0;
298474664626SKris Kennaway                 goto shut;
2985e71b7053SJung-uk Kim             case SSL_ERROR_WANT_ASYNC_JOB:
2986e71b7053SJung-uk Kim                 /* This shouldn't ever happen in s_client. Treat as an error */
298774664626SKris Kennaway             case SSL_ERROR_SSL:
298874664626SKris Kennaway                 ERR_print_errors(bio_err);
298974664626SKris Kennaway                 goto shut;
299074664626SKris Kennaway             }
299174664626SKris Kennaway         }
2992e71b7053SJung-uk Kim /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */
2993e71b7053SJung-uk Kim #if defined(OPENSSL_SYS_MSDOS)
2994e71b7053SJung-uk Kim         else if (has_stdin_waiting())
2995f579bf8eSKris Kennaway #else
2996aeb5019cSJung-uk Kim         else if (FD_ISSET(fileno_stdin(), &readfds))
2997f579bf8eSKris Kennaway #endif
299874664626SKris Kennaway         {
29996f9291ceSJung-uk Kim             if (crlf) {
300074664626SKris Kennaway                 int j, lf_num;
300174664626SKris Kennaway 
30021f13597dSJung-uk Kim                 i = raw_read_stdin(cbuf, BUFSIZZ / 2);
300374664626SKris Kennaway                 lf_num = 0;
300474664626SKris Kennaway                 /* both loops are skipped when i <= 0 */
300574664626SKris Kennaway                 for (j = 0; j < i; j++)
300674664626SKris Kennaway                     if (cbuf[j] == '\n')
300774664626SKris Kennaway                         lf_num++;
30086f9291ceSJung-uk Kim                 for (j = i - 1; j >= 0; j--) {
300974664626SKris Kennaway                     cbuf[j + lf_num] = cbuf[j];
30106f9291ceSJung-uk Kim                     if (cbuf[j] == '\n') {
301174664626SKris Kennaway                         lf_num--;
301274664626SKris Kennaway                         i++;
301374664626SKris Kennaway                         cbuf[j + lf_num] = '\r';
301474664626SKris Kennaway                     }
301574664626SKris Kennaway                 }
301674664626SKris Kennaway                 assert(lf_num == 0);
30176f9291ceSJung-uk Kim             } else
30181f13597dSJung-uk Kim                 i = raw_read_stdin(cbuf, BUFSIZZ);
3019e71b7053SJung-uk Kim #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
3020e71b7053SJung-uk Kim             if (i == 0)
3021e71b7053SJung-uk Kim                 at_eof = 1;
3022e71b7053SJung-uk Kim #endif
302374664626SKris Kennaway 
3024e71b7053SJung-uk Kim             if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q' && cmdletters))) {
302574664626SKris Kennaway                 BIO_printf(bio_err, "DONE\n");
30261f13597dSJung-uk Kim                 ret = 0;
302774664626SKris Kennaway                 goto shut;
302874664626SKris Kennaway             }
302974664626SKris Kennaway 
3030e71b7053SJung-uk Kim             if ((!c_ign_eof) && (cbuf[0] == 'R' && cmdletters)) {
303174664626SKris Kennaway                 BIO_printf(bio_err, "RENEGOTIATING\n");
303274664626SKris Kennaway                 SSL_renegotiate(con);
303374664626SKris Kennaway                 cbuf_len = 0;
30346935a639SJung-uk Kim             } else if (!c_ign_eof && (cbuf[0] == 'K' || cbuf[0] == 'k' )
3035e71b7053SJung-uk Kim                     && cmdletters) {
3036e71b7053SJung-uk Kim                 BIO_printf(bio_err, "KEYUPDATE\n");
3037e71b7053SJung-uk Kim                 SSL_key_update(con,
3038e71b7053SJung-uk Kim                                cbuf[0] == 'K' ? SSL_KEY_UPDATE_REQUESTED
3039e71b7053SJung-uk Kim                                               : SSL_KEY_UPDATE_NOT_REQUESTED);
3040e71b7053SJung-uk Kim                 cbuf_len = 0;
3041b077aed3SPierre Pronchery             } else {
304274664626SKris Kennaway                 cbuf_len = i;
304374664626SKris Kennaway                 cbuf_off = 0;
304474664626SKris Kennaway #ifdef CHARSET_EBCDIC
304574664626SKris Kennaway                 ebcdic2ascii(cbuf, cbuf, i);
304674664626SKris Kennaway #endif
304774664626SKris Kennaway             }
304874664626SKris Kennaway 
304974664626SKris Kennaway             write_ssl = 1;
305074664626SKris Kennaway             read_tty = 0;
305174664626SKris Kennaway         }
305274664626SKris Kennaway     }
30531f13597dSJung-uk Kim 
305474664626SKris Kennaway  shut:
30551f13597dSJung-uk Kim     if (in_init)
30561f13597dSJung-uk Kim         print_stuff(bio_c_out, con, full_log);
3057e71b7053SJung-uk Kim     do_ssl_shutdown(con);
3058e71b7053SJung-uk Kim 
3059e71b7053SJung-uk Kim     /*
3060e71b7053SJung-uk Kim      * If we ended with an alert being sent, but still with data in the
3061e71b7053SJung-uk Kim      * network buffer to be read, then calling BIO_closesocket() will
3062e71b7053SJung-uk Kim      * result in a TCP-RST being sent. On some platforms (notably
3063e71b7053SJung-uk Kim      * Windows) then this will result in the peer immediately abandoning
3064e71b7053SJung-uk Kim      * the connection including any buffered alert data before it has
3065e71b7053SJung-uk Kim      * had a chance to be read. Shutting down the sending side first,
3066e71b7053SJung-uk Kim      * and then closing the socket sends TCP-FIN first followed by
3067e71b7053SJung-uk Kim      * TCP-RST. This seems to allow the peer to read the alert data.
3068e71b7053SJung-uk Kim      */
3069e71b7053SJung-uk Kim     shutdown(SSL_get_fd(con), 1); /* SHUT_WR */
3070e71b7053SJung-uk Kim     /*
3071e71b7053SJung-uk Kim      * We just said we have nothing else to say, but it doesn't mean that
3072e71b7053SJung-uk Kim      * the other side has nothing. It's even recommended to consume incoming
3073e71b7053SJung-uk Kim      * data. [In testing context this ensures that alerts are passed on...]
3074e71b7053SJung-uk Kim      */
3075e71b7053SJung-uk Kim     timeout.tv_sec = 0;
3076e71b7053SJung-uk Kim     timeout.tv_usec = 500000;  /* some extreme round-trip */
3077e71b7053SJung-uk Kim     do {
3078e71b7053SJung-uk Kim         FD_ZERO(&readfds);
3079b077aed3SPierre Pronchery         openssl_fdset(sock, &readfds);
3080b077aed3SPierre Pronchery     } while (select(sock + 1, &readfds, NULL, NULL, &timeout) > 0
3081e71b7053SJung-uk Kim              && BIO_read(sbio, sbuf, BUFSIZZ) > 0);
3082e71b7053SJung-uk Kim 
3083e71b7053SJung-uk Kim     BIO_closesocket(SSL_get_fd(con));
308474664626SKris Kennaway  end:
30856f9291ceSJung-uk Kim     if (con != NULL) {
30861f13597dSJung-uk Kim         if (prexit != 0)
30871f13597dSJung-uk Kim             print_stuff(bio_c_out, con, 1);
30881f13597dSJung-uk Kim         SSL_free(con);
30891f13597dSJung-uk Kim     }
3090e71b7053SJung-uk Kim     SSL_SESSION_free(psksess);
3091e71b7053SJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
309209286989SJung-uk Kim     OPENSSL_free(next_proto.data);
309309286989SJung-uk Kim #endif
30946f9291ceSJung-uk Kim     SSL_CTX_free(ctx);
3095e71b7053SJung-uk Kim     set_keylog_file(NULL, NULL);
30963b4e3dcbSSimon L. B. Nielsen     X509_free(cert);
30977bded2dbSJung-uk Kim     sk_X509_CRL_pop_free(crls, X509_CRL_free);
30983b4e3dcbSSimon L. B. Nielsen     EVP_PKEY_free(key);
30997bded2dbSJung-uk Kim     sk_X509_pop_free(chain, X509_free);
31003b4e3dcbSSimon L. B. Nielsen     OPENSSL_free(pass);
310180815a77SJung-uk Kim #ifndef OPENSSL_NO_SRP
310280815a77SJung-uk Kim     OPENSSL_free(srp_arg.srppassin);
310380815a77SJung-uk Kim #endif
31045ac766abSJung-uk Kim     OPENSSL_free(sname_alloc);
3105e71b7053SJung-uk Kim     OPENSSL_free(connectstr);
3106e71b7053SJung-uk Kim     OPENSSL_free(bindstr);
3107b2bf0c7eSJung-uk Kim     OPENSSL_free(bindhost);
3108b2bf0c7eSJung-uk Kim     OPENSSL_free(bindport);
3109e71b7053SJung-uk Kim     OPENSSL_free(host);
3110e71b7053SJung-uk Kim     OPENSSL_free(port);
3111b077aed3SPierre Pronchery     OPENSSL_free(thost);
3112b077aed3SPierre Pronchery     OPENSSL_free(tport);
311309286989SJung-uk Kim     X509_VERIFY_PARAM_free(vpm);
31147bded2dbSJung-uk Kim     ssl_excert_free(exc);
31157bded2dbSJung-uk Kim     sk_OPENSSL_STRING_free(ssl_args);
3116e71b7053SJung-uk Kim     sk_OPENSSL_STRING_free(dane_tlsa_rrset);
31177bded2dbSJung-uk Kim     SSL_CONF_CTX_free(cctx);
3118e71b7053SJung-uk Kim     OPENSSL_clear_free(cbuf, BUFSIZZ);
3119e71b7053SJung-uk Kim     OPENSSL_clear_free(sbuf, BUFSIZZ);
3120e71b7053SJung-uk Kim     OPENSSL_clear_free(mbuf, BUFSIZZ);
3121b077aed3SPierre Pronchery     clear_free(proxypass);
31226cf8931aSJung-uk Kim     release_engine(e);
312374664626SKris Kennaway     BIO_free(bio_c_out);
312474664626SKris Kennaway     bio_c_out = NULL;
31257bded2dbSJung-uk Kim     BIO_free(bio_c_msg);
31267bded2dbSJung-uk Kim     bio_c_msg = NULL;
3127e71b7053SJung-uk Kim     return ret;
312874664626SKris Kennaway }
312974664626SKris Kennaway 
print_stuff(BIO * bio,SSL * s,int full)313074664626SKris Kennaway static void print_stuff(BIO *bio, SSL *s, int full)
313174664626SKris Kennaway {
313274664626SKris Kennaway     X509 *peer = NULL;
313374664626SKris Kennaway     STACK_OF(X509) *sk;
31341f13597dSJung-uk Kim     const SSL_CIPHER *c;
3135b077aed3SPierre Pronchery     EVP_PKEY *public_key;
3136e71b7053SJung-uk Kim     int i, istls13 = (SSL_version(s) == TLS1_3_VERSION);
3137e71b7053SJung-uk Kim     long verify_result;
31383b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_COMP
31393b4e3dcbSSimon L. B. Nielsen     const COMP_METHOD *comp, *expansion;
31403b4e3dcbSSimon L. B. Nielsen #endif
31411f13597dSJung-uk Kim     unsigned char *exportedkeymat;
3142e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
3143e71b7053SJung-uk Kim     const SSL_CTX *ctx = SSL_get_SSL_CTX(s);
3144e71b7053SJung-uk Kim #endif
314574664626SKris Kennaway 
31466f9291ceSJung-uk Kim     if (full) {
314774664626SKris Kennaway         int got_a_chain = 0;
314874664626SKris Kennaway 
314974664626SKris Kennaway         sk = SSL_get_peer_cert_chain(s);
31506f9291ceSJung-uk Kim         if (sk != NULL) {
3151e71b7053SJung-uk Kim             got_a_chain = 1;
315274664626SKris Kennaway 
315374664626SKris Kennaway             BIO_printf(bio, "---\nCertificate chain\n");
31546f9291ceSJung-uk Kim             for (i = 0; i < sk_X509_num(sk); i++) {
3155e71b7053SJung-uk Kim                 BIO_printf(bio, "%2d s:", i);
3156e71b7053SJung-uk Kim                 X509_NAME_print_ex(bio, X509_get_subject_name(sk_X509_value(sk, i)), 0, get_nameopt());
3157e71b7053SJung-uk Kim                 BIO_puts(bio, "\n");
3158e71b7053SJung-uk Kim                 BIO_printf(bio, "   i:");
3159e71b7053SJung-uk Kim                 X509_NAME_print_ex(bio, X509_get_issuer_name(sk_X509_value(sk, i)), 0, get_nameopt());
3160e71b7053SJung-uk Kim                 BIO_puts(bio, "\n");
3161b077aed3SPierre Pronchery                 public_key = X509_get_pubkey(sk_X509_value(sk, i));
3162b077aed3SPierre Pronchery                 if (public_key != NULL) {
3163b077aed3SPierre Pronchery                     BIO_printf(bio, "   a:PKEY: %s, %d (bit); sigalg: %s\n",
3164b077aed3SPierre Pronchery                                OBJ_nid2sn(EVP_PKEY_get_base_id(public_key)),
3165b077aed3SPierre Pronchery                                EVP_PKEY_get_bits(public_key),
3166b077aed3SPierre Pronchery                                OBJ_nid2sn(X509_get_signature_nid(sk_X509_value(sk, i))));
3167b077aed3SPierre Pronchery                     EVP_PKEY_free(public_key);
3168b077aed3SPierre Pronchery                 }
3169b077aed3SPierre Pronchery                 BIO_printf(bio, "   v:NotBefore: ");
3170b077aed3SPierre Pronchery                 ASN1_TIME_print(bio, X509_get0_notBefore(sk_X509_value(sk, i)));
3171b077aed3SPierre Pronchery                 BIO_printf(bio, "; NotAfter: ");
3172b077aed3SPierre Pronchery                 ASN1_TIME_print(bio, X509_get0_notAfter(sk_X509_value(sk, i)));
3173b077aed3SPierre Pronchery                 BIO_puts(bio, "\n");
317474664626SKris Kennaway                 if (c_showcerts)
317574664626SKris Kennaway                     PEM_write_bio_X509(bio, sk_X509_value(sk, i));
317674664626SKris Kennaway             }
317774664626SKris Kennaway         }
317874664626SKris Kennaway 
317974664626SKris Kennaway         BIO_printf(bio, "---\n");
3180b077aed3SPierre Pronchery         peer = SSL_get0_peer_certificate(s);
31816f9291ceSJung-uk Kim         if (peer != NULL) {
318274664626SKris Kennaway             BIO_printf(bio, "Server certificate\n");
31836f9291ceSJung-uk Kim 
31846f9291ceSJung-uk Kim             /* Redundant if we showed the whole chain */
31856f9291ceSJung-uk Kim             if (!(c_showcerts && got_a_chain))
318674664626SKris Kennaway                 PEM_write_bio_X509(bio, peer);
3187e71b7053SJung-uk Kim             dump_cert_text(bio, peer);
3188e71b7053SJung-uk Kim         } else {
318974664626SKris Kennaway             BIO_printf(bio, "no peer certificate available\n");
319074664626SKris Kennaway         }
3191e71b7053SJung-uk Kim         print_ca_names(bio, s);
319274664626SKris Kennaway 
31937bded2dbSJung-uk Kim         ssl_print_sigalgs(bio, s);
31947bded2dbSJung-uk Kim         ssl_print_tmp_key(bio, s);
31957bded2dbSJung-uk Kim 
3196e71b7053SJung-uk Kim #ifndef OPENSSL_NO_CT
3197e71b7053SJung-uk Kim         /*
3198e71b7053SJung-uk Kim          * When the SSL session is anonymous, or resumed via an abbreviated
3199e71b7053SJung-uk Kim          * handshake, no SCTs are provided as part of the handshake.  While in
3200e71b7053SJung-uk Kim          * a resumed session SCTs may be present in the session's certificate,
3201e71b7053SJung-uk Kim          * no callbacks are invoked to revalidate these, and in any case that
3202e71b7053SJung-uk Kim          * set of SCTs may be incomplete.  Thus it makes little sense to
3203e71b7053SJung-uk Kim          * attempt to display SCTs from a resumed session's certificate, and of
3204e71b7053SJung-uk Kim          * course none are associated with an anonymous peer.
3205e71b7053SJung-uk Kim          */
3206e71b7053SJung-uk Kim         if (peer != NULL && !SSL_session_reused(s) && SSL_ct_is_enabled(s)) {
3207e71b7053SJung-uk Kim             const STACK_OF(SCT) *scts = SSL_get0_peer_scts(s);
3208e71b7053SJung-uk Kim             int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
3209e71b7053SJung-uk Kim 
3210e71b7053SJung-uk Kim             BIO_printf(bio, "---\nSCTs present (%i)\n", sct_count);
3211e71b7053SJung-uk Kim             if (sct_count > 0) {
3212e71b7053SJung-uk Kim                 const CTLOG_STORE *log_store = SSL_CTX_get0_ctlog_store(ctx);
3213e71b7053SJung-uk Kim 
3214e71b7053SJung-uk Kim                 BIO_printf(bio, "---\n");
3215e71b7053SJung-uk Kim                 for (i = 0; i < sct_count; ++i) {
3216e71b7053SJung-uk Kim                     SCT *sct = sk_SCT_value(scts, i);
3217e71b7053SJung-uk Kim 
3218e71b7053SJung-uk Kim                     BIO_printf(bio, "SCT validation status: %s\n",
3219e71b7053SJung-uk Kim                                SCT_validation_status_string(sct));
3220e71b7053SJung-uk Kim                     SCT_print(sct, bio, 0, log_store);
3221e71b7053SJung-uk Kim                     if (i < sct_count - 1)
3222e71b7053SJung-uk Kim                         BIO_printf(bio, "\n---\n");
3223e71b7053SJung-uk Kim                 }
3224e71b7053SJung-uk Kim                 BIO_printf(bio, "\n");
3225e71b7053SJung-uk Kim             }
3226e71b7053SJung-uk Kim         }
3227e71b7053SJung-uk Kim #endif
3228e71b7053SJung-uk Kim 
32296f9291ceSJung-uk Kim         BIO_printf(bio,
3230e71b7053SJung-uk Kim                    "---\nSSL handshake has read %ju bytes "
3231e71b7053SJung-uk Kim                    "and written %ju bytes\n",
323274664626SKris Kennaway                    BIO_number_read(SSL_get_rbio(s)),
323374664626SKris Kennaway                    BIO_number_written(SSL_get_wbio(s)));
323474664626SKris Kennaway     }
3235e71b7053SJung-uk Kim     print_verify_detail(s, bio);
3236e71b7053SJung-uk Kim     BIO_printf(bio, (SSL_session_reused(s) ? "---\nReused, " : "---\nNew, "));
323774664626SKris Kennaway     c = SSL_get_current_cipher(s);
323874664626SKris Kennaway     BIO_printf(bio, "%s, Cipher is %s\n",
32396f9291ceSJung-uk Kim                SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
324074664626SKris Kennaway     if (peer != NULL) {
324174664626SKris Kennaway         EVP_PKEY *pktmp;
3242e71b7053SJung-uk Kim 
3243e71b7053SJung-uk Kim         pktmp = X509_get0_pubkey(peer);
324474664626SKris Kennaway         BIO_printf(bio, "Server public key is %d bit\n",
3245b077aed3SPierre Pronchery                    EVP_PKEY_get_bits(pktmp));
324674664626SKris Kennaway     }
32476a599222SSimon L. B. Nielsen     BIO_printf(bio, "Secure Renegotiation IS%s supported\n",
32486a599222SSimon L. B. Nielsen                SSL_get_secure_renegotiation_support(s) ? "" : " NOT");
32493b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_COMP
32503b4e3dcbSSimon L. B. Nielsen     comp = SSL_get_current_compression(s);
32513b4e3dcbSSimon L. B. Nielsen     expansion = SSL_get_current_expansion(s);
32523b4e3dcbSSimon L. B. Nielsen     BIO_printf(bio, "Compression: %s\n",
32533b4e3dcbSSimon L. B. Nielsen                comp ? SSL_COMP_get_name(comp) : "NONE");
32543b4e3dcbSSimon L. B. Nielsen     BIO_printf(bio, "Expansion: %s\n",
32553b4e3dcbSSimon L. B. Nielsen                expansion ? SSL_COMP_get_name(expansion) : "NONE");
32563b4e3dcbSSimon L. B. Nielsen #endif
3257aa906e2aSJohn Baldwin #ifndef OPENSSL_NO_KTLS
3258aa906e2aSJohn Baldwin     if (BIO_get_ktls_send(SSL_get_wbio(s)))
3259aa906e2aSJohn Baldwin         BIO_printf(bio_err, "Using Kernel TLS for sending\n");
3260aa906e2aSJohn Baldwin     if (BIO_get_ktls_recv(SSL_get_rbio(s)))
3261aa906e2aSJohn Baldwin         BIO_printf(bio_err, "Using Kernel TLS for receiving\n");
3262aa906e2aSJohn Baldwin #endif
32631f13597dSJung-uk Kim 
3264b077aed3SPierre Pronchery     if (OSSL_TRACE_ENABLED(TLS)) {
32651f13597dSJung-uk Kim         /* Print out local port of connection: useful for debugging */
32661f13597dSJung-uk Kim         int sock;
3267e71b7053SJung-uk Kim         union BIO_sock_info_u info;
3268e71b7053SJung-uk Kim 
32691f13597dSJung-uk Kim         sock = SSL_get_fd(s);
3270e71b7053SJung-uk Kim         if ((info.addr = BIO_ADDR_new()) != NULL
3271e71b7053SJung-uk Kim             && BIO_sock_info(sock, BIO_SOCK_INFO_ADDRESS, &info)) {
3272e71b7053SJung-uk Kim             BIO_printf(bio_c_out, "LOCAL PORT is %u\n",
3273e71b7053SJung-uk Kim                        ntohs(BIO_ADDR_rawport(info.addr)));
3274e71b7053SJung-uk Kim         }
3275e71b7053SJung-uk Kim         BIO_ADDR_free(info.addr);
32761f13597dSJung-uk Kim     }
32771f13597dSJung-uk Kim 
32787bded2dbSJung-uk Kim #if !defined(OPENSSL_NO_NEXTPROTONEG)
32791f13597dSJung-uk Kim     if (next_proto.status != -1) {
32801f13597dSJung-uk Kim         const unsigned char *proto;
32811f13597dSJung-uk Kim         unsigned int proto_len;
32821f13597dSJung-uk Kim         SSL_get0_next_proto_negotiated(s, &proto, &proto_len);
32831f13597dSJung-uk Kim         BIO_printf(bio, "Next protocol: (%d) ", next_proto.status);
32841f13597dSJung-uk Kim         BIO_write(bio, proto, proto_len);
32851f13597dSJung-uk Kim         BIO_write(bio, "\n", 1);
32861f13597dSJung-uk Kim     }
32871f13597dSJung-uk Kim #endif
32887bded2dbSJung-uk Kim     {
32897bded2dbSJung-uk Kim         const unsigned char *proto;
32907bded2dbSJung-uk Kim         unsigned int proto_len;
32917bded2dbSJung-uk Kim         SSL_get0_alpn_selected(s, &proto, &proto_len);
32927bded2dbSJung-uk Kim         if (proto_len > 0) {
32937bded2dbSJung-uk Kim             BIO_printf(bio, "ALPN protocol: ");
32947bded2dbSJung-uk Kim             BIO_write(bio, proto, proto_len);
32957bded2dbSJung-uk Kim             BIO_write(bio, "\n", 1);
32967bded2dbSJung-uk Kim         } else
32977bded2dbSJung-uk Kim             BIO_printf(bio, "No ALPN negotiated\n");
32987bded2dbSJung-uk Kim     }
32991f13597dSJung-uk Kim 
330009286989SJung-uk Kim #ifndef OPENSSL_NO_SRTP
33011f13597dSJung-uk Kim     {
33026f9291ceSJung-uk Kim         SRTP_PROTECTION_PROFILE *srtp_profile =
33036f9291ceSJung-uk Kim             SSL_get_selected_srtp_profile(s);
33041f13597dSJung-uk Kim 
33051f13597dSJung-uk Kim         if (srtp_profile)
33061f13597dSJung-uk Kim             BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n",
33071f13597dSJung-uk Kim                        srtp_profile->name);
33081f13597dSJung-uk Kim     }
330909286989SJung-uk Kim #endif
33101f13597dSJung-uk Kim 
3311e71b7053SJung-uk Kim     if (istls13) {
3312e71b7053SJung-uk Kim         switch (SSL_get_early_data_status(s)) {
3313e71b7053SJung-uk Kim         case SSL_EARLY_DATA_NOT_SENT:
3314e71b7053SJung-uk Kim             BIO_printf(bio, "Early data was not sent\n");
3315e71b7053SJung-uk Kim             break;
3316e71b7053SJung-uk Kim 
3317e71b7053SJung-uk Kim         case SSL_EARLY_DATA_REJECTED:
3318e71b7053SJung-uk Kim             BIO_printf(bio, "Early data was rejected\n");
3319e71b7053SJung-uk Kim             break;
3320e71b7053SJung-uk Kim 
3321e71b7053SJung-uk Kim         case SSL_EARLY_DATA_ACCEPTED:
3322e71b7053SJung-uk Kim             BIO_printf(bio, "Early data was accepted\n");
3323e71b7053SJung-uk Kim             break;
3324e71b7053SJung-uk Kim 
3325e71b7053SJung-uk Kim         }
3326e71b7053SJung-uk Kim 
3327e71b7053SJung-uk Kim         /*
3328e71b7053SJung-uk Kim          * We also print the verify results when we dump session information,
3329e71b7053SJung-uk Kim          * but in TLSv1.3 we may not get that right away (or at all) depending
3330e71b7053SJung-uk Kim          * on when we get a NewSessionTicket. Therefore we print it now as well.
3331e71b7053SJung-uk Kim          */
3332e71b7053SJung-uk Kim         verify_result = SSL_get_verify_result(s);
3333e71b7053SJung-uk Kim         BIO_printf(bio, "Verify return code: %ld (%s)\n", verify_result,
3334e71b7053SJung-uk Kim                    X509_verify_cert_error_string(verify_result));
3335e71b7053SJung-uk Kim     } else {
3336e71b7053SJung-uk Kim         /* In TLSv1.3 we do this on arrival of a NewSessionTicket */
333774664626SKris Kennaway         SSL_SESSION_print(bio, SSL_get_session(s));
3338e71b7053SJung-uk Kim     }
3339e71b7053SJung-uk Kim 
3340e71b7053SJung-uk Kim     if (SSL_get_session(s) != NULL && keymatexportlabel != NULL) {
33411f13597dSJung-uk Kim         BIO_printf(bio, "Keying material exporter:\n");
33421f13597dSJung-uk Kim         BIO_printf(bio, "    Label: '%s'\n", keymatexportlabel);
33431f13597dSJung-uk Kim         BIO_printf(bio, "    Length: %i bytes\n", keymatexportlen);
3344e71b7053SJung-uk Kim         exportedkeymat = app_malloc(keymatexportlen, "export key");
3345b077aed3SPierre Pronchery         if (SSL_export_keying_material(s, exportedkeymat,
33461f13597dSJung-uk Kim                                         keymatexportlen,
33471f13597dSJung-uk Kim                                         keymatexportlabel,
33481f13597dSJung-uk Kim                                         strlen(keymatexportlabel),
3349b077aed3SPierre Pronchery                                         NULL, 0, 0) <= 0) {
33501f13597dSJung-uk Kim             BIO_printf(bio, "    Error\n");
33516f9291ceSJung-uk Kim         } else {
33521f13597dSJung-uk Kim             BIO_printf(bio, "    Keying material: ");
33531f13597dSJung-uk Kim             for (i = 0; i < keymatexportlen; i++)
33546f9291ceSJung-uk Kim                 BIO_printf(bio, "%02X", exportedkeymat[i]);
33551f13597dSJung-uk Kim             BIO_printf(bio, "\n");
33561f13597dSJung-uk Kim         }
33571f13597dSJung-uk Kim         OPENSSL_free(exportedkeymat);
33581f13597dSJung-uk Kim     }
335974664626SKris Kennaway     BIO_printf(bio, "---\n");
3360a21b1b38SKris Kennaway     /* flush, or debugging output gets mixed with http response */
3361db522d3aSSimon L. B. Nielsen     (void)BIO_flush(bio);
336274664626SKris Kennaway }
336374664626SKris Kennaway 
3364e71b7053SJung-uk Kim # ifndef OPENSSL_NO_OCSP
ocsp_resp_cb(SSL * s,void * arg)3365db522d3aSSimon L. B. Nielsen static int ocsp_resp_cb(SSL *s, void *arg)
3366db522d3aSSimon L. B. Nielsen {
3367db522d3aSSimon L. B. Nielsen     const unsigned char *p;
3368db522d3aSSimon L. B. Nielsen     int len;
3369db522d3aSSimon L. B. Nielsen     OCSP_RESPONSE *rsp;
3370db522d3aSSimon L. B. Nielsen     len = SSL_get_tlsext_status_ocsp_resp(s, &p);
3371db522d3aSSimon L. B. Nielsen     BIO_puts(arg, "OCSP response: ");
3372e71b7053SJung-uk Kim     if (p == NULL) {
3373db522d3aSSimon L. B. Nielsen         BIO_puts(arg, "no response sent\n");
3374db522d3aSSimon L. B. Nielsen         return 1;
3375db522d3aSSimon L. B. Nielsen     }
3376db522d3aSSimon L. B. Nielsen     rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
3377e71b7053SJung-uk Kim     if (rsp == NULL) {
3378db522d3aSSimon L. B. Nielsen         BIO_puts(arg, "response parse error\n");
3379db522d3aSSimon L. B. Nielsen         BIO_dump_indent(arg, (char *)p, len, 4);
3380db522d3aSSimon L. B. Nielsen         return 0;
3381db522d3aSSimon L. B. Nielsen     }
3382db522d3aSSimon L. B. Nielsen     BIO_puts(arg, "\n======================================\n");
3383db522d3aSSimon L. B. Nielsen     OCSP_RESPONSE_print(arg, rsp, 0);
3384db522d3aSSimon L. B. Nielsen     BIO_puts(arg, "======================================\n");
3385db522d3aSSimon L. B. Nielsen     OCSP_RESPONSE_free(rsp);
3386db522d3aSSimon L. B. Nielsen     return 1;
3387db522d3aSSimon L. B. Nielsen }
33881f13597dSJung-uk Kim # endif
3389e71b7053SJung-uk Kim 
ldap_ExtendedResponse_parse(const char * buf,long rem)3390e71b7053SJung-uk Kim static int ldap_ExtendedResponse_parse(const char *buf, long rem)
3391e71b7053SJung-uk Kim {
3392e71b7053SJung-uk Kim     const unsigned char *cur, *end;
3393e71b7053SJung-uk Kim     long len;
3394e71b7053SJung-uk Kim     int tag, xclass, inf, ret = -1;
3395e71b7053SJung-uk Kim 
3396e71b7053SJung-uk Kim     cur = (const unsigned char *)buf;
3397e71b7053SJung-uk Kim     end = cur + rem;
3398e71b7053SJung-uk Kim 
3399e71b7053SJung-uk Kim     /*
3400e71b7053SJung-uk Kim      * From RFC 4511:
3401e71b7053SJung-uk Kim      *
3402e71b7053SJung-uk Kim      *    LDAPMessage ::= SEQUENCE {
3403e71b7053SJung-uk Kim      *         messageID       MessageID,
3404e71b7053SJung-uk Kim      *         protocolOp      CHOICE {
3405e71b7053SJung-uk Kim      *              ...
3406e71b7053SJung-uk Kim      *              extendedResp          ExtendedResponse,
3407e71b7053SJung-uk Kim      *              ... },
3408e71b7053SJung-uk Kim      *         controls       [0] Controls OPTIONAL }
3409e71b7053SJung-uk Kim      *
3410e71b7053SJung-uk Kim      *    ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
3411e71b7053SJung-uk Kim      *         COMPONENTS OF LDAPResult,
3412e71b7053SJung-uk Kim      *         responseName     [10] LDAPOID OPTIONAL,
3413e71b7053SJung-uk Kim      *         responseValue    [11] OCTET STRING OPTIONAL }
3414e71b7053SJung-uk Kim      *
3415e71b7053SJung-uk Kim      *    LDAPResult ::= SEQUENCE {
3416e71b7053SJung-uk Kim      *         resultCode         ENUMERATED {
3417e71b7053SJung-uk Kim      *              success                      (0),
3418e71b7053SJung-uk Kim      *              ...
3419e71b7053SJung-uk Kim      *              other                        (80),
3420e71b7053SJung-uk Kim      *              ...  },
3421e71b7053SJung-uk Kim      *         matchedDN          LDAPDN,
3422e71b7053SJung-uk Kim      *         diagnosticMessage  LDAPString,
3423e71b7053SJung-uk Kim      *         referral           [3] Referral OPTIONAL }
3424e71b7053SJung-uk Kim      */
3425e71b7053SJung-uk Kim 
3426e71b7053SJung-uk Kim     /* pull SEQUENCE */
3427e71b7053SJung-uk Kim     inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3428e71b7053SJung-uk Kim     if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE ||
3429e71b7053SJung-uk Kim         (rem = end - cur, len > rem)) {
3430e71b7053SJung-uk Kim         BIO_printf(bio_err, "Unexpected LDAP response\n");
3431e71b7053SJung-uk Kim         goto end;
3432e71b7053SJung-uk Kim     }
3433e71b7053SJung-uk Kim 
3434e71b7053SJung-uk Kim     rem = len;  /* ensure that we don't overstep the SEQUENCE */
3435e71b7053SJung-uk Kim 
3436e71b7053SJung-uk Kim     /* pull MessageID */
3437e71b7053SJung-uk Kim     inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3438e71b7053SJung-uk Kim     if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_INTEGER ||
3439e71b7053SJung-uk Kim         (rem = end - cur, len > rem)) {
3440e71b7053SJung-uk Kim         BIO_printf(bio_err, "No MessageID\n");
3441e71b7053SJung-uk Kim         goto end;
3442e71b7053SJung-uk Kim     }
3443e71b7053SJung-uk Kim 
3444e71b7053SJung-uk Kim     cur += len; /* shall we check for MessageId match or just skip? */
3445e71b7053SJung-uk Kim 
3446e71b7053SJung-uk Kim     /* pull [APPLICATION 24] */
3447e71b7053SJung-uk Kim     rem = end - cur;
3448e71b7053SJung-uk Kim     inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3449e71b7053SJung-uk Kim     if (inf != V_ASN1_CONSTRUCTED || xclass != V_ASN1_APPLICATION ||
3450e71b7053SJung-uk Kim         tag != 24) {
3451e71b7053SJung-uk Kim         BIO_printf(bio_err, "Not ExtendedResponse\n");
3452e71b7053SJung-uk Kim         goto end;
3453e71b7053SJung-uk Kim     }
3454e71b7053SJung-uk Kim 
3455e71b7053SJung-uk Kim     /* pull resultCode */
3456e71b7053SJung-uk Kim     rem = end - cur;
3457e71b7053SJung-uk Kim     inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
3458e71b7053SJung-uk Kim     if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_ENUMERATED || len == 0 ||
3459e71b7053SJung-uk Kim         (rem = end - cur, len > rem)) {
3460e71b7053SJung-uk Kim         BIO_printf(bio_err, "Not LDAPResult\n");
3461e71b7053SJung-uk Kim         goto end;
3462e71b7053SJung-uk Kim     }
3463e71b7053SJung-uk Kim 
3464e71b7053SJung-uk Kim     /* len should always be one, but just in case... */
3465e71b7053SJung-uk Kim     for (ret = 0, inf = 0; inf < len; inf++) {
3466e71b7053SJung-uk Kim         ret <<= 8;
3467e71b7053SJung-uk Kim         ret |= cur[inf];
3468e71b7053SJung-uk Kim     }
3469e71b7053SJung-uk Kim     /* There is more data, but we don't care... */
3470e71b7053SJung-uk Kim  end:
3471e71b7053SJung-uk Kim     return ret;
3472e71b7053SJung-uk Kim }
3473e71b7053SJung-uk Kim 
34746935a639SJung-uk Kim /*
34756935a639SJung-uk Kim  * Host dNS Name verifier: used for checking that the hostname is in dNS format
34766935a639SJung-uk Kim  * before setting it as SNI
34776935a639SJung-uk Kim  */
is_dNS_name(const char * host)34786935a639SJung-uk Kim static int is_dNS_name(const char *host)
34796935a639SJung-uk Kim {
34806935a639SJung-uk Kim     const size_t MAX_LABEL_LENGTH = 63;
34816935a639SJung-uk Kim     size_t i;
34826935a639SJung-uk Kim     int isdnsname = 0;
34836935a639SJung-uk Kim     size_t length = strlen(host);
34846935a639SJung-uk Kim     size_t label_length = 0;
34856935a639SJung-uk Kim     int all_numeric = 1;
34866935a639SJung-uk Kim 
34876935a639SJung-uk Kim     /*
34886935a639SJung-uk Kim      * Deviation from strict DNS name syntax, also check names with '_'
34896935a639SJung-uk Kim      * Check DNS name syntax, any '-' or '.' must be internal,
34906935a639SJung-uk Kim      * and on either side of each '.' we can't have a '-' or '.'.
34916935a639SJung-uk Kim      *
34926935a639SJung-uk Kim      * If the name has just one label, we don't consider it a DNS name.
34936935a639SJung-uk Kim      */
34946935a639SJung-uk Kim     for (i = 0; i < length && label_length < MAX_LABEL_LENGTH; ++i) {
34956935a639SJung-uk Kim         char c = host[i];
34966935a639SJung-uk Kim 
34976935a639SJung-uk Kim         if ((c >= 'a' && c <= 'z')
34986935a639SJung-uk Kim             || (c >= 'A' && c <= 'Z')
34996935a639SJung-uk Kim             || c == '_') {
35006935a639SJung-uk Kim             label_length += 1;
35016935a639SJung-uk Kim             all_numeric = 0;
35026935a639SJung-uk Kim             continue;
35036935a639SJung-uk Kim         }
35046935a639SJung-uk Kim 
35056935a639SJung-uk Kim         if (c >= '0' && c <= '9') {
35066935a639SJung-uk Kim             label_length += 1;
35076935a639SJung-uk Kim             continue;
35086935a639SJung-uk Kim         }
35096935a639SJung-uk Kim 
35106935a639SJung-uk Kim         /* Dot and hyphen cannot be first or last. */
35116935a639SJung-uk Kim         if (i > 0 && i < length - 1) {
35126935a639SJung-uk Kim             if (c == '-') {
35136935a639SJung-uk Kim                 label_length += 1;
35146935a639SJung-uk Kim                 continue;
35156935a639SJung-uk Kim             }
35166935a639SJung-uk Kim             /*
35176935a639SJung-uk Kim              * Next to a dot the preceding and following characters must not be
35186935a639SJung-uk Kim              * another dot or a hyphen.  Otherwise, record that the name is
35196935a639SJung-uk Kim              * plausible, since it has two or more labels.
35206935a639SJung-uk Kim              */
35216935a639SJung-uk Kim             if (c == '.'
35226935a639SJung-uk Kim                 && host[i + 1] != '.'
35236935a639SJung-uk Kim                 && host[i - 1] != '-'
35246935a639SJung-uk Kim                 && host[i + 1] != '-') {
35256935a639SJung-uk Kim                 label_length = 0;
35266935a639SJung-uk Kim                 isdnsname = 1;
35276935a639SJung-uk Kim                 continue;
35286935a639SJung-uk Kim             }
35296935a639SJung-uk Kim         }
35306935a639SJung-uk Kim         isdnsname = 0;
35316935a639SJung-uk Kim         break;
35326935a639SJung-uk Kim     }
35336935a639SJung-uk Kim 
35346935a639SJung-uk Kim     /* dNS name must not be all numeric and labels must be shorter than 64 characters. */
35356935a639SJung-uk Kim     isdnsname &= !all_numeric && !(label_length == MAX_LABEL_LENGTH);
35366935a639SJung-uk Kim 
35376935a639SJung-uk Kim     return isdnsname;
35386935a639SJung-uk Kim }
3539e71b7053SJung-uk Kim #endif                          /* OPENSSL_NO_SOCK */
3540