Skip to content

Commit 61abbc1

Browse files
authored
Merge pull request #350 from dgarske/policyauth
Improvements in auth handling to support Policy Password and Policy Auth Value
2 parents c40f1e2 + 9a9ac2a commit 61abbc1

8 files changed

Lines changed: 514 additions & 116 deletions

File tree

examples/run_examples.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ if [ $WOLFCRYPT_ENABLE -eq 1 ]; then
163163

164164
./examples/keygen/keygen ecckeyblobeh.bin -ecc -eh >> run.out 2>&1
165165
RESULT=$?
166-
[ $RESULT -ne 0 ] && echo -e "keygen endorsement rsa failed! $RESULT" && exit 1
166+
[ $RESULT -ne 0 ] && echo -e "keygen endorsement ecc failed! $RESULT" && exit 1
167167
./examples/keygen/keyload ecckeyblobeh.bin -ecc -eh >> run.out 2>&1
168168
RESULT=$?
169-
[ $RESULT -ne 0 ] && echo -e "keygen endorsement rsa failed! $RESULT" && exit 1
169+
[ $RESULT -ne 0 ] && echo -e "keyload endorsement ecc failed! $RESULT" && exit 1
170170
fi
171171

172172

src/tpm2.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
108108
BYTE *param, *encParam = NULL;
109109
int paramSz, encParamSz = 0;
110110
int i, authPos;
111-
int tmpSz = 0; /* Used to calculate the new total size of the Auth Area */
111+
int authTotalSzPos = 0;
112112
#ifndef WOLFTPM2_NO_WOLFCRYPT
113113
UINT32 handleValue1, handleValue2, handleValue3;
114114
int handlePos;
@@ -120,8 +120,8 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
120120
/* Parse Auth */
121121
TPM2_Packet_ParseU32(packet, &authSz);
122122
packet->pos -= sizeof(authSz);
123-
/* Later Auth Area size is updated */
124-
TPM2_Packet_MarkU32(packet, &tmpSz);
123+
/* Get position for total auth size to be updated later */
124+
TPM2_Packet_MarkU32(packet, &authTotalSzPos);
125125
/* Mark the position of the Auth Area data */
126126
authPos = packet->pos;
127127
packet->pos += authSz;
@@ -174,17 +174,32 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
174174
}
175175
}
176176

177-
/* Note: Copy between TPM2_AUTH_SESSION and TPMS_AUTH_COMMAND is allowed */
178-
XMEMCPY(&authCmd, session, sizeof(TPMS_AUTH_COMMAND));
179-
180-
if (TPM2_IS_HMAC_SESSION(session->sessionHandle) ||
181-
TPM2_IS_POLICY_SESSION(session->sessionHandle))
177+
/* Build auth */
178+
XMEMSET(&authCmd, 0, sizeof(authCmd));
179+
authCmd.sessionHandle = session->sessionHandle;
180+
authCmd.sessionAttributes = session->sessionAttributes;
181+
authCmd.nonce.size = session->nonceCaller.size;
182+
XMEMCPY(authCmd.nonce.buffer, session->nonceCaller.buffer,
183+
authCmd.nonce.size);
184+
185+
/* Password Auth */
186+
if (session->sessionHandle == TPM_RS_PW) {
187+
authCmd.hmac.size = session->auth.size;
188+
XMEMCPY(authCmd.hmac.buffer, session->auth.buffer,
189+
session->auth.size);
190+
}
191+
/* HMAC or Policy Session */
192+
else if (TPM2_IS_HMAC_SESSION(session->sessionHandle) ||
193+
TPM2_IS_POLICY_SESSION(session->sessionHandle))
182194
{
183195
#ifndef WOLFTPM2_NO_WOLFCRYPT
184196
TPM2B_NAME name1, name2, name3;
185197
TPM2B_DIGEST hash;
186198
#endif
187199

200+
/* default is a HMAC output (using alg authHash) */
201+
authCmd.hmac.size = TPM2_GetHashDigestSize(session->authHash);
202+
188203
/* if param enc is not supported for this command then clear flag */
189204
/* session attribute flags are from TPM perspective */
190205
if ((info->flags & (CMD_FLAG_ENC2 | CMD_FLAG_ENC4)) == 0) {
@@ -240,16 +255,28 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
240255
#endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_HMAC */
241256
}
242257

243-
/* Replace auth in session */
258+
/* Place session auth */
244259
packet->pos = authPos;
245260
TPM2_Packet_AppendAuthCmd(packet, &authCmd);
246261
authPos = packet->pos; /* update auth position */
247262
}
248263

