import base64
-import re
+import regex as re
import time
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
-_BODY_LINES_RE = re.compile(r"\r?\n")
+_BODY_TRAILING_WSP = re.compile(r"[\t ]+\r\n")
+_BODY_WSP_RE = re.compile(r"[\t ]+")
class SimpleCanonicalization(object):
return body.rstrip("\r\n") + "\r\n"
+class RelaxedCanonicalization(object):
+ name = "relaxed"
+
+ def canonicalize_header(self, header, value):
+ header = header.lower()
+ value = _BODY_WSP_RE.sub(" ", value.replace("\r\n", ""))
+ return header, value.strip() + b"\r\n"
+
+ def canonicalize_body(self, body):
+ body = _BODY_TRAILING_WSP.sub("\r\n", body)
+ body = _BODY_WSP_RE.sub(" ", body)
+ body = body.rstrip("\r\n")
+ return body + b"\r\n" if body else b""
+
+
class NoFWSCanonicalization(object):
_header_fws_re = re.compile(r"[\t \r\n]+")
- _body_wsp_re = re.compile(r"[\t ]+")
_body_orphan_cr_re = re.compile(b"\r([^\n])")
def canonicalize_header(self, header, value):
return header, self._header_fws_re.sub("", value) + "\r\n"
def canonicalize_body(self, body):
- body = self._body_wsp_re.sub("", body)
+ body = _BODY_WSP_RE.sub("", body)
body = self._body_orphan_cr_re.sub(r"\1", body)
body = body.rstrip()
return body + "\r\n" if body else ""
nofws_contents = f.read()
with open(path.replace("email", "simple.expected")) as f:
simple_contents = f.read()
+ with open(path.replace("email", "relaxed.expected")) as f:
+ relaxed_contents = f.read()
assert_equal(
canonicalize_contents(dkim.NoFWSCanonicalization(), contents),
canonicalize_contents(dkim.SimpleCanonicalization(), contents),
simple_contents
)
+ assert_equal(
+ canonicalize_contents(dkim.RelaxedCanonicalization(), contents),
+ relaxed_contents
+ )
def canonicalize_contents(canonicalization_rule, contents):
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)
+ output.write(b"\r\n")
+ output.write(body)
return output.getvalue()