blob: 9fbd88f9e23358a05b7c413e4f1686da127ec925 [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 <stddef.h>
25#include <inttypes.h>
26#include <string.h>
27
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020028#include <flash_map_backend/flash_map_backend.h>
29
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060031#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/sign_key.h"
33
Fabio Utzigba1fbe62017-07-21 14:01:20 -030034#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030035
Fabio Utzigba829042018-09-18 08:29:34 -030036#ifdef MCUBOOT_ENC_IMAGES
37#include "bootutil/enc_key.h"
38#endif
39#if defined(MCUBOOT_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060041#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030042#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080043#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060044#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#include "mbedtls/asn1.h"
46
47#include "bootutil_priv.h"
48
49/*
50 * Compute SHA256 over the image.
51 */
52static int
Fabio Utzig10ee6482019-08-01 12:04:52 -030053bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
54 struct image_header *hdr, const struct flash_area *fap,
55 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result,
56 uint8_t *seed, int seed_len)
Christopher Collins92ea77f2016-12-12 15:59:26 -080057{
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 uint32_t blk_off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030065 uint32_t tlv_off;
Christopher Collins92ea77f2016-12-12 15:59:26 -080066
Fabio Utzigb0f04732019-07-31 09:49:19 -030067#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig10ee6482019-08-01 12:04:52 -030068 (void)enc_state;
Fabio Utzigb0f04732019-07-31 09:49:19 -030069 (void)image_index;
Fabio Utzigc9621352019-08-08 12:15:51 -030070 (void)hdr_size;
Fabio Utzige52c08e2019-09-11 19:32:00 -030071 (void)blk_off;
72 (void)tlv_off;
Fabio Utzigb0f04732019-07-31 09:49:19 -030073#endif
74
Fabio Utzigbc077932019-08-26 11:16:34 -030075#ifdef MCUBOOT_ENC_IMAGES
76 /* Encrypted images only exist in the secondary slot */
77 if (MUST_DECRYPT(fap, image_index, hdr) &&
78 !boot_enc_valid(enc_state, image_index, fap)) {
79 return -1;
80 }
81#endif
82
David Browne629bf32017-04-24 13:37:33 -060083 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080084
85 /* in some cases (split image) the hash is seeded with data from
86 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020087 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060088 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 }
90
David Vinczee32483f2019-06-13 10:46:24 +020091 /* Hash is computed over image header and image itself. */
Fabio Utzige52c08e2019-09-11 19:32:00 -030092 size = hdr_size = hdr->ih_hdr_size;
93 size += hdr->ih_img_size;
94 tlv_off = size;
David Vinczee32483f2019-06-13 10:46:24 +020095
Fabio Utzige52c08e2019-09-11 19:32:00 -030096 /* If protected TLVs are present they are also hashed. */
97 size += hdr->ih_protect_tlv_size;
David Vinczee32483f2019-06-13 10:46:24 +020098
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 for (off = 0; off < size; off += blk_sz) {
100 blk_sz = size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300101 if (blk_sz > tmp_buf_sz) {
102 blk_sz = tmp_buf_sz;
103 }
Fabio Utzigc21c2102019-09-10 10:10:42 -0300104#ifdef MCUBOOT_ENC_IMAGES
105 /* The only data that is encrypted in an image is the payload;
106 * both header and TLVs (when protected) are not.
107 */
108 if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) {
109 /* read only the header */
110 blk_sz = hdr_size - off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300111 }
112 if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) {
113 /* read only up to the end of the image payload */
114 blk_sz = tlv_off - off;
Fabio Utzigc21c2102019-09-10 10:10:42 -0300115 }
116#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
118 if (rc) {
119 return rc;
120 }
Fabio Utzigba829042018-09-18 08:29:34 -0300121#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigc21c2102019-09-10 10:10:42 -0300122 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300123 /* Only payload is encrypted (area between header and TLVs) */
124 if (off >= hdr_size && off < tlv_off) {
Fabio Utzigc21c2102019-09-10 10:10:42 -0300125 blk_off = (off - hdr_size) & 0xf;
126 boot_encrypt(enc_state, image_index, fap, off - hdr_size,
127 blk_sz, blk_off, tmp_buf);
128 }
Fabio Utzigba829042018-09-18 08:29:34 -0300129 }
130#endif
David Browne629bf32017-04-24 13:37:33 -0600131 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132 }
David Browne629bf32017-04-24 13:37:33 -0600133 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134
135 return 0;
136}
137
138/*
David Brown43cda332017-09-01 09:53:23 -0600139 * Currently, we only support being able to verify one type of
140 * signature, because there is a single verification function that we
141 * call. List the type of TLV we are expecting. If we aren't
142 * configured for any signature, don't define this macro.
143 */
Fabio Utzig48764842019-05-10 19:28:24 -0300144#if (defined(MCUBOOT_SIGN_RSA) + \
145 defined(MCUBOOT_SIGN_EC) + \
146 defined(MCUBOOT_SIGN_EC256) + \
147 defined(MCUBOOT_SIGN_ED25519)) > 1
Fabio Utzig3501c012019-05-13 15:07:25 -0700148#error "Only a single signature type is supported!"
149#endif
150
David Brown43cda332017-09-01 09:53:23 -0600151#if defined(MCUBOOT_SIGN_RSA)
Fabio Utzig3501c012019-05-13 15:07:25 -0700152# if MCUBOOT_SIGN_RSA_LEN == 2048
153# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
154# elif MCUBOOT_SIGN_RSA_LEN == 3072
155# define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS
156# else
157# error "Unsupported RSA signature length"
David Brown43cda332017-09-01 09:53:23 -0600158# endif
Fabio Utzig3501c012019-05-13 15:07:25 -0700159# define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8)
160# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */
David Brown43cda332017-09-01 09:53:23 -0600161#elif defined(MCUBOOT_SIGN_EC)
162# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
Fabio Utzig3501c012019-05-13 15:07:25 -0700163# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600164# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
David Brown43cda332017-09-01 09:53:23 -0600165#elif defined(MCUBOOT_SIGN_EC256)
166# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
Fabio Utzig3501c012019-05-13 15:07:25 -0700167# define SIG_BUF_SIZE 128
David Brown43cda332017-09-01 09:53:23 -0600168# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
Fabio Utzig48764842019-05-10 19:28:24 -0300169#elif defined(MCUBOOT_SIGN_ED25519)
170# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
171# define SIG_BUF_SIZE 64
172# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
Fabio Utzig3501c012019-05-13 15:07:25 -0700173#else
174# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
David Brown43cda332017-09-01 09:53:23 -0600175#endif
176
177#ifdef EXPECTED_SIG_TLV
178static int
179bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
180{
181 bootutil_sha256_context sha256_ctx;
182 int i;
183 const struct bootutil_key *key;
184 uint8_t hash[32];
185
Fabio Utzig15c14672019-08-09 07:45:20 -0300186 if (keyhash_len > 32) {
187 return -1;
188 }
David Brown43cda332017-09-01 09:53:23 -0600189
190 for (i = 0; i < bootutil_key_cnt; i++) {
191 key = &bootutil_keys[i];
192 bootutil_sha256_init(&sha256_ctx);
193 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
194 bootutil_sha256_finish(&sha256_ctx, hash);
195 if (!memcmp(hash, keyhash, keyhash_len)) {
196 return i;
197 }
198 }
199 return -1;
200}
201#endif
202
203/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 * Verify the integrity of the image.
205 * Return non-zero if image could not be validated/does not validate.
206 */
207int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300208bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
209 struct image_header *hdr, const struct flash_area *fap,
210 uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed,
211 int seed_len, uint8_t *out_hash)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800212{
213 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300214 uint16_t len;
215 uint8_t type;
David Brown43cda332017-09-01 09:53:23 -0600216 int sha256_valid = 0;
217#ifdef EXPECTED_SIG_TLV
218 int valid_signature = 0;
219 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800220#endif
Fabio Utzig61fd8882019-09-14 20:00:20 -0300221 struct image_tlv_iter it;
Fabio Utzig3501c012019-05-13 15:07:25 -0700222 uint8_t buf[SIG_BUF_SIZE];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800223 uint8_t hash[32];
224 int rc;
225
Fabio Utzig10ee6482019-08-01 12:04:52 -0300226 rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf,
227 tmp_buf_sz, hash, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800228 if (rc) {
229 return rc;
230 }
231
232 if (out_hash) {
233 memcpy(out_hash, hash, 32);
234 }
235
Fabio Utzig61fd8882019-09-14 20:00:20 -0300236 rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false);
David Brownf5b33d82017-09-01 10:58:27 -0600237 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300238 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600239 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240
David Brown43cda332017-09-01 09:53:23 -0600241 /*
242 * Traverse through all of the TLVs, performing any checks we know
243 * and are able to do.
244 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300245 while (true) {
246 rc = bootutil_tlv_iter_next(&it, &off, &len, &type);
247 if (rc < 0) {
248 return -1;
249 } else if (rc > 0) {
250 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251 }
David Brown43cda332017-09-01 09:53:23 -0600252
Fabio Utzig61fd8882019-09-14 20:00:20 -0300253 if (type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600254 /*
255 * Verify the SHA256 image hash. This must always be
256 * present.
257 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300258 if (len != sizeof(hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 return -1;
260 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300261 rc = flash_area_read(fap, off, buf, sizeof hash);
David Brown43cda332017-09-01 09:53:23 -0600262 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300263 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264 }
David Brown43cda332017-09-01 09:53:23 -0600265 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300266 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267 }
David Brown43cda332017-09-01 09:53:23 -0600268
269 sha256_valid = 1;
270#ifdef EXPECTED_SIG_TLV
Fabio Utzig61fd8882019-09-14 20:00:20 -0300271 } else if (type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272 /*
David Brown43cda332017-09-01 09:53:23 -0600273 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300275 if (len > 32) {
David Brown43cda332017-09-01 09:53:23 -0600276 return -1;
277 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300278 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600279 if (rc) {
280 return rc;
281 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300282 key_id = bootutil_find_key(buf, len);
David Brown43cda332017-09-01 09:53:23 -0600283 /*
284 * The key may not be found, which is acceptable. There
285 * can be multiple signatures, each preceded by a key.
286 */
Fabio Utzig61fd8882019-09-14 20:00:20 -0300287 } else if (type == EXPECTED_SIG_TLV) {
David Brown43cda332017-09-01 09:53:23 -0600288 /* Ignore this signature if it is out of bounds. */
289 if (key_id < 0 || key_id >= bootutil_key_cnt) {
290 key_id = -1;
291 continue;
292 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300293 if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
David Brown43cda332017-09-01 09:53:23 -0600294 return -1;
295 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300296 rc = flash_area_read(fap, off, buf, len);
David Brown43cda332017-09-01 09:53:23 -0600297 if (rc) {
298 return -1;
299 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300300 rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id);
David Brown43cda332017-09-01 09:53:23 -0600301 if (rc == 0) {
302 valid_signature = 1;
303 }
304 key_id = -1;
305#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306 }
307 }
David Brown43cda332017-09-01 09:53:23 -0600308
309 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 return -1;
311 }
312
David Brown43cda332017-09-01 09:53:23 -0600313#ifdef EXPECTED_SIG_TLV
314 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315 return -1;
316 }
317#endif
David Brown43cda332017-09-01 09:53:23 -0600318
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319 return 0;
320}