return header, value
def canonicalize_body(self, body):
- return body.rstrip() + "\r\n"
+ return body.rstrip("\r\n") + "\r\n"
class NoFWSCanonicalization(object):
def canonicalize_body(self, body):
body = self._body_wsp_re.sub("", body)
- body = self._body_orphan_cr_re.sub("\1", body)
+ body = self._body_orphan_cr_re.sub(r"\1", body)
body = body.rstrip()
return body + "\r\n" if body else ""
self._signed_headers = None
def sign(self, message):
- message = io.BytesIO(_BODY_LINES_RE.sub("\r\n", message))
canonicalization = NoFWSCanonicalization()
signer = self._key.signer(padding.PKCS1v15(), hashes.SHA1())
+
+ headers, body = rfc822_parse(message)
+
h_field = []
- for header_line in parsing.split(message):
- header, value = header_line.split(b":", 1)
+ for header, value in headers:
if self._signed_headers is None or header in self._signed_headers:
h_field.append(header)
signer.update(header)
signer.update(b":")
signer.update(value)
- m = message.read()
- body = canonicalization.canonicalize_body(m)
+ body = canonicalization.canonicalize_body(body)
if body:
signer.update(b"\r\n")
signer.update(body)
if current_time is None:
current_time = int(time.time())
- message = io.BytesIO(_BODY_LINES_RE.sub("\r\n", message))
signer = self._key.signer(padding.PKCS1v15(), hashes.SHA256())
+ headers, body = rfc822_parse(message)
h_field = []
- for header_line in parsing.split(message):
- header, value = header_line.split(b":", 1)
+ for header, value in headers:
if self._signed_headers is None or header in self._signed_headers:
h_field.append(header)
signer.update(v)
h = hashes.Hash(hashes.SHA256(), backend=default_backend())
- h.update(self._body_canonicalization.canonicalize_body(message.read()))
+ h.update(self._body_canonicalization.canonicalize_body(body))
dkim_header_value = _fold(
b" a=rsa-sha256; v=1; "
b"c={header_canonicalization.name}/{body_canonicalization.name}; "
dkim_header=v,
signature=_fold(base64.b64encode(signer.finalize()))
)
+
+_RFC822_NEWLINE_RE = re.compile(r"\r?\n")
+_RFC822_WS_RE = re.compile(r"[\x09\x20]")
+_RFC822_HEADER_RE = re.compile(r"([\x21-\x7e]+?):")
+
+def rfc822_parse(message):
+ headers = []
+ lines = _RFC822_NEWLINE_RE.split(message)
+ i = 0
+ while i < len(lines):
+ if len(lines[i]) == 0:
+ # End of headers, return what we have plus the body, excluding the
+ # blank line.
+ i += 1
+ break
+ if _RFC822_WS_RE.match(lines[i][0]):
+ headers[-1][1] += lines[i] + "\r\n"
+ else:
+ m = _RFC822_HEADER_RE.match(lines[i])
+ if m is not None:
+ headers.append([m.group(1), lines[i][m.end(0):] + "\r\n"])
+ elif lines[i].startswith("From "):
+ pass
+ else:
+ raise ValueError(
+ "Unexpected characters in RFC822 header: %s" % lines[i]
+ )
+ i += 1
+ return (headers, "\r\n".join(lines[i:]))
+import glob
+import io
+import os
+
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from nose.tools import assert_equal
from flanker import dkim
+from flanker.mime.message.headers import parsing
DUMMY_EMAIL = b"""
b"Jxf4OJGbWVXwSB2mNPfPyScpqJEL+z43vhx+/ZTWBWpj3TSAuHmOT4G7wrySLAZmfDcm"
b"je\r\n J00EP9NPpJOz2oUI8NJwozkUr6k=\r\n"
)
+
+
+def test_canonicalization():
+ path = os.path.join(
+ os.path.dirname(__file__), "fixtures", "messages", "dkim", "email.*"
+ )
+ for path in glob.glob(path):
+ if path.endswith(".01"):
+ continue
+ with open(path) as f:
+ contents = f.read()
+ with open(path.replace("email", "nofws.expected")) as f:
+ nofws_contents = f.read()
+ with open(path.replace("email", "simple.expected")) as f:
+ simple_contents = f.read()
+
+ assert_equal(
+ canonicalize_contents(dkim.NoFWSCanonicalization(), contents),
+ nofws_contents
+ )
+ assert_equal(
+ canonicalize_contents(dkim.SimpleCanonicalization(), contents),
+ simple_contents
+ )
+
+
+def canonicalize_contents(canonicalization_rule, contents):
+ headers, body = dkim.rfc822_parse(contents)
+ output = io.BytesIO()
+ for header, value in headers:
+ header, value = canonicalization_rule.canonicalize_header(
+ header, value)
+ output.write(b"{h}:{v}".format(h=header, v=value))
+ body = canonicalization_rule.canonicalize_body(body)
+ if body:
+ output.write(b"\r\n")
+ output.write(body)
+ return output.getvalue()
--- /dev/null
+This set of tests are intended to exercise your canonicalization
+code. The test files contain content that represents statements or
+actions in the DK specification. In particular, whitespace and
+continuation requirements are exercised.
+
+Each canonicalization test consists of the following files:
+
+email.* The original email
+nofws.expected.* The nofws canonicalized output
+simple.expected.* The simple canonicalized output
+purpose.* What this particular test is exercising
+
+If you have a program that exercises your canonicalization routines,
+then you can probably modify the run_tests script to exercise your
+program and compare the results automatically.
+
+The run_tests scripts assumes a program that takes the
+canonicalization algorithm as argument one, the input file from STDIN
+and writes the canonicalized text to STDOUT. It's a simple-minded
+script, hack as you see fit.
+
+Alternatively, run your canonicalization program such that it creates
+a file that you can diff against the *.expected.* files.
--- /dev/null
+From: <joe@example.net>
--- /dev/null
+From: <joe@example.net>
+
--- /dev/null
+From: <joe@example.net>
+
+A single line of body text
--- /dev/null
+From: <joe@example.net>
+
+A single line of body text
+
--- /dev/null
+From: <joe@example.net>
+
+A single line of body text
+
+
--- /dev/null
+From: <joe@example.net>
+
+A single line of body text
+
--- /dev/null
+From: <joe@example.net>
+
+Single line of body text
+
--- /dev/null
+From: <joe@example.net>
+
+First line of body text
+
+Second line of body text
--- /dev/null
+From: <joe@example.net>
+
+First line of body text
+
+Second line of body text
--- /dev/null
+From: <joe@example.net>
+
+First line of body text
+
+Second line of body text
--- /dev/null
+From: <joe@example.net>
+
+First line of body text
+
+
+Second line of body text
--- /dev/null
+From: <joe@example.net>
+
+First line of body text
+
+
+Second line of body text
+
+
--- /dev/null
+From: <joe@example.net>
+Subject: This line has trailing spaces
+Comment: This line has leading and trailing tabs
+
+Plain line of body text
--- /dev/null
+From: <joe@example.net>
+Subject: This line has trailing spaces
+Comment: This line has leading and trailing tabs
+
+Plain line of body text
+
+ Tabbed in line with trailing spaces
+
+ Spaced in line with trailing tabs
+
+ Following lines have spaces and tabs
+
+
+
+
--- /dev/null
+From: <joe@example.net>
+Subject: This subject that wraps
+ onto a new line with a space
+
+Single line of text
--- /dev/null
+From: <joe@example.net>
+Subject: This subject that wraps
+ onto a new line with a space
+ and then onto another line indented by a tab
+
+Single line of text
--- /dev/null
+From: <joe@example.net>
+Subject: This subject that wraps
+ onto a new line with a space
+ and then onto another line indented by a tab
+To: <larry@example.com>
+Comment: This line has leading spaces and wraps
+ to a second and
+ a third
+ and a fourth
+ and a firth
+ and lastly a sixed
+Message-Id: <12345.mail@example.net>
+
+Single line of text
--- /dev/null
+From: <joe@example.net>
+Subject: This subject that wraps
+ onto a new line with a space
+ and then onto another line indented by a tab
+To: <larry@example.com>
+Comment: This line has leading spaces and wraps
+ to a second and
+ a third
+ and a fourth
+ and a firth
+ and lastly a sixed
+Message-Id: <12345.mail@example.net>
+
+
+
+Single line of text
--- /dev/null
+From: <joe@example.net>
+Subject: This subject that wraps
+ onto a new line with a space
+ and then onto another line indented by a tab
+To: <larry@example.com>
+Comment: This line has leading spaces and wraps
+ to a second and
+ a third
+ and a fourth
+ and a firth
+ and lastly a sixed
+Message-Id: <12345.mail@example.net>
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
--- /dev/null
+From: <joe@example.net>
+Subject: This subject that wraps
+ onto a new line with a space
+ and then onto another line indented by a tab
+To: <larry@example.com>
+Comment: This line has leading spaces and wraps
+ to a second and
+ a third
+ and a fourth
+ and a firth
+ and lastly a sixed
+Message-Id: <12345.mail@example.net>
+
+This line has embedded s p a c e s and t a b and C\rR C\rReturns
+
+
+
--- /dev/null
+From:<joe@example.net>\r
--- /dev/null
+From:<joe@example.net>\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Asinglelineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Asinglelineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Asinglelineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Asinglelineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Singlelineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Firstlineofbodytext\r
+\r
+Secondlineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Firstlineofbodytext\r
+\r
+Secondlineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Firstlineofbodytext\r
+\r
+Secondlineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Firstlineofbodytext\r
+\r
+\r
+Secondlineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+\r
+Firstlineofbodytext\r
+\r
+\r
+Secondlineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thislinehastrailingspaces\r
+Comment:Thislinehasleadingandtrailingtabs\r
+\r
+Plainlineofbodytext\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thislinehastrailingspaces\r
+Comment:Thislinehasleadingandtrailingtabs\r
+\r
+Plainlineofbodytext\r
+\r
+Tabbedinlinewithtrailingspaces\r
+\r
+Spacedinlinewithtrailingtabs\r
+\r
+Followinglineshavespacesandtabs\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thissubjectthatwrapsontoanewlinewithaspace\r
+\r
+Singlelineoftext\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindentedbyatab\r
+\r
+Singlelineoftext\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindentedbyatab\r
+To:<larry@example.com>\r
+Comment:Thislinehasleadingspacesandwrapstoasecondandathirdandafourthandafirthandlastlyasixed\r
+Message-Id:<12345.mail@example.net>\r
+\r
+Singlelineoftext\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindentedbyatab\r
+To:<larry@example.com>\r
+Comment:Thislinehasleadingspacesandwrapstoasecondandathirdandafourthandafirthandlastlyasixed\r
+Message-Id:<12345.mail@example.net>\r
+\r
+\r
+\r
+Singlelineoftext\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindentedbyatab\r
+To:<larry@example.com>\r
+Comment:Thislinehasleadingspacesandwrapstoasecondandathirdandafourthandafirthandlastlyasixed\r
+Message-Id:<12345.mail@example.net>\r
--- /dev/null
+From:<joe@example.net>\r
+Subject:Thissubjectthatwrapsontoanewlinewithaspaceandthenontoanotherlineindentedbyatab\r
+To:<larry@example.com>\r
+Comment:Thislinehasleadingspacesandwrapstoasecondandathirdandafourthandafirthandlastlyasixed\r
+Message-Id:<12345.mail@example.net>\r
+\r
+ThislinehasembeddedspacesandtabandCRCReturns\r
--- /dev/null
+A completely empty file
\ No newline at end of file
--- /dev/null
+A single header, no body, no separator
--- /dev/null
+Single header with a separator
--- /dev/null
+Single header with a separator and one body text line
--- /dev/null
+Single header with a separator, one body text line and a trailing empty line
--- /dev/null
+Single header with a separator, one body text line and two trailing empty lines
--- /dev/null
+Single header with a separator, one body text line and a trailing empty line with spaces
--- /dev/null
+Single header with a separator, one body text line and a trailing empty line with spaces and tabs
--- /dev/null
+Single header with a separator, two body lines separated by a blank line
--- /dev/null
+Single header with a separator, two body lines separated by a line with spaces
--- /dev/null
+Single header with a separator, two body lines separated by a line with spaces and tabs
--- /dev/null
+Single header with a separator, two body lines separated by two lines with spaces and tabs
--- /dev/null
+Single header with a separator, two body lines separated by two lines with spaces and tabs,
+trailing two lines have spaces and tabs
+
--- /dev/null
+Trailing spaces in headers.
--- /dev/null
+Leading and trailing spaces and tabs in headers and body.
--- /dev/null
+Single header wrap.
+
+
--- /dev/null
+Multiple header wrap.
+
+
--- /dev/null
+Multiple header wrap with normal follow-on line
+
+
--- /dev/null
+Multiple header wrap with subsequent headers and multiple blank separators
+
+
+
--- /dev/null
+Multiple header wrap with subsequent headers and multiple blank separators and no body text
--- /dev/null
+Embedded CRs with whitespace - this may not work on some OSes...
\ No newline at end of file
--- /dev/null
+From: <joe@example.net>\r
+\r
+\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+A single line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+A single line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+A single line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+A single line of body text\r
+ \r
--- /dev/null
+From: <joe@example.net>\r
+\r
+Single line of body text\r
+ \r
--- /dev/null
+From: <joe@example.net>\r
+\r
+First line of body text\r
+\r
+Second line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+First line of body text\r
+ \r
+Second line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+First line of body text\r
+ \r
+Second line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+First line of body text\r
+ \r
+ \r
+Second line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+\r
+First line of body text\r
+ \r
+ \r
+Second line of body text\r
+ \r
+ \r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This line has trailing spaces \r
+Comment: This line has leading and trailing tabs \r
+\r
+Plain line of body text\r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This line has trailing spaces \r
+Comment: This line has leading and trailing tabs \r
+\r
+Plain line of body text\r
+\r
+ Tabbed in line with trailing spaces \r
+ \r
+ Spaced in line with trailing tabs \r
+\r
+ Following lines have spaces and tabs\r
+ \r
+ \r
+ \r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This subject that wraps\r
+ onto a new line with a space\r
+\r
+Single line of text\r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This subject that wraps\r
+ onto a new line with a space\r
+ and then onto another line indented by a tab\r
+\r
+Single line of text\r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This subject that wraps\r
+ onto a new line with a space\r
+ and then onto another line indented by a tab\r
+To: <larry@example.com>\r
+Comment: This line has leading spaces and wraps\r
+ to a second and\r
+ a third\r
+ and a fourth\r
+ and a firth\r
+ and lastly a sixed\r
+Message-Id: <12345.mail@example.net>\r
+\r
+Single line of text\r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This subject that wraps\r
+ onto a new line with a space\r
+ and then onto another line indented by a tab\r
+To: <larry@example.com>\r
+Comment: This line has leading spaces and wraps\r
+ to a second and\r
+ a third\r
+ and a fourth\r
+ and a firth\r
+ and lastly a sixed\r
+Message-Id: <12345.mail@example.net>\r
+\r
+ \r
+ \r
+Single line of text\r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This subject that wraps\r
+ onto a new line with a space\r
+ and then onto another line indented by a tab\r
+To: <larry@example.com>\r
+Comment: This line has leading spaces and wraps\r
+ to a second and\r
+ a third\r
+ and a fourth\r
+ and a firth\r
+ and lastly a sixed\r
+Message-Id: <12345.mail@example.net>\r
+\r
+\r
+\r
+\r
+ \r
+ \r
+ \r
+\r
+\r
+ \r
--- /dev/null
+From: <joe@example.net>\r
+Subject: This subject that wraps\r
+ onto a new line with a space\r
+ and then onto another line indented by a tab\r
+To: <larry@example.com>\r
+Comment: This line has leading spaces and wraps\r
+ to a second and\r
+ a third\r
+ and a fourth\r
+ and a firth\r
+ and lastly a sixed\r
+Message-Id: <12345.mail@example.net>\r
+\r
+This line has embedded s p a c e s and t a b and C\rR C\rReturns\r