]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/openssl.c
Rename some BUF_xxx to OPENSSL_xxx
[thirdparty/openssl.git] / apps / openssl.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
3ac82faa 57/* ====================================================================
f3dea9a5 58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
3ac82faa
BM
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
0f113f3e 65 * notice, this list of conditions and the following disclaimer.
3ac82faa
BM
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110
d02b48c6
RE
111#include <stdio.h>
112#include <string.h>
113#include <stdlib.h>
ec577822
BM
114#include <openssl/bio.h>
115#include <openssl/crypto.h>
3d1160d5 116#include <openssl/rand.h>
ec577822
BM
117#include <openssl/lhash.h>
118#include <openssl/conf.h>
119#include <openssl/x509.h>
120#include <openssl/pem.h>
121#include <openssl/ssl.h>
0b13e9f0 122#ifndef OPENSSL_NO_ENGINE
0f113f3e 123# include <openssl/engine.h>
0b13e9f0 124#endif
ec577822 125#include <openssl/err.h>
856650de 126#ifdef OPENSSL_FIPS
0f113f3e 127# include <openssl/fips.h>
856650de 128#endif
3b061a00
RS
129#define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
130#include "s_apps.h"
131/* Needed to get the other O_xxx flags. */
132#ifdef OPENSSL_SYS_VMS
133# include <unixio.h>
134#endif
7e1b7485
RS
135#define INCLUDE_FUNCTION_TABLE
136#include "apps.h"
137
7e1b7485
RS
138
139#ifdef OPENSSL_NO_CAMELLIA
140# define FORMAT "%-15s"
141# define COLUMNS 5
142#else
143# define FORMAT "%-18s"
144# define COLUMNS 4
145#endif
146
147/* Special sentinel to exit the program. */
148#define EXIT_THE_PROGRAM (-1)
d02b48c6 149
0f113f3e
MC
150/*
151 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
152 * the base prototypes (we cast each variable inside the function to the
153 * required type of "FUNCTION*"). This removes the necessity for
154 * macro-generated wrapper functions.
155 */
7e1b7485 156DECLARE_LHASH_OF(FUNCTION);
0f113f3e
MC
157static LHASH_OF(FUNCTION) *prog_init(void);
158static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
2f58faad
RS
159static void list_pkey(void);
160static void list_type(FUNC_TYPE ft);
a760a380 161static void list_disabled(void);
0f113f3e 162char *default_config_file = NULL;
d02b48c6 163
df2ee0e2 164static CONF *config = NULL;
7e1b7485
RS
165BIO *bio_in = NULL;
166BIO *bio_out = NULL;
0f113f3e 167BIO *bio_err = NULL;
7e1b7485 168
a0a82324 169static int apps_startup()
7e1b7485
RS
170{
171#ifdef SIGPIPE
172 signal(SIGPIPE, SIG_IGN);
173#endif
174 CRYPTO_malloc_init();
175 ERR_load_crypto_strings();
176 ERR_load_SSL_strings();
a0a82324 177
1f08d945
DB
178 OPENSSL_load_builtin_modules();
179#ifndef OPENSSL_NO_ENGINE
180 ENGINE_load_builtin_engines();
181#endif
a0a82324
MC
182 if (!app_load_modules(NULL)) {
183 ERR_print_errors(bio_err);
184 BIO_printf(bio_err, "Error loading default configuration\n");
185 return 0;
186 }
187
7e1b7485
RS
188 OpenSSL_add_all_algorithms();
189 OpenSSL_add_ssl_algorithms();
190 setup_ui_method();
191 /*SSL_library_init();*/
a0a82324 192 return 1;
7e1b7485
RS
193}
194
195static void apps_shutdown()
196{
197#ifndef OPENSSL_NO_ENGINE
198 ENGINE_cleanup();
199#endif
200 destroy_ui_method();
201 CONF_modules_unload(1);
202#ifndef OPENSSL_NO_COMP
203 COMP_zlib_cleanup();
45d051c8 204 SSL_COMP_free_compression_methods();
7e1b7485
RS
205#endif
206 OBJ_cleanup();
207 EVP_cleanup();
208 CRYPTO_cleanup_all_ex_data();
209 ERR_remove_thread_state(NULL);
210 RAND_cleanup();
211 ERR_free_strings();
212}
213
214static char *make_config_name()
215{
cc01d217 216 const char *t;
7e1b7485
RS
217 size_t len;
218 char *p;
219
b0700d2c 220 if ((t = getenv("OPENSSL_CONF")) != NULL)
7644a9ae 221 return OPENSSL_strdup(t);
cc01d217
RS
222
223 t = X509_get_default_cert_area();
224 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
68dc6824 225 p = app_malloc(len, "config filename buffer");
cc01d217 226 strcpy(p, t);
7e1b7485 227#ifndef OPENSSL_SYS_VMS
cc01d217 228 strcat(p, "/");
d02b48c6 229#endif
cc01d217 230 strcat(p, OPENSSL_CONF);
7e1b7485
RS
231
232 return p;
233}
234
3ac82faa 235static void lock_dbg_cb(int mode, int type, const char *file, int line)
0f113f3e 236{
7e1b7485 237 static int modes[CRYPTO_NUM_LOCKS];
0f113f3e 238 const char *errstr = NULL;
7e1b7485 239 int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
0f113f3e 240
7e1b7485 241 if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
0f113f3e
MC
242 errstr = "invalid mode";
243 goto err;
244 }
245
246 if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
247 errstr = "type out of bounds";
248 goto err;
249 }
250
251 if (mode & CRYPTO_LOCK) {
252 if (modes[type]) {
253 errstr = "already locked";
7e1b7485 254 /* must not happen in a single-threaded program --> deadlock! */
0f113f3e
MC
255 goto err;
256 }
0f113f3e
MC
257 modes[type] = rw;
258 } else if (mode & CRYPTO_UNLOCK) {
259 if (!modes[type]) {
260 errstr = "not locked";
261 goto err;
262 }
263
264 if (modes[type] != rw) {
265 errstr = (rw == CRYPTO_READ) ?
266 "CRYPTO_r_unlock on write lock" :
267 "CRYPTO_w_unlock on read lock";
268 }
269
270 modes[type] = 0;
271 } else {
272 errstr = "invalid mode";
273 goto err;
274 }
3ac82faa
BM
275
276 err:
0f113f3e 277 if (errstr) {
7768e116
RS
278 BIO_printf(bio_err,
279 "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
280 errstr, mode, type, file, line);
0f113f3e
MC
281 }
282}
3ac82faa 283
7e1b7485
RS
284#if defined( OPENSSL_SYS_VMS)
285extern char **copy_argv(int *argc, char **argv);
286#endif
287
288int main(int argc, char *argv[])
0f113f3e 289{
0f113f3e 290 FUNCTION f, *fp;
0f113f3e 291 LHASH_OF(FUNCTION) *prog = NULL;
7e1b7485 292 char **copied_argv = NULL;
cc01d217 293 char *p, *pname;
7e1b7485
RS
294 char buf[1024];
295 const char *prompt;
296 ARGS arg;
297 int first, n, i, ret = 0;
8ecef24a 298
7e1b7485
RS
299 arg.argv = NULL;
300 arg.size = 0;
301
7768e116
RS
302 /* Set up some of the environment. */
303 default_config_file = make_config_name();
a60994df
RL
304 bio_in = dup_bio_in(FORMAT_TEXT);
305 bio_out = dup_bio_out(FORMAT_TEXT);
7768e116
RS
306 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
307
7e1b7485
RS
308#if defined( OPENSSL_SYS_VMS)
309 copied_argv = argv = copy_argv(&argc, argv);
310#endif
311
312 p = getenv("OPENSSL_DEBUG_MEMORY");
313 if (p == NULL)
314 /* if not set, use compiled-in default */
315 ;
316 else if (strcmp(p, "off") != 0) {
317 CRYPTO_malloc_debug_init();
318 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
0f113f3e 319 } else {
7e1b7485 320 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
0f113f3e
MC
321 }
322 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
75d0ebef 323 CRYPTO_set_locking_callback(lock_dbg_cb);
3ac82faa 324
0f113f3e 325 if (getenv("OPENSSL_FIPS")) {
856650de 326#ifdef OPENSSL_FIPS
0f113f3e
MC
327 if (!FIPS_mode_set(1)) {
328 ERR_load_crypto_strings();
7768e116
RS
329 ERR_print_errors(bio_err);
330 return 1;
0f113f3e 331 }
856650de 332#else
7768e116
RS
333 BIO_printf(bio_err, "FIPS mode not supported.\n");
334 return 1;
856650de 335#endif
0f113f3e
MC
336 }
337
a0a82324
MC
338 if (!apps_startup())
339 goto end;
340
7e1b7485
RS
341 prog = prog_init();
342 pname = opt_progname(argv[0]);
0f113f3e 343
0f113f3e 344 /* first check the program name */
0f113f3e
MC
345 f.name = pname;
346 fp = lh_FUNCTION_retrieve(prog, &f);
347 if (fp != NULL) {
7e1b7485
RS
348 argv[0] = pname;
349 ret = fp->func(argc, argv);
0f113f3e
MC
350 goto end;
351 }
352
7e1b7485
RS
353 /* If there is stuff on the command line, run with that. */
354 if (argc != 1) {
355 argc--;
356 argv++;
357 ret = do_cmd(prog, argc, argv);
0f113f3e
MC
358 if (ret < 0)
359 ret = 0;
360 goto end;
361 }
362
7e1b7485 363 /* ok, lets enter interactive mode */
0f113f3e
MC
364 for (;;) {
365 ret = 0;
57d5edad
RS
366 /* Read a line, continue reading if line ends with \ */
367 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
1c9c2435 368 prompt = first ? "OpenSSL> " : "> ";
0f113f3e 369 p[0] = '\0';
57d5edad 370#ifndef READLINE
0f113f3e
MC
371 fputs(prompt, stdout);
372 fflush(stdout);
373 if (!fgets(p, n, stdin))
374 goto end;
375 if (p[0] == '\0')
376 goto end;
377 i = strlen(p);
378 if (i <= 1)
379 break;
380 if (p[i - 2] != '\\')
381 break;
382 i -= 2;
383 p += i;
384 n -= i;
57d5edad
RS
385#else
386 {
387 extern char *readline(const char *);
388 extern void add_history(const char *cp);
389 char *text;
390
391 char *text = readline(prompt);
392 if (text == NULL)
393 goto end;
394 i = strlen(text);
395 if (i == 0 || i > n)
396 break;
397 if (text[i - 1] != '\\') {
398 p += strlen(strcpy(p, text));
399 free(text);
400 add_history(buf);
401 break;
402 }
403
404 text[i - 1] = '\0';
405 p += strlen(strcpy(p, text));
406 free(text);
407 n -= i;
408 }
409#endif
0f113f3e 410 }
57d5edad 411
7e1b7485
RS
412 if (!chopup_args(&arg, buf)) {
413 BIO_printf(bio_err, "Can't parse (no memory?)\n");
0f113f3e 414 break;
7e1b7485 415 }
0f113f3e 416
7e1b7485
RS
417 ret = do_cmd(prog, arg.argc, arg.argv);
418 if (ret == EXIT_THE_PROGRAM) {
0f113f3e
MC
419 ret = 0;
420 goto end;
421 }
422 if (ret != 0)
7e1b7485
RS
423 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
424 (void)BIO_flush(bio_out);
0f113f3e
MC
425 (void)BIO_flush(bio_err);
426 }
0f113f3e
MC
427 ret = 1;
428 end:
b548a1f1 429 OPENSSL_free(copied_argv);
cc01d217 430 OPENSSL_free(default_config_file);
25aaa98a
RS
431 NCONF_free(config);
432 config = NULL;
433 lh_FUNCTION_free(prog);
b548a1f1 434 OPENSSL_free(arg.argv);
0f113f3e 435
7e1b7485
RS
436 BIO_free(bio_in);
437 BIO_free_all(bio_out);
0f113f3e 438 apps_shutdown();
31b222da 439 CRYPTO_mem_leaks(bio_err);
ca3a82c3 440 BIO_free(bio_err);
7e1b7485
RS
441 return (ret);
442}
443
444OPTIONS exit_options[] = {
445 {NULL}
446};
447
2f58faad
RS
448static void list_cipher_fn(const EVP_CIPHER *c,
449 const char *from, const char *to, void *arg)
450{
451 if (c)
452 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
453 else {
454 if (!from)
455 from = "<undefined>";
456 if (!to)
457 to = "<undefined>";
458 BIO_printf(arg, "%s => %s\n", from, to);
459 }
460}
461
462static void list_md_fn(const EVP_MD *m,
463 const char *from, const char *to, void *arg)
464{
465 if (m)
466 BIO_printf(arg, "%s\n", EVP_MD_name(m));
467 else {
468 if (!from)
469 from = "<undefined>";
470 if (!to)
471 to = "<undefined>";
472 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
473 }
474}
475
7e1b7485
RS
476/* Unified enum for help and list commands. */
477typedef enum HELPLIST_CHOICE {
478 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
479 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
480 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
a760a380 481 OPT_PK_ALGORITHMS, OPT_DISABLED
7e1b7485
RS
482} HELPLIST_CHOICE;
483
484OPTIONS list_options[] = {
485 {"help", OPT_HELP, '-', "Display this summary"},
486 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
487 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
488 "List of message digest commands"},
489 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
490 "List of message digest algorithms"},
491 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
492 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
493 "List of cipher algorithms"},
494 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
495 "List of public key algorithms"},
a760a380
DSH
496 {"disabled", OPT_DISABLED, '-',
497 "List of disabled features"},
7e1b7485
RS
498 {NULL}
499};
500
501int list_main(int argc, char **argv)
502{
503 char *prog;
504 HELPLIST_CHOICE o;
19948cea 505 int done = 0;
7e1b7485
RS
506
507 prog = opt_init(argc, argv, list_options);
508 while ((o = opt_next()) != OPT_EOF) {
509 switch (o) {
19948cea 510 case OPT_EOF: /* Never hit, but suppresses warning */
7e1b7485
RS
511 case OPT_ERR:
512 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
513 return 1;
514 case OPT_HELP:
515 opt_help(list_options);
516 break;
517 case OPT_COMMANDS:
2f58faad
RS
518 list_type(FT_general);
519 break;
7e1b7485 520 case OPT_DIGEST_COMMANDS:
2f58faad
RS
521 list_type(FT_md);
522 break;
7e1b7485 523 case OPT_DIGEST_ALGORITHMS:
2f58faad
RS
524 EVP_MD_do_all_sorted(list_md_fn, bio_out);
525 break;
7e1b7485 526 case OPT_CIPHER_COMMANDS:
2f58faad
RS
527 list_type(FT_cipher);
528 break;
7e1b7485 529 case OPT_CIPHER_ALGORITHMS:
2f58faad
RS
530 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
531 break;
7e1b7485 532 case OPT_PK_ALGORITHMS:
2f58faad
RS
533 list_pkey();
534 break;
a760a380
DSH
535 case OPT_DISABLED:
536 list_disabled();
537 break;
7e1b7485 538 }
19948cea
BL
539 done = 1;
540 }
541
542 if (!done) {
543 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
544 return 1;
7e1b7485
RS
545 }
546
547 return 0;
548}
549
550OPTIONS help_options[] = {
551 {"help", OPT_HELP, '-', "Display this summary"},
552 {NULL}
553};
554
555int help_main(int argc, char **argv)
556{
557 FUNCTION *fp;
558 int i, nl;
559 FUNC_TYPE tp;
560 char *prog;
561 HELPLIST_CHOICE o;
562
563 prog = opt_init(argc, argv, help_options);
564 while ((o = opt_next()) != OPT_EOF) {
565 switch (o) {
566 default:
567 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
568 return 1;
569 case OPT_HELP:
570 opt_help(help_options);
571 return 0;
572 }
573 }
574 argc = opt_num_rest();
575 argv = opt_rest();
576
577 if (argc != 0) {
578 BIO_printf(bio_err, "Usage: %s\n", prog);
579 return 1;
580 }
581
582 BIO_printf(bio_err, "\nStandard commands");
583 i = 0;
584 tp = FT_none;
585 for (fp = functions; fp->name != NULL; fp++) {
586 nl = 0;
587 if (((i++) % COLUMNS) == 0) {
588 BIO_printf(bio_err, "\n");
589 nl = 1;
590 }
591 if (fp->type != tp) {
592 tp = fp->type;
593 if (!nl)
594 BIO_printf(bio_err, "\n");
595 if (tp == FT_md) {
596 i = 1;
597 BIO_printf(bio_err,
598 "\nMessage Digest commands (see the `dgst' command for more details)\n");
599 } else if (tp == FT_cipher) {
600 i = 1;
601 BIO_printf(bio_err,
602 "\nCipher commands (see the `enc' command for more details)\n");
603 }
604 }
605 BIO_printf(bio_err, FORMAT, fp->name);
606 }
607 BIO_printf(bio_err, "\n\n");
608 return 0;
609}
8c00f4cf 610
7e1b7485
RS
611int exit_main(int argc, char **argv)
612{
613 return EXIT_THE_PROGRAM;
0f113f3e 614}
d02b48c6 615
2f58faad 616static void list_type(FUNC_TYPE ft)
7e1b7485
RS
617{
618 FUNCTION *fp;
619 int i = 0;
620
621 for (fp = functions; fp->name != NULL; fp++)
2f58faad 622 if (fp->type == ft) {
7e1b7485
RS
623 if ((i++ % COLUMNS) == 0)
624 BIO_printf(bio_out, "\n");
625 BIO_printf(bio_out, FORMAT, fp->name);
626 }
627 BIO_printf(bio_out, "\n");
7e1b7485 628}
fc8ee06b 629
3c1d6bbc 630static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
0f113f3e
MC
631{
632 FUNCTION f, *fp;
0f113f3e 633
7e1b7485
RS
634 if (argc <= 0 || argv[0] == NULL)
635 return (0);
0f113f3e
MC
636 f.name = argv[0];
637 fp = lh_FUNCTION_retrieve(prog, &f);
638 if (fp == NULL) {
639 if (EVP_get_digestbyname(argv[0])) {
7e1b7485 640 f.type = FT_md;
0f113f3e
MC
641 f.func = dgst_main;
642 fp = &f;
643 } else if (EVP_get_cipherbyname(argv[0])) {
7e1b7485 644 f.type = FT_cipher;
0f113f3e
MC
645 f.func = enc_main;
646 fp = &f;
647 }
648 }
649 if (fp != NULL) {
7e1b7485
RS
650 return (fp->func(argc, argv));
651 }
652 if ((strncmp(argv[0], "no-", 3)) == 0) {
653 /*
654 * User is asking if foo is unsupported, by trying to "run" the
655 * no-foo command. Strange.
656 */
0f113f3e 657 f.name = argv[0] + 3;
7e1b7485
RS
658 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
659 BIO_printf(bio_out, "%s\n", argv[0]);
660 return (0);
0f113f3e 661 }
7e1b7485
RS
662 BIO_printf(bio_out, "%s\n", argv[0] + 3);
663 return 1;
50acf46b 664 }
7e1b7485
RS
665 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
666 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
667 /* Special value to mean "exit the program. */
668 return EXIT_THE_PROGRAM;
0f113f3e 669
7e1b7485
RS
670 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
671 argv[0]);
672 return (1);
0f113f3e 673}
50acf46b 674
2f58faad 675static void list_pkey(void)
0f113f3e
MC
676{
677 int i;
7e1b7485 678
0f113f3e
MC
679 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
680 const EVP_PKEY_ASN1_METHOD *ameth;
681 int pkey_id, pkey_base_id, pkey_flags;
682 const char *pinfo, *pem_str;
683 ameth = EVP_PKEY_asn1_get0(i);
684 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
685 &pinfo, &pem_str, ameth);
686 if (pkey_flags & ASN1_PKEY_ALIAS) {
7e1b7485
RS
687 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
688 BIO_printf(bio_out, "\tAlias for: %s\n",
0f113f3e
MC
689 OBJ_nid2ln(pkey_base_id));
690 } else {
7e1b7485
RS
691 BIO_printf(bio_out, "Name: %s\n", pinfo);
692 BIO_printf(bio_out, "\tType: %s Algorithm\n",
0f113f3e
MC
693 pkey_flags & ASN1_PKEY_DYNAMIC ?
694 "External" : "Builtin");
7e1b7485 695 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
0f113f3e
MC
696 if (pem_str == NULL)
697 pem_str = "(none)";
7e1b7485 698 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
0f113f3e
MC
699 }
700
701 }
0f113f3e 702}
3c1d6bbc 703
0f113f3e
MC
704static int function_cmp(const FUNCTION * a, const FUNCTION * b)
705{
706 return strncmp(a->name, b->name, 8);
707}
50acf46b 708
0f113f3e 709static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
d02b48c6 710
0f113f3e
MC
711static unsigned long function_hash(const FUNCTION * a)
712{
713 return lh_strhash(a->name);
714}
d02b48c6 715
0f113f3e 716static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
d02b48c6 717
7e1b7485
RS
718static int SortFnByName(const void *_f1, const void *_f2)
719{
720 const FUNCTION *f1 = _f1;
721 const FUNCTION *f2 = _f2;
722
723 if (f1->type != f2->type)
724 return f1->type - f2->type;
725 return strcmp(f1->name, f2->name);
726}
727
a760a380
DSH
728static void list_disabled(void)
729{
d230bd1d 730 BIO_puts(bio_out, "Disabled algorithms:\n");
27dae1b0
RL
731#ifdef OPENSSL_NO_AES
732 BIO_puts(bio_out, "AES\n");
733#endif
734#ifdef OPENSSL_NO_BF
735 BIO_puts(bio_out, "BF\n");
736#endif
737#ifdef OPENSSL_NO_CAMELLIA
738 BIO_puts(bio_out, "CAMELLIA\n");
739#endif
740#ifdef OPENSSL_NO_CAST
741 BIO_puts(bio_out, "CAST\n");
742#endif
56c1ef05
RL
743#ifdef OPENSSL_NO_CMAC
744 BIO_puts(bio_out, "CMAC\n");
745#endif
27dae1b0
RL
746#ifdef OPENSSL_NO_CMS
747 BIO_puts(bio_out, "CMS\n");
748#endif
66b14bab
RL
749#ifdef OPENSSL_NO_COMP
750 BIO_puts(bio_out, "COMP\n");
751#endif
27dae1b0
RL
752#ifdef OPENSSL_NO_DES
753 BIO_puts(bio_out, "DES\n");
754#endif
2df84dd3
RL
755#ifdef OPENSSL_NO_DGRAM
756 BIO_puts(bio_out, "DGRAM\n");
757#endif
a760a380
DSH
758#ifdef OPENSSL_NO_DH
759 BIO_puts(bio_out, "DH\n");
760#endif
761#ifdef OPENSSL_NO_DSA
762 BIO_puts(bio_out, "DSA\n");
763#endif
a5ecdc6a
KR
764#if defined(OPENSSL_NO_DTLS)
765 BIO_puts(bio_out, "DTLS\n");
66b14bab 766#endif
a760a380
DSH
767#ifdef OPENSSL_NO_EC
768 BIO_puts(bio_out, "EC\n");
769#endif
770#ifdef OPENSSL_NO_EC2M
771 BIO_puts(bio_out, "EC2M\n");
772#endif
27dae1b0
RL
773#ifdef OPENSSL_NO_ENGINE
774 BIO_puts(bio_out, "ENGINE\n");
775#endif
2df84dd3
RL
776#ifdef OPENSSL_NO_GOST
777 BIO_puts(bio_out, "GOST\n");
778#endif
56c1ef05
RL
779#ifdef OPENSSL_NO_HMAC
780 BIO_puts(bio_out, "HMAC\n");
781#endif
27dae1b0
RL
782#ifdef OPENSSL_NO_IDEA
783 BIO_puts(bio_out, "IDEA\n");
784#endif
66b14bab
RL
785#ifdef OPENSSL_NO_JPAKE
786 BIO_puts(bio_out, "JPAKE\n");
787#endif
27dae1b0
RL
788#ifdef OPENSSL_NO_MD2
789 BIO_puts(bio_out, "MD2\n");
790#endif
791#ifdef OPENSSL_NO_MD4
792 BIO_puts(bio_out, "MD4\n");
793#endif
794#ifdef OPENSSL_NO_MD5
795 BIO_puts(bio_out, "MD5\n");
796#endif
797#ifdef OPENSSL_NO_MDC2
798 BIO_puts(bio_out, "MDC2\n");
799#endif
2df84dd3
RL
800#ifdef OPENSSL_NO_OCB
801 BIO_puts(bio_out, "OCB\n");
802#endif
27dae1b0
RL
803#ifdef OPENSSL_NO_OCSP
804 BIO_puts(bio_out, "OCSP\n");
805#endif
a760a380
DSH
806#ifdef OPENSSL_NO_PSK
807 BIO_puts(bio_out, "PSK\n");
808#endif
27dae1b0
RL
809#ifdef OPENSSL_NO_RC2
810 BIO_puts(bio_out, "RC2\n");
811#endif
812#ifdef OPENSSL_NO_RC4
813 BIO_puts(bio_out, "RC4\n");
814#endif
815#ifdef OPENSSL_NO_RC5
816 BIO_puts(bio_out, "RC5\n");
817#endif
818#ifdef OPENSSL_NO_RMD160
819 BIO_puts(bio_out, "RMD160\n");
820#endif
d230bd1d
RL
821#ifdef OPENSSL_NO_RSA
822 BIO_puts(bio_out, "RSA\n");
823#endif
66b14bab
RL
824#ifdef OPENSSL_NO_SCRYPT
825 BIO_puts(bio_out, "SCRYPT\n");
826#endif
2df84dd3
RL
827#ifdef OPENSSL_NO_SCT
828 BIO_puts(bio_out, "SCT\n");
829#endif
830#ifdef OPENSSL_NO_SCTP
831 BIO_puts(bio_out, "SCTP\n");
832#endif
27dae1b0
RL
833#ifdef OPENSSL_NO_SEED
834 BIO_puts(bio_out, "SEED\n");
835#endif
2df84dd3
RL
836#ifdef OPENSSL_NO_SHA
837 BIO_puts(bio_out, "SHA\n");
838#endif
27dae1b0
RL
839#ifdef OPENSSL_NO_SOCK
840 BIO_puts(bio_out, "SOCK\n");
841#endif
a760a380
DSH
842#ifdef OPENSSL_NO_SRP
843 BIO_puts(bio_out, "SRP\n");
844#endif
66b14bab
RL
845#ifdef OPENSSL_NO_SRTP
846 BIO_puts(bio_out, "SRTP\n");
847#endif
848#ifdef OPENSSL_NO_SSL3
849 BIO_puts(bio_out, "SSL3\n");
850#endif
27dae1b0
RL
851#ifdef OPENSSL_NO_WHIRLPOOL
852 BIO_puts(bio_out, "WHIRLPOOL\n");
853#endif
d230bd1d
RL
854#ifndef ZLIB
855 BIO_puts(bio_out, "ZLIB\n");
856#endif
a760a380
DSH
857}
858
0f113f3e
MC
859static LHASH_OF(FUNCTION) *prog_init(void)
860{
861 LHASH_OF(FUNCTION) *ret;
862 FUNCTION *f;
863 size_t i;
864
7e1b7485 865 /* Sort alphabetically within category. For nicer help displays. */
0f113f3e 866 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
b4faea50 867 qsort(functions, i, sizeof(*functions), SortFnByName);
0f113f3e
MC
868
869 if ((ret = lh_FUNCTION_new()) == NULL)
870 return (NULL);
871
872 for (f = functions; f->name != NULL; f++)
873 (void)lh_FUNCTION_insert(ret, f);
874 return (ret);
875}