77import itertools
88import logging
99import os
10+ import re
1011import six
1112from uuid import uuid4 as gen_random_key
1213from time import mktime
5960
6061SIG = '{{{ns}#}}{attribute}' .format (ns = ds .NAMESPACE , attribute = 'Signature' )
6162
62- RSA_1_5 = 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
63- TRIPLE_DES_CBC = 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc '
63+ # DEPRECATED
64+ # RSA_1_5 = 'http://www.w3.org/2001/04/xmlenc#rsa-1_5 '
6465
66+ TRIPLE_DES_CBC = 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
67+ RSA_OAEP_MGF1P = "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"
6568
6669class SigverError (SAMLError ):
6770 pass
@@ -100,6 +103,14 @@ class CertificateError(SigverError):
100103 pass
101104
102105
106+ def get_pem_wrapped_unwrapped (cert ):
107+ begin_cert = "-----BEGIN CERTIFICATE-----\n "
108+ end_cert = "\n -----END CERTIFICATE-----\n "
109+ unwrapped_cert = re .sub (f'{ begin_cert } |{ end_cert } ' , '' , cert )
110+ wrapped_cert = f'{ begin_cert } { unwrapped_cert } { end_cert } '
111+ return wrapped_cert , unwrapped_cert
112+
113+
103114def read_file (* args , ** kwargs ):
104115 with open (* args , ** kwargs ) as handler :
105116 return handler .read ()
@@ -1085,10 +1096,8 @@ def encrypt_cert_from_item(item):
10851096 pass
10861097
10871098 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-----'
1099+ wrapped_cert , unwrapped_cert = get_pem_wrapped_unwrapped (_encrypt_cert )
1100+ _encrypt_cert = wrapped_cert
10921101 return _encrypt_cert
10931102
10941103
@@ -1872,8 +1881,10 @@ 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 ):
1884+ def pre_encryption_part (msg_enc = TRIPLE_DES_CBC , key_enc = RSA_OAEP_MGF1P ,
1885+ key_name = 'my-rsa-key' ,
1886+ encrypted_key_id = None , encrypted_data_id = None ,
1887+ encrypt_cert = None ):
18771888 """
18781889
18791890 :param msg_enc:
@@ -1885,10 +1896,16 @@ def pre_encryption_part(msg_enc=TRIPLE_DES_CBC, key_enc=RSA_1_5, key_name='my-rs
18851896 ed_id = encrypted_data_id or "ED_{id}" .format (id = gen_random_key ())
18861897 msg_encryption_method = EncryptionMethod (algorithm = msg_enc )
18871898 key_encryption_method = EncryptionMethod (algorithm = key_enc )
1899+
1900+ enc_key_dict = dict (key_name = ds .KeyName (text = key_name ))
1901+ enc_key_dict ['x509_data' ] = ds .X509Data (
1902+ x509_certificate = ds .X509Certificate (text = encrypt_cert ))
1903+ key_info = ds .KeyInfo (** enc_key_dict )
1904+
18881905 encrypted_key = EncryptedKey (
18891906 id = ek_id ,
18901907 encryption_method = key_encryption_method ,
1891- key_info = ds . KeyInfo ( key_name = ds . KeyName ( text = key_name )) ,
1908+ key_info = key_info ,
18921909 cipher_data = CipherData (cipher_value = CipherValue (text = '' )),
18931910 )
18941911 key_info = ds .KeyInfo (encrypted_key = encrypted_key )
0 commit comments