From 680e1051be3c48b074d687812a44e00e3b35a8ef Mon Sep 17 00:00:00 2001 From: tb Date: Thu, 4 Jan 2024 17:08:57 +0000 Subject: [PATCH] Simplify EVP_PKEY_asn1_find() EVP_PKEY_asn1_find() finds the EVP_PKEY_ASN1_METHOD underlying the method or alias with nid (or, rather, pkey_id) passed in. Now that we have the base method stored in a pointer, we can return that method after a simple lookup of said nid (or, rather, pkey_id). ok jsing --- lib/libcrypto/evp/p_lib.c | 42 +++++++++------------------------------ 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/lib/libcrypto/evp/p_lib.c b/lib/libcrypto/evp/p_lib.c index b43a0fcd5dc..d9cb66336c1 100644 --- a/lib/libcrypto/evp/p_lib.c +++ b/lib/libcrypto/evp/p_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: p_lib.c,v 1.54 2024/01/04 17:01:26 tb Exp $ */ +/* $OpenBSD: p_lib.c,v 1.55 2024/01/04 17:08:57 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -188,44 +188,20 @@ EVP_PKEY_asn1_get0(int idx) return asn1_methods[idx]; } -static const EVP_PKEY_ASN1_METHOD * -pkey_asn1_find(int pkey_id) -{ - const EVP_PKEY_ASN1_METHOD *ameth; - int i; - - for (i = EVP_PKEY_asn1_get_count() - 1; i >= 0; i--) { - ameth = EVP_PKEY_asn1_get0(i); - if (ameth->pkey_id == pkey_id) - return ameth; - } - - return NULL; -} - -/* - * XXX - fix this. In what looks like an infinite loop, this API only makes two - * calls to pkey_asn1_find(): If the type resolves to an aliased ASN.1 method, - * the second call will find the method it aliases. Codify this in regress and - * make this explicit in code. - */ const EVP_PKEY_ASN1_METHOD * -EVP_PKEY_asn1_find(ENGINE **pe, int type) +EVP_PKEY_asn1_find(ENGINE **engine, int pkey_id) { - const EVP_PKEY_ASN1_METHOD *mp; + size_t i; - if (pe != NULL) - *pe = NULL; + if (engine != NULL) + *engine = NULL; - for (;;) { - if ((mp = pkey_asn1_find(type)) == NULL) - break; - if ((mp->pkey_flags & ASN1_PKEY_ALIAS) == 0) - break; - type = mp->base_method->pkey_id; + for (i = 0; i < N_ASN1_METHODS; i++) { + if (asn1_methods[i]->pkey_id == pkey_id) + return asn1_methods[i]->base_method; } - return mp; + return NULL; } const EVP_PKEY_ASN1_METHOD * -- 2.20.1