Update contrib.
2 /* Copyright (C) 1995-1998 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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
117 #define OPENSSL_C /* tells apps.h to use complete apps_startup() */
119 #include <openssl/bio.h>
120 #include <openssl/crypto.h>
121 #include <openssl/lhash.h>
122 #include <openssl/conf.h>
123 #include <openssl/x509.h>
124 #include <openssl/pem.h>
125 #include <openssl/ssl.h>
126 #ifndef OPENSSL_NO_ENGINE
127 #include <openssl/engine.h>
129 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
132 #include <openssl/err.h>
134 #include "topenssl.h"
137 /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
138 * base prototypes (we cast each variable inside the function to the required
139 * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
142 /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
143 static unsigned long MS_CALLBACK hash(const void *a_void);
144 /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
145 static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
146 static LHASH *prog_init(void );
147 static int do_cmd(LHASH *prog,int argc,char *argv[]);
148 char *default_config_file=NULL;
150 /* Make sure there is only one when MONOLITH is defined */
163 static void lock_dbg_cb(int mode, int type, const char *file, int line)
165 static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
166 const char *errstr = NULL;
169 rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
170 if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
172 errstr = "invalid mode";
176 if (type < 0 || type >= CRYPTO_NUM_LOCKS)
178 errstr = "type out of bounds";
182 if (mode & CRYPTO_LOCK)
186 errstr = "already locked";
187 /* must not happen in a single-threaded program
188 * (would deadlock) */
194 else if (mode & CRYPTO_UNLOCK)
198 errstr = "not locked";
202 if (modes[type] != rw)
204 errstr = (rw == CRYPTO_READ) ?
205 "CRYPTO_r_unlock on write lock" :
206 "CRYPTO_w_unlock on read lock";
213 errstr = "invalid mode";
220 /* we cannot use bio_err here */
222 fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
223 errstr, mode, type, file, line);
225 fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
226 errstr, mode, type, file, line);
235 void testResultXml(char *filename,int retval)
241 char xmlfilename[256];
243 time_t t = time(NULL);
245 struct tm *tm1 = localtime(&t);
247 char *atsinitmsg = "<test-report>\n\t<test-batch>";
249 char *atsbatchinit1 = \
251 \n\t\t\t<description></description>\
254 char *atsbatchinit2 = "</date>\
255 \n\t\t\t<factory>NA</factory>\
257 \n\t\t\t\t<name>NA</name>\
258 \n\t\t\t\t<version>NA</version>\
259 \n\t\t\t</component>\
260 \n\t\t</batch-init>";
262 char *atsbatchresult= \
263 "\n\t\t<batch-result>\
264 \n\t\t\t<run-time>00:00:00</run-time>\
265 \n\t\t</batch-result>";
267 char *atsclosemsg = \
269 \n</test-report>\n ";
271 char *atstestinit = "\n\t\t<test-case time-stamp=\"00:00:00\">";
274 char *atscaseinit1 = \
275 "\n\t\t\t<case-init>\
276 \n\t\t\t\t<version></version>\
279 char *atscaseinit2 = "</id>\
280 \n\t\t\t\t<expected-result description=\"\">0</expected-result>\
281 \n\t\t\t</case-init>";
283 char *atscaseresult1= \
284 "\n\t\t\t<case-result status=\"";
286 char *atscaseresult2= "\">\
287 \n\t\t\t\t<actual-result>0</actual-result>\
288 \n\t\t\t\t<run-time>00:00:00</run-time>\
289 \n\t\t\t</case-result>";
291 char *atstestclose = "\n\t\t</test-case>";
296 dir = opendir("c:\\spd_logs");
298 mkdir("c:\\spd_logs",0777);
300 dir = opendir("c:\\spd_logs\\xml");
302 mkdir("c:\\spd_logs\\xml",0777);
304 // create the xml file name
305 strcpy(xmlfilename,"c:/spd_logs/xml/");
306 strcat(xmlfilename,filename);
307 strcat(xmlfilename,".xml");
309 strftime(time_buf,50,"%c",tm1);
312 strcpy(result,"FAILED");
314 strcpy(result,"PASSED");
316 fp = fopen(xmlfilename,"w");
320 fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit,
321 atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2,
322 atstestclose,atsbatchresult,atsclosemsg);
329 int openssl_init(void)
332 fp_stdout = freopen(LOG_STDOUT,"w+",stdout);
335 fp_stderr = freopen(LOG_STDERR,"w+",stderr);
338 fp_stdin = freopen(LOG_STDIN,"w+",stdin);
344 void openssl_deinit(void)
355 int append(char *file1,char *file2, char *dst_file )
363 char *file1_data = NULL;
364 char *file2_data = NULL;
368 if(!(fp1 = fopen(file1,"r+")))
370 fprintf(stdout,"Faliure opening file %s.",file1);
375 if(!(fp2 = fopen(file2,"r+")))
377 fprintf(stdout,"Faliure opening file %s.",file2);
382 if(!(fp3= fopen(dst_file,"w+")))
384 fprintf(stdout,"Faliure opening file %s.",dst_file);
389 if((ret = fseek(fp1, 0, SEEK_END)) < 0)
396 if((ret = fseek(fp2, 0, SEEK_END)) < 0)
403 if((file1_size = ftell(fp1)) < 0)
409 if((file2_size = ftell(fp2)) < 0)
415 if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
421 if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
427 file1_data = (char *)malloc(file1_size);
428 file2_data = (char *)malloc(file2_size);
430 if(!file1_data||!file2_data)
435 memset(file1_data,0,file1_size);
437 memset(file2_data,0,file2_size);
439 ret = fread(file1_data,sizeof(char),file1_size,fp1);
442 fprintf(stdout,"Failed reading file %s.", file1);
447 ret = fread(file2_data,sizeof(char),file2_size,fp2);
450 fprintf(stdout,"Failed reading file %s.",file2);
455 ret = fwrite(file1_data,sizeof(char),file1_size,fp3);
458 fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file);
462 ret = fwrite(file2_data,sizeof(char),file2_size,fp3);
465 fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file);
481 void print_logs(int argc, char *argv[])
484 for(i=0;i<argc-3;i++)
486 fprintf(stdout,"%s ",argv[i+3]);
488 fprintf(stdout,"\n");
492 char** create_params(char *ip_str,int *argc)
495 char **cmd_line=NULL;
496 char *str=NULL,*ptr=NULL;
509 paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
512 strncpy(paramets[cnt],str,len);
513 paramets[cnt][len]='\0';
521 paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
523 strcpy(paramets[cnt],str);
531 cmd_line=(char **)malloc(cnt*sizeof(char *));
533 cmd_line[i]=paramets[i];
542 int compare_files(const char *file1, const char *file2)
551 char *file1_data = NULL;
552 char *file2_data = NULL;
559 if(!(fp1 = fopen(file1,"r+"))){
560 fprintf(stdout,"Faliure opening file %s.",file1);
565 if(!(fp2 = fopen(file2,"r+"))){
566 fprintf(stdout,"Faliure opening file %s.",file2);
571 if((ret = fseek(fp1, 0, SEEK_END)) < 0)
574 if((ret = fseek(fp2, 0, SEEK_END)) < 0)
577 if((file1_size = ftell(fp1)) < 0)
582 if((file2_size = ftell(fp1)) < 0)
588 if(file1_size != file2_size)
590 fprintf(stdout,"File size of %s is not same as that of %s.",file1,file2);
595 if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
598 if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
601 file1_data = (char *)malloc(file1_size);
602 file2_data = (char *)malloc(file2_size);
604 memset(file1_data,0,file1_size);
605 memset(file2_data,0,file2_size);
607 ret = fread(file1_data,sizeof(char),file1_size,fp1);
611 fprintf(stdout,"Failed reading file %s.", file1);
616 ret = fread(file2_data,sizeof(char),file2_size,fp2);
619 fprintf(stdout,"Failed reading file %s.",file2);
624 if(memcmp(file1_data,file2_data,file1_size))
627 fprintf(stdout,"File file1 is not similar to file2.");
632 ret=0; //file are smae
649 void delete_file(char *file)
654 void delete_params(char ** cmd_line,int argc)
658 if(cmd_line[i]) free(cmd_line[i]);
664 void strip(char *str)
670 ptr=strchr(str,'\r');
671 ptr1=strchr(str,'\n');
675 *ptr='\0'; //strip \r and \n
683 int main(int argc_m, char *argv_m[])
688 int total_cases,cur_case=0,fail=0;
690 char openssltestnum[20];
702 arg_fp = fopen(argv_m[1],"r+");
704 return 1; // filename needed as argument.
712 while(!feof(arg_fp)&&!fail)
714 memset(argstr,0,200);
715 fgets(argstr, 200, arg_fp);
717 argv = create_params(argstr,&argc);
718 strcpy(openssltestnum,argv[1]);
720 if(!strcmp(argv[2],"test_log"))
722 print_logs(argc,argv);
724 else if(!strcmp(argv[2],"compare"))
726 ret = compare_files(argv[3],argv[4]);
729 testResultXml(openssltestnum, ret);
734 else if(!strcmp(argv[2],"append"))
736 ret = append(argv[3],argv[4],argv[5]);
739 testResultXml(openssltestnum, ret);
744 else if(!strcmp(argv[2],"delete"))
746 delete_file(argv[3]);
751 ret = openssl_main(argc-2,argv+2);
754 testResultXml(openssltestnum, ret);
760 //delete_params(argv,argc);
764 testResultXml(openssltestnum, fail);
772 #endif /* IF_DEF SYMBIAN */
774 int main(int Argc, char *Argv[])
776 int openssl_main(int Argc, char *Argv[])
780 #define PROG_NAME_SIZE 39
781 char pname[PROG_NAME_SIZE+1];
783 MS_STATIC const char *prompt;
784 MS_STATIC char buf[1024];
796 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
798 BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT);
800 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
802 if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
804 if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
806 CRYPTO_malloc_debug_init();
807 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
811 /* OPENSSL_DEBUG_MEMORY=off */
812 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
815 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
818 if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
821 CRYPTO_set_locking_callback(lock_dbg_cb);
826 /* Lets load up our environment a little */
827 p=getenv("OPENSSL_CONF");
829 p=getenv("SSLEAY_CONF");
831 p=to_free=make_config_name();
833 default_config_file=p;
835 config=NCONF_new(NULL);
836 i=NCONF_load(config,p,&errline);
846 /* first check the program name */
847 program_name(Argv[0],pname,sizeof pname);
850 fp=(FUNCTION *)lh_retrieve(prog,&f);
854 ret=fp->func(Argc,Argv);
858 /* ok, now check that there are not arguments, if there are,
859 * run with them, shifting the ssleay off the front */
864 ret=do_cmd(prog,Argc,Argv);
869 /* ok, lets enter the old 'OpenSSL>' mode */
882 else prompt="OpenSSL> ";
884 fputs(prompt,stdout);
888 fputs(prompt,stdout);
893 if (p[0] == '\0') goto end;
896 if (p[i-2] != '\\') break;
901 if (!chopup_args(&arg,buf,&argc,&argv)) break;
903 ret=do_cmd(prog,argc,argv);
910 BIO_printf(bio_err,"error in %s\n",argv[0]);
911 (void)BIO_flush(bio_err);
913 BIO_printf(bio_err,"bad exit\n");
917 OPENSSL_free(to_free);
923 if (prog != NULL) lh_free(prog);
924 if (arg.data != NULL) OPENSSL_free(arg.data);
928 CRYPTO_mem_leaks(bio_err);
936 // OPENSSL_EXIT(ret);
939 #define LIST_STANDARD_COMMANDS "list-standard-commands"
940 #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
941 #define LIST_CIPHER_COMMANDS "list-cipher-commands"
943 static int do_cmd(LHASH *prog, int argc, char *argv[])
948 if ((argc <= 0) || (argv[0] == NULL))
951 fp=(FUNCTION *)lh_retrieve(prog,&f);
954 ret=fp->func(argc,argv);
956 else if ((strncmp(argv[0],"no-",3)) == 0)
959 BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
960 #ifdef OPENSSL_SYS_VMS
962 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
963 bio_stdout = BIO_push(tmpbio, bio_stdout);
967 ret = (lh_retrieve(prog,&f) != NULL);
969 BIO_printf(bio_stdout, "%s\n", argv[0]);
971 BIO_printf(bio_stdout, "%s\n", argv[0]+3);
972 BIO_free_all(bio_stdout);
975 else if ((strcmp(argv[0],"quit") == 0) ||
976 (strcmp(argv[0],"q") == 0) ||
977 (strcmp(argv[0],"exit") == 0) ||
978 (strcmp(argv[0],"bye") == 0))
983 else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
984 (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
985 (strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
990 if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
991 list_type = FUNC_TYPE_GENERAL;
992 else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
993 list_type = FUNC_TYPE_MD;
994 else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
995 list_type = FUNC_TYPE_CIPHER;
997 bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
998 #ifdef OPENSSL_SYS_VMS
1000 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1001 bio_stdout = BIO_push(tmpbio, bio_stdout);
1005 for (fp=functions; fp->name != NULL; fp++)
1006 if (fp->type == list_type)
1007 BIO_printf(bio_stdout, "%s\n", fp->name);
1008 BIO_free_all(bio_stdout);
1014 BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
1016 BIO_printf(bio_err, "\nStandard commands");
1019 for (fp=functions; fp->name != NULL; fp++)
1022 if (((i++) % 5) == 0)
1024 BIO_printf(bio_err,"\n");
1030 if (!nl) BIO_printf(bio_err,"\n");
1031 if (tp == FUNC_TYPE_MD)
1035 "\nMessage Digest commands (see the `dgst' command for more details)\n");
1037 else if (tp == FUNC_TYPE_CIPHER)
1040 BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
1043 BIO_printf(bio_err,"%-15s",fp->name);
1045 BIO_printf(bio_err,"\n\n");
1052 static int SortFnByName(const void *_f1,const void *_f2)
1054 const FUNCTION *f1=_f1;
1055 const FUNCTION *f2=_f2;
1057 if(f1->type != f2->type)
1058 return f1->type-f2->type;
1059 return strcmp(f1->name,f2->name);
1062 static LHASH *prog_init(void)
1068 /* Purely so it looks nice when the user hits ? */
1069 for(i=0,f=functions ; f->name != NULL ; ++f,++i)
1071 qsort(functions,i,sizeof *functions,SortFnByName);
1073 if ((ret=lh_new(hash, cmp)) == NULL)
1076 for (f=functions; f->name != NULL; f++)
1081 /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
1082 static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
1084 return(strncmp(((const FUNCTION *)a_void)->name,
1085 ((const FUNCTION *)b_void)->name,8));
1088 /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
1089 static unsigned long MS_CALLBACK hash(const void *a_void)
1091 return(lh_strhash(((const FUNCTION *)a_void)->name));