chantra / rpms / tpm2-tss

Forked from rpms/tpm2-tss 2 years ago
Clone

Blame SOURCES/0012-Implement-EVP_PKEY-export-import-for-OpenSSL-3.0.patch

a23473
From f328c68cba9d5511b7d2d2615b3a28987edbdfac Mon Sep 17 00:00:00 2001
a23473
From: Petr Gotthard <petr.gotthard@centrum.cz>
a23473
Date: Sun, 18 Jul 2021 21:30:59 +0200
a23473
Subject: Implement EVP_PKEY export/import for OpenSSL 3.0
a23473
MIME-Version: 1.0
a23473
Content-Type: text/plain; charset=UTF-8
a23473
Content-Transfer-Encoding: 8bit
a23473
a23473
The `RSA_KEY` and `EC_KEY` are not publicly available in OpenSSL 3.0 and
a23473
the generic `EVP_PKEY` must be used instead.
a23473
Since export/import of raw keys still requires access to the internal structures
a23473
the OpenSSL 3.0 introduced a completely new approach to access key internals.
a23473
a23473
This PR:
a23473
 - preserves the current export/import impementation for OpenSSL 1.1.x
a23473
 - implements key export/import for OpenSSL 3.0.0
a23473
a23473
Signed-off-by: Petr Gotthard <petr.gotthard@centrum.cz>
a23473
---
a23473
 src/tss2-esys/esys_crypto_ossl.c | 154 ++++++++++++-----
a23473
 src/tss2-fapi/fapi_crypto.c      | 275 +++++++++++++++++++++----------
a23473
 test/helper/tpm_getek.c          |  53 +++---
a23473
 test/helper/tpm_getek_ecc.c      |  61 +++++--
a23473
 4 files changed, 386 insertions(+), 157 deletions(-)
a23473
a23473
diff --git a/src/tss2-esys/esys_crypto_ossl.c b/src/tss2-esys/esys_crypto_ossl.c
a23473
index a6259346..392f97ae 100644
a23473
--- a/src/tss2-esys/esys_crypto_ossl.c
a23473
+++ b/src/tss2-esys/esys_crypto_ossl.c
a23473
@@ -8,9 +8,17 @@
a23473
 #include <config.h>
a23473
 #endif
a23473
 
a23473
+#include <openssl/rand.h>
a23473
 #include <openssl/evp.h>
a23473
-#include <openssl/aes.h>
a23473
 #include <openssl/rsa.h>
a23473
+#include <openssl/ec.h>
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+#include <openssl/aes.h>
a23473
+#else
a23473
+#include <openssl/core_names.h>
a23473
+#include <openssl/params.h>
a23473
+#include <openssl/param_build.h>
a23473
+#endif
a23473
 #include <openssl/engine.h>
a23473
 #include <stdio.h>
a23473
 
a23473
@@ -324,9 +332,14 @@ iesys_cryptossl_hmac_start(IESYS_CRYPTO_CONTEXT_BLOB ** context,
a23473
                    "Error EVP_MD_CTX_create", cleanup);
a23473
     }
a23473
 
a23473
+#if OPENSSL_VERSION_NUMBER < 0x10101000L
a23473
     if (!(hkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, size))) {
a23473
+#else
a23473
+    /* this is preferred, but available since OpenSSL 1.1.1 only */
a23473
+    if (!(hkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, key, size))) {
a23473
+#endif
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "EVP_PKEY_new_mac_key", cleanup);
a23473
+                   "Failed to create HMAC key", cleanup);
a23473
     }
a23473
 
a23473
     if(1 != EVP_DigestSignInit(mycontext->hmac.ossl_context, NULL,
a23473
@@ -517,7 +530,10 @@ iesys_cryptossl_hmac_abort(IESYS_CRYPTO_CONTEXT_BLOB ** context)
a23473
 TSS2_RC
a23473
 iesys_cryptossl_random2b(TPM2B_NONCE * nonce, size_t num_bytes)
a23473
 {
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
     const RAND_METHOD *rand_save = RAND_get_rand_method();
a23473
+    RAND_set_rand_method(RAND_OpenSSL());
a23473
+#endif
a23473
 
a23473
     if (num_bytes == 0) {
a23473
         nonce->size = sizeof(TPMU_HA);
a23473
@@ -525,13 +541,16 @@ iesys_cryptossl_random2b(TPM2B_NONCE * nonce, size_t num_bytes)
a23473
         nonce->size = num_bytes;
a23473
     }
a23473
 
a23473
-    RAND_set_rand_method(RAND_OpenSSL());
a23473
     if (1 != RAND_bytes(&nonce->buffer[0], nonce->size)) {
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
         RAND_set_rand_method(rand_save);
a23473
+#endif
a23473
         return_error(TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
                      "Failure in random number generator.");
a23473
     }
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
     RAND_set_rand_method(rand_save);
a23473
+#endif
a23473
     return TSS2_RC_SUCCESS;
a23473
 }
a23473
 
a23473
@@ -558,30 +577,33 @@ iesys_cryptossl_pk_encrypt(TPM2B_PUBLIC * pub_tpm_key,
a23473
                            BYTE * out_buffer,
a23473
                            size_t * out_size, const char *label)
a23473
 {
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    RSA *rsa_key = NULL;
a23473
     const RAND_METHOD *rand_save = RAND_get_rand_method();
a23473
+
a23473
     RAND_set_rand_method(RAND_OpenSSL());
a23473
+#else
a23473
+    OSSL_PARAM *params = NULL;
a23473
+    OSSL_PARAM_BLD *build = NULL;
a23473
+#endif
a23473
 
a23473
     TSS2_RC r = TSS2_RC_SUCCESS;
a23473
     const EVP_MD * hashAlg = NULL;
a23473
-    RSA * rsa_key = NULL;
a23473
     EVP_PKEY *evp_rsa_key = NULL;
a23473
-    EVP_PKEY_CTX *ctx = NULL;
a23473
-    BIGNUM* bne = NULL;
a23473
+    EVP_PKEY_CTX *genctx = NULL, *ctx = NULL;
a23473
+    BIGNUM *bne = NULL, *n = NULL;
a23473
     int padding;
a23473
     char *label_copy = NULL;
a23473
 
a23473
     if (!(hashAlg = get_ossl_hash_md(pub_tpm_key->publicArea.nameAlg))) {
a23473
         LOG_ERROR("Unsupported hash algorithm (%"PRIu16")",
a23473
                   pub_tpm_key->publicArea.nameAlg);
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
         RAND_set_rand_method(rand_save);
a23473
+#endif
a23473
         return TSS2_ESYS_RC_NOT_IMPLEMENTED;
a23473
     }
a23473
 
a23473
-    if (!(bne = BN_new())) {
a23473
-        goto_error(r, TSS2_ESYS_RC_MEMORY,
a23473
-                   "Could not allocate Big Number", cleanup);
a23473
-    }
a23473
-
a23473
     switch (pub_tpm_key->publicArea.parameters.rsaDetail.scheme.scheme) {
a23473
     case TPM2_ALG_NULL:
a23473
         padding = RSA_NO_PADDING;
a23473
@@ -601,44 +623,64 @@ iesys_cryptossl_pk_encrypt(TPM2B_PUBLIC * pub_tpm_key,
a23473
         exp = 65537;
a23473
     else
a23473
         exp = pub_tpm_key->publicArea.parameters.rsaDetail.exponent;
a23473
-    if (1 != BN_set_word(bne, exp)) {
a23473
+
a23473
+    if (!(n = BN_bin2bn(pub_tpm_key->publicArea.unique.rsa.buffer,
a23473
+                        pub_tpm_key->publicArea.unique.rsa.size,
a23473
+                        NULL))) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "Could not set exponent.", cleanup);
a23473
+                   "Could not create rsa n.", cleanup);
a23473
     }
a23473
 
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
     if (!(rsa_key = RSA_new())) {
a23473
         goto_error(r, TSS2_ESYS_RC_MEMORY,
a23473
                    "Could not allocate RSA key", cleanup);
a23473
     }
a23473
 
a23473
-    if (1 != RSA_generate_key_ex(rsa_key,
a23473
-                                 pub_tpm_key->publicArea.parameters.rsaDetail.keyBits,
a23473
-                                 bne, NULL)) {
a23473
-        goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Could not generate RSA key",
a23473
-                   cleanup);
a23473
+    if (!(bne = BN_new())) {
a23473
+        goto_error(r, TSS2_ESYS_RC_MEMORY,
a23473
+                   "Could not allocate Big Number", cleanup);
a23473
     }
a23473
-
a23473
-    if (!(evp_rsa_key = EVP_PKEY_new())) {
a23473
+    if (1 != BN_set_word(bne, exp)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "Could not create evp key.", cleanup);
a23473
+                   "Could not set exponent.", cleanup);
a23473
     }
a23473
-    BIGNUM *n = NULL;
a23473
-    if (!(n = BN_bin2bn(pub_tpm_key->publicArea.unique.rsa.buffer,
a23473
-                        pub_tpm_key->publicArea.unique.rsa.size,
a23473
-                        NULL))) {
a23473
+
a23473
+    if (1 != RSA_set0_key(rsa_key, n, bne, NULL)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "Could not create rsa n.", cleanup);
a23473
+                   "Could not set rsa n.", cleanup);
a23473
     }
a23473
+    /* ownership got transferred */
a23473
+    n = NULL;
a23473
+    bne = NULL;
a23473
 
a23473
-    if (1 != RSA_set0_key(rsa_key, n, NULL, NULL)) {
a23473
+    if (!(evp_rsa_key = EVP_PKEY_new())) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "Could not set rsa n.", cleanup);
a23473
+                   "Could not create evp key.", cleanup);
a23473
     }
