/contrib/bind9/lib/dns/opensslrsa_link.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1442 lines · 1229 code · 155 blank · 58 comment · 315 complexity · b514c7ed8da431e1d5da2e500228c53e MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Principal Author: Brian Wellington
  19. * $Id$
  20. */
  21. #ifdef OPENSSL
  22. #include <config.h>
  23. #ifndef USE_EVP
  24. #if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
  25. #define USE_EVP 0
  26. #else
  27. #define USE_EVP 1
  28. #endif
  29. #endif
  30. #include <isc/entropy.h>
  31. #include <isc/md5.h>
  32. #include <isc/sha1.h>
  33. #include <isc/sha2.h>
  34. #include <isc/mem.h>
  35. #include <isc/string.h>
  36. #include <isc/util.h>
  37. #include <dst/result.h>
  38. #include "dst_internal.h"
  39. #include "dst_openssl.h"
  40. #include "dst_parse.h"
  41. #include <openssl/err.h>
  42. #include <openssl/objects.h>
  43. #include <openssl/rsa.h>
  44. #if OPENSSL_VERSION_NUMBER > 0x00908000L
  45. #include <openssl/bn.h>
  46. #endif
  47. #ifdef USE_ENGINE
  48. #include <openssl/engine.h>
  49. #endif
  50. /*
  51. * We don't use configure for windows so enforce the OpenSSL version
  52. * here. Unlike with configure we don't support overriding this test.
  53. */
  54. #ifdef WIN32
  55. #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
  56. OPENSSL_VERSION_NUMBER < 0x00908000L) || \
  57. OPENSSL_VERSION_NUMBER >= 0x0090804fL)
  58. #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
  59. #endif
  60. #endif
  61. /*
  62. * XXXMPA Temporarily disable RSA_BLINDING as it requires
  63. * good quality random data that cannot currently be guaranteed.
  64. * XXXMPA Find which versions of openssl use pseudo random data
  65. * and set RSA_FLAG_BLINDING for those.
  66. */
  67. #if 0
  68. #if OPENSSL_VERSION_NUMBER < 0x0090601fL
  69. #define SET_FLAGS(rsa) \
  70. do { \
  71. (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
  72. (rsa)->flags |= RSA_FLAG_BLINDING; \
  73. } while (0)
  74. #else
  75. #define SET_FLAGS(rsa) \
  76. do { \
  77. (rsa)->flags |= RSA_FLAG_BLINDING; \
  78. } while (0)
  79. #endif
  80. #endif
  81. #if OPENSSL_VERSION_NUMBER < 0x0090601fL
  82. #define SET_FLAGS(rsa) \
  83. do { \
  84. (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
  85. (rsa)->flags &= ~RSA_FLAG_BLINDING; \
  86. } while (0)
  87. #elif defined(RSA_FLAG_NO_BLINDING)
  88. #define SET_FLAGS(rsa) \
  89. do { \
  90. (rsa)->flags &= ~RSA_FLAG_BLINDING; \
  91. (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
  92. } while (0)
  93. #else
  94. #define SET_FLAGS(rsa) \
  95. do { \
  96. (rsa)->flags &= ~RSA_FLAG_BLINDING; \
  97. } while (0)
  98. #endif
  99. #define DST_RET(a) {ret = a; goto err;}
  100. static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
  101. static isc_result_t
  102. opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
  103. #if USE_EVP
  104. EVP_MD_CTX *evp_md_ctx;
  105. const EVP_MD *type = NULL;
  106. #endif
  107. UNUSED(key);
  108. REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
  109. dctx->key->key_alg == DST_ALG_RSASHA1 ||
  110. dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
  111. dctx->key->key_alg == DST_ALG_RSASHA256 ||
  112. dctx->key->key_alg == DST_ALG_RSASHA512);
  113. #if USE_EVP
  114. evp_md_ctx = EVP_MD_CTX_create();
  115. if (evp_md_ctx == NULL)
  116. return (ISC_R_NOMEMORY);
  117. switch (dctx->key->key_alg) {
  118. case DST_ALG_RSAMD5:
  119. type = EVP_md5(); /* MD5 + RSA */
  120. break;
  121. case DST_ALG_RSASHA1:
  122. case DST_ALG_NSEC3RSASHA1:
  123. type = EVP_sha1(); /* SHA1 + RSA */
  124. break;
  125. #ifdef HAVE_EVP_SHA256
  126. case DST_ALG_RSASHA256:
  127. type = EVP_sha256(); /* SHA256 + RSA */
  128. break;
  129. #endif
  130. #ifdef HAVE_EVP_SHA512
  131. case DST_ALG_RSASHA512:
  132. type = EVP_sha512();
  133. break;
  134. #endif
  135. default:
  136. INSIST(0);
  137. }
  138. if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
  139. EVP_MD_CTX_destroy(evp_md_ctx);
  140. return (ISC_R_FAILURE);
  141. }
  142. dctx->ctxdata.evp_md_ctx = evp_md_ctx;
  143. #else
  144. switch (dctx->key->key_alg) {
  145. case DST_ALG_RSAMD5:
  146. {
  147. isc_md5_t *md5ctx;
  148. md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
  149. if (md5ctx == NULL)
  150. return (ISC_R_NOMEMORY);
  151. isc_md5_init(md5ctx);
  152. dctx->ctxdata.md5ctx = md5ctx;
  153. }
  154. break;
  155. case DST_ALG_RSASHA1:
  156. case DST_ALG_NSEC3RSASHA1:
  157. {
  158. isc_sha1_t *sha1ctx;
  159. sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
  160. if (sha1ctx == NULL)
  161. return (ISC_R_NOMEMORY);
  162. isc_sha1_init(sha1ctx);
  163. dctx->ctxdata.sha1ctx = sha1ctx;
  164. }
  165. break;
  166. case DST_ALG_RSASHA256:
  167. {
  168. isc_sha256_t *sha256ctx;
  169. sha256ctx = isc_mem_get(dctx->mctx,
  170. sizeof(isc_sha256_t));
  171. if (sha256ctx == NULL)
  172. return (ISC_R_NOMEMORY);
  173. isc_sha256_init(sha256ctx);
  174. dctx->ctxdata.sha256ctx = sha256ctx;
  175. }
  176. break;
  177. case DST_ALG_RSASHA512:
  178. {
  179. isc_sha512_t *sha512ctx;
  180. sha512ctx = isc_mem_get(dctx->mctx,
  181. sizeof(isc_sha512_t));
  182. if (sha512ctx == NULL)
  183. return (ISC_R_NOMEMORY);
  184. isc_sha512_init(sha512ctx);
  185. dctx->ctxdata.sha512ctx = sha512ctx;
  186. }
  187. break;
  188. default:
  189. INSIST(0);
  190. }
  191. #endif
  192. return (ISC_R_SUCCESS);
  193. }
  194. static void
  195. opensslrsa_destroyctx(dst_context_t *dctx) {
  196. #if USE_EVP
  197. EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
  198. #endif
  199. REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
  200. dctx->key->key_alg == DST_ALG_RSASHA1 ||
  201. dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
  202. dctx->key->key_alg == DST_ALG_RSASHA256 ||
  203. dctx->key->key_alg == DST_ALG_RSASHA512);
  204. #if USE_EVP
  205. if (evp_md_ctx != NULL) {
  206. EVP_MD_CTX_destroy(evp_md_ctx);
  207. dctx->ctxdata.evp_md_ctx = NULL;
  208. }
  209. #else
  210. switch (dctx->key->key_alg) {
  211. case DST_ALG_RSAMD5:
  212. {
  213. isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
  214. if (md5ctx != NULL) {
  215. isc_md5_invalidate(md5ctx);
  216. isc_mem_put(dctx->mctx, md5ctx,
  217. sizeof(isc_md5_t));
  218. dctx->ctxdata.md5ctx = NULL;
  219. }
  220. }
  221. break;
  222. case DST_ALG_RSASHA1:
  223. case DST_ALG_NSEC3RSASHA1:
  224. {
  225. isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
  226. if (sha1ctx != NULL) {
  227. isc_sha1_invalidate(sha1ctx);
  228. isc_mem_put(dctx->mctx, sha1ctx,
  229. sizeof(isc_sha1_t));
  230. dctx->ctxdata.sha1ctx = NULL;
  231. }
  232. }
  233. break;
  234. case DST_ALG_RSASHA256:
  235. {
  236. isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
  237. if (sha256ctx != NULL) {
  238. isc_sha256_invalidate(sha256ctx);
  239. isc_mem_put(dctx->mctx, sha256ctx,
  240. sizeof(isc_sha256_t));
  241. dctx->ctxdata.sha256ctx = NULL;
  242. }
  243. }
  244. break;
  245. case DST_ALG_RSASHA512:
  246. {
  247. isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
  248. if (sha512ctx != NULL) {
  249. isc_sha512_invalidate(sha512ctx);
  250. isc_mem_put(dctx->mctx, sha512ctx,
  251. sizeof(isc_sha512_t));
  252. dctx->ctxdata.sha512ctx = NULL;
  253. }
  254. }
  255. break;
  256. default:
  257. INSIST(0);
  258. }
  259. #endif
  260. }
  261. static isc_result_t
  262. opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
  263. #if USE_EVP
  264. EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
  265. #endif
  266. REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
  267. dctx->key->key_alg == DST_ALG_RSASHA1 ||
  268. dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
  269. dctx->key->key_alg == DST_ALG_RSASHA256 ||
  270. dctx->key->key_alg == DST_ALG_RSASHA512);
  271. #if USE_EVP
  272. if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
  273. return (ISC_R_FAILURE);
  274. }
  275. #else
  276. switch (dctx->key->key_alg) {
  277. case DST_ALG_RSAMD5:
  278. {
  279. isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
  280. isc_md5_update(md5ctx, data->base, data->length);
  281. }
  282. break;
  283. case DST_ALG_RSASHA1:
  284. case DST_ALG_NSEC3RSASHA1:
  285. {
  286. isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
  287. isc_sha1_update(sha1ctx, data->base, data->length);
  288. }
  289. break;
  290. case DST_ALG_RSASHA256:
  291. {
  292. isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
  293. isc_sha256_update(sha256ctx, data->base, data->length);
  294. }
  295. break;
  296. case DST_ALG_RSASHA512:
  297. {
  298. isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
  299. isc_sha512_update(sha512ctx, data->base, data->length);
  300. }
  301. break;
  302. default:
  303. INSIST(0);
  304. }
  305. #endif
  306. return (ISC_R_SUCCESS);
  307. }
  308. #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
  309. /*
  310. * Digest prefixes from RFC 5702.
  311. */
  312. static unsigned char sha256_prefix[] =
  313. { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
  314. 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
  315. static unsigned char sha512_prefix[] =
  316. { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
  317. 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
  318. #define PREFIXLEN sizeof(sha512_prefix)
  319. #else
  320. #define PREFIXLEN 0
  321. #endif
  322. static isc_result_t
  323. opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
  324. dst_key_t *key = dctx->key;
  325. isc_region_t r;
  326. unsigned int siglen = 0;
  327. #if USE_EVP
  328. EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
  329. EVP_PKEY *pkey = key->keydata.pkey;
  330. #else
  331. RSA *rsa = key->keydata.rsa;
  332. /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
  333. unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
  334. int status;
  335. int type = 0;
  336. unsigned int digestlen = 0;
  337. char *message;
  338. unsigned long err;
  339. const char* file;
  340. int line;
  341. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  342. unsigned int prefixlen = 0;
  343. const unsigned char *prefix = NULL;
  344. #endif
  345. #endif
  346. REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
  347. dctx->key->key_alg == DST_ALG_RSASHA1 ||
  348. dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
  349. dctx->key->key_alg == DST_ALG_RSASHA256 ||
  350. dctx->key->key_alg == DST_ALG_RSASHA512);
  351. isc_buffer_availableregion(sig, &r);
  352. #if USE_EVP
  353. if (r.length < (unsigned int) EVP_PKEY_size(pkey))
  354. return (ISC_R_NOSPACE);
  355. if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
  356. return (ISC_R_FAILURE);
  357. }
  358. #else
  359. if (r.length < (unsigned int) RSA_size(rsa))
  360. return (ISC_R_NOSPACE);
  361. switch (dctx->key->key_alg) {
  362. case DST_ALG_RSAMD5:
  363. {
  364. isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
  365. isc_md5_final(md5ctx, digest);
  366. type = NID_md5;
  367. digestlen = ISC_MD5_DIGESTLENGTH;
  368. }
  369. break;
  370. case DST_ALG_RSASHA1:
  371. case DST_ALG_NSEC3RSASHA1:
  372. {
  373. isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
  374. isc_sha1_final(sha1ctx, digest);
  375. type = NID_sha1;
  376. digestlen = ISC_SHA1_DIGESTLENGTH;
  377. }
  378. break;
  379. case DST_ALG_RSASHA256:
  380. {
  381. isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
  382. isc_sha256_final(digest, sha256ctx);
  383. digestlen = ISC_SHA256_DIGESTLENGTH;
  384. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  385. prefix = sha256_prefix;
  386. prefixlen = sizeof(sha256_prefix);
  387. #else
  388. type = NID_sha256;
  389. #endif
  390. }
  391. break;
  392. case DST_ALG_RSASHA512:
  393. {
  394. isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
  395. isc_sha512_final(digest, sha512ctx);
  396. digestlen = ISC_SHA512_DIGESTLENGTH;
  397. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  398. prefix = sha512_prefix;
  399. prefixlen = sizeof(sha512_prefix);
  400. #else
  401. type = NID_sha512;
  402. #endif
  403. }
  404. break;
  405. default:
  406. INSIST(0);
  407. }
  408. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  409. switch (dctx->key->key_alg) {
  410. case DST_ALG_RSAMD5:
  411. case DST_ALG_RSASHA1:
  412. case DST_ALG_NSEC3RSASHA1:
  413. INSIST(type != 0);
  414. status = RSA_sign(type, digest, digestlen, r.base,
  415. &siglen, rsa);
  416. break;
  417. case DST_ALG_RSASHA256:
  418. case DST_ALG_RSASHA512:
  419. INSIST(prefix != NULL);
  420. INSIST(prefixlen != 0);
  421. INSIST(prefixlen + digestlen <= sizeof(digest));
  422. memmove(digest + prefixlen, digest, digestlen);
  423. memcpy(digest, prefix, prefixlen);
  424. status = RSA_private_encrypt(digestlen + prefixlen,
  425. digest, r.base, rsa,
  426. RSA_PKCS1_PADDING);
  427. if (status < 0)
  428. status = 0;
  429. else
  430. siglen = status;
  431. break;
  432. default:
  433. INSIST(0);
  434. }
  435. #else
  436. INSIST(type != 0);
  437. status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
  438. #endif
  439. if (status == 0) {
  440. err = ERR_peek_error_line(&file, &line);
  441. if (err != 0U) {
  442. message = ERR_error_string(err, NULL);
  443. }
  444. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  445. }
  446. #endif
  447. isc_buffer_add(sig, siglen);
  448. return (ISC_R_SUCCESS);
  449. }
  450. static isc_result_t
  451. opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
  452. dst_key_t *key = dctx->key;
  453. int status = 0;
  454. #if USE_EVP
  455. EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
  456. EVP_PKEY *pkey = key->keydata.pkey;
  457. #else
  458. /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
  459. unsigned char digest[ISC_SHA512_DIGESTLENGTH];
  460. int type = 0;
  461. unsigned int digestlen = 0;
  462. RSA *rsa = key->keydata.rsa;
  463. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  464. unsigned int prefixlen = 0;
  465. const unsigned char *prefix = NULL;
  466. #endif
  467. #endif
  468. REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
  469. dctx->key->key_alg == DST_ALG_RSASHA1 ||
  470. dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
  471. dctx->key->key_alg == DST_ALG_RSASHA256 ||
  472. dctx->key->key_alg == DST_ALG_RSASHA512);
  473. #if USE_EVP
  474. status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
  475. #else
  476. switch (dctx->key->key_alg) {
  477. case DST_ALG_RSAMD5:
  478. {
  479. isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
  480. isc_md5_final(md5ctx, digest);
  481. type = NID_md5;
  482. digestlen = ISC_MD5_DIGESTLENGTH;
  483. }
  484. break;
  485. case DST_ALG_RSASHA1:
  486. case DST_ALG_NSEC3RSASHA1:
  487. {
  488. isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
  489. isc_sha1_final(sha1ctx, digest);
  490. type = NID_sha1;
  491. digestlen = ISC_SHA1_DIGESTLENGTH;
  492. }
  493. break;
  494. case DST_ALG_RSASHA256:
  495. {
  496. isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
  497. isc_sha256_final(digest, sha256ctx);
  498. digestlen = ISC_SHA256_DIGESTLENGTH;
  499. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  500. prefix = sha256_prefix;
  501. prefixlen = sizeof(sha256_prefix);
  502. #else
  503. type = NID_sha256;
  504. #endif
  505. }
  506. break;
  507. case DST_ALG_RSASHA512:
  508. {
  509. isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
  510. isc_sha512_final(digest, sha512ctx);
  511. digestlen = ISC_SHA512_DIGESTLENGTH;
  512. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  513. prefix = sha512_prefix;
  514. prefixlen = sizeof(sha512_prefix);
  515. #else
  516. type = NID_sha512;
  517. #endif
  518. }
  519. break;
  520. default:
  521. INSIST(0);
  522. }
  523. if (sig->length != (unsigned int) RSA_size(rsa))
  524. return (DST_R_VERIFYFAILURE);
  525. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  526. switch (dctx->key->key_alg) {
  527. case DST_ALG_RSAMD5:
  528. case DST_ALG_RSASHA1:
  529. case DST_ALG_NSEC3RSASHA1:
  530. INSIST(type != 0);
  531. status = RSA_verify(type, digest, digestlen, sig->base,
  532. RSA_size(rsa), rsa);
  533. break;
  534. case DST_ALG_RSASHA256:
  535. case DST_ALG_RSASHA512:
  536. {
  537. /*
  538. * 1024 is big enough for all valid RSA bit sizes
  539. * for use with DNSSEC.
  540. */
  541. unsigned char original[PREFIXLEN + 1024];
  542. INSIST(prefix != NULL);
  543. INSIST(prefixlen != 0U);
  544. if (RSA_size(rsa) > (int)sizeof(original))
  545. return (DST_R_VERIFYFAILURE);
  546. status = RSA_public_decrypt(sig->length, sig->base,
  547. original, rsa,
  548. RSA_PKCS1_PADDING);
  549. if (status <= 0)
  550. return (DST_R_VERIFYFAILURE);
  551. if (status != (int)(prefixlen + digestlen))
  552. return (DST_R_VERIFYFAILURE);
  553. if (memcmp(original, prefix, prefixlen))
  554. return (DST_R_VERIFYFAILURE);
  555. if (memcmp(original + prefixlen, digest, digestlen))
  556. return (DST_R_VERIFYFAILURE);
  557. status = 1;
  558. }
  559. break;
  560. default:
  561. INSIST(0);
  562. }
  563. #else
  564. INSIST(type != 0);
  565. status = RSA_verify(type, digest, digestlen, sig->base,
  566. RSA_size(rsa), rsa);
  567. #endif
  568. #endif
  569. if (status != 1)
  570. return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
  571. return (ISC_R_SUCCESS);
  572. }
  573. static isc_boolean_t
  574. opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
  575. int status;
  576. RSA *rsa1 = NULL, *rsa2 = NULL;
  577. #if USE_EVP
  578. EVP_PKEY *pkey1, *pkey2;
  579. #endif
  580. #if USE_EVP
  581. pkey1 = key1->keydata.pkey;
  582. pkey2 = key2->keydata.pkey;
  583. /*
  584. * The pkey reference will keep these around after
  585. * the RSA_free() call.
  586. */
  587. if (pkey1 != NULL) {
  588. rsa1 = EVP_PKEY_get1_RSA(pkey1);
  589. RSA_free(rsa1);
  590. }
  591. if (pkey2 != NULL) {
  592. rsa2 = EVP_PKEY_get1_RSA(pkey2);
  593. RSA_free(rsa2);
  594. }
  595. #else
  596. rsa1 = key1->keydata.rsa;
  597. rsa2 = key2->keydata.rsa;
  598. #endif
  599. if (rsa1 == NULL && rsa2 == NULL)
  600. return (ISC_TRUE);
  601. else if (rsa1 == NULL || rsa2 == NULL)
  602. return (ISC_FALSE);
  603. status = BN_cmp(rsa1->n, rsa2->n) ||
  604. BN_cmp(rsa1->e, rsa2->e);
  605. if (status != 0)
  606. return (ISC_FALSE);
  607. #if USE_EVP
  608. if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
  609. (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
  610. if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
  611. (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
  612. return (ISC_FALSE);
  613. /*
  614. * Can't compare private parameters, BTW does it make sense?
  615. */
  616. return (ISC_TRUE);
  617. }
  618. #endif
  619. if (rsa1->d != NULL || rsa2->d != NULL) {
  620. if (rsa1->d == NULL || rsa2->d == NULL)
  621. return (ISC_FALSE);
  622. status = BN_cmp(rsa1->d, rsa2->d) ||
  623. BN_cmp(rsa1->p, rsa2->p) ||
  624. BN_cmp(rsa1->q, rsa2->q);
  625. if (status != 0)
  626. return (ISC_FALSE);
  627. }
  628. return (ISC_TRUE);
  629. }
  630. #if OPENSSL_VERSION_NUMBER > 0x00908000L
  631. static int
  632. progress_cb(int p, int n, BN_GENCB *cb)
  633. {
  634. union {
  635. void *dptr;
  636. void (*fptr)(int);
  637. } u;
  638. UNUSED(n);
  639. u.dptr = cb->arg;
  640. if (u.fptr != NULL)
  641. u.fptr(p);
  642. return (1);
  643. }
  644. #endif
  645. static isc_result_t
  646. opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
  647. #if OPENSSL_VERSION_NUMBER > 0x00908000L
  648. BN_GENCB cb;
  649. union {
  650. void *dptr;
  651. void (*fptr)(int);
  652. } u;
  653. RSA *rsa = RSA_new();
  654. BIGNUM *e = BN_new();
  655. #if USE_EVP
  656. EVP_PKEY *pkey = EVP_PKEY_new();
  657. #endif
  658. if (rsa == NULL || e == NULL)
  659. goto err;
  660. #if USE_EVP
  661. if (pkey == NULL)
  662. goto err;
  663. if (!EVP_PKEY_set1_RSA(pkey, rsa))
  664. goto err;
  665. #endif
  666. if (exp == 0) {
  667. /* RSA_F4 0x10001 */
  668. BN_set_bit(e, 0);
  669. BN_set_bit(e, 16);
  670. } else {
  671. /* F5 0x100000001 */
  672. BN_set_bit(e, 0);
  673. BN_set_bit(e, 32);
  674. }
  675. if (callback == NULL) {
  676. BN_GENCB_set_old(&cb, NULL, NULL);
  677. } else {
  678. u.fptr = callback;
  679. BN_GENCB_set(&cb, &progress_cb, u.dptr);
  680. }
  681. if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
  682. BN_free(e);
  683. SET_FLAGS(rsa);
  684. #if USE_EVP
  685. key->keydata.pkey = pkey;
  686. RSA_free(rsa);
  687. #else
  688. key->keydata.rsa = rsa;
  689. #endif
  690. return (ISC_R_SUCCESS);
  691. }
  692. err:
  693. #if USE_EVP
  694. if (pkey != NULL)
  695. EVP_PKEY_free(pkey);
  696. #endif
  697. if (e != NULL)
  698. BN_free(e);
  699. if (rsa != NULL)
  700. RSA_free(rsa);
  701. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  702. #else
  703. RSA *rsa;
  704. unsigned long e;
  705. #if USE_EVP
  706. EVP_PKEY *pkey = EVP_PKEY_new();
  707. UNUSED(callback);
  708. if (pkey == NULL)
  709. return (ISC_R_NOMEMORY);
  710. #else
  711. UNUSED(callback);
  712. #endif
  713. if (exp == 0)
  714. e = RSA_F4;
  715. else
  716. e = 0x40000003;
  717. rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
  718. if (rsa == NULL) {
  719. #if USE_EVP
  720. EVP_PKEY_free(pkey);
  721. #endif
  722. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  723. }
  724. SET_FLAGS(rsa);
  725. #if USE_EVP
  726. if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
  727. EVP_PKEY_free(pkey);
  728. RSA_free(rsa);
  729. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  730. }
  731. key->keydata.pkey = pkey;
  732. RSA_free(rsa);
  733. #else
  734. key->keydata.rsa = rsa;
  735. #endif
  736. return (ISC_R_SUCCESS);
  737. #endif
  738. }
  739. static isc_boolean_t
  740. opensslrsa_isprivate(const dst_key_t *key) {
  741. #if USE_EVP
  742. RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
  743. INSIST(rsa != NULL);
  744. RSA_free(rsa);
  745. /* key->keydata.pkey still has a reference so rsa is still valid. */
  746. #else
  747. RSA *rsa = key->keydata.rsa;
  748. #endif
  749. if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
  750. return (ISC_TRUE);
  751. return (ISC_TF(rsa != NULL && rsa->d != NULL));
  752. }
  753. static void
  754. opensslrsa_destroy(dst_key_t *key) {
  755. #if USE_EVP
  756. EVP_PKEY *pkey = key->keydata.pkey;
  757. EVP_PKEY_free(pkey);
  758. key->keydata.pkey = NULL;
  759. #else
  760. RSA *rsa = key->keydata.rsa;
  761. RSA_free(rsa);
  762. key->keydata.rsa = NULL;
  763. #endif
  764. }
  765. static isc_result_t
  766. opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
  767. isc_region_t r;
  768. unsigned int e_bytes;
  769. unsigned int mod_bytes;
  770. isc_result_t ret;
  771. RSA *rsa;
  772. #if USE_EVP
  773. EVP_PKEY *pkey;
  774. #endif
  775. #if USE_EVP
  776. REQUIRE(key->keydata.pkey != NULL);
  777. #else
  778. REQUIRE(key->keydata.rsa != NULL);
  779. #endif
  780. #if USE_EVP
  781. pkey = key->keydata.pkey;
  782. rsa = EVP_PKEY_get1_RSA(pkey);
  783. if (rsa == NULL)
  784. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  785. #else
  786. rsa = key->keydata.rsa;
  787. #endif
  788. isc_buffer_availableregion(data, &r);
  789. e_bytes = BN_num_bytes(rsa->e);
  790. mod_bytes = BN_num_bytes(rsa->n);
  791. if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
  792. if (r.length < 1)
  793. DST_RET(ISC_R_NOSPACE);
  794. isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
  795. isc_region_consume(&r, 1);
  796. } else {
  797. if (r.length < 3)
  798. DST_RET(ISC_R_NOSPACE);
  799. isc_buffer_putuint8(data, 0);
  800. isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
  801. isc_region_consume(&r, 3);
  802. }
  803. if (r.length < e_bytes + mod_bytes)
  804. DST_RET(ISC_R_NOSPACE);
  805. BN_bn2bin(rsa->e, r.base);
  806. isc_region_consume(&r, e_bytes);
  807. BN_bn2bin(rsa->n, r.base);
  808. isc_buffer_add(data, e_bytes + mod_bytes);
  809. ret = ISC_R_SUCCESS;
  810. err:
  811. #if USE_EVP
  812. if (rsa != NULL)
  813. RSA_free(rsa);
  814. #endif
  815. return (ret);
  816. }
  817. static isc_result_t
  818. opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
  819. RSA *rsa;
  820. isc_region_t r;
  821. unsigned int e_bytes;
  822. #if USE_EVP
  823. EVP_PKEY *pkey;
  824. #endif
  825. isc_buffer_remainingregion(data, &r);
  826. if (r.length == 0)
  827. return (ISC_R_SUCCESS);
  828. rsa = RSA_new();
  829. if (rsa == NULL)
  830. return (dst__openssl_toresult(ISC_R_NOMEMORY));
  831. SET_FLAGS(rsa);
  832. if (r.length < 1) {
  833. RSA_free(rsa);
  834. return (DST_R_INVALIDPUBLICKEY);
  835. }
  836. e_bytes = *r.base++;
  837. r.length--;
  838. if (e_bytes == 0) {
  839. if (r.length < 2) {
  840. RSA_free(rsa);
  841. return (DST_R_INVALIDPUBLICKEY);
  842. }
  843. e_bytes = ((*r.base++) << 8);
  844. e_bytes += *r.base++;
  845. r.length -= 2;
  846. }
  847. if (r.length < e_bytes) {
  848. RSA_free(rsa);
  849. return (DST_R_INVALIDPUBLICKEY);
  850. }
  851. rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
  852. r.base += e_bytes;
  853. r.length -= e_bytes;
  854. rsa->n = BN_bin2bn(r.base, r.length, NULL);
  855. key->key_size = BN_num_bits(rsa->n);
  856. isc_buffer_forward(data, r.length);
  857. #if USE_EVP
  858. pkey = EVP_PKEY_new();
  859. if (pkey == NULL) {
  860. RSA_free(rsa);
  861. return (ISC_R_NOMEMORY);
  862. }
  863. if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
  864. EVP_PKEY_free(pkey);
  865. RSA_free(rsa);
  866. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  867. }
  868. key->keydata.pkey = pkey;
  869. RSA_free(rsa);
  870. #else
  871. key->keydata.rsa = rsa;
  872. #endif
  873. return (ISC_R_SUCCESS);
  874. }
  875. static isc_result_t
  876. opensslrsa_tofile(const dst_key_t *key, const char *directory) {
  877. int i;
  878. RSA *rsa;
  879. dst_private_t priv;
  880. unsigned char *bufs[8];
  881. isc_result_t result;
  882. #if USE_EVP
  883. if (key->keydata.pkey == NULL)
  884. return (DST_R_NULLKEY);
  885. rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
  886. if (rsa == NULL)
  887. return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  888. #else
  889. if (key->keydata.rsa == NULL)
  890. return (DST_R_NULLKEY);
  891. rsa = key->keydata.rsa;
  892. #endif
  893. for (i = 0; i < 8; i++) {
  894. bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
  895. if (bufs[i] == NULL) {
  896. result = ISC_R_NOMEMORY;
  897. goto fail;
  898. }
  899. }
  900. i = 0;
  901. priv.elements[i].tag = TAG_RSA_MODULUS;
  902. priv.elements[i].length = BN_num_bytes(rsa->n);
  903. BN_bn2bin(rsa->n, bufs[i]);
  904. priv.elements[i].data = bufs[i];
  905. i++;
  906. priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
  907. priv.elements[i].length = BN_num_bytes(rsa->e);
  908. BN_bn2bin(rsa->e, bufs[i]);
  909. priv.elements[i].data = bufs[i];
  910. i++;
  911. if (rsa->d != NULL) {
  912. priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
  913. priv.elements[i].length = BN_num_bytes(rsa->d);
  914. BN_bn2bin(rsa->d, bufs[i]);
  915. priv.elements[i].data = bufs[i];
  916. i++;
  917. }
  918. if (rsa->p != NULL) {
  919. priv.elements[i].tag = TAG_RSA_PRIME1;
  920. priv.elements[i].length = BN_num_bytes(rsa->p);
  921. BN_bn2bin(rsa->p, bufs[i]);
  922. priv.elements[i].data = bufs[i];
  923. i++;
  924. }
  925. if (rsa->q != NULL) {
  926. priv.elements[i].tag = TAG_RSA_PRIME2;
  927. priv.elements[i].length = BN_num_bytes(rsa->q);
  928. BN_bn2bin(rsa->q, bufs[i]);
  929. priv.elements[i].data = bufs[i];
  930. i++;
  931. }
  932. if (rsa->dmp1 != NULL) {
  933. priv.elements[i].tag = TAG_RSA_EXPONENT1;
  934. priv.elements[i].length = BN_num_bytes(rsa->dmp1);
  935. BN_bn2bin(rsa->dmp1, bufs[i]);
  936. priv.elements[i].data = bufs[i];
  937. i++;
  938. }
  939. if (rsa->dmq1 != NULL) {
  940. priv.elements[i].tag = TAG_RSA_EXPONENT2;
  941. priv.elements[i].length = BN_num_bytes(rsa->dmq1);
  942. BN_bn2bin(rsa->dmq1, bufs[i]);
  943. priv.elements[i].data = bufs[i];
  944. i++;
  945. }
  946. if (rsa->iqmp != NULL) {
  947. priv.elements[i].tag = TAG_RSA_COEFFICIENT;
  948. priv.elements[i].length = BN_num_bytes(rsa->iqmp);
  949. BN_bn2bin(rsa->iqmp, bufs[i]);
  950. priv.elements[i].data = bufs[i];
  951. i++;
  952. }
  953. if (key->engine != NULL) {
  954. priv.elements[i].tag = TAG_RSA_ENGINE;
  955. priv.elements[i].length = strlen(key->engine) + 1;
  956. priv.elements[i].data = (unsigned char *)key->engine;
  957. i++;
  958. }
  959. if (key->label != NULL) {
  960. priv.elements[i].tag = TAG_RSA_LABEL;
  961. priv.elements[i].length = strlen(key->label) + 1;
  962. priv.elements[i].data = (unsigned char *)key->label;
  963. i++;
  964. }
  965. priv.nelements = i;
  966. result = dst__privstruct_writefile(key, &priv, directory);
  967. fail:
  968. #if USE_EVP
  969. RSA_free(rsa);
  970. #endif
  971. for (i = 0; i < 8; i++) {
  972. if (bufs[i] == NULL)
  973. break;
  974. isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
  975. }
  976. return (result);
  977. }
  978. static isc_result_t
  979. rsa_check(RSA *rsa, RSA *pub)
  980. {
  981. /* Public parameters should be the same but if they are not set
  982. * copy them from the public key. */
  983. if (pub != NULL) {
  984. if (rsa->n != NULL) {
  985. if (BN_cmp(rsa->n, pub->n) != 0)
  986. return (DST_R_INVALIDPRIVATEKEY);
  987. } else {
  988. rsa->n = pub->n;
  989. pub->n = NULL;
  990. }
  991. if (rsa->e != NULL) {
  992. if (BN_cmp(rsa->e, pub->e) != 0)
  993. return (DST_R_INVALIDPRIVATEKEY);
  994. } else {
  995. rsa->e = pub->e;
  996. pub->e = NULL;
  997. }
  998. }
  999. if (rsa->n == NULL || rsa->e == NULL)
  1000. return (DST_R_INVALIDPRIVATEKEY);
  1001. return (ISC_R_SUCCESS);
  1002. }
  1003. static isc_result_t
  1004. opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
  1005. dst_private_t priv;
  1006. isc_result_t ret;
  1007. int i;
  1008. RSA *rsa = NULL, *pubrsa = NULL;
  1009. #ifdef USE_ENGINE
  1010. ENGINE *e = NULL;
  1011. #endif
  1012. isc_mem_t *mctx = key->mctx;
  1013. const char *engine = NULL, *label = NULL;
  1014. #if defined(USE_ENGINE) || USE_EVP
  1015. EVP_PKEY *pkey = NULL;
  1016. #endif
  1017. #if USE_EVP
  1018. if (pub != NULL && pub->keydata.pkey != NULL)
  1019. pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
  1020. #else
  1021. if (pub != NULL && pub->keydata.rsa != NULL) {
  1022. pubrsa = pub->keydata.rsa;
  1023. pub->keydata.rsa = NULL;
  1024. }
  1025. #endif
  1026. /* read private key file */
  1027. ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
  1028. if (ret != ISC_R_SUCCESS)
  1029. return (ret);
  1030. for (i = 0; i < priv.nelements; i++) {
  1031. switch (priv.elements[i].tag) {
  1032. case TAG_RSA_ENGINE:
  1033. engine = (char *)priv.elements[i].data;
  1034. break;
  1035. case TAG_RSA_LABEL:
  1036. label = (char *)priv.elements[i].data;
  1037. break;
  1038. default:
  1039. break;
  1040. }
  1041. }
  1042. /*
  1043. * Is this key is stored in a HSM?
  1044. * See if we can fetch it.
  1045. */
  1046. if (label != NULL) {
  1047. #ifdef USE_ENGINE
  1048. if (engine == NULL)
  1049. DST_RET(DST_R_NOENGINE);
  1050. e = dst__openssl_getengine(engine);
  1051. if (e == NULL)
  1052. DST_RET(DST_R_NOENGINE);
  1053. pkey = ENGINE_load_private_key(e, label, NULL, NULL);
  1054. if (pkey == NULL) {
  1055. /* ERR_print_errors_fp(stderr); */
  1056. DST_RET(ISC_R_NOTFOUND);
  1057. }
  1058. key->engine = isc_mem_strdup(key->mctx, engine);
  1059. if (key->engine == NULL)
  1060. DST_RET(ISC_R_NOMEMORY);
  1061. key->label = isc_mem_strdup(key->mctx, label);
  1062. if (key->label == NULL)
  1063. DST_RET(ISC_R_NOMEMORY);
  1064. rsa = EVP_PKEY_get1_RSA(pkey);
  1065. if (rsa == NULL)
  1066. DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  1067. if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
  1068. DST_RET(DST_R_INVALIDPRIVATEKEY);
  1069. if (pubrsa != NULL)
  1070. RSA_free(pubrsa);
  1071. key->key_size = EVP_PKEY_bits(pkey);
  1072. #if USE_EVP
  1073. key->keydata.pkey = pkey;
  1074. RSA_free(rsa);
  1075. #else
  1076. key->keydata.rsa = rsa;
  1077. EVP_PKEY_free(pkey);
  1078. #endif
  1079. dst__privstruct_free(&priv, mctx);
  1080. memset(&priv, 0, sizeof(priv));
  1081. return (ISC_R_SUCCESS);
  1082. #else
  1083. DST_RET(DST_R_NOENGINE);
  1084. #endif
  1085. }
  1086. rsa = RSA_new();
  1087. if (rsa == NULL)
  1088. DST_RET(ISC_R_NOMEMORY);
  1089. SET_FLAGS(rsa);
  1090. #if USE_EVP
  1091. pkey = EVP_PKEY_new();
  1092. if (pkey == NULL)
  1093. DST_RET(ISC_R_NOMEMORY);
  1094. if (!EVP_PKEY_set1_RSA(pkey, rsa))
  1095. DST_RET(ISC_R_FAILURE);
  1096. key->keydata.pkey = pkey;
  1097. #else
  1098. key->keydata.rsa = rsa;
  1099. #endif
  1100. for (i = 0; i < priv.nelements; i++) {
  1101. BIGNUM *bn;
  1102. switch (priv.elements[i].tag) {
  1103. case TAG_RSA_ENGINE:
  1104. continue;
  1105. case TAG_RSA_LABEL:
  1106. continue;
  1107. case TAG_RSA_PIN:
  1108. continue;
  1109. default:
  1110. bn = BN_bin2bn(priv.elements[i].data,
  1111. priv.elements[i].length, NULL);
  1112. if (bn == NULL)
  1113. DST_RET(ISC_R_NOMEMORY);
  1114. }
  1115. switch (priv.elements[i].tag) {
  1116. case TAG_RSA_MODULUS:
  1117. rsa->n = bn;
  1118. break;
  1119. case TAG_RSA_PUBLICEXPONENT:
  1120. rsa->e = bn;
  1121. break;
  1122. case TAG_RSA_PRIVATEEXPONENT:
  1123. rsa->d = bn;
  1124. break;
  1125. case TAG_RSA_PRIME1:
  1126. rsa->p = bn;
  1127. break;
  1128. case TAG_RSA_PRIME2:
  1129. rsa->q = bn;
  1130. break;
  1131. case TAG_RSA_EXPONENT1:
  1132. rsa->dmp1 = bn;
  1133. break;
  1134. case TAG_RSA_EXPONENT2:
  1135. rsa->dmq1 = bn;
  1136. break;
  1137. case TAG_RSA_COEFFICIENT:
  1138. rsa->iqmp = bn;
  1139. break;
  1140. }
  1141. }
  1142. dst__privstruct_free(&priv, mctx);
  1143. memset(&priv, 0, sizeof(priv));
  1144. if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
  1145. DST_RET(DST_R_INVALIDPRIVATEKEY);
  1146. key->key_size = BN_num_bits(rsa->n);
  1147. if (pubrsa != NULL)
  1148. RSA_free(pubrsa);
  1149. #if USE_EVP
  1150. RSA_free(rsa);
  1151. #endif
  1152. return (ISC_R_SUCCESS);
  1153. err:
  1154. #if USE_EVP
  1155. if (pkey != NULL)
  1156. EVP_PKEY_free(pkey);
  1157. #endif
  1158. if (rsa != NULL)
  1159. RSA_free(rsa);
  1160. if (pubrsa != NULL)
  1161. RSA_free(pubrsa);
  1162. opensslrsa_destroy(key);
  1163. dst__privstruct_free(&priv, mctx);
  1164. memset(&priv, 0, sizeof(priv));
  1165. return (ret);
  1166. }
  1167. static isc_result_t
  1168. opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
  1169. const char *pin)
  1170. {
  1171. #ifdef USE_ENGINE
  1172. ENGINE *e = NULL;
  1173. isc_result_t ret;
  1174. EVP_PKEY *pkey = NULL;
  1175. RSA *rsa = NULL, *pubrsa = NULL;
  1176. char *colon;
  1177. UNUSED(pin);
  1178. if (engine == NULL)
  1179. DST_RET(DST_R_NOENGINE);
  1180. e = dst__openssl_getengine(engine);
  1181. if (e == NULL)
  1182. DST_RET(DST_R_NOENGINE);
  1183. pkey = ENGINE_load_public_key(e, label, NULL, NULL);
  1184. if (pkey != NULL) {
  1185. pubrsa = EVP_PKEY_get1_RSA(pkey);
  1186. EVP_PKEY_free(pkey);
  1187. if (pubrsa == NULL)
  1188. DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  1189. }
  1190. pkey = ENGINE_load_private_key(e, label, NULL, NULL);
  1191. if (pkey == NULL)
  1192. DST_RET(ISC_R_NOTFOUND);
  1193. if (engine != NULL) {
  1194. key->engine = isc_mem_strdup(key->mctx, engine);
  1195. if (key->engine == NULL)
  1196. DST_RET(ISC_R_NOMEMORY);
  1197. } else {
  1198. key->engine = isc_mem_strdup(key->mctx, label);
  1199. if (key->engine == NULL)
  1200. DST_RET(ISC_R_NOMEMORY);
  1201. colon = strchr(key->engine, ':');
  1202. if (colon != NULL)
  1203. *colon = '\0';
  1204. }
  1205. key->label = isc_mem_strdup(key->mctx, label);
  1206. if (key->label == NULL)
  1207. DST_RET(ISC_R_NOMEMORY);
  1208. rsa = EVP_PKEY_get1_RSA(pkey);
  1209. if (rsa == NULL)
  1210. DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
  1211. if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
  1212. DST_RET(DST_R_INVALIDPRIVATEKEY);
  1213. if (pubrsa != NULL)
  1214. RSA_free(pubrsa);
  1215. key->key_size = EVP_PKEY_bits(pkey);
  1216. #if USE_EVP
  1217. key->keydata.pkey = pkey;
  1218. RSA_free(rsa);
  1219. #else
  1220. key->keydata.rsa = rsa;
  1221. EVP_PKEY_free(pkey);
  1222. #endif
  1223. return (ISC_R_SUCCESS);
  1224. err:
  1225. if (rsa != NULL)
  1226. RSA_free(rsa);
  1227. if (pubrsa != NULL)
  1228. RSA_free(pubrsa);
  1229. if (pkey != NULL)
  1230. EVP_PKEY_free(pkey);
  1231. return (ret);
  1232. #else
  1233. UNUSED(key);
  1234. UNUSED(engine);
  1235. UNUSED(label);
  1236. UNUSED(pin);
  1237. return(DST_R_NOENGINE);
  1238. #endif
  1239. }
  1240. static dst_func_t opensslrsa_functions = {
  1241. opensslrsa_createctx,
  1242. opensslrsa_destroyctx,
  1243. opensslrsa_adddata,
  1244. opensslrsa_sign,
  1245. opensslrsa_verify,
  1246. NULL, /*%< computesecret */
  1247. opensslrsa_compare,
  1248. NULL, /*%< paramcompare */
  1249. opensslrsa_generate,
  1250. opensslrsa_isprivate,
  1251. opensslrsa_destroy,
  1252. opensslrsa_todns,
  1253. opensslrsa_fromdns,
  1254. opensslrsa_tofile,
  1255. opensslrsa_parse,
  1256. NULL, /*%< cleanup */
  1257. opensslrsa_fromlabel,
  1258. NULL, /*%< dump */
  1259. NULL, /*%< restore */
  1260. };
  1261. isc_result_t
  1262. dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
  1263. REQUIRE(funcp != NULL);
  1264. if (*funcp == NULL) {
  1265. switch (algorithm) {
  1266. case DST_ALG_RSASHA256:
  1267. #if defined(HAVE_EVP_SHA256) || !USE_EVP
  1268. *funcp = &opensslrsa_functions;
  1269. #endif
  1270. break;
  1271. case DST_ALG_RSASHA512:
  1272. #if defined(HAVE_EVP_SHA512) || !USE_EVP
  1273. *funcp = &opensslrsa_functions;
  1274. #endif
  1275. break;
  1276. default:
  1277. *funcp = &opensslrsa_functions;
  1278. break;
  1279. }
  1280. }
  1281. return (ISC_R_SUCCESS);
  1282. }
  1283. #else /* OPENSSL */
  1284. #include <isc/util.h>
  1285. EMPTY_TRANSLATION_UNIT
  1286. #endif /* OPENSSL */
  1287. /*! \file */