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
34 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
35 * the base prototypes (we cast each variable inside the function to the
36 * required type of "FUNCTION*"). This removes the necessity for
37 * macro-generated wrapper functions.
39 static LHASH_OF(FUNCTION
) *prog_init(void);
40 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[]);
41 char *default_config_file
= NULL
;
47 static void warn_deprecated(const FUNCTION
*fp
)
49 if (fp
->deprecated_version
!= NULL
)
50 BIO_printf(bio_err
, "The command %s was deprecated in version %s.",
51 fp
->name
, fp
->deprecated_version
);
53 BIO_printf(bio_err
, "The command %s is deprecated.", fp
->name
);
54 if (strcmp(fp
->deprecated_alternative
, DEPRECATED_NO_ALTERNATIVE
) != 0)
55 BIO_printf(bio_err
, " Use '%s' instead.", fp
->deprecated_alternative
);
56 BIO_printf(bio_err
, "\n");
59 static int apps_startup(void)
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
))
75 static void apps_shutdown(void)
81 #ifndef OPENSSL_NO_TRACE
82 typedef struct tracedata_st
{
84 unsigned int ingroup
:1;
87 static size_t internal_trace_cb(const char *buf
, size_t cnt
,
88 int category
, int cmd
, void *vdata
)
91 tracedata
*trace_data
= vdata
;
92 char buffer
[256], *hex
;
96 case OSSL_TRACE_CTRL_BEGIN
:
97 if (!ossl_assert(!trace_data
->ingroup
))
99 trace_data
->ingroup
= 1;
101 tid
= CRYPTO_THREAD_get_current_id();
102 hex
= OPENSSL_buf2hexstr((const unsigned char *)&tid
, sizeof(tid
));
103 BIO_snprintf(buffer
, sizeof(buffer
), "TRACE[%s]:%s: ",
104 hex
== NULL
? "<null>" : hex
,
105 OSSL_trace_get_category_name(category
));
107 BIO_set_prefix(trace_data
->bio
, buffer
);
109 case OSSL_TRACE_CTRL_WRITE
:
110 if (!ossl_assert(trace_data
->ingroup
))
113 ret
= BIO_write(trace_data
->bio
, buf
, cnt
);
115 case OSSL_TRACE_CTRL_END
:
116 if (!ossl_assert(trace_data
->ingroup
))
118 trace_data
->ingroup
= 0;
120 BIO_set_prefix(trace_data
->bio
, NULL
);
125 return ret
< 0 ? 0 : ret
;
128 DEFINE_STACK_OF(tracedata
)
129 static STACK_OF(tracedata
) *trace_data_stack
;
131 static void tracedata_free(tracedata
*data
)
133 BIO_free_all(data
->bio
);
137 static STACK_OF(tracedata
) *trace_data_stack
;
139 static void cleanup_trace(void)
141 sk_tracedata_pop_free(trace_data_stack
, tracedata_free
);
144 static void setup_trace_category(int category
)
147 tracedata
*trace_data
;
149 if (OSSL_trace_enabled(category
))
152 channel
= BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT
));
153 trace_data
= OPENSSL_zalloc(sizeof(*trace_data
));
155 if (trace_data
== NULL
156 || (trace_data
->bio
= channel
) == NULL
157 || OSSL_trace_set_callback(category
, internal_trace_cb
,
159 || sk_tracedata_push(trace_data_stack
, trace_data
) == 0) {
162 "warning: unable to setup trace callback for category '%s'.\n",
163 OSSL_trace_get_category_name(category
));
165 OSSL_trace_set_callback(category
, NULL
, NULL
);
166 BIO_free_all(channel
);
170 static void setup_trace(const char *str
)
175 * We add this handler as early as possible to ensure it's executed
176 * as late as possible, i.e. after the TRACE code has done its cleanup
177 * (which happens last in OPENSSL_cleanup).
179 atexit(cleanup_trace
);
181 trace_data_stack
= sk_tracedata_new_null();
182 val
= OPENSSL_strdup(str
);
188 for (valp
= val
; (item
= strtok(valp
, ",")) != NULL
; valp
= NULL
) {
189 int category
= OSSL_trace_get_category_num(item
);
191 if (category
== OSSL_TRACE_CATEGORY_ALL
) {
192 while (++category
< OSSL_TRACE_CATEGORY_NUM
)
193 setup_trace_category(category
);
195 } else if (category
> 0) {
196 setup_trace_category(category
);
199 "warning: unknown trace category: '%s'.\n", item
);
206 #endif /* OPENSSL_NO_TRACE */
208 static char *help_argv
[] = { "help", NULL
};
210 int main(int argc
, char *argv
[])
213 LHASH_OF(FUNCTION
) *prog
= NULL
;
221 /* Set up some of the environment. */
222 bio_in
= dup_bio_in(FORMAT_TEXT
);
223 bio_out
= dup_bio_out(FORMAT_TEXT
);
224 bio_err
= dup_bio_err(FORMAT_TEXT
);
226 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
227 argv
= copy_argv(&argc
, argv
);
228 #elif defined(_WIN32)
230 * Replace argv[] with UTF-8 encoded strings.
232 win32_utf8argv(&argc
, &argv
);
235 #ifndef OPENSSL_NO_TRACE
236 setup_trace(getenv("OPENSSL_TRACE"));
239 if (!apps_startup()) {
241 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
242 ERR_print_errors(bio_err
);
250 "FATAL: Startup failure (dev note: prog_init() failed)\n");
251 ERR_print_errors(bio_err
);
255 pname
= opt_progname(argv
[0]);
257 default_config_file
= CONF_get1_default_config_file();
258 if (default_config_file
== NULL
)
259 app_bail_out("%s: could not get default config file\n", pname
);
261 /* first check the program name */
263 fp
= lh_FUNCTION_retrieve(prog
, &f
);
265 /* We assume we've been called as 'openssl cmd' */
270 /* If there's a command, run with that, otherwise "help". */
272 ? do_cmd(prog
, argc
, argv
)
273 : do_cmd(prog
, 1, help_argv
);
276 app_providers_cleanup();
277 OPENSSL_free(default_config_file
);
278 lh_FUNCTION_free(prog
);
279 OPENSSL_free(arg
.argv
);
283 BIO_free_all(bio_out
);
289 typedef enum HELP_CHOICE
{
290 OPT_hERR
= -1, OPT_hEOF
= 0, OPT_hHELP
293 const OPTIONS help_options
[] = {
294 {OPT_HELP_STR
, 1, '-', "Usage: help [options] [command]\n"},
296 OPT_SECTION("General"),
297 {"help", OPT_hHELP
, '-', "Display this summary"},
300 {"command", 0, 0, "Name of command to display help (optional)"},
305 int help_main(int argc
, char **argv
)
316 new_argv
[0] = "help";
318 return do_cmd(prog_init(), 1, new_argv
);
321 prog
= opt_init(argc
, argv
, help_options
);
322 while ((o
= opt_next()) != OPT_hEOF
) {
326 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
329 opt_help(help_options
);
334 if (opt_num_rest() == 1) {
335 new_argv
[0] = opt_rest()[0];
336 new_argv
[1] = "--help";
338 return do_cmd(prog_init(), 2, new_argv
);
340 if (opt_num_rest() != 0) {
341 BIO_printf(bio_err
, "Usage: %s\n", prog
);
345 calculate_columns(functions
, &dc
);
346 BIO_printf(bio_err
, "Standard commands");
349 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
351 if (i
++ % dc
.columns
== 0) {
352 BIO_printf(bio_err
, "\n");
355 if (fp
->type
!= tp
) {
358 BIO_printf(bio_err
, "\n");
362 "\nMessage Digest commands (see the `dgst' command for more details)\n");
363 } else if (tp
== FT_cipher
) {
366 "\nCipher commands (see the `enc' command for more details)\n");
369 BIO_printf(bio_err
, "%-*s", dc
.width
, fp
->name
);
371 BIO_printf(bio_err
, "\n\n");
375 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[])
379 if (argc
<= 0 || argv
[0] == NULL
)
382 fp
= lh_FUNCTION_retrieve(prog
, &f
);
384 if (EVP_get_digestbyname(argv
[0])) {
388 } else if (EVP_get_cipherbyname(argv
[0])) {
395 if (fp
->deprecated_alternative
!= NULL
)
397 return fp
->func(argc
, argv
);
399 if ((strncmp(argv
[0], "no-", 3)) == 0) {
401 * User is asking if foo is unsupported, by trying to "run" the
402 * no-foo command. Strange.
404 f
.name
= argv
[0] + 3;
405 if (lh_FUNCTION_retrieve(prog
, &f
) == NULL
) {
406 BIO_printf(bio_out
, "%s\n", argv
[0]);
409 BIO_printf(bio_out
, "%s\n", argv
[0] + 3);
413 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
418 static int function_cmp(const FUNCTION
* a
, const FUNCTION
* b
)
420 return strncmp(a
->name
, b
->name
, 8);
423 static unsigned long function_hash(const FUNCTION
* a
)
425 return OPENSSL_LH_strhash(a
->name
);
428 static int SortFnByName(const void *_f1
, const void *_f2
)
430 const FUNCTION
*f1
= _f1
;
431 const FUNCTION
*f2
= _f2
;
433 if (f1
->type
!= f2
->type
)
434 return f1
->type
- f2
->type
;
435 return strcmp(f1
->name
, f2
->name
);
438 static LHASH_OF(FUNCTION
) *prog_init(void)
440 static LHASH_OF(FUNCTION
) *ret
= NULL
;
441 static int prog_inited
= 0;
450 /* Sort alphabetically within category. For nicer help displays. */
451 for (i
= 0, f
= functions
; f
->name
!= NULL
; ++f
, ++i
)
453 qsort(functions
, i
, sizeof(*functions
), SortFnByName
);
455 if ((ret
= lh_FUNCTION_new(function_hash
, function_cmp
)) == NULL
)
458 for (f
= functions
; f
->name
!= NULL
; f
++)
459 (void)lh_FUNCTION_insert(ret
, f
);