openssl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright 1995-2021 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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "internal/common.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/trace.h>
  15. #include <openssl/lhash.h>
  16. #include <openssl/conf.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/ssl.h>
  20. #ifndef OPENSSL_NO_ENGINE
  21. # include <openssl/engine.h>
  22. #endif
  23. #include <openssl/err.h>
  24. /* Needed to get the other O_xxx flags. */
  25. #ifdef OPENSSL_SYS_VMS
  26. # include <unixio.h>
  27. #endif
  28. #include "apps.h"
  29. #include "progs.h"
  30. /*
  31. * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
  32. * the base prototypes (we cast each variable inside the function to the
  33. * required type of "FUNCTION*"). This removes the necessity for
  34. * macro-generated wrapper functions.
  35. */
  36. static LHASH_OF(FUNCTION) *prog_init(void);
  37. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
  38. char *default_config_file = NULL;
  39. BIO *bio_in = NULL;
  40. BIO *bio_out = NULL;
  41. BIO *bio_err = NULL;
  42. static void warn_deprecated(const FUNCTION *fp)
  43. {
  44. if (fp->deprecated_version != NULL)
  45. BIO_printf(bio_err, "The command %s was deprecated in version %s.",
  46. fp->name, fp->deprecated_version);
  47. else
  48. BIO_printf(bio_err, "The command %s is deprecated.", fp->name);
  49. if (strcmp(fp->deprecated_alternative, DEPRECATED_NO_ALTERNATIVE) != 0)
  50. BIO_printf(bio_err, " Use '%s' instead.", fp->deprecated_alternative);
  51. BIO_printf(bio_err, "\n");
  52. }
  53. static int apps_startup(void)
  54. {
  55. const char *use_libctx = NULL;
  56. #ifdef SIGPIPE
  57. signal(SIGPIPE, SIG_IGN);
  58. #endif
  59. /* Set non-default library initialisation settings */
  60. if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
  61. | OPENSSL_INIT_LOAD_CONFIG, NULL))
  62. return 0;
  63. (void)setup_ui_method();
  64. (void)setup_engine_loader();
  65. /*
  66. * NOTE: This is an undocumented feature required for testing only.
  67. * There are no guarantees that it will exist in future builds.
  68. */
  69. use_libctx = getenv("OPENSSL_TEST_LIBCTX");
  70. if (use_libctx != NULL) {
  71. /* Set this to "1" to create a global libctx */
  72. if (strcmp(use_libctx, "1") == 0) {
  73. if (app_create_libctx() == NULL)
  74. return 0;
  75. }
  76. }
  77. return 1;
  78. }
  79. static void apps_shutdown(void)
  80. {
  81. app_providers_cleanup();
  82. OSSL_LIB_CTX_free(app_get0_libctx());
  83. destroy_engine_loader();
  84. destroy_ui_method();
  85. }
  86. #ifndef OPENSSL_NO_TRACE
  87. typedef struct tracedata_st {
  88. BIO *bio;
  89. unsigned int ingroup:1;
  90. } tracedata;
  91. static size_t internal_trace_cb(const char *buf, size_t cnt,
  92. int category, int cmd, void *vdata)
  93. {
  94. int ret = 0;
  95. tracedata *trace_data = vdata;
  96. char buffer[256], *hex;
  97. CRYPTO_THREAD_ID tid;
  98. switch (cmd) {
  99. case OSSL_TRACE_CTRL_BEGIN:
  100. if (trace_data->ingroup) {
  101. BIO_printf(bio_err, "ERROR: tracing already started\n");
  102. return 0;
  103. }
  104. trace_data->ingroup = 1;
  105. tid = CRYPTO_THREAD_get_current_id();
  106. hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
  107. BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
  108. hex == NULL ? "<null>" : hex,
  109. OSSL_trace_get_category_name(category));
  110. OPENSSL_free(hex);
  111. BIO_set_prefix(trace_data->bio, buffer);
  112. break;
  113. case OSSL_TRACE_CTRL_WRITE:
  114. if (!trace_data->ingroup) {
  115. BIO_printf(bio_err, "ERROR: writing when tracing not started\n");
  116. return 0;
  117. }
  118. ret = BIO_write(trace_data->bio, buf, cnt);
  119. break;
  120. case OSSL_TRACE_CTRL_END:
  121. if (!trace_data->ingroup) {
  122. BIO_printf(bio_err, "ERROR: finishing when tracing not started\n");
  123. return 0;
  124. }
  125. trace_data->ingroup = 0;
  126. BIO_set_prefix(trace_data->bio, NULL);
  127. break;
  128. }
  129. return ret < 0 ? 0 : ret;
  130. }
  131. DEFINE_STACK_OF(tracedata)
  132. static STACK_OF(tracedata) *trace_data_stack;
  133. static void tracedata_free(tracedata *data)
  134. {
  135. BIO_free_all(data->bio);
  136. OPENSSL_free(data);
  137. }
  138. static STACK_OF(tracedata) *trace_data_stack;
  139. static void cleanup_trace(void)
  140. {
  141. sk_tracedata_pop_free(trace_data_stack, tracedata_free);
  142. }
  143. static void setup_trace_category(int category)
  144. {
  145. BIO *channel;
  146. tracedata *trace_data;
  147. BIO *bio = NULL;
  148. if (OSSL_trace_enabled(category))
  149. return;
  150. bio = BIO_new(BIO_f_prefix());
  151. channel = BIO_push(bio, dup_bio_err(FORMAT_TEXT));
  152. trace_data = OPENSSL_zalloc(sizeof(*trace_data));
  153. if (trace_data == NULL
  154. || bio == NULL
  155. || (trace_data->bio = channel) == NULL
  156. || OSSL_trace_set_callback(category, internal_trace_cb,
  157. trace_data) == 0
  158. || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
  159. fprintf(stderr,
  160. "warning: unable to setup trace callback for category '%s'.\n",
  161. OSSL_trace_get_category_name(category));
  162. OSSL_trace_set_callback(category, NULL, NULL);
  163. BIO_free_all(channel);
  164. }
  165. }
  166. static void setup_trace(const char *str)
  167. {
  168. char *val;
  169. /*
  170. * We add this handler as early as possible to ensure it's executed
  171. * as late as possible, i.e. after the TRACE code has done its cleanup
  172. * (which happens last in OPENSSL_cleanup).
  173. */
  174. atexit(cleanup_trace);
  175. trace_data_stack = sk_tracedata_new_null();
  176. val = OPENSSL_strdup(str);
  177. if (val != NULL) {
  178. char *valp = val;
  179. char *item;
  180. for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
  181. int category = OSSL_trace_get_category_num(item);
  182. if (category == OSSL_TRACE_CATEGORY_ALL) {
  183. while (++category < OSSL_TRACE_CATEGORY_NUM)
  184. setup_trace_category(category);
  185. break;
  186. } else if (category > 0) {
  187. setup_trace_category(category);
  188. } else {
  189. fprintf(stderr,
  190. "warning: unknown trace category: '%s'.\n", item);
  191. }
  192. }
  193. }
  194. OPENSSL_free(val);
  195. }
  196. #endif /* OPENSSL_NO_TRACE */
  197. static char *help_argv[] = { "help", NULL };
  198. int main(int argc, char *argv[])
  199. {
  200. FUNCTION f, *fp;
  201. LHASH_OF(FUNCTION) *prog = NULL;
  202. char *pname;
  203. const char *fname;
  204. ARGS arg;
  205. int global_help = 0;
  206. int ret = 0;
  207. arg.argv = NULL;
  208. arg.size = 0;
  209. /* Set up some of the environment. */
  210. bio_in = dup_bio_in(FORMAT_TEXT);
  211. bio_out = dup_bio_out(FORMAT_TEXT);
  212. bio_err = dup_bio_err(FORMAT_TEXT);
  213. #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
  214. argv = copy_argv(&argc, argv);
  215. #elif defined(_WIN32)
  216. /* Replace argv[] with UTF-8 encoded strings. */
  217. win32_utf8argv(&argc, &argv);
  218. #endif
  219. #ifndef OPENSSL_NO_TRACE
  220. setup_trace(getenv("OPENSSL_TRACE"));
  221. #endif
  222. if ((fname = "apps_startup", !apps_startup())
  223. || (fname = "prog_init", (prog = prog_init()) == NULL)) {
  224. BIO_printf(bio_err,
  225. "FATAL: Startup failure (dev note: %s()) for %s\n",
  226. fname, argv[0]);
  227. ERR_print_errors(bio_err);
  228. ret = 1;
  229. goto end;
  230. }
  231. pname = opt_progname(argv[0]);
  232. default_config_file = CONF_get1_default_config_file();
  233. if (default_config_file == NULL)
  234. app_bail_out("%s: could not get default config file\n", pname);
  235. /* first check the program name */
  236. f.name = pname;
  237. fp = lh_FUNCTION_retrieve(prog, &f);
  238. if (fp == NULL) {
  239. /* We assume we've been called as 'openssl ...' */
  240. global_help = argc > 1
  241. && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0
  242. || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0);
  243. argc--;
  244. argv++;
  245. opt_appname(argc == 1 || global_help ? "help" : argv[0]);
  246. } else {
  247. argv[0] = pname;
  248. }
  249. /* If there's a command, run with that, otherwise "help". */
  250. ret = argc == 0 || global_help
  251. ? do_cmd(prog, 1, help_argv)
  252. : do_cmd(prog, argc, argv);
  253. end:
  254. OPENSSL_free(default_config_file);
  255. lh_FUNCTION_free(prog);
  256. OPENSSL_free(arg.argv);
  257. if (!app_RAND_write())
  258. ret = EXIT_FAILURE;
  259. BIO_free(bio_in);
  260. BIO_free_all(bio_out);
  261. apps_shutdown();
  262. BIO_free_all(bio_err);
  263. EXIT(ret);
  264. }
  265. typedef enum HELP_CHOICE {
  266. OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
  267. } HELP_CHOICE;
  268. const OPTIONS help_options[] = {
  269. {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
  270. OPT_SECTION("General"),
  271. {"help", OPT_hHELP, '-', "Display this summary"},
  272. OPT_PARAMETERS(),
  273. {"command", 0, 0, "Name of command to display help (optional)"},
  274. {NULL}
  275. };
  276. int help_main(int argc, char **argv)
  277. {
  278. FUNCTION *fp;
  279. int i, nl;
  280. FUNC_TYPE tp;
  281. char *prog;
  282. HELP_CHOICE o;
  283. DISPLAY_COLUMNS dc;
  284. char *new_argv[3];
  285. prog = opt_init(argc, argv, help_options);
  286. while ((o = opt_next()) != OPT_hEOF) {
  287. switch (o) {
  288. case OPT_hERR:
  289. case OPT_hEOF:
  290. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  291. return 1;
  292. case OPT_hHELP:
  293. opt_help(help_options);
  294. return 0;
  295. }
  296. }
  297. /* One optional argument, the command to get help for. */
  298. if (opt_num_rest() == 1) {
  299. new_argv[0] = opt_rest()[0];
  300. new_argv[1] = "--help";
  301. new_argv[2] = NULL;
  302. return do_cmd(prog_init(), 2, new_argv);
  303. }
  304. if (!opt_check_rest_arg(NULL)) {
  305. BIO_printf(bio_err, "Usage: %s\n", prog);
  306. return 1;
  307. }
  308. calculate_columns(functions, &dc);
  309. BIO_printf(bio_err, "%s:\n\nStandard commands", prog);
  310. i = 0;
  311. tp = FT_none;
  312. for (fp = functions; fp->name != NULL; fp++) {
  313. nl = 0;
  314. if (i++ % dc.columns == 0) {
  315. BIO_printf(bio_err, "\n");
  316. nl = 1;
  317. }
  318. if (fp->type != tp) {
  319. tp = fp->type;
  320. if (!nl)
  321. BIO_printf(bio_err, "\n");
  322. if (tp == FT_md) {
  323. i = 1;
  324. BIO_printf(bio_err,
  325. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  326. } else if (tp == FT_cipher) {
  327. i = 1;
  328. BIO_printf(bio_err,
  329. "\nCipher commands (see the `enc' command for more details)\n");
  330. }
  331. }
  332. BIO_printf(bio_err, "%-*s", dc.width, fp->name);
  333. }
  334. BIO_printf(bio_err, "\n\n");
  335. return 0;
  336. }
  337. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
  338. {
  339. FUNCTION f, *fp;
  340. if (argc <= 0 || argv[0] == NULL)
  341. return 0;
  342. memset(&f, 0, sizeof(f));
  343. f.name = argv[0];
  344. fp = lh_FUNCTION_retrieve(prog, &f);
  345. if (fp == NULL) {
  346. if (EVP_get_digestbyname(argv[0])) {
  347. f.type = FT_md;
  348. f.func = dgst_main;
  349. fp = &f;
  350. } else if (EVP_get_cipherbyname(argv[0])) {
  351. f.type = FT_cipher;
  352. f.func = enc_main;
  353. fp = &f;
  354. }
  355. }
  356. if (fp != NULL) {
  357. if (fp->deprecated_alternative != NULL)
  358. warn_deprecated(fp);
  359. return fp->func(argc, argv);
  360. }
  361. f.name = argv[0];
  362. if (CHECK_AND_SKIP_PREFIX(f.name, "no-")) {
  363. /*
  364. * User is asking if foo is unsupported, by trying to "run" the
  365. * no-foo command. Strange.
  366. */
  367. if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
  368. BIO_printf(bio_out, "%s\n", argv[0]);
  369. return 0;
  370. }
  371. BIO_printf(bio_out, "%s\n", argv[0] + 3);
  372. return 1;
  373. }
  374. BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
  375. argv[0]);
  376. return 1;
  377. }
  378. static int function_cmp(const FUNCTION * a, const FUNCTION * b)
  379. {
  380. return strncmp(a->name, b->name, 8);
  381. }
  382. static unsigned long function_hash(const FUNCTION * a)
  383. {
  384. return OPENSSL_LH_strhash(a->name);
  385. }
  386. static int SortFnByName(const void *_f1, const void *_f2)
  387. {
  388. const FUNCTION *f1 = _f1;
  389. const FUNCTION *f2 = _f2;
  390. if (f1->type != f2->type)
  391. return f1->type - f2->type;
  392. return strcmp(f1->name, f2->name);
  393. }
  394. static LHASH_OF(FUNCTION) *prog_init(void)
  395. {
  396. static LHASH_OF(FUNCTION) *ret = NULL;
  397. static int prog_inited = 0;
  398. FUNCTION *f;
  399. size_t i;
  400. if (prog_inited)
  401. return ret;
  402. prog_inited = 1;
  403. /* Sort alphabetically within category. For nicer help displays. */
  404. for (i = 0, f = functions; f->name != NULL; ++f, ++i)
  405. ;
  406. qsort(functions, i, sizeof(*functions), SortFnByName);
  407. if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
  408. return NULL;
  409. for (f = functions; f->name != NULL; f++)
  410. (void)lh_FUNCTION_insert(ret, f);
  411. return ret;
  412. }