3
3
*
4
4
* Copyright (c) 2017-2019 Linaro LTD
5
5
* Copyright (c) 2016-2019 JUUL Labs
6
- * Copyright (c) 2019-2023 Arm Limited
6
+ * Copyright (c) 2019-2024 Arm Limited
7
7
*
8
8
* Original license:
9
9
*
@@ -201,7 +201,27 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
201
201
# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
202
202
#endif
203
203
204
+ #if (defined(MCUBOOT_HW_KEY ) + \
205
+ defined(MCUBOOT_BUILTIN_KEY )) > 1
206
+ #error "Please use either MCUBOOT_HW_KEY or the MCUBOOT_BUILTIN_KEY feature."
207
+ #endif
208
+
204
209
#ifdef EXPECTED_SIG_TLV
210
+
211
+ #if !defined(MCUBOOT_BUILTIN_KEY )
212
+ #if !defined(MCUBOOT_HW_KEY )
213
+ /* The key TLV contains the hash of the public key. */
214
+ # define EXPECTED_KEY_TLV IMAGE_TLV_KEYHASH
215
+ # define KEY_BUF_SIZE IMAGE_HASH_SIZE
216
+ #else
217
+ /* The key TLV contains the whole public key.
218
+ * Add a few extra bytes to the key buffer size for encoding and
219
+ * for public exponent.
220
+ */
221
+ # define EXPECTED_KEY_TLV IMAGE_TLV_PUBKEY
222
+ # define KEY_BUF_SIZE (SIG_BUF_SIZE + 24)
223
+ #endif /* !MCUBOOT_HW_KEY */
224
+
205
225
#if !defined(MCUBOOT_HW_KEY )
206
226
static int
207
227
bootutil_find_key (uint8_t * keyhash , uint8_t keyhash_len )
@@ -228,7 +248,7 @@ bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
228
248
bootutil_sha_drop (& sha_ctx );
229
249
return -1 ;
230
250
}
231
- #else
251
+ #else /* !MCUBOOT_HW_KEY */
232
252
extern unsigned int pub_key_len ;
233
253
static int
234
254
bootutil_find_key (uint8_t image_index , uint8_t * key , uint16_t key_len )
@@ -266,7 +286,8 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
266
286
return -1 ;
267
287
}
268
288
#endif /* !MCUBOOT_HW_KEY */
269
- #endif
289
+ #endif /* !MCUBOOT_BUILTIN_KEY */
290
+ #endif /* EXPECTED_SIG_TLV */
270
291
271
292
/**
272
293
* Reads the value of an image's security counter.
@@ -328,6 +349,30 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
328
349
return 0 ;
329
350
}
330
351
352
+ #ifndef ALLOW_ROGUE_TLVS
353
+ /*
354
+ * The following list of TLVs are the only entries allowed in the unprotected
355
+ * TLV section. All other TLV entries must be in the protected section.
356
+ */
357
+ static const uint16_t allowed_unprot_tlvs [] = {
358
+ IMAGE_TLV_KEYHASH ,
359
+ IMAGE_TLV_PUBKEY ,
360
+ IMAGE_TLV_SHA256 ,
361
+ IMAGE_TLV_SHA384 ,
362
+ IMAGE_TLV_RSA2048_PSS ,
363
+ IMAGE_TLV_ECDSA224 ,
364
+ IMAGE_TLV_ECDSA_SIG ,
365
+ IMAGE_TLV_RSA3072_PSS ,
366
+ IMAGE_TLV_ED25519 ,
367
+ IMAGE_TLV_ENC_RSA2048 ,
368
+ IMAGE_TLV_ENC_KW ,
369
+ IMAGE_TLV_ENC_EC256 ,
370
+ IMAGE_TLV_ENC_X25519 ,
371
+ /* Mark end with ANY. */
372
+ IMAGE_TLV_ANY ,
373
+ };
374
+ #endif
375
+
331
376
/*
332
377
* Verify the integrity of the image.
333
378
* Return non-zero if image could not be validated/does not validate.
@@ -344,10 +389,16 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
344
389
int image_hash_valid = 0 ;
345
390
#ifdef EXPECTED_SIG_TLV
346
391
FIH_DECLARE (valid_signature , FIH_FAILURE );
392
+ #ifndef MCUBOOT_BUILTIN_KEY
347
393
int key_id = -1 ;
394
+ #else
395
+ /* Pass a key ID equal to the image index, the underlying crypto library
396
+ * is responsible for mapping the image index to a builtin key ID.
397
+ */
398
+ int key_id = image_index ;
399
+ #endif /* !MCUBOOT_BUILTIN_KEY */
348
400
#ifdef MCUBOOT_HW_KEY
349
- /* Few extra bytes for encoding and for public exponent. */
350
- uint8_t key_buf [SIG_BUF_SIZE + 24 ];
401
+ uint8_t key_buf [KEY_BUF_SIZE ];
351
402
#endif
352
403
#endif /* EXPECTED_SIG_TLV */
353
404
struct image_tlv_iter it ;
@@ -393,6 +444,27 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
393
444
break ;
394
445
}
395
446
447
+ #ifndef ALLOW_ROGUE_TLVS
448
+ /*
449
+ * Ensure that the non-protected TLV only has entries necessary to hold
450
+ * the signature. We also allow encryption related keys to be in the
451
+ * unprotected area.
452
+ */
453
+ if (!bootutil_tlv_iter_is_prot (& it , off )) {
454
+ bool found = false;
455
+ for (const uint16_t * p = allowed_unprot_tlvs ; * p != IMAGE_TLV_ANY ; p ++ ) {
456
+ if (type == * p ) {
457
+ found = true;
458
+ break ;
459
+ }
460
+ }
461
+ if (!found ) {
462
+ FIH_SET (fih_rc , FIH_FAILURE );
463
+ goto out ;
464
+ }
465
+ }
466
+ #endif
467
+
396
468
if (type == EXPECTED_HASH_TLV ) {
397
469
/* Verify the image hash. This must always be present. */
398
470
if (len != sizeof (hash )) {
@@ -411,44 +483,34 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
411
483
}
412
484
413
485
image_hash_valid = 1 ;
414
- #ifdef EXPECTED_SIG_TLV
415
- #ifndef MCUBOOT_HW_KEY
416
- } else if (type == IMAGE_TLV_KEYHASH ) {
486
+ #ifdef EXPECTED_KEY_TLV
487
+ } else if (type == EXPECTED_KEY_TLV ) {
417
488
/*
418
489
* Determine which key we should be checking.
419
490
*/
420
- if (len > IMAGE_HASH_SIZE ) {
491
+ if (len > KEY_BUF_SIZE ) {
421
492
rc = -1 ;
422
493
goto out ;
423
494
}
495
+ #ifndef MCUBOOT_HW_KEY
424
496
rc = LOAD_IMAGE_DATA (hdr , fap , off , buf , len );
425
497
if (rc ) {
426
498
goto out ;
427
499
}
428
500
key_id = bootutil_find_key (buf , len );
429
- /*
430
- * The key may not be found, which is acceptable. There
431
- * can be multiple signatures, each preceded by a key.
432
- */
433
501
#else
434
- } else if (type == IMAGE_TLV_PUBKEY ) {
435
- /*
436
- * Determine which key we should be checking.
437
- */
438
- if (len > sizeof (key_buf )) {
439
- rc = -1 ;
440
- goto out ;
441
- }
442
502
rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
443
503
if (rc ) {
444
504
goto out ;
445
505
}
446
506
key_id = bootutil_find_key (image_index , key_buf , len );
507
+ #endif /* !MCUBOOT_HW_KEY */
447
508
/*
448
509
* The key may not be found, which is acceptable. There
449
510
* can be multiple signatures, each preceded by a key.
450
511
*/
451
- #endif /* !MCUBOOT_HW_KEY */
512
+ #endif /* EXPECTED_KEY_TLV */
513
+ #ifdef EXPECTED_SIG_TLV
452
514
} else if (type == EXPECTED_SIG_TLV ) {
453
515
/* Ignore this signature if it is out of bounds. */
454
516
if (key_id < 0 || key_id >= bootutil_key_cnt ) {
0 commit comments