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