Skip to content

Commit a7ed34c

Browse files
committed
Improved encryption to use metadata.
1 parent 4162bdd commit a7ed34c

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/saml2/entity.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,31 @@ def _add_info(self, msg, **kwargs):
501501
else:
502502
msg.extension_elements = extensions
503503

504-
def fix_cert_str(self, tmp_cert_str):
505-
tmp_cert_str = "%s" % self.sec.my_cert
506-
tmp_cert_str = tmp_cert_str.replace("-----BEGIN CERTIFICATE-----\n", "")
507-
tmp_cert_str = tmp_cert_str.replace("\n-----END CERTIFICATE-----\n", "")
508-
return tmp_cert_str
504+
def _encrypt_assertion(self, encrypt_cert, sp_entity_id, response, node_xpath=None):
505+
_certs = []
506+
cbxs = CryptoBackendXmlSec1(self.config.xmlsec_binary)
507+
if encrypt_cert:
508+
_certs = []
509+
_certs.append(encrypt_cert)
510+
elif sp_entity_id is not None:
511+
_certs = self.metadata.certs(sp_entity_id, "any", "encrypt")
512+
exception = None
513+
for _cert in _certs:
514+
try:
515+
_, cert_file = make_temp(_cert, decode=False)
516+
response = cbxs.encrypt_assertion(response, self.sec.cert_file,
517+
pre_encryption_part(), node_xpath=node_xpath)
518+
return response
519+
except Exception as ex:
520+
exception = ex
521+
pass
522+
if exception:
523+
raise exception
509524

510525
def _response(self, in_response_to, consumer_url=None, status=None,
511-
issuer=None, sign=False, to_sign=None,
526+
issuer=None, sign=False, to_sign=None, sp_entity_id=None,
512527
encrypt_assertion=False, encrypt_assertion_self_contained=False, encrypted_advice_attributes=False,
513-
encrypt_cert=None,sign_assertion=None, **kwargs):
528+
encrypt_cert=None, encrypt_cert_assertion=None,sign_assertion=None, **kwargs):
514529
""" Create a Response.
515530
Encryption:
516531
encrypt_assertion must be true for encryption to be performed. If encrypted_advice_attributes also is
@@ -530,6 +545,7 @@ def _response(self, in_response_to, consumer_url=None, status=None,
530545
:return: A Response instance
531546
"""
532547

548+
533549
if not status:
534550
status = success_status_factory()
535551

@@ -582,16 +598,11 @@ def _response(self, in_response_to, consumer_url=None, status=None,
582598

583599
if to_sign_advice:
584600
response = signed_instance_factory(response, self.sec, to_sign_advice)
585-
tmp_cert_str = self.fix_cert_str("%s" % encrypt_cert)
586-
_, cert_file = make_temp("%s" % encrypt_cert, decode=False)
587-
response = cbxs.encrypt_assertion(response, cert_file,
588-
pre_encryption_part(), node_xpath=node_xpath)
589-
encrypt_advice = True
601+
response = self._encrypt_assertion(encrypt_cert, sp_entity_id, response, node_xpath=node_xpath)
590602
if encrypt_assertion:
591603
response = response_from_string(response)
592604
if encrypt_assertion:
593605
if encrypt_assertion_self_contained:
594-
assertion_tag = None
595606
try:
596607
assertion_tag = response.assertion._to_element_tree().tag
597608
except:
@@ -607,15 +618,7 @@ def _response(self, in_response_to, consumer_url=None, status=None,
607618
to_sign_assertion.append((class_name(response.assertion), response.assertion.id))
608619
if to_sign_assertion:
609620
response = signed_instance_factory(response, self.sec, to_sign_assertion)
610-
if encrypt_cert is not None and not encrypt_advice:
611-
_, cert_file = make_temp("%s" % encrypt_cert, decode=False)
612-
else:
613-
tmp_cert_str = self.fix_cert_str("%s" % self.sec.my_cert)
614-
_, cert_file = make_temp(tmp_cert_str, decode=False)
615-
616-
response = cbxs.encrypt_assertion(response, cert_file,
617-
pre_encryption_part())
618-
# template(response.assertion.id))
621+
response = self._encrypt_assertion(encrypt_cert_assertion, sp_entity_id, response)
619622
if sign:
620623
return signed_instance_factory(response, self.sec, sign_class)
621624
else:

src/saml2/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def _authn_response(self, in_response_to, consumer_url,
399399
self.session_db.store_assertion(assertion, to_sign)
400400

401401
return self._response(in_response_to, consumer_url, status, issuer,
402-
sign_response, to_sign, encrypt_assertion=encrypt_assertion,
402+
sign_response, to_sign,sp_entity_id=sp_entity_id, encrypt_assertion=encrypt_assertion,
403403
encrypt_cert=encrypt_cert,
404404
encrypt_assertion_self_contained=encrypt_assertion_self_contained,
405405
encrypted_advice_attributes=encrypted_advice_attributes,sign_assertion=sign_assertion,

tests/test_50_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test_encrypted_response_1(self):
678678
name_id=name_id,
679679
sign_response=False,
680680
sign_assertion=False,
681-
encrypt_assertion=True,
681+
encrypt_assertion=False,
682682
encrypt_assertion_self_contained=True,
683683
encrypted_advice_attributes=True,
684684
encrypt_cert=cert_str,

0 commit comments

Comments
 (0)