@@ -205,7 +205,6 @@ def _get_xmlsec_cryptobackend(path=None, search_paths=None):
205
205
return CryptoBackendXmlSec1 (path )
206
206
207
207
208
- ID_ATTR = 'ID'
209
208
NODE_NAME = 'urn:oasis:names:tc:SAML:2.0:assertion:Assertion'
210
209
ENC_NODE_NAME = 'urn:oasis:names:tc:SAML:2.0:assertion:EncryptedAssertion'
211
210
ENC_KEY_CLASS = 'EncryptedKey'
@@ -653,7 +652,7 @@ def encrypt(self, text, recv_key, template, key_type):
653
652
def encrypt_assertion (self , statement , enc_key , template , key_type , node_xpath ):
654
653
raise NotImplementedError ()
655
654
656
- def decrypt (self , enctext , key_file ):
655
+ def decrypt (self , enctext , key_file , id_attr ):
657
656
raise NotImplementedError ()
658
657
659
658
def sign_statement (self , statement , node_name , key_file , node_id , id_attr ):
@@ -779,7 +778,7 @@ def encrypt_assertion(self, statement, enc_key, template, key_type='des-192', no
779
778
780
779
return output .decode ()
781
780
782
- def decrypt (self , enctext , key_file ):
781
+ def decrypt (self , enctext , key_file , id_attr ):
783
782
"""
784
783
785
784
:param enctext: XML document containing an encrypted part
@@ -794,7 +793,7 @@ def decrypt(self, enctext, key_file):
794
793
self .xmlsec ,
795
794
'--decrypt' ,
796
795
'--privkey-pem' , key_file ,
797
- '--id-attr:{id_attr}' .format (id_attr = ID_ATTR ),
796
+ '--id-attr:{id_attr}' .format (id_attr = id_attr ),
798
797
ENC_KEY_CLASS ,
799
798
]
800
799
@@ -1011,6 +1010,11 @@ def security_context(conf):
1011
1010
except AttributeError :
1012
1011
metadata = None
1013
1012
1013
+ try :
1014
+ id_attr = conf .id_attr_name
1015
+ except AttributeError :
1016
+ id_attr = None
1017
+
1014
1018
sec_backend = None
1015
1019
1016
1020
if conf .crypto_backend == 'xmlsec1' :
@@ -1069,7 +1073,8 @@ def security_context(conf):
1069
1073
validate_certificate = conf .validate_certificate ,
1070
1074
enc_key_files = enc_key_files ,
1071
1075
encryption_keypairs = conf .encryption_keypairs ,
1072
- sec_backend = sec_backend )
1076
+ sec_backend = sec_backend ,
1077
+ id_attr = id_attr )
1073
1078
1074
1079
1075
1080
def encrypt_cert_from_item (item ):
@@ -1239,6 +1244,7 @@ def update_cert(self, active=False, client_crt=None):
1239
1244
# openssl x509 -inform pem -noout -in server.crt -pubkey > publickey.pem
1240
1245
# openssl rsa -inform pem -noout -in publickey.pem -pubin -modulus
1241
1246
class SecurityContext (object ):
1247
+ DEFAULT_ID_ATTR_NAME = 'ID'
1242
1248
my_cert = None
1243
1249
1244
1250
def __init__ (
@@ -1257,7 +1263,10 @@ def __init__(
1257
1263
enc_key_files = None , enc_key_type = 'pem' ,
1258
1264
encryption_keypairs = None ,
1259
1265
enc_cert_type = 'pem' ,
1260
- sec_backend = None ):
1266
+ sec_backend = None ,
1267
+ id_attr = '' ):
1268
+
1269
+ self .id_attr = id_attr or SecurityContext .DEFAULT_ID_ATTR_NAME
1261
1270
1262
1271
self .crypto = crypto
1263
1272
assert (isinstance (self .crypto , CryptoBackend ))
@@ -1348,20 +1357,23 @@ def encrypt_assertion(self, statement, enc_key, template, key_type='des-192', no
1348
1357
return self .crypto .encrypt_assertion (
1349
1358
statement , enc_key , template , key_type , node_xpath )
1350
1359
1351
- def decrypt_keys (self , enctext , keys = None ):
1360
+ def decrypt_keys (self , enctext , keys = None , id_attr = '' ):
1352
1361
""" Decrypting an encrypted text by the use of a private key.
1353
1362
1354
1363
:param enctext: The encrypted text as a string
1355
1364
:return: The decrypted text
1356
1365
"""
1357
1366
_enctext = None
1358
1367
1368
+ if not id_attr :
1369
+ id_attr = self .id_attr
1370
+
1359
1371
if not isinstance (keys , list ):
1360
1372
keys = [keys ]
1361
1373
1362
1374
if self .enc_key_files is not None :
1363
1375
for _enc_key_file in self .enc_key_files :
1364
- _enctext = self .crypto .decrypt (enctext , _enc_key_file )
1376
+ _enctext = self .crypto .decrypt (enctext , _enc_key_file , id_attr )
1365
1377
if _enctext is not None and len (_enctext ) > 0 :
1366
1378
return _enctext
1367
1379
@@ -1370,28 +1382,31 @@ def decrypt_keys(self, enctext, keys=None):
1370
1382
if not isinstance (_key , six .binary_type ):
1371
1383
_key = str (_key ).encode ('ascii' )
1372
1384
_ , key_file = make_temp (_key , decode = False )
1373
- _enctext = self .crypto .decrypt (enctext , key_file )
1385
+ _enctext = self .crypto .decrypt (enctext , key_file , id_attr )
1374
1386
if _enctext is not None and len (_enctext ) > 0 :
1375
1387
return _enctext
1376
1388
1377
1389
return enctext
1378
1390
1379
- def decrypt (self , enctext , key_file = None ):
1391
+ def decrypt (self , enctext , key_file = None , id_attr = '' ):
1380
1392
""" Decrypting an encrypted text by the use of a private key.
1381
1393
1382
1394
:param enctext: The encrypted text as a string
1383
1395
:return: The decrypted text
1384
1396
"""
1385
1397
_enctext = None
1386
1398
1399
+ if not id_attr :
1400
+ id_attr = self .id_attr
1401
+
1387
1402
if self .enc_key_files is not None :
1388
1403
for _enc_key_file in self .enc_key_files :
1389
- _enctext = self .crypto .decrypt (enctext , _enc_key_file )
1404
+ _enctext = self .crypto .decrypt (enctext , _enc_key_file , id_attr )
1390
1405
if _enctext is not None and len (_enctext ) > 0 :
1391
1406
return _enctext
1392
1407
1393
1408
if key_file is not None and len (key_file .strip ()) > 0 :
1394
- _enctext = self .crypto .decrypt (enctext , key_file )
1409
+ _enctext = self .crypto .decrypt (enctext , key_file , id_attr )
1395
1410
if _enctext is not None and len (_enctext ) > 0 :
1396
1411
return _enctext
1397
1412
@@ -1415,7 +1430,7 @@ def verify_signature(self, signedtext, cert_file=None, cert_type='pem', node_nam
1415
1430
cert_type = self .cert_type
1416
1431
1417
1432
if not id_attr :
1418
- id_attr = ID_ATTR
1433
+ id_attr = self . id_attr
1419
1434
1420
1435
return self .crypto .validate_signature (
1421
1436
signedtext ,
@@ -1650,7 +1665,7 @@ def sign_statement(self, statement, node_name, key=None, key_file=None, node_id=
1650
1665
:return: The signed statement
1651
1666
"""
1652
1667
if not id_attr :
1653
- id_attr = ID_ATTR
1668
+ id_attr = self . id_attr
1654
1669
1655
1670
if not key_file and key :
1656
1671
_ , key_file = make_temp (str (key ).encode (), '.pem' )
0 commit comments