2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <internal/cryptlib.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>
25 #include <openssl/err.h>
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
33 /* Special sentinel to exit the program. */
34 #define EXIT_THE_PROGRAM (-1)
37 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
38 * the base prototypes (we cast each variable inside the function to the
39 * required type of "FUNCTION*"). This removes the necessity for
40 * macro-generated wrapper functions.
42 static LHASH_OF(FUNCTION) *prog_init(void);
43 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
44 char *default_config_file = NULL;
50 static void warn_deprecated(const FUNCTION *fp)
52 if (fp->deprecated_version != NULL)
53 BIO_printf(bio_err, "The command %s was deprecated in version %s.",
54 fp->name, fp->deprecated_version);
56 BIO_printf(bio_err, "The command %s is deprecated.", fp->name);
57 if (strcmp(fp->deprecated_alternative, DEPRECATED_NO_ALTERNATIVE) != 0)
58 BIO_printf(bio_err, " Use '%s' instead.", fp->deprecated_alternative);
59 BIO_printf(bio_err, "\n");
62 static int apps_startup(void)
65 signal(SIGPIPE, SIG_IGN);
68 /* Set non-default library initialisation settings */
69 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
70 | OPENSSL_INIT_LOAD_CONFIG, NULL))
78 static void apps_shutdown(void)
84 #ifndef OPENSSL_NO_TRACE
85 typedef struct tracedata_st {
87 unsigned int ingroup:1;
90 static size_t internal_trace_cb(const char *buf, size_t cnt,
91 int category, int cmd, void *vdata)
94 tracedata *trace_data = vdata;
95 char buffer[256], *hex;
99 case OSSL_TRACE_CTRL_BEGIN:
100 if (!ossl_assert(!trace_data->ingroup))
102 trace_data->ingroup = 1;
104 tid = CRYPTO_THREAD_get_current_id();
105 hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
106 BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
107 hex == NULL ? "<null>" : hex,
108 OSSL_trace_get_category_name(category));
110 BIO_set_prefix(trace_data->bio, buffer);
112 case OSSL_TRACE_CTRL_WRITE:
113 if (!ossl_assert(trace_data->ingroup))
116 ret = BIO_write(trace_data->bio, buf, cnt);
118 case OSSL_TRACE_CTRL_END:
119 if (!ossl_assert(trace_data->ingroup))
121 trace_data->ingroup = 0;
123 BIO_set_prefix(trace_data->bio, NULL);
128 return ret < 0 ? 0 : ret;
131 DEFINE_STACK_OF(tracedata)
132 static STACK_OF(tracedata) *trace_data_stack;
134 static void tracedata_free(tracedata *data)
136 BIO_free_all(data->bio);
140 static STACK_OF(tracedata) *trace_data_stack;
142 static void cleanup_trace(void)
144 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
147 static void setup_trace_category(int category)
150 tracedata *trace_data;
152 if (OSSL_trace_enabled(category))
155 channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
156 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
158 if (trace_data == NULL
159 || (trace_data->bio = channel) == NULL
160 || OSSL_trace_set_callback(category, internal_trace_cb,
162 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
165 "warning: unable to setup trace callback for category '%s'.\n",
166 OSSL_trace_get_category_name(category));
168 OSSL_trace_set_callback(category, NULL, NULL);
169 BIO_free_all(channel);
173 static void setup_trace(const char *str)
178 * We add this handler as early as possible to ensure it's executed
179 * as late as possible, i.e. after the TRACE code has done its cleanup
180 * (which happens last in OPENSSL_cleanup).
182 atexit(cleanup_trace);
184 trace_data_stack = sk_tracedata_new_null();
185 val = OPENSSL_strdup(str);
191 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
192 int category = OSSL_trace_get_category_num(item);
194 if (category == OSSL_TRACE_CATEGORY_ALL) {
195 while (++category < OSSL_TRACE_CATEGORY_NUM)
196 setup_trace_category(category);
198 } else if (category > 0) {
199 setup_trace_category(category);
202 "warning: unknown trace category: '%s'.\n", item);
209 #endif /* OPENSSL_NO_TRACE */
211 int main(int argc, char *argv[])
214 LHASH_OF(FUNCTION) *prog = NULL;
219 int first, n, i, ret = 0;
224 /* Set up some of the environment. */
225 bio_in = dup_bio_in(FORMAT_TEXT);
226 bio_out = dup_bio_out(FORMAT_TEXT);
227 bio_err = dup_bio_err(FORMAT_TEXT);
229 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
230 argv = copy_argv(&argc, argv);
231 #elif defined(_WIN32)
233 * Replace argv[] with UTF-8 encoded strings.
235 win32_utf8argv(&argc, &argv);
238 #ifndef OPENSSL_NO_TRACE
239 setup_trace(getenv("OPENSSL_TRACE"));
242 if (!apps_startup()) {
244 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
245 ERR_print_errors(bio_err);
253 "FATAL: Startup failure (dev note: prog_init() failed)\n");
254 ERR_print_errors(bio_err);
258 pname = opt_progname(argv[0]);
260 default_config_file = CONF_get1_default_config_file();
261 if (default_config_file == NULL)
262 app_bail_out("%s: could not get default config file\n", pname);
264 /* first check the program name */
266 fp = lh_FUNCTION_retrieve(prog, &f);
269 if (fp->deprecated_alternative != NULL)
271 ret = fp->func(argc, argv);
275 /* If there is stuff on the command line, run with that. */
279 ret = do_cmd(prog, argc, argv);
285 /* ok, lets enter interactive mode */
288 /* Read a line, continue reading if line ends with \ */
289 for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
290 prompt = first ? "OpenSSL> " : "> ";
293 fputs(prompt, stdout);
295 if (!fgets(p, n, stdin))
302 if (p[i - 2] != '\\')
309 extern char *readline(const char *);
310 extern void add_history(const char *cp);
313 text = readline(prompt);
319 if (text[i - 1] != '\\') {
320 p += strlen(strcpy(p, text));
327 p += strlen(strcpy(p, text));
334 if (!chopup_args(&arg, buf)) {
335 BIO_printf(bio_err, "Can't parse (no memory?)\n");
339 ret = do_cmd(prog, arg.argc, arg.argv);
340 if (ret == EXIT_THE_PROGRAM) {
345 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
346 (void)BIO_flush(bio_out);
347 (void)BIO_flush(bio_err);
351 app_providers_cleanup();
352 OPENSSL_free(default_config_file);
353 lh_FUNCTION_free(prog);
354 OPENSSL_free(arg.argv);
358 BIO_free_all(bio_out);
364 typedef enum HELP_CHOICE {
365 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
368 const OPTIONS help_options[] = {
369 {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
371 OPT_SECTION("General"),
372 {"help", OPT_hHELP, '-', "Display this summary"},
375 {"command", 0, 0, "Name of command to display help (optional)"},
380 int help_main(int argc, char **argv)
389 prog = opt_init(argc, argv, help_options);
390 while ((o = opt_next()) != OPT_hEOF) {
394 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
397 opt_help(help_options);
402 if (opt_num_rest() == 1) {
405 new_argv[0] = opt_rest()[0];
406 new_argv[1] = "--help";
408 return do_cmd(prog_init(), 2, new_argv);
410 if (opt_num_rest() != 0) {
411 BIO_printf(bio_err, "Usage: %s\n", prog);
415 calculate_columns(functions, &dc);
416 BIO_printf(bio_err, "Standard commands");
419 for (fp = functions; fp->name != NULL; fp++) {
421 if (i++ % dc.columns == 0) {
422 BIO_printf(bio_err, "\n");
425 if (fp->type != tp) {
428 BIO_printf(bio_err, "\n");
432 "\nMessage Digest commands (see the `dgst' command for more details)\n");
433 } else if (tp == FT_cipher) {
436 "\nCipher commands (see the `enc' command for more details)\n");
439 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
441 BIO_printf(bio_err, "\n\n");
445 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
449 if (argc <= 0 || argv[0] == NULL)
452 fp = lh_FUNCTION_retrieve(prog, &f);
454 if (EVP_get_digestbyname(argv[0])) {
458 } else if (EVP_get_cipherbyname(argv[0])) {
465 if (fp->deprecated_alternative != NULL)
467 return fp->func(argc, argv);
469 if ((strncmp(argv[0], "no-", 3)) == 0) {
471 * User is asking if foo is unsupported, by trying to "run" the
472 * no-foo command. Strange.
474 f.name = argv[0] + 3;
475 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
476 BIO_printf(bio_out, "%s\n", argv[0]);
479 BIO_printf(bio_out, "%s\n", argv[0] + 3);
482 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
483 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
484 /* Special value to mean "exit the program. */
485 return EXIT_THE_PROGRAM;
487 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
492 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
494 return strncmp(a->name, b->name, 8);
497 static unsigned long function_hash(const FUNCTION * a)
499 return OPENSSL_LH_strhash(a->name);
502 static int SortFnByName(const void *_f1, const void *_f2)
504 const FUNCTION *f1 = _f1;
505 const FUNCTION *f2 = _f2;
507 if (f1->type != f2->type)
508 return f1->type - f2->type;
509 return strcmp(f1->name, f2->name);
512 static LHASH_OF(FUNCTION) *prog_init(void)
514 static LHASH_OF(FUNCTION) *ret = NULL;
515 static int prog_inited = 0;
524 /* Sort alphabetically within category. For nicer help displays. */
525 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
527 qsort(functions, i, sizeof(*functions), SortFnByName);
529 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
532 for (f = functions; f->name != NULL; f++)
533 (void)lh_FUNCTION_insert(ret, f);