예제 #1
0
        private void DoClose()
        {
            Platform.Dispose(_out);
            _eiGen.Close();
            outer._digests.Clear();
            if (outer._certs.Count > 0)
            {
                Asn1Set obj = CmsUtilities.CreateBerSetFromList(outer._certs);
                WriteToGenerator(_sigGen, new BerTaggedObject(explicitly: false, 0, obj));
            }
            if (outer._crls.Count > 0)
            {
                Asn1Set obj2 = CmsUtilities.CreateBerSetFromList(outer._crls);
                WriteToGenerator(_sigGen, new BerTaggedObject(explicitly: false, 1, obj2));
            }
            IDictionaryEnumerator enumerator = outer._messageDigests.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
                    outer._messageHashes.Add(dictionaryEntry.Key, DigestUtilities.DoFinal((IDigest)dictionaryEntry.Value));
                }
            }
            finally
            {
                (enumerator as IDisposable)?.Dispose();
            }
            Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();

            foreach (DigestAndSignerInfoGeneratorHolder signerInf in outer._signerInfs)
            {
                AlgorithmIdentifier digestAlgorithm = signerInf.DigestAlgorithm;
                byte[] array = (byte[])outer._messageHashes[Helper.GetDigestAlgName(signerInf.digestOID)];
                outer._digests[signerInf.digestOID] = array.Clone();
                asn1EncodableVector.Add(signerInf.signerInf.Generate(_contentOID, digestAlgorithm, array));
            }
            foreach (SignerInformation signer in outer._signers)
            {
                asn1EncodableVector.Add(signer.ToSignerInfo());
            }
            WriteToGenerator(_sigGen, new DerSet(asn1EncodableVector));
            _sigGen.Close();
            _sGen.Close();
        }
예제 #2
0
    public static CmsSignedData ReplaceCertificatesAndCrls(CmsSignedData signedData, IX509Store x509Certs, IX509Store x509Crls, IX509Store x509AttrCerts)
    {
        if (x509AttrCerts != null)
        {
            throw Platform.CreateNotImplementedException("Currently can't replace attribute certificates");
        }
        CmsSignedData cmsSignedData = new CmsSignedData(signedData);
        Asn1Set       certificates  = null;

        try
        {
            Asn1Set asn1Set = CmsUtilities.CreateBerSetFromList(CmsUtilities.GetCertificatesFromStore(x509Certs));
            if (asn1Set.Count != 0)
            {
                certificates = asn1Set;
            }
        }
        catch (X509StoreException e)
        {
            throw new CmsException("error getting certificates from store", e);
        }
        Asn1Set crls = null;

        try
        {
            Asn1Set asn1Set2 = CmsUtilities.CreateBerSetFromList(CmsUtilities.GetCrlsFromStore(x509Crls));
            if (asn1Set2.Count != 0)
            {
                crls = asn1Set2;
            }
        }
        catch (X509StoreException e2)
        {
            throw new CmsException("error getting CRLs from store", e2);
        }
        SignedData signedData2 = signedData.signedData;

        cmsSignedData.signedData  = new SignedData(signedData2.DigestAlgorithms, signedData2.EncapContentInfo, certificates, crls, signedData2.SignerInfos);
        cmsSignedData.contentInfo = new ContentInfo(cmsSignedData.contentInfo.ContentType, cmsSignedData.signedData);
        return(cmsSignedData);
    }
