Exemplo n.º 1
0
		private void parserTest()
		{
			foreach (string stream in streams)
			{
				Asn1StreamParser aIn = new Asn1StreamParser(Base64.Decode(stream));

				try
				{
					Object obj;
					while ((obj = aIn.ReadObject()) != null)
					{
					}

					Fail("bad stream parsed successfully!");
				}
				catch (IOException)
				{
					// ignore
				}
				// Note: C# may throw these instead, since no InMemoryRepresentable support
				catch (Asn1ParsingException)
				{
					// ignore
				}
			}
		}
 internal BerApplicationSpecificParser(
     int					tag,
     Asn1StreamParser	parser)
 {
     this.tag = tag;
     this.parser = parser;
 }
Exemplo n.º 3
0
		public void TestReadingWritingZeroInLength()
		{
			MemoryStream bOut = new MemoryStream();
			BerOctetStringGenerator octGen = new BerOctetStringGenerator(bOut);

			Stream outStream = octGen.GetOctetOutputStream();

			outStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
			outStream.Write(new byte[512], 0, 512);  // forces a zero to appear in length

			outStream.Close();

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

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

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

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

			Assert.AreEqual(516, count);
		}
Exemplo n.º 4
0
		private void doTestNestedReading(
            byte[] data)
        {
            Asn1StreamParser aIn = new Asn1StreamParser(data);

			Asn1SequenceParser seq = (Asn1SequenceParser) aIn.ReadObject();
            object o = null;
            int count = 0;

			Assert.IsNotNull(seq, "null sequence returned");

			while ((o = seq.ReadObject()) != null)
            {
                switch (count)
                {
                    case 0:
                        Assert.IsTrue(o is DerInteger);
                        break;
                    case 1:
                        Assert.IsTrue(o is DerObjectIdentifier);
                        break;
                    case 2:
                        Assert.IsTrue(o is Asn1SequenceParser);

						Asn1SequenceParser s = (Asn1SequenceParser)o;

						// NB: Must exhaust the nested parser
						while (s.ReadObject() != null)
						{
							// Ignore
						}

						break;
                }
                count++;
            }

			Assert.AreEqual(3, count, "wrong number of objects in sequence");
        }
Exemplo n.º 5
0
		private ITestResult EnvelopedTest()
		{
			try
			{
				// Key trans
				ContentInfo info = ContentInfo.GetInstance(
					Asn1Object.FromByteArray(envDataKeyTrns));
				EnvelopedData envData = EnvelopedData.GetInstance(info.Content);
				Asn1Set s = envData.RecipientInfos;

				if (s.Count != 1)
				{
					return new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped, wrong number of recipients");
				}

				RecipientInfo recip = RecipientInfo.GetInstance(s[0]);

				if (recip.Info is KeyTransRecipientInfo)
				{
					KeyTransRecipientInfo inf = KeyTransRecipientInfo.GetInstance(recip.Info);

					inf = new KeyTransRecipientInfo(inf.RecipientIdentifier, inf.KeyEncryptionAlgorithm, inf.EncryptedKey);

					s = new DerSet(new RecipientInfo(inf));
				}
				else
				{
					return new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped, wrong recipient type");
				}

				envData = new EnvelopedData(envData.OriginatorInfo, s, envData.EncryptedContentInfo, envData.UnprotectedAttrs);
				info = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, envData);

				if (!Arrays.AreEqual(info.GetEncoded(), envDataKeyTrns))
				{
					return new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped failed to re-encode");
				}


				// KEK
				info = ContentInfo.GetInstance(
					Asn1Object.FromByteArray(envDataKEK));
				envData = EnvelopedData.GetInstance(info.Content);
				s = envData.RecipientInfos;

				if (s.Count != 1)
				{
					return new SimpleTestResult(false, Name + ": CMS KEK enveloped, wrong number of recipients");
				}

				recip = RecipientInfo.GetInstance(s[0]);

				if (recip.Info is KekRecipientInfo)
				{
					KekRecipientInfo inf = KekRecipientInfo.GetInstance(recip.Info);

					inf = new KekRecipientInfo(inf.KekID, inf.KeyEncryptionAlgorithm, inf.EncryptedKey);

					s = new DerSet(new RecipientInfo(inf));
				}
				else
				{
					return new SimpleTestResult(false, Name + ": CMS KEK enveloped, wrong recipient type");
				}

				envData = new EnvelopedData(envData.OriginatorInfo, s, envData.EncryptedContentInfo, envData.UnprotectedAttrs);
				info = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, envData);

				if (!Arrays.AreEqual(info.GetEncoded(), envDataKEK))
				{
					return new SimpleTestResult(false, Name + ": CMS KEK enveloped failed to re-encode");
				}

				// Nested NDEF problem
				Asn1StreamParser asn1In = new Asn1StreamParser(new MemoryStream(envDataNestedNDEF, false));
				ContentInfoParser ci = new ContentInfoParser((Asn1SequenceParser)asn1In.ReadObject());
				EnvelopedDataParser ed = new EnvelopedDataParser((Asn1SequenceParser)ci
					.GetContent(Asn1Tags.Sequence));
				Touch(ed.Version);
				ed.GetOriginatorInfo();
				ed.GetRecipientInfos().ToAsn1Object();
				EncryptedContentInfoParser eci = ed.GetEncryptedContentInfo();
				Touch(eci.ContentType);
				Touch(eci.ContentEncryptionAlgorithm);

				Stream dataIn = ((Asn1OctetStringParser)eci.GetEncryptedContent(Asn1Tags.OctetString))
					.GetOctetStream();
				Streams.Drain(dataIn);
				dataIn.Close();

				// Test data doesn't have unprotected attrs, bug was being thrown by this call
				Asn1SetParser upa = ed.GetUnprotectedAttrs();
				if (upa != null)
				{
					upa.ToAsn1Object();
				}

				return new SimpleTestResult(true, Name + ": Okay");
			}
			catch (Exception e)
			{
				return new SimpleTestResult(false, Name + ": CMS enveloped failed - " + e.ToString(), e);
			}
		}
