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