Skip to content

Commit

Permalink
Bring the AlgorithmIdentifier.Parameters property back
Browse files Browse the repository at this point in the history
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 dotnet/corefx@64bab98
  • Loading branch information
MaximLipnin authored and bartonjs committed Jul 11, 2018
1 parent 6225cfe commit ce99945
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>();
}
}

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down

0 comments on commit ce99945

Please sign in to comment.