Blame SOURCES/0005-Use-default-OpenSSL-context-for-internal-crypto-oper.patch

897056
From 5b777f29fd612f9972d416ed77b90156e2373e9f Mon Sep 17 00:00:00 2001
897056
From: Petr Gotthard <petr.gotthard@centrum.cz>
897056
Date: Wed, 25 Aug 2021 14:02:38 +0200
897056
Subject: [PATCH 05/23] Use default OpenSSL context for internal crypto
897056
 operations
897056
897056
The TPM2 provider may be loaded in the global library context.
897056
As we don't want the TPM to be called for some operations, we have
897056
to initialize own library context with the default provider.
897056
897056
This is similar to the RAND_set_rand_method dance with older OpenSSL.
897056
897056
Signed-off-by: Petr Gotthard <petr.gotthard@centrum.cz>
897056
---
897056
 src/tss2-esys/esys_crypto_ossl.c | 175 ++++++++++++++++++++++---------
897056
 src/tss2-fapi/fapi_crypto.c      | 152 ++++++++++++++++++---------
897056
 2 files changed, 225 insertions(+), 102 deletions(-)
897056
897056
diff --git a/src/tss2-esys/esys_crypto_ossl.c b/src/tss2-esys/esys_crypto_ossl.c
897056
index ab08b3b8..35af2028 100644
897056
--- a/src/tss2-esys/esys_crypto_ossl.c
897056
+++ b/src/tss2-esys/esys_crypto_ossl.c
897056
@@ -66,38 +66,101 @@ typedef struct _IESYS_CRYPTO_CONTEXT {
897056
     } type; /**< The type of context to hold; hash or hmac */
897056
     union {
897056
         struct {
897056
-            EVP_MD_CTX  *ossl_context;
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
             const EVP_MD *ossl_hash_alg;
897056
+#else
897056
+            OSSL_LIB_CTX *ossl_libctx;
897056
+            EVP_MD *ossl_hash_alg;
897056
+#endif
897056
+            EVP_MD_CTX  *ossl_context;
897056
             size_t hash_len;
897056
-        } hash; /**< the state variables for a hash context */
897056
-        struct {
897056
-            EVP_MD_CTX *ossl_context;
897056
-            const EVP_MD *ossl_hash_alg;
897056
-            size_t hmac_len;
897056
-        } hmac; /**< the state variables for an hmac context */
897056
+        } hash; /**< the state variables for a HASH or HMAC context */
897056
     };
897056
 } IESYS_CRYPTOSSL_CONTEXT;
897056
 
897056
-const EVP_MD *
897056
+static IESYS_CRYPTOSSL_CONTEXT *
897056
+iesys_cryptossl_context_new() {
897056
+    IESYS_CRYPTOSSL_CONTEXT *ctx;
897056
+
897056
+    if (!(ctx = calloc(1, sizeof(IESYS_CRYPTOSSL_CONTEXT))))
897056
+        return NULL;
897056
+
897056
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
897056
+    /* The TPM2 provider may be loaded in the global library context.
897056
+     * As we don't want the TPM to be called for these operations, we have
897056
+     * to initialize own library context with the default provider. */
897056
+    if (!(ctx->hash.ossl_libctx = OSSL_LIB_CTX_new())) {
897056
+        SAFE_FREE(ctx);
897056
+        return NULL;
897056
+    }
897056
+#endif
897056
+    return ctx;
897056
+}
897056
+
897056
+static void
897056
+iesys_cryptossl_context_free(IESYS_CRYPTOSSL_CONTEXT *ctx) {
897056
+    if (!ctx)
897056
+        return;
897056
+
897056
+    EVP_MD_CTX_free(ctx->hash.ossl_context);
897056
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
897056
+    EVP_MD_free(ctx->hash.ossl_hash_alg);
897056
+    OSSL_LIB_CTX_free(ctx->hash.ossl_libctx);
897056
+#endif
897056
+    SAFE_FREE(ctx);
897056
+}
897056
+
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
+static const EVP_MD *
897056
 get_ossl_hash_md(TPM2_ALG_ID hashAlg)
