|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System.Collections.Generic; |
| 5 | +using System.Formats.Asn1; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Security.Cryptography.X509Certificates; |
7 | 8 | using Test.Cryptography; |
@@ -604,6 +605,55 @@ public static void CreateSignature_Ecdsa_ThrowsWithRsaSignaturePadding() |
604 | 605 | } |
605 | 606 | } |
606 | 607 |
|
| 608 | + [Fact] |
| 609 | + public static void AddCertificate_CollectionContainsAttributeCertificate() |
| 610 | + { |
| 611 | + SignedCms signedCms = new SignedCms(); |
| 612 | + signedCms.Decode(SignedDocuments.TstWithAttributeCertificate); |
| 613 | + signedCms.CheckSignature(true); |
| 614 | + |
| 615 | + int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate); |
| 616 | + |
| 617 | + using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.GetCertificate()) |
| 618 | + { |
| 619 | + signedCms.AddCertificate(cert); |
| 620 | + byte[] reEncoded = signedCms.Encode(); |
| 621 | + int countAfter = CountCertificateChoices(reEncoded); |
| 622 | + Assert.Equal(countBefore + 1, countAfter); |
| 623 | + |
| 624 | + signedCms = new SignedCms(); |
| 625 | + signedCms.Decode(reEncoded); |
| 626 | + signedCms.CheckSignature(true); |
| 627 | + } |
| 628 | + } |
| 629 | + |
| 630 | + [Fact] |
| 631 | + public static void RemoveCertificate_Existing_CollectionContainsAttributeCertificate() |
| 632 | + { |
| 633 | + SignedCms signedCms = new SignedCms(); |
| 634 | + signedCms.Decode(SignedDocuments.TstWithAttributeCertificate); |
| 635 | + int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate); |
| 636 | + |
| 637 | + signedCms.RemoveCertificate(signedCms.Certificates[0]); |
| 638 | + byte[] reEncoded = signedCms.Encode(); |
| 639 | + int countAfter = CountCertificateChoices(reEncoded); |
| 640 | + Assert.Equal(countBefore - 1, countAfter); |
| 641 | + } |
| 642 | + |
| 643 | + [Fact] |
| 644 | + public static void RemoveCertificate_NonExisting_CollectionContainsAttributeCertificate() |
| 645 | + { |
| 646 | + SignedCms signedCms = new SignedCms(); |
| 647 | + signedCms.Decode(SignedDocuments.TstWithAttributeCertificate); |
| 648 | + |
| 649 | + using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.GetCertificate()) |
| 650 | + { |
| 651 | + // Remove a non-existing certificate so that we are forced to enumerate the entire 'certificates[0]' |
| 652 | + // collection (including attribute certificates) looking for it. |
| 653 | + Assert.Throws<CryptographicException>(() => signedCms.RemoveCertificate(cert)); |
| 654 | + } |
| 655 | + } |
| 656 | + |
607 | 657 | private static void VerifyWithExplicitPrivateKey(X509Certificate2 cert, AsymmetricAlgorithm key) |
608 | 658 | { |
609 | 659 | using (var pubCert = new X509Certificate2(cert.RawData)) |
@@ -664,5 +714,36 @@ private static void VerifyCounterSignatureWithExplicitPrivateKey(X509Certificate |
664 | 714 | Assert.Equal(counterSignerPubCert, cms.SignerInfos[0].CounterSignerInfos[0].Certificate); |
665 | 715 | } |
666 | 716 | } |
| 717 | + |
| 718 | + private static int CountCertificateChoices(byte[] encoded) |
| 719 | + { |
| 720 | + AsnReader reader = new AsnReader(encoded, AsnEncodingRules.BER); |
| 721 | + reader = reader.ReadSequence(); |
| 722 | + reader.ReadObjectIdentifier(); |
| 723 | + reader = reader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); |
| 724 | + reader = reader.ReadSequence(); |
| 725 | + |
| 726 | + reader.ReadInteger(); // version |
| 727 | + reader.ReadSetOf(); // digestAlgorithms |
| 728 | + reader.ReadSequence(); // encapsulatedContentInfo |
| 729 | + |
| 730 | + Asn1Tag expectedTag = new Asn1Tag(TagClass.ContextSpecific, 0, true); // certificates[0] |
| 731 | + |
| 732 | + if (reader.PeekTag() == expectedTag) |
| 733 | + { |
| 734 | + AsnReader certs = reader.ReadSetOf(expectedTag); |
| 735 | + int count = 0; |
| 736 | + |
| 737 | + while (certs.HasData) |
| 738 | + { |
| 739 | + certs.ReadEncodedValue(); |
| 740 | + count++; |
| 741 | + } |
| 742 | + |
| 743 | + return count; |
| 744 | + } |
| 745 | + |
| 746 | + return 0; |
| 747 | + } |
667 | 748 | } |
668 | 749 | } |
0 commit comments