Example #1
0
            public override void Close()
            {
                _out.Close();

                // TODO Parent context(s) should really be be closed explicitly

                _eiGen.Close();
                _cGen.Close();
                _sGen.Close();
                base.Close();
            }
Example #2
0
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(_out);

                    // TODO Parent context(s) should really be be closed explicitly

                    _eiGen.Close();
                    _cGen.Close();
                    _sGen.Close();
                }
                base.Dispose(disposing);
            }
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    Platform.Dispose(_out);

                    // TODO Parent context(s) should really be be closed explicitly

                    _eiGen.Close();
                    _cGen.Close();
                    _sGen.Close();
                }
                base.Dispose(disposing);
            }
Example #4
0
            protected override void Dispose(bool disposing)
            {
                try
                {
                    if (!disposing)
                    {
                        return;
                    }

                    macStream.Dispose();

                    // TODO Parent context(s) should really be be closed explicitly

                    eiGen.Close();

                    // [TODO] auth attributes go here
                    byte[] macOctets = MacUtilities.DoFinal(mac);
                    authGen.AddObject(new DerOctetString(macOctets));
                    // [TODO] unauth attributes go here

                    authGen.Close();
                    cGen.Close();
                }
                finally
                {
                    base.Dispose(disposing);
                }
            }
Example #5
0
            public override void Close()
            {
                macStream.Dispose();

                // TODO Parent context(s) should really be be closed explicitly

                eiGen.Close();

                // [TODO] auth attributes go here
                byte[] macOctets = MacUtilities.DoFinal(mac);
                authGen.AddObject(new DerOctetString(macOctets));
                // [TODO] unauth attributes go here

                authGen.Close();
                cGen.Close();
            }
Example #6
0
            public override void Close()
            {
                _out.Close();
                _eiGen.Close();

                outer._digests.Clear();                    // clear the current preserved digest state

                if (outer._certs.Count > 0)
                {
                    Asn1Set certs = CmsUtilities.CreateDerSetFromList(outer._certs);

                    WriteToGenerator(_sigGen, new DerTaggedObject(false, 0, certs));
                }

                if (outer._crls.Count > 0)
                {
                    Asn1Set crls = CmsUtilities.CreateDerSetFromList(outer._crls);

                    WriteToGenerator(_sigGen, new DerTaggedObject(false, 1, crls));
                }

                //
                // add the precalculated SignerInfo objects.
                //
                Asn1EncodableVector signerInfos = new Asn1EncodableVector();

                foreach (SignerInformation signer in outer._signers)
                {
                    signerInfos.Add(signer.ToSignerInfo());
                }

                //
                // add the SignerInfo objects
                //
                foreach (SignerInf signer in outer._signerInfs)
                {
                    try
                    {
                        signerInfos.Add(signer.ToSignerInfo(_contentOID));
                    }
                    catch (IOException e)
                    {
                        throw new IOException("encoding error." + e);
                    }
                    catch (SignatureException e)
                    {
                        throw new IOException("error creating signature." + e);
                    }
                    catch (CertificateEncodingException e)
                    {
                        throw new IOException("error creating sid." + e);
                    }
                }

                WriteToGenerator(_sigGen, new DerSet(signerInfos));

                _sigGen.Close();
                _sGen.Close();
                base.Close();
            }
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _out.Dispose();

                    // TODO Parent context(s) should really be be closed explicitly

                    _eiGen.Close();

                    if (_outer.unprotectedAttributeGenerator != null)
                    {
                        Asn1.Cms.AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());

                        Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());

                        _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
                    }

                    _envGen.Close();
                    _cGen.Close();
                }

                base.Dispose(disposing);
            }
Example #8
0
 public override void Close()
 {
     Platform.Dispose(_out);
     _eiGen.Close();
     _cGen.Close();
     _sGen.Close();
     base.Close();
 }
 public override void Close()
 {
     //_out.Close();
     _eiGen.Close();
     _cGen.Close();
     _sGen.Close();
     base.Close();
 }
 public override void Close()
 {
     Platform.Dispose((Stream)(object)_out);
     _eiGen.Close();
     _cGen.Close();
     _sGen.Close();
     base.Close();
 }
 public override void Close()
 {
     Platform.Dispose(macStream);
     eiGen.Close();
     byte[] str = MacUtilities.DoFinal(mac);
     authGen.AddObject(new DerOctetString(str));
     authGen.Close();
     cGen.Close();
     base.Close();
 }
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(macStream);

                    // TODO Parent context(s) should really be be closed explicitly

                    eiGen.Close();

                    // [TODO] auth attributes go here
                    byte[] macOctets = MacUtilities.DoFinal(mac);
                    authGen.AddObject(new DerOctetString(macOctets));
                    // [TODO] unauth attributes go here

                    authGen.Close();
                    cGen.Close();
                }
                base.Dispose(disposing);
            }
            public override void Close()
            {
                _out.Close();
                _eiGen.Close();

                // [TODO] unprotected attributes go here

                _envGen.Close();
                _cGen.Close();
                base.Close();
            }
Example #14
0
        public void TestBerExplicitTaggedSequenceWriting()
        {
            MemoryStream         bOut   = new MemoryStream();
            BerSequenceGenerator seqGen = new BerSequenceGenerator(bOut, 1, true);

            seqGen.AddObject(new DerInteger(BigInteger.Zero));

            seqGen.AddObject(new DerObjectIdentifier("1.1"));

            seqGen.Close();

            Assert.IsTrue(Arrays.AreEqual(berExpTagSeqData, bOut.ToArray()), "explicit BER tag writing test failed.");
        }
Example #15
0
            public override void Close()
            {
                macStream.Close();
                eiGen.Close();

                // [TODO] auth attributes go here
                byte[] macOctets = MacUtilities.DoFinal(macStream.WriteMac());
                authGen.AddObject(new DerOctetString(macOctets));
                // [TODO] unauth attributes go here

                authGen.Close();
                cGen.Close();
            }
 public override void Close()
 {
     Platform.Dispose((Stream)(object)_out);
     _eiGen.Close();
     if (_outer.unprotectedAttributeGenerator != null)
     {
         Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());
         Asn1Set obj = new BerSet(attributes.ToAsn1EncodableVector());
         _envGen.AddObject(new DerTaggedObject(explicitly: false, 1, obj));
     }
     _envGen.Close();
     _cGen.Close();
     base.Close();
 }
Example #17
0
        public void TestReadingWritingNested()
        {
            MemoryStream            bOut   = new MemoryStream();
            BerSequenceGenerator    sGen   = new BerSequenceGenerator(bOut);
            BerOctetStringGenerator octGen = new BerOctetStringGenerator(sGen.GetRawOutputStream());

            Stream outStream = octGen.GetOctetOutputStream();

            BerSequenceGenerator inSGen = new BerSequenceGenerator(outStream);

            BerOctetStringGenerator inOctGen = new BerOctetStringGenerator(inSGen.GetRawOutputStream());

            Stream inOut = inOctGen.GetOctetOutputStream();

            inOut.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
            inOut.Write(new byte[10], 0, 10);

            inOut.Close();

            inSGen.Close();

            outStream.Close();

            sGen.Close();

            Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray());

            BerSequenceParser sq = (BerSequenceParser)aIn.ReadObject();

            BerOctetStringParser s = (BerOctetStringParser)sq.ReadObject();

            Asn1StreamParser aIn2 = new Asn1StreamParser(s.GetOctetStream());

            BerSequenceParser sq2 = (BerSequenceParser)aIn2.ReadObject();

            BerOctetStringParser inS = (BerOctetStringParser)sq2.ReadObject();

            Stream inStream = inS.GetOctetStream();
            int    count    = 0;

            while (inStream.ReadByte() >= 0)
            {
                count++;
            }

            Assert.AreEqual(14, count);
        }
