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