897056
 {
897056
     switch (hashAlg) {
897056
     case TPM2_ALG_SHA1:
897056
         return EVP_sha1();
897056
-        break;
897056
     case TPM2_ALG_SHA256:
897056
         return EVP_sha256();
897056
-        break;
897056
     case TPM2_ALG_SHA384:
897056
         return EVP_sha384();
897056
-        break;
897056
     case TPM2_ALG_SHA512:
897056
         return EVP_sha512();
897056
-        break;
897056
     default:
897056
         return NULL;
897056
     }
897056
 }
897056
+#else
897056
+static const char *
897056
+get_ossl_hash_md(TPM2_ALG_ID hashAlg)
897056
+{
897056
+    switch (hashAlg) {
897056
+    case TPM2_ALG_SHA1:
897056
+        return "SHA1";
897056
+    case TPM2_ALG_SHA256:
897056
+        return "SHA256";
897056
+    case TPM2_ALG_SHA384:
897056
+        return "SHA384";
897056
+    case TPM2_ALG_SHA512:
897056
+        return "SHA512";
897056
+    default:
897056
+        return NULL;
897056
+    }
897056
+}
897056
+#endif
897056
+
897056
+static int
897056
+iesys_cryptossl_context_set_hash_md(IESYS_CRYPTOSSL_CONTEXT *ctx, TPM2_ALG_ID hashAlg) {
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
+    ctx->hash.ossl_hash_alg = get_ossl_hash_md(hashAlg);
897056
+#else
897056
+    const char *alg_name =  get_ossl_hash_md(hashAlg);
897056
+    if (!alg_name)
897056
+        return 0;
897056
+    ctx->hash.ossl_hash_alg = EVP_MD_fetch(ctx->hash.ossl_libctx, alg_name, NULL);
897056
+#endif
897056
+    if (!ctx->hash.ossl_hash_alg)
897056
+        return 0;
897056
+
897056
+    return 1;
897056
+}
897056
 
