openssl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* apps/openssl.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <string.h>
  113. #include <stdlib.h>
  114. #define OPENSSL_C /* tells apps.h to use complete
  115. * apps_startup() */
  116. #include "apps.h"
  117. #include <openssl/bio.h>
  118. #include <openssl/crypto.h>
  119. #include <openssl/rand.h>
  120. #include <openssl/lhash.h>
  121. #include <openssl/conf.h>
  122. #include <openssl/x509.h>
  123. #include <openssl/pem.h>
  124. #include <openssl/ssl.h>
  125. #ifndef OPENSSL_NO_ENGINE
  126. # include <openssl/engine.h>
  127. #endif
  128. #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS
  129. * world */
  130. #include "progs.h"
  131. #include "s_apps.h"
  132. #include <openssl/err.h>
  133. #ifdef OPENSSL_FIPS
  134. # include <openssl/fips.h>
  135. #endif
  136. /*
  137. * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
  138. * the base prototypes (we cast each variable inside the function to the
  139. * required type of "FUNCTION*"). This removes the necessity for
  140. * macro-generated wrapper functions.
  141. */
  142. static LHASH_OF(FUNCTION) *prog_init(void);
  143. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
  144. static void list_pkey(BIO *out);
  145. static void list_cipher(BIO *out);
  146. static void list_md(BIO *out);
  147. char *default_config_file = NULL;
  148. /* Make sure there is only one when MONOLITH is defined */
  149. #ifdef MONOLITH
  150. CONF *config = NULL;
  151. BIO *bio_err = NULL;
  152. #endif
  153. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  154. {
  155. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  156. const char *errstr = NULL;
  157. int rw;
  158. rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
  159. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
  160. errstr = "invalid mode";
  161. goto err;
  162. }
  163. if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
  164. errstr = "type out of bounds";
  165. goto err;
  166. }
  167. if (mode & CRYPTO_LOCK) {
  168. if (modes[type]) {
  169. errstr = "already locked";
  170. /*
  171. * must not happen in a single-threaded program (would deadlock)
  172. */
  173. goto err;
  174. }
  175. modes[type] = rw;
  176. } else if (mode & CRYPTO_UNLOCK) {
  177. if (!modes[type]) {
  178. errstr = "not locked";
  179. goto err;
  180. }
  181. if (modes[type] != rw) {
  182. errstr = (rw == CRYPTO_READ) ?
  183. "CRYPTO_r_unlock on write lock" :
  184. "CRYPTO_w_unlock on read lock";
  185. }
  186. modes[type] = 0;
  187. } else {
  188. errstr = "invalid mode";
  189. goto err;
  190. }
  191. err:
  192. if (errstr) {
  193. /* we cannot use bio_err here */
  194. fprintf(stderr,
  195. "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  196. errstr, mode, type, file, line);
  197. }
  198. }
  199. #if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
  200. # define ARGV _Argv
  201. #else
  202. # define ARGV Argv
  203. #endif
  204. int main(int Argc, char *ARGV[])
  205. {
  206. ARGS arg;
  207. #define PROG_NAME_SIZE 39
  208. char pname[PROG_NAME_SIZE + 1];
  209. FUNCTION f, *fp;
  210. MS_STATIC const char *prompt;
  211. MS_STATIC char buf[1024];
  212. char *to_free = NULL;
  213. int n, i, ret = 0;
  214. int argc;
  215. char **argv, *p;
  216. LHASH_OF(FUNCTION) *prog = NULL;
  217. long errline;
  218. #if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
  219. /*-
  220. * 2011-03-22 SMS.
  221. * If we have 32-bit pointers everywhere, then we're safe, and
  222. * we bypass this mess, as on non-VMS systems. (See ARGV,
  223. * above.)
  224. * Problem 1: Compaq/HP C before V7.3 always used 32-bit
  225. * pointers for argv[].
  226. * Fix 1: For a 32-bit argv[], when we're using 64-bit pointers
  227. * everywhere else, we always allocate and use a 64-bit
  228. * duplicate of argv[].
  229. * Problem 2: Compaq/HP C V7.3 (Alpha, IA64) before ECO1 failed
  230. * to NULL-terminate a 64-bit argv[]. (As this was written, the
  231. * compiler ECO was available only on IA64.)
  232. * Fix 2: Unless advised not to (VMS_TRUST_ARGV), we test a
  233. * 64-bit argv[argc] for NULL, and, if necessary, use a
  234. * (properly) NULL-terminated (64-bit) duplicate of argv[].
  235. * The same code is used in either case to duplicate argv[].
  236. * Some of these decisions could be handled in preprocessing,
  237. * but the code tends to get even uglier, and the penalty for
  238. * deciding at compile- or run-time is tiny.
  239. */
  240. char **Argv = NULL;
  241. int free_Argv = 0;
  242. if ((sizeof(_Argv) < 8) /* 32-bit argv[]. */
  243. # if !defined( VMS_TRUST_ARGV)
  244. || (_Argv[Argc] != NULL) /* Untrusted argv[argc] not NULL. */
  245. # endif
  246. ) {
  247. int i;
  248. Argv = OPENSSL_malloc((Argc + 1) * sizeof(char *));
  249. if (Argv == NULL) {
  250. ret = -1;
  251. goto end;
  252. }
  253. for (i = 0; i < Argc; i++)
  254. Argv[i] = _Argv[i];
  255. Argv[Argc] = NULL; /* Certain NULL termination. */
  256. free_Argv = 1;
  257. } else {
  258. /*
  259. * Use the known-good 32-bit argv[] (which needs the type cast to
  260. * satisfy the compiler), or the trusted or tested-good 64-bit argv[]
  261. * as-is.
  262. */
  263. Argv = (char **)_Argv;
  264. }
  265. #endif /* defined( OPENSSL_SYS_VMS) &&
  266. * (__INITIAL_POINTER_SIZE == 64) */
  267. arg.data = NULL;
  268. arg.count = 0;
  269. if (bio_err == NULL)
  270. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  271. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  272. if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) { /* if not defined, use
  273. * compiled-in library
  274. * defaults */
  275. if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))) {
  276. CRYPTO_malloc_debug_init();
  277. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  278. } else {
  279. /* OPENSSL_DEBUG_MEMORY=off */
  280. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  281. }
  282. }
  283. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  284. #if 0
  285. if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
  286. #endif
  287. {
  288. CRYPTO_set_locking_callback(lock_dbg_cb);
  289. }
  290. if (getenv("OPENSSL_FIPS")) {
  291. #ifdef OPENSSL_FIPS
  292. if (!FIPS_mode_set(1)) {
  293. ERR_load_crypto_strings();
  294. ERR_print_errors(BIO_new_fp(stderr, BIO_NOCLOSE));
  295. EXIT(1);
  296. }
  297. #else
  298. fprintf(stderr, "FIPS mode not supported.\n");
  299. EXIT(1);
  300. #endif
  301. }
  302. apps_startup();
  303. /* Lets load up our environment a little */
  304. p = getenv("OPENSSL_CONF");
  305. if (p == NULL)
  306. p = getenv("SSLEAY_CONF");
  307. if (p == NULL)
  308. p = to_free = make_config_name();
  309. default_config_file = p;
  310. config = NCONF_new(NULL);
  311. i = NCONF_load(config, p, &errline);
  312. if (i == 0) {
  313. if (ERR_GET_REASON(ERR_peek_last_error())
  314. == CONF_R_NO_SUCH_FILE) {
  315. BIO_printf(bio_err, "WARNING: can't open config file: %s\n", p);
  316. ERR_clear_error();
  317. NCONF_free(config);
  318. config = NULL;
  319. } else {
  320. ERR_print_errors(bio_err);
  321. NCONF_free(config);
  322. exit(1);
  323. }
  324. }
  325. prog = prog_init();
  326. /* first check the program name */
  327. program_name(Argv[0], pname, sizeof pname);
  328. f.name = pname;
  329. fp = lh_FUNCTION_retrieve(prog, &f);
  330. if (fp != NULL) {
  331. Argv[0] = pname;
  332. ret = fp->func(Argc, Argv);
  333. goto end;
  334. }
  335. /*
  336. * ok, now check that there are not arguments, if there are, run with
  337. * them, shifting the ssleay off the front
  338. */
  339. if (Argc != 1) {
  340. Argc--;
  341. Argv++;
  342. ret = do_cmd(prog, Argc, Argv);
  343. if (ret < 0)
  344. ret = 0;
  345. goto end;
  346. }
  347. /* ok, lets enter the old 'OpenSSL>' mode */
  348. for (;;) {
  349. ret = 0;
  350. p = buf;
  351. n = sizeof buf;
  352. i = 0;
  353. for (;;) {
  354. p[0] = '\0';
  355. if (i++)
  356. prompt = ">";
  357. else
  358. prompt = "OpenSSL> ";
  359. fputs(prompt, stdout);
  360. fflush(stdout);
  361. if (!fgets(p, n, stdin))
  362. goto end;
  363. if (p[0] == '\0')
  364. goto end;
  365. i = strlen(p);
  366. if (i <= 1)
  367. break;
  368. if (p[i - 2] != '\\')
  369. break;
  370. i -= 2;
  371. p += i;
  372. n -= i;
  373. }
  374. if (!chopup_args(&arg, buf, &argc, &argv))
  375. break;
  376. ret = do_cmd(prog, argc, argv);
  377. if (ret < 0) {
  378. ret = 0;
  379. goto end;
  380. }
  381. if (ret != 0)
  382. BIO_printf(bio_err, "error in %s\n", argv[0]);
  383. (void)BIO_flush(bio_err);
  384. }
  385. BIO_printf(bio_err, "bad exit\n");
  386. ret = 1;
  387. end:
  388. if (to_free)
  389. OPENSSL_free(to_free);
  390. if (config != NULL) {
  391. NCONF_free(config);
  392. config = NULL;
  393. }
  394. if (prog != NULL)
  395. lh_FUNCTION_free(prog);
  396. if (arg.data != NULL)
  397. OPENSSL_free(arg.data);
  398. #if defined( OPENSSL_SYS_VMS) && (__INITIAL_POINTER_SIZE == 64)
  399. /* Free any duplicate Argv[] storage. */
  400. if (free_Argv) {
  401. OPENSSL_free(Argv);
  402. }
  403. #endif
  404. apps_shutdown();
  405. CRYPTO_mem_leaks(bio_err);
  406. if (bio_err != NULL) {
  407. BIO_free(bio_err);
  408. bio_err = NULL;
  409. }
  410. OPENSSL_EXIT(ret);
  411. }
  412. #define LIST_STANDARD_COMMANDS "list-standard-commands"
  413. #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
  414. #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
  415. #define LIST_CIPHER_COMMANDS "list-cipher-commands"
  416. #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
  417. #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
  418. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
  419. {
  420. FUNCTION f, *fp;
  421. int i, ret = 1, tp, nl;
  422. if ((argc <= 0) || (argv[0] == NULL)) {
  423. ret = 0;
  424. goto end;
  425. }
  426. f.name = argv[0];
  427. fp = lh_FUNCTION_retrieve(prog, &f);
  428. if (fp == NULL) {
  429. if (EVP_get_digestbyname(argv[0])) {
  430. f.type = FUNC_TYPE_MD;
  431. f.func = dgst_main;
  432. fp = &f;
  433. } else if (EVP_get_cipherbyname(argv[0])) {
  434. f.type = FUNC_TYPE_CIPHER;
  435. f.func = enc_main;
  436. fp = &f;
  437. }
  438. }
  439. if (fp != NULL) {
  440. ret = fp->func(argc, argv);
  441. } else if ((strncmp(argv[0], "no-", 3)) == 0) {
  442. BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
  443. #ifdef OPENSSL_SYS_VMS
  444. {
  445. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  446. bio_stdout = BIO_push(tmpbio, bio_stdout);
  447. }
  448. #endif
  449. f.name = argv[0] + 3;
  450. ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
  451. if (!ret)
  452. BIO_printf(bio_stdout, "%s\n", argv[0]);
  453. else
  454. BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
  455. BIO_free_all(bio_stdout);
  456. goto end;
  457. } else if ((strcmp(argv[0], "quit") == 0) ||
  458. (strcmp(argv[0], "q") == 0) ||
  459. (strcmp(argv[0], "exit") == 0) ||
  460. (strcmp(argv[0], "bye") == 0)) {
  461. ret = -1;
  462. goto end;
  463. } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
  464. (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
  465. (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
  466. (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
  467. (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
  468. (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
  469. int list_type;
  470. BIO *bio_stdout;
  471. if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
  472. list_type = FUNC_TYPE_GENERAL;
  473. else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
  474. list_type = FUNC_TYPE_MD;
  475. else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
  476. list_type = FUNC_TYPE_MD_ALG;
  477. else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
  478. list_type = FUNC_TYPE_PKEY;
  479. else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
  480. list_type = FUNC_TYPE_CIPHER_ALG;
  481. else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
  482. list_type = FUNC_TYPE_CIPHER;
  483. bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
  484. #ifdef OPENSSL_SYS_VMS
  485. {
  486. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  487. bio_stdout = BIO_push(tmpbio, bio_stdout);
  488. }
  489. #endif
  490. if (!load_config(bio_err, NULL))
  491. goto end;
  492. if (list_type == FUNC_TYPE_PKEY)
  493. list_pkey(bio_stdout);
  494. if (list_type == FUNC_TYPE_MD_ALG)
  495. list_md(bio_stdout);
  496. if (list_type == FUNC_TYPE_CIPHER_ALG)
  497. list_cipher(bio_stdout);
  498. else {
  499. for (fp = functions; fp->name != NULL; fp++)
  500. if (fp->type == list_type)
  501. BIO_printf(bio_stdout, "%s\n", fp->name);
  502. }
  503. BIO_free_all(bio_stdout);
  504. ret = 0;
  505. goto end;
  506. } else {
  507. BIO_printf(bio_err, "openssl:Error: '%s' is an invalid command.\n",
  508. argv[0]);
  509. BIO_printf(bio_err, "\nStandard commands");
  510. i = 0;
  511. tp = 0;
  512. for (fp = functions; fp->name != NULL; fp++) {
  513. nl = 0;
  514. #ifdef OPENSSL_NO_CAMELLIA
  515. if (((i++) % 5) == 0)
  516. #else
  517. if (((i++) % 4) == 0)
  518. #endif
  519. {
  520. BIO_printf(bio_err, "\n");
  521. nl = 1;
  522. }
  523. if (fp->type != tp) {
  524. tp = fp->type;
  525. if (!nl)
  526. BIO_printf(bio_err, "\n");
  527. if (tp == FUNC_TYPE_MD) {
  528. i = 1;
  529. BIO_printf(bio_err,
  530. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  531. } else if (tp == FUNC_TYPE_CIPHER) {
  532. i = 1;
  533. BIO_printf(bio_err,
  534. "\nCipher commands (see the `enc' command for more details)\n");
  535. }
  536. }
  537. #ifdef OPENSSL_NO_CAMELLIA
  538. BIO_printf(bio_err, "%-15s", fp->name);
  539. #else
  540. BIO_printf(bio_err, "%-18s", fp->name);
  541. #endif
  542. }
  543. BIO_printf(bio_err, "\n\n");
  544. ret = 0;
  545. }
  546. end:
  547. return (ret);
  548. }
  549. static int SortFnByName(const void *_f1, const void *_f2)
  550. {
  551. const FUNCTION *f1 = _f1;
  552. const FUNCTION *f2 = _f2;
  553. if (f1->type != f2->type)
  554. return f1->type - f2->type;
  555. return strcmp(f1->name, f2->name);
  556. }
  557. static void list_pkey(BIO *out)
  558. {
  559. int i;
  560. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  561. const EVP_PKEY_ASN1_METHOD *ameth;
  562. int pkey_id, pkey_base_id, pkey_flags;
  563. const char *pinfo, *pem_str;
  564. ameth = EVP_PKEY_asn1_get0(i);
  565. EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
  566. &pinfo, &pem_str, ameth);
  567. if (pkey_flags & ASN1_PKEY_ALIAS) {
  568. BIO_printf(out, "Name: %s\n", OBJ_nid2ln(pkey_id));
  569. BIO_printf(out, "\tType: Alias to %s\n",
  570. OBJ_nid2ln(pkey_base_id));
  571. } else {
  572. BIO_printf(out, "Name: %s\n", pinfo);
  573. BIO_printf(out, "\tType: %s Algorithm\n",
  574. pkey_flags & ASN1_PKEY_DYNAMIC ?
  575. "External" : "Builtin");
  576. BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
  577. if (pem_str == NULL)
  578. pem_str = "(none)";
  579. BIO_printf(out, "\tPEM string: %s\n", pem_str);
  580. }
  581. }
  582. }
  583. static void list_cipher_fn(const EVP_CIPHER *c,
  584. const char *from, const char *to, void *arg)
  585. {
  586. if (c)
  587. BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
  588. else {
  589. if (!from)
  590. from = "<undefined>";
  591. if (!to)
  592. to = "<undefined>";
  593. BIO_printf(arg, "%s => %s\n", from, to);
  594. }
  595. }
  596. static void list_cipher(BIO *out)
  597. {
  598. EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
  599. }
  600. static void list_md_fn(const EVP_MD *m,
  601. const char *from, const char *to, void *arg)
  602. {
  603. if (m)
  604. BIO_printf(arg, "%s\n", EVP_MD_name(m));
  605. else {
  606. if (!from)
  607. from = "<undefined>";
  608. if (!to)
  609. to = "<undefined>";
  610. BIO_printf(arg, "%s => %s\n", from, to);
  611. }
  612. }
  613. static void list_md(BIO *out)
  614. {
  615. EVP_MD_do_all_sorted(list_md_fn, out);
  616. }
  617. static int MS_CALLBACK function_cmp(const FUNCTION * a, const FUNCTION * b)
  618. {
  619. return strncmp(a->name, b->name, 8);
  620. }
  621. static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
  622. static unsigned long MS_CALLBACK function_hash(const FUNCTION * a)
  623. {
  624. return lh_strhash(a->name);
  625. }
  626. static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
  627. static LHASH_OF(FUNCTION) *prog_init(void)
  628. {
  629. LHASH_OF(FUNCTION) *ret;
  630. FUNCTION *f;
  631. size_t i;
  632. /* Purely so it looks nice when the user hits ? */
  633. for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
  634. qsort(functions, i, sizeof *functions, SortFnByName);
  635. if ((ret = lh_FUNCTION_new()) == NULL)
  636. return (NULL);
  637. for (f = functions; f->name != NULL; f++)
  638. (void)lh_FUNCTION_insert(ret, f);
  639. return (ret);
  640. }