2 * Copyright 1995-2020 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
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 #ifndef OPENSSL_NO_RSA
50 # include <openssl/rsa.h>
52 #ifndef OPENSSL_NO_DSA
53 # include <openssl/dsa.h>
55 #include <openssl/bn.h>
57 # include <openssl/ct.h>
59 #include <openssl/provider.h>
63 * Or gethostname won't be declared properly
64 * on Compaq platforms (at least with DEC C).
65 * Do not try to put it earlier, or IPv6 includes
68 #define _XOPEN_SOURCE_EXTENDED 1
70 #ifdef OPENSSL_SYS_WINDOWS
76 #include "helpers/predefined_dhparams.h"
78 static SSL_CTX *s_ctx = NULL;
79 static SSL_CTX *s_ctx2 = NULL;
82 * There is really no standard for this, so let's assign something
87 static int verify_callback(int ok, X509_STORE_CTX *ctx);
88 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
89 #define APP_CALLBACK_STRING "Test Callback Argument"
90 struct app_verify_arg {
95 static char *psk_key = NULL; /* by default PSK is not used */
96 #ifndef OPENSSL_NO_PSK
97 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
99 unsigned int max_identity_len,
101 unsigned int max_psk_len);
102 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
104 unsigned int max_psk_len);
107 static BIO *bio_stdout = NULL;
109 #ifndef OPENSSL_NO_NEXTPROTONEG
110 /* Note that this code assumes that this is only a one element list: */
111 static const char NEXT_PROTO_STRING[] = "\x09testproto";
112 static int npn_client = 0;
113 static int npn_server = 0;
114 static int npn_server_reject = 0;
116 static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen,
117 const unsigned char *in, unsigned int inlen,
121 * This callback only returns the protocol string, rather than a length
122 * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
123 * and remove the first byte to chop off the length prefix.
125 *out = (unsigned char *)NEXT_PROTO_STRING + 1;
126 *outlen = sizeof(NEXT_PROTO_STRING) - 2;
127 return SSL_TLSEXT_ERR_OK;
130 static int cb_server_npn(SSL *s, const unsigned char **data,
131 unsigned int *len, void *arg)
133 *data = (const unsigned char *)NEXT_PROTO_STRING;
134 *len = sizeof(NEXT_PROTO_STRING) - 1;
135 return SSL_TLSEXT_ERR_OK;
138 static int cb_server_rejects_npn(SSL *s, const unsigned char **data,
139 unsigned int *len, void *arg)
141 return SSL_TLSEXT_ERR_NOACK;
144 static int verify_npn(SSL *client, SSL *server)
146 const unsigned char *client_s;
148 const unsigned char *server_s;
151 SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
152 SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
155 BIO_printf(bio_stdout, "Client NPN: ");
156 BIO_write(bio_stdout, client_s, client_len);
157 BIO_printf(bio_stdout, "\n");
161 BIO_printf(bio_stdout, "Server NPN: ");
162 BIO_write(bio_stdout, server_s, server_len);
163 BIO_printf(bio_stdout, "\n");
167 * If an NPN string was returned, it must be the protocol that we
168 * expected to negotiate.
170 if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 ||
171 memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
173 if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 ||
174 memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
177 if (!npn_client && client_len)
179 if (!npn_server && server_len)
181 if (npn_server_reject && server_len)
183 if (npn_client && npn_server && (!client_len || !server_len))
190 static const char *alpn_client;
191 static char *alpn_server;
192 static char *alpn_server2;
193 static const char *alpn_expected;
194 static unsigned char *alpn_selected;
195 static const char *server_min_proto;
196 static const char *server_max_proto;
197 static const char *client_min_proto;
198 static const char *client_max_proto;
199 static const char *should_negotiate;
200 static const char *sn_client;
201 static const char *sn_server1;
202 static const char *sn_server2;
203 static int sn_expect = 0;
204 static const char *server_sess_out;
205 static const char *server_sess_in;
206 static const char *client_sess_out;
207 static const char *client_sess_in;
208 static SSL_SESSION *server_sess;
209 static SSL_SESSION *client_sess;
211 static int servername_cb(SSL *s, int *ad, void *arg)
213 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
214 if (sn_server2 == NULL) {
215 BIO_printf(bio_stdout, "Servername 2 is NULL\n");
216 return SSL_TLSEXT_ERR_NOACK;
220 if (s_ctx2 != NULL && sn_server2 != NULL &&
221 !strcasecmp(servername, sn_server2)) {
222 BIO_printf(bio_stdout, "Switching server context.\n");
223 SSL_set_SSL_CTX(s, s_ctx2);
226 return SSL_TLSEXT_ERR_OK;
228 static int verify_servername(SSL *client, SSL *server)
230 /* just need to see if sn_context is what we expect */
231 SSL_CTX* ctx = SSL_get_SSL_CTX(server);
234 if (sn_expect == 1 && ctx == s_ctx)
236 if (sn_expect == 2 && ctx == s_ctx2)
238 BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect);
240 BIO_printf(bio_stdout, "Servername: context is 2\n");
241 else if (ctx == s_ctx)
242 BIO_printf(bio_stdout, "Servername: context is 1\n");
244 BIO_printf(bio_stdout, "Servername: context is unknown\n");
250 * next_protos_parse parses a comma separated list of strings into a string
251 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
252 * outlen: (output) set to the length of the resulting buffer on success.
253 * in: a NUL terminated string like "abc,def,ghi"
255 * returns: a malloced buffer or NULL on failure.
257 static unsigned char *next_protos_parse(size_t *outlen,
268 out = OPENSSL_malloc(strlen(in) + 1);
272 for (i = 0; i <= len; ++i) {
273 if (i == len || in[i] == ',') {
274 if (i - start > 255) {
278 out[start] = (unsigned char)(i - start);
288 static int cb_server_alpn(SSL *s, const unsigned char **out,
289 unsigned char *outlen, const unsigned char *in,
290 unsigned int inlen, void *arg)
292 unsigned char *protos;
294 char* alpn_str = arg;
296 protos = next_protos_parse(&protos_len, alpn_str);
297 if (protos == NULL) {
298 fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
303 if (SSL_select_next_proto
304 ((unsigned char **)out, outlen, protos, protos_len, in,
305 inlen) != OPENSSL_NPN_NEGOTIATED) {
306 OPENSSL_free(protos);
307 return SSL_TLSEXT_ERR_NOACK;
311 * Make a copy of the selected protocol which will be freed in
314 alpn_selected = OPENSSL_malloc(*outlen);
315 memcpy(alpn_selected, *out, *outlen);
316 *out = alpn_selected;
318 OPENSSL_free(protos);
319 return SSL_TLSEXT_ERR_OK;
322 static int verify_alpn(SSL *client, SSL *server)
324 const unsigned char *client_proto, *server_proto;
325 unsigned int client_proto_len = 0, server_proto_len = 0;
326 SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
327 SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
329 OPENSSL_free(alpn_selected);
330 alpn_selected = NULL;
332 if (client_proto_len != server_proto_len) {
333 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
337 if (client_proto != NULL &&
338 memcmp(client_proto, server_proto, client_proto_len) != 0) {
339 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
343 if (client_proto_len > 0 && alpn_expected == NULL) {
344 BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
348 if (alpn_expected != NULL &&
349 (client_proto_len != strlen(alpn_expected) ||
350 memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
351 BIO_printf(bio_stdout,
352 "ALPN selected protocols not equal to expected protocol: %s\n",
360 BIO_printf(bio_stdout, "ALPN results: client: '");
361 BIO_write(bio_stdout, client_proto, client_proto_len);
362 BIO_printf(bio_stdout, "', server: '");
363 BIO_write(bio_stdout, server_proto, server_proto_len);
364 BIO_printf(bio_stdout, "'\n");
365 BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '",
367 if (SSL_get_SSL_CTX(server) == s_ctx2) {
368 BIO_printf(bio_stdout, "%s'\n",
371 BIO_printf(bio_stdout, "%s'\n",
378 * WARNING : below extension types are *NOT* IETF assigned, and could
379 * conflict if these types are reassigned and handled specially by OpenSSL
382 #define TACK_EXT_TYPE 62208
383 #define CUSTOM_EXT_TYPE_0 1000
384 #define CUSTOM_EXT_TYPE_1 1001
385 #define CUSTOM_EXT_TYPE_2 1002
386 #define CUSTOM_EXT_TYPE_3 1003
388 static const char custom_ext_cli_string[] = "abc";
389 static const char custom_ext_srv_string[] = "defg";
391 /* These set from cmdline */
392 static char *serverinfo_file = NULL;
393 static int serverinfo_sct = 0;
394 static int serverinfo_tack = 0;
396 /* These set based on extension callbacks */
397 static int serverinfo_sct_seen = 0;
398 static int serverinfo_tack_seen = 0;
399 static int serverinfo_other_seen = 0;
401 /* This set from cmdline */
402 static int custom_ext = 0;
404 /* This set based on extension callbacks */
405 static int custom_ext_error = 0;
407 static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
408 const unsigned char *in, size_t inlen,
411 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp)
412 serverinfo_sct_seen++;
413 else if (ext_type == TACK_EXT_TYPE)
414 serverinfo_tack_seen++;
416 serverinfo_other_seen++;
420 static int verify_serverinfo(void)
422 if (serverinfo_sct != serverinfo_sct_seen)
424 if (serverinfo_tack != serverinfo_tack_seen)
426 if (serverinfo_other_seen)
432 * Four test cases for custom extensions:
433 * 0 - no ClientHello extension or ServerHello response
434 * 1 - ClientHello with "abc", no response
435 * 2 - ClientHello with "abc", empty response
436 * 3 - ClientHello with "abc", "defg" response
439 static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
440 const unsigned char **out,
441 size_t *outlen, int *al, void *arg)
443 if (ext_type != CUSTOM_EXT_TYPE_0)
444 custom_ext_error = 1;
445 return 0; /* Don't send an extension */
448 static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
449 const unsigned char *in,
450 size_t inlen, int *al, void *arg)
455 static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
456 const unsigned char **out,
457 size_t *outlen, int *al, void *arg)
459 if (ext_type != CUSTOM_EXT_TYPE_1)
460 custom_ext_error = 1;
461 *out = (const unsigned char *)custom_ext_cli_string;
462 *outlen = strlen(custom_ext_cli_string);
463 return 1; /* Send "abc" */
466 static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
467 const unsigned char *in,
468 size_t inlen, int *al, void *arg)
473 static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
474 const unsigned char **out,
475 size_t *outlen, int *al, void *arg)
477 if (ext_type != CUSTOM_EXT_TYPE_2)
478 custom_ext_error = 1;
479 *out = (const unsigned char *)custom_ext_cli_string;
480 *outlen = strlen(custom_ext_cli_string);
481 return 1; /* Send "abc" */
484 static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
485 const unsigned char *in,
486 size_t inlen, int *al, void *arg)
488 if (ext_type != CUSTOM_EXT_TYPE_2)
489 custom_ext_error = 1;
491 custom_ext_error = 1; /* Should be empty response */
495 static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
496 const unsigned char **out,
497 size_t *outlen, int *al, void *arg)
499 if (ext_type != CUSTOM_EXT_TYPE_3)
500 custom_ext_error = 1;
501 *out = (const unsigned char *)custom_ext_cli_string;
502 *outlen = strlen(custom_ext_cli_string);
503 return 1; /* Send "abc" */
506 static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
507 const unsigned char *in,
508 size_t inlen, int *al, void *arg)
510 if (ext_type != CUSTOM_EXT_TYPE_3)
511 custom_ext_error = 1;
512 if (inlen != strlen(custom_ext_srv_string))
513 custom_ext_error = 1;
514 if (memcmp(custom_ext_srv_string, in, inlen) != 0)
515 custom_ext_error = 1; /* Check for "defg" */
520 * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
523 static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
524 const unsigned char *in,
525 size_t inlen, int *al, void *arg)
527 custom_ext_error = 1;
531 /* 'add' callbacks are only called if the 'parse' callback is called */
532 static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
533 const unsigned char **out,
534 size_t *outlen, int *al, void *arg)
536 /* Error: should not have been called */
537 custom_ext_error = 1;
538 return 0; /* Don't send an extension */
541 static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
542 const unsigned char *in,
543 size_t inlen, int *al, void *arg)
545 if (ext_type != CUSTOM_EXT_TYPE_1)
546 custom_ext_error = 1;
547 /* Check for "abc" */
548 if (inlen != strlen(custom_ext_cli_string))
549 custom_ext_error = 1;
550 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
551 custom_ext_error = 1;
555 static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
556 const unsigned char **out,
557 size_t *outlen, int *al, void *arg)
559 return 0; /* Don't send an extension */
562 static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
563 const unsigned char *in,
564 size_t inlen, int *al, void *arg)
566 if (ext_type != CUSTOM_EXT_TYPE_2)
567 custom_ext_error = 1;
568 /* Check for "abc" */
569 if (inlen != strlen(custom_ext_cli_string))
570 custom_ext_error = 1;
571 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
572 custom_ext_error = 1;
576 static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
577 const unsigned char **out,
578 size_t *outlen, int *al, void *arg)
582 return 1; /* Send empty extension */
585 static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
586 const unsigned char *in,
587 size_t inlen, int *al, void *arg)
589 if (ext_type != CUSTOM_EXT_TYPE_3)
590 custom_ext_error = 1;
591 /* Check for "abc" */
592 if (inlen != strlen(custom_ext_cli_string))
593 custom_ext_error = 1;
594 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
595 custom_ext_error = 1;
599 static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
600 const unsigned char **out,
601 size_t *outlen, int *al, void *arg)
603 *out = (const unsigned char *)custom_ext_srv_string;
604 *outlen = strlen(custom_ext_srv_string);
605 return 1; /* Send "defg" */
608 static char *cipher = NULL;
609 static char *ciphersuites = NULL;
610 static int verbose = 0;
611 static int debug = 0;
613 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
614 long bytes, clock_t *s_time, clock_t *c_time);
615 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
617 int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
619 static void sv_usage(void)
621 fprintf(stderr, "usage: ssltest [args ...]\n");
622 fprintf(stderr, "\n");
623 fprintf(stderr, " -server_auth - check server certificate\n");
624 fprintf(stderr, " -client_auth - do client authentication\n");
625 fprintf(stderr, " -v - more output\n");
626 fprintf(stderr, " -d - debug output\n");
627 fprintf(stderr, " -reuse - use session-id reuse\n");
628 fprintf(stderr, " -num <val> - number of connections to perform\n");
630 " -bytes <val> - number of bytes to swap between client/server\n");
631 #ifndef OPENSSL_NO_DH
633 " -dhe512 - use 512 bit key for DHE (to test failure)\n");
635 " -dhe1024 - use 1024 bit key (safe prime) for DHE (default, no-op)\n");
637 " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
639 fprintf(stderr, " -no_dhe - disable DHE\n");
640 #ifndef OPENSSL_NO_EC
641 fprintf(stderr, " -no_ecdhe - disable ECDHE\nTODO(openssl-team): no_ecdhe was broken by auto ecdh. Make this work again.\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_DTLS
653 fprintf(stderr, " -dtls - use DTLS\n");
654 #ifndef OPENSSL_NO_DTLS1
655 fprintf(stderr, " -dtls1 - use DTLSv1\n");
657 #ifndef OPENSSL_NO_DTLS1_2
658 fprintf(stderr, " -dtls12 - use DTLSv1.2\n");
661 fprintf(stderr, " -CApath arg - PEM format directory of CA's\n");
662 fprintf(stderr, " -CAfile arg - PEM format file of CA's\n");
663 fprintf(stderr, " -cert arg - Server certificate file\n");
665 " -key arg - Server key file (default: same as -cert)\n");
666 fprintf(stderr, " -c_cert arg - Client certificate file\n");
668 " -c_key arg - Client key file (default: same as -c_cert)\n");
669 fprintf(stderr, " -cipher arg - The TLSv1.2 and below cipher list\n");
670 fprintf(stderr, " -ciphersuites arg - The TLSv1.3 ciphersuites\n");
671 fprintf(stderr, " -bio_pair - Use BIO pairs\n");
672 fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n");
673 fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n");
674 fprintf(stderr, " -f - Test even cases that can't work\n");
676 " -time - measure processor time used by client and server\n");
677 fprintf(stderr, " -zlib - use zlib compression\n");
678 #ifndef OPENSSL_NO_NEXTPROTONEG
679 fprintf(stderr, " -npn_client - have client side offer NPN\n");
680 fprintf(stderr, " -npn_server - have server side offer NPN\n");
681 fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
683 fprintf(stderr, " -serverinfo_file file - have server use this file\n");
684 fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n");
686 " -serverinfo_tack - have client offer and expect TACK\n");
688 " -custom_ext - try various custom extension callbacks\n");
689 fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
690 fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
691 fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n");
692 fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
694 " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
695 fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
696 fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
697 fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
698 fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
699 fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
700 #ifndef OPENSSL_NO_CT
701 fprintf(stderr, " -noct - no certificate transparency\n");
702 fprintf(stderr, " -requestct - request certificate transparency\n");
703 fprintf(stderr, " -requirect - require certificate transparency\n");
705 fprintf(stderr, " -sn_client <string> - have client request this servername\n");
706 fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n");
707 fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n");
708 fprintf(stderr, " -sn_expect1 - expected server 1\n");
709 fprintf(stderr, " -sn_expect2 - expected server 2\n");
710 fprintf(stderr, " -server_sess_out <file> - Save the server session to a file\n");
711 fprintf(stderr, " -server_sess_in <file> - Read the server session from a file\n");
712 fprintf(stderr, " -client_sess_out <file> - Save the client session to a file\n");
713 fprintf(stderr, " -client_sess_in <file> - Read the client session from a file\n");
714 fprintf(stderr, " -should_reuse <number> - The expected state of reusing the session\n");
715 fprintf(stderr, " -no_ticket - do not issue TLS session ticket\n");
716 fprintf(stderr, " -provider <name> - Load the given provider into the library context\n");
717 fprintf(stderr, " -config <cnf> - Load the given config file into the library context\n");
720 static void print_key_details(BIO *out, EVP_PKEY *key)
722 int keyid = EVP_PKEY_id(key);
723 #ifndef OPENSSL_NO_EC
724 if (keyid == EVP_PKEY_EC) {
725 EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
728 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
730 cname = EC_curve_nid2nist(nid);
732 cname = OBJ_nid2sn(nid);
733 BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_bits(key), cname);
749 algname = OBJ_nid2sn(keyid);
752 BIO_printf(out, "%d bits %s", EVP_PKEY_bits(key), algname);
756 static void print_details(SSL *c_ssl, const char *prefix)
758 const SSL_CIPHER *ciph;
763 ciph = SSL_get_current_cipher(c_ssl);
764 BIO_printf(bio_stdout, "%s%s, cipher %s %s",
766 SSL_get_version(c_ssl),
767 SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
768 cert = SSL_get0_peer_certificate(c_ssl);
770 EVP_PKEY* pubkey = X509_get0_pubkey(cert);
772 if (pubkey != NULL) {
773 BIO_puts(bio_stdout, ", ");
774 print_key_details(bio_stdout, pubkey);
777 if (SSL_get_peer_tmp_key(c_ssl, &pkey)) {
778 BIO_puts(bio_stdout, ", temp key: ");
779 print_key_details(bio_stdout, pkey);
782 if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
783 BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
784 BIO_printf(bio_stdout, "\n");
788 * protocol_from_string - converts a protocol version string to a number
790 * Returns -1 on failure or the version on success
792 static int protocol_from_string(const char *value)
794 struct protocol_versions {
798 static const struct protocol_versions versions[] = {
799 {"ssl3", SSL3_VERSION},
800 {"tls1", TLS1_VERSION},
801 {"tls1.1", TLS1_1_VERSION},
802 {"tls1.2", TLS1_2_VERSION},
803 {"tls1.3", TLS1_3_VERSION},
804 {"dtls1", DTLS1_VERSION},
805 {"dtls1.2", DTLS1_2_VERSION}};
807 size_t n = OSSL_NELEM(versions);
809 for (i = 0; i < n; i++)
810 if (strcmp(versions[i].name, value) == 0)
811 return versions[i].version;
815 static SSL_SESSION *read_session(const char *filename)
818 BIO *f = BIO_new_file(filename, "r");
821 BIO_printf(bio_err, "Can't open session file %s\n", filename);
822 ERR_print_errors(bio_err);
825 sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL);
827 BIO_printf(bio_err, "Can't parse session file %s\n", filename);
828 ERR_print_errors(bio_err);
834 static int write_session(const char *filename, SSL_SESSION *sess)
836 BIO *f = BIO_new_file(filename, "w");
839 BIO_printf(bio_err, "No session information\n");
843 BIO_printf(bio_err, "Can't open session file %s\n", filename);
844 ERR_print_errors(bio_err);
847 PEM_write_bio_SSL_SESSION(f, sess);
853 * set_protocol_version - Sets protocol version minimum or maximum
855 * Returns 0 on failure and 1 on success
857 static int set_protocol_version(const char *version, SSL *ssl, int setting)
859 if (version != NULL) {
860 int ver = protocol_from_string(version);
862 BIO_printf(bio_err, "Error parsing: %s\n", version);
865 return SSL_ctrl(ssl, setting, ver, NULL);
870 int main(int argc, char *argv[])
872 const char *CApath = NULL, *CAfile = NULL;
874 enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM;
876 int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_2 = 0, ssl3 = 0;
877 int ret = EXIT_FAILURE;
879 int server_auth = 0, i;
880 struct app_verify_arg app_verify_arg =
881 { APP_CALLBACK_STRING, 0 };
882 SSL_CTX *c_ctx = NULL;
883 const SSL_METHOD *meth = NULL;
885 int number = 1, reuse = 0;
886 int should_reuse = -1;
889 #ifndef OPENSSL_NO_DH
891 int dhe512 = 0, dhe1024dsa = 0;
896 clock_t s_time = 0, c_time = 0;
897 #ifndef OPENSSL_NO_COMP
899 COMP_METHOD *cm = NULL;
900 STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
903 int min_version = 0, max_version = 0;
904 #ifndef OPENSSL_NO_CT
906 * Disable CT validation by default, because it will interfere with
907 * anything using custom extension handlers to deal with SCT extensions.
909 int ct_validation = 0;
911 SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL;
912 STACK_OF(OPENSSL_STRING) *conf_args = NULL;
913 char *arg = NULL, *argn = NULL;
914 const char *provider = NULL, *config = NULL;
915 OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL;
916 OSSL_LIB_CTX *libctx = NULL;
921 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
922 bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
924 s_cctx = SSL_CONF_CTX_new();
925 s_cctx2 = SSL_CONF_CTX_new();
926 c_cctx = SSL_CONF_CTX_new();
928 if (!s_cctx || !c_cctx || !s_cctx2) {
929 ERR_print_errors(bio_err);
933 SSL_CONF_CTX_set_flags(s_cctx,
934 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
935 SSL_CONF_FLAG_CERTIFICATE |
936 SSL_CONF_FLAG_REQUIRE_PRIVATE);
937 SSL_CONF_CTX_set_flags(s_cctx2,
938 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
939 SSL_CONF_FLAG_CERTIFICATE |
940 SSL_CONF_FLAG_REQUIRE_PRIVATE);
941 if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
942 ERR_print_errors(bio_err);
945 if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) {
946 ERR_print_errors(bio_err);
950 SSL_CONF_CTX_set_flags(c_cctx,
951 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT |
952 SSL_CONF_FLAG_CERTIFICATE |
953 SSL_CONF_FLAG_REQUIRE_PRIVATE);
954 if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
955 ERR_print_errors(bio_err);
963 if (strcmp(*argv, "-F") == 0) {
965 "not compiled with FIPS support, so exiting without running.\n");
967 } else if (strcmp(*argv, "-server_auth") == 0)
969 else if (strcmp(*argv, "-client_auth") == 0)
971 else if (strcmp(*argv, "-v") == 0)
973 else if (strcmp(*argv, "-d") == 0)
975 else if (strcmp(*argv, "-reuse") == 0)
977 else if (strcmp(*argv, "-no_dhe") == 0)
979 /* unused in this case */;
982 else if (strcmp(*argv, "-dhe512") == 0)
984 else if (strcmp(*argv, "-dhe1024dsa") == 0)
987 else if (strcmp(*argv, "-no_ecdhe") == 0)
989 else if (strcmp(*argv, "-psk") == 0) {
993 #ifndef OPENSSL_NO_PSK
994 if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) {
995 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
1002 else if (strcmp(*argv, "-tls1_2") == 0) {
1004 } else if (strcmp(*argv, "-tls1") == 0) {
1006 } else if (strcmp(*argv, "-ssl3") == 0) {
1008 } else if (strcmp(*argv, "-dtls1") == 0) {
1010 } else if (strcmp(*argv, "-dtls12") == 0) {
1012 } else if (strcmp(*argv, "-dtls") == 0) {
1014 } else if (strncmp(*argv, "-num", 4) == 0) {
1017 number = atoi(*(++argv));
1020 } else if (strcmp(*argv, "-bytes") == 0) {
1023 bytes = atol(*(++argv));
1026 i = strlen(argv[0]);
1027 if (argv[0][i - 1] == 'k')
1029 if (argv[0][i - 1] == 'm')
1030 bytes *= 1024L * 1024L;
1031 } else if (strcmp(*argv, "-cipher") == 0) {
1035 } else if (strcmp(*argv, "-ciphersuites") == 0) {
1038 ciphersuites = *(++argv);
1039 } else if (strcmp(*argv, "-CApath") == 0) {
1043 } else if (strcmp(*argv, "-CAfile") == 0) {
1047 } else if (strcmp(*argv, "-bio_pair") == 0) {
1048 bio_type = BIO_PAIR;
1050 #ifndef OPENSSL_NO_SOCK
1051 else if (strcmp(*argv, "-ipv4") == 0) {
1052 bio_type = BIO_IPV4;
1053 } else if (strcmp(*argv, "-ipv6") == 0) {
1054 bio_type = BIO_IPV6;
1057 else if (strcmp(*argv, "-f") == 0) {
1059 } else if (strcmp(*argv, "-time") == 0) {
1062 #ifndef OPENSSL_NO_CT
1063 else if (strcmp(*argv, "-noct") == 0) {
1066 else if (strcmp(*argv, "-ct") == 0) {
1070 #ifndef OPENSSL_NO_COMP
1071 else if (strcmp(*argv, "-zlib") == 0) {
1075 else if (strcmp(*argv, "-app_verify") == 0) {
1076 app_verify_arg.app_verify = 1;
1078 #ifndef OPENSSL_NO_NEXTPROTONEG
1079 else if (strcmp(*argv, "-npn_client") == 0) {
1081 } else if (strcmp(*argv, "-npn_server") == 0) {
1083 } else if (strcmp(*argv, "-npn_server_reject") == 0) {
1084 npn_server_reject = 1;
1087 else if (strcmp(*argv, "-serverinfo_sct") == 0) {
1089 } else if (strcmp(*argv, "-serverinfo_tack") == 0) {
1090 serverinfo_tack = 1;
1091 } else if (strcmp(*argv, "-serverinfo_file") == 0) {
1094 serverinfo_file = *(++argv);
1095 } else if (strcmp(*argv, "-custom_ext") == 0) {
1097 } else if (strcmp(*argv, "-alpn_client") == 0) {
1100 alpn_client = *(++argv);
1101 } else if (strcmp(*argv, "-alpn_server") == 0 ||
1102 strcmp(*argv, "-alpn_server1") == 0) {
1105 alpn_server = *(++argv);
1106 } else if (strcmp(*argv, "-alpn_server2") == 0) {
1109 alpn_server2 = *(++argv);
1110 } else if (strcmp(*argv, "-alpn_expected") == 0) {
1113 alpn_expected = *(++argv);
1114 } else if (strcmp(*argv, "-server_min_proto") == 0) {
1117 server_min_proto = *(++argv);
1118 } else if (strcmp(*argv, "-server_max_proto") == 0) {
1121 server_max_proto = *(++argv);
1122 } else if (strcmp(*argv, "-client_min_proto") == 0) {
1125 client_min_proto = *(++argv);
1126 } else if (strcmp(*argv, "-client_max_proto") == 0) {
1129 client_max_proto = *(++argv);
1130 } else if (strcmp(*argv, "-should_negotiate") == 0) {
1133 should_negotiate = *(++argv);
1134 } else if (strcmp(*argv, "-sn_client") == 0) {
1137 sn_client = *(++argv);
1138 } else if (strcmp(*argv, "-sn_server1") == 0) {
1141 sn_server1 = *(++argv);
1142 } else if (strcmp(*argv, "-sn_server2") == 0) {
1145 sn_server2 = *(++argv);
1146 } else if (strcmp(*argv, "-sn_expect1") == 0) {
1148 } else if (strcmp(*argv, "-sn_expect2") == 0) {
1150 } else if (strcmp(*argv, "-server_sess_out") == 0) {
1153 server_sess_out = *(++argv);
1154 } else if (strcmp(*argv, "-server_sess_in") == 0) {
1157 server_sess_in = *(++argv);
1158 } else if (strcmp(*argv, "-client_sess_out") == 0) {
1161 client_sess_out = *(++argv);
1162 } else if (strcmp(*argv, "-client_sess_in") == 0) {
1165 client_sess_in = *(++argv);
1166 } else if (strcmp(*argv, "-should_reuse") == 0) {
1169 should_reuse = !!atoi(*(++argv));
1170 } else if (strcmp(*argv, "-no_ticket") == 0) {
1172 } else if (strcmp(*argv, "-provider") == 0) {
1175 provider = *(++argv);
1176 } else if (strcmp(*argv, "-config") == 0) {
1184 /* Try to process command using SSL_CONF */
1185 rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv);
1186 /* If not processed try server */
1188 rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv);
1189 /* Recognised: store it for later use */
1194 conf_args = sk_OPENSSL_STRING_new_null();
1198 if (!sk_OPENSSL_STRING_push(conf_args, arg))
1200 if (!sk_OPENSSL_STRING_push(conf_args, argn))
1205 BIO_printf(bio_err, "Missing argument for %s\n", arg);
1207 BIO_printf(bio_err, "Error with command %s\n", arg);
1209 BIO_printf(bio_err, "unknown option %s\n", arg);
1222 if (ssl3 + tls1 + tls1_2 + dtls + dtls1 + dtls12 > 1) {
1223 fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
1228 #ifdef OPENSSL_NO_SSL3
1233 #ifdef OPENSSL_NO_TLS1
1238 #ifdef OPENSSL_NO_TLS1_2
1243 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
1248 #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
1256 * Testing was requested for a compiled-out protocol (e.g. SSLv3).
1257 * Ideally, we would error out, but the generic test wrapper can't know
1258 * when to expect failure. So we do nothing and return success.
1261 fprintf(stderr, "Testing was requested for a disabled protocol. "
1262 "Skipping tests.\n");
1267 if (!ssl3 && !tls1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1
1268 && !reuse && !force) {
1269 fprintf(stderr, "This case cannot work. Use -f to perform "
1270 "the test anyway (and\n-d to see what happens), "
1271 "or add one of -ssl3, -tls1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
1272 "to avoid protocol mismatch.\n");
1277 if (bio_type != BIO_PAIR) {
1278 fprintf(stderr, "Using BIO pair (-bio_pair)\n");
1279 bio_type = BIO_PAIR;
1281 if (number < 50 && !force)
1283 "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
1286 #ifndef OPENSSL_NO_COMP
1287 if (comp == COMP_ZLIB)
1290 if (COMP_get_type(cm) != NID_undef) {
1291 if (SSL_COMP_add_compression_method(comp, cm) != 0) {
1292 fprintf(stderr, "Failed to add compression method\n");
1293 ERR_print_errors_fp(stderr);
1297 "Warning: %s compression not supported\n",
1298 comp == COMP_ZLIB ? "zlib" : "unknown");
1299 ERR_print_errors_fp(stderr);
1302 ssl_comp_methods = SSL_COMP_get_compression_methods();
1303 n = sk_SSL_COMP_num(ssl_comp_methods);
1306 printf("Available compression methods:");
1307 for (j = 0; j < n; j++) {
1308 SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
1309 printf(" %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c));
1315 #ifndef OPENSSL_NO_TLS
1316 meth = TLS_method();
1318 min_version = SSL3_VERSION;
1319 max_version = SSL3_VERSION;
1321 min_version = TLS1_VERSION;
1322 max_version = TLS1_VERSION;
1323 } else if (tls1_2) {
1324 min_version = TLS1_2_VERSION;
1325 max_version = TLS1_2_VERSION;
1331 #ifndef OPENSSL_NO_DTLS
1332 if (dtls || dtls1 || dtls12) {
1333 meth = DTLS_method();
1335 min_version = DTLS1_VERSION;
1336 max_version = DTLS1_VERSION;
1337 } else if (dtls12) {
1338 min_version = DTLS1_2_VERSION;
1339 max_version = DTLS1_2_VERSION;
1347 if (provider != NULL
1348 && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
1351 c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1352 s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1353 s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */
1354 if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) {
1355 ERR_print_errors(bio_err);
1359 * Since we will use low security ciphersuites and keys for testing set
1360 * security level to zero by default. Tests can override this by adding
1361 * "@SECLEVEL=n" to the cipher string.
1363 SSL_CTX_set_security_level(c_ctx, 0);
1364 SSL_CTX_set_security_level(s_ctx, 0);
1365 SSL_CTX_set_security_level(s_ctx2, 0);
1368 SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
1369 SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
1372 if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0)
1374 if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0)
1376 if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0)
1378 if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0)
1381 if (cipher != NULL) {
1382 if (strcmp(cipher, "") == 0) {
1383 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) {
1384 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1387 ERR_print_errors(bio_err);
1391 /* Should have failed when clearing all TLSv1.2 ciphers. */
1392 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1396 if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) {
1397 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1400 ERR_print_errors(bio_err);
1404 /* Should have failed when clearing all TLSv1.2 ciphers. */
1405 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1409 if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1410 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1413 ERR_print_errors(bio_err);
1417 /* Should have failed when clearing all TLSv1.2 ciphers. */
1418 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1422 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
1423 || !SSL_CTX_set_cipher_list(s_ctx, cipher)
1424 || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1425 ERR_print_errors(bio_err);
1430 if (ciphersuites != NULL) {
1431 if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
1432 || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
1433 || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
1434 ERR_print_errors(bio_err);
1439 #ifndef OPENSSL_NO_CT
1440 if (ct_validation &&
1441 !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
1442 ERR_print_errors(bio_err);
1447 /* Process SSL_CONF arguments */
1448 SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
1449 SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
1450 SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
1452 for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
1454 arg = sk_OPENSSL_STRING_value(conf_args, i);
1455 argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
1456 rv = SSL_CONF_cmd(c_cctx, arg, argn);
1457 /* If not recognised use server context */
1459 rv = SSL_CONF_cmd(s_cctx2, arg, argn);
1461 rv = SSL_CONF_cmd(s_cctx, arg, argn);
1464 BIO_printf(bio_err, "Error processing %s %s\n",
1465 arg, argn ? argn : "");
1466 ERR_print_errors(bio_err);
1471 if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
1472 BIO_puts(bio_err, "Error finishing context\n");
1473 ERR_print_errors(bio_err);
1476 #ifndef OPENSSL_NO_DH
1479 dhpkey = get_dh1024dsa(libctx);
1481 dhpkey = get_dh512(libctx);
1483 dhpkey = get_dh2048(libctx);
1485 if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
1486 EVP_PKEY_free(dhpkey);
1487 BIO_puts(bio_err, "Error getting DH parameters\n");
1488 ERR_print_errors(bio_err);
1491 SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey);
1492 SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey);
1496 if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
1497 || SSL_CTX_load_verify_dir(s_ctx, CApath))
1498 || !SSL_CTX_set_default_verify_paths(s_ctx)
1499 || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
1500 || SSL_CTX_load_verify_dir(s_ctx2, CApath))
1501 || !SSL_CTX_set_default_verify_paths(s_ctx2)
1502 || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
1503 || SSL_CTX_load_verify_dir(c_ctx, CApath))
1504 || !SSL_CTX_set_default_verify_paths(c_ctx)) {
1505 ERR_print_errors(bio_err);
1508 #ifndef OPENSSL_NO_CT
1509 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
1510 !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
1511 !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
1512 ERR_print_errors(bio_err);
1517 printf("client authentication\n");
1518 SSL_CTX_set_verify(s_ctx,
1519 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1521 SSL_CTX_set_verify(s_ctx2,
1522 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1524 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
1526 SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
1530 printf("server authentication\n");
1531 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
1532 SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
1537 int session_id_context = 0;
1538 if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
1539 sizeof(session_id_context)) ||
1540 !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
1541 sizeof(session_id_context))) {
1542 ERR_print_errors(bio_err);
1547 /* Use PSK only if PSK key is given */
1548 if (psk_key != NULL) {
1550 * no_psk is used to avoid putting psk command to openssl tool
1554 * if PSK is not compiled in and psk key is given, do nothing and
1560 #ifndef OPENSSL_NO_PSK
1561 SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
1562 SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
1563 SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
1565 BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
1566 if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
1567 !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
1568 BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
1569 ERR_print_errors(bio_err);
1575 #ifndef OPENSSL_NO_NEXTPROTONEG
1577 SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
1580 if (npn_server_reject) {
1582 "Can't have both -npn_server and -npn_server_reject\n");
1585 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
1586 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
1588 if (npn_server_reject) {
1589 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
1590 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
1594 if (serverinfo_sct) {
1595 if (!SSL_CTX_add_client_custom_ext(c_ctx,
1596 TLSEXT_TYPE_signed_certificate_timestamp,
1598 serverinfo_cli_parse_cb, NULL)) {
1599 BIO_printf(bio_err, "Error adding SCT extension\n");
1603 if (serverinfo_tack) {
1604 if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
1606 serverinfo_cli_parse_cb, NULL)) {
1607 BIO_printf(bio_err, "Error adding TACK extension\n");
1611 if (serverinfo_file)
1612 if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
1613 !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
1614 BIO_printf(bio_err, "missing serverinfo file\n");
1619 if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
1620 custom_ext_0_cli_add_cb,
1622 custom_ext_0_cli_parse_cb, NULL)
1623 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
1624 custom_ext_1_cli_add_cb,
1626 custom_ext_1_cli_parse_cb, NULL)
1627 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
1628 custom_ext_2_cli_add_cb,
1630 custom_ext_2_cli_parse_cb, NULL)
1631 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
1632 custom_ext_3_cli_add_cb,
1634 custom_ext_3_cli_parse_cb, NULL)
1635 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
1636 custom_ext_0_srv_add_cb,
1638 custom_ext_0_srv_parse_cb, NULL)
1639 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
1640 custom_ext_0_srv_add_cb,
1642 custom_ext_0_srv_parse_cb, NULL)
1643 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
1644 custom_ext_1_srv_add_cb,
1646 custom_ext_1_srv_parse_cb, NULL)
1647 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
1648 custom_ext_1_srv_add_cb,
1650 custom_ext_1_srv_parse_cb, NULL)
1651 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
1652 custom_ext_2_srv_add_cb,
1654 custom_ext_2_srv_parse_cb, NULL)
1655 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
1656 custom_ext_2_srv_add_cb,
1658 custom_ext_2_srv_parse_cb, NULL)
1659 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
1660 custom_ext_3_srv_add_cb,
1662 custom_ext_3_srv_parse_cb, NULL)
1663 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
1664 custom_ext_3_srv_add_cb,
1666 custom_ext_3_srv_parse_cb, NULL)) {
1667 BIO_printf(bio_err, "Error setting custom extensions\n");
1673 SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
1675 SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
1679 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
1682 BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
1685 /* Returns 0 on success!! */
1686 if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
1687 BIO_printf(bio_err, "Error setting ALPN\n");
1694 if (server_sess_in != NULL) {
1695 server_sess = read_session(server_sess_in);
1696 if (server_sess == NULL)
1699 if (client_sess_in != NULL) {
1700 client_sess = read_session(client_sess_in);
1701 if (client_sess == NULL)
1705 if (server_sess_out != NULL || server_sess_in != NULL) {
1709 /* Use a fixed key so that we can decrypt the ticket. */
1710 size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
1711 keys = OPENSSL_zalloc(size);
1712 SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
1716 if (sn_server1 != NULL || sn_server2 != NULL)
1717 SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
1719 c_ssl = SSL_new(c_ctx);
1720 s_ssl = SSL_new(s_ctx);
1723 SSL_set_tlsext_host_name(c_ssl, sn_client);
1725 if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1727 if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1729 if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1731 if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1735 if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
1736 BIO_printf(bio_err, "Can't add server session\n");
1737 ERR_print_errors(bio_err);
1742 BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
1743 for (i = 0; i < number; i++) {
1745 if (!SSL_set_session(c_ssl, NULL)) {
1746 BIO_printf(bio_err, "Failed to set session\n");
1750 if (client_sess_in != NULL) {
1751 if (SSL_set_session(c_ssl, client_sess) == 0) {
1752 BIO_printf(bio_err, "Can't set client session\n");
1753 ERR_print_errors(bio_err);
1759 ret = doit(s_ssl, c_ssl, bytes);
1762 ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
1764 #ifndef OPENSSL_NO_SOCK
1766 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
1767 bytes, &s_time, &c_time);
1770 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
1771 bytes, &s_time, &c_time);
1780 if (ret != EXIT_SUCCESS) break;
1783 if (should_negotiate && ret == EXIT_SUCCESS &&
1784 strcmp(should_negotiate, "fail-server") != 0 &&
1785 strcmp(should_negotiate, "fail-client") != 0) {
1786 int version = protocol_from_string(should_negotiate);
1788 BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
1792 if (SSL_version(c_ssl) != version) {
1793 BIO_printf(bio_err, "Unexpected version negotiated. "
1794 "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
1800 if (should_reuse != -1) {
1801 if (SSL_session_reused(s_ssl) != should_reuse ||
1802 SSL_session_reused(c_ssl) != should_reuse) {
1803 BIO_printf(bio_err, "Unexpected session reuse state. "
1804 "Expected: %d, server: %d, client: %d\n", should_reuse,
1805 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
1811 if (server_sess_out != NULL) {
1812 if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
1817 if (client_sess_out != NULL) {
1818 if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
1825 print_details(c_ssl, "");
1828 #ifdef CLOCKS_PER_SEC
1830 * "To determine the time in seconds, the value returned by the clock
1831 * function should be divided by the value of the macro
1832 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1834 BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
1835 "Approximate total client time: %6.2f s\n",
1836 (double)s_time / CLOCKS_PER_SEC,
1837 (double)c_time / CLOCKS_PER_SEC);
1839 BIO_printf(bio_stdout,
1840 "Approximate total server time: %6.2f units\n"
1841 "Approximate total client time: %6.2f units\n",
1842 (double)s_time, (double)c_time);
1851 SSL_CTX_free(s_ctx);
1852 SSL_CTX_free(s_ctx2);
1853 SSL_CTX_free(c_ctx);
1854 SSL_CONF_CTX_free(s_cctx);
1855 SSL_CONF_CTX_free(s_cctx2);
1856 SSL_CONF_CTX_free(c_cctx);
1857 sk_OPENSSL_STRING_free(conf_args);
1859 BIO_free(bio_stdout);
1861 SSL_SESSION_free(server_sess);
1862 SSL_SESSION_free(client_sess);
1864 OSSL_PROVIDER_unload(defctxnull);
1865 OSSL_PROVIDER_unload(thisprov);
1866 OSSL_LIB_CTX_free(libctx);
1872 #ifndef OPENSSL_NO_SOCK
1873 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
1874 clock_t *s_time, clock_t *c_time)
1876 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
1877 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
1878 BIO *acpt = NULL, *server = NULL, *client = NULL;
1880 int ret = EXIT_FAILURE;
1881 int err_in_client = 0;
1882 int err_in_server = 0;
1884 acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
1888 BIO_set_accept_ip_family(acpt, family);
1889 BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
1890 if (BIO_do_accept(acpt) <= 0)
1893 BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
1895 client = BIO_new_connect(addr_str);
1896 BIO_set_conn_ip_family(client, family);
1900 if (BIO_set_nbio(client, 1) <= 0)
1902 if (BIO_set_nbio(acpt, 1) <= 0)
1906 int st_connect = 0, st_accept = 0;
1908 while(!st_connect || !st_accept) {
1910 if (BIO_do_connect(client) <= 0) {
1911 if (!BIO_should_retry(client))
1918 if (BIO_do_accept(acpt) <= 0) {
1919 if (!BIO_should_retry(acpt))
1927 /* We're not interested in accepting further connects */
1928 server = BIO_pop(acpt);
1932 s_ssl_bio = BIO_new(BIO_f_ssl());
1936 c_ssl_bio = BIO_new(BIO_f_ssl());
1940 SSL_set_connect_state(c_ssl);
1941 SSL_set_bio(c_ssl, client, client);
1942 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
1944 SSL_set_accept_state(s_ssl);
1945 SSL_set_bio(s_ssl, server, server);
1946 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
1950 * c_ssl_bio: SSL filter BIO
1952 * client: I/O for SSL library
1955 * server: I/O for SSL library
1957 * s_ssl_bio: SSL filter BIO
1961 * We have non-blocking behaviour throughout this test program, but
1962 * can be sure that there is *some* progress in each iteration; so we
1963 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
1964 * we just try everything in each iteration
1970 char cbuf[1024 * 8];
1972 clock_t c_clock = clock();
1974 memset(cbuf, 0, sizeof(cbuf));
1977 if (SSL_in_init(c_ssl))
1978 printf("client waiting in SSL_connect - %s\n",
1979 SSL_state_string_long(c_ssl));
1982 /* Write to server. */
1984 if (cw_num > (long)sizeof(cbuf))
1988 r = BIO_write(c_ssl_bio, cbuf, i);
1990 if (!BIO_should_retry(c_ssl_bio)) {
1991 fprintf(stderr, "ERROR in CLIENT\n");
1996 * BIO_should_retry(...) can just be ignored here. The
1997 * library expects us to call BIO_write with the same
1998 * arguments again, and that's what we will do in the
2001 } else if (r == 0) {
2002 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2006 printf("client wrote %d\n", r);
2012 /* Read from server. */
2014 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2016 if (!BIO_should_retry(c_ssl_bio)) {
2017 fprintf(stderr, "ERROR in CLIENT\n");
2022 * Again, "BIO_should_retry" can be ignored.
2024 } else if (r == 0) {
2025 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2029 printf("client read %d\n", r);
2035 * c_time and s_time increments will typically be very small
2036 * (depending on machine speed and clock tick intervals), but
2037 * sampling over a large number of connections should result in
2038 * fairly accurate figures. We cannot guarantee a lot, however
2039 * -- if each connection lasts for exactly one clock tick, it
2040 * will be counted only for the client or only for the server or
2043 *c_time += (clock() - c_clock);
2049 char sbuf[1024 * 8];
2051 clock_t s_clock = clock();
2053 memset(sbuf, 0, sizeof(sbuf));
2056 if (SSL_in_init(s_ssl))
2057 printf("server waiting in SSL_accept - %s\n",
2058 SSL_state_string_long(s_ssl));
2061 /* Write to client. */
2063 if (sw_num > (long)sizeof(sbuf))
2067 r = BIO_write(s_ssl_bio, sbuf, i);
2069 if (!BIO_should_retry(s_ssl_bio)) {
2070 fprintf(stderr, "ERROR in SERVER\n");
2074 /* Ignore "BIO_should_retry". */
2075 } else if (r == 0) {
2076 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2080 printf("server wrote %d\n", r);
2086 /* Read from client. */
2088 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2090 if (!BIO_should_retry(s_ssl_bio)) {
2091 fprintf(stderr, "ERROR in SERVER\n");
2096 } else if (r == 0) {
2097 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2101 printf("server read %d\n", r);
2106 *s_time += (clock() - s_clock);
2109 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2112 print_details(c_ssl, "DONE via TCP connect: ");
2113 # ifndef OPENSSL_NO_NEXTPROTONEG
2114 if (verify_npn(c_ssl, s_ssl) < 0)
2117 if (verify_serverinfo() < 0) {
2118 fprintf(stderr, "Server info verify error\n");
2121 if (verify_alpn(c_ssl, s_ssl) < 0
2122 || verify_servername(c_ssl, s_ssl) < 0)
2125 if (custom_ext_error) {
2126 fprintf(stderr, "Custom extension error\n");
2130 # ifndef OPENSSL_NO_NEXTPROTONEG
2136 ERR_print_errors(bio_err);
2141 BIO_free(s_ssl_bio);
2142 BIO_free(c_ssl_bio);
2144 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2145 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2146 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2147 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2153 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
2154 clock_t *s_time, clock_t *c_time)
2156 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
2157 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
2158 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
2159 int ret = EXIT_FAILURE;
2160 int err_in_client = 0;
2161 int err_in_server = 0;
2163 size_t bufsiz = 256; /* small buffer for testing */
2165 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
2167 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
2170 s_ssl_bio = BIO_new(BIO_f_ssl());
2174 c_ssl_bio = BIO_new(BIO_f_ssl());
2178 SSL_set_connect_state(c_ssl);
2179 SSL_set_bio(c_ssl, client, client);
2180 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
2182 SSL_set_accept_state(s_ssl);
2183 SSL_set_bio(s_ssl, server, server);
2184 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
2188 * c_ssl_bio: SSL filter BIO
2190 * client: pseudo-I/O for SSL library
2192 * client_io: client's SSL communication; usually to be
2193 * relayed over some I/O facility, but in this
2194 * test program, we're the server, too:
2196 * server_io: server's SSL communication
2198 * server: pseudo-I/O for SSL library
2200 * s_ssl_bio: SSL filter BIO
2202 * The client and the server each employ a "BIO pair":
2203 * client + client_io, server + server_io.
2204 * BIO pairs are symmetric. A BIO pair behaves similar
2205 * to a non-blocking socketpair (but both endpoints must
2206 * be handled by the same thread).
2207 * [Here we could connect client and server to the ends
2208 * of a single BIO pair, but then this code would be less
2209 * suitable as an example for BIO pairs in general.]
2211 * Useful functions for querying the state of BIO pair endpoints:
2213 * BIO_ctrl_pending(bio) number of bytes we can read now
2214 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
2215 * other side's read attempt
2216 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2218 * ..._read_request is never more than ..._write_guarantee;
2219 * it depends on the application which one you should use.
2223 * We have non-blocking behaviour throughout this test program, but
2224 * can be sure that there is *some* progress in each iteration; so we
2225 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2226 * we just try everything in each iteration
2232 char cbuf[1024 * 8];
2234 clock_t c_clock = clock();
2236 memset(cbuf, 0, sizeof(cbuf));
2239 if (SSL_in_init(c_ssl))
2240 printf("client waiting in SSL_connect - %s\n",
2241 SSL_state_string_long(c_ssl));
2244 /* Write to server. */
2246 if (cw_num > (long)sizeof(cbuf))
2250 r = BIO_write(c_ssl_bio, cbuf, i);
2252 if (!BIO_should_retry(c_ssl_bio)) {
2253 fprintf(stderr, "ERROR in CLIENT\n");
2258 * BIO_should_retry(...) can just be ignored here. The
2259 * library expects us to call BIO_write with the same
2260 * arguments again, and that's what we will do in the
2263 } else if (r == 0) {
2264 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2268 printf("client wrote %d\n", r);
2274 /* Read from server. */
2276 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2278 if (!BIO_should_retry(c_ssl_bio)) {
2279 fprintf(stderr, "ERROR in CLIENT\n");
2284 * Again, "BIO_should_retry" can be ignored.
2286 } else if (r == 0) {
2287 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2291 printf("client read %d\n", r);
2297 * c_time and s_time increments will typically be very small
2298 * (depending on machine speed and clock tick intervals), but
2299 * sampling over a large number of connections should result in
2300 * fairly accurate figures. We cannot guarantee a lot, however
2301 * -- if each connection lasts for exactly one clock tick, it
2302 * will be counted only for the client or only for the server or
2305 *c_time += (clock() - c_clock);
2311 char sbuf[1024 * 8];
2313 clock_t s_clock = clock();
2315 memset(sbuf, 0, sizeof(sbuf));
2318 if (SSL_in_init(s_ssl))
2319 printf("server waiting in SSL_accept - %s\n",
2320 SSL_state_string_long(s_ssl));
2323 /* Write to client. */
2325 if (sw_num > (long)sizeof(sbuf))
2329 r = BIO_write(s_ssl_bio, sbuf, i);
2331 if (!BIO_should_retry(s_ssl_bio)) {
2332 fprintf(stderr, "ERROR in SERVER\n");
2336 /* Ignore "BIO_should_retry". */
2337 } else if (r == 0) {
2338 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2342 printf("server wrote %d\n", r);
2348 /* Read from client. */
2350 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2352 if (!BIO_should_retry(s_ssl_bio)) {
2353 fprintf(stderr, "ERROR in SERVER\n");
2358 } else if (r == 0) {
2359 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2363 printf("server read %d\n", r);
2368 *s_time += (clock() - s_clock);
2372 /* "I/O" BETWEEN CLIENT AND SERVER. */
2375 BIO *io1 = server_io, *io2 = client_io;
2377 * we use the non-copying interface for io1 and the standard
2378 * BIO_write/BIO_read interface for io2
2381 static int prev_progress = 1;
2389 r1 = BIO_ctrl_pending(io1);
2390 r2 = BIO_ctrl_get_write_guarantee(io2);
2398 if (INT_MAX < num) /* yeah, right */
2401 r = BIO_nread(io1, &dataptr, (int)num);
2403 assert(r <= (int)num);
2405 * possibly r < num (non-contiguous data)
2408 r = BIO_write(io2, dataptr, (int)num);
2409 if (r != (int)num) { /* can't happen */
2410 fprintf(stderr, "ERROR: BIO_write could not write "
2411 "BIO_ctrl_get_write_guarantee() bytes");
2417 printf((io1 == client_io) ?
2418 "C->S relaying: %d bytes\n" :
2419 "S->C relaying: %d bytes\n", (int)num);
2429 r1 = BIO_ctrl_pending(io2);
2430 r2 = BIO_ctrl_get_read_request(io1);
2432 * here we could use ..._get_write_guarantee instead of
2433 * ..._get_read_request, but by using the latter we test
2434 * restartability of the SSL implementation more thoroughly
2446 --num; /* test restartability even more thoroughly */
2448 r = BIO_nwrite0(io1, &dataptr);
2452 r = BIO_read(io2, dataptr, (int)num);
2453 if (r != (int)num) { /* can't happen */
2454 fprintf(stderr, "ERROR: BIO_read could not read "
2455 "BIO_ctrl_pending() bytes");
2459 r = BIO_nwrite(io1, &dataptr, (int)num);
2460 if (r != (int)num) { /* can't happen */
2461 fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
2462 "BIO_nwrite0() bytes");
2467 printf((io2 == client_io) ?
2468 "C->S relaying: %d bytes\n" :
2469 "S->C relaying: %d bytes\n", (int)num);
2471 } /* no loop, BIO_ctrl_get_read_request now
2472 * returns 0 anyway */
2474 if (!progress && !prev_progress)
2475 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
2476 fprintf(stderr, "ERROR: got stuck\n");
2477 fprintf(stderr, " ERROR.\n");
2480 prev_progress = progress;
2483 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2486 print_details(c_ssl, "DONE via BIO pair: ");
2487 #ifndef OPENSSL_NO_NEXTPROTONEG
2488 if (verify_npn(c_ssl, s_ssl) < 0)
2491 if (verify_serverinfo() < 0) {
2492 fprintf(stderr, "Server info verify error\n");
2495 if (verify_alpn(c_ssl, s_ssl) < 0
2496 || verify_servername(c_ssl, s_ssl) < 0)
2499 if (custom_ext_error) {
2500 fprintf(stderr, "Custom extension error\n");
2504 #ifndef OPENSSL_NO_NEXTPROTONEG
2510 ERR_print_errors(bio_err);
2513 BIO_free(server_io);
2515 BIO_free(client_io);
2516 BIO_free(s_ssl_bio);
2517 BIO_free(c_ssl_bio);
2519 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2520 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2521 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2522 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2532 int doit(SSL *s_ssl, SSL *c_ssl, long count)
2534 char *cbuf = NULL, *sbuf = NULL;
2536 long cw_num = count, cr_num = count;
2537 long sw_num = count, sr_num = count;
2538 int ret = EXIT_FAILURE;
2543 int c_r, c_w, s_r, s_w;
2546 int c_write, s_write;
2547 int do_server = 0, do_client = 0;
2548 int max_frag = 5 * 1024;
2549 int err_in_client = 0;
2550 int err_in_server = 0;
2552 bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
2554 if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2556 if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2559 c_to_s = BIO_new(BIO_s_mem());
2560 s_to_c = BIO_new(BIO_s_mem());
2561 if ((s_to_c == NULL) || (c_to_s == NULL)) {
2562 ERR_print_errors(bio_err);
2566 c_bio = BIO_new(BIO_f_ssl());
2567 s_bio = BIO_new(BIO_f_ssl());
2568 if ((c_bio == NULL) || (s_bio == NULL)) {
2569 ERR_print_errors(bio_err);
2573 SSL_set_connect_state(c_ssl);
2574 SSL_set_bio(c_ssl, s_to_c, c_to_s);
2575 SSL_set_max_send_fragment(c_ssl, max_frag);
2576 BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
2579 * We've just given our ref to these BIOs to c_ssl. We need another one to
2582 if (!BIO_up_ref(c_to_s)) {
2583 /* c_to_s and s_to_c will get freed when we free c_ssl */
2588 if (!BIO_up_ref(s_to_c)) {
2589 /* s_to_c will get freed when we free c_ssl */
2594 SSL_set_accept_state(s_ssl);
2595 SSL_set_bio(s_ssl, c_to_s, s_to_c);
2597 /* We've used up all our refs to these now */
2601 SSL_set_max_send_fragment(s_ssl, max_frag);
2602 BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
2608 c_write = 1, s_write = 0;
2610 /* We can always do writes */
2615 i = (int)BIO_pending(s_bio);
2616 if ((i && s_r) || s_w)
2619 i = (int)BIO_pending(c_bio);
2620 if ((i && c_r) || c_w)
2623 if (do_server && debug) {
2624 if (SSL_in_init(s_ssl))
2625 printf("server waiting in SSL_accept - %s\n",
2626 SSL_state_string_long(s_ssl));
2629 if (do_client && debug) {
2630 if (SSL_in_init(c_ssl))
2631 printf("client waiting in SSL_connect - %s\n",
2632 SSL_state_string_long(c_ssl));
2635 if (!do_client && !do_server) {
2636 fprintf(stdout, "ERROR IN STARTUP\n");
2637 ERR_print_errors(bio_err);
2640 if (do_client && !(done & C_DONE)) {
2642 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
2643 i = BIO_write(c_bio, cbuf, j);
2647 if (BIO_should_retry(c_bio)) {
2648 if (BIO_should_read(c_bio))
2650 if (BIO_should_write(c_bio))
2653 fprintf(stderr, "ERROR in CLIENT\n");
2655 ERR_print_errors(bio_err);
2658 } else if (i == 0) {
2659 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2663 printf("client wrote %d\n", i);
2668 if (max_frag > 1029)
2669 SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
2672 i = BIO_read(c_bio, cbuf, bufsiz);
2676 if (BIO_should_retry(c_bio)) {
2677 if (BIO_should_read(c_bio))
2679 if (BIO_should_write(c_bio))
2682 fprintf(stderr, "ERROR in CLIENT\n");
2684 ERR_print_errors(bio_err);
2687 } else if (i == 0) {
2688 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2692 printf("client read %d\n", i);
2701 done = S_DONE | C_DONE;
2707 if (do_server && !(done & S_DONE)) {
2709 i = BIO_read(s_bio, sbuf, bufsiz);
2713 if (BIO_should_retry(s_bio)) {
2714 if (BIO_should_read(s_bio))
2716 if (BIO_should_write(s_bio))
2719 fprintf(stderr, "ERROR in SERVER\n");
2721 ERR_print_errors(bio_err);
2724 } else if (i == 0) {
2725 ERR_print_errors(bio_err);
2727 "SSL SERVER STARTUP FAILED in SSL_read\n");
2731 printf("server read %d\n", i);
2744 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
2745 i = BIO_write(s_bio, sbuf, j);
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_write\n");
2767 printf("server wrote %d\n", i);
2773 if (max_frag > 1029)
2774 SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
2779 if ((done & S_DONE) && (done & C_DONE))
2784 print_details(c_ssl, "DONE: ");
2785 #ifndef OPENSSL_NO_NEXTPROTONEG
2786 if (verify_npn(c_ssl, s_ssl) < 0)
2789 if (verify_serverinfo() < 0) {
2790 fprintf(stderr, "Server info verify error\n");
2793 if (custom_ext_error) {
2794 fprintf(stderr, "Custom extension error\n");
2801 BIO_free_all(c_bio);
2802 BIO_free_all(s_bio);
2806 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2807 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2808 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2809 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2814 static int verify_callback(int ok, X509_STORE_CTX *ctx)
2818 s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
2822 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
2824 fprintf(stderr, "depth=%d error=%d %s\n",
2825 X509_STORE_CTX_get_error_depth(ctx),
2826 X509_STORE_CTX_get_error(ctx), buf);
2831 int i = X509_STORE_CTX_get_error(ctx);
2835 fprintf(stderr, "Error string: %s\n",
2836 X509_verify_cert_error_string(i));
2838 case X509_V_ERR_CERT_NOT_YET_VALID:
2839 case X509_V_ERR_CERT_HAS_EXPIRED:
2840 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2849 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
2852 struct app_verify_arg *cb_arg = arg;
2854 if (cb_arg->app_verify) {
2855 char *s = NULL, buf[256];
2856 X509 *c = X509_STORE_CTX_get0_cert(ctx);
2858 printf("In app_verify_callback, allowing cert. ");
2859 printf("Arg is: %s\n", cb_arg->string);
2860 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2861 (void *)ctx, (void *)c);
2863 s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
2865 printf("cert depth=%d %s\n",
2866 X509_STORE_CTX_get_error_depth(ctx), buf);
2871 ok = X509_verify_cert(ctx);
2876 #ifndef OPENSSL_NO_PSK
2877 /* convert the PSK key (psk_key) in ascii to binary (psk) */
2878 static int psk_key2bn(const char *pskkey, unsigned char *psk,
2879 unsigned int max_psk_len)
2884 ret = BN_hex2bn(&bn, pskkey);
2886 BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
2891 if (BN_num_bytes(bn) > (int)max_psk_len) {
2893 "psk buffer of callback is too small (%d) for key (%d)\n",
2894 max_psk_len, BN_num_bytes(bn));
2898 ret = BN_bn2bin(bn, psk);
2903 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
2905 unsigned int max_identity_len,
2907 unsigned int max_psk_len)
2910 unsigned int psk_len = 0;
2912 ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
2916 fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
2918 ret = psk_key2bn(psk_key, psk, max_psk_len);
2926 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
2928 unsigned int max_psk_len)
2930 unsigned int psk_len = 0;
2932 if (strcmp(identity, "Client_identity") != 0) {
2933 BIO_printf(bio_err, "server: PSK error: client identity not found\n");
2936 psk_len = psk_key2bn(psk_key, psk, max_psk_len);