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" |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 36 | #include "bootutil/crypto/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) { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 137 | bootutil_sha256_drop(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 138 | return rc; |
| 139 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 140 | #ifdef MCUBOOT_ENC_IMAGES |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 141 | if (MUST_DECRYPT(fap, image_index, hdr)) { |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 142 | /* Only payload is encrypted (area between header and TLVs) */ |
| 143 | if (off >= hdr_size && off < tlv_off) { |
Fabio Utzig | c21c210 | 2019-09-10 10:10:42 -0300 | [diff] [blame] | 144 | blk_off = (off - hdr_size) & 0xf; |
| 145 | boot_encrypt(enc_state, image_index, fap, off - hdr_size, |
| 146 | blk_sz, blk_off, tmp_buf); |
| 147 | } |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 148 | } |
| 149 | #endif |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 150 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 151 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 152 | #endif /* MCUBOOT_RAM_LOAD */ |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 153 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 154 | bootutil_sha256_drop(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 160 | * Currently, we only support being able to verify one type of |
| 161 | * signature, because there is a single verification function that we |
| 162 | * call. List the type of TLV we are expecting. If we aren't |
| 163 | * configured for any signature, don't define this macro. |
| 164 | */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 165 | #if (defined(MCUBOOT_SIGN_RSA) + \ |
| 166 | defined(MCUBOOT_SIGN_EC) + \ |
| 167 | defined(MCUBOOT_SIGN_EC256) + \ |
| 168 | defined(MCUBOOT_SIGN_ED25519)) > 1 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 169 | #error "Only a single signature type is supported!" |
| 170 | #endif |
| 171 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 172 | #if defined(MCUBOOT_SIGN_RSA) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 173 | # if MCUBOOT_SIGN_RSA_LEN == 2048 |
| 174 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS |
| 175 | # elif MCUBOOT_SIGN_RSA_LEN == 3072 |
| 176 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS |
| 177 | # else |
| 178 | # error "Unsupported RSA signature length" |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 179 | # endif |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 180 | # define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8) |
| 181 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 182 | #elif defined(MCUBOOT_SIGN_EC) |
| 183 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 184 | # define SIG_BUF_SIZE 128 |
David Brown | a360826 | 2019-12-12 15:35:31 -0700 | [diff] [blame] | 185 | # define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 186 | #elif defined(MCUBOOT_SIGN_EC256) |
| 187 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 188 | # define SIG_BUF_SIZE 128 |
David Brown | a360826 | 2019-12-12 15:35:31 -0700 | [diff] [blame] | 189 | # define EXPECTED_SIG_LEN(x) (1) /* always true, ASN.1 will validate */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 190 | #elif defined(MCUBOOT_SIGN_ED25519) |
| 191 | # define EXPECTED_SIG_TLV IMAGE_TLV_ED25519 |
| 192 | # define SIG_BUF_SIZE 64 |
| 193 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 194 | #else |
| 195 | # define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 196 | #endif |
| 197 | |
| 198 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 199 | #if !defined(MCUBOOT_HW_KEY) |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 200 | static int |
| 201 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 202 | { |
| 203 | bootutil_sha256_context sha256_ctx; |
| 204 | int i; |
| 205 | const struct bootutil_key *key; |
| 206 | uint8_t hash[32]; |
| 207 | |
Fabio Utzig | 15c1467 | 2019-08-09 07:45:20 -0300 | [diff] [blame] | 208 | if (keyhash_len > 32) { |
| 209 | return -1; |
| 210 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 211 | |
| 212 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 213 | key = &bootutil_keys[i]; |
| 214 | bootutil_sha256_init(&sha256_ctx); |
| 215 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 216 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 217 | if (!memcmp(hash, keyhash, keyhash_len)) { |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 218 | bootutil_sha256_drop(&sha256_ctx); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 219 | return i; |
| 220 | } |
| 221 | } |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 222 | bootutil_sha256_drop(&sha256_ctx); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 223 | return -1; |
| 224 | } |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 225 | #else |
| 226 | extern unsigned int pub_key_len; |
| 227 | static int |
| 228 | bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len) |
| 229 | { |
| 230 | bootutil_sha256_context sha256_ctx; |
| 231 | uint8_t hash[32]; |
| 232 | uint8_t key_hash[32]; |
| 233 | size_t key_hash_size = sizeof(key_hash); |
| 234 | int rc; |
| 235 | |
| 236 | bootutil_sha256_init(&sha256_ctx); |
| 237 | bootutil_sha256_update(&sha256_ctx, key, key_len); |
| 238 | bootutil_sha256_finish(&sha256_ctx, hash); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 239 | bootutil_sha256_drop(&sha256_ctx); |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 240 | |
| 241 | rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size); |
| 242 | if (rc) { |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | if (!memcmp(hash, key_hash, key_hash_size)) { |
| 247 | bootutil_keys[0].key = key; |
| 248 | pub_key_len = key_len; |
| 249 | return 0; |
| 250 | } |
| 251 | return -1; |
| 252 | } |
| 253 | #endif /* !MCUBOOT_HW_KEY */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 254 | #endif |
| 255 | |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 256 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 257 | /** |
| 258 | * Reads the value of an image's security counter. |
| 259 | * |
| 260 | * @param hdr Pointer to the image header structure. |
| 261 | * @param fap Pointer to a description structure of the image's |
| 262 | * flash area. |
| 263 | * @param security_cnt Pointer to store the security counter value. |
| 264 | * |
| 265 | * @return 0 on success; nonzero on failure. |
| 266 | */ |
| 267 | int32_t |
| 268 | bootutil_get_img_security_cnt(struct image_header *hdr, |
| 269 | const struct flash_area *fap, |
| 270 | uint32_t *img_security_cnt) |
| 271 | { |
| 272 | struct image_tlv_iter it; |
| 273 | uint32_t off; |
| 274 | uint16_t len; |
| 275 | int32_t rc; |
| 276 | |
| 277 | if ((hdr == NULL) || |
| 278 | (fap == NULL) || |
| 279 | (img_security_cnt == NULL)) { |
| 280 | /* Invalid parameter. */ |
| 281 | return BOOT_EBADARGS; |
| 282 | } |
| 283 | |
| 284 | /* The security counter TLV is in the protected part of the TLV area. */ |
| 285 | if (hdr->ih_protect_tlv_size == 0) { |
| 286 | return BOOT_EBADIMAGE; |
| 287 | } |
| 288 | |
| 289 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_SEC_CNT, true); |
| 290 | if (rc) { |
| 291 | return rc; |
| 292 | } |
| 293 | |
| 294 | /* Traverse through the protected TLV area to find |
| 295 | * the security counter TLV. |
| 296 | */ |
| 297 | |
| 298 | rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); |
| 299 | if (rc != 0) { |
| 300 | /* Security counter TLV has not been found. */ |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | if (len != sizeof(*img_security_cnt)) { |
| 305 | /* Security counter is not valid. */ |
| 306 | return BOOT_EBADIMAGE; |
| 307 | } |
| 308 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 309 | rc = LOAD_IMAGE_DATA(hdr, fap, off, img_security_cnt, len); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 310 | if (rc != 0) { |
| 311 | return BOOT_EFLASH; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
| 317 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 318 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 319 | * Verify the integrity of the image. |
| 320 | * Return non-zero if image could not be validated/does not validate. |
| 321 | */ |
| 322 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 323 | bootutil_img_validate(struct enc_key_data *enc_state, int image_index, |
| 324 | struct image_header *hdr, const struct flash_area *fap, |
| 325 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed, |
| 326 | int seed_len, uint8_t *out_hash) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 327 | { |
| 328 | uint32_t off; |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 329 | uint16_t len; |
David Brown | d13318a | 2019-12-04 17:28:40 -0700 | [diff] [blame] | 330 | uint16_t type; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 331 | int sha256_valid = 0; |
| 332 | #ifdef EXPECTED_SIG_TLV |
| 333 | int valid_signature = 0; |
| 334 | int key_id = -1; |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 335 | #ifdef MCUBOOT_HW_KEY |
| 336 | /* Few extra bytes for encoding and for public exponent. */ |
| 337 | uint8_t key_buf[SIG_BUF_SIZE + 24]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 338 | #endif |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 339 | #endif /* EXPECTED_SIG_TLV */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 340 | struct image_tlv_iter it; |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 341 | uint8_t buf[SIG_BUF_SIZE]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 342 | uint8_t hash[32]; |
| 343 | int rc; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 344 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 345 | uint32_t security_cnt = UINT32_MAX; |
| 346 | uint32_t img_security_cnt = 0; |
| 347 | int32_t security_counter_valid = 0; |
| 348 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 349 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 350 | rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf, |
| 351 | tmp_buf_sz, hash, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 352 | if (rc) { |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | if (out_hash) { |
| 357 | memcpy(out_hash, hash, 32); |
| 358 | } |
| 359 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 360 | rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_ANY, false); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 361 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 362 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 363 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 364 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 365 | /* |
| 366 | * Traverse through all of the TLVs, performing any checks we know |
| 367 | * and are able to do. |
| 368 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 369 | while (true) { |
| 370 | rc = bootutil_tlv_iter_next(&it, &off, &len, &type); |
| 371 | if (rc < 0) { |
| 372 | return -1; |
| 373 | } else if (rc > 0) { |
| 374 | break; |
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 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 377 | if (type == IMAGE_TLV_SHA256) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 378 | /* |
| 379 | * Verify the SHA256 image hash. This must always be |
| 380 | * present. |
| 381 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 382 | if (len != sizeof(hash)) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 383 | return -1; |
| 384 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 385 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, sizeof(hash)); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 386 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 387 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 388 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 389 | if (memcmp(hash, buf, sizeof(hash))) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 390 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 391 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 392 | |
| 393 | sha256_valid = 1; |
| 394 | #ifdef EXPECTED_SIG_TLV |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 395 | #ifndef MCUBOOT_HW_KEY |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 396 | } else if (type == IMAGE_TLV_KEYHASH) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 397 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 398 | * Determine which key we should be checking. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 399 | */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 400 | if (len > 32) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 401 | return -1; |
| 402 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 403 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 404 | if (rc) { |
| 405 | return rc; |
| 406 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 407 | key_id = bootutil_find_key(buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 408 | /* |
| 409 | * The key may not be found, which is acceptable. There |
| 410 | * can be multiple signatures, each preceded by a key. |
| 411 | */ |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 412 | #else |
| 413 | } else if (type == IMAGE_TLV_PUBKEY) { |
| 414 | /* |
| 415 | * Determine which key we should be checking. |
| 416 | */ |
| 417 | if (len > sizeof(key_buf)) { |
| 418 | return -1; |
| 419 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 420 | rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len); |
David Vincze | 03368b8 | 2020-04-01 12:53:53 +0200 | [diff] [blame] | 421 | if (rc) { |
| 422 | return rc; |
| 423 | } |
| 424 | key_id = bootutil_find_key(image_index, key_buf, len); |
| 425 | /* |
| 426 | * The key may not be found, which is acceptable. There |
| 427 | * can be multiple signatures, each preceded by a key. |
| 428 | */ |
| 429 | #endif /* !MCUBOOT_HW_KEY */ |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 430 | } else if (type == EXPECTED_SIG_TLV) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 431 | /* Ignore this signature if it is out of bounds. */ |
| 432 | if (key_id < 0 || key_id >= bootutil_key_cnt) { |
| 433 | key_id = -1; |
| 434 | continue; |
| 435 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 436 | if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 437 | return -1; |
| 438 | } |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 439 | rc = LOAD_IMAGE_DATA(hdr, fap, off, buf, len); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 440 | if (rc) { |
| 441 | return -1; |
| 442 | } |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 443 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, len, key_id); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 444 | if (rc == 0) { |
| 445 | valid_signature = 1; |
| 446 | } |
| 447 | key_id = -1; |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 448 | #endif /* EXPECTED_SIG_TLV */ |
| 449 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 450 | } else if (type == IMAGE_TLV_SEC_CNT) { |
| 451 | /* |
| 452 | * Verify the image's security counter. |
| 453 | * This must always be present. |
| 454 | */ |
| 455 | if (len != sizeof(img_security_cnt)) { |
| 456 | /* Security counter is not valid. */ |
| 457 | return -1; |
| 458 | } |
| 459 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame] | 460 | rc = LOAD_IMAGE_DATA(hdr, fap, off, &img_security_cnt, len); |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 461 | if (rc) { |
| 462 | return rc; |
| 463 | } |
| 464 | |
| 465 | rc = boot_nv_security_counter_get(image_index, &security_cnt); |
| 466 | if (rc) { |
| 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | /* Compare the new image's security counter value against the |
| 471 | * stored security counter value. |
| 472 | */ |
| 473 | if (img_security_cnt < security_cnt) { |
| 474 | /* The image's security counter is not accepted. */ |
| 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | /* The image's security counter has been successfully verified. */ |
| 479 | security_counter_valid = 1; |
| 480 | #endif /* MCUBOOT_HW_ROLLBACK_PROT */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 481 | } |
| 482 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 483 | |
| 484 | if (!sha256_valid) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 485 | return -1; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 486 | #ifdef EXPECTED_SIG_TLV |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 487 | } else if (!valid_signature) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 488 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 489 | #endif |
David Vincze | c308413 | 2020-02-18 14:50:47 +0100 | [diff] [blame] | 490 | #ifdef MCUBOOT_HW_ROLLBACK_PROT |
| 491 | } else if (!security_counter_valid) { |
| 492 | return -1; |
| 493 | #endif |
| 494 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 495 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 496 | return 0; |
| 497 | } |