2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/evp.h>
12 #include <openssl/err.h>
13 #include <openssl/provider.h>
14 #include <openssl/safestack.h>
19 static int verbose
= 0;
21 static int describe_param_type(char *buf
, size_t bufsz
, const OSSL_PARAM
*param
)
23 const char *type_mod
= "";
24 const char *type
= NULL
;
25 int show_type_number
= 0;
28 switch (param
->data_type
) {
29 case OSSL_PARAM_UNSIGNED_INTEGER
:
30 type_mod
= "unsigned ";
32 case OSSL_PARAM_INTEGER
:
35 case OSSL_PARAM_UTF8_PTR
:
36 type_mod
= "pointer to a ";
38 case OSSL_PARAM_UTF8_STRING
:
39 type
= "UTF8 encoded string";
41 case OSSL_PARAM_OCTET_PTR
:
42 type_mod
= "pointer to an ";
44 case OSSL_PARAM_OCTET_STRING
:
45 type
= "octet string";
48 type
= "unknown type";
53 printed_len
= BIO_snprintf(buf
, bufsz
, "%s: ", param
->key
);
54 if (printed_len
> 0) {
58 printed_len
= BIO_snprintf(buf
, bufsz
, "%s%s", type_mod
, type
);
59 if (printed_len
> 0) {
63 if (show_type_number
) {
64 printed_len
= BIO_snprintf(buf
, bufsz
, " [%d]", param
->data_type
);
65 if (printed_len
> 0) {
70 if (param
->data_size
== 0)
71 printed_len
= BIO_snprintf(buf
, bufsz
, " (arbitrary size)");
73 printed_len
= BIO_snprintf(buf
, bufsz
, " (max %zu bytes large)",
75 if (printed_len
> 0) {
83 static int print_param_types(const char *thing
, const OSSL_PARAM
*pdefs
)
86 BIO_printf(bio_out
, " No declared %s\n", thing
);
87 } else if (pdefs
->key
== NULL
) {
89 * An empty list? This shouldn't happen, but let's just make sure to
90 * say something if there's a badly written provider...
92 BIO_printf(bio_out
, " Empty list of %s (!!!)\n", thing
);
94 BIO_printf(bio_out
, " %s:\n", thing
);
95 for (; pdefs
->key
!= NULL
; pdefs
++) {
96 char buf
[200]; /* This should be ample space */
98 describe_param_type(buf
, sizeof(buf
), pdefs
);
99 BIO_printf(bio_out
, " %s\n", buf
);
105 static void legacy_cipher_fn(const EVP_CIPHER
*c
,
106 const char *from
, const char *to
, void *arg
)
109 BIO_printf(arg
, " %s\n", EVP_CIPHER_name(c
));
112 from
= "<undefined>";
115 BIO_printf(arg
, " %s => %s\n", from
, to
);
119 DEFINE_STACK_OF(EVP_CIPHER
)
120 static int cipher_cmp(const EVP_CIPHER
* const *a
,
121 const EVP_CIPHER
* const *b
)
123 int ret
= strcasecmp(EVP_CIPHER_name(*a
), EVP_CIPHER_name(*b
));
126 ret
= strcmp(OSSL_PROVIDER_name(EVP_CIPHER_provider(*a
)),
127 OSSL_PROVIDER_name(EVP_CIPHER_provider(*b
)));
132 static void collect_ciphers(EVP_CIPHER
*cipher
, void *stack
)
134 STACK_OF(EVP_CIPHER
) *cipher_stack
= stack
;
136 sk_EVP_CIPHER_push(cipher_stack
, cipher
);
137 EVP_CIPHER_up_ref(cipher
);
140 static void list_ciphers(void)
142 STACK_OF(EVP_CIPHER
) *ciphers
= sk_EVP_CIPHER_new(cipher_cmp
);
145 BIO_printf(bio_out
, "Legacy:\n");
146 EVP_CIPHER_do_all_sorted(legacy_cipher_fn
, bio_out
);
148 BIO_printf(bio_out
, "Provided:\n");
149 EVP_CIPHER_do_all_ex(NULL
, collect_ciphers
, ciphers
);
150 sk_EVP_CIPHER_sort(ciphers
);
151 for (i
= 0; i
< sk_EVP_CIPHER_num(ciphers
); i
++) {
152 const EVP_CIPHER
*c
= sk_EVP_CIPHER_value(ciphers
, i
);
154 BIO_printf(bio_out
, " %s", EVP_CIPHER_name(c
));
155 BIO_printf(bio_out
, " @ %s\n",
156 OSSL_PROVIDER_name(EVP_CIPHER_provider(c
)));
158 print_param_types("retrievable algorithm parameters",
159 EVP_CIPHER_gettable_params(c
));
160 print_param_types("retrievable operation parameters",
161 EVP_CIPHER_CTX_gettable_params(c
));
162 print_param_types("settable operation parameters",
163 EVP_CIPHER_CTX_settable_params(c
));
166 sk_EVP_CIPHER_pop_free(ciphers
, EVP_CIPHER_meth_free
);
169 static void list_md_fn(const EVP_MD
*m
,
170 const char *from
, const char *to
, void *arg
)
173 BIO_printf(arg
, " %s\n", EVP_MD_name(m
));
176 from
= "<undefined>";
179 BIO_printf((BIO
*)arg
, " %s => %s\n", from
, to
);
183 DEFINE_STACK_OF(EVP_MD
)
184 static int md_cmp(const EVP_MD
* const *a
, const EVP_MD
* const *b
)
186 int ret
= strcasecmp(EVP_MD_name(*a
), EVP_MD_name(*b
));
189 ret
= strcmp(OSSL_PROVIDER_name(EVP_MD_provider(*a
)),
190 OSSL_PROVIDER_name(EVP_MD_provider(*b
)));
195 static void collect_digests(EVP_MD
*md
, void *stack
)
197 STACK_OF(EVP_MD
) *digest_stack
= stack
;
199 sk_EVP_MD_push(digest_stack
, md
);
203 static void list_digests(void)
205 STACK_OF(EVP_MD
) *digests
= sk_EVP_MD_new(md_cmp
);
208 BIO_printf(bio_out
, "Legacy:\n");
209 EVP_MD_do_all_sorted(list_md_fn
, bio_out
);
211 BIO_printf(bio_out
, "Provided:\n");
212 EVP_MD_do_all_ex(NULL
, collect_digests
, digests
);
213 sk_EVP_MD_sort(digests
);
214 for (i
= 0; i
< sk_EVP_MD_num(digests
); i
++) {
215 const EVP_MD
*m
= sk_EVP_MD_value(digests
, i
);
217 BIO_printf(bio_out
, " %s", EVP_MD_name(m
));
218 BIO_printf(bio_out
, " @ %s\n",
219 OSSL_PROVIDER_name(EVP_MD_provider(m
)));
221 print_param_types("retrievable algorithm parameters",
222 EVP_MD_gettable_params(m
));
223 print_param_types("retrievable operation parameters",
224 EVP_MD_CTX_gettable_params(m
));
225 print_param_types("settable operation parameters",
226 EVP_MD_CTX_settable_params(m
));
229 sk_EVP_MD_pop_free(digests
, EVP_MD_meth_free
);
232 #if 0 /* Temporarly disabled */
233 static void list_mac_fn(const EVP_MAC
*m
,
234 const char *from
, const char *to
, void *arg
)
237 BIO_printf(arg
, "%s\n", EVP_MAC_name(m
));
240 from
= "<undefined>";
243 BIO_printf(arg
, "%s => %s\n", from
, to
);
248 static void list_missing_help(void)
253 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
254 if ((o
= fp
->help
) != NULL
) {
255 /* If there is help, list what flags are not documented. */
256 for ( ; o
->name
!= NULL
; o
++) {
257 if (o
->helpstr
== NULL
)
258 BIO_printf(bio_out
, "%s %s\n", fp
->name
, o
->name
);
260 } else if (fp
->func
!= dgst_main
) {
261 /* If not aliased to the dgst command, */
262 BIO_printf(bio_out
, "%s *\n", fp
->name
);
267 static void list_objects(void)
269 int max_nid
= OBJ_new_nid(0);
271 char *oid_buf
= NULL
;
274 /* Skip 0, since that's NID_undef */
275 for (i
= 1; i
< max_nid
; i
++) {
276 const ASN1_OBJECT
*obj
= OBJ_nid2obj(i
);
277 const char *sn
= OBJ_nid2sn(i
);
278 const char *ln
= OBJ_nid2ln(i
);
282 * If one of the retrieved objects somehow generated an error,
283 * we ignore it. The check for NID_undef below will detect the
284 * error and simply skip to the next NID.
288 if (OBJ_obj2nid(obj
) == NID_undef
)
291 if ((n
= OBJ_obj2txt(NULL
, 0, obj
, 1)) == 0) {
292 BIO_printf(bio_out
, "# None-OID object: %s, %s\n", sn
, ln
);
299 oid_buf
= OPENSSL_realloc(oid_buf
, n
+ 1);
300 if (oid_buf
== NULL
) {
301 BIO_printf(bio_err
, "ERROR: Memory allocation\n");
306 if (OBJ_obj2txt(oid_buf
, oid_size
, obj
, 1) < 0)
308 if (ln
== NULL
|| strcmp(sn
, ln
) == 0)
309 BIO_printf(bio_out
, "%s = %s\n", sn
, oid_buf
);
311 BIO_printf(bio_out
, "%s = %s, %s\n", sn
, ln
, oid_buf
);
314 OPENSSL_free(oid_buf
);
317 static void list_options_for_command(const char *command
)
322 for (fp
= functions
; fp
->name
!= NULL
; fp
++)
323 if (strcmp(fp
->name
, command
) == 0)
325 if (fp
->name
== NULL
) {
326 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
331 if ((o
= fp
->help
) == NULL
)
334 for ( ; o
->name
!= NULL
; o
++) {
335 if (o
->name
== OPT_HELP_STR
336 || o
->name
== OPT_MORE_STR
337 || o
->name
[0] == '\0')
339 BIO_printf(bio_out
, "%s %c\n", o
->name
, o
->valtype
);
343 static void list_type(FUNC_TYPE ft
, int one
)
349 memset(&dc
, 0, sizeof(dc
));
351 calculate_columns(functions
, &dc
);
353 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
357 BIO_printf(bio_out
, "%s\n", fp
->name
);
359 if (i
% dc
.columns
== 0 && i
> 0)
360 BIO_printf(bio_out
, "\n");
361 BIO_printf(bio_out
, "%-*s", dc
.width
, fp
->name
);
366 BIO_printf(bio_out
, "\n\n");
369 static void list_pkey(void)
373 for (i
= 0; i
< EVP_PKEY_asn1_get_count(); i
++) {
374 const EVP_PKEY_ASN1_METHOD
*ameth
;
375 int pkey_id
, pkey_base_id
, pkey_flags
;
376 const char *pinfo
, *pem_str
;
377 ameth
= EVP_PKEY_asn1_get0(i
);
378 EVP_PKEY_asn1_get0_info(&pkey_id
, &pkey_base_id
, &pkey_flags
,
379 &pinfo
, &pem_str
, ameth
);
380 if (pkey_flags
& ASN1_PKEY_ALIAS
) {
381 BIO_printf(bio_out
, "Name: %s\n", OBJ_nid2ln(pkey_id
));
382 BIO_printf(bio_out
, "\tAlias for: %s\n",
383 OBJ_nid2ln(pkey_base_id
));
385 BIO_printf(bio_out
, "Name: %s\n", pinfo
);
386 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
387 pkey_flags
& ASN1_PKEY_DYNAMIC
?
388 "External" : "Builtin");
389 BIO_printf(bio_out
, "\tOID: %s\n", OBJ_nid2ln(pkey_id
));
392 BIO_printf(bio_out
, "\tPEM string: %s\n", pem_str
);
398 static void list_pkey_meth(void)
401 size_t meth_count
= EVP_PKEY_meth_get_count();
403 for (i
= 0; i
< meth_count
; i
++) {
404 const EVP_PKEY_METHOD
*pmeth
= EVP_PKEY_meth_get0(i
);
405 int pkey_id
, pkey_flags
;
407 EVP_PKEY_meth_get0_info(&pkey_id
, &pkey_flags
, pmeth
);
408 BIO_printf(bio_out
, "%s\n", OBJ_nid2ln(pkey_id
));
409 BIO_printf(bio_out
, "\tType: %s Algorithm\n",
410 pkey_flags
& ASN1_PKEY_DYNAMIC
? "External" : "Builtin");
414 static void list_engines(void)
416 #ifndef OPENSSL_NO_ENGINE
419 BIO_puts(bio_out
, "Engines:\n");
420 e
= ENGINE_get_first();
422 BIO_printf(bio_out
, "%s\n", ENGINE_get_id(e
));
423 e
= ENGINE_get_next(e
);
426 BIO_puts(bio_out
, "Engine support is disabled.\n");
430 static void list_disabled(void)
432 BIO_puts(bio_out
, "Disabled algorithms:\n");
433 #ifdef OPENSSL_NO_ARIA
434 BIO_puts(bio_out
, "ARIA\n");
437 BIO_puts(bio_out
, "BF\n");
439 #ifdef OPENSSL_NO_BLAKE2
440 BIO_puts(bio_out
, "BLAKE2\n");
442 #ifdef OPENSSL_NO_CAMELLIA
443 BIO_puts(bio_out
, "CAMELLIA\n");
445 #ifdef OPENSSL_NO_CAST
446 BIO_puts(bio_out
, "CAST\n");
448 #ifdef OPENSSL_NO_CMAC
449 BIO_puts(bio_out
, "CMAC\n");
451 #ifdef OPENSSL_NO_CMS
452 BIO_puts(bio_out
, "CMS\n");
454 #ifdef OPENSSL_NO_COMP
455 BIO_puts(bio_out
, "COMP\n");
457 #ifdef OPENSSL_NO_DES
458 BIO_puts(bio_out
, "DES\n");
460 #ifdef OPENSSL_NO_DGRAM
461 BIO_puts(bio_out
, "DGRAM\n");
464 BIO_puts(bio_out
, "DH\n");
466 #ifdef OPENSSL_NO_DSA
467 BIO_puts(bio_out
, "DSA\n");
469 #if defined(OPENSSL_NO_DTLS)
470 BIO_puts(bio_out
, "DTLS\n");
472 #if defined(OPENSSL_NO_DTLS1)
473 BIO_puts(bio_out
, "DTLS1\n");
475 #if defined(OPENSSL_NO_DTLS1_2)
476 BIO_puts(bio_out
, "DTLS1_2\n");
479 BIO_puts(bio_out
, "EC\n");
481 #ifdef OPENSSL_NO_EC2M
482 BIO_puts(bio_out
, "EC2M\n");
484 #ifdef OPENSSL_NO_ENGINE
485 BIO_puts(bio_out
, "ENGINE\n");
487 #ifdef OPENSSL_NO_GOST
488 BIO_puts(bio_out
, "GOST\n");
490 #ifdef OPENSSL_NO_IDEA
491 BIO_puts(bio_out
, "IDEA\n");
493 #ifdef OPENSSL_NO_MD2
494 BIO_puts(bio_out
, "MD2\n");
496 #ifdef OPENSSL_NO_MD4
497 BIO_puts(bio_out
, "MD4\n");
499 #ifdef OPENSSL_NO_MD5
500 BIO_puts(bio_out
, "MD5\n");
502 #ifdef OPENSSL_NO_MDC2
503 BIO_puts(bio_out
, "MDC2\n");
505 #ifdef OPENSSL_NO_OCB
506 BIO_puts(bio_out
, "OCB\n");
508 #ifdef OPENSSL_NO_OCSP
509 BIO_puts(bio_out
, "OCSP\n");
511 #ifdef OPENSSL_NO_PSK
512 BIO_puts(bio_out
, "PSK\n");
514 #ifdef OPENSSL_NO_RC2
515 BIO_puts(bio_out
, "RC2\n");
517 #ifdef OPENSSL_NO_RC4
518 BIO_puts(bio_out
, "RC4\n");
520 #ifdef OPENSSL_NO_RC5
521 BIO_puts(bio_out
, "RC5\n");
523 #ifdef OPENSSL_NO_RMD160
524 BIO_puts(bio_out
, "RMD160\n");
526 #ifdef OPENSSL_NO_RSA
527 BIO_puts(bio_out
, "RSA\n");
529 #ifdef OPENSSL_NO_SCRYPT
530 BIO_puts(bio_out
, "SCRYPT\n");
532 #ifdef OPENSSL_NO_SCTP
533 BIO_puts(bio_out
, "SCTP\n");
535 #ifdef OPENSSL_NO_SEED
536 BIO_puts(bio_out
, "SEED\n");
538 #ifdef OPENSSL_NO_SM2
539 BIO_puts(bio_out
, "SM2\n");
541 #ifdef OPENSSL_NO_SM3
542 BIO_puts(bio_out
, "SM3\n");
544 #ifdef OPENSSL_NO_SM4
545 BIO_puts(bio_out
, "SM4\n");
547 #ifdef OPENSSL_NO_SOCK
548 BIO_puts(bio_out
, "SOCK\n");
550 #ifdef OPENSSL_NO_SRP
551 BIO_puts(bio_out
, "SRP\n");
553 #ifdef OPENSSL_NO_SRTP
554 BIO_puts(bio_out
, "SRTP\n");
556 #ifdef OPENSSL_NO_SSL3
557 BIO_puts(bio_out
, "SSL3\n");
559 #ifdef OPENSSL_NO_TLS1
560 BIO_puts(bio_out
, "TLS1\n");
562 #ifdef OPENSSL_NO_TLS1_1
563 BIO_puts(bio_out
, "TLS1_1\n");
565 #ifdef OPENSSL_NO_TLS1_2
566 BIO_puts(bio_out
, "TLS1_2\n");
568 #ifdef OPENSSL_NO_WHIRLPOOL
569 BIO_puts(bio_out
, "WHIRLPOOL\n");
572 BIO_puts(bio_out
, "ZLIB\n");
576 /* Unified enum for help and list commands. */
577 typedef enum HELPLIST_CHOICE
{
578 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ONE
, OPT_VERBOSE
,
579 OPT_COMMANDS
, OPT_DIGEST_COMMANDS
, OPT_MAC_ALGORITHMS
, OPT_OPTIONS
,
580 OPT_DIGEST_ALGORITHMS
, OPT_CIPHER_COMMANDS
, OPT_CIPHER_ALGORITHMS
,
581 OPT_PK_ALGORITHMS
, OPT_PK_METHOD
, OPT_ENGINES
, OPT_DISABLED
,
582 OPT_MISSING_HELP
, OPT_OBJECTS
585 const OPTIONS list_options
[] = {
586 {"help", OPT_HELP
, '-', "Display this summary"},
587 {"1", OPT_ONE
, '-', "List in one column"},
588 {"verbose", OPT_VERBOSE
, '-', "Verbose listing"},
589 {"commands", OPT_COMMANDS
, '-', "List of standard commands"},
590 {"digest-commands", OPT_DIGEST_COMMANDS
, '-',
591 "List of message digest commands"},
592 {"digest-algorithms", OPT_DIGEST_ALGORITHMS
, '-',
593 "List of message digest algorithms"},
594 {"mac-algorithms", OPT_MAC_ALGORITHMS
, '-',
595 "List of message authentication code algorithms"},
596 {"cipher-commands", OPT_CIPHER_COMMANDS
, '-', "List of cipher commands"},
597 {"cipher-algorithms", OPT_CIPHER_ALGORITHMS
, '-',
598 "List of cipher algorithms"},
599 {"public-key-algorithms", OPT_PK_ALGORITHMS
, '-',
600 "List of public key algorithms"},
601 {"public-key-methods", OPT_PK_METHOD
, '-',
602 "List of public key methods"},
603 {"engines", OPT_ENGINES
, '-',
604 "List of loaded engines"},
605 {"disabled", OPT_DISABLED
, '-',
606 "List of disabled features"},
607 {"missing-help", OPT_MISSING_HELP
, '-',
608 "List missing detailed help strings"},
609 {"options", OPT_OPTIONS
, 's',
610 "List options for specified command"},
611 {"objects", OPT_OBJECTS
, '-',
612 "List built in objects (OID<->name mappings)"},
616 int list_main(int argc
, char **argv
)
620 int one
= 0, done
= 0;
622 unsigned int commands
:1;
623 unsigned int digest_commands
:1;
624 unsigned int digest_algorithms
:1;
625 unsigned int mac_algorithms
:1;
626 unsigned int cipher_commands
:1;
627 unsigned int cipher_algorithms
:1;
628 unsigned int pk_algorithms
:1;
629 unsigned int pk_method
:1;
630 unsigned int engines
:1;
631 unsigned int disabled
:1;
632 unsigned int missing_help
:1;
633 unsigned int objects
:1;
634 unsigned int options
:1;
637 verbose
= 0; /* Clear a possible previous call */
639 prog
= opt_init(argc
, argv
, list_options
);
640 while ((o
= opt_next()) != OPT_EOF
) {
642 case OPT_EOF
: /* Never hit, but suppresses warning */
645 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
648 opt_help(list_options
);
656 case OPT_DIGEST_COMMANDS
:
657 todo
.digest_commands
= 1;
659 case OPT_DIGEST_ALGORITHMS
:
660 todo
.digest_algorithms
= 1;
662 case OPT_MAC_ALGORITHMS
:
663 todo
.mac_algorithms
= 1;
665 case OPT_CIPHER_COMMANDS
:
666 todo
.cipher_commands
= 1;
668 case OPT_CIPHER_ALGORITHMS
:
669 todo
.cipher_algorithms
= 1;
671 case OPT_PK_ALGORITHMS
:
672 todo
.pk_algorithms
= 1;
683 case OPT_MISSING_HELP
:
684 todo
.missing_help
= 1;
690 list_options_for_command(opt_arg());
698 if (opt_num_rest() != 0) {
699 BIO_printf(bio_err
, "Extra arguments given.\n");
704 list_type(FT_general
, one
);
705 if (todo
.digest_commands
)
706 list_type(FT_md
, one
);
707 if (todo
.digest_algorithms
)
709 #if 0 /* Temporarly disabled */
710 if (todo
.mac_algorithms
)
711 EVP_MAC_do_all_sorted(list_mac_fn
, bio_out
);
713 if (todo
.cipher_commands
)
714 list_type(FT_cipher
, one
);
715 if (todo
.cipher_algorithms
)
717 if (todo
.pk_algorithms
)
725 if (todo
.missing_help
)