blob: c8ee2d6706187249eb8617eef9db0e9c0b2c304a [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"
29#include "bootutil/sign_key.h"
30
31#include "mbedtls/sha256.h"
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{
50 mbedtls_sha256_context sha256_ctx;
51 uint32_t blk_sz;
52 uint32_t size;
53 uint32_t off;
54 int rc;
55
56 mbedtls_sha256_init(&sha256_ctx);
57 mbedtls_sha256_starts(&sha256_ctx, 0);
58
59 /* in some cases (split image) the hash is seeded with data from
60 * the loader image */
61 if(seed && (seed_len > 0)) {
62 mbedtls_sha256_update(&sha256_ctx, seed, seed_len);
63 }
64
65 size = hdr->ih_img_size + hdr->ih_hdr_size;
66
67 /*
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 }
81 mbedtls_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
82 }
83 mbedtls_sha256_finish(&sha256_ctx, hash_result);
84
85 return 0;
86}
87
88/*
89 * Verify the integrity of the image.
90 * Return non-zero if image could not be validated/does not validate.
91 */
92int
93bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
94 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
95 uint8_t *seed, int seed_len, uint8_t *out_hash)
96{
97 uint32_t off;
98 uint32_t size;
99 uint32_t sha_off = 0;
Marko Kiiskilabf943392016-12-29 17:29:48 -0800100#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC) || \
101 MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800102 uint32_t sig_off = 0;
103 uint32_t sig_len = 0;
104#endif
105 struct image_tlv tlv;
106 uint8_t buf[256];
107 uint8_t hash[32];
108 int rc;
109
110#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
111 if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
112 return -1;
113 }
114#endif
115#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
116 if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
117 return -1;
118 }
119#endif
Marko Kiiskilabf943392016-12-29 17:29:48 -0800120#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
121 if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) {
122 return -1;
123 }
124#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
126 return -1;
127 }
128
129 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
130 seed, seed_len);
131 if (rc) {
132 return rc;
133 }
134
135 if (out_hash) {
136 memcpy(out_hash, hash, 32);
137 }
138
139 /* After image there are TLVs. */
140 off = hdr->ih_img_size + hdr->ih_hdr_size;
141 size = off + hdr->ih_tlv_size;
142
143 for (; off < size; off += sizeof(tlv) + tlv.it_len) {
144 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
145 if (rc) {
146 return rc;
147 }
148 if (tlv.it_type == IMAGE_TLV_SHA256) {
149 if (tlv.it_len != sizeof(hash)) {
150 return -1;
151 }
152 sha_off = off + sizeof(tlv);
153 }
154#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
155 if (tlv.it_type == IMAGE_TLV_RSA2048) {
156 if (tlv.it_len != 256) { /* 2048 bits */
157 return -1;
158 }
159 sig_off = off + sizeof(tlv);
160 sig_len = tlv.it_len;
161 }
162#endif
163#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
164 if (tlv.it_type == IMAGE_TLV_ECDSA224) {
165 if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
166 return -1;
167 }
168 sig_off = off + sizeof(tlv);
169 sig_len = tlv.it_len;
170 }
171#endif
Marko Kiiskilabf943392016-12-29 17:29:48 -0800172#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
173 if (tlv.it_type == IMAGE_TLV_ECDSA256) {
174 if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */
175 return -1;
176 }
177 sig_off = off + sizeof(tlv);
178 sig_len = tlv.it_len;
179 }
180#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181 }
182 if (hdr->ih_flags & IMAGE_F_SHA256) {
183 if (!sha_off) {
184 /*
185 * Header said there should be hash TLV, no TLV found.
186 */
187 return -1;
188 }
189 rc = flash_area_read(fap, sha_off, buf, sizeof hash);
190 if (rc) {
191 return rc;
192 }
193 if (memcmp(hash, buf, sizeof(hash))) {
194 return -1;
195 }
196 }
Marko Kiiskilabf943392016-12-29 17:29:48 -0800197#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC) || \
198 MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 if (!sig_off) {
200 /*
201 * Header said there should be PKCS1.v5 signature, no TLV
202 * found.
203 */
204 return -1;
205 }
206 rc = flash_area_read(fap, sig_off, buf, sig_len);
207 if (rc) {
208 return -1;
209 }
210
211 if (hdr->ih_key_id >= bootutil_key_cnt) {
212 return -1;
213 }
214 rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
215 if (rc) {
216 return -1;
217 }
218#endif
219 return 0;
220}