Skip to content

Commit f7e43a2

Browse files
committed
tests: Add unit test for device ID builder
This makes the tests/run.sh script to generate the IAK and IDevID certificates if the tpm2-openssl provider is available. The added test is executed only if both the IAK and IDevID certificates are available. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 78e19f1 commit f7e43a2

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

keylime/src/device_id.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,4 +485,51 @@ mod tests {
485485
.idevid_asym_alg("")
486486
.idevid_hash_alg("");
487487
}
488+
489+
#[tokio::test]
490+
#[cfg(feature = "testing")]
491+
async fn test_device_id_builder() {
492+
let _mutex = tpm::testing::lock_tests().await;
493+
let certs_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
494+
.join("test-data")
495+
.join("iak-idevid-certs");
496+
497+
if certs_dir.exists() {
498+
let iak_cert = certs_dir.join("iak.cert.pem");
499+
let idevid_cert = certs_dir.join("idevid.cert.pem");
500+
if iak_cert.exists() && idevid_cert.exists() {
501+
let mut tpm_ctx = tpm::Context::new().unwrap(); //#[allow_ci]
502+
let result = DeviceIDBuilder::new()
503+
.iak_handle("")
504+
.iak_cert_path(
505+
iak_cert
506+
.to_str()
507+
.expect("Failed to get str for IAK cert"),
508+
)
509+
.iak_password("")
510+
.iak_template("")
511+
.iak_asym_alg("")
512+
.iak_hash_alg("")
513+
.idevid_handle("")
514+
.idevid_cert_path(
515+
idevid_cert
516+
.to_str()
517+
.expect("Failed to get str for IDevID cert"),
518+
)
519+
.idevid_password("")
520+
.idevid_template("")
521+
.idevid_asym_alg("")
522+
.idevid_hash_alg("")
523+
.build(&mut tpm_ctx);
524+
assert!(result.is_ok(), "Result: {result:?}");
525+
let dev_id = result.unwrap(); //#[allow_ci]
526+
527+
// Flush context to free TPM memory
528+
let r = tpm_ctx.flush_context(dev_id.iak.handle.into());
529+
assert!(r.is_ok(), "Result: {r:?}");
530+
let r = tpm_ctx.flush_context(dev_id.idevid.handle.into());
531+
assert!(r.is_ok(), "Result: {r:?}");
532+
}
533+
}
534+
}
488535
}

tests/generate-iak-idevid-certs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pushd "${OUTPUTDIR}" > /dev/null || exit 1
159159
-out cacert.pem
160160
popd > /dev/null || exit 1
161161
cat intermediate/cacert.pem root/cacert.pem \
162-
> cert-chain.pem
162+
> ca-cert-chain.pem
163163
popd > /dev/null || exit 1
164164

165165
mkdir "${OUTPUTDIR}/ikeys"

tests/run.sh

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# Copyright 2021 Keylime Authors
44

5+
# Check that the script is running from inside the repository tree
6+
GIT_ROOT=$(git rev-parse --show-toplevel) || {
7+
echo "Please run this script from inside the rust-keylime repository tree"
8+
exit 1
9+
}
10+
11+
TESTS_DIR="${GIT_ROOT}/tests"
12+
TEST_DATA_DIR="${GIT_ROOT}/test-data"
13+
TPMDIR="${TEST_DATA_DIR}/tpm-state"
14+
15+
# These certificates are used for the keylime/device_id tests
16+
IAK_IDEVID_CERTS="${GIT_ROOT}/keylime/test-data/iak-idevid-certs"
17+
518
# Store the old TCTI setting
619
OLD_TCTI=$TCTI
720
OLD_TPM2TOOLS_TCTI=$TPM2TOOLS_TCTI
@@ -11,14 +24,13 @@ set -euf -o pipefail
1124

1225
echo "-------- Setting up Software TPM"
1326

14-
# Create temporary directories
15-
TEMPDIR=$(mktemp -d)
16-
TPMDIR="${TEMPDIR}/tpmdir"
17-
mkdir -p ${TPMDIR}
27+
if [[ ! -d "${TPMDIR}" ]]; then
28+
mkdir -p "${TPMDIR}"
29+
fi
1830

1931
# Manufacture a new Software TPM
2032
swtpm_setup --tpm2 \
21-
--tpmstate ${TPMDIR} \
33+
--tpmstate "${TPMDIR}" \
2234
--createek --decryption --create-ek-cert \
2335
--create-platform-cert \
2436
--lock-nvram \
@@ -29,7 +41,7 @@ swtpm_setup --tpm2 \
2941
function start_swtpm {
3042
# Initialize the swtpm socket
3143
swtpm socket --tpm2 \
32-
--tpmstate dir=${TPMDIR} \
44+
--tpmstate dir="${TPMDIR}" \
3345
--flags startup-clear \
3446
--ctrl type=tcp,port=2322 \
3547
--server type=tcp,port=2321 \
@@ -39,7 +51,7 @@ function start_swtpm {
3951

4052
function stop_swtpm {
4153
# Stop swtpm if running
42-
if [[ -n "$SWTPM_PID" ]]; then
54+
if [[ -n "${SWTPM_PID}" ]]; then
4355
echo "Stopping swtpm"
4456
kill $SWTPM_PID
4557
fi
@@ -72,6 +84,25 @@ RUST_BACKTRACE=1 cargo build
7284

7385
echo "-------- Testing"
7486
start_swtpm
87+
88+
89+
# Check that tpm2-openssl provider is available
90+
if openssl list -provider tpm2 -providers > /dev/null; then
91+
# If any IAK/IDevID related certificate is missing, re-generate them
92+
if [[ ( ! -f "${IAK_IDEVID_CERTS}/iak.cert.pem" ) ||
93+
( ! -f "${IAK_IDEVID_CERTS}/iak.cert.der" ) ||
94+
( ! -f "${IAK_IDEVID_CERTS}/idevid.cert.pem" ) ||
95+
( ! -f "${IAK_IDEVID_CERTS}/idevid.cert.der" ) ||
96+
( ! -f "${IAK_IDEVID_CERTS}/ca-cert-chain.pem" ) ]]
97+
then
98+
# Remove any leftover from old certificates
99+
rm -rf "${IAK_IDEVID_CERTS}"
100+
mkdir -p "${IAK_IDEVID_CERTS}"
101+
echo "-------- Create IAK/IDevID certificates"
102+
"${GIT_ROOT}/tests/generate-iak-idevid-certs.sh" -o "${IAK_IDEVID_CERTS}"
103+
fi
104+
fi
105+
75106
mkdir -p /var/lib/keylime
76107
RUST_BACKTRACE=1 RUST_LOG=info \
77108
KEYLIME_CONFIG=$PWD/keylime-agent.conf \

0 commit comments

Comments
 (0)