blob: e62455ca7ae5d24d968ae17c52048ddae4c80a20 [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 }
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
Raef Coles25b857a2019-09-05 13:59:55 +0100232 if (boot_secure_memequal(hash, (uint32_t *)(load_address + off + tlv_sz),
Oliver Swedef9982442018-08-24 18:37:44 +0100233 sizeof(hash))) {
234 return -1;
235 }
236
237 sha256_valid = 1;
Tamas Ban576aaf92019-09-23 14:24:56 +0100238 break;
Oliver Swedef9982442018-08-24 18:37:44 +0100239 }
David Vincze060968d2019-05-23 01:13:14 +0200240
241 /* Avoid integer overflow. */
242 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
243 /* Potential overflow. */
244 break;
245 } else {
246 off += sizeof(tlv) + tlv.it_len;
247 }
Oliver Swedef9982442018-08-24 18:37:44 +0100248 }
249
250 if (!sha256_valid) {
251 return -1;
252 }
253
254 return 0;
255}
256#endif /* MCUBOOT_RAM_LOADING */
257
David Vincze060968d2019-05-23 01:13:14 +0200258/**
259 * Reads the value of an image's security counter.
260 *
261 * @param hdr Pointer to the image header structure.
262 * @param fap Pointer to a description structure of the image's
263 * flash area.
264 * @param security_cnt Pointer to store the security counter value.
265 *
266 * @return 0 on success; nonzero on failure.
267 */
268int32_t
269bootutil_get_img_security_cnt(struct image_header *hdr,
270 const struct flash_area *fap,
271 uint32_t *img_security_cnt)
272{
273 struct image_tlv_info info;
274 struct image_tlv tlv;
275 uint32_t off;
276 uint32_t end;
277 uint32_t found = 0;
278 int32_t rc;
279
280 if ((hdr == NULL) ||
281 (fap == NULL) ||
282 (img_security_cnt == NULL)) {
283 /* Invalid parameter. */
284 return BOOT_EBADARGS;
285 }
286
287 /* The TLVs come after the image. */
288 off = hdr->ih_hdr_size + hdr->ih_img_size;
289
290 /* The TLV area always starts with an image_tlv_info structure. */
291 rc = flash_area_read(fap, off, &info, sizeof(info));
292 if (rc != 0) {
293 return BOOT_EFLASH;
294 }
295
296 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
297 return BOOT_EBADMAGIC;
298 }
299
300 /* The security counter TLV is in the protected part of the TLV area. */
301 if (hdr->ih_protect_tlv_size != 0) {
302 end = off + (uint32_t)hdr->ih_protect_tlv_size;
303 off += sizeof(info);
304
305 /* Traverse through the protected TLV area to find the
306 * security counter TLV.
307 */
308 while (off < end) {
309 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
310 if (rc != 0) {
311 return BOOT_EFLASH;
312 }
313
314 if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
315
316 if (tlv.it_len != sizeof(*img_security_cnt)) {
317 /* Security counter is not valid. */
318 break;
319 }
320
321 rc = flash_area_read(fap, off + sizeof(tlv),
322 img_security_cnt, tlv.it_len);
323 if (rc != 0) {
324 return BOOT_EFLASH;
325 }
326
327 /* Security counter has been found. */
328 found = 1;
329 break;
330 }
331
332 /* Avoid integer overflow. */
333 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
334 /* Potential overflow. */
335 break;
336 } else {
337 off += sizeof(tlv) + tlv.it_len;
338 }
339 }
340 }
341
342 if (found) {
343 return 0;
344 }
345
346 return -1;
347}
348
Tamas Banf70ef8c2017-12-19 15:35:09 +0000349/*
350 * Verify the integrity of the image.
351 * Return non-zero if image could not be validated/does not validate.
352 */
353int
354bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
355 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
356 uint8_t *seed, int seed_len, uint8_t *out_hash)
357{
358 uint32_t off;
359 uint32_t end;
360 int sha256_valid = 0;
361 struct image_tlv_info info;
362#ifdef EXPECTED_SIG_TLV
363 int valid_signature = 0;
364 int key_id = -1;
Tamas Ban1d37c342019-07-11 08:55:55 +0100365#ifdef MCUBOOT_HW_KEY
366 /* Few extra bytes for encoding and for public exponent */
367 uint8_t key_buf[SIG_BUF_SIZE + 24];
368#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000369#endif
370 struct image_tlv tlv;
Tamas Ban81daed02019-05-20 15:05:22 +0100371 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000372 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200373 uint32_t security_cnt;
374 uint32_t img_security_cnt;
375 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000376 int rc;
377
378 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
379 seed, seed_len);
380 if (rc) {
381 return rc;
382 }
383
384 if (out_hash) {
385 memcpy(out_hash, hash, 32);
386 }
387
388 /* The TLVs come after the image. */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000389 off = hdr->ih_img_size + hdr->ih_hdr_size;
390
391 rc = flash_area_read(fap, off, &info, sizeof(info));
392 if (rc) {
393 return rc;
394 }
395 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200396 return BOOT_EBADMAGIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000397 }
398 end = off + info.it_tlv_tot;
399 off += sizeof(info);
400
401 /*
402 * Traverse through all of the TLVs, performing any checks we know
403 * and are able to do.
404 */
David Vincze060968d2019-05-23 01:13:14 +0200405 while (off < end) {
Tamas Ban581034a2017-12-19 19:54:37 +0000406 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000407 if (rc) {
408 return rc;
409 }
410
411 if (tlv.it_type == IMAGE_TLV_SHA256) {
412 /*
413 * Verify the SHA256 image hash. This must always be
414 * present.
415 */
416 if (tlv.it_len != sizeof(hash)) {
417 return -1;
418 }
Tamas Ban581034a2017-12-19 19:54:37 +0000419 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000420 if (rc) {
421 return rc;
422 }
Raef Coles25b857a2019-09-05 13:59:55 +0100423 if (boot_secure_memequal(hash, buf, sizeof(hash))) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000424 return -1;
425 }
426
427 sha256_valid = 1;
428#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100429#ifndef MCUBOOT_HW_KEY
Tamas Banf70ef8c2017-12-19 15:35:09 +0000430 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
431 /*
432 * Determine which key we should be checking.
433 */
434 if (tlv.it_len > 32) {
435 return -1;
436 }
Tamas Ban581034a2017-12-19 19:54:37 +0000437 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000438 if (rc) {
439 return rc;
440 }
441 key_id = bootutil_find_key(buf, tlv.it_len);
442 /*
443 * The key may not be found, which is acceptable. There
444 * can be multiple signatures, each preceded by a key.
445 */
Tamas Ban1d37c342019-07-11 08:55:55 +0100446#else
447 } else if (tlv.it_type == IMAGE_TLV_KEY) {
448 /*
449 * Determine which key we should be checking.
450 */
451 if (tlv.it_len > sizeof(key_buf)) {
452 return -1;
453 }
454 rc = flash_area_read(fap, off + sizeof(tlv), key_buf, tlv.it_len);
455 if (rc) {
456 return rc;
457 }
458 key_id = bootutil_find_key(key_buf, tlv.it_len);
459 /*
460 * The key may not be found, which is acceptable. There
461 * can be multiple signatures, each preceded by a key.
462 */
463#endif /* MCUBOOT_HW_KEY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000464 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
465 /* Ignore this signature if it is out of bounds. */
David Vinczefb068302019-07-24 11:00:22 +0200466 if (key_id >= 0 && key_id < bootutil_key_cnt) {
467 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
468 return -1;
469 }
470 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
471 if (rc) {
472 return -1;
473 }
474 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len,
475 key_id);
476 if (rc == 0) {
477 valid_signature = 1;
478 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000479 }
480 key_id = -1;
481#endif
David Vincze060968d2019-05-23 01:13:14 +0200482 } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
483 /*
484 * Verify the image's security counter.
485 * This must always be present.
486 */
487 if (tlv.it_len != sizeof(img_security_cnt)) {
488 /* Security counter is not valid. */
489 return -1;
490 }
491
492 rc = flash_area_read(fap, off + sizeof(tlv),
493 &img_security_cnt, tlv.it_len);
494 if (rc) {
495 return rc;
496 }
497
498 rc = boot_nv_security_counter_get(0, &security_cnt);
499 if (rc) {
500 return rc;
501 }
502
503 /* Compare the new image's security counter value against the
504 * stored security counter value.
505 */
506 if (img_security_cnt < security_cnt) {
507 /* The image's security counter is not accepted. */
508 return -1;
509 }
510
511 /* The image's security counter has been successfully verified. */
512 security_counter_valid = 1;
513 }
514
515 /* Avoid integer overflow. */
516 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
517 /* Potential overflow. */
518 break;
519 } else {
520 off += sizeof(tlv) + tlv.it_len;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000521 }
522 }
523
David Vincze060968d2019-05-23 01:13:14 +0200524 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000525 return -1;
526 }
527
528#ifdef EXPECTED_SIG_TLV
529 if (!valid_signature) {
530 return -1;
531 }
532#endif
533
534 return 0;
535}