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