1 /* 2 * Copyright (c) 2013, Google Inc. 3 * 4 * (C) Copyright 2008 Semihalf 5 * 6 * (C) Copyright 2000-2006 7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #include "mkimage.h" 13 #include <bootm.h> 14 #include <image.h> 15 #include <version.h> 16 17 /** 18 * fit_set_hash_value - set hash value in requested has node 19 * @fit: pointer to the FIT format image header 20 * @noffset: hash node offset 21 * @value: hash value to be set 22 * @value_len: hash value length 23 * 24 * fit_set_hash_value() attempts to set hash value in a node at offset 25 * given and returns operation status to the caller. 26 * 27 * returns 28 * 0, on success 29 * -1, on failure 30 */ 31 static int fit_set_hash_value(void *fit, int noffset, uint8_t *value, 32 int value_len) 33 { 34 int ret; 35 36 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); 37 if (ret) { 38 printf("Can't set hash '%s' property for '%s' node(%s)\n", 39 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL), 40 fdt_strerror(ret)); 41 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; 42 } 43 44 return 0; 45 } 46 47 /** 48 * fit_image_process_hash - Process a single subnode of the images/ node 49 * 50 * Check each subnode and process accordingly. For hash nodes we generate 51 * a hash of the supplised data and store it in the node. 52 * 53 * @fit: pointer to the FIT format image header 54 * @image_name: name of image being processes (used to display errors) 55 * @noffset: subnode offset 56 * @data: data to process 57 * @size: size of data in bytes 58 * @return 0 if ok, -1 on error 59 */ 60 static int fit_image_process_hash(void *fit, const char *image_name, 61 int noffset, const void *data, size_t size) 62 { 63 uint8_t value[FIT_MAX_HASH_LEN]; 64 const char *node_name; 65 int value_len; 66 char *algo; 67 int ret; 68 69 node_name = fit_get_name(fit, noffset, NULL); 70 71 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 72 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n", 73 node_name, image_name); 74 return -ENOENT; 75 } 76 77 if (calculate_hash(data, size, algo, value, &value_len)) { 78 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n", 79 algo, node_name, image_name); 80 return -EPROTONOSUPPORT; 81 } 82 83 ret = fit_set_hash_value(fit, noffset, value, value_len); 84 if (ret) { 85 printf("Can't set hash value for '%s' hash node in '%s' image node\n", 86 node_name, image_name); 87 return ret; 88 } 89 90 return 0; 91 } 92 93 /** 94 * fit_image_write_sig() - write the signature to a FIT 95 * 96 * This writes the signature and signer data to the FIT. 97 * 98 * @fit: pointer to the FIT format image header 99 * @noffset: hash node offset 100 * @value: signature value to be set 101 * @value_len: signature value length 102 * @comment: Text comment to write (NULL for none) 103 * 104 * returns 105 * 0, on success 106 * -FDT_ERR_..., on failure 107 */ 108 static int fit_image_write_sig(void *fit, int noffset, uint8_t *value, 109 int value_len, const char *comment, const char *region_prop, 110 int region_proplen) 111 { 112 int string_size; 113 int ret; 114 115 /* 116 * Get the current string size, before we update the FIT and add 117 * more 118 */ 119 string_size = fdt_size_dt_strings(fit); 120 121 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); 122 if (!ret) { 123 ret = fdt_setprop_string(fit, noffset, "signer-name", 124 "mkimage"); 125 } 126 if (!ret) { 127 ret = fdt_setprop_string(fit, noffset, "signer-version", 128 PLAIN_VERSION); 129 } 130 if (comment && !ret) 131 ret = fdt_setprop_string(fit, noffset, "comment", comment); 132 if (!ret) 133 ret = fit_set_timestamp(fit, noffset, time(NULL)); 134 if (region_prop && !ret) { 135 uint32_t strdata[2]; 136 137 ret = fdt_setprop(fit, noffset, "hashed-nodes", 138 region_prop, region_proplen); 139 strdata[0] = 0; 140 strdata[1] = cpu_to_fdt32(string_size); 141 if (!ret) { 142 ret = fdt_setprop(fit, noffset, "hashed-strings", 143 strdata, sizeof(strdata)); 144 } 145 } 146 147 return ret; 148 } 149 150 static int fit_image_setup_sig(struct image_sign_info *info, 151 const char *keydir, void *fit, const char *image_name, 152 int noffset, const char *require_keys, const char *engine_id) 153 { 154 const char *node_name; 155 char *algo_name; 156 157 node_name = fit_get_name(fit, noffset, NULL); 158 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { 159 printf("Can't get algo property for '%s' signature node in '%s' image node\n", 160 node_name, image_name); 161 return -1; 162 } 163 164 memset(info, '\0', sizeof(*info)); 165 info->keydir = keydir; 166 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); 167 info->fit = fit; 168 info->node_offset = noffset; 169 info->name = algo_name; 170 info->checksum = image_get_checksum_algo(algo_name); 171 info->crypto = image_get_crypto_algo(algo_name); 172 info->require_keys = require_keys; 173 info->engine_id = engine_id; 174 if (!info->checksum || !info->crypto) { 175 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n", 176 algo_name, node_name, image_name); 177 return -1; 178 } 179 180 return 0; 181 } 182 183 /** 184 * fit_image_process_sig- Process a single subnode of the images/ node 185 * 186 * Check each subnode and process accordingly. For signature nodes we 187 * generate a signed hash of the supplised data and store it in the node. 188 * 189 * @keydir: Directory containing keys to use for signing 190 * @keydest: Destination FDT blob to write public keys into 191 * @fit: pointer to the FIT format image header 192 * @image_name: name of image being processes (used to display errors) 193 * @noffset: subnode offset 194 * @data: data to process 195 * @size: size of data in bytes 196 * @comment: Comment to add to signature nodes 197 * @require_keys: Mark all keys as 'required' 198 * @engine_id: Engine to use for signing 199 * @return 0 if ok, -1 on error 200 */ 201 static int fit_image_process_sig(const char *keydir, void *keydest, 202 void *fit, const char *image_name, 203 int noffset, const void *data, size_t size, 204 const char *comment, int require_keys, const char *engine_id) 205 { 206 struct image_sign_info info; 207 struct image_region region; 208 const char *node_name; 209 uint8_t *value; 210 uint value_len; 211 int ret; 212 213 if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset, 214 require_keys ? "image" : NULL, engine_id)) 215 return -1; 216 217 node_name = fit_get_name(fit, noffset, NULL); 218 region.data = data; 219 region.size = size; 220 ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); 221 if (ret) { 222 printf("Failed to sign '%s' signature node in '%s' image node: %d\n", 223 node_name, image_name, ret); 224 225 /* We allow keys to be missing */ 226 if (ret == -ENOENT) 227 return 0; 228 return -1; 229 } 230 231 ret = fit_image_write_sig(fit, noffset, value, value_len, comment, 232 NULL, 0); 233 if (ret) { 234 if (ret == -FDT_ERR_NOSPACE) 235 return -ENOSPC; 236 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", 237 node_name, image_name, fdt_strerror(ret)); 238 return -1; 239 } 240 free(value); 241 242 /* Get keyname again, as FDT has changed and invalidated our pointer */ 243 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); 244 245 if (keydest) 246 ret = info.crypto->add_verify_data(&info, keydest); 247 else 248 return -1; 249 250 /* 251 * Write the public key into the supplied FDT file; this might fail 252 * several times, since we try signing with successively increasing 253 * size values 254 */ 255 if (keydest && ret) 256 return ret; 257 258 return 0; 259 } 260 261 /** 262 * fit_image_add_verification_data() - calculate/set verig. data for image node 263 * 264 * This adds hash and signature values for an component image node. 265 * 266 * All existing hash subnodes are checked, if algorithm property is set to 267 * one of the supported hash algorithms, hash value is computed and 268 * corresponding hash node property is set, for example: 269 * 270 * Input component image node structure: 271 * 272 * o image@1 (at image_noffset) 273 * | - data = [binary data] 274 * o hash@1 275 * |- algo = "sha1" 276 * 277 * Output component image node structure: 278 * 279 * o image@1 (at image_noffset) 280 * | - data = [binary data] 281 * o hash@1 282 * |- algo = "sha1" 283 * |- value = sha1(data) 284 * 285 * For signature details, please see doc/uImage.FIT/signature.txt 286 * 287 * @keydir Directory containing *.key and *.crt files (or NULL) 288 * @keydest FDT Blob to write public keys into (NULL if none) 289 * @fit: Pointer to the FIT format image header 290 * @image_noffset: Requested component image node 291 * @comment: Comment to add to signature nodes 292 * @require_keys: Mark all keys as 'required' 293 * @engine_id: Engine to use for signing 294 * @return: 0 on success, <0 on failure 295 */ 296 int fit_image_add_verification_data(const char *keydir, void *keydest, 297 void *fit, int image_noffset, const char *comment, 298 int require_keys, const char *engine_id) 299 { 300 const char *image_name; 301 const void *data; 302 size_t size; 303 int noffset; 304 305 /* Get image data and data length */ 306 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 307 printf("Can't get image data/size\n"); 308 return -1; 309 } 310 311 image_name = fit_get_name(fit, image_noffset, NULL); 312 313 /* Process all hash subnodes of the component image node */ 314 for (noffset = fdt_first_subnode(fit, image_noffset); 315 noffset >= 0; 316 noffset = fdt_next_subnode(fit, noffset)) { 317 const char *node_name; 318 int ret = 0; 319 320 /* 321 * Check subnode name, must be equal to "hash" or "signature". 322 * Multiple hash nodes require unique unit node 323 * names, e.g. hash@1, hash@2, signature@1, etc. 324 */ 325 node_name = fit_get_name(fit, noffset, NULL); 326 if (!strncmp(node_name, FIT_HASH_NODENAME, 327 strlen(FIT_HASH_NODENAME))) { 328 ret = fit_image_process_hash(fit, image_name, noffset, 329 data, size); 330 } else if (IMAGE_ENABLE_SIGN && keydir && 331 !strncmp(node_name, FIT_SIG_NODENAME, 332 strlen(FIT_SIG_NODENAME))) { 333 ret = fit_image_process_sig(keydir, keydest, 334 fit, image_name, noffset, data, size, 335 comment, require_keys, engine_id); 336 } 337 if (ret) 338 return ret; 339 } 340 341 return 0; 342 } 343 344 struct strlist { 345 int count; 346 char **strings; 347 }; 348 349 static void strlist_init(struct strlist *list) 350 { 351 memset(list, '\0', sizeof(*list)); 352 } 353 354 static void strlist_free(struct strlist *list) 355 { 356 int i; 357 358 for (i = 0; i < list->count; i++) 359 free(list->strings[i]); 360 free(list->strings); 361 } 362 363 static int strlist_add(struct strlist *list, const char *str) 364 { 365 char *dup; 366 367 dup = strdup(str); 368 list->strings = realloc(list->strings, 369 (list->count + 1) * sizeof(char *)); 370 if (!list || !str) 371 return -1; 372 list->strings[list->count++] = dup; 373 374 return 0; 375 } 376 377 static const char *fit_config_get_image_list(void *fit, int noffset, 378 int *lenp, int *allow_missingp) 379 { 380 static const char default_list[] = FIT_KERNEL_PROP "\0" 381 FIT_FDT_PROP; 382 const char *prop; 383 384 /* If there is an "image" property, use that */ 385 prop = fdt_getprop(fit, noffset, "sign-images", lenp); 386 if (prop) { 387 *allow_missingp = 0; 388 return *lenp ? prop : NULL; 389 } 390 391 /* Default image list */ 392 *allow_missingp = 1; 393 *lenp = sizeof(default_list); 394 395 return default_list; 396 } 397 398 static int fit_config_get_hash_list(void *fit, int conf_noffset, 399 int sig_offset, struct strlist *node_inc) 400 { 401 int allow_missing; 402 const char *prop, *iname, *end; 403 const char *conf_name, *sig_name; 404 char name[200], path[200]; 405 int image_count; 406 int ret, len; 407 408 conf_name = fit_get_name(fit, conf_noffset, NULL); 409 sig_name = fit_get_name(fit, sig_offset, NULL); 410 411 /* 412 * Build a list of nodes we need to hash. We always need the root 413 * node and the configuration. 414 */ 415 strlist_init(node_inc); 416 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name); 417 if (strlist_add(node_inc, "/") || 418 strlist_add(node_inc, FIT_CONFS_PATH) || 419 strlist_add(node_inc, name)) 420 goto err_mem; 421 422 /* Get a list of images that we intend to sign */ 423 prop = fit_config_get_image_list(fit, sig_offset, &len, 424 &allow_missing); 425 if (!prop) 426 return 0; 427 428 /* Locate the images */ 429 end = prop + len; 430 image_count = 0; 431 for (iname = prop; iname < end; iname += strlen(iname) + 1) { 432 int noffset; 433 int image_noffset; 434 int hash_count; 435 int i; 436 437 for (i = 0; i < 5; i++) { 438 image_noffset = 439 fit_conf_get_prop_node_index(fit, conf_noffset, 440 iname, i); 441 if (image_noffset < 0) { 442 if (i > 0) 443 break; 444 445 printf("Failed to find image '%s' in configuration '%s/%s'\n", 446 iname, conf_name, sig_name); 447 if (allow_missing) 448 continue; 449 450 return -ENOENT; 451 } 452 453 ret = fdt_get_path(fit, image_noffset, path, sizeof(path)); 454 if (ret < 0) 455 goto err_path; 456 if (strlist_add(node_inc, path)) 457 goto err_mem; 458 459 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, 460 conf_name); 461 462 /* Add all this image's hashes */ 463 hash_count = 0; 464 for (noffset = fdt_first_subnode(fit, image_noffset); 465 noffset >= 0; 466 noffset = fdt_next_subnode(fit, noffset)) { 467 const char *name = fit_get_name(fit, noffset, NULL); 468 469 if (strncmp(name, FIT_HASH_NODENAME, 470 strlen(FIT_HASH_NODENAME))) 471 continue; 472 ret = fdt_get_path(fit, noffset, path, sizeof(path)); 473 if (ret < 0) 474 goto err_path; 475 if (strlist_add(node_inc, path)) 476 goto err_mem; 477 hash_count++; 478 } 479 480 if (!hash_count) { 481 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n", 482 conf_name, sig_name, iname); 483 return -ENOMSG; 484 } 485 486 image_count++; 487 } 488 } 489 490 if (!image_count) { 491 printf("Failed to find any images for configuration '%s/%s'\n", 492 conf_name, sig_name); 493 return -ENOMSG; 494 } 495 496 return 0; 497 498 err_mem: 499 printf("Out of memory processing configuration '%s/%s'\n", conf_name, 500 sig_name); 501 return -ENOMEM; 502 503 err_path: 504 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n", 505 iname, conf_name, sig_name, fdt_strerror(ret)); 506 return -ENOENT; 507 } 508 509 static int fit_config_get_data(void *fit, int conf_noffset, int noffset, 510 struct image_region **regionp, int *region_countp, 511 char **region_propp, int *region_proplen) 512 { 513 char * const exc_prop[] = {"data"}; 514 struct strlist node_inc; 515 struct image_region *region; 516 struct fdt_region fdt_regions[100]; 517 const char *conf_name, *sig_name; 518 char path[200]; 519 int count, i; 520 char *region_prop; 521 int ret, len; 522 523 conf_name = fit_get_name(fit, conf_noffset, NULL); 524 sig_name = fit_get_name(fit, noffset, NULL); 525 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); 526 527 /* Get a list of nodes we want to hash */ 528 ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc); 529 if (ret) 530 return ret; 531 532 /* Get a list of regions to hash */ 533 count = fdt_find_regions(fit, node_inc.strings, node_inc.count, 534 exc_prop, ARRAY_SIZE(exc_prop), 535 fdt_regions, ARRAY_SIZE(fdt_regions), 536 path, sizeof(path), 1); 537 if (count < 0) { 538 printf("Failed to hash configuration '%s/%s': %s\n", conf_name, 539 sig_name, fdt_strerror(ret)); 540 return -EIO; 541 } 542 if (count == 0) { 543 printf("No data to hash for configuration '%s/%s': %s\n", 544 conf_name, sig_name, fdt_strerror(ret)); 545 return -EINVAL; 546 } 547 548 /* Build our list of data blocks */ 549 region = fit_region_make_list(fit, fdt_regions, count, NULL); 550 if (!region) { 551 printf("Out of memory hashing configuration '%s/%s'\n", 552 conf_name, sig_name); 553 return -ENOMEM; 554 } 555 556 /* Create a list of all hashed properties */ 557 debug("Hash nodes:\n"); 558 for (i = len = 0; i < node_inc.count; i++) { 559 debug(" %s\n", node_inc.strings[i]); 560 len += strlen(node_inc.strings[i]) + 1; 561 } 562 region_prop = malloc(len); 563 if (!region_prop) { 564 printf("Out of memory setting up regions for configuration '%s/%s'\n", 565 conf_name, sig_name); 566 return -ENOMEM; 567 } 568 for (i = len = 0; i < node_inc.count; 569 len += strlen(node_inc.strings[i]) + 1, i++) 570 strcpy(region_prop + len, node_inc.strings[i]); 571 strlist_free(&node_inc); 572 573 *region_countp = count; 574 *regionp = region; 575 *region_propp = region_prop; 576 *region_proplen = len; 577 578 return 0; 579 } 580 581 static int fit_config_process_sig(const char *keydir, void *keydest, 582 void *fit, const char *conf_name, int conf_noffset, 583 int noffset, const char *comment, int require_keys, 584 const char *engine_id) 585 { 586 struct image_sign_info info; 587 const char *node_name; 588 struct image_region *region; 589 char *region_prop; 590 int region_proplen; 591 int region_count; 592 uint8_t *value; 593 uint value_len; 594 int ret; 595 596 node_name = fit_get_name(fit, noffset, NULL); 597 if (fit_config_get_data(fit, conf_noffset, noffset, ®ion, 598 ®ion_count, ®ion_prop, ®ion_proplen)) 599 return -1; 600 601 if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset, 602 require_keys ? "conf" : NULL, engine_id)) 603 return -1; 604 605 ret = info.crypto->sign(&info, region, region_count, &value, 606 &value_len); 607 free(region); 608 if (ret) { 609 printf("Failed to sign '%s' signature node in '%s' conf node\n", 610 node_name, conf_name); 611 return -1; 612 } 613 614 ret = fit_image_write_sig(fit, noffset, value, value_len, comment, 615 region_prop, region_proplen); 616 if (ret) { 617 if (ret == -FDT_ERR_NOSPACE) 618 return -ENOSPC; 619 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", 620 node_name, conf_name, fdt_strerror(ret)); 621 return -1; 622 } 623 free(value); 624 free(region_prop); 625 626 /* Get keyname again, as FDT has changed and invalidated our pointer */ 627 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); 628 629 /* Write the public key into the supplied FDT file */ 630 if (keydest) { 631 ret = info.crypto->add_verify_data(&info, keydest); 632 if (ret == -ENOSPC) 633 return -ENOSPC; 634 if (ret) { 635 printf("Failed to add verification data for '%s' signature node in '%s' image node\n", 636 node_name, conf_name); 637 } 638 return ret; 639 } 640 641 return 0; 642 } 643 644 static int fit_config_add_verification_data(const char *keydir, void *keydest, 645 void *fit, int conf_noffset, const char *comment, 646 int require_keys, const char *engine_id) 647 { 648 const char *conf_name; 649 int noffset; 650 651 conf_name = fit_get_name(fit, conf_noffset, NULL); 652 653 /* Process all hash subnodes of the configuration node */ 654 for (noffset = fdt_first_subnode(fit, conf_noffset); 655 noffset >= 0; 656 noffset = fdt_next_subnode(fit, noffset)) { 657 const char *node_name; 658 int ret = 0; 659 660 node_name = fit_get_name(fit, noffset, NULL); 661 if (!strncmp(node_name, FIT_SIG_NODENAME, 662 strlen(FIT_SIG_NODENAME))) { 663 ret = fit_config_process_sig(keydir, keydest, 664 fit, conf_name, conf_noffset, noffset, comment, 665 require_keys, engine_id); 666 } 667 if (ret) 668 return ret; 669 } 670 671 return 0; 672 } 673 674 int fit_add_verification_data(const char *keydir, void *keydest, void *fit, 675 const char *comment, int require_keys, 676 const char *engine_id) 677 { 678 int images_noffset, confs_noffset; 679 int noffset; 680 int ret; 681 682 /* Find images parent node offset */ 683 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 684 if (images_noffset < 0) { 685 printf("Can't find images parent node '%s' (%s)\n", 686 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 687 return images_noffset; 688 } 689 690 /* Process its subnodes, print out component images details */ 691 for (noffset = fdt_first_subnode(fit, images_noffset); 692 noffset >= 0; 693 noffset = fdt_next_subnode(fit, noffset)) { 694 /* 695 * Direct child node of the images parent node, 696 * i.e. component image node. 697 */ 698 ret = fit_image_add_verification_data(keydir, keydest, 699 fit, noffset, comment, require_keys, engine_id); 700 if (ret) 701 return ret; 702 } 703 704 /* If there are no keys, we can't sign configurations */ 705 if (!IMAGE_ENABLE_SIGN || !keydir) 706 return 0; 707 708 /* Find configurations parent node offset */ 709 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 710 if (confs_noffset < 0) { 711 printf("Can't find images parent node '%s' (%s)\n", 712 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 713 return -ENOENT; 714 } 715 716 /* Process its subnodes, print out component images details */ 717 for (noffset = fdt_first_subnode(fit, confs_noffset); 718 noffset >= 0; 719 noffset = fdt_next_subnode(fit, noffset)) { 720 ret = fit_config_add_verification_data(keydir, keydest, 721 fit, noffset, comment, 722 require_keys, 723 engine_id); 724 if (ret) 725 return ret; 726 } 727 728 return 0; 729 } 730 731 #ifdef CONFIG_FIT_SIGNATURE 732 int fit_check_sign(const void *fit, const void *key, int is_spl) 733 { 734 int cfg_noffset; 735 int ret; 736 737 cfg_noffset = fit_conf_get_node(fit, NULL); 738 if (!cfg_noffset) 739 return -1; 740 741 printf("Verifying Hash Integrity ... "); 742 ret = fit_config_verify(fit, cfg_noffset); 743 if (ret) 744 return ret; 745 ret = bootm_host_load_images(fit, cfg_noffset, is_spl); 746 747 return ret; 748 } 749 #endif 750