openssl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 apps_startup() */
  115. #include "apps.h"
  116. #include <openssl/bio.h>
  117. #include <openssl/crypto.h>
  118. #include <openssl/lhash.h>
  119. #include <openssl/conf.h>
  120. #include <openssl/x509.h>
  121. #include <openssl/pem.h>
  122. #include <openssl/ssl.h>
  123. #ifndef OPENSSL_NO_ENGINE
  124. #include <openssl/engine.h>
  125. #endif
  126. #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
  127. #include "progs.h"
  128. #include "s_apps.h"
  129. #include <openssl/err.h>
  130. /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
  131. * base prototypes (we cast each variable inside the function to the required
  132. * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
  133. * functions. */
  134. static LHASH_OF(FUNCTION) *prog_init(void );
  135. static int do_cmd(LHASH_OF(FUNCTION) *prog,int argc,char *argv[]);
  136. static void list_pkey(BIO *out);
  137. static void list_cipher(BIO *out);
  138. static void list_md(BIO *out);
  139. char *default_config_file=NULL;
  140. /* Make sure there is only one when MONOLITH is defined */
  141. #ifdef MONOLITH
  142. CONF *config=NULL;
  143. BIO *bio_err=NULL;
  144. #endif
  145. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  146. {
  147. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  148. const char *errstr = NULL;
  149. int rw;
  150. rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
  151. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
  152. {
  153. errstr = "invalid mode";
  154. goto err;
  155. }
  156. if (type < 0 || type >= CRYPTO_NUM_LOCKS)
  157. {
  158. errstr = "type out of bounds";
  159. goto err;
  160. }
  161. if (mode & CRYPTO_LOCK)
  162. {
  163. if (modes[type])
  164. {
  165. errstr = "already locked";
  166. /* must not happen in a single-threaded program
  167. * (would deadlock) */
  168. goto err;
  169. }
  170. modes[type] = rw;
  171. }
  172. else if (mode & CRYPTO_UNLOCK)
  173. {
  174. if (!modes[type])
  175. {
  176. errstr = "not locked";
  177. goto err;
  178. }
  179. if (modes[type] != rw)
  180. {
  181. errstr = (rw == CRYPTO_READ) ?
  182. "CRYPTO_r_unlock on write lock" :
  183. "CRYPTO_w_unlock on read lock";
  184. }
  185. modes[type] = 0;
  186. }
  187. else
  188. {
  189. errstr = "invalid mode";
  190. goto err;
  191. }
  192. err:
  193. if (errstr)
  194. {
  195. /* we cannot use bio_err here */
  196. fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  197. errstr, mode, type, file, line);
  198. }
  199. }
  200. int main(int Argc, char *Argv[])
  201. {
  202. ARGS arg;
  203. #define PROG_NAME_SIZE 39
  204. char pname[PROG_NAME_SIZE+1];
  205. FUNCTION f,*fp;
  206. MS_STATIC const char *prompt;
  207. MS_STATIC char buf[1024];
  208. char *to_free=NULL;
  209. int n,i,ret=0;
  210. int argc;
  211. char **argv,*p;
  212. LHASH_OF(FUNCTION) *prog=NULL;
  213. long errline;
  214. arg.data=NULL;
  215. arg.count=0;
  216. if (bio_err == NULL)
  217. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  218. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  219. if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
  220. {
  221. if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
  222. {
  223. CRYPTO_malloc_debug_init();
  224. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  225. }
  226. else
  227. {
  228. /* OPENSSL_DEBUG_MEMORY=off */
  229. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  230. }
  231. }
  232. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  233. #if 0
  234. if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
  235. #endif
  236. {
  237. CRYPTO_set_locking_callback(lock_dbg_cb);
  238. }
  239. apps_startup();
  240. /* Lets load up our environment a little */
  241. p=getenv("OPENSSL_CONF");
  242. if (p == NULL)
  243. p=getenv("SSLEAY_CONF");
  244. if (p == NULL)
  245. p=to_free=make_config_name();
  246. default_config_file=p;
  247. config=NCONF_new(NULL);
  248. i=NCONF_load(config,p,&errline);
  249. if (i == 0)
  250. {
  251. if (ERR_GET_REASON(ERR_peek_last_error())
  252. == CONF_R_NO_SUCH_FILE)
  253. {
  254. BIO_printf(bio_err,
  255. "WARNING: can't open config file: %s\n",p);
  256. ERR_clear_error();
  257. NCONF_free(config);
  258. config = NULL;
  259. }
  260. else
  261. {
  262. ERR_print_errors(bio_err);
  263. NCONF_free(config);
  264. exit(1);
  265. }
  266. }
  267. prog=prog_init();
  268. /* first check the program name */
  269. program_name(Argv[0],pname,sizeof pname);
  270. f.name=pname;
  271. fp=lh_FUNCTION_retrieve(prog,&f);
  272. if (fp != NULL)
  273. {
  274. Argv[0]=pname;
  275. ret=fp->func(Argc,Argv);
  276. goto end;
  277. }
  278. /* ok, now check that there are not arguments, if there are,
  279. * run with them, shifting the ssleay off the front */
  280. if (Argc != 1)
  281. {
  282. Argc--;
  283. Argv++;
  284. ret=do_cmd(prog,Argc,Argv);
  285. if (ret < 0) ret=0;
  286. goto end;
  287. }
  288. /* ok, lets enter the old 'OpenSSL>' mode */
  289. for (;;)
  290. {
  291. ret=0;
  292. p=buf;
  293. n=sizeof buf;
  294. i=0;
  295. for (;;)
  296. {
  297. p[0]='\0';
  298. if (i++)
  299. prompt=">";
  300. else prompt="OpenSSL> ";
  301. fputs(prompt,stdout);
  302. fflush(stdout);
  303. fgets(p,n,stdin);
  304. if (p[0] == '\0') goto end;
  305. i=strlen(p);
  306. if (i <= 1) break;
  307. if (p[i-2] != '\\') break;
  308. i-=2;
  309. p+=i;
  310. n-=i;
  311. }
  312. if (!chopup_args(&arg,buf,&argc,&argv)) break;
  313. ret=do_cmd(prog,argc,argv);
  314. if (ret < 0)
  315. {
  316. ret=0;
  317. goto end;
  318. }
  319. if (ret != 0)
  320. BIO_printf(bio_err,"error in %s\n",argv[0]);
  321. (void)BIO_flush(bio_err);
  322. }
  323. BIO_printf(bio_err,"bad exit\n");
  324. ret=1;
  325. end:
  326. if (to_free)
  327. OPENSSL_free(to_free);
  328. if (config != NULL)
  329. {
  330. NCONF_free(config);
  331. config=NULL;
  332. }
  333. if (prog != NULL) lh_FUNCTION_free(prog);
  334. if (arg.data != NULL) OPENSSL_free(arg.data);
  335. apps_shutdown();
  336. CRYPTO_mem_leaks(bio_err);
  337. if (bio_err != NULL)
  338. {
  339. BIO_free(bio_err);
  340. bio_err=NULL;
  341. }
  342. OPENSSL_EXIT(ret);
  343. }
  344. #define LIST_STANDARD_COMMANDS "list-standard-commands"
  345. #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
  346. #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
  347. #define LIST_CIPHER_COMMANDS "list-cipher-commands"
  348. #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
  349. #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
  350. static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
  351. {
  352. FUNCTION f,*fp;
  353. int i,ret=1,tp,nl;
  354. if ((argc <= 0) || (argv[0] == NULL))
  355. { ret=0; goto end; }
  356. f.name=argv[0];
  357. fp=lh_FUNCTION_retrieve(prog,&f);
  358. if (fp == NULL)
  359. {
  360. if (EVP_get_digestbyname(argv[0]))
  361. {
  362. f.type = FUNC_TYPE_MD;
  363. f.func = dgst_main;
  364. fp = &f;
  365. }
  366. else if (EVP_get_cipherbyname(argv[0]))
  367. {
  368. f.type = FUNC_TYPE_CIPHER;
  369. f.func = enc_main;
  370. fp = &f;
  371. }
  372. }
  373. if (fp != NULL)
  374. {
  375. ret=fp->func(argc,argv);
  376. }
  377. else if ((strncmp(argv[0],"no-",3)) == 0)
  378. {
  379. BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
  380. #ifdef OPENSSL_SYS_VMS
  381. {
  382. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  383. bio_stdout = BIO_push(tmpbio, bio_stdout);
  384. }
  385. #endif
  386. f.name=argv[0]+3;
  387. ret = (lh_FUNCTION_retrieve(prog,&f) != NULL);
  388. if (!ret)
  389. BIO_printf(bio_stdout, "%s\n", argv[0]);
  390. else
  391. BIO_printf(bio_stdout, "%s\n", argv[0]+3);
  392. BIO_free_all(bio_stdout);
  393. goto end;
  394. }
  395. else if ((strcmp(argv[0],"quit") == 0) ||
  396. (strcmp(argv[0],"q") == 0) ||
  397. (strcmp(argv[0],"exit") == 0) ||
  398. (strcmp(argv[0],"bye") == 0))
  399. {
  400. ret= -1;
  401. goto end;
  402. }
  403. else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
  404. (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
  405. (strcmp(argv[0],LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
  406. (strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0) ||
  407. (strcmp(argv[0],LIST_CIPHER_ALGORITHMS) == 0) ||
  408. (strcmp(argv[0],LIST_PUBLIC_KEY_ALGORITHMS) == 0))
  409. {
  410. int list_type;
  411. BIO *bio_stdout;
  412. if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
  413. list_type = FUNC_TYPE_GENERAL;
  414. else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
  415. list_type = FUNC_TYPE_MD;
  416. else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
  417. list_type = FUNC_TYPE_MD_ALG;
  418. else if (strcmp(argv[0],LIST_PUBLIC_KEY_ALGORITHMS) == 0)
  419. list_type = FUNC_TYPE_PKEY;
  420. else if (strcmp(argv[0],LIST_CIPHER_ALGORITHMS) == 0)
  421. list_type = FUNC_TYPE_CIPHER_ALG;
  422. else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
  423. list_type = FUNC_TYPE_CIPHER;
  424. bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
  425. #ifdef OPENSSL_SYS_VMS
  426. {
  427. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  428. bio_stdout = BIO_push(tmpbio, bio_stdout);
  429. }
  430. #endif
  431. if (!load_config(bio_err, NULL))
  432. goto end;
  433. if (list_type == FUNC_TYPE_PKEY)
  434. list_pkey(bio_stdout);
  435. if (list_type == FUNC_TYPE_MD_ALG)
  436. list_md(bio_stdout);
  437. if (list_type == FUNC_TYPE_CIPHER_ALG)
  438. list_cipher(bio_stdout);
  439. else
  440. {
  441. for (fp=functions; fp->name != NULL; fp++)
  442. if (fp->type == list_type)
  443. BIO_printf(bio_stdout, "%s\n",
  444. fp->name);
  445. }
  446. BIO_free_all(bio_stdout);
  447. ret=0;
  448. goto end;
  449. }
  450. else
  451. {
  452. BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
  453. argv[0]);
  454. BIO_printf(bio_err, "\nStandard commands");
  455. i=0;
  456. tp=0;
  457. for (fp=functions; fp->name != NULL; fp++)
  458. {
  459. nl=0;
  460. #ifdef OPENSSL_NO_CAMELLIA
  461. if (((i++) % 5) == 0)
  462. #else
  463. if (((i++) % 4) == 0)
  464. #endif
  465. {
  466. BIO_printf(bio_err,"\n");
  467. nl=1;
  468. }
  469. if (fp->type != tp)
  470. {
  471. tp=fp->type;
  472. if (!nl) BIO_printf(bio_err,"\n");
  473. if (tp == FUNC_TYPE_MD)
  474. {
  475. i=1;
  476. BIO_printf(bio_err,
  477. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  478. }
  479. else if (tp == FUNC_TYPE_CIPHER)
  480. {
  481. i=1;
  482. BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
  483. }
  484. }
  485. #ifdef OPENSSL_NO_CAMELLIA
  486. BIO_printf(bio_err,"%-15s",fp->name);
  487. #else
  488. BIO_printf(bio_err,"%-18s",fp->name);
  489. #endif
  490. }
  491. BIO_printf(bio_err,"\n\n");
  492. ret=0;
  493. }
  494. end:
  495. return(ret);
  496. }
  497. static int SortFnByName(const void *_f1,const void *_f2)
  498. {
  499. const FUNCTION *f1=_f1;
  500. const FUNCTION *f2=_f2;
  501. if(f1->type != f2->type)
  502. return f1->type-f2->type;
  503. return strcmp(f1->name,f2->name);
  504. }
  505. static void list_pkey(BIO *out)
  506. {
  507. int i;
  508. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++)
  509. {
  510. const EVP_PKEY_ASN1_METHOD *ameth;
  511. int pkey_id, pkey_base_id, pkey_flags;
  512. const char *pinfo, *pem_str;
  513. ameth = EVP_PKEY_asn1_get0(i);
  514. EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
  515. &pinfo, &pem_str, ameth);
  516. if (pkey_flags & ASN1_PKEY_ALIAS)
  517. {
  518. BIO_printf(out, "Name: %s\n",
  519. OBJ_nid2ln(pkey_id));
  520. BIO_printf(out, "\tType: Alias to %s\n",
  521. OBJ_nid2ln(pkey_base_id));
  522. }
  523. else
  524. {
  525. BIO_printf(out, "Name: %s\n", pinfo);
  526. BIO_printf(out, "\tType: %s Algorithm\n",
  527. pkey_flags & ASN1_PKEY_DYNAMIC ?
  528. "External" : "Builtin");
  529. BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
  530. if (pem_str == NULL)
  531. pem_str = "(none)";
  532. BIO_printf(out, "\tPEM string: %s\n", pem_str);
  533. }
  534. }
  535. }
  536. static void list_cipher_fn(const EVP_CIPHER *c,
  537. const char *from, const char *to, void *arg)
  538. {
  539. if (c)
  540. BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
  541. else
  542. {
  543. if (!from)
  544. from = "<undefined>";
  545. if (!to)
  546. to = "<undefined>";
  547. BIO_printf(arg, "%s => %s\n", from, to);
  548. }
  549. }
  550. static void list_cipher(BIO *out)
  551. {
  552. EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
  553. }
  554. static void list_md_fn(const EVP_MD *m,
  555. const char *from, const char *to, void *arg)
  556. {
  557. if (m)
  558. BIO_printf(arg, "%s\n", EVP_MD_name(m));
  559. else
  560. {
  561. if (!from)
  562. from = "<undefined>";
  563. if (!to)
  564. to = "<undefined>";
  565. BIO_printf(arg, "%s => %s\n", from, to);
  566. }
  567. }
  568. static void list_md(BIO *out)
  569. {
  570. EVP_MD_do_all_sorted(list_md_fn, out);
  571. }
  572. static int MS_CALLBACK function_cmp(const FUNCTION *a, const FUNCTION *b)
  573. {
  574. return strncmp(a->name,b->name,8);
  575. }
  576. static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
  577. static unsigned long MS_CALLBACK function_hash(const FUNCTION *a)
  578. {
  579. return lh_strhash(a->name);
  580. }
  581. static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
  582. static LHASH_OF(FUNCTION) *prog_init(void)
  583. {
  584. LHASH_OF(FUNCTION) *ret;
  585. FUNCTION *f;
  586. size_t i;
  587. /* Purely so it looks nice when the user hits ? */
  588. for(i=0,f=functions ; f->name != NULL ; ++f,++i)
  589. ;
  590. qsort(functions,i,sizeof *functions,SortFnByName);
  591. if ((ret=lh_FUNCTION_new()) == NULL)
  592. return(NULL);
  593. for (f=functions; f->name != NULL; f++)
  594. (void)lh_FUNCTION_insert(ret,f);
  595. return(ret);
  596. }