Correctly display the signing/hmac algorithm in the dgst app
authorMatt Caswell <matt@openssl.org>
Sun, 13 Sep 2020 10:09:20 +0000 (11:09 +0100)
committerDmitry Belyavskiy <beldmit@gmail.com>
Thu, 17 Sep 2020 08:12:08 +0000 (11:12 +0300)
In OpenSSL 1.1.1 doing an HMAC operation with (say) SHA1 would produce
output like this:

HMAC-SHA1(README.md)= 553154e4c0109ddc320bb495735906ad7135c2f1

Prior to this change master would instead display this like so:

SHA1(README.md)= 553154e4c0109ddc320bb495735906ad7135c2f1

The problem is that dgst was using EVP_PKEY_asn1_get0_info() to get
the algorithm name from the EVP_PKEY. This doesn't work with provider
based keys. Instead we introduce a new EVP_PKEY_get0_first_alg_name()
function, and an equivalent EVP_KEYMGMT_get0_first_name() function.

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/12850)

apps/dgst.c
crypto/evp/evp_pkey.c
crypto/evp/keymgmt_meth.c
include/openssl/evp.h
util/libcrypto.num

index 7fc7da1e53abf18f5d9534adabafa7be902cf553..650115b468124deab1df2ff854b6f193cc98beba 100644 (file)
@@ -406,13 +406,8 @@ int dgst_main(int argc, char **argv)
     } else {
         const char *sig_name = NULL;
         if (!out_bin) {
-            if (sigkey != NULL) {
-                const EVP_PKEY_ASN1_METHOD *ameth;
-                ameth = EVP_PKEY_get0_asn1(sigkey);
-                if (ameth)
-                    EVP_PKEY_asn1_get0_info(NULL, NULL,
-                                            NULL, NULL, &sig_name, ameth);
-            }
+            if (sigkey != NULL)
+                sig_name = EVP_PKEY_get0_first_alg_name(sigkey);
         }
         ret = 0;
         for (i = 0; i < argc; i++) {
index d435c86087ca9f4002ff1be39449cb893549c044..f31d1d68f8b7c15ca3ff2a5553dcfa3fbe0b53c6 100644 (file)
@@ -163,3 +163,20 @@ int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
         return 1;
     return 0;
 }
+
+const char *EVP_PKEY_get0_first_alg_name(const EVP_PKEY *key)
+{
+    const EVP_PKEY_ASN1_METHOD *ameth;
+    const char *name = NULL;
+
+    if (key->keymgmt != NULL)
+        return EVP_KEYMGMT_get0_first_name(key->keymgmt);
+
+    /* Otherwise fallback to legacy */
+    ameth = EVP_PKEY_get0_asn1(key);
+    if (ameth != NULL)
+        EVP_PKEY_asn1_get0_info(NULL, NULL,
+                                NULL, NULL, &name, ameth);
+
+    return name;
+}
index 1459b64f0e4cacc8455a5416da70246044730a93..5453ceadda35806b9f68dbd209a09bb4e4ab443a 100644 (file)
@@ -249,6 +249,11 @@ int EVP_KEYMGMT_number(const EVP_KEYMGMT *keymgmt)
     return keymgmt->name_id;
 }
 
+const char *EVP_KEYMGMT_get0_first_name(const EVP_KEYMGMT *keymgmt)
+{
+    return evp_first_name(keymgmt->prov, keymgmt->name_id);
+}
+
 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
 {
     return evp_is_a(keymgmt->prov, keymgmt->name_id, NULL, name);
index 4d0cc9d560216585a2478ab0a4a93612393c4d13..ff97198542a005ae3e6534a76b86d1267f795097 100644 (file)
@@ -1496,6 +1496,8 @@ int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len);
 int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id);
 int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len);
 
+const char *EVP_PKEY_get0_first_alg_name(const EVP_PKEY *key);
+
 # define EVP_PKEY_OP_UNDEFINED           0
 # define EVP_PKEY_OP_PARAMGEN            (1<<1)
 # define EVP_PKEY_OP_KEYGEN              (1<<2)
@@ -1573,6 +1575,7 @@ EVP_KEYMGMT *EVP_KEYMGMT_fetch(OPENSSL_CTX *ctx, const char *algorithm,
 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt);
 void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt);
 const OSSL_PROVIDER *EVP_KEYMGMT_provider(const EVP_KEYMGMT *keymgmt);
+const char *EVP_KEYMGMT_get0_first_name(const EVP_KEYMGMT *keymgmt);
 int EVP_KEYMGMT_number(const EVP_KEYMGMT *keymgmt);
 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name);
 void EVP_KEYMGMT_do_all_provided(OPENSSL_CTX *libctx,
index 6070b570a58a0fa91d24431fa7005cbf2b975acc..a1b8ce0be12dda0e9c902249b03e15161610cbca 100644 (file)
@@ -5280,3 +5280,5 @@ EVP_PKEY_CTX_get1_id_len                ? 3_0_0   EXIST::FUNCTION:
 CMS_AuthEnvelopedData_create            ?      3_0_0   EXIST::FUNCTION:CMS
 CMS_AuthEnvelopedData_create_with_libctx ?     3_0_0   EXIST::FUNCTION:CMS
 EVP_PKEY_CTX_set_ec_param_enc           ?      3_0_0   EXIST::FUNCTION:EC
+EVP_PKEY_get0_first_alg_name            ?      3_0_0   EXIST::FUNCTION:
+EVP_KEYMGMT_get0_first_name             ?      3_0_0   EXIST::FUNCTION: