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