blob: 256d125756b17633e75b93b660adc3aacbc79d5f [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 Utzigb0f04732019-07-31 09:49:19 -030054bootutil_img_hash(int image_index, struct image_header *hdr,
55 const struct flash_area *fap, uint8_t *tmp_buf,
56 uint32_t tmp_buf_sz, uint8_t *hash_result, uint8_t *seed,
57 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)
70 (void)image_index;
71#endif
72
David Browne629bf32017-04-24 13:37:33 -060073 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080074
75 /* in some cases (split image) the hash is seeded with data from
76 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020077 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060078 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 }
80
Fabio Utzigba829042018-09-18 08:29:34 -030081#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +010082 /* Encrypted images only exist in the secondary slot */
Fabio Utzigb0f04732019-07-31 09:49:19 -030083 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
84 IS_ENCRYPTED(hdr) && !boot_enc_valid(image_index, fap)) {
Fabio Utzigba829042018-09-18 08:29:34 -030085 return -1;
86 }
87#endif
88
David Vinczee32483f2019-06-13 10:46:24 +020089 /* Hash is computed over image header and image itself. */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020090 hdr_size = hdr->ih_hdr_size;
91 size = hdr->ih_img_size + hdr_size;
David Vinczee32483f2019-06-13 10:46:24 +020092
93#if (MCUBOOT_IMAGE_NUMBER > 1)
94 /* If dependency TLVs are present then the TLV info header and the
95 * dependency TLVs are also protected and have to be included in the hash
96 * calculation.
97 */
98 if (hdr->ih_protect_tlv_size != 0) {
99 size += hdr->ih_protect_tlv_size;
100 }
101#endif
102
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103 for (off = 0; off < size; off += blk_sz) {
104 blk_sz = size - off;
105 if (blk_sz > tmp_buf_sz) {
106 blk_sz = tmp_buf_sz;
107 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200108#ifdef MCUBOOT_ENC_IMAGES
109 /* Avoid reading header data together with other image data
110 * because the header is not encrypted, so maintain correct
111 * bounds when sending encrypted data to decrypt routine.
112 */
113 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
114 blk_sz = hdr_size - off;
115 }
116#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
118 if (rc) {
119 return rc;
120 }
Fabio Utzigba829042018-09-18 08:29:34 -0300121#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigb0f04732019-07-31 09:49:19 -0300122 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
123 IS_ENCRYPTED(hdr) && off >= hdr_size) {
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200124 blk_off = (off - hdr_size) & 0xf;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300125 boot_encrypt(image_index, fap, off - hdr_size, blk_sz, blk_off,
126 tmp_buf);
Fabio Utzigba829042018-09-18 08:29:34 -0300127 }
128#endif
David Browne629bf32017-04-24 13:37:33 -0600129 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130 }
David Browne629bf32017-04-24 13:37:33 -0600131 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132
133 return 0;
134}
135
136/*
David Brown43cda332017-09-01 09:53:23 -0600137 * Currently, we only support being able to verify one type of
138 * signature, because there is a single verification function that we
139 * call. List the type of TLV we are expecting. If we aren't
140 * configured for any signature, don't define this macro.
141 */
Fabio Utzig48764842019-05-10 19:28:24 -0300142#if (defined(MCUBOOT_SIGN_RSA) + \
143 defined(MCUBOOT_SIGN_EC) + \
144 defined(MCUBOOT_SIGN_EC256) + \
145 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700146#error "Only a single signature type is supported!"
147#endif
148
David Brown43cda332017-09-01 09:53:23 -0600149#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700150# if MCUBOOT_SIGN_RSA_LEN == 2048
151# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
152# elif MCUBOOT_SIGN_RSA_LEN == 3072
153# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
154# else
155# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600156# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700157# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
158# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600159#elif defined(MCUBOOT_SIGN_EC)
160# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700161# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600162# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
David Brown43cda332017-09-01 09:53:23 -0600163#elif defined(MCUBOOT_SIGN_EC256)
164# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700165# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600166# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
Fabio Utzig48764842019-05-10 19:28:24 -0300167#elif defined(MCUBOOT_SIGN_ED25519)
168# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
169# define SIG_BUF_SIZE 64
170# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700171#else
172# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600173#endif
174
175#ifdef EXPECTED_SIG_TLV
176static int
177bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
178{
179 bootutil_sha256_context sha256_ctx;
180 int i;
181 const struct bootutil_key *key;
182 uint8_t hash[32];
183
Fabio Utzig9911b182017-09-05 20:29:42 -0300184 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600185
186 for (i = 0; i < bootutil_key_cnt; i++) {
187 key = &bootutil_keys[i];
188 bootutil_sha256_init(&sha256_ctx);
189 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
190 bootutil_sha256_finish(&sha256_ctx, hash);
191 if (!memcmp(hash, keyhash, keyhash_len)) {
192 return i;
193 }
194 }
195 return -1;
196}
197#endif
198
199/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800200 * Verify the integrity of the image.
201 * Return non-zero if image could not be validated/does not validate.
202 */
203int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300204bootutil_img_validate(int image_index, struct image_header *hdr,
205 const struct flash_area *fap, uint8_t *tmp_buf,
206 uint32_t tmp_buf_sz, uint8_t *seed, int seed_len,
207 uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800208{
209 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600210 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600211 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600212 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600213#ifdef EXPECTED_SIG_TLV
214 int valid_signature = 0;
215 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216#endif
217 struct image_tlv tlv;
Fabio Utzig3501c012019-05-13 15:07:25 -0700218 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219 uint8_t hash[32];
220 int rc;
221
Fabio Utzigb0f04732019-07-31 09:49:19 -0300222 rc = bootutil_img_hash(image_index, hdr, fap, tmp_buf, tmp_buf_sz, hash,
223 seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800224 if (rc) {
225 return rc;
226 }
227
228 if (out_hash) {
229 memcpy(out_hash, hash, 32);
230 }
231
David Brownf5b33d82017-09-01 10:58:27 -0600232 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600234
235 rc = flash_area_read(fap, off, &info, sizeof(info));
236 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300237 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600238 }
239 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300240 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600241 }
David Brown3eaa2a12017-09-01 11:19:03 -0600242 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600243 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244
David Brown43cda332017-09-01 09:53:23 -0600245 /*
246 * Traverse through all of the TLVs, performing any checks we know
247 * and are able to do.
248 */
David Brown3eaa2a12017-09-01 11:19:03 -0600249 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800250 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
251 if (rc) {
252 return rc;
253 }
David Brown43cda332017-09-01 09:53:23 -0600254
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600256 /*
257 * Verify the SHA256 image hash. This must always be
258 * present.
259 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260 if (tlv.it_len != sizeof(hash)) {
261 return -1;
262 }
David Brown43cda332017-09-01 09:53:23 -0600263 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
264 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300265 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266 }
David Brown43cda332017-09-01 09:53:23 -0600267 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300268 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269 }
David Brown43cda332017-09-01 09:53:23 -0600270
271 sha256_valid = 1;
272#ifdef EXPECTED_SIG_TLV
273 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 /*
David Brown43cda332017-09-01 09:53:23 -0600275 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276 */
David Brown43cda332017-09-01 09:53:23 -0600277 if (tlv.it_len > 32) {
278 return -1;
279 }
280 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
281 if (rc) {
282 return rc;
283 }
284 key_id = bootutil_find_key(buf, tlv.it_len);
285 /*
286 * The key may not be found, which is acceptable. There
287 * can be multiple signatures, each preceded by a key.
288 */
289 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
290 /* Ignore this signature if it is out of bounds. */
291 if (key_id < 0 || key_id >= bootutil_key_cnt) {
292 key_id = -1;
293 continue;
294 }
295 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
296 return -1;
297 }
298 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
299 if (rc) {
300 return -1;
301 }
302 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
303 if (rc == 0) {
304 valid_signature = 1;
305 }
306 key_id = -1;
307#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308 }
309 }
David Brown43cda332017-09-01 09:53:23 -0600310
311 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312 return -1;
313 }
314
David Brown43cda332017-09-01 09:53:23 -0600315#ifdef EXPECTED_SIG_TLV
316 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317 return -1;
318 }
319#endif
David Brown43cda332017-09-01 09:53:23 -0600320
Christopher Collins92ea77f2016-12-12 15:59:26 -0800321 return 0;
322}