blob: 13892d89393ce956a8eff85d56d32a15f2c2e695 [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;
Tamas Banf70ef8c2017-12-19 15:35:09 +000059 uint32_t size;
Raef Coles27a61452019-09-25 15:32:25 +010060#ifndef MCUBOOT_RAM_LOADING
61 uint32_t blk_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +000062 uint32_t off;
Raef Coles27a61452019-09-25 15:32:25 +010063#endif /* MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +000064
65 bootutil_sha256_init(&sha256_ctx);
66
67 /* in some cases (split image) the hash is seeded with data from
68 * the loader image */
Tamas Ban581034a2017-12-19 19:54:37 +000069 if (seed && (seed_len > 0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +000070 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
71 }
72
David Vinczedb32b212019-04-16 17:43:57 +020073 /* Hash is computed over image header and image itself. */
David Vincze39e78552018-10-10 17:10:01 +020074 size = hdr->ih_img_size + hdr->ih_hdr_size;
David Vinczedb32b212019-04-16 17:43:57 +020075
David Vincze219a1752019-10-14 11:35:09 +020076 /* If protected TLVs are present (e.g. security counter TLV) then the
77 * TLV info header and these TLVs must be included in the hash calculation.
David Vinczedb32b212019-04-16 17:43:57 +020078 */
79 if (hdr->ih_protect_tlv_size != 0) {
80 size += hdr->ih_protect_tlv_size;
81 }
82
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 }
Raef Coles27a61452019-09-25 15:32:25 +010091 if(flash_area_read(fap, off, tmp_buf, blk_sz)) {
92 return -1;
Tamas Banf70ef8c2017-12-19 15:35:09 +000093 }
94 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
95 }
Raef Coles27a61452019-09-25 15:32:25 +010096#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000097 bootutil_sha256_finish(&sha256_ctx, hash_result);
98
99 return 0;
100}
101
102/*
103 * Currently, we only support being able to verify one type of
104 * signature, because there is a single verification function that we
105 * call. List the type of TLV we are expecting. If we aren't
106 * configured for any signature, don't define this macro.
107 */
Tamas Ban81daed02019-05-20 15:05:22 +0100108
Tamas Banf70ef8c2017-12-19 15:35:09 +0000109#if defined(MCUBOOT_SIGN_RSA)
Tamas Ban81daed02019-05-20 15:05:22 +0100110# if MCUBOOT_SIGN_RSA_LEN == 2048
111# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
112# elif MCUBOOT_SIGN_RSA_LEN == 3072
113# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
114# else
115# error "Unsupported RSA signature length"
116# endif
117# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
118# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
119#else
120# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000121#endif
122
123#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100124#ifdef MCUBOOT_HW_KEY
125extern unsigned int pub_key_len;
Tamas Ban78676ac2019-07-11 09:05:54 +0100126extern uint8_t current_image;
Tamas Ban1d37c342019-07-11 08:55:55 +0100127static int
128bootutil_find_key(uint8_t *key, uint16_t key_len)
129{
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
Tamas Ban78676ac2019-07-11 09:05:54 +0100140 plat_err = tfm_plat_get_rotpk_hash(current_image, 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}
151#else
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
160 assert(keyhash_len <= 32);
161
162 for (i = 0; i < bootutil_key_cnt; i++) {
163 key = &bootutil_keys[i];
164 bootutil_sha256_init(&sha256_ctx);
165 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
166 bootutil_sha256_finish(&sha256_ctx, hash);
Raef Coles25b857a2019-09-05 13:59:55 +0100167 if (!boot_secure_memequal(hash, keyhash, keyhash_len)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000168 return i;
169 }
170 }
171 return -1;
172}
173#endif
Tamas Ban1d37c342019-07-11 08:55:55 +0100174#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000175
David Vincze060968d2019-05-23 01:13:14 +0200176/**
177 * Reads the value of an image's security counter.
178 *
179 * @param hdr Pointer to the image header structure.
180 * @param fap Pointer to a description structure of the image's
181 * flash area.
182 * @param security_cnt Pointer to store the security counter value.
183 *
184 * @return 0 on success; nonzero on failure.
185 */
186int32_t
187bootutil_get_img_security_cnt(struct image_header *hdr,
188 const struct flash_area *fap,
189 uint32_t *img_security_cnt)
190{
191 struct image_tlv_info info;
192 struct image_tlv tlv;
193 uint32_t off;
194 uint32_t end;
195 uint32_t found = 0;
196 int32_t rc;
197
198 if ((hdr == NULL) ||
199 (fap == NULL) ||
200 (img_security_cnt == NULL)) {
201 /* Invalid parameter. */
202 return BOOT_EBADARGS;
203 }
204
205 /* The TLVs come after the image. */
206 off = hdr->ih_hdr_size + hdr->ih_img_size;
207
208 /* The TLV area always starts with an image_tlv_info structure. */
Raef Coles27a61452019-09-25 15:32:25 +0100209 rc = LOAD_IMAGE_DATA(fap, off, &info, sizeof(info));
David Vincze060968d2019-05-23 01:13:14 +0200210 if (rc != 0) {
211 return BOOT_EFLASH;
212 }
213
214 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
215 return BOOT_EBADMAGIC;
216 }
217
218 /* The security counter TLV is in the protected part of the TLV area. */
219 if (hdr->ih_protect_tlv_size != 0) {
220 end = off + (uint32_t)hdr->ih_protect_tlv_size;
221 off += sizeof(info);
222
223 /* Traverse through the protected TLV area to find the
224 * security counter TLV.
225 */
226 while (off < end) {
Raef Coles27a61452019-09-25 15:32:25 +0100227 rc = LOAD_IMAGE_DATA(fap, off, &tlv, sizeof(tlv));
David Vincze060968d2019-05-23 01:13:14 +0200228 if (rc != 0) {
229 return BOOT_EFLASH;
230 }
231
232 if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
233
234 if (tlv.it_len != sizeof(*img_security_cnt)) {
235 /* Security counter is not valid. */
236 break;
237 }
238
Raef Coles27a61452019-09-25 15:32:25 +0100239 rc = LOAD_IMAGE_DATA(fap, off + sizeof(tlv),
David Vincze060968d2019-05-23 01:13:14 +0200240 img_security_cnt, tlv.it_len);
241 if (rc != 0) {
242 return BOOT_EFLASH;
243 }
244
245 /* Security counter has been found. */
246 found = 1;
247 break;
248 }
249
250 /* Avoid integer overflow. */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100251 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len)))
252 {
David Vincze060968d2019-05-23 01:13:14 +0200253 /* Potential overflow. */
254 break;
255 } else {
256 off += sizeof(tlv) + tlv.it_len;
257 }
258 }
259 }
260
261 if (found) {
262 return 0;
263 }
264
265 return -1;
266}
267
Tamas Banf70ef8c2017-12-19 15:35:09 +0000268/*
269 * Verify the integrity of the image.
270 * Return non-zero if image could not be validated/does not validate.
271 */
272int
273bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
274 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
275 uint8_t *seed, int seed_len, uint8_t *out_hash)
276{
277 uint32_t off;
278 uint32_t end;
279 int sha256_valid = 0;
280 struct image_tlv_info info;
281#ifdef EXPECTED_SIG_TLV
282 int valid_signature = 0;
283 int key_id = -1;
Tamas Ban1d37c342019-07-11 08:55:55 +0100284#ifdef MCUBOOT_HW_KEY
285 /* Few extra bytes for encoding and for public exponent */
286 uint8_t key_buf[SIG_BUF_SIZE + 24];
287#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000288#endif
289 struct image_tlv tlv;
Tamas Ban81daed02019-05-20 15:05:22 +0100290 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000291 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200292 uint32_t security_cnt;
293 uint32_t img_security_cnt;
294 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000295 int rc;
296
297 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
298 seed, seed_len);
299 if (rc) {
300 return rc;
301 }
302
303 if (out_hash) {
304 memcpy(out_hash, hash, 32);
305 }
306
307 /* The TLVs come after the image. */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000308 off = hdr->ih_img_size + hdr->ih_hdr_size;
309
Raef Coles27a61452019-09-25 15:32:25 +0100310 rc = LOAD_IMAGE_DATA(fap, off, &info, sizeof(info));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000311 if (rc) {
312 return rc;
313 }
314 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200315 return BOOT_EBADMAGIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000316 }
Tamas Ban056ed0b2019-09-16 12:48:32 +0100317 if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) {
318 return -1;
319 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000320 end = off + info.it_tlv_tot;
321 off += sizeof(info);
322
323 /*
324 * Traverse through all of the TLVs, performing any checks we know
325 * and are able to do.
326 */
David Vincze060968d2019-05-23 01:13:14 +0200327 while (off < end) {
Raef Coles27a61452019-09-25 15:32:25 +0100328 rc = LOAD_IMAGE_DATA(fap, off, &tlv, sizeof(tlv));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000329 if (rc) {
330 return rc;
331 }
332
333 if (tlv.it_type == IMAGE_TLV_SHA256) {
334 /*
335 * Verify the SHA256 image hash. This must always be
336 * present.
337 */
338 if (tlv.it_len != sizeof(hash)) {
339 return -1;
340 }
Raef Coles27a61452019-09-25 15:32:25 +0100341 rc = LOAD_IMAGE_DATA(fap, off + sizeof(tlv), buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000342 if (rc) {
343 return rc;
344 }
Raef Coles25b857a2019-09-05 13:59:55 +0100345 if (boot_secure_memequal(hash, buf, sizeof(hash))) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000346 return -1;
347 }
348
349 sha256_valid = 1;
350#ifdef EXPECTED_SIG_TLV
Tamas Ban1d37c342019-07-11 08:55:55 +0100351#ifndef MCUBOOT_HW_KEY
Tamas Banf70ef8c2017-12-19 15:35:09 +0000352 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
353 /*
354 * Determine which key we should be checking.
355 */
356 if (tlv.it_len > 32) {
357 return -1;
358 }
Raef Coles27a61452019-09-25 15:32:25 +0100359 rc = LOAD_IMAGE_DATA(fap, off + sizeof(tlv), buf, tlv.it_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000360 if (rc) {
361 return rc;
362 }
363 key_id = bootutil_find_key(buf, tlv.it_len);
364 /*
365 * The key may not be found, which is acceptable. There
366 * can be multiple signatures, each preceded by a key.
367 */
Tamas Ban1d37c342019-07-11 08:55:55 +0100368#else
369 } else if (tlv.it_type == IMAGE_TLV_KEY) {
370 /*
371 * Determine which key we should be checking.
372 */
373 if (tlv.it_len > sizeof(key_buf)) {
374 return -1;
375 }
Raef Coles27a61452019-09-25 15:32:25 +0100376 rc = LOAD_IMAGE_DATA(fap, off + sizeof(tlv), key_buf, tlv.it_len);
Tamas Ban1d37c342019-07-11 08:55:55 +0100377 if (rc) {
378 return rc;
379 }
380 key_id = bootutil_find_key(key_buf, tlv.it_len);
381 /*
382 * The key may not be found, which is acceptable. There
383 * can be multiple signatures, each preceded by a key.
384 */
385#endif /* MCUBOOT_HW_KEY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000386 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
387 /* Ignore this signature if it is out of bounds. */
David Vinczefb068302019-07-24 11:00:22 +0200388 if (key_id >= 0 && key_id < bootutil_key_cnt) {
389 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
390 return -1;
391 }
Raef Coles27a61452019-09-25 15:32:25 +0100392 rc = LOAD_IMAGE_DATA(fap, off + sizeof(tlv), buf, tlv.it_len);
David Vinczefb068302019-07-24 11:00:22 +0200393 if (rc) {
394 return -1;
395 }
396 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len,
397 key_id);
398 if (rc == 0) {
399 valid_signature = 1;
400 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000401 }
402 key_id = -1;
403#endif
David Vincze060968d2019-05-23 01:13:14 +0200404 } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
405 /*
406 * Verify the image's security counter.
407 * This must always be present.
408 */
409 if (tlv.it_len != sizeof(img_security_cnt)) {
410 /* Security counter is not valid. */
411 return -1;
412 }
413
Raef Coles27a61452019-09-25 15:32:25 +0100414 rc = LOAD_IMAGE_DATA(fap, off + sizeof(tlv),
David Vincze060968d2019-05-23 01:13:14 +0200415 &img_security_cnt, tlv.it_len);
416 if (rc) {
417 return rc;
418 }
419
420 rc = boot_nv_security_counter_get(0, &security_cnt);
421 if (rc) {
422 return rc;
423 }
424
425 /* Compare the new image's security counter value against the
426 * stored security counter value.
427 */
428 if (img_security_cnt < security_cnt) {
429 /* The image's security counter is not accepted. */
430 return -1;
431 }
432
433 /* The image's security counter has been successfully verified. */
434 security_counter_valid = 1;
435 }
436
437 /* Avoid integer overflow. */
Tamas Ban056ed0b2019-09-16 12:48:32 +0100438 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) {
David Vincze060968d2019-05-23 01:13:14 +0200439 /* Potential overflow. */
440 break;
441 } else {
442 off += sizeof(tlv) + tlv.it_len;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000443 }
444 }
445
David Vincze060968d2019-05-23 01:13:14 +0200446 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000447 return -1;
448 }
449
450#ifdef EXPECTED_SIG_TLV
451 if (!valid_signature) {
452 return -1;
453 }
454#endif
455
456 return 0;
457}