Esempio n. 1
0
        /**
         * return a table of the unauthenticated attributes indexed by
         * the OID of the attribute.
         * @exception java.io.IOException
         */
        public AttributeTable GetUnauthAttrs()
        {
            if (unauthAttrs == null && unauthAttrNotRead)
            {
                Asn1SetParser s = authData.GetUnauthAttrs();

                unauthAttrNotRead = false;

                if (s != null)
                {
                    Asn1EncodableVector v = new Asn1EncodableVector();

                    IAsn1Convertible o;
                    while ((o = s.ReadObject()) != null)
                    {
                        Asn1SequenceParser seq = (Asn1SequenceParser)o;

                        v.Add(seq.ToAsn1Object());
                    }

                    unauthAttrs = new AttributeTable(new DerSet(v));
                }
            }

            return(unauthAttrs);
        }
Esempio n. 2
0
        public CmsEnvelopedDataParser(
            Stream envelopedData)
            : base(envelopedData)
        {
            this._attrNotRead  = true;
            this.envelopedData = new EnvelopedDataParser(
                (Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));

            //
            // load the RecepientInfoStore
            //
            Asn1SetParser s         = this.envelopedData.GetRecipientInfos();
            IList         baseInfos = new ArrayList();
            Asn1Set       set       = Asn1Set.GetInstance(s.ToAsn1Object());

            foreach (object o in set)
            {
                baseInfos.Add(RecipientInfo.GetInstance(o));
            }

            //
            // read the encrypted content info
            //
            EncryptedContentInfoParser encInfo = this.envelopedData.GetEncryptedContentInfo();

            this._encAlg = encInfo.ContentEncryptionAlgorithm;

            //
            // prime the recepients
            //
            IList  infos      = new ArrayList();
            Stream dataStream = ((Asn1OctetStringParser)encInfo.GetEncryptedContent(Asn1Tags.OctetString)).GetOctetStream();

            foreach (Asn1.Cms.RecipientInfo info in baseInfos)
            {
                Asn1Encodable recipInfo = info.Info;
                if (recipInfo is Asn1.Cms.KeyTransRecipientInfo)
                {
                    infos.Add(new KeyTransRecipientInformation(
                                  (KeyTransRecipientInfo)recipInfo, _encAlg, dataStream));
                }
                else if (recipInfo is Asn1.Cms.KekRecipientInfo)
                {
                    infos.Add(new KekRecipientInformation(
                                  (KekRecipientInfo)recipInfo, _encAlg, dataStream));
                }
                else if (recipInfo is KeyAgreeRecipientInfo)
                {
                    infos.Add(new KeyAgreeRecipientInformation(
                                  (KeyAgreeRecipientInfo)recipInfo, _encAlg, dataStream));
                }
                else if (recipInfo is PasswordRecipientInfo)
                {
                    infos.Add(new PasswordRecipientInformation(
                                  (PasswordRecipientInfo)recipInfo, _encAlg, dataStream));
                }
            }

            this.recipientInfoStore = new RecipientInformationStore(infos);
        }
Esempio n. 3
0
 private static Asn1Set GetAsn1Set(
     Asn1SetParser asn1SetParser)
 {
     return(asn1SetParser == null
                         ?       null
                         :       Asn1Set.GetInstance(asn1SetParser.ToAsn1Object()));
 }
        /**
         * return a table of the unprotected attributes indexed by
         * the OID of the attribute.
         * @throws IOException
         */
        public Asn1.Cms.AttributeTable GetUnprotectedAttributes()
        {
            if (_unprotectedAttributes == null && _attrNotRead)
            {
                Asn1SetParser asn1Set = this.envelopedData.GetUnprotectedAttrs();

                _attrNotRead = false;

                if (asn1Set != null)
                {
                    Asn1EncodableVector v = new Asn1EncodableVector();
                    IAsn1Convertible    o;

                    while ((o = asn1Set.ReadObject()) != null)
                    {
                        Asn1SequenceParser seq = (Asn1SequenceParser)o;

                        v.Add(seq.ToAsn1Object());
                    }

                    _unprotectedAttributes = new Asn1.Cms.AttributeTable(new DerSet(v));
                }
            }

            return(_unprotectedAttributes);
        }
