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; }
public static MailMessage DkimSign([NotNull]this MailMessage message, DkimSigner 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; // get email content and generate initial signature var email = Email.Parse(message.GetText()); if (!CanSign(email)) { throw new InvalidOperationException("Unable to Domain Key sign the message"); } var value = signer.GenerateDkimHeaderValue(email); // signature value get formatted so add dummy signature value then remove it message.Headers.Prepend(DkimSigner.SignatureKey, value + new string('0', 70)); email = message.Parse(); var formattedSig = email.Headers[DkimSigner.SignatureKey].Value; email.Headers[DkimSigner.SignatureKey].Value = formattedSig.Substring(0, formattedSig.Length - 70); // sign email value += signer.GenerateSignature(email); message.Headers.Set(DkimSigner.SignatureKey, value); return message; }
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); } }