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