]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/openssl.c
apps: avoid memory overrun.
[thirdparty/openssl.git] / apps / openssl.c
1 /*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <internal/cryptlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.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>
24 #endif
25 #include <openssl/err.h>
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
28 # include <unixio.h>
29 #endif
30 #include "apps.h"
31 #include "progs.h"
32
33 /*
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.
38 */
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;
42
43 BIO *bio_in = NULL;
44 BIO *bio_out = NULL;
45 BIO *bio_err = NULL;
46
47 static void warn_deprecated(const FUNCTION *fp)
48 {
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);
52 else
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");
57 }
58
59 static int apps_startup(void)
60 {
61 #ifdef SIGPIPE
62 signal(SIGPIPE, SIG_IGN);
63 #endif
64
65 /* Set non-default library initialisation settings */
66 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
67 | OPENSSL_INIT_LOAD_CONFIG, NULL))
68 return 0;
69
70 setup_ui_method();
71
72 return 1;
73 }
74
75 static void apps_shutdown(void)
76 {
77 destroy_ui_method();
78 }
79
80
81 #ifndef OPENSSL_NO_TRACE
82 typedef struct tracedata_st {
83 BIO *bio;
84 unsigned int ingroup:1;
85 } tracedata;
86
87 static size_t internal_trace_cb(const char *buf, size_t cnt,
88 int category, int cmd, void *vdata)
89 {
90 int ret = 0;
91 tracedata *trace_data = vdata;
92 char buffer[256], *hex;
93 CRYPTO_THREAD_ID tid;
94
95 switch (cmd) {
96 case OSSL_TRACE_CTRL_BEGIN:
97 if (!ossl_assert(!trace_data->ingroup))
98 return 0;
99 trace_data->ingroup = 1;
100
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));
106 OPENSSL_free(hex);
107 BIO_set_prefix(trace_data->bio, buffer);
108 break;
109 case OSSL_TRACE_CTRL_WRITE:
110 if (!ossl_assert(trace_data->ingroup))
111 return 0;
112
113 ret = BIO_write(trace_data->bio, buf, cnt);
114 break;
115 case OSSL_TRACE_CTRL_END:
116 if (!ossl_assert(trace_data->ingroup))
117 return 0;
118 trace_data->ingroup = 0;
119
120 BIO_set_prefix(trace_data->bio, NULL);
121
122 break;
123 }
124
125 return ret < 0 ? 0 : ret;
126 }
127
128 DEFINE_STACK_OF(tracedata)
129 static STACK_OF(tracedata) *trace_data_stack;
130
131 static void tracedata_free(tracedata *data)
132 {
133 BIO_free_all(data->bio);
134 OPENSSL_free(data);
135 }
136
137 static STACK_OF(tracedata) *trace_data_stack;
138
139 static void cleanup_trace(void)
140 {
141 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
142 }
143
144 static void setup_trace_category(int category)
145 {
146 BIO *channel;
147 tracedata *trace_data;
148
149 if (OSSL_trace_enabled(category))
150 return;
151
152 channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
153 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
154
155 if (trace_data == NULL
156 || (trace_data->bio = channel) == NULL
157 || OSSL_trace_set_callback(category, internal_trace_cb,
158 trace_data) == 0
159 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
160
161 fprintf(stderr,
162 "warning: unable to setup trace callback for category '%s'.\n",
163 OSSL_trace_get_category_name(category));
164
165 OSSL_trace_set_callback(category, NULL, NULL);
166 BIO_free_all(channel);
167 }
168 }
169
170 static void setup_trace(const char *str)
171 {
172 char *val;
173
174 /*
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).
178 */
179 atexit(cleanup_trace);
180
181 trace_data_stack = sk_tracedata_new_null();
182 val = OPENSSL_strdup(str);
183
184 if (val != NULL) {
185 char *valp = val;
186 char *item;
187
188 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
189 int category = OSSL_trace_get_category_num(item);
190
191 if (category == OSSL_TRACE_CATEGORY_ALL) {
192 while (++category < OSSL_TRACE_CATEGORY_NUM)
193 setup_trace_category(category);
194 break;
195 } else if (category > 0) {
196 setup_trace_category(category);
197 } else {
198 fprintf(stderr,
199 "warning: unknown trace category: '%s'.\n", item);
200 }
201 }
202 }
203
204 OPENSSL_free(val);
205 }
206 #endif /* OPENSSL_NO_TRACE */
207
208 static char *help_argv[] = { "help", NULL };
209
210 int main(int argc, char *argv[])
211 {
212 FUNCTION f, *fp;
213 LHASH_OF(FUNCTION) *prog = NULL;
214 char *pname;
215 ARGS arg;
216 int ret = 0;
217
218 arg.argv = NULL;
219 arg.size = 0;
220
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);
225
226 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
227 argv = copy_argv(&argc, argv);
228 #elif defined(_WIN32)
229 /*
230 * Replace argv[] with UTF-8 encoded strings.
231 */
232 win32_utf8argv(&argc, &argv);
233 #endif
234
235 #ifndef OPENSSL_NO_TRACE
236 setup_trace(getenv("OPENSSL_TRACE"));
237 #endif
238
239 if (!apps_startup()) {
240 BIO_printf(bio_err,
241 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
242 ERR_print_errors(bio_err);
243 ret = 1;
244 goto end;
245 }
246
247 prog = prog_init();
248 if (prog == NULL) {
249 BIO_printf(bio_err,
250 "FATAL: Startup failure (dev note: prog_init() failed)\n");
251 ERR_print_errors(bio_err);
252 ret = 1;
253 goto end;
254 }
255 pname = opt_progname(argv[0]);
256
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);
260
261 /* first check the program name */
262 f.name = pname;
263 fp = lh_FUNCTION_retrieve(prog, &f);
264 if (fp == NULL) {
265 /* We assume we've been called as 'openssl cmd' */
266 argc--;
267 argv++;
268 }
269
270 /* If there's a command, run with that, otherwise "help". */
271 ret = argc > 0
272 ? do_cmd(prog, argc, argv)
273 : do_cmd(prog, 1, help_argv);
274
275 end:
276 app_providers_cleanup();
277 OPENSSL_free(default_config_file);
278 lh_FUNCTION_free(prog);
279 OPENSSL_free(arg.argv);
280 app_RAND_write();
281
282 BIO_free(bio_in);
283 BIO_free_all(bio_out);
284 apps_shutdown();
285 BIO_free(bio_err);
286 EXIT(ret);
287 }
288
289 typedef enum HELP_CHOICE {
290 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
291 } HELP_CHOICE;
292
293 const OPTIONS help_options[] = {
294 {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
295
296 OPT_SECTION("General"),
297 {"help", OPT_hHELP, '-', "Display this summary"},
298
299 OPT_PARAMETERS(),
300 {"command", 0, 0, "Name of command to display help (optional)"},
301 {NULL}
302 };
303
304
305 int help_main(int argc, char **argv)
306 {
307 FUNCTION *fp;
308 int i, nl;
309 FUNC_TYPE tp;
310 char *prog;
311 HELP_CHOICE o;
312 DISPLAY_COLUMNS dc;
313 char *new_argv[3];
314
315 if (argc == 0) {
316 new_argv[0] = "help";
317 new_argv[1] = NULL;
318 return do_cmd(prog_init(), 1, new_argv);
319 }
320
321 prog = opt_init(argc, argv, help_options);
322 while ((o = opt_next()) != OPT_hEOF) {
323 switch (o) {
324 case OPT_hERR:
325 case OPT_hEOF:
326 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
327 return 1;
328 case OPT_hHELP:
329 opt_help(help_options);
330 return 0;
331 }
332 }
333
334 if (opt_num_rest() == 1) {
335 new_argv[0] = opt_rest()[0];
336 new_argv[1] = "--help";
337 new_argv[2] = NULL;
338 return do_cmd(prog_init(), 2, new_argv);
339 }
340 if (opt_num_rest() != 0) {
341 BIO_printf(bio_err, "Usage: %s\n", prog);
342 return 1;
343 }
344
345 calculate_columns(functions, &dc);
346 BIO_printf(bio_err, "Standard commands");
347 i = 0;
348 tp = FT_none;
349 for (fp = functions; fp->name != NULL; fp++) {
350 nl = 0;
351 if (i++ % dc.columns == 0) {
352 BIO_printf(bio_err, "\n");
353 nl = 1;
354 }
355 if (fp->type != tp) {
356 tp = fp->type;
357 if (!nl)
358 BIO_printf(bio_err, "\n");
359 if (tp == FT_md) {
360 i = 1;
361 BIO_printf(bio_err,
362 "\nMessage Digest commands (see the `dgst' command for more details)\n");
363 } else if (tp == FT_cipher) {
364 i = 1;
365 BIO_printf(bio_err,
366 "\nCipher commands (see the `enc' command for more details)\n");
367 }
368 }
369 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
370 }
371 BIO_printf(bio_err, "\n\n");
372 return 0;
373 }
374
375 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
376 {
377 FUNCTION f, *fp;
378
379 if (argc <= 0 || argv[0] == NULL)
380 return 0;
381 f.name = argv[0];
382 fp = lh_FUNCTION_retrieve(prog, &f);
383 if (fp == NULL) {
384 if (EVP_get_digestbyname(argv[0])) {
385 f.type = FT_md;
386 f.func = dgst_main;
387 fp = &f;
388 } else if (EVP_get_cipherbyname(argv[0])) {
389 f.type = FT_cipher;
390 f.func = enc_main;
391 fp = &f;
392 }
393 }
394 if (fp != NULL) {
395 if (fp->deprecated_alternative != NULL)
396 warn_deprecated(fp);
397 return fp->func(argc, argv);
398 }
399 if ((strncmp(argv[0], "no-", 3)) == 0) {
400 /*
401 * User is asking if foo is unsupported, by trying to "run" the
402 * no-foo command. Strange.
403 */
404 f.name = argv[0] + 3;
405 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
406 BIO_printf(bio_out, "%s\n", argv[0]);
407 return 0;
408 }
409 BIO_printf(bio_out, "%s\n", argv[0] + 3);
410 return 1;
411 }
412
413 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
414 argv[0]);
415 return 1;
416 }
417
418 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
419 {
420 return strncmp(a->name, b->name, 8);
421 }
422
423 static unsigned long function_hash(const FUNCTION * a)
424 {
425 return OPENSSL_LH_strhash(a->name);
426 }
427
428 static int SortFnByName(const void *_f1, const void *_f2)
429 {
430 const FUNCTION *f1 = _f1;
431 const FUNCTION *f2 = _f2;
432
433 if (f1->type != f2->type)
434 return f1->type - f2->type;
435 return strcmp(f1->name, f2->name);
436 }
437
438 static LHASH_OF(FUNCTION) *prog_init(void)
439 {
440 static LHASH_OF(FUNCTION) *ret = NULL;
441 static int prog_inited = 0;
442 FUNCTION *f;
443 size_t i;
444
445 if (prog_inited)
446 return ret;
447
448 prog_inited = 1;
449
450 /* Sort alphabetically within category. For nicer help displays. */
451 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
452 ;
453 qsort(functions, i, sizeof(*functions), SortFnByName);
454
455 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
456 return NULL;
457
458 for (f = functions; f->name != NULL; f++)
459 (void)lh_FUNCTION_insert(ret, f);
460 return ret;
461 }