VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/test/ssl_old_test.c@ 102334

最後變更 在這個檔案從102334是 101211,由 vboxsync 提交於 17 月 前

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

檔案大小: 98.8 KB
 
1/*
2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
5 *
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
10 */
11
12#include "internal/e_os.h"
13
14/* Or gethostname won't be declared properly on Linux and GNU platforms. */
15#ifndef _BSD_SOURCE
16# define _BSD_SOURCE 1
17#endif
18#ifndef _DEFAULT_SOURCE
19# define _DEFAULT_SOURCE 1
20#endif
21
22#include <assert.h>
23#include <errno.h>
24#include <limits.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
29
30#include "internal/nelem.h"
31
32#ifdef OPENSSL_SYS_VMS
33/*
34 * Or isascii won't be declared properly on VMS (at least with DECompHP C).
35 */
36# define _XOPEN_SOURCE 500
37#endif
38
39#include <ctype.h>
40
41#include <openssl/bio.h>
42#include <openssl/crypto.h>
43#include <openssl/evp.h>
44#include <openssl/x509.h>
45#include <openssl/x509v3.h>
46#include <openssl/ssl.h>
47#include <openssl/err.h>
48#include <openssl/rand.h>
49#include <openssl/rsa.h>
50#ifndef OPENSSL_NO_DSA
51# include <openssl/dsa.h>
52#endif
53#include <openssl/bn.h>
54#ifndef OPENSSL_NO_CT
55# include <openssl/ct.h>
56#endif
57#include <openssl/provider.h>
58#include "testutil.h"
59
60/*
61 * Or gethostname won't be declared properly
62 * on Compaq platforms (at least with DEC C).
63 * Do not try to put it earlier, or IPv6 includes
64 * get screwed...
65 */
66#define _XOPEN_SOURCE_EXTENDED 1
67
68#ifdef OPENSSL_SYS_WINDOWS
69# include <winsock.h>
70#else
71# include <unistd.h>
72#endif
73
74#include "helpers/predefined_dhparams.h"
75
76static SSL_CTX *s_ctx = NULL;
77static SSL_CTX *s_ctx2 = NULL;
78
79/*
80 * There is really no standard for this, so let's assign something
81 * only for this test
82 */
83#define COMP_ZLIB 1
84
85static int verify_callback(int ok, X509_STORE_CTX *ctx);
86static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
87#define APP_CALLBACK_STRING "Test Callback Argument"
88struct app_verify_arg {
89 char *string;
90 int app_verify;
91};
92
93static char *psk_key = NULL; /* by default PSK is not used */
94#ifndef OPENSSL_NO_PSK
95static unsigned int psk_client_callback(SSL *ssl, const char *hint,
96 char *identity,
97 unsigned int max_identity_len,
98 unsigned char *psk,
99 unsigned int max_psk_len);
100static unsigned int psk_server_callback(SSL *ssl, const char *identity,
101 unsigned char *psk,
102 unsigned int max_psk_len);
103#endif
104
105static BIO *bio_stdout = NULL;
106
107#ifndef OPENSSL_NO_NEXTPROTONEG
108/* Note that this code assumes that this is only a one element list: */
109static const char NEXT_PROTO_STRING[] = "\x09testproto";
110static int npn_client = 0;
111static int npn_server = 0;
112static int npn_server_reject = 0;
113
114static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen,
115 const unsigned char *in, unsigned int inlen,
116 void *arg)
117{
118 /*
119 * This callback only returns the protocol string, rather than a length
120 * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
121 * and remove the first byte to chop off the length prefix.
122 */
123 *out = (unsigned char *)NEXT_PROTO_STRING + 1;
124 *outlen = sizeof(NEXT_PROTO_STRING) - 2;
125 return SSL_TLSEXT_ERR_OK;
126}
127
128static int cb_server_npn(SSL *s, const unsigned char **data,
129 unsigned int *len, void *arg)
130{
131 *data = (const unsigned char *)NEXT_PROTO_STRING;
132 *len = sizeof(NEXT_PROTO_STRING) - 1;
133 return SSL_TLSEXT_ERR_OK;
134}
135
136static int cb_server_rejects_npn(SSL *s, const unsigned char **data,
137 unsigned int *len, void *arg)
138{
139 return SSL_TLSEXT_ERR_NOACK;
140}
141
142static int verify_npn(SSL *client, SSL *server)
143{
144 const unsigned char *client_s;
145 unsigned client_len;
146 const unsigned char *server_s;
147 unsigned server_len;
148
149 SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
150 SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
151
152 if (client_len) {
153 BIO_printf(bio_stdout, "Client NPN: ");
154 BIO_write(bio_stdout, client_s, client_len);
155 BIO_printf(bio_stdout, "\n");
156 }
157
158 if (server_len) {
159 BIO_printf(bio_stdout, "Server NPN: ");
160 BIO_write(bio_stdout, server_s, server_len);
161 BIO_printf(bio_stdout, "\n");
162 }
163
164 /*
165 * If an NPN string was returned, it must be the protocol that we
166 * expected to negotiate.
167 */
168 if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 ||
169 memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
170 return -1;
171 if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 ||
172 memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
173 return -1;
174
175 if (!npn_client && client_len)
176 return -1;
177 if (!npn_server && server_len)
178 return -1;
179 if (npn_server_reject && server_len)
180 return -1;
181 if (npn_client && npn_server && (!client_len || !server_len))
182 return -1;
183
184 return 0;
185}
186#endif
187
188static const char *alpn_client;
189static char *alpn_server;
190static char *alpn_server2;
191static const char *alpn_expected;
192static unsigned char *alpn_selected;
193static const char *server_min_proto;
194static const char *server_max_proto;
195static const char *client_min_proto;
196static const char *client_max_proto;
197static const char *should_negotiate;
198static const char *sn_client;
199static const char *sn_server1;
200static const char *sn_server2;
201static int sn_expect = 0;
202static const char *server_sess_out;
203static const char *server_sess_in;
204static const char *client_sess_out;
205static const char *client_sess_in;
206static SSL_SESSION *server_sess;
207static SSL_SESSION *client_sess;
208
209static int servername_cb(SSL *s, int *ad, void *arg)
210{
211 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
212 if (sn_server2 == NULL) {
213 BIO_printf(bio_stdout, "Servername 2 is NULL\n");
214 return SSL_TLSEXT_ERR_NOACK;
215 }
216
217 if (servername) {
218 if (s_ctx2 != NULL && sn_server2 != NULL &&
219 !OPENSSL_strcasecmp(servername, sn_server2)) {
220 BIO_printf(bio_stdout, "Switching server context.\n");
221 SSL_set_SSL_CTX(s, s_ctx2);
222 }
223 }
224 return SSL_TLSEXT_ERR_OK;
225}
226static int verify_servername(SSL *client, SSL *server)
227{
228 /* just need to see if sn_context is what we expect */
229 SSL_CTX* ctx = SSL_get_SSL_CTX(server);
230 if (sn_expect == 0)
231 return 0;
232 if (sn_expect == 1 && ctx == s_ctx)
233 return 0;
234 if (sn_expect == 2 && ctx == s_ctx2)
235 return 0;
236 BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect);
237 if (ctx == s_ctx2)
238 BIO_printf(bio_stdout, "Servername: context is 2\n");
239 else if (ctx == s_ctx)
240 BIO_printf(bio_stdout, "Servername: context is 1\n");
241 else
242 BIO_printf(bio_stdout, "Servername: context is unknown\n");
243 return -1;
244}
245
246
247/*-
248 * next_protos_parse parses a comma separated list of strings into a string
249 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
250 * outlen: (output) set to the length of the resulting buffer on success.
251 * in: a NUL terminated string like "abc,def,ghi"
252 *
253 * returns: a malloced buffer or NULL on failure.
254 */
255static unsigned char *next_protos_parse(size_t *outlen,
256 const char *in)
257{
258 size_t len;
259 unsigned char *out;
260 size_t i, start = 0;
261
262 len = strlen(in);
263 if (len >= 65535)
264 return NULL;
265
266 out = OPENSSL_malloc(strlen(in) + 1);
267 if (!out)
268 return NULL;
269
270 for (i = 0; i <= len; ++i) {
271 if (i == len || in[i] == ',') {
272 if (i - start > 255) {
273 OPENSSL_free(out);
274 return NULL;
275 }
276 out[start] = (unsigned char)(i - start);
277 start = i + 1;
278 } else
279 out[i + 1] = in[i];
280 }
281
282 *outlen = len + 1;
283 return out;
284}
285
286static int cb_server_alpn(SSL *s, const unsigned char **out,
287 unsigned char *outlen, const unsigned char *in,
288 unsigned int inlen, void *arg)
289{
290 unsigned char *protos;
291 size_t protos_len;
292 char* alpn_str = arg;
293
294 protos = next_protos_parse(&protos_len, alpn_str);
295 if (protos == NULL) {
296 fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
297 alpn_str);
298 abort();
299 }
300
301 if (SSL_select_next_proto
302 ((unsigned char **)out, outlen, protos, protos_len, in,
303 inlen) != OPENSSL_NPN_NEGOTIATED) {
304 OPENSSL_free(protos);
305 return SSL_TLSEXT_ERR_NOACK;
306 }
307
308 /*
309 * Make a copy of the selected protocol which will be freed in
310 * verify_alpn.
311 */
312 alpn_selected = OPENSSL_malloc(*outlen);
313 if (alpn_selected == NULL) {
314 fprintf(stderr, "failed to allocate memory\n");
315 OPENSSL_free(protos);
316 abort();
317 }
318 memcpy(alpn_selected, *out, *outlen);
319 *out = alpn_selected;
320
321 OPENSSL_free(protos);
322 return SSL_TLSEXT_ERR_OK;
323}
324
325static int verify_alpn(SSL *client, SSL *server)
326{
327 const unsigned char *client_proto, *server_proto;
328 unsigned int client_proto_len = 0, server_proto_len = 0;
329 SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
330 SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
331
332 OPENSSL_free(alpn_selected);
333 alpn_selected = NULL;
334
335 if (client_proto_len != server_proto_len) {
336 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
337 goto err;
338 }
339
340 if (client_proto != NULL &&
341 memcmp(client_proto, server_proto, client_proto_len) != 0) {
342 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
343 goto err;
344 }
345
346 if (client_proto_len > 0 && alpn_expected == NULL) {
347 BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
348 goto err;
349 }
350
351 if (alpn_expected != NULL &&
352 (client_proto_len != strlen(alpn_expected) ||
353 memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
354 BIO_printf(bio_stdout,
355 "ALPN selected protocols not equal to expected protocol: %s\n",
356 alpn_expected);
357 goto err;
358 }
359
360 return 0;
361
362 err:
363 BIO_printf(bio_stdout, "ALPN results: client: '");
364 BIO_write(bio_stdout, client_proto, client_proto_len);
365 BIO_printf(bio_stdout, "', server: '");
366 BIO_write(bio_stdout, server_proto, server_proto_len);
367 BIO_printf(bio_stdout, "'\n");
368 BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '",
369 alpn_client);
370 if (SSL_get_SSL_CTX(server) == s_ctx2) {
371 BIO_printf(bio_stdout, "%s'\n",
372 alpn_server2);
373 } else {
374 BIO_printf(bio_stdout, "%s'\n",
375 alpn_server);
376 }
377 return -1;
378}
379
380/*
381 * WARNING : below extension types are *NOT* IETF assigned, and could
382 * conflict if these types are reassigned and handled specially by OpenSSL
383 * in the future
384 */
385#define TACK_EXT_TYPE 62208
386#define CUSTOM_EXT_TYPE_0 1000
387#define CUSTOM_EXT_TYPE_1 1001
388#define CUSTOM_EXT_TYPE_2 1002
389#define CUSTOM_EXT_TYPE_3 1003
390
391static const char custom_ext_cli_string[] = "abc";
392static const char custom_ext_srv_string[] = "defg";
393
394/* These set from cmdline */
395static char *serverinfo_file = NULL;
396static int serverinfo_sct = 0;
397static int serverinfo_tack = 0;
398
399/* These set based on extension callbacks */
400static int serverinfo_sct_seen = 0;
401static int serverinfo_tack_seen = 0;
402static int serverinfo_other_seen = 0;
403
404/* This set from cmdline */
405static int custom_ext = 0;
406
407/* This set based on extension callbacks */
408static int custom_ext_error = 0;
409
410static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
411 const unsigned char *in, size_t inlen,
412 int *al, void *arg)
413{
414 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp)
415 serverinfo_sct_seen++;
416 else if (ext_type == TACK_EXT_TYPE)
417 serverinfo_tack_seen++;
418 else
419 serverinfo_other_seen++;
420 return 1;
421}
422
423static int verify_serverinfo(void)
424{
425 if (serverinfo_sct != serverinfo_sct_seen)
426 return -1;
427 if (serverinfo_tack != serverinfo_tack_seen)
428 return -1;
429 if (serverinfo_other_seen)
430 return -1;
431 return 0;
432}
433
434/*-
435 * Four test cases for custom extensions:
436 * 0 - no ClientHello extension or ServerHello response
437 * 1 - ClientHello with "abc", no response
438 * 2 - ClientHello with "abc", empty response
439 * 3 - ClientHello with "abc", "defg" response
440 */
441
442static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
443 const unsigned char **out,
444 size_t *outlen, int *al, void *arg)
445{
446 if (ext_type != CUSTOM_EXT_TYPE_0)
447 custom_ext_error = 1;
448 return 0; /* Don't send an extension */
449}
450
451static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
452 const unsigned char *in,
453 size_t inlen, int *al, void *arg)
454{
455 return 1;
456}
457
458static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
459 const unsigned char **out,
460 size_t *outlen, int *al, void *arg)
461{
462 if (ext_type != CUSTOM_EXT_TYPE_1)
463 custom_ext_error = 1;
464 *out = (const unsigned char *)custom_ext_cli_string;
465 *outlen = strlen(custom_ext_cli_string);
466 return 1; /* Send "abc" */
467}
468
469static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
470 const unsigned char *in,
471 size_t inlen, int *al, void *arg)
472{
473 return 1;
474}
475
476static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
477 const unsigned char **out,
478 size_t *outlen, int *al, void *arg)
479{
480 if (ext_type != CUSTOM_EXT_TYPE_2)
481 custom_ext_error = 1;
482 *out = (const unsigned char *)custom_ext_cli_string;
483 *outlen = strlen(custom_ext_cli_string);
484 return 1; /* Send "abc" */
485}
486
487static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
488 const unsigned char *in,
489 size_t inlen, int *al, void *arg)
490{
491 if (ext_type != CUSTOM_EXT_TYPE_2)
492 custom_ext_error = 1;
493 if (inlen != 0)
494 custom_ext_error = 1; /* Should be empty response */
495 return 1;
496}
497
498static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
499 const unsigned char **out,
500 size_t *outlen, int *al, void *arg)
501{
502 if (ext_type != CUSTOM_EXT_TYPE_3)
503 custom_ext_error = 1;
504 *out = (const unsigned char *)custom_ext_cli_string;
505 *outlen = strlen(custom_ext_cli_string);
506 return 1; /* Send "abc" */
507}
508
509static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
510 const unsigned char *in,
511 size_t inlen, int *al, void *arg)
512{
513 if (ext_type != CUSTOM_EXT_TYPE_3)
514 custom_ext_error = 1;
515 if (inlen != strlen(custom_ext_srv_string))
516 custom_ext_error = 1;
517 if (memcmp(custom_ext_srv_string, in, inlen) != 0)
518 custom_ext_error = 1; /* Check for "defg" */
519 return 1;
520}
521
522/*
523 * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
524 * for this extension
525 */
526static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
527 const unsigned char *in,
528 size_t inlen, int *al, void *arg)
529{
530 custom_ext_error = 1;
531 return 1;
532}
533
534/* 'add' callbacks are only called if the 'parse' callback is called */
535static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
536 const unsigned char **out,
537 size_t *outlen, int *al, void *arg)
538{
539 /* Error: should not have been called */
540 custom_ext_error = 1;
541 return 0; /* Don't send an extension */
542}
543
544static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
545 const unsigned char *in,
546 size_t inlen, int *al, void *arg)
547{
548 if (ext_type != CUSTOM_EXT_TYPE_1)
549 custom_ext_error = 1;
550 /* Check for "abc" */
551 if (inlen != strlen(custom_ext_cli_string))
552 custom_ext_error = 1;
553 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
554 custom_ext_error = 1;
555 return 1;
556}
557
558static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
559 const unsigned char **out,
560 size_t *outlen, int *al, void *arg)
561{
562 return 0; /* Don't send an extension */
563}
564
565static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
566 const unsigned char *in,
567 size_t inlen, int *al, void *arg)
568{
569 if (ext_type != CUSTOM_EXT_TYPE_2)
570 custom_ext_error = 1;
571 /* Check for "abc" */
572 if (inlen != strlen(custom_ext_cli_string))
573 custom_ext_error = 1;
574 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
575 custom_ext_error = 1;
576 return 1;
577}
578
579static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
580 const unsigned char **out,
581 size_t *outlen, int *al, void *arg)
582{
583 *out = NULL;
584 *outlen = 0;
585 return 1; /* Send empty extension */
586}
587
588static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
589 const unsigned char *in,
590 size_t inlen, int *al, void *arg)
591{
592 if (ext_type != CUSTOM_EXT_TYPE_3)
593 custom_ext_error = 1;
594 /* Check for "abc" */
595 if (inlen != strlen(custom_ext_cli_string))
596 custom_ext_error = 1;
597 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
598 custom_ext_error = 1;
599 return 1;
600}
601
602static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
603 const unsigned char **out,
604 size_t *outlen, int *al, void *arg)
605{
606 *out = (const unsigned char *)custom_ext_srv_string;
607 *outlen = strlen(custom_ext_srv_string);
608 return 1; /* Send "defg" */
609}
610
611static char *cipher = NULL;
612static char *ciphersuites = NULL;
613static int verbose = 0;
614static int debug = 0;
615
616int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
617 long bytes, clock_t *s_time, clock_t *c_time);
618int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
619 clock_t *c_time);
620int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
621
622static void sv_usage(void)
623{
624 fprintf(stderr, "usage: ssltest [args ...]\n");
625 fprintf(stderr, "\n");
626 fprintf(stderr, " -server_auth - check server certificate\n");
627 fprintf(stderr, " -client_auth - do client authentication\n");
628 fprintf(stderr, " -v - more output\n");
629 fprintf(stderr, " -d - debug output\n");
630 fprintf(stderr, " -reuse - use session-id reuse\n");
631 fprintf(stderr, " -num <val> - number of connections to perform\n");
632 fprintf(stderr,
633 " -bytes <val> - number of bytes to swap between client/server\n");
634#ifndef OPENSSL_NO_DH
635 fprintf(stderr,
636 " -dhe512 - use 512 bit key for DHE (to test failure)\n");
637 fprintf(stderr,
638 " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
639 fprintf(stderr,
640 " -dhe2048 - use 2048 bit key (safe prime) for DHE (default, no-op)\n");
641 fprintf(stderr,
642 " -dhe4096 - use 4096 bit key (safe prime) for DHE\n");
643#endif
644 fprintf(stderr, " -no_dhe - disable DHE\n");
645#ifndef OPENSSL_NO_EC
646 fprintf(stderr, " -no_ecdhe - disable ECDHE\n");
647#endif
648#ifndef OPENSSL_NO_PSK
649 fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n");
650#endif
651#ifndef OPENSSL_NO_SSL3
652 fprintf(stderr, " -ssl3 - use SSLv3\n");
653#endif
654#ifndef OPENSSL_NO_TLS1
655 fprintf(stderr, " -tls1 - use TLSv1\n");
656#endif
657#ifndef OPENSSL_NO_TLS1_1
658 fprintf(stderr, " -tls1_1 - use TLSv1.1\n");
659#endif
660#ifndef OPENSSL_NO_TLS1_2
661 fprintf(stderr, " -tls1_2 - use TLSv1.2\n");
662#endif
663#ifndef OPENSSL_NO_DTLS
664 fprintf(stderr, " -dtls - use DTLS\n");
665#ifndef OPENSSL_NO_DTLS1
666 fprintf(stderr, " -dtls1 - use DTLSv1\n");
667#endif
668#ifndef OPENSSL_NO_DTLS1_2
669 fprintf(stderr, " -dtls12 - use DTLSv1.2\n");
670#endif
671#endif
672 fprintf(stderr, " -CApath arg - PEM format directory of CA's\n");
673 fprintf(stderr, " -CAfile arg - PEM format file of CA's\n");
674 fprintf(stderr, " -s_cert arg - Server certificate file\n");
675 fprintf(stderr,
676 " -s_key arg - Server key file (default: same as -cert)\n");
677 fprintf(stderr, " -c_cert arg - Client certificate file\n");
678 fprintf(stderr,
679 " -c_key arg - Client key file (default: same as -c_cert)\n");
680 fprintf(stderr, " -cipher arg - The TLSv1.2 and below cipher list\n");
681 fprintf(stderr, " -ciphersuites arg - The TLSv1.3 ciphersuites\n");
682 fprintf(stderr, " -bio_pair - Use BIO pairs\n");
683 fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n");
684 fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n");
685 fprintf(stderr, " -f - Test even cases that can't work\n");
686 fprintf(stderr,
687 " -time - measure processor time used by client and server\n");
688 fprintf(stderr, " -zlib - use zlib compression\n");
689#ifndef OPENSSL_NO_NEXTPROTONEG
690 fprintf(stderr, " -npn_client - have client side offer NPN\n");
691 fprintf(stderr, " -npn_server - have server side offer NPN\n");
692 fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
693#endif
694 fprintf(stderr, " -serverinfo_file file - have server use this file\n");
695 fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n");
696 fprintf(stderr,
697 " -serverinfo_tack - have client offer and expect TACK\n");
698 fprintf(stderr,
699 " -custom_ext - try various custom extension callbacks\n");
700 fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
701 fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
702 fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n");
703 fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
704 fprintf(stderr,
705 " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
706 fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
707 fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
708 fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
709 fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
710 fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
711#ifndef OPENSSL_NO_CT
712 fprintf(stderr, " -noct - no certificate transparency\n");
713 fprintf(stderr, " -requestct - request certificate transparency\n");
714 fprintf(stderr, " -requirect - require certificate transparency\n");
715#endif
716 fprintf(stderr, " -sn_client <string> - have client request this servername\n");
717 fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n");
718 fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n");
719 fprintf(stderr, " -sn_expect1 - expected server 1\n");
720 fprintf(stderr, " -sn_expect2 - expected server 2\n");
721 fprintf(stderr, " -server_sess_out <file> - Save the server session to a file\n");
722 fprintf(stderr, " -server_sess_in <file> - Read the server session from a file\n");
723 fprintf(stderr, " -client_sess_out <file> - Save the client session to a file\n");
724 fprintf(stderr, " -client_sess_in <file> - Read the client session from a file\n");
725 fprintf(stderr, " -should_reuse <number> - The expected state of reusing the session\n");
726 fprintf(stderr, " -no_ticket - do not issue TLS session ticket\n");
727 fprintf(stderr, " -client_ktls - try to enable client KTLS\n");
728 fprintf(stderr, " -server_ktls - try to enable server KTLS\n");
729 fprintf(stderr, " -provider <name> - Load the given provider into the library context\n");
730 fprintf(stderr, " -config <cnf> - Load the given config file into the library context\n");
731}
732
733static void print_key_details(BIO *out, EVP_PKEY *key)
734{
735 int keyid = EVP_PKEY_get_id(key);
736
737#ifndef OPENSSL_NO_EC
738 if (keyid == EVP_PKEY_EC) {
739 char group[80];
740 size_t size;
741
742 if (!EVP_PKEY_get_group_name(key, group, sizeof(group), &size))
743 strcpy(group, "unknown group");
744 BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_get_bits(key), group);
745 } else
746#endif
747 {
748 const char *algname;
749 switch (keyid) {
750 case EVP_PKEY_RSA:
751 algname = "RSA";
752 break;
753 case EVP_PKEY_DSA:
754 algname = "DSA";
755 break;
756 case EVP_PKEY_DH:
757 algname = "DH";
758 break;
759 default:
760 algname = OBJ_nid2sn(keyid);
761 break;
762 }
763 BIO_printf(out, "%d bits %s", EVP_PKEY_get_bits(key), algname);
764 }
765}
766
767static void print_details(SSL *c_ssl, const char *prefix)
768{
769 const SSL_CIPHER *ciph;
770 int mdnid;
771 X509 *cert;
772 EVP_PKEY *pkey;
773
774 ciph = SSL_get_current_cipher(c_ssl);
775 BIO_printf(bio_stdout, "%s%s, cipher %s %s",
776 prefix,
777 SSL_get_version(c_ssl),
778 SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
779 cert = SSL_get0_peer_certificate(c_ssl);
780 if (cert != NULL) {
781 EVP_PKEY* pubkey = X509_get0_pubkey(cert);
782
783 if (pubkey != NULL) {
784 BIO_puts(bio_stdout, ", ");
785 print_key_details(bio_stdout, pubkey);
786 }
787 }
788 if (SSL_get_peer_tmp_key(c_ssl, &pkey)) {
789 BIO_puts(bio_stdout, ", temp key: ");
790 print_key_details(bio_stdout, pkey);
791 EVP_PKEY_free(pkey);
792 }
793 if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
794 BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
795 BIO_printf(bio_stdout, "\n");
796}
797
798/*
799 * protocol_from_string - converts a protocol version string to a number
800 *
801 * Returns -1 on failure or the version on success
802 */
803static int protocol_from_string(const char *value)
804{
805 struct protocol_versions {
806 const char *name;
807 int version;
808 };
809 static const struct protocol_versions versions[] = {
810 {"ssl3", SSL3_VERSION},
811 {"tls1", TLS1_VERSION},
812 {"tls1.1", TLS1_1_VERSION},
813 {"tls1.2", TLS1_2_VERSION},
814 {"tls1.3", TLS1_3_VERSION},
815 {"dtls1", DTLS1_VERSION},
816 {"dtls1.2", DTLS1_2_VERSION}};
817 size_t i;
818 size_t n = OSSL_NELEM(versions);
819
820 for (i = 0; i < n; i++)
821 if (strcmp(versions[i].name, value) == 0)
822 return versions[i].version;
823 return -1;
824}
825
826static SSL_SESSION *read_session(const char *filename)
827{
828 SSL_SESSION *sess;
829 BIO *f = BIO_new_file(filename, "r");
830
831 if (f == NULL) {
832 BIO_printf(bio_err, "Can't open session file %s\n", filename);
833 ERR_print_errors(bio_err);
834 return NULL;
835 }
836 sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL);
837 if (sess == NULL) {
838 BIO_printf(bio_err, "Can't parse session file %s\n", filename);
839 ERR_print_errors(bio_err);
840 }
841 BIO_free(f);
842 return sess;
843}
844
845static int write_session(const char *filename, SSL_SESSION *sess)
846{
847 BIO *f;
848
849 if (sess == NULL) {
850 BIO_printf(bio_err, "No session information\n");
851 return 0;
852 }
853
854 f = BIO_new_file(filename, "w");
855 if (f == NULL) {
856 BIO_printf(bio_err, "Can't open session file %s\n", filename);
857 ERR_print_errors(bio_err);
858 return 0;
859 }
860 PEM_write_bio_SSL_SESSION(f, sess);
861 BIO_free(f);
862 return 1;
863}
864
865/*
866 * set_protocol_version - Sets protocol version minimum or maximum
867 *
868 * Returns 0 on failure and 1 on success
869 */
870static int set_protocol_version(const char *version, SSL *ssl, int setting)
871{
872 if (version != NULL) {
873 int ver = protocol_from_string(version);
874 if (ver < 0) {
875 BIO_printf(bio_err, "Error parsing: %s\n", version);
876 return 0;
877 }
878 return SSL_ctrl(ssl, setting, ver, NULL);
879 }
880 return 1;
881}
882
883int main(int argc, char *argv[])
884{
885 const char *CApath = NULL, *CAfile = NULL;
886 int badop = 0;
887 enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM;
888 int force = 0;
889 int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_1 = 0, tls1_2 = 0, ssl3 = 0;
890 int ret = EXIT_FAILURE;
891 int client_auth = 0;
892 int server_auth = 0, i;
893 struct app_verify_arg app_verify_arg =
894 { APP_CALLBACK_STRING, 0 };
895 SSL_CTX *c_ctx = NULL;
896 const SSL_METHOD *meth = NULL;
897 SSL *c_ssl, *s_ssl;
898 int number = 1, reuse = 0;
899 int should_reuse = -1;
900 int no_ticket = 0;
901 int client_ktls = 0, server_ktls = 0;
902 long bytes = 256L;
903#ifndef OPENSSL_NO_DH
904 EVP_PKEY *dhpkey;
905 int dhe512 = 0, dhe1024dsa = 0, dhe4096 = 0;
906 int no_dhe = 0;
907#endif
908 int no_psk = 0;
909 int print_time = 0;
910 clock_t s_time = 0, c_time = 0;
911#ifndef OPENSSL_NO_COMP
912 int n, comp = 0;
913 COMP_METHOD *cm = NULL;
914 STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
915#endif
916 int no_protocol;
917 int min_version = 0, max_version = 0;
918#ifndef OPENSSL_NO_CT
919 /*
920 * Disable CT validation by default, because it will interfere with
921 * anything using custom extension handlers to deal with SCT extensions.
922 */
923 int ct_validation = 0;
924#endif
925 SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL;
926 STACK_OF(OPENSSL_STRING) *conf_args = NULL;
927 char *arg = NULL, *argn = NULL;
928 const char *provider = NULL, *config = NULL;
929 OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL;
930 OSSL_LIB_CTX *libctx = NULL;
931
932 verbose = 0;
933 debug = 0;
934
935 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
936 bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
937
938 s_cctx = SSL_CONF_CTX_new();
939 s_cctx2 = SSL_CONF_CTX_new();
940 c_cctx = SSL_CONF_CTX_new();
941
942 if (!s_cctx || !c_cctx || !s_cctx2) {
943 ERR_print_errors(bio_err);
944 goto end;
945 }
946
947 SSL_CONF_CTX_set_flags(s_cctx,
948 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
949 SSL_CONF_FLAG_CERTIFICATE |
950 SSL_CONF_FLAG_REQUIRE_PRIVATE);
951 SSL_CONF_CTX_set_flags(s_cctx2,
952 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
953 SSL_CONF_FLAG_CERTIFICATE |
954 SSL_CONF_FLAG_REQUIRE_PRIVATE);
955 if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
956 ERR_print_errors(bio_err);
957 goto end;
958 }
959 if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) {
960 ERR_print_errors(bio_err);
961 goto end;
962 }
963
964 SSL_CONF_CTX_set_flags(c_cctx,
965 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT |
966 SSL_CONF_FLAG_CERTIFICATE |
967 SSL_CONF_FLAG_REQUIRE_PRIVATE);
968 if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
969 ERR_print_errors(bio_err);
970 goto end;
971 }
972
973 argc--;
974 argv++;
975
976 while (argc >= 1) {
977 if (strcmp(*argv, "-F") == 0) {
978 fprintf(stderr,
979 "not compiled with FIPS support, so exiting without running.\n");
980 EXIT(0);
981 } else if (strcmp(*argv, "-server_auth") == 0)
982 server_auth = 1;
983 else if (strcmp(*argv, "-client_auth") == 0)
984 client_auth = 1;
985 else if (strcmp(*argv, "-v") == 0)
986 verbose = 1;
987 else if (strcmp(*argv, "-d") == 0)
988 debug = 1;
989 else if (strcmp(*argv, "-reuse") == 0)
990 reuse = 1;
991 else if (strcmp(*argv, "-no_dhe") == 0)
992#ifdef OPENSSL_NO_DH
993 /* unused in this case */;
994#else
995 no_dhe = 1;
996 else if (strcmp(*argv, "-dhe512") == 0)
997 dhe512 = 1;
998 else if (strcmp(*argv, "-dhe1024dsa") == 0)
999 dhe1024dsa = 1;
1000 else if (strcmp(*argv, "-dhe4096") == 0)
1001 dhe4096 = 1;
1002#endif
1003 else if (strcmp(*argv, "-no_ecdhe") == 0)
1004 /* obsolete */;
1005 else if (strcmp(*argv, "-psk") == 0) {
1006 if (--argc < 1)
1007 goto bad;
1008 psk_key = *(++argv);
1009#ifndef OPENSSL_NO_PSK
1010 if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) {
1011 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
1012 goto bad;
1013 }
1014#else
1015 no_psk = 1;
1016#endif
1017 }
1018 else if (strcmp(*argv, "-tls1_2") == 0) {
1019 tls1_2 = 1;
1020 } else if (strcmp(*argv, "-tls1_1") == 0) {
1021 tls1_1 = 1;
1022 } else if (strcmp(*argv, "-tls1") == 0) {
1023 tls1 = 1;
1024 } else if (strcmp(*argv, "-ssl3") == 0) {
1025 ssl3 = 1;
1026 } else if (strcmp(*argv, "-dtls1") == 0) {
1027 dtls1 = 1;
1028 } else if (strcmp(*argv, "-dtls12") == 0) {
1029 dtls12 = 1;
1030 } else if (strcmp(*argv, "-dtls") == 0) {
1031 dtls = 1;
1032 } else if (strncmp(*argv, "-num", 4) == 0) {
1033 if (--argc < 1)
1034 goto bad;
1035 number = atoi(*(++argv));
1036 if (number == 0)
1037 number = 1;
1038 } else if (strcmp(*argv, "-bytes") == 0) {
1039 if (--argc < 1)
1040 goto bad;
1041 bytes = atol(*(++argv));
1042 if (bytes == 0L)
1043 bytes = 1L;
1044 i = strlen(argv[0]);
1045 if (argv[0][i - 1] == 'k')
1046 bytes *= 1024L;
1047 if (argv[0][i - 1] == 'm')
1048 bytes *= 1024L * 1024L;
1049 } else if (strcmp(*argv, "-cipher") == 0) {
1050 if (--argc < 1)
1051 goto bad;
1052 cipher = *(++argv);
1053 } else if (strcmp(*argv, "-ciphersuites") == 0) {
1054 if (--argc < 1)
1055 goto bad;
1056 ciphersuites = *(++argv);
1057 } else if (strcmp(*argv, "-CApath") == 0) {
1058 if (--argc < 1)
1059 goto bad;
1060 CApath = *(++argv);
1061 } else if (strcmp(*argv, "-CAfile") == 0) {
1062 if (--argc < 1)
1063 goto bad;
1064 CAfile = *(++argv);
1065 } else if (strcmp(*argv, "-bio_pair") == 0) {
1066 bio_type = BIO_PAIR;
1067 }
1068#ifndef OPENSSL_NO_SOCK
1069 else if (strcmp(*argv, "-ipv4") == 0) {
1070 bio_type = BIO_IPV4;
1071 } else if (strcmp(*argv, "-ipv6") == 0) {
1072 bio_type = BIO_IPV6;
1073 }
1074#endif
1075 else if (strcmp(*argv, "-f") == 0) {
1076 force = 1;
1077 } else if (strcmp(*argv, "-time") == 0) {
1078 print_time = 1;
1079 }
1080#ifndef OPENSSL_NO_CT
1081 else if (strcmp(*argv, "-noct") == 0) {
1082 ct_validation = 0;
1083 }
1084 else if (strcmp(*argv, "-ct") == 0) {
1085 ct_validation = 1;
1086 }
1087#endif
1088#ifndef OPENSSL_NO_COMP
1089 else if (strcmp(*argv, "-zlib") == 0) {
1090 comp = COMP_ZLIB;
1091 }
1092#endif
1093 else if (strcmp(*argv, "-app_verify") == 0) {
1094 app_verify_arg.app_verify = 1;
1095 }
1096#ifndef OPENSSL_NO_NEXTPROTONEG
1097 else if (strcmp(*argv, "-npn_client") == 0) {
1098 npn_client = 1;
1099 } else if (strcmp(*argv, "-npn_server") == 0) {
1100 npn_server = 1;
1101 } else if (strcmp(*argv, "-npn_server_reject") == 0) {
1102 npn_server_reject = 1;
1103 }
1104#endif
1105 else if (strcmp(*argv, "-serverinfo_sct") == 0) {
1106 serverinfo_sct = 1;
1107 } else if (strcmp(*argv, "-serverinfo_tack") == 0) {
1108 serverinfo_tack = 1;
1109 } else if (strcmp(*argv, "-serverinfo_file") == 0) {
1110 if (--argc < 1)
1111 goto bad;
1112 serverinfo_file = *(++argv);
1113 } else if (strcmp(*argv, "-custom_ext") == 0) {
1114 custom_ext = 1;
1115 } else if (strcmp(*argv, "-alpn_client") == 0) {
1116 if (--argc < 1)
1117 goto bad;
1118 alpn_client = *(++argv);
1119 } else if (strcmp(*argv, "-alpn_server") == 0 ||
1120 strcmp(*argv, "-alpn_server1") == 0) {
1121 if (--argc < 1)
1122 goto bad;
1123 alpn_server = *(++argv);
1124 } else if (strcmp(*argv, "-alpn_server2") == 0) {
1125 if (--argc < 1)
1126 goto bad;
1127 alpn_server2 = *(++argv);
1128 } else if (strcmp(*argv, "-alpn_expected") == 0) {
1129 if (--argc < 1)
1130 goto bad;
1131 alpn_expected = *(++argv);
1132 } else if (strcmp(*argv, "-server_min_proto") == 0) {
1133 if (--argc < 1)
1134 goto bad;
1135 server_min_proto = *(++argv);
1136 } else if (strcmp(*argv, "-server_max_proto") == 0) {
1137 if (--argc < 1)
1138 goto bad;
1139 server_max_proto = *(++argv);
1140 } else if (strcmp(*argv, "-client_min_proto") == 0) {
1141 if (--argc < 1)
1142 goto bad;
1143 client_min_proto = *(++argv);
1144 } else if (strcmp(*argv, "-client_max_proto") == 0) {
1145 if (--argc < 1)
1146 goto bad;
1147 client_max_proto = *(++argv);
1148 } else if (strcmp(*argv, "-should_negotiate") == 0) {
1149 if (--argc < 1)
1150 goto bad;
1151 should_negotiate = *(++argv);
1152 } else if (strcmp(*argv, "-sn_client") == 0) {
1153 if (--argc < 1)
1154 goto bad;
1155 sn_client = *(++argv);
1156 } else if (strcmp(*argv, "-sn_server1") == 0) {
1157 if (--argc < 1)
1158 goto bad;
1159 sn_server1 = *(++argv);
1160 } else if (strcmp(*argv, "-sn_server2") == 0) {
1161 if (--argc < 1)
1162 goto bad;
1163 sn_server2 = *(++argv);
1164 } else if (strcmp(*argv, "-sn_expect1") == 0) {
1165 sn_expect = 1;
1166 } else if (strcmp(*argv, "-sn_expect2") == 0) {
1167 sn_expect = 2;
1168 } else if (strcmp(*argv, "-server_sess_out") == 0) {
1169 if (--argc < 1)
1170 goto bad;
1171 server_sess_out = *(++argv);
1172 } else if (strcmp(*argv, "-server_sess_in") == 0) {
1173 if (--argc < 1)
1174 goto bad;
1175 server_sess_in = *(++argv);
1176 } else if (strcmp(*argv, "-client_sess_out") == 0) {
1177 if (--argc < 1)
1178 goto bad;
1179 client_sess_out = *(++argv);
1180 } else if (strcmp(*argv, "-client_sess_in") == 0) {
1181 if (--argc < 1)
1182 goto bad;
1183 client_sess_in = *(++argv);
1184 } else if (strcmp(*argv, "-should_reuse") == 0) {
1185 if (--argc < 1)
1186 goto bad;
1187 should_reuse = !!atoi(*(++argv));
1188 } else if (strcmp(*argv, "-no_ticket") == 0) {
1189 no_ticket = 1;
1190 } else if (strcmp(*argv, "-client_ktls") == 0) {
1191 client_ktls = 1;
1192 } else if (strcmp(*argv, "-server_ktls") == 0) {
1193 server_ktls = 1;
1194 } else if (strcmp(*argv, "-provider") == 0) {
1195 if (--argc < 1)
1196 goto bad;
1197 provider = *(++argv);
1198 } else if (strcmp(*argv, "-config") == 0) {
1199 if (--argc < 1)
1200 goto bad;
1201 config = *(++argv);
1202 } else {
1203 int rv;
1204 arg = argv[0];
1205 argn = argv[1];
1206 /* Try to process command using SSL_CONF */
1207 rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv);
1208 /* If not processed try server */
1209 if (rv == 0)
1210 rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv);
1211 /* Recognised: store it for later use */
1212 if (rv > 0) {
1213 if (rv == 1)
1214 argn = NULL;
1215 if (!conf_args) {
1216 conf_args = sk_OPENSSL_STRING_new_null();
1217 if (!conf_args)
1218 goto end;
1219 }
1220 if (!sk_OPENSSL_STRING_push(conf_args, arg))
1221 goto end;
1222 if (!sk_OPENSSL_STRING_push(conf_args, argn))
1223 goto end;
1224 continue;
1225 }
1226 if (rv == -3)
1227 BIO_printf(bio_err, "Missing argument for %s\n", arg);
1228 else if (rv < 0)
1229 BIO_printf(bio_err, "Error with command %s\n", arg);
1230 else if (rv == 0)
1231 BIO_printf(bio_err, "unknown option %s\n", arg);
1232 badop = 1;
1233 break;
1234 }
1235 argc--;
1236 argv++;
1237 }
1238 if (badop) {
1239 bad:
1240 sv_usage();
1241 goto end;
1242 }
1243
1244 if (ssl3 + tls1 + tls1_1 + tls1_2 + dtls + dtls1 + dtls12 > 1) {
1245 fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
1246 "be requested.\n");
1247 EXIT(1);
1248 }
1249
1250#ifdef OPENSSL_NO_SSL3
1251 if (ssl3)
1252 no_protocol = 1;
1253 else
1254#endif
1255#ifdef OPENSSL_NO_TLS1
1256 if (tls1)
1257 no_protocol = 1;
1258 else
1259#endif
1260#ifdef OPENSSL_NO_TLS1_1
1261 if (tls1_1)
1262 no_protocol = 1;
1263 else
1264#endif
1265#ifdef OPENSSL_NO_TLS1_2
1266 if (tls1_2)
1267 no_protocol = 1;
1268 else
1269#endif
1270#if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
1271 if (dtls1)
1272 no_protocol = 1;
1273 else
1274#endif
1275#if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
1276 if (dtls12)
1277 no_protocol = 1;
1278 else
1279#endif
1280 no_protocol = 0;
1281
1282 /*
1283 * Testing was requested for a compiled-out protocol (e.g. SSLv3).
1284 * Ideally, we would error out, but the generic test wrapper can't know
1285 * when to expect failure. So we do nothing and return success.
1286 */
1287 if (no_protocol) {
1288 fprintf(stderr, "Testing was requested for a disabled protocol. "
1289 "Skipping tests.\n");
1290 ret = EXIT_SUCCESS;
1291 goto end;
1292 }
1293
1294 if (!ssl3 && !tls1 && !tls1_1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1
1295 && !reuse && !force) {
1296 fprintf(stderr, "This case cannot work. Use -f to perform "
1297 "the test anyway (and\n-d to see what happens), "
1298 "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
1299 "to avoid protocol mismatch.\n");
1300 EXIT(1);
1301 }
1302
1303 if (print_time) {
1304 if (bio_type == BIO_MEM) {
1305 fprintf(stderr, "Using BIO pair (-bio_pair)\n");
1306 bio_type = BIO_PAIR;
1307 }
1308 if (number < 50 && !force)
1309 fprintf(stderr,
1310 "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
1311 }
1312
1313#ifndef OPENSSL_NO_COMP
1314 if (comp == COMP_ZLIB)
1315 cm = COMP_zlib();
1316 if (cm != NULL) {
1317 if (COMP_get_type(cm) != NID_undef) {
1318 if (SSL_COMP_add_compression_method(comp, cm) != 0) {
1319 fprintf(stderr, "Failed to add compression method\n");
1320 ERR_print_errors_fp(stderr);
1321 }
1322 } else {
1323 fprintf(stderr,
1324 "Warning: %s compression not supported\n",
1325 comp == COMP_ZLIB ? "zlib" : "unknown");
1326 ERR_print_errors_fp(stderr);
1327 }
1328 }
1329 ssl_comp_methods = SSL_COMP_get_compression_methods();
1330 n = sk_SSL_COMP_num(ssl_comp_methods);
1331 if (n) {
1332 int j;
1333 printf("Available compression methods:");
1334 for (j = 0; j < n; j++) {
1335 SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
1336 printf(" %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c));
1337 }
1338 printf("\n");
1339 }
1340#endif
1341
1342#ifndef OPENSSL_NO_TLS
1343 meth = TLS_method();
1344 if (ssl3) {
1345 min_version = SSL3_VERSION;
1346 max_version = SSL3_VERSION;
1347 } else if (tls1) {
1348 min_version = TLS1_VERSION;
1349 max_version = TLS1_VERSION;
1350 } else if (tls1_1) {
1351 min_version = TLS1_1_VERSION;
1352 max_version = TLS1_1_VERSION;
1353 } else if (tls1_2) {
1354 min_version = TLS1_2_VERSION;
1355 max_version = TLS1_2_VERSION;
1356 } else {
1357 min_version = 0;
1358# if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)
1359 /* We only have ec and dh based built-in groups for TLSv1.3 */
1360 max_version = TLS1_2_VERSION;
1361# else
1362 max_version = 0;
1363# endif
1364 }
1365#endif
1366#ifndef OPENSSL_NO_DTLS
1367 if (dtls || dtls1 || dtls12) {
1368 meth = DTLS_method();
1369 if (dtls1) {
1370 min_version = DTLS1_VERSION;
1371 max_version = DTLS1_VERSION;
1372 } else if (dtls12) {
1373 min_version = DTLS1_2_VERSION;
1374 max_version = DTLS1_2_VERSION;
1375 } else {
1376 min_version = 0;
1377 max_version = 0;
1378 }
1379 }
1380#endif
1381
1382 if (provider != NULL
1383 && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
1384 goto end;
1385
1386 c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1387 s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1388 s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */
1389 if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) {
1390 ERR_print_errors(bio_err);
1391 goto end;
1392 }
1393 /*
1394 * Since we will use low security ciphersuites and keys for testing set
1395 * security level to zero by default. Tests can override this by adding
1396 * "@SECLEVEL=n" to the cipher string.
1397 */
1398 SSL_CTX_set_security_level(c_ctx, 0);
1399 SSL_CTX_set_security_level(s_ctx, 0);
1400 SSL_CTX_set_security_level(s_ctx2, 0);
1401
1402 if (no_ticket) {
1403 SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
1404 SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
1405 }
1406
1407 if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0)
1408 goto end;
1409 if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0)
1410 goto end;
1411 if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0)
1412 goto end;
1413 if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0)
1414 goto end;
1415
1416 if (cipher != NULL) {
1417 if (strcmp(cipher, "") == 0) {
1418 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) {
1419 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1420 ERR_clear_error();
1421 } else {
1422 ERR_print_errors(bio_err);
1423 goto end;
1424 }
1425 } else {
1426 /* Should have failed when clearing all TLSv1.2 ciphers. */
1427 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1428 goto end;
1429 }
1430
1431 if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) {
1432 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1433 ERR_clear_error();
1434 } else {
1435 ERR_print_errors(bio_err);
1436 goto end;
1437 }
1438 } else {
1439 /* Should have failed when clearing all TLSv1.2 ciphers. */
1440 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1441 goto end;
1442 }
1443
1444 if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1445 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1446 ERR_clear_error();
1447 } else {
1448 ERR_print_errors(bio_err);
1449 goto end;
1450 }
1451 } else {
1452 /* Should have failed when clearing all TLSv1.2 ciphers. */
1453 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1454 goto end;
1455 }
1456 } else {
1457 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
1458 || !SSL_CTX_set_cipher_list(s_ctx, cipher)
1459 || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1460 ERR_print_errors(bio_err);
1461 goto end;
1462 }
1463 }
1464 }
1465 if (ciphersuites != NULL) {
1466 if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
1467 || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
1468 || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
1469 ERR_print_errors(bio_err);
1470 goto end;
1471 }
1472 }
1473
1474#ifndef OPENSSL_NO_CT
1475 if (ct_validation &&
1476 !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
1477 ERR_print_errors(bio_err);
1478 goto end;
1479 }
1480#endif
1481
1482 /* Process SSL_CONF arguments */
1483 SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
1484 SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
1485 SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
1486
1487 for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
1488 int rv;
1489 arg = sk_OPENSSL_STRING_value(conf_args, i);
1490 argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
1491 rv = SSL_CONF_cmd(c_cctx, arg, argn);
1492 /* If not recognised use server context */
1493 if (rv == -2) {
1494 rv = SSL_CONF_cmd(s_cctx2, arg, argn);
1495 if (rv > 0)
1496 rv = SSL_CONF_cmd(s_cctx, arg, argn);
1497 }
1498 if (rv <= 0) {
1499 BIO_printf(bio_err, "Error processing %s %s\n",
1500 arg, argn ? argn : "");
1501 ERR_print_errors(bio_err);
1502 goto end;
1503 }
1504 }
1505
1506 if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
1507 BIO_puts(bio_err, "Error finishing context\n");
1508 ERR_print_errors(bio_err);
1509 goto end;
1510 }
1511#ifndef OPENSSL_NO_DH
1512 if (!no_dhe) {
1513 if (dhe1024dsa)
1514 dhpkey = get_dh1024dsa(libctx);
1515 else if (dhe512)
1516 dhpkey = get_dh512(libctx);
1517 else if (dhe4096)
1518 dhpkey = get_dh4096(libctx);
1519 else
1520 dhpkey = get_dh2048(libctx);
1521
1522 if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
1523 EVP_PKEY_free(dhpkey);
1524 BIO_puts(bio_err, "Error getting DH parameters\n");
1525 ERR_print_errors(bio_err);
1526 goto end;
1527 }
1528 if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey))
1529 EVP_PKEY_free(dhpkey);
1530 if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey))
1531 EVP_PKEY_free(dhpkey);
1532 }
1533#endif
1534
1535 if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
1536 || SSL_CTX_load_verify_dir(s_ctx, CApath))
1537 || !SSL_CTX_set_default_verify_paths(s_ctx)
1538 || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
1539 || SSL_CTX_load_verify_dir(s_ctx2, CApath))
1540 || !SSL_CTX_set_default_verify_paths(s_ctx2)
1541 || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
1542 || SSL_CTX_load_verify_dir(c_ctx, CApath))
1543 || !SSL_CTX_set_default_verify_paths(c_ctx)) {
1544 ERR_print_errors(bio_err);
1545 }
1546
1547#ifndef OPENSSL_NO_CT
1548 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
1549 !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
1550 !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
1551 ERR_print_errors(bio_err);
1552 }
1553#endif
1554
1555 if (client_auth) {
1556 printf("client authentication\n");
1557 SSL_CTX_set_verify(s_ctx,
1558 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1559 verify_callback);
1560 SSL_CTX_set_verify(s_ctx2,
1561 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1562 verify_callback);
1563 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
1564 &app_verify_arg);
1565 SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
1566 &app_verify_arg);
1567 }
1568 if (server_auth) {
1569 printf("server authentication\n");
1570 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
1571 SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
1572 &app_verify_arg);
1573 }
1574
1575 {
1576 int session_id_context = 0;
1577 if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
1578 sizeof(session_id_context)) ||
1579 !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
1580 sizeof(session_id_context))) {
1581 ERR_print_errors(bio_err);
1582 goto end;
1583 }
1584 }
1585
1586 /* Use PSK only if PSK key is given */
1587 if (psk_key != NULL) {
1588 /*
1589 * no_psk is used to avoid putting psk command to openssl tool
1590 */
1591 if (no_psk) {
1592 /*
1593 * if PSK is not compiled in and psk key is given, do nothing and
1594 * exit successfully
1595 */
1596 ret = EXIT_SUCCESS;
1597 goto end;
1598 }
1599#ifndef OPENSSL_NO_PSK
1600 SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
1601 SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
1602 SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
1603 if (debug)
1604 BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
1605 if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
1606 !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
1607 BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
1608 ERR_print_errors(bio_err);
1609 goto end;
1610 }
1611#endif
1612 }
1613
1614#ifndef OPENSSL_NO_NEXTPROTONEG
1615 if (npn_client) {
1616 SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
1617 }
1618 if (npn_server) {
1619 if (npn_server_reject) {
1620 BIO_printf(bio_err,
1621 "Can't have both -npn_server and -npn_server_reject\n");
1622 goto end;
1623 }
1624 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
1625 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
1626 }
1627 if (npn_server_reject) {
1628 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
1629 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
1630 }
1631#endif
1632
1633 if (serverinfo_sct) {
1634 if (!SSL_CTX_add_client_custom_ext(c_ctx,
1635 TLSEXT_TYPE_signed_certificate_timestamp,
1636 NULL, NULL, NULL,
1637 serverinfo_cli_parse_cb, NULL)) {
1638 BIO_printf(bio_err, "Error adding SCT extension\n");
1639 goto end;
1640 }
1641 }
1642 if (serverinfo_tack) {
1643 if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
1644 NULL, NULL, NULL,
1645 serverinfo_cli_parse_cb, NULL)) {
1646 BIO_printf(bio_err, "Error adding TACK extension\n");
1647 goto end;
1648 }
1649 }
1650 if (serverinfo_file)
1651 if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
1652 !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
1653 BIO_printf(bio_err, "missing serverinfo file\n");
1654 goto end;
1655 }
1656
1657 if (custom_ext) {
1658 if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
1659 custom_ext_0_cli_add_cb,
1660 NULL, NULL,
1661 custom_ext_0_cli_parse_cb, NULL)
1662 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
1663 custom_ext_1_cli_add_cb,
1664 NULL, NULL,
1665 custom_ext_1_cli_parse_cb, NULL)
1666 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
1667 custom_ext_2_cli_add_cb,
1668 NULL, NULL,
1669 custom_ext_2_cli_parse_cb, NULL)
1670 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
1671 custom_ext_3_cli_add_cb,
1672 NULL, NULL,
1673 custom_ext_3_cli_parse_cb, NULL)
1674 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
1675 custom_ext_0_srv_add_cb,
1676 NULL, NULL,
1677 custom_ext_0_srv_parse_cb, NULL)
1678 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
1679 custom_ext_0_srv_add_cb,
1680 NULL, NULL,
1681 custom_ext_0_srv_parse_cb, NULL)
1682 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
1683 custom_ext_1_srv_add_cb,
1684 NULL, NULL,
1685 custom_ext_1_srv_parse_cb, NULL)
1686 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
1687 custom_ext_1_srv_add_cb,
1688 NULL, NULL,
1689 custom_ext_1_srv_parse_cb, NULL)
1690 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
1691 custom_ext_2_srv_add_cb,
1692 NULL, NULL,
1693 custom_ext_2_srv_parse_cb, NULL)
1694 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
1695 custom_ext_2_srv_add_cb,
1696 NULL, NULL,
1697 custom_ext_2_srv_parse_cb, NULL)
1698 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
1699 custom_ext_3_srv_add_cb,
1700 NULL, NULL,
1701 custom_ext_3_srv_parse_cb, NULL)
1702 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
1703 custom_ext_3_srv_add_cb,
1704 NULL, NULL,
1705 custom_ext_3_srv_parse_cb, NULL)) {
1706 BIO_printf(bio_err, "Error setting custom extensions\n");
1707 goto end;
1708 }
1709 }
1710
1711 if (alpn_server)
1712 SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
1713 if (alpn_server2)
1714 SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
1715
1716 if (alpn_client) {
1717 size_t alpn_len;
1718 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
1719
1720 if (alpn == NULL) {
1721 BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
1722 goto end;
1723 }
1724 /* Returns 0 on success!! */
1725 if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
1726 BIO_printf(bio_err, "Error setting ALPN\n");
1727 OPENSSL_free(alpn);
1728 goto end;
1729 }
1730 OPENSSL_free(alpn);
1731 }
1732
1733 if (server_sess_in != NULL) {
1734 server_sess = read_session(server_sess_in);
1735 if (server_sess == NULL)
1736 goto end;
1737 }
1738 if (client_sess_in != NULL) {
1739 client_sess = read_session(client_sess_in);
1740 if (client_sess == NULL)
1741 goto end;
1742 }
1743
1744 if (server_sess_out != NULL || server_sess_in != NULL) {
1745 char *keys;
1746 long size;
1747
1748 /* Use a fixed key so that we can decrypt the ticket. */
1749 size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
1750 keys = OPENSSL_zalloc(size);
1751 if (keys == NULL)
1752 goto end;
1753 SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
1754 OPENSSL_free(keys);
1755 }
1756
1757 if (sn_server1 != NULL || sn_server2 != NULL)
1758 SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
1759
1760 c_ssl = SSL_new(c_ctx);
1761 s_ssl = SSL_new(s_ctx);
1762
1763 if (sn_client)
1764 SSL_set_tlsext_host_name(c_ssl, sn_client);
1765 if (client_ktls)
1766 SSL_set_options(c_ssl, SSL_OP_ENABLE_KTLS);
1767 if (server_ktls)
1768 SSL_set_options(s_ssl, SSL_OP_ENABLE_KTLS);
1769
1770 if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1771 goto end;
1772 if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1773 goto end;
1774 if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1775 goto end;
1776 if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1777 goto end;
1778
1779 if (server_sess) {
1780 if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
1781 BIO_printf(bio_err, "Can't add server session\n");
1782 ERR_print_errors(bio_err);
1783 goto end;
1784 }
1785 }
1786
1787 BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
1788 for (i = 0; i < number; i++) {
1789 if (!reuse) {
1790 if (!SSL_set_session(c_ssl, NULL)) {
1791 BIO_printf(bio_err, "Failed to set session\n");
1792 goto end;
1793 }
1794 }
1795 if (client_sess_in != NULL) {
1796 if (SSL_set_session(c_ssl, client_sess) == 0) {
1797 BIO_printf(bio_err, "Can't set client session\n");
1798 ERR_print_errors(bio_err);
1799 goto end;
1800 }
1801 }
1802 switch (bio_type) {
1803 case BIO_MEM:
1804 ret = doit(s_ssl, c_ssl, bytes);
1805 break;
1806 case BIO_PAIR:
1807 ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
1808 break;
1809#ifndef OPENSSL_NO_SOCK
1810 case BIO_IPV4:
1811 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
1812 bytes, &s_time, &c_time);
1813 break;
1814 case BIO_IPV6:
1815 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
1816 bytes, &s_time, &c_time);
1817 break;
1818#else
1819 case BIO_IPV4:
1820 case BIO_IPV6:
1821 ret = EXIT_FAILURE;
1822 goto err;
1823#endif
1824 }
1825 if (ret != EXIT_SUCCESS) break;
1826 }
1827
1828 if (should_negotiate && ret == EXIT_SUCCESS &&
1829 strcmp(should_negotiate, "fail-server") != 0 &&
1830 strcmp(should_negotiate, "fail-client") != 0) {
1831 int version = protocol_from_string(should_negotiate);
1832 if (version < 0) {
1833 BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
1834 ret = EXIT_FAILURE;
1835 goto err;
1836 }
1837 if (SSL_version(c_ssl) != version) {
1838 BIO_printf(bio_err, "Unexpected version negotiated. "
1839 "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
1840 ret = EXIT_FAILURE;
1841 goto err;
1842 }
1843 }
1844
1845 if (should_reuse != -1) {
1846 if (SSL_session_reused(s_ssl) != should_reuse ||
1847 SSL_session_reused(c_ssl) != should_reuse) {
1848 BIO_printf(bio_err, "Unexpected session reuse state. "
1849 "Expected: %d, server: %d, client: %d\n", should_reuse,
1850 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
1851 ret = EXIT_FAILURE;
1852 goto err;
1853 }
1854 }
1855
1856 if (server_sess_out != NULL) {
1857 if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
1858 ret = EXIT_FAILURE;
1859 goto err;
1860 }
1861 }
1862 if (client_sess_out != NULL) {
1863 if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
1864 ret = EXIT_FAILURE;
1865 goto err;
1866 }
1867 }
1868
1869 if (!verbose) {
1870 print_details(c_ssl, "");
1871 }
1872 if (print_time) {
1873#ifdef CLOCKS_PER_SEC
1874 /*
1875 * "To determine the time in seconds, the value returned by the clock
1876 * function should be divided by the value of the macro
1877 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1878 */
1879 BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
1880 "Approximate total client time: %6.2f s\n",
1881 (double)s_time / CLOCKS_PER_SEC,
1882 (double)c_time / CLOCKS_PER_SEC);
1883#else
1884 BIO_printf(bio_stdout,
1885 "Approximate total server time: %6.2f units\n"
1886 "Approximate total client time: %6.2f units\n",
1887 (double)s_time, (double)c_time);
1888#endif
1889 }
1890
1891 err:
1892 SSL_free(s_ssl);
1893 SSL_free(c_ssl);
1894
1895 end:
1896 SSL_CTX_free(s_ctx);
1897 SSL_CTX_free(s_ctx2);
1898 SSL_CTX_free(c_ctx);
1899 SSL_CONF_CTX_free(s_cctx);
1900 SSL_CONF_CTX_free(s_cctx2);
1901 SSL_CONF_CTX_free(c_cctx);
1902 sk_OPENSSL_STRING_free(conf_args);
1903
1904 BIO_free(bio_stdout);
1905
1906 SSL_SESSION_free(server_sess);
1907 SSL_SESSION_free(client_sess);
1908
1909 OSSL_PROVIDER_unload(defctxnull);
1910 OSSL_PROVIDER_unload(thisprov);
1911 OSSL_LIB_CTX_free(libctx);
1912
1913 BIO_free(bio_err);
1914 EXIT(ret);
1915}
1916
1917#ifndef OPENSSL_NO_SOCK
1918int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
1919 clock_t *s_time, clock_t *c_time)
1920{
1921 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
1922 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
1923 BIO *acpt = NULL, *server = NULL, *client = NULL;
1924 char addr_str[40];
1925 int ret = EXIT_FAILURE;
1926 int err_in_client = 0;
1927 int err_in_server = 0;
1928
1929 acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
1930 : "[::1]:0");
1931 if (acpt == NULL)
1932 goto err;
1933 BIO_set_accept_ip_family(acpt, family);
1934 BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
1935 if (BIO_do_accept(acpt) <= 0)
1936 goto err;
1937
1938 BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
1939
1940 client = BIO_new_connect(addr_str);
1941 if (!client)
1942 goto err;
1943 BIO_set_conn_ip_family(client, family);
1944
1945 if (BIO_set_nbio(client, 1) <= 0)
1946 goto err;
1947 if (BIO_set_nbio(acpt, 1) <= 0)
1948 goto err;
1949
1950 {
1951 int st_connect = 0, st_accept = 0;
1952
1953 while(!st_connect || !st_accept) {
1954 if (!st_connect) {
1955 if (BIO_do_connect(client) <= 0) {
1956 if (!BIO_should_retry(client))
1957 goto err;
1958 } else {
1959 st_connect = 1;
1960 }
1961 }
1962 if (!st_accept) {
1963 if (BIO_do_accept(acpt) <= 0) {
1964 if (!BIO_should_retry(acpt))
1965 goto err;
1966 } else {
1967 st_accept = 1;
1968 }
1969 }
1970 }
1971 }
1972 /* We're not interested in accepting further connects */
1973 server = BIO_pop(acpt);
1974 BIO_free_all(acpt);
1975 acpt = NULL;
1976
1977 s_ssl_bio = BIO_new(BIO_f_ssl());
1978 if (!s_ssl_bio)
1979 goto err;
1980
1981 c_ssl_bio = BIO_new(BIO_f_ssl());
1982 if (!c_ssl_bio)
1983 goto err;
1984
1985 SSL_set_connect_state(c_ssl);
1986 SSL_set_bio(c_ssl, client, client);
1987 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
1988
1989 SSL_set_accept_state(s_ssl);
1990 SSL_set_bio(s_ssl, server, server);
1991 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
1992
1993 do {
1994 /*-
1995 * c_ssl_bio: SSL filter BIO
1996 *
1997 * client: I/O for SSL library
1998 *
1999 *
2000 * server: I/O for SSL library
2001 *
2002 * s_ssl_bio: SSL filter BIO
2003 */
2004
2005 /*
2006 * We have non-blocking behaviour throughout this test program, but
2007 * can be sure that there is *some* progress in each iteration; so we
2008 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2009 * we just try everything in each iteration
2010 */
2011
2012 {
2013 /* CLIENT */
2014
2015 char cbuf[1024 * 8];
2016 int i, r;
2017 clock_t c_clock = clock();
2018
2019 memset(cbuf, 0, sizeof(cbuf));
2020
2021 if (debug)
2022 if (SSL_in_init(c_ssl))
2023 printf("client waiting in SSL_connect - %s\n",
2024 SSL_state_string_long(c_ssl));
2025
2026 if (cw_num > 0) {
2027 /* Write to server. */
2028
2029 if (cw_num > (long)sizeof(cbuf))
2030 i = sizeof(cbuf);
2031 else
2032 i = (int)cw_num;
2033 r = BIO_write(c_ssl_bio, cbuf, i);
2034 if (r < 0) {
2035 if (!BIO_should_retry(c_ssl_bio)) {
2036 fprintf(stderr, "ERROR in CLIENT (write)\n");
2037 err_in_client = 1;
2038 goto err;
2039 }
2040 /*
2041 * BIO_should_retry(...) can just be ignored here. The
2042 * library expects us to call BIO_write with the same
2043 * arguments again, and that's what we will do in the
2044 * next iteration.
2045 */
2046 } else if (r == 0) {
2047 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2048 goto err;
2049 } else {
2050 if (debug)
2051 printf("client wrote %d\n", r);
2052 cw_num -= r;
2053 }
2054 }
2055
2056 if (cr_num > 0) {
2057 /* Read from server. */
2058
2059 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2060 if (r < 0) {
2061 if (!BIO_should_retry(c_ssl_bio)) {
2062 fprintf(stderr, "ERROR in CLIENT (read)\n");
2063 err_in_client = 1;
2064 goto err;
2065 }
2066 /*
2067 * Again, "BIO_should_retry" can be ignored.
2068 */
2069 } else if (r == 0) {
2070 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2071 goto err;
2072 } else {
2073 if (debug)
2074 printf("client read %d\n", r);
2075 cr_num -= r;
2076 }
2077 }
2078
2079 /*
2080 * c_time and s_time increments will typically be very small
2081 * (depending on machine speed and clock tick intervals), but
2082 * sampling over a large number of connections should result in
2083 * fairly accurate figures. We cannot guarantee a lot, however
2084 * -- if each connection lasts for exactly one clock tick, it
2085 * will be counted only for the client or only for the server or
2086 * even not at all.
2087 */
2088 *c_time += (clock() - c_clock);
2089 }
2090
2091 {
2092 /* SERVER */
2093
2094 char sbuf[1024 * 8];
2095 int i, r;
2096 clock_t s_clock = clock();
2097
2098 memset(sbuf, 0, sizeof(sbuf));
2099
2100 if (debug)
2101 if (SSL_in_init(s_ssl))
2102 printf("server waiting in SSL_accept - %s\n",
2103 SSL_state_string_long(s_ssl));
2104
2105 if (sw_num > 0) {
2106 /* Write to client. */
2107
2108 if (sw_num > (long)sizeof(sbuf))
2109 i = sizeof(sbuf);
2110 else
2111 i = (int)sw_num;
2112 r = BIO_write(s_ssl_bio, sbuf, i);
2113 if (r < 0) {
2114 if (!BIO_should_retry(s_ssl_bio)) {
2115 fprintf(stderr, "ERROR in SERVER (write)\n");
2116 err_in_server = 1;
2117 goto err;
2118 }
2119 /* Ignore "BIO_should_retry". */
2120 } else if (r == 0) {
2121 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2122 goto err;
2123 } else {
2124 if (debug)
2125 printf("server wrote %d\n", r);
2126 sw_num -= r;
2127 }
2128 }
2129
2130 if (sr_num > 0) {
2131 /* Read from client. */
2132
2133 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2134 if (r < 0) {
2135 if (!BIO_should_retry(s_ssl_bio)) {
2136 fprintf(stderr, "ERROR in SERVER (read)\n");
2137 err_in_server = 1;
2138 goto err;
2139 }
2140 /* blah, blah */
2141 } else if (r == 0) {
2142 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2143 goto err;
2144 } else {
2145 if (debug)
2146 printf("server read %d\n", r);
2147 sr_num -= r;
2148 }
2149 }
2150
2151 *s_time += (clock() - s_clock);
2152 }
2153 }
2154 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2155
2156 if (verbose) {
2157 print_details(c_ssl, "DONE via TCP connect: ");
2158
2159 if (BIO_get_ktls_send(SSL_get_wbio(s_ssl))
2160 && BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
2161 BIO_printf(bio_stdout, "Server using Kernel TLS in both directions\n");
2162 else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl)))
2163 BIO_printf(bio_stdout, "Server using Kernel TLS for sending\n");
2164 else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
2165 BIO_printf(bio_stdout, "Server using Kernel TLS for receiving\n");
2166
2167 if (BIO_get_ktls_send(SSL_get_wbio(c_ssl))
2168 && BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
2169 BIO_printf(bio_stdout, "Client using Kernel TLS in both directions\n");
2170 else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl)))
2171 BIO_printf(bio_stdout, "Client using Kernel TLS for sending\n");
2172 else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
2173 BIO_printf(bio_stdout, "Client using Kernel TLS for receiving\n");
2174 }
2175# ifndef OPENSSL_NO_NEXTPROTONEG
2176 if (verify_npn(c_ssl, s_ssl) < 0)
2177 goto end;
2178# endif
2179 if (verify_serverinfo() < 0) {
2180 fprintf(stderr, "Server info verify error\n");
2181 goto err;
2182 }
2183 if (verify_alpn(c_ssl, s_ssl) < 0
2184 || verify_servername(c_ssl, s_ssl) < 0)
2185 goto err;
2186
2187 if (custom_ext_error) {
2188 fprintf(stderr, "Custom extension error\n");
2189 goto err;
2190 }
2191
2192# ifndef OPENSSL_NO_NEXTPROTONEG
2193 end:
2194# endif
2195 ret = EXIT_SUCCESS;
2196
2197 err:
2198 ERR_print_errors(bio_err);
2199
2200 BIO_free_all(acpt);
2201 BIO_free(server);
2202 BIO_free(client);
2203 BIO_free(s_ssl_bio);
2204 BIO_free(c_ssl_bio);
2205
2206 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2207 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2208 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2209 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2210
2211 return ret;
2212}
2213#endif
2214
2215int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
2216 clock_t *s_time, clock_t *c_time)
2217{
2218 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
2219 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
2220 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
2221 int ret = EXIT_FAILURE;
2222 int err_in_client = 0;
2223 int err_in_server = 0;
2224
2225 size_t bufsiz = 256; /* small buffer for testing */
2226
2227 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
2228 goto err;
2229 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
2230 goto err;
2231
2232 s_ssl_bio = BIO_new(BIO_f_ssl());
2233 if (!s_ssl_bio)
2234 goto err;
2235
2236 c_ssl_bio = BIO_new(BIO_f_ssl());
2237 if (!c_ssl_bio)
2238 goto err;
2239
2240 SSL_set_connect_state(c_ssl);
2241 SSL_set_bio(c_ssl, client, client);
2242 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
2243
2244 SSL_set_accept_state(s_ssl);
2245 SSL_set_bio(s_ssl, server, server);
2246 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
2247
2248 do {
2249 /*-
2250 * c_ssl_bio: SSL filter BIO
2251 *
2252 * client: pseudo-I/O for SSL library
2253 *
2254 * client_io: client's SSL communication; usually to be
2255 * relayed over some I/O facility, but in this
2256 * test program, we're the server, too:
2257 *
2258 * server_io: server's SSL communication
2259 *
2260 * server: pseudo-I/O for SSL library
2261 *
2262 * s_ssl_bio: SSL filter BIO
2263 *
2264 * The client and the server each employ a "BIO pair":
2265 * client + client_io, server + server_io.
2266 * BIO pairs are symmetric. A BIO pair behaves similar
2267 * to a non-blocking socketpair (but both endpoints must
2268 * be handled by the same thread).
2269 * [Here we could connect client and server to the ends
2270 * of a single BIO pair, but then this code would be less
2271 * suitable as an example for BIO pairs in general.]
2272 *
2273 * Useful functions for querying the state of BIO pair endpoints:
2274 *
2275 * BIO_ctrl_pending(bio) number of bytes we can read now
2276 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
2277 * other side's read attempt
2278 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2279 *
2280 * ..._read_request is never more than ..._write_guarantee;
2281 * it depends on the application which one you should use.
2282 */
2283
2284 /*
2285 * We have non-blocking behaviour throughout this test program, but
2286 * can be sure that there is *some* progress in each iteration; so we
2287 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2288 * we just try everything in each iteration
2289 */
2290
2291 {
2292 /* CLIENT */
2293
2294 char cbuf[1024 * 8];
2295 int i, r;
2296 clock_t c_clock = clock();
2297
2298 memset(cbuf, 0, sizeof(cbuf));
2299
2300 if (debug)
2301 if (SSL_in_init(c_ssl))
2302 printf("client waiting in SSL_connect - %s\n",
2303 SSL_state_string_long(c_ssl));
2304
2305 if (cw_num > 0) {
2306 /* Write to server. */
2307
2308 if (cw_num > (long)sizeof(cbuf))
2309 i = sizeof(cbuf);
2310 else
2311 i = (int)cw_num;
2312 r = BIO_write(c_ssl_bio, cbuf, i);
2313 if (r < 0) {
2314 if (!BIO_should_retry(c_ssl_bio)) {
2315 fprintf(stderr, "ERROR in CLIENT\n");
2316 err_in_client = 1;
2317 goto err;
2318 }
2319 /*
2320 * BIO_should_retry(...) can just be ignored here. The
2321 * library expects us to call BIO_write with the same
2322 * arguments again, and that's what we will do in the
2323 * next iteration.
2324 */
2325 } else if (r == 0) {
2326 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2327 goto err;
2328 } else {
2329 if (debug)
2330 printf("client wrote %d\n", r);
2331 cw_num -= r;
2332 }
2333 }
2334
2335 if (cr_num > 0) {
2336 /* Read from server. */
2337
2338 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2339 if (r < 0) {
2340 if (!BIO_should_retry(c_ssl_bio)) {
2341 fprintf(stderr, "ERROR in CLIENT\n");
2342 err_in_client = 1;
2343 goto err;
2344 }
2345 /*
2346 * Again, "BIO_should_retry" can be ignored.
2347 */
2348 } else if (r == 0) {
2349 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2350 goto err;
2351 } else {
2352 if (debug)
2353 printf("client read %d\n", r);
2354 cr_num -= r;
2355 }
2356 }
2357
2358 /*
2359 * c_time and s_time increments will typically be very small
2360 * (depending on machine speed and clock tick intervals), but
2361 * sampling over a large number of connections should result in
2362 * fairly accurate figures. We cannot guarantee a lot, however
2363 * -- if each connection lasts for exactly one clock tick, it
2364 * will be counted only for the client or only for the server or
2365 * even not at all.
2366 */
2367 *c_time += (clock() - c_clock);
2368 }
2369
2370 {
2371 /* SERVER */
2372
2373 char sbuf[1024 * 8];
2374 int i, r;
2375 clock_t s_clock = clock();
2376
2377 memset(sbuf, 0, sizeof(sbuf));
2378
2379 if (debug)
2380 if (SSL_in_init(s_ssl))
2381 printf("server waiting in SSL_accept - %s\n",
2382 SSL_state_string_long(s_ssl));
2383
2384 if (sw_num > 0) {
2385 /* Write to client. */
2386
2387 if (sw_num > (long)sizeof(sbuf))
2388 i = sizeof(sbuf);
2389 else
2390 i = (int)sw_num;
2391 r = BIO_write(s_ssl_bio, sbuf, i);
2392 if (r < 0) {
2393 if (!BIO_should_retry(s_ssl_bio)) {
2394 fprintf(stderr, "ERROR in SERVER\n");
2395 err_in_server = 1;
2396 goto err;
2397 }
2398 /* Ignore "BIO_should_retry". */
2399 } else if (r == 0) {
2400 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2401 goto err;
2402 } else {
2403 if (debug)
2404 printf("server wrote %d\n", r);
2405 sw_num -= r;
2406 }
2407 }
2408
2409 if (sr_num > 0) {
2410 /* Read from client. */
2411
2412 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2413 if (r < 0) {
2414 if (!BIO_should_retry(s_ssl_bio)) {
2415 fprintf(stderr, "ERROR in SERVER\n");
2416 err_in_server = 1;
2417 goto err;
2418 }
2419 /* blah, blah */
2420 } else if (r == 0) {
2421 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2422 goto err;
2423 } else {
2424 if (debug)
2425 printf("server read %d\n", r);
2426 sr_num -= r;
2427 }
2428 }
2429
2430 *s_time += (clock() - s_clock);
2431 }
2432
2433 {
2434 /* "I/O" BETWEEN CLIENT AND SERVER. */
2435
2436 size_t r1, r2;
2437 BIO *io1 = server_io, *io2 = client_io;
2438 /*
2439 * we use the non-copying interface for io1 and the standard
2440 * BIO_write/BIO_read interface for io2
2441 */
2442
2443 static int prev_progress = 1;
2444 int progress = 0;
2445
2446 /* io1 to io2 */
2447 do {
2448 size_t num;
2449 int r;
2450
2451 r1 = BIO_ctrl_pending(io1);
2452 r2 = BIO_ctrl_get_write_guarantee(io2);
2453
2454 num = r1;
2455 if (r2 < num)
2456 num = r2;
2457 if (num) {
2458 char *dataptr;
2459
2460 if (INT_MAX < num) /* yeah, right */
2461 num = INT_MAX;
2462
2463 r = BIO_nread(io1, &dataptr, (int)num);
2464 assert(r > 0);
2465 assert(r <= (int)num);
2466 /*
2467 * possibly r < num (non-contiguous data)
2468 */
2469 num = r;
2470 r = BIO_write(io2, dataptr, (int)num);
2471 if (r != (int)num) { /* can't happen */
2472 fprintf(stderr, "ERROR: BIO_write could not write "
2473 "BIO_ctrl_get_write_guarantee() bytes");
2474 goto err;
2475 }
2476 progress = 1;
2477
2478 if (debug)
2479 printf((io1 == client_io) ?
2480 "C->S relaying: %d bytes\n" :
2481 "S->C relaying: %d bytes\n", (int)num);
2482 }
2483 }
2484 while (r1 && r2);
2485
2486 /* io2 to io1 */
2487 {
2488 size_t num;
2489 int r;
2490
2491 r1 = BIO_ctrl_pending(io2);
2492 r2 = BIO_ctrl_get_read_request(io1);
2493 /*
2494 * here we could use ..._get_write_guarantee instead of
2495 * ..._get_read_request, but by using the latter we test
2496 * restartability of the SSL implementation more thoroughly
2497 */
2498 num = r1;
2499 if (r2 < num)
2500 num = r2;
2501 if (num) {
2502 char *dataptr;
2503
2504 if (INT_MAX < num)
2505 num = INT_MAX;
2506
2507 if (num > 1)
2508 --num; /* test restartability even more thoroughly */
2509
2510 r = BIO_nwrite0(io1, &dataptr);
2511 assert(r > 0);
2512 if (r < (int)num)
2513 num = r;
2514 r = BIO_read(io2, dataptr, (int)num);
2515 if (r != (int)num) { /* can't happen */
2516 fprintf(stderr, "ERROR: BIO_read could not read "
2517 "BIO_ctrl_pending() bytes");
2518 goto err;
2519 }
2520 progress = 1;
2521 r = BIO_nwrite(io1, &dataptr, (int)num);
2522 if (r != (int)num) { /* can't happen */
2523 fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
2524 "BIO_nwrite0() bytes");
2525 goto err;
2526 }
2527
2528 if (debug)
2529 printf((io2 == client_io) ?
2530 "C->S relaying: %d bytes\n" :
2531 "S->C relaying: %d bytes\n", (int)num);
2532 }
2533 } /* no loop, BIO_ctrl_get_read_request now
2534 * returns 0 anyway */
2535
2536 if (!progress && !prev_progress)
2537 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
2538 fprintf(stderr, "ERROR: got stuck\n");
2539 fprintf(stderr, " ERROR.\n");
2540 goto err;
2541 }
2542 prev_progress = progress;
2543 }
2544 }
2545 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2546
2547 if (verbose)
2548 print_details(c_ssl, "DONE via BIO pair: ");
2549#ifndef OPENSSL_NO_NEXTPROTONEG
2550 if (verify_npn(c_ssl, s_ssl) < 0)
2551 goto end;
2552#endif
2553 if (verify_serverinfo() < 0) {
2554 fprintf(stderr, "Server info verify error\n");
2555 goto err;
2556 }
2557 if (verify_alpn(c_ssl, s_ssl) < 0
2558 || verify_servername(c_ssl, s_ssl) < 0)
2559 goto err;
2560
2561 if (custom_ext_error) {
2562 fprintf(stderr, "Custom extension error\n");
2563 goto err;
2564 }
2565
2566#ifndef OPENSSL_NO_NEXTPROTONEG
2567 end:
2568#endif
2569 ret = EXIT_SUCCESS;
2570
2571 err:
2572 ERR_print_errors(bio_err);
2573
2574 BIO_free(server);
2575 BIO_free(server_io);
2576 BIO_free(client);
2577 BIO_free(client_io);
2578 BIO_free(s_ssl_bio);
2579 BIO_free(c_ssl_bio);
2580
2581 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2582 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2583 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2584 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2585
2586 return ret;
2587}
2588
2589#define W_READ 1
2590#define W_WRITE 2
2591#define C_DONE 1
2592#define S_DONE 2
2593
2594int doit(SSL *s_ssl, SSL *c_ssl, long count)
2595{
2596 char *cbuf = NULL, *sbuf = NULL;
2597 long bufsiz;
2598 long cw_num = count, cr_num = count;
2599 long sw_num = count, sr_num = count;
2600 int ret = EXIT_FAILURE;
2601 BIO *c_to_s = NULL;
2602 BIO *s_to_c = NULL;
2603 BIO *c_bio = NULL;
2604 BIO *s_bio = NULL;
2605 int c_r, c_w, s_r, s_w;
2606 int i, j;
2607 int done = 0;
2608 int c_write, s_write;
2609 int do_server = 0, do_client = 0;
2610 int max_frag = 5 * 1024;
2611 int err_in_client = 0;
2612 int err_in_server = 0;
2613
2614 bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
2615
2616 if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2617 goto err;
2618 if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2619 goto err;
2620
2621 c_to_s = BIO_new(BIO_s_mem());
2622 s_to_c = BIO_new(BIO_s_mem());
2623 if ((s_to_c == NULL) || (c_to_s == NULL)) {
2624 ERR_print_errors(bio_err);
2625 goto err;
2626 }
2627
2628 c_bio = BIO_new(BIO_f_ssl());
2629 s_bio = BIO_new(BIO_f_ssl());
2630 if ((c_bio == NULL) || (s_bio == NULL)) {
2631 ERR_print_errors(bio_err);
2632 goto err;
2633 }
2634
2635 SSL_set_connect_state(c_ssl);
2636 SSL_set_bio(c_ssl, s_to_c, c_to_s);
2637 SSL_set_max_send_fragment(c_ssl, max_frag);
2638 BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
2639
2640 /*
2641 * We've just given our ref to these BIOs to c_ssl. We need another one to
2642 * give to s_ssl
2643 */
2644 if (!BIO_up_ref(c_to_s)) {
2645 /* c_to_s and s_to_c will get freed when we free c_ssl */
2646 c_to_s = NULL;
2647 s_to_c = NULL;
2648 goto err;
2649 }
2650 if (!BIO_up_ref(s_to_c)) {
2651 /* s_to_c will get freed when we free c_ssl */
2652 s_to_c = NULL;
2653 goto err;
2654 }
2655
2656 SSL_set_accept_state(s_ssl);
2657 SSL_set_bio(s_ssl, c_to_s, s_to_c);
2658
2659 /* We've used up all our refs to these now */
2660 c_to_s = NULL;
2661 s_to_c = NULL;
2662
2663 SSL_set_max_send_fragment(s_ssl, max_frag);
2664 BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
2665
2666 c_r = 0;
2667 s_r = 1;
2668 c_w = 1;
2669 s_w = 0;
2670 c_write = 1, s_write = 0;
2671
2672 /* We can always do writes */
2673 for (;;) {
2674 do_server = 0;
2675 do_client = 0;
2676
2677 i = (int)BIO_pending(s_bio);
2678 if ((i && s_r) || s_w)
2679 do_server = 1;
2680
2681 i = (int)BIO_pending(c_bio);
2682 if ((i && c_r) || c_w)
2683 do_client = 1;
2684
2685 if (do_server && debug) {
2686 if (SSL_in_init(s_ssl))
2687 printf("server waiting in SSL_accept - %s\n",
2688 SSL_state_string_long(s_ssl));
2689 }
2690
2691 if (do_client && debug) {
2692 if (SSL_in_init(c_ssl))
2693 printf("client waiting in SSL_connect - %s\n",
2694 SSL_state_string_long(c_ssl));
2695 }
2696
2697 if (!do_client && !do_server) {
2698 fprintf(stdout, "ERROR IN STARTUP\n");
2699 ERR_print_errors(bio_err);
2700 goto err;
2701 }
2702 if (do_client && !(done & C_DONE)) {
2703 if (c_write) {
2704 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
2705 i = BIO_write(c_bio, cbuf, j);
2706 if (i < 0) {
2707 c_r = 0;
2708 c_w = 0;
2709 if (BIO_should_retry(c_bio)) {
2710 if (BIO_should_read(c_bio))
2711 c_r = 1;
2712 if (BIO_should_write(c_bio))
2713 c_w = 1;
2714 } else {
2715 fprintf(stderr, "ERROR in CLIENT\n");
2716 err_in_client = 1;
2717 ERR_print_errors(bio_err);
2718 goto err;
2719 }
2720 } else if (i == 0) {
2721 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2722 goto err;
2723 } else {
2724 if (debug)
2725 printf("client wrote %d\n", i);
2726 /* ok */
2727 s_r = 1;
2728 c_write = 0;
2729 cw_num -= i;
2730 if (max_frag > 1029)
2731 SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
2732 }
2733 } else {
2734 i = BIO_read(c_bio, cbuf, bufsiz);
2735 if (i < 0) {
2736 c_r = 0;
2737 c_w = 0;
2738 if (BIO_should_retry(c_bio)) {
2739 if (BIO_should_read(c_bio))
2740 c_r = 1;
2741 if (BIO_should_write(c_bio))
2742 c_w = 1;
2743 } else {
2744 fprintf(stderr, "ERROR in CLIENT\n");
2745 err_in_client = 1;
2746 ERR_print_errors(bio_err);
2747 goto err;
2748 }
2749 } else if (i == 0) {
2750 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2751 goto err;
2752 } else {
2753 if (debug)
2754 printf("client read %d\n", i);
2755 cr_num -= i;
2756 if (sw_num > 0) {
2757 s_write = 1;
2758 s_w = 1;
2759 }
2760 if (cr_num <= 0) {
2761 s_write = 1;
2762 s_w = 1;
2763 done = S_DONE | C_DONE;
2764 }
2765 }
2766 }
2767 }
2768
2769 if (do_server && !(done & S_DONE)) {
2770 if (!s_write) {
2771 i = BIO_read(s_bio, sbuf, bufsiz);
2772 if (i < 0) {
2773 s_r = 0;
2774 s_w = 0;
2775 if (BIO_should_retry(s_bio)) {
2776 if (BIO_should_read(s_bio))
2777 s_r = 1;
2778 if (BIO_should_write(s_bio))
2779 s_w = 1;
2780 } else {
2781 fprintf(stderr, "ERROR in SERVER\n");
2782 err_in_server = 1;
2783 ERR_print_errors(bio_err);
2784 goto err;
2785 }
2786 } else if (i == 0) {
2787 ERR_print_errors(bio_err);
2788 fprintf(stderr,
2789 "SSL SERVER STARTUP FAILED in SSL_read\n");
2790 goto err;
2791 } else {
2792 if (debug)
2793 printf("server read %d\n", i);
2794 sr_num -= i;
2795 if (cw_num > 0) {
2796 c_write = 1;
2797 c_w = 1;
2798 }
2799 if (sr_num <= 0) {
2800 s_write = 1;
2801 s_w = 1;
2802 c_write = 0;
2803 }
2804 }
2805 } else {
2806 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
2807 i = BIO_write(s_bio, sbuf, j);
2808 if (i < 0) {
2809 s_r = 0;
2810 s_w = 0;
2811 if (BIO_should_retry(s_bio)) {
2812 if (BIO_should_read(s_bio))
2813 s_r = 1;
2814 if (BIO_should_write(s_bio))
2815 s_w = 1;
2816 } else {
2817 fprintf(stderr, "ERROR in SERVER\n");
2818 err_in_server = 1;
2819 ERR_print_errors(bio_err);
2820 goto err;
2821 }
2822 } else if (i == 0) {
2823 ERR_print_errors(bio_err);
2824 fprintf(stderr,
2825 "SSL SERVER STARTUP FAILED in SSL_write\n");
2826 goto err;
2827 } else {
2828 if (debug)
2829 printf("server wrote %d\n", i);
2830 sw_num -= i;
2831 s_write = 0;
2832 c_r = 1;
2833 if (sw_num <= 0)
2834 done |= S_DONE;
2835 if (max_frag > 1029)
2836 SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
2837 }
2838 }
2839 }
2840
2841 if ((done & S_DONE) && (done & C_DONE))
2842 break;
2843 }
2844
2845 if (verbose)
2846 print_details(c_ssl, "DONE: ");
2847#ifndef OPENSSL_NO_NEXTPROTONEG
2848 if (verify_npn(c_ssl, s_ssl) < 0)
2849 goto err;
2850#endif
2851 if (verify_serverinfo() < 0) {
2852 fprintf(stderr, "Server info verify error\n");
2853 goto err;
2854 }
2855 if (custom_ext_error) {
2856 fprintf(stderr, "Custom extension error\n");
2857 goto err;
2858 }
2859 ret = EXIT_SUCCESS;
2860 err:
2861 BIO_free(c_to_s);
2862 BIO_free(s_to_c);
2863 BIO_free_all(c_bio);
2864 BIO_free_all(s_bio);
2865 OPENSSL_free(cbuf);
2866 OPENSSL_free(sbuf);
2867
2868 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2869 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2870 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2871 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2872
2873 return ret;
2874}
2875
2876static int verify_callback(int ok, X509_STORE_CTX *ctx)
2877{
2878 char *s, buf[256];
2879
2880 s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
2881 buf, sizeof(buf));
2882 if (s != NULL) {
2883 if (ok)
2884 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
2885 else {
2886 fprintf(stderr, "depth=%d error=%d %s\n",
2887 X509_STORE_CTX_get_error_depth(ctx),
2888 X509_STORE_CTX_get_error(ctx), buf);
2889 }
2890 }
2891
2892 if (ok == 0) {
2893 int i = X509_STORE_CTX_get_error(ctx);
2894
2895 switch (i) {
2896 default:
2897 fprintf(stderr, "Error string: %s\n",
2898 X509_verify_cert_error_string(i));
2899 break;
2900 case X509_V_ERR_CERT_NOT_YET_VALID:
2901 case X509_V_ERR_CERT_HAS_EXPIRED:
2902 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2903 ok = 1;
2904 break;
2905 }
2906 }
2907
2908 return ok;
2909}
2910
2911static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
2912{
2913 int ok = 1;
2914 struct app_verify_arg *cb_arg = arg;
2915
2916 if (cb_arg->app_verify) {
2917 char *s = NULL, buf[256];
2918 X509 *c = X509_STORE_CTX_get0_cert(ctx);
2919
2920 printf("In app_verify_callback, allowing cert. ");
2921 printf("Arg is: %s\n", cb_arg->string);
2922 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2923 (void *)ctx, (void *)c);
2924 if (c)
2925 s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
2926 if (s != NULL) {
2927 printf("cert depth=%d %s\n",
2928 X509_STORE_CTX_get_error_depth(ctx), buf);
2929 }
2930 return 1;
2931 }
2932
2933 ok = X509_verify_cert(ctx);
2934
2935 return ok;
2936}
2937
2938#ifndef OPENSSL_NO_PSK
2939/* convert the PSK key (psk_key) in ascii to binary (psk) */
2940static int psk_key2bn(const char *pskkey, unsigned char *psk,
2941 unsigned int max_psk_len)
2942{
2943 int ret;
2944 BIGNUM *bn = NULL;
2945
2946 ret = BN_hex2bn(&bn, pskkey);
2947 if (!ret) {
2948 BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
2949 pskkey);
2950 BN_free(bn);
2951 return 0;
2952 }
2953 if (BN_num_bytes(bn) > (int)max_psk_len) {
2954 BIO_printf(bio_err,
2955 "psk buffer of callback is too small (%d) for key (%d)\n",
2956 max_psk_len, BN_num_bytes(bn));
2957 BN_free(bn);
2958 return 0;
2959 }
2960 ret = BN_bn2bin(bn, psk);
2961 BN_free(bn);
2962 return ret;
2963}
2964
2965static unsigned int psk_client_callback(SSL *ssl, const char *hint,
2966 char *identity,
2967 unsigned int max_identity_len,
2968 unsigned char *psk,
2969 unsigned int max_psk_len)
2970{
2971 int ret;
2972 unsigned int psk_len = 0;
2973
2974 ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
2975 if (ret < 0)
2976 goto out_err;
2977 if (debug)
2978 fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
2979 ret);
2980 ret = psk_key2bn(psk_key, psk, max_psk_len);
2981 if (ret < 0)
2982 goto out_err;
2983 psk_len = ret;
2984 out_err:
2985 return psk_len;
2986}
2987
2988static unsigned int psk_server_callback(SSL *ssl, const char *identity,
2989 unsigned char *psk,
2990 unsigned int max_psk_len)
2991{
2992 unsigned int psk_len = 0;
2993
2994 if (strcmp(identity, "Client_identity") != 0) {
2995 BIO_printf(bio_err, "server: PSK error: client identity not found\n");
2996 return 0;
2997 }
2998 psk_len = psk_key2bn(psk_key, psk, max_psk_len);
2999 return psk_len;
3000}
3001#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette