Skip to content

Commit ded614e

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

File tree

5 files changed

+78
-37
lines changed

5 files changed

+78
-37
lines changed

examples/run_examples.sh

Lines changed: 21 additions & 18 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

@@ -197,12 +194,12 @@ run_tpm_tls_client() { # Usage: run_tpm_tls_client [ecc/rsa] [tpmargs]]
197194
echo -e "TLS test (TPM as client) $1 $2"
198195
generate_port
199196
pushd $WOLFSSL_PATH >> run.out
200-
./examples/server/server -p $port -g -A ./certs/tpm-ca-$1-cert.pem 2>&1 >> $PWD/run.out &
197+
echo ./examples/server/server -p $port -g -A ./certs/tpm-ca-$1-cert.pem 2>&1 >> $PWD/run.out &
201198
RESULT=$?
202199
[ $RESULT -ne 0 ] && echo -e "tls server $1 $2 failed! $RESULT" && exit 1
203200
popd >> run.out
204-
sleep 0.2
205-
./examples/tls/tls_client -p=$port -$1 $2 2>&1 >> run.out
201+
sleep 0.4
202+
echo ./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
208205
}
@@ -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,18 +220,14 @@ 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

@@ -350,11 +343,21 @@ fi
350343
echo -e "Seal/Unseal (PCR policy)"
351344
./examples/seal/seal sealedkeyblob.bin mySecretMessage >> run.out
352345
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
346+
[ $RESULT -ne 0 ] && echo -e "seal failed! $RESULT" && exit 1
347+
./examples/seal/unseal message.raw sealedkeyblob.bin >> run.out
348+
RESULT=$?
349+
[ $RESULT -ne 0 ] && echo -e "unseal failed! $RESULT" && exit 1
356350
rm -f sealedkeyblob.bin
357351

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

359362
# Seal/Unseal (Policy auth)
360363
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/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;

0 commit comments

Comments
 (0)