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/6.0] Fix SignedCms certificate collection modification with attribute certificates #80209

Merged
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 @@ -6,7 +6,8 @@
<Nullable>enable</Nullable>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);netcoreapp3.1-windows;netcoreapp3.1;netstandard2.1-windows;netstandard2.1;netstandard2.0-windows;netstandard2.0;net461-windows</TargetFrameworks>
<IsPackable>true</IsPackable>
<ServicingVersion>1</ServicingVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>2</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 @@ -677,7 +677,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 @@ -712,7 +712,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 @@ -479,6 +480,92 @@ public static void CreateSignature_DigestAlgorithmWithSignatureOid_Prohibited()
}
}

[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 @@ -539,5 +626,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;
}
}
}