diff --git a/src/native/libs/System.Security.Cryptography.Native/openssl.c b/src/native/libs/System.Security.Cryptography.Native/openssl.c index 2d953bb33bab54..546f63e7500437 100644 --- a/src/native/libs/System.Security.Cryptography.Native/openssl.c +++ b/src/native/libs/System.Security.Cryptography.Native/openssl.c @@ -1041,6 +1041,13 @@ int32_t CryptoNative_BioSeek(BIO* bio, int32_t ofs) return BIO_seek(bio, ofs); } +#ifdef FEATURE_DISTRO_AGNOSTIC_SSL +static void local_sk_X509_freefunc_thunk(OPENSSL_sk_freefunc freefunc_arg, void* ptr) +{ + freefunc_arg(ptr); +} +#endif + /* Function: NewX509Stack @@ -1054,7 +1061,19 @@ A STACK_OF(X509*) with no comparator. STACK_OF(X509) * CryptoNative_NewX509Stack(void) { ERR_clear_error(); + +#ifdef FEATURE_DISTRO_AGNOSTIC_SSL + OPENSSL_STACK* sk = OPENSSL_sk_new_null(); + + if (API_EXISTS(OPENSSL_sk_set_thunks)) + { + OPENSSL_sk_set_thunks(sk, local_sk_X509_freefunc_thunk); + } + + return (STACK_OF(X509)*)sk; +#else return sk_X509_new_null(); +#endif } /* diff --git a/src/native/libs/System.Security.Cryptography.Native/opensslshim.c b/src/native/libs/System.Security.Cryptography.Native/opensslshim.c index 6d9ba5deaeff82..df1e7c65d06e2c 100644 --- a/src/native/libs/System.Security.Cryptography.Native/opensslshim.c +++ b/src/native/libs/System.Security.Cryptography.Native/opensslshim.c @@ -123,6 +123,12 @@ static void OpenLibraryOnce(void) { DlOpen(MAKELIB("1.1")); } + + // While it's still in alpha, OpenSSL 4 is probed, but not preferred. + if (libssl == NULL) + { + DlOpen(MAKELIB("4")); + } } static pthread_once_t g_openLibrary = PTHREAD_ONCE_INIT; diff --git a/src/native/libs/System.Security.Cryptography.Native/opensslshim.h b/src/native/libs/System.Security.Cryptography.Native/opensslshim.h index 43f93f86dcccfd..5ec8bde682bd4f 100644 --- a/src/native/libs/System.Security.Cryptography.Native/opensslshim.h +++ b/src/native/libs/System.Security.Cryptography.Native/opensslshim.h @@ -1423,9 +1423,6 @@ extern TYPEOF(OPENSSL_gmtime)* OPENSSL_gmtime_ptr; #define sk_X509_NAME_num(stack) OPENSSL_sk_num((const OPENSSL_STACK*)(1 ? stack : (const STACK_OF(X509_NAME)*)0)) #define sk_X509_num(stack) OPENSSL_sk_num((const OPENSSL_STACK*)(1 ? stack : (const STACK_OF(X509)*)0)) -// type-safe OPENSSL_sk_new_null -#define sk_X509_new_null() (STACK_OF(X509)*)OPENSSL_sk_new_null() - // type-safe OPENSSL_sk_push #define sk_X509_push(stack,value) OPENSSL_sk_push((OPENSSL_STACK*)(1 ? stack : (STACK_OF(X509)*)0), (const void*)(1 ? value : (X509*)0)) diff --git a/src/native/libs/System.Security.Cryptography.Native/pal_x509.c b/src/native/libs/System.Security.Cryptography.Native/pal_x509.c index cf5b6b4b07edf2..65a369585df6e7 100644 --- a/src/native/libs/System.Security.Cryptography.Native/pal_x509.c +++ b/src/native/libs/System.Security.Cryptography.Native/pal_x509.c @@ -11,6 +11,8 @@ #include #include +#include "openssl.h" + #ifndef NAME_MAX #error "NAME_MAX is not defined" #endif @@ -625,7 +627,7 @@ int32_t CryptoNative_X509StackAddDirectoryStore(X509Stack* stack, char* storePat if (storeDir != NULL) { X509* cert; - X509Stack* tmpStack = sk_X509_new_null(); + X509Stack* tmpStack = CryptoNative_NewX509Stack(); if (tmpStack == NULL) { @@ -1348,7 +1350,7 @@ int32_t CryptoNative_X509DecodeOcspToExpiration(const uint8_t* buf, int32_t len, if (store != NULL) { - bag = sk_X509_new_null(); + bag = CryptoNative_NewX509Stack(); } if (bag != NULL)