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