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