Org.BouncyCastle.Asn1.Asn1StreamParser.ReadObject C# (CSharp) Méthode

ReadObject() public méthode

public ReadObject ( ) : IAsn1Convertible
Résultat IAsn1Convertible
		public virtual IAsn1Convertible ReadObject()
		{
			int tag = _in.ReadByte();
			if (tag == -1)
				return null;

			// turn of looking for "00" while we resolve the tag
			Set00Check(false);

			//
			// calculate tag number
			//
			int tagNo = Asn1InputStream.ReadTagNumber(_in, tag);

			bool isConstructed = (tag & Asn1Tags.Constructed) != 0;

			//
			// calculate length
			//
			int length = Asn1InputStream.ReadLength(_in, _limit);

			if (length < 0) // indefinite length method
			{
				if (!isConstructed)
					throw new IOException("indefinite length primitive encoding encountered");

				IndefiniteLengthInputStream indIn = new IndefiniteLengthInputStream(_in, _limit);
				Asn1StreamParser sp = new Asn1StreamParser(indIn, _limit);

				if ((tag & Asn1Tags.Application) != 0)
				{
					return new BerApplicationSpecificParser(tagNo, sp);
				}

				if ((tag & Asn1Tags.Tagged) != 0)
				{
					return new BerTaggedObjectParser(true, tagNo, sp);
				}

				return sp.ReadIndef(tagNo);
			}
			else
			{
				DefiniteLengthInputStream defIn = new DefiniteLengthInputStream(_in, length);

				if ((tag & Asn1Tags.Application) != 0)
				{
					return new DerApplicationSpecific(isConstructed, tagNo, defIn.ToArray());
				}

				if ((tag & Asn1Tags.Tagged) != 0)
				{
					return new BerTaggedObjectParser(isConstructed, tagNo, new Asn1StreamParser(defIn));
				}

				if (isConstructed)
				{
					// TODO There are other tags that may be constructed (e.g. BitString)
					switch (tagNo)
					{
						case Asn1Tags.OctetString:
							//
							// yes, people actually do this...
							//
							return new BerOctetStringParser(new Asn1StreamParser(defIn));
						case Asn1Tags.Sequence:
							return new DerSequenceParser(new Asn1StreamParser(defIn));
						case Asn1Tags.Set:
							return new DerSetParser(new Asn1StreamParser(defIn));
						case Asn1Tags.External:
							return new DerExternalParser(new Asn1StreamParser(defIn));
						default:
							// TODO Add DerUnknownTagParser class?
							return new DerUnknownTag(true, tagNo, defIn.ToArray());
					}
				}

				// Some primitive encodings can be handled by parsers too...
				switch (tagNo)
				{
					case Asn1Tags.OctetString:
						return new DerOctetStringParser(defIn);
				}

				try
				{
					return Asn1InputStream.CreatePrimitiveDerObject(tagNo, defIn, tmpBuffers);
				}
				catch (ArgumentException e)
				{
					throw new Asn1Exception("corrupted stream detected", e);
				}
			}
		}

Usage Example

Exemple #1
0
        public byte[] UnWrapCMS(byte[] cipher, out byte[] sessionKey, out byte[] IV)
        {
            //These method calls must remain in this order.

            //Content Info
            A.Asn1StreamParser strmParser = new A.Asn1StreamParser(cipher);
            A.Cms.ContentInfoParser cInfoParser = new A.Cms.ContentInfoParser((A.Asn1SequenceParser)strmParser.ReadObject());
            A.Asn1SequenceParser seqParser = (A.Asn1SequenceParser)cInfoParser.GetContent(A.Asn1Tags.Sequence);

            //Enveloped Data
            A.Cms.EnvelopedDataParser envDataParser = new A.Cms.EnvelopedDataParser(seqParser);

            //Recipent Info
            //GetOriginatorInfo() This method gets called from GetRecipientInfos
            A.DerSetParser rec = (A.DerSetParser)envDataParser.GetRecipientInfos();
            A.Asn1Object recInfoData = rec.ReadObject().ToAsn1Object();
            A.Cms.RecipientInfo recipInfo = new A.Cms.RecipientInfo(recInfoData);

            //Symmetric session key
            A.Cms.KeyTransRecipientInfo trans = (A.Cms.KeyTransRecipientInfo)recipInfo.Info;
            byte[] cipherSessionKey = trans.EncryptedKey.GetOctets();
            X509Certificate2 cert = this.Certificate;
            RSACryptoServiceProvider rsaCrypto = (RSACryptoServiceProvider)cert.PrivateKey;
            sessionKey = rsaCrypto.Decrypt(cipherSessionKey, false);

            //Encrypted Content Info
            A.Cms.EncryptedContentInfoParser encrContentInfoParser = envDataParser.GetEncryptedContentInfo();

            //Symmetric Initialization Vector
            A.X509.AlgorithmIdentifier aes = encrContentInfoParser.ContentEncryptionAlgorithm;
            A.DerOctetString initVector = (A.DerOctetString)aes.Parameters;
            IV = initVector.GetOctets();

            //Card data
            List<byte> cipherBytes = new List<byte>();
            A.DerOctetStringParser cipherContent = (A.DerOctetStringParser)encrContentInfoParser.GetEncryptedContent(A.Asn1Tags.OctetString);
            using (System.IO.Stream cipherStrm = cipherContent.GetOctetStream())
            {
                int b = cipherStrm.ReadByte();

                while (b > -1)
                {
                    cipherBytes.Add((byte)b);
                    b = cipherStrm.ReadByte();
                }
            }

            return cipherBytes.ToArray();
        }
All Usage Examples Of Org.BouncyCastle.Asn1.Asn1StreamParser::ReadObject