diff options
Diffstat (limited to 'src/lib/libssl/src/ssl/ssltest.c')
-rw-r--r-- | src/lib/libssl/src/ssl/ssltest.c | 2392 |
1 files changed, 2392 insertions, 0 deletions
diff --git a/src/lib/libssl/src/ssl/ssltest.c b/src/lib/libssl/src/ssl/ssltest.c new file mode 100644 index 0000000000..664147cb6a --- /dev/null +++ b/src/lib/libssl/src/ssl/ssltest.c | |||
@@ -0,0 +1,2392 @@ | |||
1 | /* ssl/ssltest.c */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. | ||
60 | * | ||
61 | * Redistribution and use in source and binary forms, with or without | ||
62 | * modification, are permitted provided that the following conditions | ||
63 | * are met: | ||
64 | * | ||
65 | * 1. Redistributions of source code must retain the above copyright | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
67 | * | ||
68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
69 | * notice, this list of conditions and the following disclaimer in | ||
70 | * the documentation and/or other materials provided with the | ||
71 | * distribution. | ||
72 | * | ||
73 | * 3. All advertising materials mentioning features or use of this | ||
74 | * software must display the following acknowledgment: | ||
75 | * "This product includes software developed by the OpenSSL Project | ||
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
77 | * | ||
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
79 | * endorse or promote products derived from this software without | ||
80 | * prior written permission. For written permission, please contact | ||
81 | * openssl-core@openssl.org. | ||
82 | * | ||
83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
84 | * nor may "OpenSSL" appear in their names without prior written | ||
85 | * permission of the OpenSSL Project. | ||
86 | * | ||
87 | * 6. Redistributions of any form whatsoever must retain the following | ||
88 | * acknowledgment: | ||
89 | * "This product includes software developed by the OpenSSL Project | ||
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
91 | * | ||
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
104 | * ==================================================================== | ||
105 | * | ||
106 | * This product includes cryptographic software written by Eric Young | ||
107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
108 | * Hudson (tjh@cryptsoft.com). | ||
109 | * | ||
110 | */ | ||
111 | /* ==================================================================== | ||
112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
113 | * ECC cipher suite support in OpenSSL originally developed by | ||
114 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. | ||
115 | */ | ||
116 | /* ==================================================================== | ||
117 | * Copyright 2005 Nokia. All rights reserved. | ||
118 | * | ||
119 | * The portions of the attached software ("Contribution") is developed by | ||
120 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | ||
121 | * license. | ||
122 | * | ||
123 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | ||
124 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | ||
125 | * support (see RFC 4279) to OpenSSL. | ||
126 | * | ||
127 | * No patent licenses or other rights except those expressly stated in | ||
128 | * the OpenSSL open source license shall be deemed granted or received | ||
129 | * expressly, by implication, estoppel, or otherwise. | ||
130 | * | ||
131 | * No assurances are provided by Nokia that the Contribution does not | ||
132 | * infringe the patent or other intellectual property rights of any third | ||
133 | * party or that the license provides you with all the necessary rights | ||
134 | * to make use of the Contribution. | ||
135 | * | ||
136 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | ||
137 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | ||
138 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | ||
139 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | ||
140 | * OTHERWISE. | ||
141 | */ | ||
142 | |||
143 | #define _BSD_SOURCE 1 /* Or gethostname won't be declared properly | ||
144 | on Linux and GNU platforms. */ | ||
145 | #include <sys/types.h> | ||
146 | #include <sys/param.h> | ||
147 | #include <sys/socket.h> | ||
148 | |||
149 | #include <netinet/in.h> | ||
150 | |||
151 | #include <assert.h> | ||
152 | #include <errno.h> | ||
153 | #include <limits.h> | ||
154 | #include <netdb.h> | ||
155 | #include <stdio.h> | ||
156 | #include <stdlib.h> | ||
157 | #include <string.h> | ||
158 | #include <time.h> | ||
159 | #include <unistd.h> | ||
160 | |||
161 | #include <ctype.h> | ||
162 | |||
163 | #include <openssl/opensslconf.h> | ||
164 | #include <openssl/e_os2.h> | ||
165 | #include <openssl/bio.h> | ||
166 | #include <openssl/crypto.h> | ||
167 | #include <openssl/evp.h> | ||
168 | #include <openssl/x509.h> | ||
169 | #include <openssl/x509v3.h> | ||
170 | #include <openssl/ssl.h> | ||
171 | #ifndef OPENSSL_NO_ENGINE | ||
172 | #include <openssl/engine.h> | ||
173 | #endif | ||
174 | #include <openssl/err.h> | ||
175 | #include <openssl/rand.h> | ||
176 | #ifndef OPENSSL_NO_RSA | ||
177 | #include <openssl/rsa.h> | ||
178 | #endif | ||
179 | #ifndef OPENSSL_NO_DSA | ||
180 | #include <openssl/dsa.h> | ||
181 | #endif | ||
182 | #ifndef OPENSSL_NO_DH | ||
183 | #include <openssl/dh.h> | ||
184 | #endif | ||
185 | #ifndef OPENSSL_NO_SRP | ||
186 | #include <openssl/srp.h> | ||
187 | #endif | ||
188 | #include <openssl/bn.h> | ||
189 | |||
190 | #define _XOPEN_SOURCE_EXTENDED 1 /* Or gethostname won't be declared properly | ||
191 | on Compaq platforms (at least with DEC C). | ||
192 | Do not try to put it earlier, or IPv6 includes | ||
193 | get screwed... | ||
194 | */ | ||
195 | |||
196 | #include OPENSSL_UNISTD | ||
197 | |||
198 | # define TEST_SERVER_CERT "../apps/server.pem" | ||
199 | # define TEST_CLIENT_CERT "../apps/client.pem" | ||
200 | |||
201 | /* There is really no standard for this, so let's assign some tentative | ||
202 | numbers. In any case, these numbers are only for this test */ | ||
203 | #define COMP_RLE 255 | ||
204 | #define COMP_ZLIB 1 | ||
205 | |||
206 | static int verify_callback(int ok, X509_STORE_CTX *ctx); | ||
207 | #ifndef OPENSSL_NO_RSA | ||
208 | static RSA *tmp_rsa_cb(SSL *s, int is_export, int keylength); | ||
209 | static void free_tmp_rsa(void); | ||
210 | #endif | ||
211 | static int app_verify_callback(X509_STORE_CTX *ctx, void *arg); | ||
212 | #define APP_CALLBACK_STRING "Test Callback Argument" | ||
213 | struct app_verify_arg { | ||
214 | char *string; | ||
215 | int app_verify; | ||
216 | int allow_proxy_certs; | ||
217 | char *proxy_auth; | ||
218 | char *proxy_cond; | ||
219 | }; | ||
220 | |||
221 | #ifndef OPENSSL_NO_DH | ||
222 | static DH *get_dh512(void); | ||
223 | static DH *get_dh1024(void); | ||
224 | static DH *get_dh1024dsa(void); | ||
225 | #endif | ||
226 | |||
227 | |||
228 | static char *psk_key = NULL; /* by default PSK is not used */ | ||
229 | #ifndef OPENSSL_NO_PSK | ||
230 | static unsigned int psk_client_callback(SSL *ssl, const char *hint, | ||
231 | char *identity, unsigned int max_identity_len, unsigned char *psk, | ||
232 | unsigned int max_psk_len); | ||
233 | static unsigned int psk_server_callback(SSL *ssl, const char *identity, | ||
234 | unsigned char *psk, unsigned int max_psk_len); | ||
235 | #endif | ||
236 | |||
237 | #ifndef OPENSSL_NO_SRP | ||
238 | /* SRP client */ | ||
239 | /* This is a context that we pass to all callbacks */ | ||
240 | typedef struct srp_client_arg_st { | ||
241 | char *srppassin; | ||
242 | char *srplogin; | ||
243 | } SRP_CLIENT_ARG; | ||
244 | |||
245 | #define PWD_STRLEN 1024 | ||
246 | |||
247 | static char * | ||
248 | ssl_give_srp_client_pwd_cb(SSL *s, void *arg) | ||
249 | { | ||
250 | SRP_CLIENT_ARG *srp_client_arg = (SRP_CLIENT_ARG *)arg; | ||
251 | return BUF_strdup((char *)srp_client_arg->srppassin); | ||
252 | } | ||
253 | |||
254 | /* SRP server */ | ||
255 | /* This is a context that we pass to SRP server callbacks */ | ||
256 | typedef struct srp_server_arg_st { | ||
257 | char *expected_user; | ||
258 | char *pass; | ||
259 | } SRP_SERVER_ARG; | ||
260 | |||
261 | static int | ||
262 | ssl_srp_server_param_cb(SSL *s, int *ad, void *arg) | ||
263 | { | ||
264 | SRP_SERVER_ARG *p = (SRP_SERVER_ARG *) arg; | ||
265 | |||
266 | if (strcmp(p->expected_user, SSL_get_srp_username(s)) != 0) { | ||
267 | fprintf(stderr, "User %s doesn't exist\n", SSL_get_srp_username(s)); | ||
268 | return SSL3_AL_FATAL; | ||
269 | } | ||
270 | if (SSL_set_srp_server_param_pw(s, p->expected_user, p->pass, "1024") < 0) { | ||
271 | *ad = SSL_AD_INTERNAL_ERROR; | ||
272 | return SSL3_AL_FATAL; | ||
273 | } | ||
274 | return SSL_ERROR_NONE; | ||
275 | } | ||
276 | #endif | ||
277 | |||
278 | static BIO *bio_err = NULL; | ||
279 | static BIO *bio_stdout = NULL; | ||
280 | |||
281 | static char *cipher = NULL; | ||
282 | static int verbose = 0; | ||
283 | static int debug = 0; | ||
284 | #if 0 | ||
285 | /* Not used yet. */ | ||
286 | #ifdef FIONBIO | ||
287 | static int s_nbio = 0; | ||
288 | #endif | ||
289 | #endif | ||
290 | |||
291 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; | ||
292 | |||
293 | int doit_biopair(SSL *s_ssl, SSL *c_ssl, long bytes, clock_t *s_time, clock_t *c_time); | ||
294 | int doit(SSL *s_ssl, SSL *c_ssl, long bytes); | ||
295 | static int do_test_cipherlist(void); | ||
296 | |||
297 | static void | ||
298 | sv_usage(void) | ||
299 | { | ||
300 | fprintf(stderr, "usage: ssltest [args ...]\n"); | ||
301 | fprintf(stderr, "\n"); | ||
302 | #ifdef OPENSSL_FIPS | ||
303 | fprintf(stderr, "-F - run test in FIPS mode\n"); | ||
304 | #endif | ||
305 | fprintf(stderr, " -server_auth - check server certificate\n"); | ||
306 | fprintf(stderr, " -client_auth - do client authentication\n"); | ||
307 | fprintf(stderr, " -proxy - allow proxy certificates\n"); | ||
308 | fprintf(stderr, " -proxy_auth <val> - set proxy policy rights\n"); | ||
309 | fprintf(stderr, " -proxy_cond <val> - experssion to test proxy policy rights\n"); | ||
310 | fprintf(stderr, " -v - more output\n"); | ||
311 | fprintf(stderr, " -d - debug output\n"); | ||
312 | fprintf(stderr, " -reuse - use session-id reuse\n"); | ||
313 | fprintf(stderr, " -num <val> - number of connections to perform\n"); | ||
314 | fprintf(stderr, " -bytes <val> - number of bytes to swap between client/server\n"); | ||
315 | #ifndef OPENSSL_NO_DH | ||
316 | fprintf(stderr, " -dhe1024 - use 1024 bit key (safe prime) for DHE\n"); | ||
317 | fprintf(stderr, " -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n"); | ||
318 | fprintf(stderr, " -no_dhe - disable DHE\n"); | ||
319 | #endif | ||
320 | #ifndef OPENSSL_NO_ECDH | ||
321 | fprintf(stderr, " -no_ecdhe - disable ECDHE\n"); | ||
322 | #endif | ||
323 | #ifndef OPENSSL_NO_PSK | ||
324 | fprintf(stderr, " -psk arg - PSK in hex (without 0x)\n"); | ||
325 | #endif | ||
326 | #ifndef OPENSSL_NO_SRP | ||
327 | fprintf(stderr, " -srpuser user - SRP username to use\n"); | ||
328 | fprintf(stderr, " -srppass arg - password for 'user'\n"); | ||
329 | #endif | ||
330 | #ifndef OPENSSL_NO_SSL2 | ||
331 | fprintf(stderr, " -ssl2 - use SSLv2\n"); | ||
332 | #endif | ||
333 | #ifndef OPENSSL_NO_SSL3 | ||
334 | fprintf(stderr, " -ssl3 - use SSLv3\n"); | ||
335 | #endif | ||
336 | #ifndef OPENSSL_NO_TLS1 | ||
337 | fprintf(stderr, " -tls1 - use TLSv1\n"); | ||
338 | #endif | ||
339 | fprintf(stderr, " -CApath arg - PEM format directory of CA's\n"); | ||
340 | fprintf(stderr, " -CAfile arg - PEM format file of CA's\n"); | ||
341 | fprintf(stderr, " -cert arg - Server certificate file\n"); | ||
342 | fprintf(stderr, " -key arg - Server key file (default: same as -cert)\n"); | ||
343 | fprintf(stderr, " -c_cert arg - Client certificate file\n"); | ||
344 | fprintf(stderr, " -c_key arg - Client key file (default: same as -c_cert)\n"); | ||
345 | fprintf(stderr, " -cipher arg - The cipher list\n"); | ||
346 | fprintf(stderr, " -bio_pair - Use BIO pairs\n"); | ||
347 | fprintf(stderr, " -f - Test even cases that can't work\n"); | ||
348 | fprintf(stderr, " -time - measure processor time used by client and server\n"); | ||
349 | fprintf(stderr, " -zlib - use zlib compression\n"); | ||
350 | fprintf(stderr, " -rle - use rle compression\n"); | ||
351 | #ifndef OPENSSL_NO_ECDH | ||
352 | fprintf(stderr, " -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \ | ||
353 | " Use \"openssl ecparam -list_curves\" for all names\n" \ | ||
354 | " (default is sect163r2).\n"); | ||
355 | #endif | ||
356 | fprintf(stderr, " -test_cipherlist - verifies the order of the ssl cipher lists\n"); | ||
357 | } | ||
358 | |||
359 | static void | ||
360 | print_details(SSL *c_ssl, const char *prefix) | ||
361 | { | ||
362 | const SSL_CIPHER *ciph; | ||
363 | X509 *cert; | ||
364 | |||
365 | ciph = SSL_get_current_cipher(c_ssl); | ||
366 | BIO_printf(bio_stdout, "%s%s, cipher %s %s", | ||
367 | prefix, | ||
368 | SSL_get_version(c_ssl), | ||
369 | SSL_CIPHER_get_version(ciph), | ||
370 | SSL_CIPHER_get_name(ciph)); | ||
371 | cert = SSL_get_peer_certificate(c_ssl); | ||
372 | if (cert != NULL) { | ||
373 | EVP_PKEY *pkey = X509_get_pubkey(cert); | ||
374 | if (pkey != NULL) { | ||
375 | if (0) | ||
376 | ; | ||
377 | #ifndef OPENSSL_NO_RSA | ||
378 | else if (pkey->type == EVP_PKEY_RSA && | ||
379 | pkey->pkey.rsa != NULL && | ||
380 | pkey->pkey.rsa->n != NULL) { | ||
381 | BIO_printf(bio_stdout, ", %d bit RSA", | ||
382 | BN_num_bits(pkey->pkey.rsa->n)); | ||
383 | } | ||
384 | #endif | ||
385 | #ifndef OPENSSL_NO_DSA | ||
386 | else if (pkey->type == EVP_PKEY_DSA && | ||
387 | pkey->pkey.dsa != NULL && | ||
388 | pkey->pkey.dsa->p != NULL) { | ||
389 | BIO_printf(bio_stdout, ", %d bit DSA", | ||
390 | BN_num_bits(pkey->pkey.dsa->p)); | ||
391 | } | ||
392 | #endif | ||
393 | EVP_PKEY_free(pkey); | ||
394 | } | ||
395 | X509_free(cert); | ||
396 | } | ||
397 | /* The SSL API does not allow us to look at temporary RSA/DH keys, | ||
398 | * otherwise we should print their lengths too */ | ||
399 | BIO_printf(bio_stdout, "\n"); | ||
400 | } | ||
401 | |||
402 | static void | ||
403 | lock_dbg_cb(int mode, int type, const char *file, int line) | ||
404 | { | ||
405 | static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */ | ||
406 | const char *errstr = NULL; | ||
407 | int rw; | ||
408 | |||
409 | rw = mode & (CRYPTO_READ|CRYPTO_WRITE); | ||
410 | if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) { | ||
411 | errstr = "invalid mode"; | ||
412 | goto err; | ||
413 | } | ||
414 | |||
415 | if (type < 0 || type >= CRYPTO_NUM_LOCKS) { | ||
416 | errstr = "type out of bounds"; | ||
417 | goto err; | ||
418 | } | ||
419 | |||
420 | if (mode & CRYPTO_LOCK) { | ||
421 | if (modes[type]) { | ||
422 | errstr = "already locked"; | ||
423 | /* must not happen in a single-threaded program | ||
424 | * (would deadlock) */ | ||
425 | goto err; | ||
426 | } | ||
427 | |||
428 | modes[type] = rw; | ||
429 | } else if (mode & CRYPTO_UNLOCK) { | ||
430 | if (!modes[type]) { | ||
431 | errstr = "not locked"; | ||
432 | goto err; | ||
433 | } | ||
434 | |||
435 | if (modes[type] != rw) { | ||
436 | errstr = (rw == CRYPTO_READ) ? | ||
437 | "CRYPTO_r_unlock on write lock" : | ||
438 | "CRYPTO_w_unlock on read lock"; | ||
439 | } | ||
440 | |||
441 | modes[type] = 0; | ||
442 | } else { | ||
443 | errstr = "invalid mode"; | ||
444 | goto err; | ||
445 | } | ||
446 | |||
447 | err: | ||
448 | if (errstr) { | ||
449 | /* we cannot use bio_err here */ | ||
450 | fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n", | ||
451 | errstr, mode, type, file, line); | ||
452 | } | ||
453 | } | ||
454 | |||
455 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
456 | struct cb_info_st { void *input; | ||
457 | size_t len; | ||
458 | int ret; | ||
459 | }; | ||
460 | |||
461 | struct cb_info_st co1 = { "C", 1, 1 }; /* try to negotiate oqaque PRF input */ | ||
462 | struct cb_info_st co2 = { "C", 1, 2 }; /* insist on oqaque PRF input */ | ||
463 | struct cb_info_st so1 = { "S", 1, 1 }; /* try to negotiate oqaque PRF input */ | ||
464 | struct cb_info_st so2 = { "S", 1, 2 }; /* insist on oqaque PRF input */ | ||
465 | |||
466 | int | ||
467 | opaque_prf_input_cb(SSL *ssl, void *peerinput, size_t len, void *arg_) | ||
468 | { | ||
469 | struct cb_info_st *arg = arg_; | ||
470 | |||
471 | if (arg == NULL) | ||
472 | return 1; | ||
473 | |||
474 | if (!SSL_set_tlsext_opaque_prf_input(ssl, arg->input, arg->len)) | ||
475 | return 0; | ||
476 | return arg->ret; | ||
477 | } | ||
478 | #endif | ||
479 | |||
480 | int | ||
481 | main(int argc, char *argv[]) | ||
482 | { | ||
483 | char *CApath = NULL, *CAfile = NULL; | ||
484 | int badop = 0; | ||
485 | int bio_pair = 0; | ||
486 | int force = 0; | ||
487 | int tls1 = 0, ssl2 = 0, ssl3 = 0, ret = 1; | ||
488 | int client_auth = 0; | ||
489 | int server_auth = 0, i; | ||
490 | struct app_verify_arg app_verify_arg = | ||
491 | { APP_CALLBACK_STRING, 0, 0, NULL, NULL }; | ||
492 | char *server_cert = TEST_SERVER_CERT; | ||
493 | char *server_key = NULL; | ||
494 | char *client_cert = TEST_CLIENT_CERT; | ||
495 | char *client_key = NULL; | ||
496 | #ifndef OPENSSL_NO_ECDH | ||
497 | char *named_curve = NULL; | ||
498 | #endif | ||
499 | SSL_CTX *s_ctx = NULL; | ||
500 | SSL_CTX *c_ctx = NULL; | ||
501 | const SSL_METHOD *meth = NULL; | ||
502 | SSL *c_ssl, *s_ssl; | ||
503 | int number = 1, reuse = 0; | ||
504 | long bytes = 256L; | ||
505 | #ifndef OPENSSL_NO_DH | ||
506 | DH *dh; | ||
507 | int dhe1024 = 0, dhe1024dsa = 0; | ||
508 | #endif | ||
509 | #ifndef OPENSSL_NO_ECDH | ||
510 | EC_KEY *ecdh = NULL; | ||
511 | #endif | ||
512 | #ifndef OPENSSL_NO_SRP | ||
513 | /* client */ | ||
514 | SRP_CLIENT_ARG srp_client_arg = {NULL, NULL}; | ||
515 | /* server */ | ||
516 | SRP_SERVER_ARG srp_server_arg = {NULL, NULL}; | ||
517 | #endif | ||
518 | int no_dhe = 0; | ||
519 | int no_ecdhe = 0; | ||
520 | int no_psk = 0; | ||
521 | int print_time = 0; | ||
522 | clock_t s_time = 0, c_time = 0; | ||
523 | int comp = 0; | ||
524 | #ifndef OPENSSL_NO_COMP | ||
525 | COMP_METHOD *cm = NULL; | ||
526 | STACK_OF(SSL_COMP) *ssl_comp_methods = NULL; | ||
527 | #endif | ||
528 | int test_cipherlist = 0; | ||
529 | #ifdef OPENSSL_FIPS | ||
530 | int fips_mode = 0; | ||
531 | #endif | ||
532 | |||
533 | verbose = 0; | ||
534 | debug = 0; | ||
535 | cipher = 0; | ||
536 | |||
537 | bio_err = BIO_new_fp(stderr, BIO_NOCLOSE|BIO_FP_TEXT); | ||
538 | |||
539 | |||
540 | CRYPTO_set_locking_callback(lock_dbg_cb); | ||
541 | |||
542 | /* enable memory leak checking unless explicitly disabled */ | ||
543 | if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) { | ||
544 | CRYPTO_malloc_debug_init(); | ||
545 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | ||
546 | } else { | ||
547 | /* OPENSSL_DEBUG_MEMORY=off */ | ||
548 | CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); | ||
549 | } | ||
550 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | ||
551 | |||
552 | RAND_seed(rnd_seed, sizeof rnd_seed); | ||
553 | |||
554 | bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE|BIO_FP_TEXT); | ||
555 | |||
556 | argc--; | ||
557 | argv++; | ||
558 | |||
559 | while (argc >= 1) { | ||
560 | if (!strcmp(*argv, "-F")) { | ||
561 | #ifdef OPENSSL_FIPS | ||
562 | fips_mode = 1; | ||
563 | #else | ||
564 | fprintf(stderr, "not compiled with FIPS support, so exitting without running.\n"); | ||
565 | exit(0); | ||
566 | #endif | ||
567 | } else if (strcmp(*argv, "-server_auth") == 0) | ||
568 | server_auth = 1; | ||
569 | else if (strcmp(*argv, "-client_auth") == 0) | ||
570 | client_auth = 1; | ||
571 | else if (strcmp(*argv, "-proxy_auth") == 0) { | ||
572 | if (--argc < 1) | ||
573 | goto bad; | ||
574 | app_verify_arg.proxy_auth= *(++argv); | ||
575 | } else if (strcmp(*argv, "-proxy_cond") == 0) { | ||
576 | if (--argc < 1) | ||
577 | goto bad; | ||
578 | app_verify_arg.proxy_cond= *(++argv); | ||
579 | } else if (strcmp(*argv, "-v") == 0) | ||
580 | verbose = 1; | ||
581 | else if (strcmp(*argv, "-d") == 0) | ||
582 | debug = 1; | ||
583 | else if (strcmp(*argv, "-reuse") == 0) | ||
584 | reuse = 1; | ||
585 | else if (strcmp(*argv, "-dhe1024") == 0) { | ||
586 | #ifndef OPENSSL_NO_DH | ||
587 | dhe1024 = 1; | ||
588 | #else | ||
589 | fprintf(stderr, "ignoring -dhe1024, since I'm compiled without DH\n"); | ||
590 | #endif | ||
591 | } else if (strcmp(*argv, "-dhe1024dsa") == 0) { | ||
592 | #ifndef OPENSSL_NO_DH | ||
593 | dhe1024dsa = 1; | ||
594 | #else | ||
595 | fprintf(stderr, "ignoring -dhe1024, since I'm compiled without DH\n"); | ||
596 | #endif | ||
597 | } else if (strcmp(*argv, "-no_dhe") == 0) | ||
598 | no_dhe = 1; | ||
599 | else if (strcmp(*argv, "-no_ecdhe") == 0) | ||
600 | no_ecdhe = 1; | ||
601 | else if (strcmp(*argv, "-psk") == 0) { | ||
602 | if (--argc < 1) | ||
603 | goto bad; | ||
604 | psk_key=*(++argv); | ||
605 | #ifndef OPENSSL_NO_PSK | ||
606 | if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key)) { | ||
607 | BIO_printf(bio_err, "Not a hex number '%s'\n", *argv); | ||
608 | goto bad; | ||
609 | } | ||
610 | #else | ||
611 | no_psk = 1; | ||
612 | #endif | ||
613 | } | ||
614 | #ifndef OPENSSL_NO_SRP | ||
615 | else if (strcmp(*argv, "-srpuser") == 0) { | ||
616 | if (--argc < 1) | ||
617 | goto bad; | ||
618 | srp_server_arg.expected_user = srp_client_arg.srplogin= *(++argv); | ||
619 | tls1 = 1; | ||
620 | } else if (strcmp(*argv, "-srppass") == 0) { | ||
621 | if (--argc < 1) | ||
622 | goto bad; | ||
623 | srp_server_arg.pass = srp_client_arg.srppassin= *(++argv); | ||
624 | tls1 = 1; | ||
625 | } | ||
626 | #endif | ||
627 | else if (strcmp(*argv, "-ssl2") == 0) | ||
628 | ssl2 = 1; | ||
629 | else if (strcmp(*argv, "-tls1") == 0) | ||
630 | tls1 = 1; | ||
631 | else if (strcmp(*argv, "-ssl3") == 0) | ||
632 | ssl3 = 1; | ||
633 | else if (strncmp(*argv, "-num", 4) == 0) { | ||
634 | if (--argc < 1) | ||
635 | goto bad; | ||
636 | number = atoi(*(++argv)); | ||
637 | if (number == 0) | ||
638 | number = 1; | ||
639 | } else if (strcmp(*argv, "-bytes") == 0) { | ||
640 | if (--argc < 1) | ||
641 | goto bad; | ||
642 | bytes = atol(*(++argv)); | ||
643 | if (bytes == 0L) | ||
644 | bytes = 1L; | ||
645 | i = strlen(argv[0]); | ||
646 | if (argv[0][i - 1] == 'k') | ||
647 | bytes*=1024L; | ||
648 | if (argv[0][i - 1] == 'm') | ||
649 | bytes*=1024L*1024L; | ||
650 | } else if (strcmp(*argv, "-cert") == 0) { | ||
651 | if (--argc < 1) | ||
652 | goto bad; | ||
653 | server_cert= *(++argv); | ||
654 | } else if (strcmp(*argv, "-s_cert") == 0) { | ||
655 | if (--argc < 1) | ||
656 | goto bad; | ||
657 | server_cert= *(++argv); | ||
658 | } else if (strcmp(*argv, "-key") == 0) { | ||
659 | if (--argc < 1) | ||
660 | goto bad; | ||
661 | server_key= *(++argv); | ||
662 | } else if (strcmp(*argv, "-s_key") == 0) { | ||
663 | if (--argc < 1) | ||
664 | goto bad; | ||
665 | server_key= *(++argv); | ||
666 | } else if (strcmp(*argv, "-c_cert") == 0) { | ||
667 | if (--argc < 1) | ||
668 | goto bad; | ||
669 | client_cert= *(++argv); | ||
670 | } else if (strcmp(*argv, "-c_key") == 0) { | ||
671 | if (--argc < 1) | ||
672 | goto bad; | ||
673 | client_key= *(++argv); | ||
674 | } else if (strcmp(*argv, "-cipher") == 0) { | ||
675 | if (--argc < 1) | ||
676 | goto bad; | ||
677 | cipher= *(++argv); | ||
678 | } else if (strcmp(*argv, "-CApath") == 0) { | ||
679 | if (--argc < 1) | ||
680 | goto bad; | ||
681 | CApath= *(++argv); | ||
682 | } else if (strcmp(*argv, "-CAfile") == 0) { | ||
683 | if (--argc < 1) | ||
684 | goto bad; | ||
685 | CAfile= *(++argv); | ||
686 | } else if (strcmp(*argv, "-bio_pair") == 0) { | ||
687 | bio_pair = 1; | ||
688 | } else if (strcmp(*argv, "-f") == 0) { | ||
689 | force = 1; | ||
690 | } else if (strcmp(*argv, "-time") == 0) { | ||
691 | print_time = 1; | ||
692 | } else if (strcmp(*argv, "-zlib") == 0) { | ||
693 | comp = COMP_ZLIB; | ||
694 | } else if (strcmp(*argv, "-rle") == 0) { | ||
695 | comp = COMP_RLE; | ||
696 | } else if (strcmp(*argv, "-named_curve") == 0) { | ||
697 | if (--argc < 1) | ||
698 | goto bad; | ||
699 | #ifndef OPENSSL_NO_ECDH | ||
700 | named_curve = *(++argv); | ||
701 | #else | ||
702 | fprintf(stderr, "ignoring -named_curve, since I'm compiled without ECDH\n"); | ||
703 | ++argv; | ||
704 | #endif | ||
705 | } else if (strcmp(*argv, "-app_verify") == 0) { | ||
706 | app_verify_arg.app_verify = 1; | ||
707 | } else if (strcmp(*argv, "-proxy") == 0) { | ||
708 | app_verify_arg.allow_proxy_certs = 1; | ||
709 | } else if (strcmp(*argv, "-test_cipherlist") == 0) { | ||
710 | test_cipherlist = 1; | ||
711 | } else { | ||
712 | fprintf(stderr, "unknown option %s\n", *argv); | ||
713 | badop = 1; | ||
714 | break; | ||
715 | } | ||
716 | argc--; | ||
717 | argv++; | ||
718 | } | ||
719 | if (badop) { | ||
720 | bad: | ||
721 | sv_usage(); | ||
722 | goto end; | ||
723 | } | ||
724 | |||
725 | if (test_cipherlist == 1) { | ||
726 | /* ensure that the cipher list are correctly sorted and exit */ | ||
727 | if (do_test_cipherlist() == 0) | ||
728 | exit(1); | ||
729 | ret = 0; | ||
730 | goto end; | ||
731 | } | ||
732 | |||
733 | if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force) { | ||
734 | fprintf(stderr, | ||
735 | "This case cannot work. Use -f to perform " | ||
736 | "the test anyway (and\n-d to see what happens), " | ||
737 | "or add one of -ssl2, -ssl3, -tls1, -reuse\n" | ||
738 | "to avoid protocol mismatch.\n"); | ||
739 | exit(1); | ||
740 | } | ||
741 | |||
742 | #ifdef OPENSSL_FIPS | ||
743 | if (fips_mode) { | ||
744 | if (!FIPS_mode_set(1)) { | ||
745 | ERR_load_crypto_strings(); | ||
746 | ERR_print_errors(BIO_new_fp(stderr, BIO_NOCLOSE)); | ||
747 | exit(1); | ||
748 | } else | ||
749 | fprintf(stderr, "*** IN FIPS MODE ***\n"); | ||
750 | } | ||
751 | #endif | ||
752 | |||
753 | if (print_time) { | ||
754 | if (!bio_pair) { | ||
755 | fprintf(stderr, "Using BIO pair (-bio_pair)\n"); | ||
756 | bio_pair = 1; | ||
757 | } | ||
758 | if (number < 50 && !force) | ||
759 | fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n"); | ||
760 | } | ||
761 | |||
762 | /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */ | ||
763 | |||
764 | SSL_library_init(); | ||
765 | SSL_load_error_strings(); | ||
766 | |||
767 | #ifndef OPENSSL_NO_COMP | ||
768 | if (comp == COMP_ZLIB) | ||
769 | cm = COMP_zlib(); | ||
770 | if (comp == COMP_RLE) | ||
771 | cm = COMP_rle(); | ||
772 | if (cm != NULL) { | ||
773 | if (cm->type != NID_undef) { | ||
774 | if (SSL_COMP_add_compression_method(comp, cm) != 0) { | ||
775 | fprintf(stderr, | ||
776 | "Failed to add compression method\n"); | ||
777 | ERR_print_errors_fp(stderr); | ||
778 | } | ||
779 | } else { | ||
780 | fprintf(stderr, | ||
781 | "Warning: %s compression not supported\n", | ||
782 | (comp == COMP_RLE ? "rle" : | ||
783 | (comp == COMP_ZLIB ? "zlib" : | ||
784 | "unknown"))); | ||
785 | ERR_print_errors_fp(stderr); | ||
786 | } | ||
787 | } | ||
788 | ssl_comp_methods = SSL_COMP_get_compression_methods(); | ||
789 | fprintf(stderr, "Available compression methods:\n"); | ||
790 | { | ||
791 | int j, n = sk_SSL_COMP_num(ssl_comp_methods); | ||
792 | if (n == 0) | ||
793 | fprintf(stderr, " NONE\n"); | ||
794 | else | ||
795 | for (j = 0; j < n; j++) { | ||
796 | SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j); | ||
797 | fprintf(stderr, " %d: %s\n", c->id, c->name); | ||
798 | } | ||
799 | } | ||
800 | #endif | ||
801 | |||
802 | #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3) | ||
803 | if (ssl2) | ||
804 | meth = SSLv2_method(); | ||
805 | else if (tls1) | ||
806 | meth = TLSv1_method(); | ||
807 | else if (ssl3) | ||
808 | meth = SSLv3_method(); | ||
809 | else | ||
810 | meth = SSLv23_method(); | ||
811 | #else | ||
812 | #ifdef OPENSSL_NO_SSL2 | ||
813 | if (tls1) | ||
814 | meth = TLSv1_method(); | ||
815 | else if (ssl3) | ||
816 | meth = SSLv3_method(); | ||
817 | else | ||
818 | meth = SSLv23_method(); | ||
819 | #else | ||
820 | meth = SSLv2_method(); | ||
821 | #endif | ||
822 | #endif | ||
823 | |||
824 | c_ctx = SSL_CTX_new(meth); | ||
825 | s_ctx = SSL_CTX_new(meth); | ||
826 | if ((c_ctx == NULL) || (s_ctx == NULL)) { | ||
827 | ERR_print_errors(bio_err); | ||
828 | goto end; | ||
829 | } | ||
830 | |||
831 | if (cipher != NULL) { | ||
832 | SSL_CTX_set_cipher_list(c_ctx, cipher); | ||
833 | SSL_CTX_set_cipher_list(s_ctx, cipher); | ||
834 | } | ||
835 | |||
836 | #ifndef OPENSSL_NO_DH | ||
837 | if (!no_dhe) { | ||
838 | if (dhe1024dsa) { | ||
839 | /* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */ | ||
840 | SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE); | ||
841 | dh = get_dh1024dsa(); | ||
842 | } else if (dhe1024) | ||
843 | dh = get_dh1024(); | ||
844 | else | ||
845 | dh = get_dh512(); | ||
846 | SSL_CTX_set_tmp_dh(s_ctx, dh); | ||
847 | DH_free(dh); | ||
848 | } | ||
849 | #else | ||
850 | (void)no_dhe; | ||
851 | #endif | ||
852 | |||
853 | #ifndef OPENSSL_NO_ECDH | ||
854 | if (!no_ecdhe) { | ||
855 | int nid; | ||
856 | |||
857 | if (named_curve != NULL) { | ||
858 | nid = OBJ_sn2nid(named_curve); | ||
859 | if (nid == 0) { | ||
860 | BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve); | ||
861 | goto end; | ||
862 | } | ||
863 | } else | ||
864 | #ifdef OPENSSL_NO_EC2M | ||
865 | nid = NID_X9_62_prime256v1; | ||
866 | #else | ||
867 | nid = NID_sect163r2; | ||
868 | #endif | ||
869 | |||
870 | ecdh = EC_KEY_new_by_curve_name(nid); | ||
871 | if (ecdh == NULL) { | ||
872 | BIO_printf(bio_err, "unable to create curve\n"); | ||
873 | goto end; | ||
874 | } | ||
875 | |||
876 | SSL_CTX_set_tmp_ecdh(s_ctx, ecdh); | ||
877 | SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE); | ||
878 | EC_KEY_free(ecdh); | ||
879 | } | ||
880 | #else | ||
881 | (void)no_ecdhe; | ||
882 | #endif | ||
883 | |||
884 | #ifndef OPENSSL_NO_RSA | ||
885 | SSL_CTX_set_tmp_rsa_callback(s_ctx, tmp_rsa_cb); | ||
886 | #endif | ||
887 | |||
888 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
889 | SSL_CTX_set_tlsext_opaque_prf_input_callback(c_ctx, opaque_prf_input_cb); | ||
890 | SSL_CTX_set_tlsext_opaque_prf_input_callback(s_ctx, opaque_prf_input_cb); | ||
891 | SSL_CTX_set_tlsext_opaque_prf_input_callback_arg(c_ctx, &co1); /* or &co2 or NULL */ | ||
892 | SSL_CTX_set_tlsext_opaque_prf_input_callback_arg(s_ctx, &so1); /* or &so2 or NULL */ | ||
893 | #endif | ||
894 | |||
895 | if (!SSL_CTX_use_certificate_file(s_ctx, server_cert, SSL_FILETYPE_PEM)) { | ||
896 | ERR_print_errors(bio_err); | ||
897 | } else if (!SSL_CTX_use_PrivateKey_file(s_ctx, | ||
898 | (server_key ? server_key : server_cert), SSL_FILETYPE_PEM)) { | ||
899 | ERR_print_errors(bio_err); | ||
900 | goto end; | ||
901 | } | ||
902 | |||
903 | if (client_auth) { | ||
904 | SSL_CTX_use_certificate_file(c_ctx, client_cert, | ||
905 | SSL_FILETYPE_PEM); | ||
906 | SSL_CTX_use_PrivateKey_file(c_ctx, | ||
907 | (client_key ? client_key : client_cert), | ||
908 | SSL_FILETYPE_PEM); | ||
909 | } | ||
910 | |||
911 | if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) || | ||
912 | (!SSL_CTX_set_default_verify_paths(s_ctx)) || | ||
913 | (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) || | ||
914 | (!SSL_CTX_set_default_verify_paths(c_ctx))) { | ||
915 | /* fprintf(stderr,"SSL_load_verify_locations\n"); */ | ||
916 | ERR_print_errors(bio_err); | ||
917 | /* goto end; */ | ||
918 | } | ||
919 | |||
920 | if (client_auth) { | ||
921 | BIO_printf(bio_err, "client authentication\n"); | ||
922 | SSL_CTX_set_verify(s_ctx, | ||
923 | SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, | ||
924 | verify_callback); | ||
925 | SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg); | ||
926 | } | ||
927 | if (server_auth) { | ||
928 | BIO_printf(bio_err, "server authentication\n"); | ||
929 | SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, | ||
930 | verify_callback); | ||
931 | SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg); | ||
932 | } | ||
933 | |||
934 | { | ||
935 | int session_id_context = 0; | ||
936 | SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context); | ||
937 | } | ||
938 | |||
939 | /* Use PSK only if PSK key is given */ | ||
940 | if (psk_key != NULL) { | ||
941 | /* no_psk is used to avoid putting psk command to openssl tool */ | ||
942 | if (no_psk) { | ||
943 | /* if PSK is not compiled in and psk key is | ||
944 | * given, do nothing and exit successfully */ | ||
945 | ret = 0; | ||
946 | goto end; | ||
947 | } | ||
948 | #ifndef OPENSSL_NO_PSK | ||
949 | SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback); | ||
950 | SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback); | ||
951 | if (debug) | ||
952 | BIO_printf(bio_err, "setting PSK identity hint to s_ctx\n"); | ||
953 | if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint")) { | ||
954 | BIO_printf(bio_err, "error setting PSK identity hint to s_ctx\n"); | ||
955 | ERR_print_errors(bio_err); | ||
956 | goto end; | ||
957 | } | ||
958 | #endif | ||
959 | } | ||
960 | #ifndef OPENSSL_NO_SRP | ||
961 | if (srp_client_arg.srplogin) { | ||
962 | if (!SSL_CTX_set_srp_username(c_ctx, srp_client_arg.srplogin)) { | ||
963 | BIO_printf(bio_err, "Unable to set SRP username\n"); | ||
964 | goto end; | ||
965 | } | ||
966 | SSL_CTX_set_srp_cb_arg(c_ctx, &srp_client_arg); | ||
967 | SSL_CTX_set_srp_client_pwd_callback(c_ctx, ssl_give_srp_client_pwd_cb); | ||
968 | /*SSL_CTX_set_srp_strength(c_ctx, srp_client_arg.strength);*/ | ||
969 | } | ||
970 | |||
971 | if (srp_server_arg.expected_user != NULL) { | ||
972 | SSL_CTX_set_verify(s_ctx, SSL_VERIFY_NONE, verify_callback); | ||
973 | SSL_CTX_set_srp_cb_arg(s_ctx, &srp_server_arg); | ||
974 | SSL_CTX_set_srp_username_callback(s_ctx, ssl_srp_server_param_cb); | ||
975 | } | ||
976 | #endif | ||
977 | |||
978 | c_ssl = SSL_new(c_ctx); | ||
979 | s_ssl = SSL_new(s_ctx); | ||
980 | |||
981 | #ifndef OPENSSL_NO_KRB5 | ||
982 | if (c_ssl && c_ssl->kssl_ctx) { | ||
983 | char localhost[MAXHOSTNAMELEN + 2]; | ||
984 | |||
985 | if (gethostname(localhost, sizeof localhost - 1) == 0) { | ||
986 | localhost[sizeof localhost - 1] = '\0'; | ||
987 | if (strlen(localhost) == sizeof localhost - 1) { | ||
988 | BIO_printf(bio_err, "localhost name too long\n"); | ||
989 | goto end; | ||
990 | } | ||
991 | kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER, | ||
992 | localhost); | ||
993 | } | ||
994 | } | ||
995 | #endif /* OPENSSL_NO_KRB5 */ | ||
996 | |||
997 | for (i = 0; i < number; i++) { | ||
998 | if (!reuse) | ||
999 | SSL_set_session(c_ssl, NULL); | ||
1000 | if (bio_pair) | ||
1001 | ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time); | ||
1002 | else | ||
1003 | ret = doit(s_ssl, c_ssl, bytes); | ||
1004 | } | ||
1005 | |||
1006 | if (!verbose) { | ||
1007 | print_details(c_ssl, ""); | ||
1008 | } | ||
1009 | if ((number > 1) || (bytes > 1L)) | ||
1010 | BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n", number, bytes); | ||
1011 | if (print_time) { | ||
1012 | #ifdef CLOCKS_PER_SEC | ||
1013 | /* "To determine the time in seconds, the value returned | ||
1014 | * by the clock function should be divided by the value | ||
1015 | * of the macro CLOCKS_PER_SEC." | ||
1016 | * -- ISO/IEC 9899 */ | ||
1017 | BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n" | ||
1018 | "Approximate total client time: %6.2f s\n", | ||
1019 | (double)s_time/CLOCKS_PER_SEC, | ||
1020 | (double)c_time/CLOCKS_PER_SEC); | ||
1021 | #else | ||
1022 | /* "`CLOCKS_PER_SEC' undeclared (first use this function)" | ||
1023 | * -- cc on NeXTstep/OpenStep */ | ||
1024 | BIO_printf(bio_stdout, | ||
1025 | "Approximate total server time: %6.2f units\n" | ||
1026 | "Approximate total client time: %6.2f units\n", | ||
1027 | (double)s_time, | ||
1028 | (double)c_time); | ||
1029 | #endif | ||
1030 | } | ||
1031 | |||
1032 | SSL_free(s_ssl); | ||
1033 | SSL_free(c_ssl); | ||
1034 | |||
1035 | end: | ||
1036 | if (s_ctx != NULL) | ||
1037 | SSL_CTX_free(s_ctx); | ||
1038 | if (c_ctx != NULL) | ||
1039 | SSL_CTX_free(c_ctx); | ||
1040 | |||
1041 | if (bio_stdout != NULL) | ||
1042 | BIO_free(bio_stdout); | ||
1043 | |||
1044 | #ifndef OPENSSL_NO_RSA | ||
1045 | free_tmp_rsa(); | ||
1046 | #endif | ||
1047 | #ifndef OPENSSL_NO_ENGINE | ||
1048 | ENGINE_cleanup(); | ||
1049 | #endif | ||
1050 | CRYPTO_cleanup_all_ex_data(); | ||
1051 | ERR_free_strings(); | ||
1052 | ERR_remove_thread_state(NULL); | ||
1053 | EVP_cleanup(); | ||
1054 | CRYPTO_mem_leaks(bio_err); | ||
1055 | if (bio_err != NULL) | ||
1056 | BIO_free(bio_err); | ||
1057 | exit(ret); | ||
1058 | return ret; | ||
1059 | } | ||
1060 | |||
1061 | int | ||
1062 | doit_biopair(SSL *s_ssl, SSL *c_ssl, long count, clock_t *s_time, | ||
1063 | clock_t *c_time) | ||
1064 | { | ||
1065 | long cw_num = count, cr_num = count, sw_num = count, sr_num = count; | ||
1066 | BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL; | ||
1067 | BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL; | ||
1068 | int ret = 1; | ||
1069 | |||
1070 | size_t bufsiz = 256; /* small buffer for testing */ | ||
1071 | |||
1072 | if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz)) | ||
1073 | goto err; | ||
1074 | if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz)) | ||
1075 | goto err; | ||
1076 | |||
1077 | s_ssl_bio = BIO_new(BIO_f_ssl()); | ||
1078 | if (!s_ssl_bio) | ||
1079 | goto err; | ||
1080 | |||
1081 | c_ssl_bio = BIO_new(BIO_f_ssl()); | ||
1082 | if (!c_ssl_bio) | ||
1083 | goto err; | ||
1084 | |||
1085 | SSL_set_connect_state(c_ssl); | ||
1086 | SSL_set_bio(c_ssl, client, client); | ||
1087 | (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE); | ||
1088 | |||
1089 | SSL_set_accept_state(s_ssl); | ||
1090 | SSL_set_bio(s_ssl, server, server); | ||
1091 | (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE); | ||
1092 | |||
1093 | do { | ||
1094 | /* c_ssl_bio: SSL filter BIO | ||
1095 | * | ||
1096 | * client: pseudo-I/O for SSL library | ||
1097 | * | ||
1098 | * client_io: client's SSL communication; usually to be | ||
1099 | * relayed over some I/O facility, but in this | ||
1100 | * test program, we're the server, too: | ||
1101 | * | ||
1102 | * server_io: server's SSL communication | ||
1103 | * | ||
1104 | * server: pseudo-I/O for SSL library | ||
1105 | * | ||
1106 | * s_ssl_bio: SSL filter BIO | ||
1107 | * | ||
1108 | * The client and the server each employ a "BIO pair": | ||
1109 | * client + client_io, server + server_io. | ||
1110 | * BIO pairs are symmetric. A BIO pair behaves similar | ||
1111 | * to a non-blocking socketpair (but both endpoints must | ||
1112 | * be handled by the same thread). | ||
1113 | * [Here we could connect client and server to the ends | ||
1114 | * of a single BIO pair, but then this code would be less | ||
1115 | * suitable as an example for BIO pairs in general.] | ||
1116 | * | ||
1117 | * Useful functions for querying the state of BIO pair endpoints: | ||
1118 | * | ||
1119 | * BIO_ctrl_pending(bio) number of bytes we can read now | ||
1120 | * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil | ||
1121 | * other side's read attempt | ||
1122 | * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now | ||
1123 | * | ||
1124 | * ..._read_request is never more than ..._write_guarantee; | ||
1125 | * it depends on the application which one you should use. | ||
1126 | */ | ||
1127 | |||
1128 | /* We have non-blocking behaviour throughout this test program, but | ||
1129 | * can be sure that there is *some* progress in each iteration; so | ||
1130 | * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE | ||
1131 | * -- we just try everything in each iteration | ||
1132 | */ | ||
1133 | |||
1134 | { | ||
1135 | /* CLIENT */ | ||
1136 | |||
1137 | char cbuf[1024*8]; | ||
1138 | int i, r; | ||
1139 | clock_t c_clock = clock(); | ||
1140 | |||
1141 | memset(cbuf, 0, sizeof(cbuf)); | ||
1142 | |||
1143 | if (debug) | ||
1144 | if (SSL_in_init(c_ssl)) | ||
1145 | printf("client waiting in SSL_connect - %s\n", | ||
1146 | SSL_state_string_long(c_ssl)); | ||
1147 | |||
1148 | if (cw_num > 0) { | ||
1149 | /* Write to server. */ | ||
1150 | |||
1151 | if (cw_num > (long)sizeof cbuf) | ||
1152 | i = sizeof cbuf; | ||
1153 | else | ||
1154 | i = (int)cw_num; | ||
1155 | r = BIO_write(c_ssl_bio, cbuf, i); | ||
1156 | if (r < 0) { | ||
1157 | if (!BIO_should_retry(c_ssl_bio)) { | ||
1158 | fprintf(stderr, "ERROR in CLIENT\n"); | ||
1159 | goto err; | ||
1160 | } | ||
1161 | /* BIO_should_retry(...) can just be ignored here. | ||
1162 | * The library expects us to call BIO_write with | ||
1163 | * the same arguments again, and that's what we will | ||
1164 | * do in the next iteration. */ | ||
1165 | } else if (r == 0) { | ||
1166 | fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); | ||
1167 | goto err; | ||
1168 | } else { | ||
1169 | if (debug) | ||
1170 | printf("client wrote %d\n", r); | ||
1171 | cw_num -= r; | ||
1172 | |||
1173 | } | ||
1174 | } | ||
1175 | |||
1176 | if (cr_num > 0) { | ||
1177 | /* Read from server. */ | ||
1178 | |||
1179 | r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf)); | ||
1180 | if (r < 0) { | ||
1181 | if (!BIO_should_retry(c_ssl_bio)) { | ||
1182 | fprintf(stderr, "ERROR in CLIENT\n"); | ||
1183 | goto err; | ||
1184 | } | ||
1185 | /* Again, "BIO_should_retry" can be ignored. */ | ||
1186 | } else if (r == 0) { | ||
1187 | fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); | ||
1188 | goto err; | ||
1189 | } else { | ||
1190 | if (debug) | ||
1191 | printf("client read %d\n", r); | ||
1192 | cr_num -= r; | ||
1193 | } | ||
1194 | } | ||
1195 | |||
1196 | /* c_time and s_time increments will typically be very small | ||
1197 | * (depending on machine speed and clock tick intervals), | ||
1198 | * but sampling over a large number of connections should | ||
1199 | * result in fairly accurate figures. We cannot guarantee | ||
1200 | * a lot, however -- if each connection lasts for exactly | ||
1201 | * one clock tick, it will be counted only for the client | ||
1202 | * or only for the server or even not at all. | ||
1203 | */ | ||
1204 | *c_time += (clock() - c_clock); | ||
1205 | } | ||
1206 | |||
1207 | { | ||
1208 | /* SERVER */ | ||
1209 | |||
1210 | char sbuf[1024*8]; | ||
1211 | int i, r; | ||
1212 | clock_t s_clock = clock(); | ||
1213 | |||
1214 | memset(sbuf, 0, sizeof(sbuf)); | ||
1215 | |||
1216 | if (debug) | ||
1217 | if (SSL_in_init(s_ssl)) | ||
1218 | printf("server waiting in SSL_accept - %s\n", | ||
1219 | SSL_state_string_long(s_ssl)); | ||
1220 | |||
1221 | if (sw_num > 0) { | ||
1222 | /* Write to client. */ | ||
1223 | |||
1224 | if (sw_num > (long)sizeof sbuf) | ||
1225 | i = sizeof sbuf; | ||
1226 | else | ||
1227 | i = (int)sw_num; | ||
1228 | r = BIO_write(s_ssl_bio, sbuf, i); | ||
1229 | if (r < 0) { | ||
1230 | if (!BIO_should_retry(s_ssl_bio)) { | ||
1231 | fprintf(stderr, "ERROR in SERVER\n"); | ||
1232 | goto err; | ||
1233 | } | ||
1234 | /* Ignore "BIO_should_retry". */ | ||
1235 | } else if (r == 0) { | ||
1236 | fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); | ||
1237 | goto err; | ||
1238 | } else { | ||
1239 | if (debug) | ||
1240 | printf("server wrote %d\n", r); | ||
1241 | sw_num -= r; | ||
1242 | |||
1243 | } | ||
1244 | } | ||
1245 | |||
1246 | if (sr_num > 0) { | ||
1247 | /* Read from client. */ | ||
1248 | |||
1249 | r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf)); | ||
1250 | if (r < 0) { | ||
1251 | if (!BIO_should_retry(s_ssl_bio)) { | ||
1252 | fprintf(stderr, "ERROR in SERVER\n"); | ||
1253 | goto err; | ||
1254 | } | ||
1255 | /* blah, blah */ | ||
1256 | } else if (r == 0) { | ||
1257 | fprintf(stderr, "SSL SERVER STARTUP FAILED\n"); | ||
1258 | goto err; | ||
1259 | } else { | ||
1260 | if (debug) | ||
1261 | printf("server read %d\n", r); | ||
1262 | sr_num -= r; | ||
1263 | } | ||
1264 | } | ||
1265 | |||
1266 | *s_time += (clock() - s_clock); | ||
1267 | } | ||
1268 | |||
1269 | { | ||
1270 | /* "I/O" BETWEEN CLIENT AND SERVER. */ | ||
1271 | |||
1272 | size_t r1, r2; | ||
1273 | BIO *io1 = server_io, *io2 = client_io; | ||
1274 | /* we use the non-copying interface for io1 | ||
1275 | * and the standard BIO_write/BIO_read interface for io2 | ||
1276 | */ | ||
1277 | |||
1278 | static int prev_progress = 1; | ||
1279 | int progress = 0; | ||
1280 | |||
1281 | /* io1 to io2 */ | ||
1282 | do | ||
1283 | { | ||
1284 | size_t num; | ||
1285 | int r; | ||
1286 | |||
1287 | r1 = BIO_ctrl_pending(io1); | ||
1288 | r2 = BIO_ctrl_get_write_guarantee(io2); | ||
1289 | |||
1290 | num = r1; | ||
1291 | if (r2 < num) | ||
1292 | num = r2; | ||
1293 | if (num) { | ||
1294 | char *dataptr; | ||
1295 | |||
1296 | if (INT_MAX < num) /* yeah, right */ | ||
1297 | num = INT_MAX; | ||
1298 | |||
1299 | r = BIO_nread(io1, &dataptr, (int)num); | ||
1300 | assert(r > 0); | ||
1301 | assert(r <= (int)num); | ||
1302 | /* possibly r < num (non-contiguous data) */ | ||
1303 | num = r; | ||
1304 | r = BIO_write(io2, dataptr, (int)num); | ||
1305 | if (r != (int)num) /* can't happen */ | ||
1306 | { | ||
1307 | fprintf(stderr, "ERROR: BIO_write could not write " | ||
1308 | "BIO_ctrl_get_write_guarantee() bytes"); | ||
1309 | goto err; | ||
1310 | } | ||
1311 | progress = 1; | ||
1312 | |||
1313 | if (debug) | ||
1314 | printf((io1 == client_io) ? | ||
1315 | "C->S relaying: %d bytes\n" : | ||
1316 | "S->C relaying: %d bytes\n", | ||
1317 | (int)num); | ||
1318 | } | ||
1319 | } while (r1 && r2); | ||
1320 | |||
1321 | /* io2 to io1 */ | ||
1322 | { | ||
1323 | size_t num; | ||
1324 | int r; | ||
1325 | |||
1326 | r1 = BIO_ctrl_pending(io2); | ||
1327 | r2 = BIO_ctrl_get_read_request(io1); | ||
1328 | /* here we could use ..._get_write_guarantee instead of | ||
1329 | * ..._get_read_request, but by using the latter | ||
1330 | * we test restartability of the SSL implementation | ||
1331 | * more thoroughly */ | ||
1332 | num = r1; | ||
1333 | if (r2 < num) | ||
1334 | num = r2; | ||
1335 | if (num) { | ||
1336 | char *dataptr; | ||
1337 | |||
1338 | if (INT_MAX < num) | ||
1339 | num = INT_MAX; | ||
1340 | |||
1341 | if (num > 1) | ||
1342 | --num; /* test restartability even more thoroughly */ | ||
1343 | |||
1344 | r = BIO_nwrite0(io1, &dataptr); | ||
1345 | assert(r > 0); | ||
1346 | if (r < (int)num) | ||
1347 | num = r; | ||
1348 | r = BIO_read(io2, dataptr, (int)num); | ||
1349 | if (r != (int)num) /* can't happen */ | ||
1350 | { | ||
1351 | fprintf(stderr, "ERROR: BIO_read could not read " | ||
1352 | "BIO_ctrl_pending() bytes"); | ||
1353 | goto err; | ||
1354 | } | ||
1355 | progress = 1; | ||
1356 | r = BIO_nwrite(io1, &dataptr, (int)num); | ||
1357 | if (r != (int)num) /* can't happen */ | ||
1358 | { | ||
1359 | fprintf(stderr, "ERROR: BIO_nwrite() did not accept " | ||
1360 | "BIO_nwrite0() bytes"); | ||
1361 | goto err; | ||
1362 | } | ||
1363 | |||
1364 | if (debug) | ||
1365 | printf((io2 == client_io) ? | ||
1366 | "C->S relaying: %d bytes\n" : | ||
1367 | "S->C relaying: %d bytes\n", | ||
1368 | (int)num); | ||
1369 | } | ||
1370 | } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */ | ||
1371 | |||
1372 | if (!progress && !prev_progress) | ||
1373 | if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0) { | ||
1374 | fprintf(stderr, "ERROR: got stuck\n"); | ||
1375 | if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0) { | ||
1376 | fprintf(stderr, "This can happen for SSL2 because " | ||
1377 | "CLIENT-FINISHED and SERVER-VERIFY are written \n" | ||
1378 | "concurrently ..."); | ||
1379 | if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0 | ||
1380 | && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0) { | ||
1381 | fprintf(stderr, " ok.\n"); | ||
1382 | goto end; | ||
1383 | } | ||
1384 | } | ||
1385 | fprintf(stderr, " ERROR.\n"); | ||
1386 | goto err; | ||
1387 | } | ||
1388 | prev_progress = progress; | ||
1389 | } | ||
1390 | } while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0); | ||
1391 | |||
1392 | if (verbose) | ||
1393 | print_details(c_ssl, "DONE via BIO pair: "); | ||
1394 | end: | ||
1395 | ret = 0; | ||
1396 | |||
1397 | err: | ||
1398 | ERR_print_errors(bio_err); | ||
1399 | |||
1400 | if (server) | ||
1401 | BIO_free(server); | ||
1402 | if (server_io) | ||
1403 | BIO_free(server_io); | ||
1404 | if (client) | ||
1405 | BIO_free(client); | ||
1406 | if (client_io) | ||
1407 | BIO_free(client_io); | ||
1408 | if (s_ssl_bio) | ||
1409 | BIO_free(s_ssl_bio); | ||
1410 | if (c_ssl_bio) | ||
1411 | BIO_free(c_ssl_bio); | ||
1412 | |||
1413 | return ret; | ||
1414 | } | ||
1415 | |||
1416 | |||
1417 | #define W_READ 1 | ||
1418 | #define W_WRITE 2 | ||
1419 | #define C_DONE 1 | ||
1420 | #define S_DONE 2 | ||
1421 | |||
1422 | int | ||
1423 | doit(SSL *s_ssl, SSL *c_ssl, long count) | ||
1424 | { | ||
1425 | char cbuf[1024*8], sbuf[1024*8]; | ||
1426 | long cw_num = count, cr_num = count; | ||
1427 | long sw_num = count, sr_num = count; | ||
1428 | int ret = 1; | ||
1429 | BIO *c_to_s = NULL; | ||
1430 | BIO *s_to_c = NULL; | ||
1431 | BIO *c_bio = NULL; | ||
1432 | BIO *s_bio = NULL; | ||
1433 | int c_r, c_w, s_r, s_w; | ||
1434 | int i, j; | ||
1435 | int done = 0; | ||
1436 | int c_write, s_write; | ||
1437 | int do_server = 0, do_client = 0; | ||
1438 | |||
1439 | memset(cbuf, 0, sizeof(cbuf)); | ||
1440 | memset(sbuf, 0, sizeof(sbuf)); | ||
1441 | |||
1442 | c_to_s = BIO_new(BIO_s_mem()); | ||
1443 | s_to_c = BIO_new(BIO_s_mem()); | ||
1444 | if ((s_to_c == NULL) || (c_to_s == NULL)) { | ||
1445 | ERR_print_errors(bio_err); | ||
1446 | goto err; | ||
1447 | } | ||
1448 | |||
1449 | c_bio = BIO_new(BIO_f_ssl()); | ||
1450 | s_bio = BIO_new(BIO_f_ssl()); | ||
1451 | if ((c_bio == NULL) || (s_bio == NULL)) { | ||
1452 | ERR_print_errors(bio_err); | ||
1453 | goto err; | ||
1454 | } | ||
1455 | |||
1456 | SSL_set_connect_state(c_ssl); | ||
1457 | SSL_set_bio(c_ssl, s_to_c, c_to_s); | ||
1458 | BIO_set_ssl(c_bio, c_ssl, BIO_NOCLOSE); | ||
1459 | |||
1460 | SSL_set_accept_state(s_ssl); | ||
1461 | SSL_set_bio(s_ssl, c_to_s, s_to_c); | ||
1462 | BIO_set_ssl(s_bio, s_ssl, BIO_NOCLOSE); | ||
1463 | |||
1464 | c_r = 0; | ||
1465 | s_r = 1; | ||
1466 | c_w = 1; | ||
1467 | s_w = 0; | ||
1468 | c_write = 1, s_write = 0; | ||
1469 | |||
1470 | /* We can always do writes */ | ||
1471 | for (;;) { | ||
1472 | do_server = 0; | ||
1473 | do_client = 0; | ||
1474 | |||
1475 | i = (int)BIO_pending(s_bio); | ||
1476 | if ((i && s_r) || s_w) | ||
1477 | do_server = 1; | ||
1478 | |||
1479 | i = (int)BIO_pending(c_bio); | ||
1480 | if ((i && c_r) || c_w) | ||
1481 | do_client = 1; | ||
1482 | |||
1483 | if (do_server && debug) { | ||
1484 | if (SSL_in_init(s_ssl)) | ||
1485 | printf("server waiting in SSL_accept - %s\n", | ||
1486 | SSL_state_string_long(s_ssl)); | ||
1487 | /* else if (s_write) | ||
1488 | printf("server:SSL_write()\n"); | ||
1489 | else | ||
1490 | printf("server:SSL_read()\n"); */ | ||
1491 | } | ||
1492 | |||
1493 | if (do_client && debug) { | ||
1494 | if (SSL_in_init(c_ssl)) | ||
1495 | printf("client waiting in SSL_connect - %s\n", | ||
1496 | SSL_state_string_long(c_ssl)); | ||
1497 | /* else if (c_write) | ||
1498 | printf("client:SSL_write()\n"); | ||
1499 | else | ||
1500 | printf("client:SSL_read()\n"); */ | ||
1501 | } | ||
1502 | |||
1503 | if (!do_client && !do_server) { | ||
1504 | fprintf(stdout, "ERROR IN STARTUP\n"); | ||
1505 | ERR_print_errors(bio_err); | ||
1506 | break; | ||
1507 | } | ||
1508 | if (do_client && !(done & C_DONE)) { | ||
1509 | if (c_write) { | ||
1510 | j = (cw_num > (long)sizeof(cbuf)) ? | ||
1511 | (int)sizeof(cbuf) : (int)cw_num; | ||
1512 | i = BIO_write(c_bio, cbuf, j); | ||
1513 | if (i < 0) { | ||
1514 | c_r = 0; | ||
1515 | c_w = 0; | ||
1516 | if (BIO_should_retry(c_bio)) { | ||
1517 | if (BIO_should_read(c_bio)) | ||
1518 | c_r = 1; | ||
1519 | if (BIO_should_write(c_bio)) | ||
1520 | c_w = 1; | ||
1521 | } else { | ||
1522 | fprintf(stderr, "ERROR in CLIENT\n"); | ||
1523 | ERR_print_errors(bio_err); | ||
1524 | goto err; | ||
1525 | } | ||
1526 | } else if (i == 0) { | ||
1527 | fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); | ||
1528 | goto err; | ||
1529 | } else { | ||
1530 | if (debug) | ||
1531 | printf("client wrote %d\n", i); | ||
1532 | /* ok */ | ||
1533 | s_r = 1; | ||
1534 | c_write = 0; | ||
1535 | cw_num -= i; | ||
1536 | } | ||
1537 | } else { | ||
1538 | i = BIO_read(c_bio, cbuf, sizeof(cbuf)); | ||
1539 | if (i < 0) { | ||
1540 | c_r = 0; | ||
1541 | c_w = 0; | ||
1542 | if (BIO_should_retry(c_bio)) { | ||
1543 | if (BIO_should_read(c_bio)) | ||
1544 | c_r = 1; | ||
1545 | if (BIO_should_write(c_bio)) | ||
1546 | c_w = 1; | ||
1547 | } else { | ||
1548 | fprintf(stderr, "ERROR in CLIENT\n"); | ||
1549 | ERR_print_errors(bio_err); | ||
1550 | goto err; | ||
1551 | } | ||
1552 | } else if (i == 0) { | ||
1553 | fprintf(stderr, "SSL CLIENT STARTUP FAILED\n"); | ||
1554 | goto err; | ||
1555 | } else { | ||
1556 | if (debug) | ||
1557 | printf("client read %d\n", i); | ||
1558 | cr_num -= i; | ||
1559 | if (sw_num > 0) { | ||
1560 | s_write = 1; | ||
1561 | s_w = 1; | ||
1562 | } | ||
1563 | if (cr_num <= 0) { | ||
1564 | s_write = 1; | ||
1565 | s_w = 1; | ||
1566 | done = S_DONE|C_DONE; | ||
1567 | } | ||
1568 | } | ||
1569 | } | ||
1570 | } | ||
1571 | |||
1572 | if (do_server && !(done & S_DONE)) { | ||
1573 | if (!s_write) { | ||
1574 | i = BIO_read(s_bio, sbuf, sizeof(cbuf)); | ||
1575 | if (i < 0) { | ||
1576 | s_r = 0; | ||
1577 | s_w = 0; | ||
1578 | if (BIO_should_retry(s_bio)) { | ||
1579 | if (BIO_should_read(s_bio)) | ||
1580 | s_r = 1; | ||
1581 | if (BIO_should_write(s_bio)) | ||
1582 | s_w = 1; | ||
1583 | } else { | ||
1584 | fprintf(stderr, "ERROR in SERVER\n"); | ||
1585 | ERR_print_errors(bio_err); | ||
1586 | goto err; | ||
1587 | } | ||
1588 | } else if (i == 0) { | ||
1589 | ERR_print_errors(bio_err); | ||
1590 | fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_read\n"); | ||
1591 | goto err; | ||
1592 | } else { | ||
1593 | if (debug) | ||
1594 | printf("server read %d\n", i); | ||
1595 | sr_num -= i; | ||
1596 | if (cw_num > 0) { | ||
1597 | c_write = 1; | ||
1598 | c_w = 1; | ||
1599 | } | ||
1600 | if (sr_num <= 0) { | ||
1601 | s_write = 1; | ||
1602 | s_w = 1; | ||
1603 | c_write = 0; | ||
1604 | } | ||
1605 | } | ||
1606 | } else { | ||
1607 | j = (sw_num > (long)sizeof(sbuf)) ? | ||
1608 | (int)sizeof(sbuf) : (int)sw_num; | ||
1609 | i = BIO_write(s_bio, sbuf, j); | ||
1610 | if (i < 0) { | ||
1611 | s_r = 0; | ||
1612 | s_w = 0; | ||
1613 | if (BIO_should_retry(s_bio)) { | ||
1614 | if (BIO_should_read(s_bio)) | ||
1615 | s_r = 1; | ||
1616 | if (BIO_should_write(s_bio)) | ||
1617 | s_w = 1; | ||
1618 | } else { | ||
1619 | fprintf(stderr, "ERROR in SERVER\n"); | ||
1620 | ERR_print_errors(bio_err); | ||
1621 | goto err; | ||
1622 | } | ||
1623 | } else if (i == 0) { | ||
1624 | ERR_print_errors(bio_err); | ||
1625 | fprintf(stderr, "SSL SERVER STARTUP FAILED in SSL_write\n"); | ||
1626 | goto err; | ||
1627 | } else { | ||
1628 | if (debug) | ||
1629 | printf("server wrote %d\n", i); | ||
1630 | sw_num -= i; | ||
1631 | s_write = 0; | ||
1632 | c_r = 1; | ||
1633 | if (sw_num <= 0) | ||
1634 | done|=S_DONE; | ||
1635 | } | ||
1636 | } | ||
1637 | } | ||
1638 | |||
1639 | if ((done & S_DONE) | ||
1640 | && (done & C_DONE)) break; | ||
1641 | } | ||
1642 | |||
1643 | if (verbose) | ||
1644 | print_details(c_ssl, "DONE: "); | ||
1645 | ret = 0; | ||
1646 | err: | ||
1647 | /* We have to set the BIO's to NULL otherwise they will be | ||
1648 | * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and | ||
1649 | * again when c_ssl is SSL_free()ed. | ||
1650 | * This is a hack required because s_ssl and c_ssl are sharing the same | ||
1651 | * BIO structure and SSL_set_bio() and SSL_free() automatically | ||
1652 | * BIO_free non NULL entries. | ||
1653 | * You should not normally do this or be required to do this */ | ||
1654 | if (s_ssl != NULL) { | ||
1655 | s_ssl->rbio = NULL; | ||
1656 | s_ssl->wbio = NULL; | ||
1657 | } | ||
1658 | if (c_ssl != NULL) { | ||
1659 | c_ssl->rbio = NULL; | ||
1660 | c_ssl->wbio = NULL; | ||
1661 | } | ||
1662 | |||
1663 | if (c_to_s != NULL) | ||
1664 | BIO_free(c_to_s); | ||
1665 | if (s_to_c != NULL) | ||
1666 | BIO_free(s_to_c); | ||
1667 | if (c_bio != NULL) | ||
1668 | BIO_free_all(c_bio); | ||
1669 | if (s_bio != NULL) | ||
1670 | BIO_free_all(s_bio); | ||
1671 | return (ret); | ||
1672 | } | ||
1673 | |||
1674 | static int | ||
1675 | get_proxy_auth_ex_data_idx(void) | ||
1676 | { | ||
1677 | static volatile int idx = -1; | ||
1678 | if (idx < 0) { | ||
1679 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); | ||
1680 | if (idx < 0) { | ||
1681 | idx = X509_STORE_CTX_get_ex_new_index(0, | ||
1682 | "SSLtest for verify callback", NULL, NULL, NULL); | ||
1683 | } | ||
1684 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); | ||
1685 | } | ||
1686 | return idx; | ||
1687 | } | ||
1688 | |||
1689 | static int | ||
1690 | verify_callback(int ok, X509_STORE_CTX *ctx) | ||
1691 | { | ||
1692 | char *s, buf[256]; | ||
1693 | |||
1694 | s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), buf, | ||
1695 | sizeof buf); | ||
1696 | if (s != NULL) { | ||
1697 | if (ok) | ||
1698 | fprintf(stderr, "depth=%d %s\n", | ||
1699 | ctx->error_depth, buf); | ||
1700 | else { | ||
1701 | fprintf(stderr, "depth=%d error=%d %s\n", | ||
1702 | ctx->error_depth, ctx->error, buf); | ||
1703 | } | ||
1704 | } | ||
1705 | |||
1706 | if (ok == 0) { | ||
1707 | fprintf(stderr, "Error string: %s\n", | ||
1708 | X509_verify_cert_error_string(ctx->error)); | ||
1709 | switch (ctx->error) { | ||
1710 | case X509_V_ERR_CERT_NOT_YET_VALID: | ||
1711 | case X509_V_ERR_CERT_HAS_EXPIRED: | ||
1712 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | ||
1713 | fprintf(stderr, " ... ignored.\n"); | ||
1714 | ok = 1; | ||
1715 | } | ||
1716 | } | ||
1717 | |||
1718 | if (ok == 1) { | ||
1719 | X509 *xs = ctx->current_cert; | ||
1720 | #if 0 | ||
1721 | X509 *xi = ctx->current_issuer; | ||
1722 | #endif | ||
1723 | |||
1724 | if (xs->ex_flags & EXFLAG_PROXY) { | ||
1725 | unsigned int *letters = | ||
1726 | X509_STORE_CTX_get_ex_data(ctx, | ||
1727 | get_proxy_auth_ex_data_idx()); | ||
1728 | |||
1729 | if (letters) { | ||
1730 | int found_any = 0; | ||
1731 | int i; | ||
1732 | PROXY_CERT_INFO_EXTENSION *pci = | ||
1733 | X509_get_ext_d2i(xs, NID_proxyCertInfo, | ||
1734 | NULL, NULL); | ||
1735 | |||
1736 | switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage)) { | ||
1737 | case NID_Independent: | ||
1738 | /* Completely meaningless in this | ||
1739 | program, as there's no way to | ||
1740 | grant explicit rights to a | ||
1741 | specific PrC. Basically, using | ||
1742 | id-ppl-Independent is the perfect | ||
1743 | way to grant no rights at all. */ | ||
1744 | fprintf(stderr, " Independent proxy certificate"); | ||
1745 | for (i = 0; i < 26; i++) | ||
1746 | letters[i] = 0; | ||
1747 | break; | ||
1748 | case NID_id_ppl_inheritAll: | ||
1749 | /* This is basically a NOP, we | ||
1750 | simply let the current rights | ||
1751 | stand as they are. */ | ||
1752 | fprintf(stderr, " Proxy certificate inherits all"); | ||
1753 | break; | ||
1754 | default: | ||
1755 | s = (char *) | ||
1756 | pci->proxyPolicy->policy->data; | ||
1757 | i = pci->proxyPolicy->policy->length; | ||
1758 | |||
1759 | /* The algorithm works as follows: | ||
1760 | it is assumed that previous | ||
1761 | iterations or the initial granted | ||
1762 | rights has already set some elements | ||
1763 | of `letters'. What we need to do is | ||
1764 | to clear those that weren't granted | ||
1765 | by the current PrC as well. The | ||
1766 | easiest way to do this is to add 1 | ||
1767 | to all the elements whose letters | ||
1768 | are given with the current policy. | ||
1769 | That way, all elements that are set | ||
1770 | by the current policy and were | ||
1771 | already set by earlier policies and | ||
1772 | through the original grant of rights | ||
1773 | will get the value 2 or higher. | ||
1774 | The last thing to do is to sweep | ||
1775 | through `letters' and keep the | ||
1776 | elements having the value 2 as set, | ||
1777 | and clear all the others. */ | ||
1778 | |||
1779 | fprintf(stderr, " Certificate proxy rights = %*.*s", i, i, s); | ||
1780 | while (i-- > 0) { | ||
1781 | int c = *s++; | ||
1782 | if (isascii(c) && isalpha(c)) { | ||
1783 | if (islower(c)) | ||
1784 | c = toupper(c); | ||
1785 | letters[c - 'A']++; | ||
1786 | } | ||
1787 | } | ||
1788 | for (i = 0; i < 26; i++) | ||
1789 | if (letters[i] < 2) | ||
1790 | letters[i] = 0; | ||
1791 | else | ||
1792 | letters[i] = 1; | ||
1793 | } | ||
1794 | |||
1795 | found_any = 0; | ||
1796 | fprintf(stderr, ", resulting proxy rights = "); | ||
1797 | for (i = 0; i < 26; i++) | ||
1798 | if (letters[i]) { | ||
1799 | fprintf(stderr, "%c", i + 'A'); | ||
1800 | found_any = 1; | ||
1801 | } | ||
1802 | if (!found_any) | ||
1803 | fprintf(stderr, "none"); | ||
1804 | fprintf(stderr, "\n"); | ||
1805 | |||
1806 | PROXY_CERT_INFO_EXTENSION_free(pci); | ||
1807 | } | ||
1808 | } | ||
1809 | } | ||
1810 | |||
1811 | return (ok); | ||
1812 | } | ||
1813 | |||
1814 | static void | ||
1815 | process_proxy_debug(int indent, const char *format, ...) | ||
1816 | { | ||
1817 | static const char indentation[] = | ||
1818 | ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" | ||
1819 | ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; /* That's 80 > */ | ||
1820 | char my_format[256]; | ||
1821 | va_list args; | ||
1822 | |||
1823 | (void) snprintf(my_format, sizeof(my_format), "%*.*s %s", | ||
1824 | indent, indent, indentation, format); | ||
1825 | |||
1826 | va_start(args, format); | ||
1827 | vfprintf(stderr, my_format, args); | ||
1828 | va_end(args); | ||
1829 | } | ||
1830 | /* Priority levels: | ||
1831 | 0 [!]var, () | ||
1832 | 1 & ^ | ||
1833 | 2 | | ||
1834 | */ | ||
1835 | static int process_proxy_cond_adders(unsigned int letters[26], | ||
1836 | const char *cond, const char **cond_end, int *pos, int indent); | ||
1837 | |||
1838 | static int | ||
1839 | process_proxy_cond_val(unsigned int letters[26], const char *cond, | ||
1840 | const char **cond_end, int *pos, int indent) | ||
1841 | { | ||
1842 | int c; | ||
1843 | int ok = 1; | ||
1844 | int negate = 0; | ||
1845 | |||
1846 | while (isspace((int)*cond)) { | ||
1847 | cond++; | ||
1848 | (*pos)++; | ||
1849 | } | ||
1850 | c = *cond; | ||
1851 | |||
1852 | if (debug) | ||
1853 | process_proxy_debug(indent, | ||
1854 | "Start process_proxy_cond_val at position %d: %s\n", | ||
1855 | *pos, cond); | ||
1856 | |||
1857 | while (c == '!') { | ||
1858 | negate = !negate; | ||
1859 | cond++; | ||
1860 | (*pos)++; | ||
1861 | while (isspace((int)*cond)) { | ||
1862 | cond++; | ||
1863 | (*pos)++; | ||
1864 | } | ||
1865 | c = *cond; | ||
1866 | } | ||
1867 | |||
1868 | if (c == '(') { | ||
1869 | cond++; | ||
1870 | (*pos)++; | ||
1871 | ok = process_proxy_cond_adders(letters, cond, cond_end, pos, | ||
1872 | indent + 1); | ||
1873 | cond = *cond_end; | ||
1874 | if (ok < 0) | ||
1875 | goto end; | ||
1876 | while (isspace((int)*cond)) { | ||
1877 | cond++; | ||
1878 | (*pos)++; | ||
1879 | } | ||
1880 | c = *cond; | ||
1881 | if (c != ')') { | ||
1882 | fprintf(stderr, | ||
1883 | "Weird condition character in position %d: " | ||
1884 | "%c\n", *pos, c); | ||
1885 | ok = -1; | ||
1886 | goto end; | ||
1887 | } | ||
1888 | cond++; | ||
1889 | (*pos)++; | ||
1890 | } else if (isascii(c) && isalpha(c)) { | ||
1891 | if (islower(c)) | ||
1892 | c = toupper(c); | ||
1893 | ok = letters[c - 'A']; | ||
1894 | cond++; | ||
1895 | (*pos)++; | ||
1896 | } else { | ||
1897 | fprintf(stderr, | ||
1898 | "Weird condition character in position %d: " | ||
1899 | "%c\n", *pos, c); | ||
1900 | ok = -1; | ||
1901 | goto end; | ||
1902 | } | ||
1903 | end: | ||
1904 | *cond_end = cond; | ||
1905 | if (ok >= 0 && negate) | ||
1906 | ok = !ok; | ||
1907 | |||
1908 | if (debug) | ||
1909 | process_proxy_debug(indent, | ||
1910 | "End process_proxy_cond_val at position %d: %s, returning %d\n", | ||
1911 | *pos, cond, ok); | ||
1912 | |||
1913 | return ok; | ||
1914 | } | ||
1915 | |||
1916 | static int | ||
1917 | process_proxy_cond_multipliers(unsigned int letters[26], const char *cond, | ||
1918 | const char **cond_end, int *pos, int indent) | ||
1919 | { | ||
1920 | int ok; | ||
1921 | char c; | ||
1922 | |||
1923 | if (debug) | ||
1924 | process_proxy_debug(indent, | ||
1925 | "Start process_proxy_cond_multipliers at position %d: %s\n", | ||
1926 | *pos, cond); | ||
1927 | |||
1928 | ok = process_proxy_cond_val(letters, cond, cond_end, pos, indent + 1); | ||
1929 | cond = *cond_end; | ||
1930 | if (ok < 0) | ||
1931 | goto end; | ||
1932 | |||
1933 | while (ok >= 0) { | ||
1934 | while (isspace((int)*cond)) { | ||
1935 | cond++; | ||
1936 | (*pos)++; | ||
1937 | } | ||
1938 | c = *cond; | ||
1939 | |||
1940 | switch (c) { | ||
1941 | case '&': | ||
1942 | case '^': | ||
1943 | { | ||
1944 | int save_ok = ok; | ||
1945 | |||
1946 | cond++; | ||
1947 | (*pos)++; | ||
1948 | ok = process_proxy_cond_val(letters, | ||
1949 | cond, cond_end, pos, indent + 1); | ||
1950 | cond = *cond_end; | ||
1951 | if (ok < 0) | ||
1952 | break; | ||
1953 | |||
1954 | switch (c) { | ||
1955 | case '&': | ||
1956 | ok &= save_ok; | ||
1957 | break; | ||
1958 | case '^': | ||
1959 | ok ^= save_ok; | ||
1960 | break; | ||
1961 | default: | ||
1962 | fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!" | ||
1963 | " STOPPING\n"); | ||
1964 | exit(1); | ||
1965 | } | ||
1966 | } | ||
1967 | break; | ||
1968 | default: | ||
1969 | goto end; | ||
1970 | } | ||
1971 | } | ||
1972 | end: | ||
1973 | if (debug) | ||
1974 | process_proxy_debug(indent, | ||
1975 | "End process_proxy_cond_multipliers at position %d: %s, returning %d\n", | ||
1976 | *pos, cond, ok); | ||
1977 | |||
1978 | *cond_end = cond; | ||
1979 | return ok; | ||
1980 | } | ||
1981 | |||
1982 | static int | ||
1983 | process_proxy_cond_adders(unsigned int letters[26], const char *cond, | ||
1984 | const char **cond_end, int *pos, int indent) | ||
1985 | { | ||
1986 | int ok; | ||
1987 | char c; | ||
1988 | |||
1989 | if (debug) | ||
1990 | process_proxy_debug(indent, | ||
1991 | "Start process_proxy_cond_adders at position %d: %s\n", | ||
1992 | *pos, cond); | ||
1993 | |||
1994 | ok = process_proxy_cond_multipliers(letters, cond, cond_end, pos, | ||
1995 | indent + 1); | ||
1996 | cond = *cond_end; | ||
1997 | if (ok < 0) | ||
1998 | goto end; | ||
1999 | |||
2000 | while (ok >= 0) { | ||
2001 | while (isspace((int)*cond)) { | ||
2002 | cond++; | ||
2003 | (*pos)++; | ||
2004 | } | ||
2005 | c = *cond; | ||
2006 | |||
2007 | switch (c) { | ||
2008 | case '|': | ||
2009 | { | ||
2010 | int save_ok = ok; | ||
2011 | |||
2012 | cond++; | ||
2013 | (*pos)++; | ||
2014 | ok = process_proxy_cond_multipliers(letters, | ||
2015 | cond, cond_end, pos, indent + 1); | ||
2016 | cond = *cond_end; | ||
2017 | if (ok < 0) | ||
2018 | break; | ||
2019 | |||
2020 | switch (c) { | ||
2021 | case '|': | ||
2022 | ok |= save_ok; | ||
2023 | break; | ||
2024 | default: | ||
2025 | fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!" | ||
2026 | " STOPPING\n"); | ||
2027 | exit(1); | ||
2028 | } | ||
2029 | } | ||
2030 | break; | ||
2031 | default: | ||
2032 | goto end; | ||
2033 | } | ||
2034 | } | ||
2035 | end: | ||
2036 | if (debug) | ||
2037 | process_proxy_debug(indent, | ||
2038 | "End process_proxy_cond_adders at position %d: %s, returning %d\n", | ||
2039 | *pos, cond, ok); | ||
2040 | |||
2041 | *cond_end = cond; | ||
2042 | return ok; | ||
2043 | } | ||
2044 | |||
2045 | static int | ||
2046 | process_proxy_cond(unsigned int letters[26], const char *cond, | ||
2047 | const char **cond_end) | ||
2048 | { | ||
2049 | int pos = 1; | ||
2050 | return process_proxy_cond_adders(letters, cond, cond_end, &pos, 1); | ||
2051 | } | ||
2052 | |||
2053 | static int | ||
2054 | app_verify_callback(X509_STORE_CTX *ctx, void *arg) | ||
2055 | { | ||
2056 | int ok = 1; | ||
2057 | struct app_verify_arg *cb_arg = arg; | ||
2058 | unsigned int letters[26]; /* only used with proxy_auth */ | ||
2059 | |||
2060 | if (cb_arg->app_verify) { | ||
2061 | char *s = NULL, buf[256]; | ||
2062 | |||
2063 | fprintf(stderr, "In app_verify_callback, allowing cert. "); | ||
2064 | fprintf(stderr, "Arg is: %s\n", cb_arg->string); | ||
2065 | fprintf(stderr, "Finished printing do we have a context? 0x%p a cert? 0x%p\n", | ||
2066 | (void *)ctx, (void *)ctx->cert); | ||
2067 | if (ctx->cert) | ||
2068 | s = X509_NAME_oneline(X509_get_subject_name(ctx->cert), buf, 256); | ||
2069 | if (s != NULL) { | ||
2070 | fprintf(stderr, "cert depth=%d %s\n", ctx->error_depth, buf); | ||
2071 | } | ||
2072 | return (1); | ||
2073 | } | ||
2074 | if (cb_arg->proxy_auth) { | ||
2075 | int found_any = 0, i; | ||
2076 | char *sp; | ||
2077 | |||
2078 | for (i = 0; i < 26; i++) | ||
2079 | letters[i] = 0; | ||
2080 | for (sp = cb_arg->proxy_auth; *sp; sp++) { | ||
2081 | int c = *sp; | ||
2082 | if (isascii(c) && isalpha(c)) { | ||
2083 | if (islower(c)) | ||
2084 | c = toupper(c); | ||
2085 | letters[c - 'A'] = 1; | ||
2086 | } | ||
2087 | } | ||
2088 | |||
2089 | fprintf(stderr, " Initial proxy rights = "); | ||
2090 | for (i = 0; i < 26; i++) | ||
2091 | if (letters[i]) { | ||
2092 | fprintf(stderr, "%c", i + 'A'); | ||
2093 | found_any = 1; | ||
2094 | } | ||
2095 | if (!found_any) | ||
2096 | fprintf(stderr, "none"); | ||
2097 | fprintf(stderr, "\n"); | ||
2098 | |||
2099 | X509_STORE_CTX_set_ex_data(ctx, | ||
2100 | get_proxy_auth_ex_data_idx(), letters); | ||
2101 | } | ||
2102 | if (cb_arg->allow_proxy_certs) { | ||
2103 | X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS); | ||
2104 | } | ||
2105 | |||
2106 | #ifndef OPENSSL_NO_X509_VERIFY | ||
2107 | ok = X509_verify_cert(ctx); | ||
2108 | #endif | ||
2109 | |||
2110 | if (cb_arg->proxy_auth) { | ||
2111 | if (ok > 0) { | ||
2112 | const char *cond_end = NULL; | ||
2113 | |||
2114 | ok = process_proxy_cond(letters, | ||
2115 | cb_arg->proxy_cond, &cond_end); | ||
2116 | |||
2117 | if (ok < 0) | ||
2118 | exit(3); | ||
2119 | if (*cond_end) { | ||
2120 | fprintf(stderr, "Stopped processing condition before it's end.\n"); | ||
2121 | ok = 0; | ||
2122 | } | ||
2123 | if (!ok) | ||
2124 | fprintf(stderr, "Proxy rights check with condition '%s' proved invalid\n", | ||
2125 | cb_arg->proxy_cond); | ||
2126 | else | ||
2127 | fprintf(stderr, "Proxy rights check with condition '%s' proved valid\n", | ||
2128 | cb_arg->proxy_cond); | ||
2129 | } | ||
2130 | } | ||
2131 | return (ok); | ||
2132 | } | ||
2133 | |||
2134 | #ifndef OPENSSL_NO_RSA | ||
2135 | static RSA *rsa_tmp = NULL; | ||
2136 | |||
2137 | static RSA | ||
2138 | *tmp_rsa_cb(SSL *s, int is_export, int keylength) | ||
2139 | { | ||
2140 | BIGNUM *bn = NULL; | ||
2141 | if (rsa_tmp == NULL) { | ||
2142 | bn = BN_new(); | ||
2143 | rsa_tmp = RSA_new(); | ||
2144 | if (!bn || !rsa_tmp || !BN_set_word(bn, RSA_F4)) { | ||
2145 | BIO_printf(bio_err, "Memory error..."); | ||
2146 | goto end; | ||
2147 | } | ||
2148 | BIO_printf(bio_err, "Generating temp (%d bit) RSA key...", keylength); | ||
2149 | (void)BIO_flush(bio_err); | ||
2150 | if (!RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) { | ||
2151 | BIO_printf(bio_err, "Error generating key."); | ||
2152 | RSA_free(rsa_tmp); | ||
2153 | rsa_tmp = NULL; | ||
2154 | } | ||
2155 | end: | ||
2156 | BIO_printf(bio_err, "\n"); | ||
2157 | (void)BIO_flush(bio_err); | ||
2158 | } | ||
2159 | if (bn) | ||
2160 | BN_free(bn); | ||
2161 | return (rsa_tmp); | ||
2162 | } | ||
2163 | |||
2164 | static void | ||
2165 | free_tmp_rsa(void) | ||
2166 | { | ||
2167 | if (rsa_tmp != NULL) { | ||
2168 | RSA_free(rsa_tmp); | ||
2169 | rsa_tmp = NULL; | ||
2170 | } | ||
2171 | } | ||
2172 | #endif | ||
2173 | |||
2174 | #ifndef OPENSSL_NO_DH | ||
2175 | /* These DH parameters have been generated as follows: | ||
2176 | * $ openssl dhparam -C -noout 512 | ||
2177 | * $ openssl dhparam -C -noout 1024 | ||
2178 | * $ openssl dhparam -C -noout -dsaparam 1024 | ||
2179 | * (The third function has been renamed to avoid name conflicts.) | ||
2180 | */ | ||
2181 | static DH | ||
2182 | *get_dh512() | ||
2183 | { | ||
2184 | static unsigned char dh512_p[] = { | ||
2185 | 0xCB, 0xC8, 0xE1, 0x86, 0xD0, 0x1F, 0x94, 0x17, 0xA6, 0x99, 0xF0, 0xC6, | ||
2186 | 0x1F, 0x0D, 0xAC, 0xB6, 0x25, 0x3E, 0x06, 0x39, 0xCA, 0x72, 0x04, 0xB0, | ||
2187 | 0x6E, 0xDA, 0xC0, 0x61, 0xE6, 0x7A, 0x77, 0x25, 0xE8, 0x3B, 0xB9, 0x5F, | ||
2188 | 0x9A, 0xB6, 0xB5, 0xFE, 0x99, 0x0B, 0xA1, 0x93, 0x4E, 0x35, 0x33, 0xB8, | ||
2189 | 0xE1, 0xF1, 0x13, 0x4F, 0x59, 0x1A, 0xD2, 0x57, 0xC0, 0x26, 0x21, 0x33, | ||
2190 | 0x02, 0xC5, 0xAE, 0x23, | ||
2191 | }; | ||
2192 | static unsigned char dh512_g[] = { | ||
2193 | 0x02, | ||
2194 | }; | ||
2195 | DH *dh; | ||
2196 | |||
2197 | if ((dh = DH_new()) == NULL) return (NULL); | ||
2198 | dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL); | ||
2199 | dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL); | ||
2200 | if ((dh->p == NULL) || (dh->g == NULL)) { | ||
2201 | DH_free(dh); | ||
2202 | return (NULL); | ||
2203 | } | ||
2204 | return (dh); | ||
2205 | } | ||
2206 | |||
2207 | static DH | ||
2208 | *get_dh1024() | ||
2209 | { | ||
2210 | static unsigned char dh1024_p[] = { | ||
2211 | 0xF8, 0x81, 0x89, 0x7D, 0x14, 0x24, 0xC5, 0xD1, 0xE6, 0xF7, 0xBF, 0x3A, | ||
2212 | 0xE4, 0x90, 0xF4, 0xFC, 0x73, 0xFB, 0x34, 0xB5, 0xFA, 0x4C, 0x56, 0xA2, | ||
2213 | 0xEA, 0xA7, 0xE9, 0xC0, 0xC0, 0xCE, 0x89, 0xE1, 0xFA, 0x63, 0x3F, 0xB0, | ||
2214 | 0x6B, 0x32, 0x66, 0xF1, 0xD1, 0x7B, 0xB0, 0x00, 0x8F, 0xCA, 0x87, 0xC2, | ||
2215 | 0xAE, 0x98, 0x89, 0x26, 0x17, 0xC2, 0x05, 0xD2, 0xEC, 0x08, 0xD0, 0x8C, | ||
2216 | 0xFF, 0x17, 0x52, 0x8C, 0xC5, 0x07, 0x93, 0x03, 0xB1, 0xF6, 0x2F, 0xB8, | ||
2217 | 0x1C, 0x52, 0x47, 0x27, 0x1B, 0xDB, 0xD1, 0x8D, 0x9D, 0x69, 0x1D, 0x52, | ||
2218 | 0x4B, 0x32, 0x81, 0xAA, 0x7F, 0x00, 0xC8, 0xDC, 0xE6, 0xD9, 0xCC, 0xC1, | ||
2219 | 0x11, 0x2D, 0x37, 0x34, 0x6C, 0xEA, 0x02, 0x97, 0x4B, 0x0E, 0xBB, 0xB1, | ||
2220 | 0x71, 0x33, 0x09, 0x15, 0xFD, 0xDD, 0x23, 0x87, 0x07, 0x5E, 0x89, 0xAB, | ||
2221 | 0x6B, 0x7C, 0x5F, 0xEC, 0xA6, 0x24, 0xDC, 0x53, | ||
2222 | }; | ||
2223 | static unsigned char dh1024_g[] = { | ||
2224 | 0x02, | ||
2225 | }; | ||
2226 | DH *dh; | ||
2227 | |||
2228 | if ((dh = DH_new()) == NULL) return (NULL); | ||
2229 | dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL); | ||
2230 | dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL); | ||
2231 | if ((dh->p == NULL) || (dh->g == NULL)) { | ||
2232 | DH_free(dh); | ||
2233 | return (NULL); | ||
2234 | } | ||
2235 | return (dh); | ||
2236 | } | ||
2237 | |||
2238 | static DH | ||
2239 | *get_dh1024dsa() | ||
2240 | { | ||
2241 | static unsigned char dh1024_p[] = { | ||
2242 | 0xC8, 0x00, 0xF7, 0x08, 0x07, 0x89, 0x4D, 0x90, 0x53, 0xF3, 0xD5, 0x00, | ||
2243 | 0x21, 0x1B, 0xF7, 0x31, 0xA6, 0xA2, 0xDA, 0x23, 0x9A, 0xC7, 0x87, 0x19, | ||
2244 | 0x3B, 0x47, 0xB6, 0x8C, 0x04, 0x6F, 0xFF, 0xC6, 0x9B, 0xB8, 0x65, 0xD2, | ||
2245 | 0xC2, 0x5F, 0x31, 0x83, 0x4A, 0xA7, 0x5F, 0x2F, 0x88, 0x38, 0xB6, 0x55, | ||
2246 | 0xCF, 0xD9, 0x87, 0x6D, 0x6F, 0x9F, 0xDA, 0xAC, 0xA6, 0x48, 0xAF, 0xFC, | ||
2247 | 0x33, 0x84, 0x37, 0x5B, 0x82, 0x4A, 0x31, 0x5D, 0xE7, 0xBD, 0x52, 0x97, | ||
2248 | 0xA1, 0x77, 0xBF, 0x10, 0x9E, 0x37, 0xEA, 0x64, 0xFA, 0xCA, 0x28, 0x8D, | ||
2249 | 0x9D, 0x3B, 0xD2, 0x6E, 0x09, 0x5C, 0x68, 0xC7, 0x45, 0x90, 0xFD, 0xBB, | ||
2250 | 0x70, 0xC9, 0x3A, 0xBB, 0xDF, 0xD4, 0x21, 0x0F, 0xC4, 0x6A, 0x3C, 0xF6, | ||
2251 | 0x61, 0xCF, 0x3F, 0xD6, 0x13, 0xF1, 0x5F, 0xBC, 0xCF, 0xBC, 0x26, 0x9E, | ||
2252 | 0xBC, 0x0B, 0xBD, 0xAB, 0x5D, 0xC9, 0x54, 0x39, | ||
2253 | }; | ||
2254 | static unsigned char dh1024_g[] = { | ||
2255 | 0x3B, 0x40, 0x86, 0xE7, 0xF3, 0x6C, 0xDE, 0x67, 0x1C, 0xCC, 0x80, 0x05, | ||
2256 | 0x5A, 0xDF, 0xFE, 0xBD, 0x20, 0x27, 0x74, 0x6C, 0x24, 0xC9, 0x03, 0xF3, | ||
2257 | 0xE1, 0x8D, 0xC3, 0x7D, 0x98, 0x27, 0x40, 0x08, 0xB8, 0x8C, 0x6A, 0xE9, | ||
2258 | 0xBB, 0x1A, 0x3A, 0xD6, 0x86, 0x83, 0x5E, 0x72, 0x41, 0xCE, 0x85, 0x3C, | ||
2259 | 0xD2, 0xB3, 0xFC, 0x13, 0xCE, 0x37, 0x81, 0x9E, 0x4C, 0x1C, 0x7B, 0x65, | ||
2260 | 0xD3, 0xE6, 0xA6, 0x00, 0xF5, 0x5A, 0x95, 0x43, 0x5E, 0x81, 0xCF, 0x60, | ||
2261 | 0xA2, 0x23, 0xFC, 0x36, 0xA7, 0x5D, 0x7A, 0x4C, 0x06, 0x91, 0x6E, 0xF6, | ||
2262 | 0x57, 0xEE, 0x36, 0xCB, 0x06, 0xEA, 0xF5, 0x3D, 0x95, 0x49, 0xCB, 0xA7, | ||
2263 | 0xDD, 0x81, 0xDF, 0x80, 0x09, 0x4A, 0x97, 0x4D, 0xA8, 0x22, 0x72, 0xA1, | ||
2264 | 0x7F, 0xC4, 0x70, 0x56, 0x70, 0xE8, 0x20, 0x10, 0x18, 0x8F, 0x2E, 0x60, | ||
2265 | 0x07, 0xE7, 0x68, 0x1A, 0x82, 0x5D, 0x32, 0xA2, | ||
2266 | }; | ||
2267 | DH *dh; | ||
2268 | |||
2269 | if ((dh = DH_new()) == NULL) return (NULL); | ||
2270 | dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL); | ||
2271 | dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL); | ||
2272 | if ((dh->p == NULL) || (dh->g == NULL)) { | ||
2273 | DH_free(dh); | ||
2274 | return (NULL); | ||
2275 | } | ||
2276 | dh->length = 160; | ||
2277 | return (dh); | ||
2278 | } | ||
2279 | #endif | ||
2280 | |||
2281 | #ifndef OPENSSL_NO_PSK | ||
2282 | /* convert the PSK key (psk_key) in ascii to binary (psk) */ | ||
2283 | static int | ||
2284 | psk_key2bn(const char *pskkey, unsigned char *psk, unsigned int max_psk_len) | ||
2285 | { | ||
2286 | int ret; | ||
2287 | BIGNUM *bn = NULL; | ||
2288 | |||
2289 | ret = BN_hex2bn(&bn, pskkey); | ||
2290 | if (!ret) { | ||
2291 | BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", pskkey); | ||
2292 | if (bn) | ||
2293 | BN_free(bn); | ||
2294 | return 0; | ||
2295 | } | ||
2296 | if (BN_num_bytes(bn) > (int)max_psk_len) { | ||
2297 | BIO_printf(bio_err, "psk buffer of callback is too small (%d) for key (%d)\n", | ||
2298 | max_psk_len, BN_num_bytes(bn)); | ||
2299 | BN_free(bn); | ||
2300 | return 0; | ||
2301 | } | ||
2302 | ret = BN_bn2bin(bn, psk); | ||
2303 | BN_free(bn); | ||
2304 | return ret; | ||
2305 | } | ||
2306 | |||
2307 | static unsigned int | ||
2308 | psk_client_callback(SSL *ssl, const char *hint, char *identity, | ||
2309 | unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len) | ||
2310 | { | ||
2311 | int ret; | ||
2312 | unsigned int psk_len = 0; | ||
2313 | |||
2314 | ret = snprintf(identity, max_identity_len, "Client_identity"); | ||
2315 | if (ret >= max_identity_len || ret == -1) | ||
2316 | goto out_err; | ||
2317 | if (debug) | ||
2318 | fprintf(stderr, "client: created identity '%s' len=%d\n", identity, ret); | ||
2319 | ret = psk_key2bn(psk_key, psk, max_psk_len); | ||
2320 | if (ret < 0) | ||
2321 | goto out_err; | ||
2322 | psk_len = ret; | ||
2323 | out_err: | ||
2324 | return psk_len; | ||
2325 | } | ||
2326 | |||
2327 | static unsigned int | ||
2328 | psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk, | ||
2329 | unsigned int max_psk_len) | ||
2330 | { | ||
2331 | unsigned int psk_len = 0; | ||
2332 | |||
2333 | if (strcmp(identity, "Client_identity") != 0) { | ||
2334 | BIO_printf(bio_err, "server: PSK error: client identity not found\n"); | ||
2335 | return 0; | ||
2336 | } | ||
2337 | psk_len = psk_key2bn(psk_key, psk, max_psk_len); | ||
2338 | return psk_len; | ||
2339 | } | ||
2340 | #endif | ||
2341 | |||
2342 | static int | ||
2343 | do_test_cipherlist(void) | ||
2344 | { | ||
2345 | int i = 0; | ||
2346 | const SSL_METHOD *meth; | ||
2347 | const SSL_CIPHER *ci, *tci = NULL; | ||
2348 | |||
2349 | #ifndef OPENSSL_NO_SSL2 | ||
2350 | fprintf(stderr, "testing SSLv2 cipher list order: "); | ||
2351 | meth = SSLv2_method(); | ||
2352 | while ((ci = meth->get_cipher(i++)) != NULL) { | ||
2353 | if (tci != NULL) | ||
2354 | if (ci->id >= tci->id) { | ||
2355 | fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id); | ||
2356 | return 0; | ||
2357 | } | ||
2358 | tci = ci; | ||
2359 | } | ||
2360 | fprintf(stderr, "ok\n"); | ||
2361 | #endif | ||
2362 | #ifndef OPENSSL_NO_SSL3 | ||
2363 | fprintf(stderr, "testing SSLv3 cipher list order: "); | ||
2364 | meth = SSLv3_method(); | ||
2365 | tci = NULL; | ||
2366 | while ((ci = meth->get_cipher(i++)) != NULL) { | ||
2367 | if (tci != NULL) | ||
2368 | if (ci->id >= tci->id) { | ||
2369 | fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id); | ||
2370 | return 0; | ||
2371 | } | ||
2372 | tci = ci; | ||
2373 | } | ||
2374 | fprintf(stderr, "ok\n"); | ||
2375 | #endif | ||
2376 | #ifndef OPENSSL_NO_TLS1 | ||
2377 | fprintf(stderr, "testing TLSv1 cipher list order: "); | ||
2378 | meth = TLSv1_method(); | ||
2379 | tci = NULL; | ||
2380 | while ((ci = meth->get_cipher(i++)) != NULL) { | ||
2381 | if (tci != NULL) | ||
2382 | if (ci->id >= tci->id) { | ||
2383 | fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id); | ||
2384 | return 0; | ||
2385 | } | ||
2386 | tci = ci; | ||
2387 | } | ||
2388 | fprintf(stderr, "ok\n"); | ||
2389 | #endif | ||
2390 | |||
2391 | return 1; | ||
2392 | } | ||