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