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