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