コード例 #1
0
 internal protected virtual Asn1Set GetAttributeSet(
     AttributeTable attr)
 {
     return(attr == null
         ? null
         : new DerSet(attr.ToAsn1EncodableVector()));
 }
コード例 #2
0
            public override void Close()
            {
                Platform.Dispose(_out);

                // TODO Parent context(s) should really be closed explicitly

                _eiGen.Close();

                if (_outer.unprotectedAttributeGenerator != null)
                {
                    AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());

                    Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());

                    _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
                }

                _envGen.Close();
                _cGen.Close();
                base.Close();
            }
コード例 #3
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;
            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 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));
        }
コード例 #4
0
        /**
         * Return a signer information object with the passed in unsigned
         * attributes replacing the ones that are current associated with
         * the object passed in.
         *
         * @param signerInformation the signerInfo to be used as the basis.
         * @param unsignedAttributes the unsigned attributes to add.
         * @return a copy of the original SignerInformationObject with the changed attributes.
         */
        public static SignerInformation ReplaceUnsignedAttributes(
            SignerInformation signerInformation,
            AttributeTable unsignedAttributes)
        {
            SignerInfo sInfo        = signerInformation.info;
            Asn1Set    unsignedAttr = null;

            if (unsignedAttributes != null)
            {
                unsignedAttr = new DerSet(unsignedAttributes.ToAsn1EncodableVector());
            }

            return(new SignerInformation(
                       new SignerInfo(
                           sInfo.SignerID,
                           sInfo.DigestAlgorithm,
                           sInfo.AuthenticatedAttributes,
                           sInfo.DigestEncryptionAlgorithm,
                           sInfo.EncryptedDigest,
                           unsignedAttr),
                       signerInformation.contentType,
                       signerInformation.content,
                       null));
        }
コード例 #5
0
        /// <summary>
        /// Generate an enveloped object that contains a CMS Enveloped Data
        /// object using the passed in key generator.
        /// </summary>
        private CmsEnvelopedData Generate(
            CmsProcessable content,
            string encryptionOid,
            CipherKeyGenerator keyGen)
        {
            AlgorithmIdentifier encAlgId = null;
            KeyParameter        encKey;
            Asn1OctetString     encContent;

            try
            {
                byte[] encKeyBytes = keyGen.GenerateKey();
                encKey = ParameterUtilities.CreateKeyParameter(encryptionOid, encKeyBytes);

                Asn1Encodable asn1Params = GenerateAsn1Parameters(encryptionOid, encKeyBytes);

                ICipherParameters cipherParameters;
                encAlgId = GetAlgorithmIdentifier(
                    encryptionOid, encKey, asn1Params, out cipherParameters);

                IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid);
                cipher.Init(true, new ParametersWithRandom(cipherParameters, rand));

                MemoryStream bOut = new MemoryStream();
                CipherStream cOut = new CipherStream(bOut, null, cipher);

                content.Write(cOut);

                Platform.Dispose(cOut);

                encContent = new BerOctetString(bOut.ToArray());
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CmsException("key invalid in message.", e);
            }
            catch (IOException e)
            {
                throw new CmsException("exception decoding algorithm parameters.", e);
            }


            Asn1EncodableVector recipientInfos = new Asn1EncodableVector();

            foreach (RecipientInfoGenerator rig in recipientInfoGenerators)
            {
                try
                {
                    recipientInfos.Add(rig.Generate(encKey, rand));
                }
                catch (InvalidKeyException e)
                {
                    throw new CmsException("key inappropriate for algorithm.", e);
                }
                catch (GeneralSecurityException e)
                {
                    throw new CmsException("error making encrypted content.", e);
                }
            }

            EncryptedContentInfo eci = new EncryptedContentInfo(
                CmsObjectIdentifiers.Data,
                encAlgId,
                encContent);

            Asn1Set unprotectedAttrSet = null;

            if (unprotectedAttributeGenerator != null)
            {
                AttributeTable attrTable = unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());

                unprotectedAttrSet = new BerSet(attrTable.ToAsn1EncodableVector());
            }

            ContentInfo contentInfo = new ContentInfo(
                CmsObjectIdentifiers.EnvelopedData,
                new EnvelopedData(null, new DerSet(recipientInfos), eci, unprotectedAttrSet));

            return(new CmsEnvelopedData(contentInfo));
        }