897056
 /** Provide the context for the computation of a hash digest.
897056
  *
897056
@@ -117,12 +180,12 @@ iesys_cryptossl_hash_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
     LOG_TRACE("call: context=%p hashAlg=%"PRIu16, context, hashAlg);
897056
     return_if_null(context, "Context is NULL", TSS2_ESYS_RC_BAD_REFERENCE);
897056
     return_if_null(context, "Null-Pointer passed for context", TSS2_ESYS_RC_BAD_REFERENCE);
897056
-    IESYS_CRYPTOSSL_CONTEXT *mycontext;
897056
-    mycontext = calloc(1, sizeof(IESYS_CRYPTOSSL_CONTEXT));
897056
+
897056
+    IESYS_CRYPTOSSL_CONTEXT *mycontext = iesys_cryptossl_context_new();
897056
     return_if_null(mycontext, "Out of Memory", TSS2_ESYS_RC_MEMORY);
897056
     mycontext->type = IESYS_CRYPTOSSL_TYPE_HASH;
897056
 
897056
-    if (!(mycontext->hash.ossl_hash_alg = get_ossl_hash_md(hashAlg))) {
897056
+    if (!iesys_cryptossl_context_set_hash_md(mycontext, hashAlg)) {
897056
         goto_error(r, TSS2_ESYS_RC_NOT_IMPLEMENTED,
897056
                    "Unsupported hash algorithm (%"PRIu16")", cleanup, hashAlg);
897056
     }
897056
@@ -132,12 +195,12 @@ iesys_cryptossl_hash_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
                    "Unsupported hash algorithm (%"PRIu16")", cleanup, hashAlg);
897056
     }
897056
 
897056
-    if (!(mycontext->hash.ossl_context =  EVP_MD_CTX_create())) {
897056
+    if (!(mycontext->hash.ossl_context = EVP_MD_CTX_create())) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Error EVP_MD_CTX_create", cleanup);
897056
     }
897056
 
897056
     if (1 != EVP_DigestInit(mycontext->hash.ossl_context,
897056
-                               mycontext->hash.ossl_hash_alg)) {
897056
+                            mycontext->hash.ossl_hash_alg)) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Errror EVP_DigestInit", cleanup);
897056
     }
897056
 
897056
@@ -146,9 +209,7 @@ iesys_cryptossl_hash_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
     return TSS2_RC_SUCCESS;
897056
 
897056
  cleanup:
897056
-    if (mycontext->hash.ossl_context)
897056
-        EVP_MD_CTX_destroy(mycontext->hash.ossl_context);
897056
-    SAFE_FREE(mycontext);
897056
+    iesys_cryptossl_context_free(mycontext);
897056
 
897056
     return r;
897056
 }
897056
@@ -252,8 +313,8 @@ iesys_cryptossl_hash_finish(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
     LOGBLOB_TRACE(buffer, mycontext->hash.hash_len, "read hash result");
897056
 
897056
     *size = mycontext->hash.hash_len;
897056
-    EVP_MD_CTX_destroy(mycontext->hash.ossl_context);
897056
-    free(mycontext);
897056
+
897056
+    iesys_cryptossl_context_free(mycontext);
897056
     *context = NULL;
897056
 
897056
     return TSS2_RC_SUCCESS;
897056
@@ -279,8 +340,7 @@ iesys_cryptossl_hash_abort(IESYS_CRYPTO_CONTEXT_BLOB ** context)
897056
         return;
897056
     }
897056
 
897056
-    EVP_MD_CTX_destroy(mycontext->hash.ossl_context);
897056
-    free(mycontext);
897056
+    iesys_cryptossl_context_free(mycontext);
897056
     *context = NULL;
897056
 }
897056
 
897056
@@ -313,20 +373,20 @@ iesys_cryptossl_hmac_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
         return_error(TSS2_ESYS_RC_BAD_REFERENCE,
897056
                      "Null-Pointer passed in for context");
897056
     }
897056
-    IESYS_CRYPTOSSL_CONTEXT *mycontext = calloc(1, sizeof(IESYS_CRYPTOSSL_CONTEXT));
897056
+    IESYS_CRYPTOSSL_CONTEXT *mycontext = iesys_cryptossl_context_new();
897056
     return_if_null(mycontext, "Out of Memory", TSS2_ESYS_RC_MEMORY);
897056
 
897056
-    if (!(mycontext->hmac.ossl_hash_alg = get_ossl_hash_md(hashAlg))) {
897056
+    if (!iesys_cryptossl_context_set_hash_md(mycontext, hashAlg)) {
897056
         goto_error(r, TSS2_ESYS_RC_NOT_IMPLEMENTED,
897056
                    "Unsupported hash algorithm (%"PRIu16")", cleanup, hashAlg);
897056
     }
897056
 
897056
-    if (iesys_crypto_hash_get_digest_size(hashAlg, &mycontext->hmac.hmac_len)) {
897056
+    if (iesys_crypto_hash_get_digest_size(hashAlg, &mycontext->hash.hash_len)) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
897056
                    "Unsupported hash algorithm (%"PRIu16")", cleanup, hashAlg);
897056
     }
897056
 
897056
-    if (!(mycontext->hmac.ossl_context =  EVP_MD_CTX_create())) {
897056
+    if (!(mycontext->hash.ossl_context = EVP_MD_CTX_create())) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
897056
                    "Error EVP_MD_CTX_create", cleanup);
897056
     }
897056
@@ -341,8 +401,8 @@ iesys_cryptossl_hmac_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
                    "Failed to create HMAC key", cleanup);
897056
     }
897056
 
897056
-    if(1 != EVP_DigestSignInit(mycontext->hmac.ossl_context, NULL,
897056
-                               mycontext->hmac.ossl_hash_alg, NULL, hkey)) {
897056
+    if(1 != EVP_DigestSignInit(mycontext->hash.ossl_context, NULL,
897056
+                               mycontext->hash.ossl_hash_alg, NULL, hkey)) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
897056
                    "DigestSignInit", cleanup);
897056
     }
897056
@@ -356,11 +416,9 @@ iesys_cryptossl_hmac_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
     return TSS2_RC_SUCCESS;
897056
 
897056
  cleanup:
897056
-    if (mycontext->hmac.ossl_context)
897056
-        EVP_MD_CTX_destroy(mycontext->hmac.ossl_context);
897056
     if(hkey)
897056
         EVP_PKEY_free(hkey);
897056
-    SAFE_FREE(mycontext);
897056
+    iesys_cryptossl_context_free(mycontext);
897056
     return r;
897056
 }
897056
 
897056
@@ -391,7 +449,7 @@ iesys_cryptossl_hmac_update(IESYS_CRYPTO_CONTEXT_BLOB * context,
897056
     LOGBLOB_TRACE(buffer, size, "Updating hmac with");
897056
 
897056
     /* Call update with the message */
