Skip to content

Commit 41948a5

Browse files
committed
WiP TPM storage tests
1 parent ed48205 commit 41948a5

5 files changed

Lines changed: 217 additions & 12 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name: wolfPKCS11 Storage Format Upgrade Test (TPM)
2+
3+
on:
4+
pull_request:
5+
branches: [ '*' ]
6+
7+
env:
8+
WOLFSSL_VERSION: v5.8.0-stable
9+
10+
jobs:
11+
storage-upgrade-test-tpm:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
base-ref:
16+
- name: master
17+
ref: master
18+
branch-dir: master-branch
19+
- name: v1.3.0
20+
ref: v1.3.0-stable
21+
branch-dir: v1.3.0-stable-branch
22+
23+
steps:
24+
# Checkout the PR branch
25+
- name: Checkout PR branch
26+
uses: actions/checkout@v4
27+
with:
28+
path: pr-branch
29+
30+
# Checkout base branch/tag separately
31+
- name: Checkout ${{ matrix.base-ref.name }} branch
32+
uses: actions/checkout@v4
33+
with:
34+
ref: ${{ matrix.base-ref.ref }}
35+
path: ${{ matrix.base-ref.branch-dir }}
36+
37+
- name: Cache wolfSSL
38+
id: cache-wolfssl
39+
uses: actions/cache@v4
40+
with:
41+
path: wolfssl
42+
key: wolfssl-${{ env.WOLFSSL_VERSION }}
43+
44+
# Setup wolfssl (required dependency)
45+
- name: Checkout wolfssl
46+
if: steps.cache-wolfssl.outputs.cache-hit != 'true'
47+
uses: actions/checkout@v4
48+
with:
49+
repository: wolfssl/wolfssl
50+
path: wolfssl
51+
ref: ${{ env.WOLFSSL_VERSION }}
52+
53+
- name: Build wolfssl
54+
if: steps.cache-wolfssl.outputs.cache-hit != 'true'
55+
working-directory: ./wolfssl
56+
run: |
57+
./autogen.sh
58+
./configure --enable-md5 --enable-cryptocb --enable-aescfb --enable-rsapss --enable-keygen --enable-pwdbased --enable-scrypt \
59+
C_EXTRA_FLAGS="-DWOLFSSL_PUBLIC_MP -DWC_RSA_DIRECT"
60+
make
61+
62+
- name: Install wolfssl
63+
working-directory: ./wolfssl
64+
run: |
65+
sudo make install
66+
sudo ldconfig
67+
68+
# Setup IBM Software TPM simulator
69+
- name: Setup IBM Software TPM
70+
run: |
71+
git clone https://github.com/kgoldman/ibmswtpm2.git
72+
cd ibmswtpm2/src
73+
make
74+
./tpm_server &
75+
sleep 2
76+
cd ../..
77+
78+
# Build and install wolfTPM (required for TPM operations)
79+
- name: Build and install wolfTPM
80+
run: |
81+
git clone https://github.com/wolfSSL/wolftpm.git
82+
cd wolftpm
83+
./autogen.sh
84+
./configure --enable-swtpm --enable-debug
85+
make -j$(nproc)
86+
sudo make install
87+
sudo ldconfig
88+
cd ..
89+
90+
# Phase 1: Build and test base branch/tag with TPM
91+
- name: Modify pkcs11test.c for TPM storage generation
92+
working-directory: ./${{ matrix.base-ref.branch-dir }}
93+
run: |
94+
echo "=== Modifying pkcs11test.c for TPM storage generation ==="
95+
# Check if WOLFPKCS11_NO_STORE is used and change it to use token path
96+
if grep -q 'XSETENV("WOLFPKCS11_NO_STORE"' tests/pkcs11test.c; then
97+
echo "Found WOLFPKCS11_NO_STORE, changing to WOLFPKCS11_TOKEN_PATH"
98+
sed -i 's/XSETENV("WOLFPKCS11_NO_STORE", "1", 1);/XSETENV("WOLFPKCS11_TOKEN_PATH", ".\/store\/pkcs11test", 1);/' tests/pkcs11test.c
99+
else
100+
echo "WOLFPKCS11_NO_STORE not found, assuming WOLFPKCS11_TOKEN_PATH is already set"
101+
fi
102+
echo "=== pkcs11test.c modification completed ==="
103+
104+
- name: Build wolfPKCS11 ${{ matrix.base-ref.name }} with TPM
105+
working-directory: ./${{ matrix.base-ref.branch-dir }}
106+
run: |
107+
echo "=== Building wolfPKCS11 ${{ matrix.base-ref.name }} branch with TPM support ==="
108+
./autogen.sh
109+
./configure --enable-singlethreaded --enable-wolftpm --disable-dh C_EXTRA_FLAGS="-DWOLFPKCS11_TPM_STORE"
110+
make
111+
112+
- name: Run TPM tests on ${{ matrix.base-ref.name }} to generate storage files
113+
working-directory: ./${{ matrix.base-ref.branch-dir }}
114+
run: |
115+
echo "=== Running TPM tests on ${{ matrix.base-ref.name }} branch ==="
116+
# Run specific TPM tests that generate storage files
117+
./tests/pkcs11test
118+
echo "=== ${{ matrix.base-ref.name }} branch TPM test completed ==="
119+
120+
# Phase 2: Build PR branch with TPM and copy storage files from base
121+
- name: Build wolfPKCS11 PR branch with TPM
122+
working-directory: ./pr-branch
123+
run: |
124+
echo "=== Building wolfPKCS11 PR branch with TPM support ==="
125+
./autogen.sh
126+
./configure --enable-singlethreaded --enable-wolftpm --disable-dh C_EXTRA_FLAGS="-DWOLFPKCS11_TPM_STORE"
127+
make
128+
129+
- name: Test TPM storage format compatibility (${{ matrix.base-ref.name }} → PR)
130+
working-directory: ./pr-branch
131+
run: |
132+
echo "=== Testing TPM storage format compatibility with PR branch ==="
133+
echo "This tests that the PR can read TPM storage files created by ${{ matrix.base-ref.name }} branch"
134+
135+
# Run the TPM-specific tests with the copied storage files
136+
echo "=== Running TPM compatibility tests ==="
137+
./tests/pkcs11test
138+
echo "=== TPM storage format upgrade test (${{ matrix.base-ref.name }} → PR) completed successfully ==="
139+
140+
# Capture logs on failure with TPM-specific information
141+
- name: Upload TPM failure logs
142+
if: failure() || cancelled()
143+
uses: actions/upload-artifact@v4
144+
with:
145+
name: tpm-storage-upgrade-test-failure-logs-${{ matrix.base-ref.name }}
146+
path: |
147+
pr-branch/test-suite.log
148+
pr-branch/config.log
149+
${{ matrix.base-ref.branch-dir }}/test-suite.log
150+
${{ matrix.base-ref.branch-dir }}/config.log
151+
retention-days: 5
152+
153+
# Clean up TPM simulator on exit
154+
- name: Cleanup TPM simulator
155+
if: always()
156+
run: |
157+
echo "=== Cleaning up TPM simulator ==="
158+
pkill -f tpm_server || echo "TPM server was not running"