Esempio n. 5
0
 public SignerInformationStore GetSignerInfos()
 {
     if (_signerInfoStore == null)
     {
         PopulateCertCrlSets();
         IList       list       = Platform.CreateArrayList();
         IDictionary dictionary = Platform.CreateHashtable();
         foreach (object key in _digests.Keys)
         {
             dictionary[key] = DigestUtilities.DoFinal((IDigest)_digests[key]);
         }
         try
         {
             Asn1SetParser    signerInfos = _signedData.GetSignerInfos();
             IAsn1Convertible asn1Convertible;
             while ((asn1Convertible = signerInfos.ReadObject()) != null)
             {
                 SignerInfo instance      = SignerInfo.GetInstance(asn1Convertible.ToAsn1Object());
                 string     digestAlgName = Helper.GetDigestAlgName(instance.DigestAlgorithm.Algorithm.Id);
                 byte[]     digest        = (byte[])dictionary[digestAlgName];
                 list.Add(new SignerInformation(instance, _signedContentType, null, new BaseDigestCalculator(digest)));
             }
         }
         catch (IOException ex)
         {
             throw new CmsException("io exception: " + ex.Message, ex);
         }
         _signerInfoStore = new SignerInformationStore(list);
     }
     return(_signerInfoStore);
 }
Esempio n. 6
0
 private static Asn1Set GetAsn1Set(Asn1SetParser asn1SetParser)
 {
     if (asn1SetParser != null)
     {
         return(Asn1Set.GetInstance(asn1SetParser.ToAsn1Object()));
     }
     return(null);
 }
Esempio n. 7
0
 public Asn1SetParser GetCertificates()
 {
     this._certsCalled = true;
     this._nextObject  = this._seq.ReadObject();
     if (this._nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)this._nextObject).TagNo == 0)
     {
         Asn1SetParser result = (Asn1SetParser)((Asn1TaggedObjectParser)this._nextObject).GetObjectParser(17, false);
         this._nextObject = null;
         return(result);
     }
     return(null);
 }
 public Asn1SetParser GetCertificates()
 {
     _certsCalled = true;
     _nextObject  = _seq.ReadObject();
     if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 0)
     {
         Asn1SetParser result = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(17, isExplicit: false);
         _nextObject = null;
         return(result);
     }
     return(null);
 }
Esempio n. 9
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 Asn1SetParser GetCertificates()
        {
            _nextObject = _seq.ReadObject();

            if (_nextObject is Asn1TaggedObject && ((Asn1TaggedObject)_nextObject).TagNo == 0)
            {
                Asn1SetParser certs = (Asn1SetParser)((Asn1TaggedObject)_nextObject).GetObjectParser(Asn1Tags.Set, false);
                _nextObject = null;

                return(certs);
            }

            return(null);
        }
Esempio n. 11
0
        public Asn1SetParser GetRecipientInfos()
        {
            if (!this.originatorInfoCalled)
            {
                this.GetOriginatorInfo();
            }
            if (this.nextObject == null)
            {
                this.nextObject = this.seq.ReadObject();
            }
            Asn1SetParser result = (Asn1SetParser)this.nextObject;

            this.nextObject = null;
            return(result);
        }
Esempio n. 12
0
    public Asn1SetParser GetRecipientInfos()
    {
        if (!originatorInfoCalled)
        {
            GetOriginatorInfo();
        }
        if (nextObject == null)
        {
            nextObject = seq.ReadObject();
        }
        Asn1SetParser result = (Asn1SetParser)nextObject;

        nextObject = null;
        return(result);
    }
