1313import shutil
1414import tempfile
1515
16- from defusedxml import lxml
1716from lxml import etree
1817
1918import xmlsec
3736
3837component_default_paths = {
3938 'P11_MODULE' : [
40- '/usr/lib/libsofthsm.so' ,
41- '/usr/lib/softhsm/libsofthsm.so' ,
4239 '/usr/lib/softhsm/libsofthsm2.so' ,
40+ '/usr/lib/softhsm/libsofthsm.so' ,
4341 ],
4442 'P11_ENGINE' : [
4543 '/usr/lib/ssl/engines/libpkcs11.so' ,
4644 '/usr/lib/engines/engine_pkcs11.so' ,
4745 '/usr/lib/x86_64-linux-gnu/engines-1.1/pkcs11.so' ,
4846 ],
49- 'P11_SPY' : [
50- '/usr/lib/pkcs11/pkcs11-spy.so' ,
51- ],
5247 'PKCS11_TOOL' : [
5348 '/usr/bin/pkcs11-tool' ,
5449 ],
5550 'OPENSC_TOOL' : [
5651 '/usr/bin/opensc-tool' ,
5752 ],
5853 'SOFTHSM' : [
59- '/usr/bin/softhsm' ,
6054 '/usr/bin/softhsm2-util' ,
55+ '/usr/bin/softhsm' ,
6156 ],
6257 'OPENSSL' : [
6358 '/usr/bin/openssl' ,
8075 raise unittest .SkipTest ("Required components missing: {}" .format (missing ))
8176
8277softhsm_version = 1
83- if component_path ['SOFTHSM' ] == '/usr/bin/ softhsm2-util' :
78+ if component_path ['SOFTHSM' ]. endswith ( ' softhsm2-util') :
8479 softhsm_version = 2
8580
81+ openssl_version = subprocess .check_output ([component_path ['OPENSSL' ],
82+ 'version' ]
83+ )[8 :11 ]
84+
8685p11_test_files = []
8786softhsm_conf = None
8887server_cert_pem = None
@@ -117,11 +116,18 @@ def setup():
117116 with open (softhsm_conf , "w" ) as f :
118117 if softhsm_version == 2 :
119118 softhsm_db = _td ()
120- f .write ("#Generated by pyXMLSecurity test\n directories.tokendir = %s\n objectstore.backend = file\n log.level = DEBUG\n " % softhsm_db )
121-
119+ f .write ("""
120+ # Generated by pyXMLSecurity test
121+ directories.tokendir = %s
122+ objectstore.backend = file
123+ log.level = DEBUG
124+ """ % softhsm_db )
122125 else :
123126 softhsm_db = _tf ()
124- f .write ("#Generated by pyXMLSecurity test\n 0:%s\n " % softhsm_db )
127+ f .write ("""
128+ # Generated by pyXMLSecurity test
129+ 0:%s
130+ """ % softhsm_db )
125131
126132 logging .debug ("Initializing the token" )
127133 run_cmd ([component_path ['SOFTHSM' ],
@@ -150,27 +156,27 @@ def setup():
150156 openssl_conf = _tf ()
151157 logging .debug ("Generating OpenSSL config" )
152158 with open (openssl_conf , "w" ) as f :
153- f . write ( """
154- openssl_conf = openssl_def
155-
156- [openssl_def]
157- engines = engine_section
158-
159- [engine_section]
160- pkcs11 = pkcs11_section
161-
162- [pkcs11_section]
163- engine_id = pkcs11
164- dynamic_path = %s
165- MODULE_PATH = %s
166- PIN = secret1
167- init = 0
168-
169- [req]
170- distinguished_name = req_distinguished_name
171-
172- [req_distinguished_name]
173- """ % ( component_path [ 'P11_ENGINE' ], component_path [ 'P11_MODULE' ]))
159+ dynamic_path = (
160+ "dynamic_path = %s" % component_path [ 'P11_ENGINE' ]
161+ if openssl_version == "1.0"
162+ else ""
163+ )
164+ f . write ( " \n " . join ([
165+ "openssl_conf = openssl_def" ,
166+ "[openssl_def]" ,
167+ "engines = engine_section" ,
168+ "[engine_section]" ,
169+ " pkcs11 = pkcs11_section" ,
170+ "[req]" ,
171+ "distinguished_name = req_distinguished_name" ,
172+ "[req_distinguished_name]" ,
173+ "[pkcs11_section]" ,
174+ "engine_id = pkcs11" ,
175+ dynamic_path ,
176+ "MODULE_PATH = %s" % component_path [ 'P11_MODULE' ],
177+ "PIN = secret1" ,
178+ "init = 0" ,
179+ ]))
174180
175181 signer_cert_der = _tf ()
176182
@@ -182,7 +188,7 @@ def setup():
182188 '-engine' , 'pkcs11' ,
183189 '-config' , openssl_conf ,
184190 '-keyform' , 'engine' ,
185- '-key' , 'pkcs11:token=test ' ,
191+ '-key' , 'label_test ' ,
186192 '-passin' , 'pass:secret1' ,
187193 '-out' , signer_cert_pem ], softhsm_conf = softhsm_conf )
188194
@@ -269,7 +275,8 @@ def test_open_session_no_pin(self):
269275 if session is not None :
270276 pk11 ._close_session (session )
271277
272- @unittest .skipIf (component_path ['P11_MODULE' ] is None , "SoftHSM PKCS11 module not installed" )
278+ # @unittest.skipIf(component_path['P11_MODULE'] is None, "SoftHSM PKCS11 module not installed")
279+ @unittest .skip ("SoftHSM PKCS11 module does not support 2 sessions" )
273280 def test_two_sessions (self ):
274281 session1 = None
275282 session2 = None
0 commit comments