a23473
 
a23473
-    if (1 != EVP_PKEY_set1_RSA(evp_rsa_key, rsa_key)) {
a23473
+    if (1 != EVP_PKEY_assign_RSA(evp_rsa_key, rsa_key)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
                    "Could not set rsa key.", cleanup);
a23473
     }
a23473
+    /* ownership got transferred */
a23473
+    rsa_key = NULL;
a23473
+#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
a23473
+    if ((build = OSSL_PARAM_BLD_new()) == NULL
a23473
+            || !OSSL_PARAM_BLD_push_BN(build, OSSL_PKEY_PARAM_RSA_N, n)
a23473
+            || !OSSL_PARAM_BLD_push_uint32(build, OSSL_PKEY_PARAM_RSA_E, exp)
a23473
+            || (params = OSSL_PARAM_BLD_to_param(build)) == NULL) {
a23473
+        goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Could not create rsa parameters.",
a23473
+                   cleanup);
a23473
+    }
a23473
+
a23473
+    if ((genctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL
a23473
+            || EVP_PKEY_fromdata_init(genctx) <= 0
a23473
+            || EVP_PKEY_fromdata(genctx, &evp_rsa_key, EVP_PKEY_PUBLIC_KEY, params) <= 0) {
a23473
+        goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Could not create rsa key.",
a23473
+                   cleanup);
a23473
+    }
a23473
+#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
a23473
 
a23473
     if (!(ctx = EVP_PKEY_CTX_new(evp_rsa_key, NULL))) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
@@ -692,11 +734,18 @@ iesys_cryptossl_pk_encrypt(TPM2B_PUBLIC * pub_tpm_key,
a23473
     r = TSS2_RC_SUCCESS;
a23473
 
a23473
  cleanup:
a23473
+    OSSL_FREE(genctx, EVP_PKEY_CTX);
a23473
     OSSL_FREE(ctx, EVP_PKEY_CTX);
a23473
     OSSL_FREE(evp_rsa_key, EVP_PKEY);
a23473
-    OSSL_FREE(rsa_key, RSA);
a23473
     OSSL_FREE(bne, BN);
a23473
+    OSSL_FREE(n, BN);
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    OSSL_FREE(rsa_key, RSA);
a23473
     RAND_set_rand_method(rand_save);
a23473
+#else
a23473
+    OSSL_FREE(params, OSSL_PARAM);
a23473
+    OSSL_FREE(build, OSSL_PARAM_BLD);
a23473
+#endif
a23473
     return r;
a23473
 }
a23473
 
a23473
@@ -784,8 +833,14 @@ iesys_cryptossl_get_ecdh_point(TPM2B_PUBLIC *key,
a23473
 {
a23473
     TSS2_RC r = TSS2_RC_SUCCESS;
a23473
     EC_GROUP *group = NULL;               /* Group defines the used curve */
a23473
-    EC_KEY *eph_ec_key = NULL;            /* Ephemeral ec key of application */
a23473
+    EVP_PKEY_CTX *ctx = NULL;
a23473
+    EVP_PKEY *eph_pkey = NULL;
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
     const EC_POINT *eph_pub_key = NULL;   /* Public part of ephemeral key */
a23473
+    const BIGNUM *eph_priv_key = NULL;
a23473
+#else
a23473
+    BIGNUM *eph_priv_key = NULL;
a23473
+#endif
a23473
     EC_POINT *tpm_pub_key = NULL;         /* Public part of TPM key */
a23473
     EC_POINT *mul_eph_tpm = NULL;
a23473
     BIGNUM *bn_x = NULL;
a23473
@@ -827,23 +882,25 @@ iesys_cryptossl_get_ecdh_point(TPM2B_PUBLIC *key,
a23473
     }
a23473
 
a23473
     /* Create ephemeral key */
a23473
-    if (!(eph_ec_key = EC_KEY_new())) {
a23473
+    if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
a23473
+            || EVP_PKEY_keygen_init(ctx) <= 0) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "Create ec key", cleanup);
a23473
+                   "Initialize ec key generation", cleanup);
a23473
     }
