@@ -708,7 +708,7 @@ static int getPrimaryStoragekey(WOLFTPM2_DEV* pDev,
708708 rc = wolfTPM2_CreateSRK (pDev , pStorageKey , alg ,
709709 (byte * )gStorageKeyAuth , sizeof (gStorageKeyAuth )- 1 );
710710 #ifndef WOLFTPM_WINAPI
711- if (rc == TPM_RC_SUCCESS ) {
711+ if (rc == 0 ) {
712712 /* Move storage key into persistent NV */
713713 rc = wolfTPM2_NVStoreKey (pDev , TPM_RH_OWNER , pStorageKey ,
714714 TPM2_DEMO_STORAGE_KEY_HANDLE );
@@ -777,7 +777,7 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
777777 /* Decode the byte stream into a publicArea structure ready for use */
778778 rc = TPM2_ParsePublic (& key -> pub , pubAreaBuffer ,
779779 (word32 )sizeof (pubAreaBuffer ), & pubAreaSize );
780- if (rc != TPM_RC_SUCCESS ) return rc ;
780+ if (rc != 0 ) return rc ;
781781
782782 if (fileSz > 0 ) {
783783 printf ("Reading the private part of the key\n" );
@@ -816,94 +816,85 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
816816 WLOG (WS_LOG_DEBUG , "Leaving readKeyBlob(), rc = %d" , rc );
817817 return rc ;
818818}
819- // make rc check cleanup at end and get rid of uneeded returns
819+
820820static int wolfSSH_TPM_InitKey (WOLFTPM2_DEV * dev , const char * name ,
821821 WOLFTPM2_KEY * pTpmKey )
822822{
823- int rc ;
823+ int rc = 0 ;
824824 WOLFTPM2_KEY storage ;
825825 WOLFTPM2_KEYBLOB tpmKeyBlob ;
826826 byte * p = NULL ;
827+ /* TODO: workaround until password can be supplied */
828+ /* consider a refactor to take a 32-bit handle and key auth password */
829+ static const char gKeyAuth [] = "ThisIsMyKeyAuth" ;
827830
828831 WLOG (WS_LOG_DEBUG , "Entering wolfSSH_TPM_InitKey()" );
829832
830- rc = wolfTPM2_Init (dev , TPM2_IoCb , NULL );
831- if (rc != TPM_RC_SUCCESS ) {
832- #ifdef DEBUG_WOLFSSH
833- printf ("TPM 2.0 Device initialization failed\n" );
834- #endif
835- return WS_ERROR ;
833+ /* Initilize the TPM 2.0 device */
834+ if (rc == 0 ) {
835+ rc = wolfTPM2_Init (dev , TPM2_IoCb , NULL );
836+ if (rc != 0 )
837+ WLOG (WS_LOG_DEBUG , "TPM 2.0 Device initialization failed, rc: %d" , rc );
836838 }
837839
838840 /* TPM 2.0 keys live under a Primary Key, acquire such key */
839- rc = getPrimaryStoragekey (dev , & storage , TPM_ALG_RSA );
840- if (rc != TPM_RC_SUCCESS ) {
841- #ifdef DEBUG_WOLFSSH
842- printf ("Acquiring a Primary TPM 2.0 Key failed\n" );
843- #endif
844- return WS_BAD_ARGUMENT ;
841+ if (rc == 0 ) {
842+ rc = getPrimaryStoragekey (dev , & storage , TPM_ALG_RSA );
843+ if (rc != 0 )
844+ WLOG (WS_LOG_DEBUG , "Acquiring a Primary TPM 2.0 Key failed, rc: %d" , rc );
845845 }
846846
847847 /* Load the TPM 2.0 key blob from disk */
848- rc = readKeyBlob (name , & tpmKeyBlob );
849- if (rc != TPM_RC_SUCCESS ) {
850- #ifdef DEBUG_WOLFSSH
851- printf ("Reading key blob from disk failed\n" );
852- #endif
853- return WS_DECRYPT_E ;
848+ if (rc == 0 ) {
849+ rc = readKeyBlob (name , & tpmKeyBlob );
850+ if (rc != 0 )
851+ WLOG (WS_LOG_DEBUG , "Reading key blob from disk failed, rc: %d" , rc );
854852 }
855853
856- /* TODO: workaround until password can be supplied */
857- /* consider a refactor to take a 32-bit handle and key auth password */
858- static const char gKeyAuth [] = "ThisIsMyKeyAuth" ;
859854 /* set session for authorization key */
860- tpmKeyBlob .handle .auth .size = (int )sizeof (gKeyAuth )- 1 ;
861- XMEMCPY (tpmKeyBlob .handle .auth .buffer , gKeyAuth , tpmKeyBlob .handle .auth .size );
855+ if (rc == 0 ) {
856+ tpmKeyBlob .handle .auth .size = (int )sizeof (gKeyAuth )- 1 ;
857+ XMEMCPY (tpmKeyBlob .handle .auth .buffer , gKeyAuth ,
858+ tpmKeyBlob .handle .auth .size );
859+ }
862860
863861 /* Load the public key into the TPM device */
864- rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & storage .handle );
865- if (rc != TPM_RC_SUCCESS ) {
866- #ifdef DEBUG_WOLFSSH
867- printf ("wolfTPM2_LoadKey failed\n" );
868- #endif
869- return WS_BAD_ARGUMENT ;
862+ if (rc == 0 ) {
863+ rc = wolfTPM2_LoadKey (dev , & tpmKeyBlob , & storage .handle );
864+ if (rc != 0 )
865+ WLOG (WS_LOG_DEBUG , "wolfTPM2_LoadKey failed, rc: %d" , rc );
866+ WLOG (WS_LOG_DEBUG , "Loaded key to 0x%x\n" , (word32 )tpmKeyBlob .handle .hndl );
870867 }
871- #ifdef DEBUG_WOLFSSH
872- printf ("Loaded key to 0x%x\n" , (word32 )tpmKeyBlob .handle .hndl );
873- #endif
874868
875869 /* Read the public key and extract the public key as a DER/ASN.1 */
876- userPublicKeySz = sizeof (userPublicKeyBuf );
877- rc = wolfTPM2_ExportPublicKeyBuffer (dev , (WOLFTPM2_KEY * )& tpmKeyBlob ,
878- ENCODING_TYPE_ASN1 , userPublicKey , & userPublicKeySz );
879- if (rc != TPM_RC_SUCCESS ) {
880- #ifdef DEBUG_WOLFSSH
881- printf ("Exporting TPM key failed\n" );
882- #endif
883- return WS_MEMORY_E ;
870+ if (rc == 0 ) {
871+ userPublicKeySz = sizeof (userPublicKeyBuf );
872+ rc = wolfTPM2_ExportPublicKeyBuffer (dev , (WOLFTPM2_KEY * )& tpmKeyBlob ,
873+ ENCODING_TYPE_ASN1 , userPublicKey , & userPublicKeySz );
874+ if (rc != 0 )
875+ WLOG (WS_LOG_DEBUG , "Exporting TPM key failed, rc: %d" , rc );
884876 }
885877
886- /* Read public key from the buffer and convert the key to OpenSSH format */
887- rc = wolfSSH_ReadPublicKey_buffer ( userPublicKey , userPublicKeySz ,
888- WOLFSSH_FORMAT_ASN1 , & p , & userPublicKeySz , & userPublicKeyType ,
889- & userPublicKeyTypeSz , NULL );
890- if ( rc != TPM_RC_SUCCESS ) {
891- #ifdef DEBUG_WOLFSSH
892- printf ( "Reading public key failed returned : %d\n " , rc );
893- #endif
894- return WS_PUBKEY_REJECTED_E ;
878+ /* Read public key from buffer and convert key to OpenSSH format */
879+ if ( rc == 0 ) {
880+ rc = wolfSSH_ReadPublicKey_buffer ( userPublicKey , userPublicKeySz ,
881+ WOLFSSH_FORMAT_ASN1 , & p , & userPublicKeySz , & userPublicKeyType ,
882+ & userPublicKeyTypeSz , NULL );
883+ if ( rc != 0 )
884+ WLOG ( WS_LOG_DEBUG , "Reading public key failed, rc : %d" , rc );
885+ else
886+ userPublicKey = p ;
895887 }
896- userPublicKey = p ;
897-
898- XMEMCPY (& pTpmKey -> handle , & tpmKeyBlob .handle , sizeof (pTpmKey -> handle ));
899- XMEMCPY (& pTpmKey -> pub , & tpmKeyBlob .pub , sizeof (pTpmKey -> pub ));
900888
901889 /* Unload SRK storage handle */
902- wolfTPM2_UnloadHandle (dev , & storage .handle );
903- /* Key handle is unloaded on TPM cleanup */
890+ if (rc == 0 ) {
891+ XMEMCPY (& pTpmKey -> handle , & tpmKeyBlob .handle , sizeof (pTpmKey -> handle ));
892+ XMEMCPY (& pTpmKey -> pub , & tpmKeyBlob .pub , sizeof (pTpmKey -> pub ));
893+ wolfTPM2_UnloadHandle (dev , & storage .handle );
894+ }
904895
905896 WLOG (WS_LOG_DEBUG , "Leaving wolfSSH_TPM_InitKey()" );
906- return WS_SUCCESS ;
897+ return rc ;
907898}
908899
909900static void wolfSSH_TPM_Cleanup (WOLFTPM2_DEV * dev , WOLFTPM2_KEY * key )
0 commit comments