897056
-    if(1 != EVP_DigestSignUpdate(mycontext->hmac.ossl_context, buffer, size)) {
897056
+    if(1 != EVP_DigestSignUpdate(mycontext->hash.ossl_context, buffer, size)) {
897056
         return_error(TSS2_ESYS_RC_GENERAL_FAILURE, "OSSL HMAC update");
897056
     }
897056
 
897056
@@ -448,19 +506,18 @@ iesys_cryptossl_hmac_finish(IESYS_CRYPTO_CONTEXT_BLOB ** context,
897056
         return_error(TSS2_ESYS_RC_BAD_REFERENCE, "bad context");
897056
     }
897056
 
897056
-    if (*size < mycontext->hmac.hmac_len) {
897056
+    if (*size < mycontext->hash.hash_len) {
897056
         return_error(TSS2_ESYS_RC_BAD_SIZE, "Buffer too small");
897056
     }
897056
 
897056
-    if (1 != EVP_DigestSignFinal(mycontext->hmac.ossl_context, buffer, size)) {
897056
+    if (1 != EVP_DigestSignFinal(mycontext->hash.ossl_context, buffer, size)) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "DigestSignFinal", cleanup);
897056
     }
897056
 
897056
     LOGBLOB_TRACE(buffer, *size, "read hmac result");
897056
 
897056
  cleanup:
897056
-    EVP_MD_CTX_destroy(mycontext->hmac.ossl_context);
897056
-    SAFE_FREE(mycontext);
897056
+    iesys_cryptossl_context_free(mycontext);
897056
     *context = NULL;
897056
     return r;
897056
 }
897056
@@ -510,9 +567,7 @@ iesys_cryptossl_hmac_abort(IESYS_CRYPTO_CONTEXT_BLOB ** context)
897056
             return;
897056
         }
897056
 
897056
-        EVP_MD_CTX_destroy(mycontext->hmac.ossl_context);
897056
-
897056
-        free(mycontext);
897056
+        iesys_cryptossl_context_free(mycontext);
897056
         *context = NULL;
897056
     }
897056
 }
897056
@@ -529,9 +584,14 @@ iesys_cryptossl_hmac_abort(IESYS_CRYPTO_CONTEXT_BLOB ** context)
897056
 TSS2_RC
897056
 iesys_cryptossl_random2b(TPM2B_NONCE * nonce, size_t num_bytes)
897056
 {
897056
+    int rc;
897056
 #if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
     const RAND_METHOD *rand_save = RAND_get_rand_method();
897056
     RAND_set_rand_method(RAND_OpenSSL());
897056
+#else
897056
+    OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
897056
+    if (!libctx)
897056
+        return TSS2_ESYS_RC_MEMORY;
897056
 #endif
897056
 
897056
     if (num_bytes == 0) {
897056
@@ -540,16 +600,16 @@ iesys_cryptossl_random2b(TPM2B_NONCE * nonce, size_t num_bytes)
897056
         nonce->size = num_bytes;
897056
     }
897056
 
897056
-    if (1 != RAND_bytes(&nonce->buffer[0], nonce->size)) {
897056
 #if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
-        RAND_set_rand_method(rand_save);
897056
+    rc = RAND_bytes(&nonce->buffer[0], nonce->size);
897056
+    RAND_set_rand_method(rand_save);
897056
+#else
897056
+    rc = RAND_bytes_ex(libctx, &nonce->buffer[0], nonce->size, 0);
897056
+    OSSL_LIB_CTX_free(libctx);
897056
 #endif
897056
+    if (rc != 1)
897056
         return_error(TSS2_ESYS_RC_GENERAL_FAILURE,
897056
                      "Failure in random number generator.");
897056
-    }
897056
-#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
-    RAND_set_rand_method(rand_save);
897056
-#endif
897056
     return TSS2_RC_SUCCESS;
897056
 }