a23473
-    if (1 !=   EC_KEY_set_group(eph_ec_key , group)) {
a23473
 
a23473
-        goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Set group", cleanup);
a23473
-    }
a23473
-
a23473
-    if (1 != EC_KEY_generate_key(eph_ec_key)) {
a23473
+    if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, curveId) <= 0
a23473
+            || EVP_PKEY_keygen(ctx, &eph_pkey) <= 0) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Generate ec key", cleanup);
a23473
     }
a23473
 
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    EC_KEY *eph_ec_key = EVP_PKEY_get0_EC_KEY(eph_pkey);
a23473
+
a23473
     if (!(eph_pub_key =  EC_KEY_get0_public_key(eph_ec_key))) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Get public key", cleanup);
a23473
     }
a23473
 
a23473
+    eph_priv_key = EC_KEY_get0_private_key(eph_ec_key);
a23473
     if (1 != EC_POINT_is_on_curve(group, eph_pub_key, NULL)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
                    "Ephemeral public key is on curve",cleanup);
a23473
@@ -861,8 +918,16 @@ iesys_cryptossl_get_ecdh_point(TPM2B_PUBLIC *key,
a23473
     if (1 != EC_POINT_get_affine_coordinates_tss(group, eph_pub_key, bn_x,
a23473
                                                  bn_y, NULL)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
-                   "Get affine x coordinate", cleanup);
a23473
+                   "Get affine coordinates", cleanup);
a23473
+    }
a23473
+#else
a23473
+    if (!EVP_PKEY_get_bn_param(eph_pkey, OSSL_PKEY_PARAM_PRIV_KEY, &eph_priv_key)
a23473
+            || !EVP_PKEY_get_bn_param(eph_pkey, OSSL_PKEY_PARAM_EC_PUB_X, &bn_x)
a23473
+            || !EVP_PKEY_get_bn_param(eph_pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &bn_y)) {
a23473
+        goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
+                   "Get ephemeral key", cleanup);
a23473
     }
a23473
+#endif
a23473
 
a23473
     if (1 != iesys_bn2binpad(bn_x, &Q->x.buffer[0], key_size)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
@@ -881,13 +946,11 @@ iesys_cryptossl_get_ecdh_point(TPM2B_PUBLIC *key,
a23473
     r = tpm_pub_to_ossl_pub(group, key, &tpm_pub_key);
a23473
     goto_if_error(r, "Convert TPM pub point to ossl pub point", cleanup);
a23473
 
a23473
-    /* Multiply the ephemeral private key with TPM public key */
a23473
-    const BIGNUM * eph_priv_key = EC_KEY_get0_private_key(eph_ec_key);
a23473
-
a23473
     if (!(mul_eph_tpm = EC_POINT_new(group))) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE, "Create point.", cleanup);
a23473
     }
a23473
 
