public override int ReadByte()
 {
     if (this._currentStream == null)
     {
         if (!this._first)
         {
             return(0);
         }
         Asn1OctetStringParser parser = (Asn1OctetStringParser)this._parser.ReadObject();
         if (parser == null)
         {
             return(0);
         }
         this._first         = false;
         this._currentStream = parser.GetOctetStream();
     }
     while (true)
     {
         int num = this._currentStream.ReadByte();
         if (num >= 0)
         {
             return(num);
         }
         Asn1OctetStringParser parser2 = (Asn1OctetStringParser)this._parser.ReadObject();
         if (parser2 == null)
         {
             this._currentStream = null;
             return(-1);
         }
         this._currentStream = parser2.GetOctetStream();
     }
 }
        private TimeStampedDataParser(Asn1SequenceParser parser)
        {
            this.parser = parser;
            this.version = DerInteger.GetInstance(parser.ReadObject());

            Asn1Object obj = parser.ReadObject().ToAsn1Object();

            if (obj is DerIA5String)
            {
                this.dataUri = DerIA5String.GetInstance(obj);
                obj = parser.ReadObject().ToAsn1Object();
            }

            if (//obj is MetaData ||
                obj is Asn1SequenceParser)
            {
                this.metaData = MetaData.GetInstance(obj.ToAsn1Object());
                obj = parser.ReadObject().ToAsn1Object();
            }

            if (obj is Asn1OctetStringParser)
            {
                this.content = (Asn1OctetStringParser)obj;
            }
        }
 public override int ReadByte()
 {
     if (_currentStream == null)
     {
         if (!_first)
         {
             return(0);
         }
         Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)_parser.ReadObject();
         if (asn1OctetStringParser == null)
         {
             return(0);
         }
         _first         = false;
         _currentStream = asn1OctetStringParser.GetOctetStream();
     }
     while (true)
     {
         int num = _currentStream.ReadByte();
         if (num >= 0)
         {
             return(num);
         }
         Asn1OctetStringParser asn1OctetStringParser2 = (Asn1OctetStringParser)_parser.ReadObject();
         if (asn1OctetStringParser2 == null)
         {
             break;
         }
         _currentStream = asn1OctetStringParser2.GetOctetStream();
     }
     _currentStream = null;
     return(-1);
 }
Пример #4
0
        private TimeStampedDataParser(Asn1SequenceParser parser)
        {
            this.parser  = parser;
            this.version = DerInteger.GetInstance(parser.ReadObject());

            Asn1Object obj = parser.ReadObject().ToAsn1Object();

            if (obj is DerIA5String)
            {
                this.dataUri = DerIA5String.GetInstance(obj);
                obj          = parser.ReadObject().ToAsn1Object();
            }

            if (//obj is MetaData ||
                obj is Asn1SequenceParser)
            {
                this.metaData = MetaData.GetInstance(obj.ToAsn1Object());
                obj           = parser.ReadObject().ToAsn1Object();
            }

            if (obj is Asn1OctetStringParser)
            {
                this.content = (Asn1OctetStringParser)obj;
            }
        }
