]> git.ipfire.org Git - thirdparty/openssl.git/blobdiff - crypto/evp/m_sigver.c
EVP: Add EVP_PKEY_get_default_digest_name() and use it
[thirdparty/openssl.git] / crypto / evp / m_sigver.c
index 79099b1e356818d57cbabb6cbe0fb9c2fa518f94..05dc46e3b19d06836ca0aefb28c24f2d0d642ec8 100644 (file)
@@ -33,6 +33,7 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
     EVP_SIGNATURE *signature = NULL;
     EVP_KEYMGMT *tmp_keymgmt = NULL;
     const char *supported_sig = NULL;
+    char locmdname[80] = "";     /* 80 chars should be enough */
     void *provkey = NULL;
     int ret;
 
@@ -63,22 +64,6 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
     if (locpctx->keytype == NULL)
         goto legacy;
 
-    if (mdname == NULL) {
-        if (type != NULL) {
-            mdname = EVP_MD_name(type);
-        } else if (pkey != NULL) {
-            /*
-             * TODO(v3.0) work out a better way for EVP_PKEYs with no legacy
-             * component.
-             */
-            if (pkey->pkey.ptr != NULL) {
-                int def_nid;
-                if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
-                    mdname = OBJ_nid2sn(def_nid);
-            }
-        }
-    }
-
     /* Ensure that the key is provided.  If not, go legacy */
     tmp_keymgmt = locpctx->keymgmt;
     provkey = evp_pkey_make_provided(locpctx->pkey, locpctx->libctx,
@@ -131,6 +116,9 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
 
     /* No more legacy from here down to legacy: */
 
+    if (pctx != NULL)
+        *pctx = locpctx;
+
     locpctx->op.sig.signature = signature;
     locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
                              : EVP_PKEY_OP_SIGNCTX;
@@ -142,15 +130,25 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
     }
     if (type != NULL) {
         ctx->reqdigest = type;
+        if (mdname == NULL)
+            mdname = EVP_MD_name(type);
     } else {
-        /*
-         * This might be requested by a later call to EVP_MD_CTX_md(). In that
-         * case the "explicit fetch" rules apply for that function (as per
-         * man pages), i.e. the ref count is not updated so the EVP_MD should
-         * not be used beyound the lifetime of the EVP_MD_CTX.
-         */
-        ctx->reqdigest = ctx->fetched_digest =
-            EVP_MD_fetch(locpctx->libctx, mdname, props);
+        if (mdname == NULL
+            && EVP_PKEY_get_default_digest_name(locpctx->pkey, locmdname,
+                                                sizeof(locmdname)))
+            mdname = locmdname;
+
+        if (mdname != NULL) {
+            /*
+             * This might be requested by a later call to EVP_MD_CTX_md().
+             * In that case the "explicit fetch" rules apply for that
+             * function (as per man pages), i.e. the ref count is not updated
+             * so the EVP_MD should not be used beyound the lifetime of the
+             * EVP_MD_CTX.
+             */
+            ctx->reqdigest = ctx->fetched_digest =
+                EVP_MD_fetch(locpctx->libctx, mdname, props);
+        }
     }
 
     if (ver) {