Skip to content

Commit c89082f

Browse files
Merge pull request #778 from peppelinux/shibsp_enc
Replace encryption method rsa-1_5 with rsa-oaep-mgf1p Use `http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p` over `http://www.w3.org/2001/04/xmlenc#rsa-1_5`
2 parents 3d54fc7 + 82b921b commit c89082f

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

src/saml2/data/templates/template_enc.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
<EncryptedData
33
xmlns="http://www.w3.org/2001/04/xmlenc#"
44
Type="http://www.w3.org/2001/04/xmlenc#Element">
5-
<EncryptionMethod Algorithm=
6-
"http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
5+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
76
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
87
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
9-
<EncryptionMethod Algorithm=
10-
"http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
8+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
119
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
1210
<KeyName/>
1311
</KeyInfo>

src/saml2/entity.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
from saml2.sigver import SigverError
6666
from saml2.sigver import SignatureError
6767
from saml2.sigver import make_temp
68+
from saml2.sigver import get_pem_wrapped_unwrapped
6869
from saml2.sigver import pre_encryption_part
6970
from saml2.sigver import pre_signature_part
7071
from saml2.sigver import pre_encrypt_assertion
@@ -654,19 +655,19 @@ def _encrypt_assertion(self, encrypt_cert, sp_entity_id, response, node_xpath=No
654655
_certs = self.metadata.certs(sp_entity_id, "any", "encryption")
655656
exception = None
656657
for _cert in _certs:
658+
wrapped_cert, unwrapped_cert = get_pem_wrapped_unwrapped(_cert)
657659
try:
658-
begin_cert = "-----BEGIN CERTIFICATE-----\n"
659-
end_cert = "\n-----END CERTIFICATE-----\n"
660-
if begin_cert not in _cert:
661-
_cert = "%s%s" % (begin_cert, _cert)
662-
if end_cert not in _cert:
663-
_cert = "%s%s" % (_cert, end_cert)
664-
tmp = make_temp(_cert.encode('ascii'),
665-
decode=False,
666-
delete_tmpfiles=self.config.delete_tmpfiles)
667-
response = self.sec.encrypt_assertion(response, tmp.name,
668-
pre_encryption_part(),
669-
node_xpath=node_xpath)
660+
tmp = make_temp(
661+
wrapped_cert.encode('ascii'),
662+
decode=False,
663+
delete_tmpfiles=self.config.delete_tmpfiles,
664+
)
665+
response = self.sec.encrypt_assertion(
666+
response,
667+
tmp.name,
668+
pre_encryption_part(encrypt_cert=unwrapped_cert),
669+
node_xpath=node_xpath,
670+
)
670671
return response
671672
except Exception as ex:
672673
exception = ex

src/saml2/sigver.py

+37-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import itertools
88
import logging
99
import os
10+
import re
1011
import six
1112
from uuid import uuid4 as gen_random_key
1213
from time import mktime
@@ -59,9 +60,10 @@
5960

6061
SIG = '{{{ns}#}}{attribute}'.format(ns=ds.NAMESPACE, attribute='Signature')
6162

63+
# RSA_1_5 is considered deprecated
6264
RSA_1_5 = 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
6365
TRIPLE_DES_CBC = 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
64-
66+
RSA_OAEP_MGF1P = "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"
6567

6668
class SigverError(SAMLError):
6769
pass
@@ -100,6 +102,14 @@ class CertificateError(SigverError):
100102
pass
101103

102104

105+
def get_pem_wrapped_unwrapped(cert):
106+
begin_cert = "-----BEGIN CERTIFICATE-----\n"
107+
end_cert = "\n-----END CERTIFICATE-----\n"
108+
unwrapped_cert = re.sub(f'{begin_cert}|{end_cert}', '', cert)
109+
wrapped_cert = f'{begin_cert}{unwrapped_cert}{end_cert}'
110+
return wrapped_cert, unwrapped_cert
111+
112+
103113
def read_file(*args, **kwargs):
104114
with open(*args, **kwargs) as handler:
105115
return handler.read()
@@ -1085,10 +1095,8 @@ def encrypt_cert_from_item(item):
10851095
pass
10861096

10871097
if _encrypt_cert is not None:
1088-
if _encrypt_cert.find('-----BEGIN CERTIFICATE-----\n') == -1:
1089-
_encrypt_cert = '-----BEGIN CERTIFICATE-----\n' + _encrypt_cert
1090-
if _encrypt_cert.find('\n-----END CERTIFICATE-----') == -1:
1091-
_encrypt_cert = _encrypt_cert + '\n-----END CERTIFICATE-----'
1098+
wrapped_cert, unwrapped_cert = get_pem_wrapped_unwrapped(_encrypt_cert)
1099+
_encrypt_cert = wrapped_cert
10921100
return _encrypt_cert
10931101

10941102

@@ -1835,6 +1843,7 @@ def pre_signature_part(
18351843
if identifier:
18361844
signature.id = 'Signature{n}'.format(n=identifier)
18371845

1846+
# XXX remove - do not embed the cert
18381847
if public_key:
18391848
x509_data = ds.X509Data(
18401849
x509_certificate=[ds.X509Certificate(text=public_key)])
@@ -1872,23 +1881,34 @@ def pre_signature_part(
18721881
# </EncryptedData>
18731882

18741883

1875-
def pre_encryption_part(msg_enc=TRIPLE_DES_CBC, key_enc=RSA_1_5, key_name='my-rsa-key',
1876-
encrypted_key_id=None, encrypted_data_id=None):
1877-
"""
1878-
1879-
:param msg_enc:
1880-
:param key_enc:
1881-
:param key_name:
1882-
:return:
1883-
"""
1884+
def pre_encryption_part(
1885+
*,
1886+
msg_enc=TRIPLE_DES_CBC,
1887+
key_enc=RSA_OAEP_MGF1P,
1888+
key_name='my-rsa-key',
1889+
encrypted_key_id=None,
1890+
encrypted_data_id=None,
1891+
encrypt_cert=None,
1892+
):
18841893
ek_id = encrypted_key_id or "EK_{id}".format(id=gen_random_key())
18851894
ed_id = encrypted_data_id or "ED_{id}".format(id=gen_random_key())
18861895
msg_encryption_method = EncryptionMethod(algorithm=msg_enc)
18871896
key_encryption_method = EncryptionMethod(algorithm=key_enc)
1897+
1898+
x509_data = (
1899+
ds.X509Data(x509_certificate=ds.X509Certificate(text=encrypt_cert))
1900+
if encrypt_cert
1901+
else None
1902+
)
1903+
key_info = ds.KeyInfo(
1904+
key_name=ds.KeyName(text=key_name),
1905+
x509_data=x509_data,
1906+
)
1907+
18881908
encrypted_key = EncryptedKey(
18891909
id=ek_id,
18901910
encryption_method=key_encryption_method,
1891-
key_info=ds.KeyInfo(key_name=ds.KeyName(text=key_name)),
1911+
key_info=key_info,
18921912
cipher_data=CipherData(cipher_value=CipherValue(text='')),
18931913
)
18941914
key_info = ds.KeyInfo(encrypted_key=encrypted_key)
@@ -1897,7 +1917,8 @@ def pre_encryption_part(msg_enc=TRIPLE_DES_CBC, key_enc=RSA_1_5, key_name='my-rs
18971917
type='http://www.w3.org/2001/04/xmlenc#Element',
18981918
encryption_method=msg_encryption_method,
18991919
key_info=key_info,
1900-
cipher_data=CipherData(cipher_value=CipherValue(text='')))
1920+
cipher_data=CipherData(cipher_value=CipherValue(text='')),
1921+
)
19011922
return encrypted_data
19021923

19031924

tests/test_42_enc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
__author__ = 'roland'
1414

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>"""
16-
TMPL = "<?xml version='1.0' encoding='UTF-8'?>\n%s" % TMPL_NO_HEADER
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-oaep-mgf1p" /><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>"""
16+
TMPL = f"<?xml version='1.0' encoding='UTF-8'?>\n{TMPL_NO_HEADER}"
1717

1818
IDENTITY = {"eduPersonAffiliation": ["staff", "member"],
1919
"surName": ["Jeter"], "givenName": ["Derek"],

0 commit comments

Comments
 (0)