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