Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifdef USE_HOSTCC |
| 7 | #include "mkimage.h" |
| 8 | #include <time.h> |
| 9 | #else |
| 10 | #include <common.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 13 | #endif /* !USE_HOSTCC*/ |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 14 | #include <image.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 15 | #include <u-boot/rsa.h> |
| 16 | #include <u-boot/rsa-checksum.h> |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 17 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 18 | #define IMAGE_MAX_HASHED_NODES 100 |
| 19 | |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 20 | #ifdef USE_HOSTCC |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 21 | void *host_blob; |
| 22 | void image_set_host_blob(void *blob) |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 23 | { |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 24 | host_blob = blob; |
| 25 | } |
| 26 | void *image_get_host_blob(void) |
| 27 | { |
| 28 | return host_blob; |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 29 | } |
| 30 | #endif |
| 31 | |
| 32 | struct checksum_algo checksum_algos[] = { |
| 33 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 34 | .name = "sha1", |
| 35 | .checksum_len = SHA1_SUM_LEN, |
| 36 | .der_len = SHA1_DER_LEN, |
| 37 | .der_prefix = sha1_der_prefix, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 38 | #if IMAGE_ENABLE_SIGN |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 39 | .calculate_sign = EVP_sha1, |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 40 | #endif |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 41 | .calculate = hash_calculate, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 42 | }, |
| 43 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 44 | .name = "sha256", |
| 45 | .checksum_len = SHA256_SUM_LEN, |
| 46 | .der_len = SHA256_DER_LEN, |
| 47 | .der_prefix = sha256_der_prefix, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 48 | #if IMAGE_ENABLE_SIGN |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 49 | .calculate_sign = EVP_sha256, |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 50 | #endif |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 51 | .calculate = hash_calculate, |
Andrew Duda | 06ca6d6 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | }; |
| 55 | |
| 56 | struct crypto_algo crypto_algos[] = { |
| 57 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 58 | .name = "rsa2048", |
| 59 | .key_len = RSA2048_BYTES, |
| 60 | .sign = rsa_sign, |
| 61 | .add_verify_data = rsa_add_verify_data, |
| 62 | .verify = rsa_verify, |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 63 | }, |
| 64 | { |
Masahiro Yamada | 79f3c59 | 2017-10-23 10:03:40 +0900 | [diff] [blame] | 65 | .name = "rsa4096", |
| 66 | .key_len = RSA4096_BYTES, |
| 67 | .sign = rsa_sign, |
| 68 | .add_verify_data = rsa_add_verify_data, |
| 69 | .verify = rsa_verify, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 70 | } |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 71 | |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 72 | }; |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 73 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 74 | struct padding_algo padding_algos[] = { |
| 75 | { |
| 76 | .name = "pkcs-1.5", |
| 77 | .verify = padding_pkcs_15_verify, |
| 78 | }, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT |
| 80 | { |
| 81 | .name = "pss", |
| 82 | .verify = padding_pss_verify, |
| 83 | } |
| 84 | #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */ |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 87 | struct checksum_algo *image_get_checksum_algo(const char *full_name) |
| 88 | { |
| 89 | int i; |
| 90 | const char *name; |
| 91 | |
T Karthik Reddy | 0164dc8 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 92 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 93 | static bool done; |
| 94 | |
| 95 | if (!done) { |
| 96 | done = true; |
| 97 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 98 | checksum_algos[i].name += gd->reloc_off; |
| 99 | #if IMAGE_ENABLE_SIGN |
| 100 | checksum_algos[i].calculate_sign += gd->reloc_off; |
| 101 | #endif |
| 102 | checksum_algos[i].calculate += gd->reloc_off; |
| 103 | } |
| 104 | } |
| 105 | #endif |
| 106 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 107 | for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { |
| 108 | name = checksum_algos[i].name; |
| 109 | /* Make sure names match and next char is a comma */ |
| 110 | if (!strncmp(name, full_name, strlen(name)) && |
| 111 | full_name[strlen(name)] == ',') |
| 112 | return &checksum_algos[i]; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 113 | } |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 114 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 117 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 118 | struct crypto_algo *image_get_crypto_algo(const char *full_name) |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 119 | { |
| 120 | int i; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 121 | const char *name; |
| 122 | |
T Karthik Reddy | 0164dc8 | 2019-03-16 15:23:03 +0530 | [diff] [blame] | 123 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 124 | static bool done; |
| 125 | |
| 126 | if (!done) { |
| 127 | done = true; |
| 128 | for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { |
| 129 | crypto_algos[i].name += gd->reloc_off; |
| 130 | crypto_algos[i].sign += gd->reloc_off; |
| 131 | crypto_algos[i].add_verify_data += gd->reloc_off; |
| 132 | crypto_algos[i].verify += gd->reloc_off; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 137 | /* Move name to after the comma */ |
| 138 | name = strchr(full_name, ','); |
| 139 | if (!name) |
| 140 | return NULL; |
| 141 | name += 1; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 142 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 143 | for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { |
| 144 | if (!strcmp(crypto_algos[i].name, name)) |
| 145 | return &crypto_algos[i]; |
Simon Glass | 58fe7e5 | 2013-06-13 15:10:00 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return NULL; |
| 149 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 150 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 151 | struct padding_algo *image_get_padding_algo(const char *name) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | if (!name) |
| 156 | return NULL; |
| 157 | |
| 158 | for (i = 0; i < ARRAY_SIZE(padding_algos); i++) { |
| 159 | if (!strcmp(padding_algos[i].name, name)) |
| 160 | return &padding_algos[i]; |
| 161 | } |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 166 | /** |
| 167 | * fit_region_make_list() - Make a list of image regions |
| 168 | * |
| 169 | * Given a list of fdt_regions, create a list of image_regions. This is a |
| 170 | * simple conversion routine since the FDT and image code use different |
| 171 | * structures. |
| 172 | * |
| 173 | * @fit: FIT image |
| 174 | * @fdt_regions: Pointer to FDT regions |
| 175 | * @count: Number of FDT regions |
| 176 | * @region: Pointer to image regions, which must hold @count records. If |
| 177 | * region is NULL, then (except for an SPL build) the array will be |
| 178 | * allocated. |
| 179 | * @return: Pointer to image regions |
| 180 | */ |
| 181 | struct image_region *fit_region_make_list(const void *fit, |
| 182 | struct fdt_region *fdt_regions, int count, |
| 183 | struct image_region *region) |
| 184 | { |
| 185 | int i; |
| 186 | |
| 187 | debug("Hash regions:\n"); |
| 188 | debug("%10s %10s\n", "Offset", "Size"); |
| 189 | |
| 190 | /* |
| 191 | * Use malloc() except in SPL (to save code size). In SPL the caller |
| 192 | * must allocate the array. |
| 193 | */ |
| 194 | #ifndef CONFIG_SPL_BUILD |
| 195 | if (!region) |
| 196 | region = calloc(sizeof(*region), count); |
| 197 | #endif |
| 198 | if (!region) |
| 199 | return NULL; |
| 200 | for (i = 0; i < count; i++) { |
| 201 | debug("%10x %10x\n", fdt_regions[i].offset, |
| 202 | fdt_regions[i].size); |
| 203 | region[i].data = fit + fdt_regions[i].offset; |
| 204 | region[i].size = fdt_regions[i].size; |
| 205 | } |
| 206 | |
| 207 | return region; |
| 208 | } |
| 209 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 210 | static int fit_image_setup_verify(struct image_sign_info *info, |
| 211 | const void *fit, int noffset, int required_keynode, |
| 212 | char **err_msgp) |
| 213 | { |
| 214 | char *algo_name; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 215 | const char *padding_name; |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 216 | |
Teddy Reed | e6a4783 | 2018-06-09 11:38:05 -0400 | [diff] [blame] | 217 | if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) { |
| 218 | *err_msgp = "Total size too large"; |
| 219 | return 1; |
| 220 | } |
| 221 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 222 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 223 | *err_msgp = "Can't get hash algo property"; |
| 224 | return -1; |
| 225 | } |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 226 | |
| 227 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 228 | if (!padding_name) |
| 229 | padding_name = RSA_DEFAULT_PADDING_NAME; |
| 230 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 231 | memset(info, '\0', sizeof(*info)); |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 232 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 233 | info->fit = (void *)fit; |
| 234 | info->node_offset = noffset; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 235 | info->name = algo_name; |
| 236 | info->checksum = image_get_checksum_algo(algo_name); |
| 237 | info->crypto = image_get_crypto_algo(algo_name); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 238 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 239 | info->fdt_blob = gd_fdt_blob(); |
| 240 | info->required_keynode = required_keynode; |
| 241 | printf("%s:%s", algo_name, info->keyname); |
| 242 | |
Patrick Doyle | 4f4e6a0 | 2019-06-14 13:43:38 -0400 | [diff] [blame] | 243 | if (!info->checksum || !info->crypto || !info->padding) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 244 | *err_msgp = "Unknown signature algorithm"; |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int fit_image_check_sig(const void *fit, int noffset, const void *data, |
| 252 | size_t size, int required_keynode, char **err_msgp) |
| 253 | { |
| 254 | struct image_sign_info info; |
| 255 | struct image_region region; |
| 256 | uint8_t *fit_value; |
| 257 | int fit_value_len; |
| 258 | |
| 259 | *err_msgp = NULL; |
| 260 | if (fit_image_setup_verify(&info, fit, noffset, required_keynode, |
| 261 | err_msgp)) |
| 262 | return -1; |
| 263 | |
| 264 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 265 | &fit_value_len)) { |
| 266 | *err_msgp = "Can't get hash value property"; |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | region.data = data; |
| 271 | region.size = size; |
| 272 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 273 | if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 274 | *err_msgp = "Verification failed"; |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int fit_image_verify_sig(const void *fit, int image_noffset, |
| 282 | const char *data, size_t size, const void *sig_blob, |
| 283 | int sig_offset) |
| 284 | { |
| 285 | int noffset; |
| 286 | char *err_msg = ""; |
| 287 | int verified = 0; |
| 288 | int ret; |
| 289 | |
| 290 | /* Process all hash subnodes of the component image node */ |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 291 | fdt_for_each_subnode(noffset, fit, image_noffset) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 292 | const char *name = fit_get_name(fit, noffset, NULL); |
| 293 | |
| 294 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 295 | strlen(FIT_SIG_NODENAME))) { |
| 296 | ret = fit_image_check_sig(fit, noffset, data, |
| 297 | size, -1, &err_msg); |
| 298 | if (ret) { |
| 299 | puts("- "); |
| 300 | } else { |
| 301 | puts("+ "); |
| 302 | verified = 1; |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 309 | err_msg = "Corrupted or truncated tree"; |
| 310 | goto error; |
| 311 | } |
| 312 | |
| 313 | return verified ? 0 : -EPERM; |
| 314 | |
| 315 | error: |
| 316 | printf(" error!\n%s for '%s' hash node in '%s' image node\n", |
| 317 | err_msg, fit_get_name(fit, noffset, NULL), |
| 318 | fit_get_name(fit, image_noffset, NULL)); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | int fit_image_verify_required_sigs(const void *fit, int image_noffset, |
| 323 | const char *data, size_t size, const void *sig_blob, |
| 324 | int *no_sigsp) |
| 325 | { |
| 326 | int verify_count = 0; |
| 327 | int noffset; |
| 328 | int sig_node; |
| 329 | |
| 330 | /* Work out what we need to verify */ |
| 331 | *no_sigsp = 1; |
| 332 | sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); |
| 333 | if (sig_node < 0) { |
| 334 | debug("%s: No signature node found: %s\n", __func__, |
| 335 | fdt_strerror(sig_node)); |
| 336 | return 0; |
| 337 | } |
| 338 | |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 339 | fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 340 | const char *required; |
| 341 | int ret; |
| 342 | |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 343 | required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, |
| 344 | NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 345 | if (!required || strcmp(required, "image")) |
| 346 | continue; |
| 347 | ret = fit_image_verify_sig(fit, image_noffset, data, size, |
| 348 | sig_blob, noffset); |
| 349 | if (ret) { |
| 350 | printf("Failed to verify required signature '%s'\n", |
| 351 | fit_get_name(sig_blob, noffset, NULL)); |
| 352 | return ret; |
| 353 | } |
| 354 | verify_count++; |
| 355 | } |
| 356 | |
| 357 | if (verify_count) |
| 358 | *no_sigsp = 0; |
| 359 | |
| 360 | return 0; |
| 361 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 362 | |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 363 | /** |
| 364 | * fit_config_check_sig() - Check the signature of a config |
| 365 | * |
| 366 | * @fit: FIT to check |
| 367 | * @noffset: Offset of configuration node (e.g. /configurations/conf-1) |
| 368 | * @required_keynode: Offset in the control FDT of the required key node, |
| 369 | * if any. If this is given, then the configuration wil not |
| 370 | * pass verification unless that key is used. If this is |
| 371 | * -1 then any signature will do. |
| 372 | * @conf_noffset: Offset of the configuration subnode being checked (e.g. |
| 373 | * /configurations/conf-1/kernel) |
| 374 | * @err_msgp: In the event of an error, this will be pointed to a |
| 375 | * help error string to display to the user. |
| 376 | * @return 0 if all verified ok, <0 on error |
| 377 | */ |
| 378 | static int fit_config_check_sig(const void *fit, int noffset, |
| 379 | int required_keynode, int conf_noffset, |
| 380 | char **err_msgp) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 381 | { |
| 382 | char * const exc_prop[] = {"data"}; |
| 383 | const char *prop, *end, *name; |
| 384 | struct image_sign_info info; |
| 385 | const uint32_t *strings; |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 386 | const char *config_name; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 387 | uint8_t *fit_value; |
| 388 | int fit_value_len; |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 389 | bool found_config; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 390 | int max_regions; |
| 391 | int i, prop_len; |
| 392 | char path[200]; |
| 393 | int count; |
| 394 | |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 395 | config_name = fit_get_name(fit, conf_noffset, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 396 | debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(), |
| 397 | fit_get_name(fit, noffset, NULL), |
| 398 | fit_get_name(gd_fdt_blob(), required_keynode, NULL)); |
| 399 | *err_msgp = NULL; |
| 400 | if (fit_image_setup_verify(&info, fit, noffset, required_keynode, |
| 401 | err_msgp)) |
| 402 | return -1; |
| 403 | |
| 404 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 405 | &fit_value_len)) { |
| 406 | *err_msgp = "Can't get hash value property"; |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | /* Count the number of strings in the property */ |
| 411 | prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len); |
| 412 | end = prop ? prop + prop_len : prop; |
| 413 | for (name = prop, count = 0; name < end; name++) |
| 414 | if (!*name) |
| 415 | count++; |
| 416 | if (!count) { |
| 417 | *err_msgp = "Can't get hashed-nodes property"; |
| 418 | return -1; |
| 419 | } |
| 420 | |
Konrad Beckmann | 198f39a | 2018-11-07 14:51:45 -0500 | [diff] [blame] | 421 | if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') { |
| 422 | *err_msgp = "hashed-nodes property must be null-terminated"; |
| 423 | return -1; |
| 424 | } |
| 425 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 426 | /* Add a sanity check here since we are using the stack */ |
| 427 | if (count > IMAGE_MAX_HASHED_NODES) { |
| 428 | *err_msgp = "Number of hashed nodes exceeds maximum"; |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | /* Create a list of node names from those strings */ |
| 433 | char *node_inc[count]; |
| 434 | |
| 435 | debug("Hash nodes (%d):\n", count); |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 436 | found_config = false; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 437 | for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) { |
| 438 | debug(" '%s'\n", name); |
| 439 | node_inc[i] = (char *)name; |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 440 | if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) && |
| 441 | name[sizeof(FIT_CONFS_PATH) - 1] == '/' && |
| 442 | !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) { |
| 443 | debug(" (found config node %s)", config_name); |
| 444 | found_config = true; |
| 445 | } |
| 446 | } |
| 447 | if (!found_config) { |
| 448 | *err_msgp = "Selected config not in hashed nodes"; |
| 449 | return -1; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Each node can generate one region for each sub-node. Allow for |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 454 | * 7 sub-nodes (hash-1, signature-1, etc.) and some extra. |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 455 | */ |
| 456 | max_regions = 20 + count * 7; |
| 457 | struct fdt_region fdt_regions[max_regions]; |
| 458 | |
| 459 | /* Get a list of regions to hash */ |
| 460 | count = fdt_find_regions(fit, node_inc, count, |
| 461 | exc_prop, ARRAY_SIZE(exc_prop), |
| 462 | fdt_regions, max_regions - 1, |
| 463 | path, sizeof(path), 0); |
| 464 | if (count < 0) { |
| 465 | *err_msgp = "Failed to hash configuration"; |
| 466 | return -1; |
| 467 | } |
| 468 | if (count == 0) { |
| 469 | *err_msgp = "No data to hash"; |
| 470 | return -1; |
| 471 | } |
| 472 | if (count >= max_regions - 1) { |
| 473 | *err_msgp = "Too many hash regions"; |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | /* Add the strings */ |
| 478 | strings = fdt_getprop(fit, noffset, "hashed-strings", NULL); |
| 479 | if (strings) { |
Teddy Reed | 9767bf4 | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 480 | /* |
| 481 | * The strings region offset must be a static 0x0. |
| 482 | * This is set in tool/image-host.c |
| 483 | */ |
| 484 | fdt_regions[count].offset = fdt_off_dt_strings(fit); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 485 | fdt_regions[count].size = fdt32_to_cpu(strings[1]); |
| 486 | count++; |
| 487 | } |
| 488 | |
| 489 | /* Allocate the region list on the stack */ |
| 490 | struct image_region region[count]; |
| 491 | |
| 492 | fit_region_make_list(fit, fdt_regions, count, region); |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 493 | if (info.crypto->verify(&info, region, count, fit_value, |
| 494 | fit_value_len)) { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 495 | *err_msgp = "Verification failed"; |
| 496 | return -1; |
| 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int fit_config_verify_sig(const void *fit, int conf_noffset, |
| 503 | const void *sig_blob, int sig_offset) |
| 504 | { |
| 505 | int noffset; |
| 506 | char *err_msg = ""; |
| 507 | int verified = 0; |
| 508 | int ret; |
| 509 | |
| 510 | /* Process all hash subnodes of the component conf node */ |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 511 | fdt_for_each_subnode(noffset, fit, conf_noffset) { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 512 | const char *name = fit_get_name(fit, noffset, NULL); |
| 513 | |
| 514 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 515 | strlen(FIT_SIG_NODENAME))) { |
| 516 | ret = fit_config_check_sig(fit, noffset, sig_offset, |
Simon Glass | 086ae65 | 2020-03-18 11:44:01 -0600 | [diff] [blame] | 517 | conf_noffset, &err_msg); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 518 | if (ret) { |
| 519 | puts("- "); |
| 520 | } else { |
| 521 | puts("+ "); |
| 522 | verified = 1; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 529 | err_msg = "Corrupted or truncated tree"; |
| 530 | goto error; |
| 531 | } |
| 532 | |
Simon Glass | ff35f0c | 2020-03-18 11:43:57 -0600 | [diff] [blame] | 533 | if (verified) |
| 534 | return 0; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 535 | |
| 536 | error: |
| 537 | printf(" error!\n%s for '%s' hash node in '%s' config node\n", |
| 538 | err_msg, fit_get_name(fit, noffset, NULL), |
| 539 | fit_get_name(fit, conf_noffset, NULL)); |
Simon Glass | ff35f0c | 2020-03-18 11:43:57 -0600 | [diff] [blame] | 540 | return -EPERM; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | int fit_config_verify_required_sigs(const void *fit, int conf_noffset, |
| 544 | const void *sig_blob) |
| 545 | { |
| 546 | int noffset; |
| 547 | int sig_node; |
| 548 | |
| 549 | /* Work out what we need to verify */ |
| 550 | sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); |
| 551 | if (sig_node < 0) { |
| 552 | debug("%s: No signature node found: %s\n", __func__, |
| 553 | fdt_strerror(sig_node)); |
| 554 | return 0; |
| 555 | } |
| 556 | |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 557 | fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 558 | const char *required; |
| 559 | int ret; |
| 560 | |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 561 | required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, |
| 562 | NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 563 | if (!required || strcmp(required, "conf")) |
| 564 | continue; |
| 565 | ret = fit_config_verify_sig(fit, conf_noffset, sig_blob, |
| 566 | noffset); |
| 567 | if (ret) { |
| 568 | printf("Failed to verify required signature '%s'\n", |
| 569 | fit_get_name(sig_blob, noffset, NULL)); |
| 570 | return ret; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | int fit_config_verify(const void *fit, int conf_noffset) |
| 578 | { |
Simon Glass | 5da42d9 | 2014-06-12 07:24:45 -0600 | [diff] [blame] | 579 | return fit_config_verify_required_sigs(fit, conf_noffset, |
| 580 | gd_fdt_blob()); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 581 | } |