blob: 9a5fe57b87cee9746abe11626dd6cdcc0cf91724 [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
20#include <assert.h>
21#include <stddef.h>
22#include <inttypes.h>
23#include <string.h>
24
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020025#include <flash_map_backend/flash_map_backend.h>
26
Christopher Collins92ea77f2016-12-12 15:59:26 -080027#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060028#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include "bootutil/sign_key.h"
30
Fabio Utzigba1fbe62017-07-21 14:01:20 -030031#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030032
Fabio Utzigba829042018-09-18 08:29:34 -030033#ifdef MCUBOOT_ENC_IMAGES
34#include "bootutil/enc_key.h"
35#endif
36#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060038#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030039#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060041#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080042#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{
David Browne629bf32017-04-24 13:37:33 -060054 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080055 uint32_t blk_sz;
56 uint32_t size;
Fabio Utzig2fc80df2018-12-14 06:47:38 -020057 uint16_t hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080058 uint32_t off;
59 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030060#ifdef MCUBOOT_ENC_IMAGES
61 uint32_t blk_off;
Fabio Utzigba829042018-09-18 08:29:34 -030062#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080063
David Browne629bf32017-04-24 13:37:33 -060064 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
66 /* in some cases (split image) the hash is seeded with data from
67 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020068 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060069 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080070 }
71
Fabio Utzigba829042018-09-18 08:29:34 -030072#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +010073 /* Encrypted images only exist in the secondary slot */
74 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY &&
75 IS_ENCRYPTED(hdr) &&
76 !boot_enc_valid(fap)) {
Fabio Utzigba829042018-09-18 08:29:34 -030077 return -1;
78 }
79#endif
80
Christopher Collins92ea77f2016-12-12 15:59:26 -080081 /*
82 * Hash is computed over image header and image itself. No TLV is
83 * included ATM.
84 */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020085 hdr_size = hdr->ih_hdr_size;
86 size = hdr->ih_img_size + hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 for (off = 0; off < size; off += blk_sz) {
88 blk_sz = size - off;
89 if (blk_sz > tmp_buf_sz) {
90 blk_sz = tmp_buf_sz;
91 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -020092#ifdef MCUBOOT_ENC_IMAGES
93 /* Avoid reading header data together with other image data
94 * because the header is not encrypted, so maintain correct
95 * bounds when sending encrypted data to decrypt routine.
96 */
97 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
98 blk_sz = hdr_size - off;
99 }
100#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
102 if (rc) {
103 return rc;
104 }
Fabio Utzigba829042018-09-18 08:29:34 -0300105#ifdef MCUBOOT_ENC_IMAGES
David Vincze2d736ad2019-02-18 11:50:22 +0100106 if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY &&
107 IS_ENCRYPTED(hdr) &&
108 off >= hdr_size) {
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200109 blk_off = (off - hdr_size) & 0xf;
110 boot_encrypt(fap, off - hdr_size, blk_sz, blk_off, tmp_buf);
Fabio Utzigba829042018-09-18 08:29:34 -0300111 }
112#endif
David Browne629bf32017-04-24 13:37:33 -0600113 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 }
David Browne629bf32017-04-24 13:37:33 -0600115 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116
117 return 0;
118}
119
120/*
David Brown43cda332017-09-01 09:53:23 -0600121 * Currently, we only support being able to verify one type of
122 * signature, because there is a single verification function that we
123 * call. List the type of TLV we are expecting. If we aren't
124 * configured for any signature, don't define this macro.
125 */
126#if defined(MCUBOOT_SIGN_RSA)
Marko Kiiskila8dd56f32017-08-22 21:40:49 -0700127# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
David Brown43cda332017-09-01 09:53:23 -0600128# define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */
129# if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
130# error "Multiple signature types not yet supported"
131# endif
132#elif defined(MCUBOOT_SIGN_EC)
133# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
134# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
135# if defined(MCUBOOT_SIGN_EC256)
136# error "Multiple signature types not yet supported"
137# endif
138#elif defined(MCUBOOT_SIGN_EC256)
139# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
140# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
141#endif
142
143#ifdef EXPECTED_SIG_TLV
144static int
145bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
146{
147 bootutil_sha256_context sha256_ctx;
148 int i;
149 const struct bootutil_key *key;
150 uint8_t hash[32];
151
Fabio Utzig9911b182017-09-05 20:29:42 -0300152 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600153
154 for (i = 0; i < bootutil_key_cnt; i++) {
155 key = &bootutil_keys[i];
156 bootutil_sha256_init(&sha256_ctx);
157 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
158 bootutil_sha256_finish(&sha256_ctx, hash);
159 if (!memcmp(hash, keyhash, keyhash_len)) {
160 return i;
161 }
162 }
163 return -1;
164}
165#endif
166
167/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 * Verify the integrity of the image.
169 * Return non-zero if image could not be validated/does not validate.
170 */
171int
172bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
173 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
174 uint8_t *seed, int seed_len, uint8_t *out_hash)
175{
176 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600177 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600178 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600179 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600180#ifdef EXPECTED_SIG_TLV
181 int valid_signature = 0;
182 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183#endif
184 struct image_tlv tlv;
185 uint8_t buf[256];
186 uint8_t hash[32];
187 int rc;
188
Fabio Utzigba829042018-09-18 08:29:34 -0300189 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190 if (rc) {
191 return rc;
192 }
193
194 if (out_hash) {
195 memcpy(out_hash, hash, 32);
196 }
197
David Brownf5b33d82017-09-01 10:58:27 -0600198 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 /* After image there are TLVs. */
200 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600201
202 rc = flash_area_read(fap, off, &info, sizeof(info));
203 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300204 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600205 }
206 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300207 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600208 }
David Brown3eaa2a12017-09-01 11:19:03 -0600209 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600210 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211
David Brown43cda332017-09-01 09:53:23 -0600212 /*
213 * Traverse through all of the TLVs, performing any checks we know
214 * and are able to do.
215 */
David Brown3eaa2a12017-09-01 11:19:03 -0600216 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
218 if (rc) {
219 return rc;
220 }
David Brown43cda332017-09-01 09:53:23 -0600221
Christopher Collins92ea77f2016-12-12 15:59:26 -0800222 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600223 /*
224 * Verify the SHA256 image hash. This must always be
225 * present.
226 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800227 if (tlv.it_len != sizeof(hash)) {
228 return -1;
229 }
David Brown43cda332017-09-01 09:53:23 -0600230 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
231 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300232 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233 }
David Brown43cda332017-09-01 09:53:23 -0600234 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300235 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800236 }
David Brown43cda332017-09-01 09:53:23 -0600237
238 sha256_valid = 1;
239#ifdef EXPECTED_SIG_TLV
240 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800241 /*
David Brown43cda332017-09-01 09:53:23 -0600242 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243 */
David Brown43cda332017-09-01 09:53:23 -0600244 if (tlv.it_len > 32) {
245 return -1;
246 }
247 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
248 if (rc) {
249 return rc;
250 }
251 key_id = bootutil_find_key(buf, tlv.it_len);
252 /*
253 * The key may not be found, which is acceptable. There
254 * can be multiple signatures, each preceded by a key.
255 */
256 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
257 /* Ignore this signature if it is out of bounds. */
258 if (key_id < 0 || key_id >= bootutil_key_cnt) {
259 key_id = -1;
260 continue;
261 }
262 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
263 return -1;
264 }
265 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
266 if (rc) {
267 return -1;
268 }
269 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
270 if (rc == 0) {
271 valid_signature = 1;
272 }
273 key_id = -1;
274#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800275 }
276 }
David Brown43cda332017-09-01 09:53:23 -0600277
278 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279 return -1;
280 }
281
David Brown43cda332017-09-01 09:53:23 -0600282#ifdef EXPECTED_SIG_TLV
283 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284 return -1;
285 }
286#endif
David Brown43cda332017-09-01 09:53:23 -0600287
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288 return 0;
289}