1/* crypto/threads/mttest.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#include <stdlib.h> 60#include <string.h> 61#include <errno.h> 62#ifdef LINUX 63# include <typedefs.h> 64#endif 65#ifdef OPENSSL_SYS_WIN32 66# include <windows.h> 67#endif 68#ifdef SOLARIS 69# include <synch.h> 70# include <thread.h> 71#endif 72#ifdef IRIX 73# include <ulocks.h> 74# include <sys/prctl.h> 75#endif 76#ifdef PTHREADS 77# include <pthread.h> 78#endif 79#ifdef OPENSSL_SYS_NETWARE 80# if !defined __int64 81# define __int64 long long 82# endif 83# include <nwmpk.h> 84#endif 85#include <openssl/lhash.h> 86#include <openssl/crypto.h> 87#include <openssl/buffer.h> 88#include <openssl/x509.h> 89#include <openssl/ssl.h> 90#include <openssl/err.h> 91#include <openssl/rand.h> 92 93#ifdef OPENSSL_SYS_NETWARE 94# define TEST_SERVER_CERT "/openssl/apps/server.pem" 95# define TEST_CLIENT_CERT "/openssl/apps/client.pem" 96#else 97# define TEST_SERVER_CERT "../../apps/server.pem" 98# define TEST_CLIENT_CERT "../../apps/client.pem" 99#endif 100 101#define MAX_THREAD_NUMBER 100 102 103int verify_callback(int ok, X509_STORE_CTX *xs); 104void thread_setup(void); 105void thread_cleanup(void); 106void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx); 107 108void irix_locking_callback(int mode, int type, const char *file, int line); 109void solaris_locking_callback(int mode, int type, const char *file, int line); 110void win32_locking_callback(int mode, int type, const char *file, int line); 111void pthreads_locking_callback(int mode, int type, const char *file, int line); 112void netware_locking_callback(int mode, int type, const char *file, int line); 113void beos_locking_callback(int mode, int type, const char *file, int line); 114 115void irix_thread_id(CRYPTO_THREADID *tid); 116void solaris_thread_id(CRYPTO_THREADID *tid); 117void pthreads_thread_id(CRYPTO_THREADID *tid); 118void netware_thread_id(CRYPTO_THREADID *tid); 119void beos_thread_id(CRYPTO_THREADID *tid); 120 121#if defined(OPENSSL_SYS_NETWARE) 122static MPKMutex *lock_cs; 123static MPKSema ThreadSem; 124static long *lock_count; 125#endif 126 127BIO *bio_err = NULL; 128BIO *bio_stdout = NULL; 129 130static char *cipher = NULL; 131int verbose = 0; 132#ifdef FIONBIO 133static int s_nbio = 0; 134#endif 135 136int thread_number = 10; 137int number_of_loops = 10; 138int reconnect = 0; 139int cache_stats = 0; 140 141static const char rnd_seed[] = 142 "string to make the random number generator think it has entropy"; 143 144int doit(char *ctx[4]); 145static void print_stats(BIO *bio, SSL_CTX *ctx) 146{ 147 BIO_printf(bio, "%4ld items in the session cache\n", 148 SSL_CTX_sess_number(ctx)); 149 BIO_printf(bio, "%4d client connects (SSL_connect())\n", 150 SSL_CTX_sess_connect(ctx)); 151 BIO_printf(bio, "%4d client connects that finished\n", 152 SSL_CTX_sess_connect_good(ctx)); 153 BIO_printf(bio, "%4d server connects (SSL_accept())\n", 154 SSL_CTX_sess_accept(ctx)); 155 BIO_printf(bio, "%4d server connects that finished\n", 156 SSL_CTX_sess_accept_good(ctx)); 157 BIO_printf(bio, "%4d session cache hits\n", SSL_CTX_sess_hits(ctx)); 158 BIO_printf(bio, "%4d session cache misses\n", SSL_CTX_sess_misses(ctx)); 159 BIO_printf(bio, "%4d session cache timeouts\n", SSL_CTX_sess_timeouts(ctx)); 160} 161 162static void sv_usage(void) 163{ 164 BIO_printf(bio_err, "usage: ssltest [args ...]\n"); 165 BIO_printf(bio_err, "\n"); 166 BIO_printf(bio_err, " -server_auth - check server certificate\n"); 167 BIO_printf(bio_err, " -client_auth - do client authentication\n"); 168 BIO_printf(bio_err, " -v - more output\n"); 169 BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n"); 170 BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n"); 171 BIO_printf(bio_err, " -threads arg - number of threads\n"); 172 BIO_printf(bio_err, " -loops arg - number of 'connections', per thread\n"); 173 BIO_printf(bio_err, " -reconnect - reuse session-id's\n"); 174 BIO_printf(bio_err, " -stats - server session-id cache stats\n"); 175 BIO_printf(bio_err, " -cert arg - server certificate/key\n"); 176 BIO_printf(bio_err, " -ccert arg - client certificate/key\n"); 177 BIO_printf(bio_err, " -ssl3 - just SSLv3n\n"); 178} 179 180int main(int argc, char *argv[]) 181{ 182 char *CApath = NULL, *CAfile = NULL; 183 int badop = 0; 184 int ret = 1; 185 int client_auth = 0; 186 int server_auth = 0; 187 SSL_CTX *s_ctx = NULL; 188 SSL_CTX *c_ctx = NULL; 189 char *scert = TEST_SERVER_CERT; 190 char *ccert = TEST_CLIENT_CERT; 191 const SSL_METHOD *ssl_method = SSLv23_method(); 192 193 RAND_seed(rnd_seed, sizeof rnd_seed); 194 195 if (bio_err == NULL) 196 bio_err = BIO_new_fd(2, BIO_NOCLOSE); 197 if (bio_stdout == NULL) 198 bio_stdout = BIO_new_fd(1, BIO_NOCLOSE); 199 argc--; 200 argv++; 201 202 while (argc >= 1) { 203 if (strcmp(*argv, "-server_auth") == 0) 204 server_auth = 1; 205 else if (strcmp(*argv, "-client_auth") == 0) 206 client_auth = 1; 207 else if (strcmp(*argv, "-reconnect") == 0) 208 reconnect = 1; 209 else if (strcmp(*argv, "-stats") == 0) 210 cache_stats = 1; 211 else if (strcmp(*argv, "-ssl3") == 0) 212 ssl_method = SSLv3_method(); 213 else if (strcmp(*argv, "-ssl2") == 0) 214 ssl_method = SSLv2_method(); 215 else if (strcmp(*argv, "-CApath") == 0) { 216 if (--argc < 1) 217 goto bad; 218 CApath = *(++argv); 219 } else if (strcmp(*argv, "-CAfile") == 0) { 220 if (--argc < 1) 221 goto bad; 222 CAfile = *(++argv); 223 } else if (strcmp(*argv, "-cert") == 0) { 224 if (--argc < 1) 225 goto bad; 226 scert = *(++argv); 227 } else if (strcmp(*argv, "-ccert") == 0) { 228 if (--argc < 1) 229 goto bad; 230 ccert = *(++argv); 231 } else if (strcmp(*argv, "-threads") == 0) { 232 if (--argc < 1) 233 goto bad; 234 thread_number = atoi(*(++argv)); 235 if (thread_number == 0) 236 thread_number = 1; 237 if (thread_number > MAX_THREAD_NUMBER) 238 thread_number = MAX_THREAD_NUMBER; 239 } else if (strcmp(*argv, "-loops") == 0) { 240 if (--argc < 1) 241 goto bad; 242 number_of_loops = atoi(*(++argv)); 243 if (number_of_loops == 0) 244 number_of_loops = 1; 245 } else { 246 BIO_printf(bio_err, "unknown option %s\n", *argv); 247 badop = 1; 248 break; 249 } 250 argc--; 251 argv++; 252 } 253 if (badop) { 254 bad: 255 sv_usage(); 256 goto end; 257 } 258 259 if (cipher == NULL && OPENSSL_issetugid() == 0) 260 cipher = getenv("SSL_CIPHER"); 261 262 SSL_load_error_strings(); 263 OpenSSL_add_ssl_algorithms(); 264 265 c_ctx = SSL_CTX_new(ssl_method); 266 s_ctx = SSL_CTX_new(ssl_method); 267 if ((c_ctx == NULL) || (s_ctx == NULL)) { 268 ERR_print_errors(bio_err); 269 goto end; 270 } 271 272 SSL_CTX_set_session_cache_mode(s_ctx, 273 SSL_SESS_CACHE_NO_AUTO_CLEAR | 274 SSL_SESS_CACHE_SERVER); 275 SSL_CTX_set_session_cache_mode(c_ctx, 276 SSL_SESS_CACHE_NO_AUTO_CLEAR | 277 SSL_SESS_CACHE_SERVER); 278 279 if (!SSL_CTX_use_certificate_file(s_ctx, scert, SSL_FILETYPE_PEM)) { 280 BIO_printf(bio_err, "SSL_CTX_use_certificate_file (%s)\n", scert); 281 ERR_print_errors(bio_err); 282 goto end; 283 } else 284 if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx, scert, SSL_FILETYPE_PEM)) { 285 BIO_printf(bio_err, "SSL_CTX_use_RSAPrivateKey_file (%s)\n", scert); 286 ERR_print_errors(bio_err); 287 goto end; 288 } 289 290 if (client_auth) { 291 SSL_CTX_use_certificate_file(c_ctx, ccert, SSL_FILETYPE_PEM); 292 SSL_CTX_use_RSAPrivateKey_file(c_ctx, ccert, SSL_FILETYPE_PEM); 293 } 294 295 if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) || 296 (!SSL_CTX_set_default_verify_paths(s_ctx)) || 297 (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) || 298 (!SSL_CTX_set_default_verify_paths(c_ctx))) { 299 BIO_printf(bio_err, "SSL_load_verify_locations\n"); 300 ERR_print_errors(bio_err); 301 goto end; 302 } 303 304 if (client_auth) { 305 BIO_printf(bio_err, "client authentication\n"); 306 SSL_CTX_set_verify(s_ctx, 307 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 308 verify_callback); 309 } 310 if (server_auth) { 311 BIO_printf(bio_err, "server authentication\n"); 312 SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback); 313 } 314 315 thread_setup(); 316 do_threads(s_ctx, c_ctx); 317 thread_cleanup(); 318 end: 319 320 if (c_ctx != NULL) { 321 BIO_printf(bio_err, "Client SSL_CTX stats then free it\n"); 322 print_stats(bio_err, c_ctx); 323 SSL_CTX_free(c_ctx); 324 } 325 if (s_ctx != NULL) { 326 BIO_printf(bio_err, "Server SSL_CTX stats then free it\n"); 327 print_stats(bio_err, s_ctx); 328 if (cache_stats) { 329 BIO_printf(bio_err, "-----\n"); 330 lh_SSL_SESSION_stats_bio(SSL_CTX_sessions(s_ctx), bio_err); 331 BIO_printf(bio_err, "-----\n"); 332 /*- lh_SSL_SESSION_node_stats_bio(SSL_CTX_sessions(s_ctx),bio_err); 333 BIO_printf(bio_err,"-----\n"); */ 334 lh_SSL_SESSION_node_usage_stats_bio(SSL_CTX_sessions(s_ctx), bio_err); 335 BIO_printf(bio_err, "-----\n"); 336 } 337 SSL_CTX_free(s_ctx); 338 BIO_printf(bio_err, "done free\n"); 339 } 340 exit(ret); 341 return (0); 342} 343 344#define W_READ 1 345#define W_WRITE 2 346#define C_DONE 1 347#define S_DONE 2 348 349int ndoit(SSL_CTX *ssl_ctx[2]) 350{ 351 int i; 352 int ret; 353 char *ctx[4]; 354 CRYPTO_THREADID thread_id; 355 356 ctx[0] = (char *)ssl_ctx[0]; 357 ctx[1] = (char *)ssl_ctx[1]; 358 359 if (reconnect) { 360 ctx[2] = (char *)SSL_new(ssl_ctx[0]); 361 ctx[3] = (char *)SSL_new(ssl_ctx[1]); 362 } else { 363 ctx[2] = NULL; 364 ctx[3] = NULL; 365 } 366 367 CRYPTO_THREADID_current(&thread_id); 368 BIO_printf(bio_stdout, "started thread %lu\n", 369 CRYPTO_THREADID_hash(&thread_id)); 370 for (i = 0; i < number_of_loops; i++) { 371/*- BIO_printf(bio_err,"%4d %2d ctx->ref (%3d,%3d)\n", 372 CRYPTO_THREADID_hash(&thread_id),i, 373 ssl_ctx[0]->references, 374 ssl_ctx[1]->references); */ 375/* pthread_delay_np(&tm); */ 376 377 ret = doit(ctx); 378 if (ret != 0) { 379 BIO_printf(bio_stdout, "error[%d] %lu - %d\n", 380 i, CRYPTO_THREADID_hash(&thread_id), ret); 381 return (ret); 382 } 383 } 384 BIO_printf(bio_stdout, "DONE %lu\n", CRYPTO_THREADID_hash(&thread_id)); 385 if (reconnect) { 386 SSL_free((SSL *)ctx[2]); 387 SSL_free((SSL *)ctx[3]); 388 } 389#ifdef OPENSSL_SYS_NETWARE 390 MPKSemaphoreSignal(ThreadSem); 391#endif 392 return (0); 393} 394 395int doit(char *ctx[4]) 396{ 397 SSL_CTX *s_ctx, *c_ctx; 398 static char cbuf[200], sbuf[200]; 399 SSL *c_ssl = NULL; 400 SSL *s_ssl = NULL; 401 BIO *c_to_s = NULL; 402 BIO *s_to_c = NULL; 403 BIO *c_bio = NULL; 404 BIO *s_bio = NULL; 405 int c_r, c_w, s_r, s_w; 406 int c_want, s_want; 407 int i; 408 int done = 0; 409 int c_write, s_write; 410 int do_server = 0, do_client = 0; 411 412 s_ctx = (SSL_CTX *)ctx[0]; 413 c_ctx = (SSL_CTX *)ctx[1]; 414 415 if (ctx[2] != NULL) 416 s_ssl = (SSL *)ctx[2]; 417 else 418 s_ssl = SSL_new(s_ctx); 419 420 if (ctx[3] != NULL) 421 c_ssl = (SSL *)ctx[3]; 422 else 423 c_ssl = SSL_new(c_ctx); 424 425 if ((s_ssl == NULL) || (c_ssl == NULL)) 426 goto err; 427 428 c_to_s = BIO_new(BIO_s_mem()); 429 s_to_c = BIO_new(BIO_s_mem()); 430 if ((s_to_c == NULL) || (c_to_s == NULL)) 431 goto err; 432 433 c_bio = BIO_new(BIO_f_ssl()); 434 s_bio = BIO_new(BIO_f_ssl()); 435 if ((c_bio == NULL) || (s_bio == NULL)) 436 goto err; 437 438 SSL_set_connect_state(c_ssl); 439 SSL_set_bio(c_ssl, s_to_c, c_to_s); 440 BIO_set_ssl(c_bio, c_ssl, (ctx[2] == NULL) ? BIO_CLOSE : BIO_NOCLOSE); 441 442 SSL_set_accept_state(s_ssl); 443 SSL_set_bio(s_ssl, c_to_s, s_to_c); 444 BIO_set_ssl(s_bio, s_ssl, (ctx[3] == NULL) ? BIO_CLOSE : BIO_NOCLOSE); 445 446 c_r = 0; 447 s_r = 1; 448 c_w = 1; 449 s_w = 0; 450 c_want = W_WRITE; 451 s_want = 0; 452 c_write = 1, s_write = 0; 453 454 /* We can always do writes */ 455 for (;;) { 456 do_server = 0; 457 do_client = 0; 458 459 i = (int)BIO_pending(s_bio); 460 if ((i && s_r) || s_w) 461 do_server = 1; 462 463 i = (int)BIO_pending(c_bio); 464 if ((i && c_r) || c_w) 465 do_client = 1; 466 467 if (do_server && verbose) { 468 if (SSL_in_init(s_ssl)) 469 BIO_printf(bio_stdout, "server waiting in SSL_accept - %s\n", 470 SSL_state_string_long(s_ssl)); 471 else if (s_write) 472 BIO_printf(bio_stdout, "server:SSL_write()\n"); 473 else 474 BIO_printf(bio_stdout, "server:SSL_read()\n"); 475 } 476 477 if (do_client && verbose) { 478 if (SSL_in_init(c_ssl)) 479 BIO_printf(bio_stdout, "client waiting in SSL_connect - %s\n", 480 SSL_state_string_long(c_ssl)); 481 else if (c_write) 482 BIO_printf(bio_stdout, "client:SSL_write()\n"); 483 else 484 BIO_printf(bio_stdout, "client:SSL_read()\n"); 485 } 486 487 if (!do_client && !do_server) { 488 BIO_printf(bio_stdout, "ERROR IN STARTUP\n"); 489 break; 490 } 491 if (do_client && !(done & C_DONE)) { 492 if (c_write) { 493 i = BIO_write(c_bio, "hello from client\n", 18); 494 if (i < 0) { 495 c_r = 0; 496 c_w = 0; 497 if (BIO_should_retry(c_bio)) { 498 if (BIO_should_read(c_bio)) 499 c_r = 1; 500 if (BIO_should_write(c_bio)) 501 c_w = 1; 502 } else { 503 BIO_printf(bio_err, "ERROR in CLIENT\n"); 504 ERR_print_errors_fp(stderr); 505 return (1); 506 } 507 } else if (i == 0) { 508 BIO_printf(bio_err, "SSL CLIENT STARTUP FAILED\n"); 509 return (1); 510 } else { 511 /* ok */ 512 c_write = 0; 513 } 514 } else { 515 i = BIO_read(c_bio, cbuf, 100); 516 if (i < 0) { 517 c_r = 0; 518 c_w = 0; 519 if (BIO_should_retry(c_bio)) { 520 if (BIO_should_read(c_bio)) 521 c_r = 1; 522 if (BIO_should_write(c_bio)) 523 c_w = 1; 524 } else { 525 BIO_printf(bio_err, "ERROR in CLIENT\n"); 526 ERR_print_errors_fp(stderr); 527 return (1); 528 } 529 } else if (i == 0) { 530 BIO_printf(bio_err, "SSL CLIENT STARTUP FAILED\n"); 531 return (1); 532 } else { 533 done |= C_DONE; 534#ifdef undef 535 BIO_printf(bio_stdout, "CLIENT:from server:"); 536 BIO_write(bio_stdout, cbuf, i); 537 BIO_flush(bio_stdout); 538#endif 539 } 540 } 541 } 542 543 if (do_server && !(done & S_DONE)) { 544 if (!s_write) { 545 i = BIO_read(s_bio, sbuf, 100); 546 if (i < 0) { 547 s_r = 0; 548 s_w = 0; 549 if (BIO_should_retry(s_bio)) { 550 if (BIO_should_read(s_bio)) 551 s_r = 1; 552 if (BIO_should_write(s_bio)) 553 s_w = 1; 554 } else { 555 BIO_printf(bio_err, "ERROR in SERVER\n"); 556 ERR_print_errors_fp(stderr); 557 return (1); 558 } 559 } else if (i == 0) { 560 BIO_printf(bio_err, "SSL SERVER STARTUP FAILED\n"); 561 return (1); 562 } else { 563 s_write = 1; 564 s_w = 1; 565#ifdef undef 566 BIO_printf(bio_stdout, "SERVER:from client:"); 567 BIO_write(bio_stdout, sbuf, i); 568 BIO_flush(bio_stdout); 569#endif 570 } 571 } else { 572 i = BIO_write(s_bio, "hello from server\n", 18); 573 if (i < 0) { 574 s_r = 0; 575 s_w = 0; 576 if (BIO_should_retry(s_bio)) { 577 if (BIO_should_read(s_bio)) 578 s_r = 1; 579 if (BIO_should_write(s_bio)) 580 s_w = 1; 581 } else { 582 BIO_printf(bio_err, "ERROR in SERVER\n"); 583 ERR_print_errors_fp(stderr); 584 return (1); 585 } 586 } else if (i == 0) { 587 BIO_printf(bio_err, "SSL SERVER STARTUP FAILED\n"); 588 return (1); 589 } else { 590 s_write = 0; 591 s_r = 1; 592 done |= S_DONE; 593 } 594 } 595 } 596 597 if ((done & S_DONE) && (done & C_DONE)) 598 break; 599#if defined(OPENSSL_SYS_NETWARE) 600 ThreadSwitchWithDelay(); 601#endif 602 } 603 604 SSL_set_shutdown(c_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); 605 SSL_set_shutdown(s_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); 606 607#ifdef undef 608 BIO_printf(bio_stdout, "DONE\n"); 609#endif 610 err: 611 /* 612 * We have to set the BIO's to NULL otherwise they will be free()ed 613 * twice. Once when th s_ssl is SSL_free()ed and again when c_ssl is 614 * SSL_free()ed. This is a hack required because s_ssl and c_ssl are 615 * sharing the same BIO structure and SSL_set_bio() and SSL_free() 616 * automatically BIO_free non NULL entries. You should not normally do 617 * this or be required to do this 618 */ 619 620 if (s_ssl != NULL) { 621 s_ssl->rbio = NULL; 622 s_ssl->wbio = NULL; 623 } 624 if (c_ssl != NULL) { 625 c_ssl->rbio = NULL; 626 c_ssl->wbio = NULL; 627 } 628 629 /* The SSL's are optionally freed in the following calls */ 630 if (c_to_s != NULL) 631 BIO_free(c_to_s); 632 if (s_to_c != NULL) 633 BIO_free(s_to_c); 634 635 if (c_bio != NULL) 636 BIO_free(c_bio); 637 if (s_bio != NULL) 638 BIO_free(s_bio); 639 return (0); 640} 641 642int verify_callback(int ok, X509_STORE_CTX *ctx) 643{ 644 char *s, buf[256]; 645 646 if (verbose) { 647 s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), 648 buf, 256); 649 if (s != NULL) { 650 if (ok) 651 BIO_printf(bio_err, "depth=%d %s\n", ctx->error_depth, buf); 652 else 653 BIO_printf(bio_err, "depth=%d error=%d %s\n", 654 ctx->error_depth, ctx->error, buf); 655 } 656 } 657 return (ok); 658} 659 660#define THREAD_STACK_SIZE (16*1024) 661 662#ifdef OPENSSL_SYS_WIN32 663 664static HANDLE *lock_cs; 665 666void thread_setup(void) 667{ 668 int i; 669 670 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); 671 for (i = 0; i < CRYPTO_num_locks(); i++) { 672 lock_cs[i] = CreateMutex(NULL, FALSE, NULL); 673 } 674 675 CRYPTO_set_locking_callback((void (*)(int, int, char *, int)) 676 win32_locking_callback); 677 /* id callback defined */ 678} 679 680void thread_cleanup(void) 681{ 682 int i; 683 684 CRYPTO_set_locking_callback(NULL); 685 for (i = 0; i < CRYPTO_num_locks(); i++) 686 CloseHandle(lock_cs[i]); 687 OPENSSL_free(lock_cs); 688} 689 690void win32_locking_callback(int mode, int type, const char *file, int line) 691{ 692 if (mode & CRYPTO_LOCK) { 693 WaitForSingleObject(lock_cs[type], INFINITE); 694 } else { 695 ReleaseMutex(lock_cs[type]); 696 } 697} 698 699void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 700{ 701 double ret; 702 SSL_CTX *ssl_ctx[2]; 703 DWORD thread_id[MAX_THREAD_NUMBER]; 704 HANDLE thread_handle[MAX_THREAD_NUMBER]; 705 int i; 706 SYSTEMTIME start, end; 707 708 ssl_ctx[0] = s_ctx; 709 ssl_ctx[1] = c_ctx; 710 711 GetSystemTime(&start); 712 for (i = 0; i < thread_number; i++) { 713 thread_handle[i] = CreateThread(NULL, 714 THREAD_STACK_SIZE, 715 (LPTHREAD_START_ROUTINE) ndoit, 716 (void *)ssl_ctx, 0L, &(thread_id[i])); 717 } 718 719 BIO_printf(bio_stdout, "reaping\n"); 720 for (i = 0; i < thread_number; i += 50) { 721 int j; 722 723 j = (thread_number < (i + 50)) ? (thread_number - i) : 50; 724 725 if (WaitForMultipleObjects(j, 726 (CONST HANDLE *) & (thread_handle[i]), 727 TRUE, INFINITE) 728 == WAIT_FAILED) { 729 BIO_printf(bio_err, "WaitForMultipleObjects failed:%d\n", 730 GetLastError()); 731 exit(1); 732 } 733 } 734 GetSystemTime(&end); 735 736 if (start.wDayOfWeek > end.wDayOfWeek) 737 end.wDayOfWeek += 7; 738 ret = (end.wDayOfWeek - start.wDayOfWeek) * 24; 739 740 ret = (ret + end.wHour - start.wHour) * 60; 741 ret = (ret + end.wMinute - start.wMinute) * 60; 742 ret = (ret + end.wSecond - start.wSecond); 743 ret += (end.wMilliseconds - start.wMilliseconds) / 1000.0; 744 745 BIO_printf(bio_stdout, "win32 threads done - %.3f seconds\n", ret); 746} 747 748#endif /* OPENSSL_SYS_WIN32 */ 749 750#ifdef SOLARIS 751 752static mutex_t *lock_cs; 753/* 754 * static rwlock_t *lock_cs; 755 */ 756static long *lock_count; 757 758void thread_setup(void) 759{ 760 int i; 761 762 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t)); 763 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 764 for (i = 0; i < CRYPTO_num_locks(); i++) { 765 lock_count[i] = 0; 766 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */ 767 mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL); 768 } 769 770 CRYPTO_set_id_callback(solaris_thread_id); 771 CRYPTO_set_locking_callback(solaris_locking_callback); 772} 773 774void thread_cleanup(void) 775{ 776 int i; 777 778 CRYPTO_set_locking_callback(NULL); 779 780 BIO_printf(bio_err, "cleanup\n"); 781 782 for (i = 0; i < CRYPTO_num_locks(); i++) { 783 /* rwlock_destroy(&(lock_cs[i])); */ 784 mutex_destroy(&(lock_cs[i])); 785 BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 786 } 787 OPENSSL_free(lock_cs); 788 OPENSSL_free(lock_count); 789 790 BIO_printf(bio_err, "done cleanup\n"); 791 792} 793 794void solaris_locking_callback(int mode, int type, const char *file, int line) 795{ 796# ifdef undef 797 BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n", 798 CRYPTO_thread_id(), 799 (mode & CRYPTO_LOCK) ? "l" : "u", 800 (type & CRYPTO_READ) ? "r" : "w", file, line); 801# endif 802 803 /*- 804 if (CRYPTO_LOCK_SSL_CERT == type) 805 BIO_printf(bio_err,"(t,m,f,l) %ld %d %s %d\n", 806 CRYPTO_thread_id(), 807 mode,file,line); 808 */ 809 if (mode & CRYPTO_LOCK) { 810 /*- 811 if (mode & CRYPTO_READ) 812 rw_rdlock(&(lock_cs[type])); 813 else 814 rw_wrlock(&(lock_cs[type])); */ 815 816 mutex_lock(&(lock_cs[type])); 817 lock_count[type]++; 818 } else { 819/* rw_unlock(&(lock_cs[type])); */ 820 mutex_unlock(&(lock_cs[type])); 821 } 822} 823 824void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 825{ 826 SSL_CTX *ssl_ctx[2]; 827 thread_t thread_ctx[MAX_THREAD_NUMBER]; 828 int i; 829 830 ssl_ctx[0] = s_ctx; 831 ssl_ctx[1] = c_ctx; 832 833 thr_setconcurrency(thread_number); 834 for (i = 0; i < thread_number; i++) { 835 thr_create(NULL, THREAD_STACK_SIZE, 836 (void *(*)())ndoit, (void *)ssl_ctx, 0L, &(thread_ctx[i])); 837 } 838 839 BIO_printf(bio_stdout, "reaping\n"); 840 for (i = 0; i < thread_number; i++) { 841 thr_join(thread_ctx[i], NULL, NULL); 842 } 843 844#if 0 /* We can't currently find out the reference amount */ 845 BIO_printf(bio_stdout, "solaris threads done (%d,%d)\n", 846 s_ctx->references, c_ctx->references); 847#else 848 BIO_printf(bio_stdout, "solaris threads done\n"); 849#endif 850} 851 852void solaris_thread_id(CRYPTO_THREADID *tid) 853{ 854 CRYPTO_THREADID_set_numeric((unsigned long)thr_self()); 855} 856#endif /* SOLARIS */ 857 858#ifdef IRIX 859 860static usptr_t *arena; 861static usema_t **lock_cs; 862 863void thread_setup(void) 864{ 865 int i; 866 char filename[20]; 867 868 strcpy(filename, "/tmp/mttest.XXXXXX"); 869 mktemp(filename); 870 871 usconfig(CONF_STHREADIOOFF); 872 usconfig(CONF_STHREADMALLOCOFF); 873 usconfig(CONF_INITUSERS, 100); 874 usconfig(CONF_LOCKTYPE, US_DEBUGPLUS); 875 arena = usinit(filename); 876 unlink(filename); 877 878 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *)); 879 for (i = 0; i < CRYPTO_num_locks(); i++) { 880 lock_cs[i] = usnewsema(arena, 1); 881 } 882 883 CRYPTO_set_id_callback(irix_thread_id); 884 CRYPTO_set_locking_callback(irix_locking_callback); 885} 886 887void thread_cleanup(void) 888{ 889 int i; 890 891 CRYPTO_set_locking_callback(NULL); 892 for (i = 0; i < CRYPTO_num_locks(); i++) { 893 char buf[10]; 894 895 sprintf(buf, "%2d:", i); 896 usdumpsema(lock_cs[i], stdout, buf); 897 usfreesema(lock_cs[i], arena); 898 } 899 OPENSSL_free(lock_cs); 900} 901 902void irix_locking_callback(int mode, int type, const char *file, int line) 903{ 904 if (mode & CRYPTO_LOCK) { 905 BIO_printf(bio_stdout, "lock %d\n", type); 906 uspsema(lock_cs[type]); 907 } else { 908 BIO_printf(bio_stdout, "unlock %d\n", type); 909 usvsema(lock_cs[type]); 910 } 911} 912 913void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 914{ 915 SSL_CTX *ssl_ctx[2]; 916 int thread_ctx[MAX_THREAD_NUMBER]; 917 int i; 918 919 ssl_ctx[0] = s_ctx; 920 ssl_ctx[1] = c_ctx; 921 922 for (i = 0; i < thread_number; i++) { 923 thread_ctx[i] = sproc((void (*)())ndoit, 924 PR_SADDR | PR_SFDS, (void *)ssl_ctx); 925 } 926 927 BIO_printf(bio_stdout, "reaping\n"); 928 for (i = 0; i < thread_number; i++) { 929 wait(NULL); 930 } 931 932#if 0 /* We can't currently find out the reference amount */ 933 BIO_printf(bio_stdout, "irix threads done (%d,%d)\n", 934 s_ctx->references, c_ctx->references); 935#else 936 BIO_printf(bio_stdout, "irix threads done\n"); 937#endif 938} 939 940unsigned long irix_thread_id(void) 941{ 942 CRYPTO_THREADID_set_numeric((unsigned long)getpid()); 943} 944#endif /* IRIX */ 945 946#ifdef PTHREADS 947 948static pthread_mutex_t *lock_cs; 949static long *lock_count; 950 951void thread_setup(void) 952{ 953 int i; 954 955 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); 956 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 957 for (i = 0; i < CRYPTO_num_locks(); i++) { 958 lock_count[i] = 0; 959 pthread_mutex_init(&(lock_cs[i]), NULL); 960 } 961 962 CRYPTO_THREADID_set_callback(pthreads_thread_id); 963 CRYPTO_set_locking_callback(pthreads_locking_callback); 964} 965 966void thread_cleanup(void) 967{ 968 int i; 969 970 CRYPTO_set_locking_callback(NULL); 971 BIO_printf(bio_err, "cleanup\n"); 972 for (i = 0; i < CRYPTO_num_locks(); i++) { 973 pthread_mutex_destroy(&(lock_cs[i])); 974 BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 975 } 976 OPENSSL_free(lock_cs); 977 OPENSSL_free(lock_count); 978 979 BIO_printf(bio_err, "done cleanup\n"); 980} 981 982void pthreads_locking_callback(int mode, int type, const char *file, int line) 983{ 984# ifdef undef 985 BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n", 986 CRYPTO_thread_id(), 987 (mode & CRYPTO_LOCK) ? "l" : "u", 988 (type & CRYPTO_READ) ? "r" : "w", file, line); 989# endif 990/*- 991 if (CRYPTO_LOCK_SSL_CERT == type) 992 BIO_printf(bio_err,"(t,m,f,l) %ld %d %s %d\n", 993 CRYPTO_thread_id(), 994 mode,file,line); 995*/ 996 if (mode & CRYPTO_LOCK) { 997 pthread_mutex_lock(&(lock_cs[type])); 998 lock_count[type]++; 999 } else { 1000 pthread_mutex_unlock(&(lock_cs[type])); 1001 } 1002} 1003 1004void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 1005{ 1006 SSL_CTX *ssl_ctx[2]; 1007 pthread_t thread_ctx[MAX_THREAD_NUMBER]; 1008 int i; 1009 1010 ssl_ctx[0] = s_ctx; 1011 ssl_ctx[1] = c_ctx; 1012 1013 /* 1014 * thr_setconcurrency(thread_number); 1015 */ 1016 for (i = 0; i < thread_number; i++) { 1017 pthread_create(&(thread_ctx[i]), NULL, 1018 (void *(*)())ndoit, (void *)ssl_ctx); 1019 } 1020 1021 BIO_printf(bio_stdout, "reaping\n"); 1022 for (i = 0; i < thread_number; i++) { 1023 pthread_join(thread_ctx[i], NULL); 1024 } 1025 1026#if 0 /* We can't currently find out the reference amount */ 1027 BIO_printf(bio_stdout, "pthreads threads done (%d,%d)\n", 1028 s_ctx->references, c_ctx->references); 1029#else 1030 BIO_printf(bio_stdout, "pthreads threads done\n"); 1031#endif 1032} 1033 1034void pthreads_thread_id(CRYPTO_THREADID *tid) 1035{ 1036 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); 1037} 1038 1039#endif /* PTHREADS */ 1040 1041#ifdef OPENSSL_SYS_NETWARE 1042 1043void thread_setup(void) 1044{ 1045 int i; 1046 1047 lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex)); 1048 lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 1049 for (i = 0; i < CRYPTO_num_locks(); i++) { 1050 lock_count[i] = 0; 1051 lock_cs[i] = MPKMutexAlloc("OpenSSL mutex"); 1052 } 1053 1054 ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0); 1055 1056 CRYPTO_set_id_callback(netware_thread_id); 1057 CRYPTO_set_locking_callback(netware_locking_callback); 1058} 1059 1060void thread_cleanup(void) 1061{ 1062 int i; 1063 1064 CRYPTO_set_locking_callback(NULL); 1065 1066 BIO_printf(bio_stdout, "thread_cleanup\n"); 1067 1068 for (i = 0; i < CRYPTO_num_locks(); i++) { 1069 MPKMutexFree(lock_cs[i]); 1070 BIO_printf(bio_stdout, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 1071 } 1072 OPENSSL_free(lock_cs); 1073 OPENSSL_free(lock_count); 1074 1075 MPKSemaphoreFree(ThreadSem); 1076 1077 BIO_printf(bio_stdout, "done cleanup\n"); 1078} 1079 1080void netware_locking_callback(int mode, int type, const char *file, int line) 1081{ 1082 if (mode & CRYPTO_LOCK) { 1083 MPKMutexLock(lock_cs[type]); 1084 lock_count[type]++; 1085 } else 1086 MPKMutexUnlock(lock_cs[type]); 1087} 1088 1089void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 1090{ 1091 SSL_CTX *ssl_ctx[2]; 1092 int i; 1093 ssl_ctx[0] = s_ctx; 1094 ssl_ctx[1] = c_ctx; 1095 1096 for (i = 0; i < thread_number; i++) { 1097 BeginThread((void (*)(void *))ndoit, NULL, THREAD_STACK_SIZE, 1098 (void *)ssl_ctx); 1099 ThreadSwitchWithDelay(); 1100 } 1101 1102 BIO_printf(bio_stdout, "reaping\n"); 1103 1104 /* loop until all threads have signaled the semaphore */ 1105 for (i = 0; i < thread_number; i++) { 1106 MPKSemaphoreWait(ThreadSem); 1107 } 1108#if 0 /* We can't currently find out the reference amount */ 1109 BIO_printf(bio_stdout, "netware threads done (%d,%d)\n", 1110 s_ctx->references, c_ctx->references); 1111#else 1112 BIO_printf(bio_stdout, "netware threads done\n"); 1113#endif 1114} 1115 1116unsigned long netware_thread_id(void) 1117{ 1118 CRYPTO_THREADID_set_numeric((unsigned long)GetThreadID()); 1119} 1120#endif /* NETWARE */ 1121 1122#ifdef BEOS_THREADS 1123 1124# include <Locker.h> 1125 1126static BLocker **lock_cs; 1127static long *lock_count; 1128 1129void thread_setup(void) 1130{ 1131 int i; 1132 1133 lock_cs = 1134 (BLocker **) OPENSSL_malloc(CRYPTO_num_locks() * sizeof(BLocker *)); 1135 lock_count = (long *)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 1136 for (i = 0; i < CRYPTO_num_locks(); i++) { 1137 lock_count[i] = 0; 1138 lock_cs[i] = new BLocker(CRYPTO_get_lock_name(i)); 1139 } 1140 1141 CRYPTO_set_id_callback((unsigned long (*)())beos_thread_id); 1142 CRYPTO_set_locking_callback(beos_locking_callback); 1143} 1144 1145void thread_cleanup(void) 1146{ 1147 int i; 1148 1149 CRYPTO_set_locking_callback(NULL); 1150 BIO_printf(bio_err, "cleanup\n"); 1151 for (i = 0; i < CRYPTO_num_locks(); i++) { 1152 delete lock_cs[i]; 1153 BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 1154 } 1155 OPENSSL_free(lock_cs); 1156 OPENSSL_free(lock_count); 1157 1158 BIO_printf(bio_err, "done cleanup\n"); 1159} 1160 1161void beos_locking_callback(int mode, int type, const char *file, int line) 1162{ 1163# if 0 1164 BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n", 1165 CRYPTO_thread_id(), 1166 (mode & CRYPTO_LOCK) ? "l" : "u", 1167 (type & CRYPTO_READ) ? "r" : "w", file, line); 1168# endif 1169 if (mode & CRYPTO_LOCK) { 1170 lock_cs[type]->Lock(); 1171 lock_count[type]++; 1172 } else { 1173 lock_cs[type]->Unlock(); 1174 } 1175} 1176 1177void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 1178{ 1179 SSL_CTX *ssl_ctx[2]; 1180 thread_id thread_ctx[MAX_THREAD_NUMBER]; 1181 int i; 1182 1183 ssl_ctx[0] = s_ctx; 1184 ssl_ctx[1] = c_ctx; 1185 1186 for (i = 0; i < thread_number; i++) { 1187 thread_ctx[i] = spawn_thread((thread_func) ndoit, 1188 NULL, B_NORMAL_PRIORITY, 1189 (void *)ssl_ctx); 1190 resume_thread(thread_ctx[i]); 1191 } 1192 1193 BIO_printf(bio_stdout, "waiting...\n"); 1194 for (i = 0; i < thread_number; i++) { 1195 status_t result; 1196 wait_for_thread(thread_ctx[i], &result); 1197 } 1198 1199 BIO_printf(bio_stdout, "beos threads done (%d,%d)\n", 1200 s_ctx->references, c_ctx->references); 1201} 1202 1203unsigned long beos_thread_id(void) 1204{ 1205 unsigned long ret; 1206 1207 ret = (unsigned long)find_thread(NULL); 1208 return (ret); 1209} 1210 1211#endif /* BEOS_THREADS */ 1212