blob: 931f459a8ffaa0258754758500308af3b60ee365 [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 }
149 if (!memcmp(hash, key_hash, key_hash_size)) {
150 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);
172 if (!memcmp(hash, keyhash, keyhash_len)) {
173 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 }
213 end = off + info.it_tlv_tot;
214 off += sizeof(info);
215
216 /*
217 * Traverse through all of the TLVs, performing any checks we know
218 * and are able to do.
219 */
David Vincze060968d2019-05-23 01:13:14 +0200220 while (off < end) {
Oliver Swedef9982442018-08-24 18:37:44 +0100221 tlv = *((struct image_tlv *)(load_address + off));
222 tlv_sz = sizeof(tlv);
223
224 if (tlv.it_type == IMAGE_TLV_SHA256) {
225 /*
226 * Verify the SHA256 image hash. This must always be present.
227 */
228 if (tlv.it_len != sizeof(hash)) {
229 return -1;
230 }
231
232 if (memcmp(hash, (uint32_t *)(load_address + off + tlv_sz),
233 sizeof(hash))) {
234 return -1;
235 }
236
237 sha256_valid = 1;
238 }
David Vincze060968d2019-05-23 01:13:14 +0200239
240 /* Avoid integer overflow. */
241 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
242 /* Potential overflow. */
243 break;
244 } else {
245 off += sizeof(tlv) + tlv.it_len;
246 }
Oliver Swedef9982442018-08-24 18:37:44 +0100247 }
248
249 if (!sha256_valid) {
250 return -1;
251 }
252
253 return 0;
254}
255#endif /* MCUBOOT_RAM_LOADING */
256
David Vincze060968d2019-05-23 01:13:14 +0200257/**
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_info info;
273 struct image_tlv tlv;
274 uint32_t off;
275 uint32_t end;
276 uint32_t found = 0;
277 int32_t rc;
278
279 if ((hdr == NULL) ||
280 (fap == NULL) ||
281 (img_security_cnt == NULL)) {
282 /* Invalid parameter. */
283 return BOOT_EBADARGS;
284 }
285
286 /* The TLVs come after the image. */
287 off = hdr->ih_hdr_size + hdr->ih_img_size;
288
289 /* The TLV area always starts with an image_tlv_info structure. */
290 rc = flash_area_read(fap, off, &info, sizeof(info));
291 if (rc != 0) {
292 return BOOT_EFLASH;
293 }
294
295 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
296 return BOOT_EBADMAGIC;
297 }
298
299 /* The security counter TLV is in the protected part of the TLV area. */
300 if (hdr->ih_protect_tlv_size != 0) {
301 end = off + (uint32_t)hdr->ih_protect_tlv_size;
302 off += sizeof(info);
303
304 /* Traverse through the protected TLV area to find the
305 * security counter TLV.
306 */
307 while (off < end) {
308 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
309 if (rc != 0) {
310 return BOOT_EFLASH;
311 }
312
313 if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
314
315 if (tlv.it_len != sizeof(*img_security_cnt)) {
316 /* Security counter is not valid. */
317 break;
318 }
319
320 rc = flash_area_read(fap, off + sizeof(tlv),
321 img_security_cnt, tlv.it_len);
322 if (rc != 0) {
323 return BOOT_EFLASH;
324 }
325
326 /* Security counter has been found. */
327 found = 1;
328 break;
329 }
330
331 /* Avoid integer overflow. */
332 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
333 /* Potential overflow. */
334 break;
335 } else {
336 off += sizeof(tlv) + tlv.it_len;
337 }
338 }
339 }
340
341 if (found) {
342 return 0;
343 }
344
345 return -1;
346}
347
Tamas Banf70ef8c2017-12-19 15:35:09 +0000348/*
349 * Verify the integrity of the image.
350 * Return non-zero if image could not be validated/does not validate.
351 */
352int
353bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
354 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
355 uint8_t *seed, int seed_len, uint8_t *out_hash)
356{
357 uint32_t off;
358 uint32_t end;
359 int sha256_valid = 0;
360 struct image_tlv_info info;
361#ifdef EXPECTED_SIG_TLV
362 int valid_signature = 0;
363 int key_id = -1;
Tamas Ban1d37c342019-07-11 08:55:55 +0100364#ifdef MCUBOOT_HW_KEY
365 /* Few extra bytes for encoding and for public exponent */
366 uint8_t key_buf[SIG_BUF_SIZE + 24];
367#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000368#endif
369 struct image_tlv tlv;
Tamas Ban81daed02019-05-20 15:05:22 +0100370 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000371 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200372 uint32_t security_cnt;
373 uint32_t img_security_cnt;
374 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000375 int rc;
376
377 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
378 seed, seed_len);
379 if (rc) {
380 return rc;
381 }
382
383 if (out_hash) {
384 memcpy(out_hash, hash, 32);
385 }
386
387 /* The TLVs come after the image. */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000388 off = hdr->ih_img_size + hdr->ih_hdr_size;
389
390 rc = flash_area_read(fap, off, &info, sizeof(info));
391 if (rc) {
392 return rc;
393 }
394 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200395 return BOOT_EBADMAGIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000396 }
397 end = off + info.it_tlv_tot;
398 off += sizeof(info);
399
400 /*
401 * Traverse through all of the TLVs, performing any checks we know
402 * and are able to do.
403 */
David Vincze060968d2019-05-23 01:13:14 +0200404 while (off < end) {
Tamas Ban581034a2017-12-19 19:54:37 +0000405 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000406 if (rc) {
407 return rc;
408 }
409
410 if (tlv.it_type == IMAGE_TLV_SHA256) {
411 /*
412 * Verify the SHA256 image hash. This must always be
413 * present.
414 */
415 if (tlv.it_len != sizeof(hash)) {
416 return -1;
417 }
Tamas Ban581034a2017-12-19 19:54:37 +0000418 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000419 if (rc) {
420 return rc;
421 }
422 if (memcmp(hash, buf, sizeof(hash))) {
423 return -1;
424 }
425
426 sha256_valid = 1;
427#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100428#ifndef MCUBOOT_HW_KEY
Tamas Banf70ef8c2017-12-19 15:35:09 +0000429 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
430 /*
431 * Determine which key we should be checking.
432 */
433 if (tlv.it_len > 32) {
434 return -1;
435 }
Tamas Ban581034a2017-12-19 19:54:37 +0000436 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000437 if (rc) {
438 return rc;
439 }
440 key_id = bootutil_find_key(buf, tlv.it_len);
441 /*
442 * The key may not be found, which is acceptable. There
443 * can be multiple signatures, each preceded by a key.
444 */
Tamas Ban1d37c342019-07-11 08:55:55 +0100445#else
446 } else if (tlv.it_type == IMAGE_TLV_KEY) {
447 /*
448 * Determine which key we should be checking.
449 */
450 if (tlv.it_len > sizeof(key_buf)) {
451 return -1;
452 }
453 rc = flash_area_read(fap, off + sizeof(tlv), key_buf, tlv.it_len);
454 if (rc) {
455 return rc;
456 }
457 key_id = bootutil_find_key(key_buf, tlv.it_len);
458 /*
459 * The key may not be found, which is acceptable. There
460 * can be multiple signatures, each preceded by a key.
461 */
462#endif /* MCUBOOT_HW_KEY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000463 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
464 /* Ignore this signature if it is out of bounds. */
David Vinczefb068302019-07-24 11:00:22 +0200465 if (key_id >= 0 && key_id < bootutil_key_cnt) {
466 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
467 return -1;
468 }
469 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
470 if (rc) {
471 return -1;
472 }
473 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len,
474 key_id);
475 if (rc == 0) {
476 valid_signature = 1;
477 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000478 }
479 key_id = -1;
480#endif
David Vincze060968d2019-05-23 01:13:14 +0200481 } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
482 /*
483 * Verify the image's security counter.
484 * This must always be present.
485 */
486 if (tlv.it_len != sizeof(img_security_cnt)) {
487 /* Security counter is not valid. */
488 return -1;
489 }
490
491 rc = flash_area_read(fap, off + sizeof(tlv),
492 &img_security_cnt, tlv.it_len);
493 if (rc) {
494 return rc;
495 }
496
497 rc = boot_nv_security_counter_get(0, &security_cnt);
498 if (rc) {
499 return rc;
500 }
501
502 /* Compare the new image's security counter value against the
503 * stored security counter value.
504 */
505 if (img_security_cnt < security_cnt) {
506 /* The image's security counter is not accepted. */
507 return -1;
508 }
509
510 /* The image's security counter has been successfully verified. */
511 security_counter_valid = 1;
512 }
513
514 /* Avoid integer overflow. */
515 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
516 /* Potential overflow. */
517 break;
518 } else {
519 off += sizeof(tlv) + tlv.it_len;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000520 }
521 }
522
David Vincze060968d2019-05-23 01:13:14 +0200523 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000524 return -1;
525 }
526
527#ifdef EXPECTED_SIG_TLV
528 if (!valid_signature) {
529 return -1;
530 }
531#endif
532
533 return 0;
534}