a23473
+    /* Multiply the ephemeral private key with TPM public key */
a23473
     if (1 != EC_POINT_mul(group, mul_eph_tpm, NULL,
a23473
                           tpm_pub_key, eph_priv_key, NULL)) {
a23473
         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
@@ -918,8 +981,13 @@ iesys_cryptossl_get_ecdh_point(TPM2B_PUBLIC *key,
a23473
     OSSL_FREE(mul_eph_tpm, EC_POINT);
a23473
     OSSL_FREE(tpm_pub_key, EC_POINT);
a23473
     OSSL_FREE(group,EC_GROUP);
a23473
-    OSSL_FREE(eph_ec_key, EC_KEY);
a23473
+    OSSL_FREE(ctx, EVP_PKEY_CTX);
a23473
+    OSSL_FREE(eph_pkey, EVP_PKEY);
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
     /* Note: free of eph_pub_key already done by free of eph_ec_key */
a23473
+#else
a23473
+    OSSL_FREE(eph_priv_key, BN);
a23473
+#endif
a23473
     OSSL_FREE(bn_x, BN);
a23473
     OSSL_FREE(bn_y, BN);
a23473
     return r;
a23473
diff --git a/src/tss2-fapi/fapi_crypto.c b/src/tss2-fapi/fapi_crypto.c
a23473
index c97b0a1d..c50b5f0a 100644
a23473
--- a/src/tss2-fapi/fapi_crypto.c
a23473
+++ b/src/tss2-fapi/fapi_crypto.c
a23473
@@ -11,10 +11,15 @@
a23473
 #include <string.h>
a23473
 
a23473
 #include <openssl/evp.h>
a23473
-#include <openssl/aes.h>
a23473
 #include <openssl/rsa.h>
a23473
-#include <openssl/engine.h>
a23473
 #include <openssl/pem.h>
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+#include <openssl/aes.h>
a23473
+#else
a23473
+#include <openssl/core_names.h>
a23473
+#include <openssl/params.h>
a23473
+#include <openssl/param_build.h>
a23473
+#endif
a23473
 #include <openssl/x509v3.h>
a23473
 #include <curl/curl.h>
a23473
 #include <openssl/err.h>
a23473
@@ -380,66 +385,89 @@ cleanup:
a23473
  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
a23473
  */
a23473
 static TSS2_RC
a23473
-ossl_rsa_pub_from_tpm(const TPM2B_PUBLIC *tpmPublicKey, EVP_PKEY *evpPublicKey)
a23473
+ossl_rsa_pub_from_tpm(const TPM2B_PUBLIC *tpmPublicKey, EVP_PKEY **evpPublicKey)
a23473
 {
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    RSA *rsa = NULL;
a23473
+#else
a23473
+    OSSL_PARAM_BLD *build = NULL;
a23473
+    OSSL_PARAM *params = NULL;
a23473
+    EVP_PKEY_CTX *ctx = NULL;
a23473
+#endif
a23473
+
a23473
     /* Check for NULL parameters */
a23473
     return_if_null(tpmPublicKey, "tpmPublicKey is NULL", TSS2_FAPI_RC_BAD_REFERENCE);
a23473
     return_if_null(evpPublicKey, "evpPublicKey is NULL", TSS2_FAPI_RC_BAD_REFERENCE);
a23473
 
a23473
+    TSS2_RC r = TSS2_RC_SUCCESS;
a23473
     /* Initialize the RSA parameters */
a23473
-    TSS2_RC r;
a23473
-    RSA *rsa = RSA_new();
a23473
-    BIGNUM *e = BN_new();
a23473
-    BIGNUM *d = BN_new();
a23473
-    BIGNUM *p = BN_new();
a23473
-    BIGNUM *q = BN_new();
a23473
-    BIGNUM *dmp1 = BN_new();
a23473
-    BIGNUM *dmq1 = BN_new();
a23473
-    BIGNUM *iqmp = BN_new();
a23473
+    BIGNUM *e = NULL;
a23473
     BIGNUM *n = BN_bin2bn(tpmPublicKey->publicArea.unique.rsa.buffer,
a23473
                           tpmPublicKey->publicArea.unique.rsa.size, NULL);
a23473
-
a23473
-    if (!n || !e || !d || !p || !q || !dmp1 || !dmq1 || !iqmp || !rsa) {
a23473
+    if (!n) {
a23473
         goto_error(r, TSS2_FAPI_RC_MEMORY, "Out of memory", error_cleanup);
a23473
     }
a23473
 
a23473
-    BN_set_word(d, 0);
a23473
-    BN_set_word(p, 0);
a23473
-    BN_set_word(q, 0);
a23473
-    BN_set_word(dmp1, 0);
a23473
-    BN_set_word(dmq1, 0);
a23473
-    BN_set_word(iqmp, 0);
a23473
     uint32_t exp;
a23473
     if (tpmPublicKey->publicArea.parameters.rsaDetail.exponent == 0)
a23473
         exp = 65537;
a23473
     else
a23473
         exp = tpmPublicKey->publicArea.parameters.rsaDetail.exponent;
a23473
+
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    if ((rsa = RSA_new()) == NULL) {
a23473
+        goto_error(r, TSS2_FAPI_RC_MEMORY, "Out of memory", error_cleanup);
a23473
+    }
a23473
+
a23473
+    if ((e = BN_new()) == NULL) {
a23473
+        goto_error(r, TSS2_FAPI_RC_MEMORY, "Out of memory", error_cleanup);
a23473
+    }
a23473
     if (1 != BN_set_word(e, exp)) {
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
                    "Could not set exponent.", error_cleanup);
a23473
     }
a23473
 
a23473
-    RSA_set0_key(rsa, n, e, d);
a23473
-    RSA_set0_factors(rsa, p, q);
a23473
-    RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
a23473
+    if (!RSA_set0_key(rsa, n, e, NULL)) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
+                   "Could not set public key.", error_cleanup);
a23473
+    }
a23473
+    n = NULL; /* ownership transferred */
a23473
+    e = NULL;
a23473
+
a23473
+    *evpPublicKey = EVP_PKEY_new();
a23473
+    goto_if_null2(*evpPublicKey, "Out of memory.", r, TSS2_FAPI_RC_MEMORY, error_cleanup);
a23473
 
a23473
     /* Assign the parameters to the key */
a23473
-    if (!EVP_PKEY_assign_RSA(evpPublicKey, rsa)) {
a23473
+    if (!EVP_PKEY_assign_RSA(*evpPublicKey, rsa)) {
a23473
+        EVP_PKEY_free(*evpPublicKey);
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Assign rsa key",
a23473
                    error_cleanup);
a23473
     }
a23473
-    return TSS2_RC_SUCCESS;
a23473
-
a23473
+    rsa = NULL; /* ownership transferred */
a23473
 error_cleanup:
a23473
     OSSL_FREE(rsa, RSA);
a23473
+#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
a23473
+    if ((build = OSSL_PARAM_BLD_new()) == NULL
a23473
+            || !OSSL_PARAM_BLD_push_BN(build, OSSL_PKEY_PARAM_RSA_N, n)
a23473
+            || !OSSL_PARAM_BLD_push_uint32(build, OSSL_PKEY_PARAM_RSA_E, exp)
a23473
+            || (params = OSSL_PARAM_BLD_to_param(build)) == NULL) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Create rsa key parameters",
a23473
+                   error_cleanup);
a23473
+    }
a23473
+
a23473
+    if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL
a23473
+            || EVP_PKEY_fromdata_init(ctx) <= 0
a23473
+            || EVP_PKEY_fromdata(ctx, evpPublicKey, EVP_PKEY_PUBLIC_KEY, params) <= 0) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Create rsa key",
a23473
+                   error_cleanup);
a23473
+    }
a23473
+error_cleanup:
a23473
+    OSSL_FREE(ctx, EVP_PKEY_CTX);
a23473
+    OSSL_FREE(params, OSSL_PARAM);
a23473
+    OSSL_FREE(build, OSSL_PARAM_BLD);
a23473
+#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
a23473
     OSSL_FREE(e, BN);
a23473
     OSSL_FREE(n, BN);
a23473
-    OSSL_FREE(d, BN);
a23473
-    OSSL_FREE(p, BN);
a23473
-    OSSL_FREE(q, BN);
a23473
-    OSSL_FREE(dmp1, BN);
a23473
-    OSSL_FREE(dmq1, BN);
a23473
-    OSSL_FREE(iqmp, BN);
a23473
     return r;
a23473
 }
a23473
 
a23473
@@ -459,18 +487,26 @@ error_cleanup:
a23473
  *         the function.
a23473
  */
a23473
 static TSS2_RC
a23473
-ossl_ecc_pub_from_tpm(const TPM2B_PUBLIC *tpmPublicKey, EVP_PKEY *evpPublicKey)
a23473
+ossl_ecc_pub_from_tpm(const TPM2B_PUBLIC *tpmPublicKey, EVP_PKEY **evpPublicKey)
a23473
 {
a23473
     /* Check for NULL parameters */
a23473
     return_if_null(tpmPublicKey, "tpmPublicKey is NULL", TSS2_FAPI_RC_BAD_REFERENCE);
a23473
     return_if_null(evpPublicKey, "evpPublicKey is NULL", TSS2_FAPI_RC_BAD_REFERENCE);
a23473
 
a23473
-    TSS2_RC r;
a23473
+    TSS2_RC r = TSS2_RC_SUCCESS;
a23473
     EC_GROUP *ecgroup = NULL;
a23473
     int curveId;
a23473
     BIGNUM *x = NULL, *y = NULL;
a23473
-    EC_KEY *ecKey = EC_KEY_new();
a23473
-    return_if_null(ecKey, "Out of memory.", TSS2_FAPI_RC_MEMORY);
a23473
+    EC_POINT *ecPoint = NULL;
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    EC_KEY *ecKey = NULL;
a23473
+#else
a23473
+    OSSL_PARAM_BLD *build = NULL;
a23473
+    OSSL_PARAM *params = NULL;
a23473
+    EVP_PKEY_CTX *ctx = NULL;
a23473
+    unsigned char *puboct = NULL;
a23473
+    size_t bsize;
a23473
+#endif
a23473
 
a23473
     /* Find the curve of the ECC key */
a23473
     switch (tpmPublicKey->publicArea.parameters.eccDetail.curveID) {
a23473
@@ -499,12 +535,6 @@ ossl_ecc_pub_from_tpm(const TPM2B_PUBLIC *tpmPublicKey, EVP_PKEY *evpPublicKey)
a23473
     goto_if_null(ecgroup, "new EC group.", TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
                   error_cleanup);
a23473
 
a23473
-    if (!EC_KEY_set_group(ecKey, ecgroup)) {
a23473
-        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "EC_KEY_set_group",
a23473
-                   error_cleanup);
a23473
-    }
a23473
-    EC_GROUP_free(ecgroup);
a23473
-
a23473
     /* Set the ECC parameters in the OpenSSL key */
a23473
     x = BN_bin2bn(tpmPublicKey->publicArea.unique.ecc.x.buffer,
a23473
                   tpmPublicKey->publicArea.unique.ecc.x.size, NULL);
a23473
@@ -516,23 +546,67 @@ ossl_ecc_pub_from_tpm(const TPM2B_PUBLIC *tpmPublicKey, EVP_PKEY *evpPublicKey)
a23473
         goto_error(r, TSS2_FAPI_RC_MEMORY, "Out of memory", error_cleanup);
a23473
     }
