Skip to content

Commit 230abf9

Browse files
committed
Updated test structure
1 parent ee97390 commit 230abf9

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

openleadr/messaging.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,19 @@ def load_private_key(key_data, passphrase=None):
8080

8181
def get_signature_algorithm_from_private_key(key_data, passphrase=None, default_algorithm="rsa-sha256"):
8282
"""
83-
Derive a signature algorithm based on the private key type. Accepted key types are EC, DSA and RSA keys.
84-
Returns a string that can be used to lookup a signature algorithm by fragment.
85-
By default the lookup will return rsa-sha256, which is the default signature algorithm for XMLSigner objects.
83+
Derive a signature algorithm based on the private key type. Returns a string that can be used to lookup
84+
a signature algorithm by fragment. Algorithms are chosen based on NIST recommendations.
85+
86+
SignXML supports only RSA-, DSA- and EC-based signature methods. As XMLSigner uses RSA_SHA256 as default
87+
signature algorithm, a fragment that results in this algorithm is returned for unsupported keys.
8688
"""
8789
key = load_private_key(key_data, passphrase)
8890
if isinstance(key, rsa.RSAPrivateKey):
8991
return "rsa-sha256"
9092
elif isinstance(key, dsa.DSAPrivateKey):
9193
return "dsa-sha256"
9294
elif isinstance(key, ec.EllipticCurvePrivateKey):
93-
return "ecdsa-sha3-256"
95+
return "ecdsa-sha-256"
9496
elif isinstance(key, ed25519.Ed25519PrivateKey):
9597
logger.warning("ED25519 keys are not supported")
9698
elif isinstance(key, ed448.Ed448PrivateKey):

test/test_signature_algorithms.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
import pytest
2+
import warnings
23
from cryptography.hazmat.primitives import serialization
34
from cryptography.hazmat.primitives.asymmetric import rsa, dsa, ec, ed25519, ed448
45
from openleadr.messaging import get_signature_algorithm_from_private_key
56

67

7-
@pytest.mark.parametrize("key, expected_alg", [
8-
(rsa.generate_private_key(public_exponent=65537, key_size=2048), "rsa-sha256"),
9-
(dsa.generate_private_key(key_size=2048), "dsa-sha256"),
10-
(ec.generate_private_key(ec.SECP256R1()), "ecdsa-sha3-256"),
11-
(ed25519.Ed25519PrivateKey.generate(), "rsa-sha256"),
12-
(ed448.Ed448PrivateKey.generate(), "rsa-sha256"),
8+
def fxn():
9+
warnings.warn("deprecated", DeprecationWarning)
10+
11+
with warnings.catch_warnings(action="ignore"):
12+
fxn()
13+
14+
15+
test_keys = {
16+
"rsa": rsa.generate_private_key(public_exponent=65537, key_size=2048),
17+
"dsa": dsa.generate_private_key(key_size=2048),
18+
"ec": ec.generate_private_key(ec.SECP256R1()),
19+
"ed25519": ed25519.Ed25519PrivateKey.generate(),
20+
"ed448": ed448.Ed448PrivateKey.generate()
21+
}
22+
23+
24+
@pytest.mark.parametrize("key_type, expected_alg", [
25+
("rsa", "rsa-sha256"),
26+
("dsa", "dsa-sha256"),
27+
("ec", "ecdsa-sha-256"),
28+
("ed25519", "rsa-sha256"),
29+
("ed448", "rsa-sha256"),
1330
])
14-
def test_key_type_sign_alg_match(key, expected_alg):
31+
def test_key_type_sign_alg_match(key_type, expected_alg):
32+
test_key = test_keys[key_type]
1533
key_encoding = serialization.Encoding.PEM
1634
key_format = serialization.PrivateFormat.PKCS8
1735
key_encryption_alg = serialization.NoEncryption()
18-
key_bytes = key.private_bytes(key_encoding, key_format, key_encryption_alg)
36+
key_bytes = test_key.private_bytes(key_encoding, key_format, key_encryption_alg)
37+
1938
detected_alg = get_signature_algorithm_from_private_key(key_bytes)
2039

21-
assert detected_alg == expected_alg, f"Expected {expected_alg} but got {detected_alg}"
40+
assert detected_alg == expected_alg

0 commit comments

Comments
 (0)