Skip to content

Commit d5c21f5

Browse files
moidxtimothytrippel
authored andcommitted
[hsm] Add support for Gem engine.
This commits enables the use of the gem engine with Thales HSMs. The test infrastructure uses the `OTPROV_USE_GEM_ENGINE=true` enable it. The token_init.sh script was moved under //config:token_init.sh. The script is now called in prod mode, but restristed to only run certgen. Signed-off-by: Miguel Osorio <miguelosorio@google.com>
1 parent e2d5534 commit d5c21f5

File tree

8 files changed

+235
-149
lines changed

8 files changed

+235
-149
lines changed

config/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ filegroup(
88
name = "deploy_script",
99
srcs = [
1010
":deploy.sh",
11+
":token_init.sh",
1112
],
1213
)

config/dev/BUILD.bazel

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ package(default_visibility = ["//visibility:public"])
66

77
filegroup(
88
name = "deploy_config",
9-
srcs = [
10-
":token_init.sh",
11-
] + glob(
9+
srcs = glob(
1210
[
1311
"env/**",
1412
"certs/**",

config/dev/token_init.sh

Lines changed: 0 additions & 98 deletions
This file was deleted.

config/prod/env/spm.env

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ export HSMTOOL_MODULE=/usr/safenet/lunaclient/lib/libCryptoki2_64.so
3434
export HSMTOOL_USER="user"
3535
export HSMTOOL_TOKEN="${SPM_HSM_TOKEN_SPM}"
3636
export HSMTOOL_PIN="${SPM_HSM_PIN_USER}"
37+
38+
# `openssl` PKCS11 engine support.
39+
# In production environments, this flag needs to be set to "true" to support
40+
# generation of certificates with openssl and the target HSM.
41+
# The `SLOT` environment variables need to be set to the target HSM slot
42+
# numbers for offline and SPM instances.
43+
export OTPROV_USE_GEM_ENGINE=true
44+
export OTPROV_GEM_SLOT_OFFLINE=1
45+
export OTPROV_GEM_SLOT_SPM=0
46+
export OTPROV_GEM_SLOT_CERT_OPS="${OTPROV_GEM_SLOT_OFFLINE}"

config/token_init.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
# Copyright lowRISC contributors (OpenTitan project).
3+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
set -e
7+
8+
if [[ -z "${CONFIG_SUBDIR}" ]]; then
9+
echo "Error: CONFIG_SUBDIR environment variable is not set."
10+
exit 1
11+
fi
12+
13+
CONFIG_DIR="${OPENTITAN_VAR_DIR}/config/${CONFIG_SUBDIR}"
14+
15+
source "${CONFIG_DIR}/env/spm.env"
16+
17+
export HSMTOOL_BIN="${OPENTITAN_VAR_DIR}/bin/hsmtool"
18+
19+
# Check token initialization dependencies.
20+
if [ -z "${OPENTITAN_VAR_DIR}" ]; then
21+
echo "Error: OPENTITAN_VAR_DIR environment variable is not set."
22+
return 1
23+
fi
24+
25+
if [ ! -d "${OPENTITAN_VAR_DIR}" ]; then
26+
echo "Error: OPENTITAN_VAR_DIR directory '${OPENTITAN_VAR_DIR}' does not exist."
27+
return 1
28+
fi
29+
30+
if [ ! -x "${HSMTOOL_BIN}" ]; then
31+
echo "Error: '${HSMTOOL_BIN}' is not executable or does not exist."
32+
return 1
33+
fi
34+
35+
function run_hsm_init() {
36+
local init_script="$1"
37+
local original_dir="$(pwd)"
38+
39+
trap 'cd "${original_dir}" || { echo "Error: Could not change back to original directory '${original_dir}'."; return 1; }' EXIT
40+
41+
if [ ! -f "${init_script}" ]; then
42+
echo "Error: File '${init_script}' does not exist."
43+
return 1
44+
fi
45+
46+
local file_dir="$(dirname "${init_script}")"
47+
48+
cd "${file_dir}" || {
49+
echo "Error: Could not change directory to '${init_script}'."
50+
return 1
51+
}
52+
53+
shift
54+
55+
echo "Running HSM initialization script: ${init_script}"
56+
"${init_script}" "$@"
57+
58+
cd "${original_dir}" || {
59+
echo "Error: Could not change back to original directory '${original_dir}'."
60+
return 1
61+
}
62+
}
63+
64+
if [[ "dev" == "${CONFIG_SUBDIR}" ]]; then
65+
# Run the HSM initialization script for SPM.
66+
run_hsm_init "${CONFIG_DIR}/spm/sku/spm_init.bash" \
67+
-m "${HSMTOOL_MODULE}" \
68+
-t "${SPM_HSM_TOKEN_SPM}" \
69+
-s "${SOFTHSM2_CONF_SPM}" \
70+
-p "${HSMTOOL_PIN}"
71+
72+
run_hsm_init "${CONFIG_DIR}/spm/sku/spm_export.bash" \
73+
-m "${HSMTOOL_MODULE}" \
74+
-t "${SPM_HSM_TOKEN_SPM}" \
75+
-s "${SOFTHSM2_CONF_SPM}" \
76+
-p "${HSMTOOL_PIN}" \
77+
-o "${CONFIG_DIR}/spm/sku/spm_hsm_init.tar.gz"
78+
79+
# Run the SKU initilization script in the offline HSM partition.
80+
run_hsm_init "${CONFIG_DIR}/spm/sku/sival/offline_init.bash" \
81+
-m "${HSMTOOL_MODULE}" \
82+
-t "${SPM_HSM_TOKEN_OFFLINE}" \
83+
-s "${SOFTHSM2_CONF_OFFLINE}" \
84+
-p "${HSMTOOL_PIN}"
85+
86+
run_hsm_init "${CONFIG_DIR}/spm/sku/sival/offline_export.bash" \
87+
-m "${HSMTOOL_MODULE}" \
88+
-t "${SPM_HSM_TOKEN_OFFLINE}" \
89+
-s "${SOFTHSM2_CONF_OFFLINE}" \
90+
-p "${HSMTOOL_PIN}" \
91+
-i "${CONFIG_DIR}/spm/sku/spm_hsm_init.tar.gz" \
92+
-o "${CONFIG_DIR}/spm/sku/sival/hsm_offline_init.tar.gz"
93+
94+
# Run the SKU initialization script in the SPM partition.
95+
run_hsm_init "${CONFIG_DIR}/spm/sku/sival/spm_sku_init.bash" \
96+
-m "${HSMTOOL_MODULE}" \
97+
-t "${SPM_HSM_TOKEN_SPM}" \
98+
-s "${SOFTHSM2_CONF_SPM}" \
99+
-p "${HSMTOOL_PIN}" \
100+
-i "${CONFIG_DIR}/spm/sku/sival/hsm_offline_init.tar.gz" \
101+
-o "${CONFIG_DIR}/spm/sku/sival/hsm_sival_sku.tar.gz"
102+
else
103+
# In production, we only run the offline export script with the -c flag
104+
# to generate the CA certificates. The -s flag is not used here.
105+
run_hsm_init "${CONFIG_DIR}/spm/sku/sival/offline_export.bash" \
106+
-m "${HSMTOOL_MODULE}" \
107+
-t "${SPM_HSM_TOKEN_OFFLINE}" \
108+
-s "${SOFTHSM2_CONF_OFFLINE}" \
109+
-p "${HSMTOOL_PIN}" \
110+
-o "${CONFIG_DIR}/spm/sku/sival/hsm_offline_init.tar.gz" \
111+
-c
112+
fi
113+
114+
echo "HSM initialization complete."

docs/spm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ The following command initializes the keys in the HSM for test purposes.
103103
> step.
104104
105105
```console
106-
$ config/dev/token_init.sh
106+
$ config/token_init.sh
107107
```
108108

109109
### Start SPM Server

0 commit comments

Comments
 (0)