2 * Copyright (C) 2004-2009, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
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.
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.
19 * Principal Author: Brian Wellington
26 #if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
34 #include <isc/entropy.h>
39 #include <isc/string.h>
42 #include <dst/result.h>
44 #include "dst_internal.h"
45 #include "dst_openssl.h"
46 #include "dst_parse.h"
48 #include <openssl/err.h>
49 #include <openssl/objects.h>
50 #include <openssl/rsa.h>
51 #if OPENSSL_VERSION_NUMBER > 0x00908000L
52 #include <openssl/bn.h>
55 #include <openssl/engine.h>
59 * Limit the size of public exponents.
61 #ifndef RSA_MAX_PUBEXP_BITS
62 #define RSA_MAX_PUBEXP_BITS 35
66 * We don't use configure for windows so enforce the OpenSSL version
67 * here. Unlike with configure we don't support overriding this test.
70 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
71 OPENSSL_VERSION_NUMBER < 0x00908000L) || \
72 OPENSSL_VERSION_NUMBER >= 0x0090804fL)
73 #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
79 * XXXMPA Temporarily disable RSA_BLINDING as it requires
80 * good quality random data that cannot currently be guaranteed.
81 * XXXMPA Find which versions of openssl use pseudo random data
82 * and set RSA_FLAG_BLINDING for those.
86 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
87 #define SET_FLAGS(rsa) \
89 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
90 (rsa)->flags |= RSA_FLAG_BLINDING; \
93 #define SET_FLAGS(rsa) \
95 (rsa)->flags |= RSA_FLAG_BLINDING; \
100 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
101 #define SET_FLAGS(rsa) \
103 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
104 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
106 #elif defined(RSA_FLAG_NO_BLINDING)
107 #define SET_FLAGS(rsa) \
109 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
110 (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
113 #define SET_FLAGS(rsa) \
115 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
119 #define DST_RET(a) {ret = a; goto err;}
121 static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
124 opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
126 EVP_MD_CTX *evp_md_ctx;
127 const EVP_MD *type = NULL;
131 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
132 dctx->key->key_alg == DST_ALG_RSASHA1 ||
133 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
134 dctx->key->key_alg == DST_ALG_RSASHA256 ||
135 dctx->key->key_alg == DST_ALG_RSASHA512);
138 evp_md_ctx = EVP_MD_CTX_create();
139 if (evp_md_ctx == NULL)
140 return (ISC_R_NOMEMORY);
142 switch (dctx->key->key_alg) {
144 type = EVP_md5(); /* MD5 + RSA */
146 case DST_ALG_RSASHA1:
147 case DST_ALG_NSEC3RSASHA1:
148 type = EVP_sha1(); /* SHA1 + RSA */
150 #ifdef HAVE_EVP_SHA256
151 case DST_ALG_RSASHA256:
152 type = EVP_sha256(); /* SHA256 + RSA */
155 #ifdef HAVE_EVP_SHA512
156 case DST_ALG_RSASHA512:
164 if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
165 EVP_MD_CTX_destroy(evp_md_ctx);
166 return (dst__openssl_toresult3(dctx->category,
170 dctx->ctxdata.evp_md_ctx = evp_md_ctx;
172 switch (dctx->key->key_alg) {
177 md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
179 return (ISC_R_NOMEMORY);
180 isc_md5_init(md5ctx);
181 dctx->ctxdata.md5ctx = md5ctx;
184 case DST_ALG_RSASHA1:
185 case DST_ALG_NSEC3RSASHA1:
189 sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
191 return (ISC_R_NOMEMORY);
192 isc_sha1_init(sha1ctx);
193 dctx->ctxdata.sha1ctx = sha1ctx;
196 case DST_ALG_RSASHA256:
198 isc_sha256_t *sha256ctx;
200 sha256ctx = isc_mem_get(dctx->mctx,
201 sizeof(isc_sha256_t));
202 if (sha256ctx == NULL)
203 return (ISC_R_NOMEMORY);
204 isc_sha256_init(sha256ctx);
205 dctx->ctxdata.sha256ctx = sha256ctx;
208 case DST_ALG_RSASHA512:
210 isc_sha512_t *sha512ctx;
212 sha512ctx = isc_mem_get(dctx->mctx,
213 sizeof(isc_sha512_t));
214 if (sha512ctx == NULL)
215 return (ISC_R_NOMEMORY);
216 isc_sha512_init(sha512ctx);
217 dctx->ctxdata.sha512ctx = sha512ctx;
225 return (ISC_R_SUCCESS);
229 opensslrsa_destroyctx(dst_context_t *dctx) {
231 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
234 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
235 dctx->key->key_alg == DST_ALG_RSASHA1 ||
236 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
237 dctx->key->key_alg == DST_ALG_RSASHA256 ||
238 dctx->key->key_alg == DST_ALG_RSASHA512);
241 if (evp_md_ctx != NULL) {
242 EVP_MD_CTX_destroy(evp_md_ctx);
243 dctx->ctxdata.evp_md_ctx = NULL;
246 switch (dctx->key->key_alg) {
249 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
251 if (md5ctx != NULL) {
252 isc_md5_invalidate(md5ctx);
253 isc_mem_put(dctx->mctx, md5ctx,
255 dctx->ctxdata.md5ctx = NULL;
259 case DST_ALG_RSASHA1:
260 case DST_ALG_NSEC3RSASHA1:
262 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
264 if (sha1ctx != NULL) {
265 isc_sha1_invalidate(sha1ctx);
266 isc_mem_put(dctx->mctx, sha1ctx,
268 dctx->ctxdata.sha1ctx = NULL;
272 case DST_ALG_RSASHA256:
274 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
276 if (sha256ctx != NULL) {
277 isc_sha256_invalidate(sha256ctx);
278 isc_mem_put(dctx->mctx, sha256ctx,
279 sizeof(isc_sha256_t));
280 dctx->ctxdata.sha256ctx = NULL;
284 case DST_ALG_RSASHA512:
286 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
288 if (sha512ctx != NULL) {
289 isc_sha512_invalidate(sha512ctx);
290 isc_mem_put(dctx->mctx, sha512ctx,
291 sizeof(isc_sha512_t));
292 dctx->ctxdata.sha512ctx = NULL;
303 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
305 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
308 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
309 dctx->key->key_alg == DST_ALG_RSASHA1 ||
310 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
311 dctx->key->key_alg == DST_ALG_RSASHA256 ||
312 dctx->key->key_alg == DST_ALG_RSASHA512);
315 if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
316 return (dst__openssl_toresult3(dctx->category,
321 switch (dctx->key->key_alg) {
324 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
326 isc_md5_update(md5ctx, data->base, data->length);
329 case DST_ALG_RSASHA1:
330 case DST_ALG_NSEC3RSASHA1:
332 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
334 isc_sha1_update(sha1ctx, data->base, data->length);
337 case DST_ALG_RSASHA256:
339 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
341 isc_sha256_update(sha256ctx, data->base, data->length);
344 case DST_ALG_RSASHA512:
346 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
348 isc_sha512_update(sha512ctx, data->base, data->length);
355 return (ISC_R_SUCCESS);
358 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
360 * Digest prefixes from RFC 5702.
362 static unsigned char sha256_prefix[] =
363 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
364 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
365 static unsigned char sha512_prefix[] =
366 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
367 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
368 #define PREFIXLEN sizeof(sha512_prefix)
374 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
375 dst_key_t *key = dctx->key;
377 unsigned int siglen = 0;
379 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
380 EVP_PKEY *pkey = key->keydata.pkey;
382 RSA *rsa = key->keydata.rsa;
383 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
384 unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
387 unsigned int digestlen = 0;
388 #if OPENSSL_VERSION_NUMBER < 0x00908000L
389 unsigned int prefixlen = 0;
390 const unsigned char *prefix = NULL;
394 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
395 dctx->key->key_alg == DST_ALG_RSASHA1 ||
396 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
397 dctx->key->key_alg == DST_ALG_RSASHA256 ||
398 dctx->key->key_alg == DST_ALG_RSASHA512);
400 isc_buffer_availableregion(sig, &r);
403 if (r.length < (unsigned int) EVP_PKEY_size(pkey))
404 return (ISC_R_NOSPACE);
406 if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
407 return (dst__openssl_toresult3(dctx->category,
412 if (r.length < (unsigned int) RSA_size(rsa))
413 return (ISC_R_NOSPACE);
415 switch (dctx->key->key_alg) {
418 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
420 isc_md5_final(md5ctx, digest);
422 digestlen = ISC_MD5_DIGESTLENGTH;
425 case DST_ALG_RSASHA1:
426 case DST_ALG_NSEC3RSASHA1:
428 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
430 isc_sha1_final(sha1ctx, digest);
432 digestlen = ISC_SHA1_DIGESTLENGTH;
435 case DST_ALG_RSASHA256:
437 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
439 isc_sha256_final(digest, sha256ctx);
440 digestlen = ISC_SHA256_DIGESTLENGTH;
441 #if OPENSSL_VERSION_NUMBER < 0x00908000L
442 prefix = sha256_prefix;
443 prefixlen = sizeof(sha256_prefix);
449 case DST_ALG_RSASHA512:
451 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
453 isc_sha512_final(digest, sha512ctx);
454 digestlen = ISC_SHA512_DIGESTLENGTH;
455 #if OPENSSL_VERSION_NUMBER < 0x00908000L
456 prefix = sha512_prefix;
457 prefixlen = sizeof(sha512_prefix);
467 #if OPENSSL_VERSION_NUMBER < 0x00908000L
468 switch (dctx->key->key_alg) {
470 case DST_ALG_RSASHA1:
471 case DST_ALG_NSEC3RSASHA1:
473 status = RSA_sign(type, digest, digestlen, r.base,
477 case DST_ALG_RSASHA256:
478 case DST_ALG_RSASHA512:
479 INSIST(prefix != NULL);
480 INSIST(prefixlen != 0);
481 INSIST(prefixlen + digestlen <= sizeof(digest));
483 memmove(digest + prefixlen, digest, digestlen);
484 memmove(digest, prefix, prefixlen);
485 status = RSA_private_encrypt(digestlen + prefixlen,
499 status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
502 return (dst__openssl_toresult3(dctx->category,
504 DST_R_OPENSSLFAILURE));
507 isc_buffer_add(sig, siglen);
509 return (ISC_R_SUCCESS);
513 opensslrsa_verify2(dst_context_t *dctx, int maxbits, const isc_region_t *sig) {
514 dst_key_t *key = dctx->key;
517 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
518 EVP_PKEY *pkey = key->keydata.pkey;
522 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
523 unsigned char digest[ISC_SHA512_DIGESTLENGTH];
525 unsigned int digestlen = 0;
526 RSA *rsa = key->keydata.rsa;
527 #if OPENSSL_VERSION_NUMBER < 0x00908000L
528 unsigned int prefixlen = 0;
529 const unsigned char *prefix = NULL;
533 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
534 dctx->key->key_alg == DST_ALG_RSASHA1 ||
535 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
536 dctx->key->key_alg == DST_ALG_RSASHA256 ||
537 dctx->key->key_alg == DST_ALG_RSASHA512);
540 rsa = EVP_PKEY_get1_RSA(pkey);
542 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
543 bits = BN_num_bits(rsa->e);
545 if (bits > maxbits && maxbits != 0)
546 return (DST_R_VERIFYFAILURE);
548 status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
551 return (ISC_R_SUCCESS);
553 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
555 return (dst__openssl_toresult3(dctx->category,
557 DST_R_VERIFYFAILURE));
560 if (BN_num_bits(rsa->e) > maxbits && maxbits != 0)
561 return (DST_R_VERIFYFAILURE);
563 switch (dctx->key->key_alg) {
566 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
568 isc_md5_final(md5ctx, digest);
570 digestlen = ISC_MD5_DIGESTLENGTH;
573 case DST_ALG_RSASHA1:
574 case DST_ALG_NSEC3RSASHA1:
576 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
578 isc_sha1_final(sha1ctx, digest);
580 digestlen = ISC_SHA1_DIGESTLENGTH;
583 case DST_ALG_RSASHA256:
585 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
587 isc_sha256_final(digest, sha256ctx);
588 digestlen = ISC_SHA256_DIGESTLENGTH;
589 #if OPENSSL_VERSION_NUMBER < 0x00908000L
590 prefix = sha256_prefix;
591 prefixlen = sizeof(sha256_prefix);
597 case DST_ALG_RSASHA512:
599 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
601 isc_sha512_final(digest, sha512ctx);
602 digestlen = ISC_SHA512_DIGESTLENGTH;
603 #if OPENSSL_VERSION_NUMBER < 0x00908000L
604 prefix = sha512_prefix;
605 prefixlen = sizeof(sha512_prefix);
615 if (sig->length != (unsigned int) RSA_size(rsa))
616 return (DST_R_VERIFYFAILURE);
618 #if OPENSSL_VERSION_NUMBER < 0x00908000L
619 switch (dctx->key->key_alg) {
621 case DST_ALG_RSASHA1:
622 case DST_ALG_NSEC3RSASHA1:
624 status = RSA_verify(type, digest, digestlen, sig->base,
628 case DST_ALG_RSASHA256:
629 case DST_ALG_RSASHA512:
632 * 1024 is big enough for all valid RSA bit sizes
633 * for use with DNSSEC.
635 unsigned char original[PREFIXLEN + 1024];
637 INSIST(prefix != NULL);
638 INSIST(prefixlen != 0U);
640 if (RSA_size(rsa) > (int)sizeof(original))
641 return (DST_R_VERIFYFAILURE);
643 status = RSA_public_decrypt(sig->length, sig->base,
647 return (dst__openssl_toresult3(
649 "RSA_public_decrypt",
650 DST_R_VERIFYFAILURE));
651 if (status != (int)(prefixlen + digestlen))
652 return (DST_R_VERIFYFAILURE);
653 if (memcmp(original, prefix, prefixlen))
654 return (DST_R_VERIFYFAILURE);
655 if (memcmp(original + prefixlen, digest, digestlen))
656 return (DST_R_VERIFYFAILURE);
666 status = RSA_verify(type, digest, digestlen, sig->base,
670 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
671 return (ISC_R_SUCCESS);
676 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
677 return (opensslrsa_verify2(dctx, 0, sig));
681 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
683 RSA *rsa1 = NULL, *rsa2 = NULL;
685 EVP_PKEY *pkey1, *pkey2;
689 pkey1 = key1->keydata.pkey;
690 pkey2 = key2->keydata.pkey;
692 * The pkey reference will keep these around after
693 * the RSA_free() call.
696 rsa1 = EVP_PKEY_get1_RSA(pkey1);
700 rsa2 = EVP_PKEY_get1_RSA(pkey2);
704 rsa1 = key1->keydata.rsa;
705 rsa2 = key2->keydata.rsa;
708 if (rsa1 == NULL && rsa2 == NULL)
710 else if (rsa1 == NULL || rsa2 == NULL)
713 status = BN_cmp(rsa1->n, rsa2->n) ||
714 BN_cmp(rsa1->e, rsa2->e);
720 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
721 (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
722 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
723 (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
726 * Can't compare private parameters, BTW does it make sense?
732 if (rsa1->d != NULL || rsa2->d != NULL) {
733 if (rsa1->d == NULL || rsa2->d == NULL)
735 status = BN_cmp(rsa1->d, rsa2->d) ||
736 BN_cmp(rsa1->p, rsa2->p) ||
737 BN_cmp(rsa1->q, rsa2->q);
745 #if OPENSSL_VERSION_NUMBER > 0x00908000L
747 progress_cb(int p, int n, BN_GENCB *cb)
764 opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
765 #if OPENSSL_VERSION_NUMBER > 0x00908000L
766 isc_result_t ret = DST_R_OPENSSLFAILURE;
772 RSA *rsa = RSA_new();
773 BIGNUM *e = BN_new();
775 EVP_PKEY *pkey = EVP_PKEY_new();
778 if (rsa == NULL || e == NULL)
783 if (!EVP_PKEY_set1_RSA(pkey, rsa))
792 /* (phased-out) F5 0x100000001 */
797 if (callback == NULL) {
798 BN_GENCB_set_old(&cb, NULL, NULL);
801 BN_GENCB_set(&cb, &progress_cb, u.dptr);
804 if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
808 key->keydata.pkey = pkey;
812 key->keydata.rsa = rsa;
814 return (ISC_R_SUCCESS);
816 ret = dst__openssl_toresult2("RSA_generate_key_ex",
817 DST_R_OPENSSLFAILURE);
828 return (dst__openssl_toresult(ret));
833 EVP_PKEY *pkey = EVP_PKEY_new();
838 return (ISC_R_NOMEMORY);
847 rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
852 return (dst__openssl_toresult2("RSA_generate_key",
853 DST_R_OPENSSLFAILURE));
857 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
860 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
862 key->keydata.pkey = pkey;
865 key->keydata.rsa = rsa;
868 return (ISC_R_SUCCESS);
873 opensslrsa_isprivate(const dst_key_t *key) {
875 RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
878 /* key->keydata.pkey still has a reference so rsa is still valid. */
880 RSA *rsa = key->keydata.rsa;
882 if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
884 return (ISC_TF(rsa != NULL && rsa->d != NULL));
888 opensslrsa_destroy(dst_key_t *key) {
890 EVP_PKEY *pkey = key->keydata.pkey;
892 key->keydata.pkey = NULL;
894 RSA *rsa = key->keydata.rsa;
896 key->keydata.rsa = NULL;
902 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
904 unsigned int e_bytes;
905 unsigned int mod_bytes;
913 REQUIRE(key->keydata.pkey != NULL);
915 REQUIRE(key->keydata.rsa != NULL);
919 pkey = key->keydata.pkey;
920 rsa = EVP_PKEY_get1_RSA(pkey);
922 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
924 rsa = key->keydata.rsa;
927 isc_buffer_availableregion(data, &r);
929 e_bytes = BN_num_bytes(rsa->e);
930 mod_bytes = BN_num_bytes(rsa->n);
932 if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
934 DST_RET(ISC_R_NOSPACE);
935 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
936 isc_region_consume(&r, 1);
939 DST_RET(ISC_R_NOSPACE);
940 isc_buffer_putuint8(data, 0);
941 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
942 isc_region_consume(&r, 3);
945 if (r.length < e_bytes + mod_bytes)
946 DST_RET(ISC_R_NOSPACE);
948 BN_bn2bin(rsa->e, r.base);
949 isc_region_consume(&r, e_bytes);
950 BN_bn2bin(rsa->n, r.base);
952 isc_buffer_add(data, e_bytes + mod_bytes);
964 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
967 unsigned int e_bytes;
973 isc_buffer_remainingregion(data, &r);
975 return (ISC_R_SUCCESS);
980 return (dst__openssl_toresult(ISC_R_NOMEMORY));
985 return (DST_R_INVALIDPUBLICKEY);
988 isc_region_consume(&r, 1);
993 return (DST_R_INVALIDPUBLICKEY);
995 e_bytes = (*r.base) << 8;
996 isc_region_consume(&r, 1);
998 isc_region_consume(&r, 1);
1001 if (r.length < e_bytes) {
1003 return (DST_R_INVALIDPUBLICKEY);
1005 rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
1006 isc_region_consume(&r, e_bytes);
1008 rsa->n = BN_bin2bn(r.base, r.length, NULL);
1010 key->key_size = BN_num_bits(rsa->n);
1012 isc_buffer_forward(data, length);
1015 pkey = EVP_PKEY_new();
1018 return (ISC_R_NOMEMORY);
1020 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
1021 EVP_PKEY_free(pkey);
1023 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1025 key->keydata.pkey = pkey;
1028 key->keydata.rsa = rsa;
1031 return (ISC_R_SUCCESS);
1035 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
1039 unsigned char *bufs[8];
1040 isc_result_t result;
1043 if (key->keydata.pkey == NULL)
1044 return (DST_R_NULLKEY);
1045 rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
1047 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1049 if (key->keydata.rsa == NULL)
1050 return (DST_R_NULLKEY);
1051 rsa = key->keydata.rsa;
1053 memset(bufs, 0, sizeof(bufs));
1055 if (key->external) {
1057 result = dst__privstruct_writefile(key, &priv, directory);
1061 for (i = 0; i < 8; i++) {
1062 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
1063 if (bufs[i] == NULL) {
1064 result = ISC_R_NOMEMORY;
1071 priv.elements[i].tag = TAG_RSA_MODULUS;
1072 priv.elements[i].length = BN_num_bytes(rsa->n);
1073 BN_bn2bin(rsa->n, bufs[i]);
1074 priv.elements[i].data = bufs[i];
1077 priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
1078 priv.elements[i].length = BN_num_bytes(rsa->e);
1079 BN_bn2bin(rsa->e, bufs[i]);
1080 priv.elements[i].data = bufs[i];
1083 if (rsa->d != NULL) {
1084 priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
1085 priv.elements[i].length = BN_num_bytes(rsa->d);
1086 BN_bn2bin(rsa->d, bufs[i]);
1087 priv.elements[i].data = bufs[i];
1091 if (rsa->p != NULL) {
1092 priv.elements[i].tag = TAG_RSA_PRIME1;
1093 priv.elements[i].length = BN_num_bytes(rsa->p);
1094 BN_bn2bin(rsa->p, bufs[i]);
1095 priv.elements[i].data = bufs[i];
1099 if (rsa->q != NULL) {
1100 priv.elements[i].tag = TAG_RSA_PRIME2;
1101 priv.elements[i].length = BN_num_bytes(rsa->q);
1102 BN_bn2bin(rsa->q, bufs[i]);
1103 priv.elements[i].data = bufs[i];
1107 if (rsa->dmp1 != NULL) {
1108 priv.elements[i].tag = TAG_RSA_EXPONENT1;
1109 priv.elements[i].length = BN_num_bytes(rsa->dmp1);
1110 BN_bn2bin(rsa->dmp1, bufs[i]);
1111 priv.elements[i].data = bufs[i];
1115 if (rsa->dmq1 != NULL) {
1116 priv.elements[i].tag = TAG_RSA_EXPONENT2;
1117 priv.elements[i].length = BN_num_bytes(rsa->dmq1);
1118 BN_bn2bin(rsa->dmq1, bufs[i]);
1119 priv.elements[i].data = bufs[i];
1123 if (rsa->iqmp != NULL) {
1124 priv.elements[i].tag = TAG_RSA_COEFFICIENT;
1125 priv.elements[i].length = BN_num_bytes(rsa->iqmp);
1126 BN_bn2bin(rsa->iqmp, bufs[i]);
1127 priv.elements[i].data = bufs[i];
1131 if (key->engine != NULL) {
1132 priv.elements[i].tag = TAG_RSA_ENGINE;
1133 priv.elements[i].length = strlen(key->engine) + 1;
1134 priv.elements[i].data = (unsigned char *)key->engine;
1138 if (key->label != NULL) {
1139 priv.elements[i].tag = TAG_RSA_LABEL;
1140 priv.elements[i].length = strlen(key->label) + 1;
1141 priv.elements[i].data = (unsigned char *)key->label;
1147 result = dst__privstruct_writefile(key, &priv, directory);
1152 for (i = 0; i < 8; i++) {
1153 if (bufs[i] == NULL)
1155 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
1161 rsa_check(RSA *rsa, RSA *pub)
1163 /* Public parameters should be the same but if they are not set
1164 * copy them from the public key. */
1166 if (rsa->n != NULL) {
1167 if (BN_cmp(rsa->n, pub->n) != 0)
1168 return (DST_R_INVALIDPRIVATEKEY);
1173 if (rsa->e != NULL) {
1174 if (BN_cmp(rsa->e, pub->e) != 0)
1175 return (DST_R_INVALIDPRIVATEKEY);
1181 if (rsa->n == NULL || rsa->e == NULL)
1182 return (DST_R_INVALIDPRIVATEKEY);
1183 return (ISC_R_SUCCESS);
1187 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
1191 RSA *rsa = NULL, *pubrsa = NULL;
1195 isc_mem_t *mctx = key->mctx;
1196 const char *engine = NULL, *label = NULL;
1197 #if defined(USE_ENGINE) || USE_EVP
1198 EVP_PKEY *pkey = NULL;
1202 if (pub != NULL && pub->keydata.pkey != NULL)
1203 pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
1205 if (pub != NULL && pub->keydata.rsa != NULL) {
1206 pubrsa = pub->keydata.rsa;
1207 pub->keydata.rsa = NULL;
1211 /* read private key file */
1212 ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
1213 if (ret != ISC_R_SUCCESS)
1216 if (key->external && priv.nelements != 0)
1217 DST_RET(DST_R_INVALIDPRIVATEKEY);
1219 for (i = 0; i < priv.nelements; i++) {
1220 switch (priv.elements[i].tag) {
1221 case TAG_RSA_ENGINE:
1222 engine = (char *)priv.elements[i].data;
1225 label = (char *)priv.elements[i].data;
1233 * Is this key is stored in a HSM?
1234 * See if we can fetch it.
1236 if (label != NULL) {
1239 DST_RET(DST_R_NOENGINE);
1240 e = dst__openssl_getengine(engine);
1242 DST_RET(DST_R_NOENGINE);
1243 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1245 DST_RET(dst__openssl_toresult2(
1246 "ENGINE_load_private_key",
1248 key->engine = isc_mem_strdup(key->mctx, engine);
1249 if (key->engine == NULL)
1250 DST_RET(ISC_R_NOMEMORY);
1251 key->label = isc_mem_strdup(key->mctx, label);
1252 if (key->label == NULL)
1253 DST_RET(ISC_R_NOMEMORY);
1254 rsa = EVP_PKEY_get1_RSA(pkey);
1256 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1257 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1258 DST_RET(DST_R_INVALIDPRIVATEKEY);
1259 if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
1260 DST_RET(ISC_R_RANGE);
1263 key->key_size = EVP_PKEY_bits(pkey);
1265 key->keydata.pkey = pkey;
1268 key->keydata.rsa = rsa;
1269 EVP_PKEY_free(pkey);
1271 dst__privstruct_free(&priv, mctx);
1272 memset(&priv, 0, sizeof(priv));
1273 return (ISC_R_SUCCESS);
1275 DST_RET(DST_R_NOENGINE);
1281 DST_RET(ISC_R_NOMEMORY);
1285 pkey = EVP_PKEY_new();
1287 DST_RET(ISC_R_NOMEMORY);
1288 if (!EVP_PKEY_set1_RSA(pkey, rsa))
1289 DST_RET(ISC_R_FAILURE);
1290 key->keydata.pkey = pkey;
1292 key->keydata.rsa = rsa;
1295 for (i = 0; i < priv.nelements; i++) {
1297 switch (priv.elements[i].tag) {
1298 case TAG_RSA_ENGINE:
1305 bn = BN_bin2bn(priv.elements[i].data,
1306 priv.elements[i].length, NULL);
1308 DST_RET(ISC_R_NOMEMORY);
1311 switch (priv.elements[i].tag) {
1312 case TAG_RSA_MODULUS:
1315 case TAG_RSA_PUBLICEXPONENT:
1318 case TAG_RSA_PRIVATEEXPONENT:
1321 case TAG_RSA_PRIME1:
1324 case TAG_RSA_PRIME2:
1327 case TAG_RSA_EXPONENT1:
1330 case TAG_RSA_EXPONENT2:
1333 case TAG_RSA_COEFFICIENT:
1338 dst__privstruct_free(&priv, mctx);
1339 memset(&priv, 0, sizeof(priv));
1341 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1342 DST_RET(DST_R_INVALIDPRIVATEKEY);
1343 if (!key->external) {
1344 if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
1345 DST_RET(ISC_R_RANGE);
1347 key->key_size = BN_num_bits(rsa->n);
1354 return (ISC_R_SUCCESS);
1359 EVP_PKEY_free(pkey);
1365 key->keydata.generic = NULL;
1366 dst__privstruct_free(&priv, mctx);
1367 memset(&priv, 0, sizeof(priv));
1372 opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
1378 EVP_PKEY *pkey = NULL;
1379 RSA *rsa = NULL, *pubrsa = NULL;
1385 DST_RET(DST_R_NOENGINE);
1386 e = dst__openssl_getengine(engine);
1388 DST_RET(DST_R_NOENGINE);
1389 pkey = ENGINE_load_public_key(e, label, NULL, NULL);
1391 pubrsa = EVP_PKEY_get1_RSA(pkey);
1392 EVP_PKEY_free(pkey);
1394 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1396 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1398 DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
1400 if (engine != NULL) {
1401 key->engine = isc_mem_strdup(key->mctx, engine);
1402 if (key->engine == NULL)
1403 DST_RET(ISC_R_NOMEMORY);
1405 key->engine = isc_mem_strdup(key->mctx, label);
1406 if (key->engine == NULL)
1407 DST_RET(ISC_R_NOMEMORY);
1408 colon = strchr(key->engine, ':');
1412 key->label = isc_mem_strdup(key->mctx, label);
1413 if (key->label == NULL)
1414 DST_RET(ISC_R_NOMEMORY);
1415 rsa = EVP_PKEY_get1_RSA(pkey);
1417 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1418 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1419 DST_RET(DST_R_INVALIDPRIVATEKEY);
1420 if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
1421 DST_RET(ISC_R_RANGE);
1424 key->key_size = EVP_PKEY_bits(pkey);
1426 key->keydata.pkey = pkey;
1429 key->keydata.rsa = rsa;
1430 EVP_PKEY_free(pkey);
1432 return (ISC_R_SUCCESS);
1440 EVP_PKEY_free(pkey);
1447 return(DST_R_NOENGINE);
1451 static dst_func_t opensslrsa_functions = {
1452 opensslrsa_createctx,
1453 opensslrsa_destroyctx,
1458 NULL, /*%< computesecret */
1460 NULL, /*%< paramcompare */
1461 opensslrsa_generate,
1462 opensslrsa_isprivate,
1468 NULL, /*%< cleanup */
1469 opensslrsa_fromlabel,
1471 NULL, /*%< restore */
1475 dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
1476 REQUIRE(funcp != NULL);
1478 if (*funcp == NULL) {
1479 switch (algorithm) {
1480 case DST_ALG_RSASHA256:
1481 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1482 *funcp = &opensslrsa_functions;
1485 case DST_ALG_RSASHA512:
1486 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1487 *funcp = &opensslrsa_functions;
1491 *funcp = &opensslrsa_functions;
1495 return (ISC_R_SUCCESS);
1500 #include <isc/util.h>
1502 EMPTY_TRANSLATION_UNIT
1504 #endif /* OPENSSL */