AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
| 4 | */ |
| 5 | |
| 6 | #ifdef USE_HOSTCC |
| 7 | #include "mkimage.h" |
| 8 | #include <time.h> |
| 9 | #else |
| 10 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | #endif /* !USE_HOSTCC*/ |
Masahiro Yamada | 6dd1052 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 15 | #include <fdt_region.h> |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 16 | #include <image.h> |
| 17 | #include <u-boot/rsa.h> |
| 18 | #include <u-boot/rsa-checksum.h> |
| 19 | |
| 20 | #define IMAGE_MAX_HASHED_NODES 100 |
| 21 | |
| 22 | #ifdef USE_HOSTCC |
| 23 | void *host_blob; |
| 24 | |
| 25 | void image_set_host_blob(void *blob) |
| 26 | { |
| 27 | host_blob = blob; |
| 28 | } |
| 29 | |
| 30 | void *image_get_host_blob(void) |
| 31 | { |
| 32 | return host_blob; |
| 33 | } |
| 34 | #endif |
| 35 | |
| 36 | /** |
| 37 | * fit_region_make_list() - Make a list of image regions |
| 38 | * |
| 39 | * Given a list of fdt_regions, create a list of image_regions. This is a |
| 40 | * simple conversion routine since the FDT and image code use different |
| 41 | * structures. |
| 42 | * |
| 43 | * @fit: FIT image |
| 44 | * @fdt_regions: Pointer to FDT regions |
| 45 | * @count: Number of FDT regions |
| 46 | * @region: Pointer to image regions, which must hold @count records. If |
| 47 | * region is NULL, then (except for an SPL build) the array will be |
| 48 | * allocated. |
| 49 | * @return: Pointer to image regions |
| 50 | */ |
| 51 | struct image_region *fit_region_make_list(const void *fit, |
| 52 | struct fdt_region *fdt_regions, |
| 53 | int count, |
| 54 | struct image_region *region) |
| 55 | { |
| 56 | int i; |
| 57 | |
| 58 | debug("Hash regions:\n"); |
| 59 | debug("%10s %10s\n", "Offset", "Size"); |
| 60 | |
| 61 | /* |
| 62 | * Use malloc() except in SPL (to save code size). In SPL the caller |
| 63 | * must allocate the array. |
| 64 | */ |
| 65 | #ifndef CONFIG_SPL_BUILD |
| 66 | if (!region) |
| 67 | region = calloc(sizeof(*region), count); |
| 68 | #endif |
| 69 | if (!region) |
| 70 | return NULL; |
| 71 | for (i = 0; i < count; i++) { |
| 72 | debug("%10x %10x\n", fdt_regions[i].offset, |
| 73 | fdt_regions[i].size); |
| 74 | region[i].data = fit + fdt_regions[i].offset; |
| 75 | region[i].size = fdt_regions[i].size; |
| 76 | } |
| 77 | |
| 78 | return region; |
| 79 | } |
| 80 | |
| 81 | static int fit_image_setup_verify(struct image_sign_info *info, |
| 82 | const void *fit, int noffset, |
| 83 | int required_keynode, char **err_msgp) |
| 84 | { |
| 85 | char *algo_name; |
| 86 | const char *padding_name; |
| 87 | |
| 88 | if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) { |
| 89 | *err_msgp = "Total size too large"; |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
| 94 | *err_msgp = "Can't get hash algo property"; |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 99 | if (!padding_name) |
| 100 | padding_name = RSA_DEFAULT_PADDING_NAME; |
| 101 | |
| 102 | memset(info, '\0', sizeof(*info)); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 103 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 104 | info->fit = (void *)fit; |
| 105 | info->node_offset = noffset; |
| 106 | info->name = algo_name; |
| 107 | info->checksum = image_get_checksum_algo(algo_name); |
| 108 | info->crypto = image_get_crypto_algo(algo_name); |
| 109 | info->padding = image_get_padding_algo(padding_name); |
| 110 | info->fdt_blob = gd_fdt_blob(); |
| 111 | info->required_keynode = required_keynode; |
| 112 | printf("%s:%s", algo_name, info->keyname); |
| 113 | |
| 114 | if (!info->checksum || !info->crypto || !info->padding) { |
| 115 | *err_msgp = "Unknown signature algorithm"; |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | int fit_image_check_sig(const void *fit, int noffset, const void *data, |
| 123 | size_t size, int required_keynode, char **err_msgp) |
| 124 | { |
| 125 | struct image_sign_info info; |
| 126 | struct image_region region; |
| 127 | uint8_t *fit_value; |
| 128 | int fit_value_len; |
| 129 | |
| 130 | *err_msgp = NULL; |
| 131 | if (fit_image_setup_verify(&info, fit, noffset, required_keynode, |
| 132 | err_msgp)) |
| 133 | return -1; |
| 134 | |
| 135 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 136 | &fit_value_len)) { |
| 137 | *err_msgp = "Can't get hash value property"; |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | region.data = data; |
| 142 | region.size = size; |
| 143 | |
| 144 | if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) { |
| 145 | *err_msgp = "Verification failed"; |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int fit_image_verify_sig(const void *fit, int image_noffset, |
| 153 | const char *data, size_t size, |
| 154 | const void *sig_blob, int sig_offset) |
| 155 | { |
| 156 | int noffset; |
| 157 | char *err_msg = ""; |
| 158 | int verified = 0; |
| 159 | int ret; |
| 160 | |
| 161 | /* Process all hash subnodes of the component image node */ |
| 162 | fdt_for_each_subnode(noffset, fit, image_noffset) { |
| 163 | const char *name = fit_get_name(fit, noffset, NULL); |
| 164 | |
| 165 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 166 | strlen(FIT_SIG_NODENAME))) { |
| 167 | ret = fit_image_check_sig(fit, noffset, data, |
| 168 | size, -1, &err_msg); |
| 169 | if (ret) { |
| 170 | puts("- "); |
| 171 | } else { |
| 172 | puts("+ "); |
| 173 | verified = 1; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 180 | err_msg = "Corrupted or truncated tree"; |
| 181 | goto error; |
| 182 | } |
| 183 | |
| 184 | return verified ? 0 : -EPERM; |
| 185 | |
| 186 | error: |
| 187 | printf(" error!\n%s for '%s' hash node in '%s' image node\n", |
| 188 | err_msg, fit_get_name(fit, noffset, NULL), |
| 189 | fit_get_name(fit, image_noffset, NULL)); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | int fit_image_verify_required_sigs(const void *fit, int image_noffset, |
| 194 | const char *data, size_t size, |
| 195 | const void *sig_blob, int *no_sigsp) |
| 196 | { |
| 197 | int verify_count = 0; |
| 198 | int noffset; |
| 199 | int sig_node; |
| 200 | |
| 201 | /* Work out what we need to verify */ |
| 202 | *no_sigsp = 1; |
| 203 | sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); |
| 204 | if (sig_node < 0) { |
| 205 | debug("%s: No signature node found: %s\n", __func__, |
| 206 | fdt_strerror(sig_node)); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
| 211 | const char *required; |
| 212 | int ret; |
| 213 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 214 | required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, |
| 215 | NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 216 | if (!required || strcmp(required, "image")) |
| 217 | continue; |
| 218 | ret = fit_image_verify_sig(fit, image_noffset, data, size, |
| 219 | sig_blob, noffset); |
| 220 | if (ret) { |
| 221 | printf("Failed to verify required signature '%s'\n", |
| 222 | fit_get_name(sig_blob, noffset, NULL)); |
| 223 | return ret; |
| 224 | } |
| 225 | verify_count++; |
| 226 | } |
| 227 | |
| 228 | if (verify_count) |
| 229 | *no_sigsp = 0; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 234 | /** |
| 235 | * fit_config_check_sig() - Check the signature of a config |
| 236 | * |
| 237 | * @fit: FIT to check |
| 238 | * @noffset: Offset of configuration node (e.g. /configurations/conf-1) |
| 239 | * @required_keynode: Offset in the control FDT of the required key node, |
| 240 | * if any. If this is given, then the configuration wil not |
| 241 | * pass verification unless that key is used. If this is |
| 242 | * -1 then any signature will do. |
| 243 | * @conf_noffset: Offset of the configuration subnode being checked (e.g. |
| 244 | * /configurations/conf-1/kernel) |
| 245 | * @err_msgp: In the event of an error, this will be pointed to a |
| 246 | * help error string to display to the user. |
| 247 | * @return 0 if all verified ok, <0 on error |
| 248 | */ |
| 249 | static int fit_config_check_sig(const void *fit, int noffset, |
| 250 | int required_keynode, int conf_noffset, |
| 251 | char **err_msgp) |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 252 | { |
Philippe Reynes | 093f01c | 2020-04-29 15:26:17 +0200 | [diff] [blame] | 253 | char * const exc_prop[] = {"data", "data-size", "data-position"}; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 254 | const char *prop, *end, *name; |
| 255 | struct image_sign_info info; |
| 256 | const uint32_t *strings; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 257 | const char *config_name; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 258 | uint8_t *fit_value; |
| 259 | int fit_value_len; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 260 | bool found_config; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 261 | int max_regions; |
| 262 | int i, prop_len; |
| 263 | char path[200]; |
| 264 | int count; |
| 265 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 266 | config_name = fit_get_name(fit, conf_noffset, NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 267 | debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(), |
| 268 | fit_get_name(fit, noffset, NULL), |
| 269 | fit_get_name(gd_fdt_blob(), required_keynode, NULL)); |
| 270 | *err_msgp = NULL; |
| 271 | if (fit_image_setup_verify(&info, fit, noffset, required_keynode, |
| 272 | err_msgp)) |
| 273 | return -1; |
| 274 | |
| 275 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 276 | &fit_value_len)) { |
| 277 | *err_msgp = "Can't get hash value property"; |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | /* Count the number of strings in the property */ |
| 282 | prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len); |
| 283 | end = prop ? prop + prop_len : prop; |
| 284 | for (name = prop, count = 0; name < end; name++) |
| 285 | if (!*name) |
| 286 | count++; |
| 287 | if (!count) { |
| 288 | *err_msgp = "Can't get hashed-nodes property"; |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') { |
| 293 | *err_msgp = "hashed-nodes property must be null-terminated"; |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | /* Add a sanity check here since we are using the stack */ |
| 298 | if (count > IMAGE_MAX_HASHED_NODES) { |
| 299 | *err_msgp = "Number of hashed nodes exceeds maximum"; |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | /* Create a list of node names from those strings */ |
| 304 | char *node_inc[count]; |
| 305 | |
| 306 | debug("Hash nodes (%d):\n", count); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 307 | found_config = false; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 308 | for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) { |
| 309 | debug(" '%s'\n", name); |
| 310 | node_inc[i] = (char *)name; |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 311 | if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) && |
| 312 | name[sizeof(FIT_CONFS_PATH) - 1] == '/' && |
| 313 | !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) { |
| 314 | debug(" (found config node %s)", config_name); |
| 315 | found_config = true; |
| 316 | } |
| 317 | } |
| 318 | if (!found_config) { |
| 319 | *err_msgp = "Selected config not in hashed nodes"; |
| 320 | return -1; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Each node can generate one region for each sub-node. Allow for |
| 325 | * 7 sub-nodes (hash-1, signature-1, etc.) and some extra. |
| 326 | */ |
| 327 | max_regions = 20 + count * 7; |
| 328 | struct fdt_region fdt_regions[max_regions]; |
| 329 | |
| 330 | /* Get a list of regions to hash */ |
| 331 | count = fdt_find_regions(fit, node_inc, count, |
| 332 | exc_prop, ARRAY_SIZE(exc_prop), |
| 333 | fdt_regions, max_regions - 1, |
| 334 | path, sizeof(path), 0); |
| 335 | if (count < 0) { |
| 336 | *err_msgp = "Failed to hash configuration"; |
| 337 | return -1; |
| 338 | } |
| 339 | if (count == 0) { |
| 340 | *err_msgp = "No data to hash"; |
| 341 | return -1; |
| 342 | } |
| 343 | if (count >= max_regions - 1) { |
| 344 | *err_msgp = "Too many hash regions"; |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | /* Add the strings */ |
| 349 | strings = fdt_getprop(fit, noffset, "hashed-strings", NULL); |
| 350 | if (strings) { |
| 351 | /* |
| 352 | * The strings region offset must be a static 0x0. |
| 353 | * This is set in tool/image-host.c |
| 354 | */ |
| 355 | fdt_regions[count].offset = fdt_off_dt_strings(fit); |
| 356 | fdt_regions[count].size = fdt32_to_cpu(strings[1]); |
| 357 | count++; |
| 358 | } |
| 359 | |
| 360 | /* Allocate the region list on the stack */ |
| 361 | struct image_region region[count]; |
| 362 | |
| 363 | fit_region_make_list(fit, fdt_regions, count, region); |
| 364 | if (info.crypto->verify(&info, region, count, fit_value, |
| 365 | fit_value_len)) { |
| 366 | *err_msgp = "Verification failed"; |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int fit_config_verify_sig(const void *fit, int conf_noffset, |
| 374 | const void *sig_blob, int sig_offset) |
| 375 | { |
| 376 | int noffset; |
| 377 | char *err_msg = ""; |
| 378 | int verified = 0; |
| 379 | int ret; |
| 380 | |
| 381 | /* Process all hash subnodes of the component conf node */ |
| 382 | fdt_for_each_subnode(noffset, fit, conf_noffset) { |
| 383 | const char *name = fit_get_name(fit, noffset, NULL); |
| 384 | |
| 385 | if (!strncmp(name, FIT_SIG_NODENAME, |
| 386 | strlen(FIT_SIG_NODENAME))) { |
| 387 | ret = fit_config_check_sig(fit, noffset, sig_offset, |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 388 | conf_noffset, &err_msg); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 389 | if (ret) { |
| 390 | puts("- "); |
| 391 | } else { |
| 392 | puts("+ "); |
| 393 | verified = 1; |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 400 | err_msg = "Corrupted or truncated tree"; |
| 401 | goto error; |
| 402 | } |
| 403 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 404 | if (verified) |
| 405 | return 0; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 406 | |
| 407 | error: |
| 408 | printf(" error!\n%s for '%s' hash node in '%s' config node\n", |
| 409 | err_msg, fit_get_name(fit, noffset, NULL), |
| 410 | fit_get_name(fit, conf_noffset, NULL)); |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 411 | return -EPERM; |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | int fit_config_verify_required_sigs(const void *fit, int conf_noffset, |
| 415 | const void *sig_blob) |
| 416 | { |
| 417 | int noffset; |
| 418 | int sig_node; |
| 419 | |
| 420 | /* Work out what we need to verify */ |
| 421 | sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); |
| 422 | if (sig_node < 0) { |
| 423 | debug("%s: No signature node found: %s\n", __func__, |
| 424 | fdt_strerror(sig_node)); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
| 429 | const char *required; |
| 430 | int ret; |
| 431 | |
Tom Rini | 7357a35 | 2020-04-07 11:58:44 -0400 | [diff] [blame] | 432 | required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, |
| 433 | NULL); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 434 | if (!required || strcmp(required, "conf")) |
| 435 | continue; |
| 436 | ret = fit_config_verify_sig(fit, conf_noffset, sig_blob, |
| 437 | noffset); |
| 438 | if (ret) { |
| 439 | printf("Failed to verify required signature '%s'\n", |
| 440 | fit_get_name(sig_blob, noffset, NULL)); |
| 441 | return ret; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int fit_config_verify(const void *fit, int conf_noffset) |
| 449 | { |
| 450 | return fit_config_verify_required_sigs(fit, conf_noffset, |
| 451 | gd_fdt_blob()); |
| 452 | } |