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