]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/ssl_old_test.c
test cleanup: move helper .c and .h files to test/helpers/
[thirdparty/openssl.git] / test / ssl_old_test.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>
09867a47 60
dbd87ffc
MC
61/*
62 * Or gethostname won't be declared properly
63 * on Compaq platforms (at least with DEC C).
64 * Do not try to put it earlier, or IPv6 includes
65 * get screwed...
0f113f3e
MC
66 */
67#define _XOPEN_SOURCE_EXTENDED 1
09867a47 68
bc36ee62 69#ifdef OPENSSL_SYS_WINDOWS
0f113f3e 70# include <winsock.h>
37289744 71#else
6b10d29c 72# include <unistd.h>
d02b48c6
RE
73#endif
74
20f8bc72 75#include "helpers/predefined_dhparams.h"
5658470c 76
b76998b8
RS
77static SSL_CTX *s_ctx = NULL;
78static SSL_CTX *s_ctx2 = NULL;
817cd0d5 79
0f113f3e 80/*
45ddce21
RS
81 * There is really no standard for this, so let's assign something
82 * only for this test
0f113f3e 83 */
0f113f3e 84#define COMP_ZLIB 1
23f80f46 85
6d23cf97 86static int verify_callback(int ok, X509_STORE_CTX *ctx);
6d23cf97 87static int app_verify_callback(X509_STORE_CTX *ctx, void *arg);
a7201e9a 88#define APP_CALLBACK_STRING "Test Callback Argument"
0f113f3e
MC
89struct app_verify_arg {
90 char *string;
91 int app_verify;
0f113f3e 92};
023ec151 93
0f113f3e 94static char *psk_key = NULL; /* by default PSK is not used */
ddac1974 95#ifndef OPENSSL_NO_PSK
0f113f3e
MC
96static unsigned int psk_client_callback(SSL *ssl, const char *hint,
97 char *identity,
98 unsigned int max_identity_len,
99 unsigned char *psk,
100 unsigned int max_psk_len);
101static unsigned int psk_server_callback(SSL *ssl, const char *identity,
102 unsigned char *psk,
103 unsigned int max_psk_len);
ddac1974
NL
104#endif
105
0f113f3e
MC
106static BIO *bio_err = NULL;
107static BIO *bio_stdout = NULL;
d02b48c6 108
2911575c 109#ifndef OPENSSL_NO_NEXTPROTONEG
d9a268b9
BL
110/* Note that this code assumes that this is only a one element list: */
111static const char NEXT_PROTO_STRING[] = "\x09testproto";
df2ee0e2
BL
112static int npn_client = 0;
113static int npn_server = 0;
114static int npn_server_reject = 0;
d9a268b9 115
0f113f3e
MC
116static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen,
117 const unsigned char *in, unsigned int inlen,
118 void *arg)
119{
120 /*
121 * This callback only returns the protocol string, rather than a length
122 * prefixed set. We assume that NEXT_PROTO_STRING is a one element list
123 * and remove the first byte to chop off the length prefix.
124 */
125 *out = (unsigned char *)NEXT_PROTO_STRING + 1;
126 *outlen = sizeof(NEXT_PROTO_STRING) - 2;
127 return SSL_TLSEXT_ERR_OK;
128}
129
130static int cb_server_npn(SSL *s, const unsigned char **data,
131 unsigned int *len, void *arg)
132{
133 *data = (const unsigned char *)NEXT_PROTO_STRING;
134 *len = sizeof(NEXT_PROTO_STRING) - 1;
135 return SSL_TLSEXT_ERR_OK;
136}
137
138static int cb_server_rejects_npn(SSL *s, const unsigned char **data,
139 unsigned int *len, void *arg)
140{
141 return SSL_TLSEXT_ERR_NOACK;
142}
d9a268b9
BL
143
144static int verify_npn(SSL *client, SSL *server)
0f113f3e
MC
145{
146 const unsigned char *client_s;
147 unsigned client_len;
148 const unsigned char *server_s;
149 unsigned server_len;
150
151 SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
152 SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
153
154 if (client_len) {
155 BIO_printf(bio_stdout, "Client NPN: ");
156 BIO_write(bio_stdout, client_s, client_len);
157 BIO_printf(bio_stdout, "\n");
158 }
159
160 if (server_len) {
161 BIO_printf(bio_stdout, "Server NPN: ");
162 BIO_write(bio_stdout, server_s, server_len);
163 BIO_printf(bio_stdout, "\n");
164 }
165
166 /*
167 * If an NPN string was returned, it must be the protocol that we
168 * expected to negotiate.
169 */
170 if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 ||
171 memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
172 return -1;
173 if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 ||
174 memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
175 return -1;
176
177 if (!npn_client && client_len)
178 return -1;
179 if (!npn_server && server_len)
180 return -1;
181 if (npn_server_reject && server_len)
182 return -1;
183 if (npn_client && npn_server && (!client_len || !server_len))
184 return -1;
185
186 return 0;
187}
d9a268b9
BL
188#endif
189
a8989362 190static const char *alpn_client;
817cd0d5
TS
191static char *alpn_server;
192static char *alpn_server2;
a8989362
AL
193static const char *alpn_expected;
194static unsigned char *alpn_selected;
7946ab33
KR
195static const char *server_min_proto;
196static const char *server_max_proto;
197static const char *client_min_proto;
198static const char *client_max_proto;
199static const char *should_negotiate;
817cd0d5
TS
200static const char *sn_client;
201static const char *sn_server1;
202static const char *sn_server2;
203static int sn_expect = 0;
b7dffce0
KR
204static const char *server_sess_out;
205static const char *server_sess_in;
206static const char *client_sess_out;
207static const char *client_sess_in;
208static SSL_SESSION *server_sess;
209static SSL_SESSION *client_sess;
817cd0d5
TS
210
211static int servername_cb(SSL *s, int *ad, void *arg)
212{
213 const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
214 if (sn_server2 == NULL) {
215 BIO_printf(bio_stdout, "Servername 2 is NULL\n");
216 return SSL_TLSEXT_ERR_NOACK;
217 }
218
219 if (servername) {
220 if (s_ctx2 != NULL && sn_server2 != NULL &&
221 !strcasecmp(servername, sn_server2)) {
222 BIO_printf(bio_stdout, "Switching server context.\n");
223 SSL_set_SSL_CTX(s, s_ctx2);
224 }
225 }
226 return SSL_TLSEXT_ERR_OK;
227}
228static int verify_servername(SSL *client, SSL *server)
229{
230 /* just need to see if sn_context is what we expect */
231 SSL_CTX* ctx = SSL_get_SSL_CTX(server);
232 if (sn_expect == 0)
233 return 0;
234 if (sn_expect == 1 && ctx == s_ctx)
235 return 0;
236 if (sn_expect == 2 && ctx == s_ctx2)
237 return 0;
238 BIO_printf(bio_stdout, "Servername: expected context %d\n", sn_expect);
239 if (ctx == s_ctx2)
240 BIO_printf(bio_stdout, "Servername: context is 2\n");
241 else if (ctx == s_ctx)
242 BIO_printf(bio_stdout, "Servername: context is 1\n");
243 else
244 BIO_printf(bio_stdout, "Servername: context is unknown\n");
245 return -1;
246}
247
a8989362 248
3a83462d
MC
249/*-
250 * next_protos_parse parses a comma separated list of strings into a string
a8989362
AL
251 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
252 * outlen: (output) set to the length of the resulting buffer on success.
4eadd11c 253 * in: a NUL terminated string like "abc,def,ghi"
a8989362
AL
254 *
255 * returns: a malloced buffer or NULL on failure.
256 */
817cd0d5 257static unsigned char *next_protos_parse(size_t *outlen,
0f113f3e
MC
258 const char *in)
259{
260 size_t len;
261 unsigned char *out;
262 size_t i, start = 0;
263
264 len = strlen(in);
265 if (len >= 65535)
266 return NULL;
267
268 out = OPENSSL_malloc(strlen(in) + 1);
269 if (!out)
270 return NULL;
271
272 for (i = 0; i <= len; ++i) {
273 if (i == len || in[i] == ',') {
274 if (i - start > 255) {
275 OPENSSL_free(out);
276 return NULL;
277 }
3a63c0ed 278 out[start] = (unsigned char)(i - start);
0f113f3e
MC
279 start = i + 1;
280 } else
281 out[i + 1] = in[i];
282 }
283
284 *outlen = len + 1;
285 return out;
286}
287
288static int cb_server_alpn(SSL *s, const unsigned char **out,
289 unsigned char *outlen, const unsigned char *in,
290 unsigned int inlen, void *arg)
291{
292 unsigned char *protos;
817cd0d5
TS
293 size_t protos_len;
294 char* alpn_str = arg;
0f113f3e 295
817cd0d5 296 protos = next_protos_parse(&protos_len, alpn_str);
0f113f3e
MC
297 if (protos == NULL) {
298 fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
817cd0d5 299 alpn_str);
0f113f3e
MC
300 abort();
301 }
302
303 if (SSL_select_next_proto
304 ((unsigned char **)out, outlen, protos, protos_len, in,
305 inlen) != OPENSSL_NPN_NEGOTIATED) {
306 OPENSSL_free(protos);
307 return SSL_TLSEXT_ERR_NOACK;
308 }
309
310 /*
311 * Make a copy of the selected protocol which will be freed in
312 * verify_alpn.
313 */
314 alpn_selected = OPENSSL_malloc(*outlen);
315 memcpy(alpn_selected, *out, *outlen);
316 *out = alpn_selected;
317
318 OPENSSL_free(protos);
319 return SSL_TLSEXT_ERR_OK;
320}
a8989362
AL
321
322static int verify_alpn(SSL *client, SSL *server)
0f113f3e
MC
323{
324 const unsigned char *client_proto, *server_proto;
325 unsigned int client_proto_len = 0, server_proto_len = 0;
326 SSL_get0_alpn_selected(client, &client_proto, &client_proto_len);
327 SSL_get0_alpn_selected(server, &server_proto, &server_proto_len);
328
b548a1f1
RS
329 OPENSSL_free(alpn_selected);
330 alpn_selected = NULL;
0f113f3e 331
3003e0a4
AG
332 if (client_proto_len != server_proto_len) {
333 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
334 goto err;
335 }
336
337 if (client_proto != NULL &&
0f113f3e
MC
338 memcmp(client_proto, server_proto, client_proto_len) != 0) {
339 BIO_printf(bio_stdout, "ALPN selected protocols differ!\n");
340 goto err;
341 }
342
343 if (client_proto_len > 0 && alpn_expected == NULL) {
344 BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n");
345 goto err;
346 }
347
348 if (alpn_expected != NULL &&
349 (client_proto_len != strlen(alpn_expected) ||
350 memcmp(client_proto, alpn_expected, client_proto_len) != 0)) {
351 BIO_printf(bio_stdout,
352 "ALPN selected protocols not equal to expected protocol: %s\n",
353 alpn_expected);
354 goto err;
355 }
356
357 return 0;
358
359 err:
360 BIO_printf(bio_stdout, "ALPN results: client: '");
361 BIO_write(bio_stdout, client_proto, client_proto_len);
362 BIO_printf(bio_stdout, "', server: '");
363 BIO_write(bio_stdout, server_proto, server_proto_len);
364 BIO_printf(bio_stdout, "'\n");
817cd0d5
TS
365 BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '",
366 alpn_client);
367 if (SSL_get_SSL_CTX(server) == s_ctx2) {
368 BIO_printf(bio_stdout, "%s'\n",
369 alpn_server2);
370 } else {
371 BIO_printf(bio_stdout, "%s'\n",
372 alpn_server);
373 }
0f113f3e
MC
374 return -1;
375}
a8989362 376
0f113f3e
MC
377/*
378 * WARNING : below extension types are *NOT* IETF assigned, and could
379 * conflict if these types are reassigned and handled specially by OpenSSL
380 * in the future
381 */
a398f821 382#define TACK_EXT_TYPE 62208
9cd50f73
T
383#define CUSTOM_EXT_TYPE_0 1000
384#define CUSTOM_EXT_TYPE_1 1001
385#define CUSTOM_EXT_TYPE_2 1002
386#define CUSTOM_EXT_TYPE_3 1003
387
df2ee0e2
BL
388static const char custom_ext_cli_string[] = "abc";
389static const char custom_ext_srv_string[] = "defg";
a398f821
T
390
391/* These set from cmdline */
df2ee0e2
BL
392static char *serverinfo_file = NULL;
393static int serverinfo_sct = 0;
394static int serverinfo_tack = 0;
9cd50f73
T
395
396/* These set based on extension callbacks */
df2ee0e2
BL
397static int serverinfo_sct_seen = 0;
398static int serverinfo_tack_seen = 0;
399static int serverinfo_other_seen = 0;
a398f821 400
9cd50f73 401/* This set from cmdline */
df2ee0e2 402static int custom_ext = 0;
9cd50f73
T
403
404/* This set based on extension callbacks */
df2ee0e2 405static int custom_ext_error = 0;
9cd50f73 406
0cfefe4b 407static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
408 const unsigned char *in, size_t inlen,
409 int *al, void *arg)
410{
dd696a55 411 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp)
0f113f3e
MC
412 serverinfo_sct_seen++;
413 else if (ext_type == TACK_EXT_TYPE)
414 serverinfo_tack_seen++;
415 else
416 serverinfo_other_seen++;
417 return 1;
418}
a398f821 419
3cb7c5cf 420static int verify_serverinfo(void)
0f113f3e
MC
421{
422 if (serverinfo_sct != serverinfo_sct_seen)
423 return -1;
424 if (serverinfo_tack != serverinfo_tack_seen)
425 return -1;
426 if (serverinfo_other_seen)
427 return -1;
428 return 0;
429}
a398f821 430
1d97c843
TH
431/*-
432 * Four test cases for custom extensions:
9cd50f73
T
433 * 0 - no ClientHello extension or ServerHello response
434 * 1 - ClientHello with "abc", no response
435 * 2 - ClientHello with "abc", empty response
436 * 3 - ClientHello with "abc", "defg" response
437 */
438
de2a9e38 439static int custom_ext_0_cli_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
440 const unsigned char **out,
441 size_t *outlen, int *al, void *arg)
442{
443 if (ext_type != CUSTOM_EXT_TYPE_0)
444 custom_ext_error = 1;
445 return 0; /* Don't send an extension */
446}
9cd50f73 447
de2a9e38 448static int custom_ext_0_cli_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
449 const unsigned char *in,
450 size_t inlen, int *al, void *arg)
451{
452 return 1;
453}
9cd50f73 454
de2a9e38 455static int custom_ext_1_cli_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
456 const unsigned char **out,
457 size_t *outlen, int *al, void *arg)
458{
459 if (ext_type != CUSTOM_EXT_TYPE_1)
460 custom_ext_error = 1;
461 *out = (const unsigned char *)custom_ext_cli_string;
462 *outlen = strlen(custom_ext_cli_string);
463 return 1; /* Send "abc" */
464}
9cd50f73 465
de2a9e38 466static int custom_ext_1_cli_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
467 const unsigned char *in,
468 size_t inlen, int *al, void *arg)
469{
470 return 1;
471}
9cd50f73 472
de2a9e38 473static int custom_ext_2_cli_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
474 const unsigned char **out,
475 size_t *outlen, int *al, void *arg)
476{
477 if (ext_type != CUSTOM_EXT_TYPE_2)
478 custom_ext_error = 1;
479 *out = (const unsigned char *)custom_ext_cli_string;
480 *outlen = strlen(custom_ext_cli_string);
481 return 1; /* Send "abc" */
482}
9cd50f73 483
de2a9e38 484static int custom_ext_2_cli_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
485 const unsigned char *in,
486 size_t inlen, int *al, void *arg)
487{
488 if (ext_type != CUSTOM_EXT_TYPE_2)
489 custom_ext_error = 1;
490 if (inlen != 0)
491 custom_ext_error = 1; /* Should be empty response */
492 return 1;
493}
9cd50f73 494
de2a9e38 495static int custom_ext_3_cli_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
496 const unsigned char **out,
497 size_t *outlen, int *al, void *arg)
498{
499 if (ext_type != CUSTOM_EXT_TYPE_3)
500 custom_ext_error = 1;
501 *out = (const unsigned char *)custom_ext_cli_string;
502 *outlen = strlen(custom_ext_cli_string);
503 return 1; /* Send "abc" */
504}
9cd50f73 505
de2a9e38 506static int custom_ext_3_cli_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
507 const unsigned char *in,
508 size_t inlen, int *al, void *arg)
509{
510 if (ext_type != CUSTOM_EXT_TYPE_3)
511 custom_ext_error = 1;
512 if (inlen != strlen(custom_ext_srv_string))
513 custom_ext_error = 1;
514 if (memcmp(custom_ext_srv_string, in, inlen) != 0)
515 custom_ext_error = 1; /* Check for "defg" */
516 return 1;
517}
518
519/*
520 * custom_ext_0_cli_add_cb returns 0 - the server won't receive a callback
521 * for this extension
522 */
de2a9e38 523static int custom_ext_0_srv_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
524 const unsigned char *in,
525 size_t inlen, int *al, void *arg)
526{
527 custom_ext_error = 1;
528 return 1;
529}
9cd50f73 530
f47e2039 531/* 'add' callbacks are only called if the 'parse' callback is called */
de2a9e38 532static int custom_ext_0_srv_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
533 const unsigned char **out,
534 size_t *outlen, int *al, void *arg)
535{
536 /* Error: should not have been called */
537 custom_ext_error = 1;
538 return 0; /* Don't send an extension */
539}
9cd50f73 540
de2a9e38 541static int custom_ext_1_srv_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
542 const unsigned char *in,
543 size_t inlen, int *al, void *arg)
544{
545 if (ext_type != CUSTOM_EXT_TYPE_1)
546 custom_ext_error = 1;
547 /* Check for "abc" */
548 if (inlen != strlen(custom_ext_cli_string))
549 custom_ext_error = 1;
550 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
551 custom_ext_error = 1;
552 return 1;
553}
9cd50f73 554
de2a9e38 555static int custom_ext_1_srv_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
556 const unsigned char **out,
557 size_t *outlen, int *al, void *arg)
558{
559 return 0; /* Don't send an extension */
560}
9cd50f73 561
de2a9e38 562static int custom_ext_2_srv_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
563 const unsigned char *in,
564 size_t inlen, int *al, void *arg)
565{
566 if (ext_type != CUSTOM_EXT_TYPE_2)
567 custom_ext_error = 1;
568 /* Check for "abc" */
569 if (inlen != strlen(custom_ext_cli_string))
570 custom_ext_error = 1;
571 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
572 custom_ext_error = 1;
573 return 1;
574}
9cd50f73 575
de2a9e38 576static int custom_ext_2_srv_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
577 const unsigned char **out,
578 size_t *outlen, int *al, void *arg)
579{
580 *out = NULL;
581 *outlen = 0;
582 return 1; /* Send empty extension */
583}
9cd50f73 584
de2a9e38 585static int custom_ext_3_srv_parse_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
586 const unsigned char *in,
587 size_t inlen, int *al, void *arg)
588{
589 if (ext_type != CUSTOM_EXT_TYPE_3)
590 custom_ext_error = 1;
591 /* Check for "abc" */
592 if (inlen != strlen(custom_ext_cli_string))
593 custom_ext_error = 1;
594 if (memcmp(in, custom_ext_cli_string, inlen) != 0)
595 custom_ext_error = 1;
596 return 1;
597}
9cd50f73 598
de2a9e38 599static int custom_ext_3_srv_add_cb(SSL *s, unsigned int ext_type,
0f113f3e
MC
600 const unsigned char **out,
601 size_t *outlen, int *al, void *arg)
602{
603 *out = (const unsigned char *)custom_ext_srv_string;
604 *outlen = strlen(custom_ext_srv_string);
605 return 1; /* Send "defg" */
606}
607
608static char *cipher = NULL;
f865b081 609static char *ciphersuites = NULL;
0f113f3e
MC
610static int verbose = 0;
611static int debug = 0;
d02b48c6 612
75d5bd4e
RL
613int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family,
614 long bytes, clock_t *s_time, clock_t *c_time);
0f113f3e
MC
615int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time,
616 clock_t *c_time);
617int doit(SSL *s_ssl, SSL *c_ssl, long bytes);
9ccc00ef 618
6b691a5c 619static void sv_usage(void)
0f113f3e
MC
620{
621 fprintf(stderr, "usage: ssltest [args ...]\n");
622 fprintf(stderr, "\n");
0f113f3e
MC
623 fprintf(stderr, " -server_auth - check server certificate\n");
624 fprintf(stderr, " -client_auth - do client authentication\n");
0f113f3e
MC
625 fprintf(stderr, " -v - more output\n");
626 fprintf(stderr, " -d - debug output\n");
627 fprintf(stderr, " -reuse - use session-id reuse\n");
628 fprintf(stderr, " -num <val> - number of connections to perform\n");
629 fprintf(stderr,
630 " -bytes <val> - number of bytes to swap between client/server\n");
5658470c 631#ifndef OPENSSL_NO_DH
0f113f3e 632 fprintf(stderr,
e8356e32
EK
633 " -dhe512 - use 512 bit key for DHE (to test failure)\n");
634 fprintf(stderr,
635 " -dhe1024 - use 1024 bit key (safe prime) for DHE (default, no-op)\n");
0f113f3e
MC
636 fprintf(stderr,
637 " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
5658470c 638#endif
0f113f3e 639 fprintf(stderr, " -no_dhe - disable DHE\n");
10bf4fc2 640#ifndef OPENSSL_NO_EC
817cd0d5 641 fprintf(stderr, " -no_ecdhe - disable ECDHE\nTODO(openssl-team): no_ecdhe was broken by auto ecdh. Make this work again.\n");
ea262260 642#endif
ddac1974 643#ifndef OPENSSL_NO_PSK
0f113f3e 644 fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n");
ddac1974 645#endif
6b01bed2 646#ifndef OPENSSL_NO_SSL3
0f113f3e 647 fprintf(stderr, " -ssl3 - use SSLv3\n");
58964a49 648#endif
6b01bed2 649#ifndef OPENSSL_NO_TLS1
0f113f3e 650 fprintf(stderr, " -tls1 - use TLSv1\n");
6b01bed2 651#endif
98b8cdd3 652#ifndef OPENSSL_NO_DTLS
0d5301af 653 fprintf(stderr, " -dtls - use DTLS\n");
6b01bed2 654#ifndef OPENSSL_NO_DTLS1
98b8cdd3 655 fprintf(stderr, " -dtls1 - use DTLSv1\n");
6b01bed2
VD
656#endif
657#ifndef OPENSSL_NO_DTLS1_2
98b8cdd3 658 fprintf(stderr, " -dtls12 - use DTLSv1.2\n");
6b01bed2 659#endif
98b8cdd3 660#endif
0f113f3e
MC
661 fprintf(stderr, " -CApath arg - PEM format directory of CA's\n");
662 fprintf(stderr, " -CAfile arg - PEM format file of CA's\n");
663 fprintf(stderr, " -cert arg - Server certificate file\n");
664 fprintf(stderr,
665 " -key arg - Server key file (default: same as -cert)\n");
666 fprintf(stderr, " -c_cert arg - Client certificate file\n");
667 fprintf(stderr,
668 " -c_key arg - Client key file (default: same as -c_cert)\n");
f865b081
MC
669 fprintf(stderr, " -cipher arg - The TLSv1.2 and below cipher list\n");
670 fprintf(stderr, " -ciphersuites arg - The TLSv1.3 ciphersuites\n");
0f113f3e 671 fprintf(stderr, " -bio_pair - Use BIO pairs\n");
75d5bd4e
RL
672 fprintf(stderr, " -ipv4 - Use IPv4 connection on localhost\n");
673 fprintf(stderr, " -ipv6 - Use IPv6 connection on localhost\n");
0f113f3e
MC
674 fprintf(stderr, " -f - Test even cases that can't work\n");
675 fprintf(stderr,
676 " -time - measure processor time used by client and server\n");
677 fprintf(stderr, " -zlib - use zlib compression\n");
2911575c 678#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e
MC
679 fprintf(stderr, " -npn_client - have client side offer NPN\n");
680 fprintf(stderr, " -npn_server - have server side offer NPN\n");
681 fprintf(stderr, " -npn_server_reject - have server reject NPN\n");
682#endif
683 fprintf(stderr, " -serverinfo_file file - have server use this file\n");
684 fprintf(stderr, " -serverinfo_sct - have client offer and expect SCT\n");
685 fprintf(stderr,
686 " -serverinfo_tack - have client offer and expect TACK\n");
687 fprintf(stderr,
688 " -custom_ext - try various custom extension callbacks\n");
689 fprintf(stderr, " -alpn_client <string> - have client side offer ALPN\n");
690 fprintf(stderr, " -alpn_server <string> - have server side offer ALPN\n");
817cd0d5
TS
691 fprintf(stderr, " -alpn_server1 <string> - alias for -alpn_server\n");
692 fprintf(stderr, " -alpn_server2 <string> - have server side context 2 offer ALPN\n");
0f113f3e
MC
693 fprintf(stderr,
694 " -alpn_expected <string> - the ALPN protocol that should be negotiated\n");
7946ab33
KR
695 fprintf(stderr, " -server_min_proto <string> - Minimum version the server should support\n");
696 fprintf(stderr, " -server_max_proto <string> - Maximum version the server should support\n");
697 fprintf(stderr, " -client_min_proto <string> - Minimum version the client should support\n");
698 fprintf(stderr, " -client_max_proto <string> - Maximum version the client should support\n");
699 fprintf(stderr, " -should_negotiate <string> - The version that should be negotiated, fail-client or fail-server\n");
dd696a55
RP
700#ifndef OPENSSL_NO_CT
701 fprintf(stderr, " -noct - no certificate transparency\n");
702 fprintf(stderr, " -requestct - request certificate transparency\n");
703 fprintf(stderr, " -requirect - require certificate transparency\n");
704#endif
817cd0d5
TS
705 fprintf(stderr, " -sn_client <string> - have client request this servername\n");
706 fprintf(stderr, " -sn_server1 <string> - have server context 1 respond to this servername\n");
707 fprintf(stderr, " -sn_server2 <string> - have server context 2 respond to this servername\n");
708 fprintf(stderr, " -sn_expect1 - expected server 1\n");
709 fprintf(stderr, " -sn_expect2 - expected server 2\n");
b7dffce0
KR
710 fprintf(stderr, " -server_sess_out <file> - Save the server session to a file\n");
711 fprintf(stderr, " -server_sess_in <file> - Read the server session from a file\n");
712 fprintf(stderr, " -client_sess_out <file> - Save the client session to a file\n");
713 fprintf(stderr, " -client_sess_in <file> - Read the client session from a file\n");
714 fprintf(stderr, " -should_reuse <number> - The expected state of reusing the session\n");
715 fprintf(stderr, " -no_ticket - do not issue TLS session ticket\n");
63215d04 716 fprintf(stderr, " -provider <name> - Load the given provider into the library context\n");
be9d82bb 717 fprintf(stderr, " -config <cnf> - Load the given config file into the library context\n");
0f113f3e 718}
563f1503 719
f756f1fc 720static void print_key_details(BIO *out, EVP_PKEY *key)
0f113f3e
MC
721{
722 int keyid = EVP_PKEY_id(key);
f756f1fc 723#ifndef OPENSSL_NO_EC
0f113f3e
MC
724 if (keyid == EVP_PKEY_EC) {
725 EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
726 int nid;
727 const char *cname;
728 nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
729 EC_KEY_free(ec);
730 cname = EC_curve_nid2nist(nid);
731 if (!cname)
732 cname = OBJ_nid2sn(nid);
733 BIO_printf(out, "%d bits EC (%s)", EVP_PKEY_bits(key), cname);
734 } else
735#endif
736 {
737 const char *algname;
738 switch (keyid) {
739 case EVP_PKEY_RSA:
740 algname = "RSA";
741 break;
742 case EVP_PKEY_DSA:
743 algname = "DSA";
744 break;
745 case EVP_PKEY_DH:
746 algname = "DH";
747 break;
748 default:
749 algname = OBJ_nid2sn(keyid);
750 break;
751 }
752 BIO_printf(out, "%d bits %s", EVP_PKEY_bits(key), algname);
753 }
754}
f756f1fc 755
563f1503 756static void print_details(SSL *c_ssl, const char *prefix)
0f113f3e
MC
757{
758 const SSL_CIPHER *ciph;
759 int mdnid;
760 X509 *cert;
761 EVP_PKEY *pkey;
762
763 ciph = SSL_get_current_cipher(c_ssl);
764 BIO_printf(bio_stdout, "%s%s, cipher %s %s",
765 prefix,
766 SSL_get_version(c_ssl),
767 SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
8c2bfd25 768 cert = SSL_get0_peer_certificate(c_ssl);
0f113f3e 769 if (cert != NULL) {
1c72f70d
F
770 EVP_PKEY* pubkey = X509_get0_pubkey(cert);
771
772 if (pubkey != NULL) {
0f113f3e 773 BIO_puts(bio_stdout, ", ");
1c72f70d 774 print_key_details(bio_stdout, pubkey);
0f113f3e 775 }
0f113f3e 776 }
a51c9f63 777 if (SSL_get_peer_tmp_key(c_ssl, &pkey)) {
0f113f3e
MC
778 BIO_puts(bio_stdout, ", temp key: ");
779 print_key_details(bio_stdout, pkey);
780 EVP_PKEY_free(pkey);
781 }
782 if (SSL_get_peer_signature_nid(c_ssl, &mdnid))
783 BIO_printf(bio_stdout, ", digest=%s", OBJ_nid2sn(mdnid));
784 BIO_printf(bio_stdout, "\n");
785}
d02b48c6 786
7946ab33
KR
787/*
788 * protocol_from_string - converts a protocol version string to a number
789 *
790 * Returns -1 on failure or the version on success
791 */
792static int protocol_from_string(const char *value)
793{
794 struct protocol_versions {
795 const char *name;
796 int version;
797 };
798 static const struct protocol_versions versions[] = {
799 {"ssl3", SSL3_VERSION},
800 {"tls1", TLS1_VERSION},
801 {"tls1.1", TLS1_1_VERSION},
802 {"tls1.2", TLS1_2_VERSION},
582a17d6 803 {"tls1.3", TLS1_3_VERSION},
7946ab33
KR
804 {"dtls1", DTLS1_VERSION},
805 {"dtls1.2", DTLS1_2_VERSION}};
806 size_t i;
807 size_t n = OSSL_NELEM(versions);
808
809 for (i = 0; i < n; i++)
810 if (strcmp(versions[i].name, value) == 0)
811 return versions[i].version;
812 return -1;
813}
814
b7dffce0
KR
815static SSL_SESSION *read_session(const char *filename)
816{
817 SSL_SESSION *sess;
818 BIO *f = BIO_new_file(filename, "r");
819
820 if (f == NULL) {
821 BIO_printf(bio_err, "Can't open session file %s\n", filename);
822 ERR_print_errors(bio_err);
823 return NULL;
824 }
825 sess = PEM_read_bio_SSL_SESSION(f, NULL, 0, NULL);
826 if (sess == NULL) {
827 BIO_printf(bio_err, "Can't parse session file %s\n", filename);
828 ERR_print_errors(bio_err);
829 }
830 BIO_free(f);
831 return sess;
832}
833
834static int write_session(const char *filename, SSL_SESSION *sess)
835{
836 BIO *f = BIO_new_file(filename, "w");
837
838 if (sess == NULL) {
839 BIO_printf(bio_err, "No session information\n");
840 return 0;
841 }
842 if (f == NULL) {
843 BIO_printf(bio_err, "Can't open session file %s\n", filename);
844 ERR_print_errors(bio_err);
845 return 0;
846 }
847 PEM_write_bio_SSL_SESSION(f, sess);
848 BIO_free(f);
849 return 1;
850}
851
7946ab33
KR
852/*
853 * set_protocol_version - Sets protocol version minimum or maximum
854 *
855 * Returns 0 on failure and 1 on success
856 */
857static int set_protocol_version(const char *version, SSL *ssl, int setting)
858{
859 if (version != NULL) {
860 int ver = protocol_from_string(version);
861 if (ver < 0) {
862 BIO_printf(bio_err, "Error parsing: %s\n", version);
863 return 0;
864 }
865 return SSL_ctrl(ssl, setting, ver, NULL);
866 }
867 return 1;
868}
869
6b691a5c 870int main(int argc, char *argv[])
0f113f3e 871{
cc696296 872 const char *CApath = NULL, *CAfile = NULL;
0f113f3e 873 int badop = 0;
75d5bd4e 874 enum { BIO_MEM, BIO_PAIR, BIO_IPV4, BIO_IPV6 } bio_type = BIO_MEM;
0f113f3e 875 int force = 0;
c2500f65
P
876 int dtls1 = 0, dtls12 = 0, dtls = 0, tls1 = 0, tls1_2 = 0, ssl3 = 0;
877 int ret = EXIT_FAILURE;
0f113f3e
MC
878 int client_auth = 0;
879 int server_auth = 0, i;
880 struct app_verify_arg app_verify_arg =
a263f320 881 { APP_CALLBACK_STRING, 0 };
0f113f3e
MC
882 SSL_CTX *c_ctx = NULL;
883 const SSL_METHOD *meth = NULL;
884 SSL *c_ssl, *s_ssl;
885 int number = 1, reuse = 0;
b7dffce0
KR
886 int should_reuse = -1;
887 int no_ticket = 0;
0f113f3e 888 long bytes = 256L;
5658470c 889#ifndef OPENSSL_NO_DH
d3d2c0dc 890 EVP_PKEY *dhpkey;
e8356e32 891 int dhe512 = 0, dhe1024dsa = 0;
0f113f3e 892 int no_dhe = 0;
d3d2c0dc 893#endif
0f113f3e
MC
894 int no_psk = 0;
895 int print_time = 0;
896 clock_t s_time = 0, c_time = 0;
09b6c2ef 897#ifndef OPENSSL_NO_COMP
9a555706 898 int n, comp = 0;
0f113f3e
MC
899 COMP_METHOD *cm = NULL;
900 STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
0f113f3e 901#endif
6b01bed2 902 int no_protocol;
0d5301af 903 int min_version = 0, max_version = 0;
dd696a55
RP
904#ifndef OPENSSL_NO_CT
905 /*
906 * Disable CT validation by default, because it will interfere with
907 * anything using custom extension handlers to deal with SCT extensions.
908 */
43341433 909 int ct_validation = 0;
dd696a55 910#endif
817cd0d5 911 SSL_CONF_CTX *s_cctx = NULL, *c_cctx = NULL, *s_cctx2 = NULL;
0f113f3e 912 STACK_OF(OPENSSL_STRING) *conf_args = NULL;
4a640fb6 913 char *arg = NULL, *argn = NULL;
63215d04
MC
914 const char *provider = NULL, *config = NULL;
915 OSSL_PROVIDER *thisprov = NULL, *defctxnull = NULL;
b4250010 916 OSSL_LIB_CTX *libctx = NULL;
0f113f3e
MC
917
918 verbose = 0;
919 debug = 0;
0f113f3e
MC
920
921 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
0f113f3e
MC
922 bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
923
924 s_cctx = SSL_CONF_CTX_new();
817cd0d5 925 s_cctx2 = SSL_CONF_CTX_new();
0f113f3e
MC
926 c_cctx = SSL_CONF_CTX_new();
927
817cd0d5 928 if (!s_cctx || !c_cctx || !s_cctx2) {
0f113f3e
MC
929 ERR_print_errors(bio_err);
930 goto end;
931 }
932
933 SSL_CONF_CTX_set_flags(s_cctx,
6a096889
DSH
934 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
935 SSL_CONF_FLAG_CERTIFICATE |
936 SSL_CONF_FLAG_REQUIRE_PRIVATE);
817cd0d5
TS
937 SSL_CONF_CTX_set_flags(s_cctx2,
938 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_SERVER |
939 SSL_CONF_FLAG_CERTIFICATE |
940 SSL_CONF_FLAG_REQUIRE_PRIVATE);
0f113f3e
MC
941 if (!SSL_CONF_CTX_set1_prefix(s_cctx, "-s_")) {
942 ERR_print_errors(bio_err);
943 goto end;
944 }
817cd0d5
TS
945 if (!SSL_CONF_CTX_set1_prefix(s_cctx2, "-s_")) {
946 ERR_print_errors(bio_err);
947 goto end;
948 }
0f113f3e
MC
949
950 SSL_CONF_CTX_set_flags(c_cctx,
6a096889
DSH
951 SSL_CONF_FLAG_CMDLINE | SSL_CONF_FLAG_CLIENT |
952 SSL_CONF_FLAG_CERTIFICATE |
953 SSL_CONF_FLAG_REQUIRE_PRIVATE);
0f113f3e
MC
954 if (!SSL_CONF_CTX_set1_prefix(c_cctx, "-c_")) {
955 ERR_print_errors(bio_err);
956 goto end;
957 }
958
959 argc--;
960 argv++;
961
962 while (argc >= 1) {
86885c28 963 if (strcmp(*argv, "-F") == 0) {
0f113f3e
MC
964 fprintf(stderr,
965 "not compiled with FIPS support, so exiting without running.\n");
966 EXIT(0);
0f113f3e
MC
967 } else if (strcmp(*argv, "-server_auth") == 0)
968 server_auth = 1;
969 else if (strcmp(*argv, "-client_auth") == 0)
970 client_auth = 1;
a263f320 971 else if (strcmp(*argv, "-v") == 0)
0f113f3e
MC
972 verbose = 1;
973 else if (strcmp(*argv, "-d") == 0)
974 debug = 1;
975 else if (strcmp(*argv, "-reuse") == 0)
976 reuse = 1;
5658470c
DDO
977 else if (strcmp(*argv, "-no_dhe") == 0)
978#ifdef OPENSSL_NO_DH
979 /* unused in this case */;
980#else
981 no_dhe = 1;
982 else if (strcmp(*argv, "-dhe512") == 0)
e8356e32 983 dhe512 = 1;
5658470c 984 else if (strcmp(*argv, "-dhe1024dsa") == 0)
0f113f3e 985 dhe1024dsa = 1;
5658470c 986#endif
0f113f3e 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 }
5658470c 1489#ifndef OPENSSL_NO_DH
0f113f3e 1490 if (!no_dhe) {
6955e3f7 1491 if (dhe1024dsa)
d3d2c0dc 1492 dhpkey = get_dh1024dsa(libctx);
6955e3f7 1493 else if (dhe512)
d3d2c0dc 1494 dhpkey = get_dh512(libctx);
e8356e32 1495 else
6955e3f7
MC
1496 dhpkey = get_dh2048(libctx);
1497
d3d2c0dc
MC
1498 if (dhpkey == NULL || !EVP_PKEY_up_ref(dhpkey)) {
1499 EVP_PKEY_free(dhpkey);
1500 BIO_puts(bio_err, "Error getting DH parameters\n");
1501 ERR_print_errors(bio_err);
1502 goto end;
1503 }
1504 SSL_CTX_set0_tmp_dh_pkey(s_ctx, dhpkey);
1505 SSL_CTX_set0_tmp_dh_pkey(s_ctx2, dhpkey);
0f113f3e 1506 }
5658470c 1507#endif
58964a49 1508
573e4bf0
RL
1509 if (!(SSL_CTX_load_verify_file(s_ctx, CAfile)
1510 || SSL_CTX_load_verify_dir(s_ctx, CApath))
1511 || !SSL_CTX_set_default_verify_paths(s_ctx)
1512 || !(SSL_CTX_load_verify_file(s_ctx2, CAfile)
1513 || SSL_CTX_load_verify_dir(s_ctx2, CApath))
1514 || !SSL_CTX_set_default_verify_paths(s_ctx2)
1515 || !(SSL_CTX_load_verify_file(c_ctx, CAfile)
1516 || SSL_CTX_load_verify_dir(c_ctx, CApath))
1517 || !SSL_CTX_set_default_verify_paths(c_ctx)) {
0f113f3e 1518 ERR_print_errors(bio_err);
0f113f3e
MC
1519 }
1520
b5369582 1521#ifndef OPENSSL_NO_CT
dd696a55 1522 if (!SSL_CTX_set_default_ctlog_list_file(s_ctx) ||
817cd0d5 1523 !SSL_CTX_set_default_ctlog_list_file(s_ctx2) ||
dd696a55
RP
1524 !SSL_CTX_set_default_ctlog_list_file(c_ctx)) {
1525 ERR_print_errors(bio_err);
1526 }
b5369582 1527#endif
dd696a55 1528
0f113f3e 1529 if (client_auth) {
3dca57f8 1530 printf("client authentication\n");
0f113f3e
MC
1531 SSL_CTX_set_verify(s_ctx,
1532 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1533 verify_callback);
817cd0d5
TS
1534 SSL_CTX_set_verify(s_ctx2,
1535 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1536 verify_callback);
0f113f3e
MC
1537 SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback,
1538 &app_verify_arg);
817cd0d5
TS
1539 SSL_CTX_set_cert_verify_callback(s_ctx2, app_verify_callback,
1540 &app_verify_arg);
0f113f3e
MC
1541 }
1542 if (server_auth) {
3dca57f8 1543 printf("server authentication\n");
0f113f3e
MC
1544 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback);
1545 SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback,
1546 &app_verify_arg);
1547 }
1548
1549 {
1550 int session_id_context = 0;
61986d32 1551 if (!SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context,
cbe29648 1552 sizeof(session_id_context)) ||
817cd0d5 1553 !SSL_CTX_set_session_id_context(s_ctx2, (void *)&session_id_context,
cbe29648 1554 sizeof(session_id_context))) {
69f68237
MC
1555 ERR_print_errors(bio_err);
1556 goto end;
1557 }
0f113f3e
MC
1558 }
1559
1560 /* Use PSK only if PSK key is given */
1561 if (psk_key != NULL) {
1562 /*
1563 * no_psk is used to avoid putting psk command to openssl tool
1564 */
1565 if (no_psk) {
1566 /*
1567 * if PSK is not compiled in and psk key is given, do nothing and
1568 * exit successfully
1569 */
c2500f65 1570 ret = EXIT_SUCCESS;
0f113f3e
MC
1571 goto end;
1572 }
ddac1974 1573#ifndef OPENSSL_NO_PSK
0f113f3e
MC
1574 SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
1575 SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
817cd0d5 1576 SSL_CTX_set_psk_server_callback(s_ctx2, psk_server_callback);
0f113f3e
MC
1577 if (debug)
1578 BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n");
817cd0d5
TS
1579 if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint") ||
1580 !SSL_CTX_use_psk_identity_hint(s_ctx2, "ctx server identity_hint")) {
0f113f3e
MC
1581 BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n");
1582 ERR_print_errors(bio_err);
1583 goto end;
1584 }
1585#endif
1586 }
ddac1974 1587
2911575c 1588#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e
MC
1589 if (npn_client) {
1590 SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
1591 }
1592 if (npn_server) {
1593 if (npn_server_reject) {
1594 BIO_printf(bio_err,
1595 "Can't have both -npn_server and -npn_server_reject\n");
1596 goto end;
1597 }
aff8c126
RS
1598 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_npn, NULL);
1599 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_npn, NULL);
0f113f3e
MC
1600 }
1601 if (npn_server_reject) {
aff8c126
RS
1602 SSL_CTX_set_npn_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
1603 SSL_CTX_set_npn_advertised_cb(s_ctx2, cb_server_rejects_npn, NULL);
0f113f3e
MC
1604 }
1605#endif
1606
69f68237 1607 if (serverinfo_sct) {
dd696a55
RP
1608 if (!SSL_CTX_add_client_custom_ext(c_ctx,
1609 TLSEXT_TYPE_signed_certificate_timestamp,
1610 NULL, NULL, NULL,
1611 serverinfo_cli_parse_cb, NULL)) {
69f68237
MC
1612 BIO_printf(bio_err, "Error adding SCT extension\n");
1613 goto end;
1614 }
1615 }
1616 if (serverinfo_tack) {
61986d32 1617 if (!SSL_CTX_add_client_custom_ext(c_ctx, TACK_EXT_TYPE,
0f113f3e 1618 NULL, NULL, NULL,
69f68237
MC
1619 serverinfo_cli_parse_cb, NULL)) {
1620 BIO_printf(bio_err, "Error adding TACK extension\n");
1621 goto end;
1622 }
1623 }
0f113f3e 1624 if (serverinfo_file)
817cd0d5
TS
1625 if (!SSL_CTX_use_serverinfo_file(s_ctx, serverinfo_file) ||
1626 !SSL_CTX_use_serverinfo_file(s_ctx2, serverinfo_file)) {
0f113f3e
MC
1627 BIO_printf(bio_err, "missing serverinfo file\n");
1628 goto end;
1629 }
1630
1631 if (custom_ext) {
61986d32 1632 if (!SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_0,
0f113f3e
MC
1633 custom_ext_0_cli_add_cb,
1634 NULL, NULL,
69f68237 1635 custom_ext_0_cli_parse_cb, NULL)
61986d32 1636 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_1,
0f113f3e
MC
1637 custom_ext_1_cli_add_cb,
1638 NULL, NULL,
69f68237 1639 custom_ext_1_cli_parse_cb, NULL)
61986d32 1640 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_2,
0f113f3e
MC
1641 custom_ext_2_cli_add_cb,
1642 NULL, NULL,
69f68237 1643 custom_ext_2_cli_parse_cb, NULL)
61986d32 1644 || !SSL_CTX_add_client_custom_ext(c_ctx, CUSTOM_EXT_TYPE_3,
0f113f3e
MC
1645 custom_ext_3_cli_add_cb,
1646 NULL, NULL,
69f68237 1647 custom_ext_3_cli_parse_cb, NULL)
61986d32 1648 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_0,
0f113f3e
MC
1649 custom_ext_0_srv_add_cb,
1650 NULL, NULL,
69f68237 1651 custom_ext_0_srv_parse_cb, NULL)
817cd0d5
TS
1652 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_0,
1653 custom_ext_0_srv_add_cb,
1654 NULL, NULL,
1655 custom_ext_0_srv_parse_cb, NULL)
61986d32 1656 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_1,
0f113f3e
MC
1657 custom_ext_1_srv_add_cb,
1658 NULL, NULL,
69f68237 1659 custom_ext_1_srv_parse_cb, NULL)
817cd0d5
TS
1660 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_1,
1661 custom_ext_1_srv_add_cb,
1662 NULL, NULL,
1663 custom_ext_1_srv_parse_cb, NULL)
61986d32 1664 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_2,
0f113f3e
MC
1665 custom_ext_2_srv_add_cb,
1666 NULL, NULL,
69f68237 1667 custom_ext_2_srv_parse_cb, NULL)
817cd0d5
TS
1668 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_2,
1669 custom_ext_2_srv_add_cb,
1670 NULL, NULL,
1671 custom_ext_2_srv_parse_cb, NULL)
61986d32 1672 || !SSL_CTX_add_server_custom_ext(s_ctx, CUSTOM_EXT_TYPE_3,
817cd0d5
TS
1673 custom_ext_3_srv_add_cb,
1674 NULL, NULL,
1675 custom_ext_3_srv_parse_cb, NULL)
1676 || !SSL_CTX_add_server_custom_ext(s_ctx2, CUSTOM_EXT_TYPE_3,
0f113f3e
MC
1677 custom_ext_3_srv_add_cb,
1678 NULL, NULL,
69f68237
MC
1679 custom_ext_3_srv_parse_cb, NULL)) {
1680 BIO_printf(bio_err, "Error setting custom extensions\n");
1681 goto end;
1682 }
0f113f3e
MC
1683 }
1684
1685 if (alpn_server)
817cd0d5
TS
1686 SSL_CTX_set_alpn_select_cb(s_ctx, cb_server_alpn, alpn_server);
1687 if (alpn_server2)
1688 SSL_CTX_set_alpn_select_cb(s_ctx2, cb_server_alpn, alpn_server2);
0f113f3e
MC
1689
1690 if (alpn_client) {
817cd0d5 1691 size_t alpn_len;
0f113f3e
MC
1692 unsigned char *alpn = next_protos_parse(&alpn_len, alpn_client);
1693
1694 if (alpn == NULL) {
1695 BIO_printf(bio_err, "Error parsing -alpn_client argument\n");
1696 goto end;
1697 }
69f68237 1698 /* Returns 0 on success!! */
61986d32 1699 if (SSL_CTX_set_alpn_protos(c_ctx, alpn, alpn_len)) {
69f68237
MC
1700 BIO_printf(bio_err, "Error setting ALPN\n");
1701 OPENSSL_free(alpn);
1702 goto end;
1703 }
0f113f3e
MC
1704 OPENSSL_free(alpn);
1705 }
1706
b7dffce0
KR
1707 if (server_sess_in != NULL) {
1708 server_sess = read_session(server_sess_in);
1709 if (server_sess == NULL)
1710 goto end;
1711 }
1712 if (client_sess_in != NULL) {
1713 client_sess = read_session(client_sess_in);
1714 if (client_sess == NULL)
1715 goto end;
1716 }
1717
1718 if (server_sess_out != NULL || server_sess_in != NULL) {
1719 char *keys;
1720 long size;
1721
1722 /* Use a fixed key so that we can decrypt the ticket. */
1723 size = SSL_CTX_set_tlsext_ticket_keys(s_ctx, NULL, 0);
1724 keys = OPENSSL_zalloc(size);
1725 SSL_CTX_set_tlsext_ticket_keys(s_ctx, keys, size);
1726 OPENSSL_free(keys);
1727 }
1728
817cd0d5
TS
1729 if (sn_server1 != NULL || sn_server2 != NULL)
1730 SSL_CTX_set_tlsext_servername_callback(s_ctx, servername_cb);
1731
0f113f3e
MC
1732 c_ssl = SSL_new(c_ctx);
1733 s_ssl = SSL_new(s_ctx);
58964a49 1734
817cd0d5
TS
1735 if (sn_client)
1736 SSL_set_tlsext_host_name(c_ssl, sn_client);
1737
7946ab33
KR
1738 if (!set_protocol_version(server_min_proto, s_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1739 goto end;
1740 if (!set_protocol_version(server_max_proto, s_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1741 goto end;
1742 if (!set_protocol_version(client_min_proto, c_ssl, SSL_CTRL_SET_MIN_PROTO_VERSION))
1743 goto end;
1744 if (!set_protocol_version(client_max_proto, c_ssl, SSL_CTRL_SET_MAX_PROTO_VERSION))
1745 goto end;
1746
b7dffce0
KR
1747 if (server_sess) {
1748 if (SSL_CTX_add_session(s_ctx, server_sess) == 0) {
1749 BIO_printf(bio_err, "Can't add server session\n");
1750 ERR_print_errors(bio_err);
1751 goto end;
1752 }
1753 }
1754
3dca57f8 1755 BIO_printf(bio_stdout, "Doing handshakes=%d bytes=%ld\n", number, bytes);
0f113f3e 1756 for (i = 0; i < number; i++) {
69f68237 1757 if (!reuse) {
61986d32 1758 if (!SSL_set_session(c_ssl, NULL)) {
69f68237
MC
1759 BIO_printf(bio_err, "Failed to set session\n");
1760 goto end;
1761 }
1762 }
b7dffce0
KR
1763 if (client_sess_in != NULL) {
1764 if (SSL_set_session(c_ssl, client_sess) == 0) {
1765 BIO_printf(bio_err, "Can't set client session\n");
1766 ERR_print_errors(bio_err);
1767 goto end;
1768 }
1769 }
75d5bd4e
RL
1770 switch (bio_type) {
1771 case BIO_MEM:
0f113f3e 1772 ret = doit(s_ssl, c_ssl, bytes);
75d5bd4e
RL
1773 break;
1774 case BIO_PAIR:
1775 ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
1776 break;
f9e55034 1777#ifndef OPENSSL_NO_SOCK
75d5bd4e
RL
1778 case BIO_IPV4:
1779 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV4,
1780 bytes, &s_time, &c_time);
1781 break;
1782 case BIO_IPV6:
1783 ret = doit_localhost(s_ssl, c_ssl, BIO_FAMILY_IPV6,
1784 bytes, &s_time, &c_time);
1785 break;
f9e55034
MC
1786#else
1787 case BIO_IPV4:
1788 case BIO_IPV6:
c2500f65 1789 ret = EXIT_FAILURE;
f9e55034
MC
1790 goto err;
1791#endif
75d5bd4e 1792 }
c2500f65 1793 if (ret != EXIT_SUCCESS) break;
0f113f3e
MC
1794 }
1795
c2500f65 1796 if (should_negotiate && ret == EXIT_SUCCESS &&
7946ab33
KR
1797 strcmp(should_negotiate, "fail-server") != 0 &&
1798 strcmp(should_negotiate, "fail-client") != 0) {
1799 int version = protocol_from_string(should_negotiate);
1800 if (version < 0) {
1801 BIO_printf(bio_err, "Error parsing: %s\n", should_negotiate);
c2500f65 1802 ret = EXIT_FAILURE;
7946ab33
KR
1803 goto err;
1804 }
1805 if (SSL_version(c_ssl) != version) {
69687aa8 1806 BIO_printf(bio_err, "Unexpected version negotiated. "
7946ab33 1807 "Expected: %s, got %s\n", should_negotiate, SSL_get_version(c_ssl));
c2500f65 1808 ret = EXIT_FAILURE;
7946ab33
KR
1809 goto err;
1810 }
1811 }
1812
b7dffce0
KR
1813 if (should_reuse != -1) {
1814 if (SSL_session_reused(s_ssl) != should_reuse ||
1815 SSL_session_reused(c_ssl) != should_reuse) {
1816 BIO_printf(bio_err, "Unexpected session reuse state. "
1817 "Expected: %d, server: %d, client: %d\n", should_reuse,
1818 SSL_session_reused(s_ssl), SSL_session_reused(c_ssl));
c2500f65 1819 ret = EXIT_FAILURE;
b7dffce0
KR
1820 goto err;
1821 }
1822 }
1823
1824 if (server_sess_out != NULL) {
1825 if (write_session(server_sess_out, SSL_get_session(s_ssl)) == 0) {
c2500f65 1826 ret = EXIT_FAILURE;
b7dffce0
KR
1827 goto err;
1828 }
1829 }
1830 if (client_sess_out != NULL) {
1831 if (write_session(client_sess_out, SSL_get_session(c_ssl)) == 0) {
c2500f65 1832 ret = EXIT_FAILURE;
b7dffce0
KR
1833 goto err;
1834 }
1835 }
1836
0f113f3e
MC
1837 if (!verbose) {
1838 print_details(c_ssl, "");
1839 }
0f113f3e 1840 if (print_time) {
617d71bc 1841#ifdef CLOCKS_PER_SEC
0f113f3e
MC
1842 /*
1843 * "To determine the time in seconds, the value returned by the clock
1844 * function should be divided by the value of the macro
1845 * CLOCKS_PER_SEC." -- ISO/IEC 9899
1846 */
1847 BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
1848 "Approximate total client time: %6.2f s\n",
1849 (double)s_time / CLOCKS_PER_SEC,
1850 (double)c_time / CLOCKS_PER_SEC);
617d71bc 1851#else
0f113f3e
MC
1852 BIO_printf(bio_stdout,
1853 "Approximate total server time: %6.2f units\n"
1854 "Approximate total client time: %6.2f units\n",
1855 (double)s_time, (double)c_time);
617d71bc 1856#endif
0f113f3e 1857 }
58964a49 1858
7946ab33 1859 err:
0f113f3e
MC
1860 SSL_free(s_ssl);
1861 SSL_free(c_ssl);
58964a49 1862
0f113f3e 1863 end:
62adbcee 1864 SSL_CTX_free(s_ctx);
817cd0d5 1865 SSL_CTX_free(s_ctx2);
62adbcee 1866 SSL_CTX_free(c_ctx);
62adbcee 1867 SSL_CONF_CTX_free(s_cctx);
817cd0d5 1868 SSL_CONF_CTX_free(s_cctx2);
62adbcee 1869 SSL_CONF_CTX_free(c_cctx);
0f113f3e 1870 sk_OPENSSL_STRING_free(conf_args);
de94222d 1871
ca3a82c3 1872 BIO_free(bio_stdout);
d02b48c6 1873
b7dffce0
KR
1874 SSL_SESSION_free(server_sess);
1875 SSL_SESSION_free(client_sess);
1876
63215d04
MC
1877 OSSL_PROVIDER_unload(defctxnull);
1878 OSSL_PROVIDER_unload(thisprov);
b4250010 1879 OSSL_LIB_CTX_free(libctx);
63215d04 1880
ca3a82c3 1881 BIO_free(bio_err);
0f113f3e 1882 EXIT(ret);
0f113f3e 1883}
d02b48c6 1884
f9e55034 1885#ifndef OPENSSL_NO_SOCK
75d5bd4e
RL
1886int doit_localhost(SSL *s_ssl, SSL *c_ssl, int family, long count,
1887 clock_t *s_time, clock_t *c_time)
1888{
1889 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
1890 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
1891 BIO *acpt = NULL, *server = NULL, *client = NULL;
1892 char addr_str[40];
c2500f65 1893 int ret = EXIT_FAILURE;
75d5bd4e
RL
1894 int err_in_client = 0;
1895 int err_in_server = 0;
1896
de5b3a86
AP
1897 acpt = BIO_new_accept(family == BIO_FAMILY_IPV4 ? "127.0.0.1:0"
1898 : "[::1]:0");
75d5bd4e
RL
1899 if (acpt == NULL)
1900 goto err;
1901 BIO_set_accept_ip_family(acpt, family);
1902 BIO_set_bind_mode(acpt, BIO_SOCK_NONBLOCK | BIO_SOCK_REUSEADDR);
1903 if (BIO_do_accept(acpt) <= 0)
1904 goto err;
1905
6339ece1 1906 BIO_snprintf(addr_str, sizeof(addr_str), ":%s", BIO_get_accept_port(acpt));
75d5bd4e
RL
1907
1908 client = BIO_new_connect(addr_str);
1909 BIO_set_conn_ip_family(client, family);
1910 if (!client)
1911 goto err;
1912
1913 if (BIO_set_nbio(client, 1) <= 0)
1914 goto err;
1915 if (BIO_set_nbio(acpt, 1) <= 0)
1916 goto err;
1917
1918 {
1919 int st_connect = 0, st_accept = 0;
1920
1921 while(!st_connect || !st_accept) {
1922 if (!st_connect) {
1923 if (BIO_do_connect(client) <= 0) {
1924 if (!BIO_should_retry(client))
1925 goto err;
1926 } else {
1927 st_connect = 1;
1928 }
1929 }
1930 if (!st_accept) {
1931 if (BIO_do_accept(acpt) <= 0) {
1932 if (!BIO_should_retry(acpt))
1933 goto err;
1934 } else {
1935 st_accept = 1;
1936 }
1937 }
1938 }
1939 }
1940 /* We're not interested in accepting further connects */
1941 server = BIO_pop(acpt);
1942 BIO_free_all(acpt);
1943 acpt = NULL;
1944
1945 s_ssl_bio = BIO_new(BIO_f_ssl());
1946 if (!s_ssl_bio)
1947 goto err;
1948
1949 c_ssl_bio = BIO_new(BIO_f_ssl());
1950 if (!c_ssl_bio)
1951 goto err;
1952
1953 SSL_set_connect_state(c_ssl);
1954 SSL_set_bio(c_ssl, client, client);
1955 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
1956
1957 SSL_set_accept_state(s_ssl);
1958 SSL_set_bio(s_ssl, server, server);
1959 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
1960
1961 do {
1962 /*-
1963 * c_ssl_bio: SSL filter BIO
1964 *
1965 * client: I/O for SSL library
1966 *
1967 *
1968 * server: I/O for SSL library
1969 *
1970 * s_ssl_bio: SSL filter BIO
1971 */
1972
1973 /*
1974 * We have non-blocking behaviour throughout this test program, but
1975 * can be sure that there is *some* progress in each iteration; so we
1976 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
1977 * we just try everything in each iteration
1978 */
1979
1980 {
1981 /* CLIENT */
1982
1983 char cbuf[1024 * 8];
1984 int i, r;
1985 clock_t c_clock = clock();
1986
1987 memset(cbuf, 0, sizeof(cbuf));
1988
1989 if (debug)
1990 if (SSL_in_init(c_ssl))
1991 printf("client waiting in SSL_connect - %s\n",
1992 SSL_state_string_long(c_ssl));
1993
1994 if (cw_num > 0) {
1995 /* Write to server. */
1996
cbe29648
RS
1997 if (cw_num > (long)sizeof(cbuf))
1998 i = sizeof(cbuf);
75d5bd4e
RL
1999 else
2000 i = (int)cw_num;
2001 r = BIO_write(c_ssl_bio, cbuf, i);
2002 if (r < 0) {
2003 if (!BIO_should_retry(c_ssl_bio)) {
2004 fprintf(stderr, "ERROR in CLIENT\n");
2005 err_in_client = 1;
2006 goto err;
2007 }
2008 /*
2009 * BIO_should_retry(...) can just be ignored here. The
2010 * library expects us to call BIO_write with the same
2011 * arguments again, and that's what we will do in the
2012 * next iteration.
2013 */
2014 } else if (r == 0) {
2015 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2016 goto err;
2017 } else {
2018 if (debug)
2019 printf("client wrote %d\n", r);
2020 cw_num -= r;
2021 }
2022 }
2023
2024 if (cr_num > 0) {
2025 /* Read from server. */
2026
2027 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2028 if (r < 0) {
2029 if (!BIO_should_retry(c_ssl_bio)) {
2030 fprintf(stderr, "ERROR in CLIENT\n");
2031 err_in_client = 1;
2032 goto err;
2033 }
2034 /*
2035 * Again, "BIO_should_retry" can be ignored.
2036 */
2037 } else if (r == 0) {
2038 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2039 goto err;
2040 } else {
2041 if (debug)
2042 printf("client read %d\n", r);
2043 cr_num -= r;
2044 }
2045 }
2046
2047 /*
2048 * c_time and s_time increments will typically be very small
2049 * (depending on machine speed and clock tick intervals), but
2050 * sampling over a large number of connections should result in
2051 * fairly accurate figures. We cannot guarantee a lot, however
2052 * -- if each connection lasts for exactly one clock tick, it
2053 * will be counted only for the client or only for the server or
2054 * even not at all.
2055 */
2056 *c_time += (clock() - c_clock);
2057 }
2058
2059 {
2060 /* SERVER */
2061
2062 char sbuf[1024 * 8];
2063 int i, r;
2064 clock_t s_clock = clock();
2065
2066 memset(sbuf, 0, sizeof(sbuf));
2067
2068 if (debug)
2069 if (SSL_in_init(s_ssl))
2070 printf("server waiting in SSL_accept - %s\n",
2071 SSL_state_string_long(s_ssl));
2072
2073 if (sw_num > 0) {
2074 /* Write to client. */
2075
cbe29648
RS
2076 if (sw_num > (long)sizeof(sbuf))
2077 i = sizeof(sbuf);
75d5bd4e
RL
2078 else
2079 i = (int)sw_num;
2080 r = BIO_write(s_ssl_bio, sbuf, i);
2081 if (r < 0) {
2082 if (!BIO_should_retry(s_ssl_bio)) {
2083 fprintf(stderr, "ERROR in SERVER\n");
2084 err_in_server = 1;
2085 goto err;
2086 }
2087 /* Ignore "BIO_should_retry". */
2088 } else if (r == 0) {
2089 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2090 goto err;
2091 } else {
2092 if (debug)
2093 printf("server wrote %d\n", r);
2094 sw_num -= r;
2095 }
2096 }
2097
2098 if (sr_num > 0) {
2099 /* Read from client. */
2100
2101 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2102 if (r < 0) {
2103 if (!BIO_should_retry(s_ssl_bio)) {
2104 fprintf(stderr, "ERROR in SERVER\n");
2105 err_in_server = 1;
2106 goto err;
2107 }
2108 /* blah, blah */
2109 } else if (r == 0) {
2110 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2111 goto err;
2112 } else {
2113 if (debug)
2114 printf("server read %d\n", r);
2115 sr_num -= r;
2116 }
2117 }
2118
2119 *s_time += (clock() - s_clock);
2120 }
2121 }
2122 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2123
2124 if (verbose)
2125 print_details(c_ssl, "DONE via TCP connect: ");
f9e55034 2126# ifndef OPENSSL_NO_NEXTPROTONEG
c2500f65 2127 if (verify_npn(c_ssl, s_ssl) < 0)
75d5bd4e 2128 goto end;
f9e55034 2129# endif
75d5bd4e
RL
2130 if (verify_serverinfo() < 0) {
2131 fprintf(stderr, "Server info verify error\n");
75d5bd4e
RL
2132 goto err;
2133 }
c2500f65
P
2134 if (verify_alpn(c_ssl, s_ssl) < 0
2135 || verify_servername(c_ssl, s_ssl) < 0)
817cd0d5 2136 goto err;
75d5bd4e
RL
2137
2138 if (custom_ext_error) {
2139 fprintf(stderr, "Custom extension error\n");
75d5bd4e
RL
2140 goto err;
2141 }
2142
1595ca02 2143# ifndef OPENSSL_NO_NEXTPROTONEG
75d5bd4e 2144 end:
1595ca02 2145# endif
c2500f65 2146 ret = EXIT_SUCCESS;
75d5bd4e
RL
2147
2148 err:
2149 ERR_print_errors(bio_err);
2150
2151 BIO_free_all(acpt);
2152 BIO_free(server);
2153 BIO_free(client);
2154 BIO_free(s_ssl_bio);
2155 BIO_free(c_ssl_bio);
2156
2157 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
c2500f65 2158 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
75d5bd4e 2159 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
c2500f65 2160 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
75d5bd4e
RL
2161
2162 return ret;
2163}
f9e55034 2164#endif
75d5bd4e 2165
563f1503 2166int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
0f113f3e
MC
2167 clock_t *s_time, clock_t *c_time)
2168{
2169 long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
2170 BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
2171 BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
c2500f65 2172 int ret = EXIT_FAILURE;
7946ab33
KR
2173 int err_in_client = 0;
2174 int err_in_server = 0;
0f113f3e
MC
2175
2176 size_t bufsiz = 256; /* small buffer for testing */
2177
2178 if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
2179 goto err;
2180 if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
2181 goto err;
2182
2183 s_ssl_bio = BIO_new(BIO_f_ssl());
2184 if (!s_ssl_bio)
2185 goto err;
2186
2187 c_ssl_bio = BIO_new(BIO_f_ssl());
2188 if (!c_ssl_bio)
2189 goto err;
2190
2191 SSL_set_connect_state(c_ssl);
2192 SSL_set_bio(c_ssl, client, client);
2193 (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
2194
2195 SSL_set_accept_state(s_ssl);
2196 SSL_set_bio(s_ssl, server, server);
2197 (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
2198
2199 do {
35a1cc90
MC
2200 /*-
2201 * c_ssl_bio: SSL filter BIO
2202 *
2203 * client: pseudo-I/O for SSL library
2204 *
2205 * client_io: client's SSL communication; usually to be
2206 * relayed over some I/O facility, but in this
2207 * test program, we're the server, too:
2208 *
2209 * server_io: server's SSL communication
2210 *
2211 * server: pseudo-I/O for SSL library
2212 *
2213 * s_ssl_bio: SSL filter BIO
2214 *
2215 * The client and the server each employ a "BIO pair":
2216 * client + client_io, server + server_io.
2217 * BIO pairs are symmetric. A BIO pair behaves similar
2218 * to a non-blocking socketpair (but both endpoints must
2219 * be handled by the same thread).
2220 * [Here we could connect client and server to the ends
2221 * of a single BIO pair, but then this code would be less
2222 * suitable as an example for BIO pairs in general.]
2223 *
2224 * Useful functions for querying the state of BIO pair endpoints:
2225 *
2226 * BIO_ctrl_pending(bio) number of bytes we can read now
44e69951 2227 * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfill
35a1cc90
MC
2228 * other side's read attempt
2229 * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
2230 *
2231 * ..._read_request is never more than ..._write_guarantee;
2232 * it depends on the application which one you should use.
2233 */
0f113f3e
MC
2234
2235 /*
2236 * We have non-blocking behaviour throughout this test program, but
2237 * can be sure that there is *some* progress in each iteration; so we
2238 * don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE --
2239 * we just try everything in each iteration
2240 */
2241
2242 {
2243 /* CLIENT */
2244
68b00c23 2245 char cbuf[1024 * 8];
0f113f3e
MC
2246 int i, r;
2247 clock_t c_clock = clock();
2248
2249 memset(cbuf, 0, sizeof(cbuf));
2250
2251 if (debug)
2252 if (SSL_in_init(c_ssl))
2253 printf("client waiting in SSL_connect - %s\n",
2254 SSL_state_string_long(c_ssl));
2255
2256 if (cw_num > 0) {
2257 /* Write to server. */
2258
cbe29648
RS
2259 if (cw_num > (long)sizeof(cbuf))
2260 i = sizeof(cbuf);
0f113f3e
MC
2261 else
2262 i = (int)cw_num;
2263 r = BIO_write(c_ssl_bio, cbuf, i);
2264 if (r < 0) {
2265 if (!BIO_should_retry(c_ssl_bio)) {
2266 fprintf(stderr, "ERROR in CLIENT\n");
7946ab33 2267 err_in_client = 1;
0f113f3e
MC
2268 goto err;
2269 }
2270 /*
2271 * BIO_should_retry(...) can just be ignored here. The
2272 * library expects us to call BIO_write with the same
2273 * arguments again, and that's what we will do in the
2274 * next iteration.
2275 */
2276 } else if (r == 0) {
2277 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2278 goto err;
2279 } else {
2280 if (debug)
2281 printf("client wrote %d\n", r);
2282 cw_num -= r;
2283 }
2284 }
2285
2286 if (cr_num > 0) {
2287 /* Read from server. */
2288
2289 r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
2290 if (r < 0) {
2291 if (!BIO_should_retry(c_ssl_bio)) {
2292 fprintf(stderr, "ERROR in CLIENT\n");
7946ab33 2293 err_in_client = 1;
0f113f3e
MC
2294 goto err;
2295 }
2296 /*
2297 * Again, "BIO_should_retry" can be ignored.
2298 */
2299 } else if (r == 0) {
2300 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2301 goto err;
2302 } else {
2303 if (debug)
2304 printf("client read %d\n", r);
2305 cr_num -= r;
2306 }
2307 }
2308
2309 /*
2310 * c_time and s_time increments will typically be very small
2311 * (depending on machine speed and clock tick intervals), but
2312 * sampling over a large number of connections should result in
2313 * fairly accurate figures. We cannot guarantee a lot, however
2314 * -- if each connection lasts for exactly one clock tick, it
2315 * will be counted only for the client or only for the server or
2316 * even not at all.
2317 */
2318 *c_time += (clock() - c_clock);
2319 }
2320
2321 {
2322 /* SERVER */
2323
68b00c23 2324 char sbuf[1024 * 8];
0f113f3e
MC
2325 int i, r;
2326 clock_t s_clock = clock();
2327
2328 memset(sbuf, 0, sizeof(sbuf));
2329
2330 if (debug)
2331 if (SSL_in_init(s_ssl))
2332 printf("server waiting in SSL_accept - %s\n",
2333 SSL_state_string_long(s_ssl));
2334
2335 if (sw_num > 0) {
2336 /* Write to client. */
2337
cbe29648
RS
2338 if (sw_num > (long)sizeof(sbuf))
2339 i = sizeof(sbuf);
0f113f3e
MC
2340 else
2341 i = (int)sw_num;
2342 r = BIO_write(s_ssl_bio, sbuf, i);
2343 if (r < 0) {
2344 if (!BIO_should_retry(s_ssl_bio)) {
2345 fprintf(stderr, "ERROR in SERVER\n");
7946ab33 2346 err_in_server = 1;
0f113f3e
MC
2347 goto err;
2348 }
2349 /* Ignore "BIO_should_retry". */
2350 } else if (r == 0) {
2351 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2352 goto err;
2353 } else {
2354 if (debug)
2355 printf("server wrote %d\n", r);
2356 sw_num -= r;
2357 }
2358 }
2359
2360 if (sr_num > 0) {
2361 /* Read from client. */
2362
2363 r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
2364 if (r < 0) {
2365 if (!BIO_should_retry(s_ssl_bio)) {
2366 fprintf(stderr, "ERROR in SERVER\n");
7946ab33 2367 err_in_server = 1;
0f113f3e
MC
2368 goto err;
2369 }
2370 /* blah, blah */
2371 } else if (r == 0) {
2372 fprintf(stderr, "SSL SERVER STARTUP FAILED\n");
2373 goto err;
2374 } else {
2375 if (debug)
2376 printf("server read %d\n", r);
2377 sr_num -= r;
2378 }
2379 }
2380
2381 *s_time += (clock() - s_clock);
2382 }
2383
2384 {
2385 /* "I/O" BETWEEN CLIENT AND SERVER. */
2386
2387 size_t r1, r2;
2388 BIO *io1 = server_io, *io2 = client_io;
2389 /*
2390 * we use the non-copying interface for io1 and the standard
2391 * BIO_write/BIO_read interface for io2
2392 */
2393
2394 static int prev_progress = 1;
2395 int progress = 0;
2396
2397 /* io1 to io2 */
2398 do {
2399 size_t num;
2400 int r;
2401
2402 r1 = BIO_ctrl_pending(io1);
2403 r2 = BIO_ctrl_get_write_guarantee(io2);
2404
2405 num = r1;
2406 if (r2 < num)
2407 num = r2;
2408 if (num) {
2409 char *dataptr;
2410
2411 if (INT_MAX < num) /* yeah, right */
2412 num = INT_MAX;
2413
2414 r = BIO_nread(io1, &dataptr, (int)num);
2415 assert(r > 0);
2416 assert(r <= (int)num);
2417 /*
2418 * possibly r < num (non-contiguous data)
2419 */
2420 num = r;
2421 r = BIO_write(io2, dataptr, (int)num);
2422 if (r != (int)num) { /* can't happen */
2423 fprintf(stderr, "ERROR: BIO_write could not write "
2424 "BIO_ctrl_get_write_guarantee() bytes");
2425 goto err;
2426 }
2427 progress = 1;
2428
2429 if (debug)
2430 printf((io1 == client_io) ?
2431 "C->S relaying: %d bytes\n" :
2432 "S->C relaying: %d bytes\n", (int)num);
2433 }
2434 }
2435 while (r1 && r2);
2436
2437 /* io2 to io1 */
2438 {
2439 size_t num;
2440 int r;
2441
2442 r1 = BIO_ctrl_pending(io2);
2443 r2 = BIO_ctrl_get_read_request(io1);
2444 /*
2445 * here we could use ..._get_write_guarantee instead of
2446 * ..._get_read_request, but by using the latter we test
2447 * restartability of the SSL implementation more thoroughly
2448 */
2449 num = r1;
2450 if (r2 < num)
2451 num = r2;
2452 if (num) {
2453 char *dataptr;
2454
2455 if (INT_MAX < num)
2456 num = INT_MAX;
2457
2458 if (num > 1)
2459 --num; /* test restartability even more thoroughly */
2460
2461 r = BIO_nwrite0(io1, &dataptr);
2462 assert(r > 0);
2463 if (r < (int)num)
2464 num = r;
2465 r = BIO_read(io2, dataptr, (int)num);
2466 if (r != (int)num) { /* can't happen */
2467 fprintf(stderr, "ERROR: BIO_read could not read "
2468 "BIO_ctrl_pending() bytes");
2469 goto err;
2470 }
2471 progress = 1;
2472 r = BIO_nwrite(io1, &dataptr, (int)num);
2473 if (r != (int)num) { /* can't happen */
2474 fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
2475 "BIO_nwrite0() bytes");
2476 goto err;
2477 }
2478
2479 if (debug)
2480 printf((io2 == client_io) ?
2481 "C->S relaying: %d bytes\n" :
2482 "S->C relaying: %d bytes\n", (int)num);
2483 }
2484 } /* no loop, BIO_ctrl_get_read_request now
2485 * returns 0 anyway */
2486
2487 if (!progress && !prev_progress)
2488 if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) {
2489 fprintf(stderr, "ERROR: got stuck\n");
2490 fprintf(stderr, " ERROR.\n");
2491 goto err;
2492 }
2493 prev_progress = progress;
2494 }
2495 }
2496 while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
2497
2498 if (verbose)
2499 print_details(c_ssl, "DONE via BIO pair: ");
2911575c 2500#ifndef OPENSSL_NO_NEXTPROTONEG
c2500f65 2501 if (verify_npn(c_ssl, s_ssl) < 0)
0f113f3e 2502 goto end;
0f113f3e
MC
2503#endif
2504 if (verify_serverinfo() < 0) {
2505 fprintf(stderr, "Server info verify error\n");
0f113f3e 2506 goto err;
817cd0d5 2507 }
c2500f65
P
2508 if (verify_alpn(c_ssl, s_ssl) < 0
2509 || verify_servername(c_ssl, s_ssl) < 0)
817cd0d5 2510 goto err;
0f113f3e
MC
2511
2512 if (custom_ext_error) {
2513 fprintf(stderr, "Custom extension error\n");
0f113f3e
MC
2514 goto err;
2515 }
2516
1595ca02 2517#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e 2518 end:
1595ca02 2519#endif
c2500f65 2520 ret = EXIT_SUCCESS;
95d29597
BM
2521
2522 err:
0f113f3e
MC
2523 ERR_print_errors(bio_err);
2524
ca3a82c3
RS
2525 BIO_free(server);
2526 BIO_free(server_io);
2527 BIO_free(client);
2528 BIO_free(client_io);
2529 BIO_free(s_ssl_bio);
2530 BIO_free(c_ssl_bio);
0f113f3e 2531
7946ab33 2532 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
c2500f65 2533 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
7946ab33 2534 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
c2500f65 2535 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
7946ab33 2536
0f113f3e
MC
2537 return ret;
2538}
2539
2540#define W_READ 1
2541#define W_WRITE 2
2542#define C_DONE 1
2543#define S_DONE 2
d02b48c6 2544
6b691a5c 2545int doit(SSL *s_ssl, SSL *c_ssl, long count)
0f113f3e
MC
2546{
2547 char *cbuf = NULL, *sbuf = NULL;
2548 long bufsiz;
2549 long cw_num = count, cr_num = count;
2550 long sw_num = count, sr_num = count;
c2500f65 2551 int ret = EXIT_FAILURE;
0f113f3e
MC
2552 BIO *c_to_s = NULL;
2553 BIO *s_to_c = NULL;
2554 BIO *c_bio = NULL;
2555 BIO *s_bio = NULL;
2556 int c_r, c_w, s_r, s_w;
2557 int i, j;
2558 int done = 0;
2559 int c_write, s_write;
2560 int do_server = 0, do_client = 0;
2561 int max_frag = 5 * 1024;
7946ab33
KR
2562 int err_in_client = 0;
2563 int err_in_server = 0;
0f113f3e
MC
2564
2565 bufsiz = count > 40 * 1024 ? 40 * 1024 : count;
2566
b51bce94 2567 if ((cbuf = OPENSSL_zalloc(bufsiz)) == NULL)
0f113f3e 2568 goto err;
b51bce94 2569 if ((sbuf = OPENSSL_zalloc(bufsiz)) == NULL)
0f113f3e
MC
2570 goto err;
2571
0f113f3e
MC
2572 c_to_s = BIO_new(BIO_s_mem());
2573 s_to_c = BIO_new(BIO_s_mem());
2574 if ((s_to_c == NULL) || (c_to_s == NULL)) {
2575 ERR_print_errors(bio_err);
2576 goto err;
2577 }
2578
2579 c_bio = BIO_new(BIO_f_ssl());
2580 s_bio = BIO_new(BIO_f_ssl());
2581 if ((c_bio == NULL) || (s_bio == NULL)) {
2582 ERR_print_errors(bio_err);
2583 goto err;
2584 }
2585
2586 SSL_set_connect_state(c_ssl);
2587 SSL_set_bio(c_ssl, s_to_c, c_to_s);
2588 SSL_set_max_send_fragment(c_ssl, max_frag);
2589 BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE);
2590
e304d3e2
MC
2591 /*
2592 * We've just given our ref to these BIOs to c_ssl. We need another one to
2593 * give to s_ssl
2594 */
2595 if (!BIO_up_ref(c_to_s)) {
2596 /* c_to_s and s_to_c will get freed when we free c_ssl */
2597 c_to_s = NULL;
2598 s_to_c = NULL;
2599 goto err;
2600 }
2601 if (!BIO_up_ref(s_to_c)) {
2602 /* s_to_c will get freed when we free c_ssl */
2603 s_to_c = NULL;
2604 goto err;
2605 }
2606
0f113f3e
MC
2607 SSL_set_accept_state(s_ssl);
2608 SSL_set_bio(s_ssl, c_to_s, s_to_c);
e304d3e2
MC
2609
2610 /* We've used up all our refs to these now */
2611 c_to_s = NULL;
2612 s_to_c = NULL;
2613
0f113f3e
MC
2614 SSL_set_max_send_fragment(s_ssl, max_frag);
2615 BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE);
2616
2617 c_r = 0;
2618 s_r = 1;
2619 c_w = 1;
2620 s_w = 0;
2621 c_write = 1, s_write = 0;
2622
2623 /* We can always do writes */
2624 for (;;) {
2625 do_server = 0;
2626 do_client = 0;
2627
2628 i = (int)BIO_pending(s_bio);
2629 if ((i && s_r) || s_w)
2630 do_server = 1;
2631
2632 i = (int)BIO_pending(c_bio);
2633 if ((i && c_r) || c_w)
2634 do_client = 1;
2635
2636 if (do_server && debug) {
2637 if (SSL_in_init(s_ssl))
2638 printf("server waiting in SSL_accept - %s\n",
2639 SSL_state_string_long(s_ssl));
0f113f3e
MC
2640 }
2641
2642 if (do_client && debug) {
2643 if (SSL_in_init(c_ssl))
2644 printf("client waiting in SSL_connect - %s\n",
2645 SSL_state_string_long(c_ssl));
0f113f3e
MC
2646 }
2647
2648 if (!do_client && !do_server) {
2649 fprintf(stdout, "ERROR IN STARTUP\n");
2650 ERR_print_errors(bio_err);
ae632974 2651 goto err;
0f113f3e
MC
2652 }
2653 if (do_client && !(done & C_DONE)) {
2654 if (c_write) {
2655 j = (cw_num > bufsiz) ? (int)bufsiz : (int)cw_num;
2656 i = BIO_write(c_bio, cbuf, j);
2657 if (i < 0) {
2658 c_r = 0;
2659 c_w = 0;
2660 if (BIO_should_retry(c_bio)) {
2661 if (BIO_should_read(c_bio))
2662 c_r = 1;
2663 if (BIO_should_write(c_bio))
2664 c_w = 1;
2665 } else {
2666 fprintf(stderr, "ERROR in CLIENT\n");
7946ab33 2667 err_in_client = 1;
0f113f3e
MC
2668 ERR_print_errors(bio_err);
2669 goto err;
2670 }
2671 } else if (i == 0) {
2672 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2673 goto err;
2674 } else {
2675 if (debug)
2676 printf("client wrote %d\n", i);
2677 /* ok */
2678 s_r = 1;
2679 c_write = 0;
2680 cw_num -= i;
2681 if (max_frag > 1029)
2682 SSL_set_max_send_fragment(c_ssl, max_frag -= 5);
2683 }
2684 } else {
2685 i = BIO_read(c_bio, cbuf, bufsiz);
2686 if (i < 0) {
2687 c_r = 0;
2688 c_w = 0;
2689 if (BIO_should_retry(c_bio)) {
2690 if (BIO_should_read(c_bio))
2691 c_r = 1;
2692 if (BIO_should_write(c_bio))
2693 c_w = 1;
2694 } else {
2695 fprintf(stderr, "ERROR in CLIENT\n");
7946ab33 2696 err_in_client = 1;
0f113f3e
MC
2697 ERR_print_errors(bio_err);
2698 goto err;
2699 }
2700 } else if (i == 0) {
2701 fprintf(stderr, "SSL CLIENT STARTUP FAILED\n");
2702 goto err;
2703 } else {
2704 if (debug)
2705 printf("client read %d\n", i);
2706 cr_num -= i;
2707 if (sw_num > 0) {
2708 s_write = 1;
2709 s_w = 1;
2710 }
2711 if (cr_num <= 0) {
2712 s_write = 1;
2713 s_w = 1;
2714 done = S_DONE | C_DONE;
2715 }
2716 }
2717 }
2718 }
2719
2720 if (do_server && !(done & S_DONE)) {
2721 if (!s_write) {
2722 i = BIO_read(s_bio, sbuf, bufsiz);
2723 if (i < 0) {
2724 s_r = 0;
2725 s_w = 0;
2726 if (BIO_should_retry(s_bio)) {
2727 if (BIO_should_read(s_bio))
2728 s_r = 1;
2729 if (BIO_should_write(s_bio))
2730 s_w = 1;
2731 } else {
2732 fprintf(stderr, "ERROR in SERVER\n");
7946ab33 2733 err_in_server = 1;
0f113f3e
MC
2734 ERR_print_errors(bio_err);
2735 goto err;
2736 }
2737 } else if (i == 0) {
2738 ERR_print_errors(bio_err);
2739 fprintf(stderr,
2740 "SSL SERVER STARTUP FAILED in SSL_read\n");
2741 goto err;
2742 } else {
2743 if (debug)
2744 printf("server read %d\n", i);
2745 sr_num -= i;
2746 if (cw_num > 0) {
2747 c_write = 1;
2748 c_w = 1;
2749 }
2750 if (sr_num <= 0) {
2751 s_write = 1;
2752 s_w = 1;
2753 c_write = 0;
2754 }
2755 }
2756 } else {
2757 j = (sw_num > bufsiz) ? (int)bufsiz : (int)sw_num;
2758 i = BIO_write(s_bio, sbuf, j);
2759 if (i < 0) {
2760 s_r = 0;
2761 s_w = 0;
2762 if (BIO_should_retry(s_bio)) {
2763 if (BIO_should_read(s_bio))
2764 s_r = 1;
2765 if (BIO_should_write(s_bio))
2766 s_w = 1;
2767 } else {
2768 fprintf(stderr, "ERROR in SERVER\n");
7946ab33 2769 err_in_server = 1;
0f113f3e
MC
2770 ERR_print_errors(bio_err);
2771 goto err;
2772 }
2773 } else if (i == 0) {
2774 ERR_print_errors(bio_err);
2775 fprintf(stderr,
2776 "SSL SERVER STARTUP FAILED in SSL_write\n");
2777 goto err;
2778 } else {
2779 if (debug)
2780 printf("server wrote %d\n", i);
2781 sw_num -= i;
2782 s_write = 0;
2783 c_r = 1;
2784 if (sw_num <= 0)
2785 done |= S_DONE;
2786 if (max_frag > 1029)
2787 SSL_set_max_send_fragment(s_ssl, max_frag -= 5);
2788 }
2789 }
2790 }
2791
2792 if ((done & S_DONE) && (done & C_DONE))
2793 break;
2794 }
2795
2796 if (verbose)
2797 print_details(c_ssl, "DONE: ");
2911575c 2798#ifndef OPENSSL_NO_NEXTPROTONEG
c2500f65 2799 if (verify_npn(c_ssl, s_ssl) < 0)
0f113f3e 2800 goto err;
0f113f3e
MC
2801#endif
2802 if (verify_serverinfo() < 0) {
2803 fprintf(stderr, "Server info verify error\n");
0f113f3e
MC
2804 goto err;
2805 }
2806 if (custom_ext_error) {
2807 fprintf(stderr, "Custom extension error\n");
0f113f3e
MC
2808 goto err;
2809 }
c2500f65 2810 ret = EXIT_SUCCESS;
0f113f3e 2811 err:
ca3a82c3
RS
2812 BIO_free(c_to_s);
2813 BIO_free(s_to_c);
2814 BIO_free_all(c_bio);
2815 BIO_free_all(s_bio);
b548a1f1
RS
2816 OPENSSL_free(cbuf);
2817 OPENSSL_free(sbuf);
0f113f3e 2818
7946ab33 2819 if (should_negotiate != NULL && strcmp(should_negotiate, "fail-client") == 0)
c2500f65 2820 ret = (err_in_client != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
7946ab33 2821 else if (should_negotiate != NULL && strcmp(should_negotiate, "fail-server") == 0)
c2500f65 2822 ret = (err_in_server != 0) ? EXIT_SUCCESS : EXIT_FAILURE;
7946ab33 2823
c2500f65 2824 return ret;
0f113f3e 2825}
d02b48c6 2826
6d23cf97 2827static int verify_callback(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
2828{
2829 char *s, buf[256];
2830
f0e0fd51 2831 s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),
cbe29648 2832 buf, sizeof(buf));
0f113f3e
MC
2833 if (s != NULL) {
2834 if (ok)
f0e0fd51 2835 printf("depth=%d %s\n", X509_STORE_CTX_get_error_depth(ctx), buf);
0f113f3e
MC
2836 else {
2837 fprintf(stderr, "depth=%d error=%d %s\n",
f0e0fd51
RS
2838 X509_STORE_CTX_get_error_depth(ctx),
2839 X509_STORE_CTX_get_error(ctx), buf);
0f113f3e
MC
2840 }
2841 }
2842
2843 if (ok == 0) {
f0e0fd51
RS
2844 int i = X509_STORE_CTX_get_error(ctx);
2845
2846 switch (i) {
3dca57f8
RS
2847 default:
2848 fprintf(stderr, "Error string: %s\n",
f0e0fd51 2849 X509_verify_cert_error_string(i));
3dca57f8 2850 break;
0f113f3e
MC
2851 case X509_V_ERR_CERT_NOT_YET_VALID:
2852 case X509_V_ERR_CERT_HAS_EXPIRED:
2853 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
0f113f3e 2854 ok = 1;
f0e0fd51 2855 break;
0f113f3e
MC
2856 }
2857 }
2858
c2500f65 2859 return ok;
0f113f3e 2860}
d02b48c6 2861
6d23cf97 2862static int app_verify_callback(X509_STORE_CTX *ctx, void *arg)
0f113f3e
MC
2863{
2864 int ok = 1;
2865 struct app_verify_arg *cb_arg = arg;
0f113f3e
MC
2866
2867 if (cb_arg->app_verify) {
2868 char *s = NULL, buf[256];
f0e0fd51 2869 X509 *c = X509_STORE_CTX_get0_cert(ctx);
0f113f3e 2870
3dca57f8
RS
2871 printf("In app_verify_callback, allowing cert. ");
2872 printf("Arg is: %s\n", cb_arg->string);
2873 printf("Finished printing do we have a context? 0x%p a cert? 0x%p\n",
f0e0fd51
RS
2874 (void *)ctx, (void *)c);
2875 if (c)
2876 s = X509_NAME_oneline(X509_get_subject_name(c), buf, 256);
0f113f3e 2877 if (s != NULL) {
f0e0fd51
RS
2878 printf("cert depth=%d %s\n",
2879 X509_STORE_CTX_get_error_depth(ctx), buf);
0f113f3e 2880 }
c2500f65 2881 return 1;
0f113f3e 2882 }
0f113f3e 2883
0f113f3e 2884 ok = X509_verify_cert(ctx);
0f113f3e 2885
c2500f65 2886 return ok;
0f113f3e 2887}
023ec151 2888
ddac1974
NL
2889#ifndef OPENSSL_NO_PSK
2890/* convert the PSK key (psk_key) in ascii to binary (psk) */
2891static int psk_key2bn(const char *pskkey, unsigned char *psk,
0f113f3e
MC
2892 unsigned int max_psk_len)
2893{
2894 int ret;
2895 BIGNUM *bn = NULL;
2896
2897 ret = BN_hex2bn(&bn, pskkey);
2898 if (!ret) {
2899 BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n",
2900 pskkey);
23a1d5e9 2901 BN_free(bn);
0f113f3e
MC
2902 return 0;
2903 }
2904 if (BN_num_bytes(bn) > (int)max_psk_len) {
2905 BIO_printf(bio_err,
2906 "psk buffer of callback is too small (%d) for key (%d)\n",
2907 max_psk_len, BN_num_bytes(bn));
2908 BN_free(bn);
2909 return 0;
2910 }
2911 ret = BN_bn2bin(bn, psk);
2912 BN_free(bn);
2913 return ret;
2914}
2915
2916static unsigned int psk_client_callback(SSL *ssl, const char *hint,
2917 char *identity,
2918 unsigned int max_identity_len,
2919 unsigned char *psk,
2920 unsigned int max_psk_len)
2921{
2922 int ret;
2923 unsigned int psk_len = 0;
2924
2925 ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
2926 if (ret < 0)
2927 goto out_err;
2928 if (debug)
2929 fprintf(stderr, "client: created identity '%s' len=%d\n", identity,
2930 ret);
2931 ret = psk_key2bn(psk_key, psk, max_psk_len);
2932 if (ret < 0)
2933 goto out_err;
2934 psk_len = ret;
2935 out_err:
2936 return psk_len;
2937}
ddac1974
NL
2938
2939static unsigned int psk_server_callback(SSL *ssl, const char *identity,
0f113f3e
MC
2940 unsigned char *psk,
2941 unsigned int max_psk_len)
2942{
2943 unsigned int psk_len = 0;
ddac1974 2944
0f113f3e
MC
2945 if (strcmp(identity, "Client_identity") != 0) {
2946 BIO_printf(bio_err, "server: PSK error: client identity not found\n");
2947 return 0;
2948 }
2949 psk_len = psk_key2bn(psk_key, psk, max_psk_len);
2950 return psk_len;
2951}
ddac1974 2952#endif