Skip to content

Commit 076687f

Browse files
committed
Revert "Disable implicit rejection for RSA PKCS#1 (dotnet#95218)"
This reverts commit e3500b8. To quote Clemens Lang: > [Disabling implcit rejection] re-enables a Bleichenbacher timing oracle > attack against PKCS#1v1.5 decryption. See > https://people.redhat.com/~hkario/marvin/ for details and > dotnet#95157 (comment) for a > comment by the researcher who published the vulnerability and proposed the > change in OpenSSL. For more details, see: dotnet#95216 (comment)
1 parent a43d69f commit 076687f

3 files changed

Lines changed: 10 additions & 58 deletions

File tree

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,19 @@ private void RsaCryptRoundtrip(RSAEncryptionPadding paddingMode, bool expectSucc
338338
Assert.Equal(TestData.HelloBytes, output);
339339
}
340340

341-
[ConditionalFact(nameof(PlatformSupportsEmptyRSAEncryption))]
341+
[ConditionalFact]
342342
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
343343
public void RoundtripEmptyArray()
344344
{
345+
if (OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(13, 6))
346+
{
347+
throw new SkipTestException("iOS prior to 13.6 does not reliably support RSA encryption of empty data.");
348+
}
349+
if (OperatingSystem.IsTvOS() && !OperatingSystem.IsTvOSVersionAtLeast(14, 0))
350+
{
351+
throw new SkipTestException("tvOS prior to 14.0 does not reliably support RSA encryption of empty data.");
352+
}
353+
345354
using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params))
346355
{
347356
void RoundtripEmpty(RSAEncryptionPadding paddingMode)
@@ -692,26 +701,6 @@ public void NotSupportedValueMethods()
692701
}
693702
}
694703

695-
[ConditionalTheory]
696-
[InlineData(new byte[] { 1, 2, 3, 4 })]
697-
[InlineData(new byte[0])]
698-
public void Decrypt_Pkcs1_ErrorsForInvalidPadding(byte[] data)
699-
{
700-
if (data.Length == 0 && !PlatformSupportsEmptyRSAEncryption)
701-
{
702-
throw new SkipTestException("Platform does not support RSA encryption of empty data.");
703-
}
704-
705-
using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params))
706-
{
707-
byte[] encrypted = Encrypt(rsa, data, RSAEncryptionPadding.Pkcs1);
708-
encrypted[1] ^= 0xFF;
709-
710-
// PKCS#1, the data, and the key are all deterministic so this should always throw an exception.
711-
Assert.ThrowsAny<CryptographicException>(() => Decrypt(rsa, encrypted, RSAEncryptionPadding.Pkcs1));
712-
}
713-
}
714-
715704
public static IEnumerable<object[]> OaepPaddingModes
716705
{
717706
get
@@ -726,23 +715,5 @@ public static IEnumerable<object[]> OaepPaddingModes
726715
}
727716
}
728717
}
729-
730-
public static bool PlatformSupportsEmptyRSAEncryption
731-
{
732-
get
733-
{
734-
if (OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(13, 6))
735-
{
736-
return false;
737-
}
738-
739-
if (OperatingSystem.IsTvOS() && !OperatingSystem.IsTvOSVersionAtLeast(14, 0))
740-
{
741-
return false;
742-
}
743-
744-
return true;
745-
}
746-
}
747718
}
748719
}

src/libraries/Native/Unix/System.Security.Cryptography.Native/opensslshim.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,8 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
272272
REQUIRED_FUNCTION(ERR_peek_error) \
273273
REQUIRED_FUNCTION(ERR_peek_error_line) \
274274
REQUIRED_FUNCTION(ERR_peek_last_error) \
275-
REQUIRED_FUNCTION(ERR_pop_to_mark) \
276275
FALLBACK_FUNCTION(ERR_put_error) \
277276
REQUIRED_FUNCTION(ERR_reason_error_string) \
278-
REQUIRED_FUNCTION(ERR_set_mark) \
279277
LIGHTUP_FUNCTION(ERR_set_debug) \
280278
LIGHTUP_FUNCTION(ERR_set_error) \
281279
REQUIRED_FUNCTION(EVP_aes_128_cbc) \
@@ -330,7 +328,6 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
330328
REQUIRED_FUNCTION(EVP_PKCS82PKEY) \
331329
REQUIRED_FUNCTION(EVP_PKEY2PKCS8) \
332330
REQUIRED_FUNCTION(EVP_PKEY_CTX_ctrl) \
333-
REQUIRED_FUNCTION(EVP_PKEY_CTX_ctrl_str) \
334331
REQUIRED_FUNCTION(EVP_PKEY_CTX_free) \
335332
REQUIRED_FUNCTION(EVP_PKEY_CTX_get0_pkey) \
336333
REQUIRED_FUNCTION(EVP_PKEY_CTX_new) \
@@ -728,10 +725,8 @@ FOR_ALL_OPENSSL_FUNCTIONS
728725
#define ERR_peek_error_line ERR_peek_error_line_ptr
729726
#define ERR_peek_last_error ERR_peek_last_error_ptr
730727
#define ERR_put_error ERR_put_error_ptr
731-
#define ERR_pop_to_mark ERR_pop_to_mark_ptr
732728
#define ERR_reason_error_string ERR_reason_error_string_ptr
733729
#define ERR_set_debug ERR_set_debug_ptr
734-
#define ERR_set_mark ERR_set_mark_ptr
735730
#define ERR_set_error ERR_set_error_ptr
736731
#define EVP_aes_128_cbc EVP_aes_128_cbc_ptr
737732
#define EVP_aes_128_cfb8 EVP_aes_128_cfb8_ptr
@@ -785,7 +780,6 @@ FOR_ALL_OPENSSL_FUNCTIONS
785780
#define EVP_PKCS82PKEY EVP_PKCS82PKEY_ptr
786781
#define EVP_PKEY2PKCS8 EVP_PKEY2PKCS8_ptr
787782
#define EVP_PKEY_CTX_ctrl EVP_PKEY_CTX_ctrl_ptr
788-
#define EVP_PKEY_CTX_ctrl_str EVP_PKEY_CTX_ctrl_str_ptr
789783
#define EVP_PKEY_CTX_free EVP_PKEY_CTX_free_ptr
790784
#define EVP_PKEY_CTX_get0_pkey EVP_PKEY_CTX_get0_pkey_ptr
791785
#define EVP_PKEY_CTX_new EVP_PKEY_CTX_new_ptr

src/libraries/Native/Unix/System.Security.Cryptography.Native/pal_evp_pkey_rsa.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,6 @@ static bool ConfigureEncryption(EVP_PKEY_CTX* ctx, RsaPaddingMode padding, const
6363
{
6464
return false;
6565
}
66-
67-
// OpenSSL 3.2 introduced a change where PKCS#1 RSA decryption does not fail for invalid padding.
68-
// If the padding is invalid, the decryption operation returns random data.
69-
// See https://github.com/openssl/openssl/pull/13817 for background.
70-
// Some Linux distributions backported this change to previous versions of OpenSSL.
71-
// Here we do a best-effort to set a flag to revert the behavior to failing if the padding is invalid.
72-
ERR_set_mark();
73-
74-
EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pkcs1_implicit_rejection", "0");
75-
76-
// Undo any changes to the error queue that may have occured while configuring implicit rejection if the
77-
// current version does not support implicit rejection.
78-
ERR_pop_to_mark();
7966
}
8067
else
8168
{

0 commit comments

Comments
 (0)