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
63+ # RSA_1_5 is considered deprecated
6264RSA_1_5 = 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
6365TRIPLE_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
6668class 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+
103113def 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
0 commit comments