blob: a854fd37f315946b43011475fb1ca4d4d786474d [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#ifdef MCUBOOT_MYNEWT
32#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030033#endif
34
Fabio Utzig19356bf2017-05-11 16:19:36 -030035#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060037#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030038#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060040#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include "mbedtls/asn1.h"
42
43#include "bootutil_priv.h"
44
45/*
46 * Compute SHA256 over the image.
47 */
48static int
49bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
50 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
51 uint8_t *hash_result, uint8_t *seed, int seed_len)
52{
David Browne629bf32017-04-24 13:37:33 -060053 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080054 uint32_t blk_sz;
55 uint32_t size;
56 uint32_t off;
57 int rc;
58
David Browne629bf32017-04-24 13:37:33 -060059 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080060
61 /* in some cases (split image) the hash is seeded with data from
62 * the loader image */
Fabio Utzig53986042017-12-12 14:57:07 -020063 if (seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060064 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080065 }
66
Christopher Collins92ea77f2016-12-12 15:59:26 -080067 /*
68 * Hash is computed over image header and image itself. No TLV is
69 * included ATM.
70 */
71 size = hdr->ih_img_size + hdr->ih_hdr_size;
72 for (off = 0; off < size; off += blk_sz) {
73 blk_sz = size - off;
74 if (blk_sz > tmp_buf_sz) {
75 blk_sz = tmp_buf_sz;
76 }
77 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
78 if (rc) {
79 return rc;
80 }
David Browne629bf32017-04-24 13:37:33 -060081 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 }
David Browne629bf32017-04-24 13:37:33 -060083 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -080084
85 return 0;
86}
87
88/*
David Brown43cda332017-09-01 09:53:23 -060089 * Currently, we only support being able to verify one type of
90 * signature, because there is a single verification function that we
91 * call. List the type of TLV we are expecting. If we aren't
92 * configured for any signature, don't define this macro.
93 */
94#if defined(MCUBOOT_SIGN_RSA)
Marko Kiiskila8dd56f32017-08-22 21:40:49 -070095# define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS
David Brown43cda332017-09-01 09:53:23 -060096# define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */
97# if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
98# error "Multiple signature types not yet supported"
99# endif
100#elif defined(MCUBOOT_SIGN_EC)
101# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224
102# define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */
103# if defined(MCUBOOT_SIGN_EC256)
104# error "Multiple signature types not yet supported"
105# endif
106#elif defined(MCUBOOT_SIGN_EC256)
107# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
108# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
109#endif
110
111#ifdef EXPECTED_SIG_TLV
112static int
113bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
114{
115 bootutil_sha256_context sha256_ctx;
116 int i;
117 const struct bootutil_key *key;
118 uint8_t hash[32];
119
Fabio Utzig9911b182017-09-05 20:29:42 -0300120 assert(keyhash_len <= 32);
David Brown43cda332017-09-01 09:53:23 -0600121
122 for (i = 0; i < bootutil_key_cnt; i++) {
123 key = &bootutil_keys[i];
124 bootutil_sha256_init(&sha256_ctx);
125 bootutil_sha256_update(&sha256_ctx, key->key, *key->len);
126 bootutil_sha256_finish(&sha256_ctx, hash);
127 if (!memcmp(hash, keyhash, keyhash_len)) {
128 return i;
129 }
130 }
131 return -1;
132}
133#endif
134
135/*
Christopher Collins92ea77f2016-12-12 15:59:26 -0800136 * Verify the integrity of the image.
137 * Return non-zero if image could not be validated/does not validate.
138 */
139int
140bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
141 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
142 uint8_t *seed, int seed_len, uint8_t *out_hash)
143{
144 uint32_t off;
David Brown3eaa2a12017-09-01 11:19:03 -0600145 uint32_t end;
David Brown43cda332017-09-01 09:53:23 -0600146 int sha256_valid = 0;
David Brownf5b33d82017-09-01 10:58:27 -0600147 struct image_tlv_info info;
David Brown43cda332017-09-01 09:53:23 -0600148#ifdef EXPECTED_SIG_TLV
149 int valid_signature = 0;
150 int key_id = -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151#endif
152 struct image_tlv tlv;
153 uint8_t buf[256];
154 uint8_t hash[32];
155 int rc;
156
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
158 seed, seed_len);
159 if (rc) {
160 return rc;
161 }
162
163 if (out_hash) {
164 memcpy(out_hash, hash, 32);
165 }
166
David Brownf5b33d82017-09-01 10:58:27 -0600167 /* The TLVs come after the image. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 /* After image there are TLVs. */
169 off = hdr->ih_img_size + hdr->ih_hdr_size;
David Brownf5b33d82017-09-01 10:58:27 -0600170
171 rc = flash_area_read(fap, off, &info, sizeof(info));
172 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300173 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600174 }
175 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300176 return -1;
David Brownf5b33d82017-09-01 10:58:27 -0600177 }
David Brown3eaa2a12017-09-01 11:19:03 -0600178 end = off + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600179 off += sizeof(info);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
David Brown43cda332017-09-01 09:53:23 -0600181 /*
182 * Traverse through all of the TLVs, performing any checks we know
183 * and are able to do.
184 */
David Brown3eaa2a12017-09-01 11:19:03 -0600185 for (; off < end; off += sizeof(tlv) + tlv.it_len) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
187 if (rc) {
188 return rc;
189 }
David Brown43cda332017-09-01 09:53:23 -0600190
Christopher Collins92ea77f2016-12-12 15:59:26 -0800191 if (tlv.it_type == IMAGE_TLV_SHA256) {
David Brown43cda332017-09-01 09:53:23 -0600192 /*
193 * Verify the SHA256 image hash. This must always be
194 * present.
195 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196 if (tlv.it_len != sizeof(hash)) {
197 return -1;
198 }
David Brown43cda332017-09-01 09:53:23 -0600199 rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash);
200 if (rc) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300201 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202 }
David Brown43cda332017-09-01 09:53:23 -0600203 if (memcmp(hash, buf, sizeof(hash))) {
Fabio Utzigde6edc32017-09-04 14:35:49 -0300204 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800205 }
David Brown43cda332017-09-01 09:53:23 -0600206
207 sha256_valid = 1;
208#ifdef EXPECTED_SIG_TLV
209 } else if (tlv.it_type == IMAGE_TLV_KEYHASH) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210 /*
David Brown43cda332017-09-01 09:53:23 -0600211 * Determine which key we should be checking.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800212 */
David Brown43cda332017-09-01 09:53:23 -0600213 if (tlv.it_len > 32) {
214 return -1;
215 }
216 rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len);
217 if (rc) {
218 return rc;
219 }
220 key_id = bootutil_find_key(buf, tlv.it_len);
221 /*
222 * The key may not be found, which is acceptable. There
223 * can be multiple signatures, each preceded by a key.
224 */
225 } else if (tlv.it_type == EXPECTED_SIG_TLV) {
226 /* Ignore this signature if it is out of bounds. */
227 if (key_id < 0 || key_id >= bootutil_key_cnt) {
228 key_id = -1;
229 continue;
230 }
231 if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) {
232 return -1;
233 }
234 rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len);
235 if (rc) {
236 return -1;
237 }
238 rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id);
239 if (rc == 0) {
240 valid_signature = 1;
241 }
242 key_id = -1;
243#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244 }
245 }
David Brown43cda332017-09-01 09:53:23 -0600246
247 if (!sha256_valid) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800248 return -1;
249 }
250
David Brown43cda332017-09-01 09:53:23 -0600251#ifdef EXPECTED_SIG_TLV
252 if (!valid_signature) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800253 return -1;
254 }
255#endif
David Brown43cda332017-09-01 09:53:23 -0600256
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257 return 0;
258}