a23473
 
a23473
-    if (!EC_KEY_set_public_key_affine_coordinates(ecKey, x, y)) {
a23473
+    if ((ecPoint = EC_POINT_new(ecgroup)) == NULL
a23473
+            || !EC_POINT_set_affine_coordinates_tss(ecgroup, ecPoint, x, y, NULL)) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "EC_POINT_set_affine_coordinates",
a23473
+                   error_cleanup);
a23473
+    }
a23473
+
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+    ecKey = EC_KEY_new();
a23473
+    return_if_null(ecKey, "Out of memory.", TSS2_FAPI_RC_MEMORY);
a23473
+
a23473
+    if (!EC_KEY_set_group(ecKey, ecgroup)) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "EC_KEY_set_group",
a23473
+                   error_cleanup);
a23473
+    }
a23473
+
a23473
+    if (!EC_KEY_set_public_key(ecKey, ecPoint)) {
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
-                   "EC_KEY_set_public_key_affine_coordinates", error_cleanup);
a23473
+                   "EC_KEY_set_public_key", error_cleanup);
a23473
     }
a23473
 
a23473
-    if (!EVP_PKEY_assign_EC_KEY(evpPublicKey, ecKey)) {
a23473
+    *evpPublicKey = EVP_PKEY_new();
a23473
+    goto_if_null2(*evpPublicKey, "Out of memory.", r, TSS2_FAPI_RC_MEMORY, error_cleanup);
a23473
+
a23473
+    if (!EVP_PKEY_assign_EC_KEY(*evpPublicKey, ecKey)) {
a23473
+        EVP_PKEY_free(*evpPublicKey);
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Assign ecc key",
a23473
                    error_cleanup);
a23473
     }
a23473
-    OSSL_FREE(y, BN);
a23473
-    OSSL_FREE(x, BN);
a23473
-    return TSS2_RC_SUCCESS;
a23473
+    ecKey = NULL; /* ownership transferred */
a23473
+error_cleanup:
a23473
+    OSSL_FREE(ecKey, EC_KEY);
a23473
+#else
a23473
+    if ((build = OSSL_PARAM_BLD_new()) == NULL
a23473
+            || !OSSL_PARAM_BLD_push_utf8_string(build, OSSL_PKEY_PARAM_GROUP_NAME,
a23473
+                                                (char *)OBJ_nid2sn(curveId), 0)
a23473
+            || (bsize = EC_POINT_point2buf(ecgroup, ecPoint,
a23473
+                                           POINT_CONVERSION_COMPRESSED,
a23473
+                                           &puboct, NULL)) == 0
a23473
+            || !OSSL_PARAM_BLD_push_octet_string(build, OSSL_PKEY_PARAM_PUB_KEY,
a23473
+                                                 puboct, bsize)
a23473
+            || (params = OSSL_PARAM_BLD_to_param(build)) == NULL) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Create ecc key parameters",
a23473
+                   error_cleanup);
a23473
+    }
a23473
 
a23473
+    if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL
a23473
+            || EVP_PKEY_fromdata_init(ctx) <= 0
a23473
+            || EVP_PKEY_fromdata(ctx, evpPublicKey, EVP_PKEY_PUBLIC_KEY, params) <= 0) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Create ecc key",
a23473
+                   error_cleanup);
a23473
+    }
a23473
 error_cleanup:
a23473
+    EVP_PKEY_CTX_free(ctx);
a23473
+    OSSL_PARAM_free(params);
a23473
+    OSSL_PARAM_BLD_free(build);
a23473
+    OPENSSL_free(puboct);
a23473
+#endif
a23473
+    OSSL_FREE(ecPoint, EC_POINT);
a23473
+    OSSL_FREE(ecgroup, EC_GROUP);
a23473
     OSSL_FREE(y, BN);
a23473
     OSSL_FREE(x, BN);
a23473
-    OSSL_FREE(ecKey, EC_KEY);
a23473
     return r;
a23473
 }
a23473
 
a23473
@@ -567,18 +641,15 @@ ifapi_pub_pem_key_from_tpm(
a23473
     BIO *bio = NULL;
a23473
     TSS2_RC r = TPM2_RC_SUCCESS;
a23473
 
a23473
-    evpPublicKey = EVP_PKEY_new();
a23473
-    goto_if_null2(evpPublicKey, "Out of memory.", r, TSS2_FAPI_RC_MEMORY, cleanup);
a23473
-
a23473
     /* Memory IO will be used for OSSL key conversion */
a23473
     bio = BIO_new(BIO_s_mem());
a23473
-    goto_if_null2(evpPublicKey, "Out of memory.", r, TSS2_FAPI_RC_MEMORY, cleanup);
a23473
+    goto_if_null2(bio, "Out of memory.", r, TSS2_FAPI_RC_MEMORY, cleanup);
a23473
 
a23473
     if (tpmPublicKey->publicArea.type == TPM2_ALG_RSA) {
a23473
-        r = ossl_rsa_pub_from_tpm(tpmPublicKey, evpPublicKey);
a23473
-    } else if (tpmPublicKey->publicArea.type == TPM2_ALG_ECC)
a23473
-        r = ossl_ecc_pub_from_tpm(tpmPublicKey, evpPublicKey);
a23473
-    else {
a23473
+        r = ossl_rsa_pub_from_tpm(tpmPublicKey, &evpPublicKey);
a23473
+    } else if (tpmPublicKey->publicArea.type == TPM2_ALG_ECC) {
a23473
+        r = ossl_ecc_pub_from_tpm(tpmPublicKey, &evpPublicKey);
a23473
+    } else {
a23473
         goto_error(r, TSS2_FAPI_RC_BAD_VALUE, "Invalid alg id.", cleanup);
a23473
     }
a23473
     goto_if_error(r, "Get ossl public key.", cleanup);
a23473
@@ -708,7 +779,6 @@ ifapi_der_sig_to_tpm(
a23473
                     signatureSize);
a23473
         } else {
a23473
             return_error(TSS2_FAPI_RC_BAD_VALUE, "Invalid RSA scheme.");
a23473
-
a23473
         }
