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