From 57e10cbbe75069be92c7e9720c180a05833481fc Mon Sep 17 00:00:00 2001
From: Shawn C <citypw@hardenedlinux.org>
Date: Sat, 25 Apr 2026 20:05:34 +0800
Subject: [PATCH] Fix the memory leak in OSSL::byteString2oid (#863)

The function `OSSL::byteString2oid` in `SoftHSM2` (OpenSSL backend)
leaks memory when decoding PKCS#11 attribute values as ASN.1 objects
or strings. It calls `d2i_ASN1_OBJECT` and `d2i_ASN1_PRINTABLESTRING`
which allocate new OpenSSL objects, but these objects are never
freed after their NIDs are extracted.

Signed-off-by: Shawn C <citypw@hardenedlinux.org>
---
 src/lib/crypto/OSSLUtil.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/lib/crypto/OSSLUtil.cpp b/src/lib/crypto/OSSLUtil.cpp
index df74f7b18..2792623d7 100644
--- a/src/lib/crypto/OSSLUtil.cpp
+++ b/src/lib/crypto/OSSLUtil.cpp
@@ -189,8 +189,10 @@ int OSSL::byteString2oid(const ByteString& byteString)
 		{
 			return NID_undef;
 		}
-
-		return OBJ_obj2nid(oid);
+		
+		int nid = OBJ_obj2nid(oid);
+		ASN1_OBJECT_free(oid);
+		return nid;
 	}
 	else if (pclass == V_ASN1_UNIVERSAL && tag == V_ASN1_PRINTABLESTRING)
 	{
@@ -216,6 +218,8 @@ int OSSL::byteString2oid(const ByteString& byteString)
 		{
 			return EVP_PKEY_X448;
 		}
+
+		ASN1_STRING_free(curve_name);
 	}
 
 	return NID_undef;
