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