Lookup OID if only friendly name is specified for ECCurve
authorKevin Jones <kevin@vcsjones.com>
Sat, 29 Jun 2019 06:01:53 +0000 (02:01 -0400)
committerJeremy Barton <jbarton@microsoft.com>
Sat, 29 Jun 2019 06:01:53 +0000 (23:01 -0700)
Fixes the use of Windows-specific curve identifiers with ECDsaOpenSsl

Commit migrated from https://github.com/dotnet/corefx/commit/0509d602e8b83468279151cdec11694484b07a86

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/ECCurve.cs
src/libraries/System.Security.Cryptography.OpenSsl/tests/EcDsaOpenSslTests.cs

index b4ac0de..d29d85f 100644 (file)
@@ -142,7 +142,21 @@ namespace System.Security.Cryptography
 
         private static ECCurve CreateFromValueAndName(string oidValue, string oidFriendlyName)
         {
-            return ECCurve.Create(new Oid(oidValue, oidFriendlyName));
+            Oid oid = null;
+
+            if (oidValue == null && oidFriendlyName != null)
+            {
+                try
+                {
+                    oid = Oid.FromFriendlyName(oidFriendlyName, OidGroup.PublicKeyAlgorithm);
+                }
+                catch (CryptographicException)
+                {
+                }
+            }
+
+            oid ??= new Oid(oidValue, oidFriendlyName);
+            return ECCurve.Create(oid);
         }
 
         public bool IsPrime
index ddc8251..22807e8 100644 (file)
@@ -256,6 +256,25 @@ namespace System.Security.Cryptography.EcDsa.OpenSsl.Tests
             Assert.Equal(ECDSA_P256_OID_VALUE, param.Curve.Oid.Value);
         }
 
+        [Theory]
+        [InlineData("ECDSA_P521")]
+        [InlineData("ECDSA_P384")]
+        [InlineData("ECDSA_P256")]
+        public void LookupCurveByOidWindowsFriendlyName(string friendlyName)
+        {
+            ECDsaOpenSsl ec = new ECDsaOpenSsl(ECCurve.CreateFromFriendlyName(friendlyName));
+            ECParameters param = ec.ExportParameters(false);
+            param.Validate();
+        }
+
+        [Fact]
+        public void LookupCurveByOidWithInvalidThrowsPlatformNotSupported()
+        {
+            Assert.Throws<PlatformNotSupportedException>(() => {
+                new ECDsaOpenSsl(ECCurve.CreateFromFriendlyName("Invalid"));
+            });
+        }
+
         [Fact]
         public void LookupCurveByOidFriendlyName()
         {