Skip to content

Commit 314f0a5

Browse files
committed
RSA: validate key size buffer when decoding signature
F/93
1 parent e73b518 commit 314f0a5

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

src/image.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ static void wolfBoot_verify_signature_ecc(uint8_t key_slot,
367367
static inline int DecodeAsn1Tag(const uint8_t* input, int inputSz, int* inOutIdx,
368368
int* tag_len, uint8_t tag)
369369
{
370+
if ((*inOutIdx + 1) >= inputSz) {
371+
return -1;
372+
}
370373
if (input[*inOutIdx] != tag) {
371374
return -1;
372375
}

tools/unit-tests/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ endif
3535

3636
TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
3737
unit-aes256 unit-chacha20 unit-pci unit-mock-state unit-sectorflags \
38-
unit-image unit-nvm unit-nvm-flagshome unit-enc-nvm \
38+
unit-image unit-image-rsa unit-nvm unit-nvm-flagshome unit-enc-nvm \
3939
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4040
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
4141
unit-multiboot
@@ -130,6 +130,11 @@ unit-sectorflags: ../../include/target.h unit-sectorflags.c
130130
unit-image: unit-image.c unit-common.c $(WOLFCRYPT_SRC)
131131
gcc -o $@ $^ $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS)
132132

133+
unit-image-rsa: CFLAGS += -DWOLFBOOT_SIGN_RSA2048
134+
unit-image-rsa: ../../include/target.h unit-image.c unit-common.c
135+
gcc -o $@ unit-image.c unit-common.c $(WOLFCRYPT_SRC) \
136+
$(CFLAGS) -D__WOLFBOOT $(LDFLAGS)
137+
133138
unit-nvm: ../../include/target.h unit-nvm.c
134139
gcc -o $@ unit-nvm.c $(CFLAGS) $(LDFLAGS)
135140

tools/unit-tests/unit-image.c

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr)
319319
}
320320
}
321321

322-
322+
#if defined(WOLFBOOT_SIGN_ECC256)
323323
int wc_ecc_init(ecc_key* key) {
324324
if (ecc_init_fail)
325325
return -1;
@@ -348,7 +348,9 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
348348
*res = 1;
349349
return 0;
350350
}
351+
#endif
351352

353+
#if defined(WOLFBOOT_SIGN_ECC256)
352354
START_TEST(test_verify_signature)
353355
{
354356
uint8_t pubkey[32];
@@ -382,6 +384,60 @@ START_TEST(test_verify_signature)
382384
ck_assert_int_eq(verify_called, 1);
383385
}
384386
END_TEST
387+
#endif
388+
389+
#if defined(WOLFBOOT_SIGN_RSA2048) || defined(WOLFBOOT_SIGN_RSA3072) || \
390+
defined(WOLFBOOT_SIGN_RSA4096) || defined(WOLFBOOT_SIGN_SECONDARY_RSA2048) || \
391+
defined(WOLFBOOT_SIGN_SECONDARY_RSA3072) || \
392+
defined(WOLFBOOT_SIGN_SECONDARY_RSA4096)
393+
int wc_InitRsaKey(RsaKey* key, void* heap)
394+
{
395+
(void)key;
396+
(void)heap;
397+
return 0;
398+
}
399+
400+
int wc_FreeRsaKey(RsaKey* key)
401+
{
402+
(void)key;
403+
return 0;
404+
}
405+
406+
int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
407+
word32 inSz)
408+
{
409+
(void)input;
410+
(void)inOutIdx;
411+
(void)key;
412+
(void)inSz;
413+
return 0;
414+
}
415+
416+
int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, RsaKey* key)
417+
{
418+
(void)in;
419+
(void)inLen;
420+
(void)out;
421+
(void)key;
422+
return 0;
423+
}
424+
425+
START_TEST(test_decode_asn1_tag_start_bounds)
426+
{
427+
uint8_t *input = malloc(1);
428+
int idx = 0;
429+
volatile int input_sz = 1;
430+
int tag_len = -1;
431+
432+
ck_assert_ptr_nonnull(input);
433+
input[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
434+
435+
ck_assert_int_eq(DecodeAsn1Tag(input, input_sz, &idx, &tag_len,
436+
ASN_SEQUENCE | ASN_CONSTRUCTED), -1);
437+
free(input);
438+
}
439+
END_TEST
440+
#endif
385441

386442

387443
START_TEST(test_sha_ops)
@@ -517,6 +573,7 @@ START_TEST(test_headers)
517573
ck_assert_uint_eq(sz, test_img_len - 256);
518574
}
519575

