blob: 8865bb4c268c5586a2f3b98f560f94d489c552d7 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2017-2019 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
6 * Copyright (c) 2019-2020 Arm Limited
7 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <stddef.h>
David Vinczec3084132020-02-18 14:50:47 +010029#include <stdint.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include <inttypes.h>
31#include <string.h>
32
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020033#include <flash_map_backend/flash_map_backend.h>
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "bootutil/image.h"
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +090036#include "bootutil/crypto/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010038#include "bootutil/security_cnt.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080039
Fabio Utzigba1fbe62017-07-21 14:01:20 -030040#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030041
Fabio Utzigba829042018-09-18 08:29:34 -030042#ifdef MCUBOOT_ENC_IMAGES
43#include "bootutil/enc_key.h"
44#endif
45#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080046#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060047#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030048#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080049#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060050#endif
Arvin Farahmandf8240192020-05-05 11:43:52 -040051#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SIGN_RSA) || \
52 defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080053#include "mbedtls/asn1.h"
Arvin Farahmandf8240192020-05-05 11:43:52 -040054#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080055
56#include "bootutil_priv.h"
57
58/*
59 * Compute SHA256 over the image.
60 */
61static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030062bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
63 struct image_header *hdr, const struct flash_area *fap,
64 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
65 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080066{
David Browne629bf32017-04-24 13:37:33 -060067 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 uint32_t blk_sz;
69 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020070 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080071 uint32_t off;
72 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030073 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030074 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080075
Tamas Banfe031092020-09-10 17:32:39 +020076#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) || \
77 defined(MCUBOOT_RAM_LOAD)
Fabio Utzig10ee6482019-08-01 12:04:52 -030078 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030079 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030080 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030081 (void)blk_off;
82 (void)tlv_off;
Tamas Banfe031092020-09-10 17:32:39 +020083#ifdef MCUBOOT_RAM_LOAD
84 (void)blk_sz;
85 (void)off;
86 (void)rc;
87#endif
Fabio Utzigb0f04732019-07-31 09:49:19 -030088#endif
89
Fabio Utzigbc077932019-08-26 11:16:34 -030090#ifdef MCUBOOT_ENC_IMAGES
91 /* Encrypted images only exist in the secondary slot */
92 if (MUST_DECRYPT(fap, image_index, hdr) &&
93 !boot_enc_valid(enc_state, image_index, fap)) {
94 return -1;
95 }
96#endif
97
David Browne629bf32017-04-24 13:37:33 -060098 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080099
100 /* in some cases (split image) the hash is seeded with data from
101 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -0200102 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -0600103 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 }
105
David Vinczee32483f2019-06-13 10:46:24 +0200106 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300107 size = hdr_size = hdr->ih_hdr_size;
108 size += hdr->ih_img_size;
109 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +0200110
Fabio Utzige52c08e2019-09-11 19:32:00 -0300111 /* If protected TLVs are present they are also hashed. */
112 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200113
Tamas Banfe031092020-09-10 17:32:39 +0200114#ifdef MCUBOOT_RAM_LOAD
115 bootutil_sha256_update(&sha256_ctx,(void*)(hdr->ih_load_addr), size);
116#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117 for (off = 0; off < size; off += blk_sz) {
118 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300119 if (blk_sz > tmp_buf_sz) {
120 blk_sz = tmp_buf_sz;
121 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300122#ifdef MCUBOOT_ENC_IMAGES
123 /* The only data that is encrypted in an image is the payload;
124 * both header and TLVs (when protected) are not.
125 */
126 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
127 /* read only the header */
128 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300129 }
130 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
131 /* read only up to the end of the image payload */
132 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300133 }
134#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
136 if (rc) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900137 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138 return rc;
139 }
Fabio Utzigba829042018-09-18 08:29:34 -0300140#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300141 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300142 /* Only payload is encrypted (area between header and TLVs) */
143 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300144 blk_off = (off - hdr_size) & 0xf;
145 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
146 blk_sz, blk_off, tmp_buf);
147 }
Fabio Utzigba829042018-09-18 08:29:34 -0300148 }
149#endif
David Browne629bf32017-04-24 13:37:33 -0600150 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 }
Tamas Banfe031092020-09-10 17:32:39 +0200152#endif /* MCUBOOT_RAM_LOAD */
David Browne629bf32017-04-24 13:37:33 -0600153 bootutil_sha256_finish(&sha256_ctx, hash_result);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900154 bootutil_sha256_drop(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155
156 return 0;
157}
158
159/*
David Brown43cda332017-09-01 09:53:23 -0600160 * Currently, we only support being able to verify one type of
161 * signature, because there is a single verification function that we
162 * call. List the type of TLV we are expecting. If we aren't
163 * configured for any signature, don't define this macro.
164 */
Fabio Utzig48764842019-05-10 19:28:24 -0300165#if (defined(MCUBOOT_SIGN_RSA) + \
166 defined(MCUBOOT_SIGN_EC) + \
167 defined(MCUBOOT_SIGN_EC256) + \
168 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700169#error "Only a single signature type is supported!"
170#endif
171
David Brown43cda332017-09-01 09:53:23 -0600172#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700173# if MCUBOOT_SIGN_RSA_LEN == 2048
174# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
175# elif MCUBOOT_SIGN_RSA_LEN == 3072
176# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
177# else
178# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600179# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700180# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
181# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600182#elif defined(MCUBOOT_SIGN_EC)
183# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700184# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700185# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600186#elif defined(MCUBOOT_SIGN_EC256)
187# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700188# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700189# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300190#elif defined(MCUBOOT_SIGN_ED25519)
191# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
192# define SIG_BUF_SIZE 64
193# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700194#else
195# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600196#endif
197
198#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200199#if !defined(MCUBOOT_HW_KEY)
David Brown43cda332017-09-01 09:53:23 -0600200static int
201bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
202{
203 bootutil_sha256_context sha256_ctx;
204 int i;
205 const struct bootutil_key *key;
206 uint8_t hash[32];
207
Fabio Utzig15c14672019-08-09 07:45:20 -0300208 if (keyhash_len > 32) {
209 return -1;
210 }
David Brown43cda332017-09-01 09:53:23 -0600211
212 for (i = 0; i < bootutil_key_cnt; i++) {
213 key = &bootutil_keys[i];
214 bootutil_sha256_init(&sha256_ctx);
215 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
216 bootutil_sha256_finish(&sha256_ctx, hash);
217 if (!memcmp(hash, keyhash, keyhash_len)) {
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900218 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600219 return i;
220 }
221 }
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900222 bootutil_sha256_drop(&sha256_ctx);
David Brown43cda332017-09-01 09:53:23 -0600223 return -1;
224}
David Vincze03368b82020-04-01 12:53:53 +0200225#else
226extern unsigned int pub_key_len;
227static int
228bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
229{
230 bootutil_sha256_context sha256_ctx;
231 uint8_t hash[32];
232 uint8_t key_hash[32];
233 size_t key_hash_size = sizeof(key_hash);
234 int rc;
235
236 bootutil_sha256_init(&sha256_ctx);
237 bootutil_sha256_update(&sha256_ctx, key, key_len);
238 bootutil_sha256_finish(&sha256_ctx, hash);
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900239 bootutil_sha256_drop(&sha256_ctx);
David Vincze03368b82020-04-01 12:53:53 +0200240
241 rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
242 if (rc) {
243 return rc;
244 }
245
246 if (!memcmp(hash, key_hash, key_hash_size)) {
247 bootutil_keys[0].key = key;
248 pub_key_len = key_len;
249 return 0;
250 }
251 return -1;
252}
253#endif /* !MCUBOOT_HW_KEY */
David Brown43cda332017-09-01 09:53:23 -0600254#endif
255
David Vinczec3084132020-02-18 14:50:47 +0100256#ifdef MCUBOOT_HW_ROLLBACK_PROT
257/**
258 * Reads the value of an image's security counter.
259 *
260 * @param hdr Pointer to the image header structure.
261 * @param fap Pointer to a description structure of the image's
262 * flash area.
263 * @param security_cnt Pointer to store the security counter value.
264 *
265 * @return 0 on success; nonzero on failure.
266 */
267int32_t
268bootutil_get_img_security_cnt(struct image_header *hdr,
269 const struct flash_area *fap,
270 uint32_t *img_security_cnt)
271{
272 struct image_tlv_iter it;
273 uint32_t off;
274 uint16_t len;
275 int32_t rc;
276
277 if ((hdr == NULL) ||
278 (fap == NULL) ||
279 (img_security_cnt == NULL)) {
280 /* Invalid parameter. */
281 return BOOT_EBADARGS;
282 }
283
284 /* The security counter TLV is in the protected part of the TLV area. */
285 if (hdr->ih_protect_tlv_size == 0) {
286 return BOOT_EBADIMAGE;
287 }
288
289 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
290 if (rc) {
291 return rc;
292 }
293
294 /* Traverse through the protected TLV area to find
295 * the security counter TLV.
296 */
297
298 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
299 if (rc != 0) {
300 /* Security counter TLV has not been found. */
301 return -1;
302 }
303
304 if (len != sizeof(*img_security_cnt)) {
305 /* Security counter is not valid. */
306 return BOOT_EBADIMAGE;
307 }
308
Tamas Banfe031092020-09-10 17:32:39 +0200309 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100310 if (rc != 0) {
311 return BOOT_EFLASH;
312 }
313
314 return 0;
315}
316#endif /* MCUBOOT_HW_ROLLBACK_PROT */
317
David Brown43cda332017-09-01 09:53:23 -0600318/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319 * Verify the integrity of the image.
320 * Return non-zero if image could not be validated/does not validate.
321 */
322int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300323bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
324 struct image_header *hdr, const struct flash_area *fap,
325 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
326 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327{
328 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300329 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700330 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600331 int sha256_valid = 0;
332#ifdef EXPECTED_SIG_TLV
333 int valid_signature = 0;
334 int key_id = -1;
David Vincze03368b82020-04-01 12:53:53 +0200335#ifdef MCUBOOT_HW_KEY
336 /* Few extra bytes for encoding and for public exponent. */
337 uint8_t key_buf[SIG_BUF_SIZE + 24];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338#endif
David Vincze03368b82020-04-01 12:53:53 +0200339#endif /* EXPECTED_SIG_TLV */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300340 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700341 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342 uint8_t hash[32];
343 int rc;
David Vinczec3084132020-02-18 14:50:47 +0100344#ifdef MCUBOOT_HW_ROLLBACK_PROT
345 uint32_t security_cnt = UINT32_MAX;
346 uint32_t img_security_cnt = 0;
347 int32_t security_counter_valid = 0;
348#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349
Fabio Utzig10ee6482019-08-01 12:04:52 -0300350 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
351 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800352 if (rc) {
353 return rc;
354 }
355
356 if (out_hash) {
357 memcpy(out_hash, hash, 32);
358 }
359
Fabio Utzig61fd8882019-09-14 20:00:20 -0300360 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600361 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300362 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600363 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364
David Brown43cda332017-09-01 09:53:23 -0600365 /*
366 * Traverse through all of the TLVs, performing any checks we know
367 * and are able to do.
368 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300369 while (true) {
370 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
371 if (rc < 0) {
372 return -1;
373 } else if (rc > 0) {
374 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 }
David Brown43cda332017-09-01 09:53:23 -0600376
Fabio Utzig61fd8882019-09-14 20:00:20 -0300377 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600378 /*
379 * Verify the SHA256 image hash. This must always be
380 * present.
381 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300382 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 return -1;
384 }
Tamas Banfe031092020-09-10 17:32:39 +0200385 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
David Brown43cda332017-09-01 09:53:23 -0600386 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300387 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388 }
David Brown43cda332017-09-01 09:53:23 -0600389 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300390 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 }
David Brown43cda332017-09-01 09:53:23 -0600392
393 sha256_valid = 1;
394#ifdef EXPECTED_SIG_TLV
David Vincze03368b82020-04-01 12:53:53 +0200395#ifndef MCUBOOT_HW_KEY
Fabio Utzig61fd8882019-09-14 20:00:20 -0300396 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800397 /*
David Brown43cda332017-09-01 09:53:23 -0600398 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300400 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600401 return -1;
402 }
Tamas Banfe031092020-09-10 17:32:39 +0200403 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600404 if (rc) {
405 return rc;
406 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300407 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600408 /*
409 * The key may not be found, which is acceptable. There
410 * can be multiple signatures, each preceded by a key.
411 */
David Vincze03368b82020-04-01 12:53:53 +0200412#else
413 } else if (type == IMAGE_TLV_PUBKEY) {
414 /*
415 * Determine which key we should be checking.
416 */
417 if (len > sizeof(key_buf)) {
418 return -1;
419 }
Tamas Banfe031092020-09-10 17:32:39 +0200420 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
David Vincze03368b82020-04-01 12:53:53 +0200421 if (rc) {
422 return rc;
423 }
424 key_id = bootutil_find_key(image_index, key_buf, len);
425 /*
426 * The key may not be found, which is acceptable. There
427 * can be multiple signatures, each preceded by a key.
428 */
429#endif /* !MCUBOOT_HW_KEY */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300430 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600431 /* Ignore this signature if it is out of bounds. */
432 if (key_id < 0 || key_id >= bootutil_key_cnt) {
433 key_id = -1;
434 continue;
435 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300436 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600437 return -1;
438 }
Tamas Banfe031092020-09-10 17:32:39 +0200439 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600440 if (rc) {
441 return -1;
442 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300443 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600444 if (rc == 0) {
445 valid_signature = 1;
446 }
447 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100448#endif /* EXPECTED_SIG_TLV */
449#ifdef MCUBOOT_HW_ROLLBACK_PROT
450 } else if (type == IMAGE_TLV_SEC_CNT) {
451 /*
452 * Verify the image's security counter.
453 * This must always be present.
454 */
455 if (len != sizeof(img_security_cnt)) {
456 /* Security counter is not valid. */
457 return -1;
458 }
459
Tamas Banfe031092020-09-10 17:32:39 +0200460 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vinczec3084132020-02-18 14:50:47 +0100461 if (rc) {
462 return rc;
463 }
464
465 rc = boot_nv_security_counter_get(image_index, &security_cnt);
466 if (rc) {
467 return rc;
468 }
469
470 /* Compare the new image's security counter value against the
471 * stored security counter value.
472 */
473 if (img_security_cnt < security_cnt) {
474 /* The image's security counter is not accepted. */
475 return -1;
476 }
477
478 /* The image's security counter has been successfully verified. */
479 security_counter_valid = 1;
480#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 }
482 }
David Brown43cda332017-09-01 09:53:23 -0600483
484 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 return -1;
David Brown43cda332017-09-01 09:53:23 -0600486#ifdef EXPECTED_SIG_TLV
David Vinczec3084132020-02-18 14:50:47 +0100487 } else if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489#endif
David Vinczec3084132020-02-18 14:50:47 +0100490#ifdef MCUBOOT_HW_ROLLBACK_PROT
491 } else if (!security_counter_valid) {
492 return -1;
493#endif
494 }
David Brown43cda332017-09-01 09:53:23 -0600495
Christopher Collins92ea77f2016-12-12 15:59:26 -0800496 return 0;
497}