rsa-sign.c 17 KB

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