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 | eed80b6 | 2017-06-10 08:03:05 -0300 | [diff] [blame] | 31 | #ifdef APP_mynewt |
| 32 | #include "mynewt/config.h" |
| 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 | /* |
| 91 | * Verify the integrity of the image. |
| 92 | * Return non-zero if image could not be validated/does not validate. |
| 93 | */ |
| 94 | int |
| 95 | bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap, |
| 96 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 97 | uint8_t *seed, int seed_len, uint8_t *out_hash) |
| 98 | { |
| 99 | uint32_t off; |
| 100 | uint32_t size; |
| 101 | uint32_t sha_off = 0; |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 102 | #if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \ |
| 103 | defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 104 | uint32_t sig_off = 0; |
| 105 | uint32_t sig_len = 0; |
| 106 | #endif |
| 107 | struct image_tlv tlv; |
| 108 | uint8_t buf[256]; |
| 109 | uint8_t hash[32]; |
| 110 | int rc; |
| 111 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 112 | #ifdef MCUBOOT_SIGN_RSA |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 113 | #ifdef MCUBOOT_RSA_PKCS1_15 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 114 | if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) { |
| 115 | return -1; |
| 116 | } |
David Brown | f4e904d | 2017-05-31 13:21:39 -0600 | [diff] [blame] | 117 | #else |
| 118 | if ((hdr->ih_flags & IMAGE_F_PKCS1_PSS_RSA2048_SHA256) == 0) { |
| 119 | return -1; |
| 120 | } |
| 121 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 122 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 123 | #ifdef MCUBOOT_SIGN_EC |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 124 | if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) { |
| 125 | return -1; |
| 126 | } |
| 127 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 128 | #ifdef MCUBOOT_SIGN_EC256 |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 129 | if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) { |
| 130 | return -1; |
| 131 | } |
| 132 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 133 | if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) { |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash, |
| 138 | seed, seed_len); |
| 139 | if (rc) { |
| 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | if (out_hash) { |
| 144 | memcpy(out_hash, hash, 32); |
| 145 | } |
| 146 | |
| 147 | /* After image there are TLVs. */ |
| 148 | off = hdr->ih_img_size + hdr->ih_hdr_size; |
| 149 | size = off + hdr->ih_tlv_size; |
| 150 | |
| 151 | for (; off < size; off += sizeof(tlv) + tlv.it_len) { |
| 152 | rc = flash_area_read(fap, off, &tlv, sizeof tlv); |
| 153 | if (rc) { |
| 154 | return rc; |
| 155 | } |
| 156 | if (tlv.it_type == IMAGE_TLV_SHA256) { |
| 157 | if (tlv.it_len != sizeof(hash)) { |
| 158 | return -1; |
| 159 | } |
| 160 | sha_off = off + sizeof(tlv); |
| 161 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 162 | #ifdef MCUBOOT_SIGN_RSA |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 163 | if (tlv.it_type == IMAGE_TLV_RSA2048) { |
| 164 | if (tlv.it_len != 256) { /* 2048 bits */ |
| 165 | return -1; |
| 166 | } |
| 167 | sig_off = off + sizeof(tlv); |
| 168 | sig_len = tlv.it_len; |
| 169 | } |
| 170 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 171 | #ifdef MCUBOOT_SIGN_EC |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 172 | if (tlv.it_type == IMAGE_TLV_ECDSA224) { |
| 173 | if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */ |
| 174 | return -1; |
| 175 | } |
| 176 | sig_off = off + sizeof(tlv); |
| 177 | sig_len = tlv.it_len; |
| 178 | } |
| 179 | #endif |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 180 | #ifdef MCUBOOT_SIGN_EC256 |
Marko Kiiskila | bf94339 | 2016-12-29 17:29:48 -0800 | [diff] [blame] | 181 | if (tlv.it_type == IMAGE_TLV_ECDSA256) { |
| 182 | if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */ |
| 183 | return -1; |
| 184 | } |
| 185 | sig_off = off + sizeof(tlv); |
| 186 | sig_len = tlv.it_len; |
| 187 | } |
| 188 | #endif |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 189 | } |
| 190 | if (hdr->ih_flags & IMAGE_F_SHA256) { |
| 191 | if (!sha_off) { |
| 192 | /* |
| 193 | * Header said there should be hash TLV, no TLV found. |
| 194 | */ |
| 195 | return -1; |
| 196 | } |
| 197 | rc = flash_area_read(fap, sha_off, buf, sizeof hash); |
| 198 | if (rc) { |
| 199 | return rc; |
| 200 | } |
| 201 | if (memcmp(hash, buf, sizeof(hash))) { |
| 202 | return -1; |
| 203 | } |
| 204 | } |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 205 | #if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \ |
| 206 | defined(MCUBOOT_SIGN_EC256) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 207 | if (!sig_off) { |
| 208 | /* |
| 209 | * Header said there should be PKCS1.v5 signature, no TLV |
| 210 | * found. |
| 211 | */ |
| 212 | return -1; |
| 213 | } |
| 214 | rc = flash_area_read(fap, sig_off, buf, sig_len); |
| 215 | if (rc) { |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | if (hdr->ih_key_id >= bootutil_key_cnt) { |
| 220 | return -1; |
| 221 | } |
| 222 | rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id); |
| 223 | if (rc) { |
| 224 | return -1; |
| 225 | } |
| 226 | #endif |
| 227 | return 0; |
| 228 | } |