Example #18
0
        private void DoClose()
        {
            Platform.Dispose(_out);
            _eiGen.Close();
            outer._digests.Clear();
            if (outer._certs.Count > 0)
            {
                Asn1Set obj = CmsUtilities.CreateBerSetFromList(outer._certs);
                WriteToGenerator(_sigGen, new BerTaggedObject(explicitly: false, 0, obj));
            }
            if (outer._crls.Count > 0)
            {
                Asn1Set obj2 = CmsUtilities.CreateBerSetFromList(outer._crls);
                WriteToGenerator(_sigGen, new BerTaggedObject(explicitly: false, 1, obj2));
            }
            IDictionaryEnumerator enumerator = outer._messageDigests.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
                    outer._messageHashes.Add(dictionaryEntry.Key, DigestUtilities.DoFinal((IDigest)dictionaryEntry.Value));
                }
            }
            finally
            {
                (enumerator as IDisposable)?.Dispose();
            }
            Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();

            foreach (DigestAndSignerInfoGeneratorHolder signerInf in outer._signerInfs)
            {
                AlgorithmIdentifier digestAlgorithm = signerInf.DigestAlgorithm;
                byte[] array = (byte[])outer._messageHashes[Helper.GetDigestAlgName(signerInf.digestOID)];
                outer._digests[signerInf.digestOID] = array.Clone();
                asn1EncodableVector.Add(signerInf.signerInf.Generate(_contentOID, digestAlgorithm, array));
            }
            foreach (SignerInformation signer in outer._signers)
            {
                asn1EncodableVector.Add(signer.ToSignerInfo());
            }
            WriteToGenerator(_sigGen, new DerSet(asn1EncodableVector));
            _sigGen.Close();
            _sGen.Close();
        }
            protected override void Dispose(bool isDisposing)
            {
                try
                {
                    _out.Dispose();

                    // TODO Parent context(s) should really be be closed explicitly

                    _eiGen.Close();
                    _cGen.Close();
                    _sGen.Close();
                }
                finally
                {
                    base.Dispose(isDisposing);
                }
            }
Example #20
0
        public void TestNestedBerWriting()
        {
            MemoryStream         bOut    = new MemoryStream();
            BerSequenceGenerator seqGen1 = new BerSequenceGenerator(bOut);

            seqGen1.AddObject(new DerInteger(BigInteger.Zero));

            seqGen1.AddObject(new DerObjectIdentifier("1.1"));

            BerSequenceGenerator seqGen2 = new BerSequenceGenerator(seqGen1.GetRawOutputStream());

            seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1)));

            seqGen2.Close();

            seqGen1.Close();

            Assert.IsTrue(Arrays.AreEqual(berNestedSeqData, bOut.ToArray()), "nested BER writing test failed.");
        }
