blob: 9cff0ac73ff39d2cfdbcdd5287db474ef1a3eff9 [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 */
Fabio Utzig48764842019-05-10 19:28:24 -0300126#if (defined(MCUBOOT_SIGN_RSA) + \
127 defined(MCUBOOT_SIGN_EC) + \
128 defined(MCUBOOT_SIGN_EC256) + \
129 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700130#error "Only a single signature type is supported!"
131#endif
132
David Brown43cda332017-09-01 09:53:23 -0600133#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700134# if MCUBOOT_SIGN_RSA_LEN == 2048
135# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
136# elif MCUBOOT_SIGN_RSA_LEN == 3072
137# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
138# else
139# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600140# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700141# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
142# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600143#elif defined(MCUBOOT_SIGN_EC)
144# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700145# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600146# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
David Brown43cda332017-09-01 09:53:23 -0600147#elif defined(MCUBOOT_SIGN_EC256)
148# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700149# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600150# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
Fabio Utzig48764842019-05-10 19:28:24 -0300151#elif defined(MCUBOOT_SIGN_ED25519)
152# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
153# define SIG_BUF_SIZE 64
154# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700155#else
156# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600157#endif
158
159#ifdef EXPECTED_SIG_TLV
160static int
161bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
162{
163 bootutil_sha256_context sha256_ctx;
164 int i;
165 const struct bootutil_key *key;
166 uint8_t hash[32];
167
Fabio Utzig9911b182017-09-05 20:29:42 -0300168 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600169
170 for (i = 0; i < bootutil_key_cnt; i++) {
171 key = &bootutil_keys[i];
172 bootutil_sha256_init(&sha256_ctx);
173 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
174 bootutil_sha256_finish(&sha256_ctx, hash);
175 if (!memcmp(hash, keyhash, keyhash_len)) {
176 return i;
177 }
178 }
179 return -1;
180}
181#endif
182
183/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184 * Verify the integrity of the image.
185 * Return non-zero if image could not be validated/does not validate.
186 */
187int
188bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
189 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
190 uint8_t *seed, int seed_len, uint8_t *out_hash)
191{
192 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600193 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600194 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600195 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600196#ifdef EXPECTED_SIG_TLV
197 int valid_signature = 0;
198 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199#endif
200 struct image_tlv tlv;
Fabio Utzig3501c012019-05-13 15:07:25 -0700201 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202 uint8_t hash[32];
203 int rc;
204
Fabio Utzigba829042018-09-18 08:29:34 -0300205 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206 if (rc) {
207 return rc;
208 }
209
210 if (out_hash) {
211 memcpy(out_hash, hash, 32);
212 }
213
David Brownf5b33d82017-09-01 10:58:27 -0600214 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215 /* After image there are TLVs. */
216 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600217
218 rc = flash_area_read(fap, off, &info, sizeof(info));
219 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300220 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600221 }
222 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300223 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600224 }
David Brown3eaa2a12017-09-01 11:19:03 -0600225 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600226 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800227
David Brown43cda332017-09-01 09:53:23 -0600228 /*
229 * Traverse through all of the TLVs, performing any checks we know
230 * and are able to do.
231 */
David Brown3eaa2a12017-09-01 11:19:03 -0600232 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
234 if (rc) {
235 return rc;
236 }
David Brown43cda332017-09-01 09:53:23 -0600237
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600239 /*
240 * Verify the SHA256 image hash. This must always be
241 * present.
242 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243 if (tlv.it_len != sizeof(hash)) {
244 return -1;
245 }
David Brown43cda332017-09-01 09:53:23 -0600246 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
247 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300248 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249 }
David Brown43cda332017-09-01 09:53:23 -0600250 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300251 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800252 }
David Brown43cda332017-09-01 09:53:23 -0600253
254 sha256_valid = 1;
255#ifdef EXPECTED_SIG_TLV
256 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257 /*
David Brown43cda332017-09-01 09:53:23 -0600258 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 */
David Brown43cda332017-09-01 09:53:23 -0600260 if (tlv.it_len > 32) {
261 return -1;
262 }
263 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
264 if (rc) {
265 return rc;
266 }
267 key_id = bootutil_find_key(buf, tlv.it_len);
268 /*
269 * The key may not be found, which is acceptable. There
270 * can be multiple signatures, each preceded by a key.
271 */
272 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
273 /* Ignore this signature if it is out of bounds. */
274 if (key_id < 0 || key_id >= bootutil_key_cnt) {
275 key_id = -1;
276 continue;
277 }
278 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
279 return -1;
280 }
281 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
282 if (rc) {
283 return -1;
284 }
285 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
286 if (rc == 0) {
287 valid_signature = 1;
288 }
289 key_id = -1;
290#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291 }
292 }
David Brown43cda332017-09-01 09:53:23 -0600293
294 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295 return -1;
296 }
297
David Brown43cda332017-09-01 09:53:23 -0600298#ifdef EXPECTED_SIG_TLV
299 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800300 return -1;
301 }
302#endif
David Brown43cda332017-09-01 09:53:23 -0600303
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 return 0;
305}