diff --git a/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs b/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs
index 8e8726fb3cd2c0..dcd36ca435cdc0 100644
--- a/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs
+++ b/src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.cs
@@ -384,7 +384,7 @@ private static bool IsMagicValueOfKeyPublic(KeyBlobMagicNumber magic)
/// that don't have the named curve functionality.
///
private static KeyBlobMagicNumber EcdsaCurveNameToMagicNumber(string? name, bool includePrivateParameters) =>
- EcdsaCurveNameToAlgorithm(name) switch
+ CngKey.EcdsaCurveNameToAlgorithm(name).Algorithm switch
{
AlgorithmName.ECDsaP256 => includePrivateParameters ?
KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC :
@@ -409,7 +409,7 @@ private static KeyBlobMagicNumber EcdsaCurveNameToMagicNumber(string? name, bool
/// that don't have the named curve functionality.
///
private static KeyBlobMagicNumber EcdhCurveNameToMagicNumber(string? name, bool includePrivateParameters) =>
- EcdhCurveNameToAlgorithm(name) switch
+ CngKey.EcdhCurveNameToAlgorithm(name).Algorithm switch
{
AlgorithmName.ECDHP256 => includePrivateParameters ?
KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P256_MAGIC :
@@ -513,58 +513,5 @@ ref MemoryMarshal.GetReference(keyBlob),
return keyHandle;
}
-
- ///
- /// Map a curve name to algorithm. This enables curves that worked pre-Win10
- /// to work with newer APIs for import and export.
- ///
- internal static string EcdsaCurveNameToAlgorithm(string? algorithm)
- {
- switch (algorithm)
- {
- case "nistP256":
- case "ECDSA_P256":
- return AlgorithmName.ECDsaP256;
-
- case "nistP384":
- case "ECDSA_P384":
- return AlgorithmName.ECDsaP384;
-
- case "nistP521":
- case "ECDSA_P521":
- return AlgorithmName.ECDsaP521;
- }
-
- // All other curves are new in Win10 so use generic algorithm
- return AlgorithmName.ECDsa;
- }
-
- ///
- /// Map a curve name to algorithm. This enables curves that worked pre-Win10
- /// to work with newer APIs for import and export.
- ///
- internal static string EcdhCurveNameToAlgorithm(string? algorithm)
- {
- switch (algorithm)
- {
- case "nistP256":
- case "ECDH_P256":
- case "ECDSA_P256":
- return AlgorithmName.ECDHP256;
-
- case "nistP384":
- case "ECDH_P384":
- case "ECDSA_P384":
- return AlgorithmName.ECDHP384;
-
- case "nistP521":
- case "ECDH_P521":
- case "ECDSA_P521":
- return AlgorithmName.ECDHP521;
- }
-
- // All other curves are new in Win10 so use generic algorithm
- return AlgorithmName.ECDH;
- }
}
}
diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/EccTestBase.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/EccTestBase.cs
index 2f347469e12cb9..5f9dae4c8fcefb 100644
--- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/EccTestBase.cs
+++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/EccTestBase.cs
@@ -249,6 +249,18 @@ internal static void CompareCurve(in ECCurve c1, in ECCurve c2)
}
}
}
+
+ internal static string InvertStringCase(string str)
+ {
+ return string.Create(str.Length, str, static (destination, str) =>
+ {
+ for (int i = 0; i < str.Length; i++)
+ {
+ char c = str[i];
+ destination[i] = char.IsAsciiLetter(c) ? (char)(c ^ 0b0100000) : c;
+ }
+ });
+ }
#endif
}
}
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 0c06a3f12f1516..82f78094bb1000 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
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Collections.Generic;
using System.Security.Cryptography.Tests;
using Test.Cryptography;
using Xunit;
@@ -14,7 +15,7 @@ public partial class ECDiffieHellmanTests
// probe for this capability before depending on it.
internal static bool ECDsa224Available =>
ECDiffieHellmanFactory.IsCurveValid(new Oid(ECDSA_P224_OID_VALUE));
-
+
internal static bool CanDeriveNewPublicKey { get; }
= EcDiffieHellman.Tests.ECDiffieHellmanFactory.CanDeriveNewPublicKey;
@@ -416,6 +417,50 @@ public static void ImportFromPrivateOnlyKey()
}
}
+ [Theory]
+ [MemberData(nameof(NamedCurves))]
+ public static void OidPresentOnCurveMiscased(ECCurve curve)
+ {
+ ECCurve miscasedCurve = ECCurve.CreateFromFriendlyName(InvertStringCase(curve.Oid.FriendlyName));
+ Assert.NotEqual(miscasedCurve.Oid.FriendlyName, curve.Oid.FriendlyName);
+ Assert.Equal(miscasedCurve.Oid.FriendlyName, curve.Oid.FriendlyName, ignoreCase: true);
+
+ using (ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create())
+ {
+ ecdh.GenerateKey(miscasedCurve);
+ ECParameters exportedParameters = ecdh.ExportParameters(false);
+ Assert.Equal(curve.Oid.Value, exportedParameters.Curve.Oid.Value);
+
+ exportedParameters.Curve = miscasedCurve;
+
+ // Assert.NoThrow. Make sure we can import the mis-cased curve.
+ ecdh.ImportParameters(exportedParameters);
+ }
+ }
+
+ public static IEnumerable