897056
 
897056
@@ -578,28 +638,37 @@ iesys_cryptossl_pk_encrypt(TPM2B_PUBLIC * pub_tpm_key,
897056
 {
897056
 #if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
     RSA *rsa_key = NULL;
897056
+    const EVP_MD * hashAlg = NULL;
897056
     const RAND_METHOD *rand_save = RAND_get_rand_method();
897056
 
897056
     RAND_set_rand_method(RAND_OpenSSL());
897056
 #else
897056
+    OSSL_LIB_CTX *libctx = NULL;
897056
+    EVP_MD * hashAlg = NULL;
897056
     OSSL_PARAM *params = NULL;
897056
     OSSL_PARAM_BLD *build = NULL;
897056
 #endif
897056
 
897056
     TSS2_RC r = TSS2_RC_SUCCESS;
897056
-    const EVP_MD * hashAlg = NULL;
897056
     EVP_PKEY *evp_rsa_key = NULL;
897056
     EVP_PKEY_CTX *genctx = NULL, *ctx = NULL;
897056
     BIGNUM *bne = NULL, *n = NULL;
897056
     int padding;
897056
     char *label_copy = NULL;
897056
 
897056
-    if (!(hashAlg = get_ossl_hash_md(pub_tpm_key->publicArea.nameAlg))) {
897056
-        LOG_ERROR("Unsupported hash algorithm (%"PRIu16")",
897056
-                  pub_tpm_key->publicArea.nameAlg);
897056
 #if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
+    if (!(hashAlg = get_ossl_hash_md(pub_tpm_key->publicArea.nameAlg))) {
897056
         RAND_set_rand_method(rand_save);
897056
+#else
897056
+    if (!(libctx = OSSL_LIB_CTX_new()))
897056
+        return TSS2_ESYS_RC_MEMORY;
897056
+
897056
+    if (!(hashAlg = EVP_MD_fetch(libctx,
897056
+            get_ossl_hash_md(pub_tpm_key->publicArea.nameAlg), NULL))) {
897056
+        OSSL_LIB_CTX_free(libctx);
897056
 #endif
897056
+        LOG_ERROR("Unsupported hash algorithm (%"PRIu16")",
897056
+                  pub_tpm_key->publicArea.nameAlg);
897056
         return TSS2_ESYS_RC_NOT_IMPLEMENTED;
897056
     }
897056
 
897056
@@ -673,7 +742,7 @@ iesys_cryptossl_pk_encrypt(TPM2B_PUBLIC * pub_tpm_key,
897056
                    cleanup);
897056
     }
897056
 
897056
-    if ((genctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL
897056
+    if ((genctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)) == NULL
897056
             || EVP_PKEY_fromdata_init(genctx) <= 0
897056
             || EVP_PKEY_fromdata(genctx, &evp_rsa_key, EVP_PKEY_PUBLIC_KEY, params) <= 0) {
897056
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Could not create rsa key.",
897056
@@ -744,6 +813,8 @@ iesys_cryptossl_pk_encrypt(TPM2B_PUBLIC * pub_tpm_key,
897056
 #else
897056
     OSSL_FREE(params, OSSL_PARAM);
897056
     OSSL_FREE(build, OSSL_PARAM_BLD);
897056
+    OSSL_FREE(hashAlg, EVP_MD);
897056
+    OSSL_FREE(libctx, OSSL_LIB_CTX);
897056
 #endif
897056
     return r;
897056
 }
897056
diff --git a/src/tss2-fapi/fapi_crypto.c b/src/tss2-fapi/fapi_crypto.c
897056
index 9c7e566c..d061cf48 100644
897056
--- a/src/tss2-fapi/fapi_crypto.c
897056
+++ b/src/tss2-fapi/fapi_crypto.c
897056
@@ -48,14 +48,34 @@
897056
 
897056
 /** Context to hold temporary values for ifapi_crypto */
897056
 typedef struct _IFAPI_CRYPTO_CONTEXT {
897056
-    /** The hash engine's context */
897056
-    EVP_MD_CTX *osslContext;
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
     /** The currently used hash algorithm */
897056
     const EVP_MD *osslHashAlgorithm;
897056
+#else
897056
+    OSSL_LIB_CTX *libctx;
897056
+    /** The currently used hash algorithm */
897056
+    EVP_MD *osslHashAlgorithm;
897056
+#endif
897056
+    /** The hash engine's context */
897056
+    EVP_MD_CTX *osslContext;
897056
     /** The size of the hash's digest */
897056
     size_t hashSize;
897056
 } IFAPI_CRYPTO_CONTEXT;
897056
 
897056
+static void
897056
+ifapi_crypto_context_free(IFAPI_CRYPTO_CONTEXT *ctx)
897056
+{
897056
+    if (!ctx)
897056
+        return;
897056
+
897056
+    EVP_MD_CTX_destroy(ctx->osslContext);
897056
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
897056
+    EVP_MD_free(ctx->osslHashAlgorithm);
897056
+    OSSL_LIB_CTX_free(ctx->libctx);
897056
+#endif
897056
+    SAFE_FREE(ctx);
897056
+}
897056
+
897056
 /**
897056
  * Returns the signature scheme that is currently used in the FAPI context.
897056
  *
897056
@@ -225,6 +245,33 @@ ifapi_bn2binpad(const BIGNUM *bn, unsigned char *bin, int binSize)
897056
     return 1;
897056
 }
897056
 
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
+/**
897056
+ * Converts a TSS hash algorithm identifier into an OpenSSL hash algorithm
897056
+ * identifier object.
897056
+ *
897056
+ * @param[in] hashAlgorithm The TSS hash algorithm identifier to convert
897056
+ *
897056
+ * @retval A suitable OpenSSL identifier object if one could be found
897056
+ * @retval NULL if no suitable identifier object could be found
897056
+ */
897056
+static const EVP_MD *
897056
+get_ossl_hash_md(TPM2_ALG_ID hashAlgorithm)
897056
+{
897056
+    switch (hashAlgorithm) {
897056
+    case TPM2_ALG_SHA1:
897056
+        return EVP_sha1();
897056
+    case TPM2_ALG_SHA256:
897056
+        return EVP_sha256();
897056
+    case TPM2_ALG_SHA384:
897056
+        return EVP_sha384();
897056
+    case TPM2_ALG_SHA512:
897056
+        return EVP_sha512();
897056
+    default:
897056
+        return NULL;
897056
+    }
897056
+}
897056
+#else
897056
 /**
897056
  * Returns a suitable openSSL hash algorithm identifier for a given TSS hash
897056
  * algorithm identifier.
897056
@@ -235,22 +282,23 @@ ifapi_bn2binpad(const BIGNUM *bn, unsigned char *bin, int binSize)
897056
  *         hashAlgorithm could be found
897056
  * @retval NULL if no suitable hash algorithm identifier could be found
897056
  */
897056
-static const EVP_MD *
897056
+static const char *
897056
 get_hash_md(TPM2_ALG_ID hashAlgorithm)
897056
 {
897056
     switch (hashAlgorithm) {
897056
     case TPM2_ALG_SHA1:
897056
-        return EVP_sha1();
897056
+        return "SHA1";
897056
     case TPM2_ALG_SHA256:
897056
-        return EVP_sha256();
897056
+        return "SHA256";
897056
     case TPM2_ALG_SHA384:
897056
-        return EVP_sha384();
897056
+        return "SHA384";
897056
     case TPM2_ALG_SHA512:
897056
-        return EVP_sha512();
897056
+        return "SHA512";
897056
     default:
897056
         return NULL;
897056
     }
897056
 }
897056
+#endif
897056
 
897056
 /**
897056
  * Returns a suitable openSSL RSA signature scheme identifiver for a given TSS
897056
@@ -1274,6 +1322,9 @@ ifapi_verify_signature_quote(
897056
     BIO *bufio = NULL;
897056
     EVP_PKEY_CTX *pctx = NULL;
897056
     EVP_MD_CTX *mdctx = NULL;
897056
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
897056
+    OSSL_LIB_CTX *libctx = NULL;
897056
+#endif
897056
 
897056
     /* Check whether or not the key is valid */
897056
     if (keyObject->objectType == IFAPI_KEY_OBJ) {
897056
@@ -1304,8 +1355,8 @@ ifapi_verify_signature_quote(
897056
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "EVP_MD_CTX_create",
897056
                    error_cleanup);
897056
     }
897056
-
897056
-    const EVP_MD *hashAlgorithm = get_hash_md(signatureScheme->details.any.hashAlg);
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
+    const EVP_MD *hashAlgorithm = get_ossl_hash_md(signatureScheme->details.any.hashAlg);
897056
     if (!hashAlgorithm) {
897056
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Invalid hash alg.",
897056
                    error_cleanup);
897056
@@ -1316,6 +1367,26 @@ ifapi_verify_signature_quote(
897056
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "EVP_DigestVerifyInit",
897056
                    error_cleanup);
897056
     }
897056
+#else
897056
+    const char *hashAlgorithm = get_hash_md(signatureScheme->details.any.hashAlg);
897056
+    if (!hashAlgorithm) {
897056
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Invalid hash alg.",
897056
+                   error_cleanup);
897056
+    }
897056
+
897056
+    /* The TPM2 provider may be loaded in the global library context.
897056
+     * As we don't want the TPM to be called for these operations, we have
897056
+     * to initialize own library context with the default provider. */
897056
+    libctx = OSSL_LIB_CTX_new();
897056
+    goto_if_null(libctx, "Out of memory", TSS2_FAPI_RC_MEMORY, error_cleanup);
897056
+
897056
+    /* Verify the digest of the signature */
897056
+    if (1 != EVP_DigestVerifyInit_ex(mdctx, &pctx, hashAlgorithm, libctx,
897056
+                                     NULL, publicKey, NULL)) {
897056
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "EVP_DigestVerifyInit_ex",
897056
+                   error_cleanup);
897056
+    }
897056
+#endif
897056
     goto_if_null(pctx, "Out of memory", TSS2_FAPI_RC_MEMORY, error_cleanup);
897056
     if (EVP_PKEY_type(EVP_PKEY_id(publicKey)) == EVP_PKEY_RSA) {
897056
         int padding = get_sig_scheme(signatureScheme->scheme);
897056
@@ -1339,12 +1410,13 @@ ifapi_verify_signature_quote(
897056
     }
897056
 
897056
 error_cleanup:
897056
-    if (mdctx != NULL) {
897056
-        EVP_MD_CTX_destroy(mdctx);
897056
-    }
897056
+    EVP_MD_CTX_destroy(mdctx);
897056
     SAFE_FREE(public_pem_key);
897056
     EVP_PKEY_free(publicKey);
897056
     BIO_free(bufio);
897056
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
897056
+    OSSL_LIB_CTX_free(libctx);
897056
+#endif
897056
     return r;
897056
 }
897056
 
897056
@@ -1464,36 +1536,6 @@ ifapi_hash_get_digest_size(TPM2_ALG_ID hashAlgorithm)
897056
     }
897056
 }
