From 6dcc3b2984e3f4dc1849c79d89c6ce736a8435b3 Mon Sep 17 00:00:00 2001
From: Petr Gotthard <petr.gotthard@advantech.cz>
Date: Sat, 7 Mar 2026 15:51:12 +0100
Subject: [PATCH] Fix ASN1_OCTET_STRING and X509_NAME access for OpenSSL 4.0
 compatibility

Fixes: #166
---
 src/tpm2-provider-pkey.c    |  8 ++++----
 test/ec_genpkey_x509_csr.c  | 10 ++++++----
 test/rsa_pki/etc/email.conf |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/tpm2-provider-pkey.c b/src/tpm2-provider-pkey.c
index 38f748e..80213af 100644
--- a/src/tpm2-provider-pkey.c
+++ b/src/tpm2-provider-pkey.c
@@ -149,13 +149,13 @@ tpm2_keydata_read(BIO *bin, TPM2_KEYDATA *keydata, TPM2_PKEY_FORMAT format)
             strcmp(type_oid, OID_loadableKey))
         goto error;
 
-    if (Tss2_MU_TPM2B_PRIVATE_Unmarshal(tpk->privkey->data,
-                                        tpk->privkey->length, NULL,
+    if (Tss2_MU_TPM2B_PRIVATE_Unmarshal(ASN1_STRING_get0_data(tpk->privkey),
+                                        ASN1_STRING_length(tpk->privkey), NULL,
                                         &keydata->priv))
         goto error;
 
-    if (Tss2_MU_TPM2B_PUBLIC_Unmarshal(tpk->pubkey->data,
-                                       tpk->pubkey->length, NULL,
+    if (Tss2_MU_TPM2B_PUBLIC_Unmarshal(ASN1_STRING_get0_data(tpk->pubkey),
+                                       ASN1_STRING_length(tpk->pubkey), NULL,
                                        &keydata->pub))
         goto error;
 
diff --git a/test/ec_genpkey_x509_csr.c b/test/ec_genpkey_x509_csr.c
index e3523ec..5c9790a 100644
--- a/test/ec_genpkey_x509_csr.c
+++ b/test/ec_genpkey_x509_csr.c
@@ -15,7 +15,7 @@ int generate_csr(const char *password, const char *filename)
     EVP_PKEY_CTX *pctx = NULL;
     EVP_PKEY *pkey = NULL;
     X509_REQ *x509 = NULL;
-    X509_NAME *name;
+    X509_NAME *name = NULL;
     STACK_OF(X509_EXTENSION) *exts = NULL;
     X509_EXTENSION *ex;
     FILE *csr_file = NULL;
@@ -40,9 +40,10 @@ int generate_csr(const char *password, const char *filename)
             || X509_REQ_set_pubkey(x509, pkey) != 1)
         goto error1;
 
-    name = X509_REQ_get_subject_name(x509);
-    if (!X509_NAME_add_entry_by_NID(name, NID_countryName, MBSTRING_ASC, (unsigned char *)"CZ", -1, -1, 0)
-            || !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC, (const unsigned char *)"www.example.com", -1, -1, 0))
+    if (!(name = X509_NAME_new())
+            || !X509_NAME_add_entry_by_NID(name, NID_countryName, MBSTRING_ASC, (unsigned char *)"CZ", -1, -1, 0)
+            || !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC, (const unsigned char *)"www.example.com", -1, -1, 0)
+            || X509_REQ_set_subject_name(x509, name) != 1)
         goto error1;
 
     // set requested extensions
@@ -75,6 +76,7 @@ int generate_csr(const char *password, const char *filename)
     fclose(csr_file);
 error1:
     sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
+    X509_NAME_free(name);
     X509_REQ_free(x509);
     EVP_PKEY_free(pkey);
     EVP_PKEY_CTX_free(pctx);
diff --git a/test/rsa_pki/etc/email.conf b/test/rsa_pki/etc/email.conf
index 7606c38..73c06d2 100644
--- a/test/rsa_pki/etc/email.conf
+++ b/test/rsa_pki/etc/email.conf
@@ -24,4 +24,4 @@ emailAddress            = "fred@simple.org"
 keyUsage                = critical,digitalSignature,keyEncipherment
 extendedKeyUsage        = emailProtection,clientAuth
 subjectKeyIdentifier    = hash
-subjectAltName          = email:move
+subjectAltName          = email:copy
