Skip to content

Commit 604e3f3

Browse files
committed
Remove use of VLA in key unwrapping function
1 parent adb4534 commit 604e3f3

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/wh_server_keystore.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,12 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
572572
uint8_t iv[WOLFHSM_KEYWRAP_AES_GCM_IV_SIZE];
573573
uint8_t serverKey[AES_MAX_KEY_SIZE];
574574
uint32_t serverKeySz = sizeof(serverKey);
575+
uint8_t plainBlob[sizeof(*metadataIn) + WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE];
576+
uint32_t plainBlobSz = sizeof(*metadataIn) + keySz;
577+
uint8_t* encBlob;
575578

576579
if (server == NULL || keyIn == NULL || metadataIn == NULL ||
577-
wrappedKeyOut == NULL) {
580+
wrappedKeyOut == NULL || plainBlobSz > sizeof(plainBlob)) {
578581
return WH_ERROR_BADARGS;
579582
}
580583

@@ -613,15 +616,14 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
613616
}
614617

615618
/* Combine key and metadata into one blob */
616-
uint8_t plainBlob[sizeof(*metadataIn) + keySz];
617619
memcpy(plainBlob, metadataIn, sizeof(*metadataIn));
618620
memcpy(plainBlob + sizeof(*metadataIn), keyIn, keySz);
619621

620-
/* Place the encrypted blob after the IV and Auth Tag*/
621-
uint8_t* encBlob = (uint8_t*)wrappedKeyOut + sizeof(iv) + sizeof(authTag);
622+
/* Place the encrypted blob after the IV and Auth Tag */
623+
encBlob = (uint8_t*)wrappedKeyOut + sizeof(iv) + sizeof(authTag);
622624

623625
/* Encrypt the blob */
624-
ret = wc_AesGcmEncrypt(aes, encBlob, plainBlob, sizeof(plainBlob), iv,
626+
ret = wc_AesGcmEncrypt(aes, encBlob, plainBlob, plainBlobSz, iv,
625627
sizeof(iv), authTag, sizeof(authTag), NULL, 0);
626628
if (ret != 0) {
627629
wc_AesFree(aes);
@@ -650,10 +652,10 @@ static int _AesGcmUnwrapKey(whServerContext* server, uint16_t serverKeyId,
650652
uint32_t serverKeySz = sizeof(serverKey);
651653
uint8_t* encBlob = (uint8_t*)wrappedKeyIn + sizeof(iv) + sizeof(authTag);
652654
uint16_t encBlobSz = wrappedKeySz - sizeof(iv) - sizeof(authTag);
653-
uint8_t plainBlob[sizeof(*metadataOut) + keySz];
655+
uint8_t plainBlob[sizeof(*metadataOut) + WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE];
654656

655657
if (server == NULL || wrappedKeyIn == NULL || metadataOut == NULL ||
656-
keyOut == NULL) {
658+
keyOut == NULL || keySz > WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE) {
657659
return WH_ERROR_BADARGS;
658660
}
659661

0 commit comments

Comments
 (0)