sl@0: /* apps/openssl.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #define OPENSSL_C /* tells apps.h to use complete apps_startup() */ sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: #include sl@0: #endif sl@0: #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */ sl@0: #include "progs.h" sl@0: #include "s_apps.h" sl@0: #include sl@0: #ifdef SYMBIAN sl@0: #include "topenssl.h" sl@0: #endif sl@0: sl@0: /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the sl@0: * base prototypes (we cast each variable inside the function to the required sl@0: * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper sl@0: * functions. */ sl@0: sl@0: /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */ sl@0: static unsigned long MS_CALLBACK hash(const void *a_void); sl@0: /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */ sl@0: static int MS_CALLBACK cmp(const void *a_void,const void *b_void); sl@0: static LHASH *prog_init(void ); sl@0: static int do_cmd(LHASH *prog,int argc,char *argv[]); sl@0: char *default_config_file=NULL; sl@0: sl@0: /* Make sure there is only one when MONOLITH is defined */ sl@0: #ifdef MONOLITH sl@0: CONF *config=NULL; sl@0: BIO *bio_err=NULL; sl@0: #endif sl@0: sl@0: #ifdef SYMBIAN sl@0: FILE *fp_stdout; sl@0: FILE *fp_stderr; sl@0: FILE *fp_stdin; sl@0: #endif sl@0: sl@0: sl@0: static void lock_dbg_cb(int mode, int type, const char *file, int line) sl@0: { sl@0: static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */ sl@0: const char *errstr = NULL; sl@0: int rw; sl@0: sl@0: rw = mode & (CRYPTO_READ|CRYPTO_WRITE); sl@0: if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) sl@0: { sl@0: errstr = "invalid mode"; sl@0: goto err; sl@0: } sl@0: sl@0: if (type < 0 || type >= CRYPTO_NUM_LOCKS) sl@0: { sl@0: errstr = "type out of bounds"; sl@0: goto err; sl@0: } sl@0: sl@0: if (mode & CRYPTO_LOCK) sl@0: { sl@0: if (modes[type]) sl@0: { sl@0: errstr = "already locked"; sl@0: /* must not happen in a single-threaded program sl@0: * (would deadlock) */ sl@0: goto err; sl@0: } sl@0: sl@0: modes[type] = rw; sl@0: } sl@0: else if (mode & CRYPTO_UNLOCK) sl@0: { sl@0: if (!modes[type]) sl@0: { sl@0: errstr = "not locked"; sl@0: goto err; sl@0: } sl@0: sl@0: if (modes[type] != rw) sl@0: { sl@0: errstr = (rw == CRYPTO_READ) ? sl@0: "CRYPTO_r_unlock on write lock" : sl@0: "CRYPTO_w_unlock on read lock"; sl@0: } sl@0: sl@0: modes[type] = 0; sl@0: } sl@0: else sl@0: { sl@0: errstr = "invalid mode"; sl@0: goto err; sl@0: } sl@0: sl@0: err: sl@0: if (errstr) sl@0: { sl@0: /* we cannot use bio_err here */ sl@0: #ifndef SYMBIAN sl@0: fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n", sl@0: errstr, mode, type, file, line); sl@0: #else sl@0: fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n", sl@0: errstr, mode, type, file, line); sl@0: fclose(fp_stderr) ; sl@0: #endif sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: #ifdef SYMBIAN sl@0: void testResultXml(char *filename,int retval) sl@0: { sl@0: char time_buf[50]; sl@0: sl@0: char result[10]; sl@0: sl@0: char xmlfilename[256]; sl@0: sl@0: time_t t = time(NULL); sl@0: sl@0: struct tm *tm1 = localtime(&t); sl@0: sl@0: char *atsinitmsg = "\n\t"; sl@0: sl@0: char *atsbatchinit1 = \ sl@0: "\n\t\t\ sl@0: \n\t\t\t\ sl@0: \n\t\t\t"; sl@0: sl@0: char *atsbatchinit2 = "\ sl@0: \n\t\t\tNA\ sl@0: \n\t\t\t\ sl@0: \n\t\t\t\tNA\ sl@0: \n\t\t\t\tNA\ sl@0: \n\t\t\t\ sl@0: \n\t\t"; sl@0: sl@0: char *atsbatchresult= \ sl@0: "\n\t\t\ sl@0: \n\t\t\t00:00:00\ sl@0: \n\t\t"; sl@0: sl@0: char *atsclosemsg = \ sl@0: "\n\t\ sl@0: \n\n "; sl@0: sl@0: char *atstestinit = "\n\t\t"; sl@0: sl@0: sl@0: char *atscaseinit1 = \ sl@0: "\n\t\t\t\ sl@0: \n\t\t\t\t\ sl@0: \n\t\t\t\t"; sl@0: sl@0: char *atscaseinit2 = "\ sl@0: \n\t\t\t\t0\ sl@0: \n\t\t\t"; sl@0: sl@0: char *atscaseresult1= \ sl@0: "\n\t\t\t\ sl@0: \n\t\t\t\t0\ sl@0: \n\t\t\t\t00:00:00\ sl@0: \n\t\t\t"; sl@0: sl@0: char *atstestclose = "\n\t\t"; sl@0: sl@0: DIR *dir; sl@0: FILE *fp; sl@0: sl@0: dir = opendir("c:\\spd_logs"); sl@0: if(!dir) sl@0: mkdir("c:\\spd_logs",0777); sl@0: sl@0: dir = opendir("c:\\spd_logs\\xml"); sl@0: if(!dir) sl@0: mkdir("c:\\spd_logs\\xml",0777); sl@0: sl@0: // create the xml file name sl@0: strcpy(xmlfilename,"c:/spd_logs/xml/"); sl@0: strcat(xmlfilename,filename); sl@0: strcat(xmlfilename,".xml"); sl@0: sl@0: strftime(time_buf,50,"%c",tm1); sl@0: sl@0: if(retval) sl@0: strcpy(result,"FAILED"); sl@0: else sl@0: strcpy(result,"PASSED"); sl@0: sl@0: fp = fopen(xmlfilename,"w"); sl@0: sl@0: if(fp) sl@0: { sl@0: fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit, sl@0: atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2, sl@0: atstestclose,atsbatchresult,atsclosemsg); sl@0: sl@0: fclose(fp); sl@0: } sl@0: } sl@0: sl@0: sl@0: int openssl_init(void) sl@0: { sl@0: sl@0: fp_stdout = freopen(LOG_STDOUT,"w+",stdout); sl@0: if(!fp_stdout) sl@0: return 1; sl@0: fp_stderr = freopen(LOG_STDERR,"w+",stderr); sl@0: if(!fp_stderr) sl@0: return 1; sl@0: fp_stdin = freopen(LOG_STDIN,"w+",stdin); sl@0: if(!fp_stdin) sl@0: return 1; sl@0: return 0; sl@0: } sl@0: sl@0: void openssl_deinit(void) sl@0: { sl@0: fclose(fp_stdout); sl@0: fclose(fp_stderr); sl@0: fclose(fp_stdin); sl@0: fp_stdout=NULL; sl@0: fp_stdin=NULL; sl@0: fp_stderr=NULL; sl@0: sl@0: } sl@0: sl@0: int append(char *file1,char *file2, char *dst_file ) sl@0: { sl@0: FILE *fp1 = NULL; sl@0: FILE *fp2 = NULL; sl@0: FILE *fp3 = NULL; sl@0: sl@0: long file1_size; sl@0: long file2_size; sl@0: char *file1_data = NULL; sl@0: char *file2_data = NULL; sl@0: sl@0: int ret; sl@0: sl@0: if(!(fp1 = fopen(file1,"r+"))) sl@0: { sl@0: fprintf(stdout,"Faliure opening file %s.",file1); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if(!(fp2 = fopen(file2,"r+"))) sl@0: { sl@0: fprintf(stdout,"Faliure opening file %s.",file2); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if(!(fp3= fopen(dst_file,"w+"))) sl@0: { sl@0: fprintf(stdout,"Faliure opening file %s.",dst_file); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if((ret = fseek(fp1, 0, SEEK_END)) < 0) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: sl@0: if((ret = fseek(fp2, 0, SEEK_END)) < 0) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: sl@0: if((file1_size = ftell(fp1)) < 0) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if((file2_size = ftell(fp2)) < 0) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if((ret = fseek(fp1, 0, SEEK_SET)) < 0) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: if((ret = fseek(fp2, 0, SEEK_SET)) < 0) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: file1_data = (char *)malloc(file1_size); sl@0: file2_data = (char *)malloc(file2_size); sl@0: sl@0: if(!file1_data||!file2_data) sl@0: { sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: memset(file1_data,0,file1_size); sl@0: sl@0: memset(file2_data,0,file2_size); sl@0: sl@0: ret = fread(file1_data,sizeof(char),file1_size,fp1); sl@0: if(ret < file1_size) sl@0: { sl@0: fprintf(stdout,"Failed reading file %s.", file1); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: ret = fread(file2_data,sizeof(char),file2_size,fp2); sl@0: if(ret < file2_size) sl@0: { sl@0: fprintf(stdout,"Failed reading file %s.",file2); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: sl@0: ret = fwrite(file1_data,sizeof(char),file1_size,fp3); sl@0: if(ret < file1_size) sl@0: { sl@0: fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: ret = fwrite(file2_data,sizeof(char),file2_size,fp3); sl@0: if(ret < file2_size) sl@0: { sl@0: fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file); sl@0: ret = -1; sl@0: goto err; sl@0: } sl@0: ret=0; sl@0: sl@0: err: if(file1_data) sl@0: free(file1_data); sl@0: if(file2_data) sl@0: free(file2_data); sl@0: fclose(fp1); sl@0: fclose(fp2); sl@0: fclose(fp3); sl@0: return ret; sl@0: sl@0: } sl@0: void print_logs(int argc, char *argv[]) sl@0: { sl@0: int i=0; sl@0: for(i=0;i 1) sl@0: arg_fp = fopen(argv_m[1],"r+"); sl@0: else sl@0: return 1; // filename needed as argument. sl@0: sl@0: sl@0: if(!arg_fp) sl@0: { sl@0: return 1; sl@0: } sl@0: sl@0: while(!feof(arg_fp)&&!fail) sl@0: { sl@0: memset(argstr,0,200); sl@0: fgets(argstr, 200, arg_fp); sl@0: strip(argstr); sl@0: argv = create_params(argstr,&argc); sl@0: strcpy(openssltestnum,argv[1]); sl@0: sl@0: if(!strcmp(argv[2],"test_log")) sl@0: { sl@0: print_logs(argc,argv); sl@0: } sl@0: else if(!strcmp(argv[2],"compare")) sl@0: { sl@0: ret = compare_files(argv[3],argv[4]); sl@0: if(ret) sl@0: { sl@0: testResultXml(openssltestnum, ret); sl@0: fail=1; sl@0: } sl@0: sl@0: } sl@0: else if(!strcmp(argv[2],"append")) sl@0: { sl@0: ret = append(argv[3],argv[4],argv[5]); sl@0: if(ret) sl@0: { sl@0: testResultXml(openssltestnum, ret); sl@0: fail=1; sl@0: } sl@0: sl@0: } sl@0: else if(!strcmp(argv[2],"delete")) sl@0: { sl@0: delete_file(argv[3]); sl@0: sl@0: } sl@0: else sl@0: { sl@0: ret = openssl_main(argc-2,argv+2); sl@0: if(ret) sl@0: { sl@0: testResultXml(openssltestnum, ret); sl@0: fail=1; sl@0: } sl@0: sl@0: } sl@0: sl@0: //delete_params(argv,argc); sl@0: }//while sl@0: sl@0: if(!fail&&!ret) sl@0: testResultXml(openssltestnum, fail); sl@0: fclose(arg_fp); sl@0: sl@0: sl@0: openssl_deinit(); sl@0: return ret; sl@0: } sl@0: sl@0: #endif /* IF_DEF SYMBIAN */ sl@0: #ifndef SYMBIAN sl@0: int main(int Argc, char *Argv[]) sl@0: #else sl@0: int openssl_main(int Argc, char *Argv[]) sl@0: #endif sl@0: { sl@0: ARGS arg; sl@0: #define PROG_NAME_SIZE 39 sl@0: char pname[PROG_NAME_SIZE+1]; sl@0: FUNCTION f,*fp; sl@0: MS_STATIC const char *prompt; sl@0: MS_STATIC char buf[1024]; sl@0: char *to_free=NULL; sl@0: int n,i,ret=0; sl@0: int argc; sl@0: char **argv,*p; sl@0: LHASH *prog=NULL; sl@0: long errline; sl@0: sl@0: arg.data=NULL; sl@0: arg.count=0; sl@0: sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: #ifdef SYMBIAN sl@0: BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: #else sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: #endif sl@0: if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */ sl@0: { sl@0: if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))) sl@0: { sl@0: CRYPTO_malloc_debug_init(); sl@0: CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); sl@0: } sl@0: else sl@0: { sl@0: /* OPENSSL_DEBUG_MEMORY=off */ sl@0: CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); sl@0: } sl@0: } sl@0: CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); sl@0: sl@0: #if 0 sl@0: if (getenv("OPENSSL_DEBUG_LOCKING") != NULL) sl@0: #endif sl@0: { sl@0: CRYPTO_set_locking_callback(lock_dbg_cb); sl@0: } sl@0: sl@0: apps_startup(); sl@0: sl@0: /* Lets load up our environment a little */ sl@0: p=getenv("OPENSSL_CONF"); sl@0: if (p == NULL) sl@0: p=getenv("SSLEAY_CONF"); sl@0: if (p == NULL) sl@0: p=to_free=make_config_name(); sl@0: sl@0: default_config_file=p; sl@0: sl@0: config=NCONF_new(NULL); sl@0: i=NCONF_load(config,p,&errline); sl@0: if (i == 0) sl@0: { sl@0: NCONF_free(config); sl@0: config = NULL; sl@0: ERR_clear_error(); sl@0: } sl@0: sl@0: prog=prog_init(); sl@0: sl@0: /* first check the program name */ sl@0: program_name(Argv[0],pname,sizeof pname); sl@0: sl@0: f.name=pname; sl@0: fp=(FUNCTION *)lh_retrieve(prog,&f); sl@0: if (fp != NULL) sl@0: { sl@0: Argv[0]=pname; sl@0: ret=fp->func(Argc,Argv); sl@0: goto end; sl@0: } sl@0: sl@0: /* ok, now check that there are not arguments, if there are, sl@0: * run with them, shifting the ssleay off the front */ sl@0: if (Argc != 1) sl@0: { sl@0: Argc--; sl@0: Argv++; sl@0: ret=do_cmd(prog,Argc,Argv); sl@0: if (ret < 0) ret=0; sl@0: goto end; sl@0: } sl@0: sl@0: /* ok, lets enter the old 'OpenSSL>' mode */ sl@0: sl@0: for (;;) sl@0: { sl@0: ret=0; sl@0: p=buf; sl@0: n=sizeof buf; sl@0: i=0; sl@0: for (;;) sl@0: { sl@0: p[0]='\0'; sl@0: if (i++) sl@0: prompt=">"; sl@0: else prompt="OpenSSL> "; sl@0: #ifndef SYMBIAN sl@0: fputs(prompt,stdout); sl@0: fflush(stdout); sl@0: fgets(p,n,stdin); sl@0: #else sl@0: fputs(prompt,stdout); sl@0: fflush(stdout); sl@0: fgets(p,n,stdin); sl@0: sl@0: #endif sl@0: if (p[0] == '\0') goto end; sl@0: i=strlen(p); sl@0: if (i <= 1) break; sl@0: if (p[i-2] != '\\') break; sl@0: i-=2; sl@0: p+=i; sl@0: n-=i; sl@0: } sl@0: if (!chopup_args(&arg,buf,&argc,&argv)) break; sl@0: sl@0: ret=do_cmd(prog,argc,argv); sl@0: if (ret < 0) sl@0: { sl@0: ret=0; sl@0: goto end; sl@0: } sl@0: if (ret != 0) sl@0: BIO_printf(bio_err,"error in %s\n",argv[0]); sl@0: (void)BIO_flush(bio_err); sl@0: } sl@0: BIO_printf(bio_err,"bad exit\n"); sl@0: ret=1; sl@0: end: sl@0: if (to_free) sl@0: OPENSSL_free(to_free); sl@0: if (config != NULL) sl@0: { sl@0: NCONF_free(config); sl@0: config=NULL; sl@0: } sl@0: if (prog != NULL) lh_free(prog); sl@0: if (arg.data != NULL) OPENSSL_free(arg.data); sl@0: sl@0: apps_shutdown(); sl@0: sl@0: CRYPTO_mem_leaks(bio_err); sl@0: if (bio_err != NULL) sl@0: { sl@0: BIO_free(bio_err); sl@0: bio_err=NULL; sl@0: } sl@0: sl@0: return ret; sl@0: // OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: #define LIST_STANDARD_COMMANDS "list-standard-commands" sl@0: #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands" sl@0: #define LIST_CIPHER_COMMANDS "list-cipher-commands" sl@0: sl@0: static int do_cmd(LHASH *prog, int argc, char *argv[]) sl@0: { sl@0: FUNCTION f,*fp; sl@0: int i,ret=1,tp,nl; sl@0: sl@0: if ((argc <= 0) || (argv[0] == NULL)) sl@0: { ret=0; goto end; } sl@0: f.name=argv[0]; sl@0: fp=(FUNCTION *)lh_retrieve(prog,&f); sl@0: if (fp != NULL) sl@0: { sl@0: ret=fp->func(argc,argv); sl@0: } sl@0: else if ((strncmp(argv[0],"no-",3)) == 0) sl@0: { sl@0: sl@0: BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: bio_stdout = BIO_push(tmpbio, bio_stdout); sl@0: } sl@0: #endif sl@0: f.name=argv[0]+3; sl@0: ret = (lh_retrieve(prog,&f) != NULL); sl@0: if (!ret) sl@0: BIO_printf(bio_stdout, "%s\n", argv[0]); sl@0: else sl@0: BIO_printf(bio_stdout, "%s\n", argv[0]+3); sl@0: BIO_free_all(bio_stdout); sl@0: goto end; sl@0: } sl@0: else if ((strcmp(argv[0],"quit") == 0) || sl@0: (strcmp(argv[0],"q") == 0) || sl@0: (strcmp(argv[0],"exit") == 0) || sl@0: (strcmp(argv[0],"bye") == 0)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) || sl@0: (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) || sl@0: (strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0)) sl@0: { sl@0: int list_type; sl@0: BIO *bio_stdout; sl@0: sl@0: if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) sl@0: list_type = FUNC_TYPE_GENERAL; sl@0: else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) sl@0: list_type = FUNC_TYPE_MD; sl@0: else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */ sl@0: list_type = FUNC_TYPE_CIPHER; sl@0: sl@0: bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: bio_stdout = BIO_push(tmpbio, bio_stdout); sl@0: } sl@0: #endif sl@0: sl@0: for (fp=functions; fp->name != NULL; fp++) sl@0: if (fp->type == list_type) sl@0: BIO_printf(bio_stdout, "%s\n", fp->name); sl@0: BIO_free_all(bio_stdout); sl@0: ret=0; sl@0: goto end; sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n", sl@0: argv[0]); sl@0: BIO_printf(bio_err, "\nStandard commands"); sl@0: i=0; sl@0: tp=0; sl@0: for (fp=functions; fp->name != NULL; fp++) sl@0: { sl@0: nl=0; sl@0: if (((i++) % 5) == 0) sl@0: { sl@0: BIO_printf(bio_err,"\n"); sl@0: nl=1; sl@0: } sl@0: if (fp->type != tp) sl@0: { sl@0: tp=fp->type; sl@0: if (!nl) BIO_printf(bio_err,"\n"); sl@0: if (tp == FUNC_TYPE_MD) sl@0: { sl@0: i=1; sl@0: BIO_printf(bio_err, sl@0: "\nMessage Digest commands (see the `dgst' command for more details)\n"); sl@0: } sl@0: else if (tp == FUNC_TYPE_CIPHER) sl@0: { sl@0: i=1; sl@0: BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n"); sl@0: } sl@0: } sl@0: BIO_printf(bio_err,"%-15s",fp->name); sl@0: } sl@0: BIO_printf(bio_err,"\n\n"); sl@0: ret=0; sl@0: } sl@0: end: sl@0: return(ret); sl@0: } sl@0: sl@0: static int SortFnByName(const void *_f1,const void *_f2) sl@0: { sl@0: const FUNCTION *f1=_f1; sl@0: const FUNCTION *f2=_f2; sl@0: sl@0: if(f1->type != f2->type) sl@0: return f1->type-f2->type; sl@0: return strcmp(f1->name,f2->name); sl@0: } sl@0: sl@0: static LHASH *prog_init(void) sl@0: { sl@0: LHASH *ret; sl@0: FUNCTION *f; sl@0: size_t i; sl@0: sl@0: /* Purely so it looks nice when the user hits ? */ sl@0: for(i=0,f=functions ; f->name != NULL ; ++f,++i) sl@0: ; sl@0: qsort(functions,i,sizeof *functions,SortFnByName); sl@0: sl@0: if ((ret=lh_new(hash, cmp)) == NULL) sl@0: return(NULL); sl@0: sl@0: for (f=functions; f->name != NULL; f++) sl@0: lh_insert(ret,f); sl@0: return(ret); sl@0: } sl@0: sl@0: /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */ sl@0: static int MS_CALLBACK cmp(const void *a_void, const void *b_void) sl@0: { sl@0: return(strncmp(((const FUNCTION *)a_void)->name, sl@0: ((const FUNCTION *)b_void)->name,8)); sl@0: } sl@0: sl@0: /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */ sl@0: static unsigned long MS_CALLBACK hash(const void *a_void) sl@0: { sl@0: return(lh_strhash(((const FUNCTION *)a_void)->name)); sl@0: }