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 | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 31 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include "mbedtls/rsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 33 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 34 | #if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 35 | #include "mbedtls/ecdsa.h" |
David Brown | d7e350d | 2017-04-24 13:29:28 -0600 | [diff] [blame] | 36 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 37 | #include "mbedtls/asn1.h" |
| 38 | |
| 39 | #include "bootutil_priv.h" |
| 40 | |
| 41 | /* |
| 42 | * Compute SHA256 over the image. |
| 43 | */ |
| 44 | static int |
| 45 | bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap, |
| 46 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 47 | uint8_t *hash_result, uint8_t *seed, int seed_len) |
| 48 | { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 49 | bootutil_sha256_context sha256_ctx; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | uint32_t blk_sz; |
| 51 | uint32_t size; |
| 52 | uint32_t off; |
| 53 | int rc; |
| 54 | |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 55 | bootutil_sha256_init(&sha256_ctx); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 56 | |
| 57 | /* in some cases (split image) the hash is seeded with data from |
| 58 | * the loader image */ |
| 59 | if(seed && (seed_len > 0)) { |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 60 | bootutil_sha256_update(&sha256_ctx, seed, seed_len); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | size = hdr->ih_img_size + hdr->ih_hdr_size; |
| 64 | |
| 65 | /* |
| 66 | * Hash is computed over image header and image itself. No TLV is |
| 67 | * included ATM. |
| 68 | */ |
| 69 | size = hdr->ih_img_size + hdr->ih_hdr_size; |
| 70 | for (off = 0; off < size; off += blk_sz) { |
| 71 | blk_sz = size - off; |
| 72 | if (blk_sz > tmp_buf_sz) { |
| 73 | blk_sz = tmp_buf_sz; |
| 74 | } |
| 75 | rc = flash_area_read(fap, off, tmp_buf, blk_sz); |
| 76 | if (rc) { |
| 77 | return rc; |
| 78 | } |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 79 | bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 80 | } |
David Brown | e629bf3 | 2017-04-24 13:37:33 -0600 | [diff] [blame] | 81 | bootutil_sha256_finish(&sha256_ctx, hash_result); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Verify the integrity of the image. |
| 88 | * Return non-zero if image could not be validated/does not validate. |
| 89 | */ |
| 90 | int |
| 91 | bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap, |
| 92 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 93 | uint8_t *seed, int seed_len, uint8_t *out_hash) |
| 94 | { |
| 95 | uint32_t off; |
| 96 | uint32_t size; |
| 97 | uint32_t sha_off = 0; |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 98 | #if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \ |
| 99 | defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 100 | uint32_t sig_off = 0; |
| 101 | uint32_t sig_len = 0; |
| 102 | #endif |
| 103 | struct image_tlv tlv; |
| 104 | uint8_t buf[256]; |
| 105 | uint8_t hash[32]; |
| 106 | int rc; |
| 107 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 108 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 109 | if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) { |
| 110 | return -1; |
| 111 | } |
| 112 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 113 | #ifdef MCUBOOT_SIGN_EC |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 114 | if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) { |
| 115 | return -1; |
| 116 | } |
| 117 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 118 | #ifdef MCUBOOT_SIGN_EC256 |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 119 | if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) { |
| 120 | return -1; |
| 121 | } |
| 122 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 123 | if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) { |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, |
| 128 | seed, seed_len); |
| 129 | if (rc) { |
| 130 | return rc; |
| 131 | } |
| 132 | |
| 133 | if (out_hash) { |
| 134 | memcpy(out_hash, hash, 32); |
| 135 | } |
| 136 | |
| 137 | /* After image there are TLVs. */ |
| 138 | off = hdr->ih_img_size + hdr->ih_hdr_size; |
| 139 | size = off + hdr->ih_tlv_size; |
| 140 | |
| 141 | for (; off < size; off += sizeof(tlv) + tlv.it_len) { |
| 142 | rc = flash_area_read(fap, off, &tlv, sizeof tlv); |
| 143 | if (rc) { |
| 144 | return rc; |
| 145 | } |
| 146 | if (tlv.it_type == IMAGE_TLV_SHA256) { |
| 147 | if (tlv.it_len != sizeof(hash)) { |
| 148 | return -1; |
| 149 | } |
| 150 | sha_off = off + sizeof(tlv); |
| 151 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 152 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 153 | if (tlv.it_type == IMAGE_TLV_RSA2048) { |
| 154 | if (tlv.it_len != 256) { /* 2048 bits */ |
| 155 | return -1; |
| 156 | } |
| 157 | sig_off = off + sizeof(tlv); |
| 158 | sig_len = tlv.it_len; |
| 159 | } |
| 160 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 161 | #ifdef MCUBOOT_SIGN_EC |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 162 | if (tlv.it_type == IMAGE_TLV_ECDSA224) { |
| 163 | if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */ |
| 164 | return -1; |
| 165 | } |
| 166 | sig_off = off + sizeof(tlv); |
| 167 | sig_len = tlv.it_len; |
| 168 | } |
| 169 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 170 | #ifdef MCUBOOT_SIGN_EC256 |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 171 | if (tlv.it_type == IMAGE_TLV_ECDSA256) { |
| 172 | if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */ |
| 173 | return -1; |
| 174 | } |
| 175 | sig_off = off + sizeof(tlv); |
| 176 | sig_len = tlv.it_len; |
| 177 | } |
| 178 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 179 | } |
| 180 | if (hdr->ih_flags & IMAGE_F_SHA256) { |
| 181 | if (!sha_off) { |
| 182 | /* |
| 183 | * Header said there should be hash TLV, no TLV found. |
| 184 | */ |
| 185 | return -1; |
| 186 | } |
| 187 | rc = flash_area_read(fap, sha_off, buf, sizeof hash); |
| 188 | if (rc) { |
| 189 | return rc; |
| 190 | } |
| 191 | if (memcmp(hash, buf, sizeof(hash))) { |
| 192 | return -1; |
| 193 | } |
| 194 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 195 | #if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \ |
| 196 | defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 197 | if (!sig_off) { |
| 198 | /* |
| 199 | * Header said there should be PKCS1.v5 signature, no TLV |
| 200 | * found. |
| 201 | */ |
| 202 | return -1; |
| 203 | } |
| 204 | rc = flash_area_read(fap, sig_off, buf, sig_len); |
| 205 | if (rc) { |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | if (hdr->ih_key_id >= bootutil_key_cnt) { |
| 210 | return -1; |
| 211 | } |
| 212 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id); |
| 213 | if (rc) { |
| 214 | return -1; |
| 215 | } |
| 216 | #endif |
| 217 | return 0; |
| 218 | } |