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