From d70714fcecb108747ccacf331b7e23cd5a7bb3df Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Wed, 11 Nov 2026 10:46:34 -0500 Subject: [PATCH] Restore EC consistency checks for Linux --- .../ECDiffieHellmanTests.ImportExport.cs | 23 +++++++++++++++++++ .../ECDsa/ECDsaImportExport.cs | 23 +++++++++++++++++++ .../pal_ecc_import_export.c | 17 ++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDiffieHellman/ECDiffieHellmanTests.ImportExport.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDiffieHellman/ECDiffieHellmanTests.ImportExport.cs index a0b6d80aa1ec4c..78c653e27afe2f 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDiffieHellman/ECDiffieHellmanTests.ImportExport.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDiffieHellman/ECDiffieHellmanTests.ImportExport.cs @@ -301,6 +301,29 @@ public void TestNamedImportValidationNegative() } } + [ConditionalTheory] + [InlineData(false)] + [InlineData(true)] + [PlatformSpecific(TestPlatforms.Windows | TestPlatforms.Linux)] + public void ImportParameters_MismatchedKeyPair_Throws(bool explicitCurve) + { + if (explicitCurve) + { + SkipTestException.ThrowUnless(ECDiffieHellmanFactory.ExplicitCurvesSupported); + } + + ECParameters parameters = explicitCurve ? + EccTestData.GetNistP256ReferenceKeyExplicit() : + EccTestData.GetNistP256ReferenceKey(); + + parameters.D[0] ^= 1; + + using (ECDiffieHellman ec = ECDiffieHellmanFactory.Create()) + { + Assert.ThrowsAny(() => ec.ImportParameters(parameters)); + } + } + [Fact] public void TestGeneralExportWithExplicitParameters() { diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaImportExport.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaImportExport.cs index b4cfc179ae8265..fe4a7d2927e897 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaImportExport.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaImportExport.cs @@ -290,6 +290,29 @@ public void TestNamedImportValidationNegative() } } + [ConditionalTheory] + [InlineData(false)] + [InlineData(true)] + [PlatformSpecific(TestPlatforms.Windows | TestPlatforms.Linux)] + public void ImportParameters_MismatchedKeyPair_Throws(bool explicitCurve) + { + if (explicitCurve) + { + SkipTestException.ThrowUnless(ECExplicitCurvesSupported); + } + + ECParameters parameters = explicitCurve ? + EccTestData.GetNistP256ReferenceKeyExplicit() : + EccTestData.GetNistP256ReferenceKey(); + + parameters.D[0] ^= 1; + + using (ECDsa ec = ECDsaFactory.Create()) + { + Assert.ThrowsAny(() => ec.ImportParameters(parameters)); + } + } + [ConditionalFact] public void TestGeneralExportWithExplicitParameters() { diff --git a/src/native/libs/System.Security.Cryptography.Native/pal_ecc_import_export.c b/src/native/libs/System.Security.Cryptography.Native/pal_ecc_import_export.c index cd452b403eb75b..3ba37a18d1047d 100644 --- a/src/native/libs/System.Security.Cryptography.Native/pal_ecc_import_export.c +++ b/src/native/libs/System.Security.Cryptography.Native/pal_ecc_import_export.c @@ -115,6 +115,17 @@ static int PushPublicKeyParam( return 1; } + +static int CheckEcKey(EVP_PKEY* key, int hasPrivateKey) +{ + EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(key, NULL); + if (ctx == NULL) + return 0; + + int ret = hasPrivateKey ? EVP_PKEY_check(ctx) : EVP_PKEY_public_check(ctx); + EVP_PKEY_CTX_free(ctx); + return ret == 1; +} #endif static ECCurveType MethodToCurveType(const EC_METHOD* method) @@ -1835,6 +1846,9 @@ static int32_t EvpPKeyCreateByEcParameters( goto error; } + if (!CheckEcKey(*pkey, hasPrivateKey)) + goto error; + if (keySize != NULL) { *keySize = CryptoNative_EvpPKeyGetEcKeySize(*pkey); @@ -2178,6 +2192,9 @@ static int32_t EvpPKeyCreateByEcExplicitParameters( int selection = hasPrivateKey ? EVP_PKEY_KEYPAIR : EVP_PKEY_PUBLIC_KEY; if (EVP_PKEY_fromdata(ctx, pkey, selection, params) != 1) goto error; + + if (!CheckEcKey(*pkey, hasPrivateKey)) + goto error; } if (keySize != NULL)