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