engine.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */
  2. /* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #ifdef OPENSSL_NO_STDIO
  62. #define APPS_WIN16
  63. #endif
  64. #include "apps.h"
  65. #include <openssl/err.h>
  66. #include <openssl/engine.h>
  67. #include <openssl/ssl.h>
  68. #undef PROG
  69. #define PROG engine_main
  70. static char *engine_usage[]={
  71. "usage: engine opts [engine ...]\n",
  72. " -v - verbose mode, a textual listing of the engines in OpenSSL\n",
  73. " -c - for each engine, also list the capabilities\n",
  74. " -t - for each engine, check that they are really available\n",
  75. NULL
  76. };
  77. static void identity(void *ptr)
  78. {
  79. return;
  80. }
  81. static int append_buf(char **buf, char *s, int *size, int step)
  82. {
  83. int l = strlen(s);
  84. if (*buf == NULL)
  85. {
  86. *size = step;
  87. *buf = OPENSSL_malloc(*size);
  88. if (*buf == NULL)
  89. return 0;
  90. **buf = '\0';
  91. }
  92. if (**buf != '\0')
  93. l += 2; /* ", " */
  94. if (strlen(*buf) + strlen(s) >= (unsigned int)*size)
  95. {
  96. *size += step;
  97. *buf = OPENSSL_realloc(*buf, *size);
  98. }
  99. if (*buf == NULL)
  100. return 0;
  101. if (**buf != '\0')
  102. strcat(*buf, ", ");
  103. strcat(*buf, s);
  104. return 1;
  105. }
  106. int MAIN(int, char **);
  107. int MAIN(int argc, char **argv)
  108. {
  109. int ret=1,i;
  110. char **pp;
  111. int verbose=0, list_cap=0, test_avail=0;
  112. ENGINE *e;
  113. STACK *engines = sk_new_null();
  114. int badops=0;
  115. BIO *bio_out=NULL;
  116. apps_startup();
  117. SSL_load_error_strings();
  118. if (bio_err == NULL)
  119. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
  120. bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
  121. #ifdef OPENSSL_SYS_VMS
  122. {
  123. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  124. bio_out = BIO_push(tmpbio, bio_out);
  125. }
  126. #endif
  127. argc--;
  128. argv++;
  129. while (argc >= 1)
  130. {
  131. if (strcmp(*argv,"-v") == 0)
  132. verbose=1;
  133. else if (strcmp(*argv,"-c") == 0)
  134. list_cap=1;
  135. else if (strcmp(*argv,"-t") == 0)
  136. test_avail=1;
  137. else if ((strncmp(*argv,"-h",2) == 0) ||
  138. (strcmp(*argv,"-?") == 0))
  139. {
  140. badops=1;
  141. break;
  142. }
  143. else
  144. {
  145. sk_push(engines,*argv);
  146. }
  147. argc--;
  148. argv++;
  149. }
  150. if (badops)
  151. {
  152. for (pp=engine_usage; (*pp != NULL); pp++)
  153. BIO_printf(bio_err,"%s",*pp);
  154. goto end;
  155. }
  156. if (sk_num(engines) == 0)
  157. {
  158. for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
  159. {
  160. sk_push(engines,(char *)ENGINE_get_id(e));
  161. }
  162. }
  163. for (i=0; i<sk_num(engines); i++)
  164. {
  165. const char *id = sk_value(engines,i);
  166. if ((e = ENGINE_by_id(id)) != NULL)
  167. {
  168. const char *name = ENGINE_get_name(e);
  169. BIO_printf(bio_out, "%s (%s)", name, id);
  170. if (list_cap || test_avail)
  171. BIO_printf(bio_out, ":");
  172. if (test_avail)
  173. {
  174. if (ENGINE_init(e))
  175. {
  176. BIO_printf(bio_out, " available");
  177. ENGINE_finish(e);
  178. }
  179. else
  180. {
  181. BIO_printf(bio_out, " unavailable");
  182. ERR_clear_error();
  183. }
  184. }
  185. if (list_cap)
  186. {
  187. int cap_size = 256;
  188. char *cap_buf = NULL;
  189. if (ENGINE_get_RSA(e) != NULL
  190. && !append_buf(&cap_buf, "RSA",
  191. &cap_size, 256))
  192. goto end;
  193. if (ENGINE_get_DSA(e) != NULL
  194. && !append_buf(&cap_buf, "DSA",
  195. &cap_size, 256))
  196. goto end;
  197. if (ENGINE_get_DH(e) != NULL
  198. && !append_buf(&cap_buf, "DH",
  199. &cap_size, 256))
  200. goto end;
  201. if (ENGINE_get_RAND(e) != NULL
  202. && !append_buf(&cap_buf, "RAND",
  203. &cap_size, 256))
  204. goto end;
  205. if (*cap_buf != '\0')
  206. BIO_printf(bio_out, " [%s]", cap_buf);
  207. OPENSSL_free(cap_buf);
  208. }
  209. BIO_printf(bio_out, "\n");
  210. }
  211. else
  212. ERR_print_errors(bio_err);
  213. }
  214. ret=0;
  215. end:
  216. ERR_print_errors(bio_err);
  217. sk_pop_free(engines, identity);
  218. if (bio_out != NULL) BIO_free_all(bio_out);
  219. EXIT(ret);
  220. }