@@ -699,6 +699,53 @@ START_TEST(test_make_header_ex_rejects_cert_chain_tlv_length_overflow)
699699}
700700END_TEST
701701
702+ START_TEST (test_make_header_ex_rejects_signature_tlv_length_overflow )
703+ {
704+ char tempdir [] = "/tmp/wolfboot-sign-XXXXXX" ;
705+ char image_path [PATH_MAX ];
706+ char output_path [PATH_MAX ];
707+ char signature_path [PATH_MAX ];
708+ uint8_t image_buf [] = { 0x11 , 0x22 , 0x33 , 0x44 };
709+ uint8_t pubkey [] = { 0xA5 };
710+ uint8_t * signature_buf = NULL ;
711+ const uint32_t signature_len = 65536U ;
712+ int ret ;
713+
714+ ck_assert_ptr_nonnull (mkdtemp (tempdir ));
715+
716+ snprintf (image_path , sizeof (image_path ), "%s/image.bin" , tempdir );
717+ snprintf (output_path , sizeof (output_path ), "%s/output.bin" , tempdir );
718+ snprintf (signature_path , sizeof (signature_path ), "%s/signature.bin" , tempdir );
719+
720+ signature_buf = malloc (signature_len );
721+ ck_assert_ptr_nonnull (signature_buf );
722+ memset (signature_buf , 0x5A , signature_len );
723+
724+ ck_assert_int_eq (write_file (image_path , image_buf , sizeof (image_buf )), 0 );
725+ ck_assert_int_eq (write_file (signature_path , signature_buf , signature_len ), 0 );
726+
727+ reset_cmd_defaults ();
728+ CMD .sign = SIGN_RSA2048 ;
729+ CMD .manual_sign = 1 ;
730+ CMD .signature_file = signature_path ;
731+ CMD .signature_sz = signature_len ;
732+ /* Keep room for a large signature TLV to expose uint16_t truncation. */
733+ CMD .header_sz = 131072U ;
734+
735+ reset_mocks (NULL , 0 );
736+ ret = make_header_ex (0 , pubkey , sizeof (pubkey ), image_path , output_path ,
737+ 0 , 0 , 0 , 0 , NULL , 0 , NULL , 0 );
738+
739+ ck_assert_int_ne (ret , 0 );
740+
741+ free (signature_buf );
742+ unlink (output_path );
743+ unlink (signature_path );
744+ unlink (image_path );
745+ rmdir (tempdir );
746+ }
747+ END_TEST
748+
702749Suite * wolfboot_suite (void )
703750{
704751 Suite * s = suite_create ("sign-encrypted-output" );
@@ -717,6 +764,8 @@ Suite *wolfboot_suite(void)
717764 test_make_header_ex_keeps_boundary_header_for_sha384_sha3_hybrid_cert_chain );
718765 tcase_add_test (tcase ,
719766 test_make_header_ex_rejects_cert_chain_tlv_length_overflow );
767+ tcase_add_test (tcase ,
768+ test_make_header_ex_rejects_signature_tlv_length_overflow );
720769 suite_add_tcase (s , tcase );
721770
722771 return s ;
0 commit comments