jcs's openbsd hax
openbsd
1/* $OpenBSD: openssl.c,v 1.41 2026/01/02 00:14:24 kenjiro Exp $ */
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
112#include <err.h>
113#include <signal.h>
114#include <stdio.h>
115#include <string.h>
116#include <stdlib.h>
117#include <unistd.h>
118
119#include "apps.h"
120
121#include <openssl/bio.h>
122#include <openssl/conf.h>
123#include <openssl/crypto.h>
124#include <openssl/err.h>
125#include <openssl/lhash.h>
126#include <openssl/pem.h>
127#include <openssl/ssl.h>
128#include <openssl/x509.h>
129
130#define FUNC_TYPE_GENERAL 1
131#define FUNC_TYPE_MD 2
132#define FUNC_TYPE_CIPHER 3
133#define FUNC_TYPE_PKEY 4
134#define FUNC_TYPE_MD_ALG 5
135#define FUNC_TYPE_CIPHER_ALG 6
136
137typedef struct {
138 int type;
139 const char *name;
140 int (*func)(int argc, char **argv);
141} FUNCTION;
142
143DECLARE_LHASH_OF(FUNCTION);
144
145FUNCTION functions[] = {
146
147 /* General functions. */
148 { FUNC_TYPE_GENERAL, "asn1parse", asn1parse_main },
149 { FUNC_TYPE_GENERAL, "ca", ca_main },
150 { FUNC_TYPE_GENERAL, "certhash", certhash_main },
151 { FUNC_TYPE_GENERAL, "ciphers", ciphers_main },
152#ifndef OPENSSL_NO_CMS
153 { FUNC_TYPE_GENERAL, "cms", cms_main },
154#endif
155 { FUNC_TYPE_GENERAL, "crl2pkcs7", crl2pkcs7_main },
156 { FUNC_TYPE_GENERAL, "crl", crl_main },
157 { FUNC_TYPE_GENERAL, "dgst", dgst_main },
158 { FUNC_TYPE_GENERAL, "enc", enc_main },
159 { FUNC_TYPE_GENERAL, "errstr", errstr_main },
160 { FUNC_TYPE_GENERAL, "genpkey", genpkey_main },
161#ifndef OPENSSL_NO_OCSP
162 { FUNC_TYPE_GENERAL, "ocsp", ocsp_main },
163#endif
164 { FUNC_TYPE_GENERAL, "passwd", passwd_main },
165 { FUNC_TYPE_GENERAL, "pkcs7", pkcs7_main },
166 { FUNC_TYPE_GENERAL, "pkcs8", pkcs8_main },
167#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
168 { FUNC_TYPE_GENERAL, "pkcs12", pkcs12_main },
169#endif
170 { FUNC_TYPE_GENERAL, "pkey", pkey_main },
171 { FUNC_TYPE_GENERAL, "pkeyparam", pkeyparam_main },
172 { FUNC_TYPE_GENERAL, "pkeyutl", pkeyutl_main },
173 { FUNC_TYPE_GENERAL, "prime", prime_main },
174 { FUNC_TYPE_GENERAL, "rand", rand_main },
175 { FUNC_TYPE_GENERAL, "req", req_main },
176 { FUNC_TYPE_GENERAL, "s_client", s_client_main },
177 { FUNC_TYPE_GENERAL, "s_server", s_server_main },
178 { FUNC_TYPE_GENERAL, "s_time", s_time_main },
179 { FUNC_TYPE_GENERAL, "sess_id", sess_id_main },
180 { FUNC_TYPE_GENERAL, "smime", smime_main },
181#ifndef OPENSSL_NO_SPEED
182 { FUNC_TYPE_GENERAL, "speed", speed_main },
183#endif
184 { FUNC_TYPE_GENERAL, "ts", ts_main },
185 { FUNC_TYPE_GENERAL, "verify", verify_main },
186 { FUNC_TYPE_GENERAL, "version", version_main },
187 { FUNC_TYPE_GENERAL, "x509", x509_main },
188
189#ifndef OPENSSL_NO_DH
190 { FUNC_TYPE_GENERAL, "dh", dh_main },
191 { FUNC_TYPE_GENERAL, "dhparam", dhparam_main },
192 { FUNC_TYPE_GENERAL, "gendh", gendh_main },
193#endif
194#ifndef OPENSSL_NO_DSA
195 { FUNC_TYPE_GENERAL, "dsa", dsa_main },
196 { FUNC_TYPE_GENERAL, "dsaparam", dsaparam_main },
197 { FUNC_TYPE_GENERAL, "gendsa", gendsa_main },
198#endif
199#ifndef OPENSSL_NO_EC
200 { FUNC_TYPE_GENERAL, "ec", ec_main },
201 { FUNC_TYPE_GENERAL, "ecparam", ecparam_main },
202#endif
203#ifndef OPENSSL_NO_RSA
204 { FUNC_TYPE_GENERAL, "genrsa", genrsa_main },
205 { FUNC_TYPE_GENERAL, "rsa", rsa_main },
206 { FUNC_TYPE_GENERAL, "rsautl", rsautl_main },
207#endif
208
209 /* Message Digests. */
210#ifndef OPENSSL_NO_MD4
211 { FUNC_TYPE_MD, "md4", dgst_main },
212#endif
213#ifndef OPENSSL_NO_MD5
214 { FUNC_TYPE_MD, "md5", dgst_main },
215#endif
216#ifndef OPENSSL_NO_RIPEMD160
217 { FUNC_TYPE_MD, "ripemd160", dgst_main },
218#endif
219#ifndef OPENSSL_NO_SHA1
220 { FUNC_TYPE_MD, "sha1", dgst_main },
221#endif
222#ifndef OPENSSL_NO_SHA224
223 { FUNC_TYPE_MD, "sha224", dgst_main },
224#endif
225#ifndef OPENSSL_NO_SHA256
226 { FUNC_TYPE_MD, "sha256", dgst_main },
227#endif
228#ifndef OPENSSL_NO_SHA384
229 { FUNC_TYPE_MD, "sha384", dgst_main },
230#endif
231#ifndef OPENSSL_NO_SHA512
232 { FUNC_TYPE_MD, "sha512", dgst_main },
233#endif
234 { FUNC_TYPE_MD, "sha3-224", dgst_main },
235 { FUNC_TYPE_MD, "sha3-256", dgst_main },
236 { FUNC_TYPE_MD, "sha3-384", dgst_main },
237 { FUNC_TYPE_MD, "sha3-512", dgst_main },
238#ifndef OPENSSL_NO_SM3
239 { FUNC_TYPE_MD, "sm3", dgst_main },
240 { FUNC_TYPE_MD, "sm3WithRSAEncryption", dgst_main },
241#endif
242
243 /* Ciphers. */
244 { FUNC_TYPE_CIPHER, "base64", enc_main },
245#ifndef OPENSSL_NO_AES
246 { FUNC_TYPE_CIPHER, "aes-128-cbc", enc_main },
247 { FUNC_TYPE_CIPHER, "aes-128-ecb", enc_main },
248 { FUNC_TYPE_CIPHER, "aes-192-cbc", enc_main },
249 { FUNC_TYPE_CIPHER, "aes-192-ecb", enc_main },
250 { FUNC_TYPE_CIPHER, "aes-256-cbc", enc_main },
251 { FUNC_TYPE_CIPHER, "aes-256-ecb", enc_main },
252#endif
253#ifndef OPENSSL_NO_BF
254 { FUNC_TYPE_CIPHER, "bf", enc_main },
255 { FUNC_TYPE_CIPHER, "bf-cbc", enc_main },
256 { FUNC_TYPE_CIPHER, "bf-ecb", enc_main },
257 { FUNC_TYPE_CIPHER, "bf-cfb", enc_main },
258 { FUNC_TYPE_CIPHER, "bf-ofb", enc_main },
259#endif
260#ifndef OPENSSL_NO_CAMELLIA
261 { FUNC_TYPE_CIPHER, "camellia-128-cbc", enc_main },
262 { FUNC_TYPE_CIPHER, "camellia-128-ecb", enc_main },
263 { FUNC_TYPE_CIPHER, "camellia-192-cbc", enc_main },
264 { FUNC_TYPE_CIPHER, "camellia-192-ecb", enc_main },
265 { FUNC_TYPE_CIPHER, "camellia-256-cbc", enc_main },
266 { FUNC_TYPE_CIPHER, "camellia-256-ecb", enc_main },
267#endif
268#ifndef OPENSSL_NO_CAST
269 { FUNC_TYPE_CIPHER, "cast", enc_main },
270 { FUNC_TYPE_CIPHER, "cast5-cbc", enc_main },
271 { FUNC_TYPE_CIPHER, "cast5-ecb", enc_main },
272 { FUNC_TYPE_CIPHER, "cast5-cfb", enc_main },
273 { FUNC_TYPE_CIPHER, "cast5-ofb", enc_main },
274 { FUNC_TYPE_CIPHER, "cast-cbc", enc_main },
275#endif
276#ifndef OPENSSL_NO_CHACHA
277 { FUNC_TYPE_CIPHER, "chacha", enc_main },
278#endif
279#ifndef OPENSSL_NO_DES
280 { FUNC_TYPE_CIPHER, "des", enc_main },
281 { FUNC_TYPE_CIPHER, "des3", enc_main },
282 { FUNC_TYPE_CIPHER, "desx", enc_main },
283 { FUNC_TYPE_CIPHER, "des-ecb", enc_main },
284 { FUNC_TYPE_CIPHER, "des-ede", enc_main },
285 { FUNC_TYPE_CIPHER, "des-ede3", enc_main },
286 { FUNC_TYPE_CIPHER, "des-cbc", enc_main },
287 { FUNC_TYPE_CIPHER, "des-ede-cbc", enc_main },
288 { FUNC_TYPE_CIPHER, "des-ede3-cbc", enc_main },
289 { FUNC_TYPE_CIPHER, "des-cfb", enc_main },
290 { FUNC_TYPE_CIPHER, "des-ede-cfb", enc_main },
291 { FUNC_TYPE_CIPHER, "des-ede3-cfb", enc_main },
292 { FUNC_TYPE_CIPHER, "des-ofb", enc_main },
293 { FUNC_TYPE_CIPHER, "des-ede-ofb", enc_main },
294 { FUNC_TYPE_CIPHER, "des-ede3-ofb", enc_main },
295#endif
296#ifndef OPENSSL_NO_IDEA
297 { FUNC_TYPE_CIPHER, "idea", enc_main },
298 { FUNC_TYPE_CIPHER, "idea-cbc", enc_main },
299 { FUNC_TYPE_CIPHER, "idea-ecb", enc_main },
300 { FUNC_TYPE_CIPHER, "idea-cfb", enc_main },
301 { FUNC_TYPE_CIPHER, "idea-ofb", enc_main },
302#endif
303#ifndef OPENSSL_NO_RC2
304 { FUNC_TYPE_CIPHER, "rc2", enc_main },
305 { FUNC_TYPE_CIPHER, "rc2-cbc", enc_main },
306 { FUNC_TYPE_CIPHER, "rc2-ecb", enc_main },
307 { FUNC_TYPE_CIPHER, "rc2-cfb", enc_main },
308 { FUNC_TYPE_CIPHER, "rc2-ofb", enc_main },
309 { FUNC_TYPE_CIPHER, "rc2-64-cbc", enc_main },
310 { FUNC_TYPE_CIPHER, "rc2-40-cbc", enc_main },
311#endif
312#ifndef OPENSSL_NO_RC4
313 { FUNC_TYPE_CIPHER, "rc4", enc_main },
314 { FUNC_TYPE_CIPHER, "rc4-40", enc_main },
315#endif
316#ifndef OPENSSL_NO_SM4
317 { FUNC_TYPE_CIPHER, "sm4", enc_main },
318 { FUNC_TYPE_CIPHER, "sm4-ecb", enc_main },
319 { FUNC_TYPE_CIPHER, "sm4-cbc", enc_main },
320 { FUNC_TYPE_CIPHER, "sm4-ofb", enc_main },
321 { FUNC_TYPE_CIPHER, "sm4-cfb", enc_main },
322#endif
323 { 0, NULL, NULL }
324};
325
326static void openssl_startup(void);
327static void openssl_shutdown(void);
328
329/* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
330 * base prototypes (we cast each variable inside the function to the required
331 * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
332 * functions. */
333
334static LHASH_OF(FUNCTION) *prog_init(void);
335static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
336static void print_help(void);
337static void list_pkey(BIO * out);
338static void list_cipher(BIO * out);
339static void list_md(BIO * out);
340char *default_config_file = NULL;
341
342CONF *config = NULL;
343BIO *bio_err = NULL;
344
345static void
346openssl_startup(void)
347{
348 signal(SIGPIPE, SIG_IGN);
349
350 OpenSSL_add_all_algorithms();
351 SSL_library_init();
352 SSL_load_error_strings();
353
354 setup_ui();
355}
356
357static void
358openssl_shutdown(void)
359{
360 CONF_modules_unload(1);
361 destroy_ui();
362 OBJ_cleanup();
363 EVP_cleanup();
364 CRYPTO_cleanup_all_ex_data();
365 ERR_remove_thread_state(NULL);
366 ERR_free_strings();
367}
368
369int
370main(int argc, char **argv)
371{
372 char *to_free = NULL;
373 int i, ret = 0;
374 char *p;
375 LHASH_OF(FUNCTION) * prog = NULL;
376 long errline;
377
378 if (pledge("stdio cpath wpath rpath inet dns proc flock tty", NULL) == -1) {
379 fprintf(stderr, "openssl: pledge: %s\n", strerror(errno));
380 exit(1);
381 }
382
383 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
384 if (bio_err == NULL) {
385 fprintf(stderr, "openssl: failed to initialise bio_err\n");
386 exit(1);
387 }
388
389 if (BIO_sock_init() != 1) {
390 BIO_printf(bio_err, "BIO_sock_init failed\n");
391 exit(1);
392 }
393
394 openssl_startup();
395
396 /* Lets load up our environment a little */
397 p = getenv("OPENSSL_CONF");
398 if (p == NULL) {
399 p = to_free = make_config_name();
400 if (p == NULL) {
401 BIO_printf(bio_err, "error making config file name\n");
402 goto end;
403 }
404 }
405
406 default_config_file = p;
407
408 config = NCONF_new(NULL);
409 i = NCONF_load(config, p, &errline);
410 if (i == 0) {
411 if (ERR_GET_REASON(ERR_peek_last_error()) ==
412 CONF_R_NO_SUCH_FILE) {
413 BIO_printf(bio_err,
414 "WARNING: can't open config file: %s\n", p);
415 ERR_clear_error();
416 NCONF_free(config);
417 config = NULL;
418 } else {
419 ERR_print_errors(bio_err);
420 NCONF_free(config);
421 exit(1);
422 }
423 }
424
425 if (!load_config(bio_err, NULL)) {
426 BIO_printf(bio_err, "failed to load configuration\n");
427 goto end;
428 }
429
430 prog = prog_init();
431
432 /*
433 * ok, now check that there are not arguments, if there are, run with
434 * them, shifting the executable name off the front
435 */
436 argc--;
437 argv++;
438
439 if (argc < 1) {
440 print_help();
441 goto end;
442 }
443
444 ret = do_cmd(prog, argc, argv);
445 if (ret < 0)
446 ret = 0;
447
448 end:
449 free(to_free);
450
451 if (config != NULL) {
452 NCONF_free(config);
453 config = NULL;
454 }
455 if (prog != NULL)
456 lh_FUNCTION_free(prog);
457
458 openssl_shutdown();
459
460 if (bio_err != NULL) {
461 BIO_free(bio_err);
462 bio_err = NULL;
463 }
464 return (ret);
465}
466
467#define LIST_STANDARD_COMMANDS "list-standard-commands"
468#define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
469#define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
470#define LIST_CIPHER_COMMANDS "list-cipher-commands"
471#define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
472#define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
473
474
475static int
476do_cmd(LHASH_OF(FUNCTION) * prog, int argc, char *argv[])
477{
478 FUNCTION f, *fp;
479 int ret = 1;
480
481 if (argc <= 0 || argv[0] == NULL)
482 return 0;
483
484 f.name = argv[0];
485 fp = lh_FUNCTION_retrieve(prog, &f);
486 if (fp == NULL) {
487 if (EVP_get_digestbyname(argv[0])) {
488 f.type = FUNC_TYPE_MD;
489 f.func = dgst_main;
490 fp = &f;
491 } else if (EVP_get_cipherbyname(argv[0])) {
492 f.type = FUNC_TYPE_CIPHER;
493 f.func = enc_main;
494 fp = &f;
495 }
496 }
497
498 if (fp != NULL)
499 return fp->func(argc, argv);
500
501 if (strcmp(argv[0], "help") == 0) {
502 print_help();
503 return 0;
504 }
505
506 if ((strncmp(argv[0], "no-", 3)) == 0) {
507 BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
508 f.name = argv[0] + 3;
509 ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
510 if (!ret)
511 BIO_printf(bio_stdout, "%s\n", argv[0]);
512 else
513 BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
514 BIO_free_all(bio_stdout);
515 return ret;
516 }
517
518 if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
519 (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
520 (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
521 (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
522 (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
523 (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
524 int list_type;
525 BIO *bio_stdout;
526
527 if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
528 list_type = FUNC_TYPE_GENERAL;
529 else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
530 list_type = FUNC_TYPE_MD;
531 else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
532 list_type = FUNC_TYPE_MD_ALG;
533 else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
534 list_type = FUNC_TYPE_PKEY;
535 else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
536 list_type = FUNC_TYPE_CIPHER_ALG;
537 else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
538 list_type = FUNC_TYPE_CIPHER;
539 bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
540
541 if (list_type == FUNC_TYPE_PKEY)
542 list_pkey(bio_stdout);
543 if (list_type == FUNC_TYPE_MD_ALG)
544 list_md(bio_stdout);
545 if (list_type == FUNC_TYPE_CIPHER_ALG)
546 list_cipher(bio_stdout);
547 else {
548 for (fp = functions; fp->name != NULL; fp++)
549 if (fp->type == list_type)
550 BIO_printf(bio_stdout, "%s\n",
551 fp->name);
552 }
553 BIO_free_all(bio_stdout);
554 return 0;
555 }
556
557 BIO_printf(bio_err,
558 "openssl:Error: '%s' is an invalid command.\n",
559 argv[0]);
560 print_help();
561
562 return 1;
563}
564
565static void
566print_help(void)
567{
568 FUNCTION *fp;
569 int i = 0;
570 int tp = 0;
571 int nl;
572
573 BIO_printf(bio_err, "\nStandard commands");
574 for (fp = functions; fp->name != NULL; fp++) {
575 nl = 0;
576#ifdef OPENSSL_NO_CAMELLIA
577 if (((i++) % 5) == 0)
578#else
579 if (((i++) % 4) == 0)
580#endif
581 {
582 BIO_printf(bio_err, "\n");
583 nl = 1;
584 }
585 if (fp->type != tp) {
586 tp = fp->type;
587 if (!nl)
588 BIO_printf(bio_err, "\n");
589 if (tp == FUNC_TYPE_MD) {
590 i = 1;
591 BIO_printf(bio_err,
592 "\nMessage Digest commands (see the `dgst' command for more details)\n");
593 } else if (tp == FUNC_TYPE_CIPHER) {
594 i = 1;
595 BIO_printf(bio_err, "\nCipher commands (see the `enc' command for more details)\n");
596 }
597 }
598#ifdef OPENSSL_NO_CAMELLIA
599 BIO_printf(bio_err, "%-15s", fp->name);
600#else
601 BIO_printf(bio_err, "%-18s", fp->name);
602#endif
603 }
604
605 BIO_printf(bio_err, "\n\n");
606}
607
608static int
609SortFnByName(const void *_f1, const void *_f2)
610{
611 const FUNCTION *f1 = _f1;
612 const FUNCTION *f2 = _f2;
613
614 if (f1->type != f2->type)
615 return f1->type - f2->type;
616 return strcmp(f1->name, f2->name);
617}
618
619static void
620list_pkey(BIO * out)
621{
622 int i;
623
624 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
625 const EVP_PKEY_ASN1_METHOD *ameth;
626 int pkey_id, pkey_base_id, pkey_flags;
627 const char *pinfo, *pem_str;
628 ameth = EVP_PKEY_asn1_get0(i);
629 EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
630 &pinfo, &pem_str, ameth);
631 if (pkey_flags & ASN1_PKEY_ALIAS) {
632 BIO_printf(out, "Name: %s\n",
633 OBJ_nid2ln(pkey_id));
634 BIO_printf(out, "\tType: Alias to %s\n",
635 OBJ_nid2ln(pkey_base_id));
636 } else {
637 BIO_printf(out, "Name: %s\n", pinfo);
638 BIO_printf(out, "\tType: %s Algorithm\n",
639 pkey_flags & ASN1_PKEY_DYNAMIC ?
640 "External" : "Builtin");
641 BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
642 if (pem_str == NULL)
643 pem_str = "(none)";
644 BIO_printf(out, "\tPEM string: %s\n", pem_str);
645 }
646
647 }
648}
649
650static void
651list_cipher_fn(const EVP_CIPHER * c, const char *from, const char *to,
652 void *arg)
653{
654 if (c)
655 BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
656 else {
657 if (!from)
658 from = "<undefined>";
659 if (!to)
660 to = "<undefined>";
661 BIO_printf(arg, "%s => %s\n", from, to);
662 }
663}
664
665static void
666list_cipher(BIO * out)
667{
668 EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
669}
670
671static void
672list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg)
673{
674 if (m)
675 BIO_printf(arg, "%s\n", EVP_MD_name(m));
676 else {
677 if (!from)
678 from = "<undefined>";
679 if (!to)
680 to = "<undefined>";
681 BIO_printf(arg, "%s => %s\n", from, to);
682 }
683}
684
685static void
686list_md(BIO * out)
687{
688 EVP_MD_do_all_sorted(list_md_fn, out);
689}
690
691static int
692function_cmp(const FUNCTION * a, const FUNCTION * b)
693{
694 return strncmp(a->name, b->name, 8);
695}
696
697static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
698
699static unsigned long
700function_hash(const FUNCTION * a)
701{
702 return lh_strhash(a->name);
703}
704
705static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
706
707static LHASH_OF(FUNCTION) *
708prog_init(void)
709{
710 LHASH_OF(FUNCTION) * ret;
711 FUNCTION *f;
712 size_t i;
713
714 /* Purely so it looks nice when the user hits ? */
715 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
716 ;
717 qsort(functions, i, sizeof *functions, SortFnByName);
718
719 if ((ret = lh_FUNCTION_new()) == NULL)
720 return (NULL);
721
722 for (f = functions; f->name != NULL; f++)
723 (void) lh_FUNCTION_insert(ret, f);
724 return (ret);
725}