Skip to content

Commit 9cad7f4

Browse files
committed
Fix some of the example TODO's. Fixes for seal/unseal example. Fixes for TLS with param enc. Fix signed_timestamp with ECC.
1 parent f35dbbb commit 9cad7f4

File tree

7 files changed

+97
-52
lines changed

7 files changed

+97
-52
lines changed

examples/run_examples.sh

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
7777
./examples/keygen/keyload ecckeyblob.bin -aes >> run.out
7878
RESULT=$?
7979
[ $RESULT -ne 0 ] && echo -e "keyload ecc param enc failed! $RESULT" && exit 1
80-
8180
./examples/keygen/keyimport ecckeyblob.bin -ecc >> run.out
8281
RESULT=$?
8382
[ $RESULT -ne 0 ] && echo -e "keyload ecc import failed! $RESULT" && exit 1
84-
# TODO: TPM2_Load (TPM_RC_INTEGRITY)
85-
#./examples/keygen/keyload ecckeyblob.bin >> run.out
8683
fi
8784
rm -f ecckeyblob.bin
8885

@@ -201,7 +198,7 @@ run_tpm_tls_client() { # Usage: run_tpm_tls_client [ecc/rsa] [tpmargs]]
201198
RESULT=$?
202199
[ $RESULT -ne 0 ] && echo -e "tls server $1 $2 failed! $RESULT" && exit 1
203200
popd >> run.out
204-
sleep 0.2
201+
sleep 0.4
205202
./examples/tls/tls_client -p=$port -$1 $2 2>&1 >> run.out
206203
RESULT=$?
207204
[ $RESULT -ne 0 ] && echo -e "tpm tls client $1 $2 failed! $RESULT" && exit 1
@@ -214,7 +211,7 @@ run_tpm_tls_server() { # Usage: run_tpm_tls_server [ecc/rsa] [tpmargs]]
214211
RESULT=$?
215212
[ $RESULT -ne 0 ] && echo -e "tpm tls server $1 $2 failed! $RESULT" && exit 1
216213
pushd $WOLFSSL_PATH >> run.out
217-
sleep 0.2
214+
sleep 0.4
218215
./examples/client/client -p $port -g -A ./certs/tpm-ca-$1-cert.pem 2>&1 >> $PWD/run.out
219216
RESULT=$?
220217
[ $RESULT -ne 0 ] && echo -e "tls client $1 $2 failed! $RESULT" && exit 1
@@ -223,24 +220,20 @@ run_tpm_tls_server() { # Usage: run_tpm_tls_server [ecc/rsa] [tpmargs]]
223220

224221
if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
225222
run_tpm_tls_client "rsa" ""
226-
# TODO: Not working (TPM2_Load TPM_RC_COMMAND_SIZE)
227-
#run_tpm_tls_client "rsa" "-aes"
223+
run_tpm_tls_client "rsa" "-aes"
228224
run_tpm_tls_client "ecc" ""
229-
# TODO: Not working (TPM2_Load TPM_RC_COMMAND_SIZE)
230-
#run_tpm_tls_client "ecc" "-aes"
225+
run_tpm_tls_client "ecc" "-aes"
231226

232227
run_tpm_tls_server "rsa" ""
233-
# TODO: Not working (TPM2_Load TPM_RC_COMMAND_SIZE)
234-
#run_tpm_tls_server "rsa" "-aes"
228+
run_tpm_tls_server "rsa" "-aes"
235229
run_tpm_tls_server "ecc" ""
236-
# TODO: Not working (TPM2_Load TPM_RC_COMMAND_SIZE)
237-
#run_tpm_tls_server "ecc" "-aes"
230+
run_tpm_tls_server "ecc" "-aes"
238231
fi
239232

240233

241234
# Clock Tests
242235
echo -e "Clock tests"
243-
./examples/timestamp/clock_set
236+
./examples/timestamp/clock_set >> run.out
244237
RESULT=$?
245238
[ $RESULT -ne 0 ] && echo -e "clock set failed! $RESULT" && exit 1
246239

@@ -255,11 +248,14 @@ if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
255248
RESULT=$?
256249
[ $RESULT -ne 0 ] && echo -e "signed_timestamp param enc failed! $RESULT" && exit 1
257250
fi
258-
# TODO: Test broken (wolfTPM2_GetTime TPM_RC_SCHEME)
259-
#./examples/timestamp/signed_timestamp -ecc >> run.out
260-
#if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
261-
#./examples/timestamp/signed_timestamp -ecc -aes >> run.out
262-
#fi
251+
./examples/timestamp/signed_timestamp -ecc >> run.out
252+
RESULT=$?
253+
[ $RESULT -ne 0 ] && echo -e "signed_timestamp ecc failed! $RESULT" && exit 1
254+
if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
255+
./examples/timestamp/signed_timestamp -ecc -aes >> run.out
256+
RESULT=$?
257+
[ $RESULT -ne 0 ] && echo -e "signed_timestamp ecc param enc failed! $RESULT" && exit 1
258+
fi
263259

264260
./examples/attestation/make_credential >> run.out
265261
RESULT=$?
@@ -350,11 +346,21 @@ fi
350346
echo -e "Seal/Unseal (PCR policy)"
351347
./examples/seal/seal sealedkeyblob.bin mySecretMessage >> run.out
352348
RESULT=$?
353-
[ $RESULT -ne 0 ] && echo -e "seal pcr failed! $RESULT" && exit 1
354-
# TODO (TPM2_Load TPM_RC_BAD_AUTH)
355-
#./examples/seal/unseal message.raw sealedkeyblob.bin >> run.out
349+
[ $RESULT -ne 0 ] && echo -e "seal failed! $RESULT" && exit 1
350+
./examples/seal/unseal message.raw sealedkeyblob.bin >> run.out
351+
RESULT=$?
352+
[ $RESULT -ne 0 ] && echo -e "unseal failed! $RESULT" && exit 1
356353
rm -f sealedkeyblob.bin
357354

355+
if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
356+
./examples/seal/seal sealedkeyblob.bin mySecretMessage -aes >> run.out
357+
RESULT=$?
358+
[ $RESULT -ne 0 ] && echo -e "seal aes failed! $RESULT" && exit 1
359+
./examples/seal/unseal message.raw sealedkeyblob.bin -aes >> run.out
360+
RESULT=$?
361+
[ $RESULT -ne 0 ] && echo -e "unseal aes failed! $RESULT" && exit 1
362+
rm -f sealedkeyblob.bin
363+
fi
358364

359365
# Seal/Unseal (Policy auth)
360366
echo -e "Seal/Unseal (Policy auth)"

examples/seal/seal.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ static void usage(void)
4040
{
4141
printf("Expected usage:\n");
4242
printf("./examples/seal/seal [filename] [userdata]\n");
43-
printf("* filename: Name of the file where the TPM key will be stored\n");
44-
printf("* userdata: Arbitrary data to seal inside the TPM key (no whitespaces)\n");
45-
printf("Demo usage, without parameters, uses keyblob.bin as a filename\n");
43+
printf("* -aes/xor: Use Parameter Encryption\n");
44+
printf("* filename: Name of the file where the TPM key will be stored (default: keyblob.bin)\n");
45+
printf("* userdata: Arbitrary data to seal inside the TPM key (no whitespaces) (default: My1Pass2Phrase3)\n");
4646
}
4747

4848
int TPM2_Seal_Example(void* userCtx, int argc, char *argv[])
@@ -95,6 +95,7 @@ int TPM2_Seal_Example(void* userCtx, int argc, char *argv[])
9595

9696
printf("TPM2.0 Simple Seal example\n");
9797
printf("\tKey Blob: %s\n", outputFile);
98+
printf("\tUser Data: %s\n", userData);
9899
printf("\tUse Parameter Encryption: %s\n", TPM2_GetAlgName(paramEncAlg));
99100

100101
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
@@ -159,6 +160,9 @@ int TPM2_Seal_Example(void* userCtx, int argc, char *argv[])
159160
printf("\nFailure 0x%x: %s\n\n", rc, wolfTPM2_GetRCString(rc));
160161
}
161162

163+
/* Remove the auth for loaded TPM seal object */
164+
wolfTPM2_UnsetAuth(&dev, 0);
165+
162166
/* Close handles */
163167
wolfTPM2_UnloadHandle(&dev, &storage.handle);
164168
wolfTPM2_UnloadHandle(&dev, &newKey.handle);

examples/seal/unseal.c

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@ static void usage(void)
4141
{
4242
printf("Expected usage:\n");
4343
printf("./examples/seal/unseal [filename] [inkey_filename]\n");
44-
printf("* filename - File contaning a TPM seal key\n");
44+
printf("* -aes/xor: Use Parameter Encryption\n");
45+
printf("* filename: Output for unsealed data (default: unseal.bin)\n");
46+
printf("* inkey_filename: File with sealed keyed hashed object (keyblob.bin)\n");
4547
printf("Demo usage, without arguments, uses keyblob.bin file input.\n");
4648
}
4749

4850
int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
4951
{
5052
int rc;
5153
WOLFTPM2_DEV dev;
52-
WOLFTPM2_KEY key;
53-
TPM2B_AUTH auth;
54+
WOLFTPM2_KEYBLOB newKey;
55+
WOLFTPM2_KEY storage; /* SRK */
56+
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
57+
WOLFTPM2_SESSION tpmSession;
5458
const char *filename = "unseal.bin";
5559
const char *inkeyfilename = "keyblob.bin";
5660
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
@@ -60,14 +64,11 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
6064
Unseal_In cmdIn_unseal;
6165
Unseal_Out cmdOut_unseal;
6266

63-
WOLFTPM2_KEYBLOB newKey;
64-
WOLFTPM2_KEY storage; /* SRK */
65-
66-
67+
XMEMSET(&storage, 0, sizeof(storage));
68+
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
6769
XMEMSET(&cmdIn_unseal, 0, sizeof(cmdIn_unseal));
6870
XMEMSET(&cmdOut_unseal, 0, sizeof(cmdOut_unseal));
69-
XMEMSET(&key, 0, sizeof(key));
70-
XMEMSET(&auth, 0, sizeof(auth));
71+
XMEMSET(&newKey, 0, sizeof(newKey));
7172

7273
if (argc >= 2) {
7374
if (XSTRCMP(argv[1], "-?") == 0 ||
@@ -85,6 +86,23 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
8586
inkeyfilename = argv[2];
8687
}
8788
}
89+
while (argc > 1) {
90+
if (XSTRCMP(argv[argc-1], "-aes") == 0) {
91+
paramEncAlg = TPM_ALG_CFB;
92+
}
93+
else if (XSTRCMP(argv[argc-1], "-xor") == 0) {
94+
paramEncAlg = TPM_ALG_XOR;
95+
}
96+
else if (argv[argc-1][0] == '-') {
97+
printf("Warning: Unrecognized option: %s\n", argv[argc-1]);
98+
}
99+
argc--;
100+
}
101+
102+
printf("TPM2.0 Simple Unseal example\n");
103+
printf("\tKey Blob: %s\n", inkeyfilename);
104+
printf("\tUse Parameter Encryption: %s\n", TPM2_GetAlgName(paramEncAlg));
105+
88106

89107
printf("Example how to unseal data using TPM2.0\n");
90108
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
@@ -97,6 +115,21 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
97115
rc = getPrimaryStoragekey(&dev, &storage, TPM_ALG_RSA);
98116
if (rc != 0) goto exit;
99117

118+
if (paramEncAlg != TPM_ALG_NULL) {
119+
/* Start an authenticated session (salted / unbound) with parameter encryption */
120+
rc = wolfTPM2_StartSession(&dev, &tpmSession, &storage, NULL,
121+
TPM_SE_HMAC, paramEncAlg);
122+
if (rc != 0) goto exit;
123+
printf("TPM2_StartAuthSession: sessionHandle 0x%x\n",
124+
(word32)tpmSession.handle.hndl);
125+
126+
/* set session for authorization of the storage key */
127+
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSession,
128+
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt | TPMA_SESSION_continueSession));
129+
if (rc != 0) goto exit;
130+
131+
}
132+
100133
rc = readKeyBlob(inkeyfilename, &newKey);
101134
if (rc != 0) goto exit;
102135

@@ -109,9 +142,9 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
109142
(word32)newKey.handle.hndl);
110143

