Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [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 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 20 | /* |
| 21 | * Original code taken from mcuboot project at: |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 22 | * https://github.com/JuulLabs-OSS/mcuboot |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 23 | * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 24 | * Modifications are Copyright (c) 2018-2019 Arm Limited. |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 27 | #include <assert.h> |
| 28 | #include <stddef.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <string.h> |
| 31 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 32 | #include "flash_map/flash_map.h" |
| 33 | #include "bootutil/image.h" |
| 34 | #include "bootutil/sha256.h" |
| 35 | #include "bootutil/sign_key.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 36 | #include "security_cnt.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 37 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 38 | #ifdef MCUBOOT_SIGN_RSA |
| 39 | #include "mbedtls/rsa.h" |
| 40 | #endif |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 41 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 42 | #include "mbedtls/asn1.h" |
| 43 | |
| 44 | #include "bootutil_priv.h" |
| 45 | |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 46 | #ifdef MCUBOOT_HW_KEY |
| 47 | #include "platform/include/tfm_plat_crypto_keys.h" |
| 48 | #endif |
| 49 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 50 | /* |
| 51 | * Compute SHA256 over the image. |
| 52 | */ |
| 53 | static int |
| 54 | bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap, |
| 55 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 56 | uint8_t *hash_result, uint8_t *seed, int seed_len) |
| 57 | { |
| 58 | bootutil_sha256_context sha256_ctx; |
| 59 | uint32_t blk_sz; |
| 60 | uint32_t size; |
| 61 | uint32_t off; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 62 | |
| 63 | bootutil_sha256_init(&sha256_ctx); |
| 64 | |
| 65 | /* in some cases (split image) the hash is seeded with data from |
| 66 | * the loader image */ |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 67 | if (seed && (seed_len > 0)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 68 | bootutil_sha256_update(&sha256_ctx, seed, seed_len); |
| 69 | } |
| 70 | |
David Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 71 | /* Hash is computed over image header and image itself. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 72 | size = hdr->ih_img_size + hdr->ih_hdr_size; |
David Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 73 | |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 74 | /* If a security counter TLV and/or a dependency TLV(s) are present then the |
| 75 | * TLV info header, the security counter TLV and/or the dependency TLV(s) |
| 76 | * are also protected and must be included in the hash calculation. |
David Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 77 | */ |
| 78 | if (hdr->ih_protect_tlv_size != 0) { |
| 79 | size += hdr->ih_protect_tlv_size; |
| 80 | } |
| 81 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 82 | for (off = 0; off < size; off += blk_sz) { |
| 83 | blk_sz = size - off; |
| 84 | if (blk_sz > tmp_buf_sz) { |
| 85 | blk_sz = tmp_buf_sz; |
| 86 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 87 | |
| 88 | #ifdef MCUBOOT_RAM_LOADING |
| 89 | if (fap == NULL) { /* The image is in SRAM */ |
| 90 | memcpy(tmp_buf, (uint32_t *)(hdr->ih_load_addr + off), blk_sz); |
| 91 | } else { /* The image is in flash */ |
| 92 | #endif |
| 93 | if(flash_area_read(fap, off, tmp_buf, blk_sz)) { |
| 94 | return -1; |
| 95 | } |
| 96 | #ifdef MCUBOOT_RAM_LOADING |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 97 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 98 | #endif |
| 99 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 100 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
| 101 | } |
| 102 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Currently, we only support being able to verify one type of |
| 109 | * signature, because there is a single verification function that we |
| 110 | * call. List the type of TLV we are expecting. If we aren't |
| 111 | * configured for any signature, don't define this macro. |
| 112 | */ |
Tamas Ban | 81daed0 | 2019-05-20 15:05:22 +0100 | [diff] [blame] | 113 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 114 | #if defined(MCUBOOT_SIGN_RSA) |
Tamas Ban | 81daed0 | 2019-05-20 15:05:22 +0100 | [diff] [blame] | 115 | # if MCUBOOT_SIGN_RSA_LEN == 2048 |
| 116 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS |
| 117 | # elif MCUBOOT_SIGN_RSA_LEN == 3072 |
| 118 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS |
| 119 | # else |
| 120 | # error "Unsupported RSA signature length" |
| 121 | # endif |
| 122 | # define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8) |
| 123 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) |
| 124 | #else |
| 125 | # define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 126 | #endif |
| 127 | |
| 128 | #ifdef EXPECTED_SIG_TLV |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 129 | #ifdef MCUBOOT_HW_KEY |
| 130 | extern unsigned int pub_key_len; |
Tamas Ban | 78676ac | 2019-07-11 09:05:54 +0100 | [diff] [blame] | 131 | extern uint8_t current_image; |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 132 | static int |
| 133 | bootutil_find_key(uint8_t *key, uint16_t key_len) |
| 134 | { |
| 135 | bootutil_sha256_context sha256_ctx; |
| 136 | uint8_t hash[32]; |
| 137 | uint8_t key_hash[32]; |
| 138 | uint32_t key_hash_size= sizeof(key_hash); |
| 139 | enum tfm_plat_err_t plat_err; |
| 140 | |
| 141 | bootutil_sha256_init(&sha256_ctx); |
| 142 | bootutil_sha256_update(&sha256_ctx, key, key_len); |
| 143 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 144 | |
Tamas Ban | 78676ac | 2019-07-11 09:05:54 +0100 | [diff] [blame] | 145 | plat_err = tfm_plat_get_rotpk_hash(current_image, key_hash, &key_hash_size); |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 146 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 147 | return -1; |
| 148 | } |
Raef Coles | 25b857a | 2019-09-05 13:59:55 +0100 | [diff] [blame] | 149 | if (!boot_secure_memequal(hash, key_hash, key_hash_size)) { |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 150 | bootutil_keys[0].key = key; |
| 151 | pub_key_len = key_len; |
| 152 | return 0; |
| 153 | } |
| 154 | return -1; |
| 155 | } |
| 156 | #else |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 157 | static int |
| 158 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 159 | { |
| 160 | bootutil_sha256_context sha256_ctx; |
| 161 | int i; |
| 162 | const struct bootutil_key *key; |
| 163 | uint8_t hash[32]; |
| 164 | |
| 165 | assert(keyhash_len <= 32); |
| 166 | |
| 167 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 168 | key = &bootutil_keys[i]; |
| 169 | bootutil_sha256_init(&sha256_ctx); |
| 170 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 171 | bootutil_sha256_finish(&sha256_ctx, hash); |
Raef Coles | 25b857a | 2019-09-05 13:59:55 +0100 | [diff] [blame] | 172 | if (!boot_secure_memequal(hash, keyhash, keyhash_len)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 173 | return i; |
| 174 | } |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | #endif |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 179 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 180 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 181 | #ifdef MCUBOOT_RAM_LOADING |
| 182 | /* Check the hash of an image after it has been copied to SRAM */ |
| 183 | int |
| 184 | bootutil_check_hash_after_loading(struct image_header *hdr) |
| 185 | { |
| 186 | uint32_t off; |
| 187 | uint32_t end; |
| 188 | int sha256_valid = 0; |
| 189 | struct image_tlv_info info; |
| 190 | struct image_tlv tlv; |
| 191 | uint8_t tmp_buf[BOOT_TMPBUF_SZ]; |
| 192 | uint8_t hash[32] = {0}; |
| 193 | int rc; |
| 194 | uint32_t load_address; |
| 195 | uint32_t tlv_sz; |
| 196 | |
| 197 | rc = bootutil_img_hash(hdr, NULL, tmp_buf, BOOT_TMPBUF_SZ, hash, NULL, 0); |
| 198 | |
| 199 | if (rc) { |
| 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | load_address = (uint32_t) hdr->ih_load_addr; |
| 204 | |
| 205 | /* The TLVs come after the image. */ |
| 206 | off = hdr->ih_img_size + hdr->ih_hdr_size; |
| 207 | |
| 208 | info = *((struct image_tlv_info *)(load_address + off)); |
| 209 | |
| 210 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 211 | return BOOT_EBADMAGIC; |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 212 | } |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 213 | if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) { |
| 214 | return -1; |
| 215 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 216 | end = off + info.it_tlv_tot; |
| 217 | off += sizeof(info); |
| 218 | |
| 219 | /* |
| 220 | * Traverse through all of the TLVs, performing any checks we know |
| 221 | * and are able to do. |
| 222 | */ |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 223 | if (boot_add_uint32_overflow_check(load_address, end)) { |
| 224 | return -1; |
| 225 | } |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 226 | while (off < end) { |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 227 | tlv = *((struct image_tlv *)(load_address + off)); |
| 228 | tlv_sz = sizeof(tlv); |
| 229 | |
| 230 | if (tlv.it_type == IMAGE_TLV_SHA256) { |
| 231 | /* |
| 232 | * Verify the SHA256 image hash. This must always be present. |
| 233 | */ |
| 234 | if (tlv.it_len != sizeof(hash)) { |
| 235 | return -1; |
| 236 | } |
| 237 | |
Raef Coles | 25b857a | 2019-09-05 13:59:55 +0100 | [diff] [blame] | 238 | if (boot_secure_memequal(hash, (uint32_t *)(load_address + off + tlv_sz), |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 239 | sizeof(hash))) { |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | sha256_valid = 1; |
Tamas Ban | 576aaf9 | 2019-09-23 14:24:56 +0100 | [diff] [blame] | 244 | break; |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 245 | } |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 246 | |
| 247 | /* Avoid integer overflow. */ |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 248 | if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) { |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 249 | /* Potential overflow. */ |
| 250 | break; |
| 251 | } else { |
| 252 | off += sizeof(tlv) + tlv.it_len; |
| 253 | } |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (!sha256_valid) { |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | #endif /* MCUBOOT_RAM_LOADING */ |
| 263 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 264 | /** |
| 265 | * Reads the value of an image's security counter. |
| 266 | * |
| 267 | * @param hdr Pointer to the image header structure. |
| 268 | * @param fap Pointer to a description structure of the image's |
| 269 | * flash area. |
| 270 | * @param security_cnt Pointer to store the security counter value. |
| 271 | * |
| 272 | * @return 0 on success; nonzero on failure. |
| 273 | */ |
| 274 | int32_t |
| 275 | bootutil_get_img_security_cnt(struct image_header *hdr, |
| 276 | const struct flash_area *fap, |
| 277 | uint32_t *img_security_cnt) |
| 278 | { |
| 279 | struct image_tlv_info info; |
| 280 | struct image_tlv tlv; |
| 281 | uint32_t off; |
| 282 | uint32_t end; |
| 283 | uint32_t found = 0; |
| 284 | int32_t rc; |
| 285 | |
| 286 | if ((hdr == NULL) || |
| 287 | (fap == NULL) || |
| 288 | (img_security_cnt == NULL)) { |
| 289 | /* Invalid parameter. */ |
| 290 | return BOOT_EBADARGS; |
| 291 | } |
| 292 | |
| 293 | /* The TLVs come after the image. */ |
| 294 | off = hdr->ih_hdr_size + hdr->ih_img_size; |
| 295 | |
| 296 | /* The TLV area always starts with an image_tlv_info structure. */ |
| 297 | rc = flash_area_read(fap, off, &info, sizeof(info)); |
| 298 | if (rc != 0) { |
| 299 | return BOOT_EFLASH; |
| 300 | } |
| 301 | |
| 302 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
| 303 | return BOOT_EBADMAGIC; |
| 304 | } |
| 305 | |
| 306 | /* The security counter TLV is in the protected part of the TLV area. */ |
| 307 | if (hdr->ih_protect_tlv_size != 0) { |
| 308 | end = off + (uint32_t)hdr->ih_protect_tlv_size; |
| 309 | off += sizeof(info); |
| 310 | |
| 311 | /* Traverse through the protected TLV area to find the |
| 312 | * security counter TLV. |
| 313 | */ |
| 314 | while (off < end) { |
| 315 | rc = flash_area_read(fap, off, &tlv, sizeof(tlv)); |
| 316 | if (rc != 0) { |
| 317 | return BOOT_EFLASH; |
| 318 | } |
| 319 | |
| 320 | if (tlv.it_type == IMAGE_TLV_SEC_CNT) { |
| 321 | |
| 322 | if (tlv.it_len != sizeof(*img_security_cnt)) { |
| 323 | /* Security counter is not valid. */ |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | rc = flash_area_read(fap, off + sizeof(tlv), |
| 328 | img_security_cnt, tlv.it_len); |
| 329 | if (rc != 0) { |
| 330 | return BOOT_EFLASH; |
| 331 | } |
| 332 | |
| 333 | /* Security counter has been found. */ |
| 334 | found = 1; |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | /* Avoid integer overflow. */ |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 339 | if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) |
| 340 | { |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 341 | /* Potential overflow. */ |
| 342 | break; |
| 343 | } else { |
| 344 | off += sizeof(tlv) + tlv.it_len; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (found) { |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | return -1; |
| 354 | } |
| 355 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 356 | /* |
| 357 | * Verify the integrity of the image. |
| 358 | * Return non-zero if image could not be validated/does not validate. |
| 359 | */ |
| 360 | int |
| 361 | bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap, |
| 362 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 363 | uint8_t *seed, int seed_len, uint8_t *out_hash) |
| 364 | { |
| 365 | uint32_t off; |
| 366 | uint32_t end; |
| 367 | int sha256_valid = 0; |
| 368 | struct image_tlv_info info; |
| 369 | #ifdef EXPECTED_SIG_TLV |
| 370 | int valid_signature = 0; |
| 371 | int key_id = -1; |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 372 | #ifdef MCUBOOT_HW_KEY |
| 373 | /* Few extra bytes for encoding and for public exponent */ |
| 374 | uint8_t key_buf[SIG_BUF_SIZE + 24]; |
| 375 | #endif |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 376 | #endif |
| 377 | struct image_tlv tlv; |
Tamas Ban | 81daed0 | 2019-05-20 15:05:22 +0100 | [diff] [blame] | 378 | uint8_t buf[SIG_BUF_SIZE]; |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 379 | uint8_t hash[32] = {0}; |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 380 | uint32_t security_cnt; |
| 381 | uint32_t img_security_cnt; |
| 382 | int32_t security_counter_valid = 0; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 383 | int rc; |
| 384 | |
| 385 | rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, |
| 386 | seed, seed_len); |
| 387 | if (rc) { |
| 388 | return rc; |
| 389 | } |
| 390 | |
| 391 | if (out_hash) { |
| 392 | memcpy(out_hash, hash, 32); |
| 393 | } |
| 394 | |
| 395 | /* The TLVs come after the image. */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 396 | off = hdr->ih_img_size + hdr->ih_hdr_size; |
| 397 | |
| 398 | rc = flash_area_read(fap, off, &info, sizeof(info)); |
| 399 | if (rc) { |
| 400 | return rc; |
| 401 | } |
| 402 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 403 | return BOOT_EBADMAGIC; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 404 | } |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 405 | if (boot_add_uint32_overflow_check(off, (info.it_tlv_tot + sizeof(info)))) { |
| 406 | return -1; |
| 407 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 408 | end = off + info.it_tlv_tot; |
| 409 | off += sizeof(info); |
| 410 | |
| 411 | /* |
| 412 | * Traverse through all of the TLVs, performing any checks we know |
| 413 | * and are able to do. |
| 414 | */ |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 415 | while (off < end) { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 416 | rc = flash_area_read(fap, off, &tlv, sizeof(tlv)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 417 | if (rc) { |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | if (tlv.it_type == IMAGE_TLV_SHA256) { |
| 422 | /* |
| 423 | * Verify the SHA256 image hash. This must always be |
| 424 | * present. |
| 425 | */ |
| 426 | if (tlv.it_len != sizeof(hash)) { |
| 427 | return -1; |
| 428 | } |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 429 | rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof(hash)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 430 | if (rc) { |
| 431 | return rc; |
| 432 | } |
Raef Coles | 25b857a | 2019-09-05 13:59:55 +0100 | [diff] [blame] | 433 | if (boot_secure_memequal(hash, buf, sizeof(hash))) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | sha256_valid = 1; |
| 438 | #ifdef EXPECTED_SIG_TLV |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 439 | #ifndef MCUBOOT_HW_KEY |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 440 | } else if (tlv.it_type == IMAGE_TLV_KEYHASH) { |
| 441 | /* |
| 442 | * Determine which key we should be checking. |
| 443 | */ |
| 444 | if (tlv.it_len > 32) { |
| 445 | return -1; |
| 446 | } |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 447 | rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 448 | if (rc) { |
| 449 | return rc; |
| 450 | } |
| 451 | key_id = bootutil_find_key(buf, tlv.it_len); |
| 452 | /* |
| 453 | * The key may not be found, which is acceptable. There |
| 454 | * can be multiple signatures, each preceded by a key. |
| 455 | */ |
Tamas Ban | 1d37c34 | 2019-07-11 08:55:55 +0100 | [diff] [blame] | 456 | #else |
| 457 | } else if (tlv.it_type == IMAGE_TLV_KEY) { |
| 458 | /* |
| 459 | * Determine which key we should be checking. |
| 460 | */ |
| 461 | if (tlv.it_len > sizeof(key_buf)) { |
| 462 | return -1; |
| 463 | } |
| 464 | rc = flash_area_read(fap, off + sizeof(tlv), key_buf, tlv.it_len); |
| 465 | if (rc) { |
| 466 | return rc; |
| 467 | } |
| 468 | key_id = bootutil_find_key(key_buf, tlv.it_len); |
| 469 | /* |
| 470 | * The key may not be found, which is acceptable. There |
| 471 | * can be multiple signatures, each preceded by a key. |
| 472 | */ |
| 473 | #endif /* MCUBOOT_HW_KEY */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 474 | } else if (tlv.it_type == EXPECTED_SIG_TLV) { |
| 475 | /* Ignore this signature if it is out of bounds. */ |
David Vincze | fb06830 | 2019-07-24 11:00:22 +0200 | [diff] [blame] | 476 | if (key_id >= 0 && key_id < bootutil_key_cnt) { |
| 477 | if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) { |
| 478 | return -1; |
| 479 | } |
| 480 | rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len); |
| 481 | if (rc) { |
| 482 | return -1; |
| 483 | } |
| 484 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, |
| 485 | key_id); |
| 486 | if (rc == 0) { |
| 487 | valid_signature = 1; |
| 488 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 489 | } |
| 490 | key_id = -1; |
| 491 | #endif |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 492 | } else if (tlv.it_type == IMAGE_TLV_SEC_CNT) { |
| 493 | /* |
| 494 | * Verify the image's security counter. |
| 495 | * This must always be present. |
| 496 | */ |
| 497 | if (tlv.it_len != sizeof(img_security_cnt)) { |
| 498 | /* Security counter is not valid. */ |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | rc = flash_area_read(fap, off + sizeof(tlv), |
| 503 | &img_security_cnt, tlv.it_len); |
| 504 | if (rc) { |
| 505 | return rc; |
| 506 | } |
| 507 | |
| 508 | rc = boot_nv_security_counter_get(0, &security_cnt); |
| 509 | if (rc) { |
| 510 | return rc; |
| 511 | } |
| 512 | |
| 513 | /* Compare the new image's security counter value against the |
| 514 | * stored security counter value. |
| 515 | */ |
| 516 | if (img_security_cnt < security_cnt) { |
| 517 | /* The image's security counter is not accepted. */ |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /* The image's security counter has been successfully verified. */ |
| 522 | security_counter_valid = 1; |
| 523 | } |
| 524 | |
| 525 | /* Avoid integer overflow. */ |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 526 | if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) { |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 527 | /* Potential overflow. */ |
| 528 | break; |
| 529 | } else { |
| 530 | off += sizeof(tlv) + tlv.it_len; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 534 | if (!sha256_valid || !security_counter_valid) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | #ifdef EXPECTED_SIG_TLV |
| 539 | if (!valid_signature) { |
| 540 | return -1; |
| 541 | } |
| 542 | #endif |
| 543 | |
| 544 | return 0; |
| 545 | } |