Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tests are not applicable for .NET Framework
  • Loading branch information
vcsjones committed Jan 4, 2023
commit 02eb28a41161a9d9fbda9300c88a499d9ac75a05
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Formats.Asn1;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
Expand Down Expand Up @@ -1612,49 +1611,6 @@ public static void Decode_CanDecodeWithAttributeCertificate()
cms.CheckSignature(verifySignatureOnly: true);
}

[Fact]
public static void AddCertificate_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();

signedCms.Decode(SignedDocuments.CmsWithAttributeCertificate);
signedCms.CheckSignature(true);

int countBefore = CountCertificateChoices(SignedDocuments.CmsWithAttributeCertificate);
int certCount = signedCms.Certificates.Count;

using (ECDsa ec = ECDsa.Create(ECCurve.NamedCurves.nistP256))
{
CertificateRequest req = new("CN=test", ec, HashAlgorithmName.SHA256);

using (X509Certificate2 cert = req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now))
{
signedCms.AddCertificate(cert);
byte[] reEncoded = signedCms.Encode();
int countAfter = CountCertificateChoices(reEncoded);
Assert.Equal(countBefore + 1, countAfter);

signedCms = new SignedCms();
signedCms.Decode(reEncoded);
signedCms.CheckSignature(true);
}
}
}

[Fact]
public static void RemoveCertificate_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();

signedCms.Decode(SignedDocuments.CmsWithAttributeCertificate);
int countBefore = CountCertificateChoices(SignedDocuments.CmsWithAttributeCertificate);

signedCms.RemoveCertificate(signedCms.Certificates[0]);
byte[] reEncoded = signedCms.Encode();
int countAfter = CountCertificateChoices(reEncoded);
Assert.Equal(countBefore - 1, countAfter);
}

private static void CheckNoSignature(byte[] encoded, bool badOid=false)
{
SignedCms cms = new SignedCms();
Expand All @@ -1674,36 +1630,5 @@ private static void CheckNoSignature(byte[] encoded, bool badOid=false)
cms.CheckHash();
}
}

public static int CountCertificateChoices(byte[] encoded)
{
AsnReader reader = new AsnReader(encoded, AsnEncodingRules.BER);
reader = reader.ReadSequence();
reader.ReadObjectIdentifier();
reader = reader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
reader = reader.ReadSequence();

reader.ReadInteger(); // version
reader.ReadSetOf(); // digestAlgorithms
reader.ReadSequence(); // encapsulatedContentInfo

Asn1Tag expectedTag = new Asn1Tag(TagClass.ContextSpecific, 0, true); // certificates[0]

if (reader.PeekTag() == expectedTag)
{
AsnReader certs = reader.ReadSetOf(expectedTag);
int count = 0;

while (certs.HasData)
{
certs.ReadEncodedValue();
count++;
}

return count;
}

return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Test.Cryptography;
Expand Down Expand Up @@ -479,6 +480,49 @@ public static void CreateSignature_DigestAlgorithmWithSignatureOid_Prohibited()
}
}

[Fact]
public static void AddCertificate_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();

signedCms.Decode(SignedDocuments.CmsWithAttributeCertificate);
signedCms.CheckSignature(true);

int countBefore = CountCertificateChoices(SignedDocuments.CmsWithAttributeCertificate);
int certCount = signedCms.Certificates.Count;

using (ECDsa ec = ECDsa.Create(ECCurve.NamedCurves.nistP256))
{
CertificateRequest req = new("CN=test", ec, HashAlgorithmName.SHA256);

using (X509Certificate2 cert = req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now))
{
signedCms.AddCertificate(cert);
byte[] reEncoded = signedCms.Encode();
int countAfter = CountCertificateChoices(reEncoded);
Assert.Equal(countBefore + 1, countAfter);

signedCms = new SignedCms();
signedCms.Decode(reEncoded);
signedCms.CheckSignature(true);
}
}
}

[Fact]
public static void RemoveCertificate_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();

signedCms.Decode(SignedDocuments.CmsWithAttributeCertificate);
int countBefore = CountCertificateChoices(SignedDocuments.CmsWithAttributeCertificate);

signedCms.RemoveCertificate(signedCms.Certificates[0]);
byte[] reEncoded = signedCms.Encode();
int countAfter = CountCertificateChoices(reEncoded);
Assert.Equal(countBefore - 1, countAfter);
}

private static void VerifyWithExplicitPrivateKey(X509Certificate2 cert, AsymmetricAlgorithm key)
{
using (var pubCert = new X509Certificate2(cert.RawData))
Expand Down Expand Up @@ -539,5 +583,36 @@ private static void VerifyCounterSignatureWithExplicitPrivateKey(X509Certificate
Assert.Equal(counterSignerPubCert, cms.SignerInfos[0].CounterSignerInfos[0].Certificate);
}
}

private static int CountCertificateChoices(byte[] encoded)
{
AsnReader reader = new AsnReader(encoded, AsnEncodingRules.BER);
reader = reader.ReadSequence();
reader.ReadObjectIdentifier();
reader = reader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
reader = reader.ReadSequence();

reader.ReadInteger(); // version
reader.ReadSetOf(); // digestAlgorithms
reader.ReadSequence(); // encapsulatedContentInfo

Asn1Tag expectedTag = new Asn1Tag(TagClass.ContextSpecific, 0, true); // certificates[0]

if (reader.PeekTag() == expectedTag)
{
AsnReader certs = reader.ReadSetOf(expectedTag);
int count = 0;

while (certs.HasData)
{
certs.ReadEncodedValue();
count++;
}

return count;
}

return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,4 @@
<ItemGroup Condition="$(TargetFramework.StartsWith('net48'))">
<Reference Include="System.Security" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Formats.Asn1\src\System.Formats.Asn1.csproj" />
</ItemGroup>
</Project>