blob: 98fdc9bb3dccf0b0d5f6839249ccc883d086e822 [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"
32#include "mbedtls/rsa.h"
33#include "mbedtls/ecdsa.h"
34#include "mbedtls/asn1.h"
35
36#include "bootutil_priv.h"
37
38/*
39 * Compute SHA256 over the image.
40 */
41static int
42bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
43 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
44 uint8_t *hash_result, uint8_t *seed, int seed_len)
45{
46 mbedtls_sha256_context sha256_ctx;
47 uint32_t blk_sz;
48 uint32_t size;
49 uint32_t off;
50 int rc;
51
52 mbedtls_sha256_init(&sha256_ctx);
53 mbedtls_sha256_starts(&sha256_ctx, 0);
54
55 /* in some cases (split image) the hash is seeded with data from
56 * the loader image */
57 if(seed && (seed_len > 0)) {
58 mbedtls_sha256_update(&sha256_ctx, seed, seed_len);
59 }
60
61 size = hdr->ih_img_size + hdr->ih_hdr_size;
62
63 /*
64 * Hash is computed over image header and image itself. No TLV is
65 * included ATM.
66 */
67 size = hdr->ih_img_size + hdr->ih_hdr_size;
68 for (off = 0; off < size; off += blk_sz) {
69 blk_sz = size - off;
70 if (blk_sz > tmp_buf_sz) {
71 blk_sz = tmp_buf_sz;
72 }
73 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
74 if (rc) {
75 return rc;
76 }
77 mbedtls_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
78 }
79 mbedtls_sha256_finish(&sha256_ctx, hash_result);
80
81 return 0;
82}
83
84/*
85 * Verify the integrity of the image.
86 * Return non-zero if image could not be validated/does not validate.
87 */
88int
89bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
90 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
91 uint8_t *seed, int seed_len, uint8_t *out_hash)
92{
93 uint32_t off;
94 uint32_t size;
95 uint32_t sha_off = 0;
96#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC)
97 uint32_t sig_off = 0;
98 uint32_t sig_len = 0;
99#endif
100 struct image_tlv tlv;
101 uint8_t buf[256];
102 uint8_t hash[32];
103 int rc;
104
105#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
106 if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
107 return -1;
108 }
109#endif
110#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
111 if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
112 return -1;
113 }
114#endif
115 if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
116 return -1;
117 }
118
119 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
120 seed, seed_len);
121 if (rc) {
122 return rc;
123 }
124
125 if (out_hash) {
126 memcpy(out_hash, hash, 32);
127 }
128
129 /* After image there are TLVs. */
130 off = hdr->ih_img_size + hdr->ih_hdr_size;
131 size = off + hdr->ih_tlv_size;
132
133 for (; off < size; off += sizeof(tlv) + tlv.it_len) {
134 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
135 if (rc) {
136 return rc;
137 }
138 if (tlv.it_type == IMAGE_TLV_SHA256) {
139 if (tlv.it_len != sizeof(hash)) {
140 return -1;
141 }
142 sha_off = off + sizeof(tlv);
143 }
144#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
145 if (tlv.it_type == IMAGE_TLV_RSA2048) {
146 if (tlv.it_len != 256) { /* 2048 bits */
147 return -1;
148 }
149 sig_off = off + sizeof(tlv);
150 sig_len = tlv.it_len;
151 }
152#endif
153#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
154 if (tlv.it_type == IMAGE_TLV_ECDSA224) {
155 if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
156 return -1;
157 }
158 sig_off = off + sizeof(tlv);
159 sig_len = tlv.it_len;
160 }
161#endif
162 }
163 if (hdr->ih_flags & IMAGE_F_SHA256) {
164 if (!sha_off) {
165 /*
166 * Header said there should be hash TLV, no TLV found.
167 */
168 return -1;
169 }
170 rc = flash_area_read(fap, sha_off, buf, sizeof hash);
171 if (rc) {
172 return rc;
173 }
174 if (memcmp(hash, buf, sizeof(hash))) {
175 return -1;
176 }
177 }
178#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC)
179 if (!sig_off) {
180 /*
181 * Header said there should be PKCS1.v5 signature, no TLV
182 * found.
183 */
184 return -1;
185 }
186 rc = flash_area_read(fap, sig_off, buf, sig_len);
187 if (rc) {
188 return -1;
189 }
190
191 if (hdr->ih_key_id >= bootutil_key_cnt) {
192 return -1;
193 }
194 rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
195 if (rc) {
196 return -1;
197 }
198#endif
199 return 0;
200}