a23473
     } else if (tpmPublic->type == TPM2_ALG_ECC) {
a23473
         return ifapi_ecc_der_sig_to_tpm(signature, signatureSize,
a23473
@@ -856,12 +926,16 @@ ecdsa_verify_signature(
a23473
     return_if_null(digest, "digest is NULL", TSS2_FAPI_RC_BAD_REFERENCE);
a23473
 
a23473
     TSS2_RC r = TSS2_RC_SUCCESS;
a23473
-    EC_KEY *eccKey = NULL;
a23473
+    EVP_PKEY_CTX *ctx = NULL;
a23473
 
a23473
-    eccKey = EVP_PKEY_get1_EC_KEY(publicKey);
a23473
+    if ((ctx = EVP_PKEY_CTX_new(publicKey, NULL)) == NULL
a23473
+            || !EVP_PKEY_verify_init(ctx)) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
+                   "Cannot initialize signature verification.", error_cleanup);
a23473
+    }
a23473
 
a23473
     /* Try to verify the signature using ECDSA, note that param 0 is unused */
a23473
-    int rc = ECDSA_verify(0, digest, digestSize, signature, signatureSize, eccKey);
a23473
+    int rc = EVP_PKEY_verify(ctx, signature, signatureSize, digest, digestSize);
a23473
     if (rc == 0) {
a23473
         goto_error(r, TSS2_FAPI_RC_SIGNATURE_VERIFICATION_FAILED,
a23473
                    "ECDSA signature verification failed.", error_cleanup);
a23473
@@ -871,7 +945,7 @@ ecdsa_verify_signature(
a23473
     }
a23473
 
a23473
 error_cleanup:
a23473
-    OSSL_FREE(eccKey, EC_KEY);
a23473
+    OSSL_FREE(ctx, EVP_PKEY_CTX);
a23473
     return r;
a23473
 }
a23473
 
a23473
@@ -900,23 +974,43 @@ get_rsa_tpm2b_public_from_evp(
a23473
 
a23473
     /* Extract the public information */
a23473
     TSS2_RC r = TSS2_RC_SUCCESS;
a23473
+    int keyBits, keySize;
a23473
+
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
+    const BIGNUM *e = NULL, *n = NULL;
a23473
     RSA *rsaKey = EVP_PKEY_get1_RSA(publicKey);
a23473
     return_if_null(rsaKey, "Out of memory.", TSS2_FAPI_RC_MEMORY);
a23473
-    const BIGNUM *e = NULL, *n = NULL;
a23473
-    int rsaKeySize = RSA_size(rsaKey);
a23473
 
a23473
+    keySize = RSA_size(rsaKey);
a23473
+    keyBits = keySize * 8;
a23473
     RSA_get0_key(rsaKey, &n, &e, NULL);
a23473
-    tpmPublic->publicArea.unique.rsa.size = rsaKeySize;
a23473
+#else
a23473
+    BIGNUM *e = NULL, *n = NULL;
a23473
+
a23473
+    keyBits = EVP_PKEY_get_bits(publicKey);
a23473
+    keySize = (keyBits + 7) / 8;
a23473
+    if (!EVP_PKEY_get_bn_param(publicKey, OSSL_PKEY_PARAM_RSA_N, &n)
a23473
+            || !EVP_PKEY_get_bn_param(publicKey, OSSL_PKEY_PARAM_RSA_E, &e)) {
a23473
+        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
+                   "Retrieve pubkey", cleanup);
a23473
+    }
a23473
+#endif
a23473
+    tpmPublic->publicArea.unique.rsa.size = keySize;
a23473
     if (1 != ifapi_bn2binpad(n, &tpmPublic->publicArea.unique.rsa.buffer[0],
a23473
-                             rsaKeySize)) {
a23473
+                             keySize)) {
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
                    "Write big num byte buffer", cleanup);
a23473
     }
a23473
-    tpmPublic->publicArea.parameters.rsaDetail.keyBits = rsaKeySize * 8;
a23473
+    tpmPublic->publicArea.parameters.rsaDetail.keyBits = keyBits;
a23473
     tpmPublic->publicArea.parameters.rsaDetail.exponent = BN_get_word(e);
a23473
 
a23473
 cleanup:
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a23473
     OSSL_FREE(rsaKey, RSA);
a23473
+#else
a23473
+    BN_free(e);
a23473
+    BN_free(n);
a23473
+#endif
a23473
     return r;
a23473
 }
a23473
 
a23473
@@ -947,27 +1041,22 @@ get_ecc_tpm2b_public_from_evp(
a23473
 
a23473
     /* Initialize variables that will contain the relevant information */
a23473
     TSS2_RC r = TSS2_RC_SUCCESS;
a23473
-    EC_KEY *ecKey = EVP_PKEY_get1_EC_KEY(publicKey);
a23473
-    return_if_null(ecKey, "Out of memory.", TSS2_FAPI_RC_MEMORY);
a23473
-    const EC_GROUP *ecGroup;
a23473
-    const EC_POINT *publicPoint;
a23473
     int curveId;
a23473
     size_t ecKeySize;
a23473
     BIGNUM *bnX = NULL;
a23473
     BIGNUM *bnY = NULL;
a23473
     TPMI_ECC_CURVE tpmCurveId;
a23473
-
a23473
-    if (!ecKey) {
a23473
-        return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "No ECC key!");
a23473
-    }
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+    const EC_GROUP *ecGroup;
a23473
+    const EC_POINT *publicPoint;
a23473
+    EC_KEY *ecKey = EVP_PKEY_get1_EC_KEY(publicKey);
a23473
+    return_if_null(ecKey, "Out of memory.", TSS2_FAPI_RC_MEMORY);
a23473
 
a23473
     /* Retrieve the relevant information and write it to tpmPublic */
a23473
     ecGroup = EC_KEY_get0_group(ecKey);
a23473
     publicPoint = EC_KEY_get0_public_key(ecKey);
a23473
     curveId = EC_GROUP_get_curve_name(ecGroup);
a23473
-    ecKeySize = EC_GROUP_get_degree(ecGroup) / 8;
a23473
-    tpmPublic->publicArea.unique.ecc.x.size = ecKeySize;
a23473
-    tpmPublic->publicArea.unique.ecc.y.size = ecKeySize;
a23473
+    ecKeySize = (EC_GROUP_get_degree(ecGroup) + 7) / 8;
a23473
 
a23473
     if (!(bnX = BN_new())) {
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "Create bignum", cleanup);
a23473
@@ -982,6 +1071,23 @@ get_ecc_tpm2b_public_from_evp(
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
                    "Get affine coordinates", cleanup);
a23473
     }
