F-6318 - Return BUFFER_E on short private read in readKeyBlob

pull/535/head
aidan garske 2026-06-22 12:37:54 -07:00
parent f9ebeb38ed
commit 9e7769a08a
2 changed files with 30 additions and 3 deletions

View File

@ -255,7 +255,7 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
if (bytes_read != sizeof(key->priv.size)) {
printf("Read %zu, expected private size marker of %zu bytes\n",
bytes_read, sizeof(key->priv.size));
goto exit;
rc = BUFFER_E; goto exit;
}
if (key->priv.size > sizeof(key->priv.buffer)) {
printf("Private key size is too large\n");
@ -265,7 +265,7 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
if (bytes_read != key->priv.size) {
printf("Read %zu, expected private blob %zu bytes\n",
bytes_read, (size_t)key->priv.size);
goto exit;
rc = BUFFER_E; goto exit;
}
rc = 0; /* success */
}

View File

@ -3203,7 +3203,8 @@ static void test_TPM2_ParsePublic_EmptyClears(void)
}
/* An oversized inner size on a classic arm must be clamped to the arm buffer
* size, matching the PQC arms, so AppendBytes does not over-read the source. */
* size, matching the PQC arms, so AppendBytes does not over-read the source.
* Each arm has its own buffer member so all four are exercised. */
static void test_TPM2_AppendSensitive_Clamp(void)
{
TPM2_Packet packet;
@ -3991,6 +3992,7 @@ static void test_readKeyBlob_PrivOverflow(void)
word32 i;
size_t privTailSz, remaining, chunk;
UINT16 privSizeMarker;
UINT16 bigMarker;
XFILE fp;
byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)];
byte filler[64];
@ -4046,6 +4048,31 @@ static void test_readKeyBlob_PrivOverflow(void)
remove(filename);
/* Rejection branch: priv.size marker larger than the destination buffer
* must be refused with BUFFER_E before any bytes are read. */
bigMarker = (UINT16)(sizeof(guarded.key.priv.buffer) + 1);
fp = XFOPEN(filename, "wb");
AssertNotNull(fp);
XFWRITE(&tmpl.pub.size, 1, sizeof(tmpl.pub.size), fp);
XFWRITE(pubAreaBuffer, 1, sizeof(UINT16) + tmpl.pub.size, fp);
XFWRITE(&bigMarker, 1, sizeof(bigMarker), fp);
XFCLOSE(fp);
XMEMSET(&guarded, 0, sizeof(guarded));
rc = readKeyBlob(filename, &guarded.key);
AssertIntEQ(rc, BUFFER_E);
remove(filename);
/* Rejection branch: pub.size marker larger than the public area buffer. */
bigMarker = (UINT16)sizeof(pubAreaBuffer);
fp = XFOPEN(filename, "wb");
AssertNotNull(fp);
XFWRITE(&bigMarker, 1, sizeof(bigMarker), fp);
XFCLOSE(fp);
XMEMSET(&guarded, 0, sizeof(guarded));
rc = readKeyBlob(filename, &guarded.key);
AssertIntEQ(rc, BUFFER_E);
remove(filename);
printf("Test TPM Wrapper: %-40s Passed\n", "readKeyBlob priv overflow:");
}
#endif