Bring the AlgorithmIdentifier.Parameters property back
authorMaxim Lipnin <mlipnin@gmail.com>
Wed, 11 Jul 2018 13:18:23 +0000 (16:18 +0300)
committerJeremy Barton <jbarton@microsoft.com>
Wed, 11 Jul 2018 13:18:23 +0000 (06:18 -0700)
Add AlgorithmIdentifier.Parameters, but always set it to the empty array.

There aren't any really understood scenarios for why someone would want to read the algorithm parameters.  Combined with .NET Framework only assigning a non-empty value some of the time, never assigning the correct value, and not having a consistent incorrect value, the empty array seemed the best thing for now.

Commit migrated from https://github.com/dotnet/corefx/commit/64bab987f50b399b78cc1dc23a8e42325e5e175a

src/libraries/System.Security.Cryptography.Pkcs/ref/System.Security.Cryptography.Pkcs.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/AlgorithmIdentifier.cs
src/libraries/System.Security.Cryptography.Pkcs/tests/AlgorithmIdentifierTest.cs [new file with mode: 0644]
src/libraries/System.Security.Cryptography.Pkcs/tests/System.Security.Cryptography.Pkcs.Tests.csproj

index 79ecb12..90ffa14 100644 (file)
@@ -48,6 +48,7 @@ namespace System.Security.Cryptography.Pkcs
         public AlgorithmIdentifier(System.Security.Cryptography.Oid oid, int keyLength) { }
         public int KeyLength { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
         public System.Security.Cryptography.Oid Oid { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
+        public byte[] Parameters { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
     }
     public sealed partial class CmsRecipient
     {
index 4d0526d..3f57bc6 100644 (file)
@@ -30,6 +30,8 @@ namespace System.Security.Cryptography.Pkcs
         public Oid Oid { get; set; }
 
         public int KeyLength { get; set; }
+
+        public byte[] Parameters { get; set; } = Array.Empty<byte>();
     }
 }
 
diff --git a/src/libraries/System.Security.Cryptography.Pkcs/tests/AlgorithmIdentifierTest.cs b/src/libraries/System.Security.Cryptography.Pkcs/tests/AlgorithmIdentifierTest.cs
new file mode 100644 (file)
index 0000000..f7647d7
--- /dev/null
@@ -0,0 +1,113 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Test.Cryptography;
+using Xunit;
+
+namespace System.Security.Cryptography.Pkcs.Tests
+{
+    public static class AlgorithmIdentifierTest
+    {
+        [Fact]
+        public static void ParameterlessConstructor()
+        {
+            AlgorithmIdentifier ai = new AlgorithmIdentifier();
+            Assert.Equal(0, ai.KeyLength);
+            Assert.Equal(Oids.TripleDesCbc, ai.Oid.Value);
+            Assert.NotNull(ai.Parameters);
+            Assert.Equal(0, ai.Parameters.Length);
+        }
+
+        [Fact]
+        public static void ConstructorTakesOid()
+        {
+            Oid o = new Oid(Oids.Rsa);
+            AlgorithmIdentifier ai = new AlgorithmIdentifier(o);
+            Assert.Equal(0, ai.KeyLength);
+            Assert.Equal(Oids.Rsa, ai.Oid.Value);
+            Assert.NotNull(ai.Parameters);
+            Assert.Equal(0, ai.Parameters.Length);
+        }
+
+        [Fact]
+        public static void ConstructorTakesNullOid()
+        {
+            AlgorithmIdentifier ai = new AlgorithmIdentifier(null);
+            Assert.Null(ai.Oid);
+            Assert.Equal(0, ai.KeyLength);
+            Assert.NotNull(ai.Parameters);
+            Assert.Equal(0, ai.Parameters.Length);
+        }
+
+        [Fact]
+        public static void ConstructorTakesOidAndKeyLength()
+        {
+            Oid o = new Oid(Oids.Rsa);
+            AlgorithmIdentifier ai = new AlgorithmIdentifier(o, 128);
+            Assert.Equal(128, ai.KeyLength);
+            Assert.Equal(Oids.Rsa, ai.Oid.Value);
+            Assert.NotNull(ai.Parameters);
+            Assert.Equal(0, ai.Parameters.Length);
+        }
+
+        [Fact]
+        public static void ConstructorTakesNullOidAndKeyLength()
+        {
+            AlgorithmIdentifier ai = new AlgorithmIdentifier(null, 128);
+            Assert.Null(ai.Oid);
+            Assert.Equal(128, ai.KeyLength);
+            Assert.NotNull(ai.Parameters);
+            Assert.Equal(0, ai.Parameters.Length);
+        }
+
+        [Fact]
+        public static void ConstructorTakesOidAndNegativeKeyLength()
+        {
+            Oid o = new Oid(Oids.Rsa);
+            AlgorithmIdentifier ai = new AlgorithmIdentifier(o, -1);
+            Assert.Equal(-1, ai.KeyLength);
+            Assert.Equal(Oids.Rsa, ai.Oid.Value);
+            Assert.NotNull(ai.Parameters);
+            Assert.Equal(0, ai.Parameters.Length);
+        }
+
+        [Fact]
+        public static void KeyLength()
+        {
+            AlgorithmIdentifier ai = new AlgorithmIdentifier
+            {
+                KeyLength = int.MaxValue
+            };
+            Assert.Equal(int.MaxValue, ai.KeyLength);
+            ai.KeyLength = 0;
+            Assert.Equal(0, ai.KeyLength);
+            ai.KeyLength = int.MinValue;
+            Assert.Equal(int.MinValue, ai.KeyLength);
+        }
+
+        [Fact]
+        public static void Oid()
+        {
+            AlgorithmIdentifier ai = new AlgorithmIdentifier
+            {
+                Oid = new Oid(Oids.Rsa)
+            };
+            Assert.Equal(Oids.Rsa, ai.Oid.Value);
+            ai.Oid = null;
+            Assert.Null(ai.Oid);
+        }
+
+        [Fact]
+        public static void Parameters()
+        {
+            AlgorithmIdentifier ai = new AlgorithmIdentifier
+            {
+                Parameters = new byte[2] { 0x05, 0x00 } // ASN.1 NULL
+            };
+            Assert.Equal("0500", ai.Parameters.ByteArrayToHex());
+            ai.Parameters = null;
+            Assert.Null(ai.Parameters);
+        }
+    }
+}
index db9a8da..334489a 100644 (file)
@@ -12,6 +12,7 @@
     <Compile Include="$(CommonTestPath)\System\Security\Cryptography\ByteUtils.cs">
       <Link>CommonTest\System\Security\Cryptography\ByteUtils.cs</Link>
     </Compile>
+    <Compile Include="AlgorithmIdentifierTest.cs" />
     <Compile Include="Certificates.cs" />
     <Compile Include="CertLoader.cs" />
     <Compile Include="CertLoader.Settings.cs" />