155714Skris/* crypto/threads/mttest.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdlib.h> 6055714Skris#include <string.h> 6155714Skris#include <errno.h> 6255714Skris#ifdef LINUX 63296341Sdelphij# include <typedefs.h> 6455714Skris#endif 65109998Smarkm#ifdef OPENSSL_SYS_WIN32 66296341Sdelphij# include <windows.h> 6755714Skris#endif 6855714Skris#ifdef SOLARIS 69296341Sdelphij# include <synch.h> 70296341Sdelphij# include <thread.h> 7155714Skris#endif 7255714Skris#ifdef IRIX 73296341Sdelphij# include <ulocks.h> 74296341Sdelphij# include <sys/prctl.h> 7555714Skris#endif 7659191Skris#ifdef PTHREADS 77296341Sdelphij# include <pthread.h> 7859191Skris#endif 79160814Ssimon#ifdef OPENSSL_SYS_NETWARE 80296341Sdelphij# if !defined __int64 81160814Ssimon# define __int64 long long 82296341Sdelphij# endif 83296341Sdelphij# include <nwmpk.h> 84160814Ssimon#endif 8555714Skris#include <openssl/lhash.h> 8655714Skris#include <openssl/crypto.h> 8755714Skris#include <openssl/buffer.h> 8855714Skris#include <openssl/x509.h> 8955714Skris#include <openssl/ssl.h> 9055714Skris#include <openssl/err.h> 9159191Skris#include <openssl/rand.h> 9255714Skris 93160814Ssimon#ifdef OPENSSL_SYS_NETWARE 94296341Sdelphij# define TEST_SERVER_CERT "/openssl/apps/server.pem" 95296341Sdelphij# define TEST_CLIENT_CERT "/openssl/apps/client.pem" 96160814Ssimon#else 97296341Sdelphij# define TEST_SERVER_CERT "../../apps/server.pem" 98296341Sdelphij# define TEST_CLIENT_CERT "../../apps/client.pem" 99160814Ssimon#endif 10055714Skris 101296341Sdelphij#define MAX_THREAD_NUMBER 100 10255714Skris 103296341Sdelphijint verify_callback(int ok, X509_STORE_CTX *xs); 10455714Skrisvoid thread_setup(void); 10555714Skrisvoid thread_cleanup(void); 106296341Sdelphijvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx); 10755714Skris 108296341Sdelphijvoid irix_locking_callback(int mode, int type, const char *file, int line); 109296341Sdelphijvoid solaris_locking_callback(int mode, int type, const char *file, int line); 110296341Sdelphijvoid win32_locking_callback(int mode, int type, const char *file, int line); 111296341Sdelphijvoid pthreads_locking_callback(int mode, int type, const char *file, int line); 112296341Sdelphijvoid netware_locking_callback(int mode, int type, const char *file, int line); 113296341Sdelphijvoid beos_locking_callback(int mode, int type, const char *file, int line); 11455714Skris 115296341Sdelphijvoid irix_thread_id(CRYPTO_THREADID *tid); 116296341Sdelphijvoid solaris_thread_id(CRYPTO_THREADID *tid); 117296341Sdelphijvoid pthreads_thread_id(CRYPTO_THREADID *tid); 118296341Sdelphijvoid netware_thread_id(CRYPTO_THREADID *tid); 119296341Sdelphijvoid beos_thread_id(CRYPTO_THREADID *tid); 12055714Skris 121160814Ssimon#if defined(OPENSSL_SYS_NETWARE) 122160814Ssimonstatic MPKMutex *lock_cs; 123160814Ssimonstatic MPKSema ThreadSem; 124160814Ssimonstatic long *lock_count; 125160814Ssimon#endif 126160814Ssimon 127296341SdelphijBIO *bio_err = NULL; 128296341SdelphijBIO *bio_stdout = NULL; 12955714Skris 130296341Sdelphijstatic char *cipher = NULL; 131296341Sdelphijint verbose = 0; 13255714Skris#ifdef FIONBIO 133296341Sdelphijstatic int s_nbio = 0; 13455714Skris#endif 13555714Skris 136296341Sdelphijint thread_number = 10; 137296341Sdelphijint number_of_loops = 10; 138296341Sdelphijint reconnect = 0; 139296341Sdelphijint cache_stats = 0; 14055714Skris 141296341Sdelphijstatic const char rnd_seed[] = 142296341Sdelphij "string to make the random number generator think it has entropy"; 14359191Skris 14455714Skrisint doit(char *ctx[4]); 145296341Sdelphijstatic void print_stats(BIO *bio, SSL_CTX *ctx) 14655714Skris{ 147296341Sdelphij BIO_printf(bio, "%4ld items in the session cache\n", 148296341Sdelphij SSL_CTX_sess_number(ctx)); 149296341Sdelphij BIO_printf(bio, "%4d client connects (SSL_connect())\n", 150296341Sdelphij SSL_CTX_sess_connect(ctx)); 151296341Sdelphij BIO_printf(bio, "%4d client connects that finished\n", 152296341Sdelphij SSL_CTX_sess_connect_good(ctx)); 153296341Sdelphij BIO_printf(bio, "%4d server connects (SSL_accept())\n", 154296341Sdelphij SSL_CTX_sess_accept(ctx)); 155296341Sdelphij BIO_printf(bio, "%4d server connects that finished\n", 156296341Sdelphij SSL_CTX_sess_accept_good(ctx)); 157296341Sdelphij BIO_printf(bio, "%4d session cache hits\n", SSL_CTX_sess_hits(ctx)); 158296341Sdelphij BIO_printf(bio, "%4d session cache misses\n", SSL_CTX_sess_misses(ctx)); 159296341Sdelphij BIO_printf(bio, "%4d session cache timeouts\n", SSL_CTX_sess_timeouts(ctx)); 160296341Sdelphij} 16155714Skris 16255714Skrisstatic void sv_usage(void) 163296341Sdelphij{ 164296341Sdelphij BIO_printf(bio_err, "usage: ssltest [args ...]\n"); 165296341Sdelphij BIO_printf(bio_err, "\n"); 166296341Sdelphij BIO_printf(bio_err, " -server_auth - check server certificate\n"); 167296341Sdelphij BIO_printf(bio_err, " -client_auth - do client authentication\n"); 168296341Sdelphij BIO_printf(bio_err, " -v - more output\n"); 169296341Sdelphij BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n"); 170296341Sdelphij BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n"); 171296341Sdelphij BIO_printf(bio_err, " -threads arg - number of threads\n"); 172296341Sdelphij BIO_printf(bio_err, " -loops arg - number of 'connections', per thread\n"); 173296341Sdelphij BIO_printf(bio_err, " -reconnect - reuse session-id's\n"); 174296341Sdelphij BIO_printf(bio_err, " -stats - server session-id cache stats\n"); 175296341Sdelphij BIO_printf(bio_err, " -cert arg - server certificate/key\n"); 176296341Sdelphij BIO_printf(bio_err, " -ccert arg - client certificate/key\n"); 177296341Sdelphij BIO_printf(bio_err, " -ssl3 - just SSLv3n\n"); 178296341Sdelphij} 17955714Skris 18055714Skrisint main(int argc, char *argv[]) 181296341Sdelphij{ 182296341Sdelphij char *CApath = NULL, *CAfile = NULL; 183296341Sdelphij int badop = 0; 184296341Sdelphij int ret = 1; 185296341Sdelphij int client_auth = 0; 186296341Sdelphij int server_auth = 0; 187296341Sdelphij SSL_CTX *s_ctx = NULL; 188296341Sdelphij SSL_CTX *c_ctx = NULL; 189296341Sdelphij char *scert = TEST_SERVER_CERT; 190296341Sdelphij char *ccert = TEST_CLIENT_CERT; 191296341Sdelphij const SSL_METHOD *ssl_method = SSLv23_method(); 19255714Skris 193296341Sdelphij RAND_seed(rnd_seed, sizeof rnd_seed); 19459191Skris 195296341Sdelphij if (bio_err == NULL) 196296341Sdelphij bio_err = BIO_new_fd(2, BIO_NOCLOSE); 197296341Sdelphij if (bio_stdout == NULL) 198296341Sdelphij bio_stdout = BIO_new_fd(1, BIO_NOCLOSE); 199296341Sdelphij argc--; 200296341Sdelphij argv++; 20155714Skris 202296341Sdelphij while (argc >= 1) { 203296341Sdelphij if (strcmp(*argv, "-server_auth") == 0) 204296341Sdelphij server_auth = 1; 205296341Sdelphij else if (strcmp(*argv, "-client_auth") == 0) 206296341Sdelphij client_auth = 1; 207296341Sdelphij else if (strcmp(*argv, "-reconnect") == 0) 208296341Sdelphij reconnect = 1; 209296341Sdelphij else if (strcmp(*argv, "-stats") == 0) 210296341Sdelphij cache_stats = 1; 211296341Sdelphij else if (strcmp(*argv, "-ssl3") == 0) 212296341Sdelphij ssl_method = SSLv3_method(); 213296341Sdelphij else if (strcmp(*argv, "-ssl2") == 0) 214296341Sdelphij ssl_method = SSLv2_method(); 215296341Sdelphij else if (strcmp(*argv, "-CApath") == 0) { 216296341Sdelphij if (--argc < 1) 217296341Sdelphij goto bad; 218296341Sdelphij CApath = *(++argv); 219296341Sdelphij } else if (strcmp(*argv, "-CAfile") == 0) { 220296341Sdelphij if (--argc < 1) 221296341Sdelphij goto bad; 222296341Sdelphij CAfile = *(++argv); 223296341Sdelphij } else if (strcmp(*argv, "-cert") == 0) { 224296341Sdelphij if (--argc < 1) 225296341Sdelphij goto bad; 226296341Sdelphij scert = *(++argv); 227296341Sdelphij } else if (strcmp(*argv, "-ccert") == 0) { 228296341Sdelphij if (--argc < 1) 229296341Sdelphij goto bad; 230296341Sdelphij ccert = *(++argv); 231296341Sdelphij } else if (strcmp(*argv, "-threads") == 0) { 232296341Sdelphij if (--argc < 1) 233296341Sdelphij goto bad; 234296341Sdelphij thread_number = atoi(*(++argv)); 235296341Sdelphij if (thread_number == 0) 236296341Sdelphij thread_number = 1; 237296341Sdelphij if (thread_number > MAX_THREAD_NUMBER) 238296341Sdelphij thread_number = MAX_THREAD_NUMBER; 239296341Sdelphij } else if (strcmp(*argv, "-loops") == 0) { 240296341Sdelphij if (--argc < 1) 241296341Sdelphij goto bad; 242296341Sdelphij number_of_loops = atoi(*(++argv)); 243296341Sdelphij if (number_of_loops == 0) 244296341Sdelphij number_of_loops = 1; 245296341Sdelphij } else { 246296341Sdelphij BIO_printf(bio_err, "unknown option %s\n", *argv); 247296341Sdelphij badop = 1; 248296341Sdelphij break; 249296341Sdelphij } 250296341Sdelphij argc--; 251296341Sdelphij argv++; 252296341Sdelphij } 253296341Sdelphij if (badop) { 254296341Sdelphij bad: 255296341Sdelphij sv_usage(); 256296341Sdelphij goto end; 257296341Sdelphij } 25855714Skris 259296341Sdelphij if (cipher == NULL && OPENSSL_issetugid() == 0) 260296341Sdelphij cipher = getenv("SSL_CIPHER"); 26155714Skris 262296341Sdelphij SSL_load_error_strings(); 263296341Sdelphij OpenSSL_add_ssl_algorithms(); 26455714Skris 265296341Sdelphij c_ctx = SSL_CTX_new(ssl_method); 266296341Sdelphij s_ctx = SSL_CTX_new(ssl_method); 267296341Sdelphij if ((c_ctx == NULL) || (s_ctx == NULL)) { 268296341Sdelphij ERR_print_errors(bio_err); 269296341Sdelphij goto end; 270296341Sdelphij } 27155714Skris 272296341Sdelphij SSL_CTX_set_session_cache_mode(s_ctx, 273296341Sdelphij SSL_SESS_CACHE_NO_AUTO_CLEAR | 274296341Sdelphij SSL_SESS_CACHE_SERVER); 275296341Sdelphij SSL_CTX_set_session_cache_mode(c_ctx, 276296341Sdelphij SSL_SESS_CACHE_NO_AUTO_CLEAR | 277296341Sdelphij SSL_SESS_CACHE_SERVER); 27855714Skris 279296341Sdelphij if (!SSL_CTX_use_certificate_file(s_ctx, scert, SSL_FILETYPE_PEM)) { 280296341Sdelphij BIO_printf(bio_err, "SSL_CTX_use_certificate_file (%s)\n", scert); 281296341Sdelphij ERR_print_errors(bio_err); 282296341Sdelphij goto end; 283296341Sdelphij } else 284296341Sdelphij if (!SSL_CTX_use_RSAPrivateKey_file(s_ctx, scert, SSL_FILETYPE_PEM)) { 285296341Sdelphij BIO_printf(bio_err, "SSL_CTX_use_RSAPrivateKey_file (%s)\n", scert); 286296341Sdelphij ERR_print_errors(bio_err); 287296341Sdelphij goto end; 288296341Sdelphij } 28955714Skris 290296341Sdelphij if (client_auth) { 291296341Sdelphij SSL_CTX_use_certificate_file(c_ctx, ccert, SSL_FILETYPE_PEM); 292296341Sdelphij SSL_CTX_use_RSAPrivateKey_file(c_ctx, ccert, SSL_FILETYPE_PEM); 293296341Sdelphij } 29455714Skris 295296341Sdelphij if ((!SSL_CTX_load_verify_locations(s_ctx, CAfile, CApath)) || 296296341Sdelphij (!SSL_CTX_set_default_verify_paths(s_ctx)) || 297296341Sdelphij (!SSL_CTX_load_verify_locations(c_ctx, CAfile, CApath)) || 298296341Sdelphij (!SSL_CTX_set_default_verify_paths(c_ctx))) { 299296341Sdelphij BIO_printf(bio_err, "SSL_load_verify_locations\n"); 300296341Sdelphij ERR_print_errors(bio_err); 301296341Sdelphij goto end; 302296341Sdelphij } 30355714Skris 304296341Sdelphij if (client_auth) { 305296341Sdelphij BIO_printf(bio_err, "client authentication\n"); 306296341Sdelphij SSL_CTX_set_verify(s_ctx, 307296341Sdelphij SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 308296341Sdelphij verify_callback); 309296341Sdelphij } 310296341Sdelphij if (server_auth) { 311296341Sdelphij BIO_printf(bio_err, "server authentication\n"); 312296341Sdelphij SSL_CTX_set_verify(c_ctx, SSL_VERIFY_PEER, verify_callback); 313296341Sdelphij } 31455714Skris 315296341Sdelphij thread_setup(); 316296341Sdelphij do_threads(s_ctx, c_ctx); 317296341Sdelphij thread_cleanup(); 318296341Sdelphij end: 31955714Skris 320296341Sdelphij if (c_ctx != NULL) { 321296341Sdelphij BIO_printf(bio_err, "Client SSL_CTX stats then free it\n"); 322296341Sdelphij print_stats(bio_err, c_ctx); 323296341Sdelphij SSL_CTX_free(c_ctx); 324296341Sdelphij } 325296341Sdelphij if (s_ctx != NULL) { 326296341Sdelphij BIO_printf(bio_err, "Server SSL_CTX stats then free it\n"); 327296341Sdelphij print_stats(bio_err, s_ctx); 328296341Sdelphij if (cache_stats) { 329296341Sdelphij BIO_printf(bio_err, "-----\n"); 330296341Sdelphij lh_SSL_SESSION_stats_bio(SSL_CTX_sessions(s_ctx), bio_err); 331296341Sdelphij BIO_printf(bio_err, "-----\n"); 332296341Sdelphij /*- lh_SSL_SESSION_node_stats_bio(SSL_CTX_sessions(s_ctx),bio_err); 333296341Sdelphij BIO_printf(bio_err,"-----\n"); */ 334296341Sdelphij lh_SSL_SESSION_node_usage_stats_bio(SSL_CTX_sessions(s_ctx), bio_err); 335296341Sdelphij BIO_printf(bio_err, "-----\n"); 336296341Sdelphij } 337296341Sdelphij SSL_CTX_free(s_ctx); 338296341Sdelphij BIO_printf(bio_err, "done free\n"); 339296341Sdelphij } 340296341Sdelphij exit(ret); 341296341Sdelphij return (0); 342296341Sdelphij} 34355714Skris 344296341Sdelphij#define W_READ 1 345296341Sdelphij#define W_WRITE 2 346296341Sdelphij#define C_DONE 1 347296341Sdelphij#define S_DONE 2 348296341Sdelphij 34955714Skrisint ndoit(SSL_CTX *ssl_ctx[2]) 350296341Sdelphij{ 351296341Sdelphij int i; 352296341Sdelphij int ret; 353296341Sdelphij char *ctx[4]; 354296341Sdelphij CRYPTO_THREADID thread_id; 35555714Skris 356296341Sdelphij ctx[0] = (char *)ssl_ctx[0]; 357296341Sdelphij ctx[1] = (char *)ssl_ctx[1]; 35855714Skris 359296341Sdelphij if (reconnect) { 360296341Sdelphij ctx[2] = (char *)SSL_new(ssl_ctx[0]); 361296341Sdelphij ctx[3] = (char *)SSL_new(ssl_ctx[1]); 362296341Sdelphij } else { 363296341Sdelphij ctx[2] = NULL; 364296341Sdelphij ctx[3] = NULL; 365296341Sdelphij } 36655714Skris 367296341Sdelphij CRYPTO_THREADID_current(&thread_id); 368296341Sdelphij BIO_printf(bio_stdout, "started thread %lu\n", 369296341Sdelphij CRYPTO_THREADID_hash(&thread_id)); 370296341Sdelphij for (i = 0; i < number_of_loops; i++) { 371296341Sdelphij/*- BIO_printf(bio_err,"%4d %2d ctx->ref (%3d,%3d)\n", 372296341Sdelphij CRYPTO_THREADID_hash(&thread_id),i, 373296341Sdelphij ssl_ctx[0]->references, 374296341Sdelphij ssl_ctx[1]->references); */ 375296341Sdelphij/* pthread_delay_np(&tm); */ 37655714Skris 377296341Sdelphij ret = doit(ctx); 378296341Sdelphij if (ret != 0) { 379296341Sdelphij BIO_printf(bio_stdout, "error[%d] %lu - %d\n", 380296341Sdelphij i, CRYPTO_THREADID_hash(&thread_id), ret); 381296341Sdelphij return (ret); 382296341Sdelphij } 383296341Sdelphij } 384296341Sdelphij BIO_printf(bio_stdout, "DONE %lu\n", CRYPTO_THREADID_hash(&thread_id)); 385296341Sdelphij if (reconnect) { 386296341Sdelphij SSL_free((SSL *)ctx[2]); 387296341Sdelphij SSL_free((SSL *)ctx[3]); 388296341Sdelphij } 389296341Sdelphij#ifdef OPENSSL_SYS_NETWARE 390296341Sdelphij MPKSemaphoreSignal(ThreadSem); 391296341Sdelphij#endif 392296341Sdelphij return (0); 393296341Sdelphij} 39455714Skris 39555714Skrisint doit(char *ctx[4]) 396296341Sdelphij{ 397296341Sdelphij SSL_CTX *s_ctx, *c_ctx; 398296341Sdelphij static char cbuf[200], sbuf[200]; 399296341Sdelphij SSL *c_ssl = NULL; 400296341Sdelphij SSL *s_ssl = NULL; 401296341Sdelphij BIO *c_to_s = NULL; 402296341Sdelphij BIO *s_to_c = NULL; 403296341Sdelphij BIO *c_bio = NULL; 404296341Sdelphij BIO *s_bio = NULL; 405296341Sdelphij int c_r, c_w, s_r, s_w; 406296341Sdelphij int c_want, s_want; 407296341Sdelphij int i; 408296341Sdelphij int done = 0; 409296341Sdelphij int c_write, s_write; 410296341Sdelphij int do_server = 0, do_client = 0; 41155714Skris 412296341Sdelphij s_ctx = (SSL_CTX *)ctx[0]; 413296341Sdelphij c_ctx = (SSL_CTX *)ctx[1]; 41455714Skris 415296341Sdelphij if (ctx[2] != NULL) 416296341Sdelphij s_ssl = (SSL *)ctx[2]; 417296341Sdelphij else 418296341Sdelphij s_ssl = SSL_new(s_ctx); 41955714Skris 420296341Sdelphij if (ctx[3] != NULL) 421296341Sdelphij c_ssl = (SSL *)ctx[3]; 422296341Sdelphij else 423296341Sdelphij c_ssl = SSL_new(c_ctx); 42455714Skris 425296341Sdelphij if ((s_ssl == NULL) || (c_ssl == NULL)) 426296341Sdelphij goto err; 42755714Skris 428296341Sdelphij c_to_s = BIO_new(BIO_s_mem()); 429296341Sdelphij s_to_c = BIO_new(BIO_s_mem()); 430296341Sdelphij if ((s_to_c == NULL) || (c_to_s == NULL)) 431296341Sdelphij goto err; 43255714Skris 433296341Sdelphij c_bio = BIO_new(BIO_f_ssl()); 434296341Sdelphij s_bio = BIO_new(BIO_f_ssl()); 435296341Sdelphij if ((c_bio == NULL) || (s_bio == NULL)) 436296341Sdelphij goto err; 43755714Skris 438296341Sdelphij SSL_set_connect_state(c_ssl); 439296341Sdelphij SSL_set_bio(c_ssl, s_to_c, c_to_s); 440296341Sdelphij BIO_set_ssl(c_bio, c_ssl, (ctx[2] == NULL) ? BIO_CLOSE : BIO_NOCLOSE); 44155714Skris 442296341Sdelphij SSL_set_accept_state(s_ssl); 443296341Sdelphij SSL_set_bio(s_ssl, c_to_s, s_to_c); 444296341Sdelphij BIO_set_ssl(s_bio, s_ssl, (ctx[3] == NULL) ? BIO_CLOSE : BIO_NOCLOSE); 44555714Skris 446296341Sdelphij c_r = 0; 447296341Sdelphij s_r = 1; 448296341Sdelphij c_w = 1; 449296341Sdelphij s_w = 0; 450296341Sdelphij c_want = W_WRITE; 451296341Sdelphij s_want = 0; 452296341Sdelphij c_write = 1, s_write = 0; 45355714Skris 454296341Sdelphij /* We can always do writes */ 455296341Sdelphij for (;;) { 456296341Sdelphij do_server = 0; 457296341Sdelphij do_client = 0; 45855714Skris 459296341Sdelphij i = (int)BIO_pending(s_bio); 460296341Sdelphij if ((i && s_r) || s_w) 461296341Sdelphij do_server = 1; 46255714Skris 463296341Sdelphij i = (int)BIO_pending(c_bio); 464296341Sdelphij if ((i && c_r) || c_w) 465296341Sdelphij do_client = 1; 46655714Skris 467296341Sdelphij if (do_server && verbose) { 468296341Sdelphij if (SSL_in_init(s_ssl)) 469296341Sdelphij BIO_printf(bio_stdout, "server waiting in SSL_accept - %s\n", 470296341Sdelphij SSL_state_string_long(s_ssl)); 471296341Sdelphij else if (s_write) 472296341Sdelphij BIO_printf(bio_stdout, "server:SSL_write()\n"); 473296341Sdelphij else 474296341Sdelphij BIO_printf(bio_stdout, "server:SSL_read()\n"); 475296341Sdelphij } 47655714Skris 477296341Sdelphij if (do_client && verbose) { 478296341Sdelphij if (SSL_in_init(c_ssl)) 479296341Sdelphij BIO_printf(bio_stdout, "client waiting in SSL_connect - %s\n", 480296341Sdelphij SSL_state_string_long(c_ssl)); 481296341Sdelphij else if (c_write) 482296341Sdelphij BIO_printf(bio_stdout, "client:SSL_write()\n"); 483296341Sdelphij else 484296341Sdelphij BIO_printf(bio_stdout, "client:SSL_read()\n"); 485296341Sdelphij } 48655714Skris 487296341Sdelphij if (!do_client && !do_server) { 488296341Sdelphij BIO_printf(bio_stdout, "ERROR IN STARTUP\n"); 489296341Sdelphij break; 490296341Sdelphij } 491296341Sdelphij if (do_client && !(done & C_DONE)) { 492296341Sdelphij if (c_write) { 493296341Sdelphij i = BIO_write(c_bio, "hello from client\n", 18); 494296341Sdelphij if (i < 0) { 495296341Sdelphij c_r = 0; 496296341Sdelphij c_w = 0; 497296341Sdelphij if (BIO_should_retry(c_bio)) { 498296341Sdelphij if (BIO_should_read(c_bio)) 499296341Sdelphij c_r = 1; 500296341Sdelphij if (BIO_should_write(c_bio)) 501296341Sdelphij c_w = 1; 502296341Sdelphij } else { 503296341Sdelphij BIO_printf(bio_err, "ERROR in CLIENT\n"); 504296341Sdelphij ERR_print_errors_fp(stderr); 505296341Sdelphij return (1); 506296341Sdelphij } 507296341Sdelphij } else if (i == 0) { 508296341Sdelphij BIO_printf(bio_err, "SSL CLIENT STARTUP FAILED\n"); 509296341Sdelphij return (1); 510296341Sdelphij } else { 511296341Sdelphij /* ok */ 512296341Sdelphij c_write = 0; 513296341Sdelphij } 514296341Sdelphij } else { 515296341Sdelphij i = BIO_read(c_bio, cbuf, 100); 516296341Sdelphij if (i < 0) { 517296341Sdelphij c_r = 0; 518296341Sdelphij c_w = 0; 519296341Sdelphij if (BIO_should_retry(c_bio)) { 520296341Sdelphij if (BIO_should_read(c_bio)) 521296341Sdelphij c_r = 1; 522296341Sdelphij if (BIO_should_write(c_bio)) 523296341Sdelphij c_w = 1; 524296341Sdelphij } else { 525296341Sdelphij BIO_printf(bio_err, "ERROR in CLIENT\n"); 526296341Sdelphij ERR_print_errors_fp(stderr); 527296341Sdelphij return (1); 528296341Sdelphij } 529296341Sdelphij } else if (i == 0) { 530296341Sdelphij BIO_printf(bio_err, "SSL CLIENT STARTUP FAILED\n"); 531296341Sdelphij return (1); 532296341Sdelphij } else { 533296341Sdelphij done |= C_DONE; 53455714Skris#ifdef undef 535296341Sdelphij BIO_printf(bio_stdout, "CLIENT:from server:"); 536296341Sdelphij BIO_write(bio_stdout, cbuf, i); 537296341Sdelphij BIO_flush(bio_stdout); 53855714Skris#endif 539296341Sdelphij } 540296341Sdelphij } 541296341Sdelphij } 54255714Skris 543296341Sdelphij if (do_server && !(done & S_DONE)) { 544296341Sdelphij if (!s_write) { 545296341Sdelphij i = BIO_read(s_bio, sbuf, 100); 546296341Sdelphij if (i < 0) { 547296341Sdelphij s_r = 0; 548296341Sdelphij s_w = 0; 549296341Sdelphij if (BIO_should_retry(s_bio)) { 550296341Sdelphij if (BIO_should_read(s_bio)) 551296341Sdelphij s_r = 1; 552296341Sdelphij if (BIO_should_write(s_bio)) 553296341Sdelphij s_w = 1; 554296341Sdelphij } else { 555296341Sdelphij BIO_printf(bio_err, "ERROR in SERVER\n"); 556296341Sdelphij ERR_print_errors_fp(stderr); 557296341Sdelphij return (1); 558296341Sdelphij } 559296341Sdelphij } else if (i == 0) { 560296341Sdelphij BIO_printf(bio_err, "SSL SERVER STARTUP FAILED\n"); 561296341Sdelphij return (1); 562296341Sdelphij } else { 563296341Sdelphij s_write = 1; 564296341Sdelphij s_w = 1; 56555714Skris#ifdef undef 566296341Sdelphij BIO_printf(bio_stdout, "SERVER:from client:"); 567296341Sdelphij BIO_write(bio_stdout, sbuf, i); 568296341Sdelphij BIO_flush(bio_stdout); 56955714Skris#endif 570296341Sdelphij } 571296341Sdelphij } else { 572296341Sdelphij i = BIO_write(s_bio, "hello from server\n", 18); 573296341Sdelphij if (i < 0) { 574296341Sdelphij s_r = 0; 575296341Sdelphij s_w = 0; 576296341Sdelphij if (BIO_should_retry(s_bio)) { 577296341Sdelphij if (BIO_should_read(s_bio)) 578296341Sdelphij s_r = 1; 579296341Sdelphij if (BIO_should_write(s_bio)) 580296341Sdelphij s_w = 1; 581296341Sdelphij } else { 582296341Sdelphij BIO_printf(bio_err, "ERROR in SERVER\n"); 583296341Sdelphij ERR_print_errors_fp(stderr); 584296341Sdelphij return (1); 585296341Sdelphij } 586296341Sdelphij } else if (i == 0) { 587296341Sdelphij BIO_printf(bio_err, "SSL SERVER STARTUP FAILED\n"); 588296341Sdelphij return (1); 589296341Sdelphij } else { 590296341Sdelphij s_write = 0; 591296341Sdelphij s_r = 1; 592296341Sdelphij done |= S_DONE; 593296341Sdelphij } 594296341Sdelphij } 595296341Sdelphij } 59655714Skris 597296341Sdelphij if ((done & S_DONE) && (done & C_DONE)) 598296341Sdelphij break; 599296341Sdelphij#if defined(OPENSSL_SYS_NETWARE) 600160814Ssimon ThreadSwitchWithDelay(); 601296341Sdelphij#endif 602296341Sdelphij } 60355714Skris 604296341Sdelphij SSL_set_shutdown(c_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); 605296341Sdelphij SSL_set_shutdown(s_ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); 60655714Skris 60755714Skris#ifdef undef 608296341Sdelphij BIO_printf(bio_stdout, "DONE\n"); 60955714Skris#endif 610296341Sdelphij err: 611296341Sdelphij /* 612296341Sdelphij * We have to set the BIO's to NULL otherwise they will be free()ed 613296341Sdelphij * twice. Once when th s_ssl is SSL_free()ed and again when c_ssl is 614296341Sdelphij * SSL_free()ed. This is a hack required because s_ssl and c_ssl are 615296341Sdelphij * sharing the same BIO structure and SSL_set_bio() and SSL_free() 616296341Sdelphij * automatically BIO_free non NULL entries. You should not normally do 617296341Sdelphij * this or be required to do this 618296341Sdelphij */ 61955714Skris 620296341Sdelphij if (s_ssl != NULL) { 621296341Sdelphij s_ssl->rbio = NULL; 622296341Sdelphij s_ssl->wbio = NULL; 623296341Sdelphij } 624296341Sdelphij if (c_ssl != NULL) { 625296341Sdelphij c_ssl->rbio = NULL; 626296341Sdelphij c_ssl->wbio = NULL; 627296341Sdelphij } 62855714Skris 629296341Sdelphij /* The SSL's are optionally freed in the following calls */ 630296341Sdelphij if (c_to_s != NULL) 631296341Sdelphij BIO_free(c_to_s); 632296341Sdelphij if (s_to_c != NULL) 633296341Sdelphij BIO_free(s_to_c); 63455714Skris 635296341Sdelphij if (c_bio != NULL) 636296341Sdelphij BIO_free(c_bio); 637296341Sdelphij if (s_bio != NULL) 638296341Sdelphij BIO_free(s_bio); 639296341Sdelphij return (0); 640296341Sdelphij} 64155714Skris 642296341Sdelphijint verify_callback(int ok, X509_STORE_CTX *ctx) 643296341Sdelphij{ 644296341Sdelphij char *s, buf[256]; 64555714Skris 646296341Sdelphij if (verbose) { 647296341Sdelphij s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), 648296341Sdelphij buf, 256); 649296341Sdelphij if (s != NULL) { 650296341Sdelphij if (ok) 651296341Sdelphij BIO_printf(bio_err, "depth=%d %s\n", ctx->error_depth, buf); 652296341Sdelphij else 653296341Sdelphij BIO_printf(bio_err, "depth=%d error=%d %s\n", 654296341Sdelphij ctx->error_depth, ctx->error, buf); 655296341Sdelphij } 656296341Sdelphij } 657296341Sdelphij return (ok); 658296341Sdelphij} 65955714Skris 66055714Skris#define THREAD_STACK_SIZE (16*1024) 66155714Skris 662109998Smarkm#ifdef OPENSSL_SYS_WIN32 66355714Skris 66459191Skrisstatic HANDLE *lock_cs; 66555714Skris 66655714Skrisvoid thread_setup(void) 667296341Sdelphij{ 668296341Sdelphij int i; 66955714Skris 670296341Sdelphij lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); 671296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 672296341Sdelphij lock_cs[i] = CreateMutex(NULL, FALSE, NULL); 673296341Sdelphij } 67455714Skris 675296341Sdelphij CRYPTO_set_locking_callback((void (*)(int, int, char *, int)) 676296341Sdelphij win32_locking_callback); 677296341Sdelphij /* id callback defined */ 678296341Sdelphij} 67955714Skris 68055714Skrisvoid thread_cleanup(void) 681296341Sdelphij{ 682296341Sdelphij int i; 68355714Skris 684296341Sdelphij CRYPTO_set_locking_callback(NULL); 685296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) 686296341Sdelphij CloseHandle(lock_cs[i]); 687296341Sdelphij OPENSSL_free(lock_cs); 688296341Sdelphij} 68955714Skris 690296341Sdelphijvoid win32_locking_callback(int mode, int type, const char *file, int line) 691296341Sdelphij{ 692296341Sdelphij if (mode & CRYPTO_LOCK) { 693296341Sdelphij WaitForSingleObject(lock_cs[type], INFINITE); 694296341Sdelphij } else { 695296341Sdelphij ReleaseMutex(lock_cs[type]); 696296341Sdelphij } 697296341Sdelphij} 69855714Skris 69955714Skrisvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 700296341Sdelphij{ 701296341Sdelphij double ret; 702296341Sdelphij SSL_CTX *ssl_ctx[2]; 703296341Sdelphij DWORD thread_id[MAX_THREAD_NUMBER]; 704296341Sdelphij HANDLE thread_handle[MAX_THREAD_NUMBER]; 705296341Sdelphij int i; 706296341Sdelphij SYSTEMTIME start, end; 70755714Skris 708296341Sdelphij ssl_ctx[0] = s_ctx; 709296341Sdelphij ssl_ctx[1] = c_ctx; 71055714Skris 711296341Sdelphij GetSystemTime(&start); 712296341Sdelphij for (i = 0; i < thread_number; i++) { 713296341Sdelphij thread_handle[i] = CreateThread(NULL, 714296341Sdelphij THREAD_STACK_SIZE, 715296341Sdelphij (LPTHREAD_START_ROUTINE) ndoit, 716296341Sdelphij (void *)ssl_ctx, 0L, &(thread_id[i])); 717296341Sdelphij } 71855714Skris 719296341Sdelphij BIO_printf(bio_stdout, "reaping\n"); 720296341Sdelphij for (i = 0; i < thread_number; i += 50) { 721296341Sdelphij int j; 72255714Skris 723296341Sdelphij j = (thread_number < (i + 50)) ? (thread_number - i) : 50; 72455714Skris 725296341Sdelphij if (WaitForMultipleObjects(j, 726296341Sdelphij (CONST HANDLE *) & (thread_handle[i]), 727296341Sdelphij TRUE, INFINITE) 728296341Sdelphij == WAIT_FAILED) { 729296341Sdelphij BIO_printf(bio_err, "WaitForMultipleObjects failed:%d\n", 730296341Sdelphij GetLastError()); 731296341Sdelphij exit(1); 732296341Sdelphij } 733296341Sdelphij } 734296341Sdelphij GetSystemTime(&end); 73555714Skris 736296341Sdelphij if (start.wDayOfWeek > end.wDayOfWeek) 737296341Sdelphij end.wDayOfWeek += 7; 738296341Sdelphij ret = (end.wDayOfWeek - start.wDayOfWeek) * 24; 73955714Skris 740296341Sdelphij ret = (ret + end.wHour - start.wHour) * 60; 741296341Sdelphij ret = (ret + end.wMinute - start.wMinute) * 60; 742296341Sdelphij ret = (ret + end.wSecond - start.wSecond); 743296341Sdelphij ret += (end.wMilliseconds - start.wMilliseconds) / 1000.0; 74455714Skris 745296341Sdelphij BIO_printf(bio_stdout, "win32 threads done - %.3f seconds\n", ret); 746296341Sdelphij} 74755714Skris 748296341Sdelphij#endif /* OPENSSL_SYS_WIN32 */ 74955714Skris 75055714Skris#ifdef SOLARIS 75155714Skris 75259191Skrisstatic mutex_t *lock_cs; 753296341Sdelphij/* 754296341Sdelphij * static rwlock_t *lock_cs; 755296341Sdelphij */ 75659191Skrisstatic long *lock_count; 75755714Skris 75855714Skrisvoid thread_setup(void) 759296341Sdelphij{ 760296341Sdelphij int i; 76155714Skris 762296341Sdelphij lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t)); 763296341Sdelphij lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 764296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 765296341Sdelphij lock_count[i] = 0; 766296341Sdelphij /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */ 767296341Sdelphij mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL); 768296341Sdelphij } 76955714Skris 770296341Sdelphij CRYPTO_set_id_callback(solaris_thread_id); 771296341Sdelphij CRYPTO_set_locking_callback(solaris_locking_callback); 772296341Sdelphij} 77355714Skris 77455714Skrisvoid thread_cleanup(void) 775296341Sdelphij{ 776296341Sdelphij int i; 77755714Skris 778296341Sdelphij CRYPTO_set_locking_callback(NULL); 77959191Skris 780296341Sdelphij BIO_printf(bio_err, "cleanup\n"); 78159191Skris 782296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 783296341Sdelphij /* rwlock_destroy(&(lock_cs[i])); */ 784296341Sdelphij mutex_destroy(&(lock_cs[i])); 785296341Sdelphij BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 786296341Sdelphij } 787296341Sdelphij OPENSSL_free(lock_cs); 788296341Sdelphij OPENSSL_free(lock_count); 78959191Skris 790296341Sdelphij BIO_printf(bio_err, "done cleanup\n"); 79159191Skris 792296341Sdelphij} 79355714Skris 794296341Sdelphijvoid solaris_locking_callback(int mode, int type, const char *file, int line) 795296341Sdelphij{ 796296341Sdelphij# ifdef undef 797296341Sdelphij BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n", 798296341Sdelphij CRYPTO_thread_id(), 799296341Sdelphij (mode & CRYPTO_LOCK) ? "l" : "u", 800296341Sdelphij (type & CRYPTO_READ) ? "r" : "w", file, line); 801296341Sdelphij# endif 80255714Skris 803296341Sdelphij /*- 804296341Sdelphij if (CRYPTO_LOCK_SSL_CERT == type) 805296341Sdelphij BIO_printf(bio_err,"(t,m,f,l) %ld %d %s %d\n", 806296341Sdelphij CRYPTO_thread_id(), 807296341Sdelphij mode,file,line); 808296341Sdelphij */ 809296341Sdelphij if (mode & CRYPTO_LOCK) { 810296341Sdelphij /*- 811296341Sdelphij if (mode & CRYPTO_READ) 812296341Sdelphij rw_rdlock(&(lock_cs[type])); 813296341Sdelphij else 814296341Sdelphij rw_wrlock(&(lock_cs[type])); */ 81555714Skris 816296341Sdelphij mutex_lock(&(lock_cs[type])); 817296341Sdelphij lock_count[type]++; 818296341Sdelphij } else { 819296341Sdelphij/* rw_unlock(&(lock_cs[type])); */ 820296341Sdelphij mutex_unlock(&(lock_cs[type])); 821296341Sdelphij } 822296341Sdelphij} 82355714Skris 82455714Skrisvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 825296341Sdelphij{ 826296341Sdelphij SSL_CTX *ssl_ctx[2]; 827296341Sdelphij thread_t thread_ctx[MAX_THREAD_NUMBER]; 828296341Sdelphij int i; 82955714Skris 830296341Sdelphij ssl_ctx[0] = s_ctx; 831296341Sdelphij ssl_ctx[1] = c_ctx; 83255714Skris 833296341Sdelphij thr_setconcurrency(thread_number); 834296341Sdelphij for (i = 0; i < thread_number; i++) { 835296341Sdelphij thr_create(NULL, THREAD_STACK_SIZE, 836296341Sdelphij (void *(*)())ndoit, (void *)ssl_ctx, 0L, &(thread_ctx[i])); 837296341Sdelphij } 83855714Skris 839296341Sdelphij BIO_printf(bio_stdout, "reaping\n"); 840296341Sdelphij for (i = 0; i < thread_number; i++) { 841296341Sdelphij thr_join(thread_ctx[i], NULL, NULL); 842296341Sdelphij } 84355714Skris 844296341Sdelphij#if 0 /* We can't currently find out the reference amount */ 845296341Sdelphij BIO_printf(bio_stdout, "solaris threads done (%d,%d)\n", 846296341Sdelphij s_ctx->references, c_ctx->references); 847296341Sdelphij#else 848296341Sdelphij BIO_printf(bio_stdout, "solaris threads done\n"); 849296341Sdelphij#endif 850296341Sdelphij} 85155714Skris 852296341Sdelphijvoid solaris_thread_id(CRYPTO_THREADID *tid) 853296341Sdelphij{ 854296341Sdelphij CRYPTO_THREADID_set_numeric((unsigned long)thr_self()); 855296341Sdelphij} 856296341Sdelphij#endif /* SOLARIS */ 85755714Skris 85855714Skris#ifdef IRIX 85955714Skris 86055714Skrisstatic usptr_t *arena; 86159191Skrisstatic usema_t **lock_cs; 86255714Skris 86355714Skrisvoid thread_setup(void) 864296341Sdelphij{ 865296341Sdelphij int i; 866296341Sdelphij char filename[20]; 86755714Skris 868296341Sdelphij strcpy(filename, "/tmp/mttest.XXXXXX"); 869296341Sdelphij mktemp(filename); 87055714Skris 871296341Sdelphij usconfig(CONF_STHREADIOOFF); 872296341Sdelphij usconfig(CONF_STHREADMALLOCOFF); 873296341Sdelphij usconfig(CONF_INITUSERS, 100); 874296341Sdelphij usconfig(CONF_LOCKTYPE, US_DEBUGPLUS); 875296341Sdelphij arena = usinit(filename); 876296341Sdelphij unlink(filename); 87755714Skris 878296341Sdelphij lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *)); 879296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 880296341Sdelphij lock_cs[i] = usnewsema(arena, 1); 881296341Sdelphij } 88255714Skris 883296341Sdelphij CRYPTO_set_id_callback(irix_thread_id); 884296341Sdelphij CRYPTO_set_locking_callback(irix_locking_callback); 885296341Sdelphij} 88655714Skris 88755714Skrisvoid thread_cleanup(void) 888296341Sdelphij{ 889296341Sdelphij int i; 89055714Skris 891296341Sdelphij CRYPTO_set_locking_callback(NULL); 892296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 893296341Sdelphij char buf[10]; 89455714Skris 895296341Sdelphij sprintf(buf, "%2d:", i); 896296341Sdelphij usdumpsema(lock_cs[i], stdout, buf); 897296341Sdelphij usfreesema(lock_cs[i], arena); 898296341Sdelphij } 899296341Sdelphij OPENSSL_free(lock_cs); 900296341Sdelphij} 90155714Skris 902296341Sdelphijvoid irix_locking_callback(int mode, int type, const char *file, int line) 903296341Sdelphij{ 904296341Sdelphij if (mode & CRYPTO_LOCK) { 905296341Sdelphij BIO_printf(bio_stdout, "lock %d\n", type); 906296341Sdelphij uspsema(lock_cs[type]); 907296341Sdelphij } else { 908296341Sdelphij BIO_printf(bio_stdout, "unlock %d\n", type); 909296341Sdelphij usvsema(lock_cs[type]); 910296341Sdelphij } 911296341Sdelphij} 91255714Skris 91355714Skrisvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 914296341Sdelphij{ 915296341Sdelphij SSL_CTX *ssl_ctx[2]; 916296341Sdelphij int thread_ctx[MAX_THREAD_NUMBER]; 917296341Sdelphij int i; 91855714Skris 919296341Sdelphij ssl_ctx[0] = s_ctx; 920296341Sdelphij ssl_ctx[1] = c_ctx; 92155714Skris 922296341Sdelphij for (i = 0; i < thread_number; i++) { 923296341Sdelphij thread_ctx[i] = sproc((void (*)())ndoit, 924296341Sdelphij PR_SADDR | PR_SFDS, (void *)ssl_ctx); 925296341Sdelphij } 92655714Skris 927296341Sdelphij BIO_printf(bio_stdout, "reaping\n"); 928296341Sdelphij for (i = 0; i < thread_number; i++) { 929296341Sdelphij wait(NULL); 930296341Sdelphij } 93155714Skris 932296341Sdelphij#if 0 /* We can't currently find out the reference amount */ 933296341Sdelphij BIO_printf(bio_stdout, "irix threads done (%d,%d)\n", 934296341Sdelphij s_ctx->references, c_ctx->references); 935296341Sdelphij#else 936296341Sdelphij BIO_printf(bio_stdout, "irix threads done\n"); 937296341Sdelphij#endif 938296341Sdelphij} 93955714Skris 94055714Skrisunsigned long irix_thread_id(void) 941296341Sdelphij{ 942296341Sdelphij CRYPTO_THREADID_set_numeric((unsigned long)getpid()); 943296341Sdelphij} 944296341Sdelphij#endif /* IRIX */ 94555714Skris 94655714Skris#ifdef PTHREADS 94755714Skris 94859191Skrisstatic pthread_mutex_t *lock_cs; 94959191Skrisstatic long *lock_count; 95055714Skris 95155714Skrisvoid thread_setup(void) 952296341Sdelphij{ 953296341Sdelphij int i; 95455714Skris 955296341Sdelphij lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); 956296341Sdelphij lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 957296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 958296341Sdelphij lock_count[i] = 0; 959296341Sdelphij pthread_mutex_init(&(lock_cs[i]), NULL); 960296341Sdelphij } 96155714Skris 962296341Sdelphij CRYPTO_THREADID_set_callback(pthreads_thread_id); 963296341Sdelphij CRYPTO_set_locking_callback(pthreads_locking_callback); 964296341Sdelphij} 96555714Skris 96655714Skrisvoid thread_cleanup(void) 967296341Sdelphij{ 968296341Sdelphij int i; 96955714Skris 970296341Sdelphij CRYPTO_set_locking_callback(NULL); 971296341Sdelphij BIO_printf(bio_err, "cleanup\n"); 972296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 973296341Sdelphij pthread_mutex_destroy(&(lock_cs[i])); 974296341Sdelphij BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 975296341Sdelphij } 976296341Sdelphij OPENSSL_free(lock_cs); 977296341Sdelphij OPENSSL_free(lock_count); 97859191Skris 979296341Sdelphij BIO_printf(bio_err, "done cleanup\n"); 980296341Sdelphij} 98155714Skris 982296341Sdelphijvoid pthreads_locking_callback(int mode, int type, const char *file, int line) 983296341Sdelphij{ 984296341Sdelphij# ifdef undef 985296341Sdelphij BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n", 986296341Sdelphij CRYPTO_thread_id(), 987296341Sdelphij (mode & CRYPTO_LOCK) ? "l" : "u", 988296341Sdelphij (type & CRYPTO_READ) ? "r" : "w", file, line); 989296341Sdelphij# endif 990296341Sdelphij/*- 991296341Sdelphij if (CRYPTO_LOCK_SSL_CERT == type) 992296341Sdelphij BIO_printf(bio_err,"(t,m,f,l) %ld %d %s %d\n", 993296341Sdelphij CRYPTO_thread_id(), 994296341Sdelphij mode,file,line); 99555714Skris*/ 996296341Sdelphij if (mode & CRYPTO_LOCK) { 997296341Sdelphij pthread_mutex_lock(&(lock_cs[type])); 998296341Sdelphij lock_count[type]++; 999296341Sdelphij } else { 1000296341Sdelphij pthread_mutex_unlock(&(lock_cs[type])); 1001296341Sdelphij } 1002296341Sdelphij} 100355714Skris 100455714Skrisvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 1005296341Sdelphij{ 1006296341Sdelphij SSL_CTX *ssl_ctx[2]; 1007296341Sdelphij pthread_t thread_ctx[MAX_THREAD_NUMBER]; 1008296341Sdelphij int i; 100955714Skris 1010296341Sdelphij ssl_ctx[0] = s_ctx; 1011296341Sdelphij ssl_ctx[1] = c_ctx; 101255714Skris 1013296341Sdelphij /* 1014296341Sdelphij * thr_setconcurrency(thread_number); 1015296341Sdelphij */ 1016296341Sdelphij for (i = 0; i < thread_number; i++) { 1017296341Sdelphij pthread_create(&(thread_ctx[i]), NULL, 1018296341Sdelphij (void *(*)())ndoit, (void *)ssl_ctx); 1019296341Sdelphij } 102055714Skris 1021296341Sdelphij BIO_printf(bio_stdout, "reaping\n"); 1022296341Sdelphij for (i = 0; i < thread_number; i++) { 1023296341Sdelphij pthread_join(thread_ctx[i], NULL); 1024296341Sdelphij } 102555714Skris 1026296341Sdelphij#if 0 /* We can't currently find out the reference amount */ 1027296341Sdelphij BIO_printf(bio_stdout, "pthreads threads done (%d,%d)\n", 1028296341Sdelphij s_ctx->references, c_ctx->references); 1029296341Sdelphij#else 1030296341Sdelphij BIO_printf(bio_stdout, "pthreads threads done\n"); 1031296341Sdelphij#endif 1032296341Sdelphij} 103355714Skris 1034296341Sdelphijvoid pthreads_thread_id(CRYPTO_THREADID *tid) 1035296341Sdelphij{ 1036296341Sdelphij CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); 1037296341Sdelphij} 103855714Skris 1039296341Sdelphij#endif /* PTHREADS */ 104055714Skris 1041160814Ssimon#ifdef OPENSSL_SYS_NETWARE 1042160814Ssimon 1043160814Ssimonvoid thread_setup(void) 1044160814Ssimon{ 1045296341Sdelphij int i; 1046160814Ssimon 1047296341Sdelphij lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex)); 1048296341Sdelphij lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 1049296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 1050296341Sdelphij lock_count[i] = 0; 1051296341Sdelphij lock_cs[i] = MPKMutexAlloc("OpenSSL mutex"); 1052296341Sdelphij } 1053160814Ssimon 1054296341Sdelphij ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0); 1055160814Ssimon 1056296341Sdelphij CRYPTO_set_id_callback(netware_thread_id); 1057296341Sdelphij CRYPTO_set_locking_callback(netware_locking_callback); 1058160814Ssimon} 1059160814Ssimon 1060160814Ssimonvoid thread_cleanup(void) 1061160814Ssimon{ 1062296341Sdelphij int i; 1063160814Ssimon 1064296341Sdelphij CRYPTO_set_locking_callback(NULL); 1065160814Ssimon 1066296341Sdelphij BIO_printf(bio_stdout, "thread_cleanup\n"); 1067160814Ssimon 1068296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 1069296341Sdelphij MPKMutexFree(lock_cs[i]); 1070296341Sdelphij BIO_printf(bio_stdout, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 1071296341Sdelphij } 1072296341Sdelphij OPENSSL_free(lock_cs); 1073296341Sdelphij OPENSSL_free(lock_count); 1074160814Ssimon 1075296341Sdelphij MPKSemaphoreFree(ThreadSem); 1076160814Ssimon 1077296341Sdelphij BIO_printf(bio_stdout, "done cleanup\n"); 1078160814Ssimon} 1079160814Ssimon 1080296341Sdelphijvoid netware_locking_callback(int mode, int type, const char *file, int line) 1081160814Ssimon{ 1082296341Sdelphij if (mode & CRYPTO_LOCK) { 1083296341Sdelphij MPKMutexLock(lock_cs[type]); 1084296341Sdelphij lock_count[type]++; 1085296341Sdelphij } else 1086296341Sdelphij MPKMutexUnlock(lock_cs[type]); 1087160814Ssimon} 1088160814Ssimon 1089160814Ssimonvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 1090160814Ssimon{ 1091296341Sdelphij SSL_CTX *ssl_ctx[2]; 1092296341Sdelphij int i; 1093296341Sdelphij ssl_ctx[0] = s_ctx; 1094296341Sdelphij ssl_ctx[1] = c_ctx; 1095160814Ssimon 1096296341Sdelphij for (i = 0; i < thread_number; i++) { 1097296341Sdelphij BeginThread((void (*)(void *))ndoit, NULL, THREAD_STACK_SIZE, 1098296341Sdelphij (void *)ssl_ctx); 1099296341Sdelphij ThreadSwitchWithDelay(); 1100296341Sdelphij } 1101160814Ssimon 1102296341Sdelphij BIO_printf(bio_stdout, "reaping\n"); 1103160814Ssimon 1104296341Sdelphij /* loop until all threads have signaled the semaphore */ 1105296341Sdelphij for (i = 0; i < thread_number; i++) { 1106296341Sdelphij MPKSemaphoreWait(ThreadSem); 1107296341Sdelphij } 1108296341Sdelphij#if 0 /* We can't currently find out the reference amount */ 1109296341Sdelphij BIO_printf(bio_stdout, "netware threads done (%d,%d)\n", 1110296341Sdelphij s_ctx->references, c_ctx->references); 1111296341Sdelphij#else 1112296341Sdelphij BIO_printf(bio_stdout, "netware threads done\n"); 1113296341Sdelphij#endif 1114160814Ssimon} 1115160814Ssimon 1116160814Ssimonunsigned long netware_thread_id(void) 1117160814Ssimon{ 1118296341Sdelphij CRYPTO_THREADID_set_numeric((unsigned long)GetThreadID()); 1119160814Ssimon} 1120296341Sdelphij#endif /* NETWARE */ 1121238405Sjkim 1122238405Sjkim#ifdef BEOS_THREADS 1123238405Sjkim 1124296341Sdelphij# include <Locker.h> 1125238405Sjkim 1126296341Sdelphijstatic BLocker **lock_cs; 1127296341Sdelphijstatic long *lock_count; 1128238405Sjkim 1129238405Sjkimvoid thread_setup(void) 1130296341Sdelphij{ 1131296341Sdelphij int i; 1132238405Sjkim 1133296341Sdelphij lock_cs = 1134296341Sdelphij (BLocker **) OPENSSL_malloc(CRYPTO_num_locks() * sizeof(BLocker *)); 1135296341Sdelphij lock_count = (long *)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); 1136296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 1137296341Sdelphij lock_count[i] = 0; 1138296341Sdelphij lock_cs[i] = new BLocker(CRYPTO_get_lock_name(i)); 1139296341Sdelphij } 1140238405Sjkim 1141296341Sdelphij CRYPTO_set_id_callback((unsigned long (*)())beos_thread_id); 1142296341Sdelphij CRYPTO_set_locking_callback(beos_locking_callback); 1143296341Sdelphij} 1144238405Sjkim 1145238405Sjkimvoid thread_cleanup(void) 1146296341Sdelphij{ 1147296341Sdelphij int i; 1148238405Sjkim 1149296341Sdelphij CRYPTO_set_locking_callback(NULL); 1150296341Sdelphij BIO_printf(bio_err, "cleanup\n"); 1151296341Sdelphij for (i = 0; i < CRYPTO_num_locks(); i++) { 1152296341Sdelphij delete lock_cs[i]; 1153296341Sdelphij BIO_printf(bio_err, "%8ld:%s\n", lock_count[i], CRYPTO_get_lock_name(i)); 1154296341Sdelphij } 1155296341Sdelphij OPENSSL_free(lock_cs); 1156296341Sdelphij OPENSSL_free(lock_count); 1157238405Sjkim 1158296341Sdelphij BIO_printf(bio_err, "done cleanup\n"); 1159296341Sdelphij} 1160238405Sjkim 1161238405Sjkimvoid beos_locking_callback(int mode, int type, const char *file, int line) 1162296341Sdelphij{ 1163296341Sdelphij# if 0 1164296341Sdelphij BIO_printf(bio_err, "thread=%4d mode=%s lock=%s %s:%d\n", 1165296341Sdelphij CRYPTO_thread_id(), 1166296341Sdelphij (mode & CRYPTO_LOCK) ? "l" : "u", 1167296341Sdelphij (type & CRYPTO_READ) ? "r" : "w", file, line); 1168296341Sdelphij# endif 1169296341Sdelphij if (mode & CRYPTO_LOCK) { 1170296341Sdelphij lock_cs[type]->Lock(); 1171296341Sdelphij lock_count[type]++; 1172296341Sdelphij } else { 1173296341Sdelphij lock_cs[type]->Unlock(); 1174296341Sdelphij } 1175296341Sdelphij} 1176238405Sjkim 1177238405Sjkimvoid do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) 1178296341Sdelphij{ 1179296341Sdelphij SSL_CTX *ssl_ctx[2]; 1180296341Sdelphij thread_id thread_ctx[MAX_THREAD_NUMBER]; 1181296341Sdelphij int i; 1182238405Sjkim 1183296341Sdelphij ssl_ctx[0] = s_ctx; 1184296341Sdelphij ssl_ctx[1] = c_ctx; 1185238405Sjkim 1186296341Sdelphij for (i = 0; i < thread_number; i++) { 1187296341Sdelphij thread_ctx[i] = spawn_thread((thread_func) ndoit, 1188296341Sdelphij NULL, B_NORMAL_PRIORITY, 1189296341Sdelphij (void *)ssl_ctx); 1190296341Sdelphij resume_thread(thread_ctx[i]); 1191296341Sdelphij } 1192238405Sjkim 1193296341Sdelphij BIO_printf(bio_stdout, "waiting...\n"); 1194296341Sdelphij for (i = 0; i < thread_number; i++) { 1195296341Sdelphij status_t result; 1196296341Sdelphij wait_for_thread(thread_ctx[i], &result); 1197296341Sdelphij } 1198238405Sjkim 1199296341Sdelphij BIO_printf(bio_stdout, "beos threads done (%d,%d)\n", 1200296341Sdelphij s_ctx->references, c_ctx->references); 1201296341Sdelphij} 1202238405Sjkim 1203238405Sjkimunsigned long beos_thread_id(void) 1204296341Sdelphij{ 1205296341Sdelphij unsigned long ret; 1206238405Sjkim 1207296341Sdelphij ret = (unsigned long)find_thread(NULL); 1208296341Sdelphij return (ret); 1209296341Sdelphij} 1210238405Sjkim 1211296341Sdelphij#endif /* BEOS_THREADS */ 1212