Esempio n. 13
0
 public SignerInformationStore GetSignerInfos()
 {
     //IL_00e9: Expected O, but got Unknown
     if (_signerInfoStore == null)
     {
         PopulateCertCrlSets();
         global::System.Collections.IList list = Platform.CreateArrayList();
         IDictionary val = Platform.CreateHashtable();
         {
             global::System.Collections.IEnumerator enumerator = ((global::System.Collections.IEnumerable)_digests.get_Keys()).GetEnumerator();
             try
             {
                 while (enumerator.MoveNext())
                 {
                     object current = enumerator.get_Current();
                     val.set_Item(current, (object)DigestUtilities.DoFinal((IDigest)_digests.get_Item(current)));
                 }
             }
             finally
             {
                 global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                 if (disposable != null)
                 {
                     disposable.Dispose();
                 }
             }
         }
         try
         {
             Asn1SetParser    signerInfos = _signedData.GetSignerInfos();
             IAsn1Convertible asn1Convertible;
             while ((asn1Convertible = signerInfos.ReadObject()) != null)
             {
                 SignerInfo instance      = SignerInfo.GetInstance(asn1Convertible.ToAsn1Object());
                 string     digestAlgName = Helper.GetDigestAlgName(instance.DigestAlgorithm.Algorithm.Id);
                 byte[]     digest        = (byte[])val.get_Item((object)digestAlgName);
                 list.Add((object)new SignerInformation(instance, _signedContentType, null, new BaseDigestCalculator(digest)));
             }
         }
         catch (IOException val2)
         {
             IOException val3 = val2;
             throw new CmsException("io exception: " + ((global::System.Exception)(object) val3).get_Message(), (global::System.Exception)(object) val3);
         }
         _signerInfoStore = new SignerInformationStore((global::System.Collections.ICollection)list);
     }
     return(_signerInfoStore);
 }
 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);
     }
 }
        public Asn1SetParser GetCrls()
        {
            if (_nextObject == null)
            {
                _nextObject = _seq.ReadObject();
            }

            if (_nextObject is Asn1TaggedObject && ((Asn1TaggedObject)_nextObject).TagNo == 1)
            {
                Asn1SetParser crls = (Asn1SetParser)((Asn1TaggedObject)_nextObject).GetObjectParser(Asn1Tags.Set, false);
                _nextObject = null;

                return(crls);
            }

            return(null);
        }
        public Asn1SetParser GetRecipientInfos()
        {
            if (!_originatorInfoCalled)
            {
                GetOriginatorInfo();
            }

            if (_nextObject == null)
            {
                _nextObject = _seq.ReadObject();
            }

            Asn1SetParser recipientInfos = (Asn1SetParser)_nextObject;

            _nextObject = null;
            return(recipientInfos);
        }
Esempio n. 17
0
        /**
         * return the collection of signers that are associated with the
         * signatures for the message.
         * @throws CmsException
         */
        public SignerInformationStore GetSignerInfos()
        {
            if (_signerInfoStore == null)
            {
                IList       signerInfos = new ArrayList();
                IDictionary hashes      = new Hashtable();

                foreach (object digestKey in _digests.Keys)
                {
                    hashes[digestKey] = DigestUtilities.DoFinal(
                        (IDigest)_digests[digestKey]);
                }

                try
                {
                    Asn1SetParser    s = _signedData.GetSignerInfos();
                    IAsn1Convertible o;

                    while ((o = s.ReadObject()) != null)
                    {
                        SignerInfo info       = SignerInfo.GetInstance(o.ToAsn1Object());
                        string     digestName = Helper.GetDigestAlgName(
                            info.DigestAlgorithm.ObjectID.Id);

                        byte[] hash = (byte[])hashes[digestName];

                        signerInfos.Add(
                            new SignerInformation(
                                info,
                                new DerObjectIdentifier(_signedContent.ContentType),
                                null,
                                hash));
                    }
                }
                catch (IOException e)
                {
                    throw new CmsException("io exception: " + e.Message, e);
                }

                _signerInfoStore = new SignerInformationStore(signerInfos);
            }

            return(_signerInfoStore);
        }
 public Asn1SetParser GetCrls()
 {
     if (!_certsCalled)
     {
         throw new IOException("GetCerts() has not been called.");
     }
     _crlsCalled = true;
     if (_nextObject == null)
     {
         _nextObject = _seq.ReadObject();
     }
     if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 1)
     {
         Asn1SetParser result = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(17, isExplicit: false);
         _nextObject = null;
         return(result);
     }
     return(null);
 }
