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)
83 static char *make_config_name(void)
89 if ((t
= getenv("OPENSSL_CONF")) != NULL
)
90 return OPENSSL_strdup(t
);
92 t
= X509_get_default_cert_area();
93 len
= strlen(t
) + 1 + strlen(OPENSSL_CONF
) + 1;
94 p
= app_malloc(len
, "config filename buffer");
96 #ifndef OPENSSL_SYS_VMS
99 strcat(p
, OPENSSL_CONF
);
105 #ifndef OPENSSL_NO_TRACE
106 typedef struct tracedata_st
{
108 unsigned int ingroup
:1;
111 static size_t internal_trace_cb(const char *buf
, size_t cnt
,
112 int category
, int cmd
, void *vdata
)
115 tracedata
*trace_data
= vdata
;
116 char buffer
[256], *hex
;
117 CRYPTO_THREAD_ID tid
;
120 case OSSL_TRACE_CTRL_BEGIN
:
121 if (!ossl_assert(!trace_data
->ingroup
))
123 trace_data
->ingroup
= 1;
125 tid
= CRYPTO_THREAD_get_current_id();
126 hex
= OPENSSL_buf2hexstr((const unsigned char *)&tid
, sizeof(tid
));
127 BIO_snprintf(buffer
, sizeof(buffer
), "TRACE[%s]:%s: ",
128 hex
== NULL
? "<null>" : hex
,
129 OSSL_trace_get_category_name(category
));
131 BIO_set_prefix(trace_data
->bio
, buffer
);
133 case OSSL_TRACE_CTRL_WRITE
:
134 if (!ossl_assert(trace_data
->ingroup
))
137 ret
= BIO_write(trace_data
->bio
, buf
, cnt
);
139 case OSSL_TRACE_CTRL_END
:
140 if (!ossl_assert(trace_data
->ingroup
))
142 trace_data
->ingroup
= 0;
144 BIO_set_prefix(trace_data
->bio
, NULL
);
149 return ret
< 0 ? 0 : ret
;
152 DEFINE_STACK_OF(tracedata
)
153 static STACK_OF(tracedata
) *trace_data_stack
;
155 static void tracedata_free(tracedata
*data
)
157 BIO_free_all(data
->bio
);
161 static STACK_OF(tracedata
) *trace_data_stack
;
163 static void cleanup_trace(void)
165 sk_tracedata_pop_free(trace_data_stack
, tracedata_free
);
168 static void setup_trace_category(int category
)
171 tracedata
*trace_data
;
173 if (OSSL_trace_enabled(category
))
176 channel
= BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT
));
177 trace_data
= OPENSSL_zalloc(sizeof(*trace_data
));
179 if (trace_data
== NULL
180 || (trace_data
->bio
= channel
) == NULL
181 || OSSL_trace_set_callback(category
, internal_trace_cb
,
183 || sk_tracedata_push(trace_data_stack
, trace_data
) == 0) {
186 "warning: unable to setup trace callback for category '%s'.\n",
187 OSSL_trace_get_category_name(category
));
189 OSSL_trace_set_callback(category
, NULL
, NULL
);
190 BIO_free_all(channel
);
194 static void setup_trace(const char *str
)
199 * We add this handler as early as possible to ensure it's executed
200 * as late as possible, i.e. after the TRACE code has done its cleanup
201 * (which happens last in OPENSSL_cleanup).
203 atexit(cleanup_trace
);
205 trace_data_stack
= sk_tracedata_new_null();
206 val
= OPENSSL_strdup(str
);
212 for (valp
= val
; (item
= strtok(valp
, ",")) != NULL
; valp
= NULL
) {
213 int category
= OSSL_trace_get_category_num(item
);
215 if (category
== OSSL_TRACE_CATEGORY_ALL
) {
216 while (++category
< OSSL_TRACE_CATEGORY_NUM
)
217 setup_trace_category(category
);
219 } else if (category
> 0) {
220 setup_trace_category(category
);
223 "warning: unknown trace category: '%s'.\n", item
);
230 #endif /* OPENSSL_NO_TRACE */
232 int main(int argc
, char *argv
[])
235 LHASH_OF(FUNCTION
) *prog
= NULL
;
240 int first
, n
, i
, ret
= 0;
245 /* Set up some of the environment. */
246 default_config_file
= make_config_name();
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)
255 * Replace argv[] with UTF-8 encoded strings.
257 win32_utf8argv(&argc
, &argv
);
260 #ifndef OPENSSL_NO_TRACE
261 setup_trace(getenv("OPENSSL_TRACE"));
264 if (getenv("OPENSSL_FIPS")) {
265 BIO_printf(bio_err
, "FIPS mode not supported.\n");
269 if (!apps_startup()) {
271 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
272 ERR_print_errors(bio_err
);
280 "FATAL: Startup failure (dev note: prog_init() failed)\n");
281 ERR_print_errors(bio_err
);
285 pname
= opt_progname(argv
[0]);
287 /* first check the program name */
289 fp
= lh_FUNCTION_retrieve(prog
, &f
);
292 if (fp
->deprecated_alternative
!= NULL
)
294 ret
= fp
->func(argc
, argv
);
298 /* If there is stuff on the command line, run with that. */
302 ret
= do_cmd(prog
, argc
, argv
);
308 /* ok, lets enter interactive mode */
311 /* Read a line, continue reading if line ends with \ */
312 for (p
= buf
, n
= sizeof(buf
), i
= 0, first
= 1; n
> 0; first
= 0) {
313 prompt
= first
? "OpenSSL> " : "> ";
316 fputs(prompt
, stdout
);
318 if (!fgets(p
, n
, stdin
))
325 if (p
[i
- 2] != '\\')
332 extern char *readline(const char *);
333 extern void add_history(const char *cp
);
336 text
= readline(prompt
);
342 if (text
[i
- 1] != '\\') {
343 p
+= strlen(strcpy(p
, text
));
350 p
+= strlen(strcpy(p
, text
));
357 if (!chopup_args(&arg
, buf
)) {
358 BIO_printf(bio_err
, "Can't parse (no memory?)\n");
362 ret
= do_cmd(prog
, arg
.argc
, arg
.argv
);
363 if (ret
== EXIT_THE_PROGRAM
) {
368 BIO_printf(bio_err
, "error in %s\n", arg
.argv
[0]);
369 (void)BIO_flush(bio_out
);
370 (void)BIO_flush(bio_err
);
374 app_providers_cleanup();
375 OPENSSL_free(default_config_file
);
376 lh_FUNCTION_free(prog
);
377 OPENSSL_free(arg
.argv
);
381 BIO_free_all(bio_out
);
387 typedef enum HELP_CHOICE
{
388 OPT_hERR
= -1, OPT_hEOF
= 0, OPT_hHELP
391 const OPTIONS help_options
[] = {
392 {OPT_HELP_STR
, 1, '-', "Usage: help [options] [command]\n"},
394 OPT_SECTION("General"),
395 {"help", OPT_hHELP
, '-', "Display this summary"},
398 {"command", 0, 0, "Name of command to display help (optional)"},
403 int help_main(int argc
, char **argv
)
412 prog
= opt_init(argc
, argv
, help_options
);
413 while ((o
= opt_next()) != OPT_hEOF
) {
417 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
420 opt_help(help_options
);
425 if (opt_num_rest() == 1) {
428 new_argv
[0] = opt_rest()[0];
429 new_argv
[1] = "--help";
431 return do_cmd(prog_init(), 2, new_argv
);
433 if (opt_num_rest() != 0) {
434 BIO_printf(bio_err
, "Usage: %s\n", prog
);
438 calculate_columns(functions
, &dc
);
439 BIO_printf(bio_err
, "Standard commands");
442 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
444 if (i
++ % dc
.columns
== 0) {
445 BIO_printf(bio_err
, "\n");
448 if (fp
->type
!= tp
) {
451 BIO_printf(bio_err
, "\n");
455 "\nMessage Digest commands (see the `dgst' command for more details)\n");
456 } else if (tp
== FT_cipher
) {
459 "\nCipher commands (see the `enc' command for more details)\n");
462 BIO_printf(bio_err
, "%-*s", dc
.width
, fp
->name
);
464 BIO_printf(bio_err
, "\n\n");
468 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[])
472 if (argc
<= 0 || argv
[0] == NULL
)
475 fp
= lh_FUNCTION_retrieve(prog
, &f
);
477 if (EVP_get_digestbyname(argv
[0])) {
481 } else if (EVP_get_cipherbyname(argv
[0])) {
488 if (fp
->deprecated_alternative
!= NULL
)
490 return fp
->func(argc
, argv
);
492 if ((strncmp(argv
[0], "no-", 3)) == 0) {
494 * User is asking if foo is unsupported, by trying to "run" the
495 * no-foo command. Strange.
497 f
.name
= argv
[0] + 3;
498 if (lh_FUNCTION_retrieve(prog
, &f
) == NULL
) {
499 BIO_printf(bio_out
, "%s\n", argv
[0]);
502 BIO_printf(bio_out
, "%s\n", argv
[0] + 3);
505 if (strcmp(argv
[0], "quit") == 0 || strcmp(argv
[0], "q") == 0 ||
506 strcmp(argv
[0], "exit") == 0 || strcmp(argv
[0], "bye") == 0)
507 /* Special value to mean "exit the program. */
508 return EXIT_THE_PROGRAM
;
510 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
515 static int function_cmp(const FUNCTION
* a
, const FUNCTION
* b
)
517 return strncmp(a
->name
, b
->name
, 8);
520 static unsigned long function_hash(const FUNCTION
* a
)
522 return OPENSSL_LH_strhash(a
->name
);
525 static int SortFnByName(const void *_f1
, const void *_f2
)
527 const FUNCTION
*f1
= _f1
;
528 const FUNCTION
*f2
= _f2
;
530 if (f1
->type
!= f2
->type
)
531 return f1
->type
- f2
->type
;
532 return strcmp(f1
->name
, f2
->name
);
535 static LHASH_OF(FUNCTION
) *prog_init(void)
537 static LHASH_OF(FUNCTION
) *ret
= NULL
;
538 static int prog_inited
= 0;
547 /* Sort alphabetically within category. For nicer help displays. */
548 for (i
= 0, f
= functions
; f
->name
!= NULL
; ++f
, ++i
)
550 qsort(functions
, i
, sizeof(*functions
), SortFnByName
);
552 if ((ret
= lh_FUNCTION_new(function_hash
, function_cmp
)) == NULL
)
555 for (f
= functions
; f
->name
!= NULL
; f
++)
556 (void)lh_FUNCTION_insert(ret
, f
);