rsa-sign.c 17 KB

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