From c27d5f27b7ba4c92fc9645b661fbdb719c200299 Mon Sep 17 00:00:00 2001 From: tb Date: Thu, 28 Dec 2023 21:58:12 +0000 Subject: [PATCH] Rework rsa_priv_decode() Turn the function into single exit and error check EVP_PKEY_assign() for style. ok jsing --- lib/libcrypto/rsa/rsa_ameth.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/libcrypto/rsa/rsa_ameth.c b/lib/libcrypto/rsa/rsa_ameth.c index b2f6b2f79b8..a43bcf9f9a0 100644 --- a/lib/libcrypto/rsa/rsa_ameth.c +++ b/lib/libcrypto/rsa/rsa_ameth.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_ameth.c,v 1.52 2023/12/28 21:57:08 tb Exp $ */ +/* $OpenBSD: rsa_ameth.c,v 1.53 2023/12/28 21:58:12 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ @@ -264,24 +264,27 @@ static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) { const unsigned char *p; - RSA *rsa; + RSA *rsa = NULL; int pklen; const X509_ALGOR *alg; + int ret = 0; if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8)) - return 0; - rsa = d2i_RSAPrivateKey(NULL, &p, pklen); - if (rsa == NULL) { - RSAerror(ERR_R_RSA_LIB); - return 0; - } - if (!rsa_param_decode(rsa, alg)) { - RSA_free(rsa); - return 0; - } - EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa); + goto err; + if ((rsa = d2i_RSAPrivateKey(NULL, &p, pklen)) == NULL) + goto err; + if (!rsa_param_decode(rsa, alg)) + goto err; + if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) + goto err; + rsa = NULL; - return 1; + ret = 1; + + err: + RSA_free(rsa); + + return ret; } static int -- 2.20.1