예제 #3
0
        /**
         * generate a signed object that for a CMS Signed Data
         * object  - if encapsulate is true a copy
         * of the message will be included in the signature. The content type
         * is set according to the OID represented by the string signedContentType.
         */
        public CmsSignedData Generate(
            string signedContentType,
            CmsProcessable content,
            bool encapsulate)
        {
            Asn1EncodableVector digestAlgs  = new Asn1EncodableVector();
            Asn1EncodableVector signerInfos = new Asn1EncodableVector();

            _digests.Clear(); // clear the current preserved digest state

            //
            // add the precalculated SignerInfo objects.
            //
            foreach (SignerInformation signer in _signers)
            {
                digestAlgs.Add(Helper.FixAlgID(signer.DigestAlgorithmID));
                signerInfos.Add(signer.ToSignerInfo());
            }

            //
            // add the SignerInfo objects
            //
            bool isCounterSignature = (signedContentType == null);

            DerObjectIdentifier contentTypeOID = isCounterSignature
                ? CmsObjectIdentifiers.Data
                : new DerObjectIdentifier(signedContentType);

            foreach (SignerInf signer in signerInfs)
            {
                try
                {
                    digestAlgs.Add(signer.DigestAlgorithmID);
                    signerInfos.Add(signer.ToSignerInfo(contentTypeOID, content, rand, isCounterSignature));
                }
                catch (IOException e)
                {
                    throw new CmsException("encoding error.", e);
                }
                catch (InvalidKeyException e)
                {
                    throw new CmsException("key inappropriate for signature.", e);
                }
                catch (SignatureException e)
                {
                    throw new CmsException("error creating signature.", e);
                }
                catch (CertificateEncodingException e)
                {
                    throw new CmsException("error creating sid.", e);
                }
            }

            Asn1Set certificates = null;

            if (_certs.Count != 0)
            {
                certificates = CmsUtilities.CreateBerSetFromList(_certs);
            }

            Asn1Set certrevlist = null;

            if (_crls.Count != 0)
            {
                certrevlist = CmsUtilities.CreateBerSetFromList(_crls);
            }

            Asn1OctetString octs = null;

            if (encapsulate)
            {
                MemoryStream bOut = new MemoryStream();
                if (content != null)
                {
                    try
                    {
                        content.Write(bOut);
                    }
                    catch (IOException e)
                    {
                        throw new CmsException("encapsulation error.", e);
                    }
                }
                octs = new BerOctetString(bOut.ToArray());
            }

            ContentInfo encInfo = new ContentInfo(contentTypeOID, octs);

            SignedData sd = new SignedData(
                new DerSet(digestAlgs),
                encInfo,
                certificates,
                certrevlist,
                new DerSet(signerInfos));

            ContentInfo contentInfo = new ContentInfo(CmsObjectIdentifiers.SignedData, sd);

            return(new CmsSignedData(content, contentInfo));
        }
    public CmsSignedData Generate(string signedContentType, CmsProcessable content, bool encapsulate)
    {
        Asn1EncodableVector asn1EncodableVector  = new Asn1EncodableVector();
        Asn1EncodableVector asn1EncodableVector2 = new Asn1EncodableVector();

        _digests.Clear();
        foreach (SignerInformation signer in _signers)
        {
            asn1EncodableVector.Add(Helper.FixAlgID(signer.DigestAlgorithmID));
            asn1EncodableVector2.Add(signer.ToSignerInfo());
        }
        DerObjectIdentifier contentType = (signedContentType == null) ? null : new DerObjectIdentifier(signedContentType);

        foreach (SignerInf signerInf in signerInfs)
        {
            try
            {
                asn1EncodableVector.Add(signerInf.DigestAlgorithmID);
                asn1EncodableVector2.Add(signerInf.ToSignerInfo(contentType, content, rand));
            }
            catch (IOException e)
            {
                throw new CmsException("encoding error.", e);
            }
            catch (InvalidKeyException e2)
            {
                throw new CmsException("key inappropriate for signature.", e2);
            }
            catch (SignatureException e3)
            {
                throw new CmsException("error creating signature.", e3);
            }
            catch (CertificateEncodingException e4)
            {
                throw new CmsException("error creating sid.", e4);
            }
        }
        Asn1Set certificates = null;

        if (_certs.Count != 0)
        {
            certificates = CmsUtilities.CreateBerSetFromList(_certs);
        }
        Asn1Set crls = null;

        if (_crls.Count != 0)
        {
            crls = CmsUtilities.CreateBerSetFromList(_crls);
        }
        Asn1OctetString content2 = null;

        if (encapsulate)
        {
            MemoryStream memoryStream = new MemoryStream();
            if (content != null)
            {
                try
                {
                    content.Write(memoryStream);
                }
                catch (IOException e5)
                {
                    throw new CmsException("encapsulation error.", e5);
                }
            }
            content2 = new BerOctetString(memoryStream.ToArray());
        }
        ContentInfo contentInfo = new ContentInfo(contentType, content2);
        SignedData  content3    = new SignedData(new DerSet(asn1EncodableVector), contentInfo, certificates, crls, new DerSet(asn1EncodableVector2));
        ContentInfo sigData     = new ContentInfo(CmsObjectIdentifiers.SignedData, content3);

        return(new CmsSignedData(content, sigData));
    }