@@ -605,6 +605,50 @@ START_TEST(test_make_header_ex_keeps_boundary_header_for_sha384_sha3_hybrid_cert
605605}
606606END_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+
608652Suite * 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