Inheritance: IEmailSigner
Example #1
0
        public static MailMessage DomainKeySign([NotNull] this MailMessage message, DomainKeySigner signer)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            message.BodyEncoding    = signer.Encoding;
            message.SubjectEncoding = signer.Encoding;
            message.HeadersEncoding = signer.Encoding;

            var email = Email.Parse(message.GetText());

            if (!CanSign(email))
            {
                throw new InvalidOperationException("Unable to Domain Key sign the message");
            }

            var sig = signer.GenerateSignature(email);

            message.Headers.Prepend(DomainKeySigner.SignatureKey, sig);

            return(message);
        }
        public void AddDkimHeader(System.Net.Mail.MailMessage message)
        {
            if (string.IsNullOrWhiteSpace(GlobalConfiguration.Configuration.DkimPrivateKey))
            {
                return;
            }
            if (message.From.Host != GlobalConfiguration.Configuration.DkimDomain)
            {
                return;
            }

            try
            {
                var privateKey   = DKIM.PrivateKeySigner.Create(GlobalConfiguration.Configuration.DkimPrivateKey);
                var headerToSign = new string[] { "From", "To", "Subject" };

                var domainKeySigner = new DKIM.DomainKeySigner(privateKey,
                                                               GlobalConfiguration.Configuration.DkimDomain,
                                                               GlobalConfiguration.Configuration.DkimSelector,
                                                               headerToSign);

                var dkimSigner = new DkimSigner(privateKey,
                                                GlobalConfiguration.Configuration.DkimDomain,
                                                GlobalConfiguration.Configuration.DkimSelector,
                                                headerToSign);


                message.DomainKeySign(domainKeySigner);
                message.DkimSign(dkimSigner);
            }
            catch (Exception ex)
            {
                ex.Data.Add("domain", GlobalConfiguration.Configuration.DkimDomain);
                ex.Data.Add("from", message.From.Address);
                foreach (var emailTo in message.To)
                {
                    ex.Data.Add(string.Format("to{0}", message.To.IndexOf(emailTo)), emailTo.Address);
                }
                ex.Data.Add("subject", message.Subject);
                GlobalConfiguration.Configuration.Logger.Error(ex);
            }
        }
Example #3
0
        public FullEmailSigner(
            [NotNull] IPrivateKeySigner privateKey,
            [NotNull] Encoding encoding,
            [NotNull] string domain,
            [NotNull] string dkimSelector,
            [NotNull] string domainKeySelector,
            [NotNull] string[] headers)
        {
            if (privateKey == null)
            {
                throw new ArgumentNullException("privateKey");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }
            if (dkimSelector == null)
            {
                throw new ArgumentNullException("dkimSelector");
            }
            if (domainKeySelector == null)
            {
                throw new ArgumentNullException("domainKeySelector");
            }
            if (headers == null)
            {
                throw new ArgumentNullException("headers");
            }


            var dkim = new DkimSigner(
                privateKey,
                domain,
                dkimSelector,
                headers
                );



            //var debugger = new ConsoleDebug();

            //dkim.Debug = debugger;

            dkim.Encoding             = encoding;
            dkim.BodyCanonicalization = DkimCanonicalizationAlgorithm.Simple;

            _dkimSigner = dkim;


            var domainKeySigner = new DomainKeySigner(
                privateKey,
                domain,
                domainKeySelector,
                headers
                );

            domainKeySigner.Encoding         = encoding;
            domainKeySigner.Canonicalization = DomainKeyCanonicalizationAlgorithm.Nofws;

            _domainKeySigner = domainKeySigner;
        }
Example #4
0
		public FullEmailSigner(
            [NotNull] IPrivateKeySigner privateKey, 
            [NotNull] Encoding encoding, 
            [NotNull] string domain, 
            [NotNull] string dkimSelector, 
            [NotNull] string domainKeySelector, 
            [NotNull] string[] headers)
		{
		    if (privateKey == null) throw new ArgumentNullException("privateKey");
		    if (encoding == null) throw new ArgumentNullException("encoding");
		    if (domain == null) throw new ArgumentNullException("domain");
		    if (dkimSelector == null) throw new ArgumentNullException("dkimSelector");
		    if (domainKeySelector == null) throw new ArgumentNullException("domainKeySelector");
		    if (headers == null) throw new ArgumentNullException("headers");


		    var dkim = new DkimSigner(
				privateKey,
				domain,
				dkimSelector,
				headers
				);




			//var debugger = new ConsoleDebug();

			//dkim.Debug = debugger;

			dkim.Encoding = encoding;
			dkim.BodyCanonicalization = DkimCanonicalizationAlgorithm.Simple;

			_dkimSigner = dkim;


			var domainKeySigner = new DomainKeySigner(
				privateKey,
				domain,
				domainKeySelector,
				headers
				);

			domainKeySigner.Encoding = encoding;
			domainKeySigner.Canonicalization = DomainKeyCanonicalizationAlgorithm.Nofws;
			
			_domainKeySigner = domainKeySigner;

		}
Example #5
0
        public static MailMessage DomainKeySign([NotNull]this MailMessage message, DomainKeySigner signer)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (signer == null)
            {
                throw new ArgumentNullException("signer");
            }

            message.BodyEncoding = signer.Encoding;
            message.SubjectEncoding = signer.Encoding;
            message.HeadersEncoding = signer.Encoding;

            var email = Email.Parse(message.GetText());

            if(!CanSign(email))
            {
                throw new InvalidOperationException("Unable to Domain Key sign the message");
            }

            var sig = signer.GenerateSignature(email);

            message.Headers.Prepend(DomainKeySigner.SignatureKey, sig);

            return message;
        }
Example #6
0
        private static void GetDkimSigners()
        {
            try
            {
                var files = new DirectoryInfo(Config.PrivateKeyFolder).GetFiles("*.pem");

                if (files.Length == 0)
                    throw new Exception("No private key files (*.pem) found");

                foreach (var file in files)
                {
                    var key = file.Name.Replace(".pem", "");
                    var selector = key.Split('@')[0];
                    var domain = key.Split('@')[1];

                    var pKeySig = PrivateKeySigner.LoadFromFile(file.FullName);
                    var dkimSig = new DkimSigner(pKeySig, domain, selector, new[] { "From", "To", "Subject" });
                    var domainKeySig = new DomainKeySigner(pKeySig, domain, selector, new[] { "From", "To", "Subject" });

                    dkimSig.HeaderCanonicalization = DkimCanonicalizationAlgorithm.RELAXED;
                    dkimSig.BodyCanonicalization = DkimCanonicalizationAlgorithm.RELAXED;

                    _dkimSignerCache.TryAdd(key, dkimSig);
                    _domailKeySignerCache.TryAdd(key, domainKeySig);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Warn("Unable to turn on DKIM: {0}", ex.Message);
            }
        }