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