blob: 0e78407947737c59593f85aee1e9c3c4307cf703 [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
25#include "syscfg/syscfg.h"
26#include "hal/hal_flash.h"
27#include "flash_map/flash_map.h"
28#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060029#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include "bootutil/sign_key.h"
31
David Brownd7e350d2017-04-24 13:29:28 -060032#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060034#endif
35#if MYNEWT_VAL(BOOTUTIL_SIGN_EC) || MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060037#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "mbedtls/asn1.h"
39
40#include "bootutil_priv.h"
41
42/*
43 * Compute SHA256 over the image.
44 */
45static int
46bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
47 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
48 uint8_t *hash_result, uint8_t *seed, int seed_len)
49{
David Browne629bf32017-04-24 13:37:33 -060050 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051 uint32_t blk_sz;
52 uint32_t size;
53 uint32_t off;
54 int rc;
55
David Browne629bf32017-04-24 13:37:33 -060056 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080057
58 /* in some cases (split image) the hash is seeded with data from
59 * the loader image */
60 if(seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060061 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080062 }
63
64 size = hdr->ih_img_size + hdr->ih_hdr_size;
65
66 /*
67 * Hash is computed over image header and image itself. No TLV is
68 * included ATM.
69 */
70 size = hdr->ih_img_size + hdr->ih_hdr_size;
71 for (off = 0; off < size; off += blk_sz) {
72 blk_sz = size - off;
73 if (blk_sz > tmp_buf_sz) {
74 blk_sz = tmp_buf_sz;
75 }
76 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
77 if (rc) {
78 return rc;
79 }
David Browne629bf32017-04-24 13:37:33 -060080 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -080081 }
David Browne629bf32017-04-24 13:37:33 -060082 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -080083
84 return 0;
85}
86
87/*
88 * Verify the integrity of the image.
89 * Return non-zero if image could not be validated/does not validate.
90 */
91int
92bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
93 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
94 uint8_t *seed, int seed_len, uint8_t *out_hash)
95{
96 uint32_t off;
97 uint32_t size;
98 uint32_t sha_off = 0;
Marko Kiiskilabf943392016-12-29 17:29:48 -080099#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC) || \
100 MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101 uint32_t sig_off = 0;
102 uint32_t sig_len = 0;
103#endif
104 struct image_tlv tlv;
105 uint8_t buf[256];
106 uint8_t hash[32];
107 int rc;
108
109#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
110 if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
111 return -1;
112 }
113#endif
114#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
115 if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
116 return -1;
117 }
118#endif
Marko Kiiskilabf943392016-12-29 17:29:48 -0800119#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
120 if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) {
121 return -1;
122 }
123#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124 if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
125 return -1;
126 }
127
128 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
129 seed, seed_len);
130 if (rc) {
131 return rc;
132 }
133
134 if (out_hash) {
135 memcpy(out_hash, hash, 32);
136 }
137
138 /* After image there are TLVs. */
139 off = hdr->ih_img_size + hdr->ih_hdr_size;
140 size = off + hdr->ih_tlv_size;
141
142 for (; off < size; off += sizeof(tlv) + tlv.it_len) {
143 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
144 if (rc) {
145 return rc;
146 }
147 if (tlv.it_type == IMAGE_TLV_SHA256) {
148 if (tlv.it_len != sizeof(hash)) {
149 return -1;
150 }
151 sha_off = off + sizeof(tlv);
152 }
153#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
154 if (tlv.it_type == IMAGE_TLV_RSA2048) {
155 if (tlv.it_len != 256) { /* 2048 bits */
156 return -1;
157 }
158 sig_off = off + sizeof(tlv);
159 sig_len = tlv.it_len;
160 }
161#endif
162#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
163 if (tlv.it_type == IMAGE_TLV_ECDSA224) {
164 if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
165 return -1;
166 }
167 sig_off = off + sizeof(tlv);
168 sig_len = tlv.it_len;
169 }
170#endif
Marko Kiiskilabf943392016-12-29 17:29:48 -0800171#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
172 if (tlv.it_type == IMAGE_TLV_ECDSA256) {
173 if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */
174 return -1;
175 }
176 sig_off = off + sizeof(tlv);
177 sig_len = tlv.it_len;
178 }
179#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180 }
181 if (hdr->ih_flags & IMAGE_F_SHA256) {
182 if (!sha_off) {
183 /*
184 * Header said there should be hash TLV, no TLV found.
185 */
186 return -1;
187 }
188 rc = flash_area_read(fap, sha_off, buf, sizeof hash);
189 if (rc) {
190 return rc;
191 }
192 if (memcmp(hash, buf, sizeof(hash))) {
193 return -1;
194 }
195 }
Marko Kiiskilabf943392016-12-29 17:29:48 -0800196#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC) || \
197 MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 if (!sig_off) {
199 /*
200 * Header said there should be PKCS1.v5 signature, no TLV
201 * found.
202 */
203 return -1;
204 }
205 rc = flash_area_read(fap, sig_off, buf, sig_len);
206 if (rc) {
207 return -1;
208 }
209
210 if (hdr->ih_key_id >= bootutil_key_cnt) {
211 return -1;
212 }
213 rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
214 if (rc) {
215 return -1;
216 }
217#endif
218 return 0;
219}