xref: /freebsd/crypto/openssl/test/ssl_old_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4*e0c4386eSCy Schubert  * Copyright 2005 Nokia. All rights reserved.
5*e0c4386eSCy Schubert  *
6*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
7*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
8*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
9*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
10*e0c4386eSCy Schubert  */
11*e0c4386eSCy Schubert 
12*e0c4386eSCy Schubert #include "e_os.h"
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert /* Or gethostname won't be declared properly on Linux and GNU platforms. */
15*e0c4386eSCy Schubert #ifndef _BSD_SOURCE
16*e0c4386eSCy Schubert # define _BSD_SOURCE 1
17*e0c4386eSCy Schubert #endif
18*e0c4386eSCy Schubert #ifndef _DEFAULT_SOURCE
19*e0c4386eSCy Schubert # define _DEFAULT_SOURCE 1
20*e0c4386eSCy Schubert #endif
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert #include <assert.h>
23*e0c4386eSCy Schubert #include <errno.h>
24*e0c4386eSCy Schubert #include <limits.h>
25*e0c4386eSCy Schubert #include <stdio.h>
26*e0c4386eSCy Schubert #include <stdlib.h>
27*e0c4386eSCy Schubert #include <string.h>
28*e0c4386eSCy Schubert #include <time.h>
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert #include "internal/nelem.h"
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert #ifdef OPENSSL_SYS_VMS
33*e0c4386eSCy Schubert /*
34*e0c4386eSCy Schubert  * Or isascii won't be declared properly on VMS (at least with DECompHP C).
35*e0c4386eSCy Schubert  */
36*e0c4386eSCy Schubert # define _XOPEN_SOURCE 500
37*e0c4386eSCy Schubert #endif
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert #include <ctype.h>
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert #include <openssl/bio.h>
42*e0c4386eSCy Schubert #include <openssl/crypto.h>
43*e0c4386eSCy Schubert #include <openssl/evp.h>
44*e0c4386eSCy Schubert #include <openssl/x509.h>
45*e0c4386eSCy Schubert #include <openssl/x509v3.h>
46*e0c4386eSCy Schubert #include <openssl/ssl.h>
47*e0c4386eSCy Schubert #include <openssl/err.h>
48*e0c4386eSCy Schubert #include <openssl/rand.h>
49*e0c4386eSCy Schubert #include <openssl/rsa.h>
50*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DSA
51*e0c4386eSCy Schubert # include <openssl/dsa.h>
52*e0c4386eSCy Schubert #endif
53*e0c4386eSCy Schubert #include <openssl/bn.h>
54*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CT
55*e0c4386eSCy Schubert # include <openssl/ct.h>
56*e0c4386eSCy Schubert #endif
57*e0c4386eSCy Schubert #include <openssl/provider.h>
58*e0c4386eSCy Schubert #include "testutil.h"
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert /*
61*e0c4386eSCy Schubert  * Or gethostname won't be declared properly
62*e0c4386eSCy Schubert  * on Compaq platforms (at least with DEC C).
63*e0c4386eSCy Schubert  * Do not try to put it earlier, or IPv6 includes
64*e0c4386eSCy Schubert  * get screwed...
65*e0c4386eSCy Schubert  */
66*e0c4386eSCy Schubert #define _XOPEN_SOURCE_EXTENDED  1
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert #ifdef OPENSSL_SYS_WINDOWS
69*e0c4386eSCy Schubert # include <winsock.h>
70*e0c4386eSCy Schubert #else
71*e0c4386eSCy Schubert # include <unistd.h>
72*e0c4386eSCy Schubert #endif
73*e0c4386eSCy Schubert 
74*e0c4386eSCy Schubert #include "helpers/predefined_dhparams.h"
75*e0c4386eSCy Schubert 
76*e0c4386eSCy Schubert static SSL_CTX *s_ctx = NULL;
77*e0c4386eSCy Schubert static SSL_CTX *s_ctx2 = NULL;
78*e0c4386eSCy Schubert 
79*e0c4386eSCy Schubert /*
80*e0c4386eSCy Schubert  * There is really no standard for this, so let's assign something
81*e0c4386eSCy Schubert  * only for this test
82*e0c4386eSCy Schubert  */
83*e0c4386eSCy Schubert #define COMP_ZLIB       1
84*e0c4386eSCy Schubert 
85*e0c4386eSCy Schubert static int verify_callback(int ok, X509_STORE_CTX *ctx);
86*e0c4386eSCy Schubert static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
87*e0c4386eSCy Schubert #define APP_CALLBACK_STRING "Test Callback Argument"
88*e0c4386eSCy Schubert struct app_verify_arg {
89*e0c4386eSCy Schubert     char *string;
90*e0c4386eSCy Schubert     int app_verify;
91*e0c4386eSCy Schubert };
92*e0c4386eSCy Schubert 
93*e0c4386eSCy Schubert static char *psk_key = NULL;    /* by default PSK is not used */
94*e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
95*e0c4386eSCy Schubert static unsigned int psk_client_callback(SSL *ssl, const char *hint,
96*e0c4386eSCy Schubert                                         char *identity,
97*e0c4386eSCy Schubert                                         unsigned int max_identity_len,
98*e0c4386eSCy Schubert                                         unsigned char *psk,
99*e0c4386eSCy Schubert                                         unsigned int max_psk_len);
100*e0c4386eSCy Schubert static unsigned int psk_server_callback(SSL *ssl, const char *identity,
101*e0c4386eSCy Schubert                                         unsigned char *psk,
102*e0c4386eSCy Schubert                                         unsigned int max_psk_len);
103*e0c4386eSCy Schubert #endif
104*e0c4386eSCy Schubert 
105*e0c4386eSCy Schubert static BIO *bio_stdout = NULL;
106*e0c4386eSCy Schubert 
107*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
108*e0c4386eSCy Schubert /* Note that this code assumes that this is only a one element list: */
109*e0c4386eSCy Schubert static const char NEXT_PROTO_STRING[] = "\x09testproto";
110*e0c4386eSCy Schubert static int npn_client = 0;
111*e0c4386eSCy Schubert static int npn_server = 0;
112*e0c4386eSCy Schubert static int npn_server_reject = 0;
113*e0c4386eSCy Schubert 
cb_client_npn(SSL * s,unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)114*e0c4386eSCy Schubert static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen,
115*e0c4386eSCy Schubert                          const unsigned char *in, unsigned int inlen,
116*e0c4386eSCy Schubert                          void *arg)
117*e0c4386eSCy Schubert {
118*e0c4386eSCy Schubert     /*
119*e0c4386eSCy Schubert      * This callback only returns the protocol string, rather than a length
120*e0c4386eSCy Schubert      * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
121*e0c4386eSCy Schubert      * and remove the first byte to chop off the length prefix.
122*e0c4386eSCy Schubert      */
123*e0c4386eSCy Schubert     *out = (unsigned char *)NEXT_PROTO_STRING + 1;
124*e0c4386eSCy Schubert     *outlen = sizeof(NEXT_PROTO_STRING) - 2;
125*e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_OK;
126*e0c4386eSCy Schubert }
127*e0c4386eSCy Schubert 
cb_server_npn(SSL * s,const unsigned char ** data,unsigned int * len,void * arg)128*e0c4386eSCy Schubert static int cb_server_npn(SSL *s, const unsigned char **data,
129*e0c4386eSCy Schubert                          unsigned int *len, void *arg)
130*e0c4386eSCy Schubert {
131*e0c4386eSCy Schubert     *data = (const unsigned char *)NEXT_PROTO_STRING;
132*e0c4386eSCy Schubert     *len = sizeof(NEXT_PROTO_STRING) - 1;
133*e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_OK;
134*e0c4386eSCy Schubert }
135*e0c4386eSCy Schubert 
cb_server_rejects_npn(SSL * s,const unsigned char ** data,unsigned int * len,void * arg)136*e0c4386eSCy Schubert static int cb_server_rejects_npn(SSL *s, const unsigned char **data,
137*e0c4386eSCy Schubert                                  unsigned int *len, void *arg)
138*e0c4386eSCy Schubert {
139*e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_NOACK;
140*e0c4386eSCy Schubert }
141*e0c4386eSCy Schubert 
verify_npn(SSL * client,SSL * server)142*e0c4386eSCy Schubert static int verify_npn(SSL *client, SSL *server)
143*e0c4386eSCy Schubert {
144*e0c4386eSCy Schubert     const unsigned char *client_s;
145*e0c4386eSCy Schubert     unsigned client_len;
146*e0c4386eSCy Schubert     const unsigned char *server_s;
147*e0c4386eSCy Schubert     unsigned server_len;
148*e0c4386eSCy Schubert 
149*e0c4386eSCy Schubert     SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
150*e0c4386eSCy Schubert     SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
151*e0c4386eSCy Schubert 
152*e0c4386eSCy Schubert     if (client_len) {
153*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Client NPN: ");
154*e0c4386eSCy Schubert         BIO_write(bio_stdout, client_s, client_len);
155*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "\n");
156*e0c4386eSCy Schubert     }
157*e0c4386eSCy Schubert 
158*e0c4386eSCy Schubert     if (server_len) {
159*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Server NPN: ");
160*e0c4386eSCy Schubert         BIO_write(bio_stdout, server_s, server_len);
161*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "\n");
162*e0c4386eSCy Schubert     }
163*e0c4386eSCy Schubert 
164*e0c4386eSCy Schubert     /*
165*e0c4386eSCy Schubert      * If an NPN string was returned, it must be the protocol that we
166*e0c4386eSCy Schubert      * expected to negotiate.
167*e0c4386eSCy Schubert      */
168*e0c4386eSCy Schubert     if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 ||
169*e0c4386eSCy Schubert                        memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
170*e0c4386eSCy Schubert         return -1;
171*e0c4386eSCy Schubert     if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 ||
172*e0c4386eSCy Schubert                        memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
173*e0c4386eSCy Schubert         return -1;
174*e0c4386eSCy Schubert 
175*e0c4386eSCy Schubert     if (!npn_client && client_len)
176*e0c4386eSCy Schubert         return -1;
177*e0c4386eSCy Schubert     if (!npn_server && server_len)
178*e0c4386eSCy Schubert         return -1;
179*e0c4386eSCy Schubert     if (npn_server_reject && server_len)
180*e0c4386eSCy Schubert         return -1;
181*e0c4386eSCy Schubert     if (npn_client && npn_server && (!client_len || !server_len))
182*e0c4386eSCy Schubert         return -1;
183*e0c4386eSCy Schubert 
184*e0c4386eSCy Schubert     return 0;
185*e0c4386eSCy Schubert }
186*e0c4386eSCy Schubert #endif
187*e0c4386eSCy Schubert 
188*e0c4386eSCy Schubert static const char *alpn_client;
189*e0c4386eSCy Schubert static char *alpn_server;
190*e0c4386eSCy Schubert static char *alpn_server2;
191*e0c4386eSCy Schubert static const char *alpn_expected;
192*e0c4386eSCy Schubert static unsigned char *alpn_selected;
193*e0c4386eSCy Schubert static const char *server_min_proto;
194*e0c4386eSCy Schubert static const char *server_max_proto;
195*e0c4386eSCy Schubert static const char *client_min_proto;
196*e0c4386eSCy Schubert static const char *client_max_proto;
197*e0c4386eSCy Schubert static const char *should_negotiate;
198*e0c4386eSCy Schubert static const char *sn_client;
199*e0c4386eSCy Schubert static const char *sn_server1;
200*e0c4386eSCy Schubert static const char *sn_server2;
201*e0c4386eSCy Schubert static int sn_expect = 0;
202*e0c4386eSCy Schubert static const char *server_sess_out;
203*e0c4386eSCy Schubert static const char *server_sess_in;
204*e0c4386eSCy Schubert static const char *client_sess_out;
205*e0c4386eSCy Schubert static const char *client_sess_in;
206*e0c4386eSCy Schubert static SSL_SESSION *server_sess;
207*e0c4386eSCy Schubert static SSL_SESSION *client_sess;
208*e0c4386eSCy Schubert 
servername_cb(SSL * s,int * ad,void * arg)209*e0c4386eSCy Schubert static int servername_cb(SSL *s, int *ad, void *arg)
210*e0c4386eSCy Schubert {
211*e0c4386eSCy Schubert     const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
212*e0c4386eSCy Schubert     if (sn_server2 == NULL) {
213*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Servername 2 is NULL\n");
214*e0c4386eSCy Schubert         return SSL_TLSEXT_ERR_NOACK;
215*e0c4386eSCy Schubert     }
216*e0c4386eSCy Schubert 
217*e0c4386eSCy Schubert     if (servername) {
218*e0c4386eSCy Schubert         if (s_ctx2 != NULL && sn_server2 != NULL &&
219*e0c4386eSCy Schubert             !OPENSSL_strcasecmp(servername, sn_server2)) {
220*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Switching server context.\n");
221*e0c4386eSCy Schubert             SSL_set_SSL_CTX(s, s_ctx2);
222*e0c4386eSCy Schubert         }
223*e0c4386eSCy Schubert     }
224*e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_OK;
225*e0c4386eSCy Schubert }
verify_servername(SSL * client,SSL * server)226*e0c4386eSCy Schubert static int verify_servername(SSL *client, SSL *server)
227*e0c4386eSCy Schubert {
228*e0c4386eSCy Schubert     /* just need to see if sn_context is what we expect */
229*e0c4386eSCy Schubert     SSL_CTX* ctx = SSL_get_SSL_CTX(server);
230*e0c4386eSCy Schubert     if (sn_expect == 0)
231*e0c4386eSCy Schubert         return 0;
232*e0c4386eSCy Schubert     if (sn_expect == 1 && ctx == s_ctx)
233*e0c4386eSCy Schubert         return 0;
234*e0c4386eSCy Schubert     if (sn_expect == 2 && ctx == s_ctx2)
235*e0c4386eSCy Schubert         return 0;
236*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect);
237*e0c4386eSCy Schubert     if (ctx == s_ctx2)
238*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Servername: context is 2\n");
239*e0c4386eSCy Schubert     else if (ctx == s_ctx)
240*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Servername: context is 1\n");
241*e0c4386eSCy Schubert     else
242*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Servername: context is unknown\n");
243*e0c4386eSCy Schubert     return -1;
244*e0c4386eSCy Schubert }
245*e0c4386eSCy Schubert 
246*e0c4386eSCy Schubert 
247*e0c4386eSCy Schubert /*-
248*e0c4386eSCy Schubert  * next_protos_parse parses a comma separated list of strings into a string
249*e0c4386eSCy Schubert  * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
250*e0c4386eSCy Schubert  *   outlen: (output) set to the length of the resulting buffer on success.
251*e0c4386eSCy Schubert  *   in: a NUL terminated string like "abc,def,ghi"
252*e0c4386eSCy Schubert  *
253*e0c4386eSCy Schubert  *   returns: a malloced buffer or NULL on failure.
254*e0c4386eSCy Schubert  */
next_protos_parse(size_t * outlen,const char * in)255*e0c4386eSCy Schubert static unsigned char *next_protos_parse(size_t *outlen,
256*e0c4386eSCy Schubert                                         const char *in)
257*e0c4386eSCy Schubert {
258*e0c4386eSCy Schubert     size_t len;
259*e0c4386eSCy Schubert     unsigned char *out;
260*e0c4386eSCy Schubert     size_t i, start = 0;
261*e0c4386eSCy Schubert 
262*e0c4386eSCy Schubert     len = strlen(in);
263*e0c4386eSCy Schubert     if (len >= 65535)
264*e0c4386eSCy Schubert         return NULL;
265*e0c4386eSCy Schubert 
266*e0c4386eSCy Schubert     out = OPENSSL_malloc(strlen(in) + 1);
267*e0c4386eSCy Schubert     if (!out)
268*e0c4386eSCy Schubert         return NULL;
269*e0c4386eSCy Schubert 
270*e0c4386eSCy Schubert     for (i = 0; i <= len; ++i) {
271*e0c4386eSCy Schubert         if (i == len || in[i] == ',') {
272*e0c4386eSCy Schubert             if (i - start > 255) {
273*e0c4386eSCy Schubert                 OPENSSL_free(out);
274*e0c4386eSCy Schubert                 return NULL;
275*e0c4386eSCy Schubert             }
276*e0c4386eSCy Schubert             out[start] = (unsigned char)(i - start);
277*e0c4386eSCy Schubert             start = i + 1;
278*e0c4386eSCy Schubert         } else
279*e0c4386eSCy Schubert             out[i + 1] = in[i];
280*e0c4386eSCy Schubert     }
281*e0c4386eSCy Schubert 
282*e0c4386eSCy Schubert     *outlen = len + 1;
283*e0c4386eSCy Schubert     return out;
284*e0c4386eSCy Schubert }
285*e0c4386eSCy Schubert 
cb_server_alpn(SSL * s,const unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)286*e0c4386eSCy Schubert static int cb_server_alpn(SSL *s, const unsigned char **out,
287*e0c4386eSCy Schubert                           unsigned char *outlen, const unsigned char *in,
288*e0c4386eSCy Schubert                           unsigned int inlen, void *arg)
289*e0c4386eSCy Schubert {
290*e0c4386eSCy Schubert     unsigned char *protos;
291*e0c4386eSCy Schubert     size_t protos_len;
292*e0c4386eSCy Schubert     char* alpn_str = arg;
293*e0c4386eSCy Schubert 
294*e0c4386eSCy Schubert     protos = next_protos_parse(&protos_len, alpn_str);
295*e0c4386eSCy Schubert     if (protos == NULL) {
296*e0c4386eSCy Schubert         fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
297*e0c4386eSCy Schubert                 alpn_str);
298*e0c4386eSCy Schubert         abort();
299*e0c4386eSCy Schubert     }
300*e0c4386eSCy Schubert 
301*e0c4386eSCy Schubert     if (SSL_select_next_proto
302*e0c4386eSCy Schubert         ((unsigned char **)out, outlen, protos, protos_len, in,
303*e0c4386eSCy Schubert          inlen) != OPENSSL_NPN_NEGOTIATED) {
304*e0c4386eSCy Schubert         OPENSSL_free(protos);
305*e0c4386eSCy Schubert         return SSL_TLSEXT_ERR_NOACK;
306*e0c4386eSCy Schubert     }
307*e0c4386eSCy Schubert 
308*e0c4386eSCy Schubert     /*
309*e0c4386eSCy Schubert      * Make a copy of the selected protocol which will be freed in
310*e0c4386eSCy Schubert      * verify_alpn.
311*e0c4386eSCy Schubert      */
312*e0c4386eSCy Schubert     alpn_selected = OPENSSL_malloc(*outlen);
313*e0c4386eSCy Schubert     if (alpn_selected == NULL) {
314*e0c4386eSCy Schubert         fprintf(stderr, "failed to allocate memory\n");
315*e0c4386eSCy Schubert         OPENSSL_free(protos);
316*e0c4386eSCy Schubert         abort();
317*e0c4386eSCy Schubert     }
318*e0c4386eSCy Schubert     memcpy(alpn_selected, *out, *outlen);
319*e0c4386eSCy Schubert     *out = alpn_selected;
320*e0c4386eSCy Schubert 
321*e0c4386eSCy Schubert     OPENSSL_free(protos);
322*e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_OK;
323*e0c4386eSCy Schubert }
324*e0c4386eSCy Schubert 
verify_alpn(SSL * client,SSL * server)325*e0c4386eSCy Schubert static int verify_alpn(SSL *client, SSL *server)
326*e0c4386eSCy Schubert {
327*e0c4386eSCy Schubert     const unsigned char *client_proto, *server_proto;
328*e0c4386eSCy Schubert     unsigned int client_proto_len = 0, server_proto_len = 0;
329*e0c4386eSCy Schubert     SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
330*e0c4386eSCy Schubert     SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
331*e0c4386eSCy Schubert 
332*e0c4386eSCy Schubert     OPENSSL_free(alpn_selected);
333*e0c4386eSCy Schubert     alpn_selected = NULL;
334*e0c4386eSCy Schubert 
335*e0c4386eSCy Schubert     if (client_proto_len != server_proto_len) {
336*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
337*e0c4386eSCy Schubert         goto err;
338*e0c4386eSCy Schubert     }
339*e0c4386eSCy Schubert 
340*e0c4386eSCy Schubert     if (client_proto != NULL &&
341*e0c4386eSCy Schubert         memcmp(client_proto, server_proto, client_proto_len) != 0) {
342*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
343*e0c4386eSCy Schubert         goto err;
344*e0c4386eSCy Schubert     }
345*e0c4386eSCy Schubert 
346*e0c4386eSCy Schubert     if (client_proto_len > 0 && alpn_expected == NULL) {
347*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
348*e0c4386eSCy Schubert         goto err;
349*e0c4386eSCy Schubert     }
350*e0c4386eSCy Schubert 
351*e0c4386eSCy Schubert     if (alpn_expected != NULL &&
352*e0c4386eSCy Schubert         (client_proto_len != strlen(alpn_expected) ||
353*e0c4386eSCy Schubert          memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
354*e0c4386eSCy Schubert         BIO_printf(bio_stdout,
355*e0c4386eSCy Schubert                    "ALPN selected protocols not equal to expected protocol: %s\n",
356*e0c4386eSCy Schubert                    alpn_expected);
357*e0c4386eSCy Schubert         goto err;
358*e0c4386eSCy Schubert     }
359*e0c4386eSCy Schubert 
360*e0c4386eSCy Schubert     return 0;
361*e0c4386eSCy Schubert 
362*e0c4386eSCy Schubert  err:
363*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "ALPN results: client: '");
364*e0c4386eSCy Schubert     BIO_write(bio_stdout, client_proto, client_proto_len);
365*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "', server: '");
366*e0c4386eSCy Schubert     BIO_write(bio_stdout, server_proto, server_proto_len);
367*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "'\n");
368*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '",
369*e0c4386eSCy Schubert                    alpn_client);
370*e0c4386eSCy Schubert     if (SSL_get_SSL_CTX(server) == s_ctx2) {
371*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "%s'\n",
372*e0c4386eSCy Schubert                    alpn_server2);
373*e0c4386eSCy Schubert     } else {
374*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "%s'\n",
375*e0c4386eSCy Schubert                    alpn_server);
376*e0c4386eSCy Schubert     }
377*e0c4386eSCy Schubert     return -1;
378*e0c4386eSCy Schubert }
379*e0c4386eSCy Schubert 
380*e0c4386eSCy Schubert /*
381*e0c4386eSCy Schubert  * WARNING : below extension types are *NOT* IETF assigned, and could
382*e0c4386eSCy Schubert  * conflict if these types are reassigned and handled specially by OpenSSL
383*e0c4386eSCy Schubert  * in the future
384*e0c4386eSCy Schubert  */
385*e0c4386eSCy Schubert #define TACK_EXT_TYPE 62208
386*e0c4386eSCy Schubert #define CUSTOM_EXT_TYPE_0 1000
387*e0c4386eSCy Schubert #define CUSTOM_EXT_TYPE_1 1001
388*e0c4386eSCy Schubert #define CUSTOM_EXT_TYPE_2 1002
389*e0c4386eSCy Schubert #define CUSTOM_EXT_TYPE_3 1003
390*e0c4386eSCy Schubert 
391*e0c4386eSCy Schubert static const char custom_ext_cli_string[] = "abc";
392*e0c4386eSCy Schubert static const char custom_ext_srv_string[] = "defg";
393*e0c4386eSCy Schubert 
394*e0c4386eSCy Schubert /* These set from cmdline */
395*e0c4386eSCy Schubert static char *serverinfo_file = NULL;
396*e0c4386eSCy Schubert static int serverinfo_sct = 0;
397*e0c4386eSCy Schubert static int serverinfo_tack = 0;
398*e0c4386eSCy Schubert 
399*e0c4386eSCy Schubert /* These set based on extension callbacks */
400*e0c4386eSCy Schubert static int serverinfo_sct_seen = 0;
401*e0c4386eSCy Schubert static int serverinfo_tack_seen = 0;
402*e0c4386eSCy Schubert static int serverinfo_other_seen = 0;
403*e0c4386eSCy Schubert 
404*e0c4386eSCy Schubert /* This set from cmdline */
405*e0c4386eSCy Schubert static int custom_ext = 0;
406*e0c4386eSCy Schubert 
407*e0c4386eSCy Schubert /* This set based on extension callbacks */
408*e0c4386eSCy Schubert static int custom_ext_error = 0;
409*e0c4386eSCy Schubert 
serverinfo_cli_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)410*e0c4386eSCy Schubert static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
411*e0c4386eSCy Schubert                                    const unsigned char *in, size_t inlen,
412*e0c4386eSCy Schubert                                    int *al, void *arg)
413*e0c4386eSCy Schubert {
414*e0c4386eSCy Schubert     if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp)
415*e0c4386eSCy Schubert         serverinfo_sct_seen++;
416*e0c4386eSCy Schubert     else if (ext_type == TACK_EXT_TYPE)
417*e0c4386eSCy Schubert         serverinfo_tack_seen++;
418*e0c4386eSCy Schubert     else
419*e0c4386eSCy Schubert         serverinfo_other_seen++;
420*e0c4386eSCy Schubert     return 1;
421*e0c4386eSCy Schubert }
422*e0c4386eSCy Schubert 
verify_serverinfo(void)423*e0c4386eSCy Schubert static int verify_serverinfo(void)
424*e0c4386eSCy Schubert {
425*e0c4386eSCy Schubert     if (serverinfo_sct != serverinfo_sct_seen)
426*e0c4386eSCy Schubert         return -1;
427*e0c4386eSCy Schubert     if (serverinfo_tack != serverinfo_tack_seen)
428*e0c4386eSCy Schubert         return -1;
429*e0c4386eSCy Schubert     if (serverinfo_other_seen)
430*e0c4386eSCy Schubert         return -1;
431*e0c4386eSCy Schubert     return 0;
432*e0c4386eSCy Schubert }
433*e0c4386eSCy Schubert 
434*e0c4386eSCy Schubert /*-
435*e0c4386eSCy Schubert  * Four test cases for custom extensions:
436*e0c4386eSCy Schubert  * 0 - no ClientHello extension or ServerHello response
437*e0c4386eSCy Schubert  * 1 - ClientHello with "abc", no response
438*e0c4386eSCy Schubert  * 2 - ClientHello with "abc", empty response
439*e0c4386eSCy Schubert  * 3 - ClientHello with "abc", "defg" response
440*e0c4386eSCy Schubert  */
441*e0c4386eSCy Schubert 
custom_ext_0_cli_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)442*e0c4386eSCy Schubert static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
443*e0c4386eSCy Schubert                                    const unsigned char **out,
444*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
445*e0c4386eSCy Schubert {
446*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_0)
447*e0c4386eSCy Schubert         custom_ext_error = 1;
448*e0c4386eSCy Schubert     return 0;                   /* Don't send an extension */
449*e0c4386eSCy Schubert }
450*e0c4386eSCy Schubert 
custom_ext_0_cli_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)451*e0c4386eSCy Schubert static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
452*e0c4386eSCy Schubert                                      const unsigned char *in,
453*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
454*e0c4386eSCy Schubert {
455*e0c4386eSCy Schubert     return 1;
456*e0c4386eSCy Schubert }
457*e0c4386eSCy Schubert 
custom_ext_1_cli_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)458*e0c4386eSCy Schubert static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
459*e0c4386eSCy Schubert                                    const unsigned char **out,
460*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
461*e0c4386eSCy Schubert {
462*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_1)
463*e0c4386eSCy Schubert         custom_ext_error = 1;
464*e0c4386eSCy Schubert     *out = (const unsigned char *)custom_ext_cli_string;
465*e0c4386eSCy Schubert     *outlen = strlen(custom_ext_cli_string);
466*e0c4386eSCy Schubert     return 1;                   /* Send "abc" */
467*e0c4386eSCy Schubert }
468*e0c4386eSCy Schubert 
custom_ext_1_cli_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)469*e0c4386eSCy Schubert static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
470*e0c4386eSCy Schubert                                      const unsigned char *in,
471*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
472*e0c4386eSCy Schubert {
473*e0c4386eSCy Schubert     return 1;
474*e0c4386eSCy Schubert }
475*e0c4386eSCy Schubert 
custom_ext_2_cli_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)476*e0c4386eSCy Schubert static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
477*e0c4386eSCy Schubert                                    const unsigned char **out,
478*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
479*e0c4386eSCy Schubert {
480*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_2)
481*e0c4386eSCy Schubert         custom_ext_error = 1;
482*e0c4386eSCy Schubert     *out = (const unsigned char *)custom_ext_cli_string;
483*e0c4386eSCy Schubert     *outlen = strlen(custom_ext_cli_string);
484*e0c4386eSCy Schubert     return 1;                   /* Send "abc" */
485*e0c4386eSCy Schubert }
486*e0c4386eSCy Schubert 
custom_ext_2_cli_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)487*e0c4386eSCy Schubert static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
488*e0c4386eSCy Schubert                                      const unsigned char *in,
489*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
490*e0c4386eSCy Schubert {
491*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_2)
492*e0c4386eSCy Schubert         custom_ext_error = 1;
493*e0c4386eSCy Schubert     if (inlen != 0)
494*e0c4386eSCy Schubert         custom_ext_error = 1;   /* Should be empty response */
495*e0c4386eSCy Schubert     return 1;
496*e0c4386eSCy Schubert }
497*e0c4386eSCy Schubert 
custom_ext_3_cli_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)498*e0c4386eSCy Schubert static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
499*e0c4386eSCy Schubert                                    const unsigned char **out,
500*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
501*e0c4386eSCy Schubert {
502*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_3)
503*e0c4386eSCy Schubert         custom_ext_error = 1;
504*e0c4386eSCy Schubert     *out = (const unsigned char *)custom_ext_cli_string;
505*e0c4386eSCy Schubert     *outlen = strlen(custom_ext_cli_string);
506*e0c4386eSCy Schubert     return 1;                   /* Send "abc" */
507*e0c4386eSCy Schubert }
508*e0c4386eSCy Schubert 
custom_ext_3_cli_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)509*e0c4386eSCy Schubert static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
510*e0c4386eSCy Schubert                                      const unsigned char *in,
511*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
512*e0c4386eSCy Schubert {
513*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_3)
514*e0c4386eSCy Schubert         custom_ext_error = 1;
515*e0c4386eSCy Schubert     if (inlen != strlen(custom_ext_srv_string))
516*e0c4386eSCy Schubert         custom_ext_error = 1;
517*e0c4386eSCy Schubert     if (memcmp(custom_ext_srv_string, in, inlen) != 0)
518*e0c4386eSCy Schubert         custom_ext_error = 1;   /* Check for "defg" */
519*e0c4386eSCy Schubert     return 1;
520*e0c4386eSCy Schubert }
521*e0c4386eSCy Schubert 
522*e0c4386eSCy Schubert /*
523*e0c4386eSCy Schubert  * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
524*e0c4386eSCy Schubert  * for this extension
525*e0c4386eSCy Schubert  */
custom_ext_0_srv_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)526*e0c4386eSCy Schubert static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
527*e0c4386eSCy Schubert                                      const unsigned char *in,
528*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
529*e0c4386eSCy Schubert {
530*e0c4386eSCy Schubert     custom_ext_error = 1;
531*e0c4386eSCy Schubert     return 1;
532*e0c4386eSCy Schubert }
533*e0c4386eSCy Schubert 
534*e0c4386eSCy Schubert /* 'add' callbacks are only called if the 'parse' callback is called */
custom_ext_0_srv_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)535*e0c4386eSCy Schubert static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
536*e0c4386eSCy Schubert                                    const unsigned char **out,
537*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
538*e0c4386eSCy Schubert {
539*e0c4386eSCy Schubert     /* Error: should not have been called */
540*e0c4386eSCy Schubert     custom_ext_error = 1;
541*e0c4386eSCy Schubert     return 0;                   /* Don't send an extension */
542*e0c4386eSCy Schubert }
543*e0c4386eSCy Schubert 
custom_ext_1_srv_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)544*e0c4386eSCy Schubert static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
545*e0c4386eSCy Schubert                                      const unsigned char *in,
546*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
547*e0c4386eSCy Schubert {
548*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_1)
549*e0c4386eSCy Schubert         custom_ext_error = 1;
550*e0c4386eSCy Schubert     /* Check for "abc" */
551*e0c4386eSCy Schubert     if (inlen != strlen(custom_ext_cli_string))
552*e0c4386eSCy Schubert         custom_ext_error = 1;
553*e0c4386eSCy Schubert     if (memcmp(in, custom_ext_cli_string, inlen) != 0)
554*e0c4386eSCy Schubert         custom_ext_error = 1;
555*e0c4386eSCy Schubert     return 1;
556*e0c4386eSCy Schubert }
557*e0c4386eSCy Schubert 
custom_ext_1_srv_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)558*e0c4386eSCy Schubert static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
559*e0c4386eSCy Schubert                                    const unsigned char **out,
560*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
561*e0c4386eSCy Schubert {
562*e0c4386eSCy Schubert     return 0;                   /* Don't send an extension */
563*e0c4386eSCy Schubert }
564*e0c4386eSCy Schubert 
custom_ext_2_srv_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)565*e0c4386eSCy Schubert static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
566*e0c4386eSCy Schubert                                      const unsigned char *in,
567*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
568*e0c4386eSCy Schubert {
569*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_2)
570*e0c4386eSCy Schubert         custom_ext_error = 1;
571*e0c4386eSCy Schubert     /* Check for "abc" */
572*e0c4386eSCy Schubert     if (inlen != strlen(custom_ext_cli_string))
573*e0c4386eSCy Schubert         custom_ext_error = 1;
574*e0c4386eSCy Schubert     if (memcmp(in, custom_ext_cli_string, inlen) != 0)
575*e0c4386eSCy Schubert         custom_ext_error = 1;
576*e0c4386eSCy Schubert     return 1;
577*e0c4386eSCy Schubert }
578*e0c4386eSCy Schubert 
custom_ext_2_srv_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)579*e0c4386eSCy Schubert static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
580*e0c4386eSCy Schubert                                    const unsigned char **out,
581*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
582*e0c4386eSCy Schubert {
583*e0c4386eSCy Schubert     *out = NULL;
584*e0c4386eSCy Schubert     *outlen = 0;
585*e0c4386eSCy Schubert     return 1;                   /* Send empty extension */
586*e0c4386eSCy Schubert }
587*e0c4386eSCy Schubert 
custom_ext_3_srv_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * arg)588*e0c4386eSCy Schubert static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
589*e0c4386eSCy Schubert                                      const unsigned char *in,
590*e0c4386eSCy Schubert                                      size_t inlen, int *al, void *arg)
591*e0c4386eSCy Schubert {
592*e0c4386eSCy Schubert     if (ext_type != CUSTOM_EXT_TYPE_3)
593*e0c4386eSCy Schubert         custom_ext_error = 1;
594*e0c4386eSCy Schubert     /* Check for "abc" */
595*e0c4386eSCy Schubert     if (inlen != strlen(custom_ext_cli_string))
596*e0c4386eSCy Schubert         custom_ext_error = 1;
597*e0c4386eSCy Schubert     if (memcmp(in, custom_ext_cli_string, inlen) != 0)
598*e0c4386eSCy Schubert         custom_ext_error = 1;
599*e0c4386eSCy Schubert     return 1;
600*e0c4386eSCy Schubert }
601*e0c4386eSCy Schubert 
custom_ext_3_srv_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * arg)602*e0c4386eSCy Schubert static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
603*e0c4386eSCy Schubert                                    const unsigned char **out,
604*e0c4386eSCy Schubert                                    size_t *outlen, int *al, void *arg)
605*e0c4386eSCy Schubert {
606*e0c4386eSCy Schubert     *out = (const unsigned char *)custom_ext_srv_string;
607*e0c4386eSCy Schubert     *outlen = strlen(custom_ext_srv_string);
608*e0c4386eSCy Schubert     return 1;                   /* Send "defg" */
609*e0c4386eSCy Schubert }
610*e0c4386eSCy Schubert 
611*e0c4386eSCy Schubert static char *cipher = NULL;
612*e0c4386eSCy Schubert static char *ciphersuites = NULL;
613*e0c4386eSCy Schubert static int verbose = 0;
614*e0c4386eSCy Schubert static int debug = 0;
615*e0c4386eSCy Schubert 
616*e0c4386eSCy Schubert int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
617*e0c4386eSCy Schubert                    long bytes, clock_t *s_time, clock_t *c_time);
618*e0c4386eSCy Schubert int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
619*e0c4386eSCy Schubert                  clock_t *c_time);
620*e0c4386eSCy Schubert int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
621*e0c4386eSCy Schubert 
sv_usage(void)622*e0c4386eSCy Schubert static void sv_usage(void)
623*e0c4386eSCy Schubert {
624*e0c4386eSCy Schubert     fprintf(stderr, "usage: ssltest [args ...]\n");
625*e0c4386eSCy Schubert     fprintf(stderr, "\n");
626*e0c4386eSCy Schubert     fprintf(stderr, " -server_auth  - check server certificate\n");
627*e0c4386eSCy Schubert     fprintf(stderr, " -client_auth  - do client authentication\n");
628*e0c4386eSCy Schubert     fprintf(stderr, " -v            - more output\n");
629*e0c4386eSCy Schubert     fprintf(stderr, " -d            - debug output\n");
630*e0c4386eSCy Schubert     fprintf(stderr, " -reuse        - use session-id reuse\n");
631*e0c4386eSCy Schubert     fprintf(stderr, " -num <val>    - number of connections to perform\n");
632*e0c4386eSCy Schubert     fprintf(stderr,
633*e0c4386eSCy Schubert             " -bytes <val>  - number of bytes to swap between client/server\n");
634*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
635*e0c4386eSCy Schubert     fprintf(stderr,
636*e0c4386eSCy Schubert             " -dhe512       - use 512 bit key for DHE (to test failure)\n");
637*e0c4386eSCy Schubert     fprintf(stderr,
638*e0c4386eSCy Schubert             " -dhe1024dsa   - use 1024 bit key (with 160-bit subprime) for DHE\n");
639*e0c4386eSCy Schubert     fprintf(stderr,
640*e0c4386eSCy Schubert             " -dhe2048      - use 2048 bit key (safe prime) for DHE (default, no-op)\n");
641*e0c4386eSCy Schubert     fprintf(stderr,
642*e0c4386eSCy Schubert             " -dhe4096      - use 4096 bit key (safe prime) for DHE\n");
643*e0c4386eSCy Schubert #endif
644*e0c4386eSCy Schubert     fprintf(stderr, " -no_dhe       - disable DHE\n");
645*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC
646*e0c4386eSCy Schubert     fprintf(stderr, " -no_ecdhe     - disable ECDHE\n");
647*e0c4386eSCy Schubert #endif
648*e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
649*e0c4386eSCy Schubert     fprintf(stderr, " -psk arg      - PSK in hex (without 0x)\n");
650*e0c4386eSCy Schubert #endif
651*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SSL3
652*e0c4386eSCy Schubert     fprintf(stderr, " -ssl3         - use SSLv3\n");
653*e0c4386eSCy Schubert #endif
654*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1
655*e0c4386eSCy Schubert     fprintf(stderr, " -tls1         - use TLSv1\n");
656*e0c4386eSCy Schubert #endif
657*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_1
658*e0c4386eSCy Schubert     fprintf(stderr, " -tls1_1       - use TLSv1.1\n");
659*e0c4386eSCy Schubert #endif
660*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
661*e0c4386eSCy Schubert     fprintf(stderr, " -tls1_2       - use TLSv1.2\n");
662*e0c4386eSCy Schubert #endif
663*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS
664*e0c4386eSCy Schubert     fprintf(stderr, " -dtls         - use DTLS\n");
665*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS1
666*e0c4386eSCy Schubert     fprintf(stderr, " -dtls1        - use DTLSv1\n");
667*e0c4386eSCy Schubert #endif
668*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS1_2
669*e0c4386eSCy Schubert     fprintf(stderr, " -dtls12       - use DTLSv1.2\n");
670*e0c4386eSCy Schubert #endif
671*e0c4386eSCy Schubert #endif
672*e0c4386eSCy Schubert     fprintf(stderr, " -CApath arg   - PEM format directory of CA's\n");
673*e0c4386eSCy Schubert     fprintf(stderr, " -CAfile arg   - PEM format file of CA's\n");
674*e0c4386eSCy Schubert     fprintf(stderr, " -s_cert arg   - Server certificate file\n");
675*e0c4386eSCy Schubert     fprintf(stderr,
676*e0c4386eSCy Schubert             " -s_key arg    - Server key file (default: same as -cert)\n");
677*e0c4386eSCy Schubert     fprintf(stderr, " -c_cert arg   - Client certificate file\n");
678*e0c4386eSCy Schubert     fprintf(stderr,
679*e0c4386eSCy Schubert             " -c_key arg    - Client key file (default: same as -c_cert)\n");
680*e0c4386eSCy Schubert     fprintf(stderr, " -cipher arg   - The TLSv1.2 and below cipher list\n");
681*e0c4386eSCy Schubert     fprintf(stderr, " -ciphersuites arg   - The TLSv1.3 ciphersuites\n");
682*e0c4386eSCy Schubert     fprintf(stderr, " -bio_pair     - Use BIO pairs\n");
683*e0c4386eSCy Schubert     fprintf(stderr, " -ipv4         - Use IPv4 connection on localhost\n");
684*e0c4386eSCy Schubert     fprintf(stderr, " -ipv6         - Use IPv6 connection on localhost\n");
685*e0c4386eSCy Schubert     fprintf(stderr, " -f            - Test even cases that can't work\n");
686*e0c4386eSCy Schubert     fprintf(stderr,
687*e0c4386eSCy Schubert             " -time         - measure processor time used by client and server\n");
688*e0c4386eSCy Schubert     fprintf(stderr, " -zlib         - use zlib compression\n");
689*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
690*e0c4386eSCy Schubert     fprintf(stderr, " -npn_client - have client side offer NPN\n");
691*e0c4386eSCy Schubert     fprintf(stderr, " -npn_server - have server side offer NPN\n");
692*e0c4386eSCy Schubert     fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
693*e0c4386eSCy Schubert #endif
694*e0c4386eSCy Schubert     fprintf(stderr, " -serverinfo_file file - have server use this file\n");
695*e0c4386eSCy Schubert     fprintf(stderr, " -serverinfo_sct  - have client offer and expect SCT\n");
696*e0c4386eSCy Schubert     fprintf(stderr,
697*e0c4386eSCy Schubert             " -serverinfo_tack - have client offer and expect TACK\n");
698*e0c4386eSCy Schubert     fprintf(stderr,
699*e0c4386eSCy Schubert             " -custom_ext - try various custom extension callbacks\n");
700*e0c4386eSCy Schubert     fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
701*e0c4386eSCy Schubert     fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
702*e0c4386eSCy Schubert     fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n");
703*e0c4386eSCy Schubert     fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
704*e0c4386eSCy Schubert     fprintf(stderr,
705*e0c4386eSCy Schubert             " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
706*e0c4386eSCy Schubert     fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
707*e0c4386eSCy Schubert     fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
708*e0c4386eSCy Schubert     fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
709*e0c4386eSCy Schubert     fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
710*e0c4386eSCy Schubert     fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
711*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CT
712*e0c4386eSCy Schubert     fprintf(stderr, " -noct         - no certificate transparency\n");
713*e0c4386eSCy Schubert     fprintf(stderr, " -requestct    - request certificate transparency\n");
714*e0c4386eSCy Schubert     fprintf(stderr, " -requirect    - require certificate transparency\n");
715*e0c4386eSCy Schubert #endif
716*e0c4386eSCy Schubert     fprintf(stderr, " -sn_client <string>  - have client request this servername\n");
717*e0c4386eSCy Schubert     fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n");
718*e0c4386eSCy Schubert     fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n");
719*e0c4386eSCy Schubert     fprintf(stderr, " -sn_expect1          - expected server 1\n");
720*e0c4386eSCy Schubert     fprintf(stderr, " -sn_expect2          - expected server 2\n");
721*e0c4386eSCy Schubert     fprintf(stderr, " -server_sess_out <file>    - Save the server session to a file\n");
722*e0c4386eSCy Schubert     fprintf(stderr, " -server_sess_in <file>     - Read the server session from a file\n");
723*e0c4386eSCy Schubert     fprintf(stderr, " -client_sess_out <file>    - Save the client session to a file\n");
724*e0c4386eSCy Schubert     fprintf(stderr, " -client_sess_in <file>     - Read the client session from a file\n");
725*e0c4386eSCy Schubert     fprintf(stderr, " -should_reuse <number>     - The expected state of reusing the session\n");
726*e0c4386eSCy Schubert     fprintf(stderr, " -no_ticket    - do not issue TLS session ticket\n");
727*e0c4386eSCy Schubert     fprintf(stderr, " -client_ktls  - try to enable client KTLS\n");
728*e0c4386eSCy Schubert     fprintf(stderr, " -server_ktls  - try to enable server KTLS\n");
729*e0c4386eSCy Schubert     fprintf(stderr, " -provider <name>    - Load the given provider into the library context\n");
730*e0c4386eSCy Schubert     fprintf(stderr, " -config <cnf>    - Load the given config file into the library context\n");
731*e0c4386eSCy Schubert }
732*e0c4386eSCy Schubert 
print_key_details(BIO * out,EVP_PKEY * key)733*e0c4386eSCy Schubert static void print_key_details(BIO *out, EVP_PKEY *key)
734*e0c4386eSCy Schubert {
735*e0c4386eSCy Schubert     int keyid = EVP_PKEY_get_id(key);
736*e0c4386eSCy Schubert 
737*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC
738*e0c4386eSCy Schubert     if (keyid == EVP_PKEY_EC) {
739*e0c4386eSCy Schubert         char group[80];
740*e0c4386eSCy Schubert         size_t size;
741*e0c4386eSCy Schubert 
742*e0c4386eSCy Schubert         if (!EVP_PKEY_get_group_name(key, group, sizeof(group), &size))
743*e0c4386eSCy Schubert             strcpy(group, "unknown group");
744*e0c4386eSCy Schubert         BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_get_bits(key), group);
745*e0c4386eSCy Schubert     } else
746*e0c4386eSCy Schubert #endif
747*e0c4386eSCy Schubert     {
748*e0c4386eSCy Schubert         const char *algname;
749*e0c4386eSCy Schubert         switch (keyid) {
750*e0c4386eSCy Schubert         case EVP_PKEY_RSA:
751*e0c4386eSCy Schubert             algname = "RSA";
752*e0c4386eSCy Schubert             break;
753*e0c4386eSCy Schubert         case EVP_PKEY_DSA:
754*e0c4386eSCy Schubert             algname = "DSA";
755*e0c4386eSCy Schubert             break;
756*e0c4386eSCy Schubert         case EVP_PKEY_DH:
757*e0c4386eSCy Schubert             algname = "DH";
758*e0c4386eSCy Schubert             break;
759*e0c4386eSCy Schubert         default:
760*e0c4386eSCy Schubert             algname = OBJ_nid2sn(keyid);
761*e0c4386eSCy Schubert             break;
762*e0c4386eSCy Schubert         }
763*e0c4386eSCy Schubert         BIO_printf(out, "%d bits %s", EVP_PKEY_get_bits(key), algname);
764*e0c4386eSCy Schubert     }
765*e0c4386eSCy Schubert }
766*e0c4386eSCy Schubert 
print_details(SSL * c_ssl,const char * prefix)767*e0c4386eSCy Schubert static void print_details(SSL *c_ssl, const char *prefix)
768*e0c4386eSCy Schubert {
769*e0c4386eSCy Schubert     const SSL_CIPHER *ciph;
770*e0c4386eSCy Schubert     int mdnid;
771*e0c4386eSCy Schubert     X509 *cert;
772*e0c4386eSCy Schubert     EVP_PKEY *pkey;
773*e0c4386eSCy Schubert 
774*e0c4386eSCy Schubert     ciph = SSL_get_current_cipher(c_ssl);
775*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "%s%s, cipher %s %s",
776*e0c4386eSCy Schubert                prefix,
777*e0c4386eSCy Schubert                SSL_get_version(c_ssl),
778*e0c4386eSCy Schubert                SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
779*e0c4386eSCy Schubert     cert = SSL_get0_peer_certificate(c_ssl);
780*e0c4386eSCy Schubert     if (cert != NULL) {
781*e0c4386eSCy Schubert         EVP_PKEY* pubkey = X509_get0_pubkey(cert);
782*e0c4386eSCy Schubert 
783*e0c4386eSCy Schubert         if (pubkey != NULL) {
784*e0c4386eSCy Schubert             BIO_puts(bio_stdout, ", ");
785*e0c4386eSCy Schubert             print_key_details(bio_stdout, pubkey);
786*e0c4386eSCy Schubert         }
787*e0c4386eSCy Schubert     }
788*e0c4386eSCy Schubert     if (SSL_get_peer_tmp_key(c_ssl, &pkey)) {
789*e0c4386eSCy Schubert         BIO_puts(bio_stdout, ", temp key: ");
790*e0c4386eSCy Schubert         print_key_details(bio_stdout, pkey);
791*e0c4386eSCy Schubert         EVP_PKEY_free(pkey);
792*e0c4386eSCy Schubert     }
793*e0c4386eSCy Schubert     if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
794*e0c4386eSCy Schubert         BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
795*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "\n");
796*e0c4386eSCy Schubert }
797*e0c4386eSCy Schubert 
798*e0c4386eSCy Schubert /*
799*e0c4386eSCy Schubert  * protocol_from_string - converts a protocol version string to a number
800*e0c4386eSCy Schubert  *
801*e0c4386eSCy Schubert  * Returns -1 on failure or the version on success
802*e0c4386eSCy Schubert  */
protocol_from_string(const char * value)803*e0c4386eSCy Schubert static int protocol_from_string(const char *value)
804*e0c4386eSCy Schubert {
805*e0c4386eSCy Schubert     struct protocol_versions {
806*e0c4386eSCy Schubert         const char *name;
807*e0c4386eSCy Schubert         int version;
808*e0c4386eSCy Schubert     };
809*e0c4386eSCy Schubert     static const struct protocol_versions versions[] = {
810*e0c4386eSCy Schubert         {"ssl3", SSL3_VERSION},
811*e0c4386eSCy Schubert         {"tls1", TLS1_VERSION},
812*e0c4386eSCy Schubert         {"tls1.1", TLS1_1_VERSION},
813*e0c4386eSCy Schubert         {"tls1.2", TLS1_2_VERSION},
814*e0c4386eSCy Schubert         {"tls1.3", TLS1_3_VERSION},
815*e0c4386eSCy Schubert         {"dtls1", DTLS1_VERSION},
816*e0c4386eSCy Schubert         {"dtls1.2", DTLS1_2_VERSION}};
817*e0c4386eSCy Schubert     size_t i;
818*e0c4386eSCy Schubert     size_t n = OSSL_NELEM(versions);
819*e0c4386eSCy Schubert 
820*e0c4386eSCy Schubert     for (i = 0; i < n; i++)
821*e0c4386eSCy Schubert         if (strcmp(versions[i].name, value) == 0)
822*e0c4386eSCy Schubert             return versions[i].version;
823*e0c4386eSCy Schubert     return -1;
824*e0c4386eSCy Schubert }
825*e0c4386eSCy Schubert 
read_session(const char * filename)826*e0c4386eSCy Schubert static SSL_SESSION *read_session(const char *filename)
827*e0c4386eSCy Schubert {
828*e0c4386eSCy Schubert     SSL_SESSION *sess;
829*e0c4386eSCy Schubert     BIO *f = BIO_new_file(filename, "r");
830*e0c4386eSCy Schubert 
831*e0c4386eSCy Schubert     if (f == NULL) {
832*e0c4386eSCy Schubert         BIO_printf(bio_err, "Can't open session file %s\n", filename);
833*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
834*e0c4386eSCy Schubert         return NULL;
835*e0c4386eSCy Schubert     }
836*e0c4386eSCy Schubert     sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL);
837*e0c4386eSCy Schubert     if (sess == NULL) {
838*e0c4386eSCy Schubert         BIO_printf(bio_err, "Can't parse session file %s\n", filename);
839*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
840*e0c4386eSCy Schubert     }
841*e0c4386eSCy Schubert     BIO_free(f);
842*e0c4386eSCy Schubert     return sess;
843*e0c4386eSCy Schubert }
844*e0c4386eSCy Schubert 
write_session(const char * filename,SSL_SESSION * sess)845*e0c4386eSCy Schubert static int write_session(const char *filename, SSL_SESSION *sess)
846*e0c4386eSCy Schubert {
847*e0c4386eSCy Schubert     BIO *f;
848*e0c4386eSCy Schubert 
849*e0c4386eSCy Schubert     if (sess == NULL) {
850*e0c4386eSCy Schubert         BIO_printf(bio_err, "No session information\n");
851*e0c4386eSCy Schubert         return 0;
852*e0c4386eSCy Schubert     }
853*e0c4386eSCy Schubert 
854*e0c4386eSCy Schubert     f = BIO_new_file(filename, "w");
855*e0c4386eSCy Schubert     if (f == NULL) {
856*e0c4386eSCy Schubert         BIO_printf(bio_err, "Can't open session file %s\n", filename);
857*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
858*e0c4386eSCy Schubert         return 0;
859*e0c4386eSCy Schubert     }
860*e0c4386eSCy Schubert     PEM_write_bio_SSL_SESSION(f, sess);
861*e0c4386eSCy Schubert     BIO_free(f);
862*e0c4386eSCy Schubert     return 1;
863*e0c4386eSCy Schubert }
864*e0c4386eSCy Schubert 
865*e0c4386eSCy Schubert /*
866*e0c4386eSCy Schubert  * set_protocol_version - Sets protocol version minimum or maximum
867*e0c4386eSCy Schubert  *
868*e0c4386eSCy Schubert  * Returns 0 on failure and 1 on success
869*e0c4386eSCy Schubert  */
set_protocol_version(const char * version,SSL * ssl,int setting)870*e0c4386eSCy Schubert static int set_protocol_version(const char *version, SSL *ssl, int setting)
871*e0c4386eSCy Schubert {
872*e0c4386eSCy Schubert     if (version != NULL) {
873*e0c4386eSCy Schubert         int ver = protocol_from_string(version);
874*e0c4386eSCy Schubert         if (ver < 0) {
875*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error parsing: %s\n", version);
876*e0c4386eSCy Schubert             return 0;
877*e0c4386eSCy Schubert         }
878*e0c4386eSCy Schubert         return SSL_ctrl(ssl, setting, ver, NULL);
879*e0c4386eSCy Schubert     }
880*e0c4386eSCy Schubert     return 1;
881*e0c4386eSCy Schubert }
882*e0c4386eSCy Schubert 
main(int argc,char * argv[])883*e0c4386eSCy Schubert int main(int argc, char *argv[])
884*e0c4386eSCy Schubert {
885*e0c4386eSCy Schubert     const char *CApath = NULL, *CAfile = NULL;
886*e0c4386eSCy Schubert     int badop = 0;
887*e0c4386eSCy Schubert     enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM;
888*e0c4386eSCy Schubert     int force = 0;
889*e0c4386eSCy Schubert     int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_1 = 0, tls1_2 = 0, ssl3 = 0;
890*e0c4386eSCy Schubert     int ret = EXIT_FAILURE;
891*e0c4386eSCy Schubert     int client_auth = 0;
892*e0c4386eSCy Schubert     int server_auth = 0, i;
893*e0c4386eSCy Schubert     struct app_verify_arg app_verify_arg =
894*e0c4386eSCy Schubert         { APP_CALLBACK_STRING, 0 };
895*e0c4386eSCy Schubert     SSL_CTX *c_ctx = NULL;
896*e0c4386eSCy Schubert     const SSL_METHOD *meth = NULL;
897*e0c4386eSCy Schubert     SSL *c_ssl = NULL;
898*e0c4386eSCy Schubert     SSL *s_ssl = NULL;
899*e0c4386eSCy Schubert     int number = 1, reuse = 0;
900*e0c4386eSCy Schubert     int should_reuse = -1;
901*e0c4386eSCy Schubert     int no_ticket = 0;
902*e0c4386eSCy Schubert     int client_ktls = 0, server_ktls = 0;
903*e0c4386eSCy Schubert     long bytes = 256L;
904*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
905*e0c4386eSCy Schubert     EVP_PKEY *dhpkey;
906*e0c4386eSCy Schubert     int dhe512 = 0, dhe1024dsa = 0, dhe4096 = 0;
907*e0c4386eSCy Schubert     int no_dhe = 0;
908*e0c4386eSCy Schubert #endif
909*e0c4386eSCy Schubert     int no_psk = 0;
910*e0c4386eSCy Schubert     int print_time = 0;
911*e0c4386eSCy Schubert     clock_t s_time = 0, c_time = 0;
912*e0c4386eSCy Schubert #ifndef OPENSSL_NO_COMP
913*e0c4386eSCy Schubert     int n, comp = 0;
914*e0c4386eSCy Schubert     COMP_METHOD *cm = NULL;
915*e0c4386eSCy Schubert     STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
916*e0c4386eSCy Schubert #endif
917*e0c4386eSCy Schubert     int no_protocol;
918*e0c4386eSCy Schubert     int min_version = 0, max_version = 0;
919*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CT
920*e0c4386eSCy Schubert     /*
921*e0c4386eSCy Schubert      * Disable CT validation by default, because it will interfere with
922*e0c4386eSCy Schubert      * anything using custom extension handlers to deal with SCT extensions.
923*e0c4386eSCy Schubert      */
924*e0c4386eSCy Schubert     int ct_validation = 0;
925*e0c4386eSCy Schubert #endif
926*e0c4386eSCy Schubert     SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL;
927*e0c4386eSCy Schubert     STACK_OF(OPENSSL_STRING) *conf_args = NULL;
928*e0c4386eSCy Schubert     char *arg = NULL, *argn = NULL;
929*e0c4386eSCy Schubert     const char *provider = NULL, *config = NULL;
930*e0c4386eSCy Schubert     OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL;
931*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx = NULL;
932*e0c4386eSCy Schubert 
933*e0c4386eSCy Schubert     verbose = 0;
934*e0c4386eSCy Schubert     debug = 0;
935*e0c4386eSCy Schubert 
936*e0c4386eSCy Schubert     bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
937*e0c4386eSCy Schubert     bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
938*e0c4386eSCy Schubert 
939*e0c4386eSCy Schubert     s_cctx = SSL_CONF_CTX_new();
940*e0c4386eSCy Schubert     s_cctx2 = SSL_CONF_CTX_new();
941*e0c4386eSCy Schubert     c_cctx = SSL_CONF_CTX_new();
942*e0c4386eSCy Schubert 
943*e0c4386eSCy Schubert     if (!s_cctx || !c_cctx || !s_cctx2) {
944*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
945*e0c4386eSCy Schubert         goto end;
946*e0c4386eSCy Schubert     }
947*e0c4386eSCy Schubert 
948*e0c4386eSCy Schubert     SSL_CONF_CTX_set_flags(s_cctx,
949*e0c4386eSCy Schubert                            SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
950*e0c4386eSCy Schubert                            SSL_CONF_FLAG_CERTIFICATE |
951*e0c4386eSCy Schubert                            SSL_CONF_FLAG_REQUIRE_PRIVATE);
952*e0c4386eSCy Schubert     SSL_CONF_CTX_set_flags(s_cctx2,
953*e0c4386eSCy Schubert                            SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
954*e0c4386eSCy Schubert                            SSL_CONF_FLAG_CERTIFICATE |
955*e0c4386eSCy Schubert                            SSL_CONF_FLAG_REQUIRE_PRIVATE);
956*e0c4386eSCy Schubert     if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
957*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
958*e0c4386eSCy Schubert         goto end;
959*e0c4386eSCy Schubert     }
960*e0c4386eSCy Schubert     if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) {
961*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
962*e0c4386eSCy Schubert         goto end;
963*e0c4386eSCy Schubert     }
964*e0c4386eSCy Schubert 
965*e0c4386eSCy Schubert     SSL_CONF_CTX_set_flags(c_cctx,
966*e0c4386eSCy Schubert                            SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT |
967*e0c4386eSCy Schubert                            SSL_CONF_FLAG_CERTIFICATE |
968*e0c4386eSCy Schubert                            SSL_CONF_FLAG_REQUIRE_PRIVATE);
969*e0c4386eSCy Schubert     if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
970*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
971*e0c4386eSCy Schubert         goto end;
972*e0c4386eSCy Schubert     }
973*e0c4386eSCy Schubert 
974*e0c4386eSCy Schubert     argc--;
975*e0c4386eSCy Schubert     argv++;
976*e0c4386eSCy Schubert 
977*e0c4386eSCy Schubert     while (argc >= 1) {
978*e0c4386eSCy Schubert         if (strcmp(*argv, "-F") == 0) {
979*e0c4386eSCy Schubert             fprintf(stderr,
980*e0c4386eSCy Schubert                     "not compiled with FIPS support, so exiting without running.\n");
981*e0c4386eSCy Schubert             EXIT(0);
982*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-server_auth") == 0)
983*e0c4386eSCy Schubert             server_auth = 1;
984*e0c4386eSCy Schubert         else if (strcmp(*argv, "-client_auth") == 0)
985*e0c4386eSCy Schubert             client_auth = 1;
986*e0c4386eSCy Schubert         else if (strcmp(*argv, "-v") == 0)
987*e0c4386eSCy Schubert             verbose = 1;
988*e0c4386eSCy Schubert         else if (strcmp(*argv, "-d") == 0)
989*e0c4386eSCy Schubert             debug = 1;
990*e0c4386eSCy Schubert         else if (strcmp(*argv, "-reuse") == 0)
991*e0c4386eSCy Schubert             reuse = 1;
992*e0c4386eSCy Schubert         else if (strcmp(*argv, "-no_dhe") == 0)
993*e0c4386eSCy Schubert #ifdef OPENSSL_NO_DH
994*e0c4386eSCy Schubert             /* unused in this case */;
995*e0c4386eSCy Schubert #else
996*e0c4386eSCy Schubert             no_dhe = 1;
997*e0c4386eSCy Schubert         else if (strcmp(*argv, "-dhe512") == 0)
998*e0c4386eSCy Schubert             dhe512 = 1;
999*e0c4386eSCy Schubert         else if (strcmp(*argv, "-dhe1024dsa") == 0)
1000*e0c4386eSCy Schubert             dhe1024dsa = 1;
1001*e0c4386eSCy Schubert         else if (strcmp(*argv, "-dhe4096") == 0)
1002*e0c4386eSCy Schubert             dhe4096 = 1;
1003*e0c4386eSCy Schubert #endif
1004*e0c4386eSCy Schubert         else if (strcmp(*argv, "-no_ecdhe") == 0)
1005*e0c4386eSCy Schubert             /* obsolete */;
1006*e0c4386eSCy Schubert         else if (strcmp(*argv, "-psk") == 0) {
1007*e0c4386eSCy Schubert             if (--argc < 1)
1008*e0c4386eSCy Schubert                 goto bad;
1009*e0c4386eSCy Schubert             psk_key = *(++argv);
1010*e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
1011*e0c4386eSCy Schubert             if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) {
1012*e0c4386eSCy Schubert                 BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
1013*e0c4386eSCy Schubert                 goto bad;
1014*e0c4386eSCy Schubert             }
1015*e0c4386eSCy Schubert #else
1016*e0c4386eSCy Schubert             no_psk = 1;
1017*e0c4386eSCy Schubert #endif
1018*e0c4386eSCy Schubert         }
1019*e0c4386eSCy Schubert         else if (strcmp(*argv, "-tls1_2") == 0) {
1020*e0c4386eSCy Schubert             tls1_2 = 1;
1021*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-tls1_1") == 0) {
1022*e0c4386eSCy Schubert             tls1_1 = 1;
1023*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-tls1") == 0) {
1024*e0c4386eSCy Schubert             tls1 = 1;
1025*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-ssl3") == 0) {
1026*e0c4386eSCy Schubert             ssl3 = 1;
1027*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-dtls1") == 0) {
1028*e0c4386eSCy Schubert             dtls1 = 1;
1029*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-dtls12") == 0) {
1030*e0c4386eSCy Schubert             dtls12 = 1;
1031*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-dtls") == 0) {
1032*e0c4386eSCy Schubert             dtls = 1;
1033*e0c4386eSCy Schubert         } else if (strncmp(*argv, "-num", 4) == 0) {
1034*e0c4386eSCy Schubert             if (--argc < 1)
1035*e0c4386eSCy Schubert                 goto bad;
1036*e0c4386eSCy Schubert             number = atoi(*(++argv));
1037*e0c4386eSCy Schubert             if (number == 0)
1038*e0c4386eSCy Schubert                 number = 1;
1039*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-bytes") == 0) {
1040*e0c4386eSCy Schubert             if (--argc < 1)
1041*e0c4386eSCy Schubert                 goto bad;
1042*e0c4386eSCy Schubert             bytes = atol(*(++argv));
1043*e0c4386eSCy Schubert             if (bytes == 0L)
1044*e0c4386eSCy Schubert                 bytes = 1L;
1045*e0c4386eSCy Schubert             i = strlen(argv[0]);
1046*e0c4386eSCy Schubert             if (argv[0][i - 1] == 'k')
1047*e0c4386eSCy Schubert                 bytes *= 1024L;
1048*e0c4386eSCy Schubert             if (argv[0][i - 1] == 'm')
1049*e0c4386eSCy Schubert                 bytes *= 1024L * 1024L;
1050*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-cipher") == 0) {
1051*e0c4386eSCy Schubert             if (--argc < 1)
1052*e0c4386eSCy Schubert                 goto bad;
1053*e0c4386eSCy Schubert             cipher = *(++argv);
1054*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-ciphersuites") == 0) {
1055*e0c4386eSCy Schubert             if (--argc < 1)
1056*e0c4386eSCy Schubert                 goto bad;
1057*e0c4386eSCy Schubert             ciphersuites = *(++argv);
1058*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-CApath") == 0) {
1059*e0c4386eSCy Schubert             if (--argc < 1)
1060*e0c4386eSCy Schubert                 goto bad;
1061*e0c4386eSCy Schubert             CApath = *(++argv);
1062*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-CAfile") == 0) {
1063*e0c4386eSCy Schubert             if (--argc < 1)
1064*e0c4386eSCy Schubert                 goto bad;
1065*e0c4386eSCy Schubert             CAfile = *(++argv);
1066*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-bio_pair") == 0) {
1067*e0c4386eSCy Schubert             bio_type = BIO_PAIR;
1068*e0c4386eSCy Schubert         }
1069*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SOCK
1070*e0c4386eSCy Schubert         else if (strcmp(*argv, "-ipv4") == 0) {
1071*e0c4386eSCy Schubert             bio_type = BIO_IPV4;
1072*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-ipv6") == 0) {
1073*e0c4386eSCy Schubert             bio_type = BIO_IPV6;
1074*e0c4386eSCy Schubert         }
1075*e0c4386eSCy Schubert #endif
1076*e0c4386eSCy Schubert         else if (strcmp(*argv, "-f") == 0) {
1077*e0c4386eSCy Schubert             force = 1;
1078*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-time") == 0) {
1079*e0c4386eSCy Schubert             print_time = 1;
1080*e0c4386eSCy Schubert         }
1081*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CT
1082*e0c4386eSCy Schubert         else if (strcmp(*argv, "-noct") == 0) {
1083*e0c4386eSCy Schubert             ct_validation = 0;
1084*e0c4386eSCy Schubert         }
1085*e0c4386eSCy Schubert         else if (strcmp(*argv, "-ct") == 0) {
1086*e0c4386eSCy Schubert             ct_validation = 1;
1087*e0c4386eSCy Schubert         }
1088*e0c4386eSCy Schubert #endif
1089*e0c4386eSCy Schubert #ifndef OPENSSL_NO_COMP
1090*e0c4386eSCy Schubert         else if (strcmp(*argv, "-zlib") == 0) {
1091*e0c4386eSCy Schubert             comp = COMP_ZLIB;
1092*e0c4386eSCy Schubert         }
1093*e0c4386eSCy Schubert #endif
1094*e0c4386eSCy Schubert         else if (strcmp(*argv, "-app_verify") == 0) {
1095*e0c4386eSCy Schubert             app_verify_arg.app_verify = 1;
1096*e0c4386eSCy Schubert         }
1097*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
1098*e0c4386eSCy Schubert           else if (strcmp(*argv, "-npn_client") == 0) {
1099*e0c4386eSCy Schubert             npn_client = 1;
1100*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-npn_server") == 0) {
1101*e0c4386eSCy Schubert             npn_server = 1;
1102*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-npn_server_reject") == 0) {
1103*e0c4386eSCy Schubert             npn_server_reject = 1;
1104*e0c4386eSCy Schubert         }
1105*e0c4386eSCy Schubert #endif
1106*e0c4386eSCy Schubert         else if (strcmp(*argv, "-serverinfo_sct") == 0) {
1107*e0c4386eSCy Schubert             serverinfo_sct = 1;
1108*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-serverinfo_tack") == 0) {
1109*e0c4386eSCy Schubert             serverinfo_tack = 1;
1110*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-serverinfo_file") == 0) {
1111*e0c4386eSCy Schubert             if (--argc < 1)
1112*e0c4386eSCy Schubert                 goto bad;
1113*e0c4386eSCy Schubert             serverinfo_file = *(++argv);
1114*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-custom_ext") == 0) {
1115*e0c4386eSCy Schubert             custom_ext = 1;
1116*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-alpn_client") == 0) {
1117*e0c4386eSCy Schubert             if (--argc < 1)
1118*e0c4386eSCy Schubert                 goto bad;
1119*e0c4386eSCy Schubert             alpn_client = *(++argv);
1120*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-alpn_server") == 0 ||
1121*e0c4386eSCy Schubert                    strcmp(*argv, "-alpn_server1") == 0) {
1122*e0c4386eSCy Schubert             if (--argc < 1)
1123*e0c4386eSCy Schubert                 goto bad;
1124*e0c4386eSCy Schubert             alpn_server = *(++argv);
1125*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-alpn_server2") == 0) {
1126*e0c4386eSCy Schubert             if (--argc < 1)
1127*e0c4386eSCy Schubert                 goto bad;
1128*e0c4386eSCy Schubert             alpn_server2 = *(++argv);
1129*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-alpn_expected") == 0) {
1130*e0c4386eSCy Schubert             if (--argc < 1)
1131*e0c4386eSCy Schubert                 goto bad;
1132*e0c4386eSCy Schubert             alpn_expected = *(++argv);
1133*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-server_min_proto") == 0) {
1134*e0c4386eSCy Schubert             if (--argc < 1)
1135*e0c4386eSCy Schubert                 goto bad;
1136*e0c4386eSCy Schubert             server_min_proto = *(++argv);
1137*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-server_max_proto") == 0) {
1138*e0c4386eSCy Schubert             if (--argc < 1)
1139*e0c4386eSCy Schubert                 goto bad;
1140*e0c4386eSCy Schubert             server_max_proto = *(++argv);
1141*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-client_min_proto") == 0) {
1142*e0c4386eSCy Schubert             if (--argc < 1)
1143*e0c4386eSCy Schubert                 goto bad;
1144*e0c4386eSCy Schubert             client_min_proto = *(++argv);
1145*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-client_max_proto") == 0) {
1146*e0c4386eSCy Schubert             if (--argc < 1)
1147*e0c4386eSCy Schubert                 goto bad;
1148*e0c4386eSCy Schubert             client_max_proto = *(++argv);
1149*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-should_negotiate") == 0) {
1150*e0c4386eSCy Schubert             if (--argc < 1)
1151*e0c4386eSCy Schubert                 goto bad;
1152*e0c4386eSCy Schubert             should_negotiate = *(++argv);
1153*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-sn_client") == 0) {
1154*e0c4386eSCy Schubert             if (--argc < 1)
1155*e0c4386eSCy Schubert                 goto bad;
1156*e0c4386eSCy Schubert             sn_client = *(++argv);
1157*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-sn_server1") == 0) {
1158*e0c4386eSCy Schubert             if (--argc < 1)
1159*e0c4386eSCy Schubert                 goto bad;
1160*e0c4386eSCy Schubert             sn_server1 = *(++argv);
1161*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-sn_server2") == 0) {
1162*e0c4386eSCy Schubert             if (--argc < 1)
1163*e0c4386eSCy Schubert                 goto bad;
1164*e0c4386eSCy Schubert             sn_server2 = *(++argv);
1165*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-sn_expect1") == 0) {
1166*e0c4386eSCy Schubert             sn_expect = 1;
1167*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-sn_expect2") == 0) {
1168*e0c4386eSCy Schubert             sn_expect = 2;
1169*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-server_sess_out") == 0) {
1170*e0c4386eSCy Schubert             if (--argc < 1)
1171*e0c4386eSCy Schubert                 goto bad;
1172*e0c4386eSCy Schubert             server_sess_out = *(++argv);
1173*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-server_sess_in") == 0) {
1174*e0c4386eSCy Schubert             if (--argc < 1)
1175*e0c4386eSCy Schubert                 goto bad;
1176*e0c4386eSCy Schubert             server_sess_in = *(++argv);
1177*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-client_sess_out") == 0) {
1178*e0c4386eSCy Schubert             if (--argc < 1)
1179*e0c4386eSCy Schubert                 goto bad;
1180*e0c4386eSCy Schubert             client_sess_out = *(++argv);
1181*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-client_sess_in") == 0) {
1182*e0c4386eSCy Schubert             if (--argc < 1)
1183*e0c4386eSCy Schubert                 goto bad;
1184*e0c4386eSCy Schubert             client_sess_in = *(++argv);
1185*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-should_reuse") == 0) {
1186*e0c4386eSCy Schubert             if (--argc < 1)
1187*e0c4386eSCy Schubert                 goto bad;
1188*e0c4386eSCy Schubert             should_reuse = !!atoi(*(++argv));
1189*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-no_ticket") == 0) {
1190*e0c4386eSCy Schubert             no_ticket = 1;
1191*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-client_ktls") == 0) {
1192*e0c4386eSCy Schubert             client_ktls = 1;
1193*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-server_ktls") == 0) {
1194*e0c4386eSCy Schubert             server_ktls = 1;
1195*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-provider") == 0) {
1196*e0c4386eSCy Schubert             if (--argc < 1)
1197*e0c4386eSCy Schubert                 goto bad;
1198*e0c4386eSCy Schubert             provider = *(++argv);
1199*e0c4386eSCy Schubert         } else if (strcmp(*argv, "-config") == 0) {
1200*e0c4386eSCy Schubert             if (--argc < 1)
1201*e0c4386eSCy Schubert                 goto bad;
1202*e0c4386eSCy Schubert             config = *(++argv);
1203*e0c4386eSCy Schubert         } else {
1204*e0c4386eSCy Schubert             int rv;
1205*e0c4386eSCy Schubert             arg = argv[0];
1206*e0c4386eSCy Schubert             argn = argv[1];
1207*e0c4386eSCy Schubert             /* Try to process command using SSL_CONF */
1208*e0c4386eSCy Schubert             rv = SSL_CONF_cmd_argv(c_cctx, &argc, &argv);
1209*e0c4386eSCy Schubert             /* If not processed try server */
1210*e0c4386eSCy Schubert             if (rv == 0)
1211*e0c4386eSCy Schubert                 rv = SSL_CONF_cmd_argv(s_cctx, &argc, &argv);
1212*e0c4386eSCy Schubert             /* Recognised: store it for later use */
1213*e0c4386eSCy Schubert             if (rv > 0) {
1214*e0c4386eSCy Schubert                 if (rv == 1)
1215*e0c4386eSCy Schubert                     argn = NULL;
1216*e0c4386eSCy Schubert                 if (!conf_args) {
1217*e0c4386eSCy Schubert                     conf_args = sk_OPENSSL_STRING_new_null();
1218*e0c4386eSCy Schubert                     if (!conf_args)
1219*e0c4386eSCy Schubert                         goto end;
1220*e0c4386eSCy Schubert                 }
1221*e0c4386eSCy Schubert                 if (!sk_OPENSSL_STRING_push(conf_args, arg))
1222*e0c4386eSCy Schubert                     goto end;
1223*e0c4386eSCy Schubert                 if (!sk_OPENSSL_STRING_push(conf_args, argn))
1224*e0c4386eSCy Schubert                     goto end;
1225*e0c4386eSCy Schubert                 continue;
1226*e0c4386eSCy Schubert             }
1227*e0c4386eSCy Schubert             if (rv == -3)
1228*e0c4386eSCy Schubert                 BIO_printf(bio_err, "Missing argument for %s\n", arg);
1229*e0c4386eSCy Schubert             else if (rv < 0)
1230*e0c4386eSCy Schubert                 BIO_printf(bio_err, "Error with command %s\n", arg);
1231*e0c4386eSCy Schubert             else if (rv == 0)
1232*e0c4386eSCy Schubert                 BIO_printf(bio_err, "unknown option %s\n", arg);
1233*e0c4386eSCy Schubert             badop = 1;
1234*e0c4386eSCy Schubert             break;
1235*e0c4386eSCy Schubert         }
1236*e0c4386eSCy Schubert         argc--;
1237*e0c4386eSCy Schubert         argv++;
1238*e0c4386eSCy Schubert     }
1239*e0c4386eSCy Schubert     if (badop) {
1240*e0c4386eSCy Schubert  bad:
1241*e0c4386eSCy Schubert         sv_usage();
1242*e0c4386eSCy Schubert         goto end;
1243*e0c4386eSCy Schubert     }
1244*e0c4386eSCy Schubert 
1245*e0c4386eSCy Schubert     if (ssl3 + tls1 + tls1_1 + tls1_2 + dtls + dtls1 + dtls12 > 1) {
1246*e0c4386eSCy Schubert         fprintf(stderr, "At most one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1 or -dtls12 should "
1247*e0c4386eSCy Schubert                 "be requested.\n");
1248*e0c4386eSCy Schubert         EXIT(1);
1249*e0c4386eSCy Schubert     }
1250*e0c4386eSCy Schubert 
1251*e0c4386eSCy Schubert #ifdef OPENSSL_NO_SSL3
1252*e0c4386eSCy Schubert     if (ssl3)
1253*e0c4386eSCy Schubert         no_protocol = 1;
1254*e0c4386eSCy Schubert     else
1255*e0c4386eSCy Schubert #endif
1256*e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1
1257*e0c4386eSCy Schubert     if (tls1)
1258*e0c4386eSCy Schubert         no_protocol = 1;
1259*e0c4386eSCy Schubert     else
1260*e0c4386eSCy Schubert #endif
1261*e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_1
1262*e0c4386eSCy Schubert     if (tls1_1)
1263*e0c4386eSCy Schubert         no_protocol = 1;
1264*e0c4386eSCy Schubert     else
1265*e0c4386eSCy Schubert #endif
1266*e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
1267*e0c4386eSCy Schubert     if (tls1_2)
1268*e0c4386eSCy Schubert         no_protocol = 1;
1269*e0c4386eSCy Schubert     else
1270*e0c4386eSCy Schubert #endif
1271*e0c4386eSCy Schubert #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1)
1272*e0c4386eSCy Schubert     if (dtls1)
1273*e0c4386eSCy Schubert         no_protocol = 1;
1274*e0c4386eSCy Schubert     else
1275*e0c4386eSCy Schubert #endif
1276*e0c4386eSCy Schubert #if defined(OPENSSL_NO_DTLS) || defined(OPENSSL_NO_DTLS1_2)
1277*e0c4386eSCy Schubert     if (dtls12)
1278*e0c4386eSCy Schubert         no_protocol = 1;
1279*e0c4386eSCy Schubert     else
1280*e0c4386eSCy Schubert #endif
1281*e0c4386eSCy Schubert         no_protocol = 0;
1282*e0c4386eSCy Schubert 
1283*e0c4386eSCy Schubert     /*
1284*e0c4386eSCy Schubert      * Testing was requested for a compiled-out protocol (e.g. SSLv3).
1285*e0c4386eSCy Schubert      * Ideally, we would error out, but the generic test wrapper can't know
1286*e0c4386eSCy Schubert      * when to expect failure. So we do nothing and return success.
1287*e0c4386eSCy Schubert      */
1288*e0c4386eSCy Schubert     if (no_protocol) {
1289*e0c4386eSCy Schubert         fprintf(stderr, "Testing was requested for a disabled protocol. "
1290*e0c4386eSCy Schubert                 "Skipping tests.\n");
1291*e0c4386eSCy Schubert         ret = EXIT_SUCCESS;
1292*e0c4386eSCy Schubert         goto end;
1293*e0c4386eSCy Schubert     }
1294*e0c4386eSCy Schubert 
1295*e0c4386eSCy Schubert     if (!ssl3 && !tls1 && !tls1_1 && !tls1_2 && !dtls && !dtls1 && !dtls12 && number > 1
1296*e0c4386eSCy Schubert             && !reuse && !force) {
1297*e0c4386eSCy Schubert         fprintf(stderr, "This case cannot work.  Use -f to perform "
1298*e0c4386eSCy Schubert                 "the test anyway (and\n-d to see what happens), "
1299*e0c4386eSCy Schubert                 "or add one of -ssl3, -tls1, -tls1_1, -tls1_2, -dtls, -dtls1, -dtls12, -reuse\n"
1300*e0c4386eSCy Schubert                 "to avoid protocol mismatch.\n");
1301*e0c4386eSCy Schubert         EXIT(1);
1302*e0c4386eSCy Schubert     }
1303*e0c4386eSCy Schubert 
1304*e0c4386eSCy Schubert     if (print_time) {
1305*e0c4386eSCy Schubert         if (bio_type == BIO_MEM) {
1306*e0c4386eSCy Schubert             fprintf(stderr, "Using BIO pair (-bio_pair)\n");
1307*e0c4386eSCy Schubert             bio_type = BIO_PAIR;
1308*e0c4386eSCy Schubert         }
1309*e0c4386eSCy Schubert         if (number < 50 && !force)
1310*e0c4386eSCy Schubert             fprintf(stderr,
1311*e0c4386eSCy Schubert                     "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
1312*e0c4386eSCy Schubert     }
1313*e0c4386eSCy Schubert 
1314*e0c4386eSCy Schubert #ifndef OPENSSL_NO_COMP
1315*e0c4386eSCy Schubert     if (comp == COMP_ZLIB)
1316*e0c4386eSCy Schubert         cm = COMP_zlib();
1317*e0c4386eSCy Schubert     if (cm != NULL) {
1318*e0c4386eSCy Schubert         if (COMP_get_type(cm) != NID_undef) {
1319*e0c4386eSCy Schubert             if (SSL_COMP_add_compression_method(comp, cm) != 0) {
1320*e0c4386eSCy Schubert                 fprintf(stderr, "Failed to add compression method\n");
1321*e0c4386eSCy Schubert                 ERR_print_errors_fp(stderr);
1322*e0c4386eSCy Schubert             }
1323*e0c4386eSCy Schubert         } else {
1324*e0c4386eSCy Schubert             fprintf(stderr,
1325*e0c4386eSCy Schubert                     "Warning: %s compression not supported\n",
1326*e0c4386eSCy Schubert                     comp == COMP_ZLIB ? "zlib" : "unknown");
1327*e0c4386eSCy Schubert             ERR_print_errors_fp(stderr);
1328*e0c4386eSCy Schubert         }
1329*e0c4386eSCy Schubert     }
1330*e0c4386eSCy Schubert     ssl_comp_methods = SSL_COMP_get_compression_methods();
1331*e0c4386eSCy Schubert     n = sk_SSL_COMP_num(ssl_comp_methods);
1332*e0c4386eSCy Schubert     if (n) {
1333*e0c4386eSCy Schubert         int j;
1334*e0c4386eSCy Schubert         printf("Available compression methods:");
1335*e0c4386eSCy Schubert         for (j = 0; j < n; j++) {
1336*e0c4386eSCy Schubert             SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
1337*e0c4386eSCy Schubert             printf("  %s:%d", SSL_COMP_get0_name(c), SSL_COMP_get_id(c));
1338*e0c4386eSCy Schubert         }
1339*e0c4386eSCy Schubert         printf("\n");
1340*e0c4386eSCy Schubert     }
1341*e0c4386eSCy Schubert #endif
1342*e0c4386eSCy Schubert 
1343*e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS
1344*e0c4386eSCy Schubert     meth = TLS_method();
1345*e0c4386eSCy Schubert     if (ssl3) {
1346*e0c4386eSCy Schubert         min_version = SSL3_VERSION;
1347*e0c4386eSCy Schubert         max_version = SSL3_VERSION;
1348*e0c4386eSCy Schubert     } else if (tls1) {
1349*e0c4386eSCy Schubert         min_version = TLS1_VERSION;
1350*e0c4386eSCy Schubert         max_version = TLS1_VERSION;
1351*e0c4386eSCy Schubert     } else if (tls1_1) {
1352*e0c4386eSCy Schubert         min_version = TLS1_1_VERSION;
1353*e0c4386eSCy Schubert         max_version = TLS1_1_VERSION;
1354*e0c4386eSCy Schubert     } else if (tls1_2) {
1355*e0c4386eSCy Schubert         min_version = TLS1_2_VERSION;
1356*e0c4386eSCy Schubert         max_version = TLS1_2_VERSION;
1357*e0c4386eSCy Schubert     } else {
1358*e0c4386eSCy Schubert         min_version = 0;
1359*e0c4386eSCy Schubert # if defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH)
1360*e0c4386eSCy Schubert         /* We only have ec and dh based built-in groups for TLSv1.3 */
1361*e0c4386eSCy Schubert         max_version = TLS1_2_VERSION;
1362*e0c4386eSCy Schubert # else
1363*e0c4386eSCy Schubert         max_version = 0;
1364*e0c4386eSCy Schubert # endif
1365*e0c4386eSCy Schubert     }
1366*e0c4386eSCy Schubert #endif
1367*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS
1368*e0c4386eSCy Schubert     if (dtls || dtls1 || dtls12) {
1369*e0c4386eSCy Schubert         meth = DTLS_method();
1370*e0c4386eSCy Schubert         if (dtls1) {
1371*e0c4386eSCy Schubert             min_version = DTLS1_VERSION;
1372*e0c4386eSCy Schubert             max_version = DTLS1_VERSION;
1373*e0c4386eSCy Schubert         } else if (dtls12) {
1374*e0c4386eSCy Schubert             min_version = DTLS1_2_VERSION;
1375*e0c4386eSCy Schubert             max_version = DTLS1_2_VERSION;
1376*e0c4386eSCy Schubert         } else {
1377*e0c4386eSCy Schubert             min_version = 0;
1378*e0c4386eSCy Schubert             max_version = 0;
1379*e0c4386eSCy Schubert         }
1380*e0c4386eSCy Schubert     }
1381*e0c4386eSCy Schubert #endif
1382*e0c4386eSCy Schubert 
1383*e0c4386eSCy Schubert     if (provider != NULL
1384*e0c4386eSCy Schubert             && !test_get_libctx(&libctx, &defctxnull, config, &thisprov, provider))
1385*e0c4386eSCy Schubert         goto end;
1386*e0c4386eSCy Schubert 
1387*e0c4386eSCy Schubert     c_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1388*e0c4386eSCy Schubert     s_ctx = SSL_CTX_new_ex(libctx, NULL, meth);
1389*e0c4386eSCy Schubert     s_ctx2 = SSL_CTX_new_ex(libctx, NULL, meth); /* no SSL_CTX_dup! */
1390*e0c4386eSCy Schubert     if ((c_ctx == NULL) || (s_ctx == NULL) || (s_ctx2 == NULL)) {
1391*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
1392*e0c4386eSCy Schubert         goto end;
1393*e0c4386eSCy Schubert     }
1394*e0c4386eSCy Schubert     /*
1395*e0c4386eSCy Schubert      * Since we will use low security ciphersuites and keys for testing set
1396*e0c4386eSCy Schubert      * security level to zero by default. Tests can override this by adding
1397*e0c4386eSCy Schubert      * "@SECLEVEL=n" to the cipher string.
1398*e0c4386eSCy Schubert      */
1399*e0c4386eSCy Schubert     SSL_CTX_set_security_level(c_ctx, 0);
1400*e0c4386eSCy Schubert     SSL_CTX_set_security_level(s_ctx, 0);
1401*e0c4386eSCy Schubert     SSL_CTX_set_security_level(s_ctx2, 0);
1402*e0c4386eSCy Schubert 
1403*e0c4386eSCy Schubert     if (no_ticket) {
1404*e0c4386eSCy Schubert         SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
1405*e0c4386eSCy Schubert         SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
1406*e0c4386eSCy Schubert     }
1407*e0c4386eSCy Schubert 
1408*e0c4386eSCy Schubert     if (SSL_CTX_set_min_proto_version(c_ctx, min_version) == 0)
1409*e0c4386eSCy Schubert         goto end;
1410*e0c4386eSCy Schubert     if (SSL_CTX_set_max_proto_version(c_ctx, max_version) == 0)
1411*e0c4386eSCy Schubert         goto end;
1412*e0c4386eSCy Schubert     if (SSL_CTX_set_min_proto_version(s_ctx, min_version) == 0)
1413*e0c4386eSCy Schubert         goto end;
1414*e0c4386eSCy Schubert     if (SSL_CTX_set_max_proto_version(s_ctx, max_version) == 0)
1415*e0c4386eSCy Schubert         goto end;
1416*e0c4386eSCy Schubert 
1417*e0c4386eSCy Schubert     if (cipher != NULL) {
1418*e0c4386eSCy Schubert         if (strcmp(cipher, "") == 0) {
1419*e0c4386eSCy Schubert             if (!SSL_CTX_set_cipher_list(c_ctx, cipher)) {
1420*e0c4386eSCy Schubert                 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1421*e0c4386eSCy Schubert                     ERR_clear_error();
1422*e0c4386eSCy Schubert                 } else {
1423*e0c4386eSCy Schubert                     ERR_print_errors(bio_err);
1424*e0c4386eSCy Schubert                     goto end;
1425*e0c4386eSCy Schubert                 }
1426*e0c4386eSCy Schubert             } else {
1427*e0c4386eSCy Schubert                 /* Should have failed when clearing all TLSv1.2 ciphers. */
1428*e0c4386eSCy Schubert                 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1429*e0c4386eSCy Schubert                 goto end;
1430*e0c4386eSCy Schubert             }
1431*e0c4386eSCy Schubert 
1432*e0c4386eSCy Schubert             if (!SSL_CTX_set_cipher_list(s_ctx, cipher)) {
1433*e0c4386eSCy Schubert                 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1434*e0c4386eSCy Schubert                     ERR_clear_error();
1435*e0c4386eSCy Schubert                 } else {
1436*e0c4386eSCy Schubert                     ERR_print_errors(bio_err);
1437*e0c4386eSCy Schubert                     goto end;
1438*e0c4386eSCy Schubert                 }
1439*e0c4386eSCy Schubert             } else {
1440*e0c4386eSCy Schubert                 /* Should have failed when clearing all TLSv1.2 ciphers. */
1441*e0c4386eSCy Schubert                 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1442*e0c4386eSCy Schubert                 goto end;
1443*e0c4386eSCy Schubert             }
1444*e0c4386eSCy Schubert 
1445*e0c4386eSCy Schubert             if (!SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1446*e0c4386eSCy Schubert                 if (ERR_GET_REASON(ERR_peek_error()) == SSL_R_NO_CIPHER_MATCH) {
1447*e0c4386eSCy Schubert                     ERR_clear_error();
1448*e0c4386eSCy Schubert                 } else {
1449*e0c4386eSCy Schubert                     ERR_print_errors(bio_err);
1450*e0c4386eSCy Schubert                     goto end;
1451*e0c4386eSCy Schubert                 }
1452*e0c4386eSCy Schubert             } else {
1453*e0c4386eSCy Schubert                 /* Should have failed when clearing all TLSv1.2 ciphers. */
1454*e0c4386eSCy Schubert                 fprintf(stderr, "CLEARING ALL TLSv1.2 CIPHERS SHOULD FAIL\n");
1455*e0c4386eSCy Schubert                 goto end;
1456*e0c4386eSCy Schubert             }
1457*e0c4386eSCy Schubert         } else {
1458*e0c4386eSCy Schubert             if (!SSL_CTX_set_cipher_list(c_ctx, cipher)
1459*e0c4386eSCy Schubert                     || !SSL_CTX_set_cipher_list(s_ctx, cipher)
1460*e0c4386eSCy Schubert                     || !SSL_CTX_set_cipher_list(s_ctx2, cipher)) {
1461*e0c4386eSCy Schubert                 ERR_print_errors(bio_err);
1462*e0c4386eSCy Schubert                 goto end;
1463*e0c4386eSCy Schubert             }
1464*e0c4386eSCy Schubert         }
1465*e0c4386eSCy Schubert     }
1466*e0c4386eSCy Schubert     if (ciphersuites != NULL) {
1467*e0c4386eSCy Schubert         if (!SSL_CTX_set_ciphersuites(c_ctx, ciphersuites)
1468*e0c4386eSCy Schubert             || !SSL_CTX_set_ciphersuites(s_ctx, ciphersuites)
1469*e0c4386eSCy Schubert             || !SSL_CTX_set_ciphersuites(s_ctx2, ciphersuites)) {
1470*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
1471*e0c4386eSCy Schubert             goto end;
1472*e0c4386eSCy Schubert         }
1473*e0c4386eSCy Schubert     }
1474*e0c4386eSCy Schubert 
1475*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CT
1476*e0c4386eSCy Schubert     if (ct_validation &&
1477*e0c4386eSCy Schubert         !SSL_CTX_enable_ct(c_ctx, SSL_CT_VALIDATION_STRICT)) {
1478*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
1479*e0c4386eSCy Schubert         goto end;
1480*e0c4386eSCy Schubert     }
1481*e0c4386eSCy Schubert #endif
1482*e0c4386eSCy Schubert 
1483*e0c4386eSCy Schubert     /* Process SSL_CONF arguments */
1484*e0c4386eSCy Schubert     SSL_CONF_CTX_set_ssl_ctx(c_cctx, c_ctx);
1485*e0c4386eSCy Schubert     SSL_CONF_CTX_set_ssl_ctx(s_cctx, s_ctx);
1486*e0c4386eSCy Schubert     SSL_CONF_CTX_set_ssl_ctx(s_cctx2, s_ctx2);
1487*e0c4386eSCy Schubert 
1488*e0c4386eSCy Schubert     for (i = 0; i < sk_OPENSSL_STRING_num(conf_args); i += 2) {
1489*e0c4386eSCy Schubert         int rv;
1490*e0c4386eSCy Schubert         arg = sk_OPENSSL_STRING_value(conf_args, i);
1491*e0c4386eSCy Schubert         argn = sk_OPENSSL_STRING_value(conf_args, i + 1);
1492*e0c4386eSCy Schubert         rv = SSL_CONF_cmd(c_cctx, arg, argn);
1493*e0c4386eSCy Schubert         /* If not recognised use server context */
1494*e0c4386eSCy Schubert         if (rv == -2) {
1495*e0c4386eSCy Schubert             rv = SSL_CONF_cmd(s_cctx2, arg, argn);
1496*e0c4386eSCy Schubert             if (rv > 0)
1497*e0c4386eSCy Schubert                 rv = SSL_CONF_cmd(s_cctx, arg, argn);
1498*e0c4386eSCy Schubert         }
1499*e0c4386eSCy Schubert         if (rv <= 0) {
1500*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error processing %s %s\n",
1501*e0c4386eSCy Schubert                        arg, argn ? argn : "");
1502*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
1503*e0c4386eSCy Schubert             goto end;
1504*e0c4386eSCy Schubert         }
1505*e0c4386eSCy Schubert     }
1506*e0c4386eSCy Schubert 
1507*e0c4386eSCy Schubert     if (!SSL_CONF_CTX_finish(s_cctx) || !SSL_CONF_CTX_finish(c_cctx) || !SSL_CONF_CTX_finish(s_cctx2)) {
1508*e0c4386eSCy Schubert         BIO_puts(bio_err, "Error finishing context\n");
1509*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
1510*e0c4386eSCy Schubert         goto end;
1511*e0c4386eSCy Schubert     }
1512*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
1513*e0c4386eSCy Schubert     if (!no_dhe) {
1514*e0c4386eSCy Schubert         if (dhe1024dsa)
1515*e0c4386eSCy Schubert             dhpkey = get_dh1024dsa(libctx);
1516*e0c4386eSCy Schubert         else if (dhe512)
1517*e0c4386eSCy Schubert             dhpkey = get_dh512(libctx);
1518*e0c4386eSCy Schubert         else if (dhe4096)
1519*e0c4386eSCy Schubert             dhpkey = get_dh4096(libctx);
1520*e0c4386eSCy Schubert         else
1521*e0c4386eSCy Schubert             dhpkey = get_dh2048(libctx);
1522*e0c4386eSCy Schubert 
1523*e0c4386eSCy Schubert         if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
1524*e0c4386eSCy Schubert             EVP_PKEY_free(dhpkey);
1525*e0c4386eSCy Schubert             BIO_puts(bio_err, "Error getting DH parameters\n");
1526*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
1527*e0c4386eSCy Schubert             goto end;
1528*e0c4386eSCy Schubert         }
1529*e0c4386eSCy Schubert         if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey))
1530*e0c4386eSCy Schubert             EVP_PKEY_free(dhpkey);
1531*e0c4386eSCy Schubert         if (!SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey))
1532*e0c4386eSCy Schubert             EVP_PKEY_free(dhpkey);
1533*e0c4386eSCy Schubert     }
1534*e0c4386eSCy Schubert #endif
1535*e0c4386eSCy Schubert 
1536*e0c4386eSCy Schubert     if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
1537*e0c4386eSCy Schubert           || SSL_CTX_load_verify_dir(s_ctx, CApath))
1538*e0c4386eSCy Schubert         || !SSL_CTX_set_default_verify_paths(s_ctx)
1539*e0c4386eSCy Schubert         || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
1540*e0c4386eSCy Schubert              || SSL_CTX_load_verify_dir(s_ctx2, CApath))
1541*e0c4386eSCy Schubert         || !SSL_CTX_set_default_verify_paths(s_ctx2)
1542*e0c4386eSCy Schubert         || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
1543*e0c4386eSCy Schubert              || SSL_CTX_load_verify_dir(c_ctx, CApath))
1544*e0c4386eSCy Schubert         || !SSL_CTX_set_default_verify_paths(c_ctx)) {
1545*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
1546*e0c4386eSCy Schubert     }
1547*e0c4386eSCy Schubert 
1548*e0c4386eSCy Schubert #ifndef OPENSSL_NO_CT
1549*e0c4386eSCy Schubert     if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
1550*e0c4386eSCy Schubert         !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
1551*e0c4386eSCy Schubert         !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
1552*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
1553*e0c4386eSCy Schubert     }
1554*e0c4386eSCy Schubert #endif
1555*e0c4386eSCy Schubert 
1556*e0c4386eSCy Schubert     if (client_auth) {
1557*e0c4386eSCy Schubert         printf("client authentication\n");
1558*e0c4386eSCy Schubert         SSL_CTX_set_verify(s_ctx,
1559*e0c4386eSCy Schubert                            SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1560*e0c4386eSCy Schubert                            verify_callback);
1561*e0c4386eSCy Schubert         SSL_CTX_set_verify(s_ctx2,
1562*e0c4386eSCy Schubert                            SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1563*e0c4386eSCy Schubert                            verify_callback);
1564*e0c4386eSCy Schubert         SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
1565*e0c4386eSCy Schubert                                          &app_verify_arg);
1566*e0c4386eSCy Schubert         SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
1567*e0c4386eSCy Schubert                                          &app_verify_arg);
1568*e0c4386eSCy Schubert     }
1569*e0c4386eSCy Schubert     if (server_auth) {
1570*e0c4386eSCy Schubert         printf("server authentication\n");
1571*e0c4386eSCy Schubert         SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
1572*e0c4386eSCy Schubert         SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
1573*e0c4386eSCy Schubert                                          &app_verify_arg);
1574*e0c4386eSCy Schubert     }
1575*e0c4386eSCy Schubert 
1576*e0c4386eSCy Schubert     {
1577*e0c4386eSCy Schubert         int session_id_context = 0;
1578*e0c4386eSCy Schubert         if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
1579*e0c4386eSCy Schubert                                             sizeof(session_id_context)) ||
1580*e0c4386eSCy Schubert             !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
1581*e0c4386eSCy Schubert                                             sizeof(session_id_context))) {
1582*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
1583*e0c4386eSCy Schubert             goto end;
1584*e0c4386eSCy Schubert         }
1585*e0c4386eSCy Schubert     }
1586*e0c4386eSCy Schubert 
1587*e0c4386eSCy Schubert     /* Use PSK only if PSK key is given */
1588*e0c4386eSCy Schubert     if (psk_key != NULL) {
1589*e0c4386eSCy Schubert         /*
1590*e0c4386eSCy Schubert          * no_psk is used to avoid putting psk command to openssl tool
1591*e0c4386eSCy Schubert          */
1592*e0c4386eSCy Schubert         if (no_psk) {
1593*e0c4386eSCy Schubert             /*
1594*e0c4386eSCy Schubert              * if PSK is not compiled in and psk key is given, do nothing and
1595*e0c4386eSCy Schubert              * exit successfully
1596*e0c4386eSCy Schubert              */
1597*e0c4386eSCy Schubert             ret = EXIT_SUCCESS;
1598*e0c4386eSCy Schubert             goto end;
1599*e0c4386eSCy Schubert         }
1600*e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
1601*e0c4386eSCy Schubert         SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
1602*e0c4386eSCy Schubert         SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
1603*e0c4386eSCy Schubert         SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
1604*e0c4386eSCy Schubert         if (debug)
1605*e0c4386eSCy Schubert             BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
1606*e0c4386eSCy Schubert         if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
1607*e0c4386eSCy Schubert             !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
1608*e0c4386eSCy Schubert             BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
1609*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
1610*e0c4386eSCy Schubert             goto end;
1611*e0c4386eSCy Schubert         }
1612*e0c4386eSCy Schubert #endif
1613*e0c4386eSCy Schubert     }
1614*e0c4386eSCy Schubert 
1615*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
1616*e0c4386eSCy Schubert     if (npn_client) {
1617*e0c4386eSCy Schubert         SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
1618*e0c4386eSCy Schubert     }
1619*e0c4386eSCy Schubert     if (npn_server) {
1620*e0c4386eSCy Schubert         if (npn_server_reject) {
1621*e0c4386eSCy Schubert             BIO_printf(bio_err,
1622*e0c4386eSCy Schubert                        "Can't have both -npn_server and -npn_server_reject\n");
1623*e0c4386eSCy Schubert             goto end;
1624*e0c4386eSCy Schubert         }
1625*e0c4386eSCy Schubert         SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
1626*e0c4386eSCy Schubert         SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
1627*e0c4386eSCy Schubert     }
1628*e0c4386eSCy Schubert     if (npn_server_reject) {
1629*e0c4386eSCy Schubert         SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
1630*e0c4386eSCy Schubert         SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
1631*e0c4386eSCy Schubert     }
1632*e0c4386eSCy Schubert #endif
1633*e0c4386eSCy Schubert 
1634*e0c4386eSCy Schubert     if (serverinfo_sct) {
1635*e0c4386eSCy Schubert         if (!SSL_CTX_add_client_custom_ext(c_ctx,
1636*e0c4386eSCy Schubert                 TLSEXT_TYPE_signed_certificate_timestamp,
1637*e0c4386eSCy Schubert                 NULL, NULL, NULL,
1638*e0c4386eSCy Schubert                 serverinfo_cli_parse_cb, NULL)) {
1639*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error adding SCT extension\n");
1640*e0c4386eSCy Schubert             goto end;
1641*e0c4386eSCy Schubert         }
1642*e0c4386eSCy Schubert     }
1643*e0c4386eSCy Schubert     if (serverinfo_tack) {
1644*e0c4386eSCy Schubert         if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
1645*e0c4386eSCy Schubert                                       NULL, NULL, NULL,
1646*e0c4386eSCy Schubert                                       serverinfo_cli_parse_cb, NULL)) {
1647*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error adding TACK extension\n");
1648*e0c4386eSCy Schubert             goto end;
1649*e0c4386eSCy Schubert         }
1650*e0c4386eSCy Schubert     }
1651*e0c4386eSCy Schubert     if (serverinfo_file)
1652*e0c4386eSCy Schubert         if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
1653*e0c4386eSCy Schubert             !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
1654*e0c4386eSCy Schubert             BIO_printf(bio_err, "missing serverinfo file\n");
1655*e0c4386eSCy Schubert             goto end;
1656*e0c4386eSCy Schubert         }
1657*e0c4386eSCy Schubert 
1658*e0c4386eSCy Schubert     if (custom_ext) {
1659*e0c4386eSCy Schubert         if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
1660*e0c4386eSCy Schubert                                       custom_ext_0_cli_add_cb,
1661*e0c4386eSCy Schubert                                       NULL, NULL,
1662*e0c4386eSCy Schubert                                       custom_ext_0_cli_parse_cb, NULL)
1663*e0c4386eSCy Schubert             || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
1664*e0c4386eSCy Schubert                                       custom_ext_1_cli_add_cb,
1665*e0c4386eSCy Schubert                                       NULL, NULL,
1666*e0c4386eSCy Schubert                                       custom_ext_1_cli_parse_cb, NULL)
1667*e0c4386eSCy Schubert             || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
1668*e0c4386eSCy Schubert                                       custom_ext_2_cli_add_cb,
1669*e0c4386eSCy Schubert                                       NULL, NULL,
1670*e0c4386eSCy Schubert                                       custom_ext_2_cli_parse_cb, NULL)
1671*e0c4386eSCy Schubert             || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
1672*e0c4386eSCy Schubert                                       custom_ext_3_cli_add_cb,
1673*e0c4386eSCy Schubert                                       NULL, NULL,
1674*e0c4386eSCy Schubert                                       custom_ext_3_cli_parse_cb, NULL)
1675*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
1676*e0c4386eSCy Schubert                                       custom_ext_0_srv_add_cb,
1677*e0c4386eSCy Schubert                                       NULL, NULL,
1678*e0c4386eSCy Schubert                                       custom_ext_0_srv_parse_cb, NULL)
1679*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
1680*e0c4386eSCy Schubert                                       custom_ext_0_srv_add_cb,
1681*e0c4386eSCy Schubert                                       NULL, NULL,
1682*e0c4386eSCy Schubert                                       custom_ext_0_srv_parse_cb, NULL)
1683*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
1684*e0c4386eSCy Schubert                                       custom_ext_1_srv_add_cb,
1685*e0c4386eSCy Schubert                                       NULL, NULL,
1686*e0c4386eSCy Schubert                                       custom_ext_1_srv_parse_cb, NULL)
1687*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
1688*e0c4386eSCy Schubert                                       custom_ext_1_srv_add_cb,
1689*e0c4386eSCy Schubert                                       NULL, NULL,
1690*e0c4386eSCy Schubert                                       custom_ext_1_srv_parse_cb, NULL)
1691*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
1692*e0c4386eSCy Schubert                                       custom_ext_2_srv_add_cb,
1693*e0c4386eSCy Schubert                                       NULL, NULL,
1694*e0c4386eSCy Schubert                                       custom_ext_2_srv_parse_cb, NULL)
1695*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
1696*e0c4386eSCy Schubert                                       custom_ext_2_srv_add_cb,
1697*e0c4386eSCy Schubert                                       NULL, NULL,
1698*e0c4386eSCy Schubert                                       custom_ext_2_srv_parse_cb, NULL)
1699*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
1700*e0c4386eSCy Schubert                                       custom_ext_3_srv_add_cb,
1701*e0c4386eSCy Schubert                                       NULL, NULL,
1702*e0c4386eSCy Schubert                                       custom_ext_3_srv_parse_cb, NULL)
1703*e0c4386eSCy Schubert             || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
1704*e0c4386eSCy Schubert                                       custom_ext_3_srv_add_cb,
1705*e0c4386eSCy Schubert                                       NULL, NULL,
1706*e0c4386eSCy Schubert                                       custom_ext_3_srv_parse_cb, NULL)) {
1707*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error setting custom extensions\n");
1708*e0c4386eSCy Schubert             goto end;
1709*e0c4386eSCy Schubert         }
1710*e0c4386eSCy Schubert     }
1711*e0c4386eSCy Schubert 
1712*e0c4386eSCy Schubert     if (alpn_server)
1713*e0c4386eSCy Schubert         SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
1714*e0c4386eSCy Schubert     if (alpn_server2)
1715*e0c4386eSCy Schubert         SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
1716*e0c4386eSCy Schubert 
1717*e0c4386eSCy Schubert     if (alpn_client) {
1718*e0c4386eSCy Schubert         size_t alpn_len;
1719*e0c4386eSCy Schubert         unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
1720*e0c4386eSCy Schubert 
1721*e0c4386eSCy Schubert         if (alpn == NULL) {
1722*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
1723*e0c4386eSCy Schubert             goto end;
1724*e0c4386eSCy Schubert         }
1725*e0c4386eSCy Schubert         /* Returns 0 on success!! */
1726*e0c4386eSCy Schubert         if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
1727*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error setting ALPN\n");
1728*e0c4386eSCy Schubert             OPENSSL_free(alpn);
1729*e0c4386eSCy Schubert             goto end;
1730*e0c4386eSCy Schubert         }
1731*e0c4386eSCy Schubert         OPENSSL_free(alpn);
1732*e0c4386eSCy Schubert     }
1733*e0c4386eSCy Schubert 
1734*e0c4386eSCy Schubert     if (server_sess_in != NULL) {
1735*e0c4386eSCy Schubert         server_sess = read_session(server_sess_in);
1736*e0c4386eSCy Schubert         if (server_sess == NULL)
1737*e0c4386eSCy Schubert             goto end;
1738*e0c4386eSCy Schubert     }
1739*e0c4386eSCy Schubert     if (client_sess_in != NULL) {
1740*e0c4386eSCy Schubert         client_sess = read_session(client_sess_in);
1741*e0c4386eSCy Schubert         if (client_sess == NULL)
1742*e0c4386eSCy Schubert             goto end;
1743*e0c4386eSCy Schubert     }
1744*e0c4386eSCy Schubert 
1745*e0c4386eSCy Schubert     if (server_sess_out != NULL || server_sess_in != NULL) {
1746*e0c4386eSCy Schubert         char *keys;
1747*e0c4386eSCy Schubert         long size;
1748*e0c4386eSCy Schubert 
1749*e0c4386eSCy Schubert         /* Use a fixed key so that we can decrypt the ticket. */
1750*e0c4386eSCy Schubert         size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
1751*e0c4386eSCy Schubert         keys = OPENSSL_zalloc(size);
1752*e0c4386eSCy Schubert         if (keys == NULL)
1753*e0c4386eSCy Schubert             goto end;
1754*e0c4386eSCy Schubert         SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
1755*e0c4386eSCy Schubert         OPENSSL_free(keys);
1756*e0c4386eSCy Schubert     }
1757*e0c4386eSCy Schubert 
1758*e0c4386eSCy Schubert     if (sn_server1 != NULL || sn_server2 != NULL)
1759*e0c4386eSCy Schubert         SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
1760*e0c4386eSCy Schubert 
1761*e0c4386eSCy Schubert     c_ssl = SSL_new(c_ctx);
1762*e0c4386eSCy Schubert     s_ssl = SSL_new(s_ctx);
1763*e0c4386eSCy Schubert     if (c_ssl == NULL || s_ssl == NULL)
1764*e0c4386eSCy Schubert         goto end;
1765*e0c4386eSCy Schubert 
1766*e0c4386eSCy Schubert     if (sn_client)
1767*e0c4386eSCy Schubert         SSL_set_tlsext_host_name(c_ssl, sn_client);
1768*e0c4386eSCy Schubert     if (client_ktls)
1769*e0c4386eSCy Schubert         SSL_set_options(c_ssl, SSL_OP_ENABLE_KTLS);
1770*e0c4386eSCy Schubert     if (server_ktls)
1771*e0c4386eSCy Schubert         SSL_set_options(s_ssl, SSL_OP_ENABLE_KTLS);
1772*e0c4386eSCy Schubert 
1773*e0c4386eSCy Schubert     if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1774*e0c4386eSCy Schubert         goto end;
1775*e0c4386eSCy Schubert     if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1776*e0c4386eSCy Schubert         goto end;
1777*e0c4386eSCy Schubert     if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1778*e0c4386eSCy Schubert         goto end;
1779*e0c4386eSCy Schubert     if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1780*e0c4386eSCy Schubert         goto end;
1781*e0c4386eSCy Schubert 
1782*e0c4386eSCy Schubert     if (server_sess) {
1783*e0c4386eSCy Schubert         if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
1784*e0c4386eSCy Schubert             BIO_printf(bio_err, "Can't add server session\n");
1785*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
1786*e0c4386eSCy Schubert             goto end;
1787*e0c4386eSCy Schubert         }
1788*e0c4386eSCy Schubert     }
1789*e0c4386eSCy Schubert 
1790*e0c4386eSCy Schubert     BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
1791*e0c4386eSCy Schubert     for (i = 0; i < number; i++) {
1792*e0c4386eSCy Schubert         if (!reuse) {
1793*e0c4386eSCy Schubert             if (!SSL_set_session(c_ssl, NULL)) {
1794*e0c4386eSCy Schubert                 BIO_printf(bio_err, "Failed to set session\n");
1795*e0c4386eSCy Schubert                 goto end;
1796*e0c4386eSCy Schubert             }
1797*e0c4386eSCy Schubert         }
1798*e0c4386eSCy Schubert         if (client_sess_in != NULL) {
1799*e0c4386eSCy Schubert             if (SSL_set_session(c_ssl, client_sess) == 0) {
1800*e0c4386eSCy Schubert                 BIO_printf(bio_err, "Can't set client session\n");
1801*e0c4386eSCy Schubert                 ERR_print_errors(bio_err);
1802*e0c4386eSCy Schubert                 goto end;
1803*e0c4386eSCy Schubert             }
1804*e0c4386eSCy Schubert         }
1805*e0c4386eSCy Schubert         switch (bio_type) {
1806*e0c4386eSCy Schubert         case BIO_MEM:
1807*e0c4386eSCy Schubert             ret = doit(s_ssl, c_ssl, bytes);
1808*e0c4386eSCy Schubert             break;
1809*e0c4386eSCy Schubert         case BIO_PAIR:
1810*e0c4386eSCy Schubert             ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
1811*e0c4386eSCy Schubert             break;
1812*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SOCK
1813*e0c4386eSCy Schubert         case BIO_IPV4:
1814*e0c4386eSCy Schubert             ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
1815*e0c4386eSCy Schubert                                  bytes, &s_time, &c_time);
1816*e0c4386eSCy Schubert             break;
1817*e0c4386eSCy Schubert         case BIO_IPV6:
1818*e0c4386eSCy Schubert             ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
1819*e0c4386eSCy Schubert                                  bytes, &s_time, &c_time);
1820*e0c4386eSCy Schubert             break;
1821*e0c4386eSCy Schubert #else
1822*e0c4386eSCy Schubert         case BIO_IPV4:
1823*e0c4386eSCy Schubert         case BIO_IPV6:
1824*e0c4386eSCy Schubert             ret = EXIT_FAILURE;
1825*e0c4386eSCy Schubert             goto end;
1826*e0c4386eSCy Schubert #endif
1827*e0c4386eSCy Schubert         }
1828*e0c4386eSCy Schubert         if (ret != EXIT_SUCCESS)
1829*e0c4386eSCy Schubert             break;
1830*e0c4386eSCy Schubert     }
1831*e0c4386eSCy Schubert 
1832*e0c4386eSCy Schubert     if (should_negotiate && ret == EXIT_SUCCESS &&
1833*e0c4386eSCy Schubert         strcmp(should_negotiate, "fail-server") != 0 &&
1834*e0c4386eSCy Schubert         strcmp(should_negotiate, "fail-client") != 0) {
1835*e0c4386eSCy Schubert         int version = protocol_from_string(should_negotiate);
1836*e0c4386eSCy Schubert         if (version < 0) {
1837*e0c4386eSCy Schubert             BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
1838*e0c4386eSCy Schubert             ret = EXIT_FAILURE;
1839*e0c4386eSCy Schubert             goto end;
1840*e0c4386eSCy Schubert         }
1841*e0c4386eSCy Schubert         if (SSL_version(c_ssl) != version) {
1842*e0c4386eSCy Schubert             BIO_printf(bio_err, "Unexpected version negotiated. "
1843*e0c4386eSCy Schubert                 "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
1844*e0c4386eSCy Schubert             ret = EXIT_FAILURE;
1845*e0c4386eSCy Schubert             goto end;
1846*e0c4386eSCy Schubert         }
1847*e0c4386eSCy Schubert     }
1848*e0c4386eSCy Schubert 
1849*e0c4386eSCy Schubert     if (should_reuse != -1) {
1850*e0c4386eSCy Schubert         if (SSL_session_reused(s_ssl) != should_reuse ||
1851*e0c4386eSCy Schubert             SSL_session_reused(c_ssl) != should_reuse) {
1852*e0c4386eSCy Schubert             BIO_printf(bio_err, "Unexpected session reuse state. "
1853*e0c4386eSCy Schubert                 "Expected: %d, server: %d, client: %d\n", should_reuse,
1854*e0c4386eSCy Schubert                 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
1855*e0c4386eSCy Schubert             ret = EXIT_FAILURE;
1856*e0c4386eSCy Schubert             goto end;
1857*e0c4386eSCy Schubert         }
1858*e0c4386eSCy Schubert     }
1859*e0c4386eSCy Schubert 
1860*e0c4386eSCy Schubert     if (server_sess_out != NULL) {
1861*e0c4386eSCy Schubert         if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
1862*e0c4386eSCy Schubert             ret = EXIT_FAILURE;
1863*e0c4386eSCy Schubert             goto end;
1864*e0c4386eSCy Schubert         }
1865*e0c4386eSCy Schubert     }
1866*e0c4386eSCy Schubert     if (client_sess_out != NULL) {
1867*e0c4386eSCy Schubert         if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
1868*e0c4386eSCy Schubert             ret = EXIT_FAILURE;
1869*e0c4386eSCy Schubert             goto end;
1870*e0c4386eSCy Schubert         }
1871*e0c4386eSCy Schubert     }
1872*e0c4386eSCy Schubert 
1873*e0c4386eSCy Schubert     if (!verbose) {
1874*e0c4386eSCy Schubert         print_details(c_ssl, "");
1875*e0c4386eSCy Schubert     }
1876*e0c4386eSCy Schubert     if (print_time) {
1877*e0c4386eSCy Schubert #ifdef CLOCKS_PER_SEC
1878*e0c4386eSCy Schubert         /*
1879*e0c4386eSCy Schubert          * "To determine the time in seconds, the value returned by the clock
1880*e0c4386eSCy Schubert          * function should be divided by the value of the macro
1881*e0c4386eSCy Schubert          * CLOCKS_PER_SEC." -- ISO/IEC 9899
1882*e0c4386eSCy Schubert          */
1883*e0c4386eSCy Schubert         BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
1884*e0c4386eSCy Schubert                    "Approximate total client time: %6.2f s\n",
1885*e0c4386eSCy Schubert                    (double)s_time / CLOCKS_PER_SEC,
1886*e0c4386eSCy Schubert                    (double)c_time / CLOCKS_PER_SEC);
1887*e0c4386eSCy Schubert #else
1888*e0c4386eSCy Schubert         BIO_printf(bio_stdout,
1889*e0c4386eSCy Schubert                    "Approximate total server time: %6.2f units\n"
1890*e0c4386eSCy Schubert                    "Approximate total client time: %6.2f units\n",
1891*e0c4386eSCy Schubert                    (double)s_time, (double)c_time);
1892*e0c4386eSCy Schubert #endif
1893*e0c4386eSCy Schubert     }
1894*e0c4386eSCy Schubert 
1895*e0c4386eSCy Schubert  end:
1896*e0c4386eSCy Schubert     SSL_free(s_ssl);
1897*e0c4386eSCy Schubert     SSL_free(c_ssl);
1898*e0c4386eSCy Schubert     SSL_CTX_free(s_ctx);
1899*e0c4386eSCy Schubert     SSL_CTX_free(s_ctx2);
1900*e0c4386eSCy Schubert     SSL_CTX_free(c_ctx);
1901*e0c4386eSCy Schubert     SSL_CONF_CTX_free(s_cctx);
1902*e0c4386eSCy Schubert     SSL_CONF_CTX_free(s_cctx2);
1903*e0c4386eSCy Schubert     SSL_CONF_CTX_free(c_cctx);
1904*e0c4386eSCy Schubert     sk_OPENSSL_STRING_free(conf_args);
1905*e0c4386eSCy Schubert 
1906*e0c4386eSCy Schubert     BIO_free(bio_stdout);
1907*e0c4386eSCy Schubert 
1908*e0c4386eSCy Schubert     SSL_SESSION_free(server_sess);
1909*e0c4386eSCy Schubert     SSL_SESSION_free(client_sess);
1910*e0c4386eSCy Schubert 
1911*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(defctxnull);
1912*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(thisprov);
1913*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(libctx);
1914*e0c4386eSCy Schubert 
1915*e0c4386eSCy Schubert     BIO_free(bio_err);
1916*e0c4386eSCy Schubert     EXIT(ret);
1917*e0c4386eSCy Schubert }
1918*e0c4386eSCy Schubert 
1919*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SOCK
doit_localhost(SSL * s_ssl,SSL * c_ssl,int family,long count,clock_t * s_time,clock_t * c_time)1920*e0c4386eSCy Schubert int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
1921*e0c4386eSCy Schubert                    clock_t *s_time, clock_t *c_time)
1922*e0c4386eSCy Schubert {
1923*e0c4386eSCy Schubert     long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
1924*e0c4386eSCy Schubert     BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
1925*e0c4386eSCy Schubert     BIO *acpt = NULL, *server = NULL, *client = NULL;
1926*e0c4386eSCy Schubert     char addr_str[40];
1927*e0c4386eSCy Schubert     int ret = EXIT_FAILURE;
1928*e0c4386eSCy Schubert     int err_in_client = 0;
1929*e0c4386eSCy Schubert     int err_in_server = 0;
1930*e0c4386eSCy Schubert 
1931*e0c4386eSCy Schubert     acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
1932*e0c4386eSCy Schubert                                                     : "[::1]:0");
1933*e0c4386eSCy Schubert     if (acpt == NULL)
1934*e0c4386eSCy Schubert         goto err;
1935*e0c4386eSCy Schubert     BIO_set_accept_ip_family(acpt, family);
1936*e0c4386eSCy Schubert     BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
1937*e0c4386eSCy Schubert     if (BIO_do_accept(acpt) <= 0)
1938*e0c4386eSCy Schubert         goto err;
1939*e0c4386eSCy Schubert 
1940*e0c4386eSCy Schubert     BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
1941*e0c4386eSCy Schubert 
1942*e0c4386eSCy Schubert     client = BIO_new_connect(addr_str);
1943*e0c4386eSCy Schubert     if (!client)
1944*e0c4386eSCy Schubert         goto err;
1945*e0c4386eSCy Schubert     BIO_set_conn_ip_family(client, family);
1946*e0c4386eSCy Schubert 
1947*e0c4386eSCy Schubert     if (BIO_set_nbio(client, 1) <= 0)
1948*e0c4386eSCy Schubert         goto err;
1949*e0c4386eSCy Schubert     if (BIO_set_nbio(acpt, 1) <= 0)
1950*e0c4386eSCy Schubert         goto err;
1951*e0c4386eSCy Schubert 
1952*e0c4386eSCy Schubert     {
1953*e0c4386eSCy Schubert         int st_connect = 0, st_accept = 0;
1954*e0c4386eSCy Schubert 
1955*e0c4386eSCy Schubert         while(!st_connect || !st_accept) {
1956*e0c4386eSCy Schubert             if (!st_connect) {
1957*e0c4386eSCy Schubert                 if (BIO_do_connect(client) <= 0) {
1958*e0c4386eSCy Schubert                     if (!BIO_should_retry(client))
1959*e0c4386eSCy Schubert                         goto err;
1960*e0c4386eSCy Schubert                 } else {
1961*e0c4386eSCy Schubert                     st_connect = 1;
1962*e0c4386eSCy Schubert                 }
1963*e0c4386eSCy Schubert             }
1964*e0c4386eSCy Schubert             if (!st_accept) {
1965*e0c4386eSCy Schubert                 if (BIO_do_accept(acpt) <= 0) {
1966*e0c4386eSCy Schubert                     if (!BIO_should_retry(acpt))
1967*e0c4386eSCy Schubert                         goto err;
1968*e0c4386eSCy Schubert                 } else {
1969*e0c4386eSCy Schubert                     st_accept = 1;
1970*e0c4386eSCy Schubert                 }
1971*e0c4386eSCy Schubert             }
1972*e0c4386eSCy Schubert         }
1973*e0c4386eSCy Schubert     }
1974*e0c4386eSCy Schubert     /* We're not interested in accepting further connects */
1975*e0c4386eSCy Schubert     server = BIO_pop(acpt);
1976*e0c4386eSCy Schubert     BIO_free_all(acpt);
1977*e0c4386eSCy Schubert     acpt = NULL;
1978*e0c4386eSCy Schubert 
1979*e0c4386eSCy Schubert     s_ssl_bio = BIO_new(BIO_f_ssl());
1980*e0c4386eSCy Schubert     if (!s_ssl_bio)
1981*e0c4386eSCy Schubert         goto err;
1982*e0c4386eSCy Schubert 
1983*e0c4386eSCy Schubert     c_ssl_bio = BIO_new(BIO_f_ssl());
1984*e0c4386eSCy Schubert     if (!c_ssl_bio)
1985*e0c4386eSCy Schubert         goto err;
1986*e0c4386eSCy Schubert 
1987*e0c4386eSCy Schubert     SSL_set_connect_state(c_ssl);
1988*e0c4386eSCy Schubert     SSL_set_bio(c_ssl, client, client);
1989*e0c4386eSCy Schubert     (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
1990*e0c4386eSCy Schubert 
1991*e0c4386eSCy Schubert     SSL_set_accept_state(s_ssl);
1992*e0c4386eSCy Schubert     SSL_set_bio(s_ssl, server, server);
1993*e0c4386eSCy Schubert     (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
1994*e0c4386eSCy Schubert 
1995*e0c4386eSCy Schubert     do {
1996*e0c4386eSCy Schubert         /*-
1997*e0c4386eSCy Schubert          * c_ssl_bio:          SSL filter BIO
1998*e0c4386eSCy Schubert          *
1999*e0c4386eSCy Schubert          * client:             I/O for SSL library
2000*e0c4386eSCy Schubert          *
2001*e0c4386eSCy Schubert          *
2002*e0c4386eSCy Schubert          * server:             I/O for SSL library
2003*e0c4386eSCy Schubert          *
2004*e0c4386eSCy Schubert          * s_ssl_bio:          SSL filter BIO
2005*e0c4386eSCy Schubert          */
2006*e0c4386eSCy Schubert 
2007*e0c4386eSCy Schubert         /*
2008*e0c4386eSCy Schubert          * We have non-blocking behaviour throughout this test program, but
2009*e0c4386eSCy Schubert          * can be sure that there is *some* progress in each iteration; so we
2010*e0c4386eSCy Schubert          * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2011*e0c4386eSCy Schubert          * we just try everything in each iteration
2012*e0c4386eSCy Schubert          */
2013*e0c4386eSCy Schubert 
2014*e0c4386eSCy Schubert         {
2015*e0c4386eSCy Schubert             /* CLIENT */
2016*e0c4386eSCy Schubert 
2017*e0c4386eSCy Schubert             char cbuf[1024 * 8];
2018*e0c4386eSCy Schubert             int i, r;
2019*e0c4386eSCy Schubert             clock_t c_clock = clock();
2020*e0c4386eSCy Schubert 
2021*e0c4386eSCy Schubert             memset(cbuf, 0, sizeof(cbuf));
2022*e0c4386eSCy Schubert 
2023*e0c4386eSCy Schubert             if (debug)
2024*e0c4386eSCy Schubert                 if (SSL_in_init(c_ssl))
2025*e0c4386eSCy Schubert                     printf("client waiting in SSL_connect - %s\n",
2026*e0c4386eSCy Schubert                            SSL_state_string_long(c_ssl));
2027*e0c4386eSCy Schubert 
2028*e0c4386eSCy Schubert             if (cw_num > 0) {
2029*e0c4386eSCy Schubert                 /* Write to server. */
2030*e0c4386eSCy Schubert 
2031*e0c4386eSCy Schubert                 if (cw_num > (long)sizeof(cbuf))
2032*e0c4386eSCy Schubert                     i = sizeof(cbuf);
2033*e0c4386eSCy Schubert                 else
2034*e0c4386eSCy Schubert                     i = (int)cw_num;
2035*e0c4386eSCy Schubert                 r = BIO_write(c_ssl_bio, cbuf, i);
2036*e0c4386eSCy Schubert                 if (r < 0) {
2037*e0c4386eSCy Schubert                     if (!BIO_should_retry(c_ssl_bio)) {
2038*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in CLIENT (write)\n");
2039*e0c4386eSCy Schubert                         err_in_client = 1;
2040*e0c4386eSCy Schubert                         goto err;
2041*e0c4386eSCy Schubert                     }
2042*e0c4386eSCy Schubert                     /*
2043*e0c4386eSCy Schubert                      * BIO_should_retry(...) can just be ignored here. The
2044*e0c4386eSCy Schubert                      * library expects us to call BIO_write with the same
2045*e0c4386eSCy Schubert                      * arguments again, and that's what we will do in the
2046*e0c4386eSCy Schubert                      * next iteration.
2047*e0c4386eSCy Schubert                      */
2048*e0c4386eSCy Schubert                 } else if (r == 0) {
2049*e0c4386eSCy Schubert                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2050*e0c4386eSCy Schubert                     goto err;
2051*e0c4386eSCy Schubert                 } else {
2052*e0c4386eSCy Schubert                     if (debug)
2053*e0c4386eSCy Schubert                         printf("client wrote %d\n", r);
2054*e0c4386eSCy Schubert                     cw_num -= r;
2055*e0c4386eSCy Schubert                 }
2056*e0c4386eSCy Schubert             }
2057*e0c4386eSCy Schubert 
2058*e0c4386eSCy Schubert             if (cr_num > 0) {
2059*e0c4386eSCy Schubert                 /* Read from server. */
2060*e0c4386eSCy Schubert 
2061*e0c4386eSCy Schubert                 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2062*e0c4386eSCy Schubert                 if (r < 0) {
2063*e0c4386eSCy Schubert                     if (!BIO_should_retry(c_ssl_bio)) {
2064*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in CLIENT (read)\n");
2065*e0c4386eSCy Schubert                         err_in_client = 1;
2066*e0c4386eSCy Schubert                         goto err;
2067*e0c4386eSCy Schubert                     }
2068*e0c4386eSCy Schubert                     /*
2069*e0c4386eSCy Schubert                      * Again, "BIO_should_retry" can be ignored.
2070*e0c4386eSCy Schubert                      */
2071*e0c4386eSCy Schubert                 } else if (r == 0) {
2072*e0c4386eSCy Schubert                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2073*e0c4386eSCy Schubert                     goto err;
2074*e0c4386eSCy Schubert                 } else {
2075*e0c4386eSCy Schubert                     if (debug)
2076*e0c4386eSCy Schubert                         printf("client read %d\n", r);
2077*e0c4386eSCy Schubert                     cr_num -= r;
2078*e0c4386eSCy Schubert                 }
2079*e0c4386eSCy Schubert             }
2080*e0c4386eSCy Schubert 
2081*e0c4386eSCy Schubert             /*
2082*e0c4386eSCy Schubert              * c_time and s_time increments will typically be very small
2083*e0c4386eSCy Schubert              * (depending on machine speed and clock tick intervals), but
2084*e0c4386eSCy Schubert              * sampling over a large number of connections should result in
2085*e0c4386eSCy Schubert              * fairly accurate figures.  We cannot guarantee a lot, however
2086*e0c4386eSCy Schubert              * -- if each connection lasts for exactly one clock tick, it
2087*e0c4386eSCy Schubert              * will be counted only for the client or only for the server or
2088*e0c4386eSCy Schubert              * even not at all.
2089*e0c4386eSCy Schubert              */
2090*e0c4386eSCy Schubert             *c_time += (clock() - c_clock);
2091*e0c4386eSCy Schubert         }
2092*e0c4386eSCy Schubert 
2093*e0c4386eSCy Schubert         {
2094*e0c4386eSCy Schubert             /* SERVER */
2095*e0c4386eSCy Schubert 
2096*e0c4386eSCy Schubert             char sbuf[1024 * 8];
2097*e0c4386eSCy Schubert             int i, r;
2098*e0c4386eSCy Schubert             clock_t s_clock = clock();
2099*e0c4386eSCy Schubert 
2100*e0c4386eSCy Schubert             memset(sbuf, 0, sizeof(sbuf));
2101*e0c4386eSCy Schubert 
2102*e0c4386eSCy Schubert             if (debug)
2103*e0c4386eSCy Schubert                 if (SSL_in_init(s_ssl))
2104*e0c4386eSCy Schubert                     printf("server waiting in SSL_accept - %s\n",
2105*e0c4386eSCy Schubert                            SSL_state_string_long(s_ssl));
2106*e0c4386eSCy Schubert 
2107*e0c4386eSCy Schubert             if (sw_num > 0) {
2108*e0c4386eSCy Schubert                 /* Write to client. */
2109*e0c4386eSCy Schubert 
2110*e0c4386eSCy Schubert                 if (sw_num > (long)sizeof(sbuf))
2111*e0c4386eSCy Schubert                     i = sizeof(sbuf);
2112*e0c4386eSCy Schubert                 else
2113*e0c4386eSCy Schubert                     i = (int)sw_num;
2114*e0c4386eSCy Schubert                 r = BIO_write(s_ssl_bio, sbuf, i);
2115*e0c4386eSCy Schubert                 if (r < 0) {
2116*e0c4386eSCy Schubert                     if (!BIO_should_retry(s_ssl_bio)) {
2117*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in SERVER (write)\n");
2118*e0c4386eSCy Schubert                         err_in_server = 1;
2119*e0c4386eSCy Schubert                         goto err;
2120*e0c4386eSCy Schubert                     }
2121*e0c4386eSCy Schubert                     /* Ignore "BIO_should_retry". */
2122*e0c4386eSCy Schubert                 } else if (r == 0) {
2123*e0c4386eSCy Schubert                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2124*e0c4386eSCy Schubert                     goto err;
2125*e0c4386eSCy Schubert                 } else {
2126*e0c4386eSCy Schubert                     if (debug)
2127*e0c4386eSCy Schubert                         printf("server wrote %d\n", r);
2128*e0c4386eSCy Schubert                     sw_num -= r;
2129*e0c4386eSCy Schubert                 }
2130*e0c4386eSCy Schubert             }
2131*e0c4386eSCy Schubert 
2132*e0c4386eSCy Schubert             if (sr_num > 0) {
2133*e0c4386eSCy Schubert                 /* Read from client. */
2134*e0c4386eSCy Schubert 
2135*e0c4386eSCy Schubert                 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2136*e0c4386eSCy Schubert                 if (r < 0) {
2137*e0c4386eSCy Schubert                     if (!BIO_should_retry(s_ssl_bio)) {
2138*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in SERVER (read)\n");
2139*e0c4386eSCy Schubert                         err_in_server = 1;
2140*e0c4386eSCy Schubert                         goto err;
2141*e0c4386eSCy Schubert                     }
2142*e0c4386eSCy Schubert                     /* blah, blah */
2143*e0c4386eSCy Schubert                 } else if (r == 0) {
2144*e0c4386eSCy Schubert                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2145*e0c4386eSCy Schubert                     goto err;
2146*e0c4386eSCy Schubert                 } else {
2147*e0c4386eSCy Schubert                     if (debug)
2148*e0c4386eSCy Schubert                         printf("server read %d\n", r);
2149*e0c4386eSCy Schubert                     sr_num -= r;
2150*e0c4386eSCy Schubert                 }
2151*e0c4386eSCy Schubert             }
2152*e0c4386eSCy Schubert 
2153*e0c4386eSCy Schubert             *s_time += (clock() - s_clock);
2154*e0c4386eSCy Schubert         }
2155*e0c4386eSCy Schubert     }
2156*e0c4386eSCy Schubert     while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2157*e0c4386eSCy Schubert 
2158*e0c4386eSCy Schubert     if (verbose) {
2159*e0c4386eSCy Schubert         print_details(c_ssl, "DONE via TCP connect: ");
2160*e0c4386eSCy Schubert 
2161*e0c4386eSCy Schubert         if (BIO_get_ktls_send(SSL_get_wbio(s_ssl))
2162*e0c4386eSCy Schubert                 && BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
2163*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Server using Kernel TLS in both directions\n");
2164*e0c4386eSCy Schubert         else if (BIO_get_ktls_send(SSL_get_wbio(s_ssl)))
2165*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Server using Kernel TLS for sending\n");
2166*e0c4386eSCy Schubert         else if (BIO_get_ktls_recv(SSL_get_rbio(s_ssl)))
2167*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Server using Kernel TLS for receiving\n");
2168*e0c4386eSCy Schubert 
2169*e0c4386eSCy Schubert         if (BIO_get_ktls_send(SSL_get_wbio(c_ssl))
2170*e0c4386eSCy Schubert                 && BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
2171*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Client using Kernel TLS in both directions\n");
2172*e0c4386eSCy Schubert         else if (BIO_get_ktls_send(SSL_get_wbio(c_ssl)))
2173*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Client using Kernel TLS for sending\n");
2174*e0c4386eSCy Schubert         else if (BIO_get_ktls_recv(SSL_get_rbio(c_ssl)))
2175*e0c4386eSCy Schubert             BIO_printf(bio_stdout, "Client using Kernel TLS for receiving\n");
2176*e0c4386eSCy Schubert     }
2177*e0c4386eSCy Schubert # ifndef OPENSSL_NO_NEXTPROTONEG
2178*e0c4386eSCy Schubert     if (verify_npn(c_ssl, s_ssl) < 0)
2179*e0c4386eSCy Schubert         goto end;
2180*e0c4386eSCy Schubert # endif
2181*e0c4386eSCy Schubert     if (verify_serverinfo() < 0) {
2182*e0c4386eSCy Schubert         fprintf(stderr, "Server info verify error\n");
2183*e0c4386eSCy Schubert         goto err;
2184*e0c4386eSCy Schubert     }
2185*e0c4386eSCy Schubert     if (verify_alpn(c_ssl, s_ssl) < 0
2186*e0c4386eSCy Schubert             || verify_servername(c_ssl, s_ssl) < 0)
2187*e0c4386eSCy Schubert         goto err;
2188*e0c4386eSCy Schubert 
2189*e0c4386eSCy Schubert     if (custom_ext_error) {
2190*e0c4386eSCy Schubert         fprintf(stderr, "Custom extension error\n");
2191*e0c4386eSCy Schubert         goto err;
2192*e0c4386eSCy Schubert     }
2193*e0c4386eSCy Schubert 
2194*e0c4386eSCy Schubert # ifndef OPENSSL_NO_NEXTPROTONEG
2195*e0c4386eSCy Schubert  end:
2196*e0c4386eSCy Schubert # endif
2197*e0c4386eSCy Schubert     ret = EXIT_SUCCESS;
2198*e0c4386eSCy Schubert 
2199*e0c4386eSCy Schubert  err:
2200*e0c4386eSCy Schubert     ERR_print_errors(bio_err);
2201*e0c4386eSCy Schubert 
2202*e0c4386eSCy Schubert     BIO_free_all(acpt);
2203*e0c4386eSCy Schubert     BIO_free(server);
2204*e0c4386eSCy Schubert     BIO_free(client);
2205*e0c4386eSCy Schubert     BIO_free(s_ssl_bio);
2206*e0c4386eSCy Schubert     BIO_free(c_ssl_bio);
2207*e0c4386eSCy Schubert 
2208*e0c4386eSCy Schubert     if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2209*e0c4386eSCy Schubert         ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2210*e0c4386eSCy Schubert     else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2211*e0c4386eSCy Schubert         ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2212*e0c4386eSCy Schubert 
2213*e0c4386eSCy Schubert     return ret;
2214*e0c4386eSCy Schubert }
2215*e0c4386eSCy Schubert #endif
2216*e0c4386eSCy Schubert 
doit_biopair(SSL * s_ssl,SSL * c_ssl,long count,clock_t * s_time,clock_t * c_time)2217*e0c4386eSCy Schubert int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
2218*e0c4386eSCy Schubert                  clock_t *s_time, clock_t *c_time)
2219*e0c4386eSCy Schubert {
2220*e0c4386eSCy Schubert     long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
2221*e0c4386eSCy Schubert     BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
2222*e0c4386eSCy Schubert     BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
2223*e0c4386eSCy Schubert     int ret = EXIT_FAILURE;
2224*e0c4386eSCy Schubert     int err_in_client = 0;
2225*e0c4386eSCy Schubert     int err_in_server = 0;
2226*e0c4386eSCy Schubert 
2227*e0c4386eSCy Schubert     size_t bufsiz = 256;        /* small buffer for testing */
2228*e0c4386eSCy Schubert 
2229*e0c4386eSCy Schubert     if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
2230*e0c4386eSCy Schubert         goto err;
2231*e0c4386eSCy Schubert     if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
2232*e0c4386eSCy Schubert         goto err;
2233*e0c4386eSCy Schubert 
2234*e0c4386eSCy Schubert     s_ssl_bio = BIO_new(BIO_f_ssl());
2235*e0c4386eSCy Schubert     if (!s_ssl_bio)
2236*e0c4386eSCy Schubert         goto err;
2237*e0c4386eSCy Schubert 
2238*e0c4386eSCy Schubert     c_ssl_bio = BIO_new(BIO_f_ssl());
2239*e0c4386eSCy Schubert     if (!c_ssl_bio)
2240*e0c4386eSCy Schubert         goto err;
2241*e0c4386eSCy Schubert 
2242*e0c4386eSCy Schubert     SSL_set_connect_state(c_ssl);
2243*e0c4386eSCy Schubert     SSL_set_bio(c_ssl, client, client);
2244*e0c4386eSCy Schubert     (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
2245*e0c4386eSCy Schubert 
2246*e0c4386eSCy Schubert     SSL_set_accept_state(s_ssl);
2247*e0c4386eSCy Schubert     SSL_set_bio(s_ssl, server, server);
2248*e0c4386eSCy Schubert     (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
2249*e0c4386eSCy Schubert 
2250*e0c4386eSCy Schubert     do {
2251*e0c4386eSCy Schubert         /*-
2252*e0c4386eSCy Schubert          * c_ssl_bio:          SSL filter BIO
2253*e0c4386eSCy Schubert          *
2254*e0c4386eSCy Schubert          * client:             pseudo-I/O for SSL library
2255*e0c4386eSCy Schubert          *
2256*e0c4386eSCy Schubert          * client_io:          client's SSL communication; usually to be
2257*e0c4386eSCy Schubert          *                     relayed over some I/O facility, but in this
2258*e0c4386eSCy Schubert          *                     test program, we're the server, too:
2259*e0c4386eSCy Schubert          *
2260*e0c4386eSCy Schubert          * server_io:          server's SSL communication
2261*e0c4386eSCy Schubert          *
2262*e0c4386eSCy Schubert          * server:             pseudo-I/O for SSL library
2263*e0c4386eSCy Schubert          *
2264*e0c4386eSCy Schubert          * s_ssl_bio:          SSL filter BIO
2265*e0c4386eSCy Schubert          *
2266*e0c4386eSCy Schubert          * The client and the server each employ a "BIO pair":
2267*e0c4386eSCy Schubert          * client + client_io, server + server_io.
2268*e0c4386eSCy Schubert          * BIO pairs are symmetric.  A BIO pair behaves similar
2269*e0c4386eSCy Schubert          * to a non-blocking socketpair (but both endpoints must
2270*e0c4386eSCy Schubert          * be handled by the same thread).
2271*e0c4386eSCy Schubert          * [Here we could connect client and server to the ends
2272*e0c4386eSCy Schubert          * of a single BIO pair, but then this code would be less
2273*e0c4386eSCy Schubert          * suitable as an example for BIO pairs in general.]
2274*e0c4386eSCy Schubert          *
2275*e0c4386eSCy Schubert          * Useful functions for querying the state of BIO pair endpoints:
2276*e0c4386eSCy Schubert          *
2277*e0c4386eSCy Schubert          * BIO_ctrl_pending(bio)              number of bytes we can read now
2278*e0c4386eSCy Schubert          * BIO_ctrl_get_read_request(bio)     number of bytes needed to fulfill
2279*e0c4386eSCy Schubert          *                                      other side's read attempt
2280*e0c4386eSCy Schubert          * BIO_ctrl_get_write_guarantee(bio)   number of bytes we can write now
2281*e0c4386eSCy Schubert          *
2282*e0c4386eSCy Schubert          * ..._read_request is never more than ..._write_guarantee;
2283*e0c4386eSCy Schubert          * it depends on the application which one you should use.
2284*e0c4386eSCy Schubert          */
2285*e0c4386eSCy Schubert 
2286*e0c4386eSCy Schubert         /*
2287*e0c4386eSCy Schubert          * We have non-blocking behaviour throughout this test program, but
2288*e0c4386eSCy Schubert          * can be sure that there is *some* progress in each iteration; so we
2289*e0c4386eSCy Schubert          * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2290*e0c4386eSCy Schubert          * we just try everything in each iteration
2291*e0c4386eSCy Schubert          */
2292*e0c4386eSCy Schubert 
2293*e0c4386eSCy Schubert         {
2294*e0c4386eSCy Schubert             /* CLIENT */
2295*e0c4386eSCy Schubert 
2296*e0c4386eSCy Schubert             char cbuf[1024 * 8];
2297*e0c4386eSCy Schubert             int i, r;
2298*e0c4386eSCy Schubert             clock_t c_clock = clock();
2299*e0c4386eSCy Schubert 
2300*e0c4386eSCy Schubert             memset(cbuf, 0, sizeof(cbuf));
2301*e0c4386eSCy Schubert 
2302*e0c4386eSCy Schubert             if (debug)
2303*e0c4386eSCy Schubert                 if (SSL_in_init(c_ssl))
2304*e0c4386eSCy Schubert                     printf("client waiting in SSL_connect - %s\n",
2305*e0c4386eSCy Schubert                            SSL_state_string_long(c_ssl));
2306*e0c4386eSCy Schubert 
2307*e0c4386eSCy Schubert             if (cw_num > 0) {
2308*e0c4386eSCy Schubert                 /* Write to server. */
2309*e0c4386eSCy Schubert 
2310*e0c4386eSCy Schubert                 if (cw_num > (long)sizeof(cbuf))
2311*e0c4386eSCy Schubert                     i = sizeof(cbuf);
2312*e0c4386eSCy Schubert                 else
2313*e0c4386eSCy Schubert                     i = (int)cw_num;
2314*e0c4386eSCy Schubert                 r = BIO_write(c_ssl_bio, cbuf, i);
2315*e0c4386eSCy Schubert                 if (r < 0) {
2316*e0c4386eSCy Schubert                     if (!BIO_should_retry(c_ssl_bio)) {
2317*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in CLIENT\n");
2318*e0c4386eSCy Schubert                         err_in_client = 1;
2319*e0c4386eSCy Schubert                         goto err;
2320*e0c4386eSCy Schubert                     }
2321*e0c4386eSCy Schubert                     /*
2322*e0c4386eSCy Schubert                      * BIO_should_retry(...) can just be ignored here. The
2323*e0c4386eSCy Schubert                      * library expects us to call BIO_write with the same
2324*e0c4386eSCy Schubert                      * arguments again, and that's what we will do in the
2325*e0c4386eSCy Schubert                      * next iteration.
2326*e0c4386eSCy Schubert                      */
2327*e0c4386eSCy Schubert                 } else if (r == 0) {
2328*e0c4386eSCy Schubert                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2329*e0c4386eSCy Schubert                     goto err;
2330*e0c4386eSCy Schubert                 } else {
2331*e0c4386eSCy Schubert                     if (debug)
2332*e0c4386eSCy Schubert                         printf("client wrote %d\n", r);
2333*e0c4386eSCy Schubert                     cw_num -= r;
2334*e0c4386eSCy Schubert                 }
2335*e0c4386eSCy Schubert             }
2336*e0c4386eSCy Schubert 
2337*e0c4386eSCy Schubert             if (cr_num > 0) {
2338*e0c4386eSCy Schubert                 /* Read from server. */
2339*e0c4386eSCy Schubert 
2340*e0c4386eSCy Schubert                 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2341*e0c4386eSCy Schubert                 if (r < 0) {
2342*e0c4386eSCy Schubert                     if (!BIO_should_retry(c_ssl_bio)) {
2343*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in CLIENT\n");
2344*e0c4386eSCy Schubert                         err_in_client = 1;
2345*e0c4386eSCy Schubert                         goto err;
2346*e0c4386eSCy Schubert                     }
2347*e0c4386eSCy Schubert                     /*
2348*e0c4386eSCy Schubert                      * Again, "BIO_should_retry" can be ignored.
2349*e0c4386eSCy Schubert                      */
2350*e0c4386eSCy Schubert                 } else if (r == 0) {
2351*e0c4386eSCy Schubert                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2352*e0c4386eSCy Schubert                     goto err;
2353*e0c4386eSCy Schubert                 } else {
2354*e0c4386eSCy Schubert                     if (debug)
2355*e0c4386eSCy Schubert                         printf("client read %d\n", r);
2356*e0c4386eSCy Schubert                     cr_num -= r;
2357*e0c4386eSCy Schubert                 }
2358*e0c4386eSCy Schubert             }
2359*e0c4386eSCy Schubert 
2360*e0c4386eSCy Schubert             /*
2361*e0c4386eSCy Schubert              * c_time and s_time increments will typically be very small
2362*e0c4386eSCy Schubert              * (depending on machine speed and clock tick intervals), but
2363*e0c4386eSCy Schubert              * sampling over a large number of connections should result in
2364*e0c4386eSCy Schubert              * fairly accurate figures.  We cannot guarantee a lot, however
2365*e0c4386eSCy Schubert              * -- if each connection lasts for exactly one clock tick, it
2366*e0c4386eSCy Schubert              * will be counted only for the client or only for the server or
2367*e0c4386eSCy Schubert              * even not at all.
2368*e0c4386eSCy Schubert              */
2369*e0c4386eSCy Schubert             *c_time += (clock() - c_clock);
2370*e0c4386eSCy Schubert         }
2371*e0c4386eSCy Schubert 
2372*e0c4386eSCy Schubert         {
2373*e0c4386eSCy Schubert             /* SERVER */
2374*e0c4386eSCy Schubert 
2375*e0c4386eSCy Schubert             char sbuf[1024 * 8];
2376*e0c4386eSCy Schubert             int i, r;
2377*e0c4386eSCy Schubert             clock_t s_clock = clock();
2378*e0c4386eSCy Schubert 
2379*e0c4386eSCy Schubert             memset(sbuf, 0, sizeof(sbuf));
2380*e0c4386eSCy Schubert 
2381*e0c4386eSCy Schubert             if (debug)
2382*e0c4386eSCy Schubert                 if (SSL_in_init(s_ssl))
2383*e0c4386eSCy Schubert                     printf("server waiting in SSL_accept - %s\n",
2384*e0c4386eSCy Schubert                            SSL_state_string_long(s_ssl));
2385*e0c4386eSCy Schubert 
2386*e0c4386eSCy Schubert             if (sw_num > 0) {
2387*e0c4386eSCy Schubert                 /* Write to client. */
2388*e0c4386eSCy Schubert 
2389*e0c4386eSCy Schubert                 if (sw_num > (long)sizeof(sbuf))
2390*e0c4386eSCy Schubert                     i = sizeof(sbuf);
2391*e0c4386eSCy Schubert                 else
2392*e0c4386eSCy Schubert                     i = (int)sw_num;
2393*e0c4386eSCy Schubert                 r = BIO_write(s_ssl_bio, sbuf, i);
2394*e0c4386eSCy Schubert                 if (r < 0) {
2395*e0c4386eSCy Schubert                     if (!BIO_should_retry(s_ssl_bio)) {
2396*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in SERVER\n");
2397*e0c4386eSCy Schubert                         err_in_server = 1;
2398*e0c4386eSCy Schubert                         goto err;
2399*e0c4386eSCy Schubert                     }
2400*e0c4386eSCy Schubert                     /* Ignore "BIO_should_retry". */
2401*e0c4386eSCy Schubert                 } else if (r == 0) {
2402*e0c4386eSCy Schubert                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2403*e0c4386eSCy Schubert                     goto err;
2404*e0c4386eSCy Schubert                 } else {
2405*e0c4386eSCy Schubert                     if (debug)
2406*e0c4386eSCy Schubert                         printf("server wrote %d\n", r);
2407*e0c4386eSCy Schubert                     sw_num -= r;
2408*e0c4386eSCy Schubert                 }
2409*e0c4386eSCy Schubert             }
2410*e0c4386eSCy Schubert 
2411*e0c4386eSCy Schubert             if (sr_num > 0) {
2412*e0c4386eSCy Schubert                 /* Read from client. */
2413*e0c4386eSCy Schubert 
2414*e0c4386eSCy Schubert                 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2415*e0c4386eSCy Schubert                 if (r < 0) {
2416*e0c4386eSCy Schubert                     if (!BIO_should_retry(s_ssl_bio)) {
2417*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in SERVER\n");
2418*e0c4386eSCy Schubert                         err_in_server = 1;
2419*e0c4386eSCy Schubert                         goto err;
2420*e0c4386eSCy Schubert                     }
2421*e0c4386eSCy Schubert                     /* blah, blah */
2422*e0c4386eSCy Schubert                 } else if (r == 0) {
2423*e0c4386eSCy Schubert                     fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2424*e0c4386eSCy Schubert                     goto err;
2425*e0c4386eSCy Schubert                 } else {
2426*e0c4386eSCy Schubert                     if (debug)
2427*e0c4386eSCy Schubert                         printf("server read %d\n", r);
2428*e0c4386eSCy Schubert                     sr_num -= r;
2429*e0c4386eSCy Schubert                 }
2430*e0c4386eSCy Schubert             }
2431*e0c4386eSCy Schubert 
2432*e0c4386eSCy Schubert             *s_time += (clock() - s_clock);
2433*e0c4386eSCy Schubert         }
2434*e0c4386eSCy Schubert 
2435*e0c4386eSCy Schubert         {
2436*e0c4386eSCy Schubert             /* "I/O" BETWEEN CLIENT AND SERVER. */
2437*e0c4386eSCy Schubert 
2438*e0c4386eSCy Schubert             size_t r1, r2;
2439*e0c4386eSCy Schubert             BIO *io1 = server_io, *io2 = client_io;
2440*e0c4386eSCy Schubert             /*
2441*e0c4386eSCy Schubert              * we use the non-copying interface for io1 and the standard
2442*e0c4386eSCy Schubert              * BIO_write/BIO_read interface for io2
2443*e0c4386eSCy Schubert              */
2444*e0c4386eSCy Schubert 
2445*e0c4386eSCy Schubert             static int prev_progress = 1;
2446*e0c4386eSCy Schubert             int progress = 0;
2447*e0c4386eSCy Schubert 
2448*e0c4386eSCy Schubert             /* io1 to io2 */
2449*e0c4386eSCy Schubert             do {
2450*e0c4386eSCy Schubert                 size_t num;
2451*e0c4386eSCy Schubert                 int r;
2452*e0c4386eSCy Schubert 
2453*e0c4386eSCy Schubert                 r1 = BIO_ctrl_pending(io1);
2454*e0c4386eSCy Schubert                 r2 = BIO_ctrl_get_write_guarantee(io2);
2455*e0c4386eSCy Schubert 
2456*e0c4386eSCy Schubert                 num = r1;
2457*e0c4386eSCy Schubert                 if (r2 < num)
2458*e0c4386eSCy Schubert                     num = r2;
2459*e0c4386eSCy Schubert                 if (num) {
2460*e0c4386eSCy Schubert                     char *dataptr;
2461*e0c4386eSCy Schubert 
2462*e0c4386eSCy Schubert                     if (INT_MAX < num) /* yeah, right */
2463*e0c4386eSCy Schubert                         num = INT_MAX;
2464*e0c4386eSCy Schubert 
2465*e0c4386eSCy Schubert                     r = BIO_nread(io1, &dataptr, (int)num);
2466*e0c4386eSCy Schubert                     assert(r > 0);
2467*e0c4386eSCy Schubert                     assert(r <= (int)num);
2468*e0c4386eSCy Schubert                     /*
2469*e0c4386eSCy Schubert                      * possibly r < num (non-contiguous data)
2470*e0c4386eSCy Schubert                      */
2471*e0c4386eSCy Schubert                     num = r;
2472*e0c4386eSCy Schubert                     r = BIO_write(io2, dataptr, (int)num);
2473*e0c4386eSCy Schubert                     if (r != (int)num) { /* can't happen */
2474*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR: BIO_write could not write "
2475*e0c4386eSCy Schubert                                 "BIO_ctrl_get_write_guarantee() bytes");
2476*e0c4386eSCy Schubert                         goto err;
2477*e0c4386eSCy Schubert                     }
2478*e0c4386eSCy Schubert                     progress = 1;
2479*e0c4386eSCy Schubert 
2480*e0c4386eSCy Schubert                     if (debug)
2481*e0c4386eSCy Schubert                         printf((io1 == client_io) ?
2482*e0c4386eSCy Schubert                                "C->S relaying: %d bytes\n" :
2483*e0c4386eSCy Schubert                                "S->C relaying: %d bytes\n", (int)num);
2484*e0c4386eSCy Schubert                 }
2485*e0c4386eSCy Schubert             }
2486*e0c4386eSCy Schubert             while (r1 && r2);
2487*e0c4386eSCy Schubert 
2488*e0c4386eSCy Schubert             /* io2 to io1 */
2489*e0c4386eSCy Schubert             {
2490*e0c4386eSCy Schubert                 size_t num;
2491*e0c4386eSCy Schubert                 int r;
2492*e0c4386eSCy Schubert 
2493*e0c4386eSCy Schubert                 r1 = BIO_ctrl_pending(io2);
2494*e0c4386eSCy Schubert                 r2 = BIO_ctrl_get_read_request(io1);
2495*e0c4386eSCy Schubert                 /*
2496*e0c4386eSCy Schubert                  * here we could use ..._get_write_guarantee instead of
2497*e0c4386eSCy Schubert                  * ..._get_read_request, but by using the latter we test
2498*e0c4386eSCy Schubert                  * restartability of the SSL implementation more thoroughly
2499*e0c4386eSCy Schubert                  */
2500*e0c4386eSCy Schubert                 num = r1;
2501*e0c4386eSCy Schubert                 if (r2 < num)
2502*e0c4386eSCy Schubert                     num = r2;
2503*e0c4386eSCy Schubert                 if (num) {
2504*e0c4386eSCy Schubert                     char *dataptr;
2505*e0c4386eSCy Schubert 
2506*e0c4386eSCy Schubert                     if (INT_MAX < num)
2507*e0c4386eSCy Schubert                         num = INT_MAX;
2508*e0c4386eSCy Schubert 
2509*e0c4386eSCy Schubert                     if (num > 1)
2510*e0c4386eSCy Schubert                         --num;  /* test restartability even more thoroughly */
2511*e0c4386eSCy Schubert 
2512*e0c4386eSCy Schubert                     r = BIO_nwrite0(io1, &dataptr);
2513*e0c4386eSCy Schubert                     assert(r > 0);
2514*e0c4386eSCy Schubert                     if (r < (int)num)
2515*e0c4386eSCy Schubert                         num = r;
2516*e0c4386eSCy Schubert                     r = BIO_read(io2, dataptr, (int)num);
2517*e0c4386eSCy Schubert                     if (r != (int)num) { /* can't happen */
2518*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR: BIO_read could not read "
2519*e0c4386eSCy Schubert                                 "BIO_ctrl_pending() bytes");
2520*e0c4386eSCy Schubert                         goto err;
2521*e0c4386eSCy Schubert                     }
2522*e0c4386eSCy Schubert                     progress = 1;
2523*e0c4386eSCy Schubert                     r = BIO_nwrite(io1, &dataptr, (int)num);
2524*e0c4386eSCy Schubert                     if (r != (int)num) { /* can't happen */
2525*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
2526*e0c4386eSCy Schubert                                 "BIO_nwrite0() bytes");
2527*e0c4386eSCy Schubert                         goto err;
2528*e0c4386eSCy Schubert                     }
2529*e0c4386eSCy Schubert 
2530*e0c4386eSCy Schubert                     if (debug)
2531*e0c4386eSCy Schubert                         printf((io2 == client_io) ?
2532*e0c4386eSCy Schubert                                "C->S relaying: %d bytes\n" :
2533*e0c4386eSCy Schubert                                "S->C relaying: %d bytes\n", (int)num);
2534*e0c4386eSCy Schubert                 }
2535*e0c4386eSCy Schubert             }                   /* no loop, BIO_ctrl_get_read_request now
2536*e0c4386eSCy Schubert                                  * returns 0 anyway */
2537*e0c4386eSCy Schubert 
2538*e0c4386eSCy Schubert             if (!progress && !prev_progress)
2539*e0c4386eSCy Schubert                 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
2540*e0c4386eSCy Schubert                     fprintf(stderr, "ERROR: got stuck\n");
2541*e0c4386eSCy Schubert                     fprintf(stderr, " ERROR.\n");
2542*e0c4386eSCy Schubert                     goto err;
2543*e0c4386eSCy Schubert                 }
2544*e0c4386eSCy Schubert             prev_progress = progress;
2545*e0c4386eSCy Schubert         }
2546*e0c4386eSCy Schubert     }
2547*e0c4386eSCy Schubert     while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2548*e0c4386eSCy Schubert 
2549*e0c4386eSCy Schubert     if (verbose)
2550*e0c4386eSCy Schubert         print_details(c_ssl, "DONE via BIO pair: ");
2551*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
2552*e0c4386eSCy Schubert     if (verify_npn(c_ssl, s_ssl) < 0)
2553*e0c4386eSCy Schubert         goto end;
2554*e0c4386eSCy Schubert #endif
2555*e0c4386eSCy Schubert     if (verify_serverinfo() < 0) {
2556*e0c4386eSCy Schubert         fprintf(stderr, "Server info verify error\n");
2557*e0c4386eSCy Schubert         goto err;
2558*e0c4386eSCy Schubert     }
2559*e0c4386eSCy Schubert     if (verify_alpn(c_ssl, s_ssl) < 0
2560*e0c4386eSCy Schubert             || verify_servername(c_ssl, s_ssl) < 0)
2561*e0c4386eSCy Schubert         goto err;
2562*e0c4386eSCy Schubert 
2563*e0c4386eSCy Schubert     if (custom_ext_error) {
2564*e0c4386eSCy Schubert         fprintf(stderr, "Custom extension error\n");
2565*e0c4386eSCy Schubert         goto err;
2566*e0c4386eSCy Schubert     }
2567*e0c4386eSCy Schubert 
2568*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
2569*e0c4386eSCy Schubert  end:
2570*e0c4386eSCy Schubert #endif
2571*e0c4386eSCy Schubert     ret = EXIT_SUCCESS;
2572*e0c4386eSCy Schubert 
2573*e0c4386eSCy Schubert  err:
2574*e0c4386eSCy Schubert     ERR_print_errors(bio_err);
2575*e0c4386eSCy Schubert 
2576*e0c4386eSCy Schubert     BIO_free(server);
2577*e0c4386eSCy Schubert     BIO_free(server_io);
2578*e0c4386eSCy Schubert     BIO_free(client);
2579*e0c4386eSCy Schubert     BIO_free(client_io);
2580*e0c4386eSCy Schubert     BIO_free(s_ssl_bio);
2581*e0c4386eSCy Schubert     BIO_free(c_ssl_bio);
2582*e0c4386eSCy Schubert 
2583*e0c4386eSCy Schubert     if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2584*e0c4386eSCy Schubert         ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2585*e0c4386eSCy Schubert     else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2586*e0c4386eSCy Schubert         ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2587*e0c4386eSCy Schubert 
2588*e0c4386eSCy Schubert     return ret;
2589*e0c4386eSCy Schubert }
2590*e0c4386eSCy Schubert 
2591*e0c4386eSCy Schubert #define W_READ  1
2592*e0c4386eSCy Schubert #define W_WRITE 2
2593*e0c4386eSCy Schubert #define C_DONE  1
2594*e0c4386eSCy Schubert #define S_DONE  2
2595*e0c4386eSCy Schubert 
doit(SSL * s_ssl,SSL * c_ssl,long count)2596*e0c4386eSCy Schubert int doit(SSL *s_ssl, SSL *c_ssl, long count)
2597*e0c4386eSCy Schubert {
2598*e0c4386eSCy Schubert     char *cbuf = NULL, *sbuf = NULL;
2599*e0c4386eSCy Schubert     long bufsiz;
2600*e0c4386eSCy Schubert     long cw_num = count, cr_num = count;
2601*e0c4386eSCy Schubert     long sw_num = count, sr_num = count;
2602*e0c4386eSCy Schubert     int ret = EXIT_FAILURE;
2603*e0c4386eSCy Schubert     BIO *c_to_s = NULL;
2604*e0c4386eSCy Schubert     BIO *s_to_c = NULL;
2605*e0c4386eSCy Schubert     BIO *c_bio = NULL;
2606*e0c4386eSCy Schubert     BIO *s_bio = NULL;
2607*e0c4386eSCy Schubert     int c_r, c_w, s_r, s_w;
2608*e0c4386eSCy Schubert     int i, j;
2609*e0c4386eSCy Schubert     int done = 0;
2610*e0c4386eSCy Schubert     int c_write, s_write;
2611*e0c4386eSCy Schubert     int do_server = 0, do_client = 0;
2612*e0c4386eSCy Schubert     int max_frag = 5 * 1024;
2613*e0c4386eSCy Schubert     int err_in_client = 0;
2614*e0c4386eSCy Schubert     int err_in_server = 0;
2615*e0c4386eSCy Schubert 
2616*e0c4386eSCy Schubert     bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
2617*e0c4386eSCy Schubert 
2618*e0c4386eSCy Schubert     if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2619*e0c4386eSCy Schubert         goto err;
2620*e0c4386eSCy Schubert     if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
2621*e0c4386eSCy Schubert         goto err;
2622*e0c4386eSCy Schubert 
2623*e0c4386eSCy Schubert     c_to_s = BIO_new(BIO_s_mem());
2624*e0c4386eSCy Schubert     s_to_c = BIO_new(BIO_s_mem());
2625*e0c4386eSCy Schubert     if ((s_to_c == NULL) || (c_to_s == NULL)) {
2626*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
2627*e0c4386eSCy Schubert         goto err;
2628*e0c4386eSCy Schubert     }
2629*e0c4386eSCy Schubert 
2630*e0c4386eSCy Schubert     c_bio = BIO_new(BIO_f_ssl());
2631*e0c4386eSCy Schubert     s_bio = BIO_new(BIO_f_ssl());
2632*e0c4386eSCy Schubert     if ((c_bio == NULL) || (s_bio == NULL)) {
2633*e0c4386eSCy Schubert         ERR_print_errors(bio_err);
2634*e0c4386eSCy Schubert         goto err;
2635*e0c4386eSCy Schubert     }
2636*e0c4386eSCy Schubert 
2637*e0c4386eSCy Schubert     SSL_set_connect_state(c_ssl);
2638*e0c4386eSCy Schubert     SSL_set_bio(c_ssl, s_to_c, c_to_s);
2639*e0c4386eSCy Schubert     SSL_set_max_send_fragment(c_ssl, max_frag);
2640*e0c4386eSCy Schubert     BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
2641*e0c4386eSCy Schubert 
2642*e0c4386eSCy Schubert     /*
2643*e0c4386eSCy Schubert      * We've just given our ref to these BIOs to c_ssl. We need another one to
2644*e0c4386eSCy Schubert      * give to s_ssl
2645*e0c4386eSCy Schubert      */
2646*e0c4386eSCy Schubert     if (!BIO_up_ref(c_to_s)) {
2647*e0c4386eSCy Schubert         /* c_to_s and s_to_c will get freed when we free c_ssl */
2648*e0c4386eSCy Schubert         c_to_s = NULL;
2649*e0c4386eSCy Schubert         s_to_c = NULL;
2650*e0c4386eSCy Schubert         goto err;
2651*e0c4386eSCy Schubert     }
2652*e0c4386eSCy Schubert     if (!BIO_up_ref(s_to_c)) {
2653*e0c4386eSCy Schubert         /* s_to_c will get freed when we free c_ssl */
2654*e0c4386eSCy Schubert         s_to_c = NULL;
2655*e0c4386eSCy Schubert         goto err;
2656*e0c4386eSCy Schubert     }
2657*e0c4386eSCy Schubert 
2658*e0c4386eSCy Schubert     SSL_set_accept_state(s_ssl);
2659*e0c4386eSCy Schubert     SSL_set_bio(s_ssl, c_to_s, s_to_c);
2660*e0c4386eSCy Schubert 
2661*e0c4386eSCy Schubert     /* We've used up all our refs to these now */
2662*e0c4386eSCy Schubert     c_to_s = NULL;
2663*e0c4386eSCy Schubert     s_to_c = NULL;
2664*e0c4386eSCy Schubert 
2665*e0c4386eSCy Schubert     SSL_set_max_send_fragment(s_ssl, max_frag);
2666*e0c4386eSCy Schubert     BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
2667*e0c4386eSCy Schubert 
2668*e0c4386eSCy Schubert     c_r = 0;
2669*e0c4386eSCy Schubert     s_r = 1;
2670*e0c4386eSCy Schubert     c_w = 1;
2671*e0c4386eSCy Schubert     s_w = 0;
2672*e0c4386eSCy Schubert     c_write = 1, s_write = 0;
2673*e0c4386eSCy Schubert 
2674*e0c4386eSCy Schubert     /* We can always do writes */
2675*e0c4386eSCy Schubert     for (;;) {
2676*e0c4386eSCy Schubert         do_server = 0;
2677*e0c4386eSCy Schubert         do_client = 0;
2678*e0c4386eSCy Schubert 
2679*e0c4386eSCy Schubert         i = (int)BIO_pending(s_bio);
2680*e0c4386eSCy Schubert         if ((i && s_r) || s_w)
2681*e0c4386eSCy Schubert             do_server = 1;
2682*e0c4386eSCy Schubert 
2683*e0c4386eSCy Schubert         i = (int)BIO_pending(c_bio);
2684*e0c4386eSCy Schubert         if ((i && c_r) || c_w)
2685*e0c4386eSCy Schubert             do_client = 1;
2686*e0c4386eSCy Schubert 
2687*e0c4386eSCy Schubert         if (do_server && debug) {
2688*e0c4386eSCy Schubert             if (SSL_in_init(s_ssl))
2689*e0c4386eSCy Schubert                 printf("server waiting in SSL_accept - %s\n",
2690*e0c4386eSCy Schubert                        SSL_state_string_long(s_ssl));
2691*e0c4386eSCy Schubert         }
2692*e0c4386eSCy Schubert 
2693*e0c4386eSCy Schubert         if (do_client && debug) {
2694*e0c4386eSCy Schubert             if (SSL_in_init(c_ssl))
2695*e0c4386eSCy Schubert                 printf("client waiting in SSL_connect - %s\n",
2696*e0c4386eSCy Schubert                        SSL_state_string_long(c_ssl));
2697*e0c4386eSCy Schubert         }
2698*e0c4386eSCy Schubert 
2699*e0c4386eSCy Schubert         if (!do_client && !do_server) {
2700*e0c4386eSCy Schubert             fprintf(stdout, "ERROR IN STARTUP\n");
2701*e0c4386eSCy Schubert             ERR_print_errors(bio_err);
2702*e0c4386eSCy Schubert             goto err;
2703*e0c4386eSCy Schubert         }
2704*e0c4386eSCy Schubert         if (do_client && !(done & C_DONE)) {
2705*e0c4386eSCy Schubert             if (c_write) {
2706*e0c4386eSCy Schubert                 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
2707*e0c4386eSCy Schubert                 i = BIO_write(c_bio, cbuf, j);
2708*e0c4386eSCy Schubert                 if (i < 0) {
2709*e0c4386eSCy Schubert                     c_r = 0;
2710*e0c4386eSCy Schubert                     c_w = 0;
2711*e0c4386eSCy Schubert                     if (BIO_should_retry(c_bio)) {
2712*e0c4386eSCy Schubert                         if (BIO_should_read(c_bio))
2713*e0c4386eSCy Schubert                             c_r = 1;
2714*e0c4386eSCy Schubert                         if (BIO_should_write(c_bio))
2715*e0c4386eSCy Schubert                             c_w = 1;
2716*e0c4386eSCy Schubert                     } else {
2717*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in CLIENT\n");
2718*e0c4386eSCy Schubert                         err_in_client = 1;
2719*e0c4386eSCy Schubert                         ERR_print_errors(bio_err);
2720*e0c4386eSCy Schubert                         goto err;
2721*e0c4386eSCy Schubert                     }
2722*e0c4386eSCy Schubert                 } else if (i == 0) {
2723*e0c4386eSCy Schubert                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2724*e0c4386eSCy Schubert                     goto err;
2725*e0c4386eSCy Schubert                 } else {
2726*e0c4386eSCy Schubert                     if (debug)
2727*e0c4386eSCy Schubert                         printf("client wrote %d\n", i);
2728*e0c4386eSCy Schubert                     /* ok */
2729*e0c4386eSCy Schubert                     s_r = 1;
2730*e0c4386eSCy Schubert                     c_write = 0;
2731*e0c4386eSCy Schubert                     cw_num -= i;
2732*e0c4386eSCy Schubert                     if (max_frag > 1029)
2733*e0c4386eSCy Schubert                         SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
2734*e0c4386eSCy Schubert                 }
2735*e0c4386eSCy Schubert             } else {
2736*e0c4386eSCy Schubert                 i = BIO_read(c_bio, cbuf, bufsiz);
2737*e0c4386eSCy Schubert                 if (i < 0) {
2738*e0c4386eSCy Schubert                     c_r = 0;
2739*e0c4386eSCy Schubert                     c_w = 0;
2740*e0c4386eSCy Schubert                     if (BIO_should_retry(c_bio)) {
2741*e0c4386eSCy Schubert                         if (BIO_should_read(c_bio))
2742*e0c4386eSCy Schubert                             c_r = 1;
2743*e0c4386eSCy Schubert                         if (BIO_should_write(c_bio))
2744*e0c4386eSCy Schubert                             c_w = 1;
2745*e0c4386eSCy Schubert                     } else {
2746*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in CLIENT\n");
2747*e0c4386eSCy Schubert                         err_in_client = 1;
2748*e0c4386eSCy Schubert                         ERR_print_errors(bio_err);
2749*e0c4386eSCy Schubert                         goto err;
2750*e0c4386eSCy Schubert                     }
2751*e0c4386eSCy Schubert                 } else if (i == 0) {
2752*e0c4386eSCy Schubert                     fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2753*e0c4386eSCy Schubert                     goto err;
2754*e0c4386eSCy Schubert                 } else {
2755*e0c4386eSCy Schubert                     if (debug)
2756*e0c4386eSCy Schubert                         printf("client read %d\n", i);
2757*e0c4386eSCy Schubert                     cr_num -= i;
2758*e0c4386eSCy Schubert                     if (sw_num > 0) {
2759*e0c4386eSCy Schubert                         s_write = 1;
2760*e0c4386eSCy Schubert                         s_w = 1;
2761*e0c4386eSCy Schubert                     }
2762*e0c4386eSCy Schubert                     if (cr_num <= 0) {
2763*e0c4386eSCy Schubert                         s_write = 1;
2764*e0c4386eSCy Schubert                         s_w = 1;
2765*e0c4386eSCy Schubert                         done = S_DONE | C_DONE;
2766*e0c4386eSCy Schubert                     }
2767*e0c4386eSCy Schubert                 }
2768*e0c4386eSCy Schubert             }
2769*e0c4386eSCy Schubert         }
2770*e0c4386eSCy Schubert 
2771*e0c4386eSCy Schubert         if (do_server && !(done & S_DONE)) {
2772*e0c4386eSCy Schubert             if (!s_write) {
2773*e0c4386eSCy Schubert                 i = BIO_read(s_bio, sbuf, bufsiz);
2774*e0c4386eSCy Schubert                 if (i < 0) {
2775*e0c4386eSCy Schubert                     s_r = 0;
2776*e0c4386eSCy Schubert                     s_w = 0;
2777*e0c4386eSCy Schubert                     if (BIO_should_retry(s_bio)) {
2778*e0c4386eSCy Schubert                         if (BIO_should_read(s_bio))
2779*e0c4386eSCy Schubert                             s_r = 1;
2780*e0c4386eSCy Schubert                         if (BIO_should_write(s_bio))
2781*e0c4386eSCy Schubert                             s_w = 1;
2782*e0c4386eSCy Schubert                     } else {
2783*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in SERVER\n");
2784*e0c4386eSCy Schubert                         err_in_server = 1;
2785*e0c4386eSCy Schubert                         ERR_print_errors(bio_err);
2786*e0c4386eSCy Schubert                         goto err;
2787*e0c4386eSCy Schubert                     }
2788*e0c4386eSCy Schubert                 } else if (i == 0) {
2789*e0c4386eSCy Schubert                     ERR_print_errors(bio_err);
2790*e0c4386eSCy Schubert                     fprintf(stderr,
2791*e0c4386eSCy Schubert                             "SSL SERVER STARTUP FAILED in SSL_read\n");
2792*e0c4386eSCy Schubert                     goto err;
2793*e0c4386eSCy Schubert                 } else {
2794*e0c4386eSCy Schubert                     if (debug)
2795*e0c4386eSCy Schubert                         printf("server read %d\n", i);
2796*e0c4386eSCy Schubert                     sr_num -= i;
2797*e0c4386eSCy Schubert                     if (cw_num > 0) {
2798*e0c4386eSCy Schubert                         c_write = 1;
2799*e0c4386eSCy Schubert                         c_w = 1;
2800*e0c4386eSCy Schubert                     }
2801*e0c4386eSCy Schubert                     if (sr_num <= 0) {
2802*e0c4386eSCy Schubert                         s_write = 1;
2803*e0c4386eSCy Schubert                         s_w = 1;
2804*e0c4386eSCy Schubert                         c_write = 0;
2805*e0c4386eSCy Schubert                     }
2806*e0c4386eSCy Schubert                 }
2807*e0c4386eSCy Schubert             } else {
2808*e0c4386eSCy Schubert                 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
2809*e0c4386eSCy Schubert                 i = BIO_write(s_bio, sbuf, j);
2810*e0c4386eSCy Schubert                 if (i < 0) {
2811*e0c4386eSCy Schubert                     s_r = 0;
2812*e0c4386eSCy Schubert                     s_w = 0;
2813*e0c4386eSCy Schubert                     if (BIO_should_retry(s_bio)) {
2814*e0c4386eSCy Schubert                         if (BIO_should_read(s_bio))
2815*e0c4386eSCy Schubert                             s_r = 1;
2816*e0c4386eSCy Schubert                         if (BIO_should_write(s_bio))
2817*e0c4386eSCy Schubert                             s_w = 1;
2818*e0c4386eSCy Schubert                     } else {
2819*e0c4386eSCy Schubert                         fprintf(stderr, "ERROR in SERVER\n");
2820*e0c4386eSCy Schubert                         err_in_server = 1;
2821*e0c4386eSCy Schubert                         ERR_print_errors(bio_err);
2822*e0c4386eSCy Schubert                         goto err;
2823*e0c4386eSCy Schubert                     }
2824*e0c4386eSCy Schubert                 } else if (i == 0) {
2825*e0c4386eSCy Schubert                     ERR_print_errors(bio_err);
2826*e0c4386eSCy Schubert                     fprintf(stderr,
2827*e0c4386eSCy Schubert                             "SSL SERVER STARTUP FAILED in SSL_write\n");
2828*e0c4386eSCy Schubert                     goto err;
2829*e0c4386eSCy Schubert                 } else {
2830*e0c4386eSCy Schubert                     if (debug)
2831*e0c4386eSCy Schubert                         printf("server wrote %d\n", i);
2832*e0c4386eSCy Schubert                     sw_num -= i;
2833*e0c4386eSCy Schubert                     s_write = 0;
2834*e0c4386eSCy Schubert                     c_r = 1;
2835*e0c4386eSCy Schubert                     if (sw_num <= 0)
2836*e0c4386eSCy Schubert                         done |= S_DONE;
2837*e0c4386eSCy Schubert                     if (max_frag > 1029)
2838*e0c4386eSCy Schubert                         SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
2839*e0c4386eSCy Schubert                 }
2840*e0c4386eSCy Schubert             }
2841*e0c4386eSCy Schubert         }
2842*e0c4386eSCy Schubert 
2843*e0c4386eSCy Schubert         if ((done & S_DONE) && (done & C_DONE))
2844*e0c4386eSCy Schubert             break;
2845*e0c4386eSCy Schubert     }
2846*e0c4386eSCy Schubert 
2847*e0c4386eSCy Schubert     if (verbose)
2848*e0c4386eSCy Schubert         print_details(c_ssl, "DONE: ");
2849*e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG
2850*e0c4386eSCy Schubert     if (verify_npn(c_ssl, s_ssl) < 0)
2851*e0c4386eSCy Schubert         goto err;
2852*e0c4386eSCy Schubert #endif
2853*e0c4386eSCy Schubert     if (verify_serverinfo() < 0) {
2854*e0c4386eSCy Schubert         fprintf(stderr, "Server info verify error\n");
2855*e0c4386eSCy Schubert         goto err;
2856*e0c4386eSCy Schubert     }
2857*e0c4386eSCy Schubert     if (custom_ext_error) {
2858*e0c4386eSCy Schubert         fprintf(stderr, "Custom extension error\n");
2859*e0c4386eSCy Schubert         goto err;
2860*e0c4386eSCy Schubert     }
2861*e0c4386eSCy Schubert     ret = EXIT_SUCCESS;
2862*e0c4386eSCy Schubert  err:
2863*e0c4386eSCy Schubert     BIO_free(c_to_s);
2864*e0c4386eSCy Schubert     BIO_free(s_to_c);
2865*e0c4386eSCy Schubert     BIO_free_all(c_bio);
2866*e0c4386eSCy Schubert     BIO_free_all(s_bio);
2867*e0c4386eSCy Schubert     OPENSSL_free(cbuf);
2868*e0c4386eSCy Schubert     OPENSSL_free(sbuf);
2869*e0c4386eSCy Schubert 
2870*e0c4386eSCy Schubert     if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
2871*e0c4386eSCy Schubert         ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2872*e0c4386eSCy Schubert     else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
2873*e0c4386eSCy Schubert         ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
2874*e0c4386eSCy Schubert 
2875*e0c4386eSCy Schubert     return ret;
2876*e0c4386eSCy Schubert }
2877*e0c4386eSCy Schubert 
verify_callback(int ok,X509_STORE_CTX * ctx)2878*e0c4386eSCy Schubert static int verify_callback(int ok, X509_STORE_CTX *ctx)
2879*e0c4386eSCy Schubert {
2880*e0c4386eSCy Schubert     char *s, buf[256];
2881*e0c4386eSCy Schubert 
2882*e0c4386eSCy Schubert     s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
2883*e0c4386eSCy Schubert                           buf, sizeof(buf));
2884*e0c4386eSCy Schubert     if (s != NULL) {
2885*e0c4386eSCy Schubert         if (ok)
2886*e0c4386eSCy Schubert             printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
2887*e0c4386eSCy Schubert         else {
2888*e0c4386eSCy Schubert             fprintf(stderr, "depth=%d error=%d %s\n",
2889*e0c4386eSCy Schubert                     X509_STORE_CTX_get_error_depth(ctx),
2890*e0c4386eSCy Schubert                     X509_STORE_CTX_get_error(ctx), buf);
2891*e0c4386eSCy Schubert         }
2892*e0c4386eSCy Schubert     }
2893*e0c4386eSCy Schubert 
2894*e0c4386eSCy Schubert     if (ok == 0) {
2895*e0c4386eSCy Schubert         int i = X509_STORE_CTX_get_error(ctx);
2896*e0c4386eSCy Schubert 
2897*e0c4386eSCy Schubert         switch (i) {
2898*e0c4386eSCy Schubert         default:
2899*e0c4386eSCy Schubert             fprintf(stderr, "Error string: %s\n",
2900*e0c4386eSCy Schubert                     X509_verify_cert_error_string(i));
2901*e0c4386eSCy Schubert             break;
2902*e0c4386eSCy Schubert         case X509_V_ERR_CERT_NOT_YET_VALID:
2903*e0c4386eSCy Schubert         case X509_V_ERR_CERT_HAS_EXPIRED:
2904*e0c4386eSCy Schubert         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
2905*e0c4386eSCy Schubert             ok = 1;
2906*e0c4386eSCy Schubert             break;
2907*e0c4386eSCy Schubert         }
2908*e0c4386eSCy Schubert     }
2909*e0c4386eSCy Schubert 
2910*e0c4386eSCy Schubert     return ok;
2911*e0c4386eSCy Schubert }
2912*e0c4386eSCy Schubert 
app_verify_callback(X509_STORE_CTX * ctx,void * arg)2913*e0c4386eSCy Schubert static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
2914*e0c4386eSCy Schubert {
2915*e0c4386eSCy Schubert     int ok = 1;
2916*e0c4386eSCy Schubert     struct app_verify_arg *cb_arg = arg;
2917*e0c4386eSCy Schubert 
2918*e0c4386eSCy Schubert     if (cb_arg->app_verify) {
2919*e0c4386eSCy Schubert         char *s = NULL, buf[256];
2920*e0c4386eSCy Schubert         X509 *c = X509_STORE_CTX_get0_cert(ctx);
2921*e0c4386eSCy Schubert 
2922*e0c4386eSCy Schubert         printf("In app_verify_callback, allowing cert. ");
2923*e0c4386eSCy Schubert         printf("Arg is: %s\n", cb_arg->string);
2924*e0c4386eSCy Schubert         printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
2925*e0c4386eSCy Schubert                 (void *)ctx, (void *)c);
2926*e0c4386eSCy Schubert         if (c)
2927*e0c4386eSCy Schubert             s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
2928*e0c4386eSCy Schubert         if (s != NULL) {
2929*e0c4386eSCy Schubert             printf("cert depth=%d %s\n",
2930*e0c4386eSCy Schubert                     X509_STORE_CTX_get_error_depth(ctx), buf);
2931*e0c4386eSCy Schubert         }
2932*e0c4386eSCy Schubert         return 1;
2933*e0c4386eSCy Schubert     }
2934*e0c4386eSCy Schubert 
2935*e0c4386eSCy Schubert     ok = X509_verify_cert(ctx);
2936*e0c4386eSCy Schubert 
2937*e0c4386eSCy Schubert     return ok;
2938*e0c4386eSCy Schubert }
2939*e0c4386eSCy Schubert 
2940*e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
2941*e0c4386eSCy Schubert /* convert the PSK key (psk_key) in ascii to binary (psk) */
psk_key2bn(const char * pskkey,unsigned char * psk,unsigned int max_psk_len)2942*e0c4386eSCy Schubert static int psk_key2bn(const char *pskkey, unsigned char *psk,
2943*e0c4386eSCy Schubert                       unsigned int max_psk_len)
2944*e0c4386eSCy Schubert {
2945*e0c4386eSCy Schubert     int ret;
2946*e0c4386eSCy Schubert     BIGNUM *bn = NULL;
2947*e0c4386eSCy Schubert 
2948*e0c4386eSCy Schubert     ret = BN_hex2bn(&bn, pskkey);
2949*e0c4386eSCy Schubert     if (!ret) {
2950*e0c4386eSCy Schubert         BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
2951*e0c4386eSCy Schubert                    pskkey);
2952*e0c4386eSCy Schubert         BN_free(bn);
2953*e0c4386eSCy Schubert         return 0;
2954*e0c4386eSCy Schubert     }
2955*e0c4386eSCy Schubert     if (BN_num_bytes(bn) > (int)max_psk_len) {
2956*e0c4386eSCy Schubert         BIO_printf(bio_err,
2957*e0c4386eSCy Schubert                    "psk buffer of callback is too small (%d) for key (%d)\n",
2958*e0c4386eSCy Schubert                    max_psk_len, BN_num_bytes(bn));
2959*e0c4386eSCy Schubert         BN_free(bn);
2960*e0c4386eSCy Schubert         return 0;
2961*e0c4386eSCy Schubert     }
2962*e0c4386eSCy Schubert     ret = BN_bn2bin(bn, psk);
2963*e0c4386eSCy Schubert     BN_free(bn);
2964*e0c4386eSCy Schubert     return ret;
2965*e0c4386eSCy Schubert }
2966*e0c4386eSCy Schubert 
psk_client_callback(SSL * ssl,const char * hint,char * identity,unsigned int max_identity_len,unsigned char * psk,unsigned int max_psk_len)2967*e0c4386eSCy Schubert static unsigned int psk_client_callback(SSL *ssl, const char *hint,
2968*e0c4386eSCy Schubert                                         char *identity,
2969*e0c4386eSCy Schubert                                         unsigned int max_identity_len,
2970*e0c4386eSCy Schubert                                         unsigned char *psk,
2971*e0c4386eSCy Schubert                                         unsigned int max_psk_len)
2972*e0c4386eSCy Schubert {
2973*e0c4386eSCy Schubert     int ret;
2974*e0c4386eSCy Schubert     unsigned int psk_len = 0;
2975*e0c4386eSCy Schubert 
2976*e0c4386eSCy Schubert     ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
2977*e0c4386eSCy Schubert     if (ret < 0)
2978*e0c4386eSCy Schubert         goto out_err;
2979*e0c4386eSCy Schubert     if (debug)
2980*e0c4386eSCy Schubert         fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
2981*e0c4386eSCy Schubert                 ret);
2982*e0c4386eSCy Schubert     ret = psk_key2bn(psk_key, psk, max_psk_len);
2983*e0c4386eSCy Schubert     if (ret < 0)
2984*e0c4386eSCy Schubert         goto out_err;
2985*e0c4386eSCy Schubert     psk_len = ret;
2986*e0c4386eSCy Schubert  out_err:
2987*e0c4386eSCy Schubert     return psk_len;
2988*e0c4386eSCy Schubert }
2989*e0c4386eSCy Schubert 
psk_server_callback(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len)2990*e0c4386eSCy Schubert static unsigned int psk_server_callback(SSL *ssl, const char *identity,
2991*e0c4386eSCy Schubert                                         unsigned char *psk,
2992*e0c4386eSCy Schubert                                         unsigned int max_psk_len)
2993*e0c4386eSCy Schubert {
2994*e0c4386eSCy Schubert     unsigned int psk_len = 0;
2995*e0c4386eSCy Schubert 
2996*e0c4386eSCy Schubert     if (strcmp(identity, "Client_identity") != 0) {
2997*e0c4386eSCy Schubert         BIO_printf(bio_err, "server: PSK error: client identity not found\n");
2998*e0c4386eSCy Schubert         return 0;
2999*e0c4386eSCy Schubert     }
3000*e0c4386eSCy Schubert     psk_len = psk_key2bn(psk_key, psk, max_psk_len);
3001*e0c4386eSCy Schubert     return psk_len;
3002*e0c4386eSCy Schubert }
3003*e0c4386eSCy Schubert #endif
3004