An S/MIME cryptography context that uses Windows' System.Security.Cryptography.X509Certificates.X509Store.
An S/MIME cryptography context that uses Windows' System.Security.Cryptography.X509Certificates.X509Store.
Inheritance: MimeKit.Cryptography.SecureMimeContext
Ejemplo n.º 1
1
 /*
  * tries to sign a MimeEntity
  */
 public static MimeEntity SignEntity(MimeEntity entity, MailboxAddress signer)
 {
     using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
     {
         return MultipartSigned.Create(ctx, signer, DigestAlgorithm.Sha1, entity);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.Cryptography.WindowsSecureMimeDigitalSignature"/> class.
        /// </summary>
        /// <remarks>
        /// Creates a new <see cref="WindowsSecureMimeDigitalSignature"/>.
        /// </remarks>
        /// <param name="signerInfo">The information about the signer.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="signerInfo"/> is <c>null</c>.
        /// </exception>
        public WindowsSecureMimeDigitalSignature(SignerInfo signerInfo)
        {
            if (signerInfo == null)
            {
                throw new ArgumentNullException(nameof(signerInfo));
            }

            SignerInfo = signerInfo;

            var             algorithms = new List <EncryptionAlgorithm> ();
            DigestAlgorithm digestAlgo;

            if (signerInfo.SignedAttributes != null)
            {
                for (int i = 0; i < signerInfo.SignedAttributes.Count; i++)
                {
                    if (signerInfo.SignedAttributes[i].Oid.Value == CmsAttributes.SigningTime.Id)
                    {
                        var signingTime = signerInfo.SignedAttributes[i].Values[0] as Pkcs9SigningTime;

                        if (signingTime != null)
                        {
                            CreationDate = signingTime.SigningTime;
                        }
                    }
                    else if (signerInfo.SignedAttributes[i].Oid.Value == SmimeAttributes.SmimeCapabilities.Id)
                    {
                        foreach (var value in signerInfo.SignedAttributes[i].Values)
                        {
                            var sequences = (DerSequence)Asn1Object.FromByteArray(value.RawData);

                            foreach (Asn1Sequence sequence in sequences)
                            {
                                var identifier = Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier.GetInstance(sequence);
                                EncryptionAlgorithm algorithm;

                                if (BouncyCastleSecureMimeContext.TryGetEncryptionAlgorithm(identifier, out algorithm))
                                {
                                    algorithms.Add(algorithm);
                                }
                            }
                        }
                    }
                }
            }

            EncryptionAlgorithms = algorithms.ToArray();

            if (WindowsSecureMimeContext.TryGetDigestAlgorithm(signerInfo.DigestAlgorithm, out digestAlgo))
            {
                DigestAlgorithm = digestAlgo;
            }

            SignerCertificate = new WindowsSecureMimeDigitalCertificate(signerInfo.Certificate);
        }
 /*
  * returns true if email address can sign email
  */
 public static bool CanSign(string email)
 {
     try
     {
         MailboxAddress signer = new MailboxAddress("", email);
         TextPart entity = new TextPart("text");
         using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
         {
             MultipartSigned.Create(ctx, signer, DigestAlgorithm.Sha1, entity);
             return true;
         }
     }
     catch
     {
         return false;
     }
 }
 /*
  * returns true if email can be encrypted
  */
 public static string CanEncrypt(Node emails)
 {
     if (emails.Count == 0)
         return "there are no recipients";
     try
     {
         List<MailboxAddress> list = new List<MailboxAddress>();
         foreach (Node idx in emails)
             list.Add(new MailboxAddress("", idx.Get<string>()));
         TextPart entity = new TextPart("text");
         using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
         {
             ApplicationPkcs7Mime.Encrypt(ctx, list, entity);
         }
         return null;
     }
     catch(Exception err)
     {
         return err.Message;
     }
 }
 /*
  * tries to sign and encrypt a MimeEntity
  */
 public static MimeEntity SignAndEncryptEntity(MimeEntity entity, MailboxAddress signer, IEnumerable<MailboxAddress> list)
 {
     using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
     {
         return ApplicationPkcs7Mime.SignAndEncrypt(ctx, signer, DigestAlgorithm.Sha1, list, entity);
     }
 }
        /*
         * imports the given certificate file into certificate database
         */
        public static void ImportCertificate(string certificateFile, string password)
        {
            Node getBase = new Node();
            ActiveEvents.Instance.RaiseActiveEvent(
                typeof(CryptographyHelper),
                "magix.file.get-base-path",
                getBase);
            string basePath = getBase["path"].Get<string>();

            using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
            {
                using (FileStream stream = File.OpenRead(basePath + certificateFile))
                {
                    if (!string.IsNullOrEmpty(password))
                        ctx.Import(stream, password);
                    else
                        ctx.Import(stream);
                }
            }
        }
 /*
  * tries to encrypt a MimeEntity
  */
 public static MimeEntity EncryptEntity(MimeEntity entity, IEnumerable<MailboxAddress> list)
 {
     using (WindowsSecureMimeContext ctx = new WindowsSecureMimeContext(sys.StoreLocation.CurrentUser))
     {
         return ApplicationPkcs7Mime.Encrypt(ctx, list, entity);
     }
 }