blob: 4390306d0447ae27b1f22aee8d8209502f487723 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee32483f2019-06-13 10:46:24 +020020/*
David Vinczec3084132020-02-18 14:50:47 +010021 * Modifications are Copyright (c) 2019-2020 Arm Limited.
David Vinczee32483f2019-06-13 10:46:24 +020022 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include <stddef.h>
David Vinczec3084132020-02-18 14:50:47 +010025#include <stdint.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080026#include <inttypes.h>
27#include <string.h>
28
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020029#include <flash_map_backend/flash_map_backend.h>
30
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060032#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "bootutil/sign_key.h"
David Vinczec3084132020-02-18 14:50:47 +010034#include "bootutil/security_cnt.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080035
Fabio Utzigba1fbe62017-07-21 14:01:20 -030036#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030037
Fabio Utzigba829042018-09-18 08:29:34 -030038#ifdef MCUBOOT_ENC_IMAGES
39#include "bootutil/enc_key.h"
40#endif
41#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080042#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060043#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030044#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060046#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080047#include "mbedtls/asn1.h"
48
49#include "bootutil_priv.h"
50
51/*
52 * Compute SHA256 over the image.
53 */
54static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030055bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
56 struct image_header *hdr, const struct flash_area *fap,
57 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
58 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080059{
David Browne629bf32017-04-24 13:37:33 -060060 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061 uint32_t blk_sz;
62 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020063 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080064 uint32_t off;
65 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030066 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030067 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080068
Fabio Utzigb0f04732019-07-31 09:49:19 -030069#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030070 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030071 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030072 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030073 (void)blk_off;
74 (void)tlv_off;
Fabio Utzigb0f04732019-07-31 09:49:19 -030075#endif
76
Fabio Utzigbc077932019-08-26 11:16:34 -030077#ifdef MCUBOOT_ENC_IMAGES
78 /* Encrypted images only exist in the secondary slot */
79 if (MUST_DECRYPT(fap, image_index, hdr) &&
80 !boot_enc_valid(enc_state, image_index, fap)) {
81 return -1;
82 }
83#endif
84
David Browne629bf32017-04-24 13:37:33 -060085 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080086
87 /* in some cases (split image) the hash is seeded with data from
88 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020089 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060090 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 }
92
David Vinczee32483f2019-06-13 10:46:24 +020093 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -030094 size = hdr_size = hdr->ih_hdr_size;
95 size += hdr->ih_img_size;
96 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +020097
Fabio Utzige52c08e2019-09-11 19:32:00 -030098 /* If protected TLVs are present they are also hashed. */
99 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +0200100
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101 for (off = 0; off < size; off += blk_sz) {
102 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300103 if (blk_sz > tmp_buf_sz) {
104 blk_sz = tmp_buf_sz;
105 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300106#ifdef MCUBOOT_ENC_IMAGES
107 /* The only data that is encrypted in an image is the payload;
108 * both header and TLVs (when protected) are not.
109 */
110 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
111 /* read only the header */
112 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300113 }
114 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
115 /* read only up to the end of the image payload */
116 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300117 }
118#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
120 if (rc) {
121 return rc;
122 }
Fabio Utzigba829042018-09-18 08:29:34 -0300123#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300124 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300125 /* Only payload is encrypted (area between header and TLVs) */
126 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300127 blk_off = (off - hdr_size) & 0xf;
128 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
129 blk_sz, blk_off, tmp_buf);
130 }
Fabio Utzigba829042018-09-18 08:29:34 -0300131 }
132#endif
David Browne629bf32017-04-24 13:37:33 -0600133 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134 }
David Browne629bf32017-04-24 13:37:33 -0600135 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800136
137 return 0;
138}
139
140/*
David Brown43cda332017-09-01 09:53:23 -0600141 * Currently, we only support being able to verify one type of
142 * signature, because there is a single verification function that we
143 * call. List the type of TLV we are expecting. If we aren't
144 * configured for any signature, don't define this macro.
145 */
Fabio Utzig48764842019-05-10 19:28:24 -0300146#if (defined(MCUBOOT_SIGN_RSA) + \
147 defined(MCUBOOT_SIGN_EC) + \
148 defined(MCUBOOT_SIGN_EC256) + \
149 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700150#error "Only a single signature type is supported!"
151#endif
152
David Brown43cda332017-09-01 09:53:23 -0600153#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700154# if MCUBOOT_SIGN_RSA_LEN == 2048
155# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
156# elif MCUBOOT_SIGN_RSA_LEN == 3072
157# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
158# else
159# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600160# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700161# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
162# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600163#elif defined(MCUBOOT_SIGN_EC)
164# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700165# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700166# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
David Brown43cda332017-09-01 09:53:23 -0600167#elif defined(MCUBOOT_SIGN_EC256)
168# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700169# define SIG_BUF_SIZE 128
David Browna3608262019-12-12 15:35:31 -0700170# define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */
Fabio Utzig48764842019-05-10 19:28:24 -0300171#elif defined(MCUBOOT_SIGN_ED25519)
172# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
173# define SIG_BUF_SIZE 64
174# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700175#else
176# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600177#endif
178
179#ifdef EXPECTED_SIG_TLV
180static int
181bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
182{
183 bootutil_sha256_context sha256_ctx;
184 int i;
185 const struct bootutil_key *key;
186 uint8_t hash[32];
187
Fabio Utzig15c14672019-08-09 07:45:20 -0300188 if (keyhash_len > 32) {
189 return -1;
190 }
David Brown43cda332017-09-01 09:53:23 -0600191
192 for (i = 0; i < bootutil_key_cnt; i++) {
193 key = &bootutil_keys[i];
194 bootutil_sha256_init(&sha256_ctx);
195 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
196 bootutil_sha256_finish(&sha256_ctx, hash);
197 if (!memcmp(hash, keyhash, keyhash_len)) {
198 return i;
199 }
200 }
201 return -1;
202}
203#endif
204
David Vinczec3084132020-02-18 14:50:47 +0100205#ifdef MCUBOOT_HW_ROLLBACK_PROT
206/**
207 * Reads the value of an image's security counter.
208 *
209 * @param hdr Pointer to the image header structure.
210 * @param fap Pointer to a description structure of the image's
211 * flash area.
212 * @param security_cnt Pointer to store the security counter value.
213 *
214 * @return 0 on success; nonzero on failure.
215 */
216int32_t
217bootutil_get_img_security_cnt(struct image_header *hdr,
218 const struct flash_area *fap,
219 uint32_t *img_security_cnt)
220{
221 struct image_tlv_iter it;
222 uint32_t off;
223 uint16_t len;
224 int32_t rc;
225
226 if ((hdr == NULL) ||
227 (fap == NULL) ||
228 (img_security_cnt == NULL)) {
229 /* Invalid parameter. */
230 return BOOT_EBADARGS;
231 }
232
233 /* The security counter TLV is in the protected part of the TLV area. */
234 if (hdr->ih_protect_tlv_size == 0) {
235 return BOOT_EBADIMAGE;
236 }
237
238 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
239 if (rc) {
240 return rc;
241 }
242
243 /* Traverse through the protected TLV area to find
244 * the security counter TLV.
245 */
246
247 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
248 if (rc != 0) {
249 /* Security counter TLV has not been found. */
250 return -1;
251 }
252
253 if (len != sizeof(*img_security_cnt)) {
254 /* Security counter is not valid. */
255 return BOOT_EBADIMAGE;
256 }
257
258 rc = flash_area_read(fap, off, img_security_cnt, len);
259 if (rc != 0) {
260 return BOOT_EFLASH;
261 }
262
263 return 0;
264}
265#endif /* MCUBOOT_HW_ROLLBACK_PROT */
266
David Brown43cda332017-09-01 09:53:23 -0600267/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268 * Verify the integrity of the image.
269 * Return non-zero if image could not be validated/does not validate.
270 */
271int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300272bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
273 struct image_header *hdr, const struct flash_area *fap,
274 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
275 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276{
277 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300278 uint16_t len;
David Brownd13318a2019-12-04 17:28:40 -0700279 uint16_t type;
David Brown43cda332017-09-01 09:53:23 -0600280 int sha256_valid = 0;
281#ifdef EXPECTED_SIG_TLV
282 int valid_signature = 0;
283 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284#endif
Fabio Utzig61fd8882019-09-14 20:00:20 -0300285 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700286 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287 uint8_t hash[32];
288 int rc;
David Vinczec3084132020-02-18 14:50:47 +0100289#ifdef MCUBOOT_HW_ROLLBACK_PROT
290 uint32_t security_cnt = UINT32_MAX;
291 uint32_t img_security_cnt = 0;
292 int32_t security_counter_valid = 0;
293#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294
Fabio Utzig10ee6482019-08-01 12:04:52 -0300295 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
296 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297 if (rc) {
298 return rc;
299 }
300
301 if (out_hash) {
302 memcpy(out_hash, hash, 32);
303 }
304
Fabio Utzig61fd8882019-09-14 20:00:20 -0300305 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600306 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300307 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600308 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309
David Brown43cda332017-09-01 09:53:23 -0600310 /*
311 * Traverse through all of the TLVs, performing any checks we know
312 * and are able to do.
313 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300314 while (true) {
315 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
316 if (rc < 0) {
317 return -1;
318 } else if (rc > 0) {
319 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320 }
David Brown43cda332017-09-01 09:53:23 -0600321
Fabio Utzig61fd8882019-09-14 20:00:20 -0300322 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600323 /*
324 * Verify the SHA256 image hash. This must always be
325 * present.
326 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300327 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800328 return -1;
329 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300330 rc = flash_area_read(fap, off, buf, sizeof hash);
David Brown43cda332017-09-01 09:53:23 -0600331 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300332 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333 }
David Brown43cda332017-09-01 09:53:23 -0600334 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300335 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800336 }
David Brown43cda332017-09-01 09:53:23 -0600337
338 sha256_valid = 1;
339#ifdef EXPECTED_SIG_TLV
Fabio Utzig61fd8882019-09-14 20:00:20 -0300340 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 /*
David Brown43cda332017-09-01 09:53:23 -0600342 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800343 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300344 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600345 return -1;
346 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300347 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600348 if (rc) {
349 return rc;
350 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300351 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600352 /*
353 * The key may not be found, which is acceptable. There
354 * can be multiple signatures, each preceded by a key.
355 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300356 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600357 /* Ignore this signature if it is out of bounds. */
358 if (key_id < 0 || key_id >= bootutil_key_cnt) {
359 key_id = -1;
360 continue;
361 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300362 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600363 return -1;
364 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300365 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600366 if (rc) {
367 return -1;
368 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300369 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600370 if (rc == 0) {
371 valid_signature = 1;
372 }
373 key_id = -1;
David Vinczec3084132020-02-18 14:50:47 +0100374#endif /* EXPECTED_SIG_TLV */
375#ifdef MCUBOOT_HW_ROLLBACK_PROT
376 } else if (type == IMAGE_TLV_SEC_CNT) {
377 /*
378 * Verify the image's security counter.
379 * This must always be present.
380 */
381 if (len != sizeof(img_security_cnt)) {
382 /* Security counter is not valid. */
383 return -1;
384 }
385
386 rc = flash_area_read(fap, off, &img_security_cnt, len);
387 if (rc) {
388 return rc;
389 }
390
391 rc = boot_nv_security_counter_get(image_index, &security_cnt);
392 if (rc) {
393 return rc;
394 }
395
396 /* Compare the new image's security counter value against the
397 * stored security counter value.
398 */
399 if (img_security_cnt < security_cnt) {
400 /* The image's security counter is not accepted. */
401 return -1;
402 }
403
404 /* The image's security counter has been successfully verified. */
405 security_counter_valid = 1;
406#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407 }
408 }
David Brown43cda332017-09-01 09:53:23 -0600409
410 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411 return -1;
David Brown43cda332017-09-01 09:53:23 -0600412#ifdef EXPECTED_SIG_TLV
David Vinczec3084132020-02-18 14:50:47 +0100413 } else if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415#endif
David Vinczec3084132020-02-18 14:50:47 +0100416#ifdef MCUBOOT_HW_ROLLBACK_PROT
417 } else if (!security_counter_valid) {
418 return -1;
419#endif
420 }
David Brown43cda332017-09-01 09:53:23 -0600421
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422 return 0;
423}