2 * Copyright 1995-2024 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.
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
12 #include "internal/e_os.h"
14 /* Or gethostname won't be declared properly on Linux and GNU platforms. */
16 # define _BSD_SOURCE 1
18 #ifndef _DEFAULT_SOURCE
19 # define _DEFAULT_SOURCE 1
30 #include "internal/nelem.h"
32 #ifdef OPENSSL_SYS_VMS
34 * Or isascii won't be declared properly on VMS (at least with DECompHP C).
36 # define _XOPEN_SOURCE 500
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>
53 #include <openssl/bn.h>
55 # include <openssl/ct.h>
57 #include <openssl/provider.h>
59 #include "testutil/output.h"
62 * Or gethostname won't be declared properly
63 * on Compaq platforms (at least with DEC C).
64 * Do not try to put it earlier, or IPv6 includes
67 #define _XOPEN_SOURCE_EXTENDED 1
69 #ifdef OPENSSL_SYS_WINDOWS
75 #include "helpers/predefined_dhparams.h"
77 static SSL_CTX
*s_ctx
= NULL
;
78 static SSL_CTX
*s_ctx2
= NULL
;
81 * There is really no standard for this, so let's assign something
86 static int verify_callback(int ok
, X509_STORE_CTX
*ctx
);
87 static int app_verify_callback(X509_STORE_CTX
*ctx
, void *arg
);
88 #define APP_CALLBACK_STRING "Test Callback Argument"
89 struct app_verify_arg
{
94 static char *psk_key
= NULL
; /* by default PSK is not used */
95 #ifndef OPENSSL_NO_PSK
96 static unsigned int psk_client_callback(SSL
*ssl
, const char *hint
,
98 unsigned int max_identity_len
,
100 unsigned int max_psk_len
);
101 static unsigned int psk_server_callback(SSL
*ssl
, const char *identity
,
103 unsigned int max_psk_len
);
106 static BIO
*bio_stdout
= NULL
;
108 #ifndef OPENSSL_NO_NEXTPROTONEG
109 /* Note that this code assumes that this is only a one element list: */
110 static const char NEXT_PROTO_STRING
[] = "\x09testproto";
111 static int npn_client
= 0;
112 static int npn_server
= 0;
113 static int npn_server_reject
= 0;
115 static int cb_client_npn(SSL
*s
, unsigned char **out
, unsigned char *outlen
,
116 const unsigned char *in
, unsigned int inlen
,
120 * This callback only returns the protocol string, rather than a length
121 * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
122 * and remove the first byte to chop off the length prefix.
124 *out
= (unsigned char *)NEXT_PROTO_STRING
+ 1;
125 *outlen
= sizeof(NEXT_PROTO_STRING
) - 2;
126 return SSL_TLSEXT_ERR_OK
;
129 static int cb_server_npn(SSL
*s
, const unsigned char **data
,
130 unsigned int *len
, void *arg
)
132 *data
= (const unsigned char *)NEXT_PROTO_STRING
;
133 *len
= sizeof(NEXT_PROTO_STRING
) - 1;
134 return SSL_TLSEXT_ERR_OK
;
137 static int cb_server_rejects_npn(SSL
*s
, const unsigned char **data
,
138 unsigned int *len
, void *arg
)
140 return SSL_TLSEXT_ERR_NOACK
;
143 static int verify_npn(SSL
*client
, SSL
*server
)
145 const unsigned char *client_s
;
147 const unsigned char *server_s
;
150 SSL_get0_next_proto_negotiated(client
, &client_s
, &client_len
);
151 SSL_get0_next_proto_negotiated(server
, &server_s
, &server_len
);
154 BIO_printf(bio_stdout
, "Client NPN: ");
155 BIO_write(bio_stdout
, client_s
, client_len
);
156 BIO_printf(bio_stdout
, "\n");
160 BIO_printf(bio_stdout
, "Server NPN: ");
161 BIO_write(bio_stdout
, server_s
, server_len
);
162 BIO_printf(bio_stdout
, "\n");
166 * If an NPN string was returned, it must be the protocol that we
167 * expected to negotiate.
169 if (client_len
&& (client_len
!= sizeof(NEXT_PROTO_STRING
) - 2 ||
170 memcmp(client_s
, NEXT_PROTO_STRING
+ 1, client_len
)))
172 if (server_len
&& (server_len
!= sizeof(NEXT_PROTO_STRING
) - 2 ||
173 memcmp(server_s
, NEXT_PROTO_STRING
+ 1, server_len
)))
176 if (!npn_client
&& client_len
)
178 if (!npn_server
&& server_len
)
180 if (npn_server_reject
&& server_len
)
182 if (npn_client
&& npn_server
&& (!client_len
|| !server_len
))
189 static const char *alpn_client
;
190 static char *alpn_server
;
191 static char *alpn_server2
;
192 static const char *alpn_expected
;
193 static unsigned char *alpn_selected
;
194 static const char *server_min_proto
;
195 static const char *server_max_proto
;
196 static const char *client_min_proto
;
197 static const char *client_max_proto
;
198 static const char *should_negotiate
;
199 static const char *sn_client
;
200 static const char *sn_server1
;
201 static const char *sn_server2
;
202 static int sn_expect
= 0;
203 static const char *server_sess_out
;
204 static const char *server_sess_in
;
205 static const char *client_sess_out
;
206 static const char *client_sess_in
;
207 static SSL_SESSION
*server_sess
;
208 static SSL_SESSION
*client_sess
;
210 static int servername_cb(SSL
*s
, int *ad
, void *arg
)
212 const char *servername
= SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
);
213 if (sn_server2
== NULL
) {
214 BIO_printf(bio_stdout
, "Servername 2 is NULL\n");
215 return SSL_TLSEXT_ERR_NOACK
;
219 if (s_ctx2
!= NULL
&& sn_server2
!= NULL
&&
220 !OPENSSL_strcasecmp(servername
, sn_server2
)) {
221 BIO_printf(bio_stdout
, "Switching server context.\n");
222 SSL_set_SSL_CTX(s
, s_ctx2
);
225 return SSL_TLSEXT_ERR_OK
;
227 static int verify_servername(SSL
*client
, SSL
*server
)
229 /* just need to see if sn_context is what we expect */
230 SSL_CTX
* ctx
= SSL_get_SSL_CTX(server
);
233 if (sn_expect
== 1 && ctx
== s_ctx
)
235 if (sn_expect
== 2 && ctx
== s_ctx2
)
237 BIO_printf(bio_stdout
, "Servername: expected context %d\n", sn_expect
);
239 BIO_printf(bio_stdout
, "Servername: context is 2\n");
240 else if (ctx
== s_ctx
)
241 BIO_printf(bio_stdout
, "Servername: context is 1\n");
243 BIO_printf(bio_stdout
, "Servername: context is unknown\n");
249 * next_protos_parse parses a comma separated list of strings into a string
250 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
251 * outlen: (output) set to the length of the resulting buffer on success.
252 * in: a NUL terminated string like "abc,def,ghi"
254 * returns: a malloced buffer or NULL on failure.
256 static unsigned char *next_protos_parse(size_t *outlen
,
267 out
= OPENSSL_malloc(strlen(in
) + 1);
271 for (i
= 0; i
<= len
; ++i
) {
272 if (i
== len
|| in
[i
] == ',') {
273 if (i
- start
> 255) {
277 out
[start
] = (unsigned char)(i
- start
);
287 static int cb_server_alpn(SSL
*s
, const unsigned char **out
,
288 unsigned char *outlen
, const unsigned char *in
,
289 unsigned int inlen
, void *arg
)
291 unsigned char *protos
;
293 char* alpn_str
= arg
;
295 protos
= next_protos_parse(&protos_len
, alpn_str
);
296 if (protos
== NULL
) {
297 fprintf(stderr
, "failed to parser ALPN server protocol string: %s\n",
302 if (SSL_select_next_proto
303 ((unsigned char **)out
, outlen
, protos
, protos_len
, in
,
304 inlen
) != OPENSSL_NPN_NEGOTIATED
) {
305 OPENSSL_free(protos
);
306 return SSL_TLSEXT_ERR_NOACK
;
310 * Make a copy of the selected protocol which will be freed in
313 alpn_selected
= OPENSSL_malloc(*outlen
);
314 if (alpn_selected
== NULL
) {
315 fprintf(stderr
, "failed to allocate memory\n");
316 OPENSSL_free(protos
);
319 memcpy(alpn_selected
, *out
, *outlen
);
320 *out
= alpn_selected
;
322 OPENSSL_free(protos
);
323 return SSL_TLSEXT_ERR_OK
;
326 static int verify_alpn(SSL
*client
, SSL
*server
)
328 const unsigned char *client_proto
, *server_proto
;
329 unsigned int client_proto_len
= 0, server_proto_len
= 0;
330 SSL_get0_alpn_selected(client
, &client_proto
, &client_proto_len
);
331 SSL_get0_alpn_selected(server
, &server_proto
, &server_proto_len
);
333 OPENSSL_free(alpn_selected
);
334 alpn_selected
= NULL
;
336 if (client_proto
== NULL
&& client_proto_len
!= 0) {
337 BIO_printf(bio_stdout
,
338 "Inconsistent SSL_get0_alpn_selected() for client!\n");
342 if (server_proto
== NULL
&& server_proto_len
!= 0) {
343 BIO_printf(bio_stdout
,
344 "Inconsistent SSL_get0_alpn_selected() for server!\n");
348 if (client_proto_len
!= server_proto_len
) {
349 BIO_printf(bio_stdout
, "ALPN selected protocols differ!\n");
353 if (client_proto
!= NULL
&&
354 memcmp(client_proto
, server_proto
, client_proto_len
) != 0) {
355 BIO_printf(bio_stdout
, "ALPN selected protocols differ!\n");
359 if (client_proto_len
> 0 && alpn_expected
== NULL
) {
360 BIO_printf(bio_stdout
, "ALPN unexpectedly negotiated\n");
364 if (alpn_expected
!= NULL
&&
365 (client_proto_len
!= strlen(alpn_expected
) ||
366 memcmp(client_proto
, alpn_expected
, client_proto_len
) != 0)) {
367 BIO_printf(bio_stdout
,
368 "ALPN selected protocols not equal to expected protocol: %s\n",
376 BIO_printf(bio_stdout
, "ALPN results: client: '");
377 BIO_write(bio_stdout
, client_proto
, client_proto_len
);
378 BIO_printf(bio_stdout
, "', server: '");
379 BIO_write(bio_stdout
, server_proto
, server_proto_len
);
380 BIO_printf(bio_stdout
, "'\n");
381 BIO_printf(bio_stdout
, "ALPN configured: client: '%s', server: '",
383 if (SSL_get_SSL_CTX(server
) == s_ctx2
) {
384 BIO_printf(bio_stdout
, "%s'\n",
387 BIO_printf(bio_stdout
, "%s'\n",
394 * WARNING : below extension types are *NOT* IETF assigned, and could
395 * conflict if these types are reassigned and handled specially by OpenSSL
398 #define TACK_EXT_TYPE 62208
399 #define CUSTOM_EXT_TYPE_0 1000
400 #define CUSTOM_EXT_TYPE_1 1001
401 #define CUSTOM_EXT_TYPE_2 1002
402 #define CUSTOM_EXT_TYPE_3 1003
404 static const char custom_ext_cli_string
[] = "abc";
405 static const char custom_ext_srv_string
[] = "defg";
407 /* These set from cmdline */
408 static char *serverinfo_file
= NULL
;
409 static int serverinfo_sct
= 0;
410 static int serverinfo_tack
= 0;
412 /* These set based on extension callbacks */
413 static int serverinfo_sct_seen
= 0;
414 static int serverinfo_tack_seen
= 0;
415 static int serverinfo_other_seen
= 0;
417 /* This set from cmdline */
418 static int custom_ext
= 0;
420 /* This set based on extension callbacks */
421 static int custom_ext_error
= 0;
423 static int serverinfo_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
424 const unsigned char *in
, size_t inlen
,
427 if (ext_type
== TLSEXT_TYPE_signed_certificate_timestamp
)
428 serverinfo_sct_seen
++;
429 else if (ext_type
== TACK_EXT_TYPE
)
430 serverinfo_tack_seen
++;
432 serverinfo_other_seen
++;
436 static int verify_serverinfo(void)
438 if (serverinfo_sct
!= serverinfo_sct_seen
)
440 if (serverinfo_tack
!= serverinfo_tack_seen
)
442 if (serverinfo_other_seen
)
448 * Four test cases for custom extensions:
449 * 0 - no ClientHello extension or ServerHello response
450 * 1 - ClientHello with "abc", no response
451 * 2 - ClientHello with "abc", empty response
452 * 3 - ClientHello with "abc", "defg" response
455 static int custom_ext_0_cli_add_cb(SSL
*s
, unsigned int ext_type
,
456 const unsigned char **out
,
457 size_t *outlen
, int *al
, void *arg
)
459 if (ext_type
!= CUSTOM_EXT_TYPE_0
)
460 custom_ext_error
= 1;
461 return 0; /* Don't send an extension */
464 static int custom_ext_0_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
465 const unsigned char *in
,
466 size_t inlen
, int *al
, void *arg
)
471 static int custom_ext_1_cli_add_cb(SSL
*s
, unsigned int ext_type
,
472 const unsigned char **out
,
473 size_t *outlen
, int *al
, void *arg
)
475 if (ext_type
!= CUSTOM_EXT_TYPE_1
)
476 custom_ext_error
= 1;
477 *out
= (const unsigned char *)custom_ext_cli_string
;
478 *outlen
= strlen(custom_ext_cli_string
);
479 return 1; /* Send "abc" */
482 static int custom_ext_1_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
483 const unsigned char *in
,
484 size_t inlen
, int *al
, void *arg
)
489 static int custom_ext_2_cli_add_cb(SSL
*s
, unsigned int ext_type
,
490 const unsigned char **out
,
491 size_t *outlen
, int *al
, void *arg
)
493 if (ext_type
!= CUSTOM_EXT_TYPE_2
)
494 custom_ext_error
= 1;
495 *out
= (const unsigned char *)custom_ext_cli_string
;
496 *outlen
= strlen(custom_ext_cli_string
);
497 return 1; /* Send "abc" */
500 static int custom_ext_2_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
501 const unsigned char *in
,
502 size_t inlen
, int *al
, void *arg
)
504 if (ext_type
!= CUSTOM_EXT_TYPE_2
)
505 custom_ext_error
= 1;
507 custom_ext_error
= 1; /* Should be empty response */
511 static int custom_ext_3_cli_add_cb(SSL
*s
, unsigned int ext_type
,
512 const unsigned char **out
,
513 size_t *outlen
, int *al
, void *arg
)
515 if (ext_type
!= CUSTOM_EXT_TYPE_3
)
516 custom_ext_error
= 1;
517 *out
= (const unsigned char *)custom_ext_cli_string
;
518 *outlen
= strlen(custom_ext_cli_string
);
519 return 1; /* Send "abc" */
522 static int custom_ext_3_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
523 const unsigned char *in
,
524 size_t inlen
, int *al
, void *arg
)
526 if (ext_type
!= CUSTOM_EXT_TYPE_3
)
527 custom_ext_error
= 1;
528 if (inlen
!= strlen(custom_ext_srv_string
))
529 custom_ext_error
= 1;
530 if (memcmp(custom_ext_srv_string
, in
, inlen
) != 0)
531 custom_ext_error
= 1; /* Check for "defg" */
536 * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
539 static int custom_ext_0_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
540 const unsigned char *in
,
541 size_t inlen
, int *al
, void *arg
)
543 custom_ext_error
= 1;
547 /* 'add' callbacks are only called if the 'parse' callback is called */
548 static int custom_ext_0_srv_add_cb(SSL
*s
, unsigned int ext_type
,
549 const unsigned char **out
,
550 size_t *outlen
, int *al
, void *arg
)
552 /* Error: should not have been called */
553 custom_ext_error
= 1;
554 return 0; /* Don't send an extension */
557 static int custom_ext_1_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
558 const unsigned char *in
,
559 size_t inlen
, int *al
, void *arg
)
561 if (ext_type
!= CUSTOM_EXT_TYPE_1
)
562 custom_ext_error
= 1;
563 /* Check for "abc" */
564 if (inlen
!= strlen(custom_ext_cli_string
))
565 custom_ext_error
= 1;
566 if (memcmp(in
, custom_ext_cli_string
, inlen
) != 0)
567 custom_ext_error
= 1;
571 static int custom_ext_1_srv_add_cb(SSL
*s
, unsigned int ext_type
,
572 const unsigned char **out
,
573 size_t *outlen
, int *al
, void *arg
)
575 return 0; /* Don't send an extension */
578 static int custom_ext_2_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
579 const unsigned char *in
,
580 size_t inlen
, int *al
, void *arg
)
582 if (ext_type
!= CUSTOM_EXT_TYPE_2
)
583 custom_ext_error
= 1;
584 /* Check for "abc" */
585 if (inlen
!= strlen(custom_ext_cli_string
))
586 custom_ext_error
= 1;
587 if (memcmp(in
, custom_ext_cli_string
, inlen
) != 0)
588 custom_ext_error
= 1;
592 static int custom_ext_2_srv_add_cb(SSL
*s
, unsigned int ext_type
,
593 const unsigned char **out
,
594 size_t *outlen
, int *al
, void *arg
)
598 return 1; /* Send empty extension */
601 static int custom_ext_3_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
602 const unsigned char *in
,
603 size_t inlen
, int *al
, void *arg
)
605 if (ext_type
!= CUSTOM_EXT_TYPE_3
)
606 custom_ext_error
= 1;
607 /* Check for "abc" */
608 if (inlen
!= strlen(custom_ext_cli_string
))
609 custom_ext_error
= 1;
610 if (memcmp(in
, custom_ext_cli_string
, inlen
) != 0)
611 custom_ext_error
= 1;
615 static int custom_ext_3_srv_add_cb(SSL
*s
, unsigned int ext_type
,
616 const unsigned char **out
,
617 size_t *outlen
, int *al
, void *arg
)
619 *out
= (const unsigned char *)custom_ext_srv_string
;
620 *outlen
= strlen(custom_ext_srv_string
);
621 return 1; /* Send "defg" */
624 static char *cipher
= NULL
;
625 static char *ciphersuites
= NULL
;
626 static int verbose
= 0;
627 static int debug
= 0;
629 int doit_localhost(SSL
*s_ssl
, SSL
*c_ssl
, int family
,
630 long bytes
, clock_t *s_time
, clock_t *c_time
);
631 int doit_biopair(SSL
*s_ssl
, SSL
*c_ssl
, long bytes
, clock_t *s_time
,
633 int doit(SSL
*s_ssl
, SSL
*c_ssl
, long bytes
);
635 static void sv_usage(void)
637 fprintf(stderr
, "usage: ssltest [args ...]\n");
638 fprintf(stderr
, "\n");
639 fprintf(stderr
, " -server_auth - check server certificate\n");
640 fprintf(stderr
, " -client_auth - do client authentication\n");
641 fprintf(stderr
, " -v - more output\n");
642 fprintf(stderr
, " -d - debug output\n");
643 fprintf(stderr
, " -reuse - use session-id reuse\n");
644 fprintf(stderr
, " -num <val> - number of connections to perform\n");
646 " -bytes <val> - number of bytes to swap between client/server\n");
647 #ifndef OPENSSL_NO_DH
649 " -dhe512 - use 512 bit key for DHE (to test failure)\n");
651 " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
653 " -dhe2048 - use 2048 bit key (safe prime) for DHE (default, no-op)\n");
655 " -dhe4096 - use 4096 bit key (safe prime) for DHE\n");
657 fprintf(stderr
, " -no_dhe - disable DHE\n");
658 #ifndef OPENSSL_NO_EC
659 fprintf(stderr
, " -no_ecdhe - disable ECDHE\n");
661 #ifndef OPENSSL_NO_PSK
662 fprintf(stderr
, " -psk arg - PSK in hex (without 0x)\n");
664 #ifndef OPENSSL_NO_SSL3
665 fprintf(stderr
, " -ssl3 - use SSLv3\n");
667 #ifndef OPENSSL_NO_TLS1
668 fprintf(stderr
, " -tls1 - use TLSv1\n");
670 #ifndef OPENSSL_NO_TLS1_1
671 fprintf(stderr
, " -tls1_1 - use TLSv1.1\n");
673 #ifndef OPENSSL_NO_TLS1_2
674 fprintf(stderr
, " -tls1_2 - use TLSv1.2\n");
676 #ifndef OPENSSL_NO_DTLS
677 fprintf(stderr
, " -dtls - use DTLS\n");
678 #ifndef OPENSSL_NO_DTLS1
679 fprintf(stderr
, " -dtls1 - use DTLSv1\n");
681 #ifndef OPENSSL_NO_DTLS1_2
682 fprintf(stderr
, " -dtls12 - use DTLSv1.2\n");
685 fprintf(stderr
, " -CApath arg - PEM format directory of CA's\n");
686 fprintf(stderr
, " -CAfile arg - PEM format file of CA's\n");
687 fprintf(stderr
, " -s_cert arg - Server certificate file\n");
689 " -s_key arg - Server key file (default: same as -cert)\n");
690 fprintf(stderr
, " -c_cert arg - Client certificate file\n");
692 " -c_key arg - Client key file (default: same as -c_cert)\n");
693 fprintf(stderr
, " -cipher arg - The TLSv1.2 and below cipher list\n");
694 fprintf(stderr
, " -ciphersuites arg - The TLSv1.3 ciphersuites\n");
695 fprintf(stderr
, " -bio_pair - Use BIO pairs\n");
696 fprintf(stderr
, " -ipv4 - Use IPv4 connection on localhost\n");
697 fprintf(stderr
, " -ipv6 - Use IPv6 connection on localhost\n");
698 fprintf(stderr
, " -f - Test even cases that can't work\n");
700 " -time - measure processor time used by client and server\n");
701 fprintf(stderr
, " -zlib - use zlib compression\n");
702 #ifndef OPENSSL_NO_NEXTPROTONEG
703 fprintf(stderr
, " -npn_client - have client side offer NPN\n");
704 fprintf(stderr
, " -npn_server - have server side offer NPN\n");
705 fprintf(stderr
, " -npn_server_reject - have server reject NPN\n");
707 fprintf(stderr
, " -serverinfo_file file - have server use this file\n");
708 fprintf(stderr
, " -serverinfo_sct - have client offer and expect SCT\n");
710 " -serverinfo_tack - have client offer and expect TACK\n");
712 " -custom_ext - try various custom extension callbacks\n");
713 fprintf(stderr
, " -alpn_client <string> - have client side offer ALPN\n");
714 fprintf(stderr
, " -alpn_server <string> - have server side offer ALPN\n");
715 fprintf(stderr
, " -alpn_server1 <string> - alias for -alpn_server\n");
716 fprintf(stderr
, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
718 " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
719 fprintf(stderr
, " -server_min_proto <string> - Minimum version the server should support\n");
720 fprintf(stderr
, " -server_max_proto <string> - Maximum version the server should support\n");
721 fprintf(stderr
, " -client_min_proto <string> - Minimum version the client should support\n");
722 fprintf(stderr
, " -client_max_proto <string> - Maximum version the client should support\n");
723 fprintf(stderr
, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
724 #ifndef OPENSSL_NO_CT
725 fprintf(stderr
, " -noct - no certificate transparency\n");
726 fprintf(stderr
, " -requestct - request certificate transparency\n");
727 fprintf(stderr
, " -requirect - require certificate transparency\n");
729 fprintf(stderr
, " -sn_client <string> - have client request this servername\n");
730 fprintf(stderr
, " -sn_server1 <string> - have server context 1 respond to this servername\n");
731 fprintf(stderr
, " -sn_server2 <string> - have server context 2 respond to this servername\n");
732 fprintf(stderr
, " -sn_expect1 - expected server 1\n");
733 fprintf(stderr
, " -sn_expect2 - expected server 2\n");
734 fprintf(stderr
, " -server_sess_out <file> - Save the server session to a file\n");
735 fprintf(stderr
, " -server_sess_in <file> - Read the server session from a file\n");
736 fprintf(stderr
, " -client_sess_out <file> - Save the client session to a file\n");
737 fprintf(stderr
, " -client_sess_in <file> - Read the client session from a file\n");
738 fprintf(stderr
, " -should_reuse <number> - The expected state of reusing the session\n");
739 fprintf(stderr
, " -no_ticket - do not issue TLS session ticket\n");
740 fprintf(stderr
, " -client_ktls - try to enable client KTLS\n");
741 fprintf(stderr
, " -server_ktls - try to enable server KTLS\n");
742 fprintf(stderr
, " -provider <name> - Load the given provider into the library context\n");
743 fprintf(stderr
, " -config <cnf> - Load the given config file into the library context\n");
746 static void print_key_details(BIO
*out
, EVP_PKEY
*key
)
748 int keyid
= EVP_PKEY_get_id(key
);
750 #ifndef OPENSSL_NO_EC
751 if (keyid
== EVP_PKEY_EC
) {
755 if (!EVP_PKEY_get_group_name(key
, group
, sizeof(group
), &size
))
756 strcpy(group
, "unknown group");
757 BIO_printf(out
, "%d bits EC (%s)", EVP_PKEY_get_bits(key
), group
);
773 algname
= OBJ_nid2sn(keyid
);
776 BIO_printf(out
, "%d bits %s", EVP_PKEY_get_bits(key
), algname
);
780 static void print_details(SSL
*c_ssl
, const char *prefix
)
782 const SSL_CIPHER
*ciph
;
787 ciph
= SSL_get_current_cipher(c_ssl
);
788 BIO_printf(bio_stdout
, "%s%s, cipher %s %s",
790 SSL_get_version(c_ssl
),
791 SSL_CIPHER_get_version(ciph
), SSL_CIPHER_get_name(ciph
));
792 cert
= SSL_get0_peer_certificate(c_ssl
);
794 EVP_PKEY
* pubkey
= X509_get0_pubkey(cert
);
796 if (pubkey
!= NULL
) {
797 BIO_puts(bio_stdout
, ", ");
798 print_key_details(bio_stdout
, pubkey
);
801 if (SSL_get_peer_tmp_key(c_ssl
, &pkey
)) {
802 BIO_puts(bio_stdout
, ", temp key: ");
803 print_key_details(bio_stdout
, pkey
);
806 if (SSL_get_peer_signature_nid(c_ssl
, &mdnid
))
807 BIO_printf(bio_stdout
, ", digest=%s", OBJ_nid2sn(mdnid
));
808 BIO_printf(bio_stdout
, "\n");
812 * protocol_from_string - converts a protocol version string to a number
814 * Returns -1 on failure or the version on success
816 static int protocol_from_string(const char *value
)
818 struct protocol_versions
{
822 static const struct protocol_versions versions
[] = {
823 {"ssl3", SSL3_VERSION
},
824 {"tls1", TLS1_VERSION
},
825 {"tls1.1", TLS1_1_VERSION
},
826 {"tls1.2", TLS1_2_VERSION
},
827 {"tls1.3", TLS1_3_VERSION
},
828 {"dtls1", DTLS1_VERSION
},
829 {"dtls1.2", DTLS1_2_VERSION
}};
831 size_t n
= OSSL_NELEM(versions
);
833 for (i
= 0; i
< n
; i
++)
834 if (strcmp(versions
[i
].name
, value
) == 0)
835 return versions
[i
].version
;
839 static SSL_SESSION
*read_session(const char *filename
)
842 BIO
*f
= BIO_new_file(filename
, "r");
845 BIO_printf(bio_err
, "Can't open session file %s\n", filename
);
846 ERR_print_errors(bio_err
);
849 sess
= PEM_read_bio_SSL_SESSION(f
, NULL
, 0, NULL
);
851 BIO_printf(bio_err
, "Can't parse session file %s\n", filename
);
852 ERR_print_errors(bio_err
);
858 static int write_session(const char *filename
, SSL_SESSION
*sess
)
863 BIO_printf(bio_err
, "No session information\n");
867 f
= BIO_new_file(filename
, "w");
869 BIO_printf(bio_err
, "Can't open session file %s\n", filename
);
870 ERR_print_errors(bio_err
);
873 PEM_write_bio_SSL_SESSION(f
, sess
);
879 * set_protocol_version - Sets protocol version minimum or maximum
881 * Returns 0 on failure and 1 on success
883 static int set_protocol_version(const char *version
, SSL
*ssl
, int setting
)
885 if (version
!= NULL
) {
886 int ver
= protocol_from_string(version
);
888 BIO_printf(bio_err
, "Error parsing: %s\n", version
);
891 return SSL_ctrl(ssl
, setting
, ver
, NULL
);
896 int main(int argc
, char *argv
[])
898 const char *CApath
= NULL
, *CAfile
= NULL
;
900 enum { BIO_MEM
, BIO_PAIR
, BIO_IPV4
, BIO_IPV6
} bio_type
= BIO_MEM
;
902 int dtls1
= 0, dtls12
= 0, dtls
= 0, tls1
= 0, tls1_1
= 0, tls1_2
= 0, ssl3
= 0;
903 int ret
= EXIT_FAILURE
;
905 int server_auth
= 0, i
;
906 struct app_verify_arg app_verify_arg
=
907 { APP_CALLBACK_STRING
, 0 };
908 SSL_CTX
*c_ctx
= NULL
;
909 const SSL_METHOD
*meth
= NULL
;
912 int number
= 1, reuse
= 0;
913 int should_reuse
= -1;
915 int client_ktls
= 0, server_ktls
= 0;
917 #ifndef OPENSSL_NO_DH
919 int dhe512
= 0, dhe1024dsa
= 0, dhe4096
= 0;
924 clock_t s_time
= 0, c_time
= 0;
925 #ifndef OPENSSL_NO_COMP
927 COMP_METHOD
*cm
= NULL
;
928 STACK_OF(SSL_COMP
) *ssl_comp_methods
= NULL
;
931 int min_version
= 0, max_version
= 0;
932 #ifndef OPENSSL_NO_CT
934 * Disable CT validation by default, because it will interfere with
935 * anything using custom extension handlers to deal with SCT extensions.
937 int ct_validation
= 0;
939 SSL_CONF_CTX
*s_cctx
= NULL
, *c_cctx
= NULL
, *s_cctx2
= NULL
;
940 STACK_OF(OPENSSL_STRING
) *conf_args
= NULL
;
941 char *arg
= NULL
, *argn
= NULL
;
942 const char *provider
= NULL
, *config
= NULL
;
943 OSSL_PROVIDER
*thisprov
= NULL
, *defctxnull
= NULL
;
944 OSSL_LIB_CTX
*libctx
= NULL
;
951 bio_stdout
= BIO_new_fp(stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
953 s_cctx
= SSL_CONF_CTX_new();
954 s_cctx2
= SSL_CONF_CTX_new();
955 c_cctx
= SSL_CONF_CTX_new();
957 if (!s_cctx
|| !c_cctx
|| !s_cctx2
) {
958 ERR_print_errors(bio_err
);
962 SSL_CONF_CTX_set_flags(s_cctx
,
963 SSL_CONF_FLAG_CMDLINE
| SSL_CONF_FLAG_SERVER
|
964 SSL_CONF_FLAG_CERTIFICATE
|
965 SSL_CONF_FLAG_REQUIRE_PRIVATE
);
966 SSL_CONF_CTX_set_flags(s_cctx2
,
967 SSL_CONF_FLAG_CMDLINE
| SSL_CONF_FLAG_SERVER
|
968 SSL_CONF_FLAG_CERTIFICATE
|
969 SSL_CONF_FLAG_REQUIRE_PRIVATE
);
970 if (!SSL_CONF_CTX_set1_prefix(s_cctx
, "-s_")) {
971 ERR_print_errors(bio_err
);
974 if (!SSL_CONF_CTX_set1_prefix(s_cctx2
, "-s_")) {
975 ERR_print_errors(bio_err
);
979 SSL_CONF_CTX_set_flags(c_cctx
,
980 SSL_CONF_FLAG_CMDLINE
| SSL_CONF_FLAG_CLIENT
|
981 SSL_CONF_FLAG_CERTIFICATE
|
982 SSL_CONF_FLAG_REQUIRE_PRIVATE
);
983 if (!SSL_CONF_CTX_set1_prefix(c_cctx
, "-c_")) {
984 ERR_print_errors(bio_err
);
992 if (strcmp(*argv
, "-F") == 0) {
994 "not compiled with FIPS support, so exiting without running.\n");
997 } else if (strcmp(*argv
, "-server_auth") == 0)
999 else if (strcmp(*argv
, "-client_auth") == 0)
1001 else if (strcmp(*argv
, "-v") == 0)
1003 else if (strcmp(*argv
, "-d") == 0)
1005 else if (strcmp(*argv
, "-reuse") == 0)
1007 else if (strcmp(*argv
, "-no_dhe") == 0)
1008 #ifdef OPENSSL_NO_DH
1009 /* unused in this case */;
1012 else if (strcmp(*argv
, "-dhe512") == 0)
1014 else if (strcmp(*argv
, "-dhe1024dsa") == 0)
1016 else if (strcmp(*argv
, "-dhe4096") == 0)
1019 else if (strcmp(*argv
, "-no_ecdhe") == 0)
1021 else if (strcmp(*argv
, "-psk") == 0) {
1024 psk_key
= *(++argv
);
1025 #ifndef OPENSSL_NO_PSK
1026 if (strspn(psk_key
, "abcdefABCDEF1234567890") != strlen(psk_key
)) {
1027 BIO_printf(bio_err
, "Not a hex number '%s'\n", *argv
);
1034 else if (strcmp(*argv
, "-tls1_2") == 0) {
1036 } else if (strcmp(*argv
, "-tls1_1") == 0) {
1038 } else if (strcmp(*argv
, "-tls1") == 0) {
1040 } else if (strcmp(*argv
, "-ssl3") == 0) {
1042 } else if (strcmp(*argv
, "-dtls1") == 0) {
1044 } else if (strcmp(*argv
, "-dtls12") == 0) {
1046 } else if (strcmp(*argv
, "-dtls") == 0) {
1048 } else if (HAS_PREFIX(*argv
, "-num")) {
1051 number
= atoi(*(++argv
));
1054 } else if (strcmp(*argv
, "-bytes") == 0) {
1057 bytes
= atol(*(++argv
));
1060 i
= strlen(argv
[0]);
1061 if (argv
[0][i
- 1] == 'k')
1063 if (argv
[0][i
- 1] == 'm')
1064 bytes
*= 1024L * 1024L;
1065 } else if (strcmp(*argv
, "-cipher") == 0) {
1069 } else if (strcmp(*argv
, "-ciphersuites") == 0) {
1072 ciphersuites
= *(++argv
);
1073 } else if (strcmp(*argv
, "-CApath") == 0) {
1077 } else if (strcmp(*argv
, "-CAfile") == 0) {
1081 } else if (strcmp(*argv
, "-bio_pair") == 0) {
1082 bio_type
= BIO_PAIR
;
1084 #ifndef OPENSSL_NO_SOCK
1085 else if (strcmp(*argv
, "-ipv4") == 0) {
1086 bio_type
= BIO_IPV4
;
1087 } else if (strcmp(*argv
, "-ipv6") == 0) {
1088 bio_type
= BIO_IPV6
;
1091 else if (strcmp(*argv
, "-f") == 0) {
1093 } else if (strcmp(*argv
, "-time") == 0) {
1096 #ifndef OPENSSL_NO_CT
1097 else if (strcmp(*argv
, "-noct") == 0) {
1100 else if (strcmp(*argv
, "-ct") == 0) {
1104 #ifndef OPENSSL_NO_COMP
1105 else if (strcmp(*argv
, "-zlib") == 0) {
1109 else if (strcmp(*argv
, "-app_verify") == 0) {
1110 app_verify_arg
.app_verify
= 1;
1112 #ifndef OPENSSL_NO_NEXTPROTONEG
1113 else if (strcmp(*argv
, "-npn_client") == 0) {
1115 } else if (strcmp(*argv
, "-npn_server") == 0) {
1117 } else if (strcmp(*argv
, "-npn_server_reject") == 0) {
1118 npn_server_reject
= 1;
1121 else if (strcmp(*argv
, "-serverinfo_sct") == 0) {
1123 } else if (strcmp(*argv
, "-serverinfo_tack") == 0) {
1124 serverinfo_tack
= 1;
1125 } else if (strcmp(*argv
, "-serverinfo_file") == 0) {
1128 serverinfo_file
= *(++argv
);
1129 } else if (strcmp(*argv
, "-custom_ext") == 0) {
1131 } else if (strcmp(*argv
, "-alpn_client") == 0) {
1134 alpn_client
= *(++argv
);
1135 } else if (strcmp(*argv
, "-alpn_server") == 0 ||
1136 strcmp(*argv
, "-alpn_server1") == 0) {
1139 alpn_server
= *(++argv
);
1140 } else if (strcmp(*argv
, "-alpn_server2") == 0) {
1143 alpn_server2
= *(++argv
);
1144 } else if (strcmp(*argv
, "-alpn_expected") == 0) {
1147 alpn_expected
= *(++argv
);
1148 } else if (strcmp(*argv
, "-server_min_proto") == 0) {
1151 server_min_proto
= *(++argv
);
1152 } else if (strcmp(*argv
, "-server_max_proto") == 0) {
1155 server_max_proto
= *(++argv
);
1156 } else if (strcmp(*argv
, "-client_min_proto") == 0) {
1159 client_min_proto
= *(++argv
);
1160 } else if (strcmp(*argv
, "-client_max_proto") == 0) {
1163 client_max_proto
= *(++argv
);
1164 } else if (strcmp(*argv
, "-should_negotiate") == 0) {
1167 should_negotiate
= *(++argv
);
1168 } else if (strcmp(*argv
, "-sn_client") == 0) {
1171 sn_client
= *(++argv
);
1172 } else if (strcmp(*argv
, "-sn_server1") == 0) {
1175 sn_server1
= *(++argv
);
1176 } else if (strcmp(*argv
, "-sn_server2") == 0) {
1179 sn_server2
= *(++argv
);
1180 } else if (strcmp(*argv
, "-sn_expect1") == 0) {
1182 } else if (strcmp(*argv
, "-sn_expect2") == 0) {
1184 } else if (strcmp(*argv
, "-server_sess_out") == 0) {
1187 server_sess_out
= *(++argv
);
1188 } else if (strcmp(*argv
, "-server_sess_in") == 0) {
1191 server_sess_in
= *(++argv
);
1192 } else if (strcmp(*argv
, "-client_sess_out") == 0) {
1195 client_sess_out
= *(++argv
);
1196 } else if (strcmp(*argv
, "-client_sess_in") == 0) {
1199 client_sess_in
= *(++argv
);
1200 } else if (strcmp(*argv
, "-should_reuse") == 0) {
1203 should_reuse
= !!atoi(*(++argv
));
1204 } else if (strcmp(*argv
, "-no_ticket") == 0) {
1206 } else if (strcmp(*argv
, "-client_ktls") == 0) {
1208 } else if (strcmp(*argv
, "-server_ktls") == 0) {
1210 } else if (strcmp(*argv
, "-provider") == 0) {
1213 provider
= *(++argv
);
1214 } else if (strcmp(*argv
, "-config") == 0) {
1222 /* Try to process command using SSL_CONF */
1223 rv
= SSL_CONF_cmd_argv(c_cctx
, &argc
, &argv
);
1224 /* If not processed try server */
1226 rv
= SSL_CONF_cmd_argv(s_cctx
, &argc
, &argv
);
1227 /* Recognised: store it for later use */
1232 conf_args
= sk_OPENSSL_STRING_new_null();
1236 if (!sk_OPENSSL_STRING_push(conf_args
, arg
))
1238 if (!sk_OPENSSL_STRING_push(conf_args
, argn
))
1243 BIO_printf(bio_err
, "Missing argument for %s\n", arg
);
1245 BIO_printf(bio_err
, "Error with command %s\n", arg
);
1247 BIO_printf(bio_err
, "unknown option %s\n", arg
);
1260 if (ssl3
+ tls1
+ tls1_1
+ tls1_2
+ dtls
+ dtls1
+ dtls12
> 1) {
1261 fprintf(stderr
, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
1266 #ifdef OPENSSL_NO_SSL3
1271 #ifdef OPENSSL_NO_TLS1
1276 #ifdef OPENSSL_NO_TLS1_1
1281 #ifdef OPENSSL_NO_TLS1_2
1286 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
1291 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
1299 * Testing was requested for a compiled-out protocol (e.g. SSLv3).
1300 * Ideally, we would error out, but the generic test wrapper can't know
1301 * when to expect failure. So we do nothing and return success.
1304 fprintf(stderr
, "Testing was requested for a disabled protocol. "
1305 "Skipping tests.\n");
1310 if (!ssl3
&& !tls1
&& !tls1_1
&& !tls1_2
&& !dtls
&& !dtls1
&& !dtls12
&& number
> 1
1311 && !reuse
&& !force
) {
1312 fprintf(stderr
, "This case cannot work. Use -f to perform "
1313 "the test anyway (and\n-d to see what happens), "
1314 "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
1315 "to avoid protocol mismatch.\n");
1320 if (bio_type
== BIO_MEM
) {
1321 fprintf(stderr
, "Using BIO pair (-bio_pair)\n");
1322 bio_type
= BIO_PAIR
;
1324 if (number
< 50 && !force
)
1326 "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
1329 #ifndef OPENSSL_NO_COMP
1330 if (comp
== COMP_ZLIB
)
1333 if (SSL_COMP_add_compression_method(comp
, cm
) != 0) {
1334 fprintf(stderr
, "Failed to add compression method\n");
1335 ERR_print_errors_fp(stderr
);
1339 "Warning: %s compression not supported\n",
1340 comp
== COMP_ZLIB
? "zlib" : "unknown");
1341 ERR_print_errors_fp(stderr
);
1343 ssl_comp_methods
= SSL_COMP_get_compression_methods();
1344 n
= sk_SSL_COMP_num(ssl_comp_methods
);
1347 printf("Available compression methods:");
1348 for (j
= 0; j
< n
; j
++) {
1349 SSL_COMP
*c
= sk_SSL_COMP_value(ssl_comp_methods
, j
);
1350 printf(" %s:%d", SSL_COMP_get0_name(c
), SSL_COMP_get_id(c
));
1356 #ifndef OPENSSL_NO_TLS
1357 meth
= TLS_method();
1359 min_version
= SSL3_VERSION
;
1360 max_version
= SSL3_VERSION
;
1362 min_version
= TLS1_VERSION
;
1363 max_version
= TLS1_VERSION
;
1364 } else if (tls1_1
) {
1365 min_version
= TLS1_1_VERSION
;
1366 max_version
= TLS1_1_VERSION
;
1367 } else if (tls1_2
) {
1368 min_version
= TLS1_2_VERSION
;
1369 max_version
= TLS1_2_VERSION
;
1372 # if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)
1373 /* We only have ec and dh based built-in groups for TLSv1.3 */
1374 max_version
= TLS1_2_VERSION
;
1380 #ifndef OPENSSL_NO_DTLS
1381 if (dtls
|| dtls1
|| dtls12
) {
1382 meth
= DTLS_method();
1384 min_version
= DTLS1_VERSION
;
1385 max_version
= DTLS1_VERSION
;
1386 } else if (dtls12
) {
1387 min_version
= DTLS1_2_VERSION
;
1388 max_version
= DTLS1_2_VERSION
;
1396 if (provider
!= NULL
1397 && !test_get_libctx(&libctx
, &defctxnull
, config
, &thisprov
, provider
))
1400 c_ctx
= SSL_CTX_new_ex(libctx
, NULL
, meth
);
1401 s_ctx
= SSL_CTX_new_ex(libctx
, NULL
, meth
);
1402 s_ctx2
= SSL_CTX_new_ex(libctx
, NULL
, meth
); /* no SSL_CTX_dup! */
1403 if ((c_ctx
== NULL
) || (s_ctx
== NULL
) || (s_ctx2
== NULL
)) {
1404 ERR_print_errors(bio_err
);
1408 * Since we will use low security ciphersuites and keys for testing set
1409 * security level to zero by default. Tests can override this by adding
1410 * "@SECLEVEL=n" to the cipher string.
1412 SSL_CTX_set_security_level(c_ctx
, 0);
1413 SSL_CTX_set_security_level(s_ctx
, 0);
1414 SSL_CTX_set_security_level(s_ctx2
, 0);
1417 SSL_CTX_set_options(c_ctx
, SSL_OP_NO_TICKET
);
1418 SSL_CTX_set_options(s_ctx
, SSL_OP_NO_TICKET
);
1421 if (SSL_CTX_set_min_proto_version(c_ctx
, min_version
) == 0)
1423 if (SSL_CTX_set_max_proto_version(c_ctx
, max_version
) == 0)
1425 if (SSL_CTX_set_min_proto_version(s_ctx
, min_version
) == 0)
1427 if (SSL_CTX_set_max_proto_version(s_ctx
, max_version
) == 0)
1430 if (cipher
!= NULL
) {
1431 if (strcmp(cipher
, "") == 0) {
1432 if (!SSL_CTX_set_cipher_list(c_ctx
, cipher
)) {
1433 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH
) {
1436 ERR_print_errors(bio_err
);
1440 /* Should have failed when clearing all TLSv1.2 ciphers. */
1441 fprintf(stderr
, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1445 if (!SSL_CTX_set_cipher_list(s_ctx
, cipher
)) {
1446 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH
) {
1449 ERR_print_errors(bio_err
);
1453 /* Should have failed when clearing all TLSv1.2 ciphers. */
1454 fprintf(stderr
, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1458 if (!SSL_CTX_set_cipher_list(s_ctx2
, cipher
)) {
1459 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH
) {
1462 ERR_print_errors(bio_err
);
1466 /* Should have failed when clearing all TLSv1.2 ciphers. */
1467 fprintf(stderr
, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1471 if (!SSL_CTX_set_cipher_list(c_ctx
, cipher
)
1472 || !SSL_CTX_set_cipher_list(s_ctx
, cipher
)
1473 || !SSL_CTX_set_cipher_list(s_ctx2
, cipher
)) {
1474 ERR_print_errors(bio_err
);
1479 if (ciphersuites
!= NULL
) {
1480 if (!SSL_CTX_set_ciphersuites(c_ctx
, ciphersuites
)
1481 || !SSL_CTX_set_ciphersuites(s_ctx
, ciphersuites
)
1482 || !SSL_CTX_set_ciphersuites(s_ctx2
, ciphersuites
)) {
1483 ERR_print_errors(bio_err
);
1488 #ifndef OPENSSL_NO_CT
1489 if (ct_validation
&&
1490 !SSL_CTX_enable_ct(c_ctx
, SSL_CT_VALIDATION_STRICT
)) {
1491 ERR_print_errors(bio_err
);
1496 /* Process SSL_CONF arguments */
1497 SSL_CONF_CTX_set_ssl_ctx(c_cctx
, c_ctx
);
1498 SSL_CONF_CTX_set_ssl_ctx(s_cctx
, s_ctx
);
1499 SSL_CONF_CTX_set_ssl_ctx(s_cctx2
, s_ctx2
);
1501 for (i
= 0; i
< sk_OPENSSL_STRING_num(conf_args
); i
+= 2) {
1503 arg
= sk_OPENSSL_STRING_value(conf_args
, i
);
1504 argn
= sk_OPENSSL_STRING_value(conf_args
, i
+ 1);
1505 rv
= SSL_CONF_cmd(c_cctx
, arg
, argn
);
1506 /* If not recognised use server context */
1508 rv
= SSL_CONF_cmd(s_cctx2
, arg
, argn
);
1510 rv
= SSL_CONF_cmd(s_cctx
, arg
, argn
);
1513 BIO_printf(bio_err
, "Error processing %s %s\n",
1514 arg
, argn
? argn
: "");
1515 ERR_print_errors(bio_err
);
1520 if (!SSL_CONF_CTX_finish(s_cctx
) || !SSL_CONF_CTX_finish(c_cctx
) || !SSL_CONF_CTX_finish(s_cctx2
)) {
1521 BIO_puts(bio_err
, "Error finishing context\n");
1522 ERR_print_errors(bio_err
);
1525 #ifndef OPENSSL_NO_DH
1528 dhpkey
= get_dh1024dsa(libctx
);
1530 dhpkey
= get_dh512(libctx
);
1532 dhpkey
= get_dh4096(libctx
);
1534 dhpkey
= get_dh2048(libctx
);
1536 if (dhpkey
== NULL
|| !EVP_PKEY_up_ref(dhpkey
)) {
1537 EVP_PKEY_free(dhpkey
);
1538 BIO_puts(bio_err
, "Error getting DH parameters\n");
1539 ERR_print_errors(bio_err
);
1542 if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx
, dhpkey
))
1543 EVP_PKEY_free(dhpkey
);
1544 if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx2
, dhpkey
))
1545 EVP_PKEY_free(dhpkey
);
1549 if (!(SSL_CTX_load_verify_file(s_ctx
, CAfile
)
1550 || SSL_CTX_load_verify_dir(s_ctx
, CApath
))
1551 || !SSL_CTX_set_default_verify_paths(s_ctx
)
1552 || !(SSL_CTX_load_verify_file(s_ctx2
, CAfile
)
1553 || SSL_CTX_load_verify_dir(s_ctx2
, CApath
))
1554 || !SSL_CTX_set_default_verify_paths(s_ctx2
)
1555 || !(SSL_CTX_load_verify_file(c_ctx
, CAfile
)
1556 || SSL_CTX_load_verify_dir(c_ctx
, CApath
))
1557 || !SSL_CTX_set_default_verify_paths(c_ctx
)) {
1558 ERR_print_errors(bio_err
);
1561 #ifndef OPENSSL_NO_CT
1562 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx
) ||
1563 !SSL_CTX_set_default_ctlog_list_file(s_ctx2
) ||
1564 !SSL_CTX_set_default_ctlog_list_file(c_ctx
)) {
1565 ERR_print_errors(bio_err
);
1570 printf("client authentication\n");
1571 SSL_CTX_set_verify(s_ctx
,
1572 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
,
1574 SSL_CTX_set_verify(s_ctx2
,
1575 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
,
1577 SSL_CTX_set_cert_verify_callback(s_ctx
, app_verify_callback
,
1579 SSL_CTX_set_cert_verify_callback(s_ctx2
, app_verify_callback
,
1583 printf("server authentication\n");
1584 SSL_CTX_set_verify(c_ctx
, SSL_VERIFY_PEER
, verify_callback
);
1585 SSL_CTX_set_cert_verify_callback(c_ctx
, app_verify_callback
,
1590 int session_id_context
= 0;
1591 if (!SSL_CTX_set_session_id_context(s_ctx
, (void *)&session_id_context
,
1592 sizeof(session_id_context
)) ||
1593 !SSL_CTX_set_session_id_context(s_ctx2
, (void *)&session_id_context
,
1594 sizeof(session_id_context
))) {
1595 ERR_print_errors(bio_err
);
1600 /* Use PSK only if PSK key is given */
1601 if (psk_key
!= NULL
) {
1603 * no_psk is used to avoid putting psk command to openssl tool
1607 * if PSK is not compiled in and psk key is given, do nothing and
1613 #ifndef OPENSSL_NO_PSK
1614 SSL_CTX_set_psk_client_callback(c_ctx
, psk_client_callback
);
1615 SSL_CTX_set_psk_server_callback(s_ctx
, psk_server_callback
);
1616 SSL_CTX_set_psk_server_callback(s_ctx2
, psk_server_callback
);
1618 BIO_printf(bio_err
, "setting PSK identity hint to s_ctx\n");
1619 if (!SSL_CTX_use_psk_identity_hint(s_ctx
, "ctx server identity_hint") ||
1620 !SSL_CTX_use_psk_identity_hint(s_ctx2
, "ctx server identity_hint")) {
1621 BIO_printf(bio_err
, "error setting PSK identity hint to s_ctx\n");
1622 ERR_print_errors(bio_err
);
1628 #ifndef OPENSSL_NO_NEXTPROTONEG
1630 SSL_CTX_set_next_proto_select_cb(c_ctx
, cb_client_npn
, NULL
);
1633 if (npn_server_reject
) {
1635 "Can't have both -npn_server and -npn_server_reject\n");
1638 SSL_CTX_set_npn_advertised_cb(s_ctx
, cb_server_npn
, NULL
);
1639 SSL_CTX_set_npn_advertised_cb(s_ctx2
, cb_server_npn
, NULL
);
1641 if (npn_server_reject
) {
1642 SSL_CTX_set_npn_advertised_cb(s_ctx
, cb_server_rejects_npn
, NULL
);
1643 SSL_CTX_set_npn_advertised_cb(s_ctx2
, cb_server_rejects_npn
, NULL
);
1647 if (serverinfo_sct
) {
1648 if (!SSL_CTX_add_client_custom_ext(c_ctx
,
1649 TLSEXT_TYPE_signed_certificate_timestamp
,
1651 serverinfo_cli_parse_cb
, NULL
)) {
1652 BIO_printf(bio_err
, "Error adding SCT extension\n");
1656 if (serverinfo_tack
) {
1657 if (!SSL_CTX_add_client_custom_ext(c_ctx
, TACK_EXT_TYPE
,
1659 serverinfo_cli_parse_cb
, NULL
)) {
1660 BIO_printf(bio_err
, "Error adding TACK extension\n");
1664 if (serverinfo_file
)
1665 if (!SSL_CTX_use_serverinfo_file(s_ctx
, serverinfo_file
) ||
1666 !SSL_CTX_use_serverinfo_file(s_ctx2
, serverinfo_file
)) {
1667 BIO_printf(bio_err
, "missing serverinfo file\n");
1672 if (!SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_0
,
1673 custom_ext_0_cli_add_cb
,
1675 custom_ext_0_cli_parse_cb
, NULL
)
1676 || !SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_1
,
1677 custom_ext_1_cli_add_cb
,
1679 custom_ext_1_cli_parse_cb
, NULL
)
1680 || !SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_2
,
1681 custom_ext_2_cli_add_cb
,
1683 custom_ext_2_cli_parse_cb
, NULL
)
1684 || !SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_3
,
1685 custom_ext_3_cli_add_cb
,
1687 custom_ext_3_cli_parse_cb
, NULL
)
1688 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_0
,
1689 custom_ext_0_srv_add_cb
,
1691 custom_ext_0_srv_parse_cb
, NULL
)
1692 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_0
,
1693 custom_ext_0_srv_add_cb
,
1695 custom_ext_0_srv_parse_cb
, NULL
)
1696 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_1
,
1697 custom_ext_1_srv_add_cb
,
1699 custom_ext_1_srv_parse_cb
, NULL
)
1700 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_1
,
1701 custom_ext_1_srv_add_cb
,
1703 custom_ext_1_srv_parse_cb
, NULL
)
1704 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_2
,
1705 custom_ext_2_srv_add_cb
,
1707 custom_ext_2_srv_parse_cb
, NULL
)
1708 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_2
,
1709 custom_ext_2_srv_add_cb
,
1711 custom_ext_2_srv_parse_cb
, NULL
)
1712 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_3
,
1713 custom_ext_3_srv_add_cb
,
1715 custom_ext_3_srv_parse_cb
, NULL
)
1716 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_3
,
1717 custom_ext_3_srv_add_cb
,
1719 custom_ext_3_srv_parse_cb
, NULL
)) {
1720 BIO_printf(bio_err
, "Error setting custom extensions\n");
1726 SSL_CTX_set_alpn_select_cb(s_ctx
, cb_server_alpn
, alpn_server
);
1728 SSL_CTX_set_alpn_select_cb(s_ctx2
, cb_server_alpn
, alpn_server2
);
1732 unsigned char *alpn
= next_protos_parse(&alpn_len
, alpn_client
);
1735 BIO_printf(bio_err
, "Error parsing -alpn_client argument\n");
1738 /* Returns 0 on success!! */
1739 if (SSL_CTX_set_alpn_protos(c_ctx
, alpn
, alpn_len
)) {
1740 BIO_printf(bio_err
, "Error setting ALPN\n");
1747 if (server_sess_in
!= NULL
) {
1748 server_sess
= read_session(server_sess_in
);
1749 if (server_sess
== NULL
)
1752 if (client_sess_in
!= NULL
) {
1753 client_sess
= read_session(client_sess_in
);
1754 if (client_sess
== NULL
)
1758 if (server_sess_out
!= NULL
|| server_sess_in
!= NULL
) {
1762 /* Use a fixed key so that we can decrypt the ticket. */
1763 size
= SSL_CTX_set_tlsext_ticket_keys(s_ctx
, NULL
, 0);
1764 keys
= OPENSSL_zalloc(size
);
1767 SSL_CTX_set_tlsext_ticket_keys(s_ctx
, keys
, size
);
1771 if (sn_server1
!= NULL
|| sn_server2
!= NULL
)
1772 SSL_CTX_set_tlsext_servername_callback(s_ctx
, servername_cb
);
1774 c_ssl
= SSL_new(c_ctx
);
1775 s_ssl
= SSL_new(s_ctx
);
1776 if (c_ssl
== NULL
|| s_ssl
== NULL
)
1780 SSL_set_tlsext_host_name(c_ssl
, sn_client
);
1782 SSL_set_options(c_ssl
, SSL_OP_ENABLE_KTLS
);
1784 SSL_set_options(s_ssl
, SSL_OP_ENABLE_KTLS
);
1786 if (!set_protocol_version(server_min_proto
, s_ssl
, SSL_CTRL_SET_MIN_PROTO_VERSION
))
1788 if (!set_protocol_version(server_max_proto
, s_ssl
, SSL_CTRL_SET_MAX_PROTO_VERSION
))
1790 if (!set_protocol_version(client_min_proto
, c_ssl
, SSL_CTRL_SET_MIN_PROTO_VERSION
))
1792 if (!set_protocol_version(client_max_proto
, c_ssl
, SSL_CTRL_SET_MAX_PROTO_VERSION
))
1796 if (SSL_CTX_add_session(s_ctx
, server_sess
) == 0) {
1797 BIO_printf(bio_err
, "Can't add server session\n");
1798 ERR_print_errors(bio_err
);
1803 BIO_printf(bio_stdout
, "Doing handshakes=%d bytes=%ld\n", number
, bytes
);
1804 for (i
= 0; i
< number
; i
++) {
1806 if (!SSL_set_session(c_ssl
, NULL
)) {
1807 BIO_printf(bio_err
, "Failed to set session\n");
1811 if (client_sess_in
!= NULL
) {
1812 if (SSL_set_session(c_ssl
, client_sess
) == 0) {
1813 BIO_printf(bio_err
, "Can't set client session\n");
1814 ERR_print_errors(bio_err
);
1820 ret
= doit(s_ssl
, c_ssl
, bytes
);
1823 ret
= doit_biopair(s_ssl
, c_ssl
, bytes
, &s_time
, &c_time
);
1825 #ifndef OPENSSL_NO_SOCK
1827 ret
= doit_localhost(s_ssl
, c_ssl
, BIO_FAMILY_IPV4
,
1828 bytes
, &s_time
, &c_time
);
1831 ret
= doit_localhost(s_ssl
, c_ssl
, BIO_FAMILY_IPV6
,
1832 bytes
, &s_time
, &c_time
);
1841 if (ret
!= EXIT_SUCCESS
)
1845 if (should_negotiate
&& ret
== EXIT_SUCCESS
&&
1846 strcmp(should_negotiate
, "fail-server") != 0 &&
1847 strcmp(should_negotiate
, "fail-client") != 0) {
1848 int version
= protocol_from_string(should_negotiate
);
1850 BIO_printf(bio_err
, "Error parsing: %s\n", should_negotiate
);
1854 if (SSL_version(c_ssl
) != version
) {
1855 BIO_printf(bio_err
, "Unexpected version negotiated. "
1856 "Expected: %s, got %s\n", should_negotiate
, SSL_get_version(c_ssl
));
1862 if (should_reuse
!= -1) {
1863 if (SSL_session_reused(s_ssl
) != should_reuse
||
1864 SSL_session_reused(c_ssl
) != should_reuse
) {
1865 BIO_printf(bio_err
, "Unexpected session reuse state. "
1866 "Expected: %d, server: %d, client: %d\n", should_reuse
,
1867 SSL_session_reused(s_ssl
), SSL_session_reused(c_ssl
));
1873 if (server_sess_out
!= NULL
) {
1874 if (write_session(server_sess_out
, SSL_get_session(s_ssl
)) == 0) {
1879 if (client_sess_out
!= NULL
) {
1880 if (write_session(client_sess_out
, SSL_get_session(c_ssl
)) == 0) {
1887 print_details(c_ssl
, "");
1890 #ifdef CLOCKS_PER_SEC
1892 * "To determine the time in seconds, the value returned by the clock
1893 * function should be divided by the value of the macro
1894 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1896 BIO_printf(bio_stdout
, "Approximate total server time: %6.2f s\n"
1897 "Approximate total client time: %6.2f s\n",
1898 (double)s_time
/ CLOCKS_PER_SEC
,
1899 (double)c_time
/ CLOCKS_PER_SEC
);
1901 BIO_printf(bio_stdout
,
1902 "Approximate total server time: %6.2f units\n"
1903 "Approximate total client time: %6.2f units\n",
1904 (double)s_time
, (double)c_time
);
1911 SSL_CTX_free(s_ctx
);
1912 SSL_CTX_free(s_ctx2
);
1913 SSL_CTX_free(c_ctx
);
1914 SSL_CONF_CTX_free(s_cctx
);
1915 SSL_CONF_CTX_free(s_cctx2
);
1916 SSL_CONF_CTX_free(c_cctx
);
1917 sk_OPENSSL_STRING_free(conf_args
);
1919 BIO_free(bio_stdout
);
1921 SSL_SESSION_free(server_sess
);
1922 SSL_SESSION_free(client_sess
);
1924 OSSL_PROVIDER_unload(defctxnull
);
1925 OSSL_PROVIDER_unload(thisprov
);
1926 OSSL_LIB_CTX_free(libctx
);
1928 test_close_streams();
1933 #ifndef OPENSSL_NO_SOCK
1934 int doit_localhost(SSL
*s_ssl
, SSL
*c_ssl
, int family
, long count
,
1935 clock_t *s_time
, clock_t *c_time
)
1937 long cw_num
= count
, cr_num
= count
, sw_num
= count
, sr_num
= count
;
1938 BIO
*s_ssl_bio
= NULL
, *c_ssl_bio
= NULL
;
1939 BIO
*acpt
= NULL
, *server
= NULL
, *client
= NULL
;
1941 int ret
= EXIT_FAILURE
;
1942 int err_in_client
= 0;
1943 int err_in_server
= 0;
1945 acpt
= BIO_new_accept(family
== BIO_FAMILY_IPV4
? "127.0.0.1:0"
1949 BIO_set_accept_ip_family(acpt
, family
);
1950 BIO_set_bind_mode(acpt
, BIO_SOCK_NONBLOCK
| BIO_SOCK_REUSEADDR
);
1951 if (BIO_do_accept(acpt
) <= 0)
1954 BIO_snprintf(addr_str
, sizeof(addr_str
), ":%s", BIO_get_accept_port(acpt
));
1956 client
= BIO_new_connect(addr_str
);
1959 BIO_set_conn_ip_family(client
, family
);
1961 if (BIO_set_nbio(client
, 1) <= 0)
1963 if (BIO_set_nbio(acpt
, 1) <= 0)
1967 int st_connect
= 0, st_accept
= 0;
1969 while (!st_connect
|| !st_accept
) {
1971 if (BIO_do_connect(client
) <= 0) {
1972 if (!BIO_should_retry(client
))
1979 if (BIO_do_accept(acpt
) <= 0) {
1980 if (!BIO_should_retry(acpt
))
1988 /* We're not interested in accepting further connects */
1989 server
= BIO_pop(acpt
);
1993 s_ssl_bio
= BIO_new(BIO_f_ssl());
1997 c_ssl_bio
= BIO_new(BIO_f_ssl());
2001 SSL_set_connect_state(c_ssl
);
2002 SSL_set_bio(c_ssl
, client
, client
);
2003 (void)BIO_set_ssl(c_ssl_bio
, c_ssl
, BIO_NOCLOSE
);
2005 SSL_set_accept_state(s_ssl
);
2006 SSL_set_bio(s_ssl
, server
, server
);
2007 (void)BIO_set_ssl(s_ssl_bio
, s_ssl
, BIO_NOCLOSE
);
2011 * c_ssl_bio: SSL filter BIO
2013 * client: I/O for SSL library
2016 * server: I/O for SSL library
2018 * s_ssl_bio: SSL filter BIO
2022 * We have non-blocking behaviour throughout this test program, but
2023 * can be sure that there is *some* progress in each iteration; so we
2024 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2025 * we just try everything in each iteration
2031 char cbuf
[1024 * 8];
2033 clock_t c_clock
= clock();
2035 memset(cbuf
, 0, sizeof(cbuf
));
2038 if (SSL_in_init(c_ssl
))
2039 printf("client waiting in SSL_connect - %s\n",
2040 SSL_state_string_long(c_ssl
));
2043 /* Write to server. */
2045 if (cw_num
> (long)sizeof(cbuf
))
2049 r
= BIO_write(c_ssl_bio
, cbuf
, i
);
2051 if (!BIO_should_retry(c_ssl_bio
)) {
2052 fprintf(stderr
, "ERROR in CLIENT (write)\n");
2057 * BIO_should_retry(...) can just be ignored here. The
2058 * library expects us to call BIO_write with the same
2059 * arguments again, and that's what we will do in the
2062 } else if (r
== 0) {
2063 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2067 printf("client wrote %d\n", r
);
2073 /* Read from server. */
2075 r
= BIO_read(c_ssl_bio
, cbuf
, sizeof(cbuf
));
2077 if (!BIO_should_retry(c_ssl_bio
)) {
2078 fprintf(stderr
, "ERROR in CLIENT (read)\n");
2083 * Again, "BIO_should_retry" can be ignored.
2085 } else if (r
== 0) {
2086 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2090 printf("client read %d\n", r
);
2096 * c_time and s_time increments will typically be very small
2097 * (depending on machine speed and clock tick intervals), but
2098 * sampling over a large number of connections should result in
2099 * fairly accurate figures. We cannot guarantee a lot, however
2100 * -- if each connection lasts for exactly one clock tick, it
2101 * will be counted only for the client or only for the server or
2104 *c_time
+= (clock() - c_clock
);
2110 char sbuf
[1024 * 8];
2112 clock_t s_clock
= clock();
2114 memset(sbuf
, 0, sizeof(sbuf
));
2117 if (SSL_in_init(s_ssl
))
2118 printf("server waiting in SSL_accept - %s\n",
2119 SSL_state_string_long(s_ssl
));
2122 /* Write to client. */
2124 if (sw_num
> (long)sizeof(sbuf
))
2128 r
= BIO_write(s_ssl_bio
, sbuf
, i
);
2130 if (!BIO_should_retry(s_ssl_bio
)) {
2131 fprintf(stderr
, "ERROR in SERVER (write)\n");
2135 /* Ignore "BIO_should_retry". */
2136 } else if (r
== 0) {
2137 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2141 printf("server wrote %d\n", r
);
2147 /* Read from client. */
2149 r
= BIO_read(s_ssl_bio
, sbuf
, sizeof(sbuf
));
2151 if (!BIO_should_retry(s_ssl_bio
)) {
2152 fprintf(stderr
, "ERROR in SERVER (read)\n");
2157 } else if (r
== 0) {
2158 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2162 printf("server read %d\n", r
);
2167 *s_time
+= (clock() - s_clock
);
2170 while (cw_num
> 0 || cr_num
> 0 || sw_num
> 0 || sr_num
> 0);
2173 print_details(c_ssl
, "DONE via TCP connect: ");
2175 if (BIO_get_ktls_send(SSL_get_wbio(s_ssl
))
2176 && BIO_get_ktls_recv(SSL_get_rbio(s_ssl
)))
2177 BIO_printf(bio_stdout
, "Server using Kernel TLS in both directions\n");
2178 else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl
)))
2179 BIO_printf(bio_stdout
, "Server using Kernel TLS for sending\n");
2180 else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl
)))
2181 BIO_printf(bio_stdout
, "Server using Kernel TLS for receiving\n");
2183 if (BIO_get_ktls_send(SSL_get_wbio(c_ssl
))
2184 && BIO_get_ktls_recv(SSL_get_rbio(c_ssl
)))
2185 BIO_printf(bio_stdout
, "Client using Kernel TLS in both directions\n");
2186 else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl
)))
2187 BIO_printf(bio_stdout
, "Client using Kernel TLS for sending\n");
2188 else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl
)))
2189 BIO_printf(bio_stdout
, "Client using Kernel TLS for receiving\n");
2191 # ifndef OPENSSL_NO_NEXTPROTONEG
2192 if (verify_npn(c_ssl
, s_ssl
) < 0)
2195 if (verify_serverinfo() < 0) {
2196 fprintf(stderr
, "Server info verify error\n");
2199 if (verify_alpn(c_ssl
, s_ssl
) < 0
2200 || verify_servername(c_ssl
, s_ssl
) < 0)
2203 if (custom_ext_error
) {
2204 fprintf(stderr
, "Custom extension error\n");
2208 # ifndef OPENSSL_NO_NEXTPROTONEG
2214 ERR_print_errors(bio_err
);
2219 BIO_free(s_ssl_bio
);
2220 BIO_free(c_ssl_bio
);
2222 if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-client") == 0)
2223 ret
= (err_in_client
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2224 else if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-server") == 0)
2225 ret
= (err_in_server
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2231 int doit_biopair(SSL
*s_ssl
, SSL
*c_ssl
, long count
,
2232 clock_t *s_time
, clock_t *c_time
)
2234 long cw_num
= count
, cr_num
= count
, sw_num
= count
, sr_num
= count
;
2235 BIO
*s_ssl_bio
= NULL
, *c_ssl_bio
= NULL
;
2236 BIO
*server
= NULL
, *server_io
= NULL
, *client
= NULL
, *client_io
= NULL
;
2237 int ret
= EXIT_FAILURE
;
2238 int err_in_client
= 0;
2239 int err_in_server
= 0;
2241 size_t bufsiz
= 256; /* small buffer for testing */
2243 if (!BIO_new_bio_pair(&server
, bufsiz
, &server_io
, bufsiz
))
2245 if (!BIO_new_bio_pair(&client
, bufsiz
, &client_io
, bufsiz
))
2248 s_ssl_bio
= BIO_new(BIO_f_ssl());
2252 c_ssl_bio
= BIO_new(BIO_f_ssl());
2256 SSL_set_connect_state(c_ssl
);
2257 SSL_set_bio(c_ssl
, client
, client
);
2258 (void)BIO_set_ssl(c_ssl_bio
, c_ssl
, BIO_NOCLOSE
);
2260 SSL_set_accept_state(s_ssl
);
2261 SSL_set_bio(s_ssl
, server
, server
);
2262 (void)BIO_set_ssl(s_ssl_bio
, s_ssl
, BIO_NOCLOSE
);
2266 * c_ssl_bio: SSL filter BIO
2268 * client: pseudo-I/O for SSL library
2270 * client_io: client's SSL communication; usually to be
2271 * relayed over some I/O facility, but in this
2272 * test program, we're the server, too:
2274 * server_io: server's SSL communication
2276 * server: pseudo-I/O for SSL library
2278 * s_ssl_bio: SSL filter BIO
2280 * The client and the server each employ a "BIO pair":
2281 * client + client_io, server + server_io.
2282 * BIO pairs are symmetric. A BIO pair behaves similar
2283 * to a non-blocking socketpair (but both endpoints must
2284 * be handled by the same thread).
2285 * [Here we could connect client and server to the ends
2286 * of a single BIO pair, but then this code would be less
2287 * suitable as an example for BIO pairs in general.]
2289 * Useful functions for querying the state of BIO pair endpoints:
2291 * BIO_ctrl_pending(bio) number of bytes we can read now
2292 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
2293 * other side's read attempt
2294 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2296 * ..._read_request is never more than ..._write_guarantee;
2297 * it depends on the application which one you should use.
2301 * We have non-blocking behaviour throughout this test program, but
2302 * can be sure that there is *some* progress in each iteration; so we
2303 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2304 * we just try everything in each iteration
2310 char cbuf
[1024 * 8];
2312 clock_t c_clock
= clock();
2314 memset(cbuf
, 0, sizeof(cbuf
));
2317 if (SSL_in_init(c_ssl
))
2318 printf("client waiting in SSL_connect - %s\n",
2319 SSL_state_string_long(c_ssl
));
2322 /* Write to server. */
2324 if (cw_num
> (long)sizeof(cbuf
))
2328 r
= BIO_write(c_ssl_bio
, cbuf
, i
);
2330 if (!BIO_should_retry(c_ssl_bio
)) {
2331 fprintf(stderr
, "ERROR in CLIENT\n");
2336 * BIO_should_retry(...) can just be ignored here. The
2337 * library expects us to call BIO_write with the same
2338 * arguments again, and that's what we will do in the
2341 } else if (r
== 0) {
2342 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2346 printf("client wrote %d\n", r
);
2352 /* Read from server. */
2354 r
= BIO_read(c_ssl_bio
, cbuf
, sizeof(cbuf
));
2356 if (!BIO_should_retry(c_ssl_bio
)) {
2357 fprintf(stderr
, "ERROR in CLIENT\n");
2362 * Again, "BIO_should_retry" can be ignored.
2364 } else if (r
== 0) {
2365 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2369 printf("client read %d\n", r
);
2375 * c_time and s_time increments will typically be very small
2376 * (depending on machine speed and clock tick intervals), but
2377 * sampling over a large number of connections should result in
2378 * fairly accurate figures. We cannot guarantee a lot, however
2379 * -- if each connection lasts for exactly one clock tick, it
2380 * will be counted only for the client or only for the server or
2383 *c_time
+= (clock() - c_clock
);
2389 char sbuf
[1024 * 8];
2391 clock_t s_clock
= clock();
2393 memset(sbuf
, 0, sizeof(sbuf
));
2396 if (SSL_in_init(s_ssl
))
2397 printf("server waiting in SSL_accept - %s\n",
2398 SSL_state_string_long(s_ssl
));
2401 /* Write to client. */
2403 if (sw_num
> (long)sizeof(sbuf
))
2407 r
= BIO_write(s_ssl_bio
, sbuf
, i
);
2409 if (!BIO_should_retry(s_ssl_bio
)) {
2410 fprintf(stderr
, "ERROR in SERVER\n");
2414 /* Ignore "BIO_should_retry". */
2415 } else if (r
== 0) {
2416 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2420 printf("server wrote %d\n", r
);
2426 /* Read from client. */
2428 r
= BIO_read(s_ssl_bio
, sbuf
, sizeof(sbuf
));
2430 if (!BIO_should_retry(s_ssl_bio
)) {
2431 fprintf(stderr
, "ERROR in SERVER\n");
2436 } else if (r
== 0) {
2437 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2441 printf("server read %d\n", r
);
2446 *s_time
+= (clock() - s_clock
);
2450 /* "I/O" BETWEEN CLIENT AND SERVER. */
2453 BIO
*io1
= server_io
, *io2
= client_io
;
2455 * we use the non-copying interface for io1 and the standard
2456 * BIO_write/BIO_read interface for io2
2459 static int prev_progress
= 1;
2467 r1
= BIO_ctrl_pending(io1
);
2468 r2
= BIO_ctrl_get_write_guarantee(io2
);
2476 if (INT_MAX
< num
) /* yeah, right */
2479 r
= BIO_nread(io1
, &dataptr
, (int)num
);
2481 assert(r
<= (int)num
);
2483 * possibly r < num (non-contiguous data)
2486 r
= BIO_write(io2
, dataptr
, (int)num
);
2487 if (r
!= (int)num
) { /* can't happen */
2488 fprintf(stderr
, "ERROR: BIO_write could not write "
2489 "BIO_ctrl_get_write_guarantee() bytes");
2495 printf((io1
== client_io
) ?
2496 "C->S relaying: %d bytes\n" :
2497 "S->C relaying: %d bytes\n", (int)num
);
2507 r1
= BIO_ctrl_pending(io2
);
2508 r2
= BIO_ctrl_get_read_request(io1
);
2510 * here we could use ..._get_write_guarantee instead of
2511 * ..._get_read_request, but by using the latter we test
2512 * restartability of the SSL implementation more thoroughly
2524 --num
; /* test restartability even more thoroughly */
2526 r
= BIO_nwrite0(io1
, &dataptr
);
2530 r
= BIO_read(io2
, dataptr
, (int)num
);
2531 if (r
!= (int)num
) { /* can't happen */
2532 fprintf(stderr
, "ERROR: BIO_read could not read "
2533 "BIO_ctrl_pending() bytes");
2537 r
= BIO_nwrite(io1
, &dataptr
, (int)num
);
2538 if (r
!= (int)num
) { /* can't happen */
2539 fprintf(stderr
, "ERROR: BIO_nwrite() did not accept "
2540 "BIO_nwrite0() bytes");
2545 printf((io2
== client_io
) ?
2546 "C->S relaying: %d bytes\n" :
2547 "S->C relaying: %d bytes\n", (int)num
);
2549 } /* no loop, BIO_ctrl_get_read_request now
2550 * returns 0 anyway */
2552 if (!progress
&& !prev_progress
)
2553 if (cw_num
> 0 || cr_num
> 0 || sw_num
> 0 || sr_num
> 0) {
2554 fprintf(stderr
, "ERROR: got stuck\n");
2555 fprintf(stderr
, " ERROR.\n");
2558 prev_progress
= progress
;
2561 while (cw_num
> 0 || cr_num
> 0 || sw_num
> 0 || sr_num
> 0);
2564 print_details(c_ssl
, "DONE via BIO pair: ");
2565 #ifndef OPENSSL_NO_NEXTPROTONEG
2566 if (verify_npn(c_ssl
, s_ssl
) < 0)
2569 if (verify_serverinfo() < 0) {
2570 fprintf(stderr
, "Server info verify error\n");
2573 if (verify_alpn(c_ssl
, s_ssl
) < 0
2574 || verify_servername(c_ssl
, s_ssl
) < 0)
2577 if (custom_ext_error
) {
2578 fprintf(stderr
, "Custom extension error\n");
2582 #ifndef OPENSSL_NO_NEXTPROTONEG
2588 ERR_print_errors(bio_err
);
2591 BIO_free(server_io
);
2593 BIO_free(client_io
);
2594 BIO_free(s_ssl_bio
);
2595 BIO_free(c_ssl_bio
);
2597 if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-client") == 0)
2598 ret
= (err_in_client
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2599 else if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-server") == 0)
2600 ret
= (err_in_server
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2610 int doit(SSL
*s_ssl
, SSL
*c_ssl
, long count
)
2612 char *cbuf
= NULL
, *sbuf
= NULL
;
2614 long cw_num
= count
, cr_num
= count
;
2615 long sw_num
= count
, sr_num
= count
;
2616 int ret
= EXIT_FAILURE
;
2621 int c_r
, c_w
, s_r
, s_w
;
2624 int c_write
, s_write
;
2625 int do_server
= 0, do_client
= 0;
2626 int max_frag
= 5 * 1024;
2627 int err_in_client
= 0;
2628 int err_in_server
= 0;
2630 bufsiz
= count
> 40 * 1024 ? 40 * 1024 : count
;
2632 if ((cbuf
= OPENSSL_zalloc(bufsiz
)) == NULL
)
2634 if ((sbuf
= OPENSSL_zalloc(bufsiz
)) == NULL
)
2637 c_to_s
= BIO_new(BIO_s_mem());
2638 s_to_c
= BIO_new(BIO_s_mem());
2639 if ((s_to_c
== NULL
) || (c_to_s
== NULL
)) {
2640 ERR_print_errors(bio_err
);
2644 c_bio
= BIO_new(BIO_f_ssl());
2645 s_bio
= BIO_new(BIO_f_ssl());
2646 if ((c_bio
== NULL
) || (s_bio
== NULL
)) {
2647 ERR_print_errors(bio_err
);
2651 SSL_set_connect_state(c_ssl
);
2652 SSL_set_bio(c_ssl
, s_to_c
, c_to_s
);
2653 SSL_set_max_send_fragment(c_ssl
, max_frag
);
2654 BIO_set_ssl(c_bio
, c_ssl
, BIO_NOCLOSE
);
2657 * We've just given our ref to these BIOs to c_ssl. We need another one to
2660 if (!BIO_up_ref(c_to_s
)) {
2661 /* c_to_s and s_to_c will get freed when we free c_ssl */
2666 if (!BIO_up_ref(s_to_c
)) {
2667 /* s_to_c will get freed when we free c_ssl */
2672 SSL_set_accept_state(s_ssl
);
2673 SSL_set_bio(s_ssl
, c_to_s
, s_to_c
);
2675 /* We've used up all our refs to these now */
2679 SSL_set_max_send_fragment(s_ssl
, max_frag
);
2680 BIO_set_ssl(s_bio
, s_ssl
, BIO_NOCLOSE
);
2686 c_write
= 1, s_write
= 0;
2688 /* We can always do writes */
2693 i
= (int)BIO_pending(s_bio
);
2694 if ((i
&& s_r
) || s_w
)
2697 i
= (int)BIO_pending(c_bio
);
2698 if ((i
&& c_r
) || c_w
)
2701 if (do_server
&& debug
) {
2702 if (SSL_in_init(s_ssl
))
2703 printf("server waiting in SSL_accept - %s\n",
2704 SSL_state_string_long(s_ssl
));
2707 if (do_client
&& debug
) {
2708 if (SSL_in_init(c_ssl
))
2709 printf("client waiting in SSL_connect - %s\n",
2710 SSL_state_string_long(c_ssl
));
2713 if (!do_client
&& !do_server
) {
2714 fprintf(stdout
, "ERROR IN STARTUP\n");
2715 ERR_print_errors(bio_err
);
2718 if (do_client
&& !(done
& C_DONE
)) {
2720 j
= (cw_num
> bufsiz
) ? (int)bufsiz
: (int)cw_num
;
2721 i
= BIO_write(c_bio
, cbuf
, j
);
2725 if (BIO_should_retry(c_bio
)) {
2726 if (BIO_should_read(c_bio
))
2728 if (BIO_should_write(c_bio
))
2731 fprintf(stderr
, "ERROR in CLIENT\n");
2733 ERR_print_errors(bio_err
);
2736 } else if (i
== 0) {
2737 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2741 printf("client wrote %d\n", i
);
2746 if (max_frag
> 1029)
2747 SSL_set_max_send_fragment(c_ssl
, max_frag
-= 5);
2750 i
= BIO_read(c_bio
, cbuf
, bufsiz
);
2754 if (BIO_should_retry(c_bio
)) {
2755 if (BIO_should_read(c_bio
))
2757 if (BIO_should_write(c_bio
))
2760 fprintf(stderr
, "ERROR in CLIENT\n");
2762 ERR_print_errors(bio_err
);
2765 } else if (i
== 0) {
2766 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2770 printf("client read %d\n", i
);
2779 done
= S_DONE
| C_DONE
;
2785 if (do_server
&& !(done
& S_DONE
)) {
2787 i
= BIO_read(s_bio
, sbuf
, bufsiz
);
2791 if (BIO_should_retry(s_bio
)) {
2792 if (BIO_should_read(s_bio
))
2794 if (BIO_should_write(s_bio
))
2797 fprintf(stderr
, "ERROR in SERVER\n");
2799 ERR_print_errors(bio_err
);
2802 } else if (i
== 0) {
2803 ERR_print_errors(bio_err
);
2805 "SSL SERVER STARTUP FAILED in SSL_read\n");
2809 printf("server read %d\n", i
);
2822 j
= (sw_num
> bufsiz
) ? (int)bufsiz
: (int)sw_num
;
2823 i
= BIO_write(s_bio
, sbuf
, j
);
2827 if (BIO_should_retry(s_bio
)) {
2828 if (BIO_should_read(s_bio
))
2830 if (BIO_should_write(s_bio
))
2833 fprintf(stderr
, "ERROR in SERVER\n");
2835 ERR_print_errors(bio_err
);
2838 } else if (i
== 0) {
2839 ERR_print_errors(bio_err
);
2841 "SSL SERVER STARTUP FAILED in SSL_write\n");
2845 printf("server wrote %d\n", i
);
2851 if (max_frag
> 1029)
2852 SSL_set_max_send_fragment(s_ssl
, max_frag
-= 5);
2857 if ((done
& S_DONE
) && (done
& C_DONE
))
2862 print_details(c_ssl
, "DONE: ");
2863 #ifndef OPENSSL_NO_NEXTPROTONEG
2864 if (verify_npn(c_ssl
, s_ssl
) < 0)
2867 if (verify_serverinfo() < 0) {
2868 fprintf(stderr
, "Server info verify error\n");
2871 if (custom_ext_error
) {
2872 fprintf(stderr
, "Custom extension error\n");
2879 BIO_free_all(c_bio
);
2880 BIO_free_all(s_bio
);
2884 if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-client") == 0)
2885 ret
= (err_in_client
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2886 else if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-server") == 0)
2887 ret
= (err_in_server
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2892 static int verify_callback(int ok
, X509_STORE_CTX
*ctx
)
2896 s
= X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx
)),
2900 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx
), buf
);
2902 fprintf(stderr
, "depth=%d error=%d %s\n",
2903 X509_STORE_CTX_get_error_depth(ctx
),
2904 X509_STORE_CTX_get_error(ctx
), buf
);
2909 int i
= X509_STORE_CTX_get_error(ctx
);
2913 fprintf(stderr
, "Error string: %s\n",
2914 X509_verify_cert_error_string(i
));
2916 case X509_V_ERR_CERT_NOT_YET_VALID
:
2917 case X509_V_ERR_CERT_HAS_EXPIRED
:
2918 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
2927 static int app_verify_callback(X509_STORE_CTX
*ctx
, void *arg
)
2930 struct app_verify_arg
*cb_arg
= arg
;
2932 if (cb_arg
->app_verify
) {
2933 char *s
= NULL
, buf
[256];
2934 X509
*c
= X509_STORE_CTX_get0_cert(ctx
);
2936 printf("In app_verify_callback, allowing cert. ");
2937 printf("Arg is: %s\n", cb_arg
->string
);
2938 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2939 (void *)ctx
, (void *)c
);
2941 s
= X509_NAME_oneline(X509_get_subject_name(c
), buf
, 256);
2943 printf("cert depth=%d %s\n",
2944 X509_STORE_CTX_get_error_depth(ctx
), buf
);
2949 ok
= X509_verify_cert(ctx
);
2954 #ifndef OPENSSL_NO_PSK
2955 /* convert the PSK key (psk_key) in ascii to binary (psk) */
2956 static int psk_key2bn(const char *pskkey
, unsigned char *psk
,
2957 unsigned int max_psk_len
)
2962 ret
= BN_hex2bn(&bn
, pskkey
);
2964 BIO_printf(bio_err
, "Could not convert PSK key '%s' to BIGNUM\n",
2969 if (BN_num_bytes(bn
) > (int)max_psk_len
) {
2971 "psk buffer of callback is too small (%d) for key (%d)\n",
2972 max_psk_len
, BN_num_bytes(bn
));
2976 ret
= BN_bn2bin(bn
, psk
);
2981 static unsigned int psk_client_callback(SSL
*ssl
, const char *hint
,
2983 unsigned int max_identity_len
,
2985 unsigned int max_psk_len
)
2988 unsigned int psk_len
= 0;
2990 ret
= BIO_snprintf(identity
, max_identity_len
, "Client_identity");
2994 fprintf(stderr
, "client: created identity '%s' len=%d\n", identity
,
2996 ret
= psk_key2bn(psk_key
, psk
, max_psk_len
);
3004 static unsigned int psk_server_callback(SSL
*ssl
, const char *identity
,
3006 unsigned int max_psk_len
)
3008 unsigned int psk_len
= 0;
3010 if (strcmp(identity
, "Client_identity") != 0) {
3011 BIO_printf(bio_err
, "server: PSK error: client identity not found\n");
3014 psk_len
= psk_key2bn(psk_key
, psk
, max_psk_len
);