249-
/* Update the Auth Area size in the command packet */
250-
TPM2_Packet_PlaceU32(packet, tmpSz);
264+
/* Update the Auth Area total size in the command packet */
265+
i = TPM2_Packet_PlaceU32(packet, authTotalSzPos);
266+
267+
#ifdef DEBUG_WOLFTPM
268+
if ((int)authSz != i) {
269+
/* actual auth size did not match estimated size from
270+
* TPM2_Packet_AppendAuth */
271+
printf("Error: Calculated auth size %d did not match actual %d!\n",
272+
authSz, i);
273+
return BUFFER_E;
274+
}
275+
#endif
251276

252277
(void)cmdCode;
278+
(void)i;
279+
253280
return rc;
254281
}
255282

@@ -343,6 +370,11 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
343370
return TPM_RC_HMAC;
344371
}
345372
}
373+
374+
/* Save off last known HMAC */
375+
session->hmac.size = authRsp.hmac.size;
376+
XMEMCMP(session->hmac.buffer, authRsp.hmac.buffer,
377+
authRsp.hmac.size);
346378
#else
347379
(void)cmdCode;
348380
#endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_HMAC */

src/tpm2_packet.c

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,36 +233,34 @@ void TPM2_Packet_MarkU32(TPM2_Packet* packet, int* markSz)
233233
TPM2_Packet_AppendU32(packet, 0);
234234
}
235235
}
236-
void TPM2_Packet_PlaceU32(TPM2_Packet* packet, int markSz)
236+
int TPM2_Packet_PlaceU32(TPM2_Packet* packet, int markSz)
237237
{
238+
int actSz = 0;
238239
/* update with actual size */
239240
if (packet) {
240241
UINT32 data;
241242
byte* sizePtr = &packet->buf[markSz];
242243
markSz += sizeof(UINT32); /* skip marker */
243244
if (markSz <= packet->pos) {
244-
markSz = packet->pos - markSz;
245+
actSz = packet->pos - markSz;
245246

246-
data = cpu_to_be32(markSz);
247+
data = cpu_to_be32(actSz);
247248
XMEMCPY(sizePtr, &data, sizeof(UINT32));
248249
}
249250
}
251+
return actSz;
250252
}
251253