897056
 
897056
-/**
897056
- * Converts a TSS hash algorithm identifier into an OpenSSL hash algorithm
897056
- * identifier object.
897056
- *
897056
- * @param[in] hashAlgorithm The TSS hash algorithm identifier to convert
897056
- *
897056
- * @retval A suitable OpenSSL identifier object if one could be found
897056
- * @retval NULL if no suitable identifier object could be found
897056
- */
897056
-static const EVP_MD *
897056
-get_ossl_hash_md(TPM2_ALG_ID hashAlgorithm)
897056
-{
897056
-    switch (hashAlgorithm) {
897056
-    case TPM2_ALG_SHA1:
897056
-        return EVP_sha1();
897056
-        break;
897056
-    case TPM2_ALG_SHA256:
897056
-        return EVP_sha256();
897056
-        break;
897056
-    case TPM2_ALG_SHA384:
897056
-        return EVP_sha384();
897056
-        break;
897056
-    case TPM2_ALG_SHA512:
897056
-        return EVP_sha512();
897056
-        break;
897056
-    default:
897056
-        return NULL;
897056
-    }
897056
-}
897056
-
897056
 /**
897056
  * Starts the computation of a hash digest.
897056
  *
897056
@@ -1520,11 +1562,26 @@ ifapi_crypto_hash_start(IFAPI_CRYPTO_CONTEXT_BLOB **context,
897056
     mycontext = calloc(1, sizeof(IFAPI_CRYPTO_CONTEXT));
897056
     return_if_null(mycontext, "Out of memory", TSS2_FAPI_RC_MEMORY);
897056
 
897056
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
897056
     if (!(mycontext->osslHashAlgorithm = get_ossl_hash_md(hashAlgorithm))) {
897056
         goto_error(r, TSS2_FAPI_RC_BAD_VALUE,
897056
                    "Unsupported hash algorithm (%" PRIu16 ")", cleanup,
897056
                    hashAlgorithm);
897056
     }
897056
+#else
897056
+    /* The TPM2 provider may be loaded in the global library context.
897056
+     * As we don't want the TPM to be called for these operations, we have
897056
+     * to initialize own library context with the default provider. */
897056
+    mycontext->libctx = OSSL_LIB_CTX_new();
897056
+    return_if_null(mycontext->libctx, "Out of memory", TSS2_FAPI_RC_MEMORY);
897056
+
897056
+    if (!(mycontext->osslHashAlgorithm =
897056
+            EVP_MD_fetch(mycontext->libctx, get_hash_md(hashAlgorithm), NULL))) {
897056
+        goto_error(r, TSS2_FAPI_RC_BAD_VALUE,
897056
+                   "Unsupported hash algorithm (%" PRIu16 ")", cleanup,
897056
+                   hashAlgorithm);
897056
+    }
897056
+#endif
897056
 
