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