diff --git a/src/libraries/System.Security.Cryptography.Pkcs/ref/System.Security.Cryptography.Pkcs.cs b/src/libraries/System.Security.Cryptography.Pkcs/ref/System.Security.Cryptography.Pkcs.cs index 79ecb125bbcd2..90ffa1454feeb 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/ref/System.Security.Cryptography.Pkcs.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/ref/System.Security.Cryptography.Pkcs.cs @@ -48,6 +48,7 @@ public AlgorithmIdentifier(System.Security.Cryptography.Oid oid) { } 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 { diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/AlgorithmIdentifier.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/AlgorithmIdentifier.cs index 4d0526dc472af..3f57bc67f5ca7 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/AlgorithmIdentifier.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/AlgorithmIdentifier.cs @@ -30,6 +30,8 @@ public AlgorithmIdentifier(Oid oid, int keyLength) public Oid Oid { get; set; } public int KeyLength { get; set; } + + public byte[] Parameters { get; set; } = Array.Empty(); } } 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 index 0000000000000..f7647d7207ce4 --- /dev/null +++ b/src/libraries/System.Security.Cryptography.Pkcs/tests/AlgorithmIdentifierTest.cs @@ -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); + } + } +} diff --git a/src/libraries/System.Security.Cryptography.Pkcs/tests/System.Security.Cryptography.Pkcs.Tests.csproj b/src/libraries/System.Security.Cryptography.Pkcs/tests/System.Security.Cryptography.Pkcs.Tests.csproj index db9a8da869c69..334489aa5e7a3 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/tests/System.Security.Cryptography.Pkcs.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.Pkcs/tests/System.Security.Cryptography.Pkcs.Tests.csproj @@ -12,6 +12,7 @@ CommonTest\System\Security\Cryptography\ByteUtils.cs +