]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
providers/implementations/keymgmt/rsa_kmgmt.c: refactor gen_init
authorPeiwei Hu <jlu.hpw@foxmail.com>
Thu, 6 Jan 2022 01:47:05 +0000 (09:47 +0800)
committerPauli <ppzgs1@gmail.com>
Fri, 7 Jan 2022 10:08:13 +0000 (21:08 +1100)
There is risk to pass the gctx with NULL value to rsa_gen_set_params
which dereference gctx directly.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17429)

providers/implementations/keymgmt/rsa_kmgmt.c

index b1c3011f1452d86b520960bfcf2ddd5a2f12c052..29e5d108137ec441eca28e8c73763080a438ffca 100644 (file)
@@ -454,19 +454,24 @@ static void *gen_init(void *provctx, int selection, int rsa_type,
         gctx->libctx = libctx;
         if ((gctx->pub_exp = BN_new()) == NULL
             || !BN_set_word(gctx->pub_exp, RSA_F4)) {
-            BN_free(gctx->pub_exp);
-            OPENSSL_free(gctx);
-            return NULL;
+            goto err;
         }
         gctx->nbits = 2048;
         gctx->primes = RSA_DEFAULT_PRIME_NUM;
         gctx->rsa_type = rsa_type;
+    } else {
+        goto err;
     }
-    if (!rsa_gen_set_params(gctx, params)) {
-        OPENSSL_free(gctx);
-        return NULL;
-    }
+
+    if (!rsa_gen_set_params(gctx, params))
+        goto err;
     return gctx;
+
+err:
+    if (gctx != NULL)
+        BN_free(gctx->pub_exp);
+    OPENSSL_free(gctx);
+    return NULL;
 }
 
 static void *rsa_gen_init(void *provctx, int selection,