From dab60c8dc20a1dff10951345f67fb8b7e44f430a Mon Sep 17 00:00:00 2001
From: Michele Calgaro <michele.calgaro@yahoo.it>
Date: Thu, 15 Oct 2026 14:43:15 +0900
Subject: Add support for openssl 4

Co-authored-by: Opencode/Big Pickle
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
---
 tdecore/tdehw/tdecryptographiccarddevice.cpp |  6 ++---
 tdeio/kssl/kopenssl.cpp                      | 39 +++++++++++++++++++++++++---
 tdeio/kssl/kopenssl.h                        | 24 +++++++++++++++--
 tdeio/kssl/ksslcertificate.cpp               | 18 ++++++++-----
 tdeio/kssl/ksslutils.cpp                     |  4 +--
 5 files changed, 74 insertions(+), 17 deletions(-)

diff --git a/tdecore/tdehw/tdecryptographiccarddevice.cpp b/tdecore/tdehw/tdecryptographiccarddevice.cpp
index 8062504f9..ad6b4bd88 100644
--- a/tdecore/tdehw/tdecryptographiccarddevice.cpp
+++ b/tdecore/tdehw/tdecryptographiccarddevice.cpp
@@ -648,9 +648,9 @@ TQString TDECryptographicCardDevice::autoPIN() {
 						asnSeqValueStack = d2i_ASN1_SEQUENCE_ANY(NULL, &asnSeqValueString, ASN1_STRING_length(asnValue->value.sequence));
 						asnSeqValue = sk_ASN1_TYPE_value(asnSeqValueStack, index);
 						if (asnSeqValue) {
-							if (asnSeqValue->value.octet_string->data[0] == ((V_ASN1_CONSTRUCTED | V_ASN1_CONTEXT_SPECIFIC) + index)) {
-								const unsigned char* asn1SeqValueObjectData = asnSeqValue->value.sequence->data;
-								returnCode = ASN1_get_object(&asn1SeqValueObjectData, &asn1SeqValueObjectLength, &asn1SeqValueObjectTag, &asn1SeqValueObjectClass, asnSeqValue->value.sequence->length);
+							if (ASN1_STRING_get0_data(asnSeqValue->value.octet_string)[0] == ((V_ASN1_CONSTRUCTED | V_ASN1_CONTEXT_SPECIFIC) + index)) {
+								const unsigned char* asn1SeqValueObjectData = ASN1_STRING_get0_data(asnSeqValue->value.sequence);
+								returnCode = ASN1_get_object(&asn1SeqValueObjectData, &asn1SeqValueObjectLength, &asn1SeqValueObjectTag, &asn1SeqValueObjectClass, ASN1_STRING_length(asnSeqValue->value.sequence));
 								if (!(returnCode & 0x80)) {
 									if (returnCode == (V_ASN1_CONSTRUCTED + index)) {
 										if (d2i_ASN1_GENERALSTRING(&asnGeneralString, &asn1SeqValueObjectData, asn1SeqValueObjectLength) != NULL) {
diff --git a/tdeio/kssl/kopenssl.cpp b/tdeio/kssl/kopenssl.cpp
index 27a90694c..3a73b3263 100644
--- a/tdeio/kssl/kopenssl.cpp
+++ b/tdeio/kssl/kopenssl.cpp
@@ -220,7 +220,11 @@ static int (*K_X509_NAME_add_entry_by_txt)(X509_NAME*, char*, int, unsigned char
 static X509_NAME *(*K_X509_NAME_new)() = 0L;
 static int (*K_X509_REQ_set_subject_name)(X509_REQ*,X509_NAME*) = 0L;
 static unsigned char *(*K_ASN1_STRING_data)(ASN1_STRING*) = 0L;
-static int (*K_ASN1_STRING_length)(ASN1_STRING*) = 0L;
+static const unsigned char *(*K_ASN1_STRING_get0_data)(const ASN1_STRING*) = 0L;
+static int (*K_ASN1_STRING_length)(const ASN1_STRING*) = 0L;
+static ASN1_OCTET_STRING *(*K_ASN1_OCTET_STRING_new)() = 0L;
+static void (*K_ASN1_OCTET_STRING_free)(ASN1_OCTET_STRING *) = 0L;
+static int (*K_ASN1_OCTET_STRING_set)(ASN1_OCTET_STRING *, const unsigned char *, int) = 0L;
 static STACK_OF(SSL_CIPHER) *(*K_SSL_get_ciphers)(const SSL *ssl) = 0L;
 static const ASN1_TIME* (*K_X509_CRL_get0_lastUpdate)(const X509_CRL *crl) = 0L;
 static const ASN1_TIME* (*K_X509_CRL_get0_nextUpdate)(const X509_CRL *crl) = 0L;
@@ -377,6 +381,7 @@ TDEConfig *cfg;
 	       << "libssl.so." SHLIB_VERSION_NUMBER
 	       #endif
 	       << "libssl.so"
+	       << "libssl.so.4"
 	       << "libssl.so.3"
 	       << "libssl.so.0"
 	     #endif
@@ -396,6 +401,7 @@ TDEConfig *cfg;
 	       << "libcrypto.so." SHLIB_VERSION_NUMBER
 	       #endif
 	       << "libcrypto.so"
+	       << "libcrypto.so.4"
 	       << "libcrypto.so.3"
 	       << "libcrypto.so.0"
 	     #endif
@@ -563,7 +569,11 @@ TDEConfig *cfg;
       K_X509_NAME_new = (X509_NAME *(*)()) GET_CRYPTOLIB_SYMBOL("X509_NAME_new");
       K_X509_REQ_set_subject_name = (int (*)(X509_REQ*,X509_NAME*)) GET_CRYPTOLIB_SYMBOL("X509_REQ_set_subject_name");
       K_ASN1_STRING_data = (unsigned char *(*)(ASN1_STRING*)) GET_CRYPTOLIB_SYMBOL("ASN1_STRING_data");
-      K_ASN1_STRING_length = (int (*)(ASN1_STRING*)) GET_CRYPTOLIB_SYMBOL("ASN1_STRING_length");
+      K_ASN1_STRING_get0_data = (const unsigned char *(*)(const ASN1_STRING*)) GET_CRYPTOLIB_SYMBOL("ASN1_STRING_get0_data");
+      K_ASN1_STRING_length = (int (*)(const ASN1_STRING*)) GET_CRYPTOLIB_SYMBOL("ASN1_STRING_length");
+      K_ASN1_OCTET_STRING_new = (ASN1_OCTET_STRING *(*)()) GET_CRYPTOLIB_SYMBOL("ASN1_OCTET_STRING_new");
+      K_ASN1_OCTET_STRING_free = (void (*)(ASN1_OCTET_STRING *)) GET_CRYPTOLIB_SYMBOL("ASN1_OCTET_STRING_free");
+      K_ASN1_OCTET_STRING_set = (int (*)(ASN1_OCTET_STRING *, const unsigned char *, int)) GET_CRYPTOLIB_SYMBOL("ASN1_OCTET_STRING_set");
       K_X509_CRL_get0_lastUpdate = (const ASN1_TIME* (*)(const X509_CRL *crl)) GET_CRYPTOLIB_SYMBOL("X509_CRL_get0_lastUpdate");
       K_X509_CRL_get0_nextUpdate = (const ASN1_TIME* (*)(const X509_CRL *crl)) GET_CRYPTOLIB_SYMBOL("X509_CRL_get0_nextUpdate");
       K_X509_STORE_CTX_get_current_cert = (X509* (*)(X509_STORE_CTX *ctx)) GET_CRYPTOLIB_SYMBOL("X509_STORE_CTX_get_current_cert");
@@ -1895,12 +1905,35 @@ unsigned char *KOpenSSLProxy::ASN1_STRING_data(ASN1_STRING *x) {
    return 0L;
 }
 
-int KOpenSSLProxy::ASN1_STRING_length(ASN1_STRING *x) {
+const unsigned char *KOpenSSLProxy::ASN1_STRING_get0_data(const ASN1_STRING *x) {
+   if (K_ASN1_STRING_get0_data) return (K_ASN1_STRING_get0_data)(x);
+   kdWarning() << "ASN1_STRING_get0_data not defined!" << endl;
+   return 0L;
+}
+
+int KOpenSSLProxy::ASN1_STRING_length(const ASN1_STRING *x) {
    if (K_ASN1_STRING_length) return (K_ASN1_STRING_length)(x);
    kdWarning() << "ASN1_STRING_length not defined!" << endl;
    return 0L;
 }
 
+ASN1_OCTET_STRING *KOpenSSLProxy::ASN1_OCTET_STRING_new() {
+   if (K_ASN1_OCTET_STRING_new) return (K_ASN1_OCTET_STRING_new)();
+   kdWarning() << "ASN1_OCTET_STRING_new not defined!" << endl;
+   return 0L;
+}
+
+void KOpenSSLProxy::ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *x) {
+   if (K_ASN1_OCTET_STRING_free) (K_ASN1_OCTET_STRING_free)(x);
+   else kdWarning() << "ASN1_OCTET_STRING_free not defined!" << endl;
+}
+
+int KOpenSSLProxy::ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data, int len) {
+   if (K_ASN1_OCTET_STRING_set) return (K_ASN1_OCTET_STRING_set)(str, data, len);
+   kdWarning() << "ASN1_OCTET_STRING_set not defined!" << endl;
+   return 0L;
+}
+
 STACK_OF(SSL_CIPHER) *KOpenSSLProxy::SSL_get_ciphers(const SSL* ssl) {
   if (K_SSL_get_ciphers) return (K_SSL_get_ciphers)(ssl);
   kdWarning() << "SSL_get_ciphers not defined!" << endl;
diff --git a/tdeio/kssl/kopenssl.h b/tdeio/kssl/kopenssl.h
index 8f11f4f1c..036e7abd8 100644
--- a/tdeio/kssl/kopenssl.h
+++ b/tdeio/kssl/kopenssl.h
@@ -729,10 +729,30 @@ public:
     */
    unsigned char *ASN1_STRING_data(ASN1_STRING *x);
 
-    /*
+   /*
+    *  ASN1_STRING_get0_data
+    */
+   const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x);
+
+   /*
     *  ASN1_STRING_length
     */
-   int ASN1_STRING_length(ASN1_STRING *x);
+   int ASN1_STRING_length(const ASN1_STRING *x);
+
+   /*
+    *  ASN1_OCTET_STRING_new
+    */
+   ASN1_OCTET_STRING* ASN1_OCTET_STRING_new();
+
+   /*
+    *  ASN1_OCTET_STRING_free
+    */
+   void ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *x);
+
+   /*
+    *  ASN1_OCTET_STRING_set
+    */
+   int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data, int len);
 
    /*
     *  
diff --git a/tdeio/kssl/ksslcertificate.cpp b/tdeio/kssl/ksslcertificate.cpp
index 2d5aa3634..bcb8ff69f 100644
--- a/tdeio/kssl/ksslcertificate.cpp
+++ b/tdeio/kssl/ksslcertificate.cpp
@@ -234,8 +234,8 @@ int n, i;
 
 	rc += "\n";
 	rc += i18n("Signature Contents:");
-	n = signature->length;
-	s = (char *)signature->data;
+	n = d->kossl->ASN1_STRING_length(signature);
+	s = (char *)d->kossl->ASN1_STRING_get0_data(signature);
 	for (i = 0; i < n; i++) {
 		if (i%20 != 0) rc += ":";
 		else rc += "\n";
@@ -1081,18 +1081,22 @@ TQByteArray KSSLCertificate::toNetscape() {
 TQByteArray qba;
 #ifdef KSSL_HAVE_SSL
 	NETSCAPE_X509 nx;
-	ASN1_OCTET_STRING hdr;
+	ASN1_OCTET_STRING *hdr;
 	KTempFile ktf;
 
-	hdr.data = (unsigned char *)NETSCAPE_CERT_HDR;
-	hdr.length = strlen(NETSCAPE_CERT_HDR);
-	nx.header = &hdr;
+	hdr = d->kossl->ASN1_OCTET_STRING_new();
+	if (hdr)
+		d->kossl->ASN1_OCTET_STRING_set(hdr, (const unsigned char *)NETSCAPE_CERT_HDR, strlen(NETSCAPE_CERT_HDR));
+	nx.header = hdr;
 	nx.cert = getCert();
 
 	d->kossl->ASN1_i2d_fp(ktf.fstream(),(unsigned char *)&nx);
 
 	ktf.close();
 
+	if (hdr)
+		d->kossl->ASN1_OCTET_STRING_free(hdr);
+
 	TQFile qf(ktf.name());
 	qf.open(IO_ReadOnly);
 	char *buf = new char[qf.size()];
@@ -1174,7 +1178,7 @@ TQStringList KSSLCertificate::subjAltNames() const {
 			continue;
 		}
 
-		TQString s = (const char *)d->kossl->ASN1_STRING_data(val->d.ia5);
+		TQString s = (const char *)d->kossl->ASN1_STRING_get0_data(val->d.ia5);
 		if (!s.isEmpty()  &&
 				/* skip subjectAltNames with embedded NULs */
 				s.length() == (unsigned int)d->kossl->ASN1_STRING_length(val->d.ia5)) {
diff --git a/tdeio/kssl/ksslutils.cpp b/tdeio/kssl/ksslutils.cpp
index 2e6e3a303..8b005d8a6 100644
--- a/tdeio/kssl/ksslutils.cpp
+++ b/tdeio/kssl/ksslutils.cpp
@@ -43,8 +43,8 @@ int y=0,M=0,d=0,h=0,m=0,s=0;
 TQDate qdate;
 TQTime qtime;
  
-  i = tm->length;
-  v = (char *)tm->data;
+  i = KOSSL::self()->ASN1_STRING_length(tm);
+  v = (char *)KOSSL::self()->ASN1_STRING_get0_data(tm);
   if ((i == 15) || (i == 21)) {
           gentime=1;
           yoffset=2;
-- 
cgit v1.2.3

