private static byte[] SignData(byte[] data, X509Certificate2 signCertificate, DateTime? requestTimestamp = null)
 {
     var contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(data);
     var signedCms = new System.Security.Cryptography.Pkcs.SignedCms(contentInfo);
     var signer = new System.Security.Cryptography.Pkcs.CmsSigner(signCertificate)
     {
         DigestAlgorithm = GetSignatureAlgorithmForCert(signCertificate),
         IncludeOption = X509IncludeOption.EndCertOnly
     };
     if (requestTimestamp.HasValue)
         signer.SignedAttributes.Add(new System.Security.Cryptography.Pkcs.Pkcs9SigningTime(requestTimestamp.Value));
     signedCms.ComputeSignature(signer);
     return signedCms.Encode();
 }
Esempio n. 2
0
        private static byte[] SignData(byte[] data, X509Certificate2 signCertificate, DateTime?requestTimestamp = null)
        {
            var contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(data);
            var signedCms   = new System.Security.Cryptography.Pkcs.SignedCms(contentInfo);
            var signer      = new System.Security.Cryptography.Pkcs.CmsSigner(signCertificate)
            {
                DigestAlgorithm = GetSignatureAlgorithmForCert(signCertificate),
                IncludeOption   = X509IncludeOption.EndCertOnly
            };

            if (requestTimestamp.HasValue)
            {
                signer.SignedAttributes.Add(new System.Security.Cryptography.Pkcs.Pkcs9SigningTime(requestTimestamp.Value));
            }
            signedCms.ComputeSignature(signer);
            return(signedCms.Encode());
        }
Esempio n. 3
0
        private void TestPKCS7Signature(object sender, EventArgs e)
        {
            BodyPart b = sender as BodyPart;

            // Now look at the contents of the body as a signature
            System.Security.Cryptography.Pkcs.SignedCms cms = new System.Security.Cryptography.Pkcs.SignedCms();

            cms.Decode(b.Data);

            foreach (var sig in cms.SignerInfos)
            {
                if (sig.Certificate.Subject.Contains(From.Address))
                {
                    TrustedSender = true;

                    break;
                }
            }
        }
        /// <inheritdoc />
        public byte[] Decrypt(byte[] data)
        {
            try
            {
                var env = new System.Security.Cryptography.Pkcs.EnvelopedCms();
                env.Decode(data);
                env.Decrypt(_allSenderCertificates);

                var decryptedData = env.ContentInfo.Content;
                var sig = new System.Security.Cryptography.Pkcs.SignedCms();
                sig.Decode(decryptedData);
                sig.CheckSignature(true);

                var verifiedData = sig.ContentInfo.Content;

                return verifiedData;
            }
            catch (Exception ex)
            {
                throw new ExtraEncryptionException("No certificate for decryption found.", ex);
            }
        }
Esempio n. 5
0
        /// <inheritdoc />
        public byte[] Decrypt(byte[] data)
        {
            try
            {
                var env = new System.Security.Cryptography.Pkcs.EnvelopedCms();
                env.Decode(data);
                env.Decrypt(_allSenderCertificates);

                var decryptedData = env.ContentInfo.Content;
                var sig           = new System.Security.Cryptography.Pkcs.SignedCms();
                sig.Decode(decryptedData);
                sig.CheckSignature(true);

                var verifiedData = sig.ContentInfo.Content;

                return(verifiedData);
            }
            catch (Exception ex)
            {
                throw new ExtraEncryptionException("No certificate for decryption found.", ex);
            }
        }