rsa-sign.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. const char *key_pass;
  299. ENGINE *e;
  300. int ret;
  301. ENGINE_load_builtin_engines();
  302. e = ENGINE_by_id(engine_id);
  303. if (!e) {
  304. fprintf(stderr, "Engine isn't available\n");
  305. ret = -1;
  306. goto err_engine_by_id;
  307. }
  308. if (!ENGINE_init(e)) {
  309. fprintf(stderr, "Couldn't initialize engine\n");
  310. ret = -1;
  311. goto err_engine_init;
  312. }
  313. if (!ENGINE_set_default_RSA(e)) {
  314. fprintf(stderr, "Couldn't set engine as default for RSA\n");
  315. ret = -1;
  316. goto err_set_rsa;
  317. }
  318. key_pass = getenv("MKIMAGE_SIGN_PIN");
  319. if (key_pass) {
  320. if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
  321. fprintf(stderr, "Couldn't set PIN\n");
  322. ret = -1;
  323. goto err_set_pin;
  324. }
  325. }
  326. *pe = e;
  327. return 0;
  328. err_set_pin:
  329. err_set_rsa:
  330. ENGINE_finish(e);
  331. err_engine_init:
  332. ENGINE_free(e);
  333. err_engine_by_id:
  334. #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
  335. (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
  336. ENGINE_cleanup();
  337. #endif
  338. return ret;
  339. }
  340. static void rsa_remove(void)
  341. {
  342. #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
  343. (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
  344. CRYPTO_cleanup_all_ex_data();
  345. ERR_free_strings();
  346. #ifdef HAVE_ERR_REMOVE_THREAD_STATE
  347. ERR_remove_thread_state(NULL);
  348. #else
  349. ERR_remove_state(0);
  350. #endif
  351. EVP_cleanup();
  352. #endif
  353. }
  354. static void rsa_engine_remove(ENGINE *e)
  355. {
  356. if (e) {
  357. ENGINE_finish(e);
  358. ENGINE_free(e);
  359. }
  360. }
  361. static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo,
  362. struct checksum_algo *checksum_algo,
  363. const struct image_region region[], int region_count,
  364. uint8_t **sigp, uint *sig_size)
  365. {
  366. EVP_PKEY_CTX *ckey;
  367. EVP_MD_CTX *context;
  368. int ret = 0;
  369. size_t size;
  370. uint8_t *sig;
  371. int i;
  372. size = EVP_PKEY_size(pkey);
  373. sig = malloc(size);
  374. if (!sig) {
  375. fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
  376. size);
  377. ret = -ENOMEM;
  378. goto err_alloc;
  379. }
  380. context = EVP_MD_CTX_create();
  381. if (!context) {
  382. ret = rsa_err("EVP context creation failed");
  383. goto err_create;
  384. }
  385. EVP_MD_CTX_init(context);
  386. ckey = EVP_PKEY_CTX_new(pkey, NULL);
  387. if (!ckey) {
  388. ret = rsa_err("EVP key context creation failed");
  389. goto err_create;
  390. }
  391. if (EVP_DigestSignInit(context, &ckey,
  392. checksum_algo->calculate_sign(),
  393. NULL, pkey) <= 0) {
  394. ret = rsa_err("Signer setup failed");
  395. goto err_sign;
  396. }
  397. #ifdef CONFIG_FIT_RSASSA_PSS
  398. if (padding_algo && !strcmp(padding_algo->name, "pss")) {
  399. if (EVP_PKEY_CTX_set_rsa_padding(ckey,
  400. RSA_PKCS1_PSS_PADDING) <= 0) {
  401. ret = rsa_err("Signer padding setup failed");
  402. goto err_sign;
  403. }
  404. }
  405. #endif /* CONFIG_FIT_RSASSA_PSS */
  406. for (i = 0; i < region_count; i++) {
  407. if (!EVP_DigestSignUpdate(context, region[i].data,
  408. region[i].size)) {
  409. ret = rsa_err("Signing data failed");
  410. goto err_sign;
  411. }
  412. }
  413. if (!EVP_DigestSignFinal(context, sig, &size)) {
  414. ret = rsa_err("Could not obtain signature");
  415. goto err_sign;
  416. }
  417. #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
  418. (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
  419. EVP_MD_CTX_cleanup(context);
  420. #else
  421. EVP_MD_CTX_reset(context);
  422. #endif
  423. EVP_MD_CTX_destroy(context);
  424. debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey));
  425. *sigp = sig;
  426. *sig_size = size;
  427. return 0;
  428. err_sign:
  429. EVP_MD_CTX_destroy(context);
  430. err_create:
  431. free(sig);
  432. err_alloc:
  433. return ret;
  434. }
  435. int rsa_sign(struct image_sign_info *info,
  436. const struct image_region region[], int region_count,
  437. uint8_t **sigp, uint *sig_len)
  438. {
  439. EVP_PKEY *pkey = NULL;
  440. ENGINE *e = NULL;
  441. int ret;
  442. ret = rsa_init();
  443. if (ret)
  444. return ret;
  445. if (info->engine_id) {
  446. ret = rsa_engine_init(info->engine_id, &e);
  447. if (ret)
  448. goto err_engine;
  449. }
  450. ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile,
  451. e, &pkey);
  452. if (ret)
  453. goto err_priv;
  454. ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region,
  455. region_count, sigp, sig_len);
  456. if (ret)
  457. goto err_sign;
  458. EVP_PKEY_free(pkey);
  459. if (info->engine_id)
  460. rsa_engine_remove(e);
  461. rsa_remove();
  462. return ret;
  463. err_sign:
  464. EVP_PKEY_free(pkey);
  465. err_priv:
  466. if (info->engine_id)
  467. rsa_engine_remove(e);
  468. err_engine:
  469. rsa_remove();
  470. return ret;
  471. }
  472. /*
  473. * rsa_get_exponent(): - Get the public exponent from an RSA key
  474. */
  475. static int rsa_get_exponent(RSA *key, uint64_t *e)
  476. {
  477. int ret;
  478. BIGNUM *bn_te;
  479. const BIGNUM *key_e;
  480. uint64_t te;
  481. ret = -EINVAL;
  482. bn_te = NULL;
  483. if (!e)
  484. goto cleanup;
  485. RSA_get0_key(key, NULL, &key_e, NULL);
  486. if (BN_num_bits(key_e) > 64)
  487. goto cleanup;
  488. *e = BN_get_word(key_e);
  489. if (BN_num_bits(key_e) < 33) {
  490. ret = 0;
  491. goto cleanup;
  492. }
  493. bn_te = BN_dup(key_e);
  494. if (!bn_te)
  495. goto cleanup;
  496. if (!BN_rshift(bn_te, bn_te, 32))
  497. goto cleanup;
  498. if (!BN_mask_bits(bn_te, 32))
  499. goto cleanup;
  500. te = BN_get_word(bn_te);
  501. te <<= 32;
  502. *e |= te;
  503. ret = 0;
  504. cleanup:
  505. if (bn_te)
  506. BN_free(bn_te);
  507. return ret;
  508. }
  509. /*
  510. * rsa_get_params(): - Get the important parameters of an RSA public key
  511. */
  512. int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
  513. BIGNUM **modulusp, BIGNUM **r_squaredp)
  514. {
  515. BIGNUM *big1, *big2, *big32, *big2_32;
  516. BIGNUM *n, *r, *r_squared, *tmp;
  517. const BIGNUM *key_n;
  518. BN_CTX *bn_ctx = BN_CTX_new();
  519. int ret = 0;
  520. /* Initialize BIGNUMs */
  521. big1 = BN_new();
  522. big2 = BN_new();
  523. big32 = BN_new();
  524. r = BN_new();
  525. r_squared = BN_new();
  526. tmp = BN_new();
  527. big2_32 = BN_new();
  528. n = BN_new();
  529. if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
  530. !n) {
  531. fprintf(stderr, "Out of memory (bignum)\n");
  532. return -ENOMEM;
  533. }
  534. if (0 != rsa_get_exponent(key, exponent))
  535. ret = -1;
  536. RSA_get0_key(key, &key_n, NULL, NULL);
  537. if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
  538. !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
  539. ret = -1;
  540. /* big2_32 = 2^32 */
  541. if (!BN_exp(big2_32, big2, big32, bn_ctx))
  542. ret = -1;
  543. /* Calculate n0_inv = -1 / n[0] mod 2^32 */
  544. if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
  545. !BN_sub(tmp, big2_32, tmp))
  546. ret = -1;
  547. *n0_invp = BN_get_word(tmp);
  548. /* Calculate R = 2^(# of key bits) */
  549. if (!BN_set_word(tmp, BN_num_bits(n)) ||
  550. !BN_exp(r, big2, tmp, bn_ctx))
  551. ret = -1;
  552. /* Calculate r_squared = R^2 mod n */
  553. if (!BN_copy(r_squared, r) ||
  554. !BN_mul(tmp, r_squared, r, bn_ctx) ||
  555. !BN_mod(r_squared, tmp, n, bn_ctx))
  556. ret = -1;
  557. *modulusp = n;
  558. *r_squaredp = r_squared;
  559. BN_free(big1);
  560. BN_free(big2);
  561. BN_free(big32);
  562. BN_free(r);
  563. BN_free(tmp);
  564. BN_free(big2_32);
  565. if (ret) {
  566. fprintf(stderr, "Bignum operations failed\n");
  567. return -ENOMEM;
  568. }
  569. return ret;
  570. }
  571. int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
  572. {
  573. BIGNUM *modulus, *r_squared;
  574. uint64_t exponent;
  575. uint32_t n0_inv;
  576. int parent, node;
  577. char name[100];
  578. int ret;
  579. int bits;
  580. RSA *rsa;
  581. EVP_PKEY *pkey = NULL;
  582. ENGINE *e = NULL;
  583. debug("%s: Getting verification data\n", __func__);
  584. if (info->engine_id) {
  585. ret = rsa_engine_init(info->engine_id, &e);
  586. if (ret)
  587. return ret;
  588. }
  589. ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
  590. if (ret)
  591. goto err_get_pub_key;
  592. #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
  593. (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
  594. rsa = EVP_PKEY_get1_RSA(pkey);
  595. #else
  596. rsa = EVP_PKEY_get0_RSA(pkey);
  597. #endif
  598. ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
  599. if (ret)
  600. goto err_get_params;
  601. bits = BN_num_bits(modulus);
  602. parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
  603. if (parent == -FDT_ERR_NOTFOUND) {
  604. parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
  605. if (parent < 0) {
  606. ret = parent;
  607. if (ret != -FDT_ERR_NOSPACE) {
  608. fprintf(stderr, "Couldn't create signature node: %s\n",
  609. fdt_strerror(parent));
  610. }
  611. }
  612. }
  613. if (ret)
  614. goto done;
  615. /* Either create or overwrite the named key node */
  616. snprintf(name, sizeof(name), "key-%s", info->keyname);
  617. node = fdt_subnode_offset(keydest, parent, name);
  618. if (node == -FDT_ERR_NOTFOUND) {
  619. node = fdt_add_subnode(keydest, parent, name);
  620. if (node < 0) {
  621. ret = node;
  622. if (ret != -FDT_ERR_NOSPACE) {
  623. fprintf(stderr, "Could not create key subnode: %s\n",
  624. fdt_strerror(node));
  625. }
  626. }
  627. } else if (node < 0) {
  628. fprintf(stderr, "Cannot select keys parent: %s\n",
  629. fdt_strerror(node));
  630. ret = node;
  631. }
  632. if (!ret) {
  633. ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
  634. info->keyname);
  635. }
  636. if (!ret)
  637. ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
  638. if (!ret)
  639. ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
  640. if (!ret) {
  641. ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
  642. }
  643. if (!ret) {
  644. ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
  645. bits);
  646. }
  647. if (!ret) {
  648. ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
  649. bits);
  650. }
  651. if (!ret) {
  652. ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
  653. info->name);
  654. }
  655. if (!ret && info->require_keys) {
  656. ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
  657. info->require_keys);
  658. }
  659. done:
  660. BN_free(modulus);
  661. BN_free(r_squared);
  662. if (ret)
  663. ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
  664. err_get_params:
  665. #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
  666. (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
  667. RSA_free(rsa);
  668. #endif
  669. EVP_PKEY_free(pkey);
  670. err_get_pub_key:
  671. if (info->engine_id)
  672. rsa_engine_remove(e);
  673. return ret;
  674. }