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