Skip to content

Commit e9ca868

Browse files
authored
Merge pull request #320 from dgarske/fix_endorsement
Fixes for using endorsement hierarchy
2 parents 7c079dd + 310fe7c commit e9ca868

File tree

14 files changed

+357
-214
lines changed

14 files changed

+357
-214
lines changed

.github/workflows/cmake-build.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: WolfTPM CMake Build Tests
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
#pull wolfTPM
16+
- uses: actions/checkout@master
17+
18+
# Install cmake
19+
- name: Install cmake
20+
run: |
21+
sudo apt-get update
22+
sudo apt-get install -y cmake
23+
24+
#pull and build wolfssl
25+
- uses: actions/checkout@master
26+
with:
27+
repository: wolfssl/wolfssl
28+
path: wolfssl
29+
- name: Build wolfssl
30+
working-directory: ./wolfssl
31+
run: |
32+
mkdir build
33+
cd build
34+
cmake -DWOLFSSL_TPM=yes ..
35+
make
36+
sudo make install
37+
38+
#build wolftpm
39+
- name: Build wolfTPM
40+
run: |
41+
mkdir build
42+
cd build
43+
cmake -DWOLFTPM_INTERFACE=SWTPM ..
44+
make

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(TPM_SOURCES
3535
src/tpm2_tis.c
3636
src/tpm2_winapi.c
3737
src/tpm2_wrap.c
38+
src/tpm2_cryptocb.c
3839
hal/tpm_io.c
3940
)
4041

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,10 @@ Connection: close
791791

792792
## Todo
793793

794-
* Key Generation and Attestation examples using endorsement hierarchy "-eh" are broken.
795-
* Update to v1.59 of specification (adding CertifyX509)
794+
* Add support for Endorsement certificates (EK Credential Profile).
795+
* Update to v1.59 of specification (adding CertifyX509).
796796
* Inner wrap support for SensitiveToPrivate.
797+
* Firmware upgrade support on TPM's.
797798

798799
## Support
799800

examples/attestation/activate_credential.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static void usage(void)
4444
printf("Expected usage:\n");
4545
printf("./examples/attestation/activate_credential [cred.blob] [-eh]\n");
4646
printf("* cred.blob is a input file holding the generated credential.\n");
47+
printf("* -eh: Use the EK public key to encrypt the challenge\n");
4748
printf("Demo usage without parameters, uses \"cred.blob\" filename.\n");
4849
}
4950

@@ -64,14 +65,8 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
6465
const char *input = "cred.blob";
6566
const char *keyblob = "keyblob.bin";
6667

67-
union {
68-
ActivateCredential_In activCred;
69-
byte maxInput[MAX_COMMAND_SIZE];
70-
} cmdIn;
71-
union {
72-
ActivateCredential_Out activCred;
73-
byte maxOutput[MAX_RESPONSE_SIZE];
74-
} cmdOut;
68+
ActivateCredential_In activCredIn;
69+
ActivateCredential_Out activCredOut;
7570

7671
if (argc == 1) {
7772
printf("Using default values\n");
@@ -157,9 +152,13 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
157152
/* Set the created Policy Session for use in next operation */
158153
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSession, 0);
159154
if (rc != 0) goto exit;
155+
/* Set the name for the endorsement handle */
156+
rc = wolfTPM2_SetAuthHandleName(&dev, 1, &endorse.handle);
157+
if (rc != 0) goto exit;
160158
}
161159
else {
162-
wolfTPM2_SetAuthHandle(&dev, 1, &storage.handle);
160+
rc = wolfTPM2_SetAuthHandle(&dev, 1, &storage.handle);
161+
if (rc != 0) goto exit;
163162
}
164163

165164
/* Prepare the auth password for the Attestation Key */
@@ -169,19 +168,19 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
169168
wolfTPM2_SetAuthHandle(&dev, 0, &akKey.handle);
170169