Example #21
0
        /**
         * Replace the certificate and CRL information associated with this
         * CMSSignedData object with the new one passed in.
         * <p>
         * The output stream is returned unclosed.
         * </p>
         * @param original the signed data stream to be used as a base.
         * @param certsAndCrls the new certificates and CRLs to be used.
         * @param out the stream to Write the new signed data object to.
         * @return out.
         * @exception CmsException if there is an error processing the CertStore
         */
        public static Stream ReplaceCertificatesAndCrls(
            Stream original,
            IX509Store x509Certs,
            IX509Store x509Crls,
            IX509Store x509AttrCerts,
            Stream outStr)
        {
            if (x509AttrCerts != null)
            {
                throw new NotImplementedException("Currently can't replace attribute certificates");
            }

            Asn1StreamParser  inStr       = new Asn1StreamParser(original, CmsUtilities.MaximumMemory);
            ContentInfoParser contentInfo = new ContentInfoParser((Asn1SequenceParser)inStr.ReadObject());
            SignedDataParser  signedData  = SignedDataParser.GetInstance(contentInfo.GetContent(Asn1Tags.Sequence));

            BerSequenceGenerator sGen = new BerSequenceGenerator(outStr);

            sGen.AddObject(CmsObjectIdentifiers.SignedData);

            BerSequenceGenerator sigGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true);

            // version number
            sigGen.AddObject(signedData.Version);

            // digests
            WriteToGenerator(sigGen, signedData.GetDigestAlgorithms().ToAsn1Object());

            // encap content info
            ContentInfoParser encapContentInfo = signedData.GetEncapContentInfo();

            BerSequenceGenerator eiGen = new BerSequenceGenerator(sigGen.GetRawOutputStream());

            eiGen.AddObject(encapContentInfo.ContentType);

            Asn1OctetStringParser octs = (Asn1OctetStringParser)encapContentInfo.GetContent(Asn1Tags.OctetString);

            if (octs != null)
            {
                BerOctetStringGenerator octGen = new BerOctetStringGenerator(eiGen.GetRawOutputStream(), 0, true);
                byte[] inBuffer  = new byte[4096];
                byte[] outBuffer = new byte[4096];
                Stream inOctets  = octs.GetOctetStream();
                Stream outOctets = octGen.GetOctetOutputStream(outBuffer);

                int len;
                while ((len = inOctets.Read(inBuffer, 0, inBuffer.Length)) > 0)
                {
                    outOctets.Write(inBuffer, 0, len);
                }

                outOctets.Close();
            }

            eiGen.Close();

            //
            // skip existing certs and CRLs
            //
            Asn1SetParser set = signedData.GetCertificates();

            if (set != null)
            {
                set.ToAsn1Object();
            }

            set = signedData.GetCrls();

            if (set != null)
            {
                set.ToAsn1Object();
            }

            //
            // replace the certs and crls in the SignedData object
            //
            Asn1Set certs;

            try
            {
                certs = CmsUtilities.CreateDerSetFromList(
                    CmsUtilities.GetCertificatesFromStore(x509Certs));
            }
            catch (X509StoreException e)
            {
                throw new CmsException("error getting certs from certStore", e);
            }

            if (certs.Count > 0)
            {
                WriteToGenerator(sigGen, new DerTaggedObject(false, 0, certs));
            }

            Asn1Set crls;

            try
            {
                crls = CmsUtilities.CreateDerSetFromList(
                    CmsUtilities.GetCrlsFromStore(x509Crls));
            }
            catch (X509StoreException e)
            {
                throw new CmsException("error getting crls from certStore", e);
            }

            if (crls.Count > 0)
            {
                WriteToGenerator(sigGen, new DerTaggedObject(false, 1, crls));
            }

            WriteToGenerator(sigGen, signedData.GetSignerInfos().ToAsn1Object());

            sigGen.Close();

            sGen.Close();

            return(outStr);
        }
Example #22
0
		public void TestNestedStructure()
		{
			MemoryStream bOut = new MemoryStream();

			BerSequenceGenerator sGen = new BerSequenceGenerator(bOut);

			sGen.AddObject(new DerObjectIdentifier(CmsObjectIdentifiers.CompressedData.Id));

			BerSequenceGenerator cGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true);

			cGen.AddObject(new DerInteger(0));

			//
			// AlgorithmIdentifier
			//
			DerSequenceGenerator algGen = new DerSequenceGenerator(cGen.GetRawOutputStream());

			algGen.AddObject(new DerObjectIdentifier("1.2"));

			algGen.Close();

			//
			// Encapsulated ContentInfo
			//
			BerSequenceGenerator eiGen = new BerSequenceGenerator(cGen.GetRawOutputStream());

			eiGen.AddObject(new DerObjectIdentifier("1.1"));

			BerOctetStringGenerator octGen = new BerOctetStringGenerator(eiGen.GetRawOutputStream(), 0, true);

			//
			// output containing zeroes
			//
			Stream outStream = octGen.GetOctetOutputStream();

			outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
			outStream.Write(new byte[4], 0, 4);
			outStream.Write(new byte[20], 0, 20);

			outStream.Close();
			eiGen.Close();
			cGen.Close();
			sGen.Close();

			//
			// reading back
			//
			Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray());

			ContentInfoParser cp = new ContentInfoParser((Asn1SequenceParser)aIn.ReadObject());

			CompressedDataParser comData = new CompressedDataParser((Asn1SequenceParser)cp.GetContent(Asn1Tags.Sequence));
			ContentInfoParser content = comData.GetEncapContentInfo();

			Asn1OctetStringParser bytes = (Asn1OctetStringParser)content.GetContent(Asn1Tags.OctetString);

			Stream inStream = bytes.GetOctetStream();
			int count = 0;

			while (inStream.ReadByte() >= 0)
			{
				count++;
			}

			Assert.AreEqual(28, count);
		}
		public void TestBerExplicitTaggedSequenceWriting()
        {
            MemoryStream bOut = new MemoryStream();
            BerSequenceGenerator seqGen = new BerSequenceGenerator(bOut, 1, true);

			seqGen.AddObject(new DerInteger(BigInteger.Zero));

			seqGen.AddObject(new DerObjectIdentifier("1.1"));

			seqGen.Close();

			Assert.IsTrue(Arrays.AreEqual(berExpTagSeqData, bOut.ToArray()), "explicit BER tag writing test failed.");
        }
		public void TestNestedBerWriting()
        {
            MemoryStream bOut = new MemoryStream();
            BerSequenceGenerator seqGen1 = new BerSequenceGenerator(bOut);

			seqGen1.AddObject(new DerInteger(BigInteger.Zero));

			seqGen1.AddObject(new DerObjectIdentifier("1.1"));

			BerSequenceGenerator seqGen2 = new BerSequenceGenerator(seqGen1.GetRawOutputStream());

			seqGen2.AddObject(new DerInteger(BigInteger.ValueOf(1)));

			seqGen2.Close();

			seqGen1.Close();

			Assert.IsTrue(Arrays.AreEqual(berNestedSeqData, bOut.ToArray()), "nested BER writing test failed.");
        }
