EVP: Only use the engine when one is defined, in pkey_mac_ctrl()
[openssl.git] / crypto / evp / pkey_mac.c
index 7452e0320d8c9d6fade16e7ae54120608e027bec..3503aac6d306855e22ca7f610da93c67b8cd02d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -308,11 +308,14 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
                 OSSL_PARAM params[3];
                 size_t params_n = 0;
                 char *ciphname = (char *)OBJ_nid2sn(EVP_CIPHER_nid(p2));
+
 #ifndef OPENSSL_NO_ENGINE
-                char *engineid = (char *)ENGINE_get_id(ctx->engine);
+                if (ctx->engine != NULL) {
+                    char *engid = (char *)ENGINE_get_id(ctx->engine);
 
-                params[params_n++] =
-                    OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
+                    params[params_n++] =
+                        OSSL_PARAM_construct_utf8_string("engine", engid, 0);
+                }
 #endif
                 params[params_n++] =
                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
@@ -458,13 +461,14 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
                 size_t params_n = 0;
                 char *mdname =
                     (char *)OBJ_nid2sn(EVP_MD_nid(hctx->raw_data.md));
+
 #ifndef OPENSSL_NO_ENGINE
-                char *engineid = ctx->engine == NULL
-                    ? NULL : (char *)ENGINE_get_id(ctx->engine);
+                if (ctx->engine != NULL) {
+                    char *engid = (char *)ENGINE_get_id(ctx->engine);
 
-                if (engineid != NULL)
                     params[params_n++] =
-                        OSSL_PARAM_construct_utf8_string("engine", engineid, 0);
+                        OSSL_PARAM_construct_utf8_string("engine", engid, 0);
+                }
 #endif
                 params[params_n++] =
                     OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
@@ -493,13 +497,24 @@ static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
 }
 
 static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
-                              const char *type, const char *value)
+                             const char *type, const char *value)
 {
     MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
-    const EVP_MAC *mac = EVP_MAC_CTX_mac(hctx->ctx);
+    const EVP_MAC *mac;
     OSSL_PARAM params[2];
     int ok = 0;
 
+    if (hctx == NULL) {
+        EVPerr(0, EVP_R_NULL_MAC_PKEY_CTX);
+        return 0;
+    }
+    if (hctx->ctx == NULL) {
+        /* This actually means the fetch failed during the init call */
+        EVPerr(0, EVP_R_FETCH_FAILED);
+        return 0;
+    }
+    mac = EVP_MAC_CTX_mac(hctx->ctx);
+
     /*
      * Translation of some control names that are equivalent to a single
      * parameter name.
@@ -520,12 +535,6 @@ static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
         return 0;
     params[1] = OSSL_PARAM_construct_end();
 
-    if (hctx->ctx == NULL) {
-        /* This actually means the fetch failed during the init call */
-        EVPerr(0, EVP_R_FETCH_FAILED);
-        return 0;
-    }
-
     ok = EVP_MAC_CTX_set_params(hctx->ctx, params);
     OPENSSL_free(params[0].data);
     return ok;