Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/7.0] Fix SignedCms certificate collection modification with attribute certificates #80188

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<NoWarn>$(NoWarn);CA5384</NoWarn>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>1</ServicingVersion>
<PackageDescription>Provides support for PKCS and CMS algorithms.

Commonly Used Types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public void AddCertificate(X509Certificate2 certificate)
{
foreach (CertificateChoiceAsn cert in _signedData.CertificateSet!)
{
if (cert.Certificate!.Value.Span.SequenceEqual(rawData))
if (cert.Certificate is not null && cert.Certificate.Value.Span.SequenceEqual(rawData))
{
throw new CryptographicException(SR.Cryptography_Cms_CertificateAlreadyInCollection);
}
Expand Down Expand Up @@ -721,7 +721,7 @@ public void RemoveCertificate(X509Certificate2 certificate)

foreach (CertificateChoiceAsn cert in _signedData.CertificateSet!)
{
if (cert.Certificate!.Value.Span.SequenceEqual(rawData))
if (cert.Certificate is not null && cert.Certificate.Value.Span.SequenceEqual(rawData))
{
PkcsHelpers.RemoveAt(ref _signedData.CertificateSet, idx);
Reencode();
Expand Down
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 @@ -604,6 +605,92 @@ public static void CreateSignature_Ecdsa_ThrowsWithRsaSignaturePadding()
}
}

[Fact]
public static void AddCertificate_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();
signedCms.Decode(SignedDocuments.TstWithAttributeCertificate);
signedCms.CheckSignature(true);

int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate);

using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.GetCertificate())
{
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_Existing_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();
signedCms.Decode(SignedDocuments.TstWithAttributeCertificate);
int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate);

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

[Fact]
public static void RemoveCertificate_NonExisting_CollectionContainsAttributeCertificate()
{
SignedCms signedCms = new SignedCms();
signedCms.Decode(SignedDocuments.TstWithAttributeCertificate);

using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.GetCertificate())
{
// Remove a non-existing certificate so that we are forced to enumerate the entire 'certificates[0]'
// collection (including attribute certificates) looking for it.
Assert.Throws<CryptographicException>(() => signedCms.RemoveCertificate(cert));
}
}

[Fact]
public static void ComputeCounterSignature_PreservesAttributeCertificate()
{
SignedCms signedCms = new SignedCms();
signedCms.Decode(SignedDocuments.TstWithAttributeCertificate);
int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate);

using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.TryGetCertificateWithPrivateKey())
{
CmsSigner signer = new CmsSigner(cert);
SignerInfo info = signedCms.SignerInfos[0];
info.ComputeCounterSignature(signer);
}

byte[] encoded = signedCms.Encode();
int countAfter = CountCertificateChoices(encoded);
Assert.Equal(countBefore + 1, countAfter);
}

[Fact]
public static void ComputeSignature_PreservesAttributeCertificate()
{
SignedCms signedCms = new SignedCms();
signedCms.Decode(SignedDocuments.TstWithAttributeCertificate);
int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate);

using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.TryGetCertificateWithPrivateKey())
{
CmsSigner signer = new CmsSigner(cert);
signedCms.ComputeSignature(signer);
}

byte[] encoded = signedCms.Encode();
int countAfter = CountCertificateChoices(encoded);
Assert.Equal(countBefore + 1, countAfter);
}

private static void VerifyWithExplicitPrivateKey(X509Certificate2 cert, AsymmetricAlgorithm key)
{
using (var pubCert = new X509Certificate2(cert.RawData))
Expand Down Expand Up @@ -664,5 +751,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;
}
}
}