Skip to content

Commit

Permalink
Feature to force binary canonicalization (#55)
Browse files Browse the repository at this point in the history
* Added partner setting to force canonicalize binary.

* Formatted with black
  • Loading branch information
chadgates authored Jul 28, 2022
1 parent 1a64e8f commit 7f11cb4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pyas2lib/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class Partner:
:param mdn_confirm_text: The text to be used in the MDN for successfully
processed messages received from this partner.
:param canonicalize_as_binary: force binary canonicalization for this partner
"""

as2_name: str
Expand All @@ -194,6 +196,7 @@ class Partner:
mdn_digest_alg: str = None
mdn_confirm_text: str = MDN_CONFIRM_TEXT
ignore_self_signed: bool = True
canonicalize_as_binary: bool = False

def __post_init__(self):
"""Run the post initialisation checks for this class."""
Expand Down Expand Up @@ -638,7 +641,10 @@ def parse(self, raw_content, find_org_cb, find_partner_cb, find_message_cb=None)

# Verify the message, first using raw message and if it fails
# then convert to canonical form and try again
mic_content = canonicalize(self.payload)
mic_content = canonicalize(
self.payload,
canonicalize_as_binary=self.sender.canonicalize_as_binary,
)
verify_cert = self.sender.load_verify_cert()
self.digest_alg = verify_message(mic_content, signature, verify_cert)

Expand Down
8 changes: 6 additions & 2 deletions pyas2lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,19 @@ def mime_to_bytes(msg: message.Message, email_policy: policy.Policy = policy.HTT
return fp.getvalue()


def canonicalize(email_message: message.Message):
def canonicalize(email_message: message.Message, canonicalize_as_binary: bool = False):
"""
Function to convert an email Message to standard format string/
:param email_message: email.message.Message to be converted to standard string
:param canonicalize_as_binary: force binary canonicalization
:return: the standard representation of the email message in bytes
"""

if email_message.get("Content-Transfer-Encoding") == "binary":
if (
email_message.get("Content-Transfer-Encoding") == "binary"
or canonicalize_as_binary
):
message_header = ""
message_body = email_message.get_payload(decode=True)
for k, v in email_message.items():
Expand Down

0 comments on commit 7f11cb4

Please sign in to comment.