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 if (alpn_selected == NULL) {
314 fprintf(stderr, "failed to allocate memory\n");
315 OPENSSL_free(protos);
318 memcpy(alpn_selected, *out, *outlen);
319 *out = alpn_selected;
321 OPENSSL_free(protos);
322 return SSL_TLSEXT_ERR_OK;
325 static int verify_alpn(SSL *client, SSL *server)
327 const unsigned char *client_proto, *server_proto;
328 unsigned int client_proto_len = 0, server_proto_len = 0;
329 SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
330 SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
332 OPENSSL_free(alpn_selected);
333 alpn_selected = NULL;
335 if (client_proto_len != server_proto_len) {
336 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
340 if (client_proto != NULL &&
341 memcmp(client_proto, server_proto, client_proto_len) != 0) {
342 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
346 if (client_proto_len > 0 && alpn_expected == NULL) {
347 BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
351 if (alpn_expected != NULL &&
352 (client_proto_len != strlen(alpn_expected) ||
353 memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
354 BIO_printf(bio_stdout,
355 "ALPN selected protocols not equal to expected protocol: %s\n",
363 BIO_printf(bio_stdout, "ALPN results: client: '");
364 BIO_write(bio_stdout, client_proto, client_proto_len);
365 BIO_printf(bio_stdout, "', server: '");
366 BIO_write(bio_stdout, server_proto, server_proto_len);
367 BIO_printf(bio_stdout, "'\n");
368 BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '",
370 if (SSL_get_SSL_CTX(server) == s_ctx2) {
371 BIO_printf(bio_stdout, "%s'\n",
374 BIO_printf(bio_stdout, "%s'\n",
381 * WARNING : below extension types are *NOT* IETF assigned, and could
382 * conflict if these types are reassigned and handled specially by OpenSSL
385 #define TACK_EXT_TYPE 62208
386 #define CUSTOM_EXT_TYPE_0 1000
387 #define CUSTOM_EXT_TYPE_1 1001
388 #define CUSTOM_EXT_TYPE_2 1002
389 #define CUSTOM_EXT_TYPE_3 1003
391 static const char custom_ext_cli_string[] = "abc";
392 static const char custom_ext_srv_string[] = "defg";
394 /* These set from cmdline */
395 static char *serverinfo_file = NULL;
396 static int serverinfo_sct = 0;
397 static int serverinfo_tack = 0;
399 /* These set based on extension callbacks */
400 static int serverinfo_sct_seen = 0;
401 static int serverinfo_tack_seen = 0;
402 static int serverinfo_other_seen = 0;
404 /* This set from cmdline */
405 static int custom_ext = 0;
407 /* This set based on extension callbacks */
408 static int custom_ext_error = 0;
410 static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
411 const unsigned char *in, size_t inlen,
414 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp)
415 serverinfo_sct_seen++;
416 else if (ext_type == TACK_EXT_TYPE)
417 serverinfo_tack_seen++;
419 serverinfo_other_seen++;
423 static int verify_serverinfo(void)
425 if (serverinfo_sct != serverinfo_sct_seen)
427 if (serverinfo_tack != serverinfo_tack_seen)
429 if (serverinfo_other_seen)
435 * Four test cases for custom extensions:
436 * 0 - no ClientHello extension or ServerHello response
437 * 1 - ClientHello with "abc", no response
438 * 2 - ClientHello with "abc", empty response
439 * 3 - ClientHello with "abc", "defg" response
442 static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
443 const unsigned char **out,
444 size_t *outlen, int *al, void *arg)
446 if (ext_type != CUSTOM_EXT_TYPE_0)
447 custom_ext_error = 1;
448 return 0; /* Don't send an extension */
451 static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
452 const unsigned char *in,
453 size_t inlen, int *al, void *arg)
458 static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
459 const unsigned char **out,
460 size_t *outlen, int *al, void *arg)
462 if (ext_type != CUSTOM_EXT_TYPE_1)
463 custom_ext_error = 1;
464 *out = (const unsigned char *)custom_ext_cli_string;
465 *outlen = strlen(custom_ext_cli_string);
466 return 1; /* Send "abc" */
469 static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
470 const unsigned char *in,
471 size_t inlen, int *al, void *arg)
476 static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
477 const unsigned char **out,
478 size_t *outlen, int *al, void *arg)
480 if (ext_type != CUSTOM_EXT_TYPE_2)
481 custom_ext_error = 1;
482 *out = (const unsigned char *)custom_ext_cli_string;
483 *outlen = strlen(custom_ext_cli_string);
484 return 1; /* Send "abc" */
487 static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
488 const unsigned char *in,
489 size_t inlen, int *al, void *arg)
491 if (ext_type != CUSTOM_EXT_TYPE_2)
492 custom_ext_error = 1;
494 custom_ext_error = 1; /* Should be empty response */
498 static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
499 const unsigned char **out,
500 size_t *outlen, int *al, void *arg)
502 if (ext_type != CUSTOM_EXT_TYPE_3)
503 custom_ext_error = 1;
504 *out = (const unsigned char *)custom_ext_cli_string;
505 *outlen = strlen(custom_ext_cli_string);
506 return 1; /* Send "abc" */
509 static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
510 const unsigned char *in,
511 size_t inlen, int *al, void *arg)
513 if (ext_type != CUSTOM_EXT_TYPE_3)
514 custom_ext_error = 1;
515 if (inlen != strlen(custom_ext_srv_string))
516 custom_ext_error = 1;
517 if (memcmp(custom_ext_srv_string, in, inlen) != 0)
518 custom_ext_error = 1; /* Check for "defg" */
523 * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
526 static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
527 const unsigned char *in,
528 size_t inlen, int *al, void *arg)
530 custom_ext_error = 1;
534 /* 'add' callbacks are only called if the 'parse' callback is called */
535 static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
536 const unsigned char **out,
537 size_t *outlen, int *al, void *arg)
539 /* Error: should not have been called */
540 custom_ext_error = 1;
541 return 0; /* Don't send an extension */
544 static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
545 const unsigned char *in,
546 size_t inlen, int *al, void *arg)
548 if (ext_type != CUSTOM_EXT_TYPE_1)
549 custom_ext_error = 1;
550 /* Check for "abc" */
551 if (inlen != strlen(custom_ext_cli_string))
552 custom_ext_error = 1;
553 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
554 custom_ext_error = 1;
558 static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
559 const unsigned char **out,
560 size_t *outlen, int *al, void *arg)
562 return 0; /* Don't send an extension */
565 static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
566 const unsigned char *in,
567 size_t inlen, int *al, void *arg)
569 if (ext_type != CUSTOM_EXT_TYPE_2)
570 custom_ext_error = 1;
571 /* Check for "abc" */
572 if (inlen != strlen(custom_ext_cli_string))
573 custom_ext_error = 1;
574 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
575 custom_ext_error = 1;
579 static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
580 const unsigned char **out,
581 size_t *outlen, int *al, void *arg)
585 return 1; /* Send empty extension */
588 static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
589 const unsigned char *in,
590 size_t inlen, int *al, void *arg)
592 if (ext_type != CUSTOM_EXT_TYPE_3)
593 custom_ext_error = 1;
594 /* Check for "abc" */
595 if (inlen != strlen(custom_ext_cli_string))
596 custom_ext_error = 1;
597 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
598 custom_ext_error = 1;
602 static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
603 const unsigned char **out,
604 size_t *outlen, int *al, void *arg)
606 *out = (const unsigned char *)custom_ext_srv_string;
607 *outlen = strlen(custom_ext_srv_string);
608 return 1; /* Send "defg" */
611 static char *cipher = NULL;
612 static char *ciphersuites = NULL;
613 static int verbose = 0;
614 static int debug = 0;
616 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
617 long bytes, clock_t *s_time, clock_t *c_time);
618 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
620 int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
622 static void sv_usage(void)
624 fprintf(stderr, "usage: ssltest [args ...]\n");
625 fprintf(stderr, "\n");
626 fprintf(stderr, " -server_auth - check server certificate\n");
627 fprintf(stderr, " -client_auth - do client authentication\n");
628 fprintf(stderr, " -v - more output\n");
629 fprintf(stderr, " -d - debug output\n");
630 fprintf(stderr, " -reuse - use session-id reuse\n");
631 fprintf(stderr, " -num <val> - number of connections to perform\n");
633 " -bytes <val> - number of bytes to swap between client/server\n");
634 #ifndef OPENSSL_NO_DH
636 " -dhe512 - use 512 bit key for DHE (to test failure)\n");
638 " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
640 " -dhe2048 - use 2048 bit key (safe prime) for DHE (default, no-op)\n");
642 " -dhe4096 - use 4096 bit key (safe prime) for DHE\n");
644 fprintf(stderr, " -no_dhe - disable DHE\n");
645 #ifndef OPENSSL_NO_EC
646 fprintf(stderr, " -no_ecdhe - disable ECDHE\n");
648 #ifndef OPENSSL_NO_PSK
649 fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n");
651 #ifndef OPENSSL_NO_SSL3
652 fprintf(stderr, " -ssl3 - use SSLv3\n");
654 #ifndef OPENSSL_NO_TLS1
655 fprintf(stderr, " -tls1 - use TLSv1\n");
657 #ifndef OPENSSL_NO_TLS1_1
658 fprintf(stderr, " -tls1_1 - use TLSv1.1\n");
660 #ifndef OPENSSL_NO_TLS1_2
661 fprintf(stderr, " -tls1_2 - use TLSv1.2\n");
663 #ifndef OPENSSL_NO_DTLS
664 fprintf(stderr, " -dtls - use DTLS\n");
665 #ifndef OPENSSL_NO_DTLS1
666 fprintf(stderr, " -dtls1 - use DTLSv1\n");
668 #ifndef OPENSSL_NO_DTLS1_2
669 fprintf(stderr, " -dtls12 - use DTLSv1.2\n");
672 fprintf(stderr, " -CApath arg - PEM format directory of CA's\n");
673 fprintf(stderr, " -CAfile arg - PEM format file of CA's\n");
674 fprintf(stderr, " -s_cert arg - Server certificate file\n");
676 " -s_key arg - Server key file (default: same as -cert)\n");
677 fprintf(stderr, " -c_cert arg - Client certificate file\n");
679 " -c_key arg - Client key file (default: same as -c_cert)\n");
680 fprintf(stderr, " -cipher arg - The TLSv1.2 and below cipher list\n");
681 fprintf(stderr, " -ciphersuites arg - The TLSv1.3 ciphersuites\n");
682 fprintf(stderr, " -bio_pair - Use BIO pairs\n");
683 fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n");
684 fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n");
685 fprintf(stderr, " -f - Test even cases that can't work\n");
687 " -time - measure processor time used by client and server\n");
688 fprintf(stderr, " -zlib - use zlib compression\n");
689 #ifndef OPENSSL_NO_NEXTPROTONEG
690 fprintf(stderr, " -npn_client - have client side offer NPN\n");
691 fprintf(stderr, " -npn_server - have server side offer NPN\n");
692 fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
694 fprintf(stderr, " -serverinfo_file file - have server use this file\n");
695 fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n");
697 " -serverinfo_tack - have client offer and expect TACK\n");
699 " -custom_ext - try various custom extension callbacks\n");
700 fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
701 fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
702 fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n");
703 fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
705 " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
706 fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
707 fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
708 fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
709 fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
710 fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
711 #ifndef OPENSSL_NO_CT
712 fprintf(stderr, " -noct - no certificate transparency\n");
713 fprintf(stderr, " -requestct - request certificate transparency\n");
714 fprintf(stderr, " -requirect - require certificate transparency\n");
716 fprintf(stderr, " -sn_client <string> - have client request this servername\n");
717 fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n");
718 fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n");
719 fprintf(stderr, " -sn_expect1 - expected server 1\n");
720 fprintf(stderr, " -sn_expect2 - expected server 2\n");
721 fprintf(stderr, " -server_sess_out <file> - Save the server session to a file\n");
722 fprintf(stderr, " -server_sess_in <file> - Read the server session from a file\n");
723 fprintf(stderr, " -client_sess_out <file> - Save the client session to a file\n");
724 fprintf(stderr, " -client_sess_in <file> - Read the client session from a file\n");
725 fprintf(stderr, " -should_reuse <number> - The expected state of reusing the session\n");
726 fprintf(stderr, " -no_ticket - do not issue TLS session ticket\n");
727 fprintf(stderr, " -client_ktls - try to enable client KTLS\n");
728 fprintf(stderr, " -server_ktls - try to enable server KTLS\n");
729 fprintf(stderr, " -provider <name> - Load the given provider into the library context\n");
730 fprintf(stderr, " -config <cnf> - Load the given config file into the library context\n");
733 static void print_key_details(BIO *out, EVP_PKEY *key)
735 int keyid = EVP_PKEY_get_id(key);
737 #ifndef OPENSSL_NO_EC
738 if (keyid == EVP_PKEY_EC) {
742 if (!EVP_PKEY_get_group_name(key, group, sizeof(group), &size))
743 strcpy(group, "unknown group");
744 BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_get_bits(key), group);
760 algname = OBJ_nid2sn(keyid);
763 BIO_printf(out, "%d bits %s", EVP_PKEY_get_bits(key), algname);
767 static void print_details(SSL *c_ssl, const char *prefix)
769 const SSL_CIPHER *ciph;
774 ciph = SSL_get_current_cipher(c_ssl);
775 BIO_printf(bio_stdout, "%s%s, cipher %s %s",
777 SSL_get_version(c_ssl),
778 SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
779 cert = SSL_get0_peer_certificate(c_ssl);
781 EVP_PKEY* pubkey = X509_get0_pubkey(cert);
783 if (pubkey != NULL) {
784 BIO_puts(bio_stdout, ", ");
785 print_key_details(bio_stdout, pubkey);
788 if (SSL_get_peer_tmp_key(c_ssl, &pkey)) {
789 BIO_puts(bio_stdout, ", temp key: ");
790 print_key_details(bio_stdout, pkey);
793 if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
794 BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
795 BIO_printf(bio_stdout, "\n");
799 * protocol_from_string - converts a protocol version string to a number
801 * Returns -1 on failure or the version on success
803 static int protocol_from_string(const char *value)
805 struct protocol_versions {
809 static const struct protocol_versions versions[] = {
810 {"ssl3", SSL3_VERSION},
811 {"tls1", TLS1_VERSION},
812 {"tls1.1", TLS1_1_VERSION},
813 {"tls1.2", TLS1_2_VERSION},
814 {"tls1.3", TLS1_3_VERSION},
815 {"dtls1", DTLS1_VERSION},
816 {"dtls1.2", DTLS1_2_VERSION}};
818 size_t n = OSSL_NELEM(versions);
820 for (i = 0; i < n; i++)
821 if (strcmp(versions[i].name, value) == 0)
822 return versions[i].version;
826 static SSL_SESSION *read_session(const char *filename)
829 BIO *f = BIO_new_file(filename, "r");
832 BIO_printf(bio_err, "Can't open session file %s\n", filename);
833 ERR_print_errors(bio_err);
836 sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL);
838 BIO_printf(bio_err, "Can't parse session file %s\n", filename);
839 ERR_print_errors(bio_err);
845 static int write_session(const char *filename, SSL_SESSION *sess)
850 BIO_printf(bio_err, "No session information\n");
854 f = BIO_new_file(filename, "w");
856 BIO_printf(bio_err, "Can't open session file %s\n", filename);
857 ERR_print_errors(bio_err);
860 PEM_write_bio_SSL_SESSION(f, sess);
866 * set_protocol_version - Sets protocol version minimum or maximum
868 * Returns 0 on failure and 1 on success
870 static int set_protocol_version(const char *version, SSL *ssl, int setting)
872 if (version != NULL) {
873 int ver = protocol_from_string(version);
875 BIO_printf(bio_err, "Error parsing: %s\n", version);
878 return SSL_ctrl(ssl, setting, ver, NULL);
883 int main(int argc, char *argv[])
885 const char *CApath = NULL, *CAfile = NULL;
887 enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM;
889 int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_1 = 0, tls1_2 = 0, ssl3 = 0;
890 int ret = EXIT_FAILURE;
892 int server_auth = 0, i;
893 struct app_verify_arg app_verify_arg =
894 { APP_CALLBACK_STRING, 0 };
895 SSL_CTX *c_ctx = NULL;
896 const SSL_METHOD *meth = NULL;
898 int number = 1, reuse = 0;
899 int should_reuse = -1;
901 int client_ktls = 0, server_ktls = 0;
903 #ifndef OPENSSL_NO_DH
905 int dhe512 = 0, dhe1024dsa = 0, dhe4096 = 0;
910 clock_t s_time = 0, c_time = 0;
911 #ifndef OPENSSL_NO_COMP
913 COMP_METHOD *cm = NULL;
914 STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
917 int min_version = 0, max_version = 0;
918 #ifndef OPENSSL_NO_CT
920 * Disable CT validation by default, because it will interfere with
921 * anything using custom extension handlers to deal with SCT extensions.
923 int ct_validation = 0;
925 SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL;
926 STACK_OF(OPENSSL_STRING) *conf_args = NULL;
927 char *arg = NULL, *argn = NULL;
928 const char *provider = NULL, *config = NULL;
929 OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL;
930 OSSL_LIB_CTX *libctx = NULL;
935 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
936 bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
938 s_cctx = SSL_CONF_CTX_new();
939 s_cctx2 = SSL_CONF_CTX_new();
940 c_cctx = SSL_CONF_CTX_new();
942 if (!s_cctx || !c_cctx || !s_cctx2) {
943 ERR_print_errors(bio_err);
947 SSL_CONF_CTX_set_flags(s_cctx,
948 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
949 SSL_CONF_FLAG_CERTIFICATE |
950 SSL_CONF_FLAG_REQUIRE_PRIVATE);
951 SSL_CONF_CTX_set_flags(s_cctx2,
952 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
953 SSL_CONF_FLAG_CERTIFICATE |
954 SSL_CONF_FLAG_REQUIRE_PRIVATE);
955 if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
956 ERR_print_errors(bio_err);
959 if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) {
960 ERR_print_errors(bio_err);
964 SSL_CONF_CTX_set_flags(c_cctx,
965 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT |
966 SSL_CONF_FLAG_CERTIFICATE |
967 SSL_CONF_FLAG_REQUIRE_PRIVATE);
968 if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
969 ERR_print_errors(bio_err);
977 if (strcmp(*argv, "-F") == 0) {
979 "not compiled with FIPS support, so exiting without running.\n");
981 } else if (strcmp(*argv, "-server_auth") == 0)
983 else if (strcmp(*argv, "-client_auth") == 0)
985 else if (strcmp(*argv, "-v") == 0)
987 else if (strcmp(*argv, "-d") == 0)
989 else if (strcmp(*argv, "-reuse") == 0)
991 else if (strcmp(*argv, "-no_dhe") == 0)
993 /* unused in this case */;
996 else if (strcmp(*argv, "-dhe512") == 0)
998 else if (strcmp(*argv, "-dhe1024dsa") == 0)
1000 else if (strcmp(*argv, "-dhe4096") == 0)
1003 else if (strcmp(*argv, "-no_ecdhe") == 0)
1005 else if (strcmp(*argv, "-psk") == 0) {
1008 psk_key = *(++argv);
1009 #ifndef OPENSSL_NO_PSK
1010 if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) {
1011 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
1018 else if (strcmp(*argv, "-tls1_2") == 0) {
1020 } else if (strcmp(*argv, "-tls1_1") == 0) {
1022 } else if (strcmp(*argv, "-tls1") == 0) {
1024 } else if (strcmp(*argv, "-ssl3") == 0) {
1026 } else if (strcmp(*argv, "-dtls1") == 0) {
1028 } else if (strcmp(*argv, "-dtls12") == 0) {
1030 } else if (strcmp(*argv, "-dtls") == 0) {
1032 } else if (HAS_PREFIX(*argv, "-num")) {
1035 number = atoi(*(++argv));
1038 } else if (strcmp(*argv, "-bytes") == 0) {
1041 bytes = atol(*(++argv));
1044 i = strlen(argv[0]);
1045 if (argv[0][i - 1] == 'k')
1047 if (argv[0][i - 1] == 'm')
1048 bytes *= 1024L * 1024L;
1049 } else if (strcmp(*argv, "-cipher") == 0) {
1053 } else if (strcmp(*argv, "-ciphersuites") == 0) {
1056 ciphersuites = *(++argv);
1057 } else if (strcmp(*argv, "-CApath") == 0) {
1061 } else if (strcmp(*argv, "-CAfile") == 0) {
1065 } else if (strcmp(*argv, "-bio_pair") == 0) {
1066 bio_type = BIO_PAIR;
1068 #ifndef OPENSSL_NO_SOCK
1069 else if (strcmp(*argv, "-ipv4") == 0) {
1070 bio_type = BIO_IPV4;
1071 } else if (strcmp(*argv, "-ipv6") == 0) {
1072 bio_type = BIO_IPV6;
1075 else if (strcmp(*argv, "-f") == 0) {
1077 } else if (strcmp(*argv, "-time") == 0) {
1080 #ifndef OPENSSL_NO_CT
1081 else if (strcmp(*argv, "-noct") == 0) {
1084 else if (strcmp(*argv, "-ct") == 0) {
1088 #ifndef OPENSSL_NO_COMP
1089 else if (strcmp(*argv, "-zlib") == 0) {
1093 else if (strcmp(*argv, "-app_verify") == 0) {
1094 app_verify_arg.app_verify = 1;
1096 #ifndef OPENSSL_NO_NEXTPROTONEG
1097 else if (strcmp(*argv, "-npn_client") == 0) {
1099 } else if (strcmp(*argv, "-npn_server") == 0) {
1101 } else if (strcmp(*argv, "-npn_server_reject") == 0) {
1102 npn_server_reject = 1;
1105 else if (strcmp(*argv, "-serverinfo_sct") == 0) {
1107 } else if (strcmp(*argv, "-serverinfo_tack") == 0) {
1108 serverinfo_tack = 1;
1109 } else if (strcmp(*argv, "-serverinfo_file") == 0) {
1112 serverinfo_file = *(++argv);
1113 } else if (strcmp(*argv, "-custom_ext") == 0) {
1115 } else if (strcmp(*argv, "-alpn_client") == 0) {
1118 alpn_client = *(++argv);
1119 } else if (strcmp(*argv, "-alpn_server") == 0 ||
1120 strcmp(*argv, "-alpn_server1") == 0) {
1123 alpn_server = *(++argv);
1124 } else if (strcmp(*argv, "-alpn_server2") == 0) {
1127 alpn_server2 = *(++argv);
1128 } else if (strcmp(*argv, "-alpn_expected") == 0) {
1131 alpn_expected = *(++argv);
1132 } else if (strcmp(*argv, "-server_min_proto") == 0) {
1135 server_min_proto = *(++argv);
1136 } else if (strcmp(*argv, "-server_max_proto") == 0) {
1139 server_max_proto = *(++argv);
1140 } else if (strcmp(*argv, "-client_min_proto") == 0) {
1143 client_min_proto = *(++argv);
1144 } else if (strcmp(*argv, "-client_max_proto") == 0) {
1147 client_max_proto = *(++argv);
1148 } else if (strcmp(*argv, "-should_negotiate") == 0) {
1151 should_negotiate = *(++argv);
1152 } else if (strcmp(*argv, "-sn_client") == 0) {
1155 sn_client = *(++argv);
1156 } else if (strcmp(*argv, "-sn_server1") == 0) {
1159 sn_server1 = *(++argv);
1160 } else if (strcmp(*argv, "-sn_server2") == 0) {
1163 sn_server2 = *(++argv);
1164 } else if (strcmp(*argv, "-sn_expect1") == 0) {
1166 } else if (strcmp(*argv, "-sn_expect2") == 0) {
1168 } else if (strcmp(*argv, "-server_sess_out") == 0) {
1171 server_sess_out = *(++argv);
1172 } else if (strcmp(*argv, "-server_sess_in") == 0) {
1175 server_sess_in = *(++argv);
1176 } else if (strcmp(*argv, "-client_sess_out") == 0) {
1179 client_sess_out = *(++argv);
1180 } else if (strcmp(*argv, "-client_sess_in") == 0) {
1183 client_sess_in = *(++argv);
1184 } else if (strcmp(*argv, "-should_reuse") == 0) {
1187 should_reuse = !!atoi(*(++argv));
1188 } else if (strcmp(*argv, "-no_ticket") == 0) {
1190 } else if (strcmp(*argv, "-client_ktls") == 0) {
1192 } else if (strcmp(*argv, "-server_ktls") == 0) {
1194 } else if (strcmp(*argv, "-provider") == 0) {
1197 provider = *(++argv);
1198 } else if (strcmp(*argv, "-config") == 0) {
1206 /* Try to process command using SSL_CONF */
1207 rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv);
1208 /* If not processed try server */
1210 rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv);
1211 /* Recognised: store it for later use */
1216 conf_args = sk_OPENSSL_STRING_new_null();
1220 if (!sk_OPENSSL_STRING_push(conf_args, arg))
1222 if (!sk_OPENSSL_STRING_push(conf_args, argn))
1227 BIO_printf(bio_err, "Missing argument for %s\n", arg);
1229 BIO_printf(bio_err, "Error with command %s\n", arg);
1231 BIO_printf(bio_err, "unknown option %s\n", arg);
1244 if (ssl3 + tls1 + tls1_1 + tls1_2 + dtls + dtls1 + dtls12 > 1) {
1245 fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
1250 #ifdef OPENSSL_NO_SSL3
1255 #ifdef OPENSSL_NO_TLS1
1260 #ifdef OPENSSL_NO_TLS1_1
1265 #ifdef OPENSSL_NO_TLS1_2
1270 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
1275 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
1283 * Testing was requested for a compiled-out protocol (e.g. SSLv3).
1284 * Ideally, we would error out, but the generic test wrapper can't know
1285 * when to expect failure. So we do nothing and return success.
1288 fprintf(stderr, "Testing was requested for a disabled protocol. "
1289 "Skipping tests.\n");
1294 if (!ssl3 && !tls1 && !tls1_1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1
1295 && !reuse && !force) {
1296 fprintf(stderr, "This case cannot work. Use -f to perform "
1297 "the test anyway (and\n-d to see what happens), "
1298 "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
1299 "to avoid protocol mismatch.\n");
1304 if (bio_type == BIO_MEM) {
1305 fprintf(stderr, "Using BIO pair (-bio_pair)\n");
1306 bio_type = BIO_PAIR;
1308 if (number < 50 && !force)
1310 "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
1313 #ifndef OPENSSL_NO_COMP
1314 if (comp == COMP_ZLIB)
1317 if (COMP_get_type(cm) != NID_undef) {
1318 if (SSL_COMP_add_compression_method(comp, cm) != 0) {
1319 fprintf(stderr, "Failed to add compression method\n");
1320 ERR_print_errors_fp(stderr);
1324 "Warning: %s compression not supported\n",
1325 comp == COMP_ZLIB ? "zlib" : "unknown");
1326 ERR_print_errors_fp(stderr);
1329 ssl_comp_methods = SSL_COMP_get_compression_methods();
1330 n = sk_SSL_COMP_num(ssl_comp_methods);
1333 printf("Available compression methods:");
1334 for (j = 0; j < n; j++) {
1335 SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
1336 printf(" %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c));
1342 #ifndef OPENSSL_NO_TLS
1343 meth = TLS_method();
1345 min_version = SSL3_VERSION;
1346 max_version = SSL3_VERSION;
1348 min_version = TLS1_VERSION;
1349 max_version = TLS1_VERSION;
1350 } else if (tls1_1) {
1351 min_version = TLS1_1_VERSION;
1352 max_version = TLS1_1_VERSION;
1353 } else if (tls1_2) {
1354 min_version = TLS1_2_VERSION;
1355 max_version = TLS1_2_VERSION;
1358 # if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)
1359 /* We only have ec and dh based built-in groups for TLSv1.3 */
1360 max_version = TLS1_2_VERSION;
1366 #ifndef OPENSSL_NO_DTLS
1367 if (dtls || dtls1 || dtls12) {
1368 meth = DTLS_method();
1370 min_version = DTLS1_VERSION;
1371 max_version = DTLS1_VERSION;
1372 } else if (dtls12) {
1373 min_version = DTLS1_2_VERSION;
1374 max_version = DTLS1_2_VERSION;
1382 if (provider != NULL
1383 && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
1386 c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1387 s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1388 s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */
1389 if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) {
1390 ERR_print_errors(bio_err);
1394 * Since we will use low security ciphersuites and keys for testing set
1395 * security level to zero by default. Tests can override this by adding
1396 * "@SECLEVEL=n" to the cipher string.
1398 SSL_CTX_set_security_level(c_ctx, 0);
1399 SSL_CTX_set_security_level(s_ctx, 0);
1400 SSL_CTX_set_security_level(s_ctx2, 0);
1403 SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
1404 SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
1407 if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0)
1409 if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0)
1411 if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0)
1413 if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0)
1416 if (cipher != NULL) {
1417 if (strcmp(cipher, "") == 0) {
1418 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) {
1419 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1422 ERR_print_errors(bio_err);
1426 /* Should have failed when clearing all TLSv1.2 ciphers. */
1427 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1431 if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) {
1432 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1435 ERR_print_errors(bio_err);
1439 /* Should have failed when clearing all TLSv1.2 ciphers. */
1440 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1444 if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1445 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1448 ERR_print_errors(bio_err);
1452 /* Should have failed when clearing all TLSv1.2 ciphers. */
1453 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1457 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
1458 || !SSL_CTX_set_cipher_list(s_ctx, cipher)
1459 || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1460 ERR_print_errors(bio_err);
1465 if (ciphersuites != NULL) {
1466 if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
1467 || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
1468 || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
1469 ERR_print_errors(bio_err);
1474 #ifndef OPENSSL_NO_CT
1475 if (ct_validation &&
1476 !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
1477 ERR_print_errors(bio_err);
1482 /* Process SSL_CONF arguments */
1483 SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
1484 SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
1485 SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
1487 for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
1489 arg = sk_OPENSSL_STRING_value(conf_args, i);
1490 argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
1491 rv = SSL_CONF_cmd(c_cctx, arg, argn);
1492 /* If not recognised use server context */
1494 rv = SSL_CONF_cmd(s_cctx2, arg, argn);
1496 rv = SSL_CONF_cmd(s_cctx, arg, argn);
1499 BIO_printf(bio_err, "Error processing %s %s\n",
1500 arg, argn ? argn : "");
1501 ERR_print_errors(bio_err);
1506 if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
1507 BIO_puts(bio_err, "Error finishing context\n");
1508 ERR_print_errors(bio_err);
1511 #ifndef OPENSSL_NO_DH
1514 dhpkey = get_dh1024dsa(libctx);
1516 dhpkey = get_dh512(libctx);
1518 dhpkey = get_dh4096(libctx);
1520 dhpkey = get_dh2048(libctx);
1522 if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
1523 EVP_PKEY_free(dhpkey);
1524 BIO_puts(bio_err, "Error getting DH parameters\n");
1525 ERR_print_errors(bio_err);
1528 SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey);
1529 SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey);
1533 if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
1534 || SSL_CTX_load_verify_dir(s_ctx, CApath))
1535 || !SSL_CTX_set_default_verify_paths(s_ctx)
1536 || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
1537 || SSL_CTX_load_verify_dir(s_ctx2, CApath))
1538 || !SSL_CTX_set_default_verify_paths(s_ctx2)
1539 || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
1540 || SSL_CTX_load_verify_dir(c_ctx, CApath))
1541 || !SSL_CTX_set_default_verify_paths(c_ctx)) {
1542 ERR_print_errors(bio_err);
1545 #ifndef OPENSSL_NO_CT
1546 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
1547 !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
1548 !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
1549 ERR_print_errors(bio_err);
1554 printf("client authentication\n");
1555 SSL_CTX_set_verify(s_ctx,
1556 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1558 SSL_CTX_set_verify(s_ctx2,
1559 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1561 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
1563 SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
1567 printf("server authentication\n");
1568 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
1569 SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
1574 int session_id_context = 0;
1575 if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
1576 sizeof(session_id_context)) ||
1577 !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
1578 sizeof(session_id_context))) {
1579 ERR_print_errors(bio_err);
1584 /* Use PSK only if PSK key is given */
1585 if (psk_key != NULL) {
1587 * no_psk is used to avoid putting psk command to openssl tool
1591 * if PSK is not compiled in and psk key is given, do nothing and
1597 #ifndef OPENSSL_NO_PSK
1598 SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
1599 SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
1600 SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
1602 BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
1603 if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
1604 !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
1605 BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
1606 ERR_print_errors(bio_err);
1612 #ifndef OPENSSL_NO_NEXTPROTONEG
1614 SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
1617 if (npn_server_reject) {
1619 "Can't have both -npn_server and -npn_server_reject\n");
1622 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
1623 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
1625 if (npn_server_reject) {
1626 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
1627 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
1631 if (serverinfo_sct) {
1632 if (!SSL_CTX_add_client_custom_ext(c_ctx,
1633 TLSEXT_TYPE_signed_certificate_timestamp,
1635 serverinfo_cli_parse_cb, NULL)) {
1636 BIO_printf(bio_err, "Error adding SCT extension\n");
1640 if (serverinfo_tack) {
1641 if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
1643 serverinfo_cli_parse_cb, NULL)) {
1644 BIO_printf(bio_err, "Error adding TACK extension\n");
1648 if (serverinfo_file)
1649 if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
1650 !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
1651 BIO_printf(bio_err, "missing serverinfo file\n");
1656 if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
1657 custom_ext_0_cli_add_cb,
1659 custom_ext_0_cli_parse_cb, NULL)
1660 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
1661 custom_ext_1_cli_add_cb,
1663 custom_ext_1_cli_parse_cb, NULL)
1664 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
1665 custom_ext_2_cli_add_cb,
1667 custom_ext_2_cli_parse_cb, NULL)
1668 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
1669 custom_ext_3_cli_add_cb,
1671 custom_ext_3_cli_parse_cb, NULL)
1672 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
1673 custom_ext_0_srv_add_cb,
1675 custom_ext_0_srv_parse_cb, NULL)
1676 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
1677 custom_ext_0_srv_add_cb,
1679 custom_ext_0_srv_parse_cb, NULL)
1680 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
1681 custom_ext_1_srv_add_cb,
1683 custom_ext_1_srv_parse_cb, NULL)
1684 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
1685 custom_ext_1_srv_add_cb,
1687 custom_ext_1_srv_parse_cb, NULL)
1688 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
1689 custom_ext_2_srv_add_cb,
1691 custom_ext_2_srv_parse_cb, NULL)
1692 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
1693 custom_ext_2_srv_add_cb,
1695 custom_ext_2_srv_parse_cb, NULL)
1696 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
1697 custom_ext_3_srv_add_cb,
1699 custom_ext_3_srv_parse_cb, NULL)
1700 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
1701 custom_ext_3_srv_add_cb,
1703 custom_ext_3_srv_parse_cb, NULL)) {
1704 BIO_printf(bio_err, "Error setting custom extensions\n");
1710 SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
1712 SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
1716 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
1719 BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
1722 /* Returns 0 on success!! */
1723 if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
1724 BIO_printf(bio_err, "Error setting ALPN\n");
1731 if (server_sess_in != NULL) {
1732 server_sess = read_session(server_sess_in);
1733 if (server_sess == NULL)
1736 if (client_sess_in != NULL) {
1737 client_sess = read_session(client_sess_in);
1738 if (client_sess == NULL)
1742 if (server_sess_out != NULL || server_sess_in != NULL) {
1746 /* Use a fixed key so that we can decrypt the ticket. */
1747 size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
1748 keys = OPENSSL_zalloc(size);
1751 SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
1755 if (sn_server1 != NULL || sn_server2 != NULL)
1756 SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
1758 c_ssl = SSL_new(c_ctx);
1759 s_ssl = SSL_new(s_ctx);
1762 SSL_set_tlsext_host_name(c_ssl, sn_client);
1764 SSL_set_options(c_ssl, SSL_OP_ENABLE_KTLS);
1766 SSL_set_options(s_ssl, SSL_OP_ENABLE_KTLS);
1768 if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1770 if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1772 if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1774 if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1778 if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
1779 BIO_printf(bio_err, "Can't add server session\n");
1780 ERR_print_errors(bio_err);
1785 BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
1786 for (i = 0; i < number; i++) {
1788 if (!SSL_set_session(c_ssl, NULL)) {
1789 BIO_printf(bio_err, "Failed to set session\n");
1793 if (client_sess_in != NULL) {
1794 if (SSL_set_session(c_ssl, client_sess) == 0) {
1795 BIO_printf(bio_err, "Can't set client session\n");
1796 ERR_print_errors(bio_err);
1802 ret = doit(s_ssl, c_ssl, bytes);
1805 ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
1807 #ifndef OPENSSL_NO_SOCK
1809 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
1810 bytes, &s_time, &c_time);
1813 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
1814 bytes, &s_time, &c_time);
1823 if (ret != EXIT_SUCCESS) break;
1826 if (should_negotiate && ret == EXIT_SUCCESS &&
1827 strcmp(should_negotiate, "fail-server") != 0 &&
1828 strcmp(should_negotiate, "fail-client") != 0) {
1829 int version = protocol_from_string(should_negotiate);
1831 BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
1835 if (SSL_version(c_ssl) != version) {
1836 BIO_printf(bio_err, "Unexpected version negotiated. "
1837 "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
1843 if (should_reuse != -1) {
1844 if (SSL_session_reused(s_ssl) != should_reuse ||
1845 SSL_session_reused(c_ssl) != should_reuse) {
1846 BIO_printf(bio_err, "Unexpected session reuse state. "
1847 "Expected: %d, server: %d, client: %d\n", should_reuse,
1848 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
1854 if (server_sess_out != NULL) {
1855 if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
1860 if (client_sess_out != NULL) {
1861 if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
1868 print_details(c_ssl, "");
1871 #ifdef CLOCKS_PER_SEC
1873 * "To determine the time in seconds, the value returned by the clock
1874 * function should be divided by the value of the macro
1875 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1877 BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
1878 "Approximate total client time: %6.2f s\n",
1879 (double)s_time / CLOCKS_PER_SEC,
1880 (double)c_time / CLOCKS_PER_SEC);
1882 BIO_printf(bio_stdout,
1883 "Approximate total server time: %6.2f units\n"
1884 "Approximate total client time: %6.2f units\n",
1885 (double)s_time, (double)c_time);
1894 SSL_CTX_free(s_ctx);
1895 SSL_CTX_free(s_ctx2);
1896 SSL_CTX_free(c_ctx);
1897 SSL_CONF_CTX_free(s_cctx);
1898 SSL_CONF_CTX_free(s_cctx2);
1899 SSL_CONF_CTX_free(c_cctx);
1900 sk_OPENSSL_STRING_free(conf_args);
1902 BIO_free(bio_stdout);
1904 SSL_SESSION_free(server_sess);
1905 SSL_SESSION_free(client_sess);
1907 OSSL_PROVIDER_unload(defctxnull);
1908 OSSL_PROVIDER_unload(thisprov);
1909 OSSL_LIB_CTX_free(libctx);
1915 #ifndef OPENSSL_NO_SOCK
1916 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
1917 clock_t *s_time, clock_t *c_time)
1919 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
1920 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
1921 BIO *acpt = NULL, *server = NULL, *client = NULL;
1923 int ret = EXIT_FAILURE;
1924 int err_in_client = 0;
1925 int err_in_server = 0;
1927 acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
1931 BIO_set_accept_ip_family(acpt, family);
1932 BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
1933 if (BIO_do_accept(acpt) <= 0)
1936 BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
1938 client = BIO_new_connect(addr_str);
1941 BIO_set_conn_ip_family(client, family);
1943 if (BIO_set_nbio(client, 1) <= 0)
1945 if (BIO_set_nbio(acpt, 1) <= 0)
1949 int st_connect = 0, st_accept = 0;
1951 while (!st_connect || !st_accept) {
1953 if (BIO_do_connect(client) <= 0) {
1954 if (!BIO_should_retry(client))
1961 if (BIO_do_accept(acpt) <= 0) {
1962 if (!BIO_should_retry(acpt))
1970 /* We're not interested in accepting further connects */
1971 server = BIO_pop(acpt);
1975 s_ssl_bio = BIO_new(BIO_f_ssl());
1979 c_ssl_bio = BIO_new(BIO_f_ssl());
1983 SSL_set_connect_state(c_ssl);
1984 SSL_set_bio(c_ssl, client, client);
1985 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
1987 SSL_set_accept_state(s_ssl);
1988 SSL_set_bio(s_ssl, server, server);
1989 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
1993 * c_ssl_bio: SSL filter BIO
1995 * client: I/O for SSL library
1998 * server: I/O for SSL library
2000 * s_ssl_bio: SSL filter BIO
2004 * We have non-blocking behaviour throughout this test program, but
2005 * can be sure that there is *some* progress in each iteration; so we
2006 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2007 * we just try everything in each iteration
2013 char cbuf[1024 * 8];
2015 clock_t c_clock = clock();
2017 memset(cbuf, 0, sizeof(cbuf));
2020 if (SSL_in_init(c_ssl))
2021 printf("client waiting in SSL_connect - %s\n",
2022 SSL_state_string_long(c_ssl));
2025 /* Write to server. */
2027 if (cw_num > (long)sizeof(cbuf))
2031 r = BIO_write(c_ssl_bio, cbuf, i);
2033 if (!BIO_should_retry(c_ssl_bio)) {
2034 fprintf(stderr, "ERROR in CLIENT (write)\n");
2039 * BIO_should_retry(...) can just be ignored here. The
2040 * library expects us to call BIO_write with the same
2041 * arguments again, and that's what we will do in the
2044 } else if (r == 0) {
2045 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2049 printf("client wrote %d\n", r);
2055 /* Read from server. */
2057 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2059 if (!BIO_should_retry(c_ssl_bio)) {
2060 fprintf(stderr, "ERROR in CLIENT (read)\n");
2065 * Again, "BIO_should_retry" can be ignored.
2067 } else if (r == 0) {
2068 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2072 printf("client read %d\n", r);
2078 * c_time and s_time increments will typically be very small
2079 * (depending on machine speed and clock tick intervals), but
2080 * sampling over a large number of connections should result in
2081 * fairly accurate figures. We cannot guarantee a lot, however
2082 * -- if each connection lasts for exactly one clock tick, it
2083 * will be counted only for the client or only for the server or
2086 *c_time += (clock() - c_clock);
2092 char sbuf[1024 * 8];
2094 clock_t s_clock = clock();
2096 memset(sbuf, 0, sizeof(sbuf));
2099 if (SSL_in_init(s_ssl))
2100 printf("server waiting in SSL_accept - %s\n",
2101 SSL_state_string_long(s_ssl));
2104 /* Write to client. */
2106 if (sw_num > (long)sizeof(sbuf))
2110 r = BIO_write(s_ssl_bio, sbuf, i);
2112 if (!BIO_should_retry(s_ssl_bio)) {
2113 fprintf(stderr, "ERROR in SERVER (write)\n");
2117 /* Ignore "BIO_should_retry". */
2118 } else if (r == 0) {
2119 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2123 printf("server wrote %d\n", r);
2129 /* Read from client. */
2131 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2133 if (!BIO_should_retry(s_ssl_bio)) {
2134 fprintf(stderr, "ERROR in SERVER (read)\n");
2139 } else if (r == 0) {
2140 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2144 printf("server read %d\n", r);
2149 *s_time += (clock() - s_clock);
2152 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2155 print_details(c_ssl, "DONE via TCP connect: ");
2157 if (BIO_get_ktls_send(SSL_get_wbio(s_ssl))
2158 && BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
2159 BIO_printf(bio_stdout, "Server using Kernel TLS in both directions\n");
2160 else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl)))
2161 BIO_printf(bio_stdout, "Server using Kernel TLS for sending\n");
2162 else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
2163 BIO_printf(bio_stdout, "Server using Kernel TLS for receiving\n");
2165 if (BIO_get_ktls_send(SSL_get_wbio(c_ssl))
2166 && BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
2167 BIO_printf(bio_stdout, "Client using Kernel TLS in both directions\n");
2168 else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl)))
2169 BIO_printf(bio_stdout, "Client using Kernel TLS for sending\n");
2170 else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
2171 BIO_printf(bio_stdout, "Client using Kernel TLS for receiving\n");
2173 # ifndef OPENSSL_NO_NEXTPROTONEG
2174 if (verify_npn(c_ssl, s_ssl) < 0)
2177 if (verify_serverinfo() < 0) {
2178 fprintf(stderr, "Server info verify error\n");
2181 if (verify_alpn(c_ssl, s_ssl) < 0
2182 || verify_servername(c_ssl, s_ssl) < 0)
2185 if (custom_ext_error) {
2186 fprintf(stderr, "Custom extension error\n");
2190 # ifndef OPENSSL_NO_NEXTPROTONEG
2196 ERR_print_errors(bio_err);
2201 BIO_free(s_ssl_bio);
2202 BIO_free(c_ssl_bio);
2204 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2205 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2206 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2207 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2213 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
2214 clock_t *s_time, clock_t *c_time)
2216 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
2217 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
2218 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
2219 int ret = EXIT_FAILURE;
2220 int err_in_client = 0;
2221 int err_in_server = 0;
2223 size_t bufsiz = 256; /* small buffer for testing */
2225 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
2227 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
2230 s_ssl_bio = BIO_new(BIO_f_ssl());
2234 c_ssl_bio = BIO_new(BIO_f_ssl());
2238 SSL_set_connect_state(c_ssl);
2239 SSL_set_bio(c_ssl, client, client);
2240 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
2242 SSL_set_accept_state(s_ssl);
2243 SSL_set_bio(s_ssl, server, server);
2244 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
2248 * c_ssl_bio: SSL filter BIO
2250 * client: pseudo-I/O for SSL library
2252 * client_io: client's SSL communication; usually to be
2253 * relayed over some I/O facility, but in this
2254 * test program, we're the server, too:
2256 * server_io: server's SSL communication
2258 * server: pseudo-I/O for SSL library
2260 * s_ssl_bio: SSL filter BIO
2262 * The client and the server each employ a "BIO pair":
2263 * client + client_io, server + server_io.
2264 * BIO pairs are symmetric. A BIO pair behaves similar
2265 * to a non-blocking socketpair (but both endpoints must
2266 * be handled by the same thread).
2267 * [Here we could connect client and server to the ends
2268 * of a single BIO pair, but then this code would be less
2269 * suitable as an example for BIO pairs in general.]
2271 * Useful functions for querying the state of BIO pair endpoints:
2273 * BIO_ctrl_pending(bio) number of bytes we can read now
2274 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
2275 * other side's read attempt
2276 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2278 * ..._read_request is never more than ..._write_guarantee;
2279 * it depends on the application which one you should use.
2283 * We have non-blocking behaviour throughout this test program, but
2284 * can be sure that there is *some* progress in each iteration; so we
2285 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2286 * we just try everything in each iteration
2292 char cbuf[1024 * 8];
2294 clock_t c_clock = clock();
2296 memset(cbuf, 0, sizeof(cbuf));
2299 if (SSL_in_init(c_ssl))
2300 printf("client waiting in SSL_connect - %s\n",
2301 SSL_state_string_long(c_ssl));
2304 /* Write to server. */
2306 if (cw_num > (long)sizeof(cbuf))
2310 r = BIO_write(c_ssl_bio, cbuf, i);
2312 if (!BIO_should_retry(c_ssl_bio)) {
2313 fprintf(stderr, "ERROR in CLIENT\n");
2318 * BIO_should_retry(...) can just be ignored here. The
2319 * library expects us to call BIO_write with the same
2320 * arguments again, and that's what we will do in the
2323 } else if (r == 0) {
2324 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2328 printf("client wrote %d\n", r);
2334 /* Read from server. */
2336 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2338 if (!BIO_should_retry(c_ssl_bio)) {
2339 fprintf(stderr, "ERROR in CLIENT\n");
2344 * Again, "BIO_should_retry" can be ignored.
2346 } else if (r == 0) {
2347 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2351 printf("client read %d\n", r);
2357 * c_time and s_time increments will typically be very small
2358 * (depending on machine speed and clock tick intervals), but
2359 * sampling over a large number of connections should result in
2360 * fairly accurate figures. We cannot guarantee a lot, however
2361 * -- if each connection lasts for exactly one clock tick, it
2362 * will be counted only for the client or only for the server or
2365 *c_time += (clock() - c_clock);
2371 char sbuf[1024 * 8];
2373 clock_t s_clock = clock();
2375 memset(sbuf, 0, sizeof(sbuf));
2378 if (SSL_in_init(s_ssl))
2379 printf("server waiting in SSL_accept - %s\n",
2380 SSL_state_string_long(s_ssl));
2383 /* Write to client. */
2385 if (sw_num > (long)sizeof(sbuf))
2389 r = BIO_write(s_ssl_bio, sbuf, i);
2391 if (!BIO_should_retry(s_ssl_bio)) {
2392 fprintf(stderr, "ERROR in SERVER\n");
2396 /* Ignore "BIO_should_retry". */
2397 } else if (r == 0) {
2398 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2402 printf("server wrote %d\n", r);
2408 /* Read from client. */
2410 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2412 if (!BIO_should_retry(s_ssl_bio)) {
2413 fprintf(stderr, "ERROR in SERVER\n");
2418 } else if (r == 0) {
2419 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2423 printf("server read %d\n", r);
2428 *s_time += (clock() - s_clock);
2432 /* "I/O" BETWEEN CLIENT AND SERVER. */
2435 BIO *io1 = server_io, *io2 = client_io;
2437 * we use the non-copying interface for io1 and the standard
2438 * BIO_write/BIO_read interface for io2
2441 static int prev_progress = 1;
2449 r1 = BIO_ctrl_pending(io1);
2450 r2 = BIO_ctrl_get_write_guarantee(io2);
2458 if (INT_MAX < num) /* yeah, right */
2461 r = BIO_nread(io1, &dataptr, (int)num);
2463 assert(r <= (int)num);
2465 * possibly r < num (non-contiguous data)
2468 r = BIO_write(io2, dataptr, (int)num);
2469 if (r != (int)num) { /* can't happen */
2470 fprintf(stderr, "ERROR: BIO_write could not write "
2471 "BIO_ctrl_get_write_guarantee() bytes");
2477 printf((io1 == client_io) ?
2478 "C->S relaying: %d bytes\n" :
2479 "S->C relaying: %d bytes\n", (int)num);
2489 r1 = BIO_ctrl_pending(io2);
2490 r2 = BIO_ctrl_get_read_request(io1);
2492 * here we could use ..._get_write_guarantee instead of
2493 * ..._get_read_request, but by using the latter we test
2494 * restartability of the SSL implementation more thoroughly
2506 --num; /* test restartability even more thoroughly */
2508 r = BIO_nwrite0(io1, &dataptr);
2512 r = BIO_read(io2, dataptr, (int)num);
2513 if (r != (int)num) { /* can't happen */
2514 fprintf(stderr, "ERROR: BIO_read could not read "
2515 "BIO_ctrl_pending() bytes");
2519 r = BIO_nwrite(io1, &dataptr, (int)num);
2520 if (r != (int)num) { /* can't happen */
2521 fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
2522 "BIO_nwrite0() bytes");
2527 printf((io2 == client_io) ?
2528 "C->S relaying: %d bytes\n" :
2529 "S->C relaying: %d bytes\n", (int)num);
2531 } /* no loop, BIO_ctrl_get_read_request now
2532 * returns 0 anyway */
2534 if (!progress && !prev_progress)
2535 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
2536 fprintf(stderr, "ERROR: got stuck\n");
2537 fprintf(stderr, " ERROR.\n");
2540 prev_progress = progress;
2543 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2546 print_details(c_ssl, "DONE via BIO pair: ");
2547 #ifndef OPENSSL_NO_NEXTPROTONEG
2548 if (verify_npn(c_ssl, s_ssl) < 0)
2551 if (verify_serverinfo() < 0) {
2552 fprintf(stderr, "Server info verify error\n");
2555 if (verify_alpn(c_ssl, s_ssl) < 0
2556 || verify_servername(c_ssl, s_ssl) < 0)
2559 if (custom_ext_error) {
2560 fprintf(stderr, "Custom extension error\n");
2564 #ifndef OPENSSL_NO_NEXTPROTONEG
2570 ERR_print_errors(bio_err);
2573 BIO_free(server_io);
2575 BIO_free(client_io);
2576 BIO_free(s_ssl_bio);
2577 BIO_free(c_ssl_bio);
2579 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2580 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2581 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2582 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2592 int doit(SSL *s_ssl, SSL *c_ssl, long count)
2594 char *cbuf = NULL, *sbuf = NULL;
2596 long cw_num = count, cr_num = count;
2597 long sw_num = count, sr_num = count;
2598 int ret = EXIT_FAILURE;
2603 int c_r, c_w, s_r, s_w;
2606 int c_write, s_write;
2607 int do_server = 0, do_client = 0;
2608 int max_frag = 5 * 1024;
2609 int err_in_client = 0;
2610 int err_in_server = 0;
2612 bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
2614 if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2616 if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2619 c_to_s = BIO_new(BIO_s_mem());
2620 s_to_c = BIO_new(BIO_s_mem());
2621 if ((s_to_c == NULL) || (c_to_s == NULL)) {
2622 ERR_print_errors(bio_err);
2626 c_bio = BIO_new(BIO_f_ssl());
2627 s_bio = BIO_new(BIO_f_ssl());
2628 if ((c_bio == NULL) || (s_bio == NULL)) {
2629 ERR_print_errors(bio_err);
2633 SSL_set_connect_state(c_ssl);
2634 SSL_set_bio(c_ssl, s_to_c, c_to_s);
2635 SSL_set_max_send_fragment(c_ssl, max_frag);
2636 BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
2639 * We've just given our ref to these BIOs to c_ssl. We need another one to
2642 if (!BIO_up_ref(c_to_s)) {
2643 /* c_to_s and s_to_c will get freed when we free c_ssl */
2648 if (!BIO_up_ref(s_to_c)) {
2649 /* s_to_c will get freed when we free c_ssl */
2654 SSL_set_accept_state(s_ssl);
2655 SSL_set_bio(s_ssl, c_to_s, s_to_c);
2657 /* We've used up all our refs to these now */
2661 SSL_set_max_send_fragment(s_ssl, max_frag);
2662 BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
2668 c_write = 1, s_write = 0;
2670 /* We can always do writes */
2675 i = (int)BIO_pending(s_bio);
2676 if ((i && s_r) || s_w)
2679 i = (int)BIO_pending(c_bio);
2680 if ((i && c_r) || c_w)
2683 if (do_server && debug) {
2684 if (SSL_in_init(s_ssl))
2685 printf("server waiting in SSL_accept - %s\n",
2686 SSL_state_string_long(s_ssl));
2689 if (do_client && debug) {
2690 if (SSL_in_init(c_ssl))
2691 printf("client waiting in SSL_connect - %s\n",
2692 SSL_state_string_long(c_ssl));
2695 if (!do_client && !do_server) {
2696 fprintf(stdout, "ERROR IN STARTUP\n");
2697 ERR_print_errors(bio_err);
2700 if (do_client && !(done & C_DONE)) {
2702 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
2703 i = BIO_write(c_bio, cbuf, j);
2707 if (BIO_should_retry(c_bio)) {
2708 if (BIO_should_read(c_bio))
2710 if (BIO_should_write(c_bio))
2713 fprintf(stderr, "ERROR in CLIENT\n");
2715 ERR_print_errors(bio_err);
2718 } else if (i == 0) {
2719 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2723 printf("client wrote %d\n", i);
2728 if (max_frag > 1029)
2729 SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
2732 i = BIO_read(c_bio, cbuf, bufsiz);
2736 if (BIO_should_retry(c_bio)) {
2737 if (BIO_should_read(c_bio))
2739 if (BIO_should_write(c_bio))
2742 fprintf(stderr, "ERROR in CLIENT\n");
2744 ERR_print_errors(bio_err);
2747 } else if (i == 0) {
2748 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2752 printf("client read %d\n", i);
2761 done = S_DONE | C_DONE;
2767 if (do_server && !(done & S_DONE)) {
2769 i = BIO_read(s_bio, sbuf, bufsiz);
2773 if (BIO_should_retry(s_bio)) {
2774 if (BIO_should_read(s_bio))
2776 if (BIO_should_write(s_bio))
2779 fprintf(stderr, "ERROR in SERVER\n");
2781 ERR_print_errors(bio_err);
2784 } else if (i == 0) {
2785 ERR_print_errors(bio_err);
2787 "SSL SERVER STARTUP FAILED in SSL_read\n");
2791 printf("server read %d\n", i);
2804 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
2805 i = BIO_write(s_bio, sbuf, j);
2809 if (BIO_should_retry(s_bio)) {
2810 if (BIO_should_read(s_bio))
2812 if (BIO_should_write(s_bio))
2815 fprintf(stderr, "ERROR in SERVER\n");
2817 ERR_print_errors(bio_err);
2820 } else if (i == 0) {
2821 ERR_print_errors(bio_err);
2823 "SSL SERVER STARTUP FAILED in SSL_write\n");
2827 printf("server wrote %d\n", i);
2833 if (max_frag > 1029)
2834 SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
2839 if ((done & S_DONE) && (done & C_DONE))
2844 print_details(c_ssl, "DONE: ");
2845 #ifndef OPENSSL_NO_NEXTPROTONEG
2846 if (verify_npn(c_ssl, s_ssl) < 0)
2849 if (verify_serverinfo() < 0) {
2850 fprintf(stderr, "Server info verify error\n");
2853 if (custom_ext_error) {
2854 fprintf(stderr, "Custom extension error\n");
2861 BIO_free_all(c_bio);
2862 BIO_free_all(s_bio);
2866 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2867 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2868 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2869 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2874 static int verify_callback(int ok, X509_STORE_CTX *ctx)
2878 s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
2882 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
2884 fprintf(stderr, "depth=%d error=%d %s\n",
2885 X509_STORE_CTX_get_error_depth(ctx),
2886 X509_STORE_CTX_get_error(ctx), buf);
2891 int i = X509_STORE_CTX_get_error(ctx);
2895 fprintf(stderr, "Error string: %s\n",
2896 X509_verify_cert_error_string(i));
2898 case X509_V_ERR_CERT_NOT_YET_VALID:
2899 case X509_V_ERR_CERT_HAS_EXPIRED:
2900 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2909 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
2912 struct app_verify_arg *cb_arg = arg;
2914 if (cb_arg->app_verify) {
2915 char *s = NULL, buf[256];
2916 X509 *c = X509_STORE_CTX_get0_cert(ctx);
2918 printf("In app_verify_callback, allowing cert. ");
2919 printf("Arg is: %s\n", cb_arg->string);
2920 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2921 (void *)ctx, (void *)c);
2923 s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
2925 printf("cert depth=%d %s\n",
2926 X509_STORE_CTX_get_error_depth(ctx), buf);
2931 ok = X509_verify_cert(ctx);
2936 #ifndef OPENSSL_NO_PSK
2937 /* convert the PSK key (psk_key) in ascii to binary (psk) */
2938 static int psk_key2bn(const char *pskkey, unsigned char *psk,
2939 unsigned int max_psk_len)
2944 ret = BN_hex2bn(&bn, pskkey);
2946 BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
2951 if (BN_num_bytes(bn) > (int)max_psk_len) {
2953 "psk buffer of callback is too small (%d) for key (%d)\n",
2954 max_psk_len, BN_num_bytes(bn));
2958 ret = BN_bn2bin(bn, psk);
2963 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
2965 unsigned int max_identity_len,
2967 unsigned int max_psk_len)
2970 unsigned int psk_len = 0;
2972 ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
2976 fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
2978 ret = psk_key2bn(psk_key, psk, max_psk_len);
2986 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
2988 unsigned int max_psk_len)
2990 unsigned int psk_len = 0;
2992 if (strcmp(identity, "Client_identity") != 0) {
2993 BIO_printf(bio_err, "server: PSK error: client identity not found\n");
2996 psk_len = psk_key2bn(psk_key, psk, max_psk_len);