Skip to content

Commit 4fb7bed

Browse files
committed
Set p11_test component paths from the environment
Signed-off-by: Ivan Kanakarakis <ivan.kanak@gmail.com>
1 parent 9f144c9 commit 4fb7bed

2 files changed

Lines changed: 75 additions & 47 deletions

File tree

src/xmlsec/test/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
__author__ = 'leifj'
66

77

8+
def paths_for_component(component, default_paths):
9+
env_path = os.environ.get(component)
10+
return [env_path] if env_path else default_paths
11+
12+
813
def find_alts(alts):
914
for a in alts:
1015
if os.path.exists(a):

src/xmlsec/test/p11_test.py

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from lxml import etree
1818

1919
import xmlsec
20+
from xmlsec.test import paths_for_component
2021
from xmlsec.test import find_alts
2122
from xmlsec.test import run_cmd
2223

@@ -28,36 +29,58 @@
2829
except ImportError:
2930
raise unittest.SkipTest("PyKCS11 not installed")
3031

31-
P11_MODULE = find_alts(['/usr/lib/libsofthsm.so', '/usr/lib/softhsm/libsofthsm.so', '/usr/lib/softhsm/libsofthsm2.so'])
32-
P11_ENGINE = find_alts(['/usr/lib/ssl/engines/libpkcs11.so','/usr/lib/engines/engine_pkcs11.so'])
33-
P11_SPY = find_alts(['/usr/lib/pkcs11/pkcs11-spy.so'])
34-
PKCS11_TOOL = find_alts(['/usr/bin/pkcs11-tool'])
35-
OPENSC_TOOL = find_alts(['/usr/bin/opensc-tool'])
36-
SOFTHSM = find_alts(['/usr/bin/softhsm','/usr/bin/softhsm2-util'])
37-
OPENSSL = find_alts(['/usr/bin/openssl'])
38-
3932
try:
4033
import xmlsec.pk11 as pk11
4134
except Exception:
4235
raise unittest.SkipTest("PyKCS11 not installed")
4336

44-
if OPENSSL is None:
45-
raise unittest.SkipTest("OpenSSL not installed")
46-
47-
if SOFTHSM is None:
48-
raise unittest.SkipTest("SoftHSM2 not installed")
49-
50-
if OPENSC_TOOL is None:
51-
raise unittest.SkipTest("OpenSC not installed")
52-
53-
if PKCS11_TOOL is None:
54-
raise unittest.SkipTest("pkcs11-tool not installed")
5537

56-
if P11_ENGINE is None:
57-
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))
5881

5982
softhsm_version = 1
60-
if SOFTHSM == '/usr/bin/softhsm2-util':
83+
if component_path['SOFTHSM'] == '/usr/bin/softhsm2-util':
6184
softhsm_version = 2
6285

6386
p11_test_files = []
@@ -79,7 +102,7 @@ def _td():
79102
return d
80103

81104

82-
@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")
83106
def setup():
84107
logging.debug("Creating test pkcs11 token using softhsm")
85108
try:
@@ -101,24 +124,24 @@ def setup():
101124
f.write("#Generated by pyXMLSecurity test\n0:%s\n" % softhsm_db)
102125

