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
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();
73 * NOTE: This is an undocumented feature required for testing only.
74 * There are no guarantees that it will exist in future builds.
76 use_libctx = getenv("OPENSSL_TEST_LIBCTX");
77 if (use_libctx != NULL) {
78 /* Set this to "1" to create a global libctx */
79 if (strcmp(use_libctx, "1") == 0) {
80 if (app_create_libctx() == NULL)
88 static void apps_shutdown(void)
90 app_providers_cleanup();
91 OSSL_LIB_CTX_free(app_get0_libctx());
96 #ifndef OPENSSL_NO_TRACE
97 typedef struct tracedata_st {
99 unsigned int ingroup:1;
102 static size_t internal_trace_cb(const char *buf, size_t cnt,
103 int category, int cmd, void *vdata)
106 tracedata *trace_data = vdata;
107 char buffer[256], *hex;
108 CRYPTO_THREAD_ID tid;
111 case OSSL_TRACE_CTRL_BEGIN:
112 if (trace_data->ingroup) {
113 BIO_printf(bio_err, "ERROR: tracing already started\n");
116 trace_data->ingroup = 1;
118 tid = CRYPTO_THREAD_get_current_id();
119 hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
120 BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
121 hex == NULL ? "<null>" : hex,
122 OSSL_trace_get_category_name(category));
124 BIO_set_prefix(trace_data->bio, buffer);
126 case OSSL_TRACE_CTRL_WRITE:
127 if (!trace_data->ingroup) {
128 BIO_printf(bio_err, "ERROR: writing when tracing not started\n");
132 ret = BIO_write(trace_data->bio, buf, cnt);
134 case OSSL_TRACE_CTRL_END:
135 if (!trace_data->ingroup) {
136 BIO_printf(bio_err, "ERROR: finishing when tracing not started\n");
139 trace_data->ingroup = 0;
141 BIO_set_prefix(trace_data->bio, NULL);
146 return ret < 0 ? 0 : ret;
149 DEFINE_STACK_OF(tracedata)
150 static STACK_OF(tracedata) *trace_data_stack;
152 static void tracedata_free(tracedata *data)
154 BIO_free_all(data->bio);
158 static STACK_OF(tracedata) *trace_data_stack;
160 static void cleanup_trace(void)
162 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
165 static void setup_trace_category(int category)
168 tracedata *trace_data;
170 if (OSSL_trace_enabled(category))
173 channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
174 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
176 if (trace_data == NULL
177 || (trace_data->bio = channel) == NULL
178 || OSSL_trace_set_callback(category, internal_trace_cb,
180 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
183 "warning: unable to setup trace callback for category '%s'.\n",
184 OSSL_trace_get_category_name(category));
186 OSSL_trace_set_callback(category, NULL, NULL);
187 BIO_free_all(channel);
191 static void setup_trace(const char *str)
196 * We add this handler as early as possible to ensure it's executed
197 * as late as possible, i.e. after the TRACE code has done its cleanup
198 * (which happens last in OPENSSL_cleanup).
200 atexit(cleanup_trace);
202 trace_data_stack = sk_tracedata_new_null();
203 val = OPENSSL_strdup(str);
209 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
210 int category = OSSL_trace_get_category_num(item);
212 if (category == OSSL_TRACE_CATEGORY_ALL) {
213 while (++category < OSSL_TRACE_CATEGORY_NUM)
214 setup_trace_category(category);
216 } else if (category > 0) {
217 setup_trace_category(category);
220 "warning: unknown trace category: '%s'.\n", item);
227 #endif /* OPENSSL_NO_TRACE */
229 static char *help_argv[] = { "help", NULL };
231 int main(int argc, char *argv[])
234 LHASH_OF(FUNCTION) *prog = NULL;
242 /* Set up some of the environment. */
243 bio_in = dup_bio_in(FORMAT_TEXT);
244 bio_out = dup_bio_out(FORMAT_TEXT);
245 bio_err = dup_bio_err(FORMAT_TEXT);
247 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
248 argv = copy_argv(&argc, argv);
249 #elif defined(_WIN32)
251 * Replace argv[] with UTF-8 encoded strings.
253 win32_utf8argv(&argc, &argv);
256 #ifndef OPENSSL_NO_TRACE
257 setup_trace(getenv("OPENSSL_TRACE"));
260 if (!apps_startup()) {
262 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
263 ERR_print_errors(bio_err);
271 "FATAL: Startup failure (dev note: prog_init() failed)\n");
272 ERR_print_errors(bio_err);
276 pname = opt_progname(argv[0]);
278 default_config_file = CONF_get1_default_config_file();
279 if (default_config_file == NULL)
280 app_bail_out("%s: could not get default config file\n", pname);
282 /* first check the program name */
284 fp = lh_FUNCTION_retrieve(prog, &f);
286 /* We assume we've been called as 'openssl cmd' */
291 /* If there's a command, run with that, otherwise "help". */
293 ? do_cmd(prog, argc, argv)
294 : do_cmd(prog, 1, help_argv);
297 OPENSSL_free(default_config_file);
298 lh_FUNCTION_free(prog);
299 OPENSSL_free(arg.argv);
303 BIO_free_all(bio_out);
309 typedef enum HELP_CHOICE {
310 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
313 const OPTIONS help_options[] = {
314 {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
316 OPT_SECTION("General"),
317 {"help", OPT_hHELP, '-', "Display this summary"},
320 {"command", 0, 0, "Name of command to display help (optional)"},
325 int help_main(int argc, char **argv)
335 prog = opt_init(argc, argv, help_options);
336 while ((o = opt_next()) != OPT_hEOF) {
340 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
343 opt_help(help_options);
348 if (opt_num_rest() == 1) {
349 new_argv[0] = opt_rest()[0];
350 new_argv[1] = "--help";
352 return do_cmd(prog_init(), 2, new_argv);
354 if (opt_num_rest() != 0) {
355 BIO_printf(bio_err, "Usage: %s\n", prog);
359 calculate_columns(functions, &dc);
360 BIO_printf(bio_err, "Standard commands");
363 for (fp = functions; fp->name != NULL; fp++) {
365 if (i++ % dc.columns == 0) {
366 BIO_printf(bio_err, "\n");
369 if (fp->type != tp) {
372 BIO_printf(bio_err, "\n");
376 "\nMessage Digest commands (see the `dgst' command for more details)\n");
377 } else if (tp == FT_cipher) {
380 "\nCipher commands (see the `enc' command for more details)\n");
383 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
385 BIO_printf(bio_err, "\n\n");
389 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
393 if (argc <= 0 || argv[0] == NULL)
396 fp = lh_FUNCTION_retrieve(prog, &f);
398 if (EVP_get_digestbyname(argv[0])) {
402 } else if (EVP_get_cipherbyname(argv[0])) {
409 if (fp->deprecated_alternative != NULL)
411 return fp->func(argc, argv);
413 if ((strncmp(argv[0], "no-", 3)) == 0) {
415 * User is asking if foo is unsupported, by trying to "run" the
416 * no-foo command. Strange.
418 f.name = argv[0] + 3;
419 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
420 BIO_printf(bio_out, "%s\n", argv[0]);
423 BIO_printf(bio_out, "%s\n", argv[0] + 3);
427 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
432 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
434 return strncmp(a->name, b->name, 8);
437 static unsigned long function_hash(const FUNCTION * a)
439 return OPENSSL_LH_strhash(a->name);
442 static int SortFnByName(const void *_f1, const void *_f2)
444 const FUNCTION *f1 = _f1;
445 const FUNCTION *f2 = _f2;
447 if (f1->type != f2->type)
448 return f1->type - f2->type;
449 return strcmp(f1->name, f2->name);
452 static LHASH_OF(FUNCTION) *prog_init(void)
454 static LHASH_OF(FUNCTION) *ret = NULL;
455 static int prog_inited = 0;
464 /* Sort alphabetically within category. For nicer help displays. */
465 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
467 qsort(functions, i, sizeof(*functions), SortFnByName);
469 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
472 for (f = functions; f->name != NULL; f++)
473 (void)lh_FUNCTION_insert(ret, f);