blob: 01efbcb6498410581c9c20f2ed56db0be25cfb8b [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
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
David Vinczee32483f2019-06-13 10:46:24 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include <assert.h>
25#include <stddef.h>
26#include <inttypes.h>
27#include <string.h>
28
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020029#include <flash_map_backend/flash_map_backend.h>
30
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060032#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "bootutil/sign_key.h"
34
Fabio Utzigba1fbe62017-07-21 14:01:20 -030035#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030036
Fabio Utzigba829042018-09-18 08:29:34 -030037#ifdef MCUBOOT_ENC_IMAGES
38#include "bootutil/enc_key.h"
39#endif
40#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060042#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030043#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080044#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060045#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080046#include "mbedtls/asn1.h"
47
48#include "bootutil_priv.h"
49
50/*
51 * Compute SHA256 over the image.
52 */
53static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030054bootutil_img_hash(struct enc_key_data *enc_state, 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)
Christopher Collins92ea77f2016-12-12 15:59:26 -080058{
David Browne629bf32017-04-24 13:37:33 -060059 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080060 uint32_t blk_sz;
61 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020062 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 uint32_t off;
64 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030065#ifdef MCUBOOT_ENC_IMAGES
66 uint32_t blk_off;
Fabio Utzigba829042018-09-18 08:29:34 -030067#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080068
Fabio Utzigb0f04732019-07-31 09:49:19 -030069#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030070 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030071 (void)image_index;
72#endif
73
David Browne629bf32017-04-24 13:37:33 -060074 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080075
76 /* in some cases (split image) the hash is seeded with data from
77 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020078 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060079 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080080 }
81
Fabio Utzigba829042018-09-18 08:29:34 -030082#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +010083 /* Encrypted images only exist in the secondary slot */
Fabio Utzigb0f04732019-07-31 09:49:19 -030084 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
Fabio Utzig10ee6482019-08-01 12:04:52 -030085 IS_ENCRYPTED(hdr) && !boot_enc_valid(enc_state, image_index, fap)) {
Fabio Utzigba829042018-09-18 08:29:34 -030086 return -1;
87 }
88#endif
89
David Vinczee32483f2019-06-13 10:46:24 +020090 /* Hash is computed over image header and image itself. */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020091 hdr_size = hdr->ih_hdr_size;
92 size = hdr->ih_img_size + hdr_size;
David Vinczee32483f2019-06-13 10:46:24 +020093
94#if (MCUBOOT_IMAGE_NUMBER > 1)
95 /* If dependency TLVs are present then the TLV info header and the
96 * dependency TLVs are also protected and have to be included in the hash
97 * calculation.
98 */
99 if (hdr->ih_protect_tlv_size != 0) {
100 size += hdr->ih_protect_tlv_size;
101 }
102#endif
103
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 for (off = 0; off < size; off += blk_sz) {
105 blk_sz = size - off;
106 if (blk_sz > tmp_buf_sz) {
107 blk_sz = tmp_buf_sz;
108 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200109#ifdef MCUBOOT_ENC_IMAGES
110 /* Avoid reading header data together with other image data
111 * because the header is not encrypted, so maintain correct
112 * bounds when sending encrypted data to decrypt routine.
113 */
114 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
115 blk_sz = hdr_size - off;
116 }
117#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
119 if (rc) {
120 return rc;
121 }
Fabio Utzigba829042018-09-18 08:29:34 -0300122#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigb0f04732019-07-31 09:49:19 -0300123 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
124 IS_ENCRYPTED(hdr) && off >= hdr_size) {
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200125 blk_off = (off - hdr_size) & 0xf;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300126 boot_encrypt(enc_state, image_index, fap, off - hdr_size, blk_sz,
127 blk_off, tmp_buf);
Fabio Utzigba829042018-09-18 08:29:34 -0300128 }
129#endif
David Browne629bf32017-04-24 13:37:33 -0600130 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131 }
David Browne629bf32017-04-24 13:37:33 -0600132 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133
134 return 0;
135}
136
137/*
David Brown43cda332017-09-01 09:53:23 -0600138 * Currently, we only support being able to verify one type of
139 * signature, because there is a single verification function that we
140 * call. List the type of TLV we are expecting. If we aren't
141 * configured for any signature, don't define this macro.
142 */
Fabio Utzig48764842019-05-10 19:28:24 -0300143#if (defined(MCUBOOT_SIGN_RSA) + \
144 defined(MCUBOOT_SIGN_EC) + \
145 defined(MCUBOOT_SIGN_EC256) + \
146 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700147#error "Only a single signature type is supported!"
148#endif
149
David Brown43cda332017-09-01 09:53:23 -0600150#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700151# if MCUBOOT_SIGN_RSA_LEN == 2048
152# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
153# elif MCUBOOT_SIGN_RSA_LEN == 3072
154# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
155# else
156# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600157# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700158# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
159# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600160#elif defined(MCUBOOT_SIGN_EC)
161# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700162# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600163# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
David Brown43cda332017-09-01 09:53:23 -0600164#elif defined(MCUBOOT_SIGN_EC256)
165# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700166# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600167# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
Fabio Utzig48764842019-05-10 19:28:24 -0300168#elif defined(MCUBOOT_SIGN_ED25519)
169# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
170# define SIG_BUF_SIZE 64
171# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700172#else
173# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600174#endif
175
176#ifdef EXPECTED_SIG_TLV
177static int
178bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
179{
180 bootutil_sha256_context sha256_ctx;
181 int i;
182 const struct bootutil_key *key;
183 uint8_t hash[32];
184
Fabio Utzig9911b182017-09-05 20:29:42 -0300185 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600186
187 for (i = 0; i < bootutil_key_cnt; i++) {
188 key = &bootutil_keys[i];
189 bootutil_sha256_init(&sha256_ctx);
190 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
191 bootutil_sha256_finish(&sha256_ctx, hash);
192 if (!memcmp(hash, keyhash, keyhash_len)) {
193 return i;
194 }
195 }
196 return -1;
197}
198#endif
199
200/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 * Verify the integrity of the image.
202 * Return non-zero if image could not be validated/does not validate.
203 */
204int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300205bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
206 struct image_header *hdr, const struct flash_area *fap,
207 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
208 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209{
210 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600211 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600212 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600213 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600214#ifdef EXPECTED_SIG_TLV
215 int valid_signature = 0;
216 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217#endif
218 struct image_tlv tlv;
Fabio Utzig3501c012019-05-13 15:07:25 -0700219 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800220 uint8_t hash[32];
221 int rc;
222
Fabio Utzig10ee6482019-08-01 12:04:52 -0300223 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
224 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800225 if (rc) {
226 return rc;
227 }
228
229 if (out_hash) {
230 memcpy(out_hash, hash, 32);
231 }
232
David Brownf5b33d82017-09-01 10:58:27 -0600233 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600235
236 rc = flash_area_read(fap, off, &info, sizeof(info));
237 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300238 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600239 }
240 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300241 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600242 }
David Brown3eaa2a12017-09-01 11:19:03 -0600243 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600244 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800245
David Brown43cda332017-09-01 09:53:23 -0600246 /*
247 * Traverse through all of the TLVs, performing any checks we know
248 * and are able to do.
249 */
David Brown3eaa2a12017-09-01 11:19:03 -0600250 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
252 if (rc) {
253 return rc;
254 }
David Brown43cda332017-09-01 09:53:23 -0600255
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600257 /*
258 * Verify the SHA256 image hash. This must always be
259 * present.
260 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261 if (tlv.it_len != sizeof(hash)) {
262 return -1;
263 }
David Brown43cda332017-09-01 09:53:23 -0600264 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
265 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300266 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267 }
David Brown43cda332017-09-01 09:53:23 -0600268 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300269 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270 }
David Brown43cda332017-09-01 09:53:23 -0600271
272 sha256_valid = 1;
273#ifdef EXPECTED_SIG_TLV
274 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800275 /*
David Brown43cda332017-09-01 09:53:23 -0600276 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800277 */
David Brown43cda332017-09-01 09:53:23 -0600278 if (tlv.it_len > 32) {
279 return -1;
280 }
281 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
282 if (rc) {
283 return rc;
284 }
285 key_id = bootutil_find_key(buf, tlv.it_len);
286 /*
287 * The key may not be found, which is acceptable. There
288 * can be multiple signatures, each preceded by a key.
289 */
290 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
291 /* Ignore this signature if it is out of bounds. */
292 if (key_id < 0 || key_id >= bootutil_key_cnt) {
293 key_id = -1;
294 continue;
295 }
296 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
297 return -1;
298 }
299 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
300 if (rc) {
301 return -1;
302 }
303 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
304 if (rc == 0) {
305 valid_signature = 1;
306 }
307 key_id = -1;
308#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309 }
310 }
David Brown43cda332017-09-01 09:53:23 -0600311
312 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 return -1;
314 }
315
David Brown43cda332017-09-01 09:53:23 -0600316#ifdef EXPECTED_SIG_TLV
317 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318 return -1;
319 }
320#endif
David Brown43cda332017-09-01 09:53:23 -0600321
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322 return 0;
323}