File: softhsm_setup.py

package info (click to toggle)
python-xmlsec 1.3.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: ansic: 4,118; python: 2,045; xml: 461; makefile: 4
file content (332 lines) | stat: -rw-r--r-- 9,598 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""Testing the PKCS#11 shim layer.

Heavily inspired by from https://github.com/IdentityPython/pyXMLSecurity by leifj
under license "As is", see https://github.com/IdentityPython/pyXMLSecurity/blob/master/LICENSE.txt
"""

import logging
import os
import shutil
import subprocess
import tempfile
import traceback
import unittest

DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')


def paths_for_component(component: str, default_paths):
    env_path = os.environ.get(component)
    return [env_path] if env_path else default_paths


def find_alts(component_name, alts) -> str:
    for a in alts:
        if os.path.exists(a):
            return a
    raise unittest.SkipTest('Required component is missing: {}'.format(component_name))


def run_cmd(args, softhsm_conf=None):
    env = {}
    if softhsm_conf is not None:
        env['SOFTHSM_CONF'] = softhsm_conf
        env['SOFTHSM2_CONF'] = softhsm_conf
    proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
    out, err = proc.communicate()
    if err is not None and len(err) > 0:
        logging.error(err)
    if out is not None and len(out) > 0:
        logging.debug(out)
    rv = proc.wait()
    if rv:
        with open(softhsm_conf) as f:
            conf = f.read()
        msg = '[cmd: {cmd}] [code: {code}] [stdout: {out}] [stderr: {err}] [config: {conf}]'
        msg = msg.format(
            cmd=' '.join(args),
            code=rv,
            out=out.strip(),
            err=err.strip(),
            conf=conf,
        )
        raise RuntimeError(msg)
    return out, err


component_default_paths = {
    'P11_MODULE': [
        '/usr/lib/softhsm/libsofthsm2.so',
        '/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so',
        '/usr/lib/softhsm/libsofthsm.so',
        '/usr/lib64/softhsm/libsofthsm2.so',
    ],
    'P11_ENGINE': [
        '/usr/lib/ssl/engines/libpkcs11.so',
        '/usr/lib/engines/engine_pkcs11.so',
        '/usr/lib/x86_64-linux-gnu/engines-1.1/pkcs11.so',
        '/usr/lib64/engines-1.1/pkcs11.so',
        '/usr/lib64/engines-1.1/libpkcs11.so',
        '/usr/lib64/engines-3/pkcs11.so',
        '/usr/lib64/engines-3/libpkcs11.so',
        '/usr/lib/x86_64-linux-gnu/engines-3/pkcs11.so',
        '/usr/lib/x86_64-linux-gnu/engines-3/libpkcs11.so',
    ],
    'PKCS11_TOOL': [
        '/usr/bin/pkcs11-tool',
    ],
    'SOFTHSM': [
        '/usr/bin/softhsm2-util',
        '/usr/bin/softhsm',
    ],
    'OPENSSL': [
        '/usr/bin/openssl',
    ],
}

component_path = {
    component_name: find_alts(component_name, paths_for_component(component_name, default_paths))
    for component_name, default_paths in component_default_paths.items()
}

softhsm_version = 1
if component_path['SOFTHSM'].endswith('softhsm2-util'):
    softhsm_version = 2

openssl_version = subprocess.check_output([component_path['OPENSSL'], 'version'])[8:11].decode()

p11_test_files = []
softhsm_conf = None
softhsm_db = None


def _temp_file() -> str:
    f = tempfile.NamedTemporaryFile(delete=False)
    p11_test_files.append(f.name)
    return f.name


def _temp_dir() -> str:
    d = tempfile.mkdtemp()
    p11_test_files.append(d)
    return d


@unittest.skipIf(component_path['P11_MODULE'] is None, 'SoftHSM PKCS11 module not installed')
def setup() -> None:
    logging.debug('Creating test pkcs11 token using softhsm')
    try:
        global softhsm_conf
        softhsm_conf = _temp_file()
        logging.debug('Generating softhsm.conf')
        with open(softhsm_conf, 'w') as f:
            if softhsm_version == 2:
                softhsm_db = _temp_dir()
                f.write(
                    """
# Generated by test
directories.tokendir = {}
objectstore.backend = file
log.level = DEBUG
""".format(
                        softhsm_db
                    )
                )
            else:
                softhsm_db = _temp_file()
                f.write(
                    """
