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