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