Пример #5
0
 public CmsSignedDataParser(CmsTypedStream signedContent, Stream sigData)
     : base(sigData)
 {
     //IL_0130: Expected O, but got Unknown
     try
     {
         _signedContent = signedContent;
         _signedData    = SignedDataParser.GetInstance(contentInfo.GetContent(16));
         _digests       = Platform.CreateHashtable();
         _digestOids    = new HashSet();
         Asn1SetParser    digestAlgorithms = _signedData.GetDigestAlgorithms();
         IAsn1Convertible asn1Convertible;
         while ((asn1Convertible = digestAlgorithms.ReadObject()) != null)
         {
             AlgorithmIdentifier instance = AlgorithmIdentifier.GetInstance(asn1Convertible.ToAsn1Object());
             try
             {
                 string id            = instance.Algorithm.Id;
                 string digestAlgName = Helper.GetDigestAlgName(id);
                 if (!_digests.Contains((object)digestAlgName))
                 {
                     _digests.set_Item((object)digestAlgName, (object)Helper.GetDigestInstance(digestAlgName));
                     _digestOids.Add(id);
                 }
             }
             catch (SecurityUtilityException)
             {
             }
         }
         ContentInfoParser     encapContentInfo      = _signedData.GetEncapContentInfo();
         Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)encapContentInfo.GetContent(4);
         if (asn1OctetStringParser != null)
         {
             CmsTypedStream cmsTypedStream = new CmsTypedStream(encapContentInfo.ContentType.Id, asn1OctetStringParser.GetOctetStream());
             if (_signedContent == null)
             {
                 _signedContent = cmsTypedStream;
             }
             else
             {
                 cmsTypedStream.Drain();
             }
         }
         _signedContentType = ((_signedContent == null) ? encapContentInfo.ContentType : new DerObjectIdentifier(_signedContent.ContentType));
     }
     catch (IOException val)
     {
         IOException val2 = val;
         throw new CmsException("io exception: " + ((global::System.Exception)(object) val2).get_Message(), (global::System.Exception)(object) val2);
     }
 }
 public CmsTypedStream GetContent()
 {
     try
     {
         CompressedDataParser  compressedDataParser  = new CompressedDataParser((Asn1SequenceParser)contentInfo.GetContent(16));
         ContentInfoParser     encapContentInfo      = compressedDataParser.GetEncapContentInfo();
         Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)encapContentInfo.GetContent(4);
         return(new CmsTypedStream(encapContentInfo.ContentType.ToString(), new ZInputStream(asn1OctetStringParser.GetOctetStream())));
     }
     catch (IOException e)
     {
         throw new CmsException("IOException reading compressed content.", e);
     }
 }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_currentStream == null)
            {
                if (!_first)
                {
                    return(0);
                }

                Asn1OctetStringParser s = (Asn1OctetStringParser)_parser.ReadObject();

                if (s == null)
                {
                    return(0);
                }

                _first         = false;
                _currentStream = s.GetOctetStream();
            }

            int totalRead = 0;

            for (;;)
            {
                int numRead = _currentStream.Read(buffer, offset + totalRead, count - totalRead);

                if (numRead > 0)
                {
                    totalRead += numRead;

                    if (totalRead == count)
                    {
                        return(totalRead);
                    }
                }
                else
                {
                    Asn1OctetStringParser aos = (Asn1OctetStringParser)_parser.ReadObject();

                    if (aos == null)
                    {
                        _currentStream = null;
                        return(totalRead);
                    }

                    _currentStream = aos.GetOctetStream();
                }
            }
        }
 public CmsSignedDataParser(CmsTypedStream signedContent, Stream sigData) : base(sigData)
 {
     try
     {
         this._signedContent = signedContent;
         this._signedData    = SignedDataParser.GetInstance(this.contentInfo.GetContent(16));
         this._digests       = Platform.CreateHashtable();
         this._digestOids    = new HashSet();
         Asn1SetParser    digestAlgorithms = this._signedData.GetDigestAlgorithms();
         IAsn1Convertible asn1Convertible;
         while ((asn1Convertible = digestAlgorithms.ReadObject()) != null)
         {
             AlgorithmIdentifier instance = AlgorithmIdentifier.GetInstance(asn1Convertible.ToAsn1Object());
             try
             {
                 string id            = instance.ObjectID.Id;
                 string digestAlgName = CmsSignedDataParser.Helper.GetDigestAlgName(id);
                 if (!this._digests.Contains(digestAlgName))
                 {
                     this._digests[digestAlgName] = CmsSignedDataParser.Helper.GetDigestInstance(digestAlgName);
                     this._digestOids.Add(id);
                 }
             }
             catch (SecurityUtilityException)
             {
             }
         }
         ContentInfoParser     encapContentInfo      = this._signedData.GetEncapContentInfo();
         Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)encapContentInfo.GetContent(4);
         if (asn1OctetStringParser != null)
         {
             CmsTypedStream cmsTypedStream = new CmsTypedStream(encapContentInfo.ContentType.Id, asn1OctetStringParser.GetOctetStream());
             if (this._signedContent == null)
             {
                 this._signedContent = cmsTypedStream;
             }
             else
             {
                 cmsTypedStream.Drain();
             }
         }
         this._signedContentType = ((this._signedContent == null) ? encapContentInfo.ContentType : new DerObjectIdentifier(this._signedContent.ContentType));
     }
     catch (IOException ex)
     {
         throw new CmsException("io exception: " + ex.Message, ex);
     }
 }
