]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Ensure pkey_set_type handles ENGINE references correctly
authorMatt Caswell <matt@openssl.org>
Fri, 15 Oct 2021 15:23:31 +0000 (16:23 +0100)
committerMatt Caswell <matt@openssl.org>
Thu, 21 Oct 2021 16:08:24 +0000 (17:08 +0100)
pkey_set_type should not consume the ENGINE references that may be
passed to it.

Fixes #16757

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

crypto/evp/p_lib.c

index 9f1a485a5b832b212e36bcb82399bbe02ffd5bf4..7e262c573b27e7133263108aaece44d261133138 100644 (file)
@@ -212,10 +212,15 @@ static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
     }
     if (pkey) {
         pkey->ameth = ameth;
-        pkey->engine = e;
-
         pkey->type = pkey->ameth->pkey_id;
         pkey->save_type = type;
+# ifndef OPENSSL_NO_ENGINE
+        if (eptr == NULL && e != NULL && !ENGINE_init(e)) {
+            EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_INITIALIZATION_ERROR);
+            return 0;
+        }
+# endif
+        pkey->engine = e;
     }
     return 1;
 }