blob: b329ea8736542c6e7187dc146b1db19cee095c5a [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
23 * Git SHA of the original version: 178be54bd6e5f035cc60e98205535682acd26e64
24 * 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
46/*
47 * Compute SHA256 over the image.
48 */
49static int
50bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
51 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
52 uint8_t *hash_result, uint8_t *seed, int seed_len)
53{
54 bootutil_sha256_context sha256_ctx;
55 uint32_t blk_sz;
56 uint32_t size;
57 uint32_t off;
Tamas Banf70ef8c2017-12-19 15:35:09 +000058
59 bootutil_sha256_init(&sha256_ctx);
60
61 /* in some cases (split image) the hash is seeded with data from
62 * the loader image */
Tamas Ban581034a2017-12-19 19:54:37 +000063 if (seed && (seed_len > 0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +000064 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
65 }
66
David Vinczedb32b212019-04-16 17:43:57 +020067 /* Hash is computed over image header and image itself. */
David Vincze39e78552018-10-10 17:10:01 +020068 size = hdr->ih_img_size + hdr->ih_hdr_size;
David Vinczedb32b212019-04-16 17:43:57 +020069
70 /* If a security counter TLV is present then the TLV info header and the
71 * security counter are also protected and must be included in the hash
72 * calculation.
73 */
74 if (hdr->ih_protect_tlv_size != 0) {
75 size += hdr->ih_protect_tlv_size;
76 }
77
Tamas Banf70ef8c2017-12-19 15:35:09 +000078 for (off = 0; off < size; off += blk_sz) {
79 blk_sz = size - off;
80 if (blk_sz > tmp_buf_sz) {
81 blk_sz = tmp_buf_sz;
82 }
Oliver Swedef9982442018-08-24 18:37:44 +010083
84#ifdef MCUBOOT_RAM_LOADING
85 if (fap == NULL) { /* The image is in SRAM */
86 memcpy(tmp_buf, (uint32_t *)(hdr->ih_load_addr + off), blk_sz);
87 } else { /* The image is in flash */
88#endif
89 if(flash_area_read(fap, off, tmp_buf, blk_sz)) {
90 return -1;
91 }
92#ifdef MCUBOOT_RAM_LOADING
Tamas Banf70ef8c2017-12-19 15:35:09 +000093 }
Oliver Swedef9982442018-08-24 18:37:44 +010094#endif
95
Tamas Banf70ef8c2017-12-19 15:35:09 +000096 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
97 }
98 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
125static int
126bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
127{
128 bootutil_sha256_context sha256_ctx;
129 int i;
130 const struct bootutil_key *key;
131 uint8_t hash[32];
132
133 assert(keyhash_len <= 32);
134
135 for (i = 0; i < bootutil_key_cnt; i++) {
136 key = &bootutil_keys[i];
137 bootutil_sha256_init(&sha256_ctx);
138 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
139 bootutil_sha256_finish(&sha256_ctx, hash);
140 if (!memcmp(hash, keyhash, keyhash_len)) {
141 return i;
142 }
143 }
144 return -1;
145}
146#endif
147
Oliver Swedef9982442018-08-24 18:37:44 +0100148#ifdef MCUBOOT_RAM_LOADING
149/* Check the hash of an image after it has been copied to SRAM */
150int
151bootutil_check_hash_after_loading(struct image_header *hdr)
152{
153 uint32_t off;
154 uint32_t end;
155 int sha256_valid = 0;
156 struct image_tlv_info info;
157 struct image_tlv tlv;
158 uint8_t tmp_buf[BOOT_TMPBUF_SZ];
159 uint8_t hash[32] = {0};
160 int rc;
161 uint32_t load_address;
162 uint32_t tlv_sz;
163
164 rc = bootutil_img_hash(hdr, NULL, tmp_buf, BOOT_TMPBUF_SZ, hash, NULL, 0);
165
166 if (rc) {
167 return rc;
168 }
169
170 load_address = (uint32_t) hdr->ih_load_addr;
171
172 /* The TLVs come after the image. */
173 off = hdr->ih_img_size + hdr->ih_hdr_size;
174
175 info = *((struct image_tlv_info *)(load_address + off));
176
177 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200178 return BOOT_EBADMAGIC;
Oliver Swedef9982442018-08-24 18:37:44 +0100179 }
180 end = off + info.it_tlv_tot;
181 off += sizeof(info);
182
183 /*
184 * Traverse through all of the TLVs, performing any checks we know
185 * and are able to do.
186 */
David Vincze060968d2019-05-23 01:13:14 +0200187 while (off < end) {
Oliver Swedef9982442018-08-24 18:37:44 +0100188 tlv = *((struct image_tlv *)(load_address + off));
189 tlv_sz = sizeof(tlv);
190
191 if (tlv.it_type == IMAGE_TLV_SHA256) {
192 /*
193 * Verify the SHA256 image hash. This must always be present.
194 */
195 if (tlv.it_len != sizeof(hash)) {
196 return -1;
197 }
198
199 if (memcmp(hash, (uint32_t *)(load_address + off + tlv_sz),
200 sizeof(hash))) {
201 return -1;
202 }
203
204 sha256_valid = 1;
205 }
David Vincze060968d2019-05-23 01:13:14 +0200206
207 /* Avoid integer overflow. */
208 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
209 /* Potential overflow. */
210 break;
211 } else {
212 off += sizeof(tlv) + tlv.it_len;
213 }
Oliver Swedef9982442018-08-24 18:37:44 +0100214 }
215
216 if (!sha256_valid) {
217 return -1;
218 }
219
220 return 0;
221}
222#endif /* MCUBOOT_RAM_LOADING */
223
David Vincze060968d2019-05-23 01:13:14 +0200224/**
225 * Reads the value of an image's security counter.
226 *
227 * @param hdr Pointer to the image header structure.
228 * @param fap Pointer to a description structure of the image's
229 * flash area.
230 * @param security_cnt Pointer to store the security counter value.
231 *
232 * @return 0 on success; nonzero on failure.
233 */
234int32_t
235bootutil_get_img_security_cnt(struct image_header *hdr,
236 const struct flash_area *fap,
237 uint32_t *img_security_cnt)
238{
239 struct image_tlv_info info;
240 struct image_tlv tlv;
241 uint32_t off;
242 uint32_t end;
243 uint32_t found = 0;
244 int32_t rc;
245
246 if ((hdr == NULL) ||
247 (fap == NULL) ||
248 (img_security_cnt == NULL)) {
249 /* Invalid parameter. */
250 return BOOT_EBADARGS;
251 }
252
253 /* The TLVs come after the image. */
254 off = hdr->ih_hdr_size + hdr->ih_img_size;
255
256 /* The TLV area always starts with an image_tlv_info structure. */
257 rc = flash_area_read(fap, off, &info, sizeof(info));
258 if (rc != 0) {
259 return BOOT_EFLASH;
260 }
261
262 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
263 return BOOT_EBADMAGIC;
264 }
265
266 /* The security counter TLV is in the protected part of the TLV area. */
267 if (hdr->ih_protect_tlv_size != 0) {
268 end = off + (uint32_t)hdr->ih_protect_tlv_size;
269 off += sizeof(info);
270
271 /* Traverse through the protected TLV area to find the
272 * security counter TLV.
273 */
274 while (off < end) {
275 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
276 if (rc != 0) {
277 return BOOT_EFLASH;
278 }
279
280 if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
281
282 if (tlv.it_len != sizeof(*img_security_cnt)) {
283 /* Security counter is not valid. */
284 break;
285 }
286
287 rc = flash_area_read(fap, off + sizeof(tlv),
288 img_security_cnt, tlv.it_len);
289 if (rc != 0) {
290 return BOOT_EFLASH;
291 }
292
293 /* Security counter has been found. */
294 found = 1;
295 break;
296 }
297
298 /* Avoid integer overflow. */
299 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
300 /* Potential overflow. */
301 break;
302 } else {
303 off += sizeof(tlv) + tlv.it_len;
304 }
305 }
306 }
307
308 if (found) {
309 return 0;
310 }
311
312 return -1;
313}
314
Tamas Banf70ef8c2017-12-19 15:35:09 +0000315/*
316 * Verify the integrity of the image.
317 * Return non-zero if image could not be validated/does not validate.
318 */
319int
320bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
321 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
322 uint8_t *seed, int seed_len, uint8_t *out_hash)
323{
324 uint32_t off;
325 uint32_t end;
326 int sha256_valid = 0;
327 struct image_tlv_info info;
328#ifdef EXPECTED_SIG_TLV
329 int valid_signature = 0;
330 int key_id = -1;
331#endif
332 struct image_tlv tlv;
Tamas Ban81daed02019-05-20 15:05:22 +0100333 uint8_t buf[SIG_BUF_SIZE];
Tamas Ban581034a2017-12-19 19:54:37 +0000334 uint8_t hash[32] = {0};
David Vincze060968d2019-05-23 01:13:14 +0200335 uint32_t security_cnt;
336 uint32_t img_security_cnt;
337 int32_t security_counter_valid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000338 int rc;
339
340 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
341 seed, seed_len);
342 if (rc) {
343 return rc;
344 }
345
346 if (out_hash) {
347 memcpy(out_hash, hash, 32);
348 }
349
350 /* The TLVs come after the image. */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000351 off = hdr->ih_img_size + hdr->ih_hdr_size;
352
353 rc = flash_area_read(fap, off, &info, sizeof(info));
354 if (rc) {
355 return rc;
356 }
357 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
David Vincze060968d2019-05-23 01:13:14 +0200358 return BOOT_EBADMAGIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000359 }
360 end = off + info.it_tlv_tot;
361 off += sizeof(info);
362
363 /*
364 * Traverse through all of the TLVs, performing any checks we know
365 * and are able to do.
366 */
David Vincze060968d2019-05-23 01:13:14 +0200367 while (off < end) {
Tamas Ban581034a2017-12-19 19:54:37 +0000368 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000369 if (rc) {
370 return rc;
371 }
372
373 if (tlv.it_type == IMAGE_TLV_SHA256) {
374 /*
375 * Verify the SHA256 image hash. This must always be
376 * present.
377 */
378 if (tlv.it_len != sizeof(hash)) {
379 return -1;
380 }
Tamas Ban581034a2017-12-19 19:54:37 +0000381 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof(hash));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000382 if (rc) {
383 return rc;
384 }
385 if (memcmp(hash, buf, sizeof(hash))) {
386 return -1;
387 }
388
389 sha256_valid = 1;
390#ifdef EXPECTED_SIG_TLV
391 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
392 /*
393 * Determine which key we should be checking.
394 */
395 if (tlv.it_len > 32) {
396 return -1;
397 }
Tamas Ban581034a2017-12-19 19:54:37 +0000398 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000399 if (rc) {
400 return rc;
401 }
402 key_id = bootutil_find_key(buf, tlv.it_len);
403 /*
404 * The key may not be found, which is acceptable. There
405 * can be multiple signatures, each preceded by a key.
406 */
407 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
408 /* Ignore this signature if it is out of bounds. */
David Vinczefb068302019-07-24 11:00:22 +0200409 if (key_id >= 0 && key_id < bootutil_key_cnt) {
410 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
411 return -1;
412 }
413 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
414 if (rc) {
415 return -1;
416 }
417 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len,
418 key_id);
419 if (rc == 0) {
420 valid_signature = 1;
421 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000422 }
423 key_id = -1;
424#endif
David Vincze060968d2019-05-23 01:13:14 +0200425 } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) {
426 /*
427 * Verify the image's security counter.
428 * This must always be present.
429 */
430 if (tlv.it_len != sizeof(img_security_cnt)) {
431 /* Security counter is not valid. */
432 return -1;
433 }
434
435 rc = flash_area_read(fap, off + sizeof(tlv),
436 &img_security_cnt, tlv.it_len);
437 if (rc) {
438 return rc;
439 }
440
441 rc = boot_nv_security_counter_get(0, &security_cnt);
442 if (rc) {
443 return rc;
444 }
445
446 /* Compare the new image's security counter value against the
447 * stored security counter value.
448 */
449 if (img_security_cnt < security_cnt) {
450 /* The image's security counter is not accepted. */
451 return -1;
452 }
453
454 /* The image's security counter has been successfully verified. */
455 security_counter_valid = 1;
456 }
457
458 /* Avoid integer overflow. */
459 if ((UINT32_MAX - off) < (sizeof(tlv) + tlv.it_len)) {
460 /* Potential overflow. */
461 break;
462 } else {
463 off += sizeof(tlv) + tlv.it_len;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000464 }
465 }
466
David Vincze060968d2019-05-23 01:13:14 +0200467 if (!sha256_valid || !security_counter_valid) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000468 return -1;
469 }
470
471#ifdef EXPECTED_SIG_TLV
472 if (!valid_signature) {
473 return -1;
474 }
475#endif
476
477 return 0;
478}