Exemplo n.º 6
0
 internal BerApplicationSpecificParser(int tag, Asn1StreamParser parser)
 {
     this.tag    = tag;
     this.parser = parser;
 }
Exemplo n.º 7
0
		internal DerSetParser(
			Asn1StreamParser parser)
		{
			this._parser = parser;
		}
 public DerExternalParser(Asn1StreamParser parser)
 {
     this._parser = parser;
 }
Exemplo n.º 9
0
 internal DerSetParser(
     Asn1StreamParser parser)
 {
     this._parser = parser;
 }
Exemplo n.º 10
0
 internal BerSetParser(Asn1StreamParser parser)
 {
     _parser = parser;
 }
Exemplo n.º 11
0
 internal DerSequenceParser(Asn1StreamParser parser)
 {
     _parser = parser;
 }
Exemplo n.º 12
0
 public DerExternalParser(Asn1StreamParser parser)
 {
     _parser = parser;
 }
 public Asn1Parser(byte[] encoded)
 {
     parser = new Asn1StreamParser(encoded);
 }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
		public void TestBerReading()
        {
            Asn1StreamParser aIn = new Asn1StreamParser(berSeqData);

			Asn1SequenceParser seq = (Asn1SequenceParser) aIn.ReadObject();
            object o = null;
            int count = 0;

			Assert.IsNotNull(seq, "null sequence returned");

			while ((o = seq.ReadObject()) != null)
            {
                switch (count)
                {
                    case 0:
                        Assert.IsTrue(o is DerInteger);
                        break;
                    case 1:
                        Assert.IsTrue(o is DerObjectIdentifier);
                        break;
                }
                count++;
            }

			Assert.AreEqual(2, count, "wrong number of objects in sequence");
        }
Exemplo n.º 17
0
		private void doTestParseWithNull(
			byte[] data)
		{
			Asn1StreamParser aIn = new Asn1StreamParser(data);
			Asn1SequenceParser seq = (Asn1SequenceParser) aIn.ReadObject();
			object o;
			int count = 0;

			Assert.IsNotNull(seq, "null sequence returned");

			while ((o = seq.ReadObject()) != null)
			{
				switch (count)
				{
					case 0:
						Assert.IsTrue(o is Asn1Null);
						break;
					case 1:
						Assert.IsTrue(o is DerInteger);
						break;
					case 2:
						Assert.IsTrue(o is DerObjectIdentifier);
						break;
				}
				count++;
			}

			Assert.AreEqual(3, count, "wrong number of objects in sequence");
		}
Exemplo n.º 18
0
 internal BerSequenceParser(
     Asn1StreamParser parser)
 {
     this._parser = parser;
 }
Exemplo n.º 19
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);
		}
Exemplo n.º 20
0
		public void TestLongTag()
		{
			Asn1StreamParser aIn = new Asn1StreamParser(longTagged);
			Asn1TaggedObjectParser tagged = (Asn1TaggedObjectParser)aIn.ReadObject();

			Assert.AreEqual(31, tagged.TagNo);
		}
Exemplo n.º 21
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);
		}
Exemplo n.º 22
0
		private void ParseEnveloped(
			byte[] data)
        {
            Asn1StreamParser aIn = new Asn1StreamParser(data);

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

			EnvelopedDataParser eP = new EnvelopedDataParser((Asn1SequenceParser)cP.GetContent(Asn1Tags.Sequence));

			eP.GetRecipientInfos().ToAsn1Object(); // Must drain the parser!

			EncryptedContentInfoParser ecP = eP.GetEncryptedContentInfo();

			Asn1OctetStringParser content = (Asn1OctetStringParser)ecP.GetEncryptedContent(Asn1Tags.OctetString);

			Streams.Drain(content.GetOctetStream());
        }
Exemplo n.º 23
0
		internal BerSequenceParser(
			Asn1StreamParser parser)
		{
			this._parser = parser;
		}
Exemplo n.º 24
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);
        }