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