@@ -1682,6 +1682,9 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
16821682 /* key is bindAuthValue || salt */
16831683 XMEMSET (& keyIn , 0 , sizeof (keyIn ));
16841684 if (bind && bind -> auth .size > 0 ) {
1685+ if (bind -> auth .size > (UINT16 )sizeof (bind -> auth .buffer )) {
1686+ return BUFFER_E ;
1687+ }
16851688 if ((keyIn .size + bind -> auth .size ) > (UINT16 )sizeof (keyIn .buffer )) {
16861689 return BUFFER_E ;
16871690 }
@@ -1690,7 +1693,10 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
16901693 keyIn .size += bind -> auth .size ;
16911694 }
16921695 if (session -> salt .size > 0 ) {
1693- if (keyIn .size + session -> salt .size > sizeof (keyIn .buffer )) {
1696+ if (session -> salt .size > (UINT16 )sizeof (session -> salt .buffer )) {
1697+ return BUFFER_E ;
1698+ }
1699+ if ((keyIn .size + session -> salt .size ) > (UINT16 )sizeof (keyIn .buffer )) {
16941700 return BUFFER_E ;
16951701 }
16961702 XMEMCPY (& keyIn .buffer [keyIn .size ], session -> salt .buffer ,
@@ -1754,6 +1760,7 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
17541760 int rc ;
17551761 CreatePrimary_In createPriIn ;
17561762 CreatePrimary_Out createPriOut ;
1763+ TPMT_TK_CREATION * ticket ;
17571764
17581765 if (dev == NULL || pkey == NULL || publicTemplate == NULL )
17591766 return BAD_FUNC_ARG ;
@@ -1819,19 +1826,17 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
18191826 pkey -> creationHash .size = sizeof (pkey -> creationHash .buffer );
18201827 }
18211828 XMEMCPY (pkey -> creationHash .buffer , createPriOut .creationHash .buffer ,
1822- createPriOut . creationHash .size );
1829+ pkey -> creationHash .size );
18231830
1824- pkey -> creationTicket .tag = createPriOut .creationTicket .tag ;
1825- pkey -> creationTicket .hierarchy = createPriOut .creationTicket .hierarchy ;
1826- pkey -> creationTicket .digest .size = createPriOut .creationTicket .digest .size ;
1827- if (pkey -> creationTicket .digest .size >
1828- sizeof (pkey -> creationTicket .digest .buffer )) {
1829- pkey -> creationTicket .digest .size =
1830- sizeof (pkey -> creationTicket .digest .buffer );
1831+ ticket = & pkey -> creationTicket ;
1832+ ticket -> tag = createPriOut .creationTicket .tag ;
1833+ ticket -> hierarchy = createPriOut .creationTicket .hierarchy ;
1834+ ticket -> digest .size = createPriOut .creationTicket .digest .size ;
1835+ if (ticket -> digest .size > sizeof (ticket -> digest .buffer )) {
1836+ ticket -> digest .size = sizeof (ticket -> digest .buffer );
18311837 }
1832- XMEMCPY (pkey -> creationTicket .digest .buffer ,
1833- createPriOut .creationTicket .digest .buffer ,
1834- createPriOut .creationTicket .digest .size );
1838+ XMEMCPY (ticket -> digest .buffer , createPriOut .creationTicket .digest .buffer ,
1839+ ticket -> digest .size );
18351840
18361841#ifdef DEBUG_WOLFTPM
18371842 printf ("TPM2_CreatePrimary: 0x%x (%d bytes)\n" ,
@@ -1880,8 +1885,8 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
18801885 if (auth ) {
18811886 if (authSz > (int )sizeof (changeIn .newAuth .buffer ))
18821887 authSz = (int )sizeof (changeIn .newAuth .buffer );
1883- changeIn .newAuth .size = authSz ;
1884- XMEMCPY (changeIn .newAuth .buffer , auth , changeIn . newAuth . size );
1888+ changeIn .newAuth .size = ( UINT16 ) authSz ;
1889+ XMEMCPY (changeIn .newAuth .buffer , auth , authSz );
18851890 }
18861891
18871892 rc = TPM2_ObjectChangeAuth (& changeIn , & changeOut );
@@ -3691,12 +3696,14 @@ int wolfTPM2_EccKey_TpmToWolf(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
36913696
36923697 /* load public key */
36933698 qxSz = tpmKey -> pub .publicArea .unique .ecc .x .size ;
3694- if (qxSz > sizeof (qx )) {
3699+ if (qxSz > sizeof (qx ) ||
3700+ qxSz > sizeof (tpmKey -> pub .publicArea .unique .ecc .x .buffer )) {
36953701 return BUFFER_E ;
36963702 }
36973703 XMEMCPY (qx , tpmKey -> pub .publicArea .unique .ecc .x .buffer , qxSz );
36983704 qySz = tpmKey -> pub .publicArea .unique .ecc .y .size ;
3699- if (qySz > sizeof (qy )) {
3705+ if (qySz > sizeof (qy ) ||
3706+ qySz > sizeof (tpmKey -> pub .publicArea .unique .ecc .y .buffer )) {
37003707 return BUFFER_E ;
37013708 }
37023709 XMEMCPY (qy , tpmKey -> pub .publicArea .unique .ecc .y .buffer , qySz );
@@ -4593,8 +4600,8 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
45934600 if (inSz > (int )sizeof (rsaDecIn .cipherText .buffer )) {
45944601 inSz = (int )sizeof (rsaDecIn .cipherText .buffer ); /* truncate */
45954602 }
4596- rsaDecIn .cipherText .size = inSz ;
4597- XMEMCPY (rsaDecIn .cipherText .buffer , in , rsaDecIn . cipherText . size );
4603+ rsaDecIn .cipherText .size = ( UINT16 ) inSz ;
4604+ XMEMCPY (rsaDecIn .cipherText .buffer , in , inSz );
45984605 /* TPM_ALG_NULL, TPM_ALG_OAEP, TPM_ALG_RSASSA or TPM_ALG_RSAPSS */
45994606 rsaDecIn .inScheme .scheme = padScheme ;
46004607 rsaDecIn .inScheme .details .anySig .hashAlg = WOLFTPM2_WRAP_DIGEST ;
@@ -5707,7 +5714,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
57075714 printf ("wolfTPM2_LoadSymmetricKey: 0x%x\n" ,
57085715 (word32 )loadExtOut .objectHandle );
57095716 #endif
5710- return rc ;
57115717 }
57125718
57135719exit :
@@ -5717,7 +5723,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
57175723 printf ("TPM2_LoadExternal: failed %d: %s\n" ,
57185724 rc , wolfTPM2_GetRCString (rc ));
57195725 #endif
5720- return rc ;
57215726 }
57225727
57235728 return rc ;
0 commit comments