Skip to content

Commit 657e337

Browse files
committed
Reject oversized cert-chain TLVs in sign tool
F/2583
1 parent bc4ec50 commit 657e337

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

tools/keytools/sign.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,15 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
14501450

14511451
cert_chain_sz = file_stat.st_size;
14521452

1453+
if (cert_chain_sz > (uint32_t)UINT16_MAX) {
1454+
printf("Error: Certificate chain too large for TLV encoding "
1455+
"(%u > %u)\n",
1456+
cert_chain_sz, (unsigned int)UINT16_MAX);
1457+
fclose(f);
1458+
f = NULL;
1459+
goto failure;
1460+
}
1461+
14531462
/* Verify that the chain will fit in our header */
14541463
if (header_idx + cert_chain_tlv_hdr_sz + cert_chain_sz >
14551464
CMD.header_sz) {

tools/unit-tests/unit-sign-encrypted-output.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,50 @@ START_TEST(test_make_header_ex_keeps_boundary_header_for_sha384_sha3_hybrid_cert
605605
}
606606
END_TEST
607607

608+
START_TEST(test_make_header_ex_rejects_cert_chain_tlv_length_overflow)
609+
{
610+
char tempdir[] = "/tmp/wolfboot-sign-XXXXXX";
611+
char image_path[PATH_MAX];
612+
char output_path[PATH_MAX];
613+
char cert_chain_path[PATH_MAX];
614+
uint8_t image_buf[] = { 0x41, 0x42, 0x43, 0x44 };
615+
uint8_t pubkey[] = { 0xA5 };
616+
uint8_t *cert_chain_buf = NULL;
617+
const uint32_t cert_chain_len = 65536U;
618+
int ret;
619+
620+
ck_assert_ptr_nonnull(mkdtemp(tempdir));
621+
622+
snprintf(image_path, sizeof(image_path), "%s/image.bin", tempdir);
623+
snprintf(output_path, sizeof(output_path), "%s/output.bin", tempdir);
624+
snprintf(cert_chain_path, sizeof(cert_chain_path), "%s/cert-chain.bin",
625+
tempdir);
626+
627+
cert_chain_buf = malloc(cert_chain_len);
628+
ck_assert_ptr_nonnull(cert_chain_buf);
629+
memset(cert_chain_buf, 0xC7, cert_chain_len);
630+
631+
ck_assert_int_eq(write_file(image_path, image_buf, sizeof(image_buf)), 0);
632+
ck_assert_int_eq(write_file(cert_chain_path, cert_chain_buf,
633+
cert_chain_len), 0);
634+
635+
reset_cmd_defaults();
636+
CMD.cert_chain_file = cert_chain_path;
637+
638+
reset_mocks(NULL, 0);
639+
ret = make_header_ex(0, pubkey, sizeof(pubkey), image_path, output_path,
640+
0, 0, 0, 0, NULL, 0, NULL, 0);
641+
642+
ck_assert_int_ne(ret, 0);
643+
644+
free(cert_chain_buf);
645+
unlink(output_path);
646+
unlink(cert_chain_path);
647+
unlink(image_path);
648+
rmdir(tempdir);
649+
}
650+
END_TEST
651+
608652
Suite *wolfboot_suite(void)
609653
{
610654
Suite *s = suite_create("sign-encrypted-output");
@@ -620,6 +664,8 @@ Suite *wolfboot_suite(void)
620664
test_make_header_ex_roundtrip_finds_tlv_that_exactly_fills_header);
621665
tcase_add_test(tcase,
622666
test_make_header_ex_keeps_boundary_header_for_sha384_sha3_hybrid_cert_chain);
667+
tcase_add_test(tcase,
668+
test_make_header_ex_rejects_cert_chain_tlv_length_overflow);
623669
suite_add_tcase(s, tcase);
624670

625671
return s;

0 commit comments

Comments
 (0)