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