103126
logging.debug("Initializing the token")
104-
run_cmd([SOFTHSM,
127+
run_cmd([component_path['SOFTHSM'],
105128
'--slot', '0',
106129
'--label', 'test',
107130
'--init-token',
108131
'--pin', 'secret1',
109132
'--so-pin', 'secret2'], softhsm_conf=softhsm_conf)
110133
logging.debug("Generating 1024 bit RSA key in token")
111-
run_cmd([PKCS11_TOOL,
112-
'--module', P11_MODULE,
134+
run_cmd([component_path['PKCS11_TOOL'],
135+
'--module', component_path['P11_MODULE'],
113136
'-l',
114137
'-k',
115138
'--key-type', 'rsa:1024',
116139
'--slot-index', '0',
117140
'--id', 'a1b2',
118141
'--label', 'test',
119142
'--pin', 'secret1'], softhsm_conf=softhsm_conf)
120-
run_cmd([PKCS11_TOOL,
121-
'--module', P11_MODULE,
143+
run_cmd([component_path['PKCS11_TOOL'],
144+
'--module', component_path['P11_MODULE'],
122145
'-l',
123146
'--pin', 'secret1', '-O'], softhsm_conf=softhsm_conf)
124147
global signer_cert_der
@@ -147,12 +170,12 @@ def setup():
147170
distinguished_name = req_distinguished_name
148171
149172
[req_distinguished_name]
150-
""" % (P11_ENGINE, P11_MODULE))
173+
""" % (component_path['P11_ENGINE'], component_path['P11_MODULE']))
151174

152175
signer_cert_der = _tf()
153176

154177
logging.debug("Generating self-signed certificate")
155-
run_cmd([OPENSSL, 'req',
178+
run_cmd([component_path['OPENSSL'], 'req',
156179
'-new',
157180
'-x509',
158181
'-subj', "/CN=Test Signer",
@@ -163,16 +186,16 @@ def setup():
163186
'-passin', 'pass:secret1',
164187
'-out', signer_cert_pem], softhsm_conf=softhsm_conf)
165188

166-
run_cmd([OPENSSL, 'x509',
189+
run_cmd([component_path['OPENSSL'], 'x509',
167190
'-inform', 'PEM',
168191
'-outform', 'DER',
169192
'-in', signer_cert_pem,
170193
'-out', signer_cert_der], softhsm_conf=softhsm_conf)
171194

172195
logging.debug("Importing certificate into token")
173196

174-
run_cmd([PKCS11_TOOL,
175-
'--module', P11_MODULE,
197+
run_cmd([component_path['PKCS11_TOOL'],
198+
'--module', component_path['P11_MODULE'],
176199
'-l',
177200
'--slot-index', '0',
178201
'--id', 'a1b2',
@@ -216,13 +239,13 @@ def setUp(self):
216239

217240
self.cases = load_test_data('data/signverify')
218241

219-
@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")
220243
def test_open_session(self):
221244
session = None
222245
try:
223246
os.environ['SOFTHSM_CONF'] = softhsm_conf
224247
os.environ['SOFTHSM2_CONF'] = softhsm_conf
225-
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)
226249
assert session is not None
227250
except Exception, ex:
228251
traceback.print_exc()
@@ -231,13 +254,13 @@ def test_open_session(self):
231254
if session is not None:
232255
pk11._close_session(session)
233256

234-
@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")
235258
def test_open_session_no_pin(self):
236259
session = None
237260
try:
238261
os.environ['SOFTHSM_CONF'] = softhsm_conf
239262
os.environ['SOFTHSM2_CONF'] = softhsm_conf
240-
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)
241264
assert session is not None
242265
except Exception, ex:
243266
traceback.print_exc()
@@ -246,15 +269,15 @@ def test_open_session_no_pin(self):
246269
if session is not None:
247270
pk11._close_session(session)
248271

249-
@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")
250273
def test_two_sessions(self):
251274
session1 = None
252275
session2 = None
253276
try:
254277
os.environ['SOFTHSM_CONF'] = softhsm_conf
255278
os.environ['SOFTHSM2_CONF'] = softhsm_conf
256-
session1 = pk11._session(P11_MODULE, pk11_uri="pkcs11://%s/test?pin=secret1" % P11_MODULE)
257-
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)
258281
assert session1 != session2
259282
assert session1 is not None
260283
assert session2 is not None
@@ -266,24 +289,24 @@ def test_two_sessions(self):
266289
if session2 is not None:
267290
pk11._close_session(session2)
268291

269-
@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")
270293
def test_bad_login(self):
271294
os.environ['SOFTHSM_CONF'] = softhsm_conf
272295
os.environ['SOFTHSM2_CONF'] = softhsm_conf
273296
try:
274-
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)
275298
assert False, "We should have failed the last login"
276299
except PyKCS11Error, ex:
277300
assert ex.value == CKR_PIN_INCORRECT
278301
pass
279302

280-
@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")
281304
def test_find_key(self):
282305
session = None
283306
try:
284307
os.environ['SOFTHSM_CONF'] = softhsm_conf
285308
os.environ['SOFTHSM2_CONF'] = softhsm_conf
286-
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)
287310
key, cert = pk11._find_key(session, "test")
288311
assert key is not None
289312
assert cert is not None
@@ -294,7 +317,7 @@ def test_find_key(self):
294317
if session is not None:
295318
pk11._close_session(session)
296319

297-
@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")
298321
def test_SAML_sign_with_pkcs11(self):
299322
"""
300323
Test signing a SAML assertion using PKCS#11 and then verifying it using plain file.
@@ -306,7 +329,7 @@ def test_SAML_sign_with_pkcs11(self):
306329
os.environ['SOFTHSM2_CONF'] = softhsm_conf
307330

308331
signed = xmlsec.sign(case.as_etree('in.xml'),
309-
key_spec="pkcs11://%s/test?pin=secret1" % P11_MODULE)
332+
key_spec="pkcs11://%s/test?pin=secret1" % component_path['P11_MODULE'])
310333

311334
# verify signature using the public key
312335
res = xmlsec.verify(signed, signer_cert_pem)
@@ -323,7 +346,7 @@ def test_SAML_sign_with_pkcs11_cert(self):
323346
os.environ['SOFTHSM2_CONF'] = softhsm_conf
324347

325348
signed = xmlsec.sign(case.as_etree('in2.xml'),
326-
key_spec="pkcs11://%s/test?pin=secret1" % P11_MODULE)
349+
key_spec="pkcs11://%s/test?pin=secret1" % component_path['P11_MODULE'])
327350

328351
print("XML output :\n{}\n\n".format(etree.tostring(signed)))
329352
# verify signature using the public key

0 commit comments

Comments
 (0)