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