171170
/* Prepare the Activate Credential command */
172-
XMEMSET(&cmdIn.activCred, 0, sizeof(cmdIn.activCred));
173-
XMEMSET(&cmdOut.activCred, 0, sizeof(cmdOut.activCred));
174-
cmdIn.activCred.activateHandle = akKey.handle.hndl;
175-
cmdIn.activCred.keyHandle = primary->handle.hndl;
171+
XMEMSET(&activCredIn, 0, sizeof(activCredIn));
172+
XMEMSET(&activCredOut, 0, sizeof(activCredOut));
173+
activCredIn.activateHandle = akKey.handle.hndl;
174+
activCredIn.keyHandle = primary->handle.hndl;
176175
/* Read credential from the user file */
177176
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
178177
fp = XFOPEN(input, "rb");
179178
if (fp != XBADFILE) {
180-
dataSize = (int)XFREAD((BYTE*)&cmdIn.activCred.credentialBlob, 1,
181-
sizeof(cmdIn.activCred.credentialBlob), fp);
179+
dataSize = (int)XFREAD((BYTE*)&activCredIn.credentialBlob, 1,
180+
sizeof(activCredIn.credentialBlob), fp);
182181
if (dataSize > 0) {
183-
dataSize += (int)XFREAD((BYTE*)&cmdIn.activCred.secret, 1,
184-
sizeof(cmdIn.activCred.secret), fp);
182+
dataSize += (int)XFREAD((BYTE*)&activCredIn.secret, 1,
183+
sizeof(activCredIn.secret), fp);
185184
}
186185
XFCLOSE(fp);
187186
}
@@ -192,18 +191,28 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
192191
goto exit;
193192
#endif
194193
/* All required data to verify the credential is prepared */
195-
rc = TPM2_ActivateCredential(&cmdIn.activCred, &cmdOut.activCred);
194+
rc = TPM2_ActivateCredential(&activCredIn, &activCredOut);
196195
if (rc != TPM_RC_SUCCESS) {
197-
printf("TPM2_ActivateCredentials failed 0x%x: %s\n", rc,
196+
printf("TPM2_ActivateCredential failed 0x%x: %s\n", rc,
198197
TPM2_GetRCString(rc));
199198
goto exit;
200199
}
201200
printf("TPM2_ActivateCredential success\n");
201+
if (endorseKey) {
202+
/* The policy session is closed after use.
203+
* Reset handle, so we don't try and free it */
204+
tpmSession.handle.hndl = TPM_RH_NULL;
205+
}
206+
207+
printf("Secret: %d\n", activCredOut.certInfo.size);
208+
TPM2_PrintBin(activCredOut.certInfo.buffer,
209+
activCredOut.certInfo.size);
202210

203211
exit:
204212

205213
wolfTPM2_UnloadHandle(&dev, &primary->handle);
206214
wolfTPM2_UnloadHandle(&dev, &akKey.handle);
215+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
207216
wolfTPM2_Cleanup(&dev);
208217

209218
exit_badargs:

examples/attestation/make_credential.c

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,10 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
6868
const char *srkPubFile = "srk.pub";
6969
const char *pubFilename = NULL;
7070

71-
union {
72-
MakeCredential_In makeCred;
73-
LoadExternal_In loadExtIn;
74-
byte maxInput[MAX_COMMAND_SIZE];
75-
} cmdIn;
76-
union {
77-
MakeCredential_Out makeCred;
78-
LoadExternal_Out loadExtOut;
79-
byte maxOutput[MAX_RESPONSE_SIZE];
80-
} cmdOut;
71+
MakeCredential_In makeCredIn;
72+
MakeCredential_Out makeCredOut;
73+
LoadExternal_In loadExtIn;
74+
LoadExternal_Out loadExtOut;
8175

8276
if (argc == 1) {
8377
printf("Using public key from SRK to create the challenge\n");
@@ -100,12 +94,6 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
10094
goto exit_badargs;
10195
}
10296

103-
XMEMSET(&name, 0, sizeof(name));
104-
XMEMSET(&cmdIn.makeCred, 0, sizeof(cmdIn.makeCred));
105-
XMEMSET(&cmdOut.makeCred, 0, sizeof(cmdOut.makeCred));
106-
XMEMSET(&cmdIn.loadExtIn, 0, sizeof(cmdIn.loadExtIn));
107-
XMEMSET(&cmdOut.loadExtOut, 0, sizeof(cmdOut.loadExtOut));
108-
10997
printf("Demo how to create a credential challenge for remote attestation\n");
11098
printf("Credential will be stored in %s\n", output);
11199

@@ -125,24 +113,26 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
125113
}
126114
rc = readKeyBlob(pubFilename, &primary);
127115
if (rc != 0) {
128-
printf("Failure to load %s\n", pubFilename);
116+
printf("Failure to read %s\n", pubFilename);
129117
goto exit;
130118
}
119+
131120
/* Prepare the key for use by the TPM */
132-
XMEMCPY(&cmdIn.loadExtIn.inPublic, &primary.pub,
133-
sizeof(cmdIn.loadExtIn.inPublic));
134-
cmdIn.loadExtIn.hierarchy = TPM_RH_NULL;
135-
rc = TPM2_LoadExternal(&cmdIn.loadExtIn, &cmdOut.loadExtOut);
121+
XMEMSET(&loadExtIn, 0, sizeof(loadExtIn));
122+
XMEMSET(&loadExtOut, 0, sizeof(loadExtOut));
123+
XMEMCPY(&loadExtIn.inPublic, &primary.pub, sizeof(loadExtIn.inPublic));
124+
loadExtIn.hierarchy = TPM_RH_NULL;
125+
rc = TPM2_LoadExternal(&loadExtIn, &loadExtOut);
136126
if (rc != TPM_RC_SUCCESS) {
137127
printf("TPM2_LoadExternal: failed %d: %s\n", rc,
138128
wolfTPM2_GetRCString(rc));
139129
return rc;
140130
}
141131
printf("Public key for encryption loaded\n");
142-
handle.hndl = cmdOut.loadExtOut.objectHandle;
143-
132+
handle.hndl = loadExtOut.objectHandle;
144133
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
145134
/* Load AK Name digest */
135+
XMEMSET(&name, 0, sizeof(name));
146136
fp = XFOPEN("ak.name", "rb");
147137
if (fp != XBADFILE) {
148138
size_t nameReadSz = XFREAD((BYTE*)&name, 1, sizeof(name), fp);
@@ -153,31 +143,37 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
153143
#endif
154144

155145
/* Create secret for the attestation server */
156-
cmdIn.makeCred.credential.size = CRED_SECRET_SIZE;
157-
wolfTPM2_GetRandom(&dev, cmdIn.makeCred.credential.buffer,
158-
cmdIn.makeCred.credential.size);
159-
/* Prepare the AK name */
160-
cmdIn.makeCred.objectName.size = name.size;
161-
XMEMCPY(cmdIn.makeCred.objectName.name, name.name,
162-
cmdIn.makeCred.objectName.size);
146+
XMEMSET(&makeCredIn, 0, sizeof(makeCredIn));
147+
XMEMSET(&makeCredOut, 0, sizeof(makeCredOut));
148+
makeCredIn.credential.size = CRED_SECRET_SIZE;
149+
wolfTPM2_GetRandom(&dev, makeCredIn.credential.buffer,
150+
makeCredIn.credential.size);
151+
/* Set the object name */
152+
makeCredIn.objectName.size = name.size;
153+
XMEMCPY(makeCredIn.objectName.name, name.name,
154+
makeCredIn.objectName.size);
163155
/* Set TPM key and execute */
164-
cmdIn.makeCred.handle = handle.hndl;
165-
rc = TPM2_MakeCredential(&cmdIn.makeCred, &cmdOut.makeCred);
156+
makeCredIn.handle = handle.hndl;
157+
rc = TPM2_MakeCredential(&makeCredIn, &makeCredOut);
166158
if (rc != TPM_RC_SUCCESS) {
167-
printf("TPM2_MakeCredentials failed 0x%x: %s\n", rc,
159+
printf("TPM2_MakeCredential failed 0x%x: %s\n", rc,
168160
TPM2_GetRCString(rc));
169161
goto exit;
170162
}
171163
printf("TPM2_MakeCredential success\n");
172164

165+
printf("Secret: %d\n", makeCredIn.credential.size);
166+
TPM2_PrintBin(makeCredIn.credential.buffer,
167+
makeCredIn.credential.size);
168+
173169
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
174170
fp = XFOPEN(output, "wb");
175171
if (fp != XBADFILE) {
176-
dataSize = (int)XFWRITE((BYTE*)&cmdOut.makeCred.credentialBlob, 1,
177-
sizeof(cmdOut.makeCred.credentialBlob), fp);
172+
dataSize = (int)XFWRITE((BYTE*)&makeCredOut.credentialBlob, 1,
173+
sizeof(makeCredOut.credentialBlob), fp);
178174
if (dataSize > 0) {
179-
dataSize += (int)XFWRITE((BYTE*)&cmdOut.makeCred.secret, 1,
180-
sizeof(cmdOut.makeCred.secret), fp);
175+
dataSize += (int)XFWRITE((BYTE*)&makeCredOut.secret, 1,
176+
sizeof(makeCredOut.secret), fp);
181177
}
182178
XFCLOSE(fp);
183179
}

examples/keygen/keygen.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
141141
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_RSA)
142142
const char *pemFilename = NULL;
143143
#endif
144-
FILE *fp;
145144
#endif
146145
size_t len = 0;
147146
char symMode[] = "aesctr";
@@ -251,20 +250,26 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
251250
rc = wolfTPM2_StartSession(&dev, &tpmSession, primary, NULL,
252251
TPM_SE_HMAC, paramEncAlg);
253252
if (rc != 0) goto exit;
254-
printf("TPM2_StartAuthSession: sessionHandle 0x%x\n",
253+
printf("HMAC Session: Handle 0x%x\n",
255254
(word32)tpmSession.handle.hndl);
256255

257256
/* set session for authorization of the primary key */
258257
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSession,
259-
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt | TPMA_SESSION_continueSession));
258+
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt |
259+
TPMA_SESSION_continueSession));
260260
if (rc != 0) goto exit;
261261
}
262262

