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