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
63
+ # RSA_1_5 is considered deprecated
62
64
RSA_1_5 = 'http://www.w3.org/2001/04/xmlenc#rsa-1_5'
63
65
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"
65
67
66
68
class SigverError (SAMLError ):
67
69
pass
@@ -100,6 +102,14 @@ class CertificateError(SigverError):
100
102
pass
101
103
102
104
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
+
103
113
def read_file (* args , ** kwargs ):
104
114
with open (* args , ** kwargs ) as handler :
105
115
return handler .read ()
@@ -1085,10 +1095,8 @@ def encrypt_cert_from_item(item):
1085
1095
pass
1086
1096
1087
1097
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
1092
1100
return _encrypt_cert
1093
1101
1094
1102
@@ -1835,6 +1843,7 @@ def pre_signature_part(
1835
1843
if identifier :
1836
1844
signature .id = 'Signature{n}' .format (n = identifier )
1837
1845
1846
+ # XXX remove - do not embed the cert
1838
1847
if public_key :
1839
1848
x509_data = ds .X509Data (
1840
1849
x509_certificate = [ds .X509Certificate (text = public_key )])
@@ -1872,23 +1881,34 @@ 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 ):
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
+ ):
1884
1893
ek_id = encrypted_key_id or "EK_{id}" .format (id = gen_random_key ())
1885
1894
ed_id = encrypted_data_id or "ED_{id}" .format (id = gen_random_key ())
1886
1895
msg_encryption_method = EncryptionMethod (algorithm = msg_enc )
1887
1896
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
+
1888
1908
encrypted_key = EncryptedKey (
1889
1909
id = ek_id ,
1890
1910
encryption_method = key_encryption_method ,
1891
- key_info = ds . KeyInfo ( key_name = ds . KeyName ( text = key_name )) ,
1911
+ key_info = key_info ,
1892
1912
cipher_data = CipherData (cipher_value = CipherValue (text = '' )),
1893
1913
)
1894
1914
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
1897
1917
type = 'http://www.w3.org/2001/04/xmlenc#Element' ,
1898
1918
encryption_method = msg_encryption_method ,
1899
1919
key_info = key_info ,
1900
- cipher_data = CipherData (cipher_value = CipherValue (text = '' )))
1920
+ cipher_data = CipherData (cipher_value = CipherValue (text = '' )),
1921
+ )
1901
1922
return encrypted_data
1902
1923
1903
1924
0 commit comments