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