Skip to content

Commit 5b6c68f

Browse files
committed
add system test for the tedge-p11-server
Signed-off-by: reubenmiller <reuben.d.miller@gmail.com>
1 parent ca073b5 commit 5b6c68f

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/sh
2+
set -e
3+
4+
DEVICE_ID="${DEVICE_ID:-}"
5+
IS_SELF_SIGNED=0
6+
export GNUTLS_PIN="${GNUTLS_PIN:-123456}"
7+
export GNUTLS_SO_PIN="${GNUTLS_SO_PIN:-123456}"
8+
export TOKEN_LABEL="${TOKEN_LABEL:-tedge}"
9+
PKCS_URI=
10+
11+
#
12+
# Parse arguments
13+
#
14+
while [ $# -gt 0 ]; do
15+
case "$1" in
16+
--self-signed)
17+
IS_SELF_SIGNED=1
18+
;;
19+
--pin)
20+
GNUTLS_PIN="$2"
21+
shift
22+
;;
23+
--so-pin)
24+
GNUTLS_SO_PIN="$2"
25+
shift
26+
;;
27+
--device-id)
28+
DEVICE_ID="$2"
29+
shift
30+
;;
31+
esac
32+
shift
33+
done
34+
35+
get_token() {
36+
p11tool --list-tokens | grep "token=$TOKEN_LABEL" | awk '{ print $2 }' | head -n1
37+
}
38+
39+
get_key() {
40+
p11tool --login --list-all "$PKCS_URI" | grep type=private | awk '{ print $2 }'
41+
}
42+
43+
#
44+
# Get/Init slot
45+
#
46+
PKCS_URI=$(get_token)
47+
if [ -z "$(get_token)" ]; then
48+
echo "Initializing softhsm2 token" >&2
49+
softhsm2-util --init-token --free --label "$TOKEN_LABEL" --pin "$GNUTLS_PIN" --so-pin "$GNUTLS_SO_PIN"
50+
PKCS_URI=$(get_token)
51+
fi
52+
echo "Using token URI: $PKCS_URI" >&2
53+
54+
55+
#
56+
# Get/Create key
57+
#
58+
KEY=$(get_key)
59+
if [ -z "$KEY" ]; then
60+
mkdir -p /etc/tedge/hsm
61+
p11tool --login --generate-privkey ECDSA --curve=secp256r1 --label "tedge" --outfile /etc/tedge/hsm/tedge.pub "$PKCS_URI"
62+
KEY=$(get_key)
63+
fi
64+
65+
66+
#
67+
# Get/Create CSR template
68+
#
69+
CSR_TEMPLATE=/etc/tedge/hsm/cert.template
70+
if [ ! -f "$CSR_TEMPLATE" ]; then
71+
if [ -z "${DEVICE_ID:-}" ]; then
72+
DEVICE_ID=$(tedge-identity 2>/dev/null)
73+
fi
74+
75+
# If it is self-signed, then Cumulocity requires the ca property
76+
# to be added, otherwise certificate will be rejected by Cumulocity
77+
# when trying to upload it
78+
IS_CA=""
79+
if [ "$IS_SELF_SIGNED" ]; then
80+
IS_CA="ca"
81+
fi
82+
83+
cat <<EOT > "$CSR_TEMPLATE"
84+
organization = "Thin Edge"
85+
unit = "Test Device"
86+
state = "QLD"
87+
country = AU
88+
cn = "$DEVICE_ID"
89+
$IS_CA
90+
EOT
91+
fi
92+
93+
#
94+
# Create CSR (to be signed externally) or create a self-signed certificate
95+
#
96+
if [ "$IS_SELF_SIGNED" = 0 ]; then
97+
#
98+
# Create CSR
99+
#
100+
CSR_PATH=$(tedge config get device.csr_path)
101+
certtool --generate-request --template "$CSR_TEMPLATE" --load-privkey "$KEY" --outfile "$CSR_PATH"
102+
echo "Created csr: $CSR_PATH" >&2
103+
else
104+
# Optional: Self sign the Certificate
105+
echo "Creating self-signed certificate" >&2
106+
CERT_PATH=$(tedge config get device.cert_path)
107+
certtool --generate-self-signed --template "$CSR_TEMPLATE" --load-privkey "$KEY" --outfile "$CERT_PATH"
108+
fi
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
*** Settings ***
2+
Documentation Test thin-edge.io MQTT client authentication using a Hardware Security Module (HSM).
3+
...
4+
... To do this, we install SoftHSM2 which allows us to create software-backed PKCS#11 (cryptoki)
5+
... cryptographic tokens that will be read by thin-edge. In real production environments a dedicated
6+
... hardware device would be used.
7+
8+
Resource ../resources/common.resource
9+
Library ThinEdgeIO
10+
11+
Suite Setup Custom Setup
12+
Suite Teardown Get Suite Logs
13+
14+
Test Tags adapter:docker theme:cryptoki
15+
16+
17+
*** Test Cases ***
18+
Use Private Key in SoftHSM2 using tedge-p11-server
19+
# initialize the soft hsm and create a self-signed certificate
20+
Configure tedge-p11-server module_path=/usr/lib/softhsm/libsofthsm2.so pin=123456
21+
Execute Command sudo -u tedge /usr/bin/init_softhsm.sh --self-signed --device-id "${DEVICE_SN}" --pin 123456
22+
23+
# configure tedge
24+
Execute Command tedge config set c8y.url "$(echo ${C8Y_CONFIG.host} | sed 's|https?://||g')"
25+
Execute Command tedge config set mqtt.bridge.built_in true
26+
Execute Command tedge config set device.cryptoki.mode socket
27+
28+
# Upload the self-signed certificate
29+
Execute Command
30+
... cmd=sudo env C8Y_USER='${C8Y_CONFIG.username}' C8Y_PASSWORD='${C8Y_CONFIG.password}' tedge cert upload c8y
31+
32+
Execute Command tedge reconnect c8y
33+
34+
35+
*** Keywords ***
36+
Custom Setup
37+
${DEVICE_SN}= Setup skip_bootstrap=${True}
38+
Set Suite Variable $DEVICE_SN
39+
Execute Command test -f ./bootstrap.sh && ./bootstrap.sh --no-connect || true
40+
# Allow the tedge user to access softhsm
41+
Execute Command sudo usermod -a -G softhsm tedge
42+
Transfer To Device ${CURDIR}/data/init_softhsm.sh /usr/bin/
43+
Remove Existing Certificates
44+
45+
Remove Existing Certificates
46+
Execute Command cmd=rm -f "$(tedge config get device.key_path)" "$(tedge config get device.cert_path)"
47+
48+
Configure tedge-p11-server
49+
[Arguments] ${module_path} ${pin}
50+
Execute Command
51+
... cmd=printf 'TEDGE_DEVICE_CRYPTOKI_MODULE_PATH=%s\nTEDGE_DEVICE_CRYPTOKI_PIN=%s\n' "${module_path}" "${pin}" | sudo tee /etc/tedge/plugins/tedge-p11-server.conf

tests/images/debian-systemd/debian-systemd.dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ RUN apt-get -y update \
2626
bash-completion \
2727
zsh \
2828
fish \
29+
# PKCS11 / cryptoki support
30+
gnutls-bin \
31+
softhsm2 \
2932
# mosquitto (default version used by Debian, see below for more details)
3033
mosquitto \
3134
mosquitto-clients

0 commit comments

Comments
 (0)