44
55__author__ = 'leifj'
66
7- import xmlsec
87import pkg_resources
98import unittest
109import logging
1312import subprocess
1413import shutil
1514import tempfile
15+
1616from defusedxml import lxml
1717from lxml import etree
18- from . import find_alts , run_cmd
18+
19+ import xmlsec
20+ from xmlsec .test import paths_for_component
21+ from xmlsec .test import find_alts
22+ from xmlsec .test import run_cmd
23+
24+ from xmlsec .test .case import load_test_data
1925
2026try :
2127 from PyKCS11 import PyKCS11Error
2228 from PyKCS11 .LowLevel import CKR_PIN_INCORRECT
2329except ImportError :
2430 raise unittest .SkipTest ("PyKCS11 not installed" )
25- from xmlsec .test .case import load_test_data
26-
27- P11_MODULE = find_alts (['/usr/lib/libsofthsm.so' , '/usr/lib/softhsm/libsofthsm.so' , '/usr/lib/softhsm/libsofthsm2.so' ])
28- P11_ENGINE = find_alts (['/usr/lib/ssl/engines/libpkcs11.so' ,'/usr/lib/engines/engine_pkcs11.so' ])
29- P11_SPY = find_alts (['/usr/lib/pkcs11/pkcs11-spy.so' ])
30- PKCS11_TOOL = find_alts (['/usr/bin/pkcs11-tool' ])
31- OPENSC_TOOL = find_alts (['/usr/bin/opensc-tool' ])
32- SOFTHSM = find_alts (['/usr/bin/softhsm' ,'/usr/bin/softhsm2-util' ])
33- OPENSSL = find_alts (['/usr/bin/openssl' ])
3431
3532try :
3633 import xmlsec .pk11 as pk11
3734except Exception :
3835 raise unittest .SkipTest ("PyKCS11 not installed" )
3936
40- if OPENSSL is None :
41- raise unittest .SkipTest ("OpenSSL not installed" )
42-
43- if SOFTHSM is None :
44- raise unittest .SkipTest ("SoftHSM2 not installed" )
45-
46- if OPENSC_TOOL is None :
47- raise unittest .SkipTest ("OpenSC not installed" )
48-
49- if PKCS11_TOOL is None :
50- raise unittest .SkipTest ("pkcs11-tool not installed" )
5137
52- if P11_ENGINE is None :
53- raise unittest .SkipTest ("libengine-pkcs11-openssl is not installed" )
38+ component_default_paths = {
39+ 'P11_MODULE' : [
40+ '/usr/lib/libsofthsm.so' ,
41+ '/usr/lib/softhsm/libsofthsm.so' ,
42+ '/usr/lib/softhsm/libsofthsm2.so' ,
43+ ],
44+ 'P11_ENGINE' : [
45+ '/usr/lib/ssl/engines/libpkcs11.so' ,
46+ '/usr/lib/engines/engine_pkcs11.so' ,
47+ '/usr/lib/x86_64-linux-gnu/engines-1.1/pkcs11.so' ,
48+ ],
49+ 'P11_SPY' : [
50+ '/usr/lib/pkcs11/pkcs11-spy.so' ,
51+ ],
52+ 'PKCS11_TOOL' : [
53+ '/usr/bin/pkcs11-tool' ,
54+ ],
55+ 'OPENSC_TOOL' : [
56+ '/usr/bin/opensc-tool' ,
57+ ],
58+ 'SOFTHSM' : [
59+ '/usr/bin/softhsm' ,
60+ '/usr/bin/softhsm2-util' ,
61+ ],
62+ 'OPENSSL' : [
63+ '/usr/bin/openssl' ,
64+ ],
65+ }
66+
67+ component_path = {
68+ component : find_alts (
69+ paths_for_component (component , component_default_paths [component ])
70+ )
71+ for component in component_default_paths .keys ()
72+ }
73+
74+ if any (path is None for component , path in component_path .items ()):
75+ missing = [
76+ component
77+ for component , path in component_path .items ()
78+ if path is None
79+ ]
80+ raise unittest .SkipTest ("Required components missing: {}" .format (missing ))
5481
5582softhsm_version = 1
56- if SOFTHSM == '/usr/bin/softhsm2-util' :
83+ if component_path [ ' SOFTHSM' ] == '/usr/bin/softhsm2-util' :
5784 softhsm_version = 2
5885
59-
6086p11_test_files = []
6187softhsm_conf = None
6288server_cert_pem = None
@@ -69,13 +95,14 @@ def _tf():
6995 p11_test_files .append (f .name )
7096 return f .name
7197
98+
7299def _td ():
73100 d = tempfile .mkdtemp ()
74101 p11_test_files .append (d )
75102 return d
76103
77104
78- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
105+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
79106def setup ():
80107 logging .debug ("Creating test pkcs11 token using softhsm" )
81108 try :
@@ -95,28 +122,28 @@ def setup():
95122 else :
96123 softhsm_db = _tf ()
97124 f .write ("#Generated by pyXMLSecurity test\n 0:%s\n " % softhsm_db )
98-
125+
99126 logging .debug ("Initializing the token" )
100- run_cmd ([SOFTHSM ,
127+ run_cmd ([component_path [ ' SOFTHSM' ] ,
101128 '--slot' , '0' ,
102129 '--label' , 'test' ,
103130 '--init-token' ,
104131 '--pin' , 'secret1' ,
105- '--so-pin' , 'secret2' ],softhsm_conf = softhsm_conf )
132+ '--so-pin' , 'secret2' ], softhsm_conf = softhsm_conf )
106133 logging .debug ("Generating 1024 bit RSA key in token" )
107- run_cmd ([PKCS11_TOOL ,
108- '--module' , P11_MODULE ,
134+ run_cmd ([component_path [ ' PKCS11_TOOL' ] ,
135+ '--module' , component_path [ ' P11_MODULE' ] ,
109136 '-l' ,
110137 '-k' ,
111138 '--key-type' , 'rsa:1024' ,
112139 '--slot-index' , '0' ,
113140 '--id' , 'a1b2' ,
114141 '--label' , 'test' ,
115- '--pin' , 'secret1' ],softhsm_conf = softhsm_conf )
116- run_cmd ([PKCS11_TOOL ,
117- '--module' , P11_MODULE ,
142+ '--pin' , 'secret1' ], softhsm_conf = softhsm_conf )
143+ run_cmd ([component_path [ ' PKCS11_TOOL' ] ,
144+ '--module' , component_path [ ' P11_MODULE' ] ,
118145 '-l' ,
119- '--pin' , 'secret1' , '-O' ],softhsm_conf = softhsm_conf )
146+ '--pin' , 'secret1' , '-O' ], softhsm_conf = softhsm_conf )
120147 global signer_cert_der
121148 global signer_cert_pem
122149 signer_cert_pem = _tf ()
@@ -143,12 +170,12 @@ def setup():
143170distinguished_name = req_distinguished_name
144171
145172[req_distinguished_name]
146- """ % (P11_ENGINE , P11_MODULE ))
173+ """ % (component_path [ ' P11_ENGINE' ], component_path [ ' P11_MODULE' ] ))
147174
148175 signer_cert_der = _tf ()
149176
150177 logging .debug ("Generating self-signed certificate" )
151- run_cmd ([OPENSSL , 'req' ,
178+ run_cmd ([component_path [ ' OPENSSL' ] , 'req' ,
152179 '-new' ,
153180 '-x509' ,
154181 '-subj' , "/CN=Test Signer" ,
@@ -157,27 +184,27 @@ def setup():
157184 '-keyform' , 'engine' ,
158185 '-key' , 'pkcs11:token=test' ,
159186 '-passin' , 'pass:secret1' ,
160- '-out' , signer_cert_pem ],softhsm_conf = softhsm_conf )
187+ '-out' , signer_cert_pem ], softhsm_conf = softhsm_conf )
161188
162- run_cmd ([OPENSSL , 'x509' ,
189+ run_cmd ([component_path [ ' OPENSSL' ] , 'x509' ,
163190 '-inform' , 'PEM' ,
164191 '-outform' , 'DER' ,
165192 '-in' , signer_cert_pem ,
166- '-out' , signer_cert_der ],softhsm_conf = softhsm_conf )
193+ '-out' , signer_cert_der ], softhsm_conf = softhsm_conf )
167194
168195 logging .debug ("Importing certificate into token" )
169196
170- run_cmd ([PKCS11_TOOL ,
171- '--module' , P11_MODULE ,
197+ run_cmd ([component_path [ ' PKCS11_TOOL' ] ,
198+ '--module' , component_path [ ' P11_MODULE' ] ,
172199 '-l' ,
173200 '--slot-index' , '0' ,
174201 '--id' , 'a1b2' ,
175202 '--label' , 'test' ,
176203 '-y' , 'cert' ,
177204 '-w' , signer_cert_der ,
178- '--pin' , 'secret1' ],softhsm_conf = softhsm_conf )
205+ '--pin' , 'secret1' ], softhsm_conf = softhsm_conf )
179206
180- except Exception , ex :
207+ except Exception as ex :
181208 print "-" * 64
182209 traceback .print_exc ()
183210 print "-" * 64
@@ -212,13 +239,13 @@ def setUp(self):
212239
213240 self .cases = load_test_data ('data/signverify' )
214241
215- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
242+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
216243 def test_open_session (self ):
217244 session = None
218245 try :
219246 os .environ ['SOFTHSM_CONF' ] = softhsm_conf
220247 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
221- session = pk11 ._session (P11_MODULE , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
248+ session = pk11 ._session (component_path [ ' P11_MODULE' ] , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
222249 assert session is not None
223250 except Exception , ex :
224251 traceback .print_exc ()
@@ -227,13 +254,13 @@ def test_open_session(self):
227254 if session is not None :
228255 pk11 ._close_session (session )
229256
230- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
257+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
231258 def test_open_session_no_pin (self ):
232259 session = None
233260 try :
234261 os .environ ['SOFTHSM_CONF' ] = softhsm_conf
235262 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
236- session = pk11 ._session (P11_MODULE , pk11_uri = "pkcs11://%s/test" % P11_MODULE )
263+ session = pk11 ._session (component_path [ ' P11_MODULE' ] , pk11_uri = "pkcs11://%s/test" % P11_MODULE )
237264 assert session is not None
238265 except Exception , ex :
239266 traceback .print_exc ()
@@ -242,15 +269,15 @@ def test_open_session_no_pin(self):
242269 if session is not None :
243270 pk11 ._close_session (session )
244271
245- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
272+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
246273 def test_two_sessions (self ):
247274 session1 = None
248275 session2 = None
249276 try :
250277 os .environ ['SOFTHSM_CONF' ] = softhsm_conf
251278 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
252- session1 = pk11 ._session (P11_MODULE , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
253- session2 = pk11 ._session (P11_MODULE , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
279+ session1 = pk11 ._session (component_path [ ' P11_MODULE' ] , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
280+ session2 = pk11 ._session (component_path [ ' P11_MODULE' ] , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
254281 assert session1 != session2
255282 assert session1 is not None
256283 assert session2 is not None
@@ -262,24 +289,24 @@ def test_two_sessions(self):
262289 if session2 is not None :
263290 pk11 ._close_session (session2 )
264291
265- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
292+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
266293 def test_bad_login (self ):
267294 os .environ ['SOFTHSM_CONF' ] = softhsm_conf
268295 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
269296 try :
270- session = pk11 ._session (P11_MODULE , pk11_uri = "pkcs11://%s/test?pin=wrong" % P11_MODULE )
297+ session = pk11 ._session (component_path [ ' P11_MODULE' ] , pk11_uri = "pkcs11://%s/test?pin=wrong" % P11_MODULE )
271298 assert False , "We should have failed the last login"
272299 except PyKCS11Error , ex :
273300 assert ex .value == CKR_PIN_INCORRECT
274301 pass
275302
276- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
303+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
277304 def test_find_key (self ):
278305 session = None
279306 try :
280307 os .environ ['SOFTHSM_CONF' ] = softhsm_conf
281308 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
282- session = pk11 ._session (P11_MODULE , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
309+ session = pk11 ._session (component_path [ ' P11_MODULE' ] , pk11_uri = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
283310 key , cert = pk11 ._find_key (session , "test" )
284311 assert key is not None
285312 assert cert is not None
@@ -290,7 +317,7 @@ def test_find_key(self):
290317 if session is not None :
291318 pk11 ._close_session (session )
292319
293- @unittest .skipIf (P11_MODULE is None , "SoftHSM PKCS11 module not installed" )
320+ @unittest .skipIf (component_path [ ' P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
294321 def test_SAML_sign_with_pkcs11 (self ):
295322 """
296323 Test signing a SAML assertion using PKCS#11 and then verifying it using plain file.
@@ -302,7 +329,7 @@ def test_SAML_sign_with_pkcs11(self):
302329 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
303330
304331 signed = xmlsec .sign (case .as_etree ('in.xml' ),
305- key_spec = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
332+ key_spec = "pkcs11://%s/test?pin=secret1" % component_path [ ' P11_MODULE' ] )
306333
307334 # verify signature using the public key
308335 res = xmlsec .verify (signed , signer_cert_pem )
@@ -319,7 +346,7 @@ def test_SAML_sign_with_pkcs11_cert(self):
319346 os .environ ['SOFTHSM2_CONF' ] = softhsm_conf
320347
321348 signed = xmlsec .sign (case .as_etree ('in2.xml' ),
322- key_spec = "pkcs11://%s/test?pin=secret1" % P11_MODULE )
349+ key_spec = "pkcs11://%s/test?pin=secret1" % component_path [ ' P11_MODULE' ] )
323350
324351 print ("XML output :\n {}\n \n " .format (etree .tostring (signed )))
325352 # verify signature using the public key
0 commit comments