diff options
author | Pauli <pauli@openssl.org> | 2021-05-10 04:12:38 +0200 |
---|---|---|
committer | Pauli <pauli@openssl.org> | 2021-05-12 10:40:57 +0200 |
commit | 482e6693b436e2de31a7c20d03fc73398b04767f (patch) | |
tree | 0fdcd2b78b2d2c7ce92d3d0699d865c35e94a2fc /apps/list.c | |
parent | encoder: add a _name() function for encoders and decoders (diff) | |
download | openssl-482e6693b436e2de31a7c20d03fc73398b04767f.tar.xz openssl-482e6693b436e2de31a7c20d03fc73398b04767f.zip |
apps: change list command to only list fetchable algorithms.
The -propquery option will work with this change. By default the output will
be the same.
Also address some inconsistencies in the code with respects to error checking.
Fixes #15196
Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15211)
Diffstat (limited to 'apps/list.c')
-rw-r--r-- | apps/list.c | 142 |
1 files changed, 101 insertions, 41 deletions
diff --git a/apps/list.c b/apps/list.c index a8646addb1..bd37f63465 100644 --- a/apps/list.c +++ b/apps/list.c @@ -29,6 +29,41 @@ static int verbose = 0; static const char *select_name = NULL; +/* Checks to see if algorithms are fetchable */ +#define IS_FETCHABLE(type, TYPE) \ + static int is_ ## type ## _fetchable(const TYPE *alg) \ + { \ + TYPE *impl; \ + const char *propq = app_get0_propq(); \ + const char *name = TYPE ## _name(alg); \ + \ + ERR_set_mark(); \ + impl = TYPE ## _fetch(NULL, name, propq); \ + ERR_pop_to_mark(); \ + if (impl == NULL) \ + return 0; \ + TYPE ## _free(impl); \ + return 1; \ + } +IS_FETCHABLE(cipher, EVP_CIPHER) +IS_FETCHABLE(digest, EVP_MD) +IS_FETCHABLE(mac, EVP_MAC) +IS_FETCHABLE(kdf, EVP_KDF) +IS_FETCHABLE(rand, EVP_RAND) +IS_FETCHABLE(keymgmt, EVP_KEYMGMT) +IS_FETCHABLE(signature, EVP_SIGNATURE) +IS_FETCHABLE(kem, EVP_KEM) +IS_FETCHABLE(asym_cipher, EVP_ASYM_CIPHER) +IS_FETCHABLE(keyexch, EVP_KEYEXCH) +IS_FETCHABLE(decoder, OSSL_DECODER) +IS_FETCHABLE(encoder, OSSL_ENCODER) + +#ifndef OPENSSL_NO_DEPRECATED_3_0 +static int include_legacy(void) +{ + return app_get0_propq() == NULL; +} + static void legacy_cipher_fn(const EVP_CIPHER *c, const char *from, const char *to, void *arg) { @@ -46,6 +81,7 @@ static void legacy_cipher_fn(const EVP_CIPHER *c, BIO_printf(arg, " %s => %s\n", from, to); } } +#endif DEFINE_STACK_OF(EVP_CIPHER) static int cipher_cmp(const EVP_CIPHER * const *a, @@ -64,7 +100,8 @@ static void collect_ciphers(EVP_CIPHER *cipher, void *stack) { STACK_OF(EVP_CIPHER) *cipher_stack = stack; - if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0) + if (is_cipher_fetchable(cipher) + && sk_EVP_CIPHER_push(cipher_stack, cipher) > 0) EVP_CIPHER_up_ref(cipher); } @@ -77,8 +114,12 @@ static void list_ciphers(void) BIO_printf(bio_err, "ERROR: Memory allocation\n"); return; } - BIO_printf(bio_out, "Legacy:\n"); - EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out); +#ifndef OPENSSL_NO_DEPRECATED_3_0 + if (include_legacy()) { + BIO_printf(bio_out, "Legacy:\n"); + EVP_CIPHER_do_all_sorted(legacy_cipher_fn, bio_out); + } +#endif BIO_printf(bio_out, "Provided:\n"); EVP_CIPHER_do_all_provided(NULL, collect_ciphers, ciphers); @@ -116,7 +157,8 @@ static void list_ciphers(void) sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free); } -static void list_md_fn(const EVP_MD *m, +#ifndef OPENSSL_NO_DEPRECATED_3_0 +static void legacy_md_fn(const EVP_MD *m, const char *from, const char *to, void *arg) { if (m != NULL) { @@ -129,6 +171,7 @@ static void list_md_fn(const EVP_MD *m, BIO_printf((BIO *)arg, " %s => %s\n", from, to); } } +#endif DEFINE_STACK_OF(EVP_MD) static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b) @@ -142,12 +185,13 @@ static int md_cmp(const EVP_MD * const *a, const EVP_MD * const *b) return ret; } -static void collect_digests(EVP_MD *md, void *stack) +static void collect_digests(EVP_MD *digest, void *stack) { STACK_OF(EVP_MD) *digest_stack = stack; - if (sk_EVP_MD_push(digest_stack, md) > 0) - EVP_MD_up_ref(md); + if (is_digest_fetchable(digest) + && sk_EVP_MD_push(digest_stack, digest) > 0) + EVP_MD_up_ref(digest); } static void list_digests(void) @@ -159,8 +203,12 @@ static void list_digests(void) BIO_printf(bio_err, "ERROR: Memory allocation\n"); return; } - BIO_printf(bio_out, "Legacy:\n"); - EVP_MD_do_all_sorted(list_md_fn, bio_out); +#ifndef OPENSSL_NO_DEPRECATED_3_0 + if (include_legacy()) { + BIO_printf(bio_out, "Legacy:\n"); + EVP_MD_do_all_sorted(legacy_md_fn, bio_out); + } +#endif BIO_printf(bio_out, "Provided:\n"); EVP_MD_do_all_provided(NULL, collect_digests, digests); @@ -213,7 +261,8 @@ static void collect_macs(EVP_MAC *mac, void *stack) { STACK_OF(EVP_MAC) *mac_stack = stack; - if (sk_EVP_MAC_push(mac_stack, mac) > 0) + if (is_mac_fetchable(mac) + && sk_EVP_MAC_push(mac_stack, mac) > 0) EVP_MAC_up_ref(mac); } @@ -280,8 +329,9 @@ static void collect_kdfs(EVP_KDF *kdf, void *stack) { STACK_OF(EVP_KDF) *kdf_stack = stack; - sk_EVP_KDF_push(kdf_stack, kdf); - EVP_KDF_up_ref(kdf); + if (is_kdf_fetchable(kdf) + && sk_EVP_KDF_push(kdf_stack, kdf) > 0) + EVP_KDF_up_ref(kdf); } static void list_kdfs(void) @@ -348,8 +398,9 @@ static void collect_rands(EVP_RAND *rand, void *stack) { STACK_OF(EVP_RAND) *rand_stack = stack; - sk_EVP_RAND_push(rand_stack, rand); - EVP_RAND_up_ref(rand); + if (is_rand_fetchable(rand) + && sk_EVP_RAND_push(rand_stack, rand) > 0) + EVP_RAND_up_ref(rand); } static void list_random_generators(void) @@ -476,8 +527,9 @@ static void collect_encoders(OSSL_ENCODER *encoder, void *stack) { STACK_OF(OSSL_ENCODER) *encoder_stack = stack; - sk_OSSL_ENCODER_push(encoder_stack, encoder); - OSSL_ENCODER_up_ref(encoder); + if (is_encoder_fetchable(encoder) + && sk_OSSL_ENCODER_push(encoder_stack, encoder) > 0) + OSSL_ENCODER_up_ref(encoder); } static void list_encoders(void) @@ -543,8 +595,9 @@ static void collect_decoders(OSSL_DECODER *decoder, void *stack) { STACK_OF(OSSL_DECODER) *decoder_stack = stack; - sk_OSSL_DECODER_push(decoder_stack, decoder); - OSSL_DECODER_up_ref(decoder); + if (is_decoder_fetchable(decoder) + && sk_OSSL_DECODER_push(decoder_stack, decoder) > 0) + OSSL_DECODER_up_ref(decoder); } static void list_decoders(void) @@ -608,8 +661,9 @@ static void collect_keymanagers(EVP_KEYMGMT *km, void *stack) { STACK_OF(EVP_KEYMGMT) *km_stack = stack; - sk_EVP_KEYMGMT_push(km_stack, km); - EVP_KEYMGMT_up_ref(km); + if (is_keymgmt_fetchable(km) + && sk_EVP_KEYMGMT_push(km_stack, km) > 0) + EVP_KEYMGMT_up_ref(km); } static void list_keymanagers(void) @@ -669,12 +723,13 @@ static int signature_cmp(const EVP_SIGNATURE * const *a, return ret; } -static void collect_signatures(EVP_SIGNATURE *km, void *stack) +static void collect_signatures(EVP_SIGNATURE *sig, void *stack) { - STACK_OF(EVP_SIGNATURE) *km_stack = stack; + STACK_OF(EVP_SIGNATURE) *sig_stack = stack; - sk_EVP_SIGNATURE_push(km_stack, km); - EVP_SIGNATURE_up_ref(km); + if (is_signature_fetchable(sig) + && sk_EVP_SIGNATURE_push(sig_stack, sig) > 0) + EVP_SIGNATURE_up_ref(sig); } static void list_signatures(void) @@ -731,12 +786,13 @@ static int kem_cmp(const EVP_KEM * const *a, return ret; } -static void collect_kem(EVP_KEM *km, void *stack) +static void collect_kem(EVP_KEM *kem, void *stack) { - STACK_OF(EVP_KEM) *km_stack = stack; + STACK_OF(EVP_KEM) *kem_stack = stack; - sk_EVP_KEM_push(km_stack, km); - EVP_KEM_up_ref(km); + if (is_kem_fetchable(kem) + && sk_EVP_KEM_push(kem_stack, kem) > 0) + EVP_KEM_up_ref(kem); } static void list_kems(void) @@ -792,12 +848,13 @@ static int asymcipher_cmp(const EVP_ASYM_CIPHER * const *a, return ret; } -static void collect_asymciph(EVP_ASYM_CIPHER *km, void *stack) +static void collect_asymciph(EVP_ASYM_CIPHER *asym_cipher, void *stack) { - STACK_OF(EVP_ASYM_CIPHER) *km_stack = stack; + STACK_OF(EVP_ASYM_CIPHER) *asym_cipher_stack = stack; - sk_EVP_ASYM_CIPHER_push(km_stack, km); - EVP_ASYM_CIPHER_up_ref(km); + if (is_asym_cipher_fetchable(asym_cipher) + && sk_EVP_ASYM_CIPHER_push(asym_cipher_stack, asym_cipher) > 0) + EVP_ASYM_CIPHER_up_ref(asym_cipher); } static void list_asymciphers(void) @@ -856,12 +913,13 @@ static int kex_cmp(const EVP_KEYEXCH * const *a, return ret; } -static void collect_kex(EVP_KEYEXCH *ke, void *stack) +static void collect_kex(EVP_KEYEXCH *kex, void *stack) { STACK_OF(EVP_KEYEXCH) *kex_stack = stack; - sk_EVP_KEYEXCH_push(kex_stack, ke); - EVP_KEYEXCH_up_ref(ke); + if (is_keyexch_fetchable(kex) + && sk_EVP_KEYEXCH_push(kex_stack, kex) > 0) + EVP_KEYEXCH_up_ref(kex); } static void list_keyexchanges(void) @@ -1012,33 +1070,35 @@ static void list_options_for_command(const char *command) static int is_md_available(const char *name) { EVP_MD *md; + const char *propq = app_get0_propq(); /* Look through providers' digests */ ERR_set_mark(); - md = EVP_MD_fetch(NULL, name, NULL); + md = EVP_MD_fetch(NULL, name, propq); ERR_pop_to_mark(); if (md != NULL) { EVP_MD_free(md); return 1; } - return (get_digest_from_engine(name) == NULL) ? 0 : 1; + return propq != NULL || get_digest_from_engine(name) == NULL ? 0 : 1; } static int is_cipher_available(const char *name) { EVP_CIPHER *cipher; + const char *propq = app_get0_propq(); /* Look through providers' ciphers */ ERR_set_mark(); - cipher = EVP_CIPHER_fetch(NULL, name, NULL); + cipher = EVP_CIPHER_fetch(NULL, name, propq); ERR_pop_to_mark(); if (cipher != NULL) { EVP_CIPHER_free(cipher); return 1; } - return (get_cipher_from_engine(name) == NULL) ? 0 : 1; + return propq != NULL || get_cipher_from_engine(name) == NULL ? 0 : 1; } static void list_type(FUNC_TYPE ft, int one) @@ -1084,7 +1144,7 @@ static void list_pkey(void) #ifndef OPENSSL_NO_DEPRECATED_3_0 int i; - if (select_name == NULL) { + if (select_name == NULL && include_legacy()) { BIO_printf(bio_out, "Legacy:\n"); for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { const EVP_PKEY_ASN1_METHOD *ameth; @@ -1121,7 +1181,7 @@ static void list_pkey_meth(void) size_t i; size_t meth_count = EVP_PKEY_meth_get_count(); - if (select_name == NULL) { + if (select_name == NULL && include_legacy()) { BIO_printf(bio_out, "Legacy:\n"); for (i = 0; i < meth_count; i++) { const EVP_PKEY_METHOD *pmeth = EVP_PKEY_meth_get0(i); |