]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/openssl.c
Following the license change, modify the boilerplates in apps/
[thirdparty/openssl.git] / apps / openssl.c
CommitLineData
846e33c7 1/*
3c7d0945 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3ac82faa 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
3ac82faa
BM
8 */
9
198c42f5 10#include <internal/cryptlib.h>
d02b48c6
RE
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
ec577822
BM
14#include <openssl/bio.h>
15#include <openssl/crypto.h>
16#include <openssl/lhash.h>
17#include <openssl/conf.h>
18#include <openssl/x509.h>
19#include <openssl/pem.h>
20#include <openssl/ssl.h>
0b13e9f0 21#ifndef OPENSSL_NO_ENGINE
0f113f3e 22# include <openssl/engine.h>
0b13e9f0 23#endif
ec577822 24#include <openssl/err.h>
3b061a00
RS
25#include "s_apps.h"
26/* Needed to get the other O_xxx flags. */
27#ifdef OPENSSL_SYS_VMS
28# include <unixio.h>
29#endif
7e1b7485 30#include "apps.h"
dab2cd68
RL
31#define INCLUDE_FUNCTION_TABLE
32#include "progs.h"
7e1b7485 33
296cbb57
P
34/* Structure to hold the number of columns to be displayed and the
35 * field width used to display them.
36 */
37typedef struct {
38 int columns;
39 int width;
40} DISPLAY_COLUMNS;
7e1b7485
RS
41
42/* Special sentinel to exit the program. */
43#define EXIT_THE_PROGRAM (-1)
d02b48c6 44
0f113f3e
MC
45/*
46 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
47 * the base prototypes (we cast each variable inside the function to the
48 * required type of "FUNCTION*"). This removes the necessity for
49 * macro-generated wrapper functions.
50 */
0f113f3e
MC
51static LHASH_OF(FUNCTION) *prog_init(void);
52static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
2f58faad 53static void list_pkey(void);
e1631f51 54static void list_pkey_meth(void);
e75138ab 55static void list_type(FUNC_TYPE ft, int one);
a760a380 56static void list_disabled(void);
0f113f3e 57char *default_config_file = NULL;
d02b48c6 58
7e1b7485
RS
59BIO *bio_in = NULL;
60BIO *bio_out = NULL;
0f113f3e 61BIO *bio_err = NULL;
7e1b7485 62
296cbb57
P
63static void calculate_columns(DISPLAY_COLUMNS *dc)
64{
65 FUNCTION *f;
66 int len, maxlen = 0;
67
68 for (f = functions; f->name != NULL; ++f)
69 if (f->type == FT_general || f->type == FT_md || f->type == FT_cipher)
70 if ((len = strlen(f->name)) > maxlen)
71 maxlen = len;
72
73 dc->width = maxlen + 2;
74 dc->columns = (80 - 1) / dc->width;
75}
76
3cb7c5cf 77static int apps_startup(void)
7e1b7485
RS
78{
79#ifdef SIGPIPE
80 signal(SIGPIPE, SIG_IGN);
81#endif
a0a82324 82
b9f75707 83 /* Set non-default library initialisation settings */
0488c0bb
RL
84 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
85 | OPENSSL_INIT_LOAD_CONFIG, NULL))
0fc32b07 86 return 0;
a0a82324 87
7e1b7485 88 setup_ui_method();
b9f75707 89
a0a82324 90 return 1;
7e1b7485
RS
91}
92
3cb7c5cf 93static void apps_shutdown(void)
7e1b7485 94{
7e1b7485 95 destroy_ui_method();
71bb86f0 96 destroy_prefix_method();
7e1b7485
RS
97}
98
3cb7c5cf 99static char *make_config_name(void)
7e1b7485 100{
cc01d217 101 const char *t;
7e1b7485
RS
102 size_t len;
103 char *p;
104
b0700d2c 105 if ((t = getenv("OPENSSL_CONF")) != NULL)
7644a9ae 106 return OPENSSL_strdup(t);
cc01d217
RS
107
108 t = X509_get_default_cert_area();
109 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
68dc6824 110 p = app_malloc(len, "config filename buffer");
cc01d217 111 strcpy(p, t);
7e1b7485 112#ifndef OPENSSL_SYS_VMS
cc01d217 113 strcat(p, "/");
d02b48c6 114#endif
cc01d217 115 strcat(p, OPENSSL_CONF);
7e1b7485
RS
116
117 return p;
118}
119
7e1b7485 120int main(int argc, char *argv[])
0f113f3e 121{
0f113f3e 122 FUNCTION f, *fp;
0f113f3e 123 LHASH_OF(FUNCTION) *prog = NULL;
7e1b7485 124 char **copied_argv = NULL;
cc01d217 125 char *p, *pname;
7e1b7485
RS
126 char buf[1024];
127 const char *prompt;
128 ARGS arg;
129 int first, n, i, ret = 0;
8ecef24a 130
7e1b7485
RS
131 arg.argv = NULL;
132 arg.size = 0;
133
7768e116
RS
134 /* Set up some of the environment. */
135 default_config_file = make_config_name();
a60994df
RL
136 bio_in = dup_bio_in(FORMAT_TEXT);
137 bio_out = dup_bio_out(FORMAT_TEXT);
149bd5d6 138 bio_err = dup_bio_err(FORMAT_TEXT);
7768e116 139
368058d0
RL
140#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
141 copied_argv = argv = copy_argv(&argc, argv);
4e155ec4
AP
142#elif defined(_WIN32)
143 /*
144 * Replace argv[] with UTF-8 encoded strings.
145 */
146 win32_utf8argv(&argc, &argv);
7e1b7485
RS
147#endif
148
149 p = getenv("OPENSSL_DEBUG_MEMORY");
bbd86bf5
RS
150 if (p != NULL && strcmp(p, "on") == 0)
151 CRYPTO_set_mem_debug(1);
0f113f3e 152 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
3ac82faa 153
0f113f3e 154 if (getenv("OPENSSL_FIPS")) {
7768e116
RS
155 BIO_printf(bio_err, "FIPS mode not supported.\n");
156 return 1;
0f113f3e
MC
157 }
158
f2da4a49
RL
159 if (!apps_startup()) {
160 BIO_printf(bio_err,
161 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
162 ERR_print_errors(bio_err);
163 ret = 1;
a0a82324 164 goto end;
f2da4a49 165 }
a0a82324 166
7e1b7485 167 prog = prog_init();
368058d0 168 pname = opt_progname(argv[0]);
0f113f3e 169
0f113f3e 170 /* first check the program name */
0f113f3e
MC
171 f.name = pname;
172 fp = lh_FUNCTION_retrieve(prog, &f);
173 if (fp != NULL) {
368058d0
RL
174 argv[0] = pname;
175 ret = fp->func(argc, argv);
0f113f3e
MC
176 goto end;
177 }
178
7e1b7485
RS
179 /* If there is stuff on the command line, run with that. */
180 if (argc != 1) {
181 argc--;
368058d0
RL
182 argv++;
183 ret = do_cmd(prog, argc, argv);
0f113f3e
MC
184 if (ret < 0)
185 ret = 0;
186 goto end;
187 }
188
7e1b7485 189 /* ok, lets enter interactive mode */
0f113f3e
MC
190 for (;;) {
191 ret = 0;
57d5edad 192 /* Read a line, continue reading if line ends with \ */
cbe29648 193 for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
1c9c2435 194 prompt = first ? "OpenSSL> " : "> ";
0f113f3e 195 p[0] = '\0';
57d5edad 196#ifndef READLINE
0f113f3e
MC
197 fputs(prompt, stdout);
198 fflush(stdout);
199 if (!fgets(p, n, stdin))
200 goto end;
201 if (p[0] == '\0')
202 goto end;
203 i = strlen(p);
204 if (i <= 1)
205 break;
206 if (p[i - 2] != '\\')
207 break;
208 i -= 2;
209 p += i;
210 n -= i;
57d5edad
RS
211#else
212 {
213 extern char *readline(const char *);
214 extern void add_history(const char *cp);
215 char *text;
216
72106aaa 217 text = readline(prompt);
57d5edad
RS
218 if (text == NULL)
219 goto end;
220 i = strlen(text);
221 if (i == 0 || i > n)
222 break;
223 if (text[i - 1] != '\\') {
224 p += strlen(strcpy(p, text));
225 free(text);
226 add_history(buf);
227 break;
228 }
229
230 text[i - 1] = '\0';
231 p += strlen(strcpy(p, text));
232 free(text);
233 n -= i;
234 }
235#endif
0f113f3e 236 }
57d5edad 237
7e1b7485
RS
238 if (!chopup_args(&arg, buf)) {
239 BIO_printf(bio_err, "Can't parse (no memory?)\n");
0f113f3e 240 break;
7e1b7485 241 }
0f113f3e 242
7e1b7485
RS
243 ret = do_cmd(prog, arg.argc, arg.argv);
244 if (ret == EXIT_THE_PROGRAM) {
0f113f3e
MC
245 ret = 0;
246 goto end;
247 }
248 if (ret != 0)
7e1b7485
RS
249 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
250 (void)BIO_flush(bio_out);
0f113f3e
MC
251 (void)BIO_flush(bio_err);
252 }
0f113f3e
MC
253 ret = 1;
254 end:
b548a1f1 255 OPENSSL_free(copied_argv);
cc01d217 256 OPENSSL_free(default_config_file);
25aaa98a 257 lh_FUNCTION_free(prog);
b548a1f1 258 OPENSSL_free(arg.argv);
3ee1eac2 259 app_RAND_write();
0f113f3e 260
7e1b7485
RS
261 BIO_free(bio_in);
262 BIO_free_all(bio_out);
0f113f3e 263 apps_shutdown();
c2e27310 264#ifndef OPENSSL_NO_CRYPTO_MDEBUG
541e9565
DSH
265 if (CRYPTO_mem_leaks(bio_err) <= 0)
266 ret = 1;
bbd86bf5 267#endif
ca3a82c3 268 BIO_free(bio_err);
aa147792 269 EXIT(ret);
7e1b7485
RS
270}
271
2f58faad
RS
272static void list_cipher_fn(const EVP_CIPHER *c,
273 const char *from, const char *to, void *arg)
274{
2234212c 275 if (c != NULL) {
2f58faad 276 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
2234212c
PY
277 } else {
278 if (from == NULL)
2f58faad 279 from = "<undefined>";
2234212c 280 if (to == NULL)
2f58faad
RS
281 to = "<undefined>";
282 BIO_printf(arg, "%s => %s\n", from, to);
283 }
284}
285
286static void list_md_fn(const EVP_MD *m,
287 const char *from, const char *to, void *arg)
288{
2234212c 289 if (m != NULL) {
2f58faad 290 BIO_printf(arg, "%s\n", EVP_MD_name(m));
2234212c
PY
291 } else {
292 if (from == NULL)
2f58faad 293 from = "<undefined>";
2234212c 294 if (to == NULL)
2f58faad
RS
295 to = "<undefined>";
296 BIO_printf((BIO *)arg, "%s => %s\n", from, to);
297 }
298}
299
0d1f7ae3
P
300static void list_mac_fn(const EVP_MAC *m,
301 const char *from, const char *to, void *arg)
302{
303 if (m != NULL) {
304 BIO_printf(arg, "%s\n", EVP_MAC_name(m));
305 } else {
306 if (from == NULL)
307 from = "<undefined>";
308 if (to == NULL)
309 to = "<undefined>";
310 BIO_printf(arg, "%s => %s\n", from, to);
311 }
312}
313
77297115
RS
314static void list_missing_help(void)
315{
316 const FUNCTION *fp;
317 const OPTIONS *o;
318
319 for (fp = functions; fp->name != NULL; fp++) {
e75138ab
RS
320 if ((o = fp->help) != NULL) {
321 /* If there is help, list what flags are not documented. */
322 for ( ; o->name != NULL; o++) {
323 if (o->helpstr == NULL)
324 BIO_printf(bio_out, "%s %s\n", fp->name, o->name);
325 }
326 } else if (fp->func != dgst_main) {
327 /* If not aliased to the dgst command, */
77297115 328 BIO_printf(bio_out, "%s *\n", fp->name);
77297115
RS
329 }
330 }
331}
332
8ddbff9c
RL
333static void list_objects(void)
334{
335 int max_nid = OBJ_new_nid(0);
336 int i;
337 char *oid_buf = NULL;
338 int oid_size = 0;
339
340 /* Skip 0, since that's NID_undef */
341 for (i = 1; i < max_nid; i++) {
342 const ASN1_OBJECT *obj = OBJ_nid2obj(i);
343 const char *sn = OBJ_nid2sn(i);
344 const char *ln = OBJ_nid2ln(i);
345 int n = 0;
346
347 /*
348 * If one of the retrieved objects somehow generated an error,
349 * we ignore it. The check for NID_undef below will detect the
350 * error and simply skip to the next NID.
351 */
352 ERR_clear_error();
353
354 if (OBJ_obj2nid(obj) == NID_undef)
355 continue;
356
357 if ((n = OBJ_obj2txt(NULL, 0, obj, 1)) == 0) {
358 BIO_printf(bio_out, "# None-OID object: %s, %s\n", sn, ln);
359 continue;
360 }
361 if (n < 0)
362 break; /* Error */
363
364 if (n > oid_size) {
365 oid_buf = OPENSSL_realloc(oid_buf, n + 1);
366 if (oid_buf == NULL) {
367 BIO_printf(bio_err, "ERROR: Memory allocation\n");
368 break; /* Error */
369 }
370 oid_size = n + 1;
371 }
372 if (OBJ_obj2txt(oid_buf, oid_size, obj, 1) < 0)
373 break; /* Error */
374 if (ln == NULL || strcmp(sn, ln) == 0)
375 BIO_printf(bio_out, "%s = %s\n", sn, oid_buf);
376 else
377 BIO_printf(bio_out, "%s = %s, %s\n", sn, ln, oid_buf);
378 }
379
380 OPENSSL_free(oid_buf);
381}
382
e75138ab
RS
383static void list_options_for_command(const char *command)
384{
385 const FUNCTION *fp;
386 const OPTIONS *o;
387
388 for (fp = functions; fp->name != NULL; fp++)
389 if (strcmp(fp->name, command) == 0)
390 break;
391 if (fp->name == NULL) {
392 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
393 command);
394 return;
395 }
396
397 if ((o = fp->help) == NULL)
398 return;
399
400 for ( ; o->name != NULL; o++) {
401 if (o->name == OPT_HELP_STR
402 || o->name == OPT_MORE_STR
403 || o->name[0] == '\0')
404 continue;
405 BIO_printf(bio_out, "%s %c\n", o->name, o->valtype);
406 }
407}
408
77297115 409
7e1b7485
RS
410/* Unified enum for help and list commands. */
411typedef enum HELPLIST_CHOICE {
e75138ab 412 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ONE,
0d1f7ae3 413 OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
7e1b7485 414 OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
8ddbff9c
RL
415 OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_DISABLED, OPT_MISSING_HELP,
416 OPT_OBJECTS
7e1b7485
RS
417} HELPLIST_CHOICE;
418
44c83ebd 419const OPTIONS list_options[] = {
7e1b7485 420 {"help", OPT_HELP, '-', "Display this summary"},
e75138ab 421 {"1", OPT_ONE, '-', "List in one column"},
7e1b7485
RS
422 {"commands", OPT_COMMANDS, '-', "List of standard commands"},
423 {"digest-commands", OPT_DIGEST_COMMANDS, '-',
424 "List of message digest commands"},
425 {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
426 "List of message digest algorithms"},
0d1f7ae3
P
427 {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
428 "List of message authentication code algorithms"},
7e1b7485
RS
429 {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
430 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
431 "List of cipher algorithms"},
432 {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
433 "List of public key algorithms"},
e1631f51
DSH
434 {"public-key-methods", OPT_PK_METHOD, '-',
435 "List of public key methods"},
a760a380
DSH
436 {"disabled", OPT_DISABLED, '-',
437 "List of disabled features"},
77297115
RS
438 {"missing-help", OPT_MISSING_HELP, '-',
439 "List missing detailed help strings"},
e75138ab
RS
440 {"options", OPT_OPTIONS, 's',
441 "List options for specified command"},
8ddbff9c
RL
442 {"objects", OPT_OBJECTS, '-',
443 "List built in objects (OID<->name mappings)"},
7e1b7485
RS
444 {NULL}
445};
446
447int list_main(int argc, char **argv)
448{
449 char *prog;
450 HELPLIST_CHOICE o;
e75138ab 451 int one = 0, done = 0;
7e1b7485
RS
452
453 prog = opt_init(argc, argv, list_options);
454 while ((o = opt_next()) != OPT_EOF) {
455 switch (o) {
19948cea 456 case OPT_EOF: /* Never hit, but suppresses warning */
7e1b7485 457 case OPT_ERR:
c27363f5 458opthelp:
7e1b7485
RS
459 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
460 return 1;
461 case OPT_HELP:
462 opt_help(list_options);
463 break;
e75138ab
RS
464 case OPT_ONE:
465 one = 1;
466 break;
7e1b7485 467 case OPT_COMMANDS:
e75138ab 468 list_type(FT_general, one);
2f58faad 469 break;
7e1b7485 470 case OPT_DIGEST_COMMANDS:
e75138ab 471 list_type(FT_md, one);
2f58faad 472 break;
7e1b7485 473 case OPT_DIGEST_ALGORITHMS:
2f58faad
RS
474 EVP_MD_do_all_sorted(list_md_fn, bio_out);
475 break;
0d1f7ae3
P
476 case OPT_MAC_ALGORITHMS:
477 EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
478 break;
7e1b7485 479 case OPT_CIPHER_COMMANDS:
e75138ab 480 list_type(FT_cipher, one);
2f58faad 481 break;
7e1b7485 482 case OPT_CIPHER_ALGORITHMS:
2f58faad
RS
483 EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
484 break;
7e1b7485 485 case OPT_PK_ALGORITHMS:
2f58faad
RS
486 list_pkey();
487 break;
e1631f51
DSH
488 case OPT_PK_METHOD:
489 list_pkey_meth();
490 break;
a760a380
DSH
491 case OPT_DISABLED:
492 list_disabled();
493 break;
77297115
RS
494 case OPT_MISSING_HELP:
495 list_missing_help();
496 break;
8ddbff9c
RL
497 case OPT_OBJECTS:
498 list_objects();
499 break;
e75138ab
RS
500 case OPT_OPTIONS:
501 list_options_for_command(opt_arg());
502 break;
7e1b7485 503 }
19948cea
BL
504 done = 1;
505 }
c27363f5
RS
506 if (opt_num_rest() != 0) {
507 BIO_printf(bio_err, "Extra arguments given.\n");
508 goto opthelp;
7e1b7485
RS
509 }
510
c27363f5
RS
511 if (!done)
512 goto opthelp;
513
7e1b7485
RS
514 return 0;
515}
516
f3b3d7f0
RS
517typedef enum HELP_CHOICE {
518 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
519} HELP_CHOICE;
520
44c83ebd 521const OPTIONS help_options[] = {
da6be198
RL
522 {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
523 {OPT_HELP_STR, 1, '-', " help [command]\n"},
f3b3d7f0 524 {"help", OPT_hHELP, '-', "Display this summary"},
7e1b7485
RS
525 {NULL}
526};
527
f3b3d7f0 528
7e1b7485
RS
529int help_main(int argc, char **argv)
530{
531 FUNCTION *fp;
532 int i, nl;
533 FUNC_TYPE tp;
534 char *prog;
f3b3d7f0 535 HELP_CHOICE o;
296cbb57 536 DISPLAY_COLUMNS dc;
7e1b7485
RS
537
538 prog = opt_init(argc, argv, help_options);
f3b3d7f0 539 while ((o = opt_next()) != OPT_hEOF) {
7e1b7485 540 switch (o) {
f3b3d7f0
RS
541 case OPT_hERR:
542 case OPT_hEOF:
7e1b7485
RS
543 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
544 return 1;
f3b3d7f0 545 case OPT_hHELP:
7e1b7485
RS
546 opt_help(help_options);
547 return 0;
548 }
549 }
7e1b7485 550
da6be198
RL
551 if (opt_num_rest() == 1) {
552 char *new_argv[3];
553
554 new_argv[0] = opt_rest()[0];
555 new_argv[1] = "--help";
556 new_argv[2] = NULL;
557 return do_cmd(prog_init(), 2, new_argv);
558 }
5d94e5b6 559 if (opt_num_rest() != 0) {
7e1b7485
RS
560 BIO_printf(bio_err, "Usage: %s\n", prog);
561 return 1;
562 }
563
296cbb57
P
564 calculate_columns(&dc);
565 BIO_printf(bio_err, "Standard commands");
7e1b7485
RS
566 i = 0;
567 tp = FT_none;
568 for (fp = functions; fp->name != NULL; fp++) {
569 nl = 0;
296cbb57 570 if (i++ % dc.columns == 0) {
7e1b7485
RS
571 BIO_printf(bio_err, "\n");
572 nl = 1;
573 }
574 if (fp->type != tp) {
575 tp = fp->type;
576 if (!nl)
577 BIO_printf(bio_err, "\n");
578 if (tp == FT_md) {
579 i = 1;
580 BIO_printf(bio_err,
581 "\nMessage Digest commands (see the `dgst' command for more details)\n");
582 } else if (tp == FT_cipher) {
583 i = 1;
584 BIO_printf(bio_err,
585 "\nCipher commands (see the `enc' command for more details)\n");
586 }
587 }
296cbb57 588 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
7e1b7485
RS
589 }
590 BIO_printf(bio_err, "\n\n");
591 return 0;
592}
8c00f4cf 593
e75138ab 594static void list_type(FUNC_TYPE ft, int one)
7e1b7485
RS
595{
596 FUNCTION *fp;
597 int i = 0;
d16a2c19 598 DISPLAY_COLUMNS dc = {0};
296cbb57
P
599
600 if (!one)
601 calculate_columns(&dc);
7e1b7485 602
e75138ab
RS
603 for (fp = functions; fp->name != NULL; fp++) {
604 if (fp->type != ft)
605 continue;
606 if (one) {
607 BIO_printf(bio_out, "%s\n", fp->name);
608 } else {
296cbb57 609 if (i % dc.columns == 0 && i > 0)
7e1b7485 610 BIO_printf(bio_out, "\n");
296cbb57
P
611 BIO_printf(bio_out, "%-*s", dc.width, fp->name);
612 i++;
7e1b7485 613 }
e75138ab
RS
614 }
615 if (!one)
296cbb57 616 BIO_printf(bio_out, "\n\n");
7e1b7485 617}
fc8ee06b 618
3c1d6bbc 619static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
0f113f3e
MC
620{
621 FUNCTION f, *fp;
0f113f3e 622
7e1b7485 623 if (argc <= 0 || argv[0] == NULL)
26a7d938 624 return 0;
0f113f3e
MC
625 f.name = argv[0];
626 fp = lh_FUNCTION_retrieve(prog, &f);
627 if (fp == NULL) {
628 if (EVP_get_digestbyname(argv[0])) {
7e1b7485 629 f.type = FT_md;
0f113f3e
MC
630 f.func = dgst_main;
631 fp = &f;
632 } else if (EVP_get_cipherbyname(argv[0])) {
7e1b7485 633 f.type = FT_cipher;
0f113f3e
MC
634 f.func = enc_main;
635 fp = &f;
636 }
637 }
638 if (fp != NULL) {
26a7d938 639 return fp->func(argc, argv);
7e1b7485
RS
640 }
641 if ((strncmp(argv[0], "no-", 3)) == 0) {
642 /*
643 * User is asking if foo is unsupported, by trying to "run" the
644 * no-foo command. Strange.
645 */
0f113f3e 646 f.name = argv[0] + 3;
7e1b7485
RS
647 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
648 BIO_printf(bio_out, "%s\n", argv[0]);
26a7d938 649 return 0;
0f113f3e 650 }
7e1b7485
RS
651 BIO_printf(bio_out, "%s\n", argv[0] + 3);
652 return 1;
50acf46b 653 }
7e1b7485
RS
654 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
655 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
656 /* Special value to mean "exit the program. */
657 return EXIT_THE_PROGRAM;
0f113f3e 658
7e1b7485
RS
659 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
660 argv[0]);
208fb891 661 return 1;
0f113f3e 662}
50acf46b 663
2f58faad 664static void list_pkey(void)
0f113f3e
MC
665{
666 int i;
7e1b7485 667
0f113f3e
MC
668 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
669 const EVP_PKEY_ASN1_METHOD *ameth;
670 int pkey_id, pkey_base_id, pkey_flags;
671 const char *pinfo, *pem_str;
672 ameth = EVP_PKEY_asn1_get0(i);
673 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
674 &pinfo, &pem_str, ameth);
675 if (pkey_flags & ASN1_PKEY_ALIAS) {
7e1b7485
RS
676 BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
677 BIO_printf(bio_out, "\tAlias for: %s\n",
0f113f3e
MC
678 OBJ_nid2ln(pkey_base_id));
679 } else {
7e1b7485
RS
680 BIO_printf(bio_out, "Name: %s\n", pinfo);
681 BIO_printf(bio_out, "\tType: %s Algorithm\n",
0f113f3e
MC
682 pkey_flags & ASN1_PKEY_DYNAMIC ?
683 "External" : "Builtin");
7e1b7485 684 BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
0f113f3e
MC
685 if (pem_str == NULL)
686 pem_str = "(none)";
7e1b7485 687 BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
0f113f3e
MC
688 }
689
690 }
0f113f3e 691}
3c1d6bbc 692
e1631f51
DSH
693static void list_pkey_meth(void)
694{
695 size_t i;
696 size_t meth_count = EVP_PKEY_meth_get_count();
697
698 for (i = 0; i < meth_count; i++) {
699 const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i);
700 int pkey_id, pkey_flags;
701
702 EVP_PKEY_meth_get0_info(&pkey_id, &pkey_flags, pmeth);
703 BIO_printf(bio_out, "%s\n", OBJ_nid2ln(pkey_id));
704 BIO_printf(bio_out, "\tType: %s Algorithm\n",
705 pkey_flags & ASN1_PKEY_DYNAMIC ? "External" : "Builtin");
706 }
707}
708
0f113f3e
MC
709static int function_cmp(const FUNCTION * a, const FUNCTION * b)
710{
711 return strncmp(a->name, b->name, 8);
712}
50acf46b 713
0f113f3e
MC
714static unsigned long function_hash(const FUNCTION * a)
715{
739a1eb1 716 return OPENSSL_LH_strhash(a->name);
0f113f3e 717}
d02b48c6 718
7e1b7485
RS
719static int SortFnByName(const void *_f1, const void *_f2)
720{
721 const FUNCTION *f1 = _f1;
722 const FUNCTION *f2 = _f2;
723
724 if (f1->type != f2->type)
725 return f1->type - f2->type;
726 return strcmp(f1->name, f2->name);
727}
728
a760a380
DSH
729static void list_disabled(void)
730{
d230bd1d 731 BIO_puts(bio_out, "Disabled algorithms:\n");
d42d0a4d
P
732#ifdef OPENSSL_NO_ARIA
733 BIO_puts(bio_out, "ARIA\n");
734#endif
27dae1b0
RL
735#ifdef OPENSSL_NO_BF
736 BIO_puts(bio_out, "BF\n");
737#endif
487d3a72 738#ifdef OPENSSL_NO_BLAKE2
2d0b4412
BC
739 BIO_puts(bio_out, "BLAKE2\n");
740#endif
27dae1b0
RL
741#ifdef OPENSSL_NO_CAMELLIA
742 BIO_puts(bio_out, "CAMELLIA\n");
743#endif
744#ifdef OPENSSL_NO_CAST
745 BIO_puts(bio_out, "CAST\n");
746#endif
56c1ef05
RL
747#ifdef OPENSSL_NO_CMAC
748 BIO_puts(bio_out, "CMAC\n");
749#endif
27dae1b0
RL
750#ifdef OPENSSL_NO_CMS
751 BIO_puts(bio_out, "CMS\n");
752#endif
66b14bab
RL
753#ifdef OPENSSL_NO_COMP
754 BIO_puts(bio_out, "COMP\n");
755#endif
27dae1b0
RL
756#ifdef OPENSSL_NO_DES
757 BIO_puts(bio_out, "DES\n");
758#endif
2df84dd3
RL
759#ifdef OPENSSL_NO_DGRAM
760 BIO_puts(bio_out, "DGRAM\n");
761#endif
a760a380
DSH
762#ifdef OPENSSL_NO_DH
763 BIO_puts(bio_out, "DH\n");
764#endif
765#ifdef OPENSSL_NO_DSA
766 BIO_puts(bio_out, "DSA\n");
767#endif
a5ecdc6a
KR
768#if defined(OPENSSL_NO_DTLS)
769 BIO_puts(bio_out, "DTLS\n");
66b14bab 770#endif
6b01bed2
VD
771#if defined(OPENSSL_NO_DTLS1)
772 BIO_puts(bio_out, "DTLS1\n");
773#endif
774#if defined(OPENSSL_NO_DTLS1_2)
775 BIO_puts(bio_out, "DTLS1_2\n");
776#endif
a760a380
DSH
777#ifdef OPENSSL_NO_EC
778 BIO_puts(bio_out, "EC\n");
779#endif
780#ifdef OPENSSL_NO_EC2M
781 BIO_puts(bio_out, "EC2M\n");
782#endif
27dae1b0
RL
783#ifdef OPENSSL_NO_ENGINE
784 BIO_puts(bio_out, "ENGINE\n");
785#endif
2df84dd3
RL
786#ifdef OPENSSL_NO_GOST
787 BIO_puts(bio_out, "GOST\n");
788#endif
b612799a
RL
789#ifdef OPENSSL_NO_HEARTBEATS
790 BIO_puts(bio_out, "HEARTBEATS\n");
791#endif
27dae1b0
RL
792#ifdef OPENSSL_NO_IDEA
793 BIO_puts(bio_out, "IDEA\n");
794#endif
795#ifdef OPENSSL_NO_MD2
796 BIO_puts(bio_out, "MD2\n");
797#endif
798#ifdef OPENSSL_NO_MD4
799 BIO_puts(bio_out, "MD4\n");
800#endif
801#ifdef OPENSSL_NO_MD5
802 BIO_puts(bio_out, "MD5\n");
803#endif
804#ifdef OPENSSL_NO_MDC2
805 BIO_puts(bio_out, "MDC2\n");
806#endif
2df84dd3
RL
807#ifdef OPENSSL_NO_OCB
808 BIO_puts(bio_out, "OCB\n");
809#endif
27dae1b0
RL
810#ifdef OPENSSL_NO_OCSP
811 BIO_puts(bio_out, "OCSP\n");
812#endif
a760a380
DSH
813#ifdef OPENSSL_NO_PSK
814 BIO_puts(bio_out, "PSK\n");
815#endif
27dae1b0
RL
816#ifdef OPENSSL_NO_RC2
817 BIO_puts(bio_out, "RC2\n");
818#endif
819#ifdef OPENSSL_NO_RC4
820 BIO_puts(bio_out, "RC4\n");
821#endif
822#ifdef OPENSSL_NO_RC5
823 BIO_puts(bio_out, "RC5\n");
824#endif
825#ifdef OPENSSL_NO_RMD160
826 BIO_puts(bio_out, "RMD160\n");
827#endif
d230bd1d
RL
828#ifdef OPENSSL_NO_RSA
829 BIO_puts(bio_out, "RSA\n");
830#endif
66b14bab
RL
831#ifdef OPENSSL_NO_SCRYPT
832 BIO_puts(bio_out, "SCRYPT\n");
833#endif
2df84dd3
RL
834#ifdef OPENSSL_NO_SCTP
835 BIO_puts(bio_out, "SCTP\n");
836#endif
27dae1b0
RL
837#ifdef OPENSSL_NO_SEED
838 BIO_puts(bio_out, "SEED\n");
839#endif
3d328a44
JL
840#ifdef OPENSSL_NO_SM2
841 BIO_puts(bio_out, "SM2\n");
842#endif
14e06391
P
843#ifdef OPENSSL_NO_SM3
844 BIO_puts(bio_out, "SM3\n");
845#endif
846#ifdef OPENSSL_NO_SM4
847 BIO_puts(bio_out, "SM4\n");
848#endif
27dae1b0
RL
849#ifdef OPENSSL_NO_SOCK
850 BIO_puts(bio_out, "SOCK\n");
851#endif
a760a380
DSH
852#ifdef OPENSSL_NO_SRP
853 BIO_puts(bio_out, "SRP\n");
854#endif
66b14bab
RL
855#ifdef OPENSSL_NO_SRTP
856 BIO_puts(bio_out, "SRTP\n");
857#endif
858#ifdef OPENSSL_NO_SSL3
859 BIO_puts(bio_out, "SSL3\n");
860#endif
6b01bed2
VD
861#ifdef OPENSSL_NO_TLS1
862 BIO_puts(bio_out, "TLS1\n");
863#endif
864#ifdef OPENSSL_NO_TLS1_1
865 BIO_puts(bio_out, "TLS1_1\n");
866#endif
867#ifdef OPENSSL_NO_TLS1_2
868 BIO_puts(bio_out, "TLS1_2\n");
869#endif
27dae1b0
RL
870#ifdef OPENSSL_NO_WHIRLPOOL
871 BIO_puts(bio_out, "WHIRLPOOL\n");
872#endif
d230bd1d
RL
873#ifndef ZLIB
874 BIO_puts(bio_out, "ZLIB\n");
875#endif
a760a380
DSH
876}
877
0f113f3e
MC
878static LHASH_OF(FUNCTION) *prog_init(void)
879{
391e987e
RL
880 static LHASH_OF(FUNCTION) *ret = NULL;
881 static int prog_inited = 0;
0f113f3e
MC
882 FUNCTION *f;
883 size_t i;
884
391e987e
RL
885 if (prog_inited)
886 return ret;
887
888 prog_inited = 1;
889
7e1b7485 890 /* Sort alphabetically within category. For nicer help displays. */
391e987e
RL
891 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
892 ;
b4faea50 893 qsort(functions, i, sizeof(*functions), SortFnByName);
0f113f3e 894
62d0577e 895 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
26a7d938 896 return NULL;
0f113f3e
MC
897
898 for (f = functions; f->name != NULL; f++)
899 (void)lh_FUNCTION_insert(ret, f);
296cbb57 900 return ret;
0f113f3e 901}