Пример #9
0
        public CmsTypedStream GetContent()
        {
            try
            {
                CompressedDataParser comData = new CompressedDataParser((Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));
                ContentInfoParser    content = comData.GetEncapContentInfo();

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

                return(new CmsTypedStream(content.ContentType.ToString(), new ZInputStream(bytes.GetOctetStream())));
            }
            catch (IOException e)
            {
                throw new CmsException("IOException reading compressed content.", e);
            }
        }
 public CmsTypedStream GetContent()
 {
     //IL_004c: Expected O, but got Unknown
     try
     {
         CompressedDataParser  compressedDataParser  = new CompressedDataParser((Asn1SequenceParser)contentInfo.GetContent(16));
         ContentInfoParser     encapContentInfo      = compressedDataParser.GetEncapContentInfo();
         Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)encapContentInfo.GetContent(4);
         return(new CmsTypedStream(encapContentInfo.ContentType.ToString(), (Stream)(object)new ZInputStream(asn1OctetStringParser.GetOctetStream())));
     }
     catch (IOException val)
     {
         IOException e = val;
         throw new CmsException("IOException reading compressed content.", (global::System.Exception)(object) e);
     }
 }
Пример #11
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_currentStream == null)
            {
                if (!_first)
                {
                    return(0);
                }

                Asn1OctetStringParser next = GetNextParser();
                if (next == null)
                {
                    return(0);
                }

                _first         = false;
                _currentStream = next.GetOctetStream();
            }

            int totalRead = 0;

            for (;;)
            {
                int numRead = _currentStream.Read(buffer, offset + totalRead, count - totalRead);

                if (numRead > 0)
                {
                    totalRead += numRead;

                    if (totalRead == count)
                    {
                        return(totalRead);
                    }
                }
                else
                {
                    Asn1OctetStringParser next = GetNextParser();
                    if (next == null)
                    {
                        _currentStream = null;
                        return(totalRead);
                    }

                    _currentStream = next.GetOctetStream();
                }
            }
        }
Пример #12
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());
        }
Пример #13
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this._currentStream == null)
            {
                if (!this._first)
                {
                    return(0);
                }
                Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)this._parser.ReadObject();
                if (asn1OctetStringParser == null)
                {
                    return(0);
                }
                this._first         = false;
                this._currentStream = asn1OctetStringParser.GetOctetStream();
            }
            int num = 0;

            while (true)
            {
                int num2 = this._currentStream.Read(buffer, offset + num, count - num);
                if (num2 > 0)
                {
                    num += num2;
                    if (num == count)
                    {
                        break;
                    }
                }
                else
                {
                    Asn1OctetStringParser asn1OctetStringParser2 = (Asn1OctetStringParser)this._parser.ReadObject();
                    if (asn1OctetStringParser2 == null)
                    {
                        goto Block_6;
                    }
                    this._currentStream = asn1OctetStringParser2.GetOctetStream();
                }
            }
            return(num);

Block_6:
            this._currentStream = null;
            return(num);
        }