a23473
+#else
a23473
+    char curveName[80];
a23473
+
a23473
+    if (!EVP_PKEY_get_utf8_string_param(publicKey, OSSL_PKEY_PARAM_GROUP_NAME,
a23473
+                                        curveName, sizeof(curveName), NULL)
a23473
+            || !EVP_PKEY_get_bn_param(publicKey, OSSL_PKEY_PARAM_EC_PUB_X, &bnX)
a23473
+            || !EVP_PKEY_get_bn_param(publicKey, OSSL_PKEY_PARAM_EC_PUB_Y, &bnY)) {
a23473
+         goto_error(r, TSS2_ESYS_RC_GENERAL_FAILURE,
a23473
+                    "Get public key", cleanup);
a23473
+     }
a23473
+    curveId = OBJ_txt2nid(curveName);
a23473
+    EC_GROUP *ecGroup = EC_GROUP_new_by_curve_name(curveId);
a23473
+    ecKeySize = (EC_GROUP_get_degree(ecGroup) + 7) / 8;
a23473
+    EC_GROUP_free(ecGroup);
a23473
+#endif
a23473
+    tpmPublic->publicArea.unique.ecc.x.size = ecKeySize;
a23473
+    tpmPublic->publicArea.unique.ecc.y.size = ecKeySize;
a23473
     if (1 != ifapi_bn2binpad(bnX, &tpmPublic->publicArea.unique.ecc.x.buffer[0],
a23473
                              ecKeySize)) {
a23473
         goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE,
a23473
@@ -1015,7 +1121,9 @@ get_ecc_tpm2b_public_from_evp(
a23473
     tpmPublic->publicArea.parameters.eccDetail.curveID = tpmCurveId;
a23473
 
a23473
 cleanup:
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
     OSSL_FREE(ecKey, EC_KEY);
a23473
+#endif
a23473
     OSSL_FREE(bnX, BN);
a23473
     OSSL_FREE(bnY, BN);
a23473
     return r;
a23473
@@ -2077,14 +2185,11 @@ ifapi_get_tpm_key_fingerprint(
a23473
                    "Unsupported hash algorithm (%" PRIu16 ")", cleanup,
a23473
                    hashAlg);
a23473
 
a23473
-    evpPublicKey = EVP_PKEY_new();
a23473
-    goto_if_null2(evpPublicKey, "Out of memory.", r, TSS2_FAPI_RC_MEMORY, cleanup);
a23473
-
a23473
     if (tpmPublicKey->publicArea.type == TPM2_ALG_RSA) {
a23473
-        r = ossl_rsa_pub_from_tpm(tpmPublicKey, evpPublicKey);
a23473
-    } else if (tpmPublicKey->publicArea.type == TPM2_ALG_ECC)
a23473
-        r = ossl_ecc_pub_from_tpm(tpmPublicKey, evpPublicKey);
a23473
-    else {
a23473
+        r = ossl_rsa_pub_from_tpm(tpmPublicKey, &evpPublicKey);
a23473
+    } else if (tpmPublicKey->publicArea.type == TPM2_ALG_ECC) {
a23473
+        r = ossl_ecc_pub_from_tpm(tpmPublicKey, &evpPublicKey);
a23473
+    } else {
a23473
         goto_error(r,TSS2_FAPI_RC_BAD_VALUE, "Invalid alg id.", cleanup);
a23473
     }
a23473
     goto_if_error(r, "Get ossl public key.", cleanup);
a23473
diff --git a/test/helper/tpm_getek.c b/test/helper/tpm_getek.c
a23473
index c6a8e906..67f76b6a 100644
a23473
--- a/test/helper/tpm_getek.c
a23473
+++ b/test/helper/tpm_getek.c
a23473
@@ -7,8 +7,14 @@
a23473
 #include <stdio.h>
a23473
 #include <inttypes.h>
a23473
 #include <openssl/evp.h>
a23473
-#include <openssl/rsa.h>
a23473
 #include <openssl/pem.h>
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+#include <openssl/rsa.h>
a23473
+#else
a23473
+#include <openssl/core_names.h>
a23473
+#include <openssl/params.h>
a23473
+#include <openssl/param_build.h>
a23473
+#endif
a23473
 
a23473
 #include "tss2_sys.h"
a23473
 #include "tss2_mu.h"
a23473
@@ -109,7 +115,7 @@ main (int argc, char *argv[])
a23473
 
a23473
     /* Convert the key from out_public to PEM */
a23473
 
a23473
-    EVP_PKEY *evp = EVP_PKEY_new();
a23473
+    EVP_PKEY *evp = NULL;
a23473
     BIO *bio;
a23473
     FILE *out = NULL;
a23473
 
a23473
@@ -124,34 +130,35 @@ main (int argc, char *argv[])
a23473
     else
a23473
         bio = BIO_new_fp(stdout, BIO_NOCLOSE);
a23473
 
a23473
-    RSA *rsa = RSA_new();
a23473
-    BIGNUM *e = BN_new();
a23473
-    BIGNUM *d = BN_new();
a23473
-    BIGNUM *p = BN_new();
a23473
-    BIGNUM *q = BN_new();
a23473
-    BIGNUM *dmp1 = BN_new();
a23473
-    BIGNUM *dmq1 = BN_new();
a23473
-    BIGNUM *iqmp = BN_new();
a23473
     BIGNUM *n = BN_bin2bn(out_public.publicArea.unique.rsa.buffer,
a23473
                           out_public.publicArea.unique.rsa.size, NULL);
a23473
-    BN_set_word(d, 0);
a23473
-    BN_set_word(p, 0);
a23473
-    BN_set_word(q, 0);
a23473
-    BN_set_word(dmp1, 0);
a23473
-    BN_set_word(dmq1, 0);
a23473
-    BN_set_word(iqmp, 0);
a23473
     uint32_t exp;
a23473
     if (out_public.publicArea.parameters.rsaDetail.exponent == 0)
a23473
         exp = 65537;
a23473
     else
a23473
         exp = out_public.publicArea.parameters.rsaDetail.exponent;
a23473
+
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+    BIGNUM *e = BN_new();
a23473
     BN_set_word(e, exp);
a23473
 
a23473
-    RSA_set0_key(rsa, n, e, d);
a23473
-    RSA_set0_factors(rsa, p, q);
a23473
-    RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
a23473
+    RSA *rsa = RSA_new();
a23473
+    RSA_set0_key(rsa, n, e, NULL);
a23473
+    n = NULL;
a23473
+    e = NULL;
a23473
 
a23473
+    evp = EVP_PKEY_new();
a23473
     EVP_PKEY_assign_RSA(evp, rsa);
a23473
+#else /* OPENSSL_VERSION_NUMBER < 0x30000000 */
a23473
+    OSSL_PARAM_BLD *build = OSSL_PARAM_BLD_new();
a23473
+    OSSL_PARAM_BLD_push_BN(build, OSSL_PKEY_PARAM_RSA_N, n);
a23473
+    OSSL_PARAM_BLD_push_uint32(build, OSSL_PKEY_PARAM_RSA_E, exp);
a23473
+    OSSL_PARAM *params = OSSL_PARAM_BLD_to_param(build);
a23473
+
a23473
+    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
a23473
+    EVP_PKEY_fromdata_init(ctx);
a23473
+    EVP_PKEY_fromdata(ctx, &evp, EVP_PKEY_PUBLIC_KEY, params);
a23473
+#endif /* OPENSSL_VERSION_NUMBER < 0x30000000 */
a23473
 
a23473
     if (!PEM_write_bio_PUBKEY(bio, evp)) {
a23473
         LOG_ERROR("PEM_write failed");
a23473
@@ -159,6 +166,14 @@ main (int argc, char *argv[])
a23473
     }
a23473
 
a23473
     EVP_PKEY_free(evp);
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+    /* ownership was taken by the EVP_PKEY */
a23473
+#else
a23473
+    EVP_PKEY_CTX_free(ctx);
a23473
+    OSSL_PARAM_free(params);
a23473
+    OSSL_PARAM_BLD_free(build);
a23473
+#endif
a23473
+    BN_free(n);
a23473
     BIO_free(bio);
a23473
     fclose(out);
a23473
 
a23473
diff --git a/test/helper/tpm_getek_ecc.c b/test/helper/tpm_getek_ecc.c
a23473
index 75165fdd..d4602925 100644
a23473
--- a/test/helper/tpm_getek_ecc.c
a23473
+++ b/test/helper/tpm_getek_ecc.c
a23473
@@ -7,9 +7,15 @@
a23473
 #include <stdio.h>
a23473
 #include <inttypes.h>
a23473
 #include <openssl/evp.h>
a23473
-#include <openssl/rsa.h>
a23473
 #include <openssl/pem.h>
a23473
 #include <openssl/err.h>
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+#include <openssl/ec.h>
a23473
+#else
a23473
+#include <openssl/core_names.h>
a23473
+#include <openssl/params.h>
a23473
+#include <openssl/param_build.h>
a23473
+#endif
a23473
 #include <string.h>
a23473
 
a23473
 #include "tss2_sys.h"
a23473
@@ -127,8 +133,7 @@ main (int argc, char *argv[])
a23473
 
a23473
     /* Convert the key from out_public to PEM */
a23473
 
a23473
-    EVP_PKEY *evp = EVP_PKEY_new();
a23473
-    EC_KEY *ecc_key = EC_KEY_new();
a23473
+    EVP_PKEY *evp = NULL;
a23473
     BIGNUM *x = NULL, *y = NULL;
a23473
     BIO *bio;
a23473
     FILE *out = NULL;
a23473
@@ -148,11 +153,6 @@ main (int argc, char *argv[])
a23473
     nid = EC_curve_nist2nid("P-256");
a23473
     EC_GROUP *ecgroup = EC_GROUP_new_by_curve_name(nid);
a23473
 
a23473
-    if (!EC_KEY_set_group(ecc_key, ecgroup))
a23473
-        exit(1);
a23473
-
a23473
-    EC_GROUP_free(ecgroup);
a23473
-
a23473
     /* Set the ECC parameters in the OpenSSL key */
a23473
     x = BN_bin2bn(out_public.publicArea.unique.ecc.x.buffer,
a23473
                   out_public.publicArea.unique.ecc.x.size, NULL);
a23473
@@ -164,15 +164,46 @@ main (int argc, char *argv[])
a23473
         exit(1);
a23473
     }
a23473
 
a23473
-    if (!EC_KEY_set_public_key_affine_coordinates(ecc_key, x, y)) {
a23473
+    EC_POINT *point = EC_POINT_new(ecgroup);
a23473
+#if OPENSSL_VERSION_NUMBER < 0x10101000L
a23473
+    EC_POINT_set_affine_coordinates_GFp(ecgroup, point, x, y, NULL);
a23473
+#else
a23473
+    EC_POINT_set_affine_coordinates(ecgroup, point, x, y, NULL);
a23473
+#endif
a23473
+
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+    EC_KEY *ecc_key = EC_KEY_new();
a23473
+    if (!EC_KEY_set_group(ecc_key, ecgroup))
a23473
+        exit(1);
a23473
+
a23473
+    if (!EC_KEY_set_public_key(ecc_key, point)) {
a23473
         exit(1);
a23473
     }
a23473
 
a23473
+    evp = EVP_PKEY_new();
a23473
     if (!EVP_PKEY_assign_EC_KEY(evp, ecc_key)) {
a23473
         handleErrors();
a23473
         LOG_ERROR("PEM_write failed");
a23473
         exit(1);
a23473
     }
a23473
+#else /* OPENSSL_VERSION_NUMBER < 0x30000000 */
a23473
+    unsigned char *puboct = NULL;
a23473
+    size_t bsize;
a23473
+
a23473
+    bsize = EC_POINT_point2buf(ecgroup, point, POINT_CONVERSION_UNCOMPRESSED,
a23473
+                               &puboct, NULL);
a23473
+
a23473
+    OSSL_PARAM_BLD *build = OSSL_PARAM_BLD_new();
a23473
+    OSSL_PARAM_BLD_push_utf8_string(build, OSSL_PKEY_PARAM_GROUP_NAME,
a23473
+                                    (char *)OBJ_nid2sn(nid), 0);
a23473
+    OSSL_PARAM_BLD_push_octet_string(build, OSSL_PKEY_PARAM_PUB_KEY,
a23473
+                                     puboct, bsize);
a23473
+    OSSL_PARAM *params = OSSL_PARAM_BLD_to_param(build);
a23473
+
a23473
+    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
a23473
+    EVP_PKEY_fromdata_init(ctx);
a23473
+    EVP_PKEY_fromdata(ctx, &evp, EVP_PKEY_PUBLIC_KEY, params);
a23473
+#endif /* OPENSSL_VERSION_NUMBER < 0x30000000 */
a23473
 
a23473
     if (!PEM_write_bio_PUBKEY(bio, evp)) {
a23473
         handleErrors();
a23473
@@ -180,9 +211,19 @@ main (int argc, char *argv[])
a23473
         exit(1);
a23473
     }
a23473
 
a23473
+    EVP_PKEY_free(evp);
a23473
+#if OPENSSL_VERSION_NUMBER < 0x30000000
a23473
+    /* ownership was taken by the EVP_PKEY */
a23473
+#else
a23473
+    EVP_PKEY_CTX_free(ctx);
a23473
+    OSSL_PARAM_free(params);
a23473
+    OSSL_PARAM_BLD_free(build);
a23473
+    OPENSSL_free(puboct);
a23473
+#endif
a23473
+    EC_POINT_free(point);
a23473
+    EC_GROUP_free(ecgroup);
a23473
     BN_free(y);
a23473
     BN_free(x);
a23473
-    EVP_PKEY_free(evp);
a23473
     BIO_free(bio);
a23473
     fclose(out);
a23473
 
a23473
-- 
a23473
2.26.3
a23473