blob: 3b1801038bb80c7710226015513e25cc50c01051 [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 Vincze61bd1e52019-10-24 16:47:31 +020023 * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
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
David Vinczecea8b592019-10-29 16:09:51 +010038#if defined(MCUBOOT_SIGN_RSA)
Tamas Banf70ef8c2017-12-19 15:35:09 +000039#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
David Vinczecea8b592019-10-29 16:09:51 +010054bootutil_img_hash(int image_index,
55 struct image_header *hdr, const struct flash_area *fap,
56 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
57 uint8_t *seed, int seed_len)
Tamas Banf70ef8c2017-12-19 15:35:09 +000058{
59 bootutil_sha256_context sha256_ctx;
Tamas Banf70ef8c2017-12-19 15:35:09 +000060 uint32_t size;
Raef Coles27a61452019-09-25 15:32:25 +010061#ifndef MCUBOOT_RAM_LOADING
62 uint32_t blk_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +000063 uint32_t off;
David Vinczecea8b592019-10-29 16:09:51 +010064 int rc;
Raef Coles27a61452019-09-25 15:32:25 +010065#endif /* MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +000066
David Vinczecea8b592019-10-29 16:09:51 +010067 (void)image_index;
68
Tamas Banf70ef8c2017-12-19 15:35:09 +000069 bootutil_sha256_init(&sha256_ctx);
70
71 /* in some cases (split image) the hash is seeded with data from
72 * the loader image */
Tamas Ban581034a2017-12-19 19:54:37 +000073 if (seed && (seed_len > 0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +000074 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
75 }
76
David Vinczedb32b212019-04-16 17:43:57 +020077 /* Hash is computed over image header and image itself. */
David Vinczecea8b592019-10-29 16:09:51 +010078 size = BOOT_TLV_OFF(hdr);
David Vinczedb32b212019-04-16 17:43:57 +020079
David Vincze61bd1e52019-10-24 16:47:31 +020080 /* If protected TLVs are present they are also hashed. */
81 size += hdr->ih_protect_tlv_size;
David Vinczedb32b212019-04-16 17:43:57 +020082
Raef Coles27a61452019-09-25 15:32:25 +010083#ifdef MCUBOOT_RAM_LOADING
84 bootutil_sha256_update(&sha256_ctx,(void*)(hdr->ih_load_addr), size);
85#else
Tamas Banf70ef8c2017-12-19 15:35:09 +000086 for (off = 0; off < size; off += blk_sz) {
87 blk_sz = size - off;
88 if (blk_sz > tmp_buf_sz) {
89 blk_sz = tmp_buf_sz;
90 }
David Vinczecea8b592019-10-29 16:09:51 +010091 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
92 if (rc) {
93 return rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +000094 }
95 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
96 }
Raef Coles27a61452019-09-25 15:32:25 +010097#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000098 bootutil_sha256_finish(&sha256_ctx, hash_result);
99
100 return 0;
101}
102
103/*
104 * Currently, we only support being able to verify one type of
105 * signature, because there is a single verification function that we
106 * call. List the type of TLV we are expecting. If we aren't
107 * configured for any signature, don't define this macro.
108 */
Tamas Ban81daed02019-05-20 15:05:22 +0100109
Tamas Banf70ef8c2017-12-19 15:35:09 +0000110#if defined(MCUBOOT_SIGN_RSA)
Tamas Ban81daed02019-05-20 15:05:22 +0100111# if MCUBOOT_SIGN_RSA_LEN == 2048
112# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
113# elif MCUBOOT_SIGN_RSA_LEN == 3072
114# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
115# else
116# error "Unsupported RSA signature length"
117# endif
118# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
119# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
120#else
121# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000122#endif
123
124#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100125#ifdef MCUBOOT_HW_KEY
126extern unsigned int pub_key_len;
127static int
David Vinczecea8b592019-10-29 16:09:51 +0100128bootutil_find_key(uint8_t image_id, uint8_t *key, uint16_t key_len)
Tamas Ban1d37c342019-07-11 08:55:55 +0100129{
130 bootutil_sha256_context sha256_ctx;
131 uint8_t hash[32];
132 uint8_t key_hash[32];
133 uint32_t key_hash_size= sizeof(key_hash);
134 enum tfm_plat_err_t plat_err;
135
136 bootutil_sha256_init(&sha256_ctx);
137 bootutil_sha256_update(&sha256_ctx, key, key_len);
138 bootutil_sha256_finish(&sha256_ctx, hash);
139
David Vinczecea8b592019-10-29 16:09:51 +0100140 plat_err = tfm_plat_get_rotpk_hash(image_id, key_hash, &key_hash_size);
Tamas Ban1d37c342019-07-11 08:55:55 +0100141 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
142 return -1;
143 }
Raef Coles25b857a2019-09-05 13:59:55 +0100144 if (!boot_secure_memequal(hash, key_hash, key_hash_size)) {
Tamas Ban1d37c342019-07-11 08:55:55 +0100145 bootutil_keys[0].key = key;
146 pub_key_len = key_len;
147 return 0;
148 }
149 return -1;
150}
David Vinczecea8b592019-10-29 16:09:51 +0100151#else /* !MCUBOOT_HW_KEY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000152static int
153bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
154{
155 bootutil_sha256_context sha256_ctx;
156 int i;
157 const struct bootutil_key *key;
158 uint8_t hash[32];
159
David Vinczecea8b592019-10-29 16:09:51 +0100160 if (keyhash_len > 32) {
161 return -1;
162 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000163
164 for (i = 0; i < bootutil_key_cnt; i++) {
165 key = &bootutil_keys[i];
166 bootutil_sha256_init(&sha256_ctx);
167 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
168 bootutil_sha256_finish(&sha256_ctx, hash);
Raef Coles25b857a2019-09-05 13:59:55 +0100169 if (!boot_secure_memequal(hash, keyhash, keyhash_len)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000170 return i;
171 }
172 }
173 return -1;
174}
175#endif
Tamas Ban1d37c342019-07-11 08:55:55 +0100176#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000177
David Vincze060968d2019-05-23 01:13:14 +0200178/**
179 * Reads the value of an image's security counter.
180 *
181 * @param hdr Pointer to the image header structure.
182 * @param fap Pointer to a description structure of the image's
183 * flash area.
184 * @param security_cnt Pointer to store the security counter value.
185 *
186 * @return 0 on success; nonzero on failure.
187 */
188int32_t
189bootutil_get_img_security_cnt(struct image_header *hdr,
190 const struct flash_area *fap,
191 uint32_t *img_security_cnt)
192{
David Vincze66a56fc2019-10-25 14:15:05 +0200193 struct image_tlv_iter it;
David Vincze060968d2019-05-23 01:13:14 +0200194 uint32_t off;
David Vincze66a56fc2019-10-25 14:15:05 +0200195 uint16_t len;
David Vincze060968d2019-05-23 01:13:14 +0200196 uint32_t found = 0;
197 int32_t rc;
198
199 if ((hdr == NULL) ||
200 (fap == NULL) ||
201 (img_security_cnt == NULL)) {
202 /* Invalid parameter. */
203 return BOOT_EBADARGS;
204 }
205
David Vincze060968d2019-05-23 01:13:14 +0200206 /* The security counter TLV is in the protected part of the TLV area. */
David Vinczec2566122019-10-25 13:18:54 +0200207 if (hdr->ih_protect_tlv_size == 0) {
208 return BOOT_EBADIMAGE;
209 }
David Vincze060968d2019-05-23 01:13:14 +0200210
David Vincze66a56fc2019-10-25 14:15:05 +0200211 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true);
David Vinczec2566122019-10-25 13:18:54 +0200212 if (rc) {
213 return rc;
214 }
215
David Vinczec2566122019-10-25 13:18:54 +0200216 /* Traverse through the protected TLV area to find
217 * the security counter TLV.
218 */
David Vincze66a56fc2019-10-25 14:15:05 +0200219 while (true) {
220 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
221 if (rc < 0) {
222 return -1;
223 } else if (rc > 0) {
224 break;
225 }
226
227 if (len != sizeof(*img_security_cnt)) {
228 /* Security counter is not valid. */
229 return BOOT_EBADIMAGE;
230 }
231
232 rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len);
David Vinczec2566122019-10-25 13:18:54 +0200233 if (rc != 0) {
234 return BOOT_EFLASH;
235 }
236
David Vincze66a56fc2019-10-25 14:15:05 +0200237 /* Security counter has been found. */
238 found = 1;
239 break;
David Vincze060968d2019-05-23 01:13:14 +0200240 }
241
242 if (found) {
243 return 0;
244 }
245
246 return -1;
247}
248
Tamas Banf70ef8c2017-12-19 15:35:09 +0000249/*
250 * Verify the integrity of the image.
251 * Return non-zero if image could not be validated/does not validate.
252 */
253int
David Vinczecea8b592019-10-29 16:09:51 +0100254bootutil_img_validate(int image_index,
255 struct image_header *hdr, const struct flash_area *fap,
256 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
257 int seed_len, uint8_t *out_hash)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000258{
259 uint32_t off;
David Vincze07706a42019-12-12 18:20:12 +0100260 uint16_t len;
261 uint8_t type;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000262 int sha256_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000263#ifdef EXPECTED_SIG_TLV
264 int valid_signature = 0;
265 int key_id = -1;
Tamas Ban1d37c342019-07-11 08:55:55 +0100266#ifdef MCUBOOT_HW_KEY
267 /* Few extra bytes for encoding and for public exponent */
268 uint8_t key_buf[SIG_BUF_SIZE + 24];
269#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000270#endif
David Vincze07706a42019-12-12 18:20:12 +0100271 struct image_tlv_iter it;
Tamas Ban81daed02019-05-20 15:05:22 +0100272 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000273 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200274 uint32_t security_cnt;
275 uint32_t img_security_cnt;
276 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000277 int rc;
278
David Vinczecea8b592019-10-29 16:09:51 +0100279 rc = bootutil_img_hash(image_index, hdr, fap, tmp_buf,
280 tmp_buf_sz, hash, seed, seed_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000281 if (rc) {
282 return rc;
283 }
284
285 if (out_hash) {
286 memcpy(out_hash, hash, 32);
287 }
288
David Vincze07706a42019-12-12 18:20:12 +0100289 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000290 if (rc) {
291 return rc;
292 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000293
294 /*
295 * Traverse through all of the TLVs, performing any checks we know
296 * and are able to do.
297 */
David Vincze07706a42019-12-12 18:20:12 +0100298 while (true) {
299 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
300 if (rc < 0) {
301 return -1;
302 } else if (rc > 0) {
303 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000304 }
305
David Vincze07706a42019-12-12 18:20:12 +0100306 if (type == IMAGE_TLV_SHA256) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000307 /*
308 * Verify the SHA256 image hash. This must always be
309 * present.
310 */
David Vincze07706a42019-12-12 18:20:12 +0100311 if (len != sizeof(hash)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000312 return -1;
313 }
David Vincze07706a42019-12-12 18:20:12 +0100314 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000315 if (rc) {
316 return rc;
317 }
Raef Coles25b857a2019-09-05 13:59:55 +0100318 if (boot_secure_memequal(hash, buf, sizeof(hash))) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000319 return -1;
320 }
321
322 sha256_valid = 1;
323#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100324#ifndef MCUBOOT_HW_KEY
David Vincze07706a42019-12-12 18:20:12 +0100325 } else if (type == IMAGE_TLV_KEYHASH) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000326 /*
327 * Determine which key we should be checking.
328 */
David Vincze07706a42019-12-12 18:20:12 +0100329 if (len > 32) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000330 return -1;
331 }
David Vincze07706a42019-12-12 18:20:12 +0100332 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000333 if (rc) {
334 return rc;
335 }
David Vincze07706a42019-12-12 18:20:12 +0100336 key_id = bootutil_find_key(buf, len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000337 /*
338 * The key may not be found, which is acceptable. There
339 * can be multiple signatures, each preceded by a key.
340 */
David Vinczecea8b592019-10-29 16:09:51 +0100341#else /* MCUBOOT_HW_KEY */
David Vincze07706a42019-12-12 18:20:12 +0100342 } else if (type == IMAGE_TLV_KEY) {
Tamas Ban1d37c342019-07-11 08:55:55 +0100343 /*
344 * Determine which key we should be checking.
345 */
David Vincze07706a42019-12-12 18:20:12 +0100346 if (len > sizeof(key_buf)) {
Tamas Ban1d37c342019-07-11 08:55:55 +0100347 return -1;
348 }
David Vincze07706a42019-12-12 18:20:12 +0100349 rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len);
Tamas Ban1d37c342019-07-11 08:55:55 +0100350 if (rc) {
351 return rc;
352 }
David Vincze07706a42019-12-12 18:20:12 +0100353 key_id = bootutil_find_key(image_index, key_buf, len);
Tamas Ban1d37c342019-07-11 08:55:55 +0100354 /*
355 * The key may not be found, which is acceptable. There
356 * can be multiple signatures, each preceded by a key.
357 */
358#endif /* MCUBOOT_HW_KEY */
David Vincze07706a42019-12-12 18:20:12 +0100359 } else if (type == EXPECTED_SIG_TLV) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000360 /* Ignore this signature if it is out of bounds. */
David Vincze07706a42019-12-12 18:20:12 +0100361 if (key_id < 0 || key_id >= bootutil_key_cnt) {
362 key_id = -1;
363 continue;
364 }
365 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
366 return -1;
367 }
368 rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len);
369 if (rc) {
370 return -1;
371 }
372 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
373 if (rc == 0) {
374 valid_signature = 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000375 }
376 key_id = -1;
377#endif
David Vincze07706a42019-12-12 18:20:12 +0100378 } else if (type == IMAGE_TLV_SEC_CNT) {
David Vincze060968d2019-05-23 01:13:14 +0200379 /*
380 * Verify the image's security counter.
381 * This must always be present.
382 */
David Vincze07706a42019-12-12 18:20:12 +0100383 if (len != sizeof(img_security_cnt)) {
David Vincze060968d2019-05-23 01:13:14 +0200384 /* Security counter is not valid. */
385 return -1;
386 }
387
David Vincze07706a42019-12-12 18:20:12 +0100388 rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len);
David Vincze060968d2019-05-23 01:13:14 +0200389 if (rc) {
390 return rc;
391 }
392
393 rc = boot_nv_security_counter_get(0, &security_cnt);
394 if (rc) {
395 return rc;
396 }
397
398 /* Compare the new image's security counter value against the
399 * stored security counter value.
400 */
401 if (img_security_cnt < security_cnt) {
402 /* The image's security counter is not accepted. */
403 return -1;
404 }
405
406 /* The image's security counter has been successfully verified. */
407 security_counter_valid = 1;
408 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000409 }
410
David Vincze060968d2019-05-23 01:13:14 +0200411 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000412 return -1;
413 }
414
415#ifdef EXPECTED_SIG_TLV
416 if (!valid_signature) {
417 return -1;
418 }
419#endif
420
421 return 0;
422}