Пример #14
0
    private TimeStampedDataParser(Asn1SequenceParser parser)
    {
        this.parser = parser;
        version     = DerInteger.GetInstance(parser.ReadObject());
        Asn1Object asn1Object = parser.ReadObject().ToAsn1Object();

        if (asn1Object is DerIA5String)
        {
            dataUri    = DerIA5String.GetInstance(asn1Object);
            asn1Object = parser.ReadObject().ToAsn1Object();
        }
        if (asn1Object is Asn1SequenceParser)
        {
            metaData   = MetaData.GetInstance(asn1Object.ToAsn1Object());
            asn1Object = parser.ReadObject().ToAsn1Object();
        }
        if (asn1Object is Asn1OctetStringParser)
        {
            content = (Asn1OctetStringParser)asn1Object;
        }
    }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_currentStream == null)
            {
                if (!_first)
                {
                    return(0);
                }
                Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)_parser.ReadObject();
                if (asn1OctetStringParser == null)
                {
                    return(0);
                }
                _first         = false;
                _currentStream = asn1OctetStringParser.GetOctetStream();
            }
            int num = 0;

            while (true)
            {
                int num2 = _currentStream.Read(buffer, offset + num, count - num);
                if (num2 > 0)
                {
                    num += num2;
                    if (num == count)
                    {
                        return(num);
                    }
                    continue;
                }
                Asn1OctetStringParser asn1OctetStringParser2 = (Asn1OctetStringParser)_parser.ReadObject();
                if (asn1OctetStringParser2 == null)
                {
                    break;
                }
                _currentStream = asn1OctetStringParser2.GetOctetStream();
            }
            _currentStream = null;
            return(num);
        }
        public override int ReadByte()
        {
            if (_currentStream == null)
            {
                if (!_first)
                {
                    return(0);
                }

                Asn1OctetStringParser s = (Asn1OctetStringParser)_parser.ReadObject();

                if (s == null)
                {
                    return(0);
                }

                _first         = false;
                _currentStream = s.GetOctetStream();
            }

            for (;;)
            {
                int b = _currentStream.ReadByte();

                if (b >= 0)
                {
                    return(b);
                }

                Asn1OctetStringParser aos = (Asn1OctetStringParser)_parser.ReadObject();

                if (aos == null)
                {
                    _currentStream = null;
                    return(-1);
                }

                _currentStream = aos.GetOctetStream();
            }
        }
Пример #17
0
        public override int ReadByte()
        {
            if (_currentStream == null)
            {
                if (!_first)
                {
                    return(0);
                }

                Asn1OctetStringParser next = GetNextParser();
                if (next == null)
                {
                    return(0);
                }

                _first         = false;
                _currentStream = next.GetOctetStream();
            }

            for (;;)
            {
                int b = _currentStream.ReadByte();

                if (b >= 0)
                {
                    return(b);
                }

                Asn1OctetStringParser next = GetNextParser();
                if (next == null)
                {
                    _currentStream = null;
                    return(-1);
                }

                _currentStream = next.GetOctetStream();
            }
        }
Пример #18
0
        public override int ReadByte()
        {
            if (this._currentStream == null)
            {
                if (!this._first)
                {
                    return(0);
                }
                Asn1OctetStringParser asn1OctetStringParser = (Asn1OctetStringParser)this._parser.ReadObject();
                if (asn1OctetStringParser == null)
                {
                    return(0);
                }
                this._first         = false;
                this._currentStream = asn1OctetStringParser.GetOctetStream();
            }
            int num;

            while (true)
            {
                num = this._currentStream.ReadByte();
                if (num >= 0)
                {
                    break;
                }
                Asn1OctetStringParser asn1OctetStringParser2 = (Asn1OctetStringParser)this._parser.ReadObject();
                if (asn1OctetStringParser2 == null)
                {
                    goto Block_5;
                }
                this._currentStream = asn1OctetStringParser2.GetOctetStream();
            }
            return(num);

Block_5:
            this._currentStream = null;
            return(-1);
        }
Пример #19
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);
        }
Пример #20
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);
        }
