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 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 20 | /* |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 21 | * Modifications are Copyright (c) 2019-2020 Arm Limited. |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | #include <stddef.h> |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 25 | #include <stdint.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
| 27 | #include <string.h> |
| 28 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 29 | #include <flash_map_backend/flash_map_backend.h> |
| 30 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 31 | #include "bootutil/image.h" |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 32 | #include "bootutil/sha256.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 33 | #include "bootutil/sign_key.h" |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 34 | #include "bootutil/security_cnt.h" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 35 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 36 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 37 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 38 | #ifdef MCUBOOT_ENC_IMAGES |
| 39 | #include "bootutil/enc_key.h" |
| 40 | #endif |
| 41 | #if defined(MCUBOOT_SIGN_RSA) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 42 | #include "mbedtls/rsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 43 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 44 | #if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 45 | #include "mbedtls/ecdsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 46 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 47 | #include "mbedtls/asn1.h" |
| 48 | |
| 49 | #include "bootutil_priv.h" |
| 50 | |
| 51 | /* |
| 52 | * Compute SHA256 over the image. |
| 53 | */ |
| 54 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 55 | bootutil_img_hash(struct enc_key_data *enc_state, int image_index, |
| 56 | struct image_header *hdr, const struct flash_area *fap, |
| 57 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *hash_result, |
| 58 | uint8_t *seed, int seed_len) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 59 | { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 60 | bootutil_sha256_context sha256_ctx; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 61 | uint32_t blk_sz; |
| 62 | uint32_t size; |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 63 | uint16_t hdr_size; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 64 | uint32_t off; |
| 65 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 66 | uint32_t blk_off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 67 | uint32_t tlv_off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 68 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 69 | #if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES) |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 70 | (void)enc_state; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 71 | (void)image_index; |
Fabio Utzig | c962135 | 2019-08-08 12:15:51 -0300 | [diff] [blame] | 72 | (void)hdr_size; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 73 | (void)blk_off; |
| 74 | (void)tlv_off; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 75 | #endif |
| 76 | |
Fabio Utzig | bc07793 | 2019-08-26 11:16:34 -0300 | [diff] [blame] | 77 | #ifdef MCUBOOT_ENC_IMAGES |
| 78 | /* Encrypted images only exist in the secondary slot */ |
| 79 | if (MUST_DECRYPT(fap, image_index, hdr) && |
| 80 | !boot_enc_valid(enc_state, image_index, fap)) { |
| 81 | return -1; |
| 82 | } |
| 83 | #endif |
| 84 | |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 85 | bootutil_sha256_init(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 86 | |
| 87 | /* in some cases (split image) the hash is seeded with data from |
| 88 | * the loader image */ |
Fabio Utzig | 5398604 | 2017-12-12 14:57:07 -0200 | [diff] [blame] | 89 | if (seed && (seed_len > 0)) { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 90 | bootutil_sha256_update(&sha256_ctx, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 91 | } |
| 92 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 93 | /* Hash is computed over image header and image itself. */ |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 94 | size = hdr_size = hdr->ih_hdr_size; |
| 95 | size += hdr->ih_img_size; |
| 96 | tlv_off = size; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 97 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 98 | /* If protected TLVs are present they are also hashed. */ |
| 99 | size += hdr->ih_protect_tlv_size; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 100 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 101 | for (off = 0; off < size; off += blk_sz) { |
| 102 | blk_sz = size - off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 103 | if (blk_sz > tmp_buf_sz) { |
| 104 | blk_sz = tmp_buf_sz; |
| 105 | } |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 106 | #ifdef MCUBOOT_ENC_IMAGES |
| 107 | /* The only data that is encrypted in an image is the payload; |
| 108 | * both header and TLVs (when protected) are not. |
| 109 | */ |
| 110 | if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) { |
| 111 | /* read only the header */ |
| 112 | blk_sz = hdr_size - off; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 113 | } |
| 114 | if ((off < tlv_off) && ((off + blk_sz) > tlv_off)) { |
| 115 | /* read only up to the end of the image payload */ |
| 116 | blk_sz = tlv_off - off; |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 117 | } |
| 118 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 119 | rc = flash_area_read(fap, off, tmp_buf, blk_sz); |
| 120 | if (rc) { |
| 121 | return rc; |
| 122 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 123 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 124 | if (MUST_DECRYPT(fap, image_index, hdr)) { |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 125 | /* Only payload is encrypted (area between header and TLVs) */ |
| 126 | if (off >= hdr_size && off < tlv_off) { |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 127 | blk_off = (off - hdr_size) & 0xf; |
| 128 | boot_encrypt(enc_state, image_index, fap, off - hdr_size, |
| 129 | blk_sz, blk_off, tmp_buf); |
| 130 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 131 | } |
| 132 | #endif |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 133 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 134 | } |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 135 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 141 | * Currently, we only support being able to verify one type of |
| 142 | * signature, because there is a single verification function that we |
| 143 | * call. List the type of TLV we are expecting. If we aren't |
| 144 | * configured for any signature, don't define this macro. |
| 145 | */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 146 | #if (defined(MCUBOOT_SIGN_RSA) + \ |
| 147 | defined(MCUBOOT_SIGN_EC) + \ |
| 148 | defined(MCUBOOT_SIGN_EC256) + \ |
| 149 | defined(MCUBOOT_SIGN_ED25519)) > 1 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 150 | #error "Only a single signature type is supported!" |
| 151 | #endif |
| 152 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 153 | #if defined(MCUBOOT_SIGN_RSA) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 154 | # if MCUBOOT_SIGN_RSA_LEN == 2048 |
| 155 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS |
| 156 | # elif MCUBOOT_SIGN_RSA_LEN == 3072 |
| 157 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS |
| 158 | # else |
| 159 | # error "Unsupported RSA signature length" |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 160 | # endif |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 161 | # define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8) |
| 162 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 163 | #elif defined(MCUBOOT_SIGN_EC) |
| 164 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 165 | # define SIG_BUF_SIZE 128 |
David Brown | a360826 | 2019-12-12 15:35:31 -0700 | [diff] [blame] | 166 | # define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 167 | #elif defined(MCUBOOT_SIGN_EC256) |
| 168 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 169 | # define SIG_BUF_SIZE 128 |
David Brown | a360826 | 2019-12-12 15:35:31 -0700 | [diff] [blame] | 170 | # define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 171 | #elif defined(MCUBOOT_SIGN_ED25519) |
| 172 | # define EXPECTED_SIG_TLV IMAGE_TLV_ED25519 |
| 173 | # define SIG_BUF_SIZE 64 |
| 174 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 175 | #else |
| 176 | # define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 180 | #if !defined(MCUBOOT_HW_KEY) |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 181 | static int |
| 182 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 183 | { |
| 184 | bootutil_sha256_context sha256_ctx; |
| 185 | int i; |
| 186 | const struct bootutil_key *key; |
| 187 | uint8_t hash[32]; |
| 188 | |
Fabio Utzig | 15c1467 | 2019-08-09 07:45:20 -0300 | [diff] [blame] | 189 | if (keyhash_len > 32) { |
| 190 | return -1; |
| 191 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 192 | |
| 193 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 194 | key = &bootutil_keys[i]; |
| 195 | bootutil_sha256_init(&sha256_ctx); |
| 196 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 197 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 198 | if (!memcmp(hash, keyhash, keyhash_len)) { |
| 199 | return i; |
| 200 | } |
| 201 | } |
| 202 | return -1; |
| 203 | } |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 204 | #else |
| 205 | extern unsigned int pub_key_len; |
| 206 | static int |
| 207 | bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len) |
| 208 | { |
| 209 | bootutil_sha256_context sha256_ctx; |
| 210 | uint8_t hash[32]; |
| 211 | uint8_t key_hash[32]; |
| 212 | size_t key_hash_size = sizeof(key_hash); |
| 213 | int rc; |
| 214 | |
| 215 | bootutil_sha256_init(&sha256_ctx); |
| 216 | bootutil_sha256_update(&sha256_ctx, key, key_len); |
| 217 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 218 | |
| 219 | rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size); |
| 220 | if (rc) { |
| 221 | return rc; |
| 222 | } |
| 223 | |
| 224 | if (!memcmp(hash, key_hash, key_hash_size)) { |
| 225 | bootutil_keys[0].key = key; |
| 226 | pub_key_len = key_len; |
| 227 | return 0; |
| 228 | } |
| 229 | return -1; |
| 230 | } |
| 231 | #endif /* !MCUBOOT_HW_KEY */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 232 | #endif |
| 233 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 234 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 235 | /** |
| 236 | * Reads the value of an image's security counter. |
| 237 | * |
| 238 | * @param hdr Pointer to the image header structure. |
| 239 | * @param fap Pointer to a description structure of the image's |
| 240 | * flash area. |
| 241 | * @param security_cnt Pointer to store the security counter value. |
| 242 | * |
| 243 | * @return 0 on success; nonzero on failure. |
| 244 | */ |
| 245 | int32_t |
| 246 | bootutil_get_img_security_cnt(struct image_header *hdr, |
| 247 | const struct flash_area *fap, |
| 248 | uint32_t *img_security_cnt) |
| 249 | { |
| 250 | struct image_tlv_iter it; |
| 251 | uint32_t off; |
| 252 | uint16_t len; |
| 253 | int32_t rc; |
| 254 | |
| 255 | if ((hdr == NULL) || |
| 256 | (fap == NULL) || |
| 257 | (img_security_cnt == NULL)) { |
| 258 | /* Invalid parameter. */ |
| 259 | return BOOT_EBADARGS; |
| 260 | } |
| 261 | |
| 262 | /* The security counter TLV is in the protected part of the TLV area. */ |
| 263 | if (hdr->ih_protect_tlv_size == 0) { |
| 264 | return BOOT_EBADIMAGE; |
| 265 | } |
| 266 | |
| 267 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true); |
| 268 | if (rc) { |
| 269 | return rc; |
| 270 | } |
| 271 | |
| 272 | /* Traverse through the protected TLV area to find |
| 273 | * the security counter TLV. |
| 274 | */ |
| 275 | |
| 276 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 277 | if (rc != 0) { |
| 278 | /* Security counter TLV has not been found. */ |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | if (len != sizeof(*img_security_cnt)) { |
| 283 | /* Security counter is not valid. */ |
| 284 | return BOOT_EBADIMAGE; |
| 285 | } |
| 286 | |
| 287 | rc = flash_area_read(fap, off, img_security_cnt, len); |
| 288 | if (rc != 0) { |
| 289 | return BOOT_EFLASH; |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 295 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 296 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 297 | * Verify the integrity of the image. |
| 298 | * Return non-zero if image could not be validated/does not validate. |
| 299 | */ |
| 300 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 301 | bootutil_img_validate(struct enc_key_data *enc_state, int image_index, |
| 302 | struct image_header *hdr, const struct flash_area *fap, |
| 303 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed, |
| 304 | int seed_len, uint8_t *out_hash) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 305 | { |
| 306 | uint32_t off; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 307 | uint16_t len; |
David Brown | d13318a | 2019-12-04 17:28:40 -0700 | [diff] [blame] | 308 | uint16_t type; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 309 | int sha256_valid = 0; |
| 310 | #ifdef EXPECTED_SIG_TLV |
| 311 | int valid_signature = 0; |
| 312 | int key_id = -1; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 313 | #ifdef MCUBOOT_HW_KEY |
| 314 | /* Few extra bytes for encoding and for public exponent. */ |
| 315 | uint8_t key_buf[SIG_BUF_SIZE + 24]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 316 | #endif |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 317 | #endif /* EXPECTED_SIG_TLV */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 318 | struct image_tlv_iter it; |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 319 | uint8_t buf[SIG_BUF_SIZE]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 320 | uint8_t hash[32]; |
| 321 | int rc; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 322 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 323 | uint32_t security_cnt = UINT32_MAX; |
| 324 | uint32_t img_security_cnt = 0; |
| 325 | int32_t security_counter_valid = 0; |
| 326 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 327 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 328 | rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf, |
| 329 | tmp_buf_sz, hash, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 330 | if (rc) { |
| 331 | return rc; |
| 332 | } |
| 333 | |
| 334 | if (out_hash) { |
| 335 | memcpy(out_hash, hash, 32); |
| 336 | } |
| 337 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 338 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 339 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 340 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 341 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 342 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 343 | /* |
| 344 | * Traverse through all of the TLVs, performing any checks we know |
| 345 | * and are able to do. |
| 346 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 347 | while (true) { |
| 348 | rc = bootutil_tlv_iter_next(&it, &off, &len, &type); |
| 349 | if (rc < 0) { |
| 350 | return -1; |
| 351 | } else if (rc > 0) { |
| 352 | break; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 353 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 354 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 355 | if (type == IMAGE_TLV_SHA256) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 356 | /* |
| 357 | * Verify the SHA256 image hash. This must always be |
| 358 | * present. |
| 359 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 360 | if (len != sizeof(hash)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 361 | return -1; |
| 362 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 363 | rc = flash_area_read(fap, off, buf, sizeof hash); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 364 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 365 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 366 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 367 | if (memcmp(hash, buf, sizeof(hash))) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 368 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 369 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 370 | |
| 371 | sha256_valid = 1; |
| 372 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 373 | #ifndef MCUBOOT_HW_KEY |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 374 | } else if (type == IMAGE_TLV_KEYHASH) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 375 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 376 | * Determine which key we should be checking. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 377 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 378 | if (len > 32) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 379 | return -1; |
| 380 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 381 | rc = flash_area_read(fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 382 | if (rc) { |
| 383 | return rc; |
| 384 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 385 | key_id = bootutil_find_key(buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 386 | /* |
| 387 | * The key may not be found, which is acceptable. There |
| 388 | * can be multiple signatures, each preceded by a key. |
| 389 | */ |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 390 | #else |
| 391 | } else if (type == IMAGE_TLV_PUBKEY) { |
| 392 | /* |
| 393 | * Determine which key we should be checking. |
| 394 | */ |
| 395 | if (len > sizeof(key_buf)) { |
| 396 | return -1; |
| 397 | } |
| 398 | rc = flash_area_read(fap, off, key_buf, len); |
| 399 | if (rc) { |
| 400 | return rc; |
| 401 | } |
| 402 | key_id = bootutil_find_key(image_index, key_buf, len); |
| 403 | /* |
| 404 | * The key may not be found, which is acceptable. There |
| 405 | * can be multiple signatures, each preceded by a key. |
| 406 | */ |
| 407 | #endif /* !MCUBOOT_HW_KEY */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 408 | } else if (type == EXPECTED_SIG_TLV) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 409 | /* Ignore this signature if it is out of bounds. */ |
| 410 | if (key_id < 0 || key_id >= bootutil_key_cnt) { |
| 411 | key_id = -1; |
| 412 | continue; |
| 413 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 414 | if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 415 | return -1; |
| 416 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 417 | rc = flash_area_read(fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 418 | if (rc) { |
| 419 | return -1; |
| 420 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 421 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 422 | if (rc == 0) { |
| 423 | valid_signature = 1; |
| 424 | } |
| 425 | key_id = -1; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 426 | #endif /* EXPECTED_SIG_TLV */ |
| 427 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 428 | } else if (type == IMAGE_TLV_SEC_CNT) { |
| 429 | /* |
| 430 | * Verify the image's security counter. |
| 431 | * This must always be present. |
| 432 | */ |
| 433 | if (len != sizeof(img_security_cnt)) { |
| 434 | /* Security counter is not valid. */ |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | rc = flash_area_read(fap, off, &img_security_cnt, len); |
| 439 | if (rc) { |
| 440 | return rc; |
| 441 | } |
| 442 | |
| 443 | rc = boot_nv_security_counter_get(image_index, &security_cnt); |
| 444 | if (rc) { |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | /* Compare the new image's security counter value against the |
| 449 | * stored security counter value. |
| 450 | */ |
| 451 | if (img_security_cnt < security_cnt) { |
| 452 | /* The image's security counter is not accepted. */ |
| 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | /* The image's security counter has been successfully verified. */ |
| 457 | security_counter_valid = 1; |
| 458 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 461 | |
| 462 | if (!sha256_valid) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 463 | return -1; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 464 | #ifdef EXPECTED_SIG_TLV |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 465 | } else if (!valid_signature) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 466 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 467 | #endif |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 468 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 469 | } else if (!security_counter_valid) { |
| 470 | return -1; |
| 471 | #endif |
| 472 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 473 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 474 | return 0; |
| 475 | } |