]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/openssl.c
Rename INIT funtions, deprecate old ones.
[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 */
0f113f3e
MC
156static LHASH_OF(FUNCTION) *prog_init(void);
157static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
2f58faad
RS
158static void list_pkey(void);
159static void list_type(FUNC_TYPE ft);
a760a380 160static void list_disabled(void);
0f113f3e 161char *default_config_file = NULL;
d02b48c6 162
df2ee0e2 163static CONF *config = NULL;
7e1b7485
RS
164BIO *bio_in = NULL;
165BIO *bio_out = NULL;
0f113f3e 166BIO *bio_err = NULL;
7e1b7485 167
a0a82324 168static int apps_startup()
7e1b7485
RS
169{
170#ifdef SIGPIPE
171 signal(SIGPIPE, SIG_IGN);
172#endif
a0a82324 173
b9f75707 174 /* Set non-default library initialisation settings */
f672aee4
RS
175 OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
176 | OPENSSL_INIT_LOAD_CONFIG, NULL);
a0a82324 177
7e1b7485 178 setup_ui_method();
b9f75707 179
a0a82324 180 return 1;
7e1b7485
RS
181}
182
183static void apps_shutdown()
184{
7e1b7485 185 destroy_ui_method();
7e1b7485
RS
186}
187
188static char *make_config_name()
189{
cc01d217 190 const char *t;
7e1b7485
RS
191 size_t len;
192 char *p;
193
b0700d2c 194 if ((t = getenv("OPENSSL_CONF")) != NULL)
7644a9ae 195 return OPENSSL_strdup(t);
cc01d217
RS
196
197 t = X509_get_default_cert_area();
198 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
68dc6824 199 p = app_malloc(len, "config filename buffer");
cc01d217 200 strcpy(p, t);
7e1b7485 201#ifndef OPENSSL_SYS_VMS
cc01d217 202 strcat(p, "/");
d02b48c6 203#endif
cc01d217 204 strcat(p, OPENSSL_CONF);
7e1b7485
RS
205
206 return p;
207}
208
3ac82faa 209static void lock_dbg_cb(int mode, int type, const char *file, int line)
0f113f3e 210{
7e1b7485 211 static int modes[CRYPTO_NUM_LOCKS];
0f113f3e 212 const char *errstr = NULL;
7e1b7485 213 int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
0f113f3e 214
7e1b7485 215 if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
0f113f3e
MC
216 errstr = "invalid mode";
217 goto err;
218 }
219
220 if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
221 errstr = "type out of bounds";
222 goto err;
223 }
224
225 if (mode & CRYPTO_LOCK) {
226 if (modes[type]) {
227 errstr = "already locked";
7e1b7485 228 /* must not happen in a single-threaded program --> deadlock! */
0f113f3e
MC
229 goto err;
230 }
0f113f3e
MC
231 modes[type] = rw;
232 } else if (mode & CRYPTO_UNLOCK) {
233 if (!modes[type]) {
234 errstr = "not locked";
235 goto err;
236 }
237
238 if (modes[type] != rw) {
239 errstr = (rw == CRYPTO_READ) ?
240 "CRYPTO_r_unlock on write lock" :
241 "CRYPTO_w_unlock on read lock";
242 }
243
244 modes[type] = 0;
245 } else {
246 errstr = "invalid mode";
247 goto err;
248 }
3ac82faa
BM
249
250 err:
0f113f3e 251 if (errstr) {
7768e116
RS
252 BIO_printf(bio_err,
253 "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
254 errstr, mode, type, file, line);
0f113f3e
MC
255 }
256}
3ac82faa 257
7e1b7485
RS
258#if defined( OPENSSL_SYS_VMS)
259extern char **copy_argv(int *argc, char **argv);
260#endif
261
262int main(int argc, char *argv[])
0f113f3e 263{
0f113f3e 264 FUNCTION f, *fp;
0f113f3e 265 LHASH_OF(FUNCTION) *prog = NULL;
7e1b7485 266 char **copied_argv = NULL;
cc01d217 267 char *p, *pname;
7e1b7485
RS
268 char buf[1024];
269 const char *prompt;
270 ARGS arg;
271 int first, n, i, ret = 0;
8ecef24a 272
7e1b7485
RS
273 arg.argv = NULL;
274 arg.size = 0;
275
7768e116
RS
276 /* Set up some of the environment. */
277 default_config_file = make_config_name();
a60994df
RL
278 bio_in = dup_bio_in(FORMAT_TEXT);
279 bio_out = dup_bio_out(FORMAT_TEXT);
7768e116
RS
280 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
281
7e1b7485
RS
282#if defined( OPENSSL_SYS_VMS)
283 copied_argv = argv = copy_argv(&argc, argv);
284#endif
285
286 p = getenv("OPENSSL_DEBUG_MEMORY");
bbd86bf5
RS
287 if (p != NULL && strcmp(p, "on") == 0)
288 CRYPTO_set_mem_debug(1);
0f113f3e 289 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
75d0ebef 290 CRYPTO_set_locking_callback(lock_dbg_cb);
3ac82faa 291
0f113f3e 292 if (getenv("OPENSSL_FIPS")) {
856650de 293#ifdef OPENSSL_FIPS
0f113f3e 294 if (!FIPS_mode_set(1)) {
7768e116
RS
295 ERR_print_errors(bio_err);
296 return 1;
0f113f3e 297 }
856650de 298#else
7768e116
RS
299 BIO_printf(bio_err, "FIPS mode not supported.\n");
300 return 1;
856650de 301#endif
0f113f3e
MC
302 }
303
a0a82324
MC
304 if (!apps_startup())
305 goto end;
306
7e1b7485
RS
307 prog = prog_init();
308 pname = opt_progname(argv[0]);
0f113f3e 309
0f113f3e 310 /* first check the program name */
0f113f3e
MC
311 f.name = pname;
312 fp = lh_FUNCTION_retrieve(prog, &f);
313 if (fp != NULL) {
7e1b7485
RS
314 argv[0] = pname;
315 ret = fp->func(argc, argv);
0f113f3e
MC
316 goto end;
317 }
318
7e1b7485
RS
319 /* If there is stuff on the command line, run with that. */
320 if (argc != 1) {
321 argc--;
322 argv++;
323 ret = do_cmd(prog, argc, argv);
0f113f3e
MC
324 if (ret < 0)
325 ret = 0;
326 goto end;
327 }
328
7e1b7485 329 /* ok, lets enter interactive mode */
0f113f3e
MC
330 for (;;) {
331 ret = 0;
57d5edad
RS
332 /* Read a line, continue reading if line ends with \ */
333 for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
1c9c2435 334 prompt = first ? "OpenSSL> " : "> ";
0f113f3e 335 p[0] = '\0';
57d5edad 336#ifndef READLINE
0f113f3e
MC
337 fputs(prompt, stdout);
338 fflush(stdout);
339 if (!fgets(p, n, stdin))
340 goto end;
341 if (p[0] == '\0')
342 goto end;
343 i = strlen(p);
344 if (i <= 1)
345 break;
346 if (p[i - 2] != '\\')
347 break;
348 i -= 2;
349 p += i;
350 n -= i;
57d5edad
RS
351#else
352 {
353 extern char *readline(const char *);
354 extern void add_history(const char *cp);
355 char *text;
356
357 char *text = readline(prompt);
358 if (text == NULL)
359 goto end;
360 i = strlen(text);
361 if (i == 0 || i > n)
362 break;
363 if (text[i - 1] != '\\') {
364 p += strlen(strcpy(p, text));
365 free(text);
366 add_history(buf);
367 break;
368 }
369
370 text[i - 1] = '\0';
371 p += strlen(strcpy(p, text));
372 free(text);
373 n -= i;
374 }
375#endif
0f113f3e 376 }
57d5edad 377
7e1b7485
RS
378 if (!chopup_args(&arg, buf)) {
379 BIO_printf(bio_err, "Can't parse (no memory?)\n");
0f113f3e 380 break;
7e1b7485 381 }
0f113f3e 382
7e1b7485
RS
383 ret = do_cmd(prog, arg.argc, arg.argv);
384 if (ret == EXIT_THE_PROGRAM) {
0f113f3e
MC
385 ret = 0;
386 goto end;
387 }
388 if (ret != 0)
7e1b7485
RS
389 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
390 (void)BIO_flush(bio_out);
0f113f3e
MC
391 (void)BIO_flush(bio_err);
392 }
0f113f3e
MC
393 ret = 1;
394 end:
b548a1f1 395 OPENSSL_free(copied_argv);
cc01d217 396 OPENSSL_free(default_config_file);
25aaa98a
RS
397 NCONF_free(config);
398 config = NULL;
399 lh_FUNCTION_free(prog);
b548a1f1 400 OPENSSL_free(arg.argv);
0f113f3e 401
7e1b7485
RS
402 BIO_free(bio_in);
403 BIO_free_all(bio_out);
0f113f3e 404 apps_shutdown();
c2e27310 405#ifndef OPENSSL_NO_CRYPTO_MDEBUG
541e9565
DSH
406 if (CRYPTO_mem_leaks(bio_err) <= 0)
407 ret = 1;
bbd86bf5 408#endif
ca3a82c3 409 BIO_free(bio_err);
aa147792 410 EXIT(ret);
7e1b7485
RS
411}
412
413OPTIONS exit_options[] = {
414 {NULL}
415};
416
2f58faad
RS
417static void list_cipher_fn(const EVP_CIPHER *c,
418 const char *from, const char *to, void *arg)
419{
420 if (c)
421 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
422 else {
423 if (!from)
424 from = "<undefined>";
425 if (!to)
426 to = "<undefined>";
427 BIO_printf(arg, "%s => %s\n", from, to);
428 }
429}
430
431static void list_md_fn(const EVP_MD *m,
432 const char *from, const char *to, void *arg)
433{
434 if (m)
435 BIO_printf(arg, "%s\n", EVP_MD_name(m));
436 else {
437 if (!from)
438 from = "<undefined>";
439 if (!to)
440 to = "<undefined>";
441 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
442 }
443}
444
7e1b7485
RS
445/* Unified enum for help and list commands. */
446typedef enum HELPLIST_CHOICE {
447 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
448 OPT_COMMANDS, OPT_DIGEST_COMMANDS,
449 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
a760a380 450 OPT_PK_ALGORITHMS, OPT_DISABLED
7e1b7485
RS
451} HELPLIST_CHOICE;
452
453OPTIONS list_options[] = {
454 {"help", OPT_HELP, '-', "Display this summary"},
455 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
456 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
457 "List of message digest commands"},
458 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
459 "List of message digest algorithms"},
460 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
461 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
462 "List of cipher algorithms"},
463 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
464 "List of public key algorithms"},
a760a380
DSH
465 {"disabled", OPT_DISABLED, '-',
466 "List of disabled features"},
7e1b7485
RS
467 {NULL}
468};
469
470int list_main(int argc, char **argv)
471{
472 char *prog;
473 HELPLIST_CHOICE o;
19948cea 474 int done = 0;
7e1b7485
RS
475
476 prog = opt_init(argc, argv, list_options);
477 while ((o = opt_next()) != OPT_EOF) {
478 switch (o) {
19948cea 479 case OPT_EOF: /* Never hit, but suppresses warning */
7e1b7485
RS
480 case OPT_ERR:
481 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
482 return 1;
483 case OPT_HELP:
484 opt_help(list_options);
485 break;
486 case OPT_COMMANDS:
2f58faad
RS
487 list_type(FT_general);
488 break;
7e1b7485 489 case OPT_DIGEST_COMMANDS:
2f58faad
RS
490 list_type(FT_md);
491 break;
7e1b7485 492 case OPT_DIGEST_ALGORITHMS:
2f58faad
RS
493 EVP_MD_do_all_sorted(list_md_fn, bio_out);
494 break;
7e1b7485 495 case OPT_CIPHER_COMMANDS:
2f58faad
RS
496 list_type(FT_cipher);
497 break;
7e1b7485 498 case OPT_CIPHER_ALGORITHMS:
2f58faad
RS
499 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
500 break;
7e1b7485 501 case OPT_PK_ALGORITHMS:
2f58faad
RS
502 list_pkey();
503 break;
a760a380
DSH
504 case OPT_DISABLED:
505 list_disabled();
506 break;
7e1b7485 507 }
19948cea
BL
508 done = 1;
509 }
510
511 if (!done) {
512 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
513 return 1;
7e1b7485
RS
514 }
515
516 return 0;
517}
518
519OPTIONS help_options[] = {
520 {"help", OPT_HELP, '-', "Display this summary"},
521 {NULL}
522};
523
524int help_main(int argc, char **argv)
525{
526 FUNCTION *fp;
527 int i, nl;
528 FUNC_TYPE tp;
529 char *prog;
530 HELPLIST_CHOICE o;
531
532 prog = opt_init(argc, argv, help_options);
533 while ((o = opt_next()) != OPT_EOF) {
534 switch (o) {
535 default:
536 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
537 return 1;
538 case OPT_HELP:
539 opt_help(help_options);
540 return 0;
541 }
542 }
543 argc = opt_num_rest();
544 argv = opt_rest();
545
546 if (argc != 0) {
547 BIO_printf(bio_err, "Usage: %s\n", prog);
548 return 1;
549 }
550
551 BIO_printf(bio_err, "\nStandard commands");
552 i = 0;
553 tp = FT_none;
554 for (fp = functions; fp->name != NULL; fp++) {
555 nl = 0;
556 if (((i++) % COLUMNS) == 0) {
557 BIO_printf(bio_err, "\n");
558 nl = 1;
559 }
560 if (fp->type != tp) {
561 tp = fp->type;
562 if (!nl)
563 BIO_printf(bio_err, "\n");
564 if (tp == FT_md) {
565 i = 1;
566 BIO_printf(bio_err,
567 "\nMessage Digest commands (see the `dgst' command for more details)\n");
568 } else if (tp == FT_cipher) {
569 i = 1;
570 BIO_printf(bio_err,
571 "\nCipher commands (see the `enc' command for more details)\n");
572 }
573 }
574 BIO_printf(bio_err, FORMAT, fp->name);
575 }
576 BIO_printf(bio_err, "\n\n");
577 return 0;
578}
8c00f4cf 579
7e1b7485
RS
580int exit_main(int argc, char **argv)
581{
582 return EXIT_THE_PROGRAM;
0f113f3e 583}
d02b48c6 584
2f58faad 585static void list_type(FUNC_TYPE ft)
7e1b7485
RS
586{
587 FUNCTION *fp;
588 int i = 0;
589
590 for (fp = functions; fp->name != NULL; fp++)
2f58faad 591 if (fp->type == ft) {
7e1b7485
RS
592 if ((i++ % COLUMNS) == 0)
593 BIO_printf(bio_out, "\n");
594 BIO_printf(bio_out, FORMAT, fp->name);
595 }
596 BIO_printf(bio_out, "\n");
7e1b7485 597}
fc8ee06b 598
3c1d6bbc 599static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
0f113f3e
MC
600{
601 FUNCTION f, *fp;
0f113f3e 602
7e1b7485
RS
603 if (argc <= 0 || argv[0] == NULL)
604 return (0);
0f113f3e
MC
605 f.name = argv[0];
606 fp = lh_FUNCTION_retrieve(prog, &f);
607 if (fp == NULL) {
608 if (EVP_get_digestbyname(argv[0])) {
7e1b7485 609 f.type = FT_md;
0f113f3e
MC
610 f.func = dgst_main;
611 fp = &f;
612 } else if (EVP_get_cipherbyname(argv[0])) {
7e1b7485 613 f.type = FT_cipher;
0f113f3e
MC
614 f.func = enc_main;
615 fp = &f;
616 }
617 }
618 if (fp != NULL) {
7e1b7485
RS
619 return (fp->func(argc, argv));
620 }
621 if ((strncmp(argv[0], "no-", 3)) == 0) {
622 /*
623 * User is asking if foo is unsupported, by trying to "run" the
624 * no-foo command. Strange.
625 */
0f113f3e 626 f.name = argv[0] + 3;
7e1b7485
RS
627 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
628 BIO_printf(bio_out, "%s\n", argv[0]);
629 return (0);
0f113f3e 630 }
7e1b7485
RS
631 BIO_printf(bio_out, "%s\n", argv[0] + 3);
632 return 1;
50acf46b 633 }
7e1b7485
RS
634 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
635 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
636 /* Special value to mean "exit the program. */
637 return EXIT_THE_PROGRAM;
0f113f3e 638
7e1b7485
RS
639 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
640 argv[0]);
641 return (1);
0f113f3e 642}
50acf46b 643
2f58faad 644static void list_pkey(void)
0f113f3e
MC
645{
646 int i;
7e1b7485 647
0f113f3e
MC
648 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
649 const EVP_PKEY_ASN1_METHOD *ameth;
650 int pkey_id, pkey_base_id, pkey_flags;
651 const char *pinfo, *pem_str;
652 ameth = EVP_PKEY_asn1_get0(i);
653 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
654 &pinfo, &pem_str, ameth);
655 if (pkey_flags & ASN1_PKEY_ALIAS) {
7e1b7485
RS
656 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
657 BIO_printf(bio_out, "\tAlias for: %s\n",
0f113f3e
MC
658 OBJ_nid2ln(pkey_base_id));
659 } else {
7e1b7485
RS
660 BIO_printf(bio_out, "Name: %s\n", pinfo);
661 BIO_printf(bio_out, "\tType: %s Algorithm\n",
0f113f3e
MC
662 pkey_flags & ASN1_PKEY_DYNAMIC ?
663 "External" : "Builtin");
7e1b7485 664 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
0f113f3e
MC
665 if (pem_str == NULL)
666 pem_str = "(none)";
7e1b7485 667 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
0f113f3e
MC
668 }
669
670 }
0f113f3e 671}
3c1d6bbc 672
0f113f3e
MC
673static int function_cmp(const FUNCTION * a, const FUNCTION * b)
674{
675 return strncmp(a->name, b->name, 8);
676}
50acf46b 677
0f113f3e
MC
678static unsigned long function_hash(const FUNCTION * a)
679{
680 return lh_strhash(a->name);
681}
d02b48c6 682
7e1b7485
RS
683static int SortFnByName(const void *_f1, const void *_f2)
684{
685 const FUNCTION *f1 = _f1;
686 const FUNCTION *f2 = _f2;
687
688 if (f1->type != f2->type)
689 return f1->type - f2->type;
690 return strcmp(f1->name, f2->name);
691}
692
a760a380
DSH
693static void list_disabled(void)
694{
d230bd1d 695 BIO_puts(bio_out, "Disabled algorithms:\n");
27dae1b0
RL
696#ifdef OPENSSL_NO_AES
697 BIO_puts(bio_out, "AES\n");
698#endif
699#ifdef OPENSSL_NO_BF
700 BIO_puts(bio_out, "BF\n");
701#endif
702#ifdef OPENSSL_NO_CAMELLIA
703 BIO_puts(bio_out, "CAMELLIA\n");
704#endif
705#ifdef OPENSSL_NO_CAST
706 BIO_puts(bio_out, "CAST\n");
707#endif
56c1ef05
RL
708#ifdef OPENSSL_NO_CMAC
709 BIO_puts(bio_out, "CMAC\n");
710#endif
27dae1b0
RL
711#ifdef OPENSSL_NO_CMS
712 BIO_puts(bio_out, "CMS\n");
713#endif
66b14bab
RL
714#ifdef OPENSSL_NO_COMP
715 BIO_puts(bio_out, "COMP\n");
716#endif
27dae1b0
RL
717#ifdef OPENSSL_NO_DES
718 BIO_puts(bio_out, "DES\n");
719#endif
2df84dd3
RL
720#ifdef OPENSSL_NO_DGRAM
721 BIO_puts(bio_out, "DGRAM\n");
722#endif
a760a380
DSH
723#ifdef OPENSSL_NO_DH
724 BIO_puts(bio_out, "DH\n");
725#endif
726#ifdef OPENSSL_NO_DSA
727 BIO_puts(bio_out, "DSA\n");
728#endif
a5ecdc6a
KR
729#if defined(OPENSSL_NO_DTLS)
730 BIO_puts(bio_out, "DTLS\n");
66b14bab 731#endif
6b01bed2
VD
732#if defined(OPENSSL_NO_DTLS1)
733 BIO_puts(bio_out, "DTLS1\n");
734#endif
735#if defined(OPENSSL_NO_DTLS1_2)
736 BIO_puts(bio_out, "DTLS1_2\n");
737#endif
a760a380
DSH
738#ifdef OPENSSL_NO_EC
739 BIO_puts(bio_out, "EC\n");
740#endif
741#ifdef OPENSSL_NO_EC2M
742 BIO_puts(bio_out, "EC2M\n");
743#endif
27dae1b0
RL
744#ifdef OPENSSL_NO_ENGINE
745 BIO_puts(bio_out, "ENGINE\n");
746#endif
2df84dd3
RL
747#ifdef OPENSSL_NO_GOST
748 BIO_puts(bio_out, "GOST\n");
749#endif
56c1ef05
RL
750#ifdef OPENSSL_NO_HMAC
751 BIO_puts(bio_out, "HMAC\n");
752#endif
27dae1b0
RL
753#ifdef OPENSSL_NO_IDEA
754 BIO_puts(bio_out, "IDEA\n");
755#endif
66b14bab
RL
756#ifdef OPENSSL_NO_JPAKE
757 BIO_puts(bio_out, "JPAKE\n");
758#endif
27dae1b0
RL
759#ifdef OPENSSL_NO_MD2
760 BIO_puts(bio_out, "MD2\n");
761#endif
762#ifdef OPENSSL_NO_MD4
763 BIO_puts(bio_out, "MD4\n");
764#endif
765#ifdef OPENSSL_NO_MD5
766 BIO_puts(bio_out, "MD5\n");
767#endif
768#ifdef OPENSSL_NO_MDC2
769 BIO_puts(bio_out, "MDC2\n");
770#endif
2df84dd3
RL
771#ifdef OPENSSL_NO_OCB
772 BIO_puts(bio_out, "OCB\n");
773#endif
27dae1b0
RL
774#ifdef OPENSSL_NO_OCSP
775 BIO_puts(bio_out, "OCSP\n");
776#endif
a760a380
DSH
777#ifdef OPENSSL_NO_PSK
778 BIO_puts(bio_out, "PSK\n");
779#endif
27dae1b0
RL
780#ifdef OPENSSL_NO_RC2
781 BIO_puts(bio_out, "RC2\n");
782#endif
783#ifdef OPENSSL_NO_RC4
784 BIO_puts(bio_out, "RC4\n");
785#endif
786#ifdef OPENSSL_NO_RC5
787 BIO_puts(bio_out, "RC5\n");
788#endif
789#ifdef OPENSSL_NO_RMD160
790 BIO_puts(bio_out, "RMD160\n");
791#endif
d230bd1d
RL
792#ifdef OPENSSL_NO_RSA
793 BIO_puts(bio_out, "RSA\n");
794#endif
66b14bab
RL
795#ifdef OPENSSL_NO_SCRYPT
796 BIO_puts(bio_out, "SCRYPT\n");
797#endif
2df84dd3
RL
798#ifdef OPENSSL_NO_SCT
799 BIO_puts(bio_out, "SCT\n");
800#endif
801#ifdef OPENSSL_NO_SCTP
802 BIO_puts(bio_out, "SCTP\n");
803#endif
27dae1b0
RL
804#ifdef OPENSSL_NO_SEED
805 BIO_puts(bio_out, "SEED\n");
806#endif
2df84dd3
RL
807#ifdef OPENSSL_NO_SHA
808 BIO_puts(bio_out, "SHA\n");
809#endif
27dae1b0
RL
810#ifdef OPENSSL_NO_SOCK
811 BIO_puts(bio_out, "SOCK\n");
812#endif
a760a380
DSH
813#ifdef OPENSSL_NO_SRP
814 BIO_puts(bio_out, "SRP\n");
815#endif
66b14bab
RL
816#ifdef OPENSSL_NO_SRTP
817 BIO_puts(bio_out, "SRTP\n");
818#endif
6b01bed2
VD
819#ifdef OPENSSL_NO_SSL
820 BIO_puts(bio_out, "SSL\n");
821#endif
66b14bab
RL
822#ifdef OPENSSL_NO_SSL3
823 BIO_puts(bio_out, "SSL3\n");
824#endif
6b01bed2
VD
825#if defined(OPENSSL_NO_TLS)
826 BIO_puts(bio_out, "TLS\n");
827#endif
828#ifdef OPENSSL_NO_TLS1
829 BIO_puts(bio_out, "TLS1\n");
830#endif
831#ifdef OPENSSL_NO_TLS1_1
832 BIO_puts(bio_out, "TLS1_1\n");
833#endif
834#ifdef OPENSSL_NO_TLS1_2
835 BIO_puts(bio_out, "TLS1_2\n");
836#endif
27dae1b0
RL
837#ifdef OPENSSL_NO_WHIRLPOOL
838 BIO_puts(bio_out, "WHIRLPOOL\n");
839#endif
d230bd1d
RL
840#ifndef ZLIB
841 BIO_puts(bio_out, "ZLIB\n");
842#endif
a760a380
DSH
843}
844
0f113f3e
MC
845static LHASH_OF(FUNCTION) *prog_init(void)
846{
847 LHASH_OF(FUNCTION) *ret;
848 FUNCTION *f;
849 size_t i;
850
7e1b7485 851 /* Sort alphabetically within category. For nicer help displays. */
0f113f3e 852 for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
b4faea50 853 qsort(functions, i, sizeof(*functions), SortFnByName);
0f113f3e 854
62d0577e 855 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
0f113f3e
MC
856 return (NULL);
857
858 for (f = functions; f->name != NULL; f++)
859 (void)lh_FUNCTION_insert(ret, f);
860 return (ret);
861}