7
7
import itertools
8
8
import logging
9
9
import os
10
+ import re
10
11
import six
11
12
from uuid import uuid4 as gen_random_key
12
13
from time import mktime
59
60
60
61
SIG = '{{{ns}#}}{attribute}' .format (ns = ds .NAMESPACE , attribute = 'Signature' )
61
62
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 '
64
65
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"
65
68
66
69
class SigverError (SAMLError ):
67
70
pass
@@ -100,6 +103,14 @@ class CertificateError(SigverError):
100
103
pass
101
104
102
105
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
+
103
114
def read_file (* args , ** kwargs ):
104
115
with open (* args , ** kwargs ) as handler :
105
116
return handler .read ()
@@ -1085,10 +1096,8 @@ def encrypt_cert_from_item(item):
1085
1096
pass
1086
1097
1087
1098
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
1092
1101
return _encrypt_cert
1093
1102
1094
1103
@@ -1872,8 +1881,10 @@ def pre_signature_part(
1872
1881
# </EncryptedData>
1873
1882
1874
1883
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 ):
1877
1888
"""
1878
1889
1879
1890
: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
1885
1896
ed_id = encrypted_data_id or "ED_{id}" .format (id = gen_random_key ())
1886
1897
msg_encryption_method = EncryptionMethod (algorithm = msg_enc )
1887
1898
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
+
1888
1905
encrypted_key = EncryptedKey (
1889
1906
id = ek_id ,
1890
1907
encryption_method = key_encryption_method ,
1891
- key_info = ds . KeyInfo ( key_name = ds . KeyName ( text = key_name )) ,
1908
+ key_info = key_info ,
1892
1909
cipher_data = CipherData (cipher_value = CipherValue (text = '' )),
1893
1910
)
1894
1911
key_info = ds .KeyInfo (encrypted_key = encrypted_key )
0 commit comments