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