111144
/* Set authorization for using the seal key */
112-
auth.size = (int)sizeof(gKeyAuth) - 1;
113-
XMEMCPY(auth.buffer, gKeyAuth, auth.size);
114-
wolfTPM2_SetAuthPassword(&dev, 0, &auth);
145+
newKey.handle.auth.size = (int)sizeof(gKeyAuth) - 1;
146+
XMEMCPY(newKey.handle.auth.buffer, gKeyAuth, newKey.handle.auth.size);
147+
wolfTPM2_SetAuthHandle(&dev, 0, &newKey.handle);
115148

116149
cmdIn_unseal.itemHandle = newKey.handle.hndl;
117150

@@ -146,12 +179,13 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
146179
(void)filename;
147180
#endif
148181

149-
/* Remove the loaded TPM seal object */
150-
wolfTPM2_SetAuthPassword(&dev, 0, NULL);
182+
/* Remove the auth for loaded TPM seal object */
183+
wolfTPM2_UnsetAuth(&dev, 0);
151184

152185
exit:
153186
wolfTPM2_UnloadHandle(&dev, &storage.handle);
154187
wolfTPM2_UnloadHandle(&dev, &newKey.handle);
188+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
155189

156190
wolfTPM2_Cleanup(&dev);
157191
return rc;