Example #25
0
        /**
         * Replace the signerinformation store associated with the passed
         * in message contained in the stream original with the new one passed in.
         * You would probably only want to do this if you wanted to change the unsigned
         * attributes associated with a signer, or perhaps delete one.
         * <p>
         * The output stream is returned unclosed.
         * </p>
         * @param original the signed data stream to be used as a base.
         * @param signerInformationStore the new signer information store to use.
         * @param out the stream to Write the new signed data object to.
         * @return out.
         */
        public static Stream ReplaceSigners(
            Stream original,
            SignerInformationStore signerInformationStore,
            Stream outStr)
        {
            Asn1StreamParser  inStr       = new Asn1StreamParser(original, CmsUtilities.MaximumMemory);
            ContentInfoParser contentInfo = new ContentInfoParser((Asn1SequenceParser)inStr.ReadObject());
            SignedDataParser  signedData  = SignedDataParser.GetInstance(contentInfo.GetContent(Asn1Tags.Sequence));

            BerSequenceGenerator sGen = new BerSequenceGenerator(outStr);

            sGen.AddObject(CmsObjectIdentifiers.SignedData);

            BerSequenceGenerator sigGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true);

            // version number
            sigGen.AddObject(signedData.Version);

            // digests
            signedData.GetDigestAlgorithms().ToAsn1Object();              // skip old ones

            Asn1EncodableVector digestAlgs = new Asn1EncodableVector();

            foreach (SignerInformation signer in signerInformationStore.GetSigners())
            {
                digestAlgs.Add(FixAlgID(signer.DigestAlgorithmID));
            }

            WriteToGenerator(sigGen, new DerSet(digestAlgs));

            // encap content info
            ContentInfoParser encapContentInfo = signedData.GetEncapContentInfo();

            BerSequenceGenerator eiGen = new BerSequenceGenerator(sigGen.GetRawOutputStream());

            eiGen.AddObject(encapContentInfo.ContentType);

            Asn1OctetStringParser octs = (Asn1OctetStringParser)encapContentInfo.GetContent(Asn1Tags.OctetString);

            if (octs != null)
            {
                BerOctetStringGenerator octGen = new BerOctetStringGenerator(
                    eiGen.GetRawOutputStream(), 0, true);
                byte[] inBuffer  = new byte[4096];
                byte[] outBuffer = new byte[4096];
                Stream inOctets  = octs.GetOctetStream();
                Stream outOctets = octGen.GetOctetOutputStream(outBuffer);

                int len;
                while ((len = inOctets.Read(inBuffer, 0, inBuffer.Length)) > 0)
                {
                    outOctets.Write(inBuffer, 0, len);
                }

                outOctets.Close();
            }

            eiGen.Close();

            {
                Asn1SetParser set = signedData.GetCertificates();

                if (set != null)
                {
                    Asn1Object       setObj    = set.ToAsn1Object();
                    Asn1TaggedObject taggedObj = (set is BerSetParser)
                                        ?       new BerTaggedObject(false, 0, setObj)
                                        :       new DerTaggedObject(false, 0, setObj);

                    WriteToGenerator(sigGen, taggedObj);
                }
            }

            {
                Asn1SetParser set = signedData.GetCrls();

                if (set != null)
                {
                    Asn1Object       setObj    = set.ToAsn1Object();
                    Asn1TaggedObject taggedObj = (set is BerSetParser)
                                        ?       new BerTaggedObject(false, 1, setObj)
                                        :       new DerTaggedObject(false, 1, setObj);

                    WriteToGenerator(sigGen, taggedObj);
                }
            }

            Asn1EncodableVector signerInfos = new Asn1EncodableVector();

            foreach (SignerInformation signer in signerInformationStore.GetSigners())
            {
                signerInfos.Add(signer.ToSignerInfo());
            }

            WriteToGenerator(sigGen, new DerSet(signerInfos));

            sigGen.Close();

            sGen.Close();

            return(outStr);
        }
            private void DoClose()
            {
                //IL_00b4: Unknown result type (might be due to invalid IL or missing references)
                //IL_00b9: Unknown result type (might be due to invalid IL or missing references)
                Platform.Dispose(_out);
                _eiGen.Close();
                outer._digests.Clear();
                if (((global::System.Collections.ICollection)outer._certs).get_Count() > 0)
                {
                    Asn1Set obj = CmsUtilities.CreateBerSetFromList(outer._certs);
                    WriteToGenerator(_sigGen, new BerTaggedObject(explicitly: false, 0, obj));
                }
                if (((global::System.Collections.ICollection)outer._crls).get_Count() > 0)
                {
                    Asn1Set obj2 = CmsUtilities.CreateBerSetFromList(outer._crls);
                    WriteToGenerator(_sigGen, new BerTaggedObject(explicitly: false, 1, obj2));
                }
                IDictionaryEnumerator enumerator = outer._messageDigests.GetEnumerator();

                try
                {
                    while (((global::System.Collections.IEnumerator)enumerator).MoveNext())
                    {
                        DictionaryEntry val = (DictionaryEntry)((global::System.Collections.IEnumerator)enumerator).get_Current();
                        outer._messageHashes.Add(((DictionaryEntry)(ref val)).get_Key(), (object)DigestUtilities.DoFinal((IDigest)((DictionaryEntry)(ref val)).get_Value()));
                    }
                }
                finally
                {
                    (enumerator as global::System.IDisposable)?.Dispose();
                }
                Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();

                global::System.Collections.IEnumerator enumerator2 = ((global::System.Collections.IEnumerable)outer._signerInfs).GetEnumerator();
                try
                {
                    while (enumerator2.MoveNext())
                    {
                        DigestAndSignerInfoGeneratorHolder digestAndSignerInfoGeneratorHolder = (DigestAndSignerInfoGeneratorHolder)enumerator2.get_Current();
                        AlgorithmIdentifier digestAlgorithm = digestAndSignerInfoGeneratorHolder.DigestAlgorithm;
                        byte[] array = (byte[])outer._messageHashes.get_Item((object)Helper.GetDigestAlgName(digestAndSignerInfoGeneratorHolder.digestOID));
                        outer._digests.set_Item((object)digestAndSignerInfoGeneratorHolder.digestOID, ((global::System.Array)array).Clone());
                        asn1EncodableVector.Add(digestAndSignerInfoGeneratorHolder.signerInf.Generate(_contentOID, digestAlgorithm, array));
                    }
                }
                finally
                {
                    global::System.IDisposable disposable = enumerator2 as global::System.IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
                enumerator2 = ((global::System.Collections.IEnumerable)outer._signers).GetEnumerator();
                try
                {
                    while (enumerator2.MoveNext())
                    {
                        SignerInformation signerInformation = (SignerInformation)enumerator2.get_Current();
                        asn1EncodableVector.Add(signerInformation.ToSignerInfo());
                    }
                }
                finally
                {
                    global::System.IDisposable disposable2 = enumerator2 as global::System.IDisposable;
                    if (disposable2 != null)
                    {
                        disposable2.Dispose();
                    }
                }
                WriteToGenerator(_sigGen, new DerSet(asn1EncodableVector));
                _sigGen.Close();
                _sGen.Close();
            }
Example #27
0
		public void TestReadingWritingNested()
		{
			MemoryStream bOut = new MemoryStream();
			BerSequenceGenerator sGen = new BerSequenceGenerator(bOut);
			BerOctetStringGenerator octGen = new BerOctetStringGenerator(sGen.GetRawOutputStream());

			Stream outStream = octGen.GetOctetOutputStream();

			BerSequenceGenerator inSGen = new BerSequenceGenerator(outStream);

			BerOctetStringGenerator inOctGen = new BerOctetStringGenerator(inSGen.GetRawOutputStream());

			Stream inOut = inOctGen.GetOctetOutputStream();

			inOut.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
			inOut.Write(new byte[10], 0, 10);

			inOut.Close();

			inSGen.Close();

			outStream.Close();

			sGen.Close();

			Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray());

			BerSequenceParser sq = (BerSequenceParser)aIn.ReadObject();

			BerOctetStringParser s = (BerOctetStringParser)sq.ReadObject();

			Asn1StreamParser aIn2 = new Asn1StreamParser(s.GetOctetStream());

			BerSequenceParser sq2 = (BerSequenceParser)aIn2.ReadObject();

			BerOctetStringParser inS = (BerOctetStringParser)sq2.ReadObject();

			Stream inStream = inS.GetOctetStream();
			int         count = 0;

			while (inStream.ReadByte() >= 0)
			{
				count++;
			}

			Assert.AreEqual(14, count);
		}
