public static void RemoveAllCertsAddBackSignerCert() { SignedCms cms = new SignedCms(); cms.Decode(SignedDocuments.CounterSignedRsaPkcs1OneSigner); SignerInfo signerInfoBeforeRemoval = cms.SignerInfos[0]; X509Certificate2 signerCert = signerInfoBeforeRemoval.Certificate; while (cms.Certificates.Count > 0) { cms.RemoveCertificate(cms.Certificates[0]); } // Signer info should be gone Assert.Throws <CryptographicException>(() => cms.CheckSignature(true)); Assert.Null(cms.SignerInfos[0].Certificate); Assert.NotNull(signerInfoBeforeRemoval.Certificate); cms.AddCertificate(signerCert); cms.CheckSignature(true); Assert.Equal(1, cms.Certificates.Count); }
public static void RemoveCertificate() { SignedCms cms = new SignedCms(); cms.Decode(SignedDocuments.CounterSignedRsaPkcs1OneSigner); var expectedCerts = new HashSet <X509Certificate2>(cms.Certificates.OfType <X509Certificate2>()); using (X509Certificate2 cert1 = Certificates.RSAKeyTransfer1.GetCertificate()) using (X509Certificate2 cert2 = Certificates.RSAKeyTransfer2.GetCertificate()) { Assert.NotEqual(cert1, cert2); cms.AddCertificate(cert1); cms.AddCertificate(cert2); expectedCerts.Add(cert2); cms.RemoveCertificate(cert1); Assert.Equal(expectedCerts.Count, cms.Certificates.Count); foreach (X509Certificate2 documentCert in cms.Certificates) { Assert.True(expectedCerts.Contains(documentCert)); } } }
public static void RemoveNonExistingCertificate() { SignedCms cms = new SignedCms(); cms.Decode(SignedDocuments.CounterSignedRsaPkcs1OneSigner); using (X509Certificate2 certToRemove = Certificates.RSAKeyTransfer1.GetCertificate()) { Assert.Throws <CryptographicException>(() => cms.RemoveCertificate(certToRemove)); } }