Skip to content

Commit f308477

Browse files
committed
The xsd:id attribute value must not start with a number
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent ec2ccd7 commit f308477

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/saml2/sigver.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import itertools
99
import logging
1010
import os
11-
import uuid
1211
import six
12+
from uuid import uuid4 as gen_random_key
1313

1414
from time import mktime
1515
import pytz
@@ -1840,17 +1840,16 @@ def pre_encryption_part(msg_enc=TRIPLE_DES_CBC, key_enc=RSA_1_5, key_name='my-rs
18401840
:param key_name:
18411841
:return:
18421842
"""
1843-
ek_id = encrypted_key_id or str(uuid.uuid4())
1844-
ed_id = encrypted_data_id or str(uuid.uuid4())
1843+
ek_id = encrypted_key_id or "EK_{id}".format(id=gen_random_key())
1844+
ed_id = encrypted_data_id or "ED_{id}".format(id=gen_random_key())
18451845
msg_encryption_method = EncryptionMethod(algorithm=msg_enc)
18461846
key_encryption_method = EncryptionMethod(algorithm=key_enc)
18471847
encrypted_key = EncryptedKey(
1848-
id=ek_id,
1849-
encryption_method=key_encryption_method,
1850-
key_info=ds.KeyInfo(
1851-
key_name=ds.KeyName(text=key_name)),
1852-
cipher_data=CipherData(
1853-
cipher_value=CipherValue(text='')))
1848+
id=ek_id,
1849+
encryption_method=key_encryption_method,
1850+
key_info=ds.KeyInfo(key_name=ds.KeyName(text=key_name)),
1851+
cipher_data=CipherData(cipher_value=CipherValue(text='')),
1852+
)
18541853
key_info = ds.KeyInfo(encrypted_key=encrypted_key)
18551854
encrypted_data = EncryptedData(
18561855
id=ed_id,

tests/test_42_enc.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import re
2+
13
from contextlib import closing
4+
25
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
36
from saml2.server import Server
47
from saml2.sigver import pre_encryption_part, ASSERT_XPATH, EncryptError
@@ -9,7 +12,7 @@
912

1013
__author__ = 'roland'
1114

12-
TMPL_NO_HEADER = """<ns0:EncryptedData xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" Id="ED" Type="http://www.w3.org/2001/04/xmlenc#Element"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /><ns1:KeyInfo><ns0:EncryptedKey Id="EK"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /><ns1:KeyInfo><ns1:KeyName>my-rsa-key</ns1:KeyName></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedKey></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedData>"""
15+
TMPL_NO_HEADER = """<ns0:EncryptedData xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" Id="{ed_id}" Type="http://www.w3.org/2001/04/xmlenc#Element"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /><ns1:KeyInfo><ns0:EncryptedKey Id="{ek_id}"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /><ns1:KeyInfo><ns1:KeyName>my-rsa-key</ns1:KeyName></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedKey></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedData>"""
1316
TMPL = "<?xml version='1.0' encoding='UTF-8'?>\n%s" % TMPL_NO_HEADER
1417

1518
IDENTITY = {"eduPersonAffiliation": ["staff", "member"],
@@ -24,10 +27,37 @@
2427
}
2528

2629

27-
def test_pre_enc():
30+
def test_pre_enc_key_format():
31+
def the_xsd_ID_value_must_start_with_either_a_letter_or_underscore(id):
32+
result = re.match(r"^[a-zA-Z_]", id[0])
33+
return result
34+
35+
def the_xsd_ID_value_may_contain_only_letters_digits_underscores_hyphens_periods(id):
36+
result = re.match(r"^[a-zA-Z0-9._-]*$", id[1:])
37+
return result
38+
39+
tmpl = pre_encryption_part()
40+
for id in (tmpl.id, tmpl.key_info.encrypted_key.id):
41+
assert the_xsd_ID_value_must_start_with_either_a_letter_or_underscore(id)
42+
assert the_xsd_ID_value_may_contain_only_letters_digits_underscores_hyphens_periods(id)
43+
44+
45+
def test_pre_enc_with_pregenerated_key():
2846
tmpl = pre_encryption_part(encrypted_key_id="EK", encrypted_data_id="ED")
29-
print(tmpl)
30-
assert "%s" % tmpl in (TMPL_NO_HEADER, TMPL)
47+
expected = TMPL_NO_HEADER.format(
48+
ed_id=tmpl.id,
49+
ek_id=tmpl.key_info.encrypted_key.id,
50+
)
51+
assert str(tmpl) == expected
52+
53+
54+
def test_pre_enc_with_generated_key():
55+
tmpl = pre_encryption_part()
56+
expected = TMPL_NO_HEADER.format(
57+
ed_id=tmpl.id,
58+
ek_id=tmpl.key_info.encrypted_key.id,
59+
)
60+
assert str(tmpl) == expected
3161

3262

3363
def test_reshuffle_response():
@@ -41,7 +71,6 @@ def test_reshuffle_response():
4171

4272
resp2 = pre_encrypt_assertion(resp_)
4373

44-
print(resp2)
4574
assert resp2.encrypted_assertion.extension_elements
4675

4776

@@ -74,7 +103,6 @@ def test_enc1():
74103
crypto = CryptoBackendXmlSec1(xmlsec_path)
75104
(_stdout, _stderr, output) = crypto._run_xmlsec(com_list, [tmpl])
76105

77-
print(output)
78106
assert _stderr == ""
79107
assert _stdout == ""
80108

@@ -93,7 +121,6 @@ def test_enc2():
93121
enc_resp = crypto.encrypt_assertion(resp_, full_path("pubkey.pem"),
94122
pre_encryption_part())
95123

96-
print(enc_resp)
97124
assert enc_resp
98125

99126
if __name__ == "__main__":

0 commit comments

Comments
 (0)