Example #28
0
        public void TestNestedStructure()
        {
            MemoryStream bOut = new MemoryStream();

            BerSequenceGenerator sGen = new BerSequenceGenerator(bOut);

            sGen.AddObject(new DerObjectIdentifier(CmsObjectIdentifiers.CompressedData.Id));

            BerSequenceGenerator cGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true);

            cGen.AddObject(new DerInteger(0));

            //
            // AlgorithmIdentifier
            //
            DerSequenceGenerator algGen = new DerSequenceGenerator(cGen.GetRawOutputStream());

            algGen.AddObject(new DerObjectIdentifier("1.2"));

            algGen.Close();

            //
            // Encapsulated ContentInfo
            //
            BerSequenceGenerator eiGen = new BerSequenceGenerator(cGen.GetRawOutputStream());

            eiGen.AddObject(new DerObjectIdentifier("1.1"));

            BerOctetStringGenerator octGen = new BerOctetStringGenerator(eiGen.GetRawOutputStream(), 0, true);

            //
            // output containing zeroes
            //
            Stream outStream = octGen.GetOctetOutputStream();

            outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
            outStream.Write(new byte[4], 0, 4);
            outStream.Write(new byte[20], 0, 20);

            outStream.Close();
            eiGen.Close();
            cGen.Close();
            sGen.Close();

            //
            // reading back
            //
            Asn1StreamParser aIn = new Asn1StreamParser(bOut.ToArray());

            ContentInfoParser cp = new ContentInfoParser((Asn1SequenceParser)aIn.ReadObject());

            CompressedDataParser comData = new CompressedDataParser((Asn1SequenceParser)cp.GetContent(Asn1Tags.Sequence));
            ContentInfoParser    content = comData.GetEncapContentInfo();

            Asn1OctetStringParser bytes = (Asn1OctetStringParser)content.GetContent(Asn1Tags.OctetString);

            Stream inStream = bytes.GetOctetStream();
            int    count    = 0;

            while (inStream.ReadByte() >= 0)
            {
                count++;
            }

            Assert.AreEqual(28, count);
        }
            private void DoClose()
            {
                Platform.Dispose(_out);

                // TODO Parent context(s) should really be be closed explicitly

                _eiGen.Close();

                outer._digests.Clear();                    // clear the current preserved digest state

                if (outer._certs.Count > 0)
                {
                    Asn1Set certs = CmsUtilities.CreateBerSetFromList(outer._certs);

                    WriteToGenerator(_sigGen, new BerTaggedObject(false, 0, certs));
                }

                if (outer._crls.Count > 0)
                {
                    Asn1Set crls = CmsUtilities.CreateBerSetFromList(outer._crls);

                    WriteToGenerator(_sigGen, new BerTaggedObject(false, 1, crls));
                }

                //
                // Calculate the digest hashes
                //
                foreach (DictionaryEntry de in outer._messageDigests)
                {
                    outer._messageHashes.Add(de.Key, DigestUtilities.DoFinal((IDigest)de.Value));
                }

                // TODO If the digest OIDs for precalculated signers weren't mixed in with
                // the others, we could fill in outer._digests here, instead of SignerInfoGenerator.Generate

                //
                // collect all the SignerInfo objects
                //
                Asn1EncodableVector signerInfos = new Asn1EncodableVector();

                //
                // add the generated SignerInfo objects
                //
                {
                    foreach (DigestAndSignerInfoGeneratorHolder holder in outer._signerInfs)
                    {
                        AlgorithmIdentifier digestAlgorithm = holder.DigestAlgorithm;

                        byte[] calculatedDigest = (byte[])outer._messageHashes[
                            Helper.GetDigestAlgName(holder.digestOID)];
                        outer._digests[holder.digestOID] = calculatedDigest.Clone();

                        signerInfos.Add(holder.signerInf.Generate(_contentOID, digestAlgorithm, calculatedDigest));
                    }
                }

                //
                // add the precalculated SignerInfo objects.
                //
                {
                    foreach (SignerInformation signer in outer._signers)
                    {
                        // TODO Verify the content type and calculated digest match the precalculated SignerInfo
//						if (!signer.ContentType.Equals(_contentOID))
//						{
//							// TODO The precalculated content type did not match - error?
//						}
//
//						byte[] calculatedDigest = (byte[])outer._digests[signer.DigestAlgOid];
//						if (calculatedDigest == null)
//						{
//							// TODO We can't confirm this digest because we didn't calculate it - error?
//						}
//						else
//						{
//							if (!Arrays.AreEqual(signer.GetContentDigest(), calculatedDigest))
//							{
//								// TODO The precalculated digest did not match - error?
//							}
//						}

                        signerInfos.Add(signer.ToSignerInfo());
                    }
                }

                WriteToGenerator(_sigGen, new DerSet(signerInfos));

                _sigGen.Close();
                _sGen.Close();
            }
            public override void Close()
            {
                _out.Close();
                _eiGen.Close();

                outer._digests.Clear();                    // clear the current preserved digest state

                if (outer._certs.Count > 0)
                {
                    Asn1Set certs = CmsUtilities.CreateBerSetFromList(outer._certs);

                    WriteToGenerator(_sigGen, new BerTaggedObject(false, 0, certs));
                }

                if (outer._crls.Count > 0)
                {
                    Asn1Set crls = CmsUtilities.CreateBerSetFromList(outer._crls);

                    WriteToGenerator(_sigGen, new BerTaggedObject(false, 1, crls));
                }

                //
                // Calculate the digest hashes
                //
                foreach (DictionaryEntry de in outer._messageDigests)
                {
                    outer._messageHashes.Add(de.Key, DigestUtilities.DoFinal((IDigest)de.Value));
                }

                // TODO If the digest OIDs for precalculated signers weren't mixed in with
                // the others, we could fill in outer._digests here, instead of SignerInf.ToSignerInfo

                //
                // add the precalculated SignerInfo objects.
                //
                Asn1EncodableVector signerInfos = new Asn1EncodableVector();

                foreach (SignerInformation signer in outer._signers)
                {
                    signerInfos.Add(signer.ToSignerInfo());
                }

                //
                // add the SignerInfo objects
                //
                foreach (SignerInf signer in outer._signerInfs)
                {
                    try
                    {
                        signerInfos.Add(signer.ToSignerInfo(_contentOID));
                    }
                    catch (IOException e)
                    {
                        throw new IOException("encoding error." + e);
                    }
                    catch (SignatureException e)
                    {
                        throw new IOException("error creating signature." + e);
                    }
                    catch (CertificateEncodingException e)
                    {
                        throw new IOException("error creating sid." + e);
                    }
                }

                WriteToGenerator(_sigGen, new DerSet(signerInfos));

                _sigGen.Close();
                _sGen.Close();
                base.Close();
            }