blob: b4defb20b75567d76b12c53559e519961e154fb0 [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
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{
David Browne629bf32017-04-24 13:37:33 -060058 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080059 uint32_t blk_sz;
60 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020061 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080062 uint32_t off;
63 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030064#ifdef MCUBOOT_ENC_IMAGES
65 uint32_t blk_off;
Fabio Utzigba829042018-09-18 08:29:34 -030066#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080067
David Browne629bf32017-04-24 13:37:33 -060068 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080069
70 /* in some cases (split image) the hash is seeded with data from
71 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020072 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060073 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 }
75
Fabio Utzigba829042018-09-18 08:29:34 -030076#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +010077 /* Encrypted images only exist in the secondary slot */
78 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY &&
79 IS_ENCRYPTED(hdr) &&
80 !boot_enc_valid(fap)) {
Fabio Utzigba829042018-09-18 08:29:34 -030081 return -1;
82 }
83#endif
84
David Vinczee32483f2019-06-13 10:46:24 +020085 /* Hash is computed over image header and image itself. */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020086 hdr_size = hdr->ih_hdr_size;
87 size = hdr->ih_img_size + hdr_size;
David Vinczee32483f2019-06-13 10:46:24 +020088
89#if (MCUBOOT_IMAGE_NUMBER > 1)
90 /* If dependency TLVs are present then the TLV info header and the
91 * dependency TLVs are also protected and have to be included in the hash
92 * calculation.
93 */
94 if (hdr->ih_protect_tlv_size != 0) {
95 size += hdr->ih_protect_tlv_size;
96 }
97#endif
98
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 for (off = 0; off < size; off += blk_sz) {
100 blk_sz = size - off;
101 if (blk_sz > tmp_buf_sz) {
102 blk_sz = tmp_buf_sz;
103 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200104#ifdef MCUBOOT_ENC_IMAGES
105 /* Avoid reading header data together with other image data
106 * because the header is not encrypted, so maintain correct
107 * bounds when sending encrypted data to decrypt routine.
108 */
109 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
110 blk_sz = hdr_size - off;
111 }
112#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
114 if (rc) {
115 return rc;
116 }
Fabio Utzigba829042018-09-18 08:29:34 -0300117#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100118 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY &&
119 IS_ENCRYPTED(hdr) &&
120 off >= hdr_size) {
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200121 blk_off = (off - hdr_size) & 0xf;
122 boot_encrypt(fap, off - hdr_size, blk_sz, blk_off, tmp_buf);
Fabio Utzigba829042018-09-18 08:29:34 -0300123 }
124#endif
David Browne629bf32017-04-24 13:37:33 -0600125 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 }
David Browne629bf32017-04-24 13:37:33 -0600127 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800128
129 return 0;
130}
131
132/*
David Brown43cda332017-09-01 09:53:23 -0600133 * Currently, we only support being able to verify one type of
134 * signature, because there is a single verification function that we
135 * call. List the type of TLV we are expecting. If we aren't
136 * configured for any signature, don't define this macro.
137 */
Fabio Utzig48764842019-05-10 19:28:24 -0300138#if (defined(MCUBOOT_SIGN_RSA) + \
139 defined(MCUBOOT_SIGN_EC) + \
140 defined(MCUBOOT_SIGN_EC256) + \
141 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700142#error "Only a single signature type is supported!"
143#endif
144
David Brown43cda332017-09-01 09:53:23 -0600145#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700146# if MCUBOOT_SIGN_RSA_LEN == 2048
147# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
148# elif MCUBOOT_SIGN_RSA_LEN == 3072
149# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
150# else
151# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600152# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700153# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
154# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600155#elif defined(MCUBOOT_SIGN_EC)
156# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700157# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600158# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
David Brown43cda332017-09-01 09:53:23 -0600159#elif defined(MCUBOOT_SIGN_EC256)
160# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
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) >= 72) /* oids + 2 * 32 bytes */
Fabio Utzig48764842019-05-10 19:28:24 -0300163#elif defined(MCUBOOT_SIGN_ED25519)
164# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
165# define SIG_BUF_SIZE 64
166# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700167#else
168# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600169#endif
170
171#ifdef EXPECTED_SIG_TLV
172static int
173bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
174{
175 bootutil_sha256_context sha256_ctx;
176 int i;
177 const struct bootutil_key *key;
178 uint8_t hash[32];
179
Fabio Utzig9911b182017-09-05 20:29:42 -0300180 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600181
182 for (i = 0; i < bootutil_key_cnt; i++) {
183 key = &bootutil_keys[i];
184 bootutil_sha256_init(&sha256_ctx);
185 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
186 bootutil_sha256_finish(&sha256_ctx, hash);
187 if (!memcmp(hash, keyhash, keyhash_len)) {
188 return i;
189 }
190 }
191 return -1;
192}
193#endif
194
195/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196 * Verify the integrity of the image.
197 * Return non-zero if image could not be validated/does not validate.
198 */
199int
200bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
201 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
202 uint8_t *seed, int seed_len, uint8_t *out_hash)
203{
204 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600205 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600206 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600207 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600208#ifdef EXPECTED_SIG_TLV
209 int valid_signature = 0;
210 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211#endif
212 struct image_tlv tlv;
Fabio Utzig3501c012019-05-13 15:07:25 -0700213 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214 uint8_t hash[32];
215 int rc;
216
Fabio Utzigba829042018-09-18 08:29:34 -0300217 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218 if (rc) {
219 return rc;
220 }
221
222 if (out_hash) {
223 memcpy(out_hash, hash, 32);
224 }
225
David Brownf5b33d82017-09-01 10:58:27 -0600226 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800227 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600228
229 rc = flash_area_read(fap, off, &info, sizeof(info));
230 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300231 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600232 }
233 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300234 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600235 }
David Brown3eaa2a12017-09-01 11:19:03 -0600236 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600237 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238
David Brown43cda332017-09-01 09:53:23 -0600239 /*
240 * Traverse through all of the TLVs, performing any checks we know
241 * and are able to do.
242 */
David Brown3eaa2a12017-09-01 11:19:03 -0600243 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
245 if (rc) {
246 return rc;
247 }
David Brown43cda332017-09-01 09:53:23 -0600248
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600250 /*
251 * Verify the SHA256 image hash. This must always be
252 * present.
253 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800254 if (tlv.it_len != sizeof(hash)) {
255 return -1;
256 }
David Brown43cda332017-09-01 09:53:23 -0600257 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
258 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300259 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260 }
David Brown43cda332017-09-01 09:53:23 -0600261 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300262 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263 }
David Brown43cda332017-09-01 09:53:23 -0600264
265 sha256_valid = 1;
266#ifdef EXPECTED_SIG_TLV
267 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268 /*
David Brown43cda332017-09-01 09:53:23 -0600269 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270 */
David Brown43cda332017-09-01 09:53:23 -0600271 if (tlv.it_len > 32) {
272 return -1;
273 }
274 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
275 if (rc) {
276 return rc;
277 }
278 key_id = bootutil_find_key(buf, tlv.it_len);
279 /*
280 * The key may not be found, which is acceptable. There
281 * can be multiple signatures, each preceded by a key.
282 */
283 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
284 /* Ignore this signature if it is out of bounds. */
285 if (key_id < 0 || key_id >= bootutil_key_cnt) {
286 key_id = -1;
287 continue;
288 }
289 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
290 return -1;
291 }
292 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
293 if (rc) {
294 return -1;
295 }
296 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
297 if (rc == 0) {
298 valid_signature = 1;
299 }
300 key_id = -1;
301#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 }
303 }
David Brown43cda332017-09-01 09:53:23 -0600304
305 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306 return -1;
307 }
308
David Brown43cda332017-09-01 09:53:23 -0600309#ifdef EXPECTED_SIG_TLV
310 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311 return -1;
312 }
313#endif
David Brown43cda332017-09-01 09:53:23 -0600314
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315 return 0;
316}