/**
         * 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)
        {
            // NB: SecureRandom would be ignored since using existing signatures only
            CmsSignedDataStreamGenerator gen    = new CmsSignedDataStreamGenerator();
            CmsSignedDataParser          parser = new CmsSignedDataParser(original);

//			gen.AddDigests(parser.DigestOids);
            gen.AddSigners(signerInformationStore);

            CmsTypedStream signedContent = parser.GetSignedContent();
            bool           encapsulate   = (signedContent != null);
            Stream         contentOut    = gen.Open(outStr, parser.SignedContentType.Id, encapsulate);

            if (encapsulate)
            {
                Streams.PipeAll(signedContent.ContentStream, contentOut);
            }

            gen.AddAttributeCertificates(parser.GetAttributeCertificates("Collection"));
            gen.AddCertificates(parser.GetCertificates("Collection"));
            gen.AddCrls(parser.GetCrls("Collection"));

//			gen.AddSigners(parser.GetSignerInfos());

            contentOut.Close();

            return(outStr);
        }
Beispiel #2
0
        /**
         * return the collection of signers that are associated with the
         * signatures for the message.
         */
        public SignerInformationStore GetSignerInfos()
        {
            if (signerInfoStore == null)
            {
                IList   signerInfos = Platform.CreateArrayList();
                Asn1Set s           = signedData.SignerInfos;

                foreach (object obj in s)
                {
                    SignerInfo          info        = SignerInfo.GetInstance(obj);
                    DerObjectIdentifier contentType = signedData.EncapContentInfo.ContentType;

                    if (hashes == null)
                    {
                        signerInfos.Add(new SignerInformation(info, contentType, signedContent, null));
                    }
                    else
                    {
                        byte[] hash = (byte[])hashes[info.DigestAlgorithm.ObjectID.Id];

                        signerInfos.Add(new SignerInformation(info, contentType, null, new BaseDigestCalculator(hash)));
                    }
                }

                signerInfoStore = new SignerInformationStore(signerInfos);
            }

            return(signerInfoStore);
        }
Beispiel #3
0
 private CmsSignedData(
     CmsSignedData c)
 {
     this.signedData      = c.signedData;
     this.contentInfo     = c.contentInfo;
     this.signedContent   = c.signedContent;
     this.signerInfoStore = c.signerInfoStore;
 }
 /**
  * Add a store of precalculated signers to the generator.
  *
  * @param signerStore store of signers
  */
 public void AddSigners(
     SignerInformationStore signerStore)
 {
     foreach (SignerInformation o in signerStore.GetSigners())
     {
         _signers.Add(o);
         AddSignerCallback(o);
     }
 }
Beispiel #5
0
        /**
         * Replace the signerinformation store associated with this
         * CmsSignedData object 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.
         *
         * @param signedData the signed data object to be used as a base.
         * @param signerInformationStore the new signer information store to use.
         * @return a new signed data object.
         */
        public static CmsSignedData ReplaceSigners(
            CmsSignedData signedData,
            SignerInformationStore signerInformationStore)
        {
            //
            // copy
            //
            CmsSignedData cms = new CmsSignedData(signedData);

            //
            // replace the store
            //
            cms.signerInfoStore = signerInformationStore;

            //
            // replace the signers in the SignedData object
            //
            Asn1EncodableVector digestAlgs = new Asn1EncodableVector();
            Asn1EncodableVector vec        = new Asn1EncodableVector();

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

            Asn1Set      digests = new DerSet(digestAlgs);
            Asn1Set      signers = new DerSet(vec);
            Asn1Sequence sD      = (Asn1Sequence)signedData.signedData.ToAsn1Object();

            //
            // signers are the last item in the sequence.
            //
            vec = new Asn1EncodableVector(
                sD[0],                 // version
                digests);

            for (int i = 2; i != sD.Count - 1; i++)
            {
                vec.Add(sD[i]);
            }

            vec.Add(signers);

            cms.signedData = SignedData.GetInstance(new BerSequence(vec));

            //
            // replace the contentInfo with the new one
            //
            cms.contentInfo = new ContentInfo(cms.contentInfo.ContentType, cms.signedData);

            return(cms);
        }
Beispiel #6
0
        /**
         * Return a signer information object with passed in SignerInformationStore representing counter
         * signatures attached as an unsigned attribute.
         *
         * @param signerInformation the signerInfo to be used as the basis.
         * @param counterSigners signer info objects carrying counter signature.
         * @return a copy of the original SignerInformationObject with the changed attributes.
         */
        public static SignerInformation AddCounterSigners(
            SignerInformation signerInformation,
            SignerInformationStore counterSigners)
        {
            // TODO Perform checks from RFC 3852 11.4

            SignerInfo sInfo = signerInformation.info;

            Asn1.Cms.AttributeTable unsignedAttr = signerInformation.UnsignedAttributes;
            Asn1EncodableVector     v;

            if (unsignedAttr != null)
            {
                v = unsignedAttr.ToAsn1EncodableVector();
            }
            else
            {
                v = new Asn1EncodableVector();
            }

            Asn1EncodableVector sigs = new Asn1EncodableVector();

            foreach (SignerInformation sigInf in counterSigners.GetSigners())
            {
                sigs.Add(sigInf.ToSignerInfo());
            }

            v.Add(new Asn1.Cms.Attribute(CmsAttributes.CounterSignature, new DerSet(sigs)));

            return(new SignerInformation(
                       new SignerInfo(
                           sInfo.SignerID,
                           sInfo.DigestAlgorithm,
                           sInfo.AuthenticatedAttributes,
                           sInfo.DigestEncryptionAlgorithm,
                           sInfo.EncryptedDigest,
                           new DerSet(v)),
                       signerInformation.contentType,
                       signerInformation.content,
                       null));
        }
        /**
         * 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 = Platform.CreateArrayList();
                IDictionary hashes      = 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.ObjectID.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);
        }