263263
if (endorseKey) {
264264
/* Endorsement Key requires authorization with Policy */
265-
wolfTPM2_CreateAuthSession_EkPolicy(&dev, &tpmSession);
265+
rc = wolfTPM2_CreateAuthSession_EkPolicy(&dev, &tpmSession);
266+
if (rc != 0) goto exit;
267+
printf("EK Policy Session: Handle 0x%x\n",
268+
(word32)tpmSession.handle.hndl);
269+
266270
/* Set the created Policy Session for use in next operation */
267-
wolfTPM2_SetAuthSession(&dev, 0, &tpmSession, 0);
271+
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSession, 0);
272+
if (rc != 0) goto exit;
268273
}
269274

270275
/* Create new key */
@@ -285,6 +290,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
285290
else {
286291
rc = BAD_FUNC_ARG;
287292
}
293+
if (rc != 0) goto exit;
288294

289295
/* set session for authorization key */
290296
auth.size = (int)sizeof(gAiKeyAuth)-1;
@@ -341,11 +347,25 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
341347
printf("wolfTPM2_CreateKey failed\n");
342348
goto exit;
343349
}
350+
if (endorseKey) {
351+
/* Endorsement policy session is closed after use, so start another */
352+
rc = wolfTPM2_CreateAuthSession_EkPolicy(&dev, &tpmSession);
353+
if (rc == 0) {
354+
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSession, 0);
355+
}
356+
if (rc != 0) goto exit;
357+
}
344358
rc = wolfTPM2_LoadKey(&dev, &newKeyBlob, &primary->handle);
345359
if (rc != TPM_RC_SUCCESS) {
346360
printf("wolfTPM2_LoadKey failed\n");
347361
goto exit;
348362
}
363+
if (endorseKey) {
364+
/* The policy session is closed after use.
365+
* Reset handle, so we don't try and free it */
366+
tpmSession.handle.hndl = TPM_RH_NULL;
367+
}
368+
349369
printf("New key created and loaded (pub %d, priv %d bytes)\n",
350370
newKeyBlob.pub.size, newKeyBlob.priv.size);
351371

