Various minor coverity fixes

pull/441/head
David Garske 2025-12-03 12:47:21 -08:00
parent 0a3b8deb56
commit 27d1a22144
10 changed files with 46 additions and 27 deletions

View File

@ -98,6 +98,8 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
goto exit_badargs;
}
XMEMSET(&primary, 0, sizeof(primary));
printf("Demo how to create a credential challenge for remote attestation\n");
printf("Credential will be stored in %s\n", output);

View File

@ -185,7 +185,7 @@ int TPM2_Boot_SecretSeal_Example(void* userCtx, int argc, char *argv[])
wc_FreeRng(&rng);
}
}
if (rc != 0 || secretSz == 0) {
if (rc != 0 || secretSz <= 0) {
printf("Error getting secret\n");
goto exit;
}

View File

@ -131,6 +131,7 @@ int TPM2_ExternalImport_Example(void* userCtx, int argc, char *argv[])
key2 = wolfTPM2_NewKeyBlob();
rsaKey3 = wolfTPM2_NewKeyBlob();
#endif
XMEMSET(&storage, 0, sizeof(storage));
primary = &storage;
rc = wolfTPM2_Init(&dev, TPM2_IoCb, NULL);

View File

@ -170,12 +170,13 @@ int TPM2_Unseal_Example(void* userCtx, int argc, char *argv[])
fp = XFOPEN(filename, "wb");
if (fp != XBADFILE) {
len = XFWRITE(cmdOut_unseal.outData.buffer, 1, cmdOut_unseal.outData.size, fp);
XFCLOSE(fp);
if (len != cmdOut_unseal.outData.size) {
printf("Error while writing the unsealed data to a file.\n");
goto exit;
}
}
XFCLOSE(fp);
printf("Stored unsealed data to file = %s\n", filename);
}
#else

View File

@ -287,7 +287,18 @@ exit:
printf("Failure %d (0x%x): %s\n", rc, rc, wolfSSL_ERR_reason_error_string(rc));
}
wolfSSL_shutdown(ssl);
if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
/* Bidirectional shutdown */
if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) {
int ret = wolfSSL_shutdown(ssl);
if (ret == WOLFSSL_SUCCESS) {
printf("Bidirectional shutdown complete\n");
}
else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
fprintf(stderr, "Bidirectional shutdown failed\n");
}
}
}
CloseAndCleanupSocket(&sockIoCtx);
wolfSSL_free(ssl);

View File