# Generated by test
0:{}
""".format(
                        softhsm_db
                    )
                )

        logging.debug('Initializing the token')
        out, err = run_cmd(
            [
                component_path['SOFTHSM'],
                '--slot',
                '0',
                '--label',
                'test',
                '--init-token',
                '--pin',
                'secret1',
                '--so-pin',
                'secret2',
            ],
            softhsm_conf=softhsm_conf,
        )

        hash_priv_key = _temp_file()
        logging.debug('Converting test private key to format for softhsm')
        run_cmd(
            [
                component_path['OPENSSL'],
                'pkcs8',
                '-topk8',
                '-inform',
                'PEM',
                '-outform',
                'PEM',
                '-nocrypt',
                '-in',
                os.path.join(DATA_DIR, 'rsakey.pem'),
                '-out',
                hash_priv_key,
            ],
            softhsm_conf=softhsm_conf,
        )

        logging.debug('Importing the test key to softhsm')
        run_cmd(
            [
                component_path['SOFTHSM'],
                '--import',
                hash_priv_key,
                '--token',
                'test',
                '--id',
                'a1b2',
                '--label',
                'test',
                '--pin',
                'secret1',
            ],
            softhsm_conf=softhsm_conf,
        )
        run_cmd(
            [
                component_path['PKCS11_TOOL'],
                '--module',
                component_path['P11_MODULE'],
                '-l',
                '--pin',
                'secret1',
                '-O',
            ],
            softhsm_conf=softhsm_conf,
        )
        signer_cert_pem = _temp_file()
        openssl_conf = _temp_file()
        logging.debug('Generating OpenSSL config for version %s', openssl_version)
        with open(openssl_conf, 'w') as f:
            f.write(
                '\n'.join(
                    [
                        'openssl_conf = openssl_def',
                        '[openssl_def]',
                        'engines = engine_section',
                        '[engine_section]',
                        'pkcs11 = pkcs11_section',
                        '[req]',
                        'distinguished_name = req_distinguished_name',
                        '[req_distinguished_name]',
                        '[pkcs11_section]',
                        'engine_id = pkcs11',
                        # dynamic_path,
                        "MODULE_PATH = {}".format(component_path['P11_MODULE']),
                        'init = 0',
                    ]
                )
            )

        with open(openssl_conf, 'r') as f:
            logging.debug('-------- START DEBUG openssl_conf --------')
            logging.debug(f.readlines())
            logging.debug('-------- END DEBUG openssl_conf --------')
        logging.debug('-------- START DEBUG paths --------')
        logging.debug(run_cmd(['ls', '-ld', component_path['P11_ENGINE']]))
        logging.debug(run_cmd(['ls', '-ld', component_path['P11_MODULE']]))
        logging.debug('-------- END DEBUG paths --------')

        signer_cert_der = _temp_file()

        logging.debug('Generating self-signed certificate')
        run_cmd(
            [
                component_path['OPENSSL'],
                'req',
                '-new',
                '-x509',
                '-subj',
                '/CN=Test Signer',
                '-engine',
                'pkcs11',
                '-config',
                openssl_conf,
                '-keyform',
                'engine',
                '-key',
                'label_test',
                '-passin',
                'pass:secret1',
                '-out',
                signer_cert_pem,
            ],
            softhsm_conf=softhsm_conf,
        )

        run_cmd(
            [
                component_path['OPENSSL'],
                'x509',
                '-inform',
                'PEM',
                '-outform',
                'DER',
                '-in',
                signer_cert_pem,
                '-out',
                signer_cert_der,
            ],
            softhsm_conf=softhsm_conf,
        )

        logging.debug('Importing certificate into token')

        run_cmd(
            [
                component_path['PKCS11_TOOL'],
                '--module',
                component_path['P11_MODULE'],
                '-l',
                '--slot-index',
                '0',
                '--id',
                'a1b2',
                '--label',
                'test',
                '-y',
                'cert',
                '-w',
                signer_cert_der,
                '--pin',
                'secret1',
            ],
            softhsm_conf=softhsm_conf,
        )

        # TODO: Should be teardowned in teardown  # noqa: T101
        os.environ['SOFTHSM_CONF'] = softhsm_conf
        os.environ['SOFTHSM2_CONF'] = softhsm_conf

    except Exception as ex:
        print('-' * 64)
        traceback.print_exc()
        print('-' * 64)
        logging.exception('PKCS11 tests disabled: unable to initialize test token')
        raise ex


def teardown() -> None:
    global p11_test_files
    for o in p11_test_files:
        if os.path.exists(o):
            if os.path.isdir(o):
                shutil.rmtree(o)
            else:
                os.unlink(o)
    p11_test_files = []