2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
75 #include <sys/prctl.h>
87 #include "../crypto/buffer/bss_file.c"
90 #define TEST_SERVER_CERT "../apps/server.pem"
91 #define TEST_CLIENT_CERT "../apps/client.pem"
93 #define MAX_THREAD_NUMBER 100
96 int MS_CALLBACK verify_callback(int ok, X509 *xs, X509 *xi, int depth,
98 void thread_setup(void);
99 void thread_cleanup(void);
100 void do_threads(SSL_CTX *s_ctx,SSL_CTX *c_ctx);
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 linux_locking_callback(int mode,int type,char *file,int line);
105 unsigned long irix_thread_id(void );
106 unsigned long solaris_thread_id(void );
107 unsigned long linix_thread_id(void );
109 int MS_CALLBACK verify_callback();
111 void thread_cleanup();
113 void irix_locking_callback();
114 void solaris_locking_callback();
115 void win32_locking_callback();
116 void linux_locking_callback();
117 unsigned long irix_thread_id();
118 unsigned long solaris_thread_id();
119 unsigned long linix_thread_id();
123 BIO *bio_stdout=NULL;
125 static char *cipher=NULL;
131 int thread_number=10;
132 int number_of_loops=10;
137 int doit(char *ctx[4]);
142 static void print_stats(fp,ctx)
146 fprintf(fp,"%4ld items in the session cache\n",
147 SSL_CTX_sess_number(ctx));
148 fprintf(fp,"%4d client connects (SSL_connect())\n",
149 SSL_CTX_sess_connect(ctx));
150 fprintf(fp,"%4d client connects that finished\n",
151 SSL_CTX_sess_connect_good(ctx));
152 fprintf(fp,"%4d server connects (SSL_accept())\n",
153 SSL_CTX_sess_accept(ctx));
154 fprintf(fp,"%4d server connects that finished\n",
155 SSL_CTX_sess_accept_good(ctx));
156 fprintf(fp,"%4d session cache hits\n",SSL_CTX_sess_hits(ctx));
157 fprintf(fp,"%4d session cache misses\n",SSL_CTX_sess_misses(ctx));
158 fprintf(fp,"%4d session cache timeouts\n",SSL_CTX_sess_timeouts(ctx));
161 static void sv_usage()
163 fprintf(stderr,"usage: ssltest [args ...]\n");
164 fprintf(stderr,"\n");
165 fprintf(stderr," -server_auth - check server certificate\n");
166 fprintf(stderr," -client_auth - do client authentication\n");
167 fprintf(stderr," -v - more output\n");
168 fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
169 fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
170 fprintf(stderr," -threads arg - number of threads\n");
171 fprintf(stderr," -loops arg - number of 'connections', per thread\n");
172 fprintf(stderr," -reconnect - reuse session-id's\n");
173 fprintf(stderr," -stats - server session-id cache stats\n");
174 fprintf(stderr," -cert arg - server certificate/key\n");
175 fprintf(stderr," -ccert arg - client certificate/key\n");
176 fprintf(stderr," -ssl3 - just SSLv3n\n");
183 char *CApath=NULL,*CAfile=NULL;
190 char *scert=TEST_SERVER_CERT;
191 char *ccert=TEST_CLIENT_CERT;
192 SSL_METHOD *ssl_method=SSLv23_method();
195 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
196 if (bio_stdout == NULL)
197 bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);
203 if (strcmp(*argv,"-server_auth") == 0)
205 else if (strcmp(*argv,"-client_auth") == 0)
207 else if (strcmp(*argv,"-reconnect") == 0)
209 else if (strcmp(*argv,"-stats") == 0)
211 else if (strcmp(*argv,"-ssl3") == 0)
212 ssl_method=SSLv3_method();
213 else if (strcmp(*argv,"-ssl2") == 0)
214 ssl_method=SSLv2_method();
215 else if (strcmp(*argv,"-CApath") == 0)
217 if (--argc < 1) goto bad;
220 else if (strcmp(*argv,"-CAfile") == 0)
222 if (--argc < 1) goto bad;
225 else if (strcmp(*argv,"-cert") == 0)
227 if (--argc < 1) goto bad;
230 else if (strcmp(*argv,"-ccert") == 0)
232 if (--argc < 1) goto bad;
235 else if (strcmp(*argv,"-threads") == 0)
237 if (--argc < 1) goto bad;
238 thread_number= atoi(*(++argv));
239 if (thread_number == 0) thread_number=1;
240 if (thread_number > MAX_THREAD_NUMBER)
241 thread_number=MAX_THREAD_NUMBER;
243 else if (strcmp(*argv,"-loops") == 0)
245 if (--argc < 1) goto bad;
246 number_of_loops= atoi(*(++argv));
247 if (number_of_loops == 0) number_of_loops=1;
251 fprintf(stderr,"unknown option %s\n",*argv);
265 if (cipher == NULL) cipher=getenv("SSL_CIPHER");
267 SSL_load_error_strings();
268 SSLeay_add_ssl_algorithms();
270 c_ctx=SSL_CTX_new(ssl_method);
271 s_ctx=SSL_CTX_new(ssl_method);
272 if ((c_ctx == NULL) || (s_ctx == NULL))
274 ERR_print_errors(bio_err);
278 SSL_CTX_set_session_cache_mode(s_ctx,
279 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
280 SSL_CTX_set_session_cache_mode(c_ctx,
281 SSL_SESS_CACHE_NO_AUTO_CLEAR|SSL_SESS_CACHE_SERVER);
283 SSL_CTX_use_certificate_file(s_ctx,scert,SSL_FILETYPE_PEM);
284 SSL_CTX_use_RSAPrivateKey_file(s_ctx,scert,SSL_FILETYPE_PEM);
288 SSL_CTX_use_certificate_file(c_ctx,ccert,
290 SSL_CTX_use_RSAPrivateKey_file(c_ctx,ccert,
294 if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
295 (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
296 (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
297 (!SSL_CTX_set_default_verify_paths(c_ctx)))
299 fprintf(stderr,"SSL_load_verify_locations\n");
300 ERR_print_errors(bio_err);
306 fprintf(stderr,"client authentication\n");
307 SSL_CTX_set_verify(s_ctx,
308 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
313 fprintf(stderr,"server authentication\n");
314 SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
319 do_threads(s_ctx,c_ctx);
325 fprintf(stderr,"Client SSL_CTX stats then free it\n");
326 print_stats(stderr,c_ctx);
331 fprintf(stderr,"Server SSL_CTX stats then free it\n");
332 print_stats(stderr,s_ctx);
335 fprintf(stderr,"-----\n");
336 lh_stats(SSL_CTX_sessions(s_ctx),stderr);
337 fprintf(stderr,"-----\n");
338 /* lh_node_stats(SSL_CTX_sessions(s_ctx),stderr);
339 fprintf(stderr,"-----\n"); */
340 lh_node_usage_stats(SSL_CTX_sessions(s_ctx),stderr);
341 fprintf(stderr,"-----\n");
344 fprintf(stderr,"done free\n");
362 ctx[0]=(char *)ssl_ctx[0];
363 ctx[1]=(char *)ssl_ctx[1];
367 ctx[2]=(char *)SSL_new(ssl_ctx[0]);
368 ctx[3]=(char *)SSL_new(ssl_ctx[1]);
376 fprintf(stdout,"started thread %lu\n",CRYPTO_thread_id());
377 for (i=0; i<number_of_loops; i++)
379 /* fprintf(stderr,"%4d %2d ctx->ref (%3d,%3d)\n",
380 CRYPTO_thread_id(),i,
381 ssl_ctx[0]->references,
382 ssl_ctx[1]->references); */
383 /* pthread_delay_np(&tm);*/
388 fprintf(stdout,"error[%d] %lu - %d\n",
389 i,CRYPTO_thread_id(),ret);
393 fprintf(stdout,"DONE %lu\n",CRYPTO_thread_id());
396 SSL_free((SSL *)ctx[2]);
397 SSL_free((SSL *)ctx[3]);
405 SSL_CTX *s_ctx,*c_ctx;
406 static char cbuf[200],sbuf[200];
418 int do_server=0,do_client=0;
420 s_ctx=(SSL_CTX *)ctx[0];
421 c_ctx=(SSL_CTX *)ctx[1];
426 s_ssl=SSL_new(s_ctx);
431 c_ssl=SSL_new(c_ctx);
433 if ((s_ssl == NULL) || (c_ssl == NULL)) goto err;
435 c_to_s=BIO_new(BIO_s_mem());
436 s_to_c=BIO_new(BIO_s_mem());
437 if ((s_to_c == NULL) || (c_to_s == NULL)) goto err;
439 c_bio=BIO_new(BIO_f_ssl());
440 s_bio=BIO_new(BIO_f_ssl());
441 if ((c_bio == NULL) || (s_bio == NULL)) goto err;
443 SSL_set_connect_state(c_ssl);
444 SSL_set_bio(c_ssl,s_to_c,c_to_s);
445 BIO_set_ssl(c_bio,c_ssl,(ctx[2] == NULL)?BIO_CLOSE:BIO_NOCLOSE);
447 SSL_set_accept_state(s_ssl);
448 SSL_set_bio(s_ssl,c_to_s,s_to_c);
449 BIO_set_ssl(s_bio,s_ssl,(ctx[3] == NULL)?BIO_CLOSE:BIO_NOCLOSE);
457 /* We can always do writes */
463 i=(int)BIO_pending(s_bio);
464 if ((i && s_r) || s_w) do_server=1;
466 i=(int)BIO_pending(c_bio);
467 if ((i && c_r) || c_w) do_client=1;
469 if (do_server && verbose)
471 if (SSL_in_init(s_ssl))
472 printf("server waiting in SSL_accept - %s\n",
473 SSL_state_string_long(s_ssl));
475 printf("server:SSL_write()\n");
477 printf("server:SSL_read()\n");
480 if (do_client && verbose)
482 if (SSL_in_init(c_ssl))
483 printf("client waiting in SSL_connect - %s\n",
484 SSL_state_string_long(c_ssl));
486 printf("client:SSL_write()\n");
488 printf("client:SSL_read()\n");
491 if (!do_client && !do_server)
493 fprintf(stdout,"ERROR IN STARTUP\n");
496 if (do_client && !(done & C_DONE))
500 i=BIO_write(c_bio,"hello from client\n",18);
505 if (BIO_should_retry(c_bio))
507 if (BIO_should_read(c_bio))
509 if (BIO_should_write(c_bio))
514 fprintf(stderr,"ERROR in CLIENT\n");
520 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
531 i=BIO_read(c_bio,cbuf,100);
536 if (BIO_should_retry(c_bio))
538 if (BIO_should_read(c_bio))
540 if (BIO_should_write(c_bio))
545 fprintf(stderr,"ERROR in CLIENT\n");
551 fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
558 fprintf(stdout,"CLIENT:from server:");
559 fwrite(cbuf,1,i,stdout);
566 if (do_server && !(done & S_DONE))
570 i=BIO_read(s_bio,sbuf,100);
575 if (BIO_should_retry(s_bio))
577 if (BIO_should_read(s_bio))
579 if (BIO_should_write(s_bio))
584 fprintf(stderr,"ERROR in SERVER\n");
585 ERR_print_errors_fp(stderr);
591 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
599 fprintf(stdout,"SERVER:from client:");
600 fwrite(sbuf,1,i,stdout);
607 i=BIO_write(s_bio,"hello from server\n",18);
612 if (BIO_should_retry(s_bio))
614 if (BIO_should_read(s_bio))
616 if (BIO_should_write(s_bio))
621 fprintf(stderr,"ERROR in SERVER\n");
622 ERR_print_errors_fp(stderr);
628 fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
640 if ((done & S_DONE) && (done & C_DONE)) break;
643 SSL_set_shutdown(c_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
644 SSL_set_shutdown(s_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
647 fprintf(stdout,"DONE\n");
650 /* We have to set the BIO's to NULL otherwise they will be
651 * free()ed twice. Once when th s_ssl is SSL_free()ed and
652 * again when c_ssl is SSL_free()ed.
653 * This is a hack required because s_ssl and c_ssl are sharing the same
654 * BIO structure and SSL_set_bio() and SSL_free() automatically
655 * BIO_free non NULL entries.
656 * You should not normally do this or be required to do this */
669 /* The SSL's are optionally freed in the following calls */
670 if (c_to_s != NULL) BIO_free(c_to_s);
671 if (s_to_c != NULL) BIO_free(s_to_c);
673 if (c_bio != NULL) BIO_free(c_bio);
674 if (s_bio != NULL) BIO_free(s_bio);
678 int MS_CALLBACK verify_callback(ok, xs, xi, depth, error, arg)
690 X509_NAME_oneline(X509_get_subject_name(xs),buf,256);
692 fprintf(stderr,"depth=%d %s\n",depth,buf);
694 fprintf(stderr,"depth=%d error=%d %s\n",depth,error,buf);
699 #define THREAD_STACK_SIZE (16*1024)
703 static HANDLE lock_cs[CRYPTO_NUM_LOCKS];
709 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
711 lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
714 CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
715 /* id callback defined */
718 void thread_cleanup()
722 CRYPTO_set_locking_callback(NULL);
723 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
724 CloseHandle(lock_cs[i]);
727 void win32_locking_callback(mode,type,file,line)
733 if (mode & CRYPTO_LOCK)
735 WaitForSingleObject(lock_cs[type],INFINITE);
739 ReleaseMutex(lock_cs[type]);
743 void do_threads(s_ctx,c_ctx)
744 SSL_CTX *s_ctx,*c_ctx;
748 DWORD thread_id[MAX_THREAD_NUMBER];
749 HANDLE thread_handle[MAX_THREAD_NUMBER];
751 SYSTEMTIME start,end;
756 GetSystemTime(&start);
757 for (i=0; i<thread_number; i++)
759 thread_handle[i]=CreateThread(NULL,
761 (LPTHREAD_START_ROUTINE)ndoit,
768 for (i=0; i<thread_number; i+=50)
772 j=(thread_number < (i+50))?(thread_number-i):50;
774 if (WaitForMultipleObjects(j,
775 (CONST HANDLE *)&(thread_handle[i]),TRUE,INFINITE)
778 fprintf(stderr,"WaitForMultipleObjects failed:%d\n",GetLastError());
784 if (start.wDayOfWeek > end.wDayOfWeek) end.wDayOfWeek+=7;
785 ret=(end.wDayOfWeek-start.wDayOfWeek)*24;
787 ret=(ret+end.wHour-start.wHour)*60;
788 ret=(ret+end.wMinute-start.wMinute)*60;
789 ret=(ret+end.wSecond-start.wSecond);
790 ret+=(end.wMilliseconds-start.wMilliseconds)/1000.0;
792 printf("win32 threads done - %.3f seconds\n",ret);
799 static mutex_t lock_cs[CRYPTO_NUM_LOCKS];
800 /*static rwlock_t lock_cs[CRYPTO_NUM_LOCKS]; */
801 static long lock_count[CRYPTO_NUM_LOCKS];
807 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
810 /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
811 mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL);
814 CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
815 CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
818 void thread_cleanup()
822 CRYPTO_set_locking_callback(NULL);
823 fprintf(stderr,"cleanup\n");
824 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
826 /* rwlock_destroy(&(lock_cs[i])); */
827 mutex_destroy(&(lock_cs[i]));
828 fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
830 fprintf(stderr,"done cleanup\n");
833 void solaris_locking_callback(mode,type,file,line)
840 fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
842 (mode&CRYPTO_LOCK)?"l":"u",
843 (type&CRYPTO_READ)?"r":"w",file,line);
847 if (CRYPTO_LOCK_SSL_CERT == type)
848 fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
852 if (mode & CRYPTO_LOCK)
854 /* if (mode & CRYPTO_READ)
855 rw_rdlock(&(lock_cs[type]));
857 rw_wrlock(&(lock_cs[type])); */
859 mutex_lock(&(lock_cs[type]));
864 /* rw_unlock(&(lock_cs[type])); */
865 mutex_unlock(&(lock_cs[type]));
869 void do_threads(s_ctx,c_ctx)
870 SSL_CTX *s_ctx,*c_ctx;
873 thread_t thread_ctx[MAX_THREAD_NUMBER];
879 thr_setconcurrency(thread_number);
880 for (i=0; i<thread_number; i++)
882 thr_create(NULL, THREAD_STACK_SIZE,
890 for (i=0; i<thread_number; i++)
892 thr_join(thread_ctx[i],NULL,NULL);
895 printf("solaris threads done (%d,%d)\n",
896 s_ctx->references,c_ctx->references);
899 unsigned long solaris_thread_id()
903 ret=(unsigned long)thr_self();
911 static usptr_t *arena;
912 static usema_t *lock_cs[CRYPTO_NUM_LOCKS];
919 strcpy(filename,"/tmp/mttest.XXXXXX");
922 usconfig(CONF_STHREADIOOFF);
923 usconfig(CONF_STHREADMALLOCOFF);
924 usconfig(CONF_INITUSERS,100);
925 usconfig(CONF_LOCKTYPE,US_DEBUGPLUS);
926 arena=usinit(filename);
929 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
931 lock_cs[i]=usnewsema(arena,1);
934 CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
935 CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
938 void thread_cleanup()
942 CRYPTO_set_locking_callback(NULL);
943 for (i=0; i<CRYPTO_NUM_LOCKS; i++)
947 sprintf(buf,"%2d:",i);
948 usdumpsema(lock_cs[i],stdout,buf);
949 usfreesema(lock_cs[i],arena);
953 void irix_locking_callback(mode,type,file,line)
959 if (mode & CRYPTO_LOCK)
961 printf("lock %d\n",type);
962 uspsema(lock_cs[type]);
966 printf("unlock %d\n",type);
967 usvsema(lock_cs[type]);
971 void do_threads(s_ctx,c_ctx)
972 SSL_CTX *s_ctx,*c_ctx;
975 int thread_ctx[MAX_THREAD_NUMBER];
981 for (i=0; i<thread_number; i++)
983 thread_ctx[i]=sproc((void (*)())ndoit,
984 PR_SADDR|PR_SFDS,(void *)ssl_ctx);
988 for (i=0; i<thread_number; i++)
993 printf("irix threads done (%d,%d)\n",
994 s_ctx->references,c_ctx->references);
997 unsigned long irix_thread_id()
1001 ret=(unsigned long)getpid();