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 | /* |
| 21 | * Modifications are Copyright (c) 2019 Arm Limited. |
| 22 | */ |
| 23 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stddef.h> |
| 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" |
| 34 | |
Fabio Utzig | ba1fbe6 | 2017-07-21 14:01:20 -0300 | [diff] [blame] | 35 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 36 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 37 | #ifdef MCUBOOT_ENC_IMAGES |
| 38 | #include "bootutil/enc_key.h" |
| 39 | #endif |
| 40 | #if defined(MCUBOOT_SIGN_RSA) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | #include "mbedtls/rsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 42 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 43 | #if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 44 | #include "mbedtls/ecdsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 45 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 46 | #include "mbedtls/asn1.h" |
| 47 | |
| 48 | #include "bootutil_priv.h" |
| 49 | |
| 50 | /* |
| 51 | * Compute SHA256 over the image. |
| 52 | */ |
| 53 | static int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 54 | bootutil_img_hash(struct enc_key_data *enc_state, 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) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 58 | { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 59 | bootutil_sha256_context sha256_ctx; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 60 | uint32_t blk_sz; |
| 61 | uint32_t size; |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 62 | uint16_t hdr_size; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 63 | uint32_t off; |
| 64 | int rc; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 65 | #ifdef MCUBOOT_ENC_IMAGES |
| 66 | uint32_t blk_off; |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 67 | #endif |
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 | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 73 | #endif |
| 74 | |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 75 | bootutil_sha256_init(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 76 | |
| 77 | /* in some cases (split image) the hash is seeded with data from |
| 78 | * the loader image */ |
Fabio Utzig | 5398604 | 2017-12-12 14:57:07 -0200 | [diff] [blame] | 79 | if (seed && (seed_len > 0)) { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 80 | bootutil_sha256_update(&sha256_ctx, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 83 | #ifdef MCUBOOT_ENC_IMAGES |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 84 | /* Encrypted images only exist in the secondary slot */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 85 | if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) && |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 86 | IS_ENCRYPTED(hdr) && !boot_enc_valid(enc_state, image_index, fap)) { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | #endif |
| 90 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 91 | /* Hash is computed over image header and image itself. */ |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 92 | hdr_size = hdr->ih_hdr_size; |
Fabio Utzig | c962135 | 2019-08-08 12:15:51 -0300 | [diff] [blame^] | 93 | size = BOOT_TLV_OFF(hdr); |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 94 | |
| 95 | #if (MCUBOOT_IMAGE_NUMBER > 1) |
| 96 | /* If dependency TLVs are present then the TLV info header and the |
| 97 | * dependency TLVs are also protected and have to be included in the hash |
| 98 | * calculation. |
| 99 | */ |
| 100 | if (hdr->ih_protect_tlv_size != 0) { |
| 101 | size += hdr->ih_protect_tlv_size; |
| 102 | } |
| 103 | #endif |
| 104 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 105 | for (off = 0; off < size; off += blk_sz) { |
| 106 | blk_sz = size - off; |
| 107 | if (blk_sz > tmp_buf_sz) { |
| 108 | blk_sz = tmp_buf_sz; |
| 109 | } |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 110 | #ifdef MCUBOOT_ENC_IMAGES |
| 111 | /* Avoid reading header data together with other image data |
| 112 | * because the header is not encrypted, so maintain correct |
| 113 | * bounds when sending encrypted data to decrypt routine. |
| 114 | */ |
| 115 | if ((off < hdr_size) && ((off + blk_sz) > hdr_size)) { |
| 116 | blk_sz = hdr_size - off; |
| 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 | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 124 | if (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) && |
| 125 | IS_ENCRYPTED(hdr) && off >= hdr_size) { |
Fabio Utzig | 2fc80df | 2018-12-14 06:47:38 -0200 | [diff] [blame] | 126 | blk_off = (off - hdr_size) & 0xf; |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 127 | boot_encrypt(enc_state, image_index, fap, off - hdr_size, blk_sz, |
| 128 | blk_off, tmp_buf); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 129 | } |
| 130 | #endif |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 131 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 132 | } |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 133 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 139 | * Currently, we only support being able to verify one type of |
| 140 | * signature, because there is a single verification function that we |
| 141 | * call. List the type of TLV we are expecting. If we aren't |
| 142 | * configured for any signature, don't define this macro. |
| 143 | */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 144 | #if (defined(MCUBOOT_SIGN_RSA) + \ |
| 145 | defined(MCUBOOT_SIGN_EC) + \ |
| 146 | defined(MCUBOOT_SIGN_EC256) + \ |
| 147 | defined(MCUBOOT_SIGN_ED25519)) > 1 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 148 | #error "Only a single signature type is supported!" |
| 149 | #endif |
| 150 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 151 | #if defined(MCUBOOT_SIGN_RSA) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 152 | # if MCUBOOT_SIGN_RSA_LEN == 2048 |
| 153 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA2048_PSS |
| 154 | # elif MCUBOOT_SIGN_RSA_LEN == 3072 |
| 155 | # define EXPECTED_SIG_TLV IMAGE_TLV_RSA3072_PSS |
| 156 | # else |
| 157 | # error "Unsupported RSA signature length" |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 158 | # endif |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 159 | # define SIG_BUF_SIZE (MCUBOOT_SIGN_RSA_LEN / 8) |
| 160 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) /* 2048 bits */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 161 | #elif defined(MCUBOOT_SIGN_EC) |
| 162 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA224 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 163 | # define SIG_BUF_SIZE 128 |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 164 | # define EXPECTED_SIG_LEN(x) ((x) >= 64) /* oids + 2 * 28 bytes */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 165 | #elif defined(MCUBOOT_SIGN_EC256) |
| 166 | # define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256 |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 167 | # define SIG_BUF_SIZE 128 |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 168 | # define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */ |
Fabio Utzig | 4876484 | 2019-05-10 19:28:24 -0300 | [diff] [blame] | 169 | #elif defined(MCUBOOT_SIGN_ED25519) |
| 170 | # define EXPECTED_SIG_TLV IMAGE_TLV_ED25519 |
| 171 | # define SIG_BUF_SIZE 64 |
| 172 | # define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE) |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 173 | #else |
| 174 | # define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 175 | #endif |
| 176 | |
| 177 | #ifdef EXPECTED_SIG_TLV |
| 178 | static int |
| 179 | bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) |
| 180 | { |
| 181 | bootutil_sha256_context sha256_ctx; |
| 182 | int i; |
| 183 | const struct bootutil_key *key; |
| 184 | uint8_t hash[32]; |
| 185 | |
Fabio Utzig | 9911b18 | 2017-09-05 20:29:42 -0300 | [diff] [blame] | 186 | assert(keyhash_len <= 32); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 187 | |
| 188 | for (i = 0; i < bootutil_key_cnt; i++) { |
| 189 | key = &bootutil_keys[i]; |
| 190 | bootutil_sha256_init(&sha256_ctx); |
| 191 | bootutil_sha256_update(&sha256_ctx, key->key, *key->len); |
| 192 | bootutil_sha256_finish(&sha256_ctx, hash); |
| 193 | if (!memcmp(hash, keyhash, keyhash_len)) { |
| 194 | return i; |
| 195 | } |
| 196 | } |
| 197 | return -1; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | /* |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 202 | * Verify the integrity of the image. |
| 203 | * Return non-zero if image could not be validated/does not validate. |
| 204 | */ |
| 205 | int |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 206 | bootutil_img_validate(struct enc_key_data *enc_state, int image_index, |
| 207 | struct image_header *hdr, const struct flash_area *fap, |
| 208 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, uint8_t *seed, |
| 209 | int seed_len, uint8_t *out_hash) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 210 | { |
| 211 | uint32_t off; |
David Brown | 3eaa2a1 | 2017-09-01 11:19:03 -0600 | [diff] [blame] | 212 | uint32_t end; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 213 | int sha256_valid = 0; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 214 | struct image_tlv_info info; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 215 | #ifdef EXPECTED_SIG_TLV |
| 216 | int valid_signature = 0; |
| 217 | int key_id = -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 218 | #endif |
| 219 | struct image_tlv tlv; |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 220 | uint8_t buf[SIG_BUF_SIZE]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 221 | uint8_t hash[32]; |
| 222 | int rc; |
| 223 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 224 | rc = bootutil_img_hash(enc_state, image_index, hdr, fap, tmp_buf, |
| 225 | tmp_buf_sz, hash, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 226 | if (rc) { |
| 227 | return rc; |
| 228 | } |
| 229 | |
| 230 | if (out_hash) { |
| 231 | memcpy(out_hash, hash, 32); |
| 232 | } |
| 233 | |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 234 | /* The TLVs come after the image. */ |
Fabio Utzig | c962135 | 2019-08-08 12:15:51 -0300 | [diff] [blame^] | 235 | off = BOOT_TLV_OFF(hdr); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 236 | |
| 237 | rc = flash_area_read(fap, off, &info, sizeof(info)); |
| 238 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 239 | return rc; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 240 | } |
| 241 | if (info.it_magic != IMAGE_TLV_INFO_MAGIC) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 242 | return -1; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 243 | } |
David Brown | 3eaa2a1 | 2017-09-01 11:19:03 -0600 | [diff] [blame] | 244 | end = off + info.it_tlv_tot; |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 245 | off += sizeof(info); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 246 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 247 | /* |
| 248 | * Traverse through all of the TLVs, performing any checks we know |
| 249 | * and are able to do. |
| 250 | */ |
David Brown | 3eaa2a1 | 2017-09-01 11:19:03 -0600 | [diff] [blame] | 251 | for (; off < end; off += sizeof(tlv) + tlv.it_len) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 252 | rc = flash_area_read(fap, off, &tlv, sizeof tlv); |
| 253 | if (rc) { |
| 254 | return rc; |
| 255 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 256 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 257 | if (tlv.it_type == IMAGE_TLV_SHA256) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 258 | /* |
| 259 | * Verify the SHA256 image hash. This must always be |
| 260 | * present. |
| 261 | */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 262 | if (tlv.it_len != sizeof(hash)) { |
| 263 | return -1; |
| 264 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 265 | rc = flash_area_read(fap, off + sizeof(tlv), buf, sizeof hash); |
| 266 | if (rc) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 267 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 268 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 269 | if (memcmp(hash, buf, sizeof(hash))) { |
Fabio Utzig | de6edc3 | 2017-09-04 14:35:49 -0300 | [diff] [blame] | 270 | return -1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 271 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 272 | |
| 273 | sha256_valid = 1; |
| 274 | #ifdef EXPECTED_SIG_TLV |
| 275 | } else if (tlv.it_type == IMAGE_TLV_KEYHASH) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 276 | /* |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 277 | * Determine which key we should be checking. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 278 | */ |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 279 | if (tlv.it_len > 32) { |
| 280 | return -1; |
| 281 | } |
| 282 | rc = flash_area_read(fap, off + sizeof tlv, buf, tlv.it_len); |
| 283 | if (rc) { |
| 284 | return rc; |
| 285 | } |
| 286 | key_id = bootutil_find_key(buf, tlv.it_len); |
| 287 | /* |
| 288 | * The key may not be found, which is acceptable. There |
| 289 | * can be multiple signatures, each preceded by a key. |
| 290 | */ |
| 291 | } else if (tlv.it_type == EXPECTED_SIG_TLV) { |
| 292 | /* Ignore this signature if it is out of bounds. */ |
| 293 | if (key_id < 0 || key_id >= bootutil_key_cnt) { |
| 294 | key_id = -1; |
| 295 | continue; |
| 296 | } |
| 297 | if (!EXPECTED_SIG_LEN(tlv.it_len) || tlv.it_len > sizeof(buf)) { |
| 298 | return -1; |
| 299 | } |
| 300 | rc = flash_area_read(fap, off + sizeof(tlv), buf, tlv.it_len); |
| 301 | if (rc) { |
| 302 | return -1; |
| 303 | } |
| 304 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, tlv.it_len, key_id); |
| 305 | if (rc == 0) { |
| 306 | valid_signature = 1; |
| 307 | } |
| 308 | key_id = -1; |
| 309 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 312 | |
| 313 | if (!sha256_valid) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 314 | return -1; |
| 315 | } |
| 316 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 317 | #ifdef EXPECTED_SIG_TLV |
| 318 | if (!valid_signature) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 319 | return -1; |
| 320 | } |
| 321 | #endif |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 322 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 323 | return 0; |
| 324 | } |