@ -149,10 +149,13 @@ int writeKeyBlob(const char* filename,
* save space */
rc = TPM2_AppendPublic(pubAreaBuffer, (word32)sizeof(pubAreaBuffer),
&pubAreaSize, &key->pub);
if (rc != TPM_RC_SUCCESS)
if (rc != TPM_RC_SUCCESS) {
XFCLOSE(fp);
return rc;
}
if (pubAreaSize != (key->pub.size + (int)sizeof(key->pub.size))) {
printf("writeKeyBlob: Sanity check for publicArea size failed\n");
XFCLOSE(fp);
return BUFFER_E;
}
#ifdef WOLFTPM_DEBUG_VERBOSE
@ -204,10 +207,13 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
goto exit;
}
fileSz -= bytes_read;
if (key->pub.size > sizeof(UINT16) + sizeof(pubAreaBuffer)) {
printf("Public key size is too large\n");
rc = BUFFER_E; goto exit;
}
bytes_read = XFREAD(pubAreaBuffer, 1,
sizeof(UINT16) + key->pub.size, fp);
if (bytes_read != sizeof(UINT16) + key->pub.size) {
if (bytes_read != (sizeof(UINT16) + key->pub.size)) {
printf("Read %zu, expected public blob %zu bytes\n",
bytes_read, sizeof(UINT16) + key->pub.size);
goto exit;

View File

@ -939,7 +939,6 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
printf("Encrypt/Decrypt test failed, result not as expected!\n");
goto exit;
}
if (rc != 0) goto exit;
#else
(void)aesIv;
#endif /* !WOLFTPM2_NO_WOLFCRYPT */

View File

@ -429,8 +429,10 @@ static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet,
/* Is auth session required for this TPM command? */
if (tag == TPM_ST_SESSIONS) {
/* Is there at least one auth session present? */
if (info->authCnt < 1 || ctx->session == NULL)
if (info->authCnt < 1 || ctx->session == NULL) {
packet->pos = cmdSz; /* restore */
return TPM_RC_AUTH_MISSING;
}
#ifdef WOLFTPM_DEBUG_VERBOSE
printf("Found %d auth sessions\n", info->authCnt);

View File

@ -237,7 +237,7 @@ int TPM2_TIS_Write(TPM2_CTX* ctx, word32 addr, const byte* value,
txBuf[2] = (addr>>8) & 0xFF;
txBuf[3] = (addr) & 0xFF;
XMEMCPY(&txBuf[TPM_TIS_HEADER_SZ], value, len);
XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len], 0,
XMEMSET(&txBuf[TPM_TIS_HEADER_SZ + len - 1], 0,
sizeof(txBuf) - TPM_TIS_HEADER_SZ - len);
XMEMSET(rxBuf, 0, sizeof(rxBuf));

View File

@ -1690,7 +1690,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
keyIn.size += bind->auth.size;
}
if (session->salt.size > 0) {
if (keyIn.size + session->salt.size > sizeof(keyIn.buffer)) {
if ((keyIn.size + session->salt.size) > (UINT16)sizeof(keyIn.buffer)) {
return BUFFER_E;
}
XMEMCPY(&keyIn.buffer[keyIn.size], session->salt.buffer,
@ -1754,6 +1754,7 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
int rc;
CreatePrimary_In createPriIn;
CreatePrimary_Out createPriOut;
TPMT_TK_CREATION* ticket;
if (dev == NULL || pkey == NULL || publicTemplate == NULL)
return BAD_FUNC_ARG;
@ -1819,19 +1820,17 @@ int wolfTPM2_CreatePrimaryKey_ex(WOLFTPM2_DEV* dev, WOLFTPM2_PKEY* pkey,
pkey->creationHash.size = sizeof(pkey->creationHash.buffer);
}
XMEMCPY(pkey->creationHash.buffer, createPriOut.creationHash.buffer,
createPriOut.creationHash.size);
pkey->creationHash.size);
pkey->creationTicket.tag = createPriOut.creationTicket.tag;
pkey->creationTicket.hierarchy = createPriOut.creationTicket.hierarchy;
pkey->creationTicket.digest.size = createPriOut.creationTicket.digest.size;
if (pkey->creationTicket.digest.size >
sizeof(pkey->creationTicket.digest.buffer)) {
pkey->creationTicket.digest.size =
sizeof(pkey->creationTicket.digest.buffer);
ticket = &pkey->creationTicket;
ticket->tag = createPriOut.creationTicket.tag;
ticket->hierarchy = createPriOut.creationTicket.hierarchy;
ticket->digest.size = createPriOut.creationTicket.digest.size;
if (ticket->digest.size > sizeof(ticket->digest.buffer)) {
ticket->digest.size = sizeof(ticket->digest.buffer);
}
XMEMCPY(pkey->creationTicket.digest.buffer,
createPriOut.creationTicket.digest.buffer,
createPriOut.creationTicket.digest.size);
XMEMCPY(ticket->digest.buffer, createPriOut.creationTicket.digest.buffer,
ticket->digest.size);
#ifdef DEBUG_WOLFTPM
printf("TPM2_CreatePrimary: 0x%x (%d bytes)\n",
@ -1880,8 +1879,8 @@ int wolfTPM2_ChangeAuthKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
if (auth) {
if (authSz > (int)sizeof(changeIn.newAuth.buffer))
authSz = (int)sizeof(changeIn.newAuth.buffer);
changeIn.newAuth.size = authSz;
XMEMCPY(changeIn.newAuth.buffer, auth, changeIn.newAuth.size);
changeIn.newAuth.size = (UINT16)authSz;
XMEMCPY(changeIn.newAuth.buffer, auth, authSz);
}
rc = TPM2_ObjectChangeAuth(&changeIn, &changeOut);
@ -4593,8 +4592,8 @@ int wolfTPM2_RsaDecrypt(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
if (inSz > (int)sizeof(rsaDecIn.cipherText.buffer)) {
inSz = (int)sizeof(rsaDecIn.cipherText.buffer); /* truncate */
}
rsaDecIn.cipherText.size = inSz;
XMEMCPY(rsaDecIn.cipherText.buffer, in, rsaDecIn.cipherText.size);
rsaDecIn.cipherText.size = (UINT16)inSz;
XMEMCPY(rsaDecIn.cipherText.buffer, in, inSz);
/* TPM_ALG_NULL, TPM_ALG_OAEP, TPM_ALG_RSASSA or TPM_ALG_RSAPSS */
rsaDecIn.inScheme.scheme = padScheme;
rsaDecIn.inScheme.details.anySig.hashAlg = WOLFTPM2_WRAP_DIGEST;
@ -5707,7 +5706,6 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
printf("wolfTPM2_LoadSymmetricKey: 0x%x\n",
(word32)loadExtOut.objectHandle);
#endif
return rc;
}
exit:
@ -5717,7 +5715,6 @@ exit:
printf("TPM2_LoadExternal: failed %d: %s\n",
rc, wolfTPM2_GetRCString(rc));
#endif
return rc;
}
return rc;