Beispiel #1
0
        public void TestEncapsulatedSignerStoreReplacement()
        {
            MemoryStream bOut = new MemoryStream();

            IX509Store x509Certs = CmsTestUtil.MakeCertStore(OrigCert, SignCert);

            CmsSignedDataStreamGenerator gen = new CmsSignedDataStreamGenerator();

            gen.AddSigner(OrigKP.Private, OrigCert, CmsSignedDataStreamGenerator.DigestSha1);
            gen.AddCertificates(x509Certs);

            byte[] testBytes = Encoding.ASCII.GetBytes(TestMessage);

            Stream sigOut = gen.Open(bOut, true);

            sigOut.Write(testBytes, 0, testBytes.Length);
            sigOut.Close();

            //
            // create new Signer
            //
            MemoryStream original = new MemoryStream(bOut.ToArray(), false);

            bOut.SetLength(0);

            gen = new CmsSignedDataStreamGenerator();
            gen.AddSigner(OrigKP.Private, OrigCert, CmsSignedDataStreamGenerator.DigestSha224);
            gen.AddCertificates(x509Certs);

            sigOut = gen.Open(bOut, true);
            sigOut.Write(testBytes, 0, testBytes.Length);
            sigOut.Close();

            CmsSignedData sd = new CmsSignedData(bOut.ToArray());

            //
            // replace signer
            //
            MemoryStream newOut = new MemoryStream();

            CmsSignedDataParser.ReplaceSigners(original, sd.GetSignerInfos(), newOut);

            sd = new CmsSignedData(newOut.ToArray());

            IEnumerator signerEnum = sd.GetSignerInfos().GetSigners().GetEnumerator();

            signerEnum.MoveNext();
            SignerInformation signer = (SignerInformation)signerEnum.Current;

            Assert.AreEqual(signer.DigestAlgOid, CmsSignedDataStreamGenerator.DigestSha224);

            CmsSignedDataParser sp = new CmsSignedDataParser(newOut.ToArray());

            sp.GetSignedContent().Drain();

            VerifySignatures(sp);
        }
        public void TestSignerStoreReplacement()
        {
            IList        certList = new ArrayList();
            MemoryStream bOut     = new MemoryStream();

            byte[] data = Encoding.ASCII.GetBytes(TestMessage);

            certList.Add(OrigCert);
            certList.Add(SignCert);

            IX509Store x509Certs = X509StoreFactory.Create(
                "Certificate/Collection",
                new X509CollectionStoreParameters(certList));

            CmsSignedDataStreamGenerator gen = new CmsSignedDataStreamGenerator();

            gen.AddSigner(OrigKP.Private, OrigCert, CmsSignedDataStreamGenerator.DigestSha1);

            gen.AddCertificates(x509Certs);

            Stream sigOut = gen.Open(bOut, false);

            sigOut.Write(data, 0, data.Length);

            sigOut.Close();

            CheckSigParseable(bOut.ToArray());

            //
            // create new Signer
            //
            MemoryStream original = new MemoryStream(bOut.ToArray(), false);

            bOut.SetLength(0);

            gen = new CmsSignedDataStreamGenerator();

            gen.AddSigner(OrigKP.Private, OrigCert, CmsSignedDataStreamGenerator.DigestSha224);

            gen.AddCertificates(x509Certs);

            sigOut = gen.Open(bOut);

            sigOut.Write(data, 0, data.Length);

            sigOut.Close();

            CheckSigParseable(bOut.ToArray());

            CmsSignedData sd = new CmsSignedData(bOut.ToArray());

            //
            // replace signer
            //
            MemoryStream newOut = new MemoryStream();

            CmsSignedDataParser.ReplaceSigners(original, sd.GetSignerInfos(), newOut);

            sd = new CmsSignedData(new CmsProcessableByteArray(data), newOut.ToArray());

            IEnumerator signerEnum = sd.GetSignerInfos().GetSigners().GetEnumerator();

            signerEnum.MoveNext();
            SignerInformation signer = (SignerInformation)signerEnum.Current;

            Assert.AreEqual(signer.DigestAlgOid, CmsSignedDataStreamGenerator.DigestSha224);

            CmsSignedDataParser sp = new CmsSignedDataParser(new CmsTypedStream(
                                                                 new MemoryStream(data, false)), newOut.ToArray());

            sp.GetSignedContent().Drain();

            VerifySignatures(sp);
        }