.github/workflows/storage-upgrade-test.yml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ jobs:
1616
- name: master
1717
ref: master
1818
branch-dir: master-branch
19-
- name: v1.3.0
20-
ref: v1.3.0-stable
21-
branch-dir: v1.3.0-stable-branch
19+
# v1.3.0 disk storage is too broken to run
20+
# - name: v1.3.0
21+
# ref: v1.3.0-stable
22+
# branch-dir: v1.3.0-stable-branch
2223

2324
steps:
2425
# Checkout the PR branch
@@ -68,6 +69,19 @@ jobs:
6869
sudo ldconfig
6970
7071
# Phase 1: Build and test base branch/tag
72+
- name: Modify pkcs11test.c for storage generation
73+
working-directory: ./${{ matrix.base-ref.branch-dir }}
74+
run: |
75+
echo "=== Modifying pkcs11test.c for storage generation ==="
76+
# Check if WOLFPKCS11_NO_STORE is used and change it to use token path
77+
if grep -q 'XSETENV("WOLFPKCS11_NO_STORE"' tests/pkcs11test.c; then
78+
echo "Found WOLFPKCS11_NO_STORE, changing to WOLFPKCS11_TOKEN_PATH"
79+
sed -i 's/XSETENV("WOLFPKCS11_NO_STORE", "1", 1);/XSETENV("WOLFPKCS11_TOKEN_PATH", ".\/store\/pkcs11test", 1);/' tests/pkcs11test.c
80+
else
81+
echo "WOLFPKCS11_NO_STORE not found, assuming WOLFPKCS11_TOKEN_PATH is already set"
82+
fi
83+
echo "=== pkcs11test.c modification completed ==="
84+
7185
- name: Build wolfPKCS11 ${{ matrix.base-ref.name }}
7286
working-directory: ./${{ matrix.base-ref.branch-dir }}
7387
run: |
@@ -80,7 +94,7 @@ jobs:
8094
working-directory: ./${{ matrix.base-ref.branch-dir }}
8195
run: |
8296
echo "=== Running tests on ${{ matrix.base-ref.name }} branch ==="
83-
make test
97+
./tests/pkcs11test
8498
echo "=== ${{ matrix.base-ref.name }} branch test completed ==="
8599
86100
# Phase 2: Build PR branch and copy storage files from base
@@ -117,7 +131,7 @@ jobs:
117131
ls -la store/* 2>/dev/null || echo "No wp* files in store/"
118132
119133
# Run the tests with the copied storage files
120-
make test
134+
./tests/pkcs11test
121135
122136
echo "=== Storage format upgrade test (${{ matrix.base-ref.name }} → PR) completed successfully ==="
123137

src/internal.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3931,6 +3931,11 @@ static void wp11_Object_Unstore(WP11_Object* object, int tokenId, int objId)
39313931
if (object->objClass == CKO_CERTIFICATE) {
39323932
storeObjType = WOLFPKCS11_STORE_CERT;
39333933
}
3934+
#ifdef WOLFPKCS11_NSS
3935+
else if (object->objClass == CKO_NSS_TRUST) {
3936+
storeObjType = WOLFPKCS11_STORE_TRUST;
3937+
}
3938+
#endif
39343939
else {
39353940
/* Open access to symmetric key. */
39363941
switch (object->type) {
@@ -4159,7 +4164,7 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
41594164
}
41604165

