Skip to content

Commit 16296e4

Browse files
authored
Merge pull request #403 from dgarske/sealing_fixes
Fixes for sealing/unsealing
2 parents c6ac284 + 30620bf commit 16296e4

4 files changed

Lines changed: 78 additions & 22 deletions

File tree

docs/TPM.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ In wolfBoot we support TPM based root of trust, sealing/unsealing, cryptographic
1515
| `MEASURED_PCR_A=16` | `WOLFBOOT_MEASURED_PCR_A=16` | The PCR index to use. See [docs/measured_boot.md](/docs/measured_boot.md). |
1616
| `WOLFBOOT_TPM_SEAL=1` | `WOLFBOOT_TPM_SEAL` | Enables support for sealing/unsealing based on PCR policy signed externally. |
1717
| `WOLFBOOT_TPM_SEAL_NV_BASE=0x01400300` | `WOLFBOOT_TPM_SEAL_NV_BASE` | To override the default sealed blob storage location in the platform hierarchy. |
18-
| `WOLFBOOT_TPM_SEAL_AUTH=secret` | `WOLFBOOT_TPM_SEAL_AUTH` | Password for sealing/unsealing secrets |
18+
| `WOLFBOOT_TPM_SEAL_AUTH=secret` | `WOLFBOOT_TPM_SEAL_AUTH` | Password for sealing/unsealing secrets, if omitted the PCR policy will be used |
1919

2020
## Root of Trust (ROT)
2121

include/tpm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ extern WOLFTPM2_KEY wolftpm_srk;
5252
int wolfBoot_tpm2_init(void);
5353
void wolfBoot_tpm2_deinit(void);
5454

