示例#1
0
        private static CryptographicAttributeObjectCollection CreateAttributeCollection(
            X509Certificate2 certificate,
            RSA privateKey,
            Action <Asn1EncodableVector> addAttributes)
        {
            var content    = new CmsProcessableByteArray(new byte[0]);
            var attributes = new Asn1EncodableVector();

            addAttributes(attributes);

            var signedAttributes   = new AttributeTable(attributes);
            var unsignedAttributes = new AttributeTable(DerSet.Empty);

            var generator = new CmsSignedDataGenerator();
            var keyPair   = DotNetUtilities.GetRsaKeyPair(privateKey);

            generator.AddSigner(
                keyPair.Private,
                DotNetUtilities.FromX509Certificate(certificate),
                Oids.Sha256,
                signedAttributes,
                unsignedAttributes);

            var bcSignedCms = generator.Generate(content, encapsulate: true);
            var signedCms   = new SignedCms();

            signedCms.Decode(bcSignedCms.GetEncoded());

            return(signedCms.SignerInfos[0].SignedAttributes);
        }