Esempio n. 19
0
 public Asn1SetParser GetCrls()
 {
     //IL_000d: Unknown result type (might be due to invalid IL or missing references)
     if (!_certsCalled)
     {
         throw new IOException("GetCerts() has not been called.");
     }
     _crlsCalled = true;
     if (_nextObject == null)
     {
         _nextObject = _seq.ReadObject();
     }
     if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 1)
     {
         Asn1SetParser result = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(17, isExplicit: false);
         _nextObject = null;
         return(result);
     }
     return(null);
 }
 public Org.BouncyCastle.Asn1.Cms.AttributeTable GetUnprotectedAttributes()
 {
     if (_unprotectedAttributes == null && _attrNotRead)
     {
         Asn1SetParser unprotectedAttrs = envelopedData.GetUnprotectedAttrs();
         _attrNotRead = false;
         if (unprotectedAttrs != null)
         {
             Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
             IAsn1Convertible    asn1Convertible;
             while ((asn1Convertible = unprotectedAttrs.ReadObject()) != null)
             {
                 Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)asn1Convertible;
                 asn1EncodableVector.Add(asn1SequenceParser.ToAsn1Object());
             }
             _unprotectedAttributes = new Org.BouncyCastle.Asn1.Cms.AttributeTable(new DerSet(asn1EncodableVector));
         }
     }
     return(_unprotectedAttributes);
 }
 public Org.BouncyCastle.Asn1.Cms.AttributeTable GetUnauthAttrs()
 {
     if (unauthAttrs == null && unauthAttrNotRead)
     {
         Asn1SetParser asn1SetParser = authData.GetUnauthAttrs();
         unauthAttrNotRead = false;
         if (asn1SetParser != null)
         {
             Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
             IAsn1Convertible    asn1Convertible;
             while ((asn1Convertible = asn1SetParser.ReadObject()) != null)
             {
                 Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)asn1Convertible;
                 asn1EncodableVector.Add(asn1SequenceParser.ToAsn1Object());
             }
             unauthAttrs = new Org.BouncyCastle.Asn1.Cms.AttributeTable(new DerSet(asn1EncodableVector));
         }
     }
     return(unauthAttrs);
 }
Esempio n. 22
0
        /**
         * return the collection of signers that are associated with the
         * signatures for the message.
         * @throws CmsException
         */
        public SignerInformationStore GetSignerInfos()
        {
            if (_signerInfoStore == null)
            {
                PopulateCertCrlSets();

                IList       signerInfos = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
                IDictionary hashes      = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();

                foreach (object digestKey in _digests.Keys)
                {
                    hashes[digestKey] = DigestUtilities.DoFinal(
                        (IDigest)_digests[digestKey]);
                }

                try
                {
                    Asn1SetParser    s = _signedData.GetSignerInfos();
                    IAsn1Convertible o;

                    while ((o = s.ReadObject()) != null)
                    {
                        SignerInfo info       = SignerInfo.GetInstance(o.ToAsn1Object());
                        string     digestName = Helper.GetDigestAlgName(
                            info.DigestAlgorithm.Algorithm.Id);

                        byte[] hash = (byte[])hashes[digestName];

                        signerInfos.Add(new SignerInformation(info, _signedContentType, null, new BaseDigestCalculator(hash)));
                    }
                }
                catch (IOException e)
                {
                    throw new CmsException("io exception: " + e.Message, e);
                }

                _signerInfoStore = new SignerInformationStore(signerInfos);
            }

            return(_signerInfoStore);
        }