41614166
/* If there is no pin, there is no login, so decode now */
4162-
if (WP11_Slot_Has_Empty_Pin(slot)) {
4167+
if (WP11_Slot_Has_Empty_Pin(slot) && (ret == 0)) {
41634168
#ifndef WOLFPKCS11_NO_STORE
41644169
object = token->object;
41654170
while (ret == 0 && object != NULL) {

tests/pkcs11test.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,22 @@ static CK_RV test_no_token_init(void* args)
410410
CK_FLAGS expFlags = CKF_RNG | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED;
411411
int flags = CKF_SERIAL_SESSION | CKF_RW_SESSION;
412412

413+
ret = funcList->C_GetTokenInfo(slot, &tokenInfo);
414+
CHECK_CKR(ret, "Get Token Info");
415+
if (ret == CKR_OK) {
416+
/* This will happen if we are re-running the tests */
417+
if (tokenInfo.flags & CKF_LOGIN_REQUIRED) {
418+
fprintf(stderr, "A test re-run, skipping ... ");
419+
return CKR_SKIPPED;
420+
}
421+
}
422+
413423
session = CK_INVALID_HANDLE;
414-
ret = funcList->C_OpenSession(slot, flags, NULL, NULL, &session);
415-
CHECK_CKR(ret, "Open Session");
424+
if (ret == CKR_OK) {
425+
ret = funcList->C_OpenSession(slot, flags, NULL, NULL, &session);
426+
CHECK_CKR(ret, "Open Session");
427+
}
428+
416429
if (ret == CKR_OK) {
417430
#ifndef WOLFPKCS11_NSS
418431
ret = funcList->C_Login(session, CKU_SO, soPin, soPinLen);
@@ -13807,7 +13820,7 @@ static CK_RV pkcs11_test(int slotId, int setPin, int onlySet, int closeDl)
1380713820
{
1380813821
CK_RV ret;
1380913822
int i;
13810-
int attempted = 0, passed = 0;
13823+
int attempted = 0, passed = 0, skipped = 0;
1381113824
int inited = 0;
1381213825

1381313826
/* Set it global. */
@@ -13850,7 +13863,10 @@ static CK_RV pkcs11_test(int slotId, int setPin, int onlySet, int closeDl)
1385013863
for (i = 0; i < testFuncCnt; i++) {
1385113864
if (testFunc[i].attempted) {
1385213865
attempted++;
13853-
if (testFunc[i].ret != CKR_OK) {
13866+
if (testFunc[i].ret == CKR_SKIPPED) {
13867+
skipped++;
13868+
}
13869+
else if (testFunc[i].ret != CKR_OK) {
1385413870
#ifdef DEBUG_WOLFPKCS11
1385513871
if (ret == CKR_OK)
1385613872
fprintf(stderr, "\nFAILED tests:\n");
@@ -13862,7 +13878,11 @@ static CK_RV pkcs11_test(int slotId, int setPin, int onlySet, int closeDl)
1386213878
passed++;
1386313879
}
1386413880
}
13865-
fprintf(stderr, "Result: %d / %d\n", passed, attempted);
13881+
fprintf(stderr, "Result: attempted: %d, passed: %d", attempted, passed);
13882+
if (skipped != 0) {
13883+
fprintf(stderr, ", skipped %d", skipped);
13884+
}
13885+
fprintf(stderr, "\n");
1386613886
if (ret == CKR_OK)
1386713887
fprintf(stderr, "Success\n");
1386813888
else
@@ -13944,7 +13964,7 @@ int pkcs11test_test(int argc, char* argv[])
1394413964
int i;
1394513965

1394613966
#ifndef WOLFPKCS11_NO_ENV
13947-
XSETENV("WOLFPKCS11_NO_STORE", "1", 1);
13967+
XSETENV("WOLFPKCS11_TOKEN_PATH", "./store/pkcs11test", 1);
1394813968
#endif
1394913969

1395013970
argc--;

tests/unit.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
{ func, #func, CKR_OK, 0, 0, flags, setup, teardown, argsSz }
118118
#endif
119119

120+
#define CKR_SKIPPED CKR_VENDOR_DEFINED+77
121+
120122
typedef struct TEST_FUNC
121123
{
122124
CK_RV (*func)(void* args);
@@ -299,6 +301,9 @@ static CK_RV run_tests(TEST_FUNC* testFunc, int testFuncCnt, int onlySet,
299301
testFunc[i].cnt);
300302
if (testFunc[i].ret == CKR_OK)
301303
fprintf(stderr, "PASSED\n");
304+
else if (testFunc[i].ret == CKR_SKIPPED) {
305+
fprintf(stderr, "SKIPPED\n");
306+
}
302307
else
303308
fprintf(stderr, "FAILED\n");
304309
}
@@ -342,6 +347,9 @@ static CK_RV run_tests(TEST_FUNC* testFunc, int testFuncCnt, int onlySet,
342347
fprintf(stderr, "%d: %s ... ", i + 1, testFunc[i].name);
343348
if (testFunc[i].ret == CKR_OK)
344349
fprintf(stderr, "PASSED\n");
350+
else if (testFunc[i].ret == CKR_SKIPPED) {
351+
fprintf(stderr, "SKIPPED\n");
352+
}
345353
else if (verbose)
346354
fprintf(stderr, "FAILED\n");
347355

0 commit comments

Comments
 (0)