]>
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 RS |
168 | |
169 | static void apps_startup() | |
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(); | |
177 | OpenSSL_add_all_algorithms(); | |
178 | OpenSSL_add_ssl_algorithms(); | |
cc01d217 | 179 | OPENSSL_load_builtin_modules(); |
7e1b7485 RS |
180 | setup_ui_method(); |
181 | /*SSL_library_init();*/ | |
182 | #ifndef OPENSSL_NO_ENGINE | |
183 | ENGINE_load_builtin_engines(); | |
184 | #endif | |
185 | } | |
186 | ||
187 | static void apps_shutdown() | |
188 | { | |
189 | #ifndef OPENSSL_NO_ENGINE | |
190 | ENGINE_cleanup(); | |
191 | #endif | |
192 | destroy_ui_method(); | |
193 | CONF_modules_unload(1); | |
194 | #ifndef OPENSSL_NO_COMP | |
195 | COMP_zlib_cleanup(); | |
45d051c8 | 196 | SSL_COMP_free_compression_methods(); |
7e1b7485 RS |
197 | #endif |
198 | OBJ_cleanup(); | |
199 | EVP_cleanup(); | |
200 | CRYPTO_cleanup_all_ex_data(); | |
201 | ERR_remove_thread_state(NULL); | |
202 | RAND_cleanup(); | |
203 | ERR_free_strings(); | |
204 | } | |
205 | ||
206 | static char *make_config_name() | |
207 | { | |
cc01d217 | 208 | const char *t; |
7e1b7485 RS |
209 | size_t len; |
210 | char *p; | |
211 | ||
cc01d217 RS |
212 | if ((t = getenv("OPENSSL_CONF")) != NULL |
213 | || (t = getenv("SSLEAY_CONF")) != NULL) | |
214 | return BUF_strdup(t); | |
215 | ||
216 | t = X509_get_default_cert_area(); | |
217 | len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1; | |
68dc6824 | 218 | p = app_malloc(len, "config filename buffer"); |
cc01d217 | 219 | strcpy(p, t); |
7e1b7485 | 220 | #ifndef OPENSSL_SYS_VMS |
cc01d217 | 221 | strcat(p, "/"); |
d02b48c6 | 222 | #endif |
cc01d217 | 223 | strcat(p, OPENSSL_CONF); |
7e1b7485 RS |
224 | |
225 | return p; | |
226 | } | |
227 | ||
3ac82faa | 228 | static void lock_dbg_cb(int mode, int type, const char *file, int line) |
0f113f3e | 229 | { |
7e1b7485 | 230 | static int modes[CRYPTO_NUM_LOCKS]; |
0f113f3e | 231 | const char *errstr = NULL; |
7e1b7485 | 232 | int rw = mode & (CRYPTO_READ | CRYPTO_WRITE); |
0f113f3e | 233 | |
7e1b7485 | 234 | if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) { |
0f113f3e MC |
235 | errstr = "invalid mode"; |
236 | goto err; | |
237 | } | |
238 | ||
239 | if (type < 0 || type >= CRYPTO_NUM_LOCKS) { | |
240 | errstr = "type out of bounds"; | |
241 | goto err; | |
242 | } | |
243 | ||
244 | if (mode & CRYPTO_LOCK) { | |
245 | if (modes[type]) { | |
246 | errstr = "already locked"; | |
7e1b7485 | 247 | /* must not happen in a single-threaded program --> deadlock! */ |
0f113f3e MC |
248 | goto err; |
249 | } | |
0f113f3e MC |
250 | modes[type] = rw; |
251 | } else if (mode & CRYPTO_UNLOCK) { | |
252 | if (!modes[type]) { | |
253 | errstr = "not locked"; | |
254 | goto err; | |
255 | } | |
256 | ||
257 | if (modes[type] != rw) { | |
258 | errstr = (rw == CRYPTO_READ) ? | |
259 | "CRYPTO_r_unlock on write lock" : | |
260 | "CRYPTO_w_unlock on read lock"; | |
261 | } | |
262 | ||
263 | modes[type] = 0; | |
264 | } else { | |
265 | errstr = "invalid mode"; | |
266 | goto err; | |
267 | } | |
3ac82faa BM |
268 | |
269 | err: | |
0f113f3e | 270 | if (errstr) { |
7768e116 RS |
271 | BIO_printf(bio_err, |
272 | "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n", | |
273 | errstr, mode, type, file, line); | |
0f113f3e MC |
274 | } |
275 | } | |
3ac82faa | 276 | |
7e1b7485 RS |
277 | #if defined( OPENSSL_SYS_VMS) |
278 | extern char **copy_argv(int *argc, char **argv); | |
279 | #endif | |
280 | ||
281 | int main(int argc, char *argv[]) | |
0f113f3e | 282 | { |
0f113f3e | 283 | FUNCTION f, *fp; |
0f113f3e | 284 | LHASH_OF(FUNCTION) *prog = NULL; |
7e1b7485 | 285 | char **copied_argv = NULL; |
cc01d217 | 286 | char *p, *pname; |
7e1b7485 RS |
287 | char buf[1024]; |
288 | const char *prompt; | |
289 | ARGS arg; | |
290 | int first, n, i, ret = 0; | |
8ecef24a | 291 | |
7e1b7485 RS |
292 | arg.argv = NULL; |
293 | arg.size = 0; | |
294 | ||
7768e116 RS |
295 | /* Set up some of the environment. */ |
296 | default_config_file = make_config_name(); | |
a60994df RL |
297 | bio_in = dup_bio_in(FORMAT_TEXT); |
298 | bio_out = dup_bio_out(FORMAT_TEXT); | |
7768e116 RS |
299 | bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT); |
300 | ||
7e1b7485 RS |
301 | #if defined( OPENSSL_SYS_VMS) |
302 | copied_argv = argv = copy_argv(&argc, argv); | |
303 | #endif | |
304 | ||
305 | p = getenv("OPENSSL_DEBUG_MEMORY"); | |
306 | if (p == NULL) | |
307 | /* if not set, use compiled-in default */ | |
308 | ; | |
309 | else if (strcmp(p, "off") != 0) { | |
310 | CRYPTO_malloc_debug_init(); | |
311 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | |
0f113f3e | 312 | } else { |
7e1b7485 | 313 | CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); |
0f113f3e MC |
314 | } |
315 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
75d0ebef | 316 | CRYPTO_set_locking_callback(lock_dbg_cb); |
3ac82faa | 317 | |
0f113f3e | 318 | if (getenv("OPENSSL_FIPS")) { |
856650de | 319 | #ifdef OPENSSL_FIPS |
0f113f3e MC |
320 | if (!FIPS_mode_set(1)) { |
321 | ERR_load_crypto_strings(); | |
7768e116 RS |
322 | ERR_print_errors(bio_err); |
323 | return 1; | |
0f113f3e | 324 | } |
856650de | 325 | #else |
7768e116 RS |
326 | BIO_printf(bio_err, "FIPS mode not supported.\n"); |
327 | return 1; | |
856650de | 328 | #endif |
0f113f3e MC |
329 | } |
330 | ||
331 | apps_startup(); | |
7e1b7485 RS |
332 | prog = prog_init(); |
333 | pname = opt_progname(argv[0]); | |
0f113f3e | 334 | |
0f113f3e | 335 | /* first check the program name */ |
0f113f3e MC |
336 | f.name = pname; |
337 | fp = lh_FUNCTION_retrieve(prog, &f); | |
338 | if (fp != NULL) { | |
7e1b7485 RS |
339 | argv[0] = pname; |
340 | ret = fp->func(argc, argv); | |
0f113f3e MC |
341 | goto end; |
342 | } | |
343 | ||
7e1b7485 RS |
344 | /* If there is stuff on the command line, run with that. */ |
345 | if (argc != 1) { | |
346 | argc--; | |
347 | argv++; | |
348 | ret = do_cmd(prog, argc, argv); | |
0f113f3e MC |
349 | if (ret < 0) |
350 | ret = 0; | |
351 | goto end; | |
352 | } | |
353 | ||
7e1b7485 | 354 | /* ok, lets enter interactive mode */ |
0f113f3e MC |
355 | for (;;) { |
356 | ret = 0; | |
57d5edad RS |
357 | /* Read a line, continue reading if line ends with \ */ |
358 | for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) { | |
1c9c2435 | 359 | prompt = first ? "OpenSSL> " : "> "; |
0f113f3e | 360 | p[0] = '\0'; |
57d5edad | 361 | #ifndef READLINE |
0f113f3e MC |
362 | fputs(prompt, stdout); |
363 | fflush(stdout); | |
364 | if (!fgets(p, n, stdin)) | |
365 | goto end; | |
366 | if (p[0] == '\0') | |
367 | goto end; | |
368 | i = strlen(p); | |
369 | if (i <= 1) | |
370 | break; | |
371 | if (p[i - 2] != '\\') | |
372 | break; | |
373 | i -= 2; | |
374 | p += i; | |
375 | n -= i; | |
57d5edad RS |
376 | #else |
377 | { | |
378 | extern char *readline(const char *); | |
379 | extern void add_history(const char *cp); | |
380 | char *text; | |
381 | ||
382 | char *text = readline(prompt); | |
383 | if (text == NULL) | |
384 | goto end; | |
385 | i = strlen(text); | |
386 | if (i == 0 || i > n) | |
387 | break; | |
388 | if (text[i - 1] != '\\') { | |
389 | p += strlen(strcpy(p, text)); | |
390 | free(text); | |
391 | add_history(buf); | |
392 | break; | |
393 | } | |
394 | ||
395 | text[i - 1] = '\0'; | |
396 | p += strlen(strcpy(p, text)); | |
397 | free(text); | |
398 | n -= i; | |
399 | } | |
400 | #endif | |
0f113f3e | 401 | } |
57d5edad | 402 | |
7e1b7485 RS |
403 | if (!chopup_args(&arg, buf)) { |
404 | BIO_printf(bio_err, "Can't parse (no memory?)\n"); | |
0f113f3e | 405 | break; |
7e1b7485 | 406 | } |
0f113f3e | 407 | |
7e1b7485 RS |
408 | ret = do_cmd(prog, arg.argc, arg.argv); |
409 | if (ret == EXIT_THE_PROGRAM) { | |
0f113f3e MC |
410 | ret = 0; |
411 | goto end; | |
412 | } | |
413 | if (ret != 0) | |
7e1b7485 RS |
414 | BIO_printf(bio_err, "error in %s\n", arg.argv[0]); |
415 | (void)BIO_flush(bio_out); | |
0f113f3e MC |
416 | (void)BIO_flush(bio_err); |
417 | } | |
0f113f3e MC |
418 | ret = 1; |
419 | end: | |
b548a1f1 | 420 | OPENSSL_free(copied_argv); |
cc01d217 | 421 | OPENSSL_free(default_config_file); |
25aaa98a RS |
422 | NCONF_free(config); |
423 | config = NULL; | |
424 | lh_FUNCTION_free(prog); | |
b548a1f1 | 425 | OPENSSL_free(arg.argv); |
0f113f3e | 426 | |
7e1b7485 RS |
427 | BIO_free(bio_in); |
428 | BIO_free_all(bio_out); | |
0f113f3e | 429 | apps_shutdown(); |
31b222da | 430 | CRYPTO_mem_leaks(bio_err); |
ca3a82c3 | 431 | BIO_free(bio_err); |
7e1b7485 RS |
432 | return (ret); |
433 | } | |
434 | ||
435 | OPTIONS exit_options[] = { | |
436 | {NULL} | |
437 | }; | |
438 | ||
2f58faad RS |
439 | static void list_cipher_fn(const EVP_CIPHER *c, |
440 | const char *from, const char *to, void *arg) | |
441 | { | |
442 | if (c) | |
443 | BIO_printf(arg, "%s\n", EVP_CIPHER_name(c)); | |
444 | else { | |
445 | if (!from) | |
446 | from = "<undefined>"; | |
447 | if (!to) | |
448 | to = "<undefined>"; | |
449 | BIO_printf(arg, "%s => %s\n", from, to); | |
450 | } | |
451 | } | |
452 | ||
453 | static void list_md_fn(const EVP_MD *m, | |
454 | const char *from, const char *to, void *arg) | |
455 | { | |
456 | if (m) | |
457 | BIO_printf(arg, "%s\n", EVP_MD_name(m)); | |
458 | else { | |
459 | if (!from) | |
460 | from = "<undefined>"; | |
461 | if (!to) | |
462 | to = "<undefined>"; | |
463 | BIO_printf((BIO *)arg, "%s => %s\n", from, to); | |
464 | } | |
465 | } | |
466 | ||
7e1b7485 RS |
467 | /* Unified enum for help and list commands. */ |
468 | typedef enum HELPLIST_CHOICE { | |
469 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
470 | OPT_COMMANDS, OPT_DIGEST_COMMANDS, | |
471 | OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS, | |
a760a380 | 472 | OPT_PK_ALGORITHMS, OPT_DISABLED |
7e1b7485 RS |
473 | } HELPLIST_CHOICE; |
474 | ||
475 | OPTIONS list_options[] = { | |
476 | {"help", OPT_HELP, '-', "Display this summary"}, | |
477 | {"commands", OPT_COMMANDS, '-', "List of standard commands"}, | |
478 | {"digest-commands", OPT_DIGEST_COMMANDS, '-', | |
479 | "List of message digest commands"}, | |
480 | {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-', | |
481 | "List of message digest algorithms"}, | |
482 | {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"}, | |
483 | {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-', | |
484 | "List of cipher algorithms"}, | |
485 | {"public-key-algorithms", OPT_PK_ALGORITHMS, '-', | |
486 | "List of public key algorithms"}, | |
a760a380 DSH |
487 | {"disabled", OPT_DISABLED, '-', |
488 | "List of disabled features"}, | |
7e1b7485 RS |
489 | {NULL} |
490 | }; | |
491 | ||
492 | int list_main(int argc, char **argv) | |
493 | { | |
494 | char *prog; | |
495 | HELPLIST_CHOICE o; | |
19948cea | 496 | int done = 0; |
7e1b7485 RS |
497 | |
498 | prog = opt_init(argc, argv, list_options); | |
499 | while ((o = opt_next()) != OPT_EOF) { | |
500 | switch (o) { | |
19948cea | 501 | case OPT_EOF: /* Never hit, but suppresses warning */ |
7e1b7485 RS |
502 | case OPT_ERR: |
503 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
504 | return 1; | |
505 | case OPT_HELP: | |
506 | opt_help(list_options); | |
507 | break; | |
508 | case OPT_COMMANDS: | |
2f58faad RS |
509 | list_type(FT_general); |
510 | break; | |
7e1b7485 | 511 | case OPT_DIGEST_COMMANDS: |
2f58faad RS |
512 | list_type(FT_md); |
513 | break; | |
7e1b7485 | 514 | case OPT_DIGEST_ALGORITHMS: |
2f58faad RS |
515 | EVP_MD_do_all_sorted(list_md_fn, bio_out); |
516 | break; | |
7e1b7485 | 517 | case OPT_CIPHER_COMMANDS: |
2f58faad RS |
518 | list_type(FT_cipher); |
519 | break; | |
7e1b7485 | 520 | case OPT_CIPHER_ALGORITHMS: |
2f58faad RS |
521 | EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out); |
522 | break; | |
7e1b7485 | 523 | case OPT_PK_ALGORITHMS: |
2f58faad RS |
524 | list_pkey(); |
525 | break; | |
a760a380 DSH |
526 | case OPT_DISABLED: |
527 | list_disabled(); | |
528 | break; | |
7e1b7485 | 529 | } |
19948cea BL |
530 | done = 1; |
531 | } | |
532 | ||
533 | if (!done) { | |
534 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
535 | return 1; | |
7e1b7485 RS |
536 | } |
537 | ||
538 | return 0; | |
539 | } | |
540 | ||
541 | OPTIONS help_options[] = { | |
542 | {"help", OPT_HELP, '-', "Display this summary"}, | |
543 | {NULL} | |
544 | }; | |
545 | ||
546 | int help_main(int argc, char **argv) | |
547 | { | |
548 | FUNCTION *fp; | |
549 | int i, nl; | |
550 | FUNC_TYPE tp; | |
551 | char *prog; | |
552 | HELPLIST_CHOICE o; | |
553 | ||
554 | prog = opt_init(argc, argv, help_options); | |
555 | while ((o = opt_next()) != OPT_EOF) { | |
556 | switch (o) { | |
557 | default: | |
558 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
559 | return 1; | |
560 | case OPT_HELP: | |
561 | opt_help(help_options); | |
562 | return 0; | |
563 | } | |
564 | } | |
565 | argc = opt_num_rest(); | |
566 | argv = opt_rest(); | |
567 | ||
568 | if (argc != 0) { | |
569 | BIO_printf(bio_err, "Usage: %s\n", prog); | |
570 | return 1; | |
571 | } | |
572 | ||
573 | BIO_printf(bio_err, "\nStandard commands"); | |
574 | i = 0; | |
575 | tp = FT_none; | |
576 | for (fp = functions; fp->name != NULL; fp++) { | |
577 | nl = 0; | |
578 | if (((i++) % COLUMNS) == 0) { | |
579 | BIO_printf(bio_err, "\n"); | |
580 | nl = 1; | |
581 | } | |
582 | if (fp->type != tp) { | |
583 | tp = fp->type; | |
584 | if (!nl) | |
585 | BIO_printf(bio_err, "\n"); | |
586 | if (tp == FT_md) { | |
587 | i = 1; | |
588 | BIO_printf(bio_err, | |
589 | "\nMessage Digest commands (see the `dgst' command for more details)\n"); | |
590 | } else if (tp == FT_cipher) { | |
591 | i = 1; | |
592 | BIO_printf(bio_err, | |
593 | "\nCipher commands (see the `enc' command for more details)\n"); | |
594 | } | |
595 | } | |
596 | BIO_printf(bio_err, FORMAT, fp->name); | |
597 | } | |
598 | BIO_printf(bio_err, "\n\n"); | |
599 | return 0; | |
600 | } | |
8c00f4cf | 601 | |
7e1b7485 RS |
602 | int exit_main(int argc, char **argv) |
603 | { | |
604 | return EXIT_THE_PROGRAM; | |
0f113f3e | 605 | } |
d02b48c6 | 606 | |
2f58faad | 607 | static void list_type(FUNC_TYPE ft) |
7e1b7485 RS |
608 | { |
609 | FUNCTION *fp; | |
610 | int i = 0; | |
611 | ||
612 | for (fp = functions; fp->name != NULL; fp++) | |
2f58faad | 613 | if (fp->type == ft) { |
7e1b7485 RS |
614 | if ((i++ % COLUMNS) == 0) |
615 | BIO_printf(bio_out, "\n"); | |
616 | BIO_printf(bio_out, FORMAT, fp->name); | |
617 | } | |
618 | BIO_printf(bio_out, "\n"); | |
7e1b7485 | 619 | } |
fc8ee06b | 620 | |
3c1d6bbc | 621 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]) |
0f113f3e MC |
622 | { |
623 | FUNCTION f, *fp; | |
0f113f3e | 624 | |
7e1b7485 RS |
625 | if (argc <= 0 || argv[0] == NULL) |
626 | return (0); | |
0f113f3e MC |
627 | f.name = argv[0]; |
628 | fp = lh_FUNCTION_retrieve(prog, &f); | |
629 | if (fp == NULL) { | |
630 | if (EVP_get_digestbyname(argv[0])) { | |
7e1b7485 | 631 | f.type = FT_md; |
0f113f3e MC |
632 | f.func = dgst_main; |
633 | fp = &f; | |
634 | } else if (EVP_get_cipherbyname(argv[0])) { | |
7e1b7485 | 635 | f.type = FT_cipher; |
0f113f3e MC |
636 | f.func = enc_main; |
637 | fp = &f; | |
638 | } | |
639 | } | |
640 | if (fp != NULL) { | |
7e1b7485 RS |
641 | return (fp->func(argc, argv)); |
642 | } | |
643 | if ((strncmp(argv[0], "no-", 3)) == 0) { | |
644 | /* | |
645 | * User is asking if foo is unsupported, by trying to "run" the | |
646 | * no-foo command. Strange. | |
647 | */ | |
0f113f3e | 648 | f.name = argv[0] + 3; |
7e1b7485 RS |
649 | if (lh_FUNCTION_retrieve(prog, &f) == NULL) { |
650 | BIO_printf(bio_out, "%s\n", argv[0]); | |
651 | return (0); | |
0f113f3e | 652 | } |
7e1b7485 RS |
653 | BIO_printf(bio_out, "%s\n", argv[0] + 3); |
654 | return 1; | |
50acf46b | 655 | } |
7e1b7485 RS |
656 | if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 || |
657 | strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0) | |
658 | /* Special value to mean "exit the program. */ | |
659 | return EXIT_THE_PROGRAM; | |
0f113f3e | 660 | |
7e1b7485 RS |
661 | BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n", |
662 | argv[0]); | |
663 | return (1); | |
0f113f3e | 664 | } |
50acf46b | 665 | |
2f58faad | 666 | static void list_pkey(void) |
0f113f3e MC |
667 | { |
668 | int i; | |
7e1b7485 | 669 | |
0f113f3e MC |
670 | for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { |
671 | const EVP_PKEY_ASN1_METHOD *ameth; | |
672 | int pkey_id, pkey_base_id, pkey_flags; | |
673 | const char *pinfo, *pem_str; | |
674 | ameth = EVP_PKEY_asn1_get0(i); | |
675 | EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags, | |
676 | &pinfo, &pem_str, ameth); | |
677 | if (pkey_flags & ASN1_PKEY_ALIAS) { | |
7e1b7485 RS |
678 | BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id)); |
679 | BIO_printf(bio_out, "\tAlias for: %s\n", | |
0f113f3e MC |
680 | OBJ_nid2ln(pkey_base_id)); |
681 | } else { | |
7e1b7485 RS |
682 | BIO_printf(bio_out, "Name: %s\n", pinfo); |
683 | BIO_printf(bio_out, "\tType: %s Algorithm\n", | |
0f113f3e MC |
684 | pkey_flags & ASN1_PKEY_DYNAMIC ? |
685 | "External" : "Builtin"); | |
7e1b7485 | 686 | BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id)); |
0f113f3e MC |
687 | if (pem_str == NULL) |
688 | pem_str = "(none)"; | |
7e1b7485 | 689 | BIO_printf(bio_out, "\tPEM string: %s\n", pem_str); |
0f113f3e MC |
690 | } |
691 | ||
692 | } | |
0f113f3e | 693 | } |
3c1d6bbc | 694 | |
0f113f3e MC |
695 | static int function_cmp(const FUNCTION * a, const FUNCTION * b) |
696 | { | |
697 | return strncmp(a->name, b->name, 8); | |
698 | } | |
50acf46b | 699 | |
0f113f3e | 700 | static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION) |
d02b48c6 | 701 | |
0f113f3e MC |
702 | static unsigned long function_hash(const FUNCTION * a) |
703 | { | |
704 | return lh_strhash(a->name); | |
705 | } | |
d02b48c6 | 706 | |
0f113f3e | 707 | static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION) |
d02b48c6 | 708 | |
7e1b7485 RS |
709 | static int SortFnByName(const void *_f1, const void *_f2) |
710 | { | |
711 | const FUNCTION *f1 = _f1; | |
712 | const FUNCTION *f2 = _f2; | |
713 | ||
714 | if (f1->type != f2->type) | |
715 | return f1->type - f2->type; | |
716 | return strcmp(f1->name, f2->name); | |
717 | } | |
718 | ||
a760a380 DSH |
719 | static void list_disabled(void) |
720 | { | |
d230bd1d | 721 | BIO_puts(bio_out, "Disabled algorithms:\n"); |
27dae1b0 RL |
722 | #ifdef OPENSSL_NO_AES |
723 | BIO_puts(bio_out, "AES\n"); | |
724 | #endif | |
725 | #ifdef OPENSSL_NO_BF | |
726 | BIO_puts(bio_out, "BF\n"); | |
727 | #endif | |
728 | #ifdef OPENSSL_NO_CAMELLIA | |
729 | BIO_puts(bio_out, "CAMELLIA\n"); | |
730 | #endif | |
731 | #ifdef OPENSSL_NO_CAST | |
732 | BIO_puts(bio_out, "CAST\n"); | |
733 | #endif | |
56c1ef05 RL |
734 | #ifdef OPENSSL_NO_CMAC |
735 | BIO_puts(bio_out, "CMAC\n"); | |
736 | #endif | |
27dae1b0 RL |
737 | #ifdef OPENSSL_NO_CMS |
738 | BIO_puts(bio_out, "CMS\n"); | |
739 | #endif | |
66b14bab RL |
740 | #ifdef OPENSSL_NO_COMP |
741 | BIO_puts(bio_out, "COMP\n"); | |
742 | #endif | |
27dae1b0 RL |
743 | #ifdef OPENSSL_NO_DES |
744 | BIO_puts(bio_out, "DES\n"); | |
745 | #endif | |
2df84dd3 RL |
746 | #ifdef OPENSSL_NO_DGRAM |
747 | BIO_puts(bio_out, "DGRAM\n"); | |
748 | #endif | |
a760a380 DSH |
749 | #ifdef OPENSSL_NO_DH |
750 | BIO_puts(bio_out, "DH\n"); | |
751 | #endif | |
752 | #ifdef OPENSSL_NO_DSA | |
753 | BIO_puts(bio_out, "DSA\n"); | |
754 | #endif | |
2df84dd3 | 755 | #if defined(OPENSSL_NO_DTLS1) || defined(OPENSSL_NO_DTLS) |
66b14bab RL |
756 | BIO_puts(bio_out, "DTLS1\n"); |
757 | #endif | |
a760a380 DSH |
758 | #ifdef OPENSSL_NO_EC |
759 | BIO_puts(bio_out, "EC\n"); | |
760 | #endif | |
761 | #ifdef OPENSSL_NO_EC2M | |
762 | BIO_puts(bio_out, "EC2M\n"); | |
763 | #endif | |
27dae1b0 RL |
764 | #ifdef OPENSSL_NO_ENGINE |
765 | BIO_puts(bio_out, "ENGINE\n"); | |
766 | #endif | |
2df84dd3 RL |
767 | #ifdef OPENSSL_NO_GOST |
768 | BIO_puts(bio_out, "GOST\n"); | |
769 | #endif | |
56c1ef05 RL |
770 | #ifdef OPENSSL_NO_HMAC |
771 | BIO_puts(bio_out, "HMAC\n"); | |
772 | #endif | |
27dae1b0 RL |
773 | #ifdef OPENSSL_NO_IDEA |
774 | BIO_puts(bio_out, "IDEA\n"); | |
775 | #endif | |
66b14bab RL |
776 | #ifdef OPENSSL_NO_JPAKE |
777 | BIO_puts(bio_out, "JPAKE\n"); | |
778 | #endif | |
27dae1b0 RL |
779 | #ifdef OPENSSL_NO_MD2 |
780 | BIO_puts(bio_out, "MD2\n"); | |
781 | #endif | |
782 | #ifdef OPENSSL_NO_MD4 | |
783 | BIO_puts(bio_out, "MD4\n"); | |
784 | #endif | |
785 | #ifdef OPENSSL_NO_MD5 | |
786 | BIO_puts(bio_out, "MD5\n"); | |
787 | #endif | |
788 | #ifdef OPENSSL_NO_MDC2 | |
789 | BIO_puts(bio_out, "MDC2\n"); | |
790 | #endif | |
2df84dd3 RL |
791 | #ifdef OPENSSL_NO_OCB |
792 | BIO_puts(bio_out, "OCB\n"); | |
793 | #endif | |
27dae1b0 RL |
794 | #ifdef OPENSSL_NO_OCSP |
795 | BIO_puts(bio_out, "OCSP\n"); | |
796 | #endif | |
a760a380 DSH |
797 | #ifdef OPENSSL_NO_PSK |
798 | BIO_puts(bio_out, "PSK\n"); | |
799 | #endif | |
27dae1b0 RL |
800 | #ifdef OPENSSL_NO_RC2 |
801 | BIO_puts(bio_out, "RC2\n"); | |
802 | #endif | |
803 | #ifdef OPENSSL_NO_RC4 | |
804 | BIO_puts(bio_out, "RC4\n"); | |
805 | #endif | |
806 | #ifdef OPENSSL_NO_RC5 | |
807 | BIO_puts(bio_out, "RC5\n"); | |
808 | #endif | |
809 | #ifdef OPENSSL_NO_RMD160 | |
810 | BIO_puts(bio_out, "RMD160\n"); | |
811 | #endif | |
d230bd1d RL |
812 | #ifdef OPENSSL_NO_RSA |
813 | BIO_puts(bio_out, "RSA\n"); | |
814 | #endif | |
66b14bab RL |
815 | #ifdef OPENSSL_NO_SCRYPT |
816 | BIO_puts(bio_out, "SCRYPT\n"); | |
817 | #endif | |
2df84dd3 RL |
818 | #ifdef OPENSSL_NO_SCT |
819 | BIO_puts(bio_out, "SCT\n"); | |
820 | #endif | |
821 | #ifdef OPENSSL_NO_SCTP | |
822 | BIO_puts(bio_out, "SCTP\n"); | |
823 | #endif | |
27dae1b0 RL |
824 | #ifdef OPENSSL_NO_SEED |
825 | BIO_puts(bio_out, "SEED\n"); | |
826 | #endif | |
2df84dd3 RL |
827 | #ifdef OPENSSL_NO_SHA |
828 | BIO_puts(bio_out, "SHA\n"); | |
829 | #endif | |
27dae1b0 RL |
830 | #ifdef OPENSSL_NO_SOCK |
831 | BIO_puts(bio_out, "SOCK\n"); | |
832 | #endif | |
a760a380 DSH |
833 | #ifdef OPENSSL_NO_SRP |
834 | BIO_puts(bio_out, "SRP\n"); | |
835 | #endif | |
66b14bab RL |
836 | #ifdef OPENSSL_NO_SRTP |
837 | BIO_puts(bio_out, "SRTP\n"); | |
838 | #endif | |
839 | #ifdef OPENSSL_NO_SSL3 | |
840 | BIO_puts(bio_out, "SSL3\n"); | |
841 | #endif | |
27dae1b0 RL |
842 | #ifdef OPENSSL_NO_WHIRLPOOL |
843 | BIO_puts(bio_out, "WHIRLPOOL\n"); | |
844 | #endif | |
d230bd1d RL |
845 | #ifndef ZLIB |
846 | BIO_puts(bio_out, "ZLIB\n"); | |
847 | #endif | |
a760a380 DSH |
848 | } |
849 | ||
0f113f3e MC |
850 | static LHASH_OF(FUNCTION) *prog_init(void) |
851 | { | |
852 | LHASH_OF(FUNCTION) *ret; | |
853 | FUNCTION *f; | |
854 | size_t i; | |
855 | ||
7e1b7485 | 856 | /* Sort alphabetically within category. For nicer help displays. */ |
0f113f3e | 857 | for (i = 0, f = functions; f->name != NULL; ++f, ++i) ; |
b4faea50 | 858 | qsort(functions, i, sizeof(*functions), SortFnByName); |
0f113f3e MC |
859 | |
860 | if ((ret = lh_FUNCTION_new()) == NULL) | |
861 | return (NULL); | |
862 | ||
863 | for (f = functions; f->name != NULL; f++) | |
864 | (void)lh_FUNCTION_insert(ret, f); | |
865 | return (ret); | |
866 | } |