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>
62 * Or gethostname won't be declared properly
63 * on Compaq platforms (at least with DEC C).
64 * Do not try to put it earlier, or IPv6 includes
67 #define _XOPEN_SOURCE_EXTENDED 1
69 #ifdef OPENSSL_SYS_WINDOWS
75 #include "helpers/predefined_dhparams.h"
77 static SSL_CTX *s_ctx = NULL;
78 static SSL_CTX *s_ctx2 = NULL;
81 * There is really no standard for this, so let's assign something
86 static int verify_callback(int ok, X509_STORE_CTX *ctx);
87 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
88 #define APP_CALLBACK_STRING "Test Callback Argument"
89 struct app_verify_arg {
94 static char *psk_key = NULL; /* by default PSK is not used */
95 #ifndef OPENSSL_NO_PSK
96 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
98 unsigned int max_identity_len,
100 unsigned int max_psk_len);
101 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
103 unsigned int max_psk_len);
106 static BIO *bio_err = NULL;
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 defctxnull = OSSL_PROVIDER_load(NULL, "null");
1349 if (defctxnull == NULL)
1351 libctx = OSSL_LIB_CTX_new();
1356 && !OSSL_LIB_CTX_load_config(libctx, config))
1359 thisprov = OSSL_PROVIDER_load(libctx, provider);
1360 if (thisprov == NULL)
1364 c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1365 s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1366 s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */
1367 if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) {
1368 ERR_print_errors(bio_err);
1372 * Since we will use low security ciphersuites and keys for testing set
1373 * security level to zero by default. Tests can override this by adding
1374 * "@SECLEVEL=n" to the cipher string.
1376 SSL_CTX_set_security_level(c_ctx, 0);
1377 SSL_CTX_set_security_level(s_ctx, 0);
1378 SSL_CTX_set_security_level(s_ctx2, 0);
1381 SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
1382 SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
1385 if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0)
1387 if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0)
1389 if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0)
1391 if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0)
1394 if (cipher != NULL) {
1395 if (strcmp(cipher, "") == 0) {
1396 if (!SSL_CTX_set_cipher_list(c_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_ctx, 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(s_ctx2, cipher)) {
1423 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1426 ERR_print_errors(bio_err);
1430 /* Should have failed when clearing all TLSv1.2 ciphers. */
1431 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1435 if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
1436 || !SSL_CTX_set_cipher_list(s_ctx, cipher)
1437 || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1438 ERR_print_errors(bio_err);
1443 if (ciphersuites != NULL) {
1444 if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
1445 || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
1446 || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
1447 ERR_print_errors(bio_err);
1452 #ifndef OPENSSL_NO_CT
1453 if (ct_validation &&
1454 !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
1455 ERR_print_errors(bio_err);
1460 /* Process SSL_CONF arguments */
1461 SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
1462 SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
1463 SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
1465 for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
1467 arg = sk_OPENSSL_STRING_value(conf_args, i);
1468 argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
1469 rv = SSL_CONF_cmd(c_cctx, arg, argn);
1470 /* If not recognised use server context */
1472 rv = SSL_CONF_cmd(s_cctx2, arg, argn);
1474 rv = SSL_CONF_cmd(s_cctx, arg, argn);
1477 BIO_printf(bio_err, "Error processing %s %s\n",
1478 arg, argn ? argn : "");
1479 ERR_print_errors(bio_err);
1484 if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
1485 BIO_puts(bio_err, "Error finishing context\n");
1486 ERR_print_errors(bio_err);
1489 #ifndef OPENSSL_NO_DH
1492 dhpkey = get_dh1024dsa(libctx);
1494 dhpkey = get_dh512(libctx);
1496 dhpkey = get_dh2048(libctx);
1498 if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
1499 EVP_PKEY_free(dhpkey);
1500 BIO_puts(bio_err, "Error getting DH parameters\n");
1501 ERR_print_errors(bio_err);
1504 SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey);
1505 SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey);
1509 if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
1510 || SSL_CTX_load_verify_dir(s_ctx, CApath))
1511 || !SSL_CTX_set_default_verify_paths(s_ctx)
1512 || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
1513 || SSL_CTX_load_verify_dir(s_ctx2, CApath))
1514 || !SSL_CTX_set_default_verify_paths(s_ctx2)
1515 || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
1516 || SSL_CTX_load_verify_dir(c_ctx, CApath))
1517 || !SSL_CTX_set_default_verify_paths(c_ctx)) {
1518 ERR_print_errors(bio_err);
1521 #ifndef OPENSSL_NO_CT
1522 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
1523 !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
1524 !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
1525 ERR_print_errors(bio_err);
1530 printf("client authentication\n");
1531 SSL_CTX_set_verify(s_ctx,
1532 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1534 SSL_CTX_set_verify(s_ctx2,
1535 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1537 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
1539 SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
1543 printf("server authentication\n");
1544 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
1545 SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
1550 int session_id_context = 0;
1551 if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
1552 sizeof(session_id_context)) ||
1553 !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
1554 sizeof(session_id_context))) {
1555 ERR_print_errors(bio_err);
1560 /* Use PSK only if PSK key is given */
1561 if (psk_key != NULL) {
1563 * no_psk is used to avoid putting psk command to openssl tool
1567 * if PSK is not compiled in and psk key is given, do nothing and
1573 #ifndef OPENSSL_NO_PSK
1574 SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
1575 SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
1576 SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
1578 BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
1579 if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
1580 !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
1581 BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
1582 ERR_print_errors(bio_err);
1588 #ifndef OPENSSL_NO_NEXTPROTONEG
1590 SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
1593 if (npn_server_reject) {
1595 "Can't have both -npn_server and -npn_server_reject\n");
1598 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
1599 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
1601 if (npn_server_reject) {
1602 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
1603 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
1607 if (serverinfo_sct) {
1608 if (!SSL_CTX_add_client_custom_ext(c_ctx,
1609 TLSEXT_TYPE_signed_certificate_timestamp,
1611 serverinfo_cli_parse_cb, NULL)) {
1612 BIO_printf(bio_err, "Error adding SCT extension\n");
1616 if (serverinfo_tack) {
1617 if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
1619 serverinfo_cli_parse_cb, NULL)) {
1620 BIO_printf(bio_err, "Error adding TACK extension\n");
1624 if (serverinfo_file)
1625 if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
1626 !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
1627 BIO_printf(bio_err, "missing serverinfo file\n");
1632 if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
1633 custom_ext_0_cli_add_cb,
1635 custom_ext_0_cli_parse_cb, NULL)
1636 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
1637 custom_ext_1_cli_add_cb,
1639 custom_ext_1_cli_parse_cb, NULL)
1640 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
1641 custom_ext_2_cli_add_cb,
1643 custom_ext_2_cli_parse_cb, NULL)
1644 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
1645 custom_ext_3_cli_add_cb,
1647 custom_ext_3_cli_parse_cb, NULL)
1648 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
1649 custom_ext_0_srv_add_cb,
1651 custom_ext_0_srv_parse_cb, NULL)
1652 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
1653 custom_ext_0_srv_add_cb,
1655 custom_ext_0_srv_parse_cb, NULL)
1656 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
1657 custom_ext_1_srv_add_cb,
1659 custom_ext_1_srv_parse_cb, NULL)
1660 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
1661 custom_ext_1_srv_add_cb,
1663 custom_ext_1_srv_parse_cb, NULL)
1664 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
1665 custom_ext_2_srv_add_cb,
1667 custom_ext_2_srv_parse_cb, NULL)
1668 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
1669 custom_ext_2_srv_add_cb,
1671 custom_ext_2_srv_parse_cb, NULL)
1672 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
1673 custom_ext_3_srv_add_cb,
1675 custom_ext_3_srv_parse_cb, NULL)
1676 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
1677 custom_ext_3_srv_add_cb,
1679 custom_ext_3_srv_parse_cb, NULL)) {
1680 BIO_printf(bio_err, "Error setting custom extensions\n");
1686 SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
1688 SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
1692 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
1695 BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
1698 /* Returns 0 on success!! */
1699 if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
1700 BIO_printf(bio_err, "Error setting ALPN\n");
1707 if (server_sess_in != NULL) {
1708 server_sess = read_session(server_sess_in);
1709 if (server_sess == NULL)
1712 if (client_sess_in != NULL) {
1713 client_sess = read_session(client_sess_in);
1714 if (client_sess == NULL)
1718 if (server_sess_out != NULL || server_sess_in != NULL) {
1722 /* Use a fixed key so that we can decrypt the ticket. */
1723 size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
1724 keys = OPENSSL_zalloc(size);
1725 SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
1729 if (sn_server1 != NULL || sn_server2 != NULL)
1730 SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
1732 c_ssl = SSL_new(c_ctx);
1733 s_ssl = SSL_new(s_ctx);
1736 SSL_set_tlsext_host_name(c_ssl, sn_client);
1738 if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1740 if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1742 if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1744 if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1748 if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
1749 BIO_printf(bio_err, "Can't add server session\n");
1750 ERR_print_errors(bio_err);
1755 BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
1756 for (i = 0; i < number; i++) {
1758 if (!SSL_set_session(c_ssl, NULL)) {
1759 BIO_printf(bio_err, "Failed to set session\n");
1763 if (client_sess_in != NULL) {
1764 if (SSL_set_session(c_ssl, client_sess) == 0) {
1765 BIO_printf(bio_err, "Can't set client session\n");
1766 ERR_print_errors(bio_err);
1772 ret = doit(s_ssl, c_ssl, bytes);
1775 ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
1777 #ifndef OPENSSL_NO_SOCK
1779 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
1780 bytes, &s_time, &c_time);
1783 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
1784 bytes, &s_time, &c_time);
1793 if (ret != EXIT_SUCCESS) break;
1796 if (should_negotiate && ret == EXIT_SUCCESS &&
1797 strcmp(should_negotiate, "fail-server") != 0 &&
1798 strcmp(should_negotiate, "fail-client") != 0) {
1799 int version = protocol_from_string(should_negotiate);
1801 BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
1805 if (SSL_version(c_ssl) != version) {
1806 BIO_printf(bio_err, "Unexpected version negotiated. "
1807 "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
1813 if (should_reuse != -1) {
1814 if (SSL_session_reused(s_ssl) != should_reuse ||
1815 SSL_session_reused(c_ssl) != should_reuse) {
1816 BIO_printf(bio_err, "Unexpected session reuse state. "
1817 "Expected: %d, server: %d, client: %d\n", should_reuse,
1818 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
1824 if (server_sess_out != NULL) {
1825 if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
1830 if (client_sess_out != NULL) {
1831 if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
1838 print_details(c_ssl, "");
1841 #ifdef CLOCKS_PER_SEC
1843 * "To determine the time in seconds, the value returned by the clock
1844 * function should be divided by the value of the macro
1845 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1847 BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
1848 "Approximate total client time: %6.2f s\n",
1849 (double)s_time / CLOCKS_PER_SEC,
1850 (double)c_time / CLOCKS_PER_SEC);
1852 BIO_printf(bio_stdout,
1853 "Approximate total server time: %6.2f units\n"
1854 "Approximate total client time: %6.2f units\n",
1855 (double)s_time, (double)c_time);
1864 SSL_CTX_free(s_ctx);
1865 SSL_CTX_free(s_ctx2);
1866 SSL_CTX_free(c_ctx);
1867 SSL_CONF_CTX_free(s_cctx);
1868 SSL_CONF_CTX_free(s_cctx2);
1869 SSL_CONF_CTX_free(c_cctx);
1870 sk_OPENSSL_STRING_free(conf_args);
1872 BIO_free(bio_stdout);
1874 SSL_SESSION_free(server_sess);
1875 SSL_SESSION_free(client_sess);
1877 OSSL_PROVIDER_unload(defctxnull);
1878 OSSL_PROVIDER_unload(thisprov);
1879 OSSL_LIB_CTX_free(libctx);
1885 #ifndef OPENSSL_NO_SOCK
1886 int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
1887 clock_t *s_time, clock_t *c_time)
1889 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
1890 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
1891 BIO *acpt = NULL, *server = NULL, *client = NULL;
1893 int ret = EXIT_FAILURE;
1894 int err_in_client = 0;
1895 int err_in_server = 0;
1897 acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
1901 BIO_set_accept_ip_family(acpt, family);
1902 BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
1903 if (BIO_do_accept(acpt) <= 0)
1906 BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
1908 client = BIO_new_connect(addr_str);
1909 BIO_set_conn_ip_family(client, family);
1913 if (BIO_set_nbio(client, 1) <= 0)
1915 if (BIO_set_nbio(acpt, 1) <= 0)
1919 int st_connect = 0, st_accept = 0;
1921 while(!st_connect || !st_accept) {
1923 if (BIO_do_connect(client) <= 0) {
1924 if (!BIO_should_retry(client))
1931 if (BIO_do_accept(acpt) <= 0) {
1932 if (!BIO_should_retry(acpt))
1940 /* We're not interested in accepting further connects */
1941 server = BIO_pop(acpt);
1945 s_ssl_bio = BIO_new(BIO_f_ssl());
1949 c_ssl_bio = BIO_new(BIO_f_ssl());
1953 SSL_set_connect_state(c_ssl);
1954 SSL_set_bio(c_ssl, client, client);
1955 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
1957 SSL_set_accept_state(s_ssl);
1958 SSL_set_bio(s_ssl, server, server);
1959 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
1963 * c_ssl_bio: SSL filter BIO
1965 * client: I/O for SSL library
1968 * server: I/O for SSL library
1970 * s_ssl_bio: SSL filter BIO
1974 * We have non-blocking behaviour throughout this test program, but
1975 * can be sure that there is *some* progress in each iteration; so we
1976 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
1977 * we just try everything in each iteration
1983 char cbuf[1024 * 8];
1985 clock_t c_clock = clock();
1987 memset(cbuf, 0, sizeof(cbuf));
1990 if (SSL_in_init(c_ssl))
1991 printf("client waiting in SSL_connect - %s\n",
1992 SSL_state_string_long(c_ssl));
1995 /* Write to server. */
1997 if (cw_num > (long)sizeof(cbuf))
2001 r = BIO_write(c_ssl_bio, cbuf, i);
2003 if (!BIO_should_retry(c_ssl_bio)) {
2004 fprintf(stderr, "ERROR in CLIENT\n");
2009 * BIO_should_retry(...) can just be ignored here. The
2010 * library expects us to call BIO_write with the same
2011 * arguments again, and that's what we will do in the
2014 } else if (r == 0) {
2015 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2019 printf("client wrote %d\n", r);
2025 /* Read from server. */
2027 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2029 if (!BIO_should_retry(c_ssl_bio)) {
2030 fprintf(stderr, "ERROR in CLIENT\n");
2035 * Again, "BIO_should_retry" can be ignored.
2037 } else if (r == 0) {
2038 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2042 printf("client read %d\n", r);
2048 * c_time and s_time increments will typically be very small
2049 * (depending on machine speed and clock tick intervals), but
2050 * sampling over a large number of connections should result in
2051 * fairly accurate figures. We cannot guarantee a lot, however
2052 * -- if each connection lasts for exactly one clock tick, it
2053 * will be counted only for the client or only for the server or
2056 *c_time += (clock() - c_clock);
2062 char sbuf[1024 * 8];
2064 clock_t s_clock = clock();
2066 memset(sbuf, 0, sizeof(sbuf));
2069 if (SSL_in_init(s_ssl))
2070 printf("server waiting in SSL_accept - %s\n",
2071 SSL_state_string_long(s_ssl));
2074 /* Write to client. */
2076 if (sw_num > (long)sizeof(sbuf))
2080 r = BIO_write(s_ssl_bio, sbuf, i);
2082 if (!BIO_should_retry(s_ssl_bio)) {
2083 fprintf(stderr, "ERROR in SERVER\n");
2087 /* Ignore "BIO_should_retry". */
2088 } else if (r == 0) {
2089 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2093 printf("server wrote %d\n", r);
2099 /* Read from client. */
2101 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2103 if (!BIO_should_retry(s_ssl_bio)) {
2104 fprintf(stderr, "ERROR in SERVER\n");
2109 } else if (r == 0) {
2110 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2114 printf("server read %d\n", r);
2119 *s_time += (clock() - s_clock);
2122 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2125 print_details(c_ssl, "DONE via TCP connect: ");
2126 # ifndef OPENSSL_NO_NEXTPROTONEG
2127 if (verify_npn(c_ssl, s_ssl) < 0)
2130 if (verify_serverinfo() < 0) {
2131 fprintf(stderr, "Server info verify error\n");
2134 if (verify_alpn(c_ssl, s_ssl) < 0
2135 || verify_servername(c_ssl, s_ssl) < 0)
2138 if (custom_ext_error) {
2139 fprintf(stderr, "Custom extension error\n");
2143 # ifndef OPENSSL_NO_NEXTPROTONEG
2149 ERR_print_errors(bio_err);
2154 BIO_free(s_ssl_bio);
2155 BIO_free(c_ssl_bio);
2157 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2158 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2159 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2160 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2166 int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
2167 clock_t *s_time, clock_t *c_time)
2169 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
2170 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
2171 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
2172 int ret = EXIT_FAILURE;
2173 int err_in_client = 0;
2174 int err_in_server = 0;
2176 size_t bufsiz = 256; /* small buffer for testing */
2178 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
2180 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
2183 s_ssl_bio = BIO_new(BIO_f_ssl());
2187 c_ssl_bio = BIO_new(BIO_f_ssl());
2191 SSL_set_connect_state(c_ssl);
2192 SSL_set_bio(c_ssl, client, client);
2193 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
2195 SSL_set_accept_state(s_ssl);
2196 SSL_set_bio(s_ssl, server, server);
2197 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
2201 * c_ssl_bio: SSL filter BIO
2203 * client: pseudo-I/O for SSL library
2205 * client_io: client's SSL communication; usually to be
2206 * relayed over some I/O facility, but in this
2207 * test program, we're the server, too:
2209 * server_io: server's SSL communication
2211 * server: pseudo-I/O for SSL library
2213 * s_ssl_bio: SSL filter BIO
2215 * The client and the server each employ a "BIO pair":
2216 * client + client_io, server + server_io.
2217 * BIO pairs are symmetric. A BIO pair behaves similar
2218 * to a non-blocking socketpair (but both endpoints must
2219 * be handled by the same thread).
2220 * [Here we could connect client and server to the ends
2221 * of a single BIO pair, but then this code would be less
2222 * suitable as an example for BIO pairs in general.]
2224 * Useful functions for querying the state of BIO pair endpoints:
2226 * BIO_ctrl_pending(bio) number of bytes we can read now
2227 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
2228 * other side's read attempt
2229 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2231 * ..._read_request is never more than ..._write_guarantee;
2232 * it depends on the application which one you should use.
2236 * We have non-blocking behaviour throughout this test program, but
2237 * can be sure that there is *some* progress in each iteration; so we
2238 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2239 * we just try everything in each iteration
2245 char cbuf[1024 * 8];
2247 clock_t c_clock = clock();
2249 memset(cbuf, 0, sizeof(cbuf));
2252 if (SSL_in_init(c_ssl))
2253 printf("client waiting in SSL_connect - %s\n",
2254 SSL_state_string_long(c_ssl));
2257 /* Write to server. */
2259 if (cw_num > (long)sizeof(cbuf))
2263 r = BIO_write(c_ssl_bio, cbuf, i);
2265 if (!BIO_should_retry(c_ssl_bio)) {
2266 fprintf(stderr, "ERROR in CLIENT\n");
2271 * BIO_should_retry(...) can just be ignored here. The
2272 * library expects us to call BIO_write with the same
2273 * arguments again, and that's what we will do in the
2276 } else if (r == 0) {
2277 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2281 printf("client wrote %d\n", r);
2287 /* Read from server. */
2289 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2291 if (!BIO_should_retry(c_ssl_bio)) {
2292 fprintf(stderr, "ERROR in CLIENT\n");
2297 * Again, "BIO_should_retry" can be ignored.
2299 } else if (r == 0) {
2300 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2304 printf("client read %d\n", r);
2310 * c_time and s_time increments will typically be very small
2311 * (depending on machine speed and clock tick intervals), but
2312 * sampling over a large number of connections should result in
2313 * fairly accurate figures. We cannot guarantee a lot, however
2314 * -- if each connection lasts for exactly one clock tick, it
2315 * will be counted only for the client or only for the server or
2318 *c_time += (clock() - c_clock);
2324 char sbuf[1024 * 8];
2326 clock_t s_clock = clock();
2328 memset(sbuf, 0, sizeof(sbuf));
2331 if (SSL_in_init(s_ssl))
2332 printf("server waiting in SSL_accept - %s\n",
2333 SSL_state_string_long(s_ssl));
2336 /* Write to client. */
2338 if (sw_num > (long)sizeof(sbuf))
2342 r = BIO_write(s_ssl_bio, sbuf, i);
2344 if (!BIO_should_retry(s_ssl_bio)) {
2345 fprintf(stderr, "ERROR in SERVER\n");
2349 /* Ignore "BIO_should_retry". */
2350 } else if (r == 0) {
2351 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2355 printf("server wrote %d\n", r);
2361 /* Read from client. */
2363 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2365 if (!BIO_should_retry(s_ssl_bio)) {
2366 fprintf(stderr, "ERROR in SERVER\n");
2371 } else if (r == 0) {
2372 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2376 printf("server read %d\n", r);
2381 *s_time += (clock() - s_clock);
2385 /* "I/O" BETWEEN CLIENT AND SERVER. */
2388 BIO *io1 = server_io, *io2 = client_io;
2390 * we use the non-copying interface for io1 and the standard
2391 * BIO_write/BIO_read interface for io2
2394 static int prev_progress = 1;
2402 r1 = BIO_ctrl_pending(io1);
2403 r2 = BIO_ctrl_get_write_guarantee(io2);
2411 if (INT_MAX < num) /* yeah, right */
2414 r = BIO_nread(io1, &dataptr, (int)num);
2416 assert(r <= (int)num);
2418 * possibly r < num (non-contiguous data)
2421 r = BIO_write(io2, dataptr, (int)num);
2422 if (r != (int)num) { /* can't happen */
2423 fprintf(stderr, "ERROR: BIO_write could not write "
2424 "BIO_ctrl_get_write_guarantee() bytes");
2430 printf((io1 == client_io) ?
2431 "C->S relaying: %d bytes\n" :
2432 "S->C relaying: %d bytes\n", (int)num);
2442 r1 = BIO_ctrl_pending(io2);
2443 r2 = BIO_ctrl_get_read_request(io1);
2445 * here we could use ..._get_write_guarantee instead of
2446 * ..._get_read_request, but by using the latter we test
2447 * restartability of the SSL implementation more thoroughly
2459 --num; /* test restartability even more thoroughly */
2461 r = BIO_nwrite0(io1, &dataptr);
2465 r = BIO_read(io2, dataptr, (int)num);
2466 if (r != (int)num) { /* can't happen */
2467 fprintf(stderr, "ERROR: BIO_read could not read "
2468 "BIO_ctrl_pending() bytes");
2472 r = BIO_nwrite(io1, &dataptr, (int)num);
2473 if (r != (int)num) { /* can't happen */
2474 fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
2475 "BIO_nwrite0() bytes");
2480 printf((io2 == client_io) ?
2481 "C->S relaying: %d bytes\n" :
2482 "S->C relaying: %d bytes\n", (int)num);
2484 } /* no loop, BIO_ctrl_get_read_request now
2485 * returns 0 anyway */
2487 if (!progress && !prev_progress)
2488 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
2489 fprintf(stderr, "ERROR: got stuck\n");
2490 fprintf(stderr, " ERROR.\n");
2493 prev_progress = progress;
2496 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2499 print_details(c_ssl, "DONE via BIO pair: ");
2500 #ifndef OPENSSL_NO_NEXTPROTONEG
2501 if (verify_npn(c_ssl, s_ssl) < 0)
2504 if (verify_serverinfo() < 0) {
2505 fprintf(stderr, "Server info verify error\n");
2508 if (verify_alpn(c_ssl, s_ssl) < 0
2509 || verify_servername(c_ssl, s_ssl) < 0)
2512 if (custom_ext_error) {
2513 fprintf(stderr, "Custom extension error\n");
2517 #ifndef OPENSSL_NO_NEXTPROTONEG
2523 ERR_print_errors(bio_err);
2526 BIO_free(server_io);
2528 BIO_free(client_io);
2529 BIO_free(s_ssl_bio);
2530 BIO_free(c_ssl_bio);
2532 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2533 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2534 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2535 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2545 int doit(SSL *s_ssl, SSL *c_ssl, long count)
2547 char *cbuf = NULL, *sbuf = NULL;
2549 long cw_num = count, cr_num = count;
2550 long sw_num = count, sr_num = count;
2551 int ret = EXIT_FAILURE;
2556 int c_r, c_w, s_r, s_w;
2559 int c_write, s_write;
2560 int do_server = 0, do_client = 0;
2561 int max_frag = 5 * 1024;
2562 int err_in_client = 0;
2563 int err_in_server = 0;
2565 bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
2567 if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2569 if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2572 c_to_s = BIO_new(BIO_s_mem());
2573 s_to_c = BIO_new(BIO_s_mem());
2574 if ((s_to_c == NULL) || (c_to_s == NULL)) {
2575 ERR_print_errors(bio_err);
2579 c_bio = BIO_new(BIO_f_ssl());
2580 s_bio = BIO_new(BIO_f_ssl());
2581 if ((c_bio == NULL) || (s_bio == NULL)) {
2582 ERR_print_errors(bio_err);
2586 SSL_set_connect_state(c_ssl);
2587 SSL_set_bio(c_ssl, s_to_c, c_to_s);
2588 SSL_set_max_send_fragment(c_ssl, max_frag);
2589 BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
2592 * We've just given our ref to these BIOs to c_ssl. We need another one to
2595 if (!BIO_up_ref(c_to_s)) {
2596 /* c_to_s and s_to_c will get freed when we free c_ssl */
2601 if (!BIO_up_ref(s_to_c)) {
2602 /* s_to_c will get freed when we free c_ssl */
2607 SSL_set_accept_state(s_ssl);
2608 SSL_set_bio(s_ssl, c_to_s, s_to_c);
2610 /* We've used up all our refs to these now */
2614 SSL_set_max_send_fragment(s_ssl, max_frag);
2615 BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
2621 c_write = 1, s_write = 0;
2623 /* We can always do writes */
2628 i = (int)BIO_pending(s_bio);
2629 if ((i && s_r) || s_w)
2632 i = (int)BIO_pending(c_bio);
2633 if ((i && c_r) || c_w)
2636 if (do_server && debug) {
2637 if (SSL_in_init(s_ssl))
2638 printf("server waiting in SSL_accept - %s\n",
2639 SSL_state_string_long(s_ssl));
2642 if (do_client && debug) {
2643 if (SSL_in_init(c_ssl))
2644 printf("client waiting in SSL_connect - %s\n",
2645 SSL_state_string_long(c_ssl));
2648 if (!do_client && !do_server) {
2649 fprintf(stdout, "ERROR IN STARTUP\n");
2650 ERR_print_errors(bio_err);
2653 if (do_client && !(done & C_DONE)) {
2655 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
2656 i = BIO_write(c_bio, cbuf, j);
2660 if (BIO_should_retry(c_bio)) {
2661 if (BIO_should_read(c_bio))
2663 if (BIO_should_write(c_bio))
2666 fprintf(stderr, "ERROR in CLIENT\n");
2668 ERR_print_errors(bio_err);
2671 } else if (i == 0) {
2672 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2676 printf("client wrote %d\n", i);
2681 if (max_frag > 1029)
2682 SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
2685 i = BIO_read(c_bio, cbuf, bufsiz);
2689 if (BIO_should_retry(c_bio)) {
2690 if (BIO_should_read(c_bio))
2692 if (BIO_should_write(c_bio))
2695 fprintf(stderr, "ERROR in CLIENT\n");
2697 ERR_print_errors(bio_err);
2700 } else if (i == 0) {
2701 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2705 printf("client read %d\n", i);
2714 done = S_DONE | C_DONE;
2720 if (do_server && !(done & S_DONE)) {
2722 i = BIO_read(s_bio, sbuf, bufsiz);
2726 if (BIO_should_retry(s_bio)) {
2727 if (BIO_should_read(s_bio))
2729 if (BIO_should_write(s_bio))
2732 fprintf(stderr, "ERROR in SERVER\n");
2734 ERR_print_errors(bio_err);
2737 } else if (i == 0) {
2738 ERR_print_errors(bio_err);
2740 "SSL SERVER STARTUP FAILED in SSL_read\n");
2744 printf("server read %d\n", i);
2757 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
2758 i = BIO_write(s_bio, sbuf, j);
2762 if (BIO_should_retry(s_bio)) {
2763 if (BIO_should_read(s_bio))
2765 if (BIO_should_write(s_bio))
2768 fprintf(stderr, "ERROR in SERVER\n");
2770 ERR_print_errors(bio_err);
2773 } else if (i == 0) {
2774 ERR_print_errors(bio_err);
2776 "SSL SERVER STARTUP FAILED in SSL_write\n");
2780 printf("server wrote %d\n", i);
2786 if (max_frag > 1029)
2787 SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
2792 if ((done & S_DONE) && (done & C_DONE))
2797 print_details(c_ssl, "DONE: ");
2798 #ifndef OPENSSL_NO_NEXTPROTONEG
2799 if (verify_npn(c_ssl, s_ssl) < 0)
2802 if (verify_serverinfo() < 0) {
2803 fprintf(stderr, "Server info verify error\n");
2806 if (custom_ext_error) {
2807 fprintf(stderr, "Custom extension error\n");
2814 BIO_free_all(c_bio);
2815 BIO_free_all(s_bio);
2819 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2820 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2821 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2822 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2827 static int verify_callback(int ok, X509_STORE_CTX *ctx)
2831 s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
2835 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
2837 fprintf(stderr, "depth=%d error=%d %s\n",
2838 X509_STORE_CTX_get_error_depth(ctx),
2839 X509_STORE_CTX_get_error(ctx), buf);
2844 int i = X509_STORE_CTX_get_error(ctx);
2848 fprintf(stderr, "Error string: %s\n",
2849 X509_verify_cert_error_string(i));
2851 case X509_V_ERR_CERT_NOT_YET_VALID:
2852 case X509_V_ERR_CERT_HAS_EXPIRED:
2853 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2862 static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
2865 struct app_verify_arg *cb_arg = arg;
2867 if (cb_arg->app_verify) {
2868 char *s = NULL, buf[256];
2869 X509 *c = X509_STORE_CTX_get0_cert(ctx);
2871 printf("In app_verify_callback, allowing cert. ");
2872 printf("Arg is: %s\n", cb_arg->string);
2873 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2874 (void *)ctx, (void *)c);
2876 s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
2878 printf("cert depth=%d %s\n",
2879 X509_STORE_CTX_get_error_depth(ctx), buf);
2884 ok = X509_verify_cert(ctx);
2889 #ifndef OPENSSL_NO_PSK
2890 /* convert the PSK key (psk_key) in ascii to binary (psk) */
2891 static int psk_key2bn(const char *pskkey, unsigned char *psk,
2892 unsigned int max_psk_len)
2897 ret = BN_hex2bn(&bn, pskkey);
2899 BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
2904 if (BN_num_bytes(bn) > (int)max_psk_len) {
2906 "psk buffer of callback is too small (%d) for key (%d)\n",
2907 max_psk_len, BN_num_bytes(bn));
2911 ret = BN_bn2bin(bn, psk);
2916 static unsigned int psk_client_callback(SSL *ssl, const char *hint,
2918 unsigned int max_identity_len,
2920 unsigned int max_psk_len)
2923 unsigned int psk_len = 0;
2925 ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
2929 fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
2931 ret = psk_key2bn(psk_key, psk, max_psk_len);
2939 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
2941 unsigned int max_psk_len)
2943 unsigned int psk_len = 0;
2945 if (strcmp(identity, "Client_identity") != 0) {
2946 BIO_printf(bio_err, "server: PSK error: client identity not found\n");
2949 psk_len = psk_key2bn(psk_key, psk, max_psk_len);