Skip to content

Commit 499fd03

Browse files
authored
Extending Partner with Signature Algo and pass the setting to signing function.
* Fix github action build fail due to: https://stackoverflow.com/questions/71673404/importerror-cannot-import-name-unicodefun-from-click * Added partner setting to force canonicalize binary. * Formatted with black * #60 Extending Partner with Signature Algo and pass the setting to signing function.
1 parent af63a9b commit 499fd03

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

pyas2lib/as2.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
MDN_CONFIRM_TEXT,
2929
MDN_FAILED_TEXT,
3030
MDN_MODES,
31+
SIGNATUR_ALGORITHMS,
3132
SYNCHRONOUS_MDN,
3233
)
3334
from pyas2lib.exceptions import (
@@ -179,6 +180,9 @@ class Partner:
179180
180181
:param canonicalize_as_binary: force binary canonicalization for this partner
181182
183+
:param sign_alg: The signing algorithm to be used for generating the
184+
signature. (default `rsassa_pkcs1v15`)
185+
182186
"""
183187

184188
as2_name: str
@@ -197,6 +201,7 @@ class Partner:
197201
mdn_confirm_text: str = MDN_CONFIRM_TEXT
198202
ignore_self_signed: bool = True
199203
canonicalize_as_binary: bool = False
204+
sign_alg: str = "rsassa_pkcs1v15"
200205

201206
def __post_init__(self):
202207
"""Run the post initialisation checks for this class."""
@@ -225,6 +230,12 @@ def __post_init__(self):
225230
f"must be one of {DIGEST_ALGORITHMS}"
226231
)
227232

233+
if self.sign_alg and self.sign_alg not in SIGNATUR_ALGORITHMS:
234+
raise ImproperlyConfigured(
235+
f"Unsupported Signature Algorithm {self.sign_alg}, "
236+
f"must be one of {SIGNATUR_ALGORITHMS}"
237+
)
238+
228239
def load_verify_cert(self):
229240
"""Load the verification certificate of the partner and returned the parsed cert."""
230241
if self.validate_certs:
@@ -466,7 +477,10 @@ def build(
466477
)
467478
del signature["MIME-Version"]
468479
signature_data = sign_message(
469-
mic_content, self.digest_alg, self.sender.sign_key
480+
mic_content,
481+
self.digest_alg,
482+
self.sender.sign_key,
483+
self.receiver.sign_alg,
470484
)
471485
signature.set_payload(signature_data)
472486
encoders.encode_base64(signature)
@@ -865,7 +879,10 @@ def build(
865879
del signature["MIME-Version"]
866880

867881
signed_data = sign_message(
868-
canonicalize(self.payload), self.digest_alg, message.receiver.sign_key
882+
canonicalize(self.payload),
883+
self.digest_alg,
884+
message.receiver.sign_key,
885+
message.sender.sign_alg,
869886
)
870887
signature.set_payload(signed_data)
871888
encoders.encode_base64(signature)

pyas2lib/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
"aes_192_cbc",
2929
"aes_256_cbc",
3030
)
31+
SIGNATUR_ALGORITHMS = (
32+
"rsassa_pkcs1v15",
33+
"rsassa_pss",
34+
)

pyas2lib/tests/test_advanced.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ def test_partner_checks(self):
334334
with self.assertRaises(ImproperlyConfigured):
335335
as2.Partner("a partner", mdn_digest_alg="xyz")
336336

337+
with self.assertRaises(ImproperlyConfigured):
338+
as2.Partner("a partner", sign_alg="xyz")
339+
337340
def test_message_checks(self):
338341
"""Test the checks and other features of Message."""
339342
msg = as2.Message()

0 commit comments

Comments
 (0)