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