Esempio n. 23
0
        public CmsEnvelopedDataParser(
            Stream envelopedData)
            : base(envelopedData)
        {
            this._attrNotRead  = true;
            this.envelopedData = new EnvelopedDataParser(
                (Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));

            // TODO Validate version?
            //DerInteger version = this.envelopedData.Version;

            //
            // load the RecipientInfoStore
            //
            Asn1SetParser s         = this.envelopedData.GetRecipientInfos();
            IList         baseInfos = new ArrayList();

            IAsn1Convertible entry;

            while ((entry = s.ReadObject()) != null)
            {
                baseInfos.Add(RecipientInfo.GetInstance(entry.ToAsn1Object()));
            }

            //
            // read the encrypted content info
            //
            EncryptedContentInfoParser encInfo = this.envelopedData.GetEncryptedContentInfo();

            this._encAlg = encInfo.ContentEncryptionAlgorithm;

            //
            // prime the recipients
            //
            Stream contentStream = ((Asn1OctetStringParser)encInfo.GetEncryptedContent(Asn1Tags.OctetString)).GetOctetStream();
            IList  infos         = CmsEnvelopedHelper.ReadRecipientInfos(
                baseInfos, contentStream, _encAlg, null, null);

            this.recipientInfoStore = new RecipientInformationStore(infos);
        }
        public CmsAuthenticatedDataParser(
            Stream envelopedData)
            : base(envelopedData)
        {
            this.authAttrNotRead = true;
            this.authData        = new AuthenticatedDataParser(
                (Asn1SequenceParser)contentInfo.GetContent(Asn1Tags.Sequence));

            // TODO Validate version?
            //DerInteger version = this.authData.getVersion();

            //
            // load the RecipientInfoStore
            //
            Asn1SetParser s         = authData.GetRecipientInfos();
            IList         baseInfos = new ArrayList();

            IAsn1Convertible entry;

            while ((entry = s.ReadObject()) != null)
            {
                baseInfos.Add(RecipientInfo.GetInstance(entry.ToAsn1Object()));
            }

            this.macAlg = authData.GetMacAlgorithm();

            //
            // read the encrypted content info
            //
            ContentInfoParser data = authData.GetEnapsulatedContentInfo();

            //
            // prime the recipients
            //
            Stream contentStream = ((Asn1OctetStringParser)data.GetContent(Asn1Tags.OctetString)).GetOctetStream();
            IList  infos         = CmsEnvelopedHelper.ReadRecipientInfos(
                baseInfos, contentStream, null, macAlg, null);

            _recipientInfoStore = new RecipientInformationStore(infos);
        }
		private static Asn1Set GetAsn1Set(
			Asn1SetParser asn1SetParser)
		{
			return asn1SetParser == null
				?	null
				:	Asn1Set.GetInstance(asn1SetParser.ToAsn1Object());
		}
Esempio n. 26
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);
        }
Esempio n. 27
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.");
            }
        }
Esempio n. 28
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);
        }
Esempio n. 29
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.");
            }
        }
Esempio n. 30
0
        private static void WriteSetToGeneratorTagged(
			Asn1Generator		asn1Gen,
			Asn1SetParser		asn1SetParser,
			int					tagNo)
        {
            Asn1Set asn1Set = GetAsn1Set(asn1SetParser);

            if (asn1Set != null)
            {
                Asn1TaggedObject taggedObj = (asn1SetParser is BerSetParser)
                    ?	new BerTaggedObject(false, tagNo, asn1Set)
                    :	new DerTaggedObject(false, tagNo, asn1Set);

                WriteToGenerator(asn1Gen, taggedObj);
            }
        }
Esempio n. 31
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));
            }
        }