xref: /minix3/crypto/external/bsd/openssl/dist/apps/openssl.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/openssl.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc  * are met:
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc  *    distribution.
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
75ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
82ebfedea0SLionel Sambuc  *
83ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc  *
87ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc  *    acknowledgment:
89ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc  *
92ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc  * ====================================================================
105ebfedea0SLionel Sambuc  *
106ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
108ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc #include <stdio.h>
113ebfedea0SLionel Sambuc #include <string.h>
114ebfedea0SLionel Sambuc #include <stdlib.h>
115*0a6a1f1dSLionel Sambuc #define OPENSSL_C               /* tells apps.h to use complete
116*0a6a1f1dSLionel Sambuc                                  * apps_startup() */
117ebfedea0SLionel Sambuc #include "apps.h"
118ebfedea0SLionel Sambuc #include <openssl/bio.h>
119ebfedea0SLionel Sambuc #include <openssl/crypto.h>
120*0a6a1f1dSLionel Sambuc #include <openssl/rand.h>
121ebfedea0SLionel Sambuc #include <openssl/lhash.h>
122ebfedea0SLionel Sambuc #include <openssl/conf.h>
123ebfedea0SLionel Sambuc #include <openssl/x509.h>
124ebfedea0SLionel Sambuc #include <openssl/pem.h>
125ebfedea0SLionel Sambuc #include <openssl/ssl.h>
126ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
127ebfedea0SLionel Sambuc # include <openssl/engine.h>
128ebfedea0SLionel Sambuc #endif
129*0a6a1f1dSLionel Sambuc #define USE_SOCKETS             /* needed for the _O_BINARY defs in the MS
130*0a6a1f1dSLionel Sambuc                                  * world */
131ebfedea0SLionel Sambuc #include "progs.h"
132ebfedea0SLionel Sambuc #include "s_apps.h"
133ebfedea0SLionel Sambuc #include <openssl/err.h>
134ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
135ebfedea0SLionel Sambuc # include <openssl/fips.h>
136ebfedea0SLionel Sambuc #endif
137ebfedea0SLionel Sambuc 
138*0a6a1f1dSLionel Sambuc /*
139*0a6a1f1dSLionel Sambuc  * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
140*0a6a1f1dSLionel Sambuc  * the base prototypes (we cast each variable inside the function to the
141*0a6a1f1dSLionel Sambuc  * required type of "FUNCTION*"). This removes the necessity for
142*0a6a1f1dSLionel Sambuc  * macro-generated wrapper functions.
143*0a6a1f1dSLionel Sambuc  */
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc static LHASH_OF(FUNCTION) *prog_init(void);
146ebfedea0SLionel Sambuc static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
147ebfedea0SLionel Sambuc static void list_pkey(BIO *out);
148ebfedea0SLionel Sambuc static void list_cipher(BIO *out);
149ebfedea0SLionel Sambuc static void list_md(BIO *out);
150ebfedea0SLionel Sambuc char *default_config_file = NULL;
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc /* Make sure there is only one when MONOLITH is defined */
153ebfedea0SLionel Sambuc #ifdef MONOLITH
154ebfedea0SLionel Sambuc CONF *config = NULL;
155ebfedea0SLionel Sambuc BIO *bio_err = NULL;
156ebfedea0SLionel Sambuc #endif
157ebfedea0SLionel Sambuc 
lock_dbg_cb(int mode,int type,const char * file,int line)158ebfedea0SLionel Sambuc static void lock_dbg_cb(int mode, int type, const char *file, int line)
159ebfedea0SLionel Sambuc {
160ebfedea0SLionel Sambuc     static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
161ebfedea0SLionel Sambuc     const char *errstr = NULL;
162ebfedea0SLionel Sambuc     int rw;
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc     rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
165*0a6a1f1dSLionel Sambuc     if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
166ebfedea0SLionel Sambuc         errstr = "invalid mode";
167ebfedea0SLionel Sambuc         goto err;
168ebfedea0SLionel Sambuc     }
169ebfedea0SLionel Sambuc 
170*0a6a1f1dSLionel Sambuc     if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
171ebfedea0SLionel Sambuc         errstr = "type out of bounds";
172ebfedea0SLionel Sambuc         goto err;
173ebfedea0SLionel Sambuc     }
174ebfedea0SLionel Sambuc 
175*0a6a1f1dSLionel Sambuc     if (mode & CRYPTO_LOCK) {
176*0a6a1f1dSLionel Sambuc         if (modes[type]) {
177ebfedea0SLionel Sambuc             errstr = "already locked";
178*0a6a1f1dSLionel Sambuc             /*
179*0a6a1f1dSLionel Sambuc              * must not happen in a single-threaded program (would deadlock)
180*0a6a1f1dSLionel Sambuc              */
181ebfedea0SLionel Sambuc             goto err;
182ebfedea0SLionel Sambuc         }
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc         modes[type] = rw;
185*0a6a1f1dSLionel Sambuc     } else if (mode & CRYPTO_UNLOCK) {
186*0a6a1f1dSLionel Sambuc         if (!modes[type]) {
187ebfedea0SLionel Sambuc             errstr = "not locked";
188ebfedea0SLionel Sambuc             goto err;
189ebfedea0SLionel Sambuc         }
190ebfedea0SLionel Sambuc 
191*0a6a1f1dSLionel Sambuc         if (modes[type] != rw) {
192ebfedea0SLionel Sambuc             errstr = (rw == CRYPTO_READ) ?
193ebfedea0SLionel Sambuc                 "CRYPTO_r_unlock on write lock" :
194ebfedea0SLionel Sambuc                 "CRYPTO_w_unlock on read lock";
195ebfedea0SLionel Sambuc         }
196ebfedea0SLionel Sambuc 
197ebfedea0SLionel Sambuc         modes[type] = 0;
198*0a6a1f1dSLionel Sambuc     } else {
199ebfedea0SLionel Sambuc         errstr = "invalid mode";
200ebfedea0SLionel Sambuc         goto err;
201ebfedea0SLionel Sambuc     }
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc  err:
204*0a6a1f1dSLionel Sambuc     if (errstr) {
205ebfedea0SLionel Sambuc         /* we cannot use bio_err here */
206*0a6a1f1dSLionel Sambuc         fprintf(stderr,
207*0a6a1f1dSLionel Sambuc                 "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
208ebfedea0SLionel Sambuc                 errstr, mode, type, file, line);
209ebfedea0SLionel Sambuc     }
210ebfedea0SLionel Sambuc }
211ebfedea0SLionel Sambuc 
212ebfedea0SLionel Sambuc #if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
213ebfedea0SLionel Sambuc # define ARGV _Argv
214ebfedea0SLionel Sambuc #else
215ebfedea0SLionel Sambuc # define ARGV Argv
216ebfedea0SLionel Sambuc #endif
217ebfedea0SLionel Sambuc 
main(int Argc,char * ARGV[])218ebfedea0SLionel Sambuc int main(int Argc, char *ARGV[])
219ebfedea0SLionel Sambuc {
220ebfedea0SLionel Sambuc     ARGS arg;
221ebfedea0SLionel Sambuc #define PROG_NAME_SIZE  39
222ebfedea0SLionel Sambuc     char pname[PROG_NAME_SIZE + 1];
223ebfedea0SLionel Sambuc     FUNCTION f, *fp;
224ebfedea0SLionel Sambuc     MS_STATIC const char *prompt;
225ebfedea0SLionel Sambuc     MS_STATIC char buf[1024];
226ebfedea0SLionel Sambuc     char *to_free = NULL;
227ebfedea0SLionel Sambuc     int n, i, ret = 0;
228ebfedea0SLionel Sambuc     int argc;
229ebfedea0SLionel Sambuc     char **argv, *p;
230ebfedea0SLionel Sambuc     LHASH_OF(FUNCTION) *prog = NULL;
231ebfedea0SLionel Sambuc     long errline;
232ebfedea0SLionel Sambuc 
233ebfedea0SLionel Sambuc #if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
234*0a6a1f1dSLionel Sambuc     /*-
235*0a6a1f1dSLionel Sambuc      * 2011-03-22 SMS.
236ebfedea0SLionel Sambuc      * If we have 32-bit pointers everywhere, then we're safe, and
237ebfedea0SLionel Sambuc      * we bypass this mess, as on non-VMS systems.  (See ARGV,
238ebfedea0SLionel Sambuc      * above.)
239ebfedea0SLionel Sambuc      * Problem 1: Compaq/HP C before V7.3 always used 32-bit
240ebfedea0SLionel Sambuc      * pointers for argv[].
241ebfedea0SLionel Sambuc      * Fix 1: For a 32-bit argv[], when we're using 64-bit pointers
242ebfedea0SLionel Sambuc      * everywhere else, we always allocate and use a 64-bit
243ebfedea0SLionel Sambuc      * duplicate of argv[].
244ebfedea0SLionel Sambuc      * Problem 2: Compaq/HP C V7.3 (Alpha, IA64) before ECO1 failed
245ebfedea0SLionel Sambuc      * to NULL-terminate a 64-bit argv[].  (As this was written, the
246ebfedea0SLionel Sambuc      * compiler ECO was available only on IA64.)
247ebfedea0SLionel Sambuc      * Fix 2: Unless advised not to (VMS_TRUST_ARGV), we test a
248ebfedea0SLionel Sambuc      * 64-bit argv[argc] for NULL, and, if necessary, use a
249ebfedea0SLionel Sambuc      * (properly) NULL-terminated (64-bit) duplicate of argv[].
250ebfedea0SLionel Sambuc      * The same code is used in either case to duplicate argv[].
251ebfedea0SLionel Sambuc      * Some of these decisions could be handled in preprocessing,
252ebfedea0SLionel Sambuc      * but the code tends to get even uglier, and the penalty for
253ebfedea0SLionel Sambuc      * deciding at compile- or run-time is tiny.
254ebfedea0SLionel Sambuc      */
255ebfedea0SLionel Sambuc     char **Argv = NULL;
256ebfedea0SLionel Sambuc     int free_Argv = 0;
257ebfedea0SLionel Sambuc 
258ebfedea0SLionel Sambuc     if ((sizeof(_Argv) < 8)     /* 32-bit argv[]. */
259ebfedea0SLionel Sambuc # if !defined( VMS_TRUST_ARGV)
260ebfedea0SLionel Sambuc         || (_Argv[Argc] != NULL) /* Untrusted argv[argc] not NULL. */
261ebfedea0SLionel Sambuc # endif
262*0a6a1f1dSLionel Sambuc         ) {
263ebfedea0SLionel Sambuc         int i;
264ebfedea0SLionel Sambuc         Argv = OPENSSL_malloc((Argc + 1) * sizeof(char *));
265*0a6a1f1dSLionel Sambuc         if (Argv == NULL) {
266*0a6a1f1dSLionel Sambuc             ret = -1;
267*0a6a1f1dSLionel Sambuc             goto end;
268*0a6a1f1dSLionel Sambuc         }
269ebfedea0SLionel Sambuc         for (i = 0; i < Argc; i++)
270ebfedea0SLionel Sambuc             Argv[i] = _Argv[i];
271ebfedea0SLionel Sambuc         Argv[Argc] = NULL;      /* Certain NULL termination. */
272ebfedea0SLionel Sambuc         free_Argv = 1;
273*0a6a1f1dSLionel Sambuc     } else {
274*0a6a1f1dSLionel Sambuc         /*
275*0a6a1f1dSLionel Sambuc          * Use the known-good 32-bit argv[] (which needs the type cast to
276*0a6a1f1dSLionel Sambuc          * satisfy the compiler), or the trusted or tested-good 64-bit argv[]
277*0a6a1f1dSLionel Sambuc          * as-is.
278*0a6a1f1dSLionel Sambuc          */
279ebfedea0SLionel Sambuc         Argv = (char **)_Argv;
280ebfedea0SLionel Sambuc     }
281*0a6a1f1dSLionel Sambuc #endif                          /* defined( OPENSSL_SYS_VMS) &&
282*0a6a1f1dSLionel Sambuc                                  * (__INITIAL_POINTER_SIZE == 64) */
283ebfedea0SLionel Sambuc 
284ebfedea0SLionel Sambuc     arg.data = NULL;
285ebfedea0SLionel Sambuc     arg.count = 0;
286ebfedea0SLionel Sambuc 
287ebfedea0SLionel Sambuc     if (bio_err == NULL)
288ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
289ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
290ebfedea0SLionel Sambuc 
291*0a6a1f1dSLionel Sambuc     if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) { /* if not defined, use
292*0a6a1f1dSLionel Sambuc                                                    * compiled-in library
293*0a6a1f1dSLionel Sambuc                                                    * defaults */
294*0a6a1f1dSLionel Sambuc         if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))) {
295ebfedea0SLionel Sambuc             CRYPTO_malloc_debug_init();
296ebfedea0SLionel Sambuc             CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
297*0a6a1f1dSLionel Sambuc         } else {
298ebfedea0SLionel Sambuc             /* OPENSSL_DEBUG_MEMORY=off */
299ebfedea0SLionel Sambuc             CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
300ebfedea0SLionel Sambuc         }
301ebfedea0SLionel Sambuc     }
302ebfedea0SLionel Sambuc     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
303ebfedea0SLionel Sambuc 
304ebfedea0SLionel Sambuc #if 0
305ebfedea0SLionel Sambuc     if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
306ebfedea0SLionel Sambuc #endif
307ebfedea0SLionel Sambuc     {
308ebfedea0SLionel Sambuc         CRYPTO_set_locking_callback(lock_dbg_cb);
309ebfedea0SLionel Sambuc     }
310ebfedea0SLionel Sambuc 
311ebfedea0SLionel Sambuc     if (getenv("OPENSSL_FIPS")) {
312ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
313ebfedea0SLionel Sambuc         if (!FIPS_mode_set(1)) {
314ebfedea0SLionel Sambuc             ERR_load_crypto_strings();
315ebfedea0SLionel Sambuc             ERR_print_errors(BIO_new_fp(stderr, BIO_NOCLOSE));
316ebfedea0SLionel Sambuc             EXIT(1);
317ebfedea0SLionel Sambuc         }
318ebfedea0SLionel Sambuc #else
319ebfedea0SLionel Sambuc         fprintf(stderr, "FIPS mode not supported.\n");
320ebfedea0SLionel Sambuc         EXIT(1);
321ebfedea0SLionel Sambuc #endif
322ebfedea0SLionel Sambuc     }
323ebfedea0SLionel Sambuc 
324ebfedea0SLionel Sambuc     apps_startup();
325ebfedea0SLionel Sambuc 
326ebfedea0SLionel Sambuc     /* Lets load up our environment a little */
327ebfedea0SLionel Sambuc     p = getenv("OPENSSL_CONF");
328ebfedea0SLionel Sambuc     if (p == NULL)
329ebfedea0SLionel Sambuc         p = getenv("SSLEAY_CONF");
330ebfedea0SLionel Sambuc     if (p == NULL)
331ebfedea0SLionel Sambuc         p = to_free = make_config_name();
332ebfedea0SLionel Sambuc 
333ebfedea0SLionel Sambuc     default_config_file = p;
334ebfedea0SLionel Sambuc 
335ebfedea0SLionel Sambuc     config = NCONF_new(NULL);
336ebfedea0SLionel Sambuc     i = NCONF_load(config, p, &errline);
337*0a6a1f1dSLionel Sambuc     if (i == 0) {
338ebfedea0SLionel Sambuc         if (ERR_GET_REASON(ERR_peek_last_error())
339*0a6a1f1dSLionel Sambuc             == CONF_R_NO_SUCH_FILE) {
340*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "WARNING: can't open config file: %s\n", p);
341ebfedea0SLionel Sambuc             ERR_clear_error();
342ebfedea0SLionel Sambuc             NCONF_free(config);
343ebfedea0SLionel Sambuc             config = NULL;
344*0a6a1f1dSLionel Sambuc         } else {
345ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
346ebfedea0SLionel Sambuc             NCONF_free(config);
347ebfedea0SLionel Sambuc             exit(1);
348ebfedea0SLionel Sambuc         }
349ebfedea0SLionel Sambuc     }
350ebfedea0SLionel Sambuc 
351ebfedea0SLionel Sambuc     prog = prog_init();
352ebfedea0SLionel Sambuc 
353ebfedea0SLionel Sambuc     /* first check the program name */
354ebfedea0SLionel Sambuc     program_name(Argv[0], pname, sizeof pname);
355ebfedea0SLionel Sambuc 
356ebfedea0SLionel Sambuc     f.name = pname;
357ebfedea0SLionel Sambuc     fp = lh_FUNCTION_retrieve(prog, &f);
358*0a6a1f1dSLionel Sambuc     if (fp != NULL) {
359ebfedea0SLionel Sambuc         Argv[0] = pname;
360ebfedea0SLionel Sambuc         ret = fp->func(Argc, Argv);
361ebfedea0SLionel Sambuc         goto end;
362ebfedea0SLionel Sambuc     }
363ebfedea0SLionel Sambuc 
364*0a6a1f1dSLionel Sambuc     /*
365*0a6a1f1dSLionel Sambuc      * ok, now check that there are not arguments, if there are, run with
366*0a6a1f1dSLionel Sambuc      * them, shifting the ssleay off the front
367*0a6a1f1dSLionel Sambuc      */
368*0a6a1f1dSLionel Sambuc     if (Argc != 1) {
369ebfedea0SLionel Sambuc         Argc--;
370ebfedea0SLionel Sambuc         Argv++;
371ebfedea0SLionel Sambuc         ret = do_cmd(prog, Argc, Argv);
372*0a6a1f1dSLionel Sambuc         if (ret < 0)
373*0a6a1f1dSLionel Sambuc             ret = 0;
374ebfedea0SLionel Sambuc         goto end;
375ebfedea0SLionel Sambuc     }
376ebfedea0SLionel Sambuc 
377ebfedea0SLionel Sambuc     /* ok, lets enter the old 'OpenSSL>' mode */
378ebfedea0SLionel Sambuc 
379*0a6a1f1dSLionel Sambuc     for (;;) {
380ebfedea0SLionel Sambuc         ret = 0;
381ebfedea0SLionel Sambuc         p = buf;
382ebfedea0SLionel Sambuc         n = sizeof buf;
383ebfedea0SLionel Sambuc         i = 0;
384*0a6a1f1dSLionel Sambuc         for (;;) {
385ebfedea0SLionel Sambuc             p[0] = '\0';
386ebfedea0SLionel Sambuc             if (i++)
387ebfedea0SLionel Sambuc                 prompt = ">";
388*0a6a1f1dSLionel Sambuc             else
389*0a6a1f1dSLionel Sambuc                 prompt = "OpenSSL> ";
390ebfedea0SLionel Sambuc             fputs(prompt, stdout);
391ebfedea0SLionel Sambuc             fflush(stdout);
392ebfedea0SLionel Sambuc             if (!fgets(p, n, stdin))
393ebfedea0SLionel Sambuc                 goto end;
394*0a6a1f1dSLionel Sambuc             if (p[0] == '\0')
395*0a6a1f1dSLionel Sambuc                 goto end;
396ebfedea0SLionel Sambuc             i = strlen(p);
397*0a6a1f1dSLionel Sambuc             if (i <= 1)
398*0a6a1f1dSLionel Sambuc                 break;
399*0a6a1f1dSLionel Sambuc             if (p[i - 2] != '\\')
400*0a6a1f1dSLionel Sambuc                 break;
401ebfedea0SLionel Sambuc             i -= 2;
402ebfedea0SLionel Sambuc             p += i;
403ebfedea0SLionel Sambuc             n -= i;
404ebfedea0SLionel Sambuc         }
405*0a6a1f1dSLionel Sambuc         if (!chopup_args(&arg, buf, &argc, &argv))
406*0a6a1f1dSLionel Sambuc             break;
407ebfedea0SLionel Sambuc 
408ebfedea0SLionel Sambuc         ret = do_cmd(prog, argc, argv);
409*0a6a1f1dSLionel Sambuc         if (ret < 0) {
410ebfedea0SLionel Sambuc             ret = 0;
411ebfedea0SLionel Sambuc             goto end;
412ebfedea0SLionel Sambuc         }
413ebfedea0SLionel Sambuc         if (ret != 0)
414ebfedea0SLionel Sambuc             BIO_printf(bio_err, "error in %s\n", argv[0]);
415ebfedea0SLionel Sambuc         (void)BIO_flush(bio_err);
416ebfedea0SLionel Sambuc     }
417ebfedea0SLionel Sambuc     BIO_printf(bio_err, "bad exit\n");
418ebfedea0SLionel Sambuc     ret = 1;
419ebfedea0SLionel Sambuc  end:
420ebfedea0SLionel Sambuc     if (to_free)
421ebfedea0SLionel Sambuc         OPENSSL_free(to_free);
422*0a6a1f1dSLionel Sambuc     if (config != NULL) {
423ebfedea0SLionel Sambuc         NCONF_free(config);
424ebfedea0SLionel Sambuc         config = NULL;
425ebfedea0SLionel Sambuc     }
426*0a6a1f1dSLionel Sambuc     if (prog != NULL)
427*0a6a1f1dSLionel Sambuc         lh_FUNCTION_free(prog);
428*0a6a1f1dSLionel Sambuc     if (arg.data != NULL)
429*0a6a1f1dSLionel Sambuc         OPENSSL_free(arg.data);
430ebfedea0SLionel Sambuc 
431ebfedea0SLionel Sambuc #if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
432ebfedea0SLionel Sambuc     /* Free any duplicate Argv[] storage. */
433*0a6a1f1dSLionel Sambuc     if (free_Argv) {
434ebfedea0SLionel Sambuc         OPENSSL_free(Argv);
435ebfedea0SLionel Sambuc     }
436ebfedea0SLionel Sambuc #endif
437*0a6a1f1dSLionel Sambuc     apps_shutdown();
438*0a6a1f1dSLionel Sambuc     CRYPTO_mem_leaks(bio_err);
439*0a6a1f1dSLionel Sambuc     if (bio_err != NULL) {
440*0a6a1f1dSLionel Sambuc         BIO_free(bio_err);
441*0a6a1f1dSLionel Sambuc         bio_err = NULL;
442*0a6a1f1dSLionel Sambuc     }
443*0a6a1f1dSLionel Sambuc 
444ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
445ebfedea0SLionel Sambuc }
446ebfedea0SLionel Sambuc 
447ebfedea0SLionel Sambuc #define LIST_STANDARD_COMMANDS "list-standard-commands"
448ebfedea0SLionel Sambuc #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
449ebfedea0SLionel Sambuc #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
450ebfedea0SLionel Sambuc #define LIST_CIPHER_COMMANDS "list-cipher-commands"
451ebfedea0SLionel Sambuc #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
452ebfedea0SLionel Sambuc #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
453ebfedea0SLionel Sambuc 
do_cmd(LHASH_OF (FUNCTION)* prog,int argc,char * argv[])454ebfedea0SLionel Sambuc static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
455ebfedea0SLionel Sambuc {
456ebfedea0SLionel Sambuc     FUNCTION f, *fp;
457ebfedea0SLionel Sambuc     int i, ret = 1, tp, nl;
458ebfedea0SLionel Sambuc 
459*0a6a1f1dSLionel Sambuc     if ((argc <= 0) || (argv[0] == NULL)) {
460*0a6a1f1dSLionel Sambuc         ret = 0;
461*0a6a1f1dSLionel Sambuc         goto end;
462*0a6a1f1dSLionel Sambuc     }
463ebfedea0SLionel Sambuc     f.name = argv[0];
464ebfedea0SLionel Sambuc     fp = lh_FUNCTION_retrieve(prog, &f);
465*0a6a1f1dSLionel Sambuc     if (fp == NULL) {
466*0a6a1f1dSLionel Sambuc         if (EVP_get_digestbyname(argv[0])) {
467ebfedea0SLionel Sambuc             f.type = FUNC_TYPE_MD;
468ebfedea0SLionel Sambuc             f.func = dgst_main;
469ebfedea0SLionel Sambuc             fp = &f;
470*0a6a1f1dSLionel Sambuc         } else if (EVP_get_cipherbyname(argv[0])) {
471ebfedea0SLionel Sambuc             f.type = FUNC_TYPE_CIPHER;
472ebfedea0SLionel Sambuc             f.func = enc_main;
473ebfedea0SLionel Sambuc             fp = &f;
474ebfedea0SLionel Sambuc         }
475ebfedea0SLionel Sambuc     }
476*0a6a1f1dSLionel Sambuc     if (fp != NULL) {
477ebfedea0SLionel Sambuc         ret = fp->func(argc, argv);
478*0a6a1f1dSLionel Sambuc     } else if ((strncmp(argv[0], "no-", 3)) == 0) {
479ebfedea0SLionel Sambuc         BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
480ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
481ebfedea0SLionel Sambuc         {
482ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
483ebfedea0SLionel Sambuc             bio_stdout = BIO_push(tmpbio, bio_stdout);
484ebfedea0SLionel Sambuc         }
485ebfedea0SLionel Sambuc #endif
486ebfedea0SLionel Sambuc         f.name = argv[0] + 3;
487ebfedea0SLionel Sambuc         ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
488ebfedea0SLionel Sambuc         if (!ret)
489ebfedea0SLionel Sambuc             BIO_printf(bio_stdout, "%s\n", argv[0]);
490ebfedea0SLionel Sambuc         else
491ebfedea0SLionel Sambuc             BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
492ebfedea0SLionel Sambuc         BIO_free_all(bio_stdout);
493ebfedea0SLionel Sambuc         goto end;
494*0a6a1f1dSLionel Sambuc     } else if ((strcmp(argv[0], "quit") == 0) ||
495ebfedea0SLionel Sambuc                (strcmp(argv[0], "q") == 0) ||
496ebfedea0SLionel Sambuc                (strcmp(argv[0], "exit") == 0) ||
497*0a6a1f1dSLionel Sambuc                (strcmp(argv[0], "bye") == 0)) {
498ebfedea0SLionel Sambuc         ret = -1;
499ebfedea0SLionel Sambuc         goto end;
500*0a6a1f1dSLionel Sambuc     } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
501ebfedea0SLionel Sambuc                (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
502ebfedea0SLionel Sambuc                (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
503ebfedea0SLionel Sambuc                (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
504ebfedea0SLionel Sambuc                (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
505*0a6a1f1dSLionel Sambuc                (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
506ebfedea0SLionel Sambuc         int list_type;
507ebfedea0SLionel Sambuc         BIO *bio_stdout;
508ebfedea0SLionel Sambuc 
509ebfedea0SLionel Sambuc         if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
510ebfedea0SLionel Sambuc             list_type = FUNC_TYPE_GENERAL;
511ebfedea0SLionel Sambuc         else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
512ebfedea0SLionel Sambuc             list_type = FUNC_TYPE_MD;
513ebfedea0SLionel Sambuc         else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
514ebfedea0SLionel Sambuc             list_type = FUNC_TYPE_MD_ALG;
515ebfedea0SLionel Sambuc         else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
516ebfedea0SLionel Sambuc             list_type = FUNC_TYPE_PKEY;
517ebfedea0SLionel Sambuc         else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
518ebfedea0SLionel Sambuc             list_type = FUNC_TYPE_CIPHER_ALG;
519ebfedea0SLionel Sambuc         else                    /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
520ebfedea0SLionel Sambuc             list_type = FUNC_TYPE_CIPHER;
521ebfedea0SLionel Sambuc         bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
522ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
523ebfedea0SLionel Sambuc         {
524ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
525ebfedea0SLionel Sambuc             bio_stdout = BIO_push(tmpbio, bio_stdout);
526ebfedea0SLionel Sambuc         }
527ebfedea0SLionel Sambuc #endif
528ebfedea0SLionel Sambuc 
529ebfedea0SLionel Sambuc         if (!load_config(bio_err, NULL))
530ebfedea0SLionel Sambuc             goto end;
531ebfedea0SLionel Sambuc 
532ebfedea0SLionel Sambuc         if (list_type == FUNC_TYPE_PKEY)
533ebfedea0SLionel Sambuc             list_pkey(bio_stdout);
534ebfedea0SLionel Sambuc         if (list_type == FUNC_TYPE_MD_ALG)
535ebfedea0SLionel Sambuc             list_md(bio_stdout);
536ebfedea0SLionel Sambuc         if (list_type == FUNC_TYPE_CIPHER_ALG)
537ebfedea0SLionel Sambuc             list_cipher(bio_stdout);
538*0a6a1f1dSLionel Sambuc         else {
539ebfedea0SLionel Sambuc             for (fp = functions; fp->name != NULL; fp++)
540ebfedea0SLionel Sambuc                 if (fp->type == list_type)
541*0a6a1f1dSLionel Sambuc                     BIO_printf(bio_stdout, "%s\n", fp->name);
542ebfedea0SLionel Sambuc         }
543ebfedea0SLionel Sambuc         BIO_free_all(bio_stdout);
544ebfedea0SLionel Sambuc         ret = 0;
545ebfedea0SLionel Sambuc         goto end;
546*0a6a1f1dSLionel Sambuc     } else {
547ebfedea0SLionel Sambuc         BIO_printf(bio_err, "openssl:Error: '%s' is an invalid command.\n",
548ebfedea0SLionel Sambuc                    argv[0]);
549ebfedea0SLionel Sambuc         BIO_printf(bio_err, "\nStandard commands");
550ebfedea0SLionel Sambuc         i = 0;
551ebfedea0SLionel Sambuc         tp = 0;
552*0a6a1f1dSLionel Sambuc         for (fp = functions; fp->name != NULL; fp++) {
553ebfedea0SLionel Sambuc             nl = 0;
554ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_CAMELLIA
555ebfedea0SLionel Sambuc             if (((i++) % 5) == 0)
556ebfedea0SLionel Sambuc #else
557ebfedea0SLionel Sambuc             if (((i++) % 4) == 0)
558ebfedea0SLionel Sambuc #endif
559ebfedea0SLionel Sambuc             {
560ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "\n");
561ebfedea0SLionel Sambuc                 nl = 1;
562ebfedea0SLionel Sambuc             }
563*0a6a1f1dSLionel Sambuc             if (fp->type != tp) {
564ebfedea0SLionel Sambuc                 tp = fp->type;
565*0a6a1f1dSLionel Sambuc                 if (!nl)
566*0a6a1f1dSLionel Sambuc                     BIO_printf(bio_err, "\n");
567*0a6a1f1dSLionel Sambuc                 if (tp == FUNC_TYPE_MD) {
568ebfedea0SLionel Sambuc                     i = 1;
569ebfedea0SLionel Sambuc                     BIO_printf(bio_err,
570ebfedea0SLionel Sambuc                                "\nMessage Digest commands (see the `dgst' command for more details)\n");
571*0a6a1f1dSLionel Sambuc                 } else if (tp == FUNC_TYPE_CIPHER) {
572ebfedea0SLionel Sambuc                     i = 1;
573*0a6a1f1dSLionel Sambuc                     BIO_printf(bio_err,
574*0a6a1f1dSLionel Sambuc                                "\nCipher commands (see the `enc' command for more details)\n");
575ebfedea0SLionel Sambuc                 }
576ebfedea0SLionel Sambuc             }
577ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_CAMELLIA
578ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-15s", fp->name);
579ebfedea0SLionel Sambuc #else
580ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-18s", fp->name);
581ebfedea0SLionel Sambuc #endif
582ebfedea0SLionel Sambuc         }
583ebfedea0SLionel Sambuc         BIO_printf(bio_err, "\n\n");
584ebfedea0SLionel Sambuc         ret = 0;
585ebfedea0SLionel Sambuc     }
586ebfedea0SLionel Sambuc  end:
587ebfedea0SLionel Sambuc     return (ret);
588ebfedea0SLionel Sambuc }
589ebfedea0SLionel Sambuc 
SortFnByName(const void * _f1,const void * _f2)590ebfedea0SLionel Sambuc static int SortFnByName(const void *_f1, const void *_f2)
591ebfedea0SLionel Sambuc {
592ebfedea0SLionel Sambuc     const FUNCTION *f1 = _f1;
593ebfedea0SLionel Sambuc     const FUNCTION *f2 = _f2;
594ebfedea0SLionel Sambuc 
595ebfedea0SLionel Sambuc     if (f1->type != f2->type)
596ebfedea0SLionel Sambuc         return f1->type - f2->type;
597ebfedea0SLionel Sambuc     return strcmp(f1->name, f2->name);
598ebfedea0SLionel Sambuc }
599ebfedea0SLionel Sambuc 
list_pkey(BIO * out)600ebfedea0SLionel Sambuc static void list_pkey(BIO *out)
601ebfedea0SLionel Sambuc {
602ebfedea0SLionel Sambuc     int i;
603*0a6a1f1dSLionel Sambuc     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
604ebfedea0SLionel Sambuc         const EVP_PKEY_ASN1_METHOD *ameth;
605ebfedea0SLionel Sambuc         int pkey_id, pkey_base_id, pkey_flags;
606ebfedea0SLionel Sambuc         const char *pinfo, *pem_str;
607ebfedea0SLionel Sambuc         ameth = EVP_PKEY_asn1_get0(i);
608ebfedea0SLionel Sambuc         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
609ebfedea0SLionel Sambuc                                 &pinfo, &pem_str, ameth);
610*0a6a1f1dSLionel Sambuc         if (pkey_flags & ASN1_PKEY_ALIAS) {
611*0a6a1f1dSLionel Sambuc             BIO_printf(out, "Name: %s\n", OBJ_nid2ln(pkey_id));
612ebfedea0SLionel Sambuc             BIO_printf(out, "\tType: Alias to %s\n",
613ebfedea0SLionel Sambuc                        OBJ_nid2ln(pkey_base_id));
614*0a6a1f1dSLionel Sambuc         } else {
615ebfedea0SLionel Sambuc             BIO_printf(out, "Name: %s\n", pinfo);
616ebfedea0SLionel Sambuc             BIO_printf(out, "\tType: %s Algorithm\n",
617ebfedea0SLionel Sambuc                        pkey_flags & ASN1_PKEY_DYNAMIC ?
618ebfedea0SLionel Sambuc                        "External" : "Builtin");
619ebfedea0SLionel Sambuc             BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
620ebfedea0SLionel Sambuc             if (pem_str == NULL)
621ebfedea0SLionel Sambuc                 pem_str = "(none)";
622ebfedea0SLionel Sambuc             BIO_printf(out, "\tPEM string: %s\n", pem_str);
623ebfedea0SLionel Sambuc         }
624ebfedea0SLionel Sambuc 
625ebfedea0SLionel Sambuc     }
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc 
list_cipher_fn(const EVP_CIPHER * c,const char * from,const char * to,void * arg)628ebfedea0SLionel Sambuc static void list_cipher_fn(const EVP_CIPHER *c,
629ebfedea0SLionel Sambuc                            const char *from, const char *to, void *arg)
630ebfedea0SLionel Sambuc {
631ebfedea0SLionel Sambuc     if (c)
632ebfedea0SLionel Sambuc         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
633*0a6a1f1dSLionel Sambuc     else {
634ebfedea0SLionel Sambuc         if (!from)
635ebfedea0SLionel Sambuc             from = "<undefined>";
636ebfedea0SLionel Sambuc         if (!to)
637ebfedea0SLionel Sambuc             to = "<undefined>";
638ebfedea0SLionel Sambuc         BIO_printf(arg, "%s => %s\n", from, to);
639ebfedea0SLionel Sambuc     }
640ebfedea0SLionel Sambuc }
641ebfedea0SLionel Sambuc 
list_cipher(BIO * out)642ebfedea0SLionel Sambuc static void list_cipher(BIO *out)
643ebfedea0SLionel Sambuc {
644ebfedea0SLionel Sambuc     EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
645ebfedea0SLionel Sambuc }
646ebfedea0SLionel Sambuc 
list_md_fn(const EVP_MD * m,const char * from,const char * to,void * arg)647ebfedea0SLionel Sambuc static void list_md_fn(const EVP_MD *m,
648ebfedea0SLionel Sambuc                        const char *from, const char *to, void *arg)
649ebfedea0SLionel Sambuc {
650ebfedea0SLionel Sambuc     if (m)
651ebfedea0SLionel Sambuc         BIO_printf(arg, "%s\n", EVP_MD_name(m));
652*0a6a1f1dSLionel Sambuc     else {
653ebfedea0SLionel Sambuc         if (!from)
654ebfedea0SLionel Sambuc             from = "<undefined>";
655ebfedea0SLionel Sambuc         if (!to)
656ebfedea0SLionel Sambuc             to = "<undefined>";
657ebfedea0SLionel Sambuc         BIO_printf(arg, "%s => %s\n", from, to);
658ebfedea0SLionel Sambuc     }
659ebfedea0SLionel Sambuc }
660ebfedea0SLionel Sambuc 
list_md(BIO * out)661ebfedea0SLionel Sambuc static void list_md(BIO *out)
662ebfedea0SLionel Sambuc {
663ebfedea0SLionel Sambuc     EVP_MD_do_all_sorted(list_md_fn, out);
664ebfedea0SLionel Sambuc }
665ebfedea0SLionel Sambuc 
function_cmp(const FUNCTION * a,const FUNCTION * b)666ebfedea0SLionel Sambuc static int MS_CALLBACK function_cmp(const FUNCTION * a, const FUNCTION * b)
667ebfedea0SLionel Sambuc {
668ebfedea0SLionel Sambuc     return strncmp(a->name, b->name, 8);
669ebfedea0SLionel Sambuc }
670*0a6a1f1dSLionel Sambuc 
IMPLEMENT_LHASH_COMP_FN(function,FUNCTION)671ebfedea0SLionel Sambuc static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
672ebfedea0SLionel Sambuc 
673ebfedea0SLionel Sambuc static unsigned long MS_CALLBACK function_hash(const FUNCTION * a)
674ebfedea0SLionel Sambuc {
675ebfedea0SLionel Sambuc     return lh_strhash(a->name);
676ebfedea0SLionel Sambuc }
677*0a6a1f1dSLionel Sambuc 
IMPLEMENT_LHASH_HASH_FN(function,FUNCTION)678ebfedea0SLionel Sambuc static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
679ebfedea0SLionel Sambuc 
680ebfedea0SLionel Sambuc static LHASH_OF(FUNCTION) *prog_init(void)
681ebfedea0SLionel Sambuc {
682ebfedea0SLionel Sambuc     LHASH_OF(FUNCTION) *ret;
683ebfedea0SLionel Sambuc     FUNCTION *f;
684ebfedea0SLionel Sambuc     size_t i;
685ebfedea0SLionel Sambuc 
686ebfedea0SLionel Sambuc     /* Purely so it looks nice when the user hits ? */
687*0a6a1f1dSLionel Sambuc     for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
688ebfedea0SLionel Sambuc     qsort(functions, i, sizeof *functions, SortFnByName);
689ebfedea0SLionel Sambuc 
690ebfedea0SLionel Sambuc     if ((ret = lh_FUNCTION_new()) == NULL)
691ebfedea0SLionel Sambuc         return (NULL);
692ebfedea0SLionel Sambuc 
693ebfedea0SLionel Sambuc     for (f = functions; f->name != NULL; f++)
694ebfedea0SLionel Sambuc         (void)lh_FUNCTION_insert(ret, f);
695ebfedea0SLionel Sambuc     return (ret);
696ebfedea0SLionel Sambuc }
697