blob: 496ed27abd367dcbfb0dfa41bcd6967b5e2ecf88 [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"
26#include "flash_map/flash_map.h"
27#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 Utzig19356bf2017-05-11 16:19:36 -030033#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080034#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060035#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030036#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060038#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "mbedtls/asn1.h"
40
41#include "bootutil_priv.h"
42
43/*
44 * Compute SHA256 over the image.
45 */
46static int
47bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
48 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
49 uint8_t *hash_result, uint8_t *seed, int seed_len)
50{
David Browne629bf32017-04-24 13:37:33 -060051 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080052 uint32_t blk_sz;
53 uint32_t size;
54 uint32_t off;
55 int rc;
56
David Browne629bf32017-04-24 13:37:33 -060057 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080058
59 /* in some cases (split image) the hash is seeded with data from
60 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020061 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060062 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 }
64
Christopher Collins92ea77f2016-12-12 15:59:26 -080065 /*
66 * Hash is computed over image header and image itself. No TLV is
67 * included ATM.
68 */
69 size = hdr->ih_img_size + hdr->ih_hdr_size;
70 for (off = 0; off < size; off += blk_sz) {
71 blk_sz = size - off;
72 if (blk_sz > tmp_buf_sz) {
73 blk_sz = tmp_buf_sz;
74 }
75 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
76 if (rc) {
77 return rc;
78 }
David Browne629bf32017-04-24 13:37:33 -060079 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -080080 }
David Browne629bf32017-04-24 13:37:33 -060081 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -080082
83 return 0;
84}
85
86/*
David Brown43cda332017-09-01 09:53:23 -060087 * Currently, we only support being able to verify one type of
88 * signature, because there is a single verification function that we
89 * call. List the type of TLV we are expecting. If we aren't
90 * configured for any signature, don't define this macro.
91 */
92#if defined(MCUBOOT_SIGN_RSA)
Marko Kiiskila8dd56f32017-08-22 21:40:49 -070093# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
David Brown43cda332017-09-01 09:53:23 -060094# define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */
95# if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
96# error "Multiple signature types not yet supported"
97# endif
98#elif defined(MCUBOOT_SIGN_EC)
99# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
100# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
101# if defined(MCUBOOT_SIGN_EC256)
102# error "Multiple signature types not yet supported"
103# endif
104#elif defined(MCUBOOT_SIGN_EC256)
105# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
106# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
107#endif
108
109#ifdef EXPECTED_SIG_TLV
110static int
111bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
112{
113 bootutil_sha256_context sha256_ctx;
114 int i;
115 const struct bootutil_key *key;
116 uint8_t hash[32];
117
Fabio Utzig9911b182017-09-05 20:29:42 -0300118 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600119
120 for (i = 0; i < bootutil_key_cnt; i++) {
121 key = &bootutil_keys[i];
122 bootutil_sha256_init(&sha256_ctx);
123 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
124 bootutil_sha256_finish(&sha256_ctx, hash);
125 if (!memcmp(hash, keyhash, keyhash_len)) {
126 return i;
127 }
128 }
129 return -1;
130}
131#endif
132
133/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134 * Verify the integrity of the image.
135 * Return non-zero if image could not be validated/does not validate.
136 */
137int
138bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
139 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
140 uint8_t *seed, int seed_len, uint8_t *out_hash)
141{
142 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600143 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600144 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600145 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600146#ifdef EXPECTED_SIG_TLV
147 int valid_signature = 0;
148 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149#endif
150 struct image_tlv tlv;
151 uint8_t buf[256];
152 uint8_t hash[32];
153 int rc;
154
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
156 seed, seed_len);
157 if (rc) {
158 return rc;
159 }
160
161 if (out_hash) {
162 memcpy(out_hash, hash, 32);
163 }
164
David Brownf5b33d82017-09-01 10:58:27 -0600165 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166 /* After image there are TLVs. */
167 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600168
169 rc = flash_area_read(fap, off, &info, sizeof(info));
170 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300171 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600172 }
173 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300174 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600175 }
David Brown3eaa2a12017-09-01 11:19:03 -0600176 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600177 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178
David Brown43cda332017-09-01 09:53:23 -0600179 /*
180 * Traverse through all of the TLVs, performing any checks we know
181 * and are able to do.
182 */
David Brown3eaa2a12017-09-01 11:19:03 -0600183 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
185 if (rc) {
186 return rc;
187 }
David Brown43cda332017-09-01 09:53:23 -0600188
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600190 /*
191 * Verify the SHA256 image hash. This must always be
192 * present.
193 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 if (tlv.it_len != sizeof(hash)) {
195 return -1;
196 }
David Brown43cda332017-09-01 09:53:23 -0600197 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
198 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300199 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800200 }
David Brown43cda332017-09-01 09:53:23 -0600201 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300202 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800203 }
David Brown43cda332017-09-01 09:53:23 -0600204
205 sha256_valid = 1;
206#ifdef EXPECTED_SIG_TLV
207 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800208 /*
David Brown43cda332017-09-01 09:53:23 -0600209 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210 */
David Brown43cda332017-09-01 09:53:23 -0600211 if (tlv.it_len > 32) {
212 return -1;
213 }
214 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
215 if (rc) {
216 return rc;
217 }
218 key_id = bootutil_find_key(buf, tlv.it_len);
219 /*
220 * The key may not be found, which is acceptable. There
221 * can be multiple signatures, each preceded by a key.
222 */
223 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
224 /* Ignore this signature if it is out of bounds. */
225 if (key_id < 0 || key_id >= bootutil_key_cnt) {
226 key_id = -1;
227 continue;
228 }
229 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
230 return -1;
231 }
232 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
233 if (rc) {
234 return -1;
235 }
236 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
237 if (rc == 0) {
238 valid_signature = 1;
239 }
240 key_id = -1;
241#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242 }
243 }
David Brown43cda332017-09-01 09:53:23 -0600244
245 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246 return -1;
247 }
248
David Brown43cda332017-09-01 09:53:23 -0600249#ifdef EXPECTED_SIG_TLV
250 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251 return -1;
252 }
253#endif
David Brown43cda332017-09-01 09:53:23 -0600254
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255 return 0;
256}