@@ -756,94 +756,12 @@ int ClientUseCert(const char* certName, void* heap)
756756
757757#ifdef WOLFSSH_TPM
758758
759- /* Persistent Storage Key Handle (RSA) */
760- #ifndef WOLFSSH_TPM_SRK_HANDLE
761- #define WOLFSSH_TPM_SRK_HANDLE 0x81000200
762- #endif
763-
764- /* Storage Key Authentication Password */
765- #ifndef WOLFSSH_TPM_SRK_AUTH
766- #define WOLFSSH_TPM_SRK_AUTH "ThisIsMyStorageKeyAuth"
767- #endif
768-
769759/* Key Authentication Password */
770760#ifndef WOLFSSH_TPM_KEY_AUTH
771761 #define WOLFSSH_TPM_KEY_AUTH "ThisIsMyKeyAuth"
772762#endif
773763
774764static const char gKeyAuth [] = WOLFSSH_TPM_KEY_AUTH ;
775- static const char gStorageKeyAuth [] = WOLFSSH_TPM_SRK_AUTH ;
776-
777- #define TPM2_DEMO_STORAGE_KEY_HANDLE WOLFSSH_TPM_SRK_HANDLE
778-
779- static int getPrimaryStoragekey (WOLFTPM2_DEV * pDev ,
780- WOLFTPM2_KEY * pStorageKey , TPM_ALG_ID alg )
781- {
782- int rc ;
783-
784- WLOG (WS_LOG_DEBUG , "Entering getPrimaryStoragekey()" );
785-
786- /* See if SRK already exists */
787- rc = wolfTPM2_ReadPublicKey (pDev , pStorageKey , TPM2_DEMO_STORAGE_KEY_HANDLE );
788- if (rc != 0 ) {
789- /* Create primary storage key */
790- rc = wolfTPM2_CreateSRK (pDev , pStorageKey , alg ,
791- (byte * )gStorageKeyAuth , sizeof (gStorageKeyAuth )- 1 );
792- #ifndef WOLFTPM_WINAPI
793- if (rc == 0 ) {
794- /* Move storage key into persistent NV */
795- rc = wolfTPM2_NVStoreKey (pDev , TPM_RH_OWNER , pStorageKey ,
796- TPM2_DEMO_STORAGE_KEY_HANDLE );
797- }
798- #endif
799- }
800- else {
801- /* specify auth password for storage key */
802- pStorageKey -> handle .auth .size = sizeof (gStorageKeyAuth )- 1 ;
803- XMEMCPY (pStorageKey -> handle .auth .buffer , gStorageKeyAuth ,
804- pStorageKey -> handle .auth .size );
805- }
806- if (rc != 0 ) {
807- printf ("Loading SRK: Storage failed 0x%x: %s\n" , rc ,
808- TPM2_GetRCString (rc ));
809- return rc ;
810- }
811- printf ("Loading SRK: Storage 0x%x (%d bytes)\n" ,
812- (word32 )pStorageKey -> handle .hndl , pStorageKey -> pub .size );
813- WLOG (WS_LOG_DEBUG , "Leaving getPrimaryStoragekey(), rc = %d" , rc );
814- return rc ;
815- }
816-
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- }
847765
848766static int readKeyBlob (const char * filename , WOLFTPM2_KEYBLOB * key )
849767{
@@ -930,51 +848,45 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
930848}
931849
932850static int wolfSSH_TPM_InitKey (WOLFTPM2_DEV * dev , const char * name ,
933- WOLFTPM2_KEY * pTpmKey , int useEndorsementKey )
851+ WOLFTPM2_KEY * pTpmKey )
934852{
935853 int rc = 0 ;
936854 WOLFTPM2_KEY endorse ;
937- WOLFTPM2_KEY storage ;
938- WOLFTPM2_KEY * primary = NULL ;
939855 WOLFTPM2_KEYBLOB tpmKeyBlob ;
856+ WOLFTPM2_SESSION tpmSession ;
940857 byte * p = NULL ;
941858
942859 WLOG (WS_LOG_DEBUG , "Entering wolfSSH_TPM_InitKey()" );
943860
944- /* Initialize structures */
945- XMEMSET (& endorse , 0 , sizeof (endorse ));
946- XMEMSET (& storage , 0 , sizeof (storage ));
947- XMEMSET (& tpmKeyBlob , 0 , sizeof (tpmKeyBlob ));
948-
949861 /* Initialize the TPM 2.0 device */
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 ;
862+ if ( rc == 0 ) {
863+ rc = wolfTPM2_Init ( dev , TPM2_IoCb , NULL );
864+ if ( rc != 0 ) {
865+ WLOG ( WS_LOG_DEBUG , "TPM 2.0 Device initialization failed, rc: %d" , rc );
866+ }
955867 }
956868
957- /* Get primary key based on type */
869+ /* Create primary endorsement key (EK) */
958870 if (rc == 0 ) {
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- }
871+ rc = wolfTPM2_CreateEK ( dev , & endorse , TPM_ALG_RSA );
872+ if ( rc != 0 ) {
873+ WLOG ( WS_LOG_DEBUG , "Creating EK failed, rc: %d" , rc );
874+ }
875+ }
876+
877+ /* Create and set policy session for EK */
878+ if ( rc == 0 ) {
879+ endorse . handle . policyAuth = 1 ;
880+ rc = wolfTPM2_CreateAuthSession_EkPolicy ( dev , & tpmSession );
881+ if ( rc != 0 ) {
882+ WLOG ( WS_LOG_DEBUG , "Creating EK policy session failed, rc: %d" , rc );
883+ }
884+ }
885+
886+ if ( rc == 0 ) {
887+ rc = wolfTPM2_SetAuthSession ( dev , 0 , & tpmSession , 0 );
888+ if ( rc != 0 ) {
889+ WLOG ( WS_LOG_DEBUG , "Setting auth session failed, rc: %d" , rc );
978890 }
979891 }
980892
@@ -996,7 +908,7 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
996908
997909 /* Load the public key into the TPM device */
998910 if (rc == 0 ) {
999- rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & primary -> handle );
911+ rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & endorse . handle );
1000912 if (rc != 0 ) {
1001913 WLOG (WS_LOG_DEBUG , "wolfTPM2_LoadKey failed, rc: %d" , rc );
1002914 } else {
@@ -1031,6 +943,7 @@ static int wolfSSH_TPM_InitKey(WOLFTPM2_DEV* dev, const char* name,
1031943 if (rc == 0 ) {
1032944 XMEMCPY (& pTpmKey -> handle , & tpmKeyBlob .handle , sizeof (pTpmKey -> handle ));
1033945 XMEMCPY (& pTpmKey -> pub , & tpmKeyBlob .pub , sizeof (pTpmKey -> pub ));
946+ wolfTPM2_UnloadHandle (dev , & endorse .handle );
1034947 }
1035948
1036949 /* Cleanup */
0 commit comments