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