1
+ from subprocess import Popen , PIPE
2
+ from saml2 .sigver import get_xmlsec_binary
3
+ from saml2 .extension .algsupport import SigningMethod
4
+ from saml2 .extension .algsupport import DigestMethod
5
+
6
+ __author__ = 'roland'
7
+
8
+ DIGEST_METHODS = {
9
+ "hmac-md5" : 'http://www.w3.org/2001/04/xmldsig-more#md5' , # test framework only!
10
+ "hmac-sha1" : 'http://www.w3.org/2000/09/xmldsig#sha1' ,
11
+ "hmac-sha224" : 'http://www.w3.org/2001/04/xmldsig-more#sha224' ,
12
+ "hmac-sha256" : 'http://www.w3.org/2001/04/xmlenc#sha256' ,
13
+ "hmac-sha384" : 'http://www.w3.org/2001/04/xmldsig-more#sha384' ,
14
+ "hmac-sha512" : 'http://www.w3.org/2001/04/xmlenc#sha512' ,
15
+ "hmac-ripemd160" : 'http://www.w3.org/2001/04/xmlenc#ripemd160'
16
+ }
17
+
18
+ SIGNING_METHODS = {
19
+ "rsa-md5" : 'http://www.w3.org/2001/04/xmldsig-more#rsa-md5' ,
20
+ "rsa-ripemd160" : 'http://www.w3.org/2001/04/xmldsig-more#rsa-ripemd160' ,
21
+ "rsa-sha1" : 'http://www.w3.org/2000/09/xmldsig#rsa-sha1' ,
22
+ "rsa-sha224" : 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha224' ,
23
+ "rsa-sha256" : 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256' ,
24
+ "rsa-sha384" : 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384' ,
25
+ "rsa-sha512" : 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512' ,
26
+ "dsa-sha1" : 'http,//www.w3.org/2000/09/xmldsig#dsa-sha1' ,
27
+ 'dsa-sha256' : 'http://www.w3.org/2009/xmldsig11#dsa-sha256' ,
28
+ 'ecdsa_sha1' : 'http://www.w3.org/2001/04/xmldsig-more#ECDSA_sha1' ,
29
+ 'ecdsa_sha224' : 'http://www.w3.org/2001/04/xmldsig-more#ECDSA_sha224' ,
30
+ 'ecdsa_sha256' : 'http://www.w3.org/2001/04/xmldsig-more#ECDSA_sha256' ,
31
+ 'ecdsa_sha384' : 'http://www.w3.org/2001/04/xmldsig-more#ECDSA_sha384' ,
32
+ 'ecdsa_sha512' : 'http://www.w3.org/2001/04/xmldsig-more#ECDSA_sha512' ,
33
+ }
34
+
35
+
36
+ def get_algorithm_support (xmlsec ):
37
+ com_list = [xmlsec , '--list-transforms' ]
38
+ pof = Popen (com_list , stderr = PIPE , stdout = PIPE )
39
+
40
+ p_out = pof .stdout .read ().decode ('utf-8' )
41
+ p_err = pof .stderr .read ().decode ('utf-8' )
42
+
43
+ if not p_err :
44
+ p = p_out .split ('\n ' )
45
+ algs = [x .strip ('"' ) for x in p [1 ].split (',' )]
46
+ digest = []
47
+ signing = []
48
+ for alg in algs :
49
+ if alg in DIGEST_METHODS :
50
+ digest .append (alg )
51
+ elif alg in SIGNING_METHODS :
52
+ signing .append (alg )
53
+
54
+ return {"digest" : digest , "signing" : signing }
55
+
56
+ raise SystemError (p_err )
57
+
58
+
59
+ def algorithm_support_in_metadata (xmlsec ):
60
+ if xmlsec is None :
61
+ return []
62
+
63
+ support = get_algorithm_support (xmlsec )
64
+ element_list = []
65
+ for alg in support ["digest" ]:
66
+ element_list .append (DigestMethod (algorithm = DIGEST_METHODS [alg ]))
67
+ for alg in support ["signing" ]:
68
+ element_list .append (SigningMethod (algorithm = SIGNING_METHODS [alg ]))
69
+ return element_list
70
+
71
+ if __name__ == '__main__' :
72
+ xmlsec = get_xmlsec_binary ()
73
+ res = get_algorithm_support (xmlsec )
74
+ print (res )
75
+ for a in algorithm_support_in_metadata (xmlsec ):
76
+ print (a )
0 commit comments