155714Skris/* apps/openssl.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
58109998Smarkm/* ====================================================================
59162911Ssimon * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60109998Smarkm *
61109998Smarkm * Redistribution and use in source and binary forms, with or without
62109998Smarkm * modification, are permitted provided that the following conditions
63109998Smarkm * are met:
64109998Smarkm *
65109998Smarkm * 1. Redistributions of source code must retain the above copyright
66109998Smarkm *    notice, this list of conditions and the following disclaimer.
67109998Smarkm *
68109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
69109998Smarkm *    notice, this list of conditions and the following disclaimer in
70109998Smarkm *    the documentation and/or other materials provided with the
71109998Smarkm *    distribution.
72109998Smarkm *
73109998Smarkm * 3. All advertising materials mentioning features or use of this
74109998Smarkm *    software must display the following acknowledgment:
75109998Smarkm *    "This product includes software developed by the OpenSSL Project
76109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77109998Smarkm *
78109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79109998Smarkm *    endorse or promote products derived from this software without
80109998Smarkm *    prior written permission. For written permission, please contact
81109998Smarkm *    openssl-core@openssl.org.
82109998Smarkm *
83109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
84109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
85109998Smarkm *    permission of the OpenSSL Project.
86109998Smarkm *
87109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
88109998Smarkm *    acknowledgment:
89109998Smarkm *    "This product includes software developed by the OpenSSL Project
90109998Smarkm *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91109998Smarkm *
92109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
104109998Smarkm * ====================================================================
105109998Smarkm *
106109998Smarkm * This product includes cryptographic software written by Eric Young
107109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
108109998Smarkm * Hudson (tjh@cryptsoft.com).
109109998Smarkm *
110109998Smarkm */
11155714Skris
112109998Smarkm
11355714Skris#include <stdio.h>
11455714Skris#include <string.h>
11555714Skris#include <stdlib.h>
11659191Skris#define OPENSSL_C /* tells apps.h to use complete apps_startup() */
117109998Smarkm#include "apps.h"
11855714Skris#include <openssl/bio.h>
11955714Skris#include <openssl/crypto.h>
120279264Sdelphij#include <openssl/rand.h>
12155714Skris#include <openssl/lhash.h>
12255714Skris#include <openssl/conf.h>
12355714Skris#include <openssl/x509.h>
12455714Skris#include <openssl/pem.h>
12555714Skris#include <openssl/ssl.h>
126111147Snectar#ifndef OPENSSL_NO_ENGINE
127109998Smarkm#include <openssl/engine.h>
128111147Snectar#endif
12955714Skris#define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
13059191Skris#include "progs.h"
13155714Skris#include "s_apps.h"
13255714Skris#include <openssl/err.h>
133238405Sjkim#ifdef OPENSSL_FIPS
134238405Sjkim#include <openssl/fips.h>
135238405Sjkim#endif
13655714Skris
137109998Smarkm/* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
138109998Smarkm * base prototypes (we cast each variable inside the function to the required
139109998Smarkm * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
140109998Smarkm * functions. */
141109998Smarkm
142238405Sjkimstatic LHASH_OF(FUNCTION) *prog_init(void );
143238405Sjkimstatic int do_cmd(LHASH_OF(FUNCTION) *prog,int argc,char *argv[]);
144238405Sjkimstatic void list_pkey(BIO *out);
145238405Sjkimstatic void list_cipher(BIO *out);
146238405Sjkimstatic void list_md(BIO *out);
14755714Skrischar *default_config_file=NULL;
14855714Skris
14955714Skris/* Make sure there is only one when MONOLITH is defined */
15055714Skris#ifdef MONOLITH
151109998SmarkmCONF *config=NULL;
15255714SkrisBIO *bio_err=NULL;
15355714Skris#endif
15455714Skris
155109998Smarkm
156109998Smarkmstatic void lock_dbg_cb(int mode, int type, const char *file, int line)
157109998Smarkm	{
158109998Smarkm	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
159109998Smarkm	const char *errstr = NULL;
160109998Smarkm	int rw;
161109998Smarkm
162109998Smarkm	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
163109998Smarkm	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
164109998Smarkm		{
165109998Smarkm		errstr = "invalid mode";
166109998Smarkm		goto err;
167109998Smarkm		}
168109998Smarkm
169120631Snectar	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
170109998Smarkm		{
171109998Smarkm		errstr = "type out of bounds";
172109998Smarkm		goto err;
173109998Smarkm		}
174109998Smarkm
175109998Smarkm	if (mode & CRYPTO_LOCK)
176109998Smarkm		{
177109998Smarkm		if (modes[type])
178109998Smarkm			{
179109998Smarkm			errstr = "already locked";
180109998Smarkm			/* must not happen in a single-threaded program
181109998Smarkm			 * (would deadlock) */
182109998Smarkm			goto err;
183109998Smarkm			}
184109998Smarkm
185109998Smarkm		modes[type] = rw;
186109998Smarkm		}
187109998Smarkm	else if (mode & CRYPTO_UNLOCK)
188109998Smarkm		{
189109998Smarkm		if (!modes[type])
190109998Smarkm			{
191109998Smarkm			errstr = "not locked";
192109998Smarkm			goto err;
193109998Smarkm			}
194109998Smarkm
195109998Smarkm		if (modes[type] != rw)
196109998Smarkm			{
197109998Smarkm			errstr = (rw == CRYPTO_READ) ?
198109998Smarkm				"CRYPTO_r_unlock on write lock" :
199109998Smarkm				"CRYPTO_w_unlock on read lock";
200109998Smarkm			}
201109998Smarkm
202109998Smarkm		modes[type] = 0;
203109998Smarkm		}
204109998Smarkm	else
205109998Smarkm		{
206109998Smarkm		errstr = "invalid mode";
207109998Smarkm		goto err;
208109998Smarkm		}
209109998Smarkm
210109998Smarkm err:
211109998Smarkm	if (errstr)
212109998Smarkm		{
213109998Smarkm		/* we cannot use bio_err here */
214109998Smarkm		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
215109998Smarkm			errstr, mode, type, file, line);
216109998Smarkm		}
217109998Smarkm	}
218109998Smarkm
219238405Sjkim#if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
220238405Sjkim# define ARGV _Argv
221238405Sjkim#else
222238405Sjkim# define ARGV Argv
223238405Sjkim#endif
224109998Smarkm
225238405Sjkimint main(int Argc, char *ARGV[])
22655714Skris	{
22755714Skris	ARGS arg;
228100928Snectar#define PROG_NAME_SIZE	39
229100928Snectar	char pname[PROG_NAME_SIZE+1];
23055714Skris	FUNCTION f,*fp;
231160814Ssimon	MS_STATIC const char *prompt;
232160814Ssimon	MS_STATIC char buf[1024];
233109998Smarkm	char *to_free=NULL;
23455714Skris	int n,i,ret=0;
23555714Skris	int argc;
23655714Skris	char **argv,*p;
237238405Sjkim	LHASH_OF(FUNCTION) *prog=NULL;
23855714Skris	long errline;
23955714Skris
240238405Sjkim#if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
241238405Sjkim	/* 2011-03-22 SMS.
242238405Sjkim	 * If we have 32-bit pointers everywhere, then we're safe, and
243238405Sjkim	 * we bypass this mess, as on non-VMS systems.  (See ARGV,
244238405Sjkim	 * above.)
245238405Sjkim	 * Problem 1: Compaq/HP C before V7.3 always used 32-bit
246238405Sjkim	 * pointers for argv[].
247238405Sjkim	 * Fix 1: For a 32-bit argv[], when we're using 64-bit pointers
248238405Sjkim	 * everywhere else, we always allocate and use a 64-bit
249238405Sjkim	 * duplicate of argv[].
250238405Sjkim	 * Problem 2: Compaq/HP C V7.3 (Alpha, IA64) before ECO1 failed
251238405Sjkim	 * to NULL-terminate a 64-bit argv[].  (As this was written, the
252238405Sjkim	 * compiler ECO was available only on IA64.)
253238405Sjkim	 * Fix 2: Unless advised not to (VMS_TRUST_ARGV), we test a
254238405Sjkim	 * 64-bit argv[argc] for NULL, and, if necessary, use a
255238405Sjkim	 * (properly) NULL-terminated (64-bit) duplicate of argv[].
256238405Sjkim	 * The same code is used in either case to duplicate argv[].
257238405Sjkim	 * Some of these decisions could be handled in preprocessing,
258238405Sjkim	 * but the code tends to get even uglier, and the penalty for
259238405Sjkim	 * deciding at compile- or run-time is tiny.
260238405Sjkim	 */
261238405Sjkim	char **Argv = NULL;
262238405Sjkim	int free_Argv = 0;
263194206Ssimon
264238405Sjkim	if ((sizeof( _Argv) < 8)        /* 32-bit argv[]. */
265238405Sjkim# if !defined( VMS_TRUST_ARGV)
266238405Sjkim	 || (_Argv[ Argc] != NULL)      /* Untrusted argv[argc] not NULL. */
267238405Sjkim# endif
268238405Sjkim		)
269238405Sjkim		{
270238405Sjkim		int i;
271238405Sjkim		Argv = OPENSSL_malloc( (Argc+ 1)* sizeof( char *));
272238405Sjkim		if (Argv == NULL)
273238405Sjkim			{ ret = -1; goto end; }
274238405Sjkim		for(i = 0; i < Argc; i++)
275238405Sjkim			Argv[i] = _Argv[i];
276238405Sjkim		Argv[ Argc] = NULL;     /* Certain NULL termination. */
277238405Sjkim		free_Argv = 1;
278194206Ssimon		}
279238405Sjkim	else
280238405Sjkim		{
281238405Sjkim		/* Use the known-good 32-bit argv[] (which needs the
282238405Sjkim		 * type cast to satisfy the compiler), or the trusted or
283238405Sjkim		 * tested-good 64-bit argv[] as-is. */
284238405Sjkim		Argv = (char **)_Argv;
285194206Ssimon		}
286238405Sjkim#endif /* defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64) */
287194206Ssimon
288238405Sjkim	arg.data=NULL;
289238405Sjkim	arg.count=0;
290238405Sjkim
29155714Skris	if (bio_err == NULL)
29255714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
29355714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
29455714Skris
295109998Smarkm	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
296109998Smarkm		{
297109998Smarkm		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
298109998Smarkm			{
299109998Smarkm			CRYPTO_malloc_debug_init();
300109998Smarkm			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
301109998Smarkm			}
302109998Smarkm		else
303109998Smarkm			{
304109998Smarkm			/* OPENSSL_DEBUG_MEMORY=off */
305109998Smarkm			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
306109998Smarkm			}
307109998Smarkm		}
308109998Smarkm	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
30955714Skris
310109998Smarkm#if 0
311109998Smarkm	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
312109998Smarkm#endif
313109998Smarkm		{
314109998Smarkm		CRYPTO_set_locking_callback(lock_dbg_cb);
315109998Smarkm		}
316109998Smarkm
317238405Sjkim	if(getenv("OPENSSL_FIPS")) {
318238405Sjkim#ifdef OPENSSL_FIPS
319238405Sjkim		if (!FIPS_mode_set(1)) {
320238405Sjkim			ERR_load_crypto_strings();
321238405Sjkim			ERR_print_errors(BIO_new_fp(stderr,BIO_NOCLOSE));
322238405Sjkim			EXIT(1);
323238405Sjkim		}
324238405Sjkim#else
325238405Sjkim		fprintf(stderr, "FIPS mode not supported.\n");
326238405Sjkim		EXIT(1);
327238405Sjkim#endif
328238405Sjkim		}
329238405Sjkim
330109998Smarkm	apps_startup();
331109998Smarkm
33255714Skris	/* Lets load up our environment a little */
33355714Skris	p=getenv("OPENSSL_CONF");
33455714Skris	if (p == NULL)
33555714Skris		p=getenv("SSLEAY_CONF");
33655714Skris	if (p == NULL)
337109998Smarkm		p=to_free=make_config_name();
33855714Skris
33955714Skris	default_config_file=p;
34055714Skris
341109998Smarkm	config=NCONF_new(NULL);
342109998Smarkm	i=NCONF_load(config,p,&errline);
343109998Smarkm	if (i == 0)
344109998Smarkm		{
345238405Sjkim		if (ERR_GET_REASON(ERR_peek_last_error())
346238405Sjkim		    == CONF_R_NO_SUCH_FILE)
347238405Sjkim			{
348238405Sjkim			BIO_printf(bio_err,
349238405Sjkim				   "WARNING: can't open config file: %s\n",p);
350238405Sjkim			ERR_clear_error();
351238405Sjkim			NCONF_free(config);
352238405Sjkim			config = NULL;
353238405Sjkim			}
354238405Sjkim		else
355238405Sjkim			{
356238405Sjkim			ERR_print_errors(bio_err);
357238405Sjkim			NCONF_free(config);
358238405Sjkim			exit(1);
359238405Sjkim			}
360109998Smarkm		}
36155714Skris
36255714Skris	prog=prog_init();
36355714Skris
36455714Skris	/* first check the program name */
365109998Smarkm	program_name(Argv[0],pname,sizeof pname);
36655714Skris
36755714Skris	f.name=pname;
368238405Sjkim	fp=lh_FUNCTION_retrieve(prog,&f);
36955714Skris	if (fp != NULL)
37055714Skris		{
37155714Skris		Argv[0]=pname;
37255714Skris		ret=fp->func(Argc,Argv);
37355714Skris		goto end;
37455714Skris		}
37555714Skris
37655714Skris	/* ok, now check that there are not arguments, if there are,
37755714Skris	 * run with them, shifting the ssleay off the front */
37855714Skris	if (Argc != 1)
37955714Skris		{
38055714Skris		Argc--;
38155714Skris		Argv++;
38255714Skris		ret=do_cmd(prog,Argc,Argv);
38355714Skris		if (ret < 0) ret=0;
38455714Skris		goto end;
38555714Skris		}
38655714Skris
38755714Skris	/* ok, lets enter the old 'OpenSSL>' mode */
38855714Skris
38955714Skris	for (;;)
39055714Skris		{
39155714Skris		ret=0;
39255714Skris		p=buf;
393109998Smarkm		n=sizeof buf;
39455714Skris		i=0;
39555714Skris		for (;;)
39655714Skris			{
39755714Skris			p[0]='\0';
39855714Skris			if (i++)
39955714Skris				prompt=">";
40055714Skris			else	prompt="OpenSSL> ";
40155714Skris			fputs(prompt,stdout);
40255714Skris			fflush(stdout);
403205128Ssimon			if (!fgets(p,n,stdin))
404205128Ssimon				goto end;
40555714Skris			if (p[0] == '\0') goto end;
40655714Skris			i=strlen(p);
40755714Skris			if (i <= 1) break;
40855714Skris			if (p[i-2] != '\\') break;
40955714Skris			i-=2;
41055714Skris			p+=i;
41155714Skris			n-=i;
41255714Skris			}
41355714Skris		if (!chopup_args(&arg,buf,&argc,&argv)) break;
41455714Skris
41555714Skris		ret=do_cmd(prog,argc,argv);
41655714Skris		if (ret < 0)
41755714Skris			{
41855714Skris			ret=0;
41955714Skris			goto end;
42055714Skris			}
42155714Skris		if (ret != 0)
42255714Skris			BIO_printf(bio_err,"error in %s\n",argv[0]);
42355714Skris		(void)BIO_flush(bio_err);
42455714Skris		}
42555714Skris	BIO_printf(bio_err,"bad exit\n");
42655714Skris	ret=1;
42755714Skrisend:
428109998Smarkm	if (to_free)
429109998Smarkm		OPENSSL_free(to_free);
43055714Skris	if (config != NULL)
43155714Skris		{
432109998Smarkm		NCONF_free(config);
43355714Skris		config=NULL;
43455714Skris		}
435238405Sjkim	if (prog != NULL) lh_FUNCTION_free(prog);
43668651Skris	if (arg.data != NULL) OPENSSL_free(arg.data);
43755714Skris
438109998Smarkm
43955714Skris	if (bio_err != NULL)
44055714Skris		{
44155714Skris		BIO_free(bio_err);
44255714Skris		bio_err=NULL;
44355714Skris		}
444238405Sjkim#if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
445238405Sjkim	/* Free any duplicate Argv[] storage. */
446238405Sjkim	if (free_Argv)
447238405Sjkim		{
448238405Sjkim		OPENSSL_free(Argv);
449238405Sjkim		}
450238405Sjkim#endif
451279264Sdelphij	apps_shutdown();
452279264Sdelphij	CRYPTO_mem_leaks(bio_err);
453279264Sdelphij
454109998Smarkm	OPENSSL_EXIT(ret);
45555714Skris	}
45655714Skris
45755714Skris#define LIST_STANDARD_COMMANDS "list-standard-commands"
45855714Skris#define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
459238405Sjkim#define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
46055714Skris#define LIST_CIPHER_COMMANDS "list-cipher-commands"
461238405Sjkim#define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
462238405Sjkim#define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
46355714Skris
464238405Sjkim
465238405Sjkimstatic int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
46655714Skris	{
46755714Skris	FUNCTION f,*fp;
46855714Skris	int i,ret=1,tp,nl;
46955714Skris
47055714Skris	if ((argc <= 0) || (argv[0] == NULL))
47155714Skris		{ ret=0; goto end; }
47255714Skris	f.name=argv[0];
473238405Sjkim	fp=lh_FUNCTION_retrieve(prog,&f);
474238405Sjkim	if (fp == NULL)
475238405Sjkim		{
476238405Sjkim		if (EVP_get_digestbyname(argv[0]))
477238405Sjkim			{
478238405Sjkim			f.type = FUNC_TYPE_MD;
479238405Sjkim			f.func = dgst_main;
480238405Sjkim			fp = &f;
481238405Sjkim			}
482238405Sjkim		else if (EVP_get_cipherbyname(argv[0]))
483238405Sjkim			{
484238405Sjkim			f.type = FUNC_TYPE_CIPHER;
485238405Sjkim			f.func = enc_main;
486238405Sjkim			fp = &f;
487238405Sjkim			}
488238405Sjkim		}
48955714Skris	if (fp != NULL)
49055714Skris		{
49155714Skris		ret=fp->func(argc,argv);
49255714Skris		}
49359191Skris	else if ((strncmp(argv[0],"no-",3)) == 0)
49459191Skris		{
49559191Skris		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
496109998Smarkm#ifdef OPENSSL_SYS_VMS
49768651Skris		{
49868651Skris		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
49968651Skris		bio_stdout = BIO_push(tmpbio, bio_stdout);
50068651Skris		}
50168651Skris#endif
50259191Skris		f.name=argv[0]+3;
503238405Sjkim		ret = (lh_FUNCTION_retrieve(prog,&f) != NULL);
50459191Skris		if (!ret)
50559191Skris			BIO_printf(bio_stdout, "%s\n", argv[0]);
50659191Skris		else
50759191Skris			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
50868651Skris		BIO_free_all(bio_stdout);
50959191Skris		goto end;
51059191Skris		}
51155714Skris	else if ((strcmp(argv[0],"quit") == 0) ||
51255714Skris		(strcmp(argv[0],"q") == 0) ||
51355714Skris		(strcmp(argv[0],"exit") == 0) ||
51455714Skris		(strcmp(argv[0],"bye") == 0))
51555714Skris		{
51655714Skris		ret= -1;
51755714Skris		goto end;
51855714Skris		}
51955714Skris	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
52055714Skris		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
521238405Sjkim		(strcmp(argv[0],LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
522238405Sjkim		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0) ||
523238405Sjkim		(strcmp(argv[0],LIST_CIPHER_ALGORITHMS) == 0) ||
524238405Sjkim		(strcmp(argv[0],LIST_PUBLIC_KEY_ALGORITHMS) == 0))
52555714Skris		{
52655714Skris		int list_type;
52755714Skris		BIO *bio_stdout;
52855714Skris
52955714Skris		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
53055714Skris			list_type = FUNC_TYPE_GENERAL;
53155714Skris		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
53255714Skris			list_type = FUNC_TYPE_MD;
533238405Sjkim		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
534238405Sjkim			list_type = FUNC_TYPE_MD_ALG;
535238405Sjkim		else if (strcmp(argv[0],LIST_PUBLIC_KEY_ALGORITHMS) == 0)
536238405Sjkim			list_type = FUNC_TYPE_PKEY;
537238405Sjkim		else if (strcmp(argv[0],LIST_CIPHER_ALGORITHMS) == 0)
538238405Sjkim			list_type = FUNC_TYPE_CIPHER_ALG;
53955714Skris		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
54055714Skris			list_type = FUNC_TYPE_CIPHER;
54155714Skris		bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
542109998Smarkm#ifdef OPENSSL_SYS_VMS
54368651Skris		{
54468651Skris		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
54568651Skris		bio_stdout = BIO_push(tmpbio, bio_stdout);
54668651Skris		}
54768651Skris#endif
548238405Sjkim
549238405Sjkim		if (!load_config(bio_err, NULL))
550238405Sjkim			goto end;
551238405Sjkim
552238405Sjkim		if (list_type == FUNC_TYPE_PKEY)
553238405Sjkim			list_pkey(bio_stdout);
554238405Sjkim		if (list_type == FUNC_TYPE_MD_ALG)
555238405Sjkim			list_md(bio_stdout);
556238405Sjkim		if (list_type == FUNC_TYPE_CIPHER_ALG)
557238405Sjkim			list_cipher(bio_stdout);
558238405Sjkim		else
559238405Sjkim			{
560238405Sjkim			for (fp=functions; fp->name != NULL; fp++)
561238405Sjkim				if (fp->type == list_type)
562238405Sjkim					BIO_printf(bio_stdout, "%s\n",
563238405Sjkim								fp->name);
564238405Sjkim			}
56568651Skris		BIO_free_all(bio_stdout);
56655714Skris		ret=0;
56755714Skris		goto end;
56855714Skris		}
56955714Skris	else
57055714Skris		{
57155714Skris		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
57255714Skris			argv[0]);
57355714Skris		BIO_printf(bio_err, "\nStandard commands");
57455714Skris		i=0;
57555714Skris		tp=0;
57655714Skris		for (fp=functions; fp->name != NULL; fp++)
57755714Skris			{
57855714Skris			nl=0;
579162911Ssimon#ifdef OPENSSL_NO_CAMELLIA
58055714Skris			if (((i++) % 5) == 0)
581162911Ssimon#else
582162911Ssimon			if (((i++) % 4) == 0)
583162911Ssimon#endif
58455714Skris				{
58555714Skris				BIO_printf(bio_err,"\n");
58655714Skris				nl=1;
58755714Skris				}
58855714Skris			if (fp->type != tp)
58955714Skris				{
59055714Skris				tp=fp->type;
59155714Skris				if (!nl) BIO_printf(bio_err,"\n");
59255714Skris				if (tp == FUNC_TYPE_MD)
59355714Skris					{
59455714Skris					i=1;
59555714Skris					BIO_printf(bio_err,
59655714Skris						"\nMessage Digest commands (see the `dgst' command for more details)\n");
59755714Skris					}
59855714Skris				else if (tp == FUNC_TYPE_CIPHER)
59955714Skris					{
60055714Skris					i=1;
60155714Skris					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
60255714Skris					}
60355714Skris				}
604162911Ssimon#ifdef OPENSSL_NO_CAMELLIA
60555714Skris			BIO_printf(bio_err,"%-15s",fp->name);
606162911Ssimon#else
607162911Ssimon			BIO_printf(bio_err,"%-18s",fp->name);
608162911Ssimon#endif
60955714Skris			}
61055714Skris		BIO_printf(bio_err,"\n\n");
61155714Skris		ret=0;
61255714Skris		}
61355714Skrisend:
61455714Skris	return(ret);
61555714Skris	}
61655714Skris
61755714Skrisstatic int SortFnByName(const void *_f1,const void *_f2)
61855714Skris    {
61955714Skris    const FUNCTION *f1=_f1;
62055714Skris    const FUNCTION *f2=_f2;
62155714Skris
62255714Skris    if(f1->type != f2->type)
62355714Skris	return f1->type-f2->type;
62455714Skris    return strcmp(f1->name,f2->name);
62555714Skris    }
62655714Skris
627238405Sjkimstatic void list_pkey(BIO *out)
62855714Skris	{
629238405Sjkim	int i;
630238405Sjkim	for (i = 0; i < EVP_PKEY_asn1_get_count(); i++)
631238405Sjkim		{
632238405Sjkim		const EVP_PKEY_ASN1_METHOD *ameth;
633238405Sjkim		int pkey_id, pkey_base_id, pkey_flags;
634238405Sjkim		const char *pinfo, *pem_str;
635238405Sjkim		ameth = EVP_PKEY_asn1_get0(i);
636238405Sjkim		EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
637238405Sjkim						&pinfo, &pem_str, ameth);
638238405Sjkim		if (pkey_flags & ASN1_PKEY_ALIAS)
639238405Sjkim			{
640238405Sjkim			BIO_printf(out, "Name: %s\n",
641238405Sjkim					OBJ_nid2ln(pkey_id));
642238405Sjkim			BIO_printf(out, "\tType: Alias to %s\n",
643238405Sjkim					OBJ_nid2ln(pkey_base_id));
644238405Sjkim			}
645238405Sjkim		else
646238405Sjkim			{
647238405Sjkim			BIO_printf(out, "Name: %s\n", pinfo);
648238405Sjkim			BIO_printf(out, "\tType: %s Algorithm\n",
649238405Sjkim				pkey_flags & ASN1_PKEY_DYNAMIC ?
650238405Sjkim					"External" : "Builtin");
651238405Sjkim			BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
652238405Sjkim			if (pem_str == NULL)
653238405Sjkim				pem_str = "(none)";
654238405Sjkim			BIO_printf(out, "\tPEM string: %s\n", pem_str);
655238405Sjkim			}
656238405Sjkim
657238405Sjkim		}
658238405Sjkim	}
659238405Sjkim
660238405Sjkimstatic void list_cipher_fn(const EVP_CIPHER *c,
661238405Sjkim			const char *from, const char *to, void *arg)
662238405Sjkim	{
663238405Sjkim	if (c)
664238405Sjkim		BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
665238405Sjkim	else
666238405Sjkim		{
667238405Sjkim		if (!from)
668238405Sjkim			from = "<undefined>";
669238405Sjkim		if (!to)
670238405Sjkim			to = "<undefined>";
671238405Sjkim		BIO_printf(arg, "%s => %s\n", from, to);
672238405Sjkim		}
673238405Sjkim	}
674238405Sjkim
675238405Sjkimstatic void list_cipher(BIO *out)
676238405Sjkim	{
677238405Sjkim	EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
678238405Sjkim	}
679238405Sjkim
680238405Sjkimstatic void list_md_fn(const EVP_MD *m,
681238405Sjkim			const char *from, const char *to, void *arg)
682238405Sjkim	{
683238405Sjkim	if (m)
684238405Sjkim		BIO_printf(arg, "%s\n", EVP_MD_name(m));
685238405Sjkim	else
686238405Sjkim		{
687238405Sjkim		if (!from)
688238405Sjkim			from = "<undefined>";
689238405Sjkim		if (!to)
690238405Sjkim			to = "<undefined>";
691238405Sjkim		BIO_printf(arg, "%s => %s\n", from, to);
692238405Sjkim		}
693238405Sjkim	}
694238405Sjkim
695238405Sjkimstatic void list_md(BIO *out)
696238405Sjkim	{
697238405Sjkim	EVP_MD_do_all_sorted(list_md_fn, out);
698238405Sjkim	}
699238405Sjkim
700238405Sjkimstatic int MS_CALLBACK function_cmp(const FUNCTION *a, const FUNCTION *b)
701238405Sjkim	{
702238405Sjkim	return strncmp(a->name,b->name,8);
703238405Sjkim	}
704238405Sjkimstatic IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
705238405Sjkim
706238405Sjkimstatic unsigned long MS_CALLBACK function_hash(const FUNCTION *a)
707238405Sjkim	{
708238405Sjkim	return lh_strhash(a->name);
709238405Sjkim	}
710238405Sjkimstatic IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
711238405Sjkim
712238405Sjkimstatic LHASH_OF(FUNCTION) *prog_init(void)
713238405Sjkim	{
714238405Sjkim	LHASH_OF(FUNCTION) *ret;
71555714Skris	FUNCTION *f;
716160814Ssimon	size_t i;
71755714Skris
71855714Skris	/* Purely so it looks nice when the user hits ? */
71955714Skris	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
72055714Skris	    ;
72155714Skris	qsort(functions,i,sizeof *functions,SortFnByName);
72255714Skris
723238405Sjkim	if ((ret=lh_FUNCTION_new()) == NULL)
724109998Smarkm		return(NULL);
72555714Skris
72655714Skris	for (f=functions; f->name != NULL; f++)
727238405Sjkim		(void)lh_FUNCTION_insert(ret,f);
72855714Skris	return(ret);
72955714Skris	}
73055714Skris
731