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