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