Пример #21
0
        /**
         * base constructor
         *
         * @param signedContent the content that was signed.
         * @param sigData the signature object.
         */
        public CmsSignedDataParser(
            CmsTypedStream signedContent,
            Stream sigData)
            : base(sigData)
        {
            try
            {
                this._signedContent = signedContent;
                this._signedData    = SignedDataParser.GetInstance(this.contentInfo.GetContent(Asn1Tags.Sequence));
                this._digests       = new Hashtable();

                Asn1SetParser    digAlgs = _signedData.GetDigestAlgorithms();
                IAsn1Convertible o;

                while ((o = digAlgs.ReadObject()) != null)
                {
                    AlgorithmIdentifier id = AlgorithmIdentifier.GetInstance(o.ToAsn1Object());

                    try
                    {
                        string  digestName = Helper.GetDigestAlgName(id.ObjectID.Id);
                        IDigest dig        = DigestUtilities.GetDigest(digestName);

                        this._digests[digestName] = dig;
                    }
                    catch (SecurityUtilityException)
                    {
                        //  ignore
                    }
                }

                if (_signedContent == null)
                {
                    //
                    // If the message is simply a certificate chain message GetContent() may return null.
                    //
                    Asn1OctetStringParser octs = (Asn1OctetStringParser)
                                                 _signedData.GetEncapContentInfo().GetContent(Asn1Tags.OctetString);

                    if (octs != null)
                    {
                        this._signedContent = new CmsTypedStream(octs.GetOctetStream());
                    }
                }
                else
                {
                    //
                    // content passed in, need to read past empty encapsulated content info object if present
                    //
                    Asn1OctetStringParser octs = (Asn1OctetStringParser)
                                                 _signedData.GetEncapContentInfo().GetContent(Asn1Tags.OctetString);

                    if (octs != null)
                    {
                        Stream inStream = octs.GetOctetStream();

                        while (inStream.ReadByte() >= 0)
                        {
                            // ignore
                        }
                    }
                }
            }
            catch (IOException e)
            {
                throw new CmsException("io exception: " + e.Message, e);
            }

            if (_digests.Count < 1)
            {
                throw new CmsException("no digests could be created for message.");
            }
        }
Пример #22
0
        /**
         * base constructor
         *
         * @param signedContent the content that was signed.
         * @param sigData the signature object.
         */
        public CmsSignedDataParser(
            CmsTypedStream signedContent,
            Stream sigData)
            : base(sigData)
        {
            try
            {
                this._signedContent = signedContent;
                this._signedData    = SignedDataParser.GetInstance(this.contentInfo.GetContent(Asn1Tags.Sequence));
                this._digests       = Platform.CreateHashtable();
                this._digestOids    = new HashSet();

                Asn1SetParser    digAlgs = _signedData.GetDigestAlgorithms();
                IAsn1Convertible o;

                while ((o = digAlgs.ReadObject()) != null)
                {
                    AlgorithmIdentifier id = AlgorithmIdentifier.GetInstance(o.ToAsn1Object());

                    try
                    {
                        string digestOid  = id.ObjectID.Id;
                        string digestName = Helper.GetDigestAlgName(digestOid);

                        if (!this._digests.Contains(digestName))
                        {
                            this._digests[digestName] = Helper.GetDigestInstance(digestName);
                            this._digestOids.Add(digestOid);
                        }
                    }
                    catch (SecurityUtilityException)
                    {
                        // TODO Should do something other than ignore it
                    }
                }

                //
                // If the message is simply a certificate chain message GetContent() may return null.
                //
                ContentInfoParser     cont = _signedData.GetEncapContentInfo();
                Asn1OctetStringParser octs = (Asn1OctetStringParser)
                                             cont.GetContent(Asn1Tags.OctetString);

                if (octs != null)
                {
                    CmsTypedStream ctStr = new CmsTypedStream(
                        cont.ContentType.Id, octs.GetOctetStream());

                    if (_signedContent == null)
                    {
                        this._signedContent = ctStr;
                    }
                    else
                    {
                        //
                        // content passed in, need to read past empty encapsulated content info object if present
                        //
                        ctStr.Drain();
                    }
                }

                _signedContentType = _signedContent == null
                                        ?       cont.ContentType
                                        :       new DerObjectIdentifier(_signedContent.ContentType);
            }
            catch (IOException e)
            {
                throw new CmsException("io exception: " + e.Message, e);
            }

            if (_digests.Count < 1)
            {
                throw new CmsException("no digests could be created for message.");
            }
        }
Пример #23
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);
        }