openssl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <string.h>
  11. #include <stdlib.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. if (OSSL_trace_enabled(category))
  148. return;
  149. channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
  150. trace_data = OPENSSL_zalloc(sizeof(*trace_data));
  151. if (trace_data == NULL
  152. || (trace_data->bio = channel) == NULL
  153. || OSSL_trace_set_callback(category, internal_trace_cb,
  154. trace_data) == 0
  155. || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
  156. fprintf(stderr,
  157. "warning: unable to setup trace callback for category '%s'.\n",
  158. OSSL_trace_get_category_name(category));
  159. OSSL_trace_set_callback(category, NULL, NULL);
  160. BIO_free_all(channel);
  161. }
  162. }
  163. static void setup_trace(const char *str)
  164. {
  165. char *val;
  166. /*
  167. * We add this handler as early as possible to ensure it's executed
  168. * as late as possible, i.e. after the TRACE code has done its cleanup
  169. * (which happens last in OPENSSL_cleanup).
  170. */
  171. atexit(cleanup_trace);
  172. trace_data_stack = sk_tracedata_new_null();
  173. val = OPENSSL_strdup(str);
  174. if (val != NULL) {
  175. char *valp = val;
  176. char *item;
  177. for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
  178. int category = OSSL_trace_get_category_num(item);
  179. if (category == OSSL_TRACE_CATEGORY_ALL) {
  180. while (++category < OSSL_TRACE_CATEGORY_NUM)
  181. setup_trace_category(category);
  182. break;
  183. } else if (category > 0) {
  184. setup_trace_category(category);
  185. } else {
  186. fprintf(stderr,
  187. "warning: unknown trace category: '%s'.\n", item);
  188. }
  189. }
  190. }
  191. OPENSSL_free(val);
  192. }
  193. #endif /* OPENSSL_NO_TRACE */
  194. static char *help_argv[] = { "help", NULL };
  195. int main(int argc, char *argv[])
  196. {
  197. FUNCTION f, *fp;
  198. LHASH_OF(FUNCTION) *prog = NULL;
  199. char *pname;
  200. const char *fname;
  201. ARGS arg;
  202. int global_help = 0;
  203. int ret = 0;
  204. arg.argv = NULL;
  205. arg.size = 0;
  206. /* Set up some of the environment. */
  207. bio_in = dup_bio_in(FORMAT_TEXT);
  208. bio_out = dup_bio_out(FORMAT_TEXT);
  209. bio_err = dup_bio_err(FORMAT_TEXT);
  210. #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
  211. argv = copy_argv(&argc, argv);
  212. #elif defined(_WIN32)
  213. /* Replace argv[] with UTF-8 encoded strings. */
  214. win32_utf8argv(&argc, &argv);
  215. #endif
  216. #ifndef OPENSSL_NO_TRACE
  217. setup_trace(getenv("OPENSSL_TRACE"));
  218. #endif
  219. if ((fname = "apps_startup", !apps_startup())
  220. || (fname = "prog_init", (prog = prog_init()) == NULL)) {
  221. BIO_printf(bio_err,
  222. "FATAL: Startup failure (dev note: %s()) for %s\n",
  223. fname, argv[0]);
  224. ERR_print_errors(bio_err);
  225. ret = 1;
  226. goto end;
  227. }
  228. pname = opt_progname(argv[0]);
  229. default_config_file = CONF_get1_default_config_file();
  230. if (default_config_file == NULL)
  231. app_bail_out("%s: could not get default config file\n", pname);
  232. /* first check the program name */
  233. f.name = pname;
  234. fp = lh_FUNCTION_retrieve(prog, &f);
  235. if (fp == NULL) {
  236. /* We assume we've been called as 'openssl ...' */
  237. global_help = argc > 1
  238. && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0
  239. || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0);
  240. argc--;
  241. argv++;
  242. opt_appname(argc == 1 || global_help ? "help" : argv[0]);
  243. } else {
  244. argv[0] = pname;
  245. }
  246. /* If there's a command, run with that, otherwise "help". */
  247. ret = argc == 0 || global_help
  248. ? do_cmd(prog, 1, help_argv)
  249. : do_cmd(prog, argc, argv);
  250. end:
  251. OPENSSL_free(default_config_file);
  252. lh_FUNCTION_free(prog);
  253. OPENSSL_free(arg.argv);
  254. app_RAND_write();
  255. BIO_free(bio_in);
  256. BIO_free_all(bio_out);
  257. apps_shutdown();
  258. BIO_free(bio_err);
  259. EXIT(ret);
  260. }
  261. typedef enum HELP_CHOICE {
  262. OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
  263. } HELP_CHOICE;
  264. const OPTIONS help_options[] = {
  265. {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
  266. OPT_SECTION("General"),
  267. {"help", OPT_hHELP, '-', "Display this summary"},
  268. OPT_PARAMETERS(),
  269. {"command", 0, 0, "Name of command to display help (optional)"},
  270. {NULL}
  271. };
  272. int help_main(int argc, char **argv)
  273. {
  274. FUNCTION *fp;
  275. int i, nl;
  276. FUNC_TYPE tp;
  277. char *prog;
  278. HELP_CHOICE o;
  279. DISPLAY_COLUMNS dc;
  280. char *new_argv[3];
  281. prog = opt_init(argc, argv, help_options);
  282. while ((o = opt_next()) != OPT_hEOF) {
  283. switch (o) {
  284. case OPT_hERR:
  285. case OPT_hEOF:
  286. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  287. return 1;
  288. case OPT_hHELP:
  289. opt_help(help_options);
  290. return 0;
  291. }
  292. }
  293. /* One optional argument, the command to get help for. */
  294. if (opt_num_rest() == 1) {
  295. new_argv[0] = opt_rest()[0];
  296. new_argv[1] = "--help";
  297. new_argv[2] = NULL;
  298. return do_cmd(prog_init(), 2, new_argv);
  299. }
  300. if (opt_num_rest() != 0) {
  301. BIO_printf(bio_err, "Usage: %s\n", prog);
  302. return 1;
  303. }
  304. calculate_columns(functions, &dc);
  305. BIO_printf(bio_err, "%s:\n\nStandard commands", prog);
  306. i = 0;
  307. tp = FT_none;
  308. for (fp = functions; fp->name != NULL; fp++) {
  309. nl = 0;
  310. if (i++ % dc.columns == 0) {
  311. BIO_printf(bio_err, "\n");
  312. nl = 1;
  313. }
  314. if (fp->type != tp) {
  315. tp = fp->type;
  316. if (!nl)
  317. BIO_printf(bio_err, "\n");
  318. if (tp == FT_md) {
  319. i = 1;
  320. BIO_printf(bio_err,
  321. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  322. } else if (tp == FT_cipher) {
  323. i = 1;
  324. BIO_printf(bio_err,
  325. "\nCipher commands (see the `enc' command for more details)\n");
  326. }
  327. }
  328. BIO_printf(bio_err, "%-*s", dc.width, fp->name);
  329. }
  330. BIO_printf(bio_err, "\n\n");
  331. return 0;
  332. }
  333. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
  334. {
  335. FUNCTION f, *fp;
  336. if (argc <= 0 || argv[0] == NULL)
  337. return 0;
  338. f.name = argv[0];
  339. fp = lh_FUNCTION_retrieve(prog, &f);
  340. if (fp == NULL) {
  341. if (EVP_get_digestbyname(argv[0])) {
  342. f.type = FT_md;
  343. f.func = dgst_main;
  344. fp = &f;
  345. } else if (EVP_get_cipherbyname(argv[0])) {
  346. f.type = FT_cipher;
  347. f.func = enc_main;
  348. fp = &f;
  349. }
  350. }
  351. if (fp != NULL) {
  352. if (fp->deprecated_alternative != NULL)
  353. warn_deprecated(fp);
  354. return fp->func(argc, argv);
  355. }
  356. if ((strncmp(argv[0], "no-", 3)) == 0) {
  357. /*
  358. * User is asking if foo is unsupported, by trying to "run" the
  359. * no-foo command. Strange.
  360. */
  361. f.name = argv[0] + 3;
  362. if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
  363. BIO_printf(bio_out, "%s\n", argv[0]);
  364. return 0;
  365. }
  366. BIO_printf(bio_out, "%s\n", argv[0] + 3);
  367. return 1;
  368. }
  369. BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
  370. argv[0]);
  371. return 1;
  372. }
  373. static int function_cmp(const FUNCTION * a, const FUNCTION * b)
  374. {
  375. return strncmp(a->name, b->name, 8);
  376. }
  377. static unsigned long function_hash(const FUNCTION * a)
  378. {
  379. return OPENSSL_LH_strhash(a->name);
  380. }
  381. static int SortFnByName(const void *_f1, const void *_f2)
  382. {
  383. const FUNCTION *f1 = _f1;
  384. const FUNCTION *f2 = _f2;
  385. if (f1->type != f2->type)
  386. return f1->type - f2->type;
  387. return strcmp(f1->name, f2->name);
  388. }
  389. static LHASH_OF(FUNCTION) *prog_init(void)
  390. {
  391. static LHASH_OF(FUNCTION) *ret = NULL;
  392. static int prog_inited = 0;
  393. FUNCTION *f;
  394. size_t i;
  395. if (prog_inited)
  396. return ret;
  397. prog_inited = 1;
  398. /* Sort alphabetically within category. For nicer help displays. */
  399. for (i = 0, f = functions; f->name != NULL; ++f, ++i)
  400. ;
  401. qsort(functions, i, sizeof(*functions), SortFnByName);
  402. if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
  403. return NULL;
  404. for (f = functions; f->name != NULL; f++)
  405. (void)lh_FUNCTION_insert(ret, f);
  406. return ret;
  407. }