Skip to content

Commit 5f5bc63

Browse files
committed
First support for ek and srk with macro gaurd
1 parent 43b557f commit 5f5bc63

1 file changed

Lines changed: 74 additions & 9 deletions

File tree

examples/client/common.c

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,30 @@ int ClientUseCert(const char* certName, void* heap)
756756

757757
#ifdef WOLFSSH_TPM
758758

759-
#define TPM2_DEMO_STORAGE_KEY_HANDLE 0x81000200 /* Persistent Storage Key Handle (RSA) */
759+
/* Persistent Storage Key Handle (RSA) */
760+
#ifndef WOLFSSH_TPM_SRK_HANDLE
761+
#define WOLFSSH_TPM_SRK_HANDLE 0x81000200
762+
#endif
760763

761-
static const char gStorageKeyAuth[] = "ThisIsMyStorageKeyAuth";
764+
/* Storage Key Authentication Password */
765+
#ifndef WOLFSSH_TPM_SRK_AUTH
766+
#define WOLFSSH_TPM_SRK_AUTH "ThisIsMyStorageKeyAuth"
767+
#endif
768+
769+
/* Key Authentication Password */
770+
#ifndef WOLFSSH_TPM_KEY_AUTH
771+
#define WOLFSSH_TPM_KEY_AUTH "ThisIsMyKeyAuth"
772+
#endif
773+
774+
/* Enable use of endorsement key instead of storage key */
775+
#ifndef WOLFSSH_TPM_ENDORSEMENT_KEY
776+
#define WOLFSSH_TPM_ENDORSEMENT_KEY 1
777+
#endif
778+
779+
static const char gStorageKeyAuth[] = WOLFSSH_TPM_SRK_AUTH;
780+
static const char gKeyAuth[] = WOLFSSH_TPM_KEY_AUTH;
781+
782+
#define TPM2_DEMO_STORAGE_KEY_HANDLE WOLFSSH_TPM_SRK_HANDLE
762783

763784
static int getPrimaryStoragekey(WOLFTPM2_DEV* pDev,
764785
WOLFTPM2_KEY* pStorageKey,
@@ -799,7 +820,6 @@ static int getPrimaryStoragekey(WOLFTPM2_DEV* pDev,
799820
return rc;
800821
}
801822

802-
803823
static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
804824
{
805825
int rc = 0;
@@ -884,20 +904,50 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
884904
return rc;
885905
}
886906

907+
static int getPrimaryEndorsementKey(WOLFTPM2_DEV* pDev, WOLFTPM2_KEY* pEndorseKey, TPM_ALG_ID alg)
908+
{
909+
int rc;
910+
WOLFTPM2_SESSION tpmSession;
911+
912+
WLOG(WS_LOG_DEBUG, "Entering getPrimaryEndorsementKey()");
913+
914+
/* Create endorsement key (EK) */
915+
rc = wolfTPM2_CreateEK(pDev, pEndorseKey, alg);
916+
if (rc != 0) {
917+
WLOG(WS_LOG_DEBUG, "Creating EK failed, rc: %d", rc);
918+
return rc;
919+
}
920+
921+
/* EK requires Policy auth, not Password */
922+
pEndorseKey->handle.policyAuth = 1;
923+
924+
/* Create and set policy session */
925+
rc = wolfTPM2_CreateAuthSession_EkPolicy(pDev, &tpmSession);
926+
if (rc != 0) {
927+
WLOG(WS_LOG_DEBUG, "Creating EK policy session failed, rc: %d", rc);
928+
return rc;
929+
}
930+
931+
rc = wolfTPM2_SetAuthSession(pDev, 0, &tpmSession, 0);
932+
WLOG(WS_LOG_DEBUG, "Leaving getPrimaryEndorsementKey(), rc = %d", rc);
933+
return rc;
934+
}
935+
887936
static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
888937
WOLFTPM2_KEY* pTpmKey)
889938
{
890939
int rc = 0;
940+
#if WOLFSSH_TPM_ENDORSEMENT_KEY
941+
WOLFTPM2_KEY endorse;
942+
#else
891943
WOLFTPM2_KEY storage;
944+
#endif
892945
WOLFTPM2_KEYBLOB tpmKeyBlob;
893946
byte* p = NULL;
894-
/* TODO: workaround until password can be supplied */
895-
/* consider a refactor to take a 32-bit handle and key auth password */
896-
static const char gKeyAuth[] = "ThisIsMyKeyAuth";
897947

898948
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_TPM_InitKey()");
899949

900-
/* Initilize the TPM 2.0 device */
950+
/* Initialize the TPM 2.0 device */
901951
if (rc == 0) {
902952
rc = wolfTPM2_Init(dev, TPM2_IoCb, NULL);
903953
if (rc != 0) {
@@ -907,10 +957,17 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
907957

908958
/* TPM 2.0 keys live under a Primary Key, acquire such key */
909959
if (rc == 0) {
960+
#if WOLFSSH_TPM_ENDORSEMENT_KEY
961+
rc = getPrimaryEndorsementKey(dev, &endorse, TPM_ALG_RSA);
962+
if (rc != 0) {
963+
WLOG(WS_LOG_DEBUG, "Acquiring Primary Endorsement Key failed, rc: %d", rc);
964+
}
965+
#else
910966
rc = getPrimaryStoragekey(dev, &storage, TPM_ALG_RSA);
911967
if (rc != 0) {
912-
WLOG(WS_LOG_DEBUG, "Acquiring a Primary TPM 2.0 Key failed, rc: %d", rc);
968+
WLOG(WS_LOG_DEBUG, "Acquiring Primary Storage Key failed, rc: %d", rc);
913969
}
970+
#endif
914971
}
915972

916973
/* Load the TPM 2.0 key blob from disk */
@@ -930,7 +987,11 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
930987

931988
/* Load the public key into the TPM device */
932989
if (rc == 0) {
990+
#if WOLFSSH_TPM_ENDORSEMENT_KEY
991+
rc = wolfTPM2_LoadKey(dev, &tpmKeyBlob, &endorse.handle);
992+
#else
933993
rc = wolfTPM2_LoadKey(dev, &tpmKeyBlob, &storage.handle);
994+
#endif
934995
if (rc != 0) {
935996
WLOG(WS_LOG_DEBUG, "wolfTPM2_LoadKey failed, rc: %d", rc);
936997
}
@@ -958,11 +1019,15 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
9581019
userPublicKey = p;
9591020
}
9601021

961-
/* Unload SRK storage handle */
1022+
/* Unload primary key handle */
9621023
if (rc == 0) {
9631024
XMEMCPY(&pTpmKey->handle, &tpmKeyBlob.handle, sizeof(pTpmKey->handle));
9641025
XMEMCPY(&pTpmKey->pub, &tpmKeyBlob.pub, sizeof(pTpmKey->pub));
1026+
#if WOLFSSH_TPM_ENDORSEMENT_KEY
1027+
wolfTPM2_UnloadHandle(dev, &endorse.handle);
1028+
#else
9651029
wolfTPM2_UnloadHandle(dev, &storage.handle);
1030+
#endif
9661031
}
9671032

9681033
WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_TPM_InitKey()");

0 commit comments

Comments
 (0)