55+
int wolfBoot_tpm2_clear(void);
56+
5557
#if defined(WOLFBOOT_TPM_VERIFY) || defined(WOLFBOOT_TPM_SEAL)
5658
int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey,
5759
TPM_ALG_ID* pAlg);
@@ -83,6 +85,7 @@ int wolfBoot_unseal_auth(const uint8_t* pubkey_hint, const uint8_t* policy, uint
8385
int wolfBoot_unseal_blob(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz,
8486
WOLFTPM2_KEYBLOB* seal_blob, uint8_t* secret, int* secret_sz, const uint8_t* auth, int authSz);
8587

88+
int wolfBoot_delete_seal(int index);
8689
int wolfBoot_read_blob(uint32_t nvIndex, WOLFTPM2_KEYBLOB* blob,
8790
const uint8_t* auth, uint32_t authSz);
8891
int wolfBoot_store_blob(TPMI_RH_NV_AUTH authHandle, uint32_t nvIndex,

src/tpm.c

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,12 @@ int wolfBoot_store_blob(TPMI_RH_NV_AUTH authHandle, uint32_t nvIndex,
591591
memset(&nv, 0, sizeof(nv));
592592

593593
nv.handle.hndl = nvIndex;
594-
nv.handle.auth.size = authSz;
595-
memcpy(nv.handle.auth.buffer, auth, authSz);
594+
if (authSz > 0) {
595+
if (auth == NULL)
596+
return BAD_FUNC_ARG;
597+
nv.handle.auth.size = authSz;
598+
memcpy(nv.handle.auth.buffer, auth, authSz);
599+
}
596600

597601
parent.hndl = authHandle;
598602

@@ -667,8 +671,12 @@ int wolfBoot_read_blob(uint32_t nvIndex, WOLFTPM2_KEYBLOB* blob,
667671
memset(&nv, 0, sizeof(nv));
668672

669673
nv.handle.hndl = nvIndex;
670-
nv.handle.auth.size = authSz;
671-
memcpy(nv.handle.auth.buffer, auth, authSz);
674+
if (authSz > 0) {
675+
if (auth == NULL)
676+
return BAD_FUNC_ARG;
677+
nv.handle.auth.size = authSz;
678+
memcpy(nv.handle.auth.buffer, auth, authSz);
679+
}
672680
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &nv.handle);
673681

674682
pos = 0;
@@ -722,8 +730,12 @@ int wolfBoot_delete_blob(TPMI_RH_NV_AUTH authHandle, uint32_t nvIndex,
722730
memset(&nv, 0, sizeof(nv));
723731

724732
nv.handle.hndl = nvIndex;
725-
nv.handle.auth.size = authSz;
726-
memcpy(nv.handle.auth.buffer, auth, authSz);
733+
if (authSz > 0) {
734+
if (auth == NULL)
735+
return BAD_FUNC_ARG;
736+
nv.handle.auth.size = authSz;
737+
memcpy(nv.handle.auth.buffer, auth, authSz);
738+
}
727739

728740
parent.hndl = authHandle;
729741

@@ -788,6 +800,8 @@ int wolfBoot_seal_blob(const uint8_t* pubkey_hint,
788800
/* build authorization policy based on public key */
789801
/* digest here is input and output, must be zero'd */
790802
uint32_t digestSz = TPM2_GetHashDigestSize(pcrAlg);
803+
/* Create a new key for sealing using external signing auth */
804+
wolfTPM2_GetKeyTemplate_KeySeal(&template, pcrAlg);
791805
memset(template.authPolicy.buffer, 0, digestSz);
792806
rc = wolfTPM2_PolicyAuthorizeMake(pcrAlg, &authKey.pub,
793807
template.authPolicy.buffer, &digestSz, NULL, 0);
@@ -800,8 +814,15 @@ int wolfBoot_seal_blob(const uint8_t* pubkey_hint,
800814
wolfBoot_print_hexstr(template.authPolicy.buffer,
801815
template.authPolicy.size, 0);
802816
#endif
803-
/* Create a new key for sealing using external signing auth */
804-
wolfTPM2_GetKeyTemplate_KeySeal(&template, pcrAlg);
817+
818+
if (auth != NULL && authSz > 0) {
819+
/* allow password based sealing */
820+
template.objectAttributes |= TPMA_OBJECT_userWithAuth;
821+
}
822+
else {
823+
/* disable password based sealing, require policy */
824+
template.objectAttributes &= ~TPMA_OBJECT_userWithAuth;
825+
}
805826
rc = wolfTPM2_CreateKeySeal_ex(&wolftpm_dev, seal_blob,
806827
&wolftpm_srk.handle, &template, auth, authSz,
807828
pcrAlg, NULL, 0, secret, secret_sz);
@@ -813,6 +834,12 @@ int wolfBoot_seal_blob(const uint8_t* pubkey_hint,
813834
return rc;
814835
}
815836

837+
int wolfBoot_delete_seal(int index)
838+
{
839+
return wolfBoot_delete_blob(TPM_RH_PLATFORM,
840+
WOLFBOOT_TPM_SEAL_NV_BASE + index, NULL, 0);
841+
}
842+
816843
/* Index (0-X) determines location in NV from WOLFBOOT_TPM_SEAL_NV_BASE to
817844
* store sealed blob */
818845
int wolfBoot_seal_auth(const uint8_t* pubkey_hint,
@@ -982,15 +1009,15 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
9821009
rc = wolfTPM2_PolicyAuthorize(&wolftpm_dev, policy_session.handle.hndl,
9831010
&authKey.pub, &checkTicket, pcrDigest, pcrDigestSz,
9841011
policyRef, policyRefSz);
985-
}
986-
else {
987-
/* A failure here means the signed policy did not match expected policy.
988-
* Use this PCR mask and policy digest with the sign tool --policy=
989-
* argument to sign */
990-
wolfBoot_printf("Policy signature failed!\n");
991-
wolfBoot_printf("Expected PCR Mask (0x%08x) and PCR Policy (%d)\n",
992-
pcrMask, policyDigestSz);
993-
wolfBoot_print_hexstr(policyDigest, policyDigestSz, 0);
1012+
if (rc != 0) {
1013+
/* A failure here means the signed policy did not match expected
1014+
* policy. Use this PCR mask and policy digest with the sign tool
1015+
* --policy= argument to sign */
1016+
wolfBoot_printf("Policy signature failed!\n");
1017+
wolfBoot_printf("Expected PCR Mask (0x%08x) and PCR Policy (%d)\n",
1018+
pcrMask, policyDigestSz);
1019+
wolfBoot_print_hexstr(policyDigest, policyDigestSz, 0);
1020+
}
9941021
}
9951022

9961023
/* done with authorization public key */
@@ -1005,9 +1032,21 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
10051032
wolfBoot_printf("Loaded seal blob to 0x%x\n",
10061033
(uint32_t)seal_blob->handle.hndl);
10071034
#endif
1008-
seal_blob->handle.auth.size = authSz;
1009-
memcpy(seal_blob->handle.auth.buffer, auth, authSz);
1010-
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &seal_blob->handle);
1035+
1036+
/* if using password auth, set it otherwise use policy auth */
1037+
if (auth != NULL && authSz > 0) {
1038+
seal_blob->handle.auth.size = authSz;
1039+
memcpy(seal_blob->handle.auth.buffer, auth, authSz);
1040+
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &seal_blob->handle);
1041+
}
1042+
else {
1043+
/* use the policy session for unseal */
1044+
rc = wolfTPM2_SetAuthSession(&wolftpm_dev, 0, &policy_session,
1045+
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt |
1046+
TPMA_SESSION_continueSession));
1047+
/* set the sealed object name 0 (required) */
1048+
wolfTPM2_SetAuthHandleName(&wolftpm_dev, 0, &seal_blob->handle);
1049+
}
10111050

10121051
/* unseal */
10131052
unsealIn.itemHandle = seal_blob->handle.hndl;
@@ -1216,6 +1255,20 @@ void wolfBoot_tpm2_deinit(void)
12161255
wolfTPM2_Cleanup(&wolftpm_dev);
12171256
}
12181257

1258+
/**
1259+
* @brief Clear the content of the TPM2 device.
1260+
*
1261+
* This function clears the TPM2 device and remove all stored secrets.
1262+
*
1263+
* @return BAD_FUNC_ARG if any of the underlying functions has passed an invalid
1264+
* argument, e.g. wolftpm_dev is not initialized
1265+
* @return TPM_RC_SUCCESS in case of success
1266+
*/
1267+
int wolfBoot_tpm2_clear(void)
1268+
{
1269+
return wolfTPM2_Clear(&wolftpm_dev);
1270+
}
1271+
12191272

12201273
#ifdef WOLFBOOT_TPM_KEYSTORE
12211274
/* check root of trust based on key_slot (index) and public key hint */

0 commit comments

Comments
 (0)