2 * Copyright 1995-2021 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
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/trace.h>
16 #include <openssl/lhash.h>
17 #include <openssl/conf.h>
18 #include <openssl/x509.h>
19 #include <openssl/pem.h>
20 #include <openssl/ssl.h>
21 #ifndef OPENSSL_NO_ENGINE
22 # include <openssl/engine.h>
24 #include <openssl/err.h>
25 /* Needed to get the other O_xxx flags. */
26 #ifdef OPENSSL_SYS_VMS
33 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
34 * the base prototypes (we cast each variable inside the function to the
35 * required type of "FUNCTION*"). This removes the necessity for
36 * macro-generated wrapper functions.
38 static LHASH_OF(FUNCTION) *prog_init(void);
39 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
40 char *default_config_file = NULL;
46 static void warn_deprecated(const FUNCTION *fp)
48 if (fp->deprecated_version != NULL)
49 BIO_printf(bio_err, "The command %s was deprecated in version %s.",
50 fp->name, fp->deprecated_version);
52 BIO_printf(bio_err, "The command %s is deprecated.", fp->name);
53 if (strcmp(fp->deprecated_alternative, DEPRECATED_NO_ALTERNATIVE) != 0)
54 BIO_printf(bio_err, " Use '%s' instead.", fp->deprecated_alternative);
55 BIO_printf(bio_err, "\n");
58 static int apps_startup(void)
60 const char *use_libctx = NULL;
62 signal(SIGPIPE, SIG_IGN);
65 /* Set non-default library initialisation settings */
66 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
67 | OPENSSL_INIT_LOAD_CONFIG, NULL))
70 (void)setup_ui_method();
71 (void)setup_engine_loader();
74 * NOTE: This is an undocumented feature required for testing only.
75 * There are no guarantees that it will exist in future builds.
77 use_libctx = getenv("OPENSSL_TEST_LIBCTX");
78 if (use_libctx != NULL) {
79 /* Set this to "1" to create a global libctx */
80 if (strcmp(use_libctx, "1") == 0) {
81 if (app_create_libctx() == NULL)
89 static void apps_shutdown(void)
91 app_providers_cleanup();
92 OSSL_LIB_CTX_free(app_get0_libctx());
93 destroy_engine_loader();
98 #ifndef OPENSSL_NO_TRACE
99 typedef struct tracedata_st {
101 unsigned int ingroup:1;
104 static size_t internal_trace_cb(const char *buf, size_t cnt,
105 int category, int cmd, void *vdata)
108 tracedata *trace_data = vdata;
109 char buffer[256], *hex;
110 CRYPTO_THREAD_ID tid;
113 case OSSL_TRACE_CTRL_BEGIN:
114 if (trace_data->ingroup) {
115 BIO_printf(bio_err, "ERROR: tracing already started\n");
118 trace_data->ingroup = 1;
120 tid = CRYPTO_THREAD_get_current_id();
121 hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
122 BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
123 hex == NULL ? "<null>" : hex,
124 OSSL_trace_get_category_name(category));
126 BIO_set_prefix(trace_data->bio, buffer);
128 case OSSL_TRACE_CTRL_WRITE:
129 if (!trace_data->ingroup) {
130 BIO_printf(bio_err, "ERROR: writing when tracing not started\n");
134 ret = BIO_write(trace_data->bio, buf, cnt);
136 case OSSL_TRACE_CTRL_END:
137 if (!trace_data->ingroup) {
138 BIO_printf(bio_err, "ERROR: finishing when tracing not started\n");
141 trace_data->ingroup = 0;
143 BIO_set_prefix(trace_data->bio, NULL);
148 return ret < 0 ? 0 : ret;
151 DEFINE_STACK_OF(tracedata)
152 static STACK_OF(tracedata) *trace_data_stack;
154 static void tracedata_free(tracedata *data)
156 BIO_free_all(data->bio);
160 static STACK_OF(tracedata) *trace_data_stack;
162 static void cleanup_trace(void)
164 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
167 static void setup_trace_category(int category)
170 tracedata *trace_data;
172 if (OSSL_trace_enabled(category))
175 channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
176 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
178 if (trace_data == NULL
179 || (trace_data->bio = channel) == NULL
180 || OSSL_trace_set_callback(category, internal_trace_cb,
182 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
185 "warning: unable to setup trace callback for category '%s'.\n",
186 OSSL_trace_get_category_name(category));
188 OSSL_trace_set_callback(category, NULL, NULL);
189 BIO_free_all(channel);
193 static void setup_trace(const char *str)
198 * We add this handler as early as possible to ensure it's executed
199 * as late as possible, i.e. after the TRACE code has done its cleanup
200 * (which happens last in OPENSSL_cleanup).
202 atexit(cleanup_trace);
204 trace_data_stack = sk_tracedata_new_null();
205 val = OPENSSL_strdup(str);
211 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
212 int category = OSSL_trace_get_category_num(item);
214 if (category == OSSL_TRACE_CATEGORY_ALL) {
215 while (++category < OSSL_TRACE_CATEGORY_NUM)
216 setup_trace_category(category);
218 } else if (category > 0) {
219 setup_trace_category(category);
222 "warning: unknown trace category: '%s'.\n", item);
229 #endif /* OPENSSL_NO_TRACE */
231 static char *help_argv[] = { "help", NULL };
233 int main(int argc, char *argv[])
236 LHASH_OF(FUNCTION) *prog = NULL;
246 /* Set up some of the environment. */
247 bio_in = dup_bio_in(FORMAT_TEXT);
248 bio_out = dup_bio_out(FORMAT_TEXT);
249 bio_err = dup_bio_err(FORMAT_TEXT);
251 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
252 argv = copy_argv(&argc, argv);
253 #elif defined(_WIN32)
254 /* Replace argv[] with UTF-8 encoded strings. */
255 win32_utf8argv(&argc, &argv);
258 #ifndef OPENSSL_NO_TRACE
259 setup_trace(getenv("OPENSSL_TRACE"));
262 if ((fname = "apps_startup", !apps_startup())
263 || (fname = "prog_init", (prog = prog_init()) == NULL)) {
265 "FATAL: Startup failure (dev note: %s()) for %s\n",
267 ERR_print_errors(bio_err);
271 pname = opt_progname(argv[0]);
273 default_config_file = CONF_get1_default_config_file();
274 if (default_config_file == NULL)
275 app_bail_out("%s: could not get default config file\n", pname);
277 /* first check the program name */
279 fp = lh_FUNCTION_retrieve(prog, &f);
281 /* We assume we've been called as 'openssl ...' */
282 global_help = argc > 1
283 && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0
284 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0);
287 opt_appname(argc == 1 || global_help ? "help" : argv[0]);
292 /* If there's a command, run with that, otherwise "help". */
293 ret = argc == 0 || global_help
294 ? do_cmd(prog, 1, help_argv)
295 : do_cmd(prog, argc, argv);
298 OPENSSL_free(default_config_file);
299 lh_FUNCTION_free(prog);
300 OPENSSL_free(arg.argv);
301 if (!app_RAND_write())
305 BIO_free_all(bio_out);
311 typedef enum HELP_CHOICE {
312 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
315 const OPTIONS help_options[] = {
316 {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
318 OPT_SECTION("General"),
319 {"help", OPT_hHELP, '-', "Display this summary"},
322 {"command", 0, 0, "Name of command to display help (optional)"},
327 int help_main(int argc, char **argv)
337 prog = opt_init(argc, argv, help_options);
338 while ((o = opt_next()) != OPT_hEOF) {
342 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
345 opt_help(help_options);
350 /* One optional argument, the command to get help for. */
351 if (opt_num_rest() == 1) {
352 new_argv[0] = opt_rest()[0];
353 new_argv[1] = "--help";
355 return do_cmd(prog_init(), 2, new_argv);
357 if (opt_num_rest() != 0) {
358 BIO_printf(bio_err, "Usage: %s\n", prog);
362 calculate_columns(functions, &dc);
363 BIO_printf(bio_err, "%s:\n\nStandard commands", prog);
366 for (fp = functions; fp->name != NULL; fp++) {
368 if (i++ % dc.columns == 0) {
369 BIO_printf(bio_err, "\n");
372 if (fp->type != tp) {
375 BIO_printf(bio_err, "\n");
379 "\nMessage Digest commands (see the `dgst' command for more details)\n");
380 } else if (tp == FT_cipher) {
383 "\nCipher commands (see the `enc' command for more details)\n");
386 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
388 BIO_printf(bio_err, "\n\n");
392 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
396 if (argc <= 0 || argv[0] == NULL)
399 fp = lh_FUNCTION_retrieve(prog, &f);
401 if (EVP_get_digestbyname(argv[0])) {
405 } else if (EVP_get_cipherbyname(argv[0])) {
412 if (fp->deprecated_alternative != NULL)
414 return fp->func(argc, argv);
416 if ((strncmp(argv[0], "no-", 3)) == 0) {
418 * User is asking if foo is unsupported, by trying to "run" the
419 * no-foo command. Strange.
421 f.name = argv[0] + 3;
422 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
423 BIO_printf(bio_out, "%s\n", argv[0]);
426 BIO_printf(bio_out, "%s\n", argv[0] + 3);
430 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
435 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
437 return strncmp(a->name, b->name, 8);
440 static unsigned long function_hash(const FUNCTION * a)
442 return OPENSSL_LH_strhash(a->name);
445 static int SortFnByName(const void *_f1, const void *_f2)
447 const FUNCTION *f1 = _f1;
448 const FUNCTION *f2 = _f2;
450 if (f1->type != f2->type)
451 return f1->type - f2->type;
452 return strcmp(f1->name, f2->name);
455 static LHASH_OF(FUNCTION) *prog_init(void)
457 static LHASH_OF(FUNCTION) *ret = NULL;
458 static int prog_inited = 0;
467 /* Sort alphabetically within category. For nicer help displays. */
468 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
470 qsort(functions, i, sizeof(*functions), SortFnByName);
472 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
475 for (f = functions; f->name != NULL; f++)
476 (void)lh_FUNCTION_insert(ret, f);