From 999865c661c045fd8bec166c451d35c2bae6ee3d Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Fri, 22 Jul 2022 16:33:52 +0200 Subject: [PATCH 1/4] Fix github action build fail due to: https://stackoverflow.com/questions/71673404/importerror-cannot-import-name-unicodefun-from-click --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 59a607f..71bda72 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ "pylint==2.12.1", "pylama==8.3.7", "pylama-pylint==3.1.1", - "black==21.12b0", + "black==22.6.0", "pytest-black==0.3.12", ] From 85d0b28666a9533aa9dcc3a40ed8de4a38ee0ca1 Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Fri, 22 Jul 2022 20:10:09 +0200 Subject: [PATCH 2/4] Added partner setting to force canonicalize binary. --- pyas2lib/as2.py | 5 ++++- pyas2lib/utils.py | 9 +++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index 9eadd4e..cd2d915 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -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 @@ -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.""" @@ -638,7 +641,7 @@ 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) diff --git a/pyas2lib/utils.py b/pyas2lib/utils.py index 5947c55..5a2e999 100644 --- a/pyas2lib/utils.py +++ b/pyas2lib/utils.py @@ -48,8 +48,8 @@ def _handle_text(self, msg): newline replacements. """ if ( - msg.get_content_type() == "application/octet-stream" - or msg.get("Content-Transfer-Encoding") == "binary" + msg.get_content_type() == "application/octet-stream" + or msg.get("Content-Transfer-Encoding") == "binary" ): payload = msg.get_payload(decode=True) if payload is None: @@ -75,15 +75,16 @@ 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(): From 0b3aeae7f4efd20c1e1a130cf094166255fc7bfb Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Fri, 22 Jul 2022 20:13:02 +0200 Subject: [PATCH 3/4] Formatted with black --- pyas2lib/as2.py | 5 ++++- pyas2lib/utils.py | 9 ++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index cd2d915..cb058ad 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -641,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, canonicalize_as_binary=self.sender.canonicalize_as_binary) + 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) diff --git a/pyas2lib/utils.py b/pyas2lib/utils.py index 5a2e999..48ad8dd 100644 --- a/pyas2lib/utils.py +++ b/pyas2lib/utils.py @@ -48,8 +48,8 @@ def _handle_text(self, msg): newline replacements. """ if ( - msg.get_content_type() == "application/octet-stream" - or msg.get("Content-Transfer-Encoding") == "binary" + msg.get_content_type() == "application/octet-stream" + or msg.get("Content-Transfer-Encoding") == "binary" ): payload = msg.get_payload(decode=True) if payload is None: @@ -84,7 +84,10 @@ def canonicalize(email_message: message.Message, canonicalize_as_binary: bool = :return: the standard representation of the email message in bytes """ - if email_message.get("Content-Transfer-Encoding") == "binary" or canonicalize_as_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(): From adbd933dc6e6cb73c77dd8d5f92a11b714a00e52 Mon Sep 17 00:00:00 2001 From: Wassilios Lytras Date: Wed, 17 Apr 2024 23:01:25 +0200 Subject: [PATCH 4/4] https://github.com/abhishek-ram/pyas2-lib/issues/60 Extending Partner with Signature Algo and pass the setting to signing function. --- pyas2lib/as2.py | 21 +++++++++++++++++++-- pyas2lib/constants.py | 4 ++++ pyas2lib/tests/test_advanced.py | 3 +++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index 12ebac7..338084d 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -28,6 +28,7 @@ MDN_CONFIRM_TEXT, MDN_FAILED_TEXT, MDN_MODES, + SIGNATUR_ALGORITHMS, SYNCHRONOUS_MDN, ) from pyas2lib.exceptions import ( @@ -179,6 +180,9 @@ class Partner: :param canonicalize_as_binary: force binary canonicalization for this partner + :param sign_alg: The signing algorithm to be used for generating the + signature. (default `rsassa_pkcs1v15`) + """ as2_name: str @@ -197,6 +201,7 @@ class Partner: mdn_confirm_text: str = MDN_CONFIRM_TEXT ignore_self_signed: bool = True canonicalize_as_binary: bool = False + sign_alg: str = "rsassa_pkcs1v15" def __post_init__(self): """Run the post initialisation checks for this class.""" @@ -225,6 +230,12 @@ def __post_init__(self): f"must be one of {DIGEST_ALGORITHMS}" ) + if self.sign_alg and self.sign_alg not in SIGNATUR_ALGORITHMS: + raise ImproperlyConfigured( + f"Unsupported Signature Algorithm {self.sign_alg}, " + f"must be one of {SIGNATUR_ALGORITHMS}" + ) + def load_verify_cert(self): """Load the verification certificate of the partner and returned the parsed cert.""" if self.validate_certs: @@ -466,7 +477,10 @@ def build( ) del signature["MIME-Version"] signature_data = sign_message( - mic_content, self.digest_alg, self.sender.sign_key + mic_content, + self.digest_alg, + self.sender.sign_key, + self.receiver.sign_alg, ) signature.set_payload(signature_data) encoders.encode_base64(signature) @@ -865,7 +879,10 @@ def build( del signature["MIME-Version"] signed_data = sign_message( - canonicalize(self.payload), self.digest_alg, message.receiver.sign_key + canonicalize(self.payload), + self.digest_alg, + message.receiver.sign_key, + message.sender.sign_alg, ) signature.set_payload(signed_data) encoders.encode_base64(signature) diff --git a/pyas2lib/constants.py b/pyas2lib/constants.py index 53e6c1f..b5d4de2 100644 --- a/pyas2lib/constants.py +++ b/pyas2lib/constants.py @@ -28,3 +28,7 @@ "aes_192_cbc", "aes_256_cbc", ) +SIGNATUR_ALGORITHMS = ( + "rsassa_pkcs1v15", + "rsassa_pss", +) diff --git a/pyas2lib/tests/test_advanced.py b/pyas2lib/tests/test_advanced.py index 0bc6db7..f010ecd 100644 --- a/pyas2lib/tests/test_advanced.py +++ b/pyas2lib/tests/test_advanced.py @@ -334,6 +334,9 @@ def test_partner_checks(self): with self.assertRaises(ImproperlyConfigured): as2.Partner("a partner", mdn_digest_alg="xyz") + with self.assertRaises(ImproperlyConfigured): + as2.Partner("a partner", sign_alg="xyz") + def test_message_checks(self): """Test the checks and other features of Message.""" msg = as2.Message()