| 1 |
""" |
| 2 |
Prepare for PKCS#11 tests by configuring SoftHSM2, if it is installed. |
| 3 |
""" |
| 4 |
|
| 5 |
import Builder |
| 6 |
|
| 7 |
import os |
| 8 |
import re |
| 9 |
|
| 10 |
|
| 11 |
class Pkcs11TestSetup(Builder.Action): |
| 12 |
""" |
| 13 |
Set up this machine for running the PKCS#11 tests. |
| 14 |
If SoftHSM2 cannot be installed, the tests are skipped. |
| 15 |
|
| 16 |
This action should be run in the 'pre_build_steps' or 'build_steps' stage. |
| 17 |
""" |
| 18 |
|
| 19 |
def run(self, env): |
| 20 |
if not env.project.needs_tests(env): |
| 21 |
print("Skipping PKCS#11 setup because tests disabled for project") |
| 22 |
return |
| 23 |
|
| 24 |
self.env = env |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
if hasattr(env.args, "cmake_extra"): |
| 31 |
if any('BUILD_SHARED_LIBS=ON' in arg for arg in env.args.cmake_extra): |
| 32 |
print( |
| 33 |
"WARNING: PKCS#11 tests disabled when BUILD_SHARED_LIBS=ON due to weird libcrypto.so behavior") |
| 34 |
return |
| 35 |
|
| 36 |
|
| 37 |
try: |
| 38 |
softhsm_install_acion = Builder.InstallPackages(['softhsm']) |
| 39 |
softhsm_install_acion.run(env) |
| 40 |
except: |
| 41 |
print("WARNING: softhsm could not be installed. PKCS#11 tests are disabled") |
| 42 |
return |
| 43 |
|
| 44 |
softhsm_lib = self._find_softhsm_lib() |
| 45 |
if softhsm_lib is None: |
| 46 |
print("WARNING: libsofthsm2.so not found. PKCS#11 tests are disabled") |
| 47 |
return |
| 48 |
|
| 49 |
|
| 50 |
self._exec_softhsm2_util('--version') |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if self._get_softhsm2_version() < (2, 2, 0): |
| 55 |
print("WARNING: SoftHSM2 installation is too old. PKCS#11 tests are disabled") |
| 56 |
return |
| 57 |
|
| 58 |
|
| 59 |
env.project.config['cmake_args'].append('-DENABLE_PKCS11_TESTS=ON') |
| 60 |
|
| 61 |
|
| 62 |
softhsm2_dir = os.path.join(env.build_dir, 'softhsm2') |
| 63 |
conf_path = os.path.join(softhsm2_dir, 'softhsm2.conf') |
| 64 |
token_dir = os.path.join(softhsm2_dir, 'tokens') |
| 65 |
env.shell.mkdir(token_dir) |
| 66 |
self._setenv('SOFTHSM2_CONF', conf_path) |
| 67 |
with open(conf_path, 'w') as conf_file: |
| 68 |
conf_file.write(f"directories.tokendir = {token_dir}\n") |
| 69 |
|
| 70 |
|
| 71 |
self._exec_softhsm2_util('--show-slots') |
| 72 |
|
| 73 |
|
| 74 |
self._setenv('TEST_PKCS11_LIB', softhsm_lib) |
| 75 |
self._setenv('TEST_PKCS11_TOKEN_DIR', token_dir) |
| 76 |
|
| 77 |
def _find_softhsm_lib(self): |
| 78 |
"""Return path to SoftHSM2 shared lib, or None if not found""" |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
for lib_dir in ['lib64', 'lib']: |
| 85 |
for base_dir in ['/usr/local', '/usr', '/', ]: |
| 86 |
search_dir = os.path.join(base_dir, lib_dir) |
| 87 |
for root, dirs, files in os.walk(search_dir): |
| 88 |
for file_name in files: |
| 89 |
if 'libsofthsm2.so' in file_name: |
| 90 |
return os.path.join(root, file_name) |
| 91 |
return None |
| 92 |
|
| 93 |
def _exec_softhsm2_util(self, *args, **kwargs): |
| 94 |
if not 'check' in kwargs: |
| 95 |
kwargs['check'] = True |
| 96 |
|
| 97 |
result = self.env.shell.exec('softhsm2-util', *args, **kwargs) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if 'Usage: softhsm2-util' in result.output: |
| 107 |
raise Exception('softhsm2-util failed') |
| 108 |
|
| 109 |
return result |
| 110 |
|
| 111 |
def _setenv(self, var, value): |
| 112 |
""" |
| 113 |
Set environment variable now, |
| 114 |
and ensure the environment variable is set again when tests run |
| 115 |
""" |
| 116 |
self.env.shell.setenv(var, value) |
| 117 |
self.env.project.config['test_env'][var] = value |
| 118 |
|
| 119 |
def _get_softhsm2_version(self): |
| 120 |
output = self._exec_softhsm2_util('--version').output |
| 121 |
match = re.match(r'([0-9]+)\.([0-9]+)\.([0-9]+)', output) |
| 122 |
return (int(match.group(1)), int(match.group(2)), int(match.group(3))) |