]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3ac82faa | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
3ac82faa BM |
8 | */ |
9 | ||
d02b48c6 RE |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include <stdlib.h> | |
ec577822 BM |
13 | #include <openssl/bio.h> |
14 | #include <openssl/crypto.h> | |
682b444f | 15 | #include <openssl/trace.h> |
ec577822 BM |
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> | |
0b13e9f0 | 21 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 22 | # include <openssl/engine.h> |
0b13e9f0 | 23 | #endif |
ec577822 | 24 | #include <openssl/err.h> |
3b061a00 RS |
25 | /* Needed to get the other O_xxx flags. */ |
26 | #ifdef OPENSSL_SYS_VMS | |
27 | # include <unixio.h> | |
28 | #endif | |
7e1b7485 | 29 | #include "apps.h" |
dab2cd68 | 30 | #include "progs.h" |
7e1b7485 | 31 | |
0f113f3e MC |
32 | /* |
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. | |
37 | */ | |
0f113f3e MC |
38 | static LHASH_OF(FUNCTION) *prog_init(void); |
39 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]); | |
0f113f3e | 40 | char *default_config_file = NULL; |
d02b48c6 | 41 | |
7e1b7485 RS |
42 | BIO *bio_in = NULL; |
43 | BIO *bio_out = NULL; | |
0f113f3e | 44 | BIO *bio_err = NULL; |
7e1b7485 | 45 | |
99a7c3a7 | 46 | static void warn_deprecated(const FUNCTION *fp) |
c2ec4a16 | 47 | { |
99a7c3a7 P |
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); | |
51 | else | |
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); | |
c2ec4a16 P |
55 | BIO_printf(bio_err, "\n"); |
56 | } | |
57 | ||
3cb7c5cf | 58 | static int apps_startup(void) |
7e1b7485 | 59 | { |
ae89578b | 60 | const char *use_libctx = NULL; |
7e1b7485 RS |
61 | #ifdef SIGPIPE |
62 | signal(SIGPIPE, SIG_IGN); | |
63 | #endif | |
a0a82324 | 64 | |
b9f75707 | 65 | /* Set non-default library initialisation settings */ |
0488c0bb RL |
66 | if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN |
67 | | OPENSSL_INIT_LOAD_CONFIG, NULL)) | |
0fc32b07 | 68 | return 0; |
a0a82324 | 69 | |
f84de16f | 70 | (void)setup_ui_method(); |
0b27381f | 71 | (void)setup_engine_loader(); |
b9f75707 | 72 | |
ae89578b SL |
73 | /* |
74 | * NOTE: This is an undocumented feature required for testing only. | |
75 | * There are no guarantees that it will exist in future builds. | |
76 | */ | |
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) | |
82 | return 0; | |
83 | } | |
84 | } | |
85 | ||
a0a82324 | 86 | return 1; |
7e1b7485 RS |
87 | } |
88 | ||
3cb7c5cf | 89 | static void apps_shutdown(void) |
7e1b7485 | 90 | { |
ae89578b | 91 | app_providers_cleanup(); |
b4250010 | 92 | OSSL_LIB_CTX_free(app_get0_libctx()); |
0b27381f | 93 | destroy_engine_loader(); |
7e1b7485 | 94 | destroy_ui_method(); |
7e1b7485 RS |
95 | } |
96 | ||
0fda9f7c DMSP |
97 | |
98 | #ifndef OPENSSL_NO_TRACE | |
682b444f RL |
99 | typedef struct tracedata_st { |
100 | BIO *bio; | |
101 | unsigned int ingroup:1; | |
102 | } tracedata; | |
103 | ||
104 | static size_t internal_trace_cb(const char *buf, size_t cnt, | |
105 | int category, int cmd, void *vdata) | |
106 | { | |
13d06925 | 107 | int ret = 0; |
682b444f | 108 | tracedata *trace_data = vdata; |
2d905f67 P |
109 | char buffer[256], *hex; |
110 | CRYPTO_THREAD_ID tid; | |
682b444f RL |
111 | |
112 | switch (cmd) { | |
113 | case OSSL_TRACE_CTRL_BEGIN: | |
1b4417ab P |
114 | if (trace_data->ingroup) { |
115 | BIO_printf(bio_err, "ERROR: tracing already started\n"); | |
fe50e115 | 116 | return 0; |
1b4417ab | 117 | } |
682b444f | 118 | trace_data->ingroup = 1; |
682b444f | 119 | |
2d905f67 P |
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: ", | |
17197a2f MC |
123 | hex == NULL ? "<null>" : hex, |
124 | OSSL_trace_get_category_name(category)); | |
2d905f67 | 125 | OPENSSL_free(hex); |
e79ae962 | 126 | BIO_set_prefix(trace_data->bio, buffer); |
13d06925 DMSP |
127 | break; |
128 | case OSSL_TRACE_CTRL_WRITE: | |
1b4417ab P |
129 | if (!trace_data->ingroup) { |
130 | BIO_printf(bio_err, "ERROR: writing when tracing not started\n"); | |
fe50e115 | 131 | return 0; |
1b4417ab | 132 | } |
fe50e115 | 133 | |
13d06925 DMSP |
134 | ret = BIO_write(trace_data->bio, buf, cnt); |
135 | break; | |
136 | case OSSL_TRACE_CTRL_END: | |
1b4417ab P |
137 | if (!trace_data->ingroup) { |
138 | BIO_printf(bio_err, "ERROR: finishing when tracing not started\n"); | |
fe50e115 | 139 | return 0; |
1b4417ab | 140 | } |
13d06925 DMSP |
141 | trace_data->ingroup = 0; |
142 | ||
e79ae962 | 143 | BIO_set_prefix(trace_data->bio, NULL); |
13d06925 DMSP |
144 | |
145 | break; | |
682b444f | 146 | } |
682b444f RL |
147 | |
148 | return ret < 0 ? 0 : ret; | |
149 | } | |
150 | ||
18e1e302 RL |
151 | DEFINE_STACK_OF(tracedata) |
152 | static STACK_OF(tracedata) *trace_data_stack; | |
153 | ||
154 | static void tracedata_free(tracedata *data) | |
155 | { | |
156 | BIO_free_all(data->bio); | |
157 | OPENSSL_free(data); | |
158 | } | |
159 | ||
160 | static STACK_OF(tracedata) *trace_data_stack; | |
161 | ||
162 | static void cleanup_trace(void) | |
163 | { | |
164 | sk_tracedata_pop_free(trace_data_stack, tracedata_free); | |
165 | } | |
166 | ||
02bd2d7f DMSP |
167 | static void setup_trace_category(int category) |
168 | { | |
169 | BIO *channel; | |
170 | tracedata *trace_data; | |
171 | ||
172 | if (OSSL_trace_enabled(category)) | |
173 | return; | |
174 | ||
e79ae962 | 175 | channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT)); |
02bd2d7f DMSP |
176 | trace_data = OPENSSL_zalloc(sizeof(*trace_data)); |
177 | ||
178 | if (trace_data == NULL | |
179 | || (trace_data->bio = channel) == NULL | |
180 | || OSSL_trace_set_callback(category, internal_trace_cb, | |
181 | trace_data) == 0 | |
182 | || sk_tracedata_push(trace_data_stack, trace_data) == 0) { | |
183 | ||
184 | fprintf(stderr, | |
185 | "warning: unable to setup trace callback for category '%s'.\n", | |
186 | OSSL_trace_get_category_name(category)); | |
187 | ||
188 | OSSL_trace_set_callback(category, NULL, NULL); | |
189 | BIO_free_all(channel); | |
190 | } | |
191 | } | |
192 | ||
682b444f RL |
193 | static void setup_trace(const char *str) |
194 | { | |
195 | char *val; | |
196 | ||
ba434131 RL |
197 | /* |
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). | |
201 | */ | |
202 | atexit(cleanup_trace); | |
203 | ||
18e1e302 | 204 | trace_data_stack = sk_tracedata_new_null(); |
682b444f RL |
205 | val = OPENSSL_strdup(str); |
206 | ||
207 | if (val != NULL) { | |
208 | char *valp = val; | |
209 | char *item; | |
210 | ||
211 | for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) { | |
212 | int category = OSSL_trace_get_category_num(item); | |
213 | ||
3a8269b3 | 214 | if (category == OSSL_TRACE_CATEGORY_ALL) { |
02bd2d7f DMSP |
215 | while (++category < OSSL_TRACE_CATEGORY_NUM) |
216 | setup_trace_category(category); | |
217 | break; | |
218 | } else if (category > 0) { | |
219 | setup_trace_category(category); | |
682b444f RL |
220 | } else { |
221 | fprintf(stderr, | |
02bd2d7f | 222 | "warning: unknown trace category: '%s'.\n", item); |
682b444f RL |
223 | } |
224 | } | |
225 | } | |
226 | ||
227 | OPENSSL_free(val); | |
228 | } | |
0fda9f7c | 229 | #endif /* OPENSSL_NO_TRACE */ |
682b444f | 230 | |
5b286641 | 231 | static char *help_argv[] = { "help", NULL }; |
96786ad1 | 232 | |
7e1b7485 | 233 | int main(int argc, char *argv[]) |
0f113f3e | 234 | { |
0f113f3e | 235 | FUNCTION f, *fp; |
0f113f3e | 236 | LHASH_OF(FUNCTION) *prog = NULL; |
eca47139 | 237 | char *pname; |
33720392 | 238 | const char *fname; |
7e1b7485 | 239 | ARGS arg; |
678cae02 | 240 | int global_help = 0; |
eca47139 | 241 | int ret = 0; |
8ecef24a | 242 | |
7e1b7485 RS |
243 | arg.argv = NULL; |
244 | arg.size = 0; | |
245 | ||
7768e116 | 246 | /* Set up some of the environment. */ |
a60994df RL |
247 | bio_in = dup_bio_in(FORMAT_TEXT); |
248 | bio_out = dup_bio_out(FORMAT_TEXT); | |
149bd5d6 | 249 | bio_err = dup_bio_err(FORMAT_TEXT); |
7768e116 | 250 | |
368058d0 | 251 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
9b542d72 | 252 | argv = copy_argv(&argc, argv); |
4e155ec4 | 253 | #elif defined(_WIN32) |
33720392 | 254 | /* Replace argv[] with UTF-8 encoded strings. */ |
4e155ec4 | 255 | win32_utf8argv(&argc, &argv); |
7e1b7485 RS |
256 | #endif |
257 | ||
0fda9f7c | 258 | #ifndef OPENSSL_NO_TRACE |
682b444f | 259 | setup_trace(getenv("OPENSSL_TRACE")); |
0fda9f7c | 260 | #endif |
682b444f | 261 | |
33720392 DDO |
262 | if ((fname = "apps_startup", !apps_startup()) |
263 | || (fname = "prog_init", (prog = prog_init()) == NULL)) { | |
acc7b9fb | 264 | BIO_printf(bio_err, |
33720392 DDO |
265 | "FATAL: Startup failure (dev note: %s()) for %s\n", |
266 | fname, argv[0]); | |
acc7b9fb P |
267 | ERR_print_errors(bio_err); |
268 | ret = 1; | |
269 | goto end; | |
270 | } | |
368058d0 | 271 | pname = opt_progname(argv[0]); |
0f113f3e | 272 | |
e306f83c RL |
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); | |
276 | ||
0f113f3e | 277 | /* first check the program name */ |
0f113f3e MC |
278 | f.name = pname; |
279 | fp = lh_FUNCTION_retrieve(prog, &f); | |
eca47139 | 280 | if (fp == NULL) { |
678cae02 DDO |
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); | |
7e1b7485 | 285 | argc--; |
368058d0 | 286 | argv++; |
678cae02 | 287 | opt_appname(argc == 1 || global_help ? "help" : argv[0]); |
33720392 DDO |
288 | } else { |
289 | argv[0] = pname; | |
0f113f3e MC |
290 | } |
291 | ||
eca47139 | 292 | /* If there's a command, run with that, otherwise "help". */ |
678cae02 DDO |
293 | ret = argc == 0 || global_help |
294 | ? do_cmd(prog, 1, help_argv) | |
295 | : do_cmd(prog, argc, argv); | |
57d5edad | 296 | |
0f113f3e | 297 | end: |
cc01d217 | 298 | OPENSSL_free(default_config_file); |
25aaa98a | 299 | lh_FUNCTION_free(prog); |
b548a1f1 | 300 | OPENSSL_free(arg.argv); |
3ee1eac2 | 301 | app_RAND_write(); |
0f113f3e | 302 | |
7e1b7485 RS |
303 | BIO_free(bio_in); |
304 | BIO_free_all(bio_out); | |
0f113f3e | 305 | apps_shutdown(); |
ca3a82c3 | 306 | BIO_free(bio_err); |
aa147792 | 307 | EXIT(ret); |
7e1b7485 RS |
308 | } |
309 | ||
f3b3d7f0 RS |
310 | typedef enum HELP_CHOICE { |
311 | OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP | |
312 | } HELP_CHOICE; | |
313 | ||
44c83ebd | 314 | const OPTIONS help_options[] = { |
92de469f | 315 | {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"}, |
5388f986 RS |
316 | |
317 | OPT_SECTION("General"), | |
f3b3d7f0 | 318 | {"help", OPT_hHELP, '-', "Display this summary"}, |
92de469f RS |
319 | |
320 | OPT_PARAMETERS(), | |
321 | {"command", 0, 0, "Name of command to display help (optional)"}, | |
7e1b7485 RS |
322 | {NULL} |
323 | }; | |
324 | ||
f3b3d7f0 | 325 | |
7e1b7485 RS |
326 | int help_main(int argc, char **argv) |
327 | { | |
328 | FUNCTION *fp; | |
329 | int i, nl; | |
330 | FUNC_TYPE tp; | |
331 | char *prog; | |
f3b3d7f0 | 332 | HELP_CHOICE o; |
296cbb57 | 333 | DISPLAY_COLUMNS dc; |
b75f08cb SL |
334 | char *new_argv[3]; |
335 | ||
7e1b7485 | 336 | prog = opt_init(argc, argv, help_options); |
f3b3d7f0 | 337 | while ((o = opt_next()) != OPT_hEOF) { |
7e1b7485 | 338 | switch (o) { |
f3b3d7f0 RS |
339 | case OPT_hERR: |
340 | case OPT_hEOF: | |
7e1b7485 RS |
341 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
342 | return 1; | |
f3b3d7f0 | 343 | case OPT_hHELP: |
7e1b7485 RS |
344 | opt_help(help_options); |
345 | return 0; | |
346 | } | |
347 | } | |
7e1b7485 | 348 | |
021410ea | 349 | /* One optional argument, the command to get help for. */ |
da6be198 | 350 | if (opt_num_rest() == 1) { |
da6be198 RL |
351 | new_argv[0] = opt_rest()[0]; |
352 | new_argv[1] = "--help"; | |
353 | new_argv[2] = NULL; | |
354 | return do_cmd(prog_init(), 2, new_argv); | |
355 | } | |
5d94e5b6 | 356 | if (opt_num_rest() != 0) { |
7e1b7485 RS |
357 | BIO_printf(bio_err, "Usage: %s\n", prog); |
358 | return 1; | |
359 | } | |
360 | ||
753149d9 | 361 | calculate_columns(functions, &dc); |
33720392 | 362 | BIO_printf(bio_err, "%s:\n\nStandard commands", prog); |
7e1b7485 RS |
363 | i = 0; |
364 | tp = FT_none; | |
365 | for (fp = functions; fp->name != NULL; fp++) { | |
366 | nl = 0; | |
296cbb57 | 367 | if (i++ % dc.columns == 0) { |
7e1b7485 RS |
368 | BIO_printf(bio_err, "\n"); |
369 | nl = 1; | |
370 | } | |
371 | if (fp->type != tp) { | |
372 | tp = fp->type; | |
373 | if (!nl) | |
374 | BIO_printf(bio_err, "\n"); | |
375 | if (tp == FT_md) { | |
376 | i = 1; | |
377 | BIO_printf(bio_err, | |
378 | "\nMessage Digest commands (see the `dgst' command for more details)\n"); | |
379 | } else if (tp == FT_cipher) { | |
380 | i = 1; | |
381 | BIO_printf(bio_err, | |
382 | "\nCipher commands (see the `enc' command for more details)\n"); | |
383 | } | |
384 | } | |
296cbb57 | 385 | BIO_printf(bio_err, "%-*s", dc.width, fp->name); |
7e1b7485 RS |
386 | } |
387 | BIO_printf(bio_err, "\n\n"); | |
388 | return 0; | |
389 | } | |
8c00f4cf | 390 | |
3c1d6bbc | 391 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]) |
0f113f3e MC |
392 | { |
393 | FUNCTION f, *fp; | |
0f113f3e | 394 | |
7e1b7485 | 395 | if (argc <= 0 || argv[0] == NULL) |
26a7d938 | 396 | return 0; |
0f113f3e MC |
397 | f.name = argv[0]; |
398 | fp = lh_FUNCTION_retrieve(prog, &f); | |
399 | if (fp == NULL) { | |
400 | if (EVP_get_digestbyname(argv[0])) { | |
7e1b7485 | 401 | f.type = FT_md; |
0f113f3e MC |
402 | f.func = dgst_main; |
403 | fp = &f; | |
404 | } else if (EVP_get_cipherbyname(argv[0])) { | |
7e1b7485 | 405 | f.type = FT_cipher; |
0f113f3e MC |
406 | f.func = enc_main; |
407 | fp = &f; | |
408 | } | |
409 | } | |
410 | if (fp != NULL) { | |
c2ec4a16 | 411 | if (fp->deprecated_alternative != NULL) |
99a7c3a7 | 412 | warn_deprecated(fp); |
26a7d938 | 413 | return fp->func(argc, argv); |
7e1b7485 RS |
414 | } |
415 | if ((strncmp(argv[0], "no-", 3)) == 0) { | |
416 | /* | |
417 | * User is asking if foo is unsupported, by trying to "run" the | |
418 | * no-foo command. Strange. | |
419 | */ | |
0f113f3e | 420 | f.name = argv[0] + 3; |
7e1b7485 RS |
421 | if (lh_FUNCTION_retrieve(prog, &f) == NULL) { |
422 | BIO_printf(bio_out, "%s\n", argv[0]); | |
26a7d938 | 423 | return 0; |
0f113f3e | 424 | } |
7e1b7485 RS |
425 | BIO_printf(bio_out, "%s\n", argv[0] + 3); |
426 | return 1; | |
50acf46b | 427 | } |
0f113f3e | 428 | |
7e1b7485 RS |
429 | BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n", |
430 | argv[0]); | |
208fb891 | 431 | return 1; |
0f113f3e | 432 | } |
50acf46b | 433 | |
0f113f3e MC |
434 | static int function_cmp(const FUNCTION * a, const FUNCTION * b) |
435 | { | |
436 | return strncmp(a->name, b->name, 8); | |
437 | } | |
50acf46b | 438 | |
0f113f3e MC |
439 | static unsigned long function_hash(const FUNCTION * a) |
440 | { | |
739a1eb1 | 441 | return OPENSSL_LH_strhash(a->name); |
0f113f3e | 442 | } |
d02b48c6 | 443 | |
7e1b7485 RS |
444 | static int SortFnByName(const void *_f1, const void *_f2) |
445 | { | |
446 | const FUNCTION *f1 = _f1; | |
447 | const FUNCTION *f2 = _f2; | |
448 | ||
449 | if (f1->type != f2->type) | |
450 | return f1->type - f2->type; | |
451 | return strcmp(f1->name, f2->name); | |
452 | } | |
453 | ||
0f113f3e MC |
454 | static LHASH_OF(FUNCTION) *prog_init(void) |
455 | { | |
391e987e RL |
456 | static LHASH_OF(FUNCTION) *ret = NULL; |
457 | static int prog_inited = 0; | |
0f113f3e MC |
458 | FUNCTION *f; |
459 | size_t i; | |
460 | ||
391e987e RL |
461 | if (prog_inited) |
462 | return ret; | |
463 | ||
464 | prog_inited = 1; | |
465 | ||
7e1b7485 | 466 | /* Sort alphabetically within category. For nicer help displays. */ |
391e987e RL |
467 | for (i = 0, f = functions; f->name != NULL; ++f, ++i) |
468 | ; | |
b4faea50 | 469 | qsort(functions, i, sizeof(*functions), SortFnByName); |
0f113f3e | 470 | |
62d0577e | 471 | if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL) |
26a7d938 | 472 | return NULL; |
0f113f3e MC |
473 | |
474 | for (f = functions; f->name != NULL; f++) | |
475 | (void)lh_FUNCTION_insert(ret, f); | |
296cbb57 | 476 | return ret; |
0f113f3e | 477 | } |