blob: 77b079c0316085f3a961f0894475dadab91a5371 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
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
Oliver Swedef9982442018-08-24 18:37:44 +010020/*
21 * Original code taken from mcuboot project at:
David Vincze39e78552018-10-10 17:10:01 +020022 * https://github.com/JuulLabs-OSS/mcuboot
David Vincze401c7422019-06-21 20:44:05 +020023 * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c
David Vincze39e78552018-10-10 17:10:01 +020024 * Modifications are Copyright (c) 2018-2019 Arm Limited.
Oliver Swedef9982442018-08-24 18:37:44 +010025 */
26
Tamas Banf70ef8c2017-12-19 15:35:09 +000027#include <assert.h>
28#include <stddef.h>
29#include <inttypes.h>
30#include <string.h>
31
Tamas Banf70ef8c2017-12-19 15:35:09 +000032#include "flash_map/flash_map.h"
33#include "bootutil/image.h"
34#include "bootutil/sha256.h"
35#include "bootutil/sign_key.h"
David Vincze060968d2019-05-23 01:13:14 +020036#include "security_cnt.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000037
Tamas Banf70ef8c2017-12-19 15:35:09 +000038#ifdef MCUBOOT_SIGN_RSA
39#include "mbedtls/rsa.h"
40#endif
Tamas Ban581034a2017-12-19 19:54:37 +000041
Tamas Banf70ef8c2017-12-19 15:35:09 +000042#include "mbedtls/asn1.h"
43
44#include "bootutil_priv.h"
45
Tamas Ban1d37c342019-07-11 08:55:55 +010046#ifdef MCUBOOT_HW_KEY
47#include "platform/include/tfm_plat_crypto_keys.h"
48#endif
49
Tamas Banf70ef8c2017-12-19 15:35:09 +000050/*
51 * Compute SHA256 over the image.
52 */
53static int
54bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
55 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
56 uint8_t *hash_result, uint8_t *seed, int seed_len)
57{
58 bootutil_sha256_context sha256_ctx;
59 uint32_t blk_sz;
60 uint32_t size;
61 uint32_t off;
Tamas Banf70ef8c2017-12-19 15:35:09 +000062
63 bootutil_sha256_init(&sha256_ctx);
64
65 /* in some cases (split image) the hash is seeded with data from
66 * the loader image */
Tamas Ban581034a2017-12-19 19:54:37 +000067 if (seed && (seed_len > 0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +000068 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
69 }
70
David Vinczedb32b212019-04-16 17:43:57 +020071 /* Hash is computed over image header and image itself. */
David Vincze39e78552018-10-10 17:10:01 +020072 size = hdr->ih_img_size + hdr->ih_hdr_size;
David Vinczedb32b212019-04-16 17:43:57 +020073
David Vinczebb6e3b62019-07-31 14:24:05 +020074 /* If a security counter TLV and/or a dependency TLV(s) are present then the
75 * TLV info header, the security counter TLV and/or the dependency TLV(s)
76 * are also protected and must be included in the hash calculation.
David Vinczedb32b212019-04-16 17:43:57 +020077 */
78 if (hdr->ih_protect_tlv_size != 0) {
79 size += hdr->ih_protect_tlv_size;
80 }
81
Tamas Banf70ef8c2017-12-19 15:35:09 +000082 for (off = 0; off < size; off += blk_sz) {
83 blk_sz = size - off;
84 if (blk_sz > tmp_buf_sz) {
85 blk_sz = tmp_buf_sz;
86 }
Oliver Swedef9982442018-08-24 18:37:44 +010087
88#ifdef MCUBOOT_RAM_LOADING
89 if (fap == NULL) { /* The image is in SRAM */
90 memcpy(tmp_buf, (uint32_t *)(hdr->ih_load_addr + off), blk_sz);
91 } else { /* The image is in flash */
92#endif
93 if(flash_area_read(fap, off, tmp_buf, blk_sz)) {
94 return -1;
95 }
96#ifdef MCUBOOT_RAM_LOADING
Tamas Banf70ef8c2017-12-19 15:35:09 +000097 }
Oliver Swedef9982442018-08-24 18:37:44 +010098#endif
99
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
101 }
102 bootutil_sha256_finish(&sha256_ctx, hash_result);
103
104 return 0;
105}
106
107/*
108 * Currently, we only support being able to verify one type of
109 * signature, because there is a single verification function that we
110 * call. List the type of TLV we are expecting. If we aren't
111 * configured for any signature, don't define this macro.
112 */
Tamas Ban81daed02019-05-20 15:05:22 +0100113
Tamas Banf70ef8c2017-12-19 15:35:09 +0000114#if defined(MCUBOOT_SIGN_RSA)
Tamas Ban81daed02019-05-20 15:05:22 +0100115# if MCUBOOT_SIGN_RSA_LEN == 2048
116# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
117# elif MCUBOOT_SIGN_RSA_LEN == 3072
118# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
119# else
120# error "Unsupported RSA signature length"
121# endif
122# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
123# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
124#else
125# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000126#endif
127
128#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100129#ifdef MCUBOOT_HW_KEY
130extern unsigned int pub_key_len;
Tamas Ban78676ac2019-07-11 09:05:54 +0100131extern uint8_t current_image;
Tamas Ban1d37c342019-07-11 08:55:55 +0100132static int
133bootutil_find_key(uint8_t *key, uint16_t key_len)
134{
135 bootutil_sha256_context sha256_ctx;
136 uint8_t hash[32];
137 uint8_t key_hash[32];
138 uint32_t key_hash_size= sizeof(key_hash);
139 enum tfm_plat_err_t plat_err;
140
141 bootutil_sha256_init(&sha256_ctx);
142 bootutil_sha256_update(&sha256_ctx, key, key_len);
143 bootutil_sha256_finish(&sha256_ctx, hash);
144
Tamas Ban78676ac2019-07-11 09:05:54 +0100145 plat_err = tfm_plat_get_rotpk_hash(current_image, key_hash, &key_hash_size);
Tamas Ban1d37c342019-07-11 08:55:55 +0100146 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
147 return -1;
148 }
Raef Coles25b857a2019-09-05 13:59:55 +0100149 if (!boot_secure_memequal(hash, key_hash, key_hash_size)) {
Tamas Ban1d37c342019-07-11 08:55:55 +0100150 bootutil_keys[0].key = key;
151 pub_key_len = key_len;
152 return 0;
153 }
154 return -1;
155}
156#else
Tamas Banf70ef8c2017-12-19 15:35:09 +0000157static int
158bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
159{
160 bootutil_sha256_context sha256_ctx;
161 int i;
162 const struct bootutil_key *key;
163 uint8_t hash[32];
164
165 assert(keyhash_len <= 32);
166
167 for (i = 0; i < bootutil_key_cnt; i++) {
168 key = &bootutil_keys[i];
169 bootutil_sha256_init(&sha256_ctx);
170 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
171 bootutil_sha256_finish(&sha256_ctx, hash);
Raef Coles25b857a2019-09-05 13:59:55 +0100172 if (!boot_secure_memequal(hash, keyhash, keyhash_len)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000173 return i;
174 }
175 }
176 return -1;
177}
178#endif
Tamas Ban1d37c342019-07-11 08:55:55 +0100179#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000180
Oliver Swedef9982442018-08-24 18:37:44 +0100181#ifdef MCUBOOT_RAM_LOADING
182/* Check the hash of an image after it has been copied to SRAM */
183int
184bootutil_check_hash_after_loading(struct image_header *hdr)
185{
186 uint32_t off;
187 uint32_t end;
188 int sha256_valid = 0;
189 struct image_tlv_info info;
190 struct image_tlv tlv;
191 uint8_t tmp_buf[BOOT_TMPBUF_SZ];
192 uint8_t hash[32] = {0};
193 int rc;
194 uint32_t load_address;
195 uint32_t tlv_sz;
196
197 rc = bootutil_img_hash(hdr, NULL, tmp_buf, BOOT_TMPBUF_SZ, hash, NULL, 0);
198
199 if (rc) {
200 return rc;
201 }
202
203 load_address = (uint32_t) hdr->ih_load_addr;
204
205 /* The TLVs come after the image. */
206 off = hdr->ih_img_size + hdr->ih_hdr_size;
207
208 info = *((struct image_tlv_info *)(load_address + off));
209
210 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200211 return BOOT_EBADMAGIC;
Oliver Swedef9982442018-08-24 18:37:44 +0100212 }
Tamas Ban056ed0b2019-09-16 12:48:32 +0100213 if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) {
214 return -1;
215 }
Oliver Swedef9982442018-08-24 18:37:44 +0100216 end = off + info.it_tlv_tot;
217 off += sizeof(info);
218
219 /*
220 * Traverse through all of the TLVs, performing any checks we know
221 * and are able to do.
222 */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100223 if (boot_add_uint32_overflow_check(load_address, end)) {
224 return -1;
225 }
David Vincze060968d2019-05-23 01:13:14 +0200226 while (off < end) {
Oliver Swedef9982442018-08-24 18:37:44 +0100227 tlv = *((struct image_tlv *)(load_address + off));
228 tlv_sz = sizeof(tlv);
229
230 if (tlv.it_type == IMAGE_TLV_SHA256) {
231 /*
232 * Verify the SHA256 image hash. This must always be present.
233 */
234 if (tlv.it_len != sizeof(hash)) {
235 return -1;
236 }
237
Raef Coles25b857a2019-09-05 13:59:55 +0100238 if (boot_secure_memequal(hash, (uint32_t *)(load_address + off + tlv_sz),
Oliver Swedef9982442018-08-24 18:37:44 +0100239 sizeof(hash))) {
240 return -1;
241 }
242
243 sha256_valid = 1;
Tamas Ban576aaf92019-09-23 14:24:56 +0100244 break;
Oliver Swedef9982442018-08-24 18:37:44 +0100245 }
David Vincze060968d2019-05-23 01:13:14 +0200246
247 /* Avoid integer overflow. */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100248 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) {
David Vincze060968d2019-05-23 01:13:14 +0200249 /* Potential overflow. */
250 break;
251 } else {
252 off += sizeof(tlv) + tlv.it_len;
253 }
Oliver Swedef9982442018-08-24 18:37:44 +0100254 }
255
256 if (!sha256_valid) {
257 return -1;
258 }
259
260 return 0;
261}
262#endif /* MCUBOOT_RAM_LOADING */
263
David Vincze060968d2019-05-23 01:13:14 +0200264/**
265 * Reads the value of an image's security counter.
266 *
267 * @param hdr Pointer to the image header structure.
268 * @param fap Pointer to a description structure of the image's
269 * flash area.
270 * @param security_cnt Pointer to store the security counter value.
271 *
272 * @return 0 on success; nonzero on failure.
273 */
274int32_t
275bootutil_get_img_security_cnt(struct image_header *hdr,
276 const struct flash_area *fap,
277 uint32_t *img_security_cnt)
278{
279 struct image_tlv_info info;
280 struct image_tlv tlv;
281 uint32_t off;
282 uint32_t end;
283 uint32_t found = 0;
284 int32_t rc;
285
286 if ((hdr == NULL) ||
287 (fap == NULL) ||
288 (img_security_cnt == NULL)) {
289 /* Invalid parameter. */
290 return BOOT_EBADARGS;
291 }
292
293 /* The TLVs come after the image. */
294 off = hdr->ih_hdr_size + hdr->ih_img_size;
295
296 /* The TLV area always starts with an image_tlv_info structure. */
297 rc = flash_area_read(fap, off, &info, sizeof(info));
298 if (rc != 0) {
299 return BOOT_EFLASH;
300 }
301
302 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
303 return BOOT_EBADMAGIC;
304 }
305
306 /* The security counter TLV is in the protected part of the TLV area. */
307 if (hdr->ih_protect_tlv_size != 0) {
308 end = off + (uint32_t)hdr->ih_protect_tlv_size;
309 off += sizeof(info);
310
311 /* Traverse through the protected TLV area to find the
312 * security counter TLV.
313 */
314 while (off < end) {
315 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
316 if (rc != 0) {
317 return BOOT_EFLASH;
318 }
319
320 if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
321
322 if (tlv.it_len != sizeof(*img_security_cnt)) {
323 /* Security counter is not valid. */
324 break;
325 }
326
327 rc = flash_area_read(fap, off + sizeof(tlv),
328 img_security_cnt, tlv.it_len);
329 if (rc != 0) {
330 return BOOT_EFLASH;
331 }
332
333 /* Security counter has been found. */
334 found = 1;
335 break;
336 }
337
338 /* Avoid integer overflow. */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100339 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len)))
340 {
David Vincze060968d2019-05-23 01:13:14 +0200341 /* Potential overflow. */
342 break;
343 } else {
344 off += sizeof(tlv) + tlv.it_len;
345 }
346 }
347 }
348
349 if (found) {
350 return 0;
351 }
352
353 return -1;
354}
355
Tamas Banf70ef8c2017-12-19 15:35:09 +0000356/*
357 * Verify the integrity of the image.
358 * Return non-zero if image could not be validated/does not validate.
359 */
360int
361bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
362 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
363 uint8_t *seed, int seed_len, uint8_t *out_hash)
364{
365 uint32_t off;
366 uint32_t end;
367 int sha256_valid = 0;
368 struct image_tlv_info info;
369#ifdef EXPECTED_SIG_TLV
370 int valid_signature = 0;
371 int key_id = -1;
Tamas Ban1d37c342019-07-11 08:55:55 +0100372#ifdef MCUBOOT_HW_KEY
373 /* Few extra bytes for encoding and for public exponent */
374 uint8_t key_buf[SIG_BUF_SIZE + 24];
375#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000376#endif
377 struct image_tlv tlv;
Tamas Ban81daed02019-05-20 15:05:22 +0100378 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000379 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200380 uint32_t security_cnt;
381 uint32_t img_security_cnt;
382 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000383 int rc;
384
385 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
386 seed, seed_len);
387 if (rc) {
388 return rc;
389 }
390
391 if (out_hash) {
392 memcpy(out_hash, hash, 32);
393 }
394
395 /* The TLVs come after the image. */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000396 off = hdr->ih_img_size + hdr->ih_hdr_size;
397
398 rc = flash_area_read(fap, off, &info, sizeof(info));
399 if (rc) {
400 return rc;
401 }
402 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200403 return BOOT_EBADMAGIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000404 }
Tamas Ban056ed0b2019-09-16 12:48:32 +0100405 if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) {
406 return -1;
407 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000408 end = off + info.it_tlv_tot;
409 off += sizeof(info);
410
411 /*
412 * Traverse through all of the TLVs, performing any checks we know
413 * and are able to do.
414 */
David Vincze060968d2019-05-23 01:13:14 +0200415 while (off < end) {
Tamas Ban581034a2017-12-19 19:54:37 +0000416 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000417 if (rc) {
418 return rc;
419 }
420
421 if (tlv.it_type == IMAGE_TLV_SHA256) {
422 /*
423 * Verify the SHA256 image hash. This must always be
424 * present.
425 */
426 if (tlv.it_len != sizeof(hash)) {
427 return -1;
428 }
Tamas Ban581034a2017-12-19 19:54:37 +0000429 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000430 if (rc) {
431 return rc;
432 }
Raef Coles25b857a2019-09-05 13:59:55 +0100433 if (boot_secure_memequal(hash, buf, sizeof(hash))) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000434 return -1;
435 }
436
437 sha256_valid = 1;
438#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100439#ifndef MCUBOOT_HW_KEY
Tamas Banf70ef8c2017-12-19 15:35:09 +0000440 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
441 /*
442 * Determine which key we should be checking.
443 */
444 if (tlv.it_len > 32) {
445 return -1;
446 }
Tamas Ban581034a2017-12-19 19:54:37 +0000447 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000448 if (rc) {
449 return rc;
450 }
451 key_id = bootutil_find_key(buf, tlv.it_len);
452 /*
453 * The key may not be found, which is acceptable. There
454 * can be multiple signatures, each preceded by a key.
455 */
Tamas Ban1d37c342019-07-11 08:55:55 +0100456#else
457 } else if (tlv.it_type == IMAGE_TLV_KEY) {
458 /*
459 * Determine which key we should be checking.
460 */
461 if (tlv.it_len > sizeof(key_buf)) {
462 return -1;
463 }
464 rc = flash_area_read(fap, off + sizeof(tlv), key_buf, tlv.it_len);
465 if (rc) {
466 return rc;
467 }
468 key_id = bootutil_find_key(key_buf, tlv.it_len);
469 /*
470 * The key may not be found, which is acceptable. There
471 * can be multiple signatures, each preceded by a key.
472 */
473#endif /* MCUBOOT_HW_KEY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000474 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
475 /* Ignore this signature if it is out of bounds. */
David Vinczefb068302019-07-24 11:00:22 +0200476 if (key_id >= 0 && key_id < bootutil_key_cnt) {
477 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
478 return -1;
479 }
480 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
481 if (rc) {
482 return -1;
483 }
484 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len,
485 key_id);
486 if (rc == 0) {
487 valid_signature = 1;
488 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000489 }
490 key_id = -1;
491#endif
David Vincze060968d2019-05-23 01:13:14 +0200492 } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
493 /*
494 * Verify the image's security counter.
495 * This must always be present.
496 */
497 if (tlv.it_len != sizeof(img_security_cnt)) {
498 /* Security counter is not valid. */
499 return -1;
500 }
501
502 rc = flash_area_read(fap, off + sizeof(tlv),
503 &img_security_cnt, tlv.it_len);
504 if (rc) {
505 return rc;
506 }
507
508 rc = boot_nv_security_counter_get(0, &security_cnt);
509 if (rc) {
510 return rc;
511 }
512
513 /* Compare the new image's security counter value against the
514 * stored security counter value.
515 */
516 if (img_security_cnt < security_cnt) {
517 /* The image's security counter is not accepted. */
518 return -1;
519 }
520
521 /* The image's security counter has been successfully verified. */
522 security_counter_valid = 1;
523 }
524
525 /* Avoid integer overflow. */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100526 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) {
David Vincze060968d2019-05-23 01:13:14 +0200527 /* Potential overflow. */
528 break;
529 } else {
530 off += sizeof(tlv) + tlv.it_len;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000531 }
532 }
533
David Vincze060968d2019-05-23 01:13:14 +0200534 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000535 return -1;
536 }
537
538#ifdef EXPECTED_SIG_TLV
539 if (!valid_signature) {
540 return -1;
541 }
542#endif
543
544 return 0;
545}