Example #1
0
        /*
         * see http://www.dkim.org/specs/rfc4871-dkimbase.html#dkim-sig-hdr
         *
         * */
        public string GenerateDkimHeaderValue(Email email, List <EmailHeader> headers)
        {
            // timestamp  - seconds since 00:00:00 on January 1, 1970 UTC
            var t = DateTime.Now.ToUniversalTime() - DateTime.SpecifyKind(DateTime.Parse("00:00:00 January 1, 1970"), DateTimeKind.Utc);

            var signatureValue = new StringBuilder();

            const string start = " ";
            const string end   = ";";

            signatureValue.Append("v=1;");

            // algorithm used
            signatureValue.Append(start);
            signatureValue.Append("a=");
            signatureValue.Append(_privateKeySigner.Algorithm);
            signatureValue.Append(end);

            // Canonicalization
            signatureValue.Append(start);
            signatureValue.Append("c=");
            signatureValue.Append(HeaderCanonicalization.ToString().ToLower());
            signatureValue.Append('/');
            signatureValue.Append(BodyCanonicalization.ToString().ToLower());
            signatureValue.Append(end);

            // signing domain
            signatureValue.Append(start);
            signatureValue.Append("d=");
            signatureValue.Append(_domain);
            signatureValue.Append(end);

            // headers to be signed
            signatureValue.Append(start);
            signatureValue.Append("h=");
            foreach (var header in headers)
            {
                signatureValue.Append(header.Key);
                signatureValue.Append(':');
            }
            signatureValue.Length--;
            signatureValue.Append(end);

            // i=identity
            // not supported

            // l=body length
            //not supported

            // public key location
            signatureValue.Append(start);
            signatureValue.Append("q=dns/txt");
            signatureValue.Append(end);

            // selector
            signatureValue.Append(start);
            signatureValue.Append("s=");
            signatureValue.Append(_selector);
            signatureValue.Append(end);

            // time sent
            signatureValue.Append(start);
            signatureValue.Append("t=");
            signatureValue.Append((int)t.TotalSeconds);
            signatureValue.Append(end);

            // x=expiration
            // not supported

            // hash of body
            signatureValue.Append(start);
            signatureValue.Append("bh=");
            signatureValue.Append(SignBody(email));
            signatureValue.Append(end);

            // x=copied header fields
            // not supported

            signatureValue.Append(start);
            signatureValue.Append("b=");

            return(signatureValue.ToString());
        }
Example #2
0
        public MailHeader CreateHeader(IEnumerable <MailHeader> headers, string body, DateTime date)
        {
            var _headers = headers
                           .Where(h => required_headers.Contains(h.Key.Trim().ToLower()))
                           .ToArray();

            TimeSpan t = date -
                         DateTime.SpecifyKind(DateTime.Parse("00:00:00 January 1, 1970"), DateTimeKind.Utc);

            var value = new StringBuilder();

            const string start = " ";
            const string end   = ";";

            value.Append("v=1;");

            // algorithm used
            value.Append(start);
            value.Append("a=");
            value.Append(GetAlgorithmName());
            value.Append(end);

            // Canonicalization
            value.Append(start);
            value.Append("c=");
            value.Append(HeaderCanonicalization.ToString().ToLower());
            value.Append('/');
            value.Append(BodyCanonicalization.ToString().ToLower());
            value.Append(end);

            // signing domain
            value.Append(start);
            value.Append("d=");
            value.Append(Domain);
            value.Append(end).Append(Keyword.CRLF);

            // headers to be signed
            value.Append(start);
            value.Append("h=");
            foreach (var header in _headers)
            {
                value.Append(header.Key.ToLower());
                value.Append(':');
            }
            value.Length--;
            value.Append(end).Append(Keyword.CRLF);

            // public key location
            value.Append(start);
            value.Append("q=dns/txt");
            value.Append(end);

            // selector
            value.Append(start);
            value.Append("s=");
            value.Append(Selector);
            value.Append(end);

            // time sent
            value.Append(start);
            value.Append("t=");
            value.Append((int)t.TotalSeconds);
            value.Append(end);

            // hash of body
            value.Append(start);
            value.Append("bh=");
            value.Append(SignBody(body));
            value.Append(end).Append(Keyword.CRLF);

            value.Append(start);
            value.Append("b=");

            var x = new MailHeader(HeaderName.DkimSignature, value.ToString());

            x.Append(SignHeaders(_headers, x));
            return(x);
        }