blob: 00d43c6198c15a491a4256579a2446c46ad0af0b [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 Utzigeed80b62017-06-10 08:03:05 -030031#ifdef APP_mynewt
32#include "mynewt/config.h"
33#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 */
63 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
67 size = hdr->ih_img_size + hdr->ih_hdr_size;
68
69 /*
70 * Hash is computed over image header and image itself. No TLV is
71 * included ATM.
72 */
73 size = hdr->ih_img_size + hdr->ih_hdr_size;
74 for (off = 0; off < size; off += blk_sz) {
75 blk_sz = size - off;
76 if (blk_sz > tmp_buf_sz) {
77 blk_sz = tmp_buf_sz;
78 }
79 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
80 if (rc) {
81 return rc;
82 }
David Browne629bf32017-04-24 13:37:33 -060083 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 }
David Browne629bf32017-04-24 13:37:33 -060085 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -080086
87 return 0;
88}
89
90/*
91 * Verify the integrity of the image.
92 * Return non-zero if image could not be validated/does not validate.
93 */
94int
95bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
96 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
97 uint8_t *seed, int seed_len, uint8_t *out_hash)
98{
99 uint32_t off;
100 uint32_t size;
101 uint32_t sha_off = 0;
Fabio Utzig19356bf2017-05-11 16:19:36 -0300102#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
103 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 uint32_t sig_off = 0;
105 uint32_t sig_len = 0;
106#endif
107 struct image_tlv tlv;
108 uint8_t buf[256];
109 uint8_t hash[32];
110 int rc;
111
Fabio Utzig19356bf2017-05-11 16:19:36 -0300112#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
114 return -1;
115 }
116#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300117#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118 if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
119 return -1;
120 }
121#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300122#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -0800123 if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) {
124 return -1;
125 }
126#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127 if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
128 return -1;
129 }
130
131 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
132 seed, seed_len);
133 if (rc) {
134 return rc;
135 }
136
137 if (out_hash) {
138 memcpy(out_hash, hash, 32);
139 }
140
141 /* After image there are TLVs. */
142 off = hdr->ih_img_size + hdr->ih_hdr_size;
143 size = off + hdr->ih_tlv_size;
144
145 for (; off < size; off += sizeof(tlv) + tlv.it_len) {
146 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
147 if (rc) {
148 return rc;
149 }
150 if (tlv.it_type == IMAGE_TLV_SHA256) {
151 if (tlv.it_len != sizeof(hash)) {
152 return -1;
153 }
154 sha_off = off + sizeof(tlv);
155 }
Fabio Utzig19356bf2017-05-11 16:19:36 -0300156#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157 if (tlv.it_type == IMAGE_TLV_RSA2048) {
158 if (tlv.it_len != 256) { /* 2048 bits */
159 return -1;
160 }
161 sig_off = off + sizeof(tlv);
162 sig_len = tlv.it_len;
163 }
164#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300165#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166 if (tlv.it_type == IMAGE_TLV_ECDSA224) {
167 if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
168 return -1;
169 }
170 sig_off = off + sizeof(tlv);
171 sig_len = tlv.it_len;
172 }
173#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300174#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -0800175 if (tlv.it_type == IMAGE_TLV_ECDSA256) {
176 if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */
177 return -1;
178 }
179 sig_off = off + sizeof(tlv);
180 sig_len = tlv.it_len;
181 }
182#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183 }
184 if (hdr->ih_flags & IMAGE_F_SHA256) {
185 if (!sha_off) {
186 /*
187 * Header said there should be hash TLV, no TLV found.
188 */
189 return -1;
190 }
191 rc = flash_area_read(fap, sha_off, buf, sizeof hash);
192 if (rc) {
193 return rc;
194 }
195 if (memcmp(hash, buf, sizeof(hash))) {
196 return -1;
197 }
198 }
Fabio Utzig19356bf2017-05-11 16:19:36 -0300199#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
200 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 if (!sig_off) {
202 /*
203 * Header said there should be PKCS1.v5 signature, no TLV
204 * found.
205 */
206 return -1;
207 }
208 rc = flash_area_read(fap, sig_off, buf, sig_len);
209 if (rc) {
210 return -1;
211 }
212
213 if (hdr->ih_key_id >= bootutil_key_cnt) {
214 return -1;
215 }
216 rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
217 if (rc) {
218 return -1;
219 }
220#endif
221 return 0;
222}