]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
3ac82faa | 57 | /* ==================================================================== |
f3dea9a5 | 58 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
3ac82faa BM |
59 | * |
60 | * Redistribution and use in source and binary forms, with or without | |
61 | * modification, are permitted provided that the following conditions | |
62 | * are met: | |
63 | * | |
64 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 65 | * notice, this list of conditions and the following disclaimer. |
3ac82faa BM |
66 | * |
67 | * 2. Redistributions in binary form must reproduce the above copyright | |
68 | * notice, this list of conditions and the following disclaimer in | |
69 | * the documentation and/or other materials provided with the | |
70 | * distribution. | |
71 | * | |
72 | * 3. All advertising materials mentioning features or use of this | |
73 | * software must display the following acknowledgment: | |
74 | * "This product includes software developed by the OpenSSL Project | |
75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
76 | * | |
77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
78 | * endorse or promote products derived from this software without | |
79 | * prior written permission. For written permission, please contact | |
80 | * openssl-core@openssl.org. | |
81 | * | |
82 | * 5. Products derived from this software may not be called "OpenSSL" | |
83 | * nor may "OpenSSL" appear in their names without prior written | |
84 | * permission of the OpenSSL Project. | |
85 | * | |
86 | * 6. Redistributions of any form whatsoever must retain the following | |
87 | * acknowledgment: | |
88 | * "This product includes software developed by the OpenSSL Project | |
89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
90 | * | |
91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
102 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
103 | * ==================================================================== | |
104 | * | |
105 | * This product includes cryptographic software written by Eric Young | |
106 | * (eay@cryptsoft.com). This product includes software written by Tim | |
107 | * Hudson (tjh@cryptsoft.com). | |
108 | * | |
109 | */ | |
110 | ||
d02b48c6 RE |
111 | #include <stdio.h> |
112 | #include <string.h> | |
113 | #include <stdlib.h> | |
ec577822 BM |
114 | #include <openssl/bio.h> |
115 | #include <openssl/crypto.h> | |
3d1160d5 | 116 | #include <openssl/rand.h> |
ec577822 BM |
117 | #include <openssl/lhash.h> |
118 | #include <openssl/conf.h> | |
119 | #include <openssl/x509.h> | |
120 | #include <openssl/pem.h> | |
121 | #include <openssl/ssl.h> | |
0b13e9f0 | 122 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 123 | # include <openssl/engine.h> |
0b13e9f0 | 124 | #endif |
ec577822 | 125 | #include <openssl/err.h> |
856650de | 126 | #ifdef OPENSSL_FIPS |
0f113f3e | 127 | # include <openssl/fips.h> |
856650de | 128 | #endif |
3b061a00 RS |
129 | #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */ |
130 | #include "s_apps.h" | |
131 | /* Needed to get the other O_xxx flags. */ | |
132 | #ifdef OPENSSL_SYS_VMS | |
133 | # include <unixio.h> | |
134 | #endif | |
7e1b7485 RS |
135 | #define INCLUDE_FUNCTION_TABLE |
136 | #include "apps.h" | |
137 | ||
7e1b7485 RS |
138 | |
139 | #ifdef OPENSSL_NO_CAMELLIA | |
140 | # define FORMAT "%-15s" | |
141 | # define COLUMNS 5 | |
142 | #else | |
143 | # define FORMAT "%-18s" | |
144 | # define COLUMNS 4 | |
145 | #endif | |
146 | ||
147 | /* Special sentinel to exit the program. */ | |
148 | #define EXIT_THE_PROGRAM (-1) | |
d02b48c6 | 149 | |
0f113f3e MC |
150 | /* |
151 | * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with | |
152 | * the base prototypes (we cast each variable inside the function to the | |
153 | * required type of "FUNCTION*"). This removes the necessity for | |
154 | * macro-generated wrapper functions. | |
155 | */ | |
7e1b7485 | 156 | DECLARE_LHASH_OF(FUNCTION); |
0f113f3e MC |
157 | static LHASH_OF(FUNCTION) *prog_init(void); |
158 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]); | |
2f58faad RS |
159 | static void list_pkey(void); |
160 | static void list_type(FUNC_TYPE ft); | |
a760a380 | 161 | static void list_disabled(void); |
0f113f3e | 162 | char *default_config_file = NULL; |
d02b48c6 | 163 | |
df2ee0e2 | 164 | static CONF *config = NULL; |
7e1b7485 RS |
165 | BIO *bio_in = NULL; |
166 | BIO *bio_out = NULL; | |
0f113f3e | 167 | BIO *bio_err = NULL; |
7e1b7485 | 168 | |
a0a82324 | 169 | static int apps_startup() |
7e1b7485 RS |
170 | { |
171 | #ifdef SIGPIPE | |
172 | signal(SIGPIPE, SIG_IGN); | |
173 | #endif | |
174 | CRYPTO_malloc_init(); | |
175 | ERR_load_crypto_strings(); | |
176 | ERR_load_SSL_strings(); | |
a0a82324 | 177 | |
1f08d945 | 178 | OPENSSL_load_builtin_modules(); |
287d0b94 | 179 | SSL_add_ssl_module(); |
1f08d945 DB |
180 | #ifndef OPENSSL_NO_ENGINE |
181 | ENGINE_load_builtin_engines(); | |
182 | #endif | |
a0a82324 MC |
183 | if (!app_load_modules(NULL)) { |
184 | ERR_print_errors(bio_err); | |
185 | BIO_printf(bio_err, "Error loading default configuration\n"); | |
186 | return 0; | |
187 | } | |
188 | ||
7e1b7485 RS |
189 | OpenSSL_add_all_algorithms(); |
190 | OpenSSL_add_ssl_algorithms(); | |
191 | setup_ui_method(); | |
192 | /*SSL_library_init();*/ | |
a0a82324 | 193 | return 1; |
7e1b7485 RS |
194 | } |
195 | ||
196 | static void apps_shutdown() | |
197 | { | |
198 | #ifndef OPENSSL_NO_ENGINE | |
199 | ENGINE_cleanup(); | |
200 | #endif | |
201 | destroy_ui_method(); | |
202 | CONF_modules_unload(1); | |
203 | #ifndef OPENSSL_NO_COMP | |
204 | COMP_zlib_cleanup(); | |
45d051c8 | 205 | SSL_COMP_free_compression_methods(); |
7e1b7485 RS |
206 | #endif |
207 | OBJ_cleanup(); | |
208 | EVP_cleanup(); | |
209 | CRYPTO_cleanup_all_ex_data(); | |
210 | ERR_remove_thread_state(NULL); | |
211 | RAND_cleanup(); | |
212 | ERR_free_strings(); | |
213 | } | |
214 | ||
215 | static char *make_config_name() | |
216 | { | |
cc01d217 | 217 | const char *t; |
7e1b7485 RS |
218 | size_t len; |
219 | char *p; | |
220 | ||
b0700d2c | 221 | if ((t = getenv("OPENSSL_CONF")) != NULL) |
7644a9ae | 222 | return OPENSSL_strdup(t); |
cc01d217 RS |
223 | |
224 | t = X509_get_default_cert_area(); | |
225 | len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1; | |
68dc6824 | 226 | p = app_malloc(len, "config filename buffer"); |
cc01d217 | 227 | strcpy(p, t); |
7e1b7485 | 228 | #ifndef OPENSSL_SYS_VMS |
cc01d217 | 229 | strcat(p, "/"); |
d02b48c6 | 230 | #endif |
cc01d217 | 231 | strcat(p, OPENSSL_CONF); |
7e1b7485 RS |
232 | |
233 | return p; | |
234 | } | |
235 | ||
3ac82faa | 236 | static void lock_dbg_cb(int mode, int type, const char *file, int line) |
0f113f3e | 237 | { |
7e1b7485 | 238 | static int modes[CRYPTO_NUM_LOCKS]; |
0f113f3e | 239 | const char *errstr = NULL; |
7e1b7485 | 240 | int rw = mode & (CRYPTO_READ | CRYPTO_WRITE); |
0f113f3e | 241 | |
7e1b7485 | 242 | if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) { |
0f113f3e MC |
243 | errstr = "invalid mode"; |
244 | goto err; | |
245 | } | |
246 | ||
247 | if (type < 0 || type >= CRYPTO_NUM_LOCKS) { | |
248 | errstr = "type out of bounds"; | |
249 | goto err; | |
250 | } | |
251 | ||
252 | if (mode & CRYPTO_LOCK) { | |
253 | if (modes[type]) { | |
254 | errstr = "already locked"; | |
7e1b7485 | 255 | /* must not happen in a single-threaded program --> deadlock! */ |
0f113f3e MC |
256 | goto err; |
257 | } | |
0f113f3e MC |
258 | modes[type] = rw; |
259 | } else if (mode & CRYPTO_UNLOCK) { | |
260 | if (!modes[type]) { | |
261 | errstr = "not locked"; | |
262 | goto err; | |
263 | } | |
264 | ||
265 | if (modes[type] != rw) { | |
266 | errstr = (rw == CRYPTO_READ) ? | |
267 | "CRYPTO_r_unlock on write lock" : | |
268 | "CRYPTO_w_unlock on read lock"; | |
269 | } | |
270 | ||
271 | modes[type] = 0; | |
272 | } else { | |
273 | errstr = "invalid mode"; | |
274 | goto err; | |
275 | } | |
3ac82faa BM |
276 | |
277 | err: | |
0f113f3e | 278 | if (errstr) { |
7768e116 RS |
279 | BIO_printf(bio_err, |
280 | "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n", | |
281 | errstr, mode, type, file, line); | |
0f113f3e MC |
282 | } |
283 | } | |
3ac82faa | 284 | |
7e1b7485 RS |
285 | #if defined( OPENSSL_SYS_VMS) |
286 | extern char **copy_argv(int *argc, char **argv); | |
287 | #endif | |
288 | ||
289 | int main(int argc, char *argv[]) | |
0f113f3e | 290 | { |
0f113f3e | 291 | FUNCTION f, *fp; |
0f113f3e | 292 | LHASH_OF(FUNCTION) *prog = NULL; |
7e1b7485 | 293 | char **copied_argv = NULL; |
cc01d217 | 294 | char *p, *pname; |
7e1b7485 RS |
295 | char buf[1024]; |
296 | const char *prompt; | |
297 | ARGS arg; | |
298 | int first, n, i, ret = 0; | |
8ecef24a | 299 | |
7e1b7485 RS |
300 | arg.argv = NULL; |
301 | arg.size = 0; | |
302 | ||
7768e116 RS |
303 | /* Set up some of the environment. */ |
304 | default_config_file = make_config_name(); | |
a60994df RL |
305 | bio_in = dup_bio_in(FORMAT_TEXT); |
306 | bio_out = dup_bio_out(FORMAT_TEXT); | |
7768e116 RS |
307 | bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); |
308 | ||
7e1b7485 RS |
309 | #if defined( OPENSSL_SYS_VMS) |
310 | copied_argv = argv = copy_argv(&argc, argv); | |
311 | #endif | |
312 | ||
313 | p = getenv("OPENSSL_DEBUG_MEMORY"); | |
314 | if (p == NULL) | |
315 | /* if not set, use compiled-in default */ | |
316 | ; | |
317 | else if (strcmp(p, "off") != 0) { | |
318 | CRYPTO_malloc_debug_init(); | |
319 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | |
0f113f3e | 320 | } else { |
7e1b7485 | 321 | CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); |
0f113f3e MC |
322 | } |
323 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
75d0ebef | 324 | CRYPTO_set_locking_callback(lock_dbg_cb); |
3ac82faa | 325 | |
0f113f3e | 326 | if (getenv("OPENSSL_FIPS")) { |
856650de | 327 | #ifdef OPENSSL_FIPS |
0f113f3e MC |
328 | if (!FIPS_mode_set(1)) { |
329 | ERR_load_crypto_strings(); | |
7768e116 RS |
330 | ERR_print_errors(bio_err); |
331 | return 1; | |
0f113f3e | 332 | } |
856650de | 333 | #else |
7768e116 RS |
334 | BIO_printf(bio_err, "FIPS mode not supported.\n"); |
335 | return 1; | |
856650de | 336 | #endif |
0f113f3e MC |
337 | } |
338 | ||
a0a82324 MC |
339 | if (!apps_startup()) |
340 | goto end; | |
341 | ||
7e1b7485 RS |
342 | prog = prog_init(); |
343 | pname = opt_progname(argv[0]); | |
0f113f3e | 344 | |
0f113f3e | 345 | /* first check the program name */ |
0f113f3e MC |
346 | f.name = pname; |
347 | fp = lh_FUNCTION_retrieve(prog, &f); | |
348 | if (fp != NULL) { | |
7e1b7485 RS |
349 | argv[0] = pname; |
350 | ret = fp->func(argc, argv); | |
0f113f3e MC |
351 | goto end; |
352 | } | |
353 | ||
7e1b7485 RS |
354 | /* If there is stuff on the command line, run with that. */ |
355 | if (argc != 1) { | |
356 | argc--; | |
357 | argv++; | |
358 | ret = do_cmd(prog, argc, argv); | |
0f113f3e MC |
359 | if (ret < 0) |
360 | ret = 0; | |
361 | goto end; | |
362 | } | |
363 | ||
7e1b7485 | 364 | /* ok, lets enter interactive mode */ |
0f113f3e MC |
365 | for (;;) { |
366 | ret = 0; | |
57d5edad RS |
367 | /* Read a line, continue reading if line ends with \ */ |
368 | for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) { | |
1c9c2435 | 369 | prompt = first ? "OpenSSL> " : "> "; |
0f113f3e | 370 | p[0] = '\0'; |
57d5edad | 371 | #ifndef READLINE |
0f113f3e MC |
372 | fputs(prompt, stdout); |
373 | fflush(stdout); | |
374 | if (!fgets(p, n, stdin)) | |
375 | goto end; | |
376 | if (p[0] == '\0') | |
377 | goto end; | |
378 | i = strlen(p); | |
379 | if (i <= 1) | |
380 | break; | |
381 | if (p[i - 2] != '\\') | |
382 | break; | |
383 | i -= 2; | |
384 | p += i; | |
385 | n -= i; | |
57d5edad RS |
386 | #else |
387 | { | |
388 | extern char *readline(const char *); | |
389 | extern void add_history(const char *cp); | |
390 | char *text; | |
391 | ||
392 | char *text = readline(prompt); | |
393 | if (text == NULL) | |
394 | goto end; | |
395 | i = strlen(text); | |
396 | if (i == 0 || i > n) | |
397 | break; | |
398 | if (text[i - 1] != '\\') { | |
399 | p += strlen(strcpy(p, text)); | |
400 | free(text); | |
401 | add_history(buf); | |
402 | break; | |
403 | } | |
404 | ||
405 | text[i - 1] = '\0'; | |
406 | p += strlen(strcpy(p, text)); | |
407 | free(text); | |
408 | n -= i; | |
409 | } | |
410 | #endif | |
0f113f3e | 411 | } |
57d5edad | 412 | |
7e1b7485 RS |
413 | if (!chopup_args(&arg, buf)) { |
414 | BIO_printf(bio_err, "Can't parse (no memory?)\n"); | |
0f113f3e | 415 | break; |
7e1b7485 | 416 | } |
0f113f3e | 417 | |
7e1b7485 RS |
418 | ret = do_cmd(prog, arg.argc, arg.argv); |
419 | if (ret == EXIT_THE_PROGRAM) { | |
0f113f3e MC |
420 | ret = 0; |
421 | goto end; | |
422 | } | |
423 | if (ret != 0) | |
7e1b7485 RS |
424 | BIO_printf(bio_err, "error in %s\n", arg.argv[0]); |
425 | (void)BIO_flush(bio_out); | |
0f113f3e MC |
426 | (void)BIO_flush(bio_err); |
427 | } | |
0f113f3e MC |
428 | ret = 1; |
429 | end: | |
b548a1f1 | 430 | OPENSSL_free(copied_argv); |
cc01d217 | 431 | OPENSSL_free(default_config_file); |
25aaa98a RS |
432 | NCONF_free(config); |
433 | config = NULL; | |
434 | lh_FUNCTION_free(prog); | |
b548a1f1 | 435 | OPENSSL_free(arg.argv); |
0f113f3e | 436 | |
7e1b7485 RS |
437 | BIO_free(bio_in); |
438 | BIO_free_all(bio_out); | |
0f113f3e | 439 | apps_shutdown(); |
31b222da | 440 | CRYPTO_mem_leaks(bio_err); |
ca3a82c3 | 441 | BIO_free(bio_err); |
7e1b7485 RS |
442 | return (ret); |
443 | } | |
444 | ||
445 | OPTIONS exit_options[] = { | |
446 | {NULL} | |
447 | }; | |
448 | ||
2f58faad RS |
449 | static void list_cipher_fn(const EVP_CIPHER *c, |
450 | const char *from, const char *to, void *arg) | |
451 | { | |
452 | if (c) | |
453 | BIO_printf(arg, "%s\n", EVP_CIPHER_name(c)); | |
454 | else { | |
455 | if (!from) | |
456 | from = "<undefined>"; | |
457 | if (!to) | |
458 | to = "<undefined>"; | |
459 | BIO_printf(arg, "%s => %s\n", from, to); | |
460 | } | |
461 | } | |
462 | ||
463 | static void list_md_fn(const EVP_MD *m, | |
464 | const char *from, const char *to, void *arg) | |
465 | { | |
466 | if (m) | |
467 | BIO_printf(arg, "%s\n", EVP_MD_name(m)); | |
468 | else { | |
469 | if (!from) | |
470 | from = "<undefined>"; | |
471 | if (!to) | |
472 | to = "<undefined>"; | |
473 | BIO_printf((BIO *)arg, "%s => %s\n", from, to); | |
474 | } | |
475 | } | |
476 | ||
7e1b7485 RS |
477 | /* Unified enum for help and list commands. */ |
478 | typedef enum HELPLIST_CHOICE { | |
479 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
480 | OPT_COMMANDS, OPT_DIGEST_COMMANDS, | |
481 | OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS, | |
a760a380 | 482 | OPT_PK_ALGORITHMS, OPT_DISABLED |
7e1b7485 RS |
483 | } HELPLIST_CHOICE; |
484 | ||
485 | OPTIONS list_options[] = { | |
486 | {"help", OPT_HELP, '-', "Display this summary"}, | |
487 | {"commands", OPT_COMMANDS, '-', "List of standard commands"}, | |
488 | {"digest-commands", OPT_DIGEST_COMMANDS, '-', | |
489 | "List of message digest commands"}, | |
490 | {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-', | |
491 | "List of message digest algorithms"}, | |
492 | {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"}, | |
493 | {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-', | |
494 | "List of cipher algorithms"}, | |
495 | {"public-key-algorithms", OPT_PK_ALGORITHMS, '-', | |
496 | "List of public key algorithms"}, | |
a760a380 DSH |
497 | {"disabled", OPT_DISABLED, '-', |
498 | "List of disabled features"}, | |
7e1b7485 RS |
499 | {NULL} |
500 | }; | |
501 | ||
502 | int list_main(int argc, char **argv) | |
503 | { | |
504 | char *prog; | |
505 | HELPLIST_CHOICE o; | |
19948cea | 506 | int done = 0; |
7e1b7485 RS |
507 | |
508 | prog = opt_init(argc, argv, list_options); | |
509 | while ((o = opt_next()) != OPT_EOF) { | |
510 | switch (o) { | |
19948cea | 511 | case OPT_EOF: /* Never hit, but suppresses warning */ |
7e1b7485 RS |
512 | case OPT_ERR: |
513 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
514 | return 1; | |
515 | case OPT_HELP: | |
516 | opt_help(list_options); | |
517 | break; | |
518 | case OPT_COMMANDS: | |
2f58faad RS |
519 | list_type(FT_general); |
520 | break; | |
7e1b7485 | 521 | case OPT_DIGEST_COMMANDS: |
2f58faad RS |
522 | list_type(FT_md); |
523 | break; | |
7e1b7485 | 524 | case OPT_DIGEST_ALGORITHMS: |
2f58faad RS |
525 | EVP_MD_do_all_sorted(list_md_fn, bio_out); |
526 | break; | |
7e1b7485 | 527 | case OPT_CIPHER_COMMANDS: |
2f58faad RS |
528 | list_type(FT_cipher); |
529 | break; | |
7e1b7485 | 530 | case OPT_CIPHER_ALGORITHMS: |
2f58faad RS |
531 | EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out); |
532 | break; | |
7e1b7485 | 533 | case OPT_PK_ALGORITHMS: |
2f58faad RS |
534 | list_pkey(); |
535 | break; | |
a760a380 DSH |
536 | case OPT_DISABLED: |
537 | list_disabled(); | |
538 | break; | |
7e1b7485 | 539 | } |
19948cea BL |
540 | done = 1; |
541 | } | |
542 | ||
543 | if (!done) { | |
544 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
545 | return 1; | |
7e1b7485 RS |
546 | } |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | OPTIONS help_options[] = { | |
552 | {"help", OPT_HELP, '-', "Display this summary"}, | |
553 | {NULL} | |
554 | }; | |
555 | ||
556 | int help_main(int argc, char **argv) | |
557 | { | |
558 | FUNCTION *fp; | |
559 | int i, nl; | |
560 | FUNC_TYPE tp; | |
561 | char *prog; | |
562 | HELPLIST_CHOICE o; | |
563 | ||
564 | prog = opt_init(argc, argv, help_options); | |
565 | while ((o = opt_next()) != OPT_EOF) { | |
566 | switch (o) { | |
567 | default: | |
568 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
569 | return 1; | |
570 | case OPT_HELP: | |
571 | opt_help(help_options); | |
572 | return 0; | |
573 | } | |
574 | } | |
575 | argc = opt_num_rest(); | |
576 | argv = opt_rest(); | |
577 | ||
578 | if (argc != 0) { | |
579 | BIO_printf(bio_err, "Usage: %s\n", prog); | |
580 | return 1; | |
581 | } | |
582 | ||
583 | BIO_printf(bio_err, "\nStandard commands"); | |
584 | i = 0; | |
585 | tp = FT_none; | |
586 | for (fp = functions; fp->name != NULL; fp++) { | |
587 | nl = 0; | |
588 | if (((i++) % COLUMNS) == 0) { | |
589 | BIO_printf(bio_err, "\n"); | |
590 | nl = 1; | |
591 | } | |
592 | if (fp->type != tp) { | |
593 | tp = fp->type; | |
594 | if (!nl) | |
595 | BIO_printf(bio_err, "\n"); | |
596 | if (tp == FT_md) { | |
597 | i = 1; | |
598 | BIO_printf(bio_err, | |
599 | "\nMessage Digest commands (see the `dgst' command for more details)\n"); | |
600 | } else if (tp == FT_cipher) { | |
601 | i = 1; | |
602 | BIO_printf(bio_err, | |
603 | "\nCipher commands (see the `enc' command for more details)\n"); | |
604 | } | |
605 | } | |
606 | BIO_printf(bio_err, FORMAT, fp->name); | |
607 | } | |
608 | BIO_printf(bio_err, "\n\n"); | |
609 | return 0; | |
610 | } | |
8c00f4cf | 611 | |
7e1b7485 RS |
612 | int exit_main(int argc, char **argv) |
613 | { | |
614 | return EXIT_THE_PROGRAM; | |
0f113f3e | 615 | } |
d02b48c6 | 616 | |
2f58faad | 617 | static void list_type(FUNC_TYPE ft) |
7e1b7485 RS |
618 | { |
619 | FUNCTION *fp; | |
620 | int i = 0; | |
621 | ||
622 | for (fp = functions; fp->name != NULL; fp++) | |
2f58faad | 623 | if (fp->type == ft) { |
7e1b7485 RS |
624 | if ((i++ % COLUMNS) == 0) |
625 | BIO_printf(bio_out, "\n"); | |
626 | BIO_printf(bio_out, FORMAT, fp->name); | |
627 | } | |
628 | BIO_printf(bio_out, "\n"); | |
7e1b7485 | 629 | } |
fc8ee06b | 630 | |
3c1d6bbc | 631 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]) |
0f113f3e MC |
632 | { |
633 | FUNCTION f, *fp; | |
0f113f3e | 634 | |
7e1b7485 RS |
635 | if (argc <= 0 || argv[0] == NULL) |
636 | return (0); | |
0f113f3e MC |
637 | f.name = argv[0]; |
638 | fp = lh_FUNCTION_retrieve(prog, &f); | |
639 | if (fp == NULL) { | |
640 | if (EVP_get_digestbyname(argv[0])) { | |
7e1b7485 | 641 | f.type = FT_md; |
0f113f3e MC |
642 | f.func = dgst_main; |
643 | fp = &f; | |
644 | } else if (EVP_get_cipherbyname(argv[0])) { | |
7e1b7485 | 645 | f.type = FT_cipher; |
0f113f3e MC |
646 | f.func = enc_main; |
647 | fp = &f; | |
648 | } | |
649 | } | |
650 | if (fp != NULL) { | |
7e1b7485 RS |
651 | return (fp->func(argc, argv)); |
652 | } | |
653 | if ((strncmp(argv[0], "no-", 3)) == 0) { | |
654 | /* | |
655 | * User is asking if foo is unsupported, by trying to "run" the | |
656 | * no-foo command. Strange. | |
657 | */ | |
0f113f3e | 658 | f.name = argv[0] + 3; |
7e1b7485 RS |
659 | if (lh_FUNCTION_retrieve(prog, &f) == NULL) { |
660 | BIO_printf(bio_out, "%s\n", argv[0]); | |
661 | return (0); | |
0f113f3e | 662 | } |
7e1b7485 RS |
663 | BIO_printf(bio_out, "%s\n", argv[0] + 3); |
664 | return 1; | |
50acf46b | 665 | } |
7e1b7485 RS |
666 | if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 || |
667 | strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0) | |
668 | /* Special value to mean "exit the program. */ | |
669 | return EXIT_THE_PROGRAM; | |
0f113f3e | 670 | |
7e1b7485 RS |
671 | BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n", |
672 | argv[0]); | |
673 | return (1); | |
0f113f3e | 674 | } |
50acf46b | 675 | |
2f58faad | 676 | static void list_pkey(void) |
0f113f3e MC |
677 | { |
678 | int i; | |
7e1b7485 | 679 | |
0f113f3e MC |
680 | for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { |
681 | const EVP_PKEY_ASN1_METHOD *ameth; | |
682 | int pkey_id, pkey_base_id, pkey_flags; | |
683 | const char *pinfo, *pem_str; | |
684 | ameth = EVP_PKEY_asn1_get0(i); | |
685 | EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags, | |
686 | &pinfo, &pem_str, ameth); | |
687 | if (pkey_flags & ASN1_PKEY_ALIAS) { | |
7e1b7485 RS |
688 | BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id)); |
689 | BIO_printf(bio_out, "\tAlias for: %s\n", | |
0f113f3e MC |
690 | OBJ_nid2ln(pkey_base_id)); |
691 | } else { | |
7e1b7485 RS |
692 | BIO_printf(bio_out, "Name: %s\n", pinfo); |
693 | BIO_printf(bio_out, "\tType: %s Algorithm\n", | |
0f113f3e MC |
694 | pkey_flags & ASN1_PKEY_DYNAMIC ? |
695 | "External" : "Builtin"); | |
7e1b7485 | 696 | BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id)); |
0f113f3e MC |
697 | if (pem_str == NULL) |
698 | pem_str = "(none)"; | |
7e1b7485 | 699 | BIO_printf(bio_out, "\tPEM string: %s\n", pem_str); |
0f113f3e MC |
700 | } |
701 | ||
702 | } | |
0f113f3e | 703 | } |
3c1d6bbc | 704 | |
0f113f3e MC |
705 | static int function_cmp(const FUNCTION * a, const FUNCTION * b) |
706 | { | |
707 | return strncmp(a->name, b->name, 8); | |
708 | } | |
50acf46b | 709 | |
0f113f3e | 710 | static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION) |
d02b48c6 | 711 | |
0f113f3e MC |
712 | static unsigned long function_hash(const FUNCTION * a) |
713 | { | |
714 | return lh_strhash(a->name); | |
715 | } | |
d02b48c6 | 716 | |
0f113f3e | 717 | static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION) |
d02b48c6 | 718 | |
7e1b7485 RS |
719 | static int SortFnByName(const void *_f1, const void *_f2) |
720 | { | |
721 | const FUNCTION *f1 = _f1; | |
722 | const FUNCTION *f2 = _f2; | |
723 | ||
724 | if (f1->type != f2->type) | |
725 | return f1->type - f2->type; | |
726 | return strcmp(f1->name, f2->name); | |
727 | } | |
728 | ||
a760a380 DSH |
729 | static void list_disabled(void) |
730 | { | |
d230bd1d | 731 | BIO_puts(bio_out, "Disabled algorithms:\n"); |
27dae1b0 RL |
732 | #ifdef OPENSSL_NO_AES |
733 | BIO_puts(bio_out, "AES\n"); | |
734 | #endif | |
735 | #ifdef OPENSSL_NO_BF | |
736 | BIO_puts(bio_out, "BF\n"); | |
737 | #endif | |
738 | #ifdef OPENSSL_NO_CAMELLIA | |
739 | BIO_puts(bio_out, "CAMELLIA\n"); | |
740 | #endif | |
741 | #ifdef OPENSSL_NO_CAST | |
742 | BIO_puts(bio_out, "CAST\n"); | |
743 | #endif | |
56c1ef05 RL |
744 | #ifdef OPENSSL_NO_CMAC |
745 | BIO_puts(bio_out, "CMAC\n"); | |
746 | #endif | |
27dae1b0 RL |
747 | #ifdef OPENSSL_NO_CMS |
748 | BIO_puts(bio_out, "CMS\n"); | |
749 | #endif | |
66b14bab RL |
750 | #ifdef OPENSSL_NO_COMP |
751 | BIO_puts(bio_out, "COMP\n"); | |
752 | #endif | |
27dae1b0 RL |
753 | #ifdef OPENSSL_NO_DES |
754 | BIO_puts(bio_out, "DES\n"); | |
755 | #endif | |
2df84dd3 RL |
756 | #ifdef OPENSSL_NO_DGRAM |
757 | BIO_puts(bio_out, "DGRAM\n"); | |
758 | #endif | |
a760a380 DSH |
759 | #ifdef OPENSSL_NO_DH |
760 | BIO_puts(bio_out, "DH\n"); | |
761 | #endif | |
762 | #ifdef OPENSSL_NO_DSA | |
763 | BIO_puts(bio_out, "DSA\n"); | |
764 | #endif | |
a5ecdc6a KR |
765 | #if defined(OPENSSL_NO_DTLS) |
766 | BIO_puts(bio_out, "DTLS\n"); | |
66b14bab | 767 | #endif |
a760a380 DSH |
768 | #ifdef OPENSSL_NO_EC |
769 | BIO_puts(bio_out, "EC\n"); | |
770 | #endif | |
771 | #ifdef OPENSSL_NO_EC2M | |
772 | BIO_puts(bio_out, "EC2M\n"); | |
773 | #endif | |
27dae1b0 RL |
774 | #ifdef OPENSSL_NO_ENGINE |
775 | BIO_puts(bio_out, "ENGINE\n"); | |
776 | #endif | |
2df84dd3 RL |
777 | #ifdef OPENSSL_NO_GOST |
778 | BIO_puts(bio_out, "GOST\n"); | |
779 | #endif | |
56c1ef05 RL |
780 | #ifdef OPENSSL_NO_HMAC |
781 | BIO_puts(bio_out, "HMAC\n"); | |
782 | #endif | |
27dae1b0 RL |
783 | #ifdef OPENSSL_NO_IDEA |
784 | BIO_puts(bio_out, "IDEA\n"); | |
785 | #endif | |
66b14bab RL |
786 | #ifdef OPENSSL_NO_JPAKE |
787 | BIO_puts(bio_out, "JPAKE\n"); | |
788 | #endif | |
27dae1b0 RL |
789 | #ifdef OPENSSL_NO_MD2 |
790 | BIO_puts(bio_out, "MD2\n"); | |
791 | #endif | |
792 | #ifdef OPENSSL_NO_MD4 | |
793 | BIO_puts(bio_out, "MD4\n"); | |
794 | #endif | |
795 | #ifdef OPENSSL_NO_MD5 | |
796 | BIO_puts(bio_out, "MD5\n"); | |
797 | #endif | |
798 | #ifdef OPENSSL_NO_MDC2 | |
799 | BIO_puts(bio_out, "MDC2\n"); | |
800 | #endif | |
2df84dd3 RL |
801 | #ifdef OPENSSL_NO_OCB |
802 | BIO_puts(bio_out, "OCB\n"); | |
803 | #endif | |
27dae1b0 RL |
804 | #ifdef OPENSSL_NO_OCSP |
805 | BIO_puts(bio_out, "OCSP\n"); | |
806 | #endif | |
a760a380 DSH |
807 | #ifdef OPENSSL_NO_PSK |
808 | BIO_puts(bio_out, "PSK\n"); | |
809 | #endif | |
27dae1b0 RL |
810 | #ifdef OPENSSL_NO_RC2 |
811 | BIO_puts(bio_out, "RC2\n"); | |
812 | #endif | |
813 | #ifdef OPENSSL_NO_RC4 | |
814 | BIO_puts(bio_out, "RC4\n"); | |
815 | #endif | |
816 | #ifdef OPENSSL_NO_RC5 | |
817 | BIO_puts(bio_out, "RC5\n"); | |
818 | #endif | |
819 | #ifdef OPENSSL_NO_RMD160 | |
820 | BIO_puts(bio_out, "RMD160\n"); | |
821 | #endif | |
d230bd1d RL |
822 | #ifdef OPENSSL_NO_RSA |
823 | BIO_puts(bio_out, "RSA\n"); | |
824 | #endif | |
66b14bab RL |
825 | #ifdef OPENSSL_NO_SCRYPT |
826 | BIO_puts(bio_out, "SCRYPT\n"); | |
827 | #endif | |
2df84dd3 RL |
828 | #ifdef OPENSSL_NO_SCT |
829 | BIO_puts(bio_out, "SCT\n"); | |
830 | #endif | |
831 | #ifdef OPENSSL_NO_SCTP | |
832 | BIO_puts(bio_out, "SCTP\n"); | |
833 | #endif | |
27dae1b0 RL |
834 | #ifdef OPENSSL_NO_SEED |
835 | BIO_puts(bio_out, "SEED\n"); | |
836 | #endif | |
2df84dd3 RL |
837 | #ifdef OPENSSL_NO_SHA |
838 | BIO_puts(bio_out, "SHA\n"); | |
839 | #endif | |
27dae1b0 RL |
840 | #ifdef OPENSSL_NO_SOCK |
841 | BIO_puts(bio_out, "SOCK\n"); | |
842 | #endif | |
a760a380 DSH |
843 | #ifdef OPENSSL_NO_SRP |
844 | BIO_puts(bio_out, "SRP\n"); | |
845 | #endif | |
66b14bab RL |
846 | #ifdef OPENSSL_NO_SRTP |
847 | BIO_puts(bio_out, "SRTP\n"); | |
848 | #endif | |
849 | #ifdef OPENSSL_NO_SSL3 | |
850 | BIO_puts(bio_out, "SSL3\n"); | |
851 | #endif | |
27dae1b0 RL |
852 | #ifdef OPENSSL_NO_WHIRLPOOL |
853 | BIO_puts(bio_out, "WHIRLPOOL\n"); | |
854 | #endif | |
d230bd1d RL |
855 | #ifndef ZLIB |
856 | BIO_puts(bio_out, "ZLIB\n"); | |
857 | #endif | |
a760a380 DSH |
858 | } |
859 | ||
0f113f3e MC |
860 | static LHASH_OF(FUNCTION) *prog_init(void) |
861 | { | |
862 | LHASH_OF(FUNCTION) *ret; | |
863 | FUNCTION *f; | |
864 | size_t i; | |
865 | ||
7e1b7485 | 866 | /* Sort alphabetically within category. For nicer help displays. */ |
0f113f3e | 867 | for (i = 0, f = functions; f->name != NULL; ++f, ++i) ; |
b4faea50 | 868 | qsort(functions, i, sizeof(*functions), SortFnByName); |
0f113f3e MC |
869 | |
870 | if ((ret = lh_FUNCTION_new()) == NULL) | |
871 | return (NULL); | |
872 | ||
873 | for (f = functions; f->name != NULL; f++) | |
874 | (void)lh_FUNCTION_insert(ret, f); | |
875 | return (ret); | |
876 | } |