6b48f22e9463c239c0e16e92868b07c255b8616b
[openssl.git] / apps / openssl.c
1 /* apps/openssl.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 #ifndef DEBUG
60 #undef DEBUG
61 #endif
62
63 #include <stdio.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <openssl/bio.h>
67 #include <openssl/crypto.h>
68 #include <openssl/lhash.h>
69 #include <openssl/conf.h>
70 #include <openssl/x509.h>
71 #include <openssl/pem.h>
72 #include <openssl/ssl.h>
73 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
74 #include "apps.h"
75 #include "progs.h"
76 #include "s_apps.h"
77 #include <openssl/err.h>
78
79 /*
80 #ifdef WINDOWS
81 #include "bss_file.c"
82 #endif
83 */
84
85 static unsigned long MS_CALLBACK hash(FUNCTION *a);
86 static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b);
87 static LHASH *prog_init(void );
88 static int do_cmd(LHASH *prog,int argc,char *argv[]);
89 LHASH *config=NULL;
90 char *default_config_file=NULL;
91
92 #ifdef DEBUG
93 static void sig_stop(int i)
94         {
95         char *a=NULL;
96
97         *a='\0';
98         }
99 #endif
100
101 /* Make sure there is only one when MONOLITH is defined */
102 #ifdef MONOLITH
103 BIO *bio_err=NULL;
104 #endif
105
106 int main(int Argc, char *Argv[])
107         {
108         ARGS arg;
109 #define PROG_NAME_SIZE  16
110         char pname[PROG_NAME_SIZE];
111         FUNCTION f,*fp;
112         MS_STATIC char *prompt,buf[1024],config_name[256];
113         int n,i,ret=0;
114         int argc;
115         char **argv,*p;
116         LHASH *prog=NULL;
117         long errline;
118  
119         arg.data=NULL;
120         arg.count=0;
121
122 #if defined(DEBUG) && !defined(WINDOWS) && !defined(MSDOS)
123 #ifdef SIGBUS
124         signal(SIGBUS,sig_stop);
125 #endif
126 #ifdef SIGSEGV
127         signal(SIGSEGV,sig_stop);
128 #endif
129 #endif
130
131         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
132
133         apps_startup();
134
135         if (bio_err == NULL)
136                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
137                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
138
139         ERR_load_crypto_strings();
140
141         /* Lets load up our environment a little */
142         p=getenv("OPENSSL_CONF");
143         if (p == NULL)
144                 p=getenv("SSLEAY_CONF");
145         if (p == NULL)
146                 {
147                 strcpy(config_name,X509_get_default_cert_area());
148 #ifndef VMS
149                 strcat(config_name,"/");
150 #endif
151                 strcat(config_name,OPENSSL_CONF);
152                 p=config_name;
153                 }
154
155         default_config_file=p;
156
157         config=CONF_load(config,p,&errline);
158         if (config == NULL) ERR_clear_error();
159
160         prog=prog_init();
161
162         /* first check the program name */
163         program_name(Argv[0],pname,PROG_NAME_SIZE);
164
165         f.name=pname;
166         fp=(FUNCTION *)lh_retrieve(prog,&f);
167         if (fp != NULL)
168                 {
169                 Argv[0]=pname;
170                 ret=fp->func(Argc,Argv);
171                 goto end;
172                 }
173
174         /* ok, now check that there are not arguments, if there are,
175          * run with them, shifting the ssleay off the front */
176         if (Argc != 1)
177                 {
178                 Argc--;
179                 Argv++;
180                 ret=do_cmd(prog,Argc,Argv);
181                 if (ret < 0) ret=0;
182                 goto end;
183                 }
184
185         /* ok, lets enter the old 'OpenSSL>' mode */
186         
187         for (;;)
188                 {
189                 ret=0;
190                 p=buf;
191                 n=1024;
192                 i=0;
193                 for (;;)
194                         {
195                         p[0]='\0';
196                         if (i++)
197                                 prompt=">";
198                         else    prompt="OpenSSL> ";
199                         fputs(prompt,stdout);
200                         fflush(stdout);
201                         fgets(p,n,stdin);
202                         if (p[0] == '\0') goto end;
203                         i=strlen(p);
204                         if (i <= 1) break;
205                         if (p[i-2] != '\\') break;
206                         i-=2;
207                         p+=i;
208                         n-=i;
209                         }
210                 if (!chopup_args(&arg,buf,&argc,&argv)) break;
211
212                 ret=do_cmd(prog,argc,argv);
213                 if (ret < 0)
214                         {
215                         ret=0;
216                         goto end;
217                         }
218                 if (ret != 0)
219                         BIO_printf(bio_err,"error in %s\n",argv[0]);
220                 (void)BIO_flush(bio_err);
221                 }
222         BIO_printf(bio_err,"bad exit\n");
223         ret=1;
224 end:
225         if (config != NULL)
226                 {
227                 CONF_free(config);
228                 config=NULL;
229                 }
230         if (prog != NULL) lh_free(prog);
231         if (arg.data != NULL) Free(arg.data);
232         ERR_remove_state(0);
233
234         EVP_cleanup();
235         ERR_free_strings();
236
237 #ifdef LEVITTE_DEBUG
238         CRYPTO_push_info("Just to make sure I get a memory leak I can see :-)");
239         (void)Malloc(1024);
240         CRYPTO_pop_info();
241 #endif
242
243         CRYPTO_mem_leaks(bio_err);
244         if (bio_err != NULL)
245                 {
246                 BIO_free(bio_err);
247                 bio_err=NULL;
248                 }
249         EXIT(ret);
250         }
251
252 #define LIST_STANDARD_COMMANDS "list-standard-commands"
253 #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
254 #define LIST_CIPHER_COMMANDS "list-cipher-commands"
255
256 static int do_cmd(LHASH *prog, int argc, char *argv[])
257         {
258         FUNCTION f,*fp;
259         int i,ret=1,tp,nl;
260
261         if ((argc <= 0) || (argv[0] == NULL))
262                 { ret=0; goto end; }
263         f.name=argv[0];
264         fp=(FUNCTION *)lh_retrieve(prog,&f);
265         if (fp != NULL)
266                 {
267                 ret=fp->func(argc,argv);
268                 }
269         else if ((strcmp(argv[0],"quit") == 0) ||
270                 (strcmp(argv[0],"q") == 0) ||
271                 (strcmp(argv[0],"exit") == 0) ||
272                 (strcmp(argv[0],"bye") == 0))
273                 {
274                 ret= -1;
275                 goto end;
276                 }
277         else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
278                 (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
279                 (strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
280                 {
281                 int list_type;
282                 BIO *bio_stdout;
283
284                 if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
285                         list_type = FUNC_TYPE_GENERAL;
286                 else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
287                         list_type = FUNC_TYPE_MD;
288                 else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
289                         list_type = FUNC_TYPE_CIPHER;
290                 bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
291                 
292                 for (fp=functions; fp->name != NULL; fp++)
293                         if (fp->type == list_type)
294                                 BIO_printf(bio_stdout, "%s\n", fp->name);
295                 BIO_free(bio_stdout);
296                 ret=0;
297                 goto end;
298                 }
299         else
300                 {
301                 BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
302                         argv[0]);
303                 BIO_printf(bio_err, "\nStandard commands");
304                 i=0;
305                 tp=0;
306                 for (fp=functions; fp->name != NULL; fp++)
307                         {
308                         nl=0;
309                         if (((i++) % 5) == 0)
310                                 {
311                                 BIO_printf(bio_err,"\n");
312                                 nl=1;
313                                 }
314                         if (fp->type != tp)
315                                 {
316                                 tp=fp->type;
317                                 if (!nl) BIO_printf(bio_err,"\n");
318                                 if (tp == FUNC_TYPE_MD)
319                                         {
320                                         i=1;
321                                         BIO_printf(bio_err,
322                                                 "\nMessage Digest commands (see the `dgst' command for more details)\n");
323                                         }
324                                 else if (tp == FUNC_TYPE_CIPHER)
325                                         {
326                                         i=1;
327                                         BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
328                                         }
329                                 }
330                         BIO_printf(bio_err,"%-15s",fp->name);
331                         }
332                 BIO_printf(bio_err,"\n\n");
333                 ret=0;
334                 }
335 end:
336         return(ret);
337         }
338
339 static int SortFnByName(const void *_f1,const void *_f2)
340     {
341     const FUNCTION *f1=_f1;
342     const FUNCTION *f2=_f2;
343
344     if(f1->type != f2->type)
345         return f1->type-f2->type;
346     return strcmp(f1->name,f2->name);
347     }
348
349 static LHASH *prog_init(void)
350         {
351         LHASH *ret;
352         FUNCTION *f;
353         int i;
354
355         /* Purely so it looks nice when the user hits ? */
356         for(i=0,f=functions ; f->name != NULL ; ++f,++i)
357             ;
358         qsort(functions,i,sizeof *functions,SortFnByName);
359
360         if ((ret=lh_new(hash,cmp)) == NULL) return(NULL);
361
362         for (f=functions; f->name != NULL; f++)
363                 lh_insert(ret,f);
364         return(ret);
365         }
366
367 static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b)
368         {
369         return(strncmp(a->name,b->name,8));
370         }
371
372 static unsigned long MS_CALLBACK hash(FUNCTION *a)
373         {
374         return(lh_strhash(a->name));
375         }