|
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,92 @@ 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 | + |
| 657 | + [Fact] |
| 658 | + public static void ComputeCounterSignature_PreservesAttributeCertificate() |
| 659 | + { |
| 660 | + SignedCms signedCms = new SignedCms(); |
| 661 | + signedCms.Decode(SignedDocuments.TstWithAttributeCertificate); |
| 662 | + int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate); |
| 663 | + |
| 664 | + using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.TryGetCertificateWithPrivateKey()) |
| 665 | + { |
| 666 | + CmsSigner signer = new CmsSigner(cert); |
| 667 | + SignerInfo info = signedCms.SignerInfos[0]; |
| 668 | + info.ComputeCounterSignature(signer); |
| 669 | + } |
| 670 | + |
| 671 | + byte[] encoded = signedCms.Encode(); |
| 672 | + int countAfter = CountCertificateChoices(encoded); |
| 673 | + Assert.Equal(countBefore + 1, countAfter); |
| 674 | + } |
| 675 | + |
| 676 | + [Fact] |
| 677 | + public static void ComputeSignature_PreservesAttributeCertificate() |
| 678 | + { |
| 679 | + SignedCms signedCms = new SignedCms(); |
| 680 | + signedCms.Decode(SignedDocuments.TstWithAttributeCertificate); |
| 681 | + int countBefore = CountCertificateChoices(SignedDocuments.TstWithAttributeCertificate); |
| 682 | + |
| 683 | + using (X509Certificate2 cert = Certificates.RSA2048SignatureOnly.TryGetCertificateWithPrivateKey()) |
| 684 | + { |
| 685 | + CmsSigner signer = new CmsSigner(cert); |
| 686 | + signedCms.ComputeSignature(signer); |
| 687 | + } |
| 688 | + |
| 689 | + byte[] encoded = signedCms.Encode(); |
| 690 | + int countAfter = CountCertificateChoices(encoded); |
| 691 | + Assert.Equal(countBefore + 1, countAfter); |
| 692 | + } |
| 693 | + |
607 | 694 | private static void VerifyWithExplicitPrivateKey(X509Certificate2 cert, AsymmetricAlgorithm key) |
608 | 695 | { |
609 | 696 | using (var pubCert = new X509Certificate2(cert.RawData)) |
@@ -664,5 +751,36 @@ private static void VerifyCounterSignatureWithExplicitPrivateKey(X509Certificate |
664 | 751 | Assert.Equal(counterSignerPubCert, cms.SignerInfos[0].CounterSignerInfos[0].Certificate); |
665 | 752 | } |
666 | 753 | } |
| 754 | + |
| 755 | + private static int CountCertificateChoices(byte[] encoded) |
| 756 | + { |
| 757 | + AsnReader reader = new AsnReader(encoded, AsnEncodingRules.BER); |
| 758 | + reader = reader.ReadSequence(); |
| 759 | + reader.ReadObjectIdentifier(); |
| 760 | + reader = reader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 0)); |
| 761 | + reader = reader.ReadSequence(); |
| 762 | + |
| 763 | + reader.ReadInteger(); // version |
| 764 | + reader.ReadSetOf(); // digestAlgorithms |
| 765 | + reader.ReadSequence(); // encapsulatedContentInfo |
| 766 | + |
| 767 | + Asn1Tag expectedTag = new Asn1Tag(TagClass.ContextSpecific, 0, true); // certificates[0] |
| 768 | + |
| 769 | + if (reader.PeekTag() == expectedTag) |
| 770 | + { |
| 771 | + AsnReader certs = reader.ReadSetOf(expectedTag); |
| 772 | + int count = 0; |
| 773 | + |
| 774 | + while (certs.HasData) |
| 775 | + { |
| 776 | + certs.ReadEncodedValue(); |
| 777 | + count++; |
| 778 | + } |
| 779 | + |
| 780 | + return count; |
| 781 | + } |
| 782 | + |
| 783 | + return 0; |
| 784 | + } |
667 | 785 | } |
668 | 786 | } |
0 commit comments