252254
void TPM2_Packet_AppendAuthCmd(TPM2_Packet* packet, TPMS_AUTH_COMMAND* authCmd)
253255
{
254-
if (packet == NULL || authCmd == NULL)
256+
if (packet == NULL || authCmd == NULL) {
255257
return;
258+
}
256259

257260
#ifdef WOLFTPM_DEBUG_VERBOSE
258261
TPM2_PrintAuth(authCmd);
259262
#endif
260263

261-
/* make sure continueSession is set for TPM_RS_PW */
262-
if (authCmd->sessionHandle == TPM_RS_PW &&
263-
(authCmd->sessionAttributes & TPMA_SESSION_continueSession) == 0) {
264-
authCmd->sessionAttributes |= TPMA_SESSION_continueSession;
265-
}
266264
TPM2_Packet_AppendU32(packet, authCmd->sessionHandle);
267265
TPM2_Packet_AppendU16(packet, authCmd->nonce.size);
268266
TPM2_Packet_AppendBytes(packet, authCmd->nonce.buffer, authCmd->nonce.size);
@@ -347,15 +345,53 @@ TPM_ST TPM2_Packet_AppendAuth(TPM2_Packet* packet, TPM2_CTX* ctx, CmdInfo_t* inf
347345

348346
info->authCnt = TPM2_GetCmdAuthCount(ctx, info);
349347
if (info->authCnt > 0) {
350-
int i, tmpSz = 0;
351-
TPM2_Packet_MarkU32(packet, &tmpSz);
348+
int i, authTotalSzPos = 0;
349+
TPM2_Packet_MarkU32(packet, &authTotalSzPos);
352350
for (i=0; i<info->authCnt; i++) {
353-
/* Note: Casting a TPM2_AUTH_SESSION to TPMS_AUTH_COMMAND here,
354-
* this is allowed because top of structure matches */
355-
TPM2_Packet_AppendAuthCmd(packet, (TPMS_AUTH_COMMAND*)&ctx->session[i]);
351+
TPM2_AUTH_SESSION* session = &ctx->session[i];
352+
353+
/* Determine auth size - appended later in TPM2_CommandProcess */
354+
355+
/* sessionHandle */
356+
packet->pos += sizeof(UINT32);
357+
358+
/* Nonce size:
359+
* Determined by us and TPM matches it on reply
360+
* Typically use SHA2-256 digest size (16 bytes). The random nonce
361+
* is populated in TPM2_CommandProcess */
362+
packet->pos += sizeof(UINT16); /* nonceSz */
363+
if (session->sessionHandle != TPM_RS_PW) {
364+
session->nonceCaller.size =
365+
TPM2_GetHashDigestSize(WOLFTPM2_WRAP_DIGEST);
366+
packet->pos += session->nonceCaller.size;
367+
}
368+
369+
/* sessionAttributes */
370+
packet->pos += sizeof(UINT8);
371+
if (session->sessionHandle == TPM_RS_PW) {
372+
/* make sure continueSession is set for TPM_RS_PW */
373+
session->sessionAttributes |= TPMA_SESSION_continueSession;
374+
}
375+
376+
/* Password Auth */
377+
packet->pos += sizeof(UINT16); /* hmac.size */
378+
if (session->sessionHandle == TPM_RS_PW) {
379+
packet->pos += session->auth.size;
380+
}
381+
/* HMAC or Policy Session */
382+
else if (TPM2_IS_HMAC_SESSION(session->sessionHandle) ||
383+
TPM2_IS_POLICY_SESSION(session->sessionHandle)) {
384+
if (!session->policyAuth && session->auth.size > 0) {
385+
packet->pos += session->auth.size;
386+
}
387+
else {
388+
/* auth is always HMAC result */
389+
packet->pos += TPM2_GetHashDigestSize(session->authHash);
390+
}
391+
}
356392
}
357393
/* based on position difference places calculated size at marked U32 above */
358-
TPM2_Packet_PlaceU32(packet, tmpSz);
394+
(void)TPM2_Packet_PlaceU32(packet, authTotalSzPos);
359395
st = TPM_ST_SESSIONS;
360396
}
361397
return st;

src/tpm2_param_enc.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,42 @@ int TPM2_CalcCpHash(TPMI_ALG_HASH authHash, TPM_CC cmdCode,
383383
/* Hash Command Code */
384384
UINT32 ccSwap = TPM2_Packet_SwapU32(cmdCode);
385385
rc = wc_HashUpdate(&hash_ctx, hashType, (byte*)&ccSwap, sizeof(ccSwap));
386+
#ifdef WOLFTPM_DEBUG_VERBOSE
387+
printf("cpHash: cmdcode size %d\n", (int)sizeof(TPM_CC));
388+
TPM2_PrintBin((unsigned char*)&cmdCode, sizeof(TPM_CC));
389+
#endif
386390

387391
/* For Command's only hash each session name */
388-
if (rc == 0 && name1 && name1->size > 0)
392+
if (rc == 0 && name1 && name1->size > 0) {
393+
#ifdef WOLFTPM_DEBUG_VERBOSE
394+
printf("Name 0: %d\n", name1->size);
395+
TPM2_PrintBin(name1->name, name1->size);
396+
#endif
389397
rc = wc_HashUpdate(&hash_ctx, hashType, name1->name, name1->size);
390-
if (rc == 0 && name2 && name2->size > 0)
398+
}
399+
if (rc == 0 && name2 && name2->size > 0) {
400+
#ifdef WOLFTPM_DEBUG_VERBOSE
401+
printf("Name 1: %d\n", name2->size);
402+
TPM2_PrintBin(name2->name, name2->size);
403+
#endif
391404
rc = wc_HashUpdate(&hash_ctx, hashType, name2->name, name2->size);
392-
if (rc == 0 && name3 && name3->size > 0)
405+
}
406+
if (rc == 0 && name3 && name3->size > 0) {
407+
#ifdef WOLFTPM_DEBUG_VERBOSE
408+
printf("Name 2: %d\n", name3->size);
409+
TPM2_PrintBin(name3->name, name3->size);
410+
#endif
393411
rc = wc_HashUpdate(&hash_ctx, hashType, name3->name, name3->size);
412+
}
394413

395414
/* Hash Remainder of parameters - after handles and auth */
396-
if (rc == 0)
415+
if (rc == 0) {
416+
#ifdef WOLFTPM_DEBUG_VERBOSE
417+
printf("cpHash: params size %d\n", paramSz);
418+
TPM2_PrintBin(param, paramSz);
419+
#endif
397420
rc = wc_HashUpdate(&hash_ctx, hashType, param, paramSz);
421+
}
398422

399423
if (rc == 0)
400424
rc = wc_HashFinal(&hash_ctx, hashType, hash->buffer);

0 commit comments

Comments
 (0)