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