From 132f578b60b39643aaf42f715819e9ea1ba6ddc4 Mon Sep 17 00:00:00 2001 From: Radek Zikmund Date: Mon, 23 Nov 2026 11:50:06 +0100 Subject: [PATCH] Fix OpenSSL 4 signature configuration and renegotiation errors Map the SM2 signature scheme advertised by OpenSSL 4 to its accepted name. Read failures from non-I/O renegotiation APIs through the error queue instead of consulting stale SSL_get_error I/O state. Add coverage for configuring all RSA-padding combinations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Interop.OpenSsl.cs | 1 + .../tests/UnitTests/OpenSslErrorTests.cs | 17 +++++++++++++++++ .../pal_ssl.c | 5 +++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs index 0871e26dbe53a1..dacb233f2e043a 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs @@ -565,6 +565,7 @@ internal static string[] GetDefaultSignatureAlgorithms() 0x0503 => "ecdsa_secp384r1_sha384", 0x0601 => "rsa_pkcs1_sha512", 0x0603 => "ecdsa_secp521r1_sha512", + 0x0708 => "sm2sig_sm3", 0x0804 => "rsa_pss_rsae_sha256", 0x0805 => "rsa_pss_rsae_sha384", 0x0806 => "rsa_pss_rsae_sha512", diff --git a/src/libraries/System.Net.Security/tests/UnitTests/OpenSslErrorTests.cs b/src/libraries/System.Net.Security/tests/UnitTests/OpenSslErrorTests.cs index 3f405600e35981..275b46cfe5528a 100644 --- a/src/libraries/System.Net.Security/tests/UnitTests/OpenSslErrorTests.cs +++ b/src/libraries/System.Net.Security/tests/UnitTests/OpenSslErrorTests.cs @@ -1,12 +1,29 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.Win32.SafeHandles; using Xunit; namespace System.Net.Security.Tests; public class OpenSslErrorTests { + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsOpenSsl3))] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void ConfigureSignatureAlgorithms_DefaultsAreAccepted(bool enablePss, bool enablePkcs1) + { + Interop.CryptoInitializer.Initialize(); + using SafeSslContextHandle context = Interop.Ssl.SslCtxCreate(Interop.Ssl.SslV2_3Method()); + Assert.False(context.IsInvalid); + using SafeSslHandle ssl = Interop.Ssl.SslCreate(context); + Assert.False(ssl.IsInvalid); + + Interop.OpenSsl.ConfigureSignatureAlgorithms(ssl, enablePss, enablePkcs1); + } + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsOpenSslSupported))] // OpenSSL 1.x: ERR_PACK(ERR_LIB_SSL, SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_EXTENSION_NOT_RECEIVED) [InlineData(0x14268117u, false, 279)] diff --git a/src/native/libs/System.Security.Cryptography.Native/pal_ssl.c b/src/native/libs/System.Security.Cryptography.Native/pal_ssl.c index 7bea91bec09c41..de794954c8f30a 100644 --- a/src/native/libs/System.Security.Cryptography.Native/pal_ssl.c +++ b/src/native/libs/System.Security.Cryptography.Native/pal_ssl.c @@ -446,6 +446,7 @@ int32_t CryptoNative_SslRenegotiate(SSL* ssl, int32_t* error) { ERR_clear_error(); + // These are not I/O operations; their failure reasons are on the error queue, not in SSL_get_error. #if defined NEED_OPENSSL_1_1 || defined NEED_OPENSSL_3_0 // TLS1.3 uses different API for renegotiation/delayed client cert request #ifndef TLS1_3_VERSION @@ -458,7 +459,7 @@ int32_t CryptoNative_SslRenegotiate(SSL* ssl, int32_t* error) int ret = SSL_verify_client_post_handshake(ssl); if (ret != 1) { - *error = CryptoNative_SslGetError(ssl, ret); + *error = SSL_ERROR_SSL; } return ret; } @@ -474,7 +475,7 @@ int32_t CryptoNative_SslRenegotiate(SSL* ssl, int32_t* error) int ret = SSL_renegotiate(ssl); if(ret != 1) { - *error = CryptoNative_SslGetError(ssl, ret); + *error = SSL_ERROR_SSL; } return ret; }