Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 25 | #include <flash_map_backend/flash_map_backend.h> |
| 26 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 27 | #include "bootutil/image.h" |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 28 | #include "bootutil/sha256.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 29 | #include "bootutil/sign_key.h" |
| 30 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 31 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 32 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 33 | #ifdef MCUBOOT_ENC_IMAGES |
| 34 | #include "bootutil/enc_key.h" |
| 35 | #endif |
| 36 | #if defined(MCUBOOT_SIGN_RSA) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 37 | #include "mbedtls/rsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 38 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 39 | #if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 40 | #include "mbedtls/ecdsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 41 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 42 | #include "mbedtls/asn1.h" |
| 43 | |
| 44 | #include "bootutil_priv.h" |
| 45 | |
| 46 | /* |
| 47 | * Compute SHA256 over the image. |
| 48 | */ |
| 49 | static int |
| 50 | bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap, |
| 51 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 52 | uint8_t *hash_result, uint8_t *seed, int seed_len) |
| 53 | { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 54 | bootutil_sha256_context sha256_ctx; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 55 | uint32_t blk_sz; |
| 56 | uint32_t size; |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 57 | uint16_t hdr_size; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 58 | uint32_t off; |
| 59 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 60 | #ifdef MCUBOOT_ENC_IMAGES |
| 61 | uint32_t blk_off; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 62 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 63 | |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 64 | bootutil_sha256_init(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 65 | |
| 66 | /* in some cases (split image) the hash is seeded with data from |
| 67 | * the loader image */ |
Fabio Utzig | 5398604 | 2017-12-12 14:57:07 -0200 | [diff] [blame] | 68 | if (seed && (seed_len > 0)) { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 69 | bootutil_sha256_update(&sha256_ctx, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 72 | #ifdef MCUBOOT_ENC_IMAGES |
| 73 | /* Encrypted images only exist in slot1 */ |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 74 | if (fap->fa_id == FLASH_AREA_IMAGE_1 && IS_ENCRYPTED(hdr) && !boot_enc_valid(fap)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | #endif |
| 78 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 79 | /* |
| 80 | * Hash is computed over image header and image itself. No TLV is |
| 81 | * included ATM. |
| 82 | */ |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 83 | hdr_size = hdr->ih_hdr_size; |
| 84 | size = hdr->ih_img_size + hdr_size; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 85 | for (off = 0; off < size; off += blk_sz) { |
| 86 | blk_sz = size - off; |
| 87 | if (blk_sz > tmp_buf_sz) { |
| 88 | blk_sz = tmp_buf_sz; |
| 89 | } |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 90 | #ifdef MCUBOOT_ENC_IMAGES |
| 91 | /* Avoid reading header data together with other image data |
| 92 | * because the header is not encrypted, so maintain correct |
| 93 | * bounds when sending encrypted data to decrypt routine. |
| 94 | */ |
| 95 | if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) { |
| 96 | blk_sz = hdr_size - off; |
| 97 | } |
| 98 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 99 | rc = flash_area_read(fap, off, tmp_buf, blk_sz); |
| 100 | if (rc) { |
| 101 | return rc; |
| 102 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 103 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 104 | if (fap->fa_id == FLASH_AREA_IMAGE_1 && IS_ENCRYPTED(hdr) && off >= hdr_size) { |
| 105 | blk_off = (off - hdr_size) & 0xf; |
| 106 | boot_encrypt(fap, off - hdr_size, blk_sz, blk_off, tmp_buf); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 107 | } |
| 108 | #endif |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 109 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 110 | } |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 111 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 117 | * Currently, we only support being able to verify one type of |
| 118 | * signature, because there is a single verification function that we |
| 119 | * call. List the type of TLV we are expecting. If we aren't |
| 120 | * configured for any signature, don't define this macro. |
| 121 | */ |
| 122 | #if defined(MCUBOOT_SIGN_RSA) |
Marko Kiiskila | 8dd56f3 | 2017-08-22 21:40:49 -0700 | [diff] [blame] | 123 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 124 | # define EXPECTED_SIG_LEN(x) ((x) == 256) /* 2048 bits */ |
| 125 | # if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
| 126 | # error "Multiple signature types not yet supported" |
| 127 | # endif |
| 128 | #elif defined(MCUBOOT_SIGN_EC) |
| 129 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224 |
| 130 | # define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */ |
| 131 | # if defined(MCUBOOT_SIGN_EC256) |
| 132 | # error "Multiple signature types not yet supported" |
| 133 | # endif |
| 134 | #elif defined(MCUBOOT_SIGN_EC256) |
| 135 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256 |
| 136 | # define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */ |
| 137 | #endif |
| 138 | |
| 139 | #ifdef EXPECTED_SIG_TLV |
| 140 | static int |
| 141 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 142 | { |
| 143 | bootutil_sha256_context sha256_ctx; |
| 144 | int i; |
| 145 | const struct bootutil_key *key; |
| 146 | uint8_t hash[32]; |
| 147 | |
Fabio Utzig | 9911b18 | 2017-09-05 20:29:42 -0300 | [diff] [blame] | 148 | assert(keyhash_len <= 32); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 149 | |
| 150 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 151 | key = &bootutil_keys[i]; |
| 152 | bootutil_sha256_init(&sha256_ctx); |
| 153 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 154 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 155 | if (!memcmp(hash, keyhash, keyhash_len)) { |
| 156 | return i; |
| 157 | } |
| 158 | } |
| 159 | return -1; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 164 | * Verify the integrity of the image. |
| 165 | * Return non-zero if image could not be validated/does not validate. |
| 166 | */ |
| 167 | int |
| 168 | bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap, |
| 169 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 170 | uint8_t *seed, int seed_len, uint8_t *out_hash) |
| 171 | { |
| 172 | uint32_t off; |
David Brown | 3eaa2a1 | 2017-09-01 11:19:03 -0600 | [diff] [blame] | 173 | uint32_t end; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 174 | int sha256_valid = 0; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 175 | struct image_tlv_info info; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 176 | #ifdef EXPECTED_SIG_TLV |
| 177 | int valid_signature = 0; |
| 178 | int key_id = -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 179 | #endif |
| 180 | struct image_tlv tlv; |
| 181 | uint8_t buf[256]; |
| 182 | uint8_t hash[32]; |
| 183 | int rc; |
| 184 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 185 | rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 186 | if (rc) { |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | if (out_hash) { |
| 191 | memcpy(out_hash, hash, 32); |
| 192 | } |
| 193 | |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 194 | /* The TLVs come after the image. */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 195 | /* After image there are TLVs. */ |
| 196 | off = hdr->ih_img_size + hdr->ih_hdr_size; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 197 | |
| 198 | rc = flash_area_read(fap, off, &info, sizeof(info)); |
| 199 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 200 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 201 | } |
| 202 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 203 | return -1; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 204 | } |
David Brown | 3eaa2a1 | 2017-09-01 11:19:03 -0600 | [diff] [blame] | 205 | end = off + info.it_tlv_tot; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 206 | off += sizeof(info); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 207 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 208 | /* |
| 209 | * Traverse through all of the TLVs, performing any checks we know |
| 210 | * and are able to do. |
| 211 | */ |
David Brown | 3eaa2a1 | 2017-09-01 11:19:03 -0600 | [diff] [blame] | 212 | for (; off < end; off += sizeof(tlv) + tlv.it_len) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 213 | rc = flash_area_read(fap, off, &tlv, sizeof tlv); |
| 214 | if (rc) { |
| 215 | return rc; |
| 216 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 217 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 218 | if (tlv.it_type == IMAGE_TLV_SHA256) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 219 | /* |
| 220 | * Verify the SHA256 image hash. This must always be |
| 221 | * present. |
| 222 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 223 | if (tlv.it_len != sizeof(hash)) { |
| 224 | return -1; |
| 225 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 226 | rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash); |
| 227 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 228 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 229 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 230 | if (memcmp(hash, buf, sizeof(hash))) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 231 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 232 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 233 | |
| 234 | sha256_valid = 1; |
| 235 | #ifdef EXPECTED_SIG_TLV |
| 236 | } else if (tlv.it_type == IMAGE_TLV_KEYHASH) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 237 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 238 | * Determine which key we should be checking. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 239 | */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 240 | if (tlv.it_len > 32) { |
| 241 | return -1; |
| 242 | } |
| 243 | rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len); |
| 244 | if (rc) { |
| 245 | return rc; |
| 246 | } |
| 247 | key_id = bootutil_find_key(buf, tlv.it_len); |
| 248 | /* |
| 249 | * The key may not be found, which is acceptable. There |
| 250 | * can be multiple signatures, each preceded by a key. |
| 251 | */ |
| 252 | } else if (tlv.it_type == EXPECTED_SIG_TLV) { |
| 253 | /* Ignore this signature if it is out of bounds. */ |
| 254 | if (key_id < 0 || key_id >= bootutil_key_cnt) { |
| 255 | key_id = -1; |
| 256 | continue; |
| 257 | } |
| 258 | if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) { |
| 259 | return -1; |
| 260 | } |
| 261 | rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len); |
| 262 | if (rc) { |
| 263 | return -1; |
| 264 | } |
| 265 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id); |
| 266 | if (rc == 0) { |
| 267 | valid_signature = 1; |
| 268 | } |
| 269 | key_id = -1; |
| 270 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 273 | |
| 274 | if (!sha256_valid) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 275 | return -1; |
| 276 | } |
| 277 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 278 | #ifdef EXPECTED_SIG_TLV |
| 279 | if (!valid_signature) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | #endif |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 283 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 284 | return 0; |
| 285 | } |