examples/timestamp/signed_timestamp.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,8 @@ int TPM2_Timestamp_TestArgs(void* userCtx, int argc, char *argv[])
180180
if (paramEncAlg != TPM_ALG_NULL) {
181181
sessionAttributes |= (TPMA_SESSION_decrypt | TPMA_SESSION_encrypt);
182182
}
183-
#if 0
184-
/* TODO: Investigate param enc with signed timestamp */
185183
rc = wolfTPM2_SetAuthSession(&dev, 2, &tpmSession, sessionAttributes);
186184
if (rc != 0) goto exit;
187-
#else
188-
(void)sessionAttributes;
189-
#endif
190185

191186
/* At this stage: The SRK is created, AIK is created and loaded,
192187
* Endorsement Hierarchy is enabled through policySecret,

examples/tls/tls_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ int TPM2_TLS_ClientArgs(void* userCtx, int argc, char *argv[])
207207
(word32)tpmSession.handle.hndl);
208208

209209
/* set session for authorization of the storage key */
210-
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSession,
210+
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSession,
211211
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt | TPMA_SESSION_continueSession));
212212
if (rc != 0) goto exit;
213213
}

examples/tls/tls_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
222222
(word32)tpmSession.handle.hndl);
223223

224224
/* set session for authorization of the storage key */
225-
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSession,
225+
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSession,
226226
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt |
227227
TPMA_SESSION_continueSession));
228228
if (rc != 0) goto exit;