@@ -357,14 +377,13 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
357377
/* Store primary public key */
358378
XMEMCPY(&primaryBlob.pub, &primary->pub, sizeof(primaryBlob.pub));
359379
rc |= writeKeyBlob(pubFilename, &primaryBlob);
380+
360381
/* Write AK's Name digest */
361-
fp = XFOPEN(nameFile, "wb");
362-
if (fp != XBADFILE) {
363-
XFWRITE((BYTE*)&newKeyBlob.name, 1, sizeof(newKeyBlob.name), fp);
364-
printf("Wrote AK Name digest\n");
365-
XFCLOSE(fp);
366-
}
382+
rc |= writeBin(nameFile, (byte*)&newKeyBlob.handle.name,
383+
sizeof(newKeyBlob.handle.name));
384+
printf("Wrote AK Name digest\n");
367385
}
386+
if (rc != TPM_RC_SUCCESS) goto exit;
368387
#else
369388
if (alg == TPM_ALG_SYMCIPHER) {
370389
printf("The Public Part of a symmetric key contains only meta data\n");
@@ -421,10 +440,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
421440
/* Close handles */
422441
wolfTPM2_UnloadHandle(&dev, &primary->handle);
423442
wolfTPM2_UnloadHandle(&dev, &newKeyBlob.handle);
424-
/* EK policy is destroyed after use, flush parameter encryption session */
425-
if (paramEncAlg != TPM_ALG_NULL && !endorseKey) {
426-
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
427-
}
443+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
428444

429445
wolfTPM2_Cleanup(&dev);
430446
return rc;

0 commit comments

Comments
 (0)