2 * Copyright 1995-2022 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>
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
66 #define _XOPEN_SOURCE_EXTENDED 1
68 #ifdef OPENSSL_SYS_WINDOWS
74 #include "helpers/predefined_dhparams.h"
76 static SSL_CTX
*s_ctx
= NULL
;
77 static SSL_CTX
*s_ctx2
= NULL
;
80 * There is really no standard for this, so let's assign something
85 static int verify_callback(int ok
, X509_STORE_CTX
*ctx
);
86 static int app_verify_callback(X509_STORE_CTX
*ctx
, void *arg
);
87 #define APP_CALLBACK_STRING "Test Callback Argument"
88 struct app_verify_arg
{
93 static char *psk_key
= NULL
; /* by default PSK is not used */
94 #ifndef OPENSSL_NO_PSK
95 static unsigned int psk_client_callback(SSL
*ssl
, const char *hint
,
97 unsigned int max_identity_len
,
99 unsigned int max_psk_len
);
100 static unsigned int psk_server_callback(SSL
*ssl
, const char *identity
,
102 unsigned int max_psk_len
);
105 static BIO
*bio_stdout
= NULL
;
107 #ifndef OPENSSL_NO_NEXTPROTONEG
108 /* Note that this code assumes that this is only a one element list: */
109 static const char NEXT_PROTO_STRING
[] = "\x09testproto";
110 static int npn_client
= 0;
111 static int npn_server
= 0;
112 static int npn_server_reject
= 0;
114 static int cb_client_npn(SSL
*s
, unsigned char **out
, unsigned char *outlen
,
115 const unsigned char *in
, unsigned int inlen
,
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.
123 *out
= (unsigned char *)NEXT_PROTO_STRING
+ 1;
124 *outlen
= sizeof(NEXT_PROTO_STRING
) - 2;
125 return SSL_TLSEXT_ERR_OK
;
128 static int cb_server_npn(SSL
*s
, const unsigned char **data
,
129 unsigned int *len
, void *arg
)
131 *data
= (const unsigned char *)NEXT_PROTO_STRING
;
132 *len
= sizeof(NEXT_PROTO_STRING
) - 1;
133 return SSL_TLSEXT_ERR_OK
;
136 static int cb_server_rejects_npn(SSL
*s
, const unsigned char **data
,
137 unsigned int *len
, void *arg
)
139 return SSL_TLSEXT_ERR_NOACK
;
142 static int verify_npn(SSL
*client
, SSL
*server
)
144 const unsigned char *client_s
;
146 const unsigned char *server_s
;
149 SSL_get0_next_proto_negotiated(client
, &client_s
, &client_len
);
150 SSL_get0_next_proto_negotiated(server
, &server_s
, &server_len
);
153 BIO_printf(bio_stdout
, "Client NPN: ");
154 BIO_write(bio_stdout
, client_s
, client_len
);
155 BIO_printf(bio_stdout
, "\n");
159 BIO_printf(bio_stdout
, "Server NPN: ");
160 BIO_write(bio_stdout
, server_s
, server_len
);
161 BIO_printf(bio_stdout
, "\n");
165 * If an NPN string was returned, it must be the protocol that we
166 * expected to negotiate.
168 if (client_len
&& (client_len
!= sizeof(NEXT_PROTO_STRING
) - 2 ||
169 memcmp(client_s
, NEXT_PROTO_STRING
+ 1, client_len
)))
171 if (server_len
&& (server_len
!= sizeof(NEXT_PROTO_STRING
) - 2 ||
172 memcmp(server_s
, NEXT_PROTO_STRING
+ 1, server_len
)))
175 if (!npn_client
&& client_len
)
177 if (!npn_server
&& server_len
)
179 if (npn_server_reject
&& server_len
)
181 if (npn_client
&& npn_server
&& (!client_len
|| !server_len
))
188 static const char *alpn_client
;
189 static char *alpn_server
;
190 static char *alpn_server2
;
191 static const char *alpn_expected
;
192 static unsigned char *alpn_selected
;
193 static const char *server_min_proto
;
194 static const char *server_max_proto
;
195 static const char *client_min_proto
;
196 static const char *client_max_proto
;
197 static const char *should_negotiate
;
198 static const char *sn_client
;
199 static const char *sn_server1
;
200 static const char *sn_server2
;
201 static int sn_expect
= 0;
202 static const char *server_sess_out
;
203 static const char *server_sess_in
;
204 static const char *client_sess_out
;
205 static const char *client_sess_in
;
206 static SSL_SESSION
*server_sess
;
207 static SSL_SESSION
*client_sess
;
209 static int servername_cb(SSL
*s
, int *ad
, void *arg
)
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
;
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
);
224 return SSL_TLSEXT_ERR_OK
;
226 static int verify_servername(SSL
*client
, SSL
*server
)
228 /* just need to see if sn_context is what we expect */
229 SSL_CTX
* ctx
= SSL_get_SSL_CTX(server
);
232 if (sn_expect
== 1 && ctx
== s_ctx
)
234 if (sn_expect
== 2 && ctx
== s_ctx2
)
236 BIO_printf(bio_stdout
, "Servername: expected context %d\n", sn_expect
);
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");
242 BIO_printf(bio_stdout
, "Servername: context is unknown\n");
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"
253 * returns: a malloced buffer or NULL on failure.
255 static unsigned char *next_protos_parse(size_t *outlen
,
266 out
= OPENSSL_malloc(strlen(in
) + 1);
270 for (i
= 0; i
<= len
; ++i
) {
271 if (i
== len
|| in
[i
] == ',') {
272 if (i
- start
> 255) {
276 out
[start
] = (unsigned char)(i
- start
);
286 static int cb_server_alpn(SSL
*s
, const unsigned char **out
,
287 unsigned char *outlen
, const unsigned char *in
,
288 unsigned int inlen
, void *arg
)
290 unsigned char *protos
;
292 char* alpn_str
= arg
;
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",
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
;
309 * Make a copy of the selected protocol which will be freed in
312 alpn_selected
= OPENSSL_malloc(*outlen
);
313 memcpy(alpn_selected
, *out
, *outlen
);
314 *out
= alpn_selected
;
316 OPENSSL_free(protos
);
317 return SSL_TLSEXT_ERR_OK
;
320 static int verify_alpn(SSL
*client
, SSL
*server
)
322 const unsigned char *client_proto
, *server_proto
;
323 unsigned int client_proto_len
= 0, server_proto_len
= 0;
324 SSL_get0_alpn_selected(client
, &client_proto
, &client_proto_len
);
325 SSL_get0_alpn_selected(server
, &server_proto
, &server_proto_len
);
327 OPENSSL_free(alpn_selected
);
328 alpn_selected
= NULL
;
330 if (client_proto_len
!= server_proto_len
) {
331 BIO_printf(bio_stdout
, "ALPN selected protocols differ!\n");
335 if (client_proto
!= NULL
&&
336 memcmp(client_proto
, server_proto
, client_proto_len
) != 0) {
337 BIO_printf(bio_stdout
, "ALPN selected protocols differ!\n");
341 if (client_proto_len
> 0 && alpn_expected
== NULL
) {
342 BIO_printf(bio_stdout
, "ALPN unexpectedly negotiated\n");
346 if (alpn_expected
!= NULL
&&
347 (client_proto_len
!= strlen(alpn_expected
) ||
348 memcmp(client_proto
, alpn_expected
, client_proto_len
) != 0)) {
349 BIO_printf(bio_stdout
,
350 "ALPN selected protocols not equal to expected protocol: %s\n",
358 BIO_printf(bio_stdout
, "ALPN results: client: '");
359 BIO_write(bio_stdout
, client_proto
, client_proto_len
);
360 BIO_printf(bio_stdout
, "', server: '");
361 BIO_write(bio_stdout
, server_proto
, server_proto_len
);
362 BIO_printf(bio_stdout
, "'\n");
363 BIO_printf(bio_stdout
, "ALPN configured: client: '%s', server: '",
365 if (SSL_get_SSL_CTX(server
) == s_ctx2
) {
366 BIO_printf(bio_stdout
, "%s'\n",
369 BIO_printf(bio_stdout
, "%s'\n",
376 * WARNING : below extension types are *NOT* IETF assigned, and could
377 * conflict if these types are reassigned and handled specially by OpenSSL
380 #define TACK_EXT_TYPE 62208
381 #define CUSTOM_EXT_TYPE_0 1000
382 #define CUSTOM_EXT_TYPE_1 1001
383 #define CUSTOM_EXT_TYPE_2 1002
384 #define CUSTOM_EXT_TYPE_3 1003
386 static const char custom_ext_cli_string
[] = "abc";
387 static const char custom_ext_srv_string
[] = "defg";
389 /* These set from cmdline */
390 static char *serverinfo_file
= NULL
;
391 static int serverinfo_sct
= 0;
392 static int serverinfo_tack
= 0;
394 /* These set based on extension callbacks */
395 static int serverinfo_sct_seen
= 0;
396 static int serverinfo_tack_seen
= 0;
397 static int serverinfo_other_seen
= 0;
399 /* This set from cmdline */
400 static int custom_ext
= 0;
402 /* This set based on extension callbacks */
403 static int custom_ext_error
= 0;
405 static int serverinfo_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
406 const unsigned char *in
, size_t inlen
,
409 if (ext_type
== TLSEXT_TYPE_signed_certificate_timestamp
)
410 serverinfo_sct_seen
++;
411 else if (ext_type
== TACK_EXT_TYPE
)
412 serverinfo_tack_seen
++;
414 serverinfo_other_seen
++;
418 static int verify_serverinfo(void)
420 if (serverinfo_sct
!= serverinfo_sct_seen
)
422 if (serverinfo_tack
!= serverinfo_tack_seen
)
424 if (serverinfo_other_seen
)
430 * Four test cases for custom extensions:
431 * 0 - no ClientHello extension or ServerHello response
432 * 1 - ClientHello with "abc", no response
433 * 2 - ClientHello with "abc", empty response
434 * 3 - ClientHello with "abc", "defg" response
437 static int custom_ext_0_cli_add_cb(SSL
*s
, unsigned int ext_type
,
438 const unsigned char **out
,
439 size_t *outlen
, int *al
, void *arg
)
441 if (ext_type
!= CUSTOM_EXT_TYPE_0
)
442 custom_ext_error
= 1;
443 return 0; /* Don't send an extension */
446 static int custom_ext_0_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
447 const unsigned char *in
,
448 size_t inlen
, int *al
, void *arg
)
453 static int custom_ext_1_cli_add_cb(SSL
*s
, unsigned int ext_type
,
454 const unsigned char **out
,
455 size_t *outlen
, int *al
, void *arg
)
457 if (ext_type
!= CUSTOM_EXT_TYPE_1
)
458 custom_ext_error
= 1;
459 *out
= (const unsigned char *)custom_ext_cli_string
;
460 *outlen
= strlen(custom_ext_cli_string
);
461 return 1; /* Send "abc" */
464 static int custom_ext_1_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_2_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_2
)
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_2_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
483 const unsigned char *in
,
484 size_t inlen
, int *al
, void *arg
)
486 if (ext_type
!= CUSTOM_EXT_TYPE_2
)
487 custom_ext_error
= 1;
489 custom_ext_error
= 1; /* Should be empty response */
493 static int custom_ext_3_cli_add_cb(SSL
*s
, unsigned int ext_type
,
494 const unsigned char **out
,
495 size_t *outlen
, int *al
, void *arg
)
497 if (ext_type
!= CUSTOM_EXT_TYPE_3
)
498 custom_ext_error
= 1;
499 *out
= (const unsigned char *)custom_ext_cli_string
;
500 *outlen
= strlen(custom_ext_cli_string
);
501 return 1; /* Send "abc" */
504 static int custom_ext_3_cli_parse_cb(SSL
*s
, unsigned int ext_type
,
505 const unsigned char *in
,
506 size_t inlen
, int *al
, void *arg
)
508 if (ext_type
!= CUSTOM_EXT_TYPE_3
)
509 custom_ext_error
= 1;
510 if (inlen
!= strlen(custom_ext_srv_string
))
511 custom_ext_error
= 1;
512 if (memcmp(custom_ext_srv_string
, in
, inlen
) != 0)
513 custom_ext_error
= 1; /* Check for "defg" */
518 * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
521 static int custom_ext_0_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
522 const unsigned char *in
,
523 size_t inlen
, int *al
, void *arg
)
525 custom_ext_error
= 1;
529 /* 'add' callbacks are only called if the 'parse' callback is called */
530 static int custom_ext_0_srv_add_cb(SSL
*s
, unsigned int ext_type
,
531 const unsigned char **out
,
532 size_t *outlen
, int *al
, void *arg
)
534 /* Error: should not have been called */
535 custom_ext_error
= 1;
536 return 0; /* Don't send an extension */
539 static int custom_ext_1_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
540 const unsigned char *in
,
541 size_t inlen
, int *al
, void *arg
)
543 if (ext_type
!= CUSTOM_EXT_TYPE_1
)
544 custom_ext_error
= 1;
545 /* Check for "abc" */
546 if (inlen
!= strlen(custom_ext_cli_string
))
547 custom_ext_error
= 1;
548 if (memcmp(in
, custom_ext_cli_string
, inlen
) != 0)
549 custom_ext_error
= 1;
553 static int custom_ext_1_srv_add_cb(SSL
*s
, unsigned int ext_type
,
554 const unsigned char **out
,
555 size_t *outlen
, int *al
, void *arg
)
557 return 0; /* Don't send an extension */
560 static int custom_ext_2_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
561 const unsigned char *in
,
562 size_t inlen
, int *al
, void *arg
)
564 if (ext_type
!= CUSTOM_EXT_TYPE_2
)
565 custom_ext_error
= 1;
566 /* Check for "abc" */
567 if (inlen
!= strlen(custom_ext_cli_string
))
568 custom_ext_error
= 1;
569 if (memcmp(in
, custom_ext_cli_string
, inlen
) != 0)
570 custom_ext_error
= 1;
574 static int custom_ext_2_srv_add_cb(SSL
*s
, unsigned int ext_type
,
575 const unsigned char **out
,
576 size_t *outlen
, int *al
, void *arg
)
580 return 1; /* Send empty extension */
583 static int custom_ext_3_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
584 const unsigned char *in
,
585 size_t inlen
, int *al
, void *arg
)
587 if (ext_type
!= CUSTOM_EXT_TYPE_3
)
588 custom_ext_error
= 1;
589 /* Check for "abc" */
590 if (inlen
!= strlen(custom_ext_cli_string
))
591 custom_ext_error
= 1;
592 if (memcmp(in
, custom_ext_cli_string
, inlen
) != 0)
593 custom_ext_error
= 1;
597 static int custom_ext_3_srv_add_cb(SSL
*s
, unsigned int ext_type
,
598 const unsigned char **out
,
599 size_t *outlen
, int *al
, void *arg
)
601 *out
= (const unsigned char *)custom_ext_srv_string
;
602 *outlen
= strlen(custom_ext_srv_string
);
603 return 1; /* Send "defg" */
606 static char *cipher
= NULL
;
607 static char *ciphersuites
= NULL
;
608 static int verbose
= 0;
609 static int debug
= 0;
611 int doit_localhost(SSL
*s_ssl
, SSL
*c_ssl
, int family
,
612 long bytes
, clock_t *s_time
, clock_t *c_time
);
613 int doit_biopair(SSL
*s_ssl
, SSL
*c_ssl
, long bytes
, clock_t *s_time
,
615 int doit(SSL
*s_ssl
, SSL
*c_ssl
, long bytes
);
617 static void sv_usage(void)
619 fprintf(stderr
, "usage: ssltest [args ...]\n");
620 fprintf(stderr
, "\n");
621 fprintf(stderr
, " -server_auth - check server certificate\n");
622 fprintf(stderr
, " -client_auth - do client authentication\n");
623 fprintf(stderr
, " -v - more output\n");
624 fprintf(stderr
, " -d - debug output\n");
625 fprintf(stderr
, " -reuse - use session-id reuse\n");
626 fprintf(stderr
, " -num <val> - number of connections to perform\n");
628 " -bytes <val> - number of bytes to swap between client/server\n");
629 #ifndef OPENSSL_NO_DH
631 " -dhe512 - use 512 bit key for DHE (to test failure)\n");
633 " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
635 " -dhe2048 - use 2048 bit key (safe prime) for DHE (default, no-op)\n");
637 " -dhe4096 - use 4096 bit key (safe prime) for DHE\n");
639 fprintf(stderr
, " -no_dhe - disable DHE\n");
640 #ifndef OPENSSL_NO_EC
641 fprintf(stderr
, " -no_ecdhe - disable ECDHE\n");
643 #ifndef OPENSSL_NO_PSK
644 fprintf(stderr
, " -psk arg - PSK in hex (without 0x)\n");
646 #ifndef OPENSSL_NO_SSL3
647 fprintf(stderr
, " -ssl3 - use SSLv3\n");
649 #ifndef OPENSSL_NO_TLS1
650 fprintf(stderr
, " -tls1 - use TLSv1\n");
652 #ifndef OPENSSL_NO_TLS1_1
653 fprintf(stderr
, " -tls1_1 - use TLSv1.1\n");
655 #ifndef OPENSSL_NO_TLS1_2
656 fprintf(stderr
, " -tls1_2 - use TLSv1.2\n");
658 #ifndef OPENSSL_NO_DTLS
659 fprintf(stderr
, " -dtls - use DTLS\n");
660 #ifndef OPENSSL_NO_DTLS1
661 fprintf(stderr
, " -dtls1 - use DTLSv1\n");
663 #ifndef OPENSSL_NO_DTLS1_2
664 fprintf(stderr
, " -dtls12 - use DTLSv1.2\n");
667 fprintf(stderr
, " -CApath arg - PEM format directory of CA's\n");
668 fprintf(stderr
, " -CAfile arg - PEM format file of CA's\n");
669 fprintf(stderr
, " -cert arg - Server certificate file\n");
671 " -key arg - Server key file (default: same as -cert)\n");
672 fprintf(stderr
, " -c_cert arg - Client certificate file\n");
674 " -c_key arg - Client key file (default: same as -c_cert)\n");
675 fprintf(stderr
, " -cipher arg - The TLSv1.2 and below cipher list\n");
676 fprintf(stderr
, " -ciphersuites arg - The TLSv1.3 ciphersuites\n");
677 fprintf(stderr
, " -bio_pair - Use BIO pairs\n");
678 fprintf(stderr
, " -ipv4 - Use IPv4 connection on localhost\n");
679 fprintf(stderr
, " -ipv6 - Use IPv6 connection on localhost\n");
680 fprintf(stderr
, " -f - Test even cases that can't work\n");
682 " -time - measure processor time used by client and server\n");
683 fprintf(stderr
, " -zlib - use zlib compression\n");
684 #ifndef OPENSSL_NO_NEXTPROTONEG
685 fprintf(stderr
, " -npn_client - have client side offer NPN\n");
686 fprintf(stderr
, " -npn_server - have server side offer NPN\n");
687 fprintf(stderr
, " -npn_server_reject - have server reject NPN\n");
689 fprintf(stderr
, " -serverinfo_file file - have server use this file\n");
690 fprintf(stderr
, " -serverinfo_sct - have client offer and expect SCT\n");
692 " -serverinfo_tack - have client offer and expect TACK\n");
694 " -custom_ext - try various custom extension callbacks\n");
695 fprintf(stderr
, " -alpn_client <string> - have client side offer ALPN\n");
696 fprintf(stderr
, " -alpn_server <string> - have server side offer ALPN\n");
697 fprintf(stderr
, " -alpn_server1 <string> - alias for -alpn_server\n");
698 fprintf(stderr
, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
700 " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
701 fprintf(stderr
, " -server_min_proto <string> - Minimum version the server should support\n");
702 fprintf(stderr
, " -server_max_proto <string> - Maximum version the server should support\n");
703 fprintf(stderr
, " -client_min_proto <string> - Minimum version the client should support\n");
704 fprintf(stderr
, " -client_max_proto <string> - Maximum version the client should support\n");
705 fprintf(stderr
, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
706 #ifndef OPENSSL_NO_CT
707 fprintf(stderr
, " -noct - no certificate transparency\n");
708 fprintf(stderr
, " -requestct - request certificate transparency\n");
709 fprintf(stderr
, " -requirect - require certificate transparency\n");
711 fprintf(stderr
, " -sn_client <string> - have client request this servername\n");
712 fprintf(stderr
, " -sn_server1 <string> - have server context 1 respond to this servername\n");
713 fprintf(stderr
, " -sn_server2 <string> - have server context 2 respond to this servername\n");
714 fprintf(stderr
, " -sn_expect1 - expected server 1\n");
715 fprintf(stderr
, " -sn_expect2 - expected server 2\n");
716 fprintf(stderr
, " -server_sess_out <file> - Save the server session to a file\n");
717 fprintf(stderr
, " -server_sess_in <file> - Read the server session from a file\n");
718 fprintf(stderr
, " -client_sess_out <file> - Save the client session to a file\n");
719 fprintf(stderr
, " -client_sess_in <file> - Read the client session from a file\n");
720 fprintf(stderr
, " -should_reuse <number> - The expected state of reusing the session\n");
721 fprintf(stderr
, " -no_ticket - do not issue TLS session ticket\n");
722 fprintf(stderr
, " -client_ktls - try to enable client KTLS\n");
723 fprintf(stderr
, " -server_ktls - try to enable server KTLS\n");
724 fprintf(stderr
, " -provider <name> - Load the given provider into the library context\n");
725 fprintf(stderr
, " -config <cnf> - Load the given config file into the library context\n");
728 static void print_key_details(BIO
*out
, EVP_PKEY
*key
)
730 int keyid
= EVP_PKEY_get_id(key
);
732 #ifndef OPENSSL_NO_EC
733 if (keyid
== EVP_PKEY_EC
) {
737 if (!EVP_PKEY_get_group_name(key
, group
, sizeof(group
), &size
))
738 strcpy(group
, "unknown group");
739 BIO_printf(out
, "%d bits EC (%s)", EVP_PKEY_get_bits(key
), group
);
755 algname
= OBJ_nid2sn(keyid
);
758 BIO_printf(out
, "%d bits %s", EVP_PKEY_get_bits(key
), algname
);
762 static void print_details(SSL
*c_ssl
, const char *prefix
)
764 const SSL_CIPHER
*ciph
;
769 ciph
= SSL_get_current_cipher(c_ssl
);
770 BIO_printf(bio_stdout
, "%s%s, cipher %s %s",
772 SSL_get_version(c_ssl
),
773 SSL_CIPHER_get_version(ciph
), SSL_CIPHER_get_name(ciph
));
774 cert
= SSL_get0_peer_certificate(c_ssl
);
776 EVP_PKEY
* pubkey
= X509_get0_pubkey(cert
);
778 if (pubkey
!= NULL
) {
779 BIO_puts(bio_stdout
, ", ");
780 print_key_details(bio_stdout
, pubkey
);
783 if (SSL_get_peer_tmp_key(c_ssl
, &pkey
)) {
784 BIO_puts(bio_stdout
, ", temp key: ");
785 print_key_details(bio_stdout
, pkey
);
788 if (SSL_get_peer_signature_nid(c_ssl
, &mdnid
))
789 BIO_printf(bio_stdout
, ", digest=%s", OBJ_nid2sn(mdnid
));
790 BIO_printf(bio_stdout
, "\n");
794 * protocol_from_string - converts a protocol version string to a number
796 * Returns -1 on failure or the version on success
798 static int protocol_from_string(const char *value
)
800 struct protocol_versions
{
804 static const struct protocol_versions versions
[] = {
805 {"ssl3", SSL3_VERSION
},
806 {"tls1", TLS1_VERSION
},
807 {"tls1.1", TLS1_1_VERSION
},
808 {"tls1.2", TLS1_2_VERSION
},
809 {"tls1.3", TLS1_3_VERSION
},
810 {"dtls1", DTLS1_VERSION
},
811 {"dtls1.2", DTLS1_2_VERSION
}};
813 size_t n
= OSSL_NELEM(versions
);
815 for (i
= 0; i
< n
; i
++)
816 if (strcmp(versions
[i
].name
, value
) == 0)
817 return versions
[i
].version
;
821 static SSL_SESSION
*read_session(const char *filename
)
824 BIO
*f
= BIO_new_file(filename
, "r");
827 BIO_printf(bio_err
, "Can't open session file %s\n", filename
);
828 ERR_print_errors(bio_err
);
831 sess
= PEM_read_bio_SSL_SESSION(f
, NULL
, 0, NULL
);
833 BIO_printf(bio_err
, "Can't parse session file %s\n", filename
);
834 ERR_print_errors(bio_err
);
840 static int write_session(const char *filename
, SSL_SESSION
*sess
)
845 BIO_printf(bio_err
, "No session information\n");
849 f
= BIO_new_file(filename
, "w");
851 BIO_printf(bio_err
, "Can't open session file %s\n", filename
);
852 ERR_print_errors(bio_err
);
855 PEM_write_bio_SSL_SESSION(f
, sess
);
861 * set_protocol_version - Sets protocol version minimum or maximum
863 * Returns 0 on failure and 1 on success
865 static int set_protocol_version(const char *version
, SSL
*ssl
, int setting
)
867 if (version
!= NULL
) {
868 int ver
= protocol_from_string(version
);
870 BIO_printf(bio_err
, "Error parsing: %s\n", version
);
873 return SSL_ctrl(ssl
, setting
, ver
, NULL
);
878 int main(int argc
, char *argv
[])
880 const char *CApath
= NULL
, *CAfile
= NULL
;
882 enum { BIO_MEM
, BIO_PAIR
, BIO_IPV4
, BIO_IPV6
} bio_type
= BIO_MEM
;
884 int dtls1
= 0, dtls12
= 0, dtls
= 0, tls1
= 0, tls1_1
= 0, tls1_2
= 0, ssl3
= 0;
885 int ret
= EXIT_FAILURE
;
887 int server_auth
= 0, i
;
888 struct app_verify_arg app_verify_arg
=
889 { APP_CALLBACK_STRING
, 0 };
890 SSL_CTX
*c_ctx
= NULL
;
891 const SSL_METHOD
*meth
= NULL
;
893 int number
= 1, reuse
= 0;
894 int should_reuse
= -1;
896 int client_ktls
= 0, server_ktls
= 0;
898 #ifndef OPENSSL_NO_DH
900 int dhe512
= 0, dhe1024dsa
= 0, dhe4096
= 0;
905 clock_t s_time
= 0, c_time
= 0;
906 #ifndef OPENSSL_NO_COMP
908 COMP_METHOD
*cm
= NULL
;
909 STACK_OF(SSL_COMP
) *ssl_comp_methods
= NULL
;
912 int min_version
= 0, max_version
= 0;
913 #ifndef OPENSSL_NO_CT
915 * Disable CT validation by default, because it will interfere with
916 * anything using custom extension handlers to deal with SCT extensions.
918 int ct_validation
= 0;
920 SSL_CONF_CTX
*s_cctx
= NULL
, *c_cctx
= NULL
, *s_cctx2
= NULL
;
921 STACK_OF(OPENSSL_STRING
) *conf_args
= NULL
;
922 char *arg
= NULL
, *argn
= NULL
;
923 const char *provider
= NULL
, *config
= NULL
;
924 OSSL_PROVIDER
*thisprov
= NULL
, *defctxnull
= NULL
;
925 OSSL_LIB_CTX
*libctx
= NULL
;
930 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
931 bio_stdout
= BIO_new_fp(stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
933 s_cctx
= SSL_CONF_CTX_new();
934 s_cctx2
= SSL_CONF_CTX_new();
935 c_cctx
= SSL_CONF_CTX_new();
937 if (!s_cctx
|| !c_cctx
|| !s_cctx2
) {
938 ERR_print_errors(bio_err
);
942 SSL_CONF_CTX_set_flags(s_cctx
,
943 SSL_CONF_FLAG_CMDLINE
| SSL_CONF_FLAG_SERVER
|
944 SSL_CONF_FLAG_CERTIFICATE
|
945 SSL_CONF_FLAG_REQUIRE_PRIVATE
);
946 SSL_CONF_CTX_set_flags(s_cctx2
,
947 SSL_CONF_FLAG_CMDLINE
| SSL_CONF_FLAG_SERVER
|
948 SSL_CONF_FLAG_CERTIFICATE
|
949 SSL_CONF_FLAG_REQUIRE_PRIVATE
);
950 if (!SSL_CONF_CTX_set1_prefix(s_cctx
, "-s_")) {
951 ERR_print_errors(bio_err
);
954 if (!SSL_CONF_CTX_set1_prefix(s_cctx2
, "-s_")) {
955 ERR_print_errors(bio_err
);
959 SSL_CONF_CTX_set_flags(c_cctx
,
960 SSL_CONF_FLAG_CMDLINE
| SSL_CONF_FLAG_CLIENT
|
961 SSL_CONF_FLAG_CERTIFICATE
|
962 SSL_CONF_FLAG_REQUIRE_PRIVATE
);
963 if (!SSL_CONF_CTX_set1_prefix(c_cctx
, "-c_")) {
964 ERR_print_errors(bio_err
);
972 if (strcmp(*argv
, "-F") == 0) {
974 "not compiled with FIPS support, so exiting without running.\n");
976 } else if (strcmp(*argv
, "-server_auth") == 0)
978 else if (strcmp(*argv
, "-client_auth") == 0)
980 else if (strcmp(*argv
, "-v") == 0)
982 else if (strcmp(*argv
, "-d") == 0)
984 else if (strcmp(*argv
, "-reuse") == 0)
986 else if (strcmp(*argv
, "-no_dhe") == 0)
988 /* unused in this case */;
991 else if (strcmp(*argv
, "-dhe512") == 0)
993 else if (strcmp(*argv
, "-dhe1024dsa") == 0)
995 else if (strcmp(*argv
, "-dhe4096") == 0)
998 else if (strcmp(*argv
, "-no_ecdhe") == 0)
1000 else if (strcmp(*argv
, "-psk") == 0) {
1003 psk_key
= *(++argv
);
1004 #ifndef OPENSSL_NO_PSK
1005 if (strspn(psk_key
, "abcdefABCDEF1234567890") != strlen(psk_key
)) {
1006 BIO_printf(bio_err
, "Not a hex number '%s'\n", *argv
);
1013 else if (strcmp(*argv
, "-tls1_2") == 0) {
1015 } else if (strcmp(*argv
, "-tls1_1") == 0) {
1017 } else if (strcmp(*argv
, "-tls1") == 0) {
1019 } else if (strcmp(*argv
, "-ssl3") == 0) {
1021 } else if (strcmp(*argv
, "-dtls1") == 0) {
1023 } else if (strcmp(*argv
, "-dtls12") == 0) {
1025 } else if (strcmp(*argv
, "-dtls") == 0) {
1027 } else if (HAS_PREFIX(*argv
, "-num")) {
1030 number
= atoi(*(++argv
));
1033 } else if (strcmp(*argv
, "-bytes") == 0) {
1036 bytes
= atol(*(++argv
));
1039 i
= strlen(argv
[0]);
1040 if (argv
[0][i
- 1] == 'k')
1042 if (argv
[0][i
- 1] == 'm')
1043 bytes
*= 1024L * 1024L;
1044 } else if (strcmp(*argv
, "-cipher") == 0) {
1048 } else if (strcmp(*argv
, "-ciphersuites") == 0) {
1051 ciphersuites
= *(++argv
);
1052 } else if (strcmp(*argv
, "-CApath") == 0) {
1056 } else if (strcmp(*argv
, "-CAfile") == 0) {
1060 } else if (strcmp(*argv
, "-bio_pair") == 0) {
1061 bio_type
= BIO_PAIR
;
1063 #ifndef OPENSSL_NO_SOCK
1064 else if (strcmp(*argv
, "-ipv4") == 0) {
1065 bio_type
= BIO_IPV4
;
1066 } else if (strcmp(*argv
, "-ipv6") == 0) {
1067 bio_type
= BIO_IPV6
;
1070 else if (strcmp(*argv
, "-f") == 0) {
1072 } else if (strcmp(*argv
, "-time") == 0) {
1075 #ifndef OPENSSL_NO_CT
1076 else if (strcmp(*argv
, "-noct") == 0) {
1079 else if (strcmp(*argv
, "-ct") == 0) {
1083 #ifndef OPENSSL_NO_COMP
1084 else if (strcmp(*argv
, "-zlib") == 0) {
1088 else if (strcmp(*argv
, "-app_verify") == 0) {
1089 app_verify_arg
.app_verify
= 1;
1091 #ifndef OPENSSL_NO_NEXTPROTONEG
1092 else if (strcmp(*argv
, "-npn_client") == 0) {
1094 } else if (strcmp(*argv
, "-npn_server") == 0) {
1096 } else if (strcmp(*argv
, "-npn_server_reject") == 0) {
1097 npn_server_reject
= 1;
1100 else if (strcmp(*argv
, "-serverinfo_sct") == 0) {
1102 } else if (strcmp(*argv
, "-serverinfo_tack") == 0) {
1103 serverinfo_tack
= 1;
1104 } else if (strcmp(*argv
, "-serverinfo_file") == 0) {
1107 serverinfo_file
= *(++argv
);
1108 } else if (strcmp(*argv
, "-custom_ext") == 0) {
1110 } else if (strcmp(*argv
, "-alpn_client") == 0) {
1113 alpn_client
= *(++argv
);
1114 } else if (strcmp(*argv
, "-alpn_server") == 0 ||
1115 strcmp(*argv
, "-alpn_server1") == 0) {
1118 alpn_server
= *(++argv
);
1119 } else if (strcmp(*argv
, "-alpn_server2") == 0) {
1122 alpn_server2
= *(++argv
);
1123 } else if (strcmp(*argv
, "-alpn_expected") == 0) {
1126 alpn_expected
= *(++argv
);
1127 } else if (strcmp(*argv
, "-server_min_proto") == 0) {
1130 server_min_proto
= *(++argv
);
1131 } else if (strcmp(*argv
, "-server_max_proto") == 0) {
1134 server_max_proto
= *(++argv
);
1135 } else if (strcmp(*argv
, "-client_min_proto") == 0) {
1138 client_min_proto
= *(++argv
);
1139 } else if (strcmp(*argv
, "-client_max_proto") == 0) {
1142 client_max_proto
= *(++argv
);
1143 } else if (strcmp(*argv
, "-should_negotiate") == 0) {
1146 should_negotiate
= *(++argv
);
1147 } else if (strcmp(*argv
, "-sn_client") == 0) {
1150 sn_client
= *(++argv
);
1151 } else if (strcmp(*argv
, "-sn_server1") == 0) {
1154 sn_server1
= *(++argv
);
1155 } else if (strcmp(*argv
, "-sn_server2") == 0) {
1158 sn_server2
= *(++argv
);
1159 } else if (strcmp(*argv
, "-sn_expect1") == 0) {
1161 } else if (strcmp(*argv
, "-sn_expect2") == 0) {
1163 } else if (strcmp(*argv
, "-server_sess_out") == 0) {
1166 server_sess_out
= *(++argv
);
1167 } else if (strcmp(*argv
, "-server_sess_in") == 0) {
1170 server_sess_in
= *(++argv
);
1171 } else if (strcmp(*argv
, "-client_sess_out") == 0) {
1174 client_sess_out
= *(++argv
);
1175 } else if (strcmp(*argv
, "-client_sess_in") == 0) {
1178 client_sess_in
= *(++argv
);
1179 } else if (strcmp(*argv
, "-should_reuse") == 0) {
1182 should_reuse
= !!atoi(*(++argv
));
1183 } else if (strcmp(*argv
, "-no_ticket") == 0) {
1185 } else if (strcmp(*argv
, "-client_ktls") == 0) {
1187 } else if (strcmp(*argv
, "-server_ktls") == 0) {
1189 } else if (strcmp(*argv
, "-provider") == 0) {
1192 provider
= *(++argv
);
1193 } else if (strcmp(*argv
, "-config") == 0) {
1201 /* Try to process command using SSL_CONF */
1202 rv
= SSL_CONF_cmd_argv(c_cctx
, &argc
, &argv
);
1203 /* If not processed try server */
1205 rv
= SSL_CONF_cmd_argv(s_cctx
, &argc
, &argv
);
1206 /* Recognised: store it for later use */
1211 conf_args
= sk_OPENSSL_STRING_new_null();
1215 if (!sk_OPENSSL_STRING_push(conf_args
, arg
))
1217 if (!sk_OPENSSL_STRING_push(conf_args
, argn
))
1222 BIO_printf(bio_err
, "Missing argument for %s\n", arg
);
1224 BIO_printf(bio_err
, "Error with command %s\n", arg
);
1226 BIO_printf(bio_err
, "unknown option %s\n", arg
);
1239 if (ssl3
+ tls1
+ tls1_1
+ tls1_2
+ dtls
+ dtls1
+ dtls12
> 1) {
1240 fprintf(stderr
, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
1245 #ifdef OPENSSL_NO_SSL3
1250 #ifdef OPENSSL_NO_TLS1
1255 #ifdef OPENSSL_NO_TLS1_1
1260 #ifdef OPENSSL_NO_TLS1_2
1265 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
1270 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
1278 * Testing was requested for a compiled-out protocol (e.g. SSLv3).
1279 * Ideally, we would error out, but the generic test wrapper can't know
1280 * when to expect failure. So we do nothing and return success.
1283 fprintf(stderr
, "Testing was requested for a disabled protocol. "
1284 "Skipping tests.\n");
1289 if (!ssl3
&& !tls1
&& !tls1_1
&& !tls1_2
&& !dtls
&& !dtls1
&& !dtls12
&& number
> 1
1290 && !reuse
&& !force
) {
1291 fprintf(stderr
, "This case cannot work. Use -f to perform "
1292 "the test anyway (and\n-d to see what happens), "
1293 "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
1294 "to avoid protocol mismatch.\n");
1299 if (bio_type
!= BIO_PAIR
) {
1300 fprintf(stderr
, "Using BIO pair (-bio_pair)\n");
1301 bio_type
= BIO_PAIR
;
1303 if (number
< 50 && !force
)
1305 "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
1308 #ifndef OPENSSL_NO_COMP
1309 if (comp
== COMP_ZLIB
)
1312 if (COMP_get_type(cm
) != NID_undef
) {
1313 if (SSL_COMP_add_compression_method(comp
, cm
) != 0) {
1314 fprintf(stderr
, "Failed to add compression method\n");
1315 ERR_print_errors_fp(stderr
);
1319 "Warning: %s compression not supported\n",
1320 comp
== COMP_ZLIB
? "zlib" : "unknown");
1321 ERR_print_errors_fp(stderr
);
1324 ssl_comp_methods
= SSL_COMP_get_compression_methods();
1325 n
= sk_SSL_COMP_num(ssl_comp_methods
);
1328 printf("Available compression methods:");
1329 for (j
= 0; j
< n
; j
++) {
1330 SSL_COMP
*c
= sk_SSL_COMP_value(ssl_comp_methods
, j
);
1331 printf(" %s:%d", SSL_COMP_get0_name(c
), SSL_COMP_get_id(c
));
1337 #ifndef OPENSSL_NO_TLS
1338 meth
= TLS_method();
1340 min_version
= SSL3_VERSION
;
1341 max_version
= SSL3_VERSION
;
1343 min_version
= TLS1_VERSION
;
1344 max_version
= TLS1_VERSION
;
1345 } else if (tls1_1
) {
1346 min_version
= TLS1_1_VERSION
;
1347 max_version
= TLS1_1_VERSION
;
1348 } else if (tls1_2
) {
1349 min_version
= TLS1_2_VERSION
;
1350 max_version
= TLS1_2_VERSION
;
1353 # if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)
1354 /* We only have ec and dh based built-in groups for TLSv1.3 */
1355 max_version
= TLS1_2_VERSION
;
1361 #ifndef OPENSSL_NO_DTLS
1362 if (dtls
|| dtls1
|| dtls12
) {
1363 meth
= DTLS_method();
1365 min_version
= DTLS1_VERSION
;
1366 max_version
= DTLS1_VERSION
;
1367 } else if (dtls12
) {
1368 min_version
= DTLS1_2_VERSION
;
1369 max_version
= DTLS1_2_VERSION
;
1377 if (provider
!= NULL
1378 && !test_get_libctx(&libctx
, &defctxnull
, config
, &thisprov
, provider
))
1381 c_ctx
= SSL_CTX_new_ex(libctx
, NULL
, meth
);
1382 s_ctx
= SSL_CTX_new_ex(libctx
, NULL
, meth
);
1383 s_ctx2
= SSL_CTX_new_ex(libctx
, NULL
, meth
); /* no SSL_CTX_dup! */
1384 if ((c_ctx
== NULL
) || (s_ctx
== NULL
) || (s_ctx2
== NULL
)) {
1385 ERR_print_errors(bio_err
);
1389 * Since we will use low security ciphersuites and keys for testing set
1390 * security level to zero by default. Tests can override this by adding
1391 * "@SECLEVEL=n" to the cipher string.
1393 SSL_CTX_set_security_level(c_ctx
, 0);
1394 SSL_CTX_set_security_level(s_ctx
, 0);
1395 SSL_CTX_set_security_level(s_ctx2
, 0);
1398 SSL_CTX_set_options(c_ctx
, SSL_OP_NO_TICKET
);
1399 SSL_CTX_set_options(s_ctx
, SSL_OP_NO_TICKET
);
1402 if (SSL_CTX_set_min_proto_version(c_ctx
, min_version
) == 0)
1404 if (SSL_CTX_set_max_proto_version(c_ctx
, max_version
) == 0)
1406 if (SSL_CTX_set_min_proto_version(s_ctx
, min_version
) == 0)
1408 if (SSL_CTX_set_max_proto_version(s_ctx
, max_version
) == 0)
1411 if (cipher
!= NULL
) {
1412 if (strcmp(cipher
, "") == 0) {
1413 if (!SSL_CTX_set_cipher_list(c_ctx
, cipher
)) {
1414 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH
) {
1417 ERR_print_errors(bio_err
);
1421 /* Should have failed when clearing all TLSv1.2 ciphers. */
1422 fprintf(stderr
, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1426 if (!SSL_CTX_set_cipher_list(s_ctx
, cipher
)) {
1427 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH
) {
1430 ERR_print_errors(bio_err
);
1434 /* Should have failed when clearing all TLSv1.2 ciphers. */
1435 fprintf(stderr
, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1439 if (!SSL_CTX_set_cipher_list(s_ctx2
, cipher
)) {
1440 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH
) {
1443 ERR_print_errors(bio_err
);
1447 /* Should have failed when clearing all TLSv1.2 ciphers. */
1448 fprintf(stderr
, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1452 if (!SSL_CTX_set_cipher_list(c_ctx
, cipher
)
1453 || !SSL_CTX_set_cipher_list(s_ctx
, cipher
)
1454 || !SSL_CTX_set_cipher_list(s_ctx2
, cipher
)) {
1455 ERR_print_errors(bio_err
);
1460 if (ciphersuites
!= NULL
) {
1461 if (!SSL_CTX_set_ciphersuites(c_ctx
, ciphersuites
)
1462 || !SSL_CTX_set_ciphersuites(s_ctx
, ciphersuites
)
1463 || !SSL_CTX_set_ciphersuites(s_ctx2
, ciphersuites
)) {
1464 ERR_print_errors(bio_err
);
1469 #ifndef OPENSSL_NO_CT
1470 if (ct_validation
&&
1471 !SSL_CTX_enable_ct(c_ctx
, SSL_CT_VALIDATION_STRICT
)) {
1472 ERR_print_errors(bio_err
);
1477 /* Process SSL_CONF arguments */
1478 SSL_CONF_CTX_set_ssl_ctx(c_cctx
, c_ctx
);
1479 SSL_CONF_CTX_set_ssl_ctx(s_cctx
, s_ctx
);
1480 SSL_CONF_CTX_set_ssl_ctx(s_cctx2
, s_ctx2
);
1482 for (i
= 0; i
< sk_OPENSSL_STRING_num(conf_args
); i
+= 2) {
1484 arg
= sk_OPENSSL_STRING_value(conf_args
, i
);
1485 argn
= sk_OPENSSL_STRING_value(conf_args
, i
+ 1);
1486 rv
= SSL_CONF_cmd(c_cctx
, arg
, argn
);
1487 /* If not recognised use server context */
1489 rv
= SSL_CONF_cmd(s_cctx2
, arg
, argn
);
1491 rv
= SSL_CONF_cmd(s_cctx
, arg
, argn
);
1494 BIO_printf(bio_err
, "Error processing %s %s\n",
1495 arg
, argn
? argn
: "");
1496 ERR_print_errors(bio_err
);
1501 if (!SSL_CONF_CTX_finish(s_cctx
) || !SSL_CONF_CTX_finish(c_cctx
) || !SSL_CONF_CTX_finish(s_cctx2
)) {
1502 BIO_puts(bio_err
, "Error finishing context\n");
1503 ERR_print_errors(bio_err
);
1506 #ifndef OPENSSL_NO_DH
1509 dhpkey
= get_dh1024dsa(libctx
);
1511 dhpkey
= get_dh512(libctx
);
1513 dhpkey
= get_dh4096(libctx
);
1515 dhpkey
= get_dh2048(libctx
);
1517 if (dhpkey
== NULL
|| !EVP_PKEY_up_ref(dhpkey
)) {
1518 EVP_PKEY_free(dhpkey
);
1519 BIO_puts(bio_err
, "Error getting DH parameters\n");
1520 ERR_print_errors(bio_err
);
1523 SSL_CTX_set0_tmp_dh_pkey(s_ctx
, dhpkey
);
1524 SSL_CTX_set0_tmp_dh_pkey(s_ctx2
, dhpkey
);
1528 if (!(SSL_CTX_load_verify_file(s_ctx
, CAfile
)
1529 || SSL_CTX_load_verify_dir(s_ctx
, CApath
))
1530 || !SSL_CTX_set_default_verify_paths(s_ctx
)
1531 || !(SSL_CTX_load_verify_file(s_ctx2
, CAfile
)
1532 || SSL_CTX_load_verify_dir(s_ctx2
, CApath
))
1533 || !SSL_CTX_set_default_verify_paths(s_ctx2
)
1534 || !(SSL_CTX_load_verify_file(c_ctx
, CAfile
)
1535 || SSL_CTX_load_verify_dir(c_ctx
, CApath
))
1536 || !SSL_CTX_set_default_verify_paths(c_ctx
)) {
1537 ERR_print_errors(bio_err
);
1540 #ifndef OPENSSL_NO_CT
1541 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx
) ||
1542 !SSL_CTX_set_default_ctlog_list_file(s_ctx2
) ||
1543 !SSL_CTX_set_default_ctlog_list_file(c_ctx
)) {
1544 ERR_print_errors(bio_err
);
1549 printf("client authentication\n");
1550 SSL_CTX_set_verify(s_ctx
,
1551 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
,
1553 SSL_CTX_set_verify(s_ctx2
,
1554 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
,
1556 SSL_CTX_set_cert_verify_callback(s_ctx
, app_verify_callback
,
1558 SSL_CTX_set_cert_verify_callback(s_ctx2
, app_verify_callback
,
1562 printf("server authentication\n");
1563 SSL_CTX_set_verify(c_ctx
, SSL_VERIFY_PEER
, verify_callback
);
1564 SSL_CTX_set_cert_verify_callback(c_ctx
, app_verify_callback
,
1569 int session_id_context
= 0;
1570 if (!SSL_CTX_set_session_id_context(s_ctx
, (void *)&session_id_context
,
1571 sizeof(session_id_context
)) ||
1572 !SSL_CTX_set_session_id_context(s_ctx2
, (void *)&session_id_context
,
1573 sizeof(session_id_context
))) {
1574 ERR_print_errors(bio_err
);
1579 /* Use PSK only if PSK key is given */
1580 if (psk_key
!= NULL
) {
1582 * no_psk is used to avoid putting psk command to openssl tool
1586 * if PSK is not compiled in and psk key is given, do nothing and
1592 #ifndef OPENSSL_NO_PSK
1593 SSL_CTX_set_psk_client_callback(c_ctx
, psk_client_callback
);
1594 SSL_CTX_set_psk_server_callback(s_ctx
, psk_server_callback
);
1595 SSL_CTX_set_psk_server_callback(s_ctx2
, psk_server_callback
);
1597 BIO_printf(bio_err
, "setting PSK identity hint to s_ctx\n");
1598 if (!SSL_CTX_use_psk_identity_hint(s_ctx
, "ctx server identity_hint") ||
1599 !SSL_CTX_use_psk_identity_hint(s_ctx2
, "ctx server identity_hint")) {
1600 BIO_printf(bio_err
, "error setting PSK identity hint to s_ctx\n");
1601 ERR_print_errors(bio_err
);
1607 #ifndef OPENSSL_NO_NEXTPROTONEG
1609 SSL_CTX_set_next_proto_select_cb(c_ctx
, cb_client_npn
, NULL
);
1612 if (npn_server_reject
) {
1614 "Can't have both -npn_server and -npn_server_reject\n");
1617 SSL_CTX_set_npn_advertised_cb(s_ctx
, cb_server_npn
, NULL
);
1618 SSL_CTX_set_npn_advertised_cb(s_ctx2
, cb_server_npn
, NULL
);
1620 if (npn_server_reject
) {
1621 SSL_CTX_set_npn_advertised_cb(s_ctx
, cb_server_rejects_npn
, NULL
);
1622 SSL_CTX_set_npn_advertised_cb(s_ctx2
, cb_server_rejects_npn
, NULL
);
1626 if (serverinfo_sct
) {
1627 if (!SSL_CTX_add_client_custom_ext(c_ctx
,
1628 TLSEXT_TYPE_signed_certificate_timestamp
,
1630 serverinfo_cli_parse_cb
, NULL
)) {
1631 BIO_printf(bio_err
, "Error adding SCT extension\n");
1635 if (serverinfo_tack
) {
1636 if (!SSL_CTX_add_client_custom_ext(c_ctx
, TACK_EXT_TYPE
,
1638 serverinfo_cli_parse_cb
, NULL
)) {
1639 BIO_printf(bio_err
, "Error adding TACK extension\n");
1643 if (serverinfo_file
)
1644 if (!SSL_CTX_use_serverinfo_file(s_ctx
, serverinfo_file
) ||
1645 !SSL_CTX_use_serverinfo_file(s_ctx2
, serverinfo_file
)) {
1646 BIO_printf(bio_err
, "missing serverinfo file\n");
1651 if (!SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_0
,
1652 custom_ext_0_cli_add_cb
,
1654 custom_ext_0_cli_parse_cb
, NULL
)
1655 || !SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_1
,
1656 custom_ext_1_cli_add_cb
,
1658 custom_ext_1_cli_parse_cb
, NULL
)
1659 || !SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_2
,
1660 custom_ext_2_cli_add_cb
,
1662 custom_ext_2_cli_parse_cb
, NULL
)
1663 || !SSL_CTX_add_client_custom_ext(c_ctx
, CUSTOM_EXT_TYPE_3
,
1664 custom_ext_3_cli_add_cb
,
1666 custom_ext_3_cli_parse_cb
, NULL
)
1667 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_0
,
1668 custom_ext_0_srv_add_cb
,
1670 custom_ext_0_srv_parse_cb
, NULL
)
1671 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_0
,
1672 custom_ext_0_srv_add_cb
,
1674 custom_ext_0_srv_parse_cb
, NULL
)
1675 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_1
,
1676 custom_ext_1_srv_add_cb
,
1678 custom_ext_1_srv_parse_cb
, NULL
)
1679 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_1
,
1680 custom_ext_1_srv_add_cb
,
1682 custom_ext_1_srv_parse_cb
, NULL
)
1683 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_2
,
1684 custom_ext_2_srv_add_cb
,
1686 custom_ext_2_srv_parse_cb
, NULL
)
1687 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_2
,
1688 custom_ext_2_srv_add_cb
,
1690 custom_ext_2_srv_parse_cb
, NULL
)
1691 || !SSL_CTX_add_server_custom_ext(s_ctx
, CUSTOM_EXT_TYPE_3
,
1692 custom_ext_3_srv_add_cb
,
1694 custom_ext_3_srv_parse_cb
, NULL
)
1695 || !SSL_CTX_add_server_custom_ext(s_ctx2
, CUSTOM_EXT_TYPE_3
,
1696 custom_ext_3_srv_add_cb
,
1698 custom_ext_3_srv_parse_cb
, NULL
)) {
1699 BIO_printf(bio_err
, "Error setting custom extensions\n");
1705 SSL_CTX_set_alpn_select_cb(s_ctx
, cb_server_alpn
, alpn_server
);
1707 SSL_CTX_set_alpn_select_cb(s_ctx2
, cb_server_alpn
, alpn_server2
);
1711 unsigned char *alpn
= next_protos_parse(&alpn_len
, alpn_client
);
1714 BIO_printf(bio_err
, "Error parsing -alpn_client argument\n");
1717 /* Returns 0 on success!! */
1718 if (SSL_CTX_set_alpn_protos(c_ctx
, alpn
, alpn_len
)) {
1719 BIO_printf(bio_err
, "Error setting ALPN\n");
1726 if (server_sess_in
!= NULL
) {
1727 server_sess
= read_session(server_sess_in
);
1728 if (server_sess
== NULL
)
1731 if (client_sess_in
!= NULL
) {
1732 client_sess
= read_session(client_sess_in
);
1733 if (client_sess
== NULL
)
1737 if (server_sess_out
!= NULL
|| server_sess_in
!= NULL
) {
1741 /* Use a fixed key so that we can decrypt the ticket. */
1742 size
= SSL_CTX_set_tlsext_ticket_keys(s_ctx
, NULL
, 0);
1743 keys
= OPENSSL_zalloc(size
);
1744 SSL_CTX_set_tlsext_ticket_keys(s_ctx
, keys
, size
);
1748 if (sn_server1
!= NULL
|| sn_server2
!= NULL
)
1749 SSL_CTX_set_tlsext_servername_callback(s_ctx
, servername_cb
);
1751 c_ssl
= SSL_new(c_ctx
);
1752 s_ssl
= SSL_new(s_ctx
);
1755 SSL_set_tlsext_host_name(c_ssl
, sn_client
);
1757 SSL_set_options(c_ssl
, SSL_OP_ENABLE_KTLS
);
1759 SSL_set_options(s_ssl
, SSL_OP_ENABLE_KTLS
);
1761 if (!set_protocol_version(server_min_proto
, s_ssl
, SSL_CTRL_SET_MIN_PROTO_VERSION
))
1763 if (!set_protocol_version(server_max_proto
, s_ssl
, SSL_CTRL_SET_MAX_PROTO_VERSION
))
1765 if (!set_protocol_version(client_min_proto
, c_ssl
, SSL_CTRL_SET_MIN_PROTO_VERSION
))
1767 if (!set_protocol_version(client_max_proto
, c_ssl
, SSL_CTRL_SET_MAX_PROTO_VERSION
))
1771 if (SSL_CTX_add_session(s_ctx
, server_sess
) == 0) {
1772 BIO_printf(bio_err
, "Can't add server session\n");
1773 ERR_print_errors(bio_err
);
1778 BIO_printf(bio_stdout
, "Doing handshakes=%d bytes=%ld\n", number
, bytes
);
1779 for (i
= 0; i
< number
; i
++) {
1781 if (!SSL_set_session(c_ssl
, NULL
)) {
1782 BIO_printf(bio_err
, "Failed to set session\n");
1786 if (client_sess_in
!= NULL
) {
1787 if (SSL_set_session(c_ssl
, client_sess
) == 0) {
1788 BIO_printf(bio_err
, "Can't set client session\n");
1789 ERR_print_errors(bio_err
);
1795 ret
= doit(s_ssl
, c_ssl
, bytes
);
1798 ret
= doit_biopair(s_ssl
, c_ssl
, bytes
, &s_time
, &c_time
);
1800 #ifndef OPENSSL_NO_SOCK
1802 ret
= doit_localhost(s_ssl
, c_ssl
, BIO_FAMILY_IPV4
,
1803 bytes
, &s_time
, &c_time
);
1806 ret
= doit_localhost(s_ssl
, c_ssl
, BIO_FAMILY_IPV6
,
1807 bytes
, &s_time
, &c_time
);
1816 if (ret
!= EXIT_SUCCESS
) break;
1819 if (should_negotiate
&& ret
== EXIT_SUCCESS
&&
1820 strcmp(should_negotiate
, "fail-server") != 0 &&
1821 strcmp(should_negotiate
, "fail-client") != 0) {
1822 int version
= protocol_from_string(should_negotiate
);
1824 BIO_printf(bio_err
, "Error parsing: %s\n", should_negotiate
);
1828 if (SSL_version(c_ssl
) != version
) {
1829 BIO_printf(bio_err
, "Unexpected version negotiated. "
1830 "Expected: %s, got %s\n", should_negotiate
, SSL_get_version(c_ssl
));
1836 if (should_reuse
!= -1) {
1837 if (SSL_session_reused(s_ssl
) != should_reuse
||
1838 SSL_session_reused(c_ssl
) != should_reuse
) {
1839 BIO_printf(bio_err
, "Unexpected session reuse state. "
1840 "Expected: %d, server: %d, client: %d\n", should_reuse
,
1841 SSL_session_reused(s_ssl
), SSL_session_reused(c_ssl
));
1847 if (server_sess_out
!= NULL
) {
1848 if (write_session(server_sess_out
, SSL_get_session(s_ssl
)) == 0) {
1853 if (client_sess_out
!= NULL
) {
1854 if (write_session(client_sess_out
, SSL_get_session(c_ssl
)) == 0) {
1861 print_details(c_ssl
, "");
1864 #ifdef CLOCKS_PER_SEC
1866 * "To determine the time in seconds, the value returned by the clock
1867 * function should be divided by the value of the macro
1868 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1870 BIO_printf(bio_stdout
, "Approximate total server time: %6.2f s\n"
1871 "Approximate total client time: %6.2f s\n",
1872 (double)s_time
/ CLOCKS_PER_SEC
,
1873 (double)c_time
/ CLOCKS_PER_SEC
);
1875 BIO_printf(bio_stdout
,
1876 "Approximate total server time: %6.2f units\n"
1877 "Approximate total client time: %6.2f units\n",
1878 (double)s_time
, (double)c_time
);
1887 SSL_CTX_free(s_ctx
);
1888 SSL_CTX_free(s_ctx2
);
1889 SSL_CTX_free(c_ctx
);
1890 SSL_CONF_CTX_free(s_cctx
);
1891 SSL_CONF_CTX_free(s_cctx2
);
1892 SSL_CONF_CTX_free(c_cctx
);
1893 sk_OPENSSL_STRING_free(conf_args
);
1895 BIO_free(bio_stdout
);
1897 SSL_SESSION_free(server_sess
);
1898 SSL_SESSION_free(client_sess
);
1900 OSSL_PROVIDER_unload(defctxnull
);
1901 OSSL_PROVIDER_unload(thisprov
);
1902 OSSL_LIB_CTX_free(libctx
);
1908 #ifndef OPENSSL_NO_SOCK
1909 int doit_localhost(SSL
*s_ssl
, SSL
*c_ssl
, int family
, long count
,
1910 clock_t *s_time
, clock_t *c_time
)
1912 long cw_num
= count
, cr_num
= count
, sw_num
= count
, sr_num
= count
;
1913 BIO
*s_ssl_bio
= NULL
, *c_ssl_bio
= NULL
;
1914 BIO
*acpt
= NULL
, *server
= NULL
, *client
= NULL
;
1916 int ret
= EXIT_FAILURE
;
1917 int err_in_client
= 0;
1918 int err_in_server
= 0;
1920 acpt
= BIO_new_accept(family
== BIO_FAMILY_IPV4
? "127.0.0.1:0"
1924 BIO_set_accept_ip_family(acpt
, family
);
1925 BIO_set_bind_mode(acpt
, BIO_SOCK_NONBLOCK
| BIO_SOCK_REUSEADDR
);
1926 if (BIO_do_accept(acpt
) <= 0)
1929 BIO_snprintf(addr_str
, sizeof(addr_str
), ":%s", BIO_get_accept_port(acpt
));
1931 client
= BIO_new_connect(addr_str
);
1934 BIO_set_conn_ip_family(client
, family
);
1936 if (BIO_set_nbio(client
, 1) <= 0)
1938 if (BIO_set_nbio(acpt
, 1) <= 0)
1942 int st_connect
= 0, st_accept
= 0;
1944 while (!st_connect
|| !st_accept
) {
1946 if (BIO_do_connect(client
) <= 0) {
1947 if (!BIO_should_retry(client
))
1954 if (BIO_do_accept(acpt
) <= 0) {
1955 if (!BIO_should_retry(acpt
))
1963 /* We're not interested in accepting further connects */
1964 server
= BIO_pop(acpt
);
1968 s_ssl_bio
= BIO_new(BIO_f_ssl());
1972 c_ssl_bio
= BIO_new(BIO_f_ssl());
1976 SSL_set_connect_state(c_ssl
);
1977 SSL_set_bio(c_ssl
, client
, client
);
1978 (void)BIO_set_ssl(c_ssl_bio
, c_ssl
, BIO_NOCLOSE
);
1980 SSL_set_accept_state(s_ssl
);
1981 SSL_set_bio(s_ssl
, server
, server
);
1982 (void)BIO_set_ssl(s_ssl_bio
, s_ssl
, BIO_NOCLOSE
);
1986 * c_ssl_bio: SSL filter BIO
1988 * client: I/O for SSL library
1991 * server: I/O for SSL library
1993 * s_ssl_bio: SSL filter BIO
1997 * We have non-blocking behaviour throughout this test program, but
1998 * can be sure that there is *some* progress in each iteration; so we
1999 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2000 * we just try everything in each iteration
2006 char cbuf
[1024 * 8];
2008 clock_t c_clock
= clock();
2010 memset(cbuf
, 0, sizeof(cbuf
));
2013 if (SSL_in_init(c_ssl
))
2014 printf("client waiting in SSL_connect - %s\n",
2015 SSL_state_string_long(c_ssl
));
2018 /* Write to server. */
2020 if (cw_num
> (long)sizeof(cbuf
))
2024 r
= BIO_write(c_ssl_bio
, cbuf
, i
);
2026 if (!BIO_should_retry(c_ssl_bio
)) {
2027 fprintf(stderr
, "ERROR in CLIENT\n");
2032 * BIO_should_retry(...) can just be ignored here. The
2033 * library expects us to call BIO_write with the same
2034 * arguments again, and that's what we will do in the
2037 } else if (r
== 0) {
2038 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2042 printf("client wrote %d\n", r
);
2048 /* Read from server. */
2050 r
= BIO_read(c_ssl_bio
, cbuf
, sizeof(cbuf
));
2052 if (!BIO_should_retry(c_ssl_bio
)) {
2053 fprintf(stderr
, "ERROR in CLIENT\n");
2058 * Again, "BIO_should_retry" can be ignored.
2060 } else if (r
== 0) {
2061 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2065 printf("client read %d\n", r
);
2071 * c_time and s_time increments will typically be very small
2072 * (depending on machine speed and clock tick intervals), but
2073 * sampling over a large number of connections should result in
2074 * fairly accurate figures. We cannot guarantee a lot, however
2075 * -- if each connection lasts for exactly one clock tick, it
2076 * will be counted only for the client or only for the server or
2079 *c_time
+= (clock() - c_clock
);
2085 char sbuf
[1024 * 8];
2087 clock_t s_clock
= clock();
2089 memset(sbuf
, 0, sizeof(sbuf
));
2092 if (SSL_in_init(s_ssl
))
2093 printf("server waiting in SSL_accept - %s\n",
2094 SSL_state_string_long(s_ssl
));
2097 /* Write to client. */
2099 if (sw_num
> (long)sizeof(sbuf
))
2103 r
= BIO_write(s_ssl_bio
, sbuf
, i
);
2105 if (!BIO_should_retry(s_ssl_bio
)) {
2106 fprintf(stderr
, "ERROR in SERVER\n");
2110 /* Ignore "BIO_should_retry". */
2111 } else if (r
== 0) {
2112 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2116 printf("server wrote %d\n", r
);
2122 /* Read from client. */
2124 r
= BIO_read(s_ssl_bio
, sbuf
, sizeof(sbuf
));
2126 if (!BIO_should_retry(s_ssl_bio
)) {
2127 fprintf(stderr
, "ERROR in SERVER\n");
2132 } else if (r
== 0) {
2133 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2137 printf("server read %d\n", r
);
2142 *s_time
+= (clock() - s_clock
);
2145 while (cw_num
> 0 || cr_num
> 0 || sw_num
> 0 || sr_num
> 0);
2148 print_details(c_ssl
, "DONE via TCP connect: ");
2149 # ifndef OPENSSL_NO_NEXTPROTONEG
2150 if (verify_npn(c_ssl
, s_ssl
) < 0)
2153 if (verify_serverinfo() < 0) {
2154 fprintf(stderr
, "Server info verify error\n");
2157 if (verify_alpn(c_ssl
, s_ssl
) < 0
2158 || verify_servername(c_ssl
, s_ssl
) < 0)
2161 if (custom_ext_error
) {
2162 fprintf(stderr
, "Custom extension error\n");
2166 # ifndef OPENSSL_NO_NEXTPROTONEG
2172 ERR_print_errors(bio_err
);
2177 BIO_free(s_ssl_bio
);
2178 BIO_free(c_ssl_bio
);
2180 if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-client") == 0)
2181 ret
= (err_in_client
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2182 else if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-server") == 0)
2183 ret
= (err_in_server
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2189 int doit_biopair(SSL
*s_ssl
, SSL
*c_ssl
, long count
,
2190 clock_t *s_time
, clock_t *c_time
)
2192 long cw_num
= count
, cr_num
= count
, sw_num
= count
, sr_num
= count
;
2193 BIO
*s_ssl_bio
= NULL
, *c_ssl_bio
= NULL
;
2194 BIO
*server
= NULL
, *server_io
= NULL
, *client
= NULL
, *client_io
= NULL
;
2195 int ret
= EXIT_FAILURE
;
2196 int err_in_client
= 0;
2197 int err_in_server
= 0;
2199 size_t bufsiz
= 256; /* small buffer for testing */
2201 if (!BIO_new_bio_pair(&server
, bufsiz
, &server_io
, bufsiz
))
2203 if (!BIO_new_bio_pair(&client
, bufsiz
, &client_io
, bufsiz
))
2206 s_ssl_bio
= BIO_new(BIO_f_ssl());
2210 c_ssl_bio
= BIO_new(BIO_f_ssl());
2214 SSL_set_connect_state(c_ssl
);
2215 SSL_set_bio(c_ssl
, client
, client
);
2216 (void)BIO_set_ssl(c_ssl_bio
, c_ssl
, BIO_NOCLOSE
);
2218 SSL_set_accept_state(s_ssl
);
2219 SSL_set_bio(s_ssl
, server
, server
);
2220 (void)BIO_set_ssl(s_ssl_bio
, s_ssl
, BIO_NOCLOSE
);
2224 * c_ssl_bio: SSL filter BIO
2226 * client: pseudo-I/O for SSL library
2228 * client_io: client's SSL communication; usually to be
2229 * relayed over some I/O facility, but in this
2230 * test program, we're the server, too:
2232 * server_io: server's SSL communication
2234 * server: pseudo-I/O for SSL library
2236 * s_ssl_bio: SSL filter BIO
2238 * The client and the server each employ a "BIO pair":
2239 * client + client_io, server + server_io.
2240 * BIO pairs are symmetric. A BIO pair behaves similar
2241 * to a non-blocking socketpair (but both endpoints must
2242 * be handled by the same thread).
2243 * [Here we could connect client and server to the ends
2244 * of a single BIO pair, but then this code would be less
2245 * suitable as an example for BIO pairs in general.]
2247 * Useful functions for querying the state of BIO pair endpoints:
2249 * BIO_ctrl_pending(bio) number of bytes we can read now
2250 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
2251 * other side's read attempt
2252 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2254 * ..._read_request is never more than ..._write_guarantee;
2255 * it depends on the application which one you should use.
2259 * We have non-blocking behaviour throughout this test program, but
2260 * can be sure that there is *some* progress in each iteration; so we
2261 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2262 * we just try everything in each iteration
2268 char cbuf
[1024 * 8];
2270 clock_t c_clock
= clock();
2272 memset(cbuf
, 0, sizeof(cbuf
));
2275 if (SSL_in_init(c_ssl
))
2276 printf("client waiting in SSL_connect - %s\n",
2277 SSL_state_string_long(c_ssl
));
2280 /* Write to server. */
2282 if (cw_num
> (long)sizeof(cbuf
))
2286 r
= BIO_write(c_ssl_bio
, cbuf
, i
);
2288 if (!BIO_should_retry(c_ssl_bio
)) {
2289 fprintf(stderr
, "ERROR in CLIENT\n");
2294 * BIO_should_retry(...) can just be ignored here. The
2295 * library expects us to call BIO_write with the same
2296 * arguments again, and that's what we will do in the
2299 } else if (r
== 0) {
2300 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2304 printf("client wrote %d\n", r
);
2310 /* Read from server. */
2312 r
= BIO_read(c_ssl_bio
, cbuf
, sizeof(cbuf
));
2314 if (!BIO_should_retry(c_ssl_bio
)) {
2315 fprintf(stderr
, "ERROR in CLIENT\n");
2320 * Again, "BIO_should_retry" can be ignored.
2322 } else if (r
== 0) {
2323 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2327 printf("client read %d\n", r
);
2333 * c_time and s_time increments will typically be very small
2334 * (depending on machine speed and clock tick intervals), but
2335 * sampling over a large number of connections should result in
2336 * fairly accurate figures. We cannot guarantee a lot, however
2337 * -- if each connection lasts for exactly one clock tick, it
2338 * will be counted only for the client or only for the server or
2341 *c_time
+= (clock() - c_clock
);
2347 char sbuf
[1024 * 8];
2349 clock_t s_clock
= clock();
2351 memset(sbuf
, 0, sizeof(sbuf
));
2354 if (SSL_in_init(s_ssl
))
2355 printf("server waiting in SSL_accept - %s\n",
2356 SSL_state_string_long(s_ssl
));
2359 /* Write to client. */
2361 if (sw_num
> (long)sizeof(sbuf
))
2365 r
= BIO_write(s_ssl_bio
, sbuf
, i
);
2367 if (!BIO_should_retry(s_ssl_bio
)) {
2368 fprintf(stderr
, "ERROR in SERVER\n");
2372 /* Ignore "BIO_should_retry". */
2373 } else if (r
== 0) {
2374 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2378 printf("server wrote %d\n", r
);
2384 /* Read from client. */
2386 r
= BIO_read(s_ssl_bio
, sbuf
, sizeof(sbuf
));
2388 if (!BIO_should_retry(s_ssl_bio
)) {
2389 fprintf(stderr
, "ERROR in SERVER\n");
2394 } else if (r
== 0) {
2395 fprintf(stderr
, "SSL SERVER STARTUP FAILED\n");
2399 printf("server read %d\n", r
);
2404 *s_time
+= (clock() - s_clock
);
2408 /* "I/O" BETWEEN CLIENT AND SERVER. */
2411 BIO
*io1
= server_io
, *io2
= client_io
;
2413 * we use the non-copying interface for io1 and the standard
2414 * BIO_write/BIO_read interface for io2
2417 static int prev_progress
= 1;
2425 r1
= BIO_ctrl_pending(io1
);
2426 r2
= BIO_ctrl_get_write_guarantee(io2
);
2434 if (INT_MAX
< num
) /* yeah, right */
2437 r
= BIO_nread(io1
, &dataptr
, (int)num
);
2439 assert(r
<= (int)num
);
2441 * possibly r < num (non-contiguous data)
2444 r
= BIO_write(io2
, dataptr
, (int)num
);
2445 if (r
!= (int)num
) { /* can't happen */
2446 fprintf(stderr
, "ERROR: BIO_write could not write "
2447 "BIO_ctrl_get_write_guarantee() bytes");
2453 printf((io1
== client_io
) ?
2454 "C->S relaying: %d bytes\n" :
2455 "S->C relaying: %d bytes\n", (int)num
);
2465 r1
= BIO_ctrl_pending(io2
);
2466 r2
= BIO_ctrl_get_read_request(io1
);
2468 * here we could use ..._get_write_guarantee instead of
2469 * ..._get_read_request, but by using the latter we test
2470 * restartability of the SSL implementation more thoroughly
2482 --num
; /* test restartability even more thoroughly */
2484 r
= BIO_nwrite0(io1
, &dataptr
);
2488 r
= BIO_read(io2
, dataptr
, (int)num
);
2489 if (r
!= (int)num
) { /* can't happen */
2490 fprintf(stderr
, "ERROR: BIO_read could not read "
2491 "BIO_ctrl_pending() bytes");
2495 r
= BIO_nwrite(io1
, &dataptr
, (int)num
);
2496 if (r
!= (int)num
) { /* can't happen */
2497 fprintf(stderr
, "ERROR: BIO_nwrite() did not accept "
2498 "BIO_nwrite0() bytes");
2503 printf((io2
== client_io
) ?
2504 "C->S relaying: %d bytes\n" :
2505 "S->C relaying: %d bytes\n", (int)num
);
2507 } /* no loop, BIO_ctrl_get_read_request now
2508 * returns 0 anyway */
2510 if (!progress
&& !prev_progress
)
2511 if (cw_num
> 0 || cr_num
> 0 || sw_num
> 0 || sr_num
> 0) {
2512 fprintf(stderr
, "ERROR: got stuck\n");
2513 fprintf(stderr
, " ERROR.\n");
2516 prev_progress
= progress
;
2519 while (cw_num
> 0 || cr_num
> 0 || sw_num
> 0 || sr_num
> 0);
2522 print_details(c_ssl
, "DONE via BIO pair: ");
2523 #ifndef OPENSSL_NO_NEXTPROTONEG
2524 if (verify_npn(c_ssl
, s_ssl
) < 0)
2527 if (verify_serverinfo() < 0) {
2528 fprintf(stderr
, "Server info verify error\n");
2531 if (verify_alpn(c_ssl
, s_ssl
) < 0
2532 || verify_servername(c_ssl
, s_ssl
) < 0)
2535 if (custom_ext_error
) {
2536 fprintf(stderr
, "Custom extension error\n");
2540 #ifndef OPENSSL_NO_NEXTPROTONEG
2546 ERR_print_errors(bio_err
);
2549 BIO_free(server_io
);
2551 BIO_free(client_io
);
2552 BIO_free(s_ssl_bio
);
2553 BIO_free(c_ssl_bio
);
2555 if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-client") == 0)
2556 ret
= (err_in_client
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2557 else if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-server") == 0)
2558 ret
= (err_in_server
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2568 int doit(SSL
*s_ssl
, SSL
*c_ssl
, long count
)
2570 char *cbuf
= NULL
, *sbuf
= NULL
;
2572 long cw_num
= count
, cr_num
= count
;
2573 long sw_num
= count
, sr_num
= count
;
2574 int ret
= EXIT_FAILURE
;
2579 int c_r
, c_w
, s_r
, s_w
;
2582 int c_write
, s_write
;
2583 int do_server
= 0, do_client
= 0;
2584 int max_frag
= 5 * 1024;
2585 int err_in_client
= 0;
2586 int err_in_server
= 0;
2588 bufsiz
= count
> 40 * 1024 ? 40 * 1024 : count
;
2590 if ((cbuf
= OPENSSL_zalloc(bufsiz
)) == NULL
)
2592 if ((sbuf
= OPENSSL_zalloc(bufsiz
)) == NULL
)
2595 c_to_s
= BIO_new(BIO_s_mem());
2596 s_to_c
= BIO_new(BIO_s_mem());
2597 if ((s_to_c
== NULL
) || (c_to_s
== NULL
)) {
2598 ERR_print_errors(bio_err
);
2602 c_bio
= BIO_new(BIO_f_ssl());
2603 s_bio
= BIO_new(BIO_f_ssl());
2604 if ((c_bio
== NULL
) || (s_bio
== NULL
)) {
2605 ERR_print_errors(bio_err
);
2609 SSL_set_connect_state(c_ssl
);
2610 SSL_set_bio(c_ssl
, s_to_c
, c_to_s
);
2611 SSL_set_max_send_fragment(c_ssl
, max_frag
);
2612 BIO_set_ssl(c_bio
, c_ssl
, BIO_NOCLOSE
);
2615 * We've just given our ref to these BIOs to c_ssl. We need another one to
2618 if (!BIO_up_ref(c_to_s
)) {
2619 /* c_to_s and s_to_c will get freed when we free c_ssl */
2624 if (!BIO_up_ref(s_to_c
)) {
2625 /* s_to_c will get freed when we free c_ssl */
2630 SSL_set_accept_state(s_ssl
);
2631 SSL_set_bio(s_ssl
, c_to_s
, s_to_c
);
2633 /* We've used up all our refs to these now */
2637 SSL_set_max_send_fragment(s_ssl
, max_frag
);
2638 BIO_set_ssl(s_bio
, s_ssl
, BIO_NOCLOSE
);
2644 c_write
= 1, s_write
= 0;
2646 /* We can always do writes */
2651 i
= (int)BIO_pending(s_bio
);
2652 if ((i
&& s_r
) || s_w
)
2655 i
= (int)BIO_pending(c_bio
);
2656 if ((i
&& c_r
) || c_w
)
2659 if (do_server
&& debug
) {
2660 if (SSL_in_init(s_ssl
))
2661 printf("server waiting in SSL_accept - %s\n",
2662 SSL_state_string_long(s_ssl
));
2665 if (do_client
&& debug
) {
2666 if (SSL_in_init(c_ssl
))
2667 printf("client waiting in SSL_connect - %s\n",
2668 SSL_state_string_long(c_ssl
));
2671 if (!do_client
&& !do_server
) {
2672 fprintf(stdout
, "ERROR IN STARTUP\n");
2673 ERR_print_errors(bio_err
);
2676 if (do_client
&& !(done
& C_DONE
)) {
2678 j
= (cw_num
> bufsiz
) ? (int)bufsiz
: (int)cw_num
;
2679 i
= BIO_write(c_bio
, cbuf
, j
);
2683 if (BIO_should_retry(c_bio
)) {
2684 if (BIO_should_read(c_bio
))
2686 if (BIO_should_write(c_bio
))
2689 fprintf(stderr
, "ERROR in CLIENT\n");
2691 ERR_print_errors(bio_err
);
2694 } else if (i
== 0) {
2695 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2699 printf("client wrote %d\n", i
);
2704 if (max_frag
> 1029)
2705 SSL_set_max_send_fragment(c_ssl
, max_frag
-= 5);
2708 i
= BIO_read(c_bio
, cbuf
, bufsiz
);
2712 if (BIO_should_retry(c_bio
)) {
2713 if (BIO_should_read(c_bio
))
2715 if (BIO_should_write(c_bio
))
2718 fprintf(stderr
, "ERROR in CLIENT\n");
2720 ERR_print_errors(bio_err
);
2723 } else if (i
== 0) {
2724 fprintf(stderr
, "SSL CLIENT STARTUP FAILED\n");
2728 printf("client read %d\n", i
);
2737 done
= S_DONE
| C_DONE
;
2743 if (do_server
&& !(done
& S_DONE
)) {
2745 i
= BIO_read(s_bio
, sbuf
, bufsiz
);
2749 if (BIO_should_retry(s_bio
)) {
2750 if (BIO_should_read(s_bio
))
2752 if (BIO_should_write(s_bio
))
2755 fprintf(stderr
, "ERROR in SERVER\n");
2757 ERR_print_errors(bio_err
);
2760 } else if (i
== 0) {
2761 ERR_print_errors(bio_err
);
2763 "SSL SERVER STARTUP FAILED in SSL_read\n");
2767 printf("server read %d\n", i
);
2780 j
= (sw_num
> bufsiz
) ? (int)bufsiz
: (int)sw_num
;
2781 i
= BIO_write(s_bio
, sbuf
, j
);
2785 if (BIO_should_retry(s_bio
)) {
2786 if (BIO_should_read(s_bio
))
2788 if (BIO_should_write(s_bio
))
2791 fprintf(stderr
, "ERROR in SERVER\n");
2793 ERR_print_errors(bio_err
);
2796 } else if (i
== 0) {
2797 ERR_print_errors(bio_err
);
2799 "SSL SERVER STARTUP FAILED in SSL_write\n");
2803 printf("server wrote %d\n", i
);
2809 if (max_frag
> 1029)
2810 SSL_set_max_send_fragment(s_ssl
, max_frag
-= 5);
2815 if ((done
& S_DONE
) && (done
& C_DONE
))
2820 print_details(c_ssl
, "DONE: ");
2821 #ifndef OPENSSL_NO_NEXTPROTONEG
2822 if (verify_npn(c_ssl
, s_ssl
) < 0)
2825 if (verify_serverinfo() < 0) {
2826 fprintf(stderr
, "Server info verify error\n");
2829 if (custom_ext_error
) {
2830 fprintf(stderr
, "Custom extension error\n");
2837 BIO_free_all(c_bio
);
2838 BIO_free_all(s_bio
);
2842 if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-client") == 0)
2843 ret
= (err_in_client
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2844 else if (should_negotiate
!= NULL
&& strcmp(should_negotiate
, "fail-server") == 0)
2845 ret
= (err_in_server
!= 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
2850 static int verify_callback(int ok
, X509_STORE_CTX
*ctx
)
2854 s
= X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx
)),
2858 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx
), buf
);
2860 fprintf(stderr
, "depth=%d error=%d %s\n",
2861 X509_STORE_CTX_get_error_depth(ctx
),
2862 X509_STORE_CTX_get_error(ctx
), buf
);
2867 int i
= X509_STORE_CTX_get_error(ctx
);
2871 fprintf(stderr
, "Error string: %s\n",
2872 X509_verify_cert_error_string(i
));
2874 case X509_V_ERR_CERT_NOT_YET_VALID
:
2875 case X509_V_ERR_CERT_HAS_EXPIRED
:
2876 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
2885 static int app_verify_callback(X509_STORE_CTX
*ctx
, void *arg
)
2888 struct app_verify_arg
*cb_arg
= arg
;
2890 if (cb_arg
->app_verify
) {
2891 char *s
= NULL
, buf
[256];
2892 X509
*c
= X509_STORE_CTX_get0_cert(ctx
);
2894 printf("In app_verify_callback, allowing cert. ");
2895 printf("Arg is: %s\n", cb_arg
->string
);
2896 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2897 (void *)ctx
, (void *)c
);
2899 s
= X509_NAME_oneline(X509_get_subject_name(c
), buf
, 256);
2901 printf("cert depth=%d %s\n",
2902 X509_STORE_CTX_get_error_depth(ctx
), buf
);
2907 ok
= X509_verify_cert(ctx
);
2912 #ifndef OPENSSL_NO_PSK
2913 /* convert the PSK key (psk_key) in ascii to binary (psk) */
2914 static int psk_key2bn(const char *pskkey
, unsigned char *psk
,
2915 unsigned int max_psk_len
)
2920 ret
= BN_hex2bn(&bn
, pskkey
);
2922 BIO_printf(bio_err
, "Could not convert PSK key '%s' to BIGNUM\n",
2927 if (BN_num_bytes(bn
) > (int)max_psk_len
) {
2929 "psk buffer of callback is too small (%d) for key (%d)\n",
2930 max_psk_len
, BN_num_bytes(bn
));
2934 ret
= BN_bn2bin(bn
, psk
);
2939 static unsigned int psk_client_callback(SSL
*ssl
, const char *hint
,
2941 unsigned int max_identity_len
,
2943 unsigned int max_psk_len
)
2946 unsigned int psk_len
= 0;
2948 ret
= BIO_snprintf(identity
, max_identity_len
, "Client_identity");
2952 fprintf(stderr
, "client: created identity '%s' len=%d\n", identity
,
2954 ret
= psk_key2bn(psk_key
, psk
, max_psk_len
);
2962 static unsigned int psk_server_callback(SSL
*ssl
, const char *identity
,
2964 unsigned int max_psk_len
)
2966 unsigned int psk_len
= 0;
2968 if (strcmp(identity
, "Client_identity") != 0) {
2969 BIO_printf(bio_err
, "server: PSK error: client identity not found\n");
2972 psk_len
= psk_key2bn(psk_key
, psk
, max_psk_len
);