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