1 /* 2 * Copyright (c) 2013, Google Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include "mkimage.h" 8 #include <stdio.h> 9 #include <string.h> 10 #include <image.h> 11 #include <time.h> 12 #include <openssl/bn.h> 13 #include <openssl/rsa.h> 14 #include <openssl/pem.h> 15 #include <openssl/err.h> 16 #include <openssl/ssl.h> 17 #include <openssl/evp.h> 18 #include <openssl/engine.h> 19 20 #if OPENSSL_VERSION_NUMBER >= 0x10000000L 21 #define HAVE_ERR_REMOVE_THREAD_STATE 22 #endif 23 24 #if OPENSSL_VERSION_NUMBER < 0x10100000L 25 static void RSA_get0_key(const RSA *r, 26 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) 27 { 28 if (n != NULL) 29 *n = r->n; 30 if (e != NULL) 31 *e = r->e; 32 if (d != NULL) 33 *d = r->d; 34 } 35 #endif 36 37 static int rsa_err(const char *msg) 38 { 39 unsigned long sslErr = ERR_get_error(); 40 41 fprintf(stderr, "%s", msg); 42 fprintf(stderr, ": %s\n", 43 ERR_error_string(sslErr, 0)); 44 45 return -1; 46 } 47 48 /** 49 * rsa_pem_get_pub_key() - read a public key from a .crt file 50 * 51 * @keydir: Directory containins the key 52 * @name Name of key file (will have a .crt extension) 53 * @rsap Returns RSA object, or NULL on failure 54 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) 55 */ 56 static int rsa_pem_get_pub_key(const char *keydir, const char *name, RSA **rsap) 57 { 58 char path[1024]; 59 EVP_PKEY *key; 60 X509 *cert; 61 RSA *rsa; 62 FILE *f; 63 int ret; 64 65 *rsap = NULL; 66 snprintf(path, sizeof(path), "%s/%s.crt", keydir, name); 67 f = fopen(path, "r"); 68 if (!f) { 69 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", 70 path, strerror(errno)); 71 return -EACCES; 72 } 73 74 /* Read the certificate */ 75 cert = NULL; 76 if (!PEM_read_X509(f, &cert, NULL, NULL)) { 77 rsa_err("Couldn't read certificate"); 78 ret = -EINVAL; 79 goto err_cert; 80 } 81 82 /* Get the public key from the certificate. */ 83 key = X509_get_pubkey(cert); 84 if (!key) { 85 rsa_err("Couldn't read public key\n"); 86 ret = -EINVAL; 87 goto err_pubkey; 88 } 89 90 /* Convert to a RSA_style key. */ 91 rsa = EVP_PKEY_get1_RSA(key); 92 if (!rsa) { 93 rsa_err("Couldn't convert to a RSA style key"); 94 ret = -EINVAL; 95 goto err_rsa; 96 } 97 fclose(f); 98 EVP_PKEY_free(key); 99 X509_free(cert); 100 *rsap = rsa; 101 102 return 0; 103 104 err_rsa: 105 EVP_PKEY_free(key); 106 err_pubkey: 107 X509_free(cert); 108 err_cert: 109 fclose(f); 110 return ret; 111 } 112 113 /** 114 * rsa_engine_get_pub_key() - read a public key from given engine 115 * 116 * @keydir: Key prefix 117 * @name Name of key 118 * @engine Engine to use 119 * @rsap Returns RSA object, or NULL on failure 120 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) 121 */ 122 static int rsa_engine_get_pub_key(const char *keydir, const char *name, 123 ENGINE *engine, RSA **rsap) 124 { 125 const char *engine_id; 126 char key_id[1024]; 127 EVP_PKEY *key; 128 RSA *rsa; 129 int ret; 130 131 *rsap = NULL; 132 133 engine_id = ENGINE_get_id(engine); 134 135 if (engine_id && !strcmp(engine_id, "pkcs11")) { 136 if (keydir) 137 snprintf(key_id, sizeof(key_id), 138 "pkcs11:%s;object=%s;type=public", 139 keydir, name); 140 else 141 snprintf(key_id, sizeof(key_id), 142 "pkcs11:object=%s;type=public", 143 name); 144 } else { 145 fprintf(stderr, "Engine not supported\n"); 146 return -ENOTSUP; 147 } 148 149 key = ENGINE_load_public_key(engine, key_id, NULL, NULL); 150 if (!key) 151 return rsa_err("Failure loading public key from engine"); 152 153 /* Convert to a RSA_style key. */ 154 rsa = EVP_PKEY_get1_RSA(key); 155 if (!rsa) { 156 rsa_err("Couldn't convert to a RSA style key"); 157 ret = -EINVAL; 158 goto err_rsa; 159 } 160 161 EVP_PKEY_free(key); 162 *rsap = rsa; 163 164 return 0; 165 166 err_rsa: 167 EVP_PKEY_free(key); 168 return ret; 169 } 170 171 /** 172 * rsa_get_pub_key() - read a public key 173 * 174 * @keydir: Directory containing the key (PEM file) or key prefix (engine) 175 * @name Name of key file (will have a .crt extension) 176 * @engine Engine to use 177 * @rsap Returns RSA object, or NULL on failure 178 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) 179 */ 180 static int rsa_get_pub_key(const char *keydir, const char *name, 181 ENGINE *engine, RSA **rsap) 182 { 183 if (engine) 184 return rsa_engine_get_pub_key(keydir, name, engine, rsap); 185 return rsa_pem_get_pub_key(keydir, name, rsap); 186 } 187 188 /** 189 * rsa_pem_get_priv_key() - read a private key from a .key file 190 * 191 * @keydir: Directory containing the key 192 * @name Name of key file (will have a .key extension) 193 * @rsap Returns RSA object, or NULL on failure 194 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) 195 */ 196 static int rsa_pem_get_priv_key(const char *keydir, const char *name, 197 RSA **rsap) 198 { 199 char path[1024]; 200 RSA *rsa; 201 FILE *f; 202 203 *rsap = NULL; 204 snprintf(path, sizeof(path), "%s/%s.key", keydir, name); 205 f = fopen(path, "r"); 206 if (!f) { 207 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n", 208 path, strerror(errno)); 209 return -ENOENT; 210 } 211 212 rsa = PEM_read_RSAPrivateKey(f, 0, NULL, path); 213 if (!rsa) { 214 rsa_err("Failure reading private key"); 215 fclose(f); 216 return -EPROTO; 217 } 218 fclose(f); 219 *rsap = rsa; 220 221 return 0; 222 } 223 224 /** 225 * rsa_engine_get_priv_key() - read a private key from given engine 226 * 227 * @keydir: Key prefix 228 * @name Name of key 229 * @engine Engine to use 230 * @rsap Returns RSA object, or NULL on failure 231 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) 232 */ 233 static int rsa_engine_get_priv_key(const char *keydir, const char *name, 234 ENGINE *engine, RSA **rsap) 235 { 236 const char *engine_id; 237 char key_id[1024]; 238 EVP_PKEY *key; 239 RSA *rsa; 240 int ret; 241 242 *rsap = NULL; 243 244 engine_id = ENGINE_get_id(engine); 245 246 if (engine_id && !strcmp(engine_id, "pkcs11")) { 247 if (keydir) 248 snprintf(key_id, sizeof(key_id), 249 "pkcs11:%s;object=%s;type=private", 250 keydir, name); 251 else 252 snprintf(key_id, sizeof(key_id), 253 "pkcs11:object=%s;type=private", 254 name); 255 } else { 256 fprintf(stderr, "Engine not supported\n"); 257 return -ENOTSUP; 258 } 259 260 key = ENGINE_load_private_key(engine, key_id, NULL, NULL); 261 if (!key) 262 return rsa_err("Failure loading private key from engine"); 263 264 /* Convert to a RSA_style key. */ 265 rsa = EVP_PKEY_get1_RSA(key); 266 if (!rsa) { 267 rsa_err("Couldn't convert to a RSA style key"); 268 ret = -EINVAL; 269 goto err_rsa; 270 } 271 272 EVP_PKEY_free(key); 273 *rsap = rsa; 274 275 return 0; 276 277 err_rsa: 278 EVP_PKEY_free(key); 279 return ret; 280 } 281 282 /** 283 * rsa_get_priv_key() - read a private key 284 * 285 * @keydir: Directory containing the key (PEM file) or key prefix (engine) 286 * @name Name of key 287 * @engine Engine to use for signing 288 * @rsap Returns RSA object, or NULL on failure 289 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) 290 */ 291 static int rsa_get_priv_key(const char *keydir, const char *name, 292 ENGINE *engine, RSA **rsap) 293 { 294 if (engine) 295 return rsa_engine_get_priv_key(keydir, name, engine, rsap); 296 return rsa_pem_get_priv_key(keydir, name, rsap); 297 } 298 299 static int rsa_init(void) 300 { 301 int ret; 302 303 #if OPENSSL_VERSION_NUMBER < 0x10100000L 304 ret = SSL_library_init(); 305 #else 306 ret = OPENSSL_init_ssl(0, NULL); 307 #endif 308 if (!ret) { 309 fprintf(stderr, "Failure to init SSL library\n"); 310 return -1; 311 } 312 #if OPENSSL_VERSION_NUMBER < 0x10100000L 313 SSL_load_error_strings(); 314 315 OpenSSL_add_all_algorithms(); 316 OpenSSL_add_all_digests(); 317 OpenSSL_add_all_ciphers(); 318 #endif 319 320 return 0; 321 } 322 323 static int rsa_engine_init(const char *engine_id, ENGINE **pe) 324 { 325 ENGINE *e; 326 int ret; 327 328 ENGINE_load_builtin_engines(); 329 330 e = ENGINE_by_id(engine_id); 331 if (!e) { 332 fprintf(stderr, "Engine isn't available\n"); 333 ret = -1; 334 goto err_engine_by_id; 335 } 336 337 if (!ENGINE_init(e)) { 338 fprintf(stderr, "Couldn't initialize engine\n"); 339 ret = -1; 340 goto err_engine_init; 341 } 342 343 if (!ENGINE_set_default_RSA(e)) { 344 fprintf(stderr, "Couldn't set engine as default for RSA\n"); 345 ret = -1; 346 goto err_set_rsa; 347 } 348 349 *pe = e; 350 351 return 0; 352 353 err_set_rsa: 354 ENGINE_finish(e); 355 err_engine_init: 356 ENGINE_free(e); 357 err_engine_by_id: 358 #if OPENSSL_VERSION_NUMBER < 0x10100000L 359 ENGINE_cleanup(); 360 #endif 361 return ret; 362 } 363 364 static void rsa_remove(void) 365 { 366 #if OPENSSL_VERSION_NUMBER < 0x10100000L 367 CRYPTO_cleanup_all_ex_data(); 368 ERR_free_strings(); 369 #ifdef HAVE_ERR_REMOVE_THREAD_STATE 370 ERR_remove_thread_state(NULL); 371 #else 372 ERR_remove_state(0); 373 #endif 374 EVP_cleanup(); 375 #endif 376 } 377 378 static void rsa_engine_remove(ENGINE *e) 379 { 380 if (e) { 381 ENGINE_finish(e); 382 ENGINE_free(e); 383 } 384 } 385 386 /* 387 * With this data2sign.bin, we can provide it to who real holds the RAS-private 388 * key to sign current fit image. Then we replace the signature in fit image 389 * with a valid one. 390 */ 391 static int gen_data2sign(const struct image_region region[], int region_count) 392 { 393 char *file = "data2sign.bin"; 394 FILE *fd; 395 int i; 396 397 fd = fopen(file, "wb"); 398 if (!fd) { 399 fprintf(stderr, "Failed to create %s: %s\n", 400 file, strerror(errno)); 401 return -ENOENT; 402 } 403 404 for (i = 0; i < region_count; i++) 405 fwrite(region[i].data, region[i].size, 1, fd); 406 407 fclose(fd); 408 409 return 0; 410 } 411 412 static int rsa_sign_with_key(RSA *rsa, struct checksum_algo *checksum_algo, 413 const struct image_region region[], int region_count, 414 uint8_t **sigp, uint *sig_size) 415 { 416 EVP_PKEY *key; 417 EVP_MD_CTX *context; 418 int size, ret = 0; 419 uint8_t *sig; 420 int i; 421 422 key = EVP_PKEY_new(); 423 if (!key) 424 return rsa_err("EVP_PKEY object creation failed"); 425 426 if (!EVP_PKEY_set1_RSA(key, rsa)) { 427 ret = rsa_err("EVP key setup failed"); 428 goto err_set; 429 } 430 431 size = EVP_PKEY_size(key); 432 sig = malloc(size); 433 if (!sig) { 434 fprintf(stderr, "Out of memory for signature (%d bytes)\n", 435 size); 436 ret = -ENOMEM; 437 goto err_alloc; 438 } 439 440 context = EVP_MD_CTX_create(); 441 if (!context) { 442 ret = rsa_err("EVP context creation failed"); 443 goto err_create; 444 } 445 EVP_MD_CTX_init(context); 446 if (!EVP_SignInit(context, checksum_algo->calculate_sign())) { 447 ret = rsa_err("Signer setup failed"); 448 goto err_sign; 449 } 450 451 for (i = 0; i < region_count; i++) { 452 if (!EVP_SignUpdate(context, region[i].data, region[i].size)) { 453 ret = rsa_err("Signing data failed"); 454 goto err_sign; 455 } 456 } 457 458 if (!EVP_SignFinal(context, sig, sig_size, key)) { 459 ret = rsa_err("Could not obtain signature"); 460 goto err_sign; 461 } 462 #if OPENSSL_VERSION_NUMBER < 0x10100000L 463 EVP_MD_CTX_cleanup(context); 464 #else 465 EVP_MD_CTX_reset(context); 466 #endif 467 EVP_MD_CTX_destroy(context); 468 EVP_PKEY_free(key); 469 470 debug("Got signature: %d bytes, expected %d\n", *sig_size, size); 471 *sigp = sig; 472 *sig_size = size; 473 474 gen_data2sign(region, region_count); 475 476 return 0; 477 478 err_sign: 479 EVP_MD_CTX_destroy(context); 480 err_create: 481 free(sig); 482 err_alloc: 483 err_set: 484 EVP_PKEY_free(key); 485 return ret; 486 } 487 488 int rsa_sign(struct image_sign_info *info, 489 const struct image_region region[], int region_count, 490 uint8_t **sigp, uint *sig_len) 491 { 492 RSA *rsa; 493 ENGINE *e = NULL; 494 int ret; 495 496 ret = rsa_init(); 497 if (ret) 498 return ret; 499 500 if (info->engine_id) { 501 ret = rsa_engine_init(info->engine_id, &e); 502 if (ret) 503 goto err_engine; 504 } 505 506 ret = rsa_get_priv_key(info->keydir, info->keyname, e, &rsa); 507 if (ret) 508 goto err_priv; 509 ret = rsa_sign_with_key(rsa, info->checksum, region, 510 region_count, sigp, sig_len); 511 if (ret) 512 goto err_sign; 513 514 RSA_free(rsa); 515 if (info->engine_id) 516 rsa_engine_remove(e); 517 rsa_remove(); 518 519 return ret; 520 521 err_sign: 522 RSA_free(rsa); 523 err_priv: 524 if (info->engine_id) 525 rsa_engine_remove(e); 526 err_engine: 527 rsa_remove(); 528 return ret; 529 } 530 531 /* 532 * rsa_get_exponent(): - Get the public exponent from an RSA key 533 */ 534 static int rsa_get_exponent(RSA *key, uint64_t *e) 535 { 536 int ret; 537 BIGNUM *bn_te; 538 const BIGNUM *key_e; 539 uint64_t te; 540 541 ret = -EINVAL; 542 bn_te = NULL; 543 544 if (!e) 545 goto cleanup; 546 547 RSA_get0_key(key, NULL, &key_e, NULL); 548 if (BN_num_bits(key_e) > 64) 549 goto cleanup; 550 551 *e = BN_get_word(key_e); 552 553 if (BN_num_bits(key_e) < 33) { 554 ret = 0; 555 goto cleanup; 556 } 557 558 bn_te = BN_dup(key_e); 559 if (!bn_te) 560 goto cleanup; 561 562 if (!BN_rshift(bn_te, bn_te, 32)) 563 goto cleanup; 564 565 if (!BN_mask_bits(bn_te, 32)) 566 goto cleanup; 567 568 te = BN_get_word(bn_te); 569 te <<= 32; 570 *e |= te; 571 ret = 0; 572 573 cleanup: 574 if (bn_te) 575 BN_free(bn_te); 576 577 return ret; 578 } 579 580 /* 581 * rsa_get_params(): - Get the important parameters of an RSA public key 582 */ 583 int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp, 584 BIGNUM **modulusp, BIGNUM **exponent_BN, BIGNUM **r_squaredp, 585 BIGNUM **c_factorp, BIGNUM **np_factorp) 586 { 587 BIGNUM *big1, *big2, *big32, *big2_32, *big4100, *big2180; 588 BIGNUM *n, *e, *r, *r_squared, *tmp, *c_factor, *np_factor; 589 const BIGNUM *key_n, *key_e; 590 BN_CTX *bn_ctx = BN_CTX_new(); 591 int ret = 0; 592 593 /* Initialize BIGNUMs */ 594 big1 = BN_new(); 595 big2 = BN_new(); 596 big32 = BN_new(); 597 big4100 = BN_new(); 598 big2180 = BN_new(); 599 600 r = BN_new(); 601 r_squared = BN_new(); 602 c_factor = BN_new(); 603 np_factor = BN_new(); 604 tmp = BN_new(); 605 big2_32 = BN_new(); 606 n = BN_new(); 607 e = BN_new(); 608 if (!big1 || !big2 || !big32 || !big4100 || !big2180 || !r || 609 !r_squared || !tmp || !big2_32 || !n || !e || 610 !c_factor || !np_factor) { 611 fprintf(stderr, "Out of memory (bignum)\n"); 612 return -ENOMEM; 613 } 614 615 if (0 != rsa_get_exponent(key, exponent)) 616 ret = -1; 617 618 RSA_get0_key(key, &key_n, &key_e, NULL); 619 if (!BN_copy(n, key_n) || !BN_copy(e, key_e) || 620 !BN_set_word(big1, 1L) || 621 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L) || 622 !BN_set_word(big4100, 4100L) || !BN_set_word(big2180, 2180L)) 623 ret = -1; 624 625 /* big2_32 = 2^32 */ 626 if (!BN_exp(big2_32, big2, big32, bn_ctx)) 627 ret = -1; 628 629 /* Calculate n0_inv = -1 / n[0] mod 2^32 */ 630 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) || 631 !BN_sub(tmp, big2_32, tmp)) 632 ret = -1; 633 *n0_invp = BN_get_word(tmp); 634 635 /* Calculate R = 2^(# of key bits) */ 636 if (!BN_set_word(tmp, BN_num_bits(n)) || 637 !BN_exp(r, big2, tmp, bn_ctx)) 638 ret = -1; 639 640 /* Calculate r_squared = R^2 mod n */ 641 if (!BN_copy(r_squared, r) || 642 !BN_mul(tmp, r_squared, r, bn_ctx) || 643 !BN_mod(r_squared, tmp, n, bn_ctx)) 644 ret = -1; 645 646 /* Calculate c_factor = 2^4100 mod n */ 647 if (!BN_exp(tmp, big2, big4100, bn_ctx) || 648 !BN_mod(c_factor, tmp, n, bn_ctx)) 649 ret = -1; 650 651 /* Calculate np_factor = 2^2180 div n */ 652 if (!BN_exp(tmp, big2, big2180, bn_ctx) || 653 !BN_div(np_factor, NULL, tmp, n, bn_ctx)) 654 ret = -1; 655 656 *modulusp = n; 657 *exponent_BN = e; 658 *r_squaredp = r_squared; 659 *c_factorp = c_factor; 660 *np_factorp = np_factor; 661 662 BN_free(big1); 663 BN_free(big2); 664 BN_free(big32); 665 BN_free(big4100); 666 BN_free(big2180); 667 BN_free(r); 668 BN_free(tmp); 669 BN_free(big2_32); 670 if (ret) { 671 fprintf(stderr, "Bignum operations failed\n"); 672 return -ENOMEM; 673 } 674 675 return ret; 676 } 677 678 static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, 679 BIGNUM *num, int num_bits) 680 { 681 int nwords = num_bits / 32; 682 int size; 683 uint32_t *buf, *ptr; 684 BIGNUM *tmp, *big2, *big32, *big2_32; 685 BN_CTX *ctx; 686 int ret; 687 688 tmp = BN_new(); 689 big2 = BN_new(); 690 big32 = BN_new(); 691 big2_32 = BN_new(); 692 if (!tmp || !big2 || !big32 || !big2_32) { 693 fprintf(stderr, "Out of memory (bignum)\n"); 694 return -ENOMEM; 695 } 696 ctx = BN_CTX_new(); 697 if (!tmp) { 698 fprintf(stderr, "Out of memory (bignum context)\n"); 699 return -ENOMEM; 700 } 701 BN_set_word(big2, 2L); 702 BN_set_word(big32, 32L); 703 BN_exp(big2_32, big2, big32, ctx); /* B = 2^32 */ 704 705 size = nwords * sizeof(uint32_t); 706 buf = malloc(size); 707 if (!buf) { 708 fprintf(stderr, "Out of memory (%d bytes)\n", size); 709 return -ENOMEM; 710 } 711 712 /* Write out modulus as big endian array of integers */ 713 for (ptr = buf + nwords - 1; ptr >= buf; ptr--) { 714 BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */ 715 *ptr = cpu_to_fdt32(BN_get_word(tmp)); 716 BN_rshift(num, num, 32); /* N = N/B */ 717 } 718 719 /* 720 * We try signing with successively increasing size values, so this 721 * might fail several times 722 */ 723 ret = fdt_setprop(blob, noffset, prop_name, buf, size); 724 if (ret) 725 return -FDT_ERR_NOSPACE; 726 free(buf); 727 BN_free(tmp); 728 BN_free(big2); 729 BN_free(big32); 730 BN_free(big2_32); 731 732 return ret; 733 } 734 735 int rsa_add_verify_data(struct image_sign_info *info, void *keydest) 736 { 737 BIGNUM *modulus, *exponent_BN, *r_squared, *c_factor, *np_factor; 738 uint64_t exponent; 739 uint32_t n0_inv; 740 int parent, node; 741 char name[100]; 742 int ret; 743 int bits; 744 RSA *rsa; 745 ENGINE *e = NULL; 746 747 debug("%s: Getting verification data\n", __func__); 748 if (info->engine_id) { 749 ret = rsa_engine_init(info->engine_id, &e); 750 if (ret) 751 return ret; 752 } 753 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &rsa); 754 if (ret) 755 goto err_get_pub_key; 756 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, 757 &exponent_BN, &r_squared, &c_factor, &np_factor); 758 if (ret) 759 goto err_get_params; 760 bits = BN_num_bits(modulus); 761 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME); 762 if (parent == -FDT_ERR_NOTFOUND) { 763 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME); 764 if (parent < 0) { 765 ret = parent; 766 if (ret != -FDT_ERR_NOSPACE) { 767 fprintf(stderr, "Couldn't create signature node: %s\n", 768 fdt_strerror(parent)); 769 } 770 } 771 } 772 if (ret) 773 goto done; 774 775 /* Either create or overwrite the named key node */ 776 snprintf(name, sizeof(name), "key-%s", info->keyname); 777 node = fdt_subnode_offset(keydest, parent, name); 778 if (node == -FDT_ERR_NOTFOUND) { 779 node = fdt_add_subnode(keydest, parent, name); 780 if (node < 0) { 781 ret = node; 782 if (ret != -FDT_ERR_NOSPACE) { 783 fprintf(stderr, "Could not create key subnode: %s\n", 784 fdt_strerror(node)); 785 } 786 } 787 } else if (node < 0) { 788 fprintf(stderr, "Cannot select keys parent: %s\n", 789 fdt_strerror(node)); 790 ret = node; 791 } 792 793 if (!ret) { 794 ret = fdt_setprop_string(keydest, node, "key-name-hint", 795 info->keyname); 796 } 797 if (!ret) 798 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits); 799 if (!ret) 800 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv); 801 if (!ret) { 802 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent); 803 } 804 if (!ret) { 805 ret = fdt_add_bignum(keydest, node, "rsa,exponent-BN", 806 exponent_BN, bits); 807 } 808 if (!ret) { 809 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus, 810 bits); 811 } 812 if (!ret) { 813 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, 814 bits); 815 } 816 if (!ret) { 817 ret = fdt_add_bignum(keydest, node, "rsa,c", c_factor, 818 bits); 819 } 820 if (!ret) { 821 ret = fdt_add_bignum(keydest, node, "rsa,np", np_factor, 822 bits); 823 } 824 if (!ret) { 825 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP, 826 info->name); 827 } 828 if (!ret && info->require_keys) { 829 ret = fdt_setprop_string(keydest, node, "required", 830 info->require_keys); 831 } 832 done: 833 BN_free(modulus); 834 BN_free(r_squared); 835 if (ret) 836 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; 837 err_get_params: 838 RSA_free(rsa); 839 err_get_pub_key: 840 if (info->engine_id) 841 rsa_engine_remove(e); 842 843 return ret; 844 } 845