src/tpm2_wrap.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5823,17 +5823,23 @@ int wolfTPM2_GetTime(WOLFTPM2_KEY* aikKey, GetTime_Out* getTimeOut)
58235823
int rc;
58245824
GetTime_In getTimeCmd;
58255825

5826-
if (getTimeOut == NULL) return BAD_FUNC_ARG;
5826+
if (getTimeOut == NULL)
5827+
return BAD_FUNC_ARG;
58275828

58285829
/* GetTime */
58295830
XMEMSET(&getTimeCmd, 0, sizeof(getTimeCmd));
58305831
XMEMSET(getTimeOut, 0, sizeof(*getTimeOut));
58315832
getTimeCmd.privacyAdminHandle = TPM_RH_ENDORSEMENT;
5832-
/* TPM_RH_NULL is a valid handle for NULL signature */
5833-
getTimeCmd.signHandle = aikKey->handle.hndl;
5834-
/* TPM_ALG_NULL is a valid handle for NULL signature */
5835-
getTimeCmd.inScheme.scheme = TPM_ALG_RSASSA;
5836-
getTimeCmd.inScheme.details.rsassa.hashAlg = TPM_ALG_SHA256;
5833+
if (aikKey != NULL) {
5834+
TPMT_ASYM_SCHEME* scheme =
5835+
&aikKey->pub.publicArea.parameters.asymDetail.scheme;
5836+
getTimeCmd.signHandle = aikKey->handle.hndl;
5837+
getTimeCmd.inScheme.scheme = scheme->scheme;
5838+
getTimeCmd.inScheme.details.any.hashAlg = scheme->details.anySig.hashAlg;
5839+
}
5840+
else {
5841+
getTimeCmd.signHandle = TPM_RH_NULL;
5842+
}
58375843
getTimeCmd.qualifyingData.size = 0; /* optional */
58385844
rc = TPM2_GetTime(&getTimeCmd, getTimeOut);
58395845
if (rc != TPM_RC_SUCCESS) {

0 commit comments

Comments
 (0)