Header GenerateArcMessageSignature(FormatOptions options, MimeMessage message, int instance, TimeSpan t, IList <string> headers) { if (message.MimeVersion == null && message.Body != null && message.Body.Headers.Count > 0) { message.MimeVersion = new Version(1, 0); } var value = CreateArcHeaderBuilder(instance); byte[] signature, hash; Header ams; value.AppendFormat("; d={0}; s={1}", Domain, Selector); value.AppendFormat("; c={0}/{1}", HeaderCanonicalizationAlgorithm.ToString().ToLowerInvariant(), BodyCanonicalizationAlgorithm.ToString().ToLowerInvariant()); value.AppendFormat("; t={0}", (long)t.TotalSeconds); using (var stream = new DkimSignatureStream(CreateSigningContext())) { using (var filtered = new FilteredStream(stream)) { filtered.Add(options.CreateNewLineFilter()); // write the specified message headers DkimVerifierBase.WriteHeaders(options, message, headers, HeaderCanonicalizationAlgorithm, filtered); value.AppendFormat("; h={0}", string.Join(":", headers.ToArray())); hash = message.HashBody(options, SignatureAlgorithm, BodyCanonicalizationAlgorithm, -1); value.AppendFormat("; bh={0}", Convert.ToBase64String(hash)); value.Append("; b="); ams = new Header(HeaderId.ArcMessageSignature, value.ToString()); switch (HeaderCanonicalizationAlgorithm) { case DkimCanonicalizationAlgorithm.Relaxed: DkimVerifierBase.WriteHeaderRelaxed(options, filtered, ams, true); break; default: DkimVerifierBase.WriteHeaderSimple(options, filtered, ams, true); break; } filtered.Flush(); } signature = stream.GenerateSignature(); ams.Value += Convert.ToBase64String(signature); return(ams); } }
void DkimSign(FormatOptions options, MimeMessage message, IList <string> headers) { var value = new StringBuilder("v=1"); var t = GetTimestamp(); byte[] signature, hash; Header dkim; options = options.Clone(); options.NewLineFormat = NewLineFormat.Dos; options.EnsureNewLine = true; switch (SignatureAlgorithm) { case DkimSignatureAlgorithm.Ed25519Sha256: value.Append("; a=ed25519-sha256"); break; case DkimSignatureAlgorithm.RsaSha256: value.Append("; a=rsa-sha256"); break; default: value.Append("; a=rsa-sha1"); break; } value.AppendFormat("; d={0}; s={1}", Domain, Selector); value.AppendFormat("; c={0}/{1}", HeaderCanonicalizationAlgorithm.ToString().ToLowerInvariant(), BodyCanonicalizationAlgorithm.ToString().ToLowerInvariant()); if (!string.IsNullOrEmpty(QueryMethod)) { value.AppendFormat("; q={0}", QueryMethod); } if (!string.IsNullOrEmpty(AgentOrUserIdentifier)) { value.AppendFormat("; i={0}", AgentOrUserIdentifier); } value.AppendFormat("; t={0}", t); using (var stream = new DkimSignatureStream(CreateSigningContext())) { using (var filtered = new FilteredStream(stream)) { filtered.Add(options.CreateNewLineFilter()); // write the specified message headers DkimVerifierBase.WriteHeaders(options, message, headers, HeaderCanonicalizationAlgorithm, filtered); value.AppendFormat("; h={0}", string.Join(":", headers.ToArray())); hash = message.HashBody(options, SignatureAlgorithm, BodyCanonicalizationAlgorithm, -1); value.AppendFormat("; bh={0}", Convert.ToBase64String(hash)); value.Append("; b="); dkim = new Header(HeaderId.DkimSignature, value.ToString()); message.Headers.Insert(0, dkim); switch (HeaderCanonicalizationAlgorithm) { case DkimCanonicalizationAlgorithm.Relaxed: DkimVerifierBase.WriteHeaderRelaxed(options, filtered, dkim, true); break; default: DkimVerifierBase.WriteHeaderSimple(options, filtered, dkim, true); break; } filtered.Flush(); } signature = stream.GenerateSignature(); dkim.Value += Convert.ToBase64String(signature); } }