@@ -771,19 +771,13 @@ int ClientUseCert(const char* certName, void* heap)
771771 #define WOLFSSH_TPM_KEY_AUTH "ThisIsMyKeyAuth"
772772#endif
773773
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 ;
780774static const char gKeyAuth [] = WOLFSSH_TPM_KEY_AUTH ;
775+ static const char gStorageKeyAuth [] = WOLFSSH_TPM_SRK_AUTH ;
781776
782777#define TPM2_DEMO_STORAGE_KEY_HANDLE WOLFSSH_TPM_SRK_HANDLE
783778
784779static int getPrimaryStoragekey (WOLFTPM2_DEV * pDev ,
785- WOLFTPM2_KEY * pStorageKey ,
786- TPM_ALG_ID alg )
780+ WOLFTPM2_KEY * pStorageKey , TPM_ALG_ID alg )
787781{
788782 int rc ;
789783
@@ -820,6 +814,37 @@ static int getPrimaryStoragekey(WOLFTPM2_DEV* pDev,
820814 return rc ;
821815}
822816
817+ /* move to wolfTPM */
818+ static int getPrimaryEndorsementKey (WOLFTPM2_DEV * pDev ,
819+ WOLFTPM2_KEY * pEndorseKey , TPM_ALG_ID alg )
820+ {
821+ int rc ;
822+ WOLFTPM2_SESSION tpmSession ;
823+
824+ WLOG (WS_LOG_DEBUG , "Entering getPrimaryEndorsementKey()" );
825+
826+ /* Create endorsement key (EK) */
827+ rc = wolfTPM2_CreateEK (pDev , pEndorseKey , alg );
828+ if (rc != 0 ) {
829+ WLOG (WS_LOG_DEBUG , "Creating EK failed, rc: %d" , rc );
830+ return rc ;
831+ }
832+
833+ /* EK requires Policy auth, not Password */
834+ pEndorseKey -> handle .policyAuth = 1 ;
835+
836+ /* Create and set policy session */
837+ rc = wolfTPM2_CreateAuthSession_EkPolicy (pDev , & tpmSession );
838+ if (rc != 0 ) {
839+ WLOG (WS_LOG_DEBUG , "Creating EK policy session failed, rc: %d" , rc );
840+ return rc ;
841+ }
842+
843+ rc = wolfTPM2_SetAuthSession (pDev , 0 , & tpmSession , 0 );
844+ WLOG (WS_LOG_DEBUG , "Leaving getPrimaryEndorsementKey(), rc = %d" , rc );
845+ return rc ;
846+ }
847+
823848static int readKeyBlob (const char * filename , WOLFTPM2_KEYBLOB * key )
824849{
825850 int rc = 0 ;
@@ -904,81 +929,65 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
904929 return rc ;
905930}
906931
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-
936932static int wolfSSH_TPM_InitKey (WOLFTPM2_DEV * dev , const char * name ,
937- WOLFTPM2_KEY * pTpmKey )
933+ WOLFTPM2_KEY * pTpmKey , int useEndorsementKey )
938934{
939935 int rc = 0 ;
940- #if WOLFSSH_TPM_ENDORSEMENT_KEY
941936 WOLFTPM2_KEY endorse ;
942- #else
943937 WOLFTPM2_KEY storage ;
944- #endif
938+ WOLFTPM2_KEY * primary = NULL ;
945939 WOLFTPM2_KEYBLOB tpmKeyBlob ;
946940 byte * p = NULL ;
947941
948942 WLOG (WS_LOG_DEBUG , "Entering wolfSSH_TPM_InitKey()" );
949943
944+ /* Initialize structures */
945+ XMEMSET (& endorse , 0 , sizeof (endorse ));
946+ XMEMSET (& storage , 0 , sizeof (storage ));
947+ XMEMSET (& tpmKeyBlob , 0 , sizeof (tpmKeyBlob ));
948+
950949 /* Initialize the TPM 2.0 device */
951- if ( rc == 0 ) {
952- rc = wolfTPM2_Init ( dev , TPM2_IoCb , NULL );
953- if ( rc != 0 ) {
954- WLOG ( WS_LOG_DEBUG , "TPM 2.0 Device initialization failed, rc: %d" , rc );
955- }
950+ rc = wolfTPM2_Init ( dev , TPM2_IoCb , NULL );
951+ if ( rc != 0 ) {
952+ WLOG ( WS_LOG_DEBUG ,
953+ "TPM 2.0 Device initialization failed, rc: %d" , rc );
954+ return rc ;
956955 }
957956
958- /* TPM 2.0 keys live under a Primary Key, acquire such key */
957+ /* Get primary key based on type */
959958 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
966- rc = getPrimaryStoragekey (dev , & storage , TPM_ALG_RSA );
967- if (rc != 0 ) {
968- WLOG (WS_LOG_DEBUG , "Acquiring Primary Storage Key failed, rc: %d" , rc );
959+ if (useEndorsementKey == 1 ) {
960+ rc = getPrimaryEndorsementKey (dev , & endorse , TPM_ALG_RSA );
961+ if (rc == 0 ) {
962+ primary = & endorse ;
963+ WLOG (WS_LOG_DEBUG , "Using Endorsement Key" );
964+ } else {
965+ WLOG (WS_LOG_DEBUG ,
966+ "Getting Primary Endorsement Key failed, rc: %d" , rc );
967+ }
968+ } else {
969+ rc = getPrimaryStoragekey (dev , & storage , TPM_ALG_RSA );
970+ if (rc == 0 ) {
971+ wolfTPM2_SetAuthHandle (dev , 0 , & storage .handle );
972+ primary = & storage ;
973+ WLOG (WS_LOG_DEBUG , "Using Storage Key" );
974+ } else {
975+ WLOG (WS_LOG_DEBUG ,
976+ "Getting Primary Storage Key failed, rc: %d" , rc );
977+ }
969978 }
970- #endif
971979 }
972980
973981 /* Load the TPM 2.0 key blob from disk */
974982 if (rc == 0 ) {
975983 rc = readKeyBlob (name , & tpmKeyBlob );
976984 if (rc != 0 ) {
977- WLOG (WS_LOG_DEBUG , "Reading key blob from disk failed, rc: %d" , rc );
985+ WLOG (WS_LOG_DEBUG ,
986+ "Reading key blob from disk failed, rc: %d" , rc );
978987 }
979988 }
980989
981- /* set session for authorization key */
990+ /* Set auth for key */
982991 if (rc == 0 ) {
983992 tpmKeyBlob .handle .auth .size = (int )sizeof (gKeyAuth )- 1 ;
984993 XMEMCPY (tpmKeyBlob .handle .auth .buffer , gKeyAuth ,
@@ -987,15 +996,13 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
987996
988997 /* Load the public key into the TPM device */
989998 if (rc == 0 ) {
990- #if WOLFSSH_TPM_ENDORSEMENT_KEY
991- rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & endorse .handle );
992- #else
993- rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & storage .handle );
994- #endif
999+ rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & primary -> handle );
9951000 if (rc != 0 ) {
9961001 WLOG (WS_LOG_DEBUG , "wolfTPM2_LoadKey failed, rc: %d" , rc );
1002+ } else {
1003+ WLOG (WS_LOG_DEBUG , "Loaded key to 0x%x\n" ,
1004+ (word32 )tpmKeyBlob .handle .hndl );
9971005 }
998- WLOG (WS_LOG_DEBUG , "Loaded key to 0x%x\n" , (word32 )tpmKeyBlob .handle .hndl );
9991006 }
10001007
10011008 /* Read the public key and extract the public key as a DER/ASN.1 */
@@ -1013,24 +1020,25 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
10131020 rc = wolfSSH_ReadPublicKey_buffer (userPublicKey , userPublicKeySz ,
10141021 WOLFSSH_FORMAT_ASN1 , & p , & userPublicKeySz , & userPublicKeyType ,
10151022 & userPublicKeyTypeSz , NULL );
1016- if (rc != 0 ) {
1023+ if (rc == 0 ) {
1024+ userPublicKey = p ;
1025+ } else {
10171026 WLOG (WS_LOG_DEBUG , "Reading public key failed, rc: %d" , rc );
10181027 }
1019- userPublicKey = p ;
10201028 }
10211029
1022- /* Unload primary key handle */
1030+ /* Copy key info */
10231031 if (rc == 0 ) {
10241032 XMEMCPY (& pTpmKey -> handle , & tpmKeyBlob .handle , sizeof (pTpmKey -> handle ));
10251033 XMEMCPY (& pTpmKey -> pub , & tpmKeyBlob .pub , sizeof (pTpmKey -> pub ));
1026- #if WOLFSSH_TPM_ENDORSEMENT_KEY
1027- wolfTPM2_UnloadHandle (dev , & endorse .handle );
1028- #else
1029- wolfTPM2_UnloadHandle (dev , & storage .handle );
1030- #endif
10311034 }
10321035
1033- WLOG (WS_LOG_DEBUG , "Leaving wolfSSH_TPM_InitKey()" );
1036+ /* Cleanup */
1037+ if (primary != NULL ) {
1038+ wolfTPM2_UnloadHandle (dev , & primary -> handle );
1039+ }
1040+
1041+ WLOG (WS_LOG_DEBUG , "Leaving wolfSSH_TPM_InitKey(), rc = %d" , rc );
10341042 return rc ;
10351043}
10361044
@@ -1063,7 +1071,8 @@ int CLientSetTpm(WOLFSSH* ssh)
10631071
10641072/* Reads the private key to use from file name privKeyName.
10651073 * returns 0 on success */
1066- int ClientSetPrivateKey (const char * privKeyName , int userEcc , void * heap )
1074+ int ClientSetPrivateKey (const char * privKeyName , int userEcc ,
1075+ void * heap , int useEndorsementKey )
10671076{
10681077 int ret = 0 ;
10691078
@@ -1099,7 +1108,8 @@ int ClientSetPrivateKey(const char* privKeyName, int userEcc, void* heap)
10991108 */
11001109 WMEMSET (& tpmDev , 0 , sizeof (tpmDev ));
11011110 WMEMSET (& tpmKey , 0 , sizeof (tpmKey ));
1102- ret = wolfSSH_TPM_InitKey (& tpmDev , privKeyName , & tpmKey );
1111+ ret = wolfSSH_TPM_InitKey (& tpmDev , privKeyName , & tpmKey ,
1112+ useEndorsementKey );
11031113 #elif !defined(NO_FILESYSTEM )
11041114 userPrivateKey = NULL ; /* create new buffer based on parsed input */
11051115 userPrivateKeyAlloc = 1 ;
0 commit comments