First public contribution.
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 #include <sys/stat.h>
127 #ifndef OPENSSL_NO_ENGINE
128 #include <openssl/engine.h>
130 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
133 #include <openssl/err.h>
135 #include "topenssl.h"
138 /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
139 * base prototypes (we cast each variable inside the function to the required
140 * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
143 /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
144 static unsigned long MS_CALLBACK hash(const void *a_void);
145 /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
146 static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
147 static LHASH *prog_init(void );
148 static int do_cmd(LHASH *prog,int argc,char *argv[]);
149 char *default_config_file=NULL;
151 /* Make sure there is only one when MONOLITH is defined */
164 static void lock_dbg_cb(int mode, int type, const char *file, int line)
166 static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
167 const char *errstr = NULL;
170 rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
171 if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
173 errstr = "invalid mode";
177 if (type < 0 || type >= CRYPTO_NUM_LOCKS)
179 errstr = "type out of bounds";
183 if (mode & CRYPTO_LOCK)
187 errstr = "already locked";
188 /* must not happen in a single-threaded program
189 * (would deadlock) */
195 else if (mode & CRYPTO_UNLOCK)
199 errstr = "not locked";
203 if (modes[type] != rw)
205 errstr = (rw == CRYPTO_READ) ?
206 "CRYPTO_r_unlock on write lock" :
207 "CRYPTO_w_unlock on read lock";
214 errstr = "invalid mode";
221 /* we cannot use bio_err here */
223 fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
224 errstr, mode, type, file, line);
226 fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
227 errstr, mode, type, file, line);
236 void testResultXml(char *filename,int retval)
242 char xmlfilename[256];
244 time_t t = time(NULL);
246 struct tm *tm1 = localtime(&t);
248 char *atsinitmsg = "<test-report>\n\t<test-batch>";
250 char *atsbatchinit1 = \
252 \n\t\t\t<description></description>\
255 char *atsbatchinit2 = "</date>\
256 \n\t\t\t<factory>NA</factory>\
258 \n\t\t\t\t<name>NA</name>\
259 \n\t\t\t\t<version>NA</version>\
260 \n\t\t\t</component>\
261 \n\t\t</batch-init>";
263 char *atsbatchresult= \
264 "\n\t\t<batch-result>\
265 \n\t\t\t<run-time>00:00:00</run-time>\
266 \n\t\t</batch-result>";
268 char *atsclosemsg = \
270 \n</test-report>\n ";
272 char *atstestinit = "\n\t\t<test-case time-stamp=\"00:00:00\">";
275 char *atscaseinit1 = \
276 "\n\t\t\t<case-init>\
277 \n\t\t\t\t<version></version>\
280 char *atscaseinit2 = "</id>\
281 \n\t\t\t\t<expected-result description=\"\">0</expected-result>\
282 \n\t\t\t</case-init>";
284 char *atscaseresult1= \
285 "\n\t\t\t<case-result status=\"";
287 char *atscaseresult2= "\">\
288 \n\t\t\t\t<actual-result>0</actual-result>\
289 \n\t\t\t\t<run-time>00:00:00</run-time>\
290 \n\t\t\t</case-result>";
292 char *atstestclose = "\n\t\t</test-case>";
297 dir = opendir("c:\\spd_logs");
299 mkdir("c:\\spd_logs",0777);
301 dir = opendir("c:\\spd_logs\\xml");
303 mkdir("c:\\spd_logs\\xml",0777);
305 // create the xml file name
306 strcpy(xmlfilename,"c:/spd_logs/xml/");
307 strcat(xmlfilename,filename);
308 strcat(xmlfilename,".xml");
310 strftime(time_buf,50,"%c",tm1);
313 strcpy(result,"FAILED");
315 strcpy(result,"PASSED");
317 fp = fopen(xmlfilename,"w");
321 fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit,
322 atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2,
323 atstestclose,atsbatchresult,atsclosemsg);
330 int openssl_init(void)
333 fp_stdout = freopen(LOG_STDOUT,"w+",stdout);
336 fp_stderr = freopen(LOG_STDERR,"w+",stderr);
339 fp_stdin = freopen(LOG_STDIN,"w+",stdin);
345 void openssl_deinit(void)
356 int append(char *file1,char *file2, char *dst_file )
364 char *file1_data = NULL;
365 char *file2_data = NULL;
369 if(!(fp1 = fopen(file1,"r+")))
371 fprintf(stdout,"Faliure opening file %s.",file1);
376 if(!(fp2 = fopen(file2,"r+")))
378 fprintf(stdout,"Faliure opening file %s.",file2);
383 if(!(fp3= fopen(dst_file,"w+")))
385 fprintf(stdout,"Faliure opening file %s.",dst_file);
390 if((ret = fseek(fp1, 0, SEEK_END)) < 0)
397 if((ret = fseek(fp2, 0, SEEK_END)) < 0)
404 if((file1_size = ftell(fp1)) < 0)
410 if((file2_size = ftell(fp2)) < 0)
416 if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
422 if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
428 file1_data = (char *)malloc(file1_size);
429 file2_data = (char *)malloc(file2_size);
431 if(!file1_data||!file2_data)
436 memset(file1_data,0,file1_size);
438 memset(file2_data,0,file2_size);
440 ret = fread(file1_data,sizeof(char),file1_size,fp1);
443 fprintf(stdout,"Failed reading file %s.", file1);
448 ret = fread(file2_data,sizeof(char),file2_size,fp2);
451 fprintf(stdout,"Failed reading file %s.",file2);
456 ret = fwrite(file1_data,sizeof(char),file1_size,fp3);
459 fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file);
463 ret = fwrite(file2_data,sizeof(char),file2_size,fp3);
466 fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file);
482 void print_logs(int argc, char *argv[])
485 for(i=0;i<argc-3;i++)
487 fprintf(stdout,"%s ",argv[i+3]);
489 fprintf(stdout,"\n");
493 char** create_params(char *ip_str,int *argc)
496 char **cmd_line=NULL;
497 char *str=NULL,*ptr=NULL;
510 paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
513 strncpy(paramets[cnt],str,len);
514 paramets[cnt][len]='\0';
522 paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
524 strcpy(paramets[cnt],str);
532 cmd_line=(char **)malloc(cnt*sizeof(char *));
534 cmd_line[i]=paramets[i];
543 int compare_files(const char *file1, const char *file2)
552 char *file1_data = NULL;
553 char *file2_data = NULL;
560 if(!(fp1 = fopen(file1,"r+"))){
561 fprintf(stdout,"Faliure opening file %s.",file1);
566 if(!(fp2 = fopen(file2,"r+"))){
567 fprintf(stdout,"Faliure opening file %s.",file2);
572 if((ret = fseek(fp1, 0, SEEK_END)) < 0)
575 if((ret = fseek(fp2, 0, SEEK_END)) < 0)
578 if((file1_size = ftell(fp1)) < 0)
583 if((file2_size = ftell(fp1)) < 0)
589 if(file1_size != file2_size)
591 fprintf(stdout,"File size of %s is not same as that of %s.",file1,file2);
596 if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
599 if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
602 file1_data = (char *)malloc(file1_size);
603 file2_data = (char *)malloc(file2_size);
605 memset(file1_data,0,file1_size);
606 memset(file2_data,0,file2_size);
608 ret = fread(file1_data,sizeof(char),file1_size,fp1);
612 fprintf(stdout,"Failed reading file %s.", file1);
617 ret = fread(file2_data,sizeof(char),file2_size,fp2);
620 fprintf(stdout,"Failed reading file %s.",file2);
625 if(memcmp(file1_data,file2_data,file1_size))
628 fprintf(stdout,"File file1 is not similar to file2.");
633 ret=0; //file are smae
650 void delete_file(char *file)
655 void delete_params(char ** cmd_line,int argc)
659 if(cmd_line[i]) free(cmd_line[i]);
665 void strip(char *str)
671 ptr=strchr(str,'\r');
672 ptr1=strchr(str,'\n');
676 *ptr='\0'; //strip \r and \n
684 int main(int argc_m, char *argv_m[])
689 int total_cases,cur_case=0,fail=0;
691 char openssltestnum[20];
701 arg_fp = fopen(argv_m[1],"r+");
703 return 1; // filename needed as argument.
711 while(!feof(arg_fp)&&!fail)
713 memset(argstr,0,200);
714 fgets(argstr, 200, arg_fp);
716 argv = create_params(argstr,&argc);
717 strcpy(openssltestnum,argv[1]);
719 if(!strcmp(argv[2],"test_log"))
721 print_logs(argc,argv);
723 else if(!strcmp(argv[2],"compare"))
725 ret = compare_files(argv[3],argv[4]);
728 testResultXml(openssltestnum, ret);
733 else if(!strcmp(argv[2],"append"))
735 ret = append(argv[3],argv[4],argv[5]);
738 testResultXml(openssltestnum, ret);
743 else if(!strcmp(argv[2],"delete"))
745 delete_file(argv[3]);
750 ret = openssl_main(argc-2,argv+2);
753 testResultXml(openssltestnum, ret);
759 //delete_params(argv,argc);
763 testResultXml(openssltestnum, fail);
771 #endif /* IF_DEF SYMBIAN */
773 int main(int Argc, char *Argv[])
775 int openssl_main(int Argc, char *Argv[])
779 #define PROG_NAME_SIZE 39
780 char pname[PROG_NAME_SIZE+1];
782 MS_STATIC const char *prompt;
783 MS_STATIC char buf[1024];
795 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
797 BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT);
799 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
801 if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
803 if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
805 CRYPTO_malloc_debug_init();
806 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
810 /* OPENSSL_DEBUG_MEMORY=off */
811 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
814 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
817 if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
820 CRYPTO_set_locking_callback(lock_dbg_cb);
825 /* Lets load up our environment a little */
826 p=getenv("OPENSSL_CONF");
828 p=getenv("SSLEAY_CONF");
830 p=to_free=make_config_name();
832 default_config_file=p;
834 config=NCONF_new(NULL);
835 i=NCONF_load(config,p,&errline);
845 /* first check the program name */
846 program_name(Argv[0],pname,sizeof pname);
849 fp=(FUNCTION *)lh_retrieve(prog,&f);
853 ret=fp->func(Argc,Argv);
857 /* ok, now check that there are not arguments, if there are,
858 * run with them, shifting the ssleay off the front */
863 ret=do_cmd(prog,Argc,Argv);
868 /* ok, lets enter the old 'OpenSSL>' mode */
881 else prompt="OpenSSL> ";
883 fputs(prompt,stdout);
887 fputs(prompt,stdout);
892 if (p[0] == '\0') goto end;
895 if (p[i-2] != '\\') break;
900 if (!chopup_args(&arg,buf,&argc,&argv)) break;
902 ret=do_cmd(prog,argc,argv);
909 BIO_printf(bio_err,"error in %s\n",argv[0]);
910 (void)BIO_flush(bio_err);
912 BIO_printf(bio_err,"bad exit\n");
916 OPENSSL_free(to_free);
922 if (prog != NULL) lh_free(prog);
923 if (arg.data != NULL) OPENSSL_free(arg.data);
927 CRYPTO_mem_leaks(bio_err);
935 // OPENSSL_EXIT(ret);
938 #define LIST_STANDARD_COMMANDS "list-standard-commands"
939 #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
940 #define LIST_CIPHER_COMMANDS "list-cipher-commands"
942 static int do_cmd(LHASH *prog, int argc, char *argv[])
947 if ((argc <= 0) || (argv[0] == NULL))
950 fp=(FUNCTION *)lh_retrieve(prog,&f);
953 ret=fp->func(argc,argv);
955 else if ((strncmp(argv[0],"no-",3)) == 0)
958 BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
959 #ifdef OPENSSL_SYS_VMS
961 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
962 bio_stdout = BIO_push(tmpbio, bio_stdout);
966 ret = (lh_retrieve(prog,&f) != NULL);
968 BIO_printf(bio_stdout, "%s\n", argv[0]);
970 BIO_printf(bio_stdout, "%s\n", argv[0]+3);
971 BIO_free_all(bio_stdout);
974 else if ((strcmp(argv[0],"quit") == 0) ||
975 (strcmp(argv[0],"q") == 0) ||
976 (strcmp(argv[0],"exit") == 0) ||
977 (strcmp(argv[0],"bye") == 0))
982 else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
983 (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
984 (strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
989 if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
990 list_type = FUNC_TYPE_GENERAL;
991 else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
992 list_type = FUNC_TYPE_MD;
993 else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
994 list_type = FUNC_TYPE_CIPHER;
996 bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
997 #ifdef OPENSSL_SYS_VMS
999 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1000 bio_stdout = BIO_push(tmpbio, bio_stdout);
1004 for (fp=functions; fp->name != NULL; fp++)
1005 if (fp->type == list_type)
1006 BIO_printf(bio_stdout, "%s\n", fp->name);
1007 BIO_free_all(bio_stdout);
1013 BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
1015 BIO_printf(bio_err, "\nStandard commands");
1018 for (fp=functions; fp->name != NULL; fp++)
1021 if (((i++) % 5) == 0)
1023 BIO_printf(bio_err,"\n");
1029 if (!nl) BIO_printf(bio_err,"\n");
1030 if (tp == FUNC_TYPE_MD)
1034 "\nMessage Digest commands (see the `dgst' command for more details)\n");
1036 else if (tp == FUNC_TYPE_CIPHER)
1039 BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
1042 BIO_printf(bio_err,"%-15s",fp->name);
1044 BIO_printf(bio_err,"\n\n");
1051 static int SortFnByName(const void *_f1,const void *_f2)
1053 const FUNCTION *f1=_f1;
1054 const FUNCTION *f2=_f2;
1056 if(f1->type != f2->type)
1057 return f1->type-f2->type;
1058 return strcmp(f1->name,f2->name);
1061 static LHASH *prog_init(void)
1067 /* Purely so it looks nice when the user hits ? */
1068 for(i=0,f=functions ; f->name != NULL ; ++f,++i)
1070 qsort(functions,i,sizeof *functions,SortFnByName);
1072 if ((ret=lh_new(hash, cmp)) == NULL)
1075 for (f=functions; f->name != NULL; f++)
1080 /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
1081 static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
1083 return(strncmp(((const FUNCTION *)a_void)->name,
1084 ((const FUNCTION *)b_void)->name,8));
1087 /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
1088 static unsigned long MS_CALLBACK hash(const void *a_void)
1090 return(lh_strhash(((const FUNCTION *)a_void)->name));