blob: de2aa0e5f9febe576ec0c3e2dabcaaf7c28373be [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
73 /* Encrypted images only exist in slot1 */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020074 if (fap->fa_id == FLASH_AREA_IMAGE_1 && IS_ENCRYPTED(hdr) && !boot_enc_valid(fap)) {
Fabio Utzigba829042018-09-18 08:29:34 -030075 return -1;
76 }
77#endif
78
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 /*
80 * Hash is computed over image header and image itself. No TLV is
81 * included ATM.
82 */
Fabio Utzig2fc80df2018-12-14 06:47:38 -020083 hdr_size = hdr->ih_hdr_size;
84 size = hdr->ih_img_size + hdr_size;
Christopher Collins92ea77f2016-12-12 15:59:26 -080085 for (off = 0; off < size; off += blk_sz) {
86 blk_sz = size - off;
87 if (blk_sz > tmp_buf_sz) {
88 blk_sz = tmp_buf_sz;
89 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -020090#ifdef MCUBOOT_ENC_IMAGES
91 /* Avoid reading header data together with other image data
92 * because the header is not encrypted, so maintain correct
93 * bounds when sending encrypted data to decrypt routine.
94 */
95 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
96 blk_sz = hdr_size - off;
97 }
98#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
100 if (rc) {
101 return rc;
102 }
Fabio Utzigba829042018-09-18 08:29:34 -0300103#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200104 if (fap->fa_id == FLASH_AREA_IMAGE_1 && IS_ENCRYPTED(hdr) && off >= hdr_size) {
105 blk_off = (off - hdr_size) & 0xf;
106 boot_encrypt(fap, off - hdr_size, blk_sz, blk_off, tmp_buf);
Fabio Utzigba829042018-09-18 08:29:34 -0300107 }
108#endif
David Browne629bf32017-04-24 13:37:33 -0600109 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800110 }
David Browne629bf32017-04-24 13:37:33 -0600111 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112
113 return 0;
114}
115
116/*
David Brown43cda332017-09-01 09:53:23 -0600117 * Currently, we only support being able to verify one type of
118 * signature, because there is a single verification function that we
119 * call. List the type of TLV we are expecting. If we aren't
120 * configured for any signature, don't define this macro.
121 */
122#if defined(MCUBOOT_SIGN_RSA)
Marko Kiiskila8dd56f32017-08-22 21:40:49 -0700123# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
David Brown43cda332017-09-01 09:53:23 -0600124# define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */
125# if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
126# error "Multiple signature types not yet supported"
127# endif
128#elif defined(MCUBOOT_SIGN_EC)
129# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
130# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
131# if defined(MCUBOOT_SIGN_EC256)
132# error "Multiple signature types not yet supported"
133# endif
134#elif defined(MCUBOOT_SIGN_EC256)
135# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
136# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
137#endif
138
139#ifdef EXPECTED_SIG_TLV
140static int
141bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
142{
143 bootutil_sha256_context sha256_ctx;
144 int i;
145 const struct bootutil_key *key;
146 uint8_t hash[32];
147
Fabio Utzig9911b182017-09-05 20:29:42 -0300148 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600149
150 for (i = 0; i < bootutil_key_cnt; i++) {
151 key = &bootutil_keys[i];
152 bootutil_sha256_init(&sha256_ctx);
153 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
154 bootutil_sha256_finish(&sha256_ctx, hash);
155 if (!memcmp(hash, keyhash, keyhash_len)) {
156 return i;
157 }
158 }
159 return -1;
160}
161#endif
162
163/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164 * Verify the integrity of the image.
165 * Return non-zero if image could not be validated/does not validate.
166 */
167int
168bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
169 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
170 uint8_t *seed, int seed_len, uint8_t *out_hash)
171{
172 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600173 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600174 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600175 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600176#ifdef EXPECTED_SIG_TLV
177 int valid_signature = 0;
178 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179#endif
180 struct image_tlv tlv;
181 uint8_t buf[256];
182 uint8_t hash[32];
183 int rc;
184
Fabio Utzigba829042018-09-18 08:29:34 -0300185 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186 if (rc) {
187 return rc;
188 }
189
190 if (out_hash) {
191 memcpy(out_hash, hash, 32);
192 }
193
David Brownf5b33d82017-09-01 10:58:27 -0600194 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195 /* After image there are TLVs. */
196 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600197
198 rc = flash_area_read(fap, off, &info, sizeof(info));
199 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300200 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600201 }
202 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300203 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600204 }
David Brown3eaa2a12017-09-01 11:19:03 -0600205 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600206 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207
David Brown43cda332017-09-01 09:53:23 -0600208 /*
209 * Traverse through all of the TLVs, performing any checks we know
210 * and are able to do.
211 */
David Brown3eaa2a12017-09-01 11:19:03 -0600212 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800213 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
214 if (rc) {
215 return rc;
216 }
David Brown43cda332017-09-01 09:53:23 -0600217
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600219 /*
220 * Verify the SHA256 image hash. This must always be
221 * present.
222 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800223 if (tlv.it_len != sizeof(hash)) {
224 return -1;
225 }
David Brown43cda332017-09-01 09:53:23 -0600226 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
227 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300228 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800229 }
David Brown43cda332017-09-01 09:53:23 -0600230 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300231 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232 }
David Brown43cda332017-09-01 09:53:23 -0600233
234 sha256_valid = 1;
235#ifdef EXPECTED_SIG_TLV
236 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237 /*
David Brown43cda332017-09-01 09:53:23 -0600238 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239 */
David Brown43cda332017-09-01 09:53:23 -0600240 if (tlv.it_len > 32) {
241 return -1;
242 }
243 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
244 if (rc) {
245 return rc;
246 }
247 key_id = bootutil_find_key(buf, tlv.it_len);
248 /*
249 * The key may not be found, which is acceptable. There
250 * can be multiple signatures, each preceded by a key.
251 */
252 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
253 /* Ignore this signature if it is out of bounds. */
254 if (key_id < 0 || key_id >= bootutil_key_cnt) {
255 key_id = -1;
256 continue;
257 }
258 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
259 return -1;
260 }
261 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
262 if (rc) {
263 return -1;
264 }
265 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
266 if (rc == 0) {
267 valid_signature = 1;
268 }
269 key_id = -1;
270#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271 }
272 }
David Brown43cda332017-09-01 09:53:23 -0600273
274 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800275 return -1;
276 }
277
David Brown43cda332017-09-01 09:53:23 -0600278#ifdef EXPECTED_SIG_TLV
279 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800280 return -1;
281 }
282#endif
David Brown43cda332017-09-01 09:53:23 -0600283
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284 return 0;
285}