blob: fee67a2bf16e0d2e2db588251eb22b465a0355a0 [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;
131static int
132bootutil_find_key(uint8_t *key, uint16_t key_len)
133{
134 bootutil_sha256_context sha256_ctx;
135 uint8_t hash[32];
136 uint8_t key_hash[32];
137 uint32_t key_hash_size= sizeof(key_hash);
138 enum tfm_plat_err_t plat_err;
139
140 bootutil_sha256_init(&sha256_ctx);
141 bootutil_sha256_update(&sha256_ctx, key, key_len);
142 bootutil_sha256_finish(&sha256_ctx, hash);
143
144 plat_err = tfm_plat_get_rotpk_hash(0, key_hash, &key_hash_size);
145 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
146 return -1;
147 }
148 if (!memcmp(hash, key_hash, key_hash_size)) {
149 bootutil_keys[0].key = key;
150 pub_key_len = key_len;
151 return 0;
152 }
153 return -1;
154}
155#else
Tamas Banf70ef8c2017-12-19 15:35:09 +0000156static int
157bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
158{
159 bootutil_sha256_context sha256_ctx;
160 int i;
161 const struct bootutil_key *key;
162 uint8_t hash[32];
163
164 assert(keyhash_len <= 32);
165
166 for (i = 0; i < bootutil_key_cnt; i++) {
167 key = &bootutil_keys[i];
168 bootutil_sha256_init(&sha256_ctx);
169 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
170 bootutil_sha256_finish(&sha256_ctx, hash);
171 if (!memcmp(hash, keyhash, keyhash_len)) {
172 return i;
173 }
174 }
175 return -1;
176}
177#endif
Tamas Ban1d37c342019-07-11 08:55:55 +0100178#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000179
Oliver Swedef9982442018-08-24 18:37:44 +0100180#ifdef MCUBOOT_RAM_LOADING
181/* Check the hash of an image after it has been copied to SRAM */
182int
183bootutil_check_hash_after_loading(struct image_header *hdr)
184{
185 uint32_t off;
186 uint32_t end;
187 int sha256_valid = 0;
188 struct image_tlv_info info;
189 struct image_tlv tlv;
190 uint8_t tmp_buf[BOOT_TMPBUF_SZ];
191 uint8_t hash[32] = {0};
192 int rc;
193 uint32_t load_address;
194 uint32_t tlv_sz;
195
196 rc = bootutil_img_hash(hdr, NULL, tmp_buf, BOOT_TMPBUF_SZ, hash, NULL, 0);
197
198 if (rc) {
199 return rc;
200 }
201
202 load_address = (uint32_t) hdr->ih_load_addr;
203
204 /* The TLVs come after the image. */
205 off = hdr->ih_img_size + hdr->ih_hdr_size;
206
207 info = *((struct image_tlv_info *)(load_address + off));
208
209 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200210 return BOOT_EBADMAGIC;
Oliver Swedef9982442018-08-24 18:37:44 +0100211 }
212 end = off + info.it_tlv_tot;
213 off += sizeof(info);
214
215 /*
216 * Traverse through all of the TLVs, performing any checks we know
217 * and are able to do.
218 */
David Vincze060968d2019-05-23 01:13:14 +0200219 while (off < end) {
Oliver Swedef9982442018-08-24 18:37:44 +0100220 tlv = *((struct image_tlv *)(load_address + off));
221 tlv_sz = sizeof(tlv);
222
223 if (tlv.it_type == IMAGE_TLV_SHA256) {
224 /*
225 * Verify the SHA256 image hash. This must always be present.
226 */
227 if (tlv.it_len != sizeof(hash)) {
228 return -1;
229 }
230
231 if (memcmp(hash, (uint32_t *)(load_address + off + tlv_sz),
232 sizeof(hash))) {
233 return -1;
234 }
235
236 sha256_valid = 1;
237 }
David Vincze060968d2019-05-23 01:13:14 +0200238
239 /* Avoid integer overflow. */
240 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
241 /* Potential overflow. */
242 break;
243 } else {
244 off += sizeof(tlv) + tlv.it_len;
245 }
Oliver Swedef9982442018-08-24 18:37:44 +0100246 }
247
248 if (!sha256_valid) {
249 return -1;
250 }
251
252 return 0;
253}
254#endif /* MCUBOOT_RAM_LOADING */
255
David Vincze060968d2019-05-23 01:13:14 +0200256/**
257 * Reads the value of an image's security counter.
258 *
259 * @param hdr Pointer to the image header structure.
260 * @param fap Pointer to a description structure of the image's
261 * flash area.
262 * @param security_cnt Pointer to store the security counter value.
263 *
264 * @return 0 on success; nonzero on failure.
265 */
266int32_t
267bootutil_get_img_security_cnt(struct image_header *hdr,
268 const struct flash_area *fap,
269 uint32_t *img_security_cnt)
270{
271 struct image_tlv_info info;
272 struct image_tlv tlv;
273 uint32_t off;
274 uint32_t end;
275 uint32_t found = 0;
276 int32_t rc;
277
278 if ((hdr == NULL) ||
279 (fap == NULL) ||
280 (img_security_cnt == NULL)) {
281 /* Invalid parameter. */
282 return BOOT_EBADARGS;
283 }
284
285 /* The TLVs come after the image. */
286 off = hdr->ih_hdr_size + hdr->ih_img_size;
287
288 /* The TLV area always starts with an image_tlv_info structure. */
289 rc = flash_area_read(fap, off, &info, sizeof(info));
290 if (rc != 0) {
291 return BOOT_EFLASH;
292 }
293
294 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
295 return BOOT_EBADMAGIC;
296 }
297
298 /* The security counter TLV is in the protected part of the TLV area. */
299 if (hdr->ih_protect_tlv_size != 0) {
300 end = off + (uint32_t)hdr->ih_protect_tlv_size;
301 off += sizeof(info);
302
303 /* Traverse through the protected TLV area to find the
304 * security counter TLV.
305 */
306 while (off < end) {
307 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
308 if (rc != 0) {
309 return BOOT_EFLASH;
310 }
311
312 if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
313
314 if (tlv.it_len != sizeof(*img_security_cnt)) {
315 /* Security counter is not valid. */
316 break;
317 }
318
319 rc = flash_area_read(fap, off + sizeof(tlv),
320 img_security_cnt, tlv.it_len);
321 if (rc != 0) {
322 return BOOT_EFLASH;
323 }
324
325 /* Security counter has been found. */
326 found = 1;
327 break;
328 }
329
330 /* Avoid integer overflow. */
331 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
332 /* Potential overflow. */
333 break;
334 } else {
335 off += sizeof(tlv) + tlv.it_len;
336 }
337 }
338 }
339
340 if (found) {
341 return 0;
342 }
343
344 return -1;
345}
346
Tamas Banf70ef8c2017-12-19 15:35:09 +0000347/*
348 * Verify the integrity of the image.
349 * Return non-zero if image could not be validated/does not validate.
350 */
351int
352bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
353 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
354 uint8_t *seed, int seed_len, uint8_t *out_hash)
355{
356 uint32_t off;
357 uint32_t end;
358 int sha256_valid = 0;
359 struct image_tlv_info info;
360#ifdef EXPECTED_SIG_TLV
361 int valid_signature = 0;
362 int key_id = -1;
Tamas Ban1d37c342019-07-11 08:55:55 +0100363#ifdef MCUBOOT_HW_KEY
364 /* Few extra bytes for encoding and for public exponent */
365 uint8_t key_buf[SIG_BUF_SIZE + 24];
366#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000367#endif
368 struct image_tlv tlv;
Tamas Ban81daed02019-05-20 15:05:22 +0100369 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000370 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200371 uint32_t security_cnt;
372 uint32_t img_security_cnt;
373 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000374 int rc;
375
376 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
377 seed, seed_len);
378 if (rc) {
379 return rc;
380 }
381
382 if (out_hash) {
383 memcpy(out_hash, hash, 32);
384 }
385
386 /* The TLVs come after the image. */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000387 off = hdr->ih_img_size + hdr->ih_hdr_size;
388
389 rc = flash_area_read(fap, off, &info, sizeof(info));
390 if (rc) {
391 return rc;
392 }
393 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200394 return BOOT_EBADMAGIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000395 }
396 end = off + info.it_tlv_tot;
397 off += sizeof(info);
398
399 /*
400 * Traverse through all of the TLVs, performing any checks we know
401 * and are able to do.
402 */
David Vincze060968d2019-05-23 01:13:14 +0200403 while (off < end) {
Tamas Ban581034a2017-12-19 19:54:37 +0000404 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000405 if (rc) {
406 return rc;
407 }
408
409 if (tlv.it_type == IMAGE_TLV_SHA256) {
410 /*
411 * Verify the SHA256 image hash. This must always be
412 * present.
413 */
414 if (tlv.it_len != sizeof(hash)) {
415 return -1;
416 }
Tamas Ban581034a2017-12-19 19:54:37 +0000417 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000418 if (rc) {
419 return rc;
420 }
421 if (memcmp(hash, buf, sizeof(hash))) {
422 return -1;
423 }
424
425 sha256_valid = 1;
426#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100427#ifndef MCUBOOT_HW_KEY
Tamas Banf70ef8c2017-12-19 15:35:09 +0000428 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
429 /*
430 * Determine which key we should be checking.
431 */
432 if (tlv.it_len > 32) {
433 return -1;
434 }
Tamas Ban581034a2017-12-19 19:54:37 +0000435 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000436 if (rc) {
437 return rc;
438 }
439 key_id = bootutil_find_key(buf, tlv.it_len);
440 /*
441 * The key may not be found, which is acceptable. There
442 * can be multiple signatures, each preceded by a key.
443 */
Tamas Ban1d37c342019-07-11 08:55:55 +0100444#else
445 } else if (tlv.it_type == IMAGE_TLV_KEY) {
446 /*
447 * Determine which key we should be checking.
448 */
449 if (tlv.it_len > sizeof(key_buf)) {
450 return -1;
451 }
452 rc = flash_area_read(fap, off + sizeof(tlv), key_buf, tlv.it_len);
453 if (rc) {
454 return rc;
455 }
456 key_id = bootutil_find_key(key_buf, tlv.it_len);
457 /*
458 * The key may not be found, which is acceptable. There
459 * can be multiple signatures, each preceded by a key.
460 */
461#endif /* MCUBOOT_HW_KEY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000462 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
463 /* Ignore this signature if it is out of bounds. */
David Vinczefb068302019-07-24 11:00:22 +0200464 if (key_id >= 0 && key_id < bootutil_key_cnt) {
465 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
466 return -1;
467 }
468 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
469 if (rc) {
470 return -1;
471 }
472 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len,
473 key_id);
474 if (rc == 0) {
475 valid_signature = 1;
476 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000477 }
478 key_id = -1;
479#endif
David Vincze060968d2019-05-23 01:13:14 +0200480 } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
481 /*
482 * Verify the image's security counter.
483 * This must always be present.
484 */
485 if (tlv.it_len != sizeof(img_security_cnt)) {
486 /* Security counter is not valid. */
487 return -1;
488 }
489
490 rc = flash_area_read(fap, off + sizeof(tlv),
491 &img_security_cnt, tlv.it_len);
492 if (rc) {
493 return rc;
494 }
495
496 rc = boot_nv_security_counter_get(0, &security_cnt);
497 if (rc) {
498 return rc;
499 }
500
501 /* Compare the new image's security counter value against the
502 * stored security counter value.
503 */
504 if (img_security_cnt < security_cnt) {
505 /* The image's security counter is not accepted. */
506 return -1;
507 }
508
509 /* The image's security counter has been successfully verified. */
510 security_counter_valid = 1;
511 }
512
513 /* Avoid integer overflow. */
514 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
515 /* Potential overflow. */
516 break;
517 } else {
518 off += sizeof(tlv) + tlv.it_len;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000519 }
520 }
521
David Vincze060968d2019-05-23 01:13:14 +0200522 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000523 return -1;
524 }
525
526#ifdef EXPECTED_SIG_TLV
527 if (!valid_signature) {
528 return -1;
529 }
530#endif
531
532 return 0;
533}