blob: e2e989ca05f7074a2109b288bd053e7cb21f82d8 [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
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "hal/hal_flash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020026
27#include <flash_map_backend/flash_map_backend.h>
28
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060030#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include "bootutil/sign_key.h"
32
Fabio Utzigba1fbe62017-07-21 14:01:20 -030033#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030034
Fabio Utzigba829042018-09-18 08:29:34 -030035#ifdef MCUBOOT_ENC_IMAGES
36#include "bootutil/enc_key.h"
37#endif
38#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060040#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030041#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080042#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060043#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080044#include "mbedtls/asn1.h"
45
46#include "bootutil_priv.h"
47
48/*
49 * Compute SHA256 over the image.
50 */
51static int
52bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
53 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
54 uint8_t *hash_result, uint8_t *seed, int seed_len)
55{
David Browne629bf32017-04-24 13:37:33 -060056 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080057 uint32_t blk_sz;
58 uint32_t size;
59 uint32_t off;
60 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -030061#ifdef MCUBOOT_ENC_IMAGES
62 uint32_t blk_off;
63 uint8_t idx;
64#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
David Browne629bf32017-04-24 13:37:33 -060066 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080067
68 /* in some cases (split image) the hash is seeded with data from
69 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020070 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060071 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080072 }
73
Fabio Utzigba829042018-09-18 08:29:34 -030074#ifdef MCUBOOT_ENC_IMAGES
75 /* Encrypted images only exist in slot1 */
76 if (fap->fa_id == FLASH_AREA_IMAGE_1 &&
77 (hdr->ih_flags & IMAGE_F_ENCRYPTED) &&
78 !boot_enc_valid(fap)) {
79 return -1;
80 }
81#endif
82
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 /*
84 * Hash is computed over image header and image itself. No TLV is
85 * included ATM.
86 */
87 size = hdr->ih_img_size + hdr->ih_hdr_size;
Fabio Utzigba829042018-09-18 08:29:34 -030088 assert(tmp_buf_sz > hdr->ih_hdr_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 for (off = 0; off < size; off += blk_sz) {
90 blk_sz = size - off;
91 if (blk_sz > tmp_buf_sz) {
92 blk_sz = tmp_buf_sz;
93 }
94 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
95 if (rc) {
96 return rc;
97 }
Fabio Utzigba829042018-09-18 08:29:34 -030098#ifdef MCUBOOT_ENC_IMAGES
99 if (fap->fa_id == FLASH_AREA_IMAGE_1 && hdr->ih_flags & IMAGE_F_ENCRYPTED) {
100 /* FIXME: fails if header size is larger than blk_sz */
101 if (off < hdr->ih_hdr_size) {
102 idx = hdr->ih_hdr_size;
103 blk_off = 0;
104 } else {
105 idx = 0;
106 blk_off = (off - hdr->ih_hdr_size) & 0xf;
107 }
108 boot_encrypt(fap, (off + idx) - hdr->ih_hdr_size, blk_sz - idx,
109 blk_off, &tmp_buf[idx]);
110 }
111#endif
David Browne629bf32017-04-24 13:37:33 -0600112 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 }
David Browne629bf32017-04-24 13:37:33 -0600114 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115
116 return 0;
117}
118
119/*
David Brown43cda332017-09-01 09:53:23 -0600120 * Currently, we only support being able to verify one type of
121 * signature, because there is a single verification function that we
122 * call. List the type of TLV we are expecting. If we aren't
123 * configured for any signature, don't define this macro.
124 */
125#if defined(MCUBOOT_SIGN_RSA)
Marko Kiiskila8dd56f32017-08-22 21:40:49 -0700126# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
David Brown43cda332017-09-01 09:53:23 -0600127# define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */
128# if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
129# error "Multiple signature types not yet supported"
130# endif
131#elif defined(MCUBOOT_SIGN_EC)
132# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
133# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
134# if defined(MCUBOOT_SIGN_EC256)
135# error "Multiple signature types not yet supported"
136# endif
137#elif defined(MCUBOOT_SIGN_EC256)
138# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
139# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
140#endif
141
142#ifdef EXPECTED_SIG_TLV
143static int
144bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
145{
146 bootutil_sha256_context sha256_ctx;
147 int i;
148 const struct bootutil_key *key;
149 uint8_t hash[32];
150
Fabio Utzig9911b182017-09-05 20:29:42 -0300151 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600152
153 for (i = 0; i < bootutil_key_cnt; i++) {
154 key = &bootutil_keys[i];
155 bootutil_sha256_init(&sha256_ctx);
156 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
157 bootutil_sha256_finish(&sha256_ctx, hash);
158 if (!memcmp(hash, keyhash, keyhash_len)) {
159 return i;
160 }
161 }
162 return -1;
163}
164#endif
165
166/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800167 * Verify the integrity of the image.
168 * Return non-zero if image could not be validated/does not validate.
169 */
170int
171bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
172 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
173 uint8_t *seed, int seed_len, uint8_t *out_hash)
174{
175 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600176 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600177 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600178 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600179#ifdef EXPECTED_SIG_TLV
180 int valid_signature = 0;
181 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182#endif
183 struct image_tlv tlv;
184 uint8_t buf[256];
185 uint8_t hash[32];
186 int rc;
187
Fabio Utzigba829042018-09-18 08:29:34 -0300188 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 if (rc) {
190 return rc;
191 }
192
193 if (out_hash) {
194 memcpy(out_hash, hash, 32);
195 }
196
David Brownf5b33d82017-09-01 10:58:27 -0600197 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 /* After image there are TLVs. */
199 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600200
201 rc = flash_area_read(fap, off, &info, sizeof(info));
202 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300203 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600204 }
205 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300206 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600207 }
David Brown3eaa2a12017-09-01 11:19:03 -0600208 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600209 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210
David Brown43cda332017-09-01 09:53:23 -0600211 /*
212 * Traverse through all of the TLVs, performing any checks we know
213 * and are able to do.
214 */
David Brown3eaa2a12017-09-01 11:19:03 -0600215 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
217 if (rc) {
218 return rc;
219 }
David Brown43cda332017-09-01 09:53:23 -0600220
Christopher Collins92ea77f2016-12-12 15:59:26 -0800221 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600222 /*
223 * Verify the SHA256 image hash. This must always be
224 * present.
225 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800226 if (tlv.it_len != sizeof(hash)) {
227 return -1;
228 }
David Brown43cda332017-09-01 09:53:23 -0600229 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
230 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300231 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232 }
David Brown43cda332017-09-01 09:53:23 -0600233 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300234 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800235 }
David Brown43cda332017-09-01 09:53:23 -0600236
237 sha256_valid = 1;
238#ifdef EXPECTED_SIG_TLV
239 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240 /*
David Brown43cda332017-09-01 09:53:23 -0600241 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 */
David Brown43cda332017-09-01 09:53:23 -0600243 if (tlv.it_len > 32) {
244 return -1;
245 }
246 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
247 if (rc) {
248 return rc;
249 }
250 key_id = bootutil_find_key(buf, tlv.it_len);
251 /*
252 * The key may not be found, which is acceptable. There
253 * can be multiple signatures, each preceded by a key.
254 */
255 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
256 /* Ignore this signature if it is out of bounds. */
257 if (key_id < 0 || key_id >= bootutil_key_cnt) {
258 key_id = -1;
259 continue;
260 }
261 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
262 return -1;
263 }
264 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
265 if (rc) {
266 return -1;
267 }
268 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
269 if (rc == 0) {
270 valid_signature = 1;
271 }
272 key_id = -1;
273#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 }
275 }
David Brown43cda332017-09-01 09:53:23 -0600276
277 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278 return -1;
279 }
280
David Brown43cda332017-09-01 09:53:23 -0600281#ifdef EXPECTED_SIG_TLV
282 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283 return -1;
284 }
285#endif
David Brown43cda332017-09-01 09:53:23 -0600286
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287 return 0;
288}