576+
#if defined(WOLFBOOT_SIGN_ECC256)
520577
START_TEST(test_verify_authenticity)
521578
{
522579
struct wolfBoot_image test_img;
@@ -583,6 +640,7 @@ START_TEST(test_verify_authenticity_bad_siglen)
583640
ck_assert_int_eq(ret, -1);
584641
}
585642
END_TEST
643+
#endif
586644

587645
START_TEST(test_verify_integrity)
588646
{
@@ -683,11 +741,35 @@ Suite *wolfboot_suite(void)
683741
/* Suite initialization */
684742
Suite *s = suite_create("wolfBoot");
685743

744+
#if defined(WOLFBOOT_SIGN_ECC256)
686745
TCase* tcase_verify_signature = tcase_create("verify_signature");
687746
tcase_set_timeout(tcase_verify_signature, 20);
688747
tcase_add_test(tcase_verify_signature, test_verify_signature);
689748
suite_add_tcase(s, tcase_verify_signature);
749+
#endif
750+
751+
#if defined(WOLFBOOT_SIGN_RSA2048) || defined(WOLFBOOT_SIGN_RSA3072) || \
752+
defined(WOLFBOOT_SIGN_RSA4096) || defined(WOLFBOOT_SIGN_SECONDARY_RSA2048) || \
753+
defined(WOLFBOOT_SIGN_SECONDARY_RSA3072) || \
754+
defined(WOLFBOOT_SIGN_SECONDARY_RSA4096)
755+
TCase* tcase_rsa_asn1 = tcase_create("rsa_asn1");
756+
tcase_set_timeout(tcase_rsa_asn1, 20);
757+
tcase_add_test(tcase_rsa_asn1, test_decode_asn1_tag_start_bounds);
758+
suite_add_tcase(s, tcase_rsa_asn1);
759+
#endif
760+
761+
#if defined(WOLFBOOT_SIGN_ECC256)
762+
TCase* tcase_verify_authenticity = tcase_create("verify_authenticity");
763+
tcase_set_timeout(tcase_verify_authenticity, 20);
764+
tcase_add_test(tcase_verify_authenticity, test_verify_authenticity);
765+
tcase_add_test(tcase_verify_authenticity, test_verify_authenticity_bad_siglen);
766+
suite_add_tcase(s, tcase_verify_authenticity);
767+
#endif
690768

769+
#if !defined(WOLFBOOT_SIGN_RSA2048) && !defined(WOLFBOOT_SIGN_RSA3072) && \
770+
!defined(WOLFBOOT_SIGN_RSA4096) && !defined(WOLFBOOT_SIGN_SECONDARY_RSA2048) && \
771+
!defined(WOLFBOOT_SIGN_SECONDARY_RSA3072) && \
772+
!defined(WOLFBOOT_SIGN_SECONDARY_RSA4096)
691773
TCase* tcase_sha_ops = tcase_create("sha_ops");
692774
tcase_set_timeout(tcase_sha_ops, 20);
693775
tcase_add_test(tcase_sha_ops, test_sha_ops);
@@ -698,12 +780,6 @@ Suite *wolfboot_suite(void)
698780
tcase_add_test(tcase_headers, test_headers);
699781
suite_add_tcase(s, tcase_headers);
700782

701-
TCase* tcase_verify_authenticity = tcase_create("verify_authenticity");
702-
tcase_set_timeout(tcase_verify_authenticity, 20);
703-
tcase_add_test(tcase_verify_authenticity, test_verify_authenticity);
704-
tcase_add_test(tcase_verify_authenticity, test_verify_authenticity_bad_siglen);
705-
suite_add_tcase(s, tcase_verify_authenticity);
706-
707783
TCase* tcase_verify_integrity = tcase_create("verify_integrity");
708784
tcase_set_timeout(tcase_verify_integrity, 20);
709785
tcase_add_test(tcase_verify_integrity, test_verify_integrity);
@@ -713,6 +789,7 @@ Suite *wolfboot_suite(void)
713789
tcase_set_timeout(tcase_open_image, 20);
714790
tcase_add_test(tcase_open_image, test_open_image);
715791
suite_add_tcase(s, tcase_open_image);
792+
#endif
716793
return s;
717794
}
718795

0 commit comments

Comments
 (0)