897056
     if (!(mycontext->hashSize = ifapi_hash_get_digest_size(hashAlgorithm))) {
897056
         goto_error(r, TSS2_FAPI_RC_BAD_VALUE,
897056
@@ -1548,10 +1605,7 @@ ifapi_crypto_hash_start(IFAPI_CRYPTO_CONTEXT_BLOB **context,
897056
     return TSS2_RC_SUCCESS;
897056
 
897056
 cleanup:
897056
-    if (mycontext->osslContext)
897056
-        EVP_MD_CTX_destroy(mycontext->osslContext);
897056
-    SAFE_FREE(mycontext);
897056
-
897056
+    ifapi_crypto_context_free(mycontext);
897056
     return r;
897056
 }
897056
 
897056
@@ -1630,8 +1684,7 @@ ifapi_crypto_hash_finish(IFAPI_CRYPTO_CONTEXT_BLOB **context,
897056
     }
897056
 
897056
     /* Finalize the hash context */
897056
-    EVP_MD_CTX_destroy(mycontext->osslContext);
897056
-    free(mycontext);
897056
+    ifapi_crypto_context_free(mycontext);
897056
     *context = NULL;
897056
 
897056
     return TSS2_RC_SUCCESS;
897056
@@ -1653,8 +1706,7 @@ ifapi_crypto_hash_abort(IFAPI_CRYPTO_CONTEXT_BLOB **context)
897056
     }
897056
     IFAPI_CRYPTO_CONTEXT *mycontext = (IFAPI_CRYPTO_CONTEXT *) * context;
897056
 
897056
-    EVP_MD_CTX_destroy(mycontext->osslContext);
897056
-    free(mycontext);
897056
+    ifapi_crypto_context_free(mycontext);
897056
     *context = NULL;
897056
 }
897056
 
897056
-- 
897056
2.34.3
897056