2 * Copyright (C) 2004-2009, 2011, 2012 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 * We don't use configure for windows so enforce the OpenSSL version
60 * here. Unlike with configure we don't support overriding this test.
63 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
64 OPENSSL_VERSION_NUMBER < 0x00908000L) || \
65 OPENSSL_VERSION_NUMBER >= 0x0090804fL)
66 #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
72 * XXXMPA Temporarily disable RSA_BLINDING as it requires
73 * good quality random data that cannot currently be guaranteed.
74 * XXXMPA Find which versions of openssl use pseudo random data
75 * and set RSA_FLAG_BLINDING for those.
79 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
80 #define SET_FLAGS(rsa) \
82 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
83 (rsa)->flags |= RSA_FLAG_BLINDING; \
86 #define SET_FLAGS(rsa) \
88 (rsa)->flags |= RSA_FLAG_BLINDING; \
93 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
94 #define SET_FLAGS(rsa) \
96 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
97 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
99 #elif defined(RSA_FLAG_NO_BLINDING)
100 #define SET_FLAGS(rsa) \
102 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
103 (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
106 #define SET_FLAGS(rsa) \
108 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
112 #define DST_RET(a) {ret = a; goto err;}
114 static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
117 opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
119 EVP_MD_CTX *evp_md_ctx;
120 const EVP_MD *type = NULL;
124 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
125 dctx->key->key_alg == DST_ALG_RSASHA1 ||
126 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
127 dctx->key->key_alg == DST_ALG_RSASHA256 ||
128 dctx->key->key_alg == DST_ALG_RSASHA512);
131 evp_md_ctx = EVP_MD_CTX_create();
132 if (evp_md_ctx == NULL)
133 return (ISC_R_NOMEMORY);
135 switch (dctx->key->key_alg) {
137 type = EVP_md5(); /* MD5 + RSA */
139 case DST_ALG_RSASHA1:
140 case DST_ALG_NSEC3RSASHA1:
141 type = EVP_sha1(); /* SHA1 + RSA */
143 #ifdef HAVE_EVP_SHA256
144 case DST_ALG_RSASHA256:
145 type = EVP_sha256(); /* SHA256 + RSA */
148 #ifdef HAVE_EVP_SHA512
149 case DST_ALG_RSASHA512:
157 if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
158 EVP_MD_CTX_destroy(evp_md_ctx);
159 return (dst__openssl_toresult2("EVP_DigestInit_ex",
162 dctx->ctxdata.evp_md_ctx = evp_md_ctx;
164 switch (dctx->key->key_alg) {
169 md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
171 return (ISC_R_NOMEMORY);
172 isc_md5_init(md5ctx);
173 dctx->ctxdata.md5ctx = md5ctx;
176 case DST_ALG_RSASHA1:
177 case DST_ALG_NSEC3RSASHA1:
181 sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
183 return (ISC_R_NOMEMORY);
184 isc_sha1_init(sha1ctx);
185 dctx->ctxdata.sha1ctx = sha1ctx;
188 case DST_ALG_RSASHA256:
190 isc_sha256_t *sha256ctx;
192 sha256ctx = isc_mem_get(dctx->mctx,
193 sizeof(isc_sha256_t));
194 if (sha256ctx == NULL)
195 return (ISC_R_NOMEMORY);
196 isc_sha256_init(sha256ctx);
197 dctx->ctxdata.sha256ctx = sha256ctx;
200 case DST_ALG_RSASHA512:
202 isc_sha512_t *sha512ctx;
204 sha512ctx = isc_mem_get(dctx->mctx,
205 sizeof(isc_sha512_t));
206 if (sha512ctx == NULL)
207 return (ISC_R_NOMEMORY);
208 isc_sha512_init(sha512ctx);
209 dctx->ctxdata.sha512ctx = sha512ctx;
217 return (ISC_R_SUCCESS);
221 opensslrsa_destroyctx(dst_context_t *dctx) {
223 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
226 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
227 dctx->key->key_alg == DST_ALG_RSASHA1 ||
228 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
229 dctx->key->key_alg == DST_ALG_RSASHA256 ||
230 dctx->key->key_alg == DST_ALG_RSASHA512);
233 if (evp_md_ctx != NULL) {
234 EVP_MD_CTX_destroy(evp_md_ctx);
235 dctx->ctxdata.evp_md_ctx = NULL;
238 switch (dctx->key->key_alg) {
241 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
243 if (md5ctx != NULL) {
244 isc_md5_invalidate(md5ctx);
245 isc_mem_put(dctx->mctx, md5ctx,
247 dctx->ctxdata.md5ctx = NULL;
251 case DST_ALG_RSASHA1:
252 case DST_ALG_NSEC3RSASHA1:
254 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
256 if (sha1ctx != NULL) {
257 isc_sha1_invalidate(sha1ctx);
258 isc_mem_put(dctx->mctx, sha1ctx,
260 dctx->ctxdata.sha1ctx = NULL;
264 case DST_ALG_RSASHA256:
266 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
268 if (sha256ctx != NULL) {
269 isc_sha256_invalidate(sha256ctx);
270 isc_mem_put(dctx->mctx, sha256ctx,
271 sizeof(isc_sha256_t));
272 dctx->ctxdata.sha256ctx = NULL;
276 case DST_ALG_RSASHA512:
278 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
280 if (sha512ctx != NULL) {
281 isc_sha512_invalidate(sha512ctx);
282 isc_mem_put(dctx->mctx, sha512ctx,
283 sizeof(isc_sha512_t));
284 dctx->ctxdata.sha512ctx = NULL;
295 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
297 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
300 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
301 dctx->key->key_alg == DST_ALG_RSASHA1 ||
302 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
303 dctx->key->key_alg == DST_ALG_RSASHA256 ||
304 dctx->key->key_alg == DST_ALG_RSASHA512);
307 if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
308 return (dst__openssl_toresult2("EVP_DigestUpdate",
312 switch (dctx->key->key_alg) {
315 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
317 isc_md5_update(md5ctx, data->base, data->length);
320 case DST_ALG_RSASHA1:
321 case DST_ALG_NSEC3RSASHA1:
323 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
325 isc_sha1_update(sha1ctx, data->base, data->length);
328 case DST_ALG_RSASHA256:
330 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
332 isc_sha256_update(sha256ctx, data->base, data->length);
335 case DST_ALG_RSASHA512:
337 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
339 isc_sha512_update(sha512ctx, data->base, data->length);
346 return (ISC_R_SUCCESS);
349 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
351 * Digest prefixes from RFC 5702.
353 static unsigned char sha256_prefix[] =
354 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
355 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
356 static unsigned char sha512_prefix[] =
357 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
358 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
359 #define PREFIXLEN sizeof(sha512_prefix)
365 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
366 dst_key_t *key = dctx->key;
368 unsigned int siglen = 0;
370 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
371 EVP_PKEY *pkey = key->keydata.pkey;
373 RSA *rsa = key->keydata.rsa;
374 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
375 unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
378 unsigned int digestlen = 0;
379 #if OPENSSL_VERSION_NUMBER < 0x00908000L
380 unsigned int prefixlen = 0;
381 const unsigned char *prefix = NULL;
385 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
386 dctx->key->key_alg == DST_ALG_RSASHA1 ||
387 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
388 dctx->key->key_alg == DST_ALG_RSASHA256 ||
389 dctx->key->key_alg == DST_ALG_RSASHA512);
391 isc_buffer_availableregion(sig, &r);
394 if (r.length < (unsigned int) EVP_PKEY_size(pkey))
395 return (ISC_R_NOSPACE);
397 if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
398 return (dst__openssl_toresult2("EVP_SignFinal",
402 if (r.length < (unsigned int) RSA_size(rsa))
403 return (ISC_R_NOSPACE);
405 switch (dctx->key->key_alg) {
408 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
410 isc_md5_final(md5ctx, digest);
412 digestlen = ISC_MD5_DIGESTLENGTH;
415 case DST_ALG_RSASHA1:
416 case DST_ALG_NSEC3RSASHA1:
418 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
420 isc_sha1_final(sha1ctx, digest);
422 digestlen = ISC_SHA1_DIGESTLENGTH;
425 case DST_ALG_RSASHA256:
427 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
429 isc_sha256_final(digest, sha256ctx);
430 digestlen = ISC_SHA256_DIGESTLENGTH;
431 #if OPENSSL_VERSION_NUMBER < 0x00908000L
432 prefix = sha256_prefix;
433 prefixlen = sizeof(sha256_prefix);
439 case DST_ALG_RSASHA512:
441 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
443 isc_sha512_final(digest, sha512ctx);
444 digestlen = ISC_SHA512_DIGESTLENGTH;
445 #if OPENSSL_VERSION_NUMBER < 0x00908000L
446 prefix = sha512_prefix;
447 prefixlen = sizeof(sha512_prefix);
457 #if OPENSSL_VERSION_NUMBER < 0x00908000L
458 switch (dctx->key->key_alg) {
460 case DST_ALG_RSASHA1:
461 case DST_ALG_NSEC3RSASHA1:
463 status = RSA_sign(type, digest, digestlen, r.base,
467 case DST_ALG_RSASHA256:
468 case DST_ALG_RSASHA512:
469 INSIST(prefix != NULL);
470 INSIST(prefixlen != 0);
471 INSIST(prefixlen + digestlen <= sizeof(digest));
473 memmove(digest + prefixlen, digest, digestlen);
474 memcpy(digest, prefix, prefixlen);
475 status = RSA_private_encrypt(digestlen + prefixlen,
489 status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
492 return (dst__openssl_toresult2("RSA_sign",
493 DST_R_OPENSSLFAILURE));
496 isc_buffer_add(sig, siglen);
498 return (ISC_R_SUCCESS);
502 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
503 dst_key_t *key = dctx->key;
506 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
507 EVP_PKEY *pkey = key->keydata.pkey;
509 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
510 unsigned char digest[ISC_SHA512_DIGESTLENGTH];
512 unsigned int digestlen = 0;
513 RSA *rsa = key->keydata.rsa;
514 #if OPENSSL_VERSION_NUMBER < 0x00908000L
515 unsigned int prefixlen = 0;
516 const unsigned char *prefix = NULL;
520 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
521 dctx->key->key_alg == DST_ALG_RSASHA1 ||
522 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
523 dctx->key->key_alg == DST_ALG_RSASHA256 ||
524 dctx->key->key_alg == DST_ALG_RSASHA512);
527 status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
529 switch (dctx->key->key_alg) {
532 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
534 isc_md5_final(md5ctx, digest);
536 digestlen = ISC_MD5_DIGESTLENGTH;
539 case DST_ALG_RSASHA1:
540 case DST_ALG_NSEC3RSASHA1:
542 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
544 isc_sha1_final(sha1ctx, digest);
546 digestlen = ISC_SHA1_DIGESTLENGTH;
549 case DST_ALG_RSASHA256:
551 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
553 isc_sha256_final(digest, sha256ctx);
554 digestlen = ISC_SHA256_DIGESTLENGTH;
555 #if OPENSSL_VERSION_NUMBER < 0x00908000L
556 prefix = sha256_prefix;
557 prefixlen = sizeof(sha256_prefix);
563 case DST_ALG_RSASHA512:
565 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
567 isc_sha512_final(digest, sha512ctx);
568 digestlen = ISC_SHA512_DIGESTLENGTH;
569 #if OPENSSL_VERSION_NUMBER < 0x00908000L
570 prefix = sha512_prefix;
571 prefixlen = sizeof(sha512_prefix);
581 if (sig->length != (unsigned int) RSA_size(rsa))
582 return (DST_R_VERIFYFAILURE);
584 #if OPENSSL_VERSION_NUMBER < 0x00908000L
585 switch (dctx->key->key_alg) {
587 case DST_ALG_RSASHA1:
588 case DST_ALG_NSEC3RSASHA1:
590 status = RSA_verify(type, digest, digestlen, sig->base,
594 case DST_ALG_RSASHA256:
595 case DST_ALG_RSASHA512:
598 * 1024 is big enough for all valid RSA bit sizes
599 * for use with DNSSEC.
601 unsigned char original[PREFIXLEN + 1024];
603 INSIST(prefix != NULL);
604 INSIST(prefixlen != 0U);
606 if (RSA_size(rsa) > (int)sizeof(original))
607 return (DST_R_VERIFYFAILURE);
609 status = RSA_public_decrypt(sig->length, sig->base,
613 return (dst__openssl_toresult2(
614 "RSA_public_decrypt",
615 DST_R_VERIFYFAILURE));
616 if (status != (int)(prefixlen + digestlen))
617 return (DST_R_VERIFYFAILURE);
618 if (memcmp(original, prefix, prefixlen))
619 return (DST_R_VERIFYFAILURE);
620 if (memcmp(original + prefixlen, digest, digestlen))
621 return (DST_R_VERIFYFAILURE);
631 status = RSA_verify(type, digest, digestlen, sig->base,
636 return (dst__openssl_toresult2("RSA_verify",
637 DST_R_VERIFYFAILURE));
639 return (ISC_R_SUCCESS);
643 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
645 RSA *rsa1 = NULL, *rsa2 = NULL;
647 EVP_PKEY *pkey1, *pkey2;
651 pkey1 = key1->keydata.pkey;
652 pkey2 = key2->keydata.pkey;
654 * The pkey reference will keep these around after
655 * the RSA_free() call.
658 rsa1 = EVP_PKEY_get1_RSA(pkey1);
662 rsa2 = EVP_PKEY_get1_RSA(pkey2);
666 rsa1 = key1->keydata.rsa;
667 rsa2 = key2->keydata.rsa;
670 if (rsa1 == NULL && rsa2 == NULL)
672 else if (rsa1 == NULL || rsa2 == NULL)
675 status = BN_cmp(rsa1->n, rsa2->n) ||
676 BN_cmp(rsa1->e, rsa2->e);
682 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
683 (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
684 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
685 (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
688 * Can't compare private parameters, BTW does it make sense?
694 if (rsa1->d != NULL || rsa2->d != NULL) {
695 if (rsa1->d == NULL || rsa2->d == NULL)
697 status = BN_cmp(rsa1->d, rsa2->d) ||
698 BN_cmp(rsa1->p, rsa2->p) ||
699 BN_cmp(rsa1->q, rsa2->q);
707 #if OPENSSL_VERSION_NUMBER > 0x00908000L
709 progress_cb(int p, int n, BN_GENCB *cb)
726 opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
727 #if OPENSSL_VERSION_NUMBER > 0x00908000L
728 isc_result_t ret = DST_R_OPENSSLFAILURE;
734 RSA *rsa = RSA_new();
735 BIGNUM *e = BN_new();
737 EVP_PKEY *pkey = EVP_PKEY_new();
740 if (rsa == NULL || e == NULL)
745 if (!EVP_PKEY_set1_RSA(pkey, rsa))
759 if (callback == NULL) {
760 BN_GENCB_set_old(&cb, NULL, NULL);
763 BN_GENCB_set(&cb, &progress_cb, u.dptr);
766 if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
770 key->keydata.pkey = pkey;
774 key->keydata.rsa = rsa;
776 return (ISC_R_SUCCESS);
778 ret = dst__openssl_toresult2("RSA_generate_key_ex",
779 DST_R_OPENSSLFAILURE);
790 return (dst__openssl_toresult(ret));
795 EVP_PKEY *pkey = EVP_PKEY_new();
800 return (ISC_R_NOMEMORY);
809 rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
814 return (dst__openssl_toresult2("RSA_generate_key",
815 DST_R_OPENSSLFAILURE));
819 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
822 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
824 key->keydata.pkey = pkey;
827 key->keydata.rsa = rsa;
830 return (ISC_R_SUCCESS);
835 opensslrsa_isprivate(const dst_key_t *key) {
837 RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
840 /* key->keydata.pkey still has a reference so rsa is still valid. */
842 RSA *rsa = key->keydata.rsa;
844 if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
846 return (ISC_TF(rsa != NULL && rsa->d != NULL));
850 opensslrsa_destroy(dst_key_t *key) {
852 EVP_PKEY *pkey = key->keydata.pkey;
854 key->keydata.pkey = NULL;
856 RSA *rsa = key->keydata.rsa;
858 key->keydata.rsa = NULL;
864 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
866 unsigned int e_bytes;
867 unsigned int mod_bytes;
875 REQUIRE(key->keydata.pkey != NULL);
877 REQUIRE(key->keydata.rsa != NULL);
881 pkey = key->keydata.pkey;
882 rsa = EVP_PKEY_get1_RSA(pkey);
884 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
886 rsa = key->keydata.rsa;
889 isc_buffer_availableregion(data, &r);
891 e_bytes = BN_num_bytes(rsa->e);
892 mod_bytes = BN_num_bytes(rsa->n);
894 if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
896 DST_RET(ISC_R_NOSPACE);
897 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
898 isc_region_consume(&r, 1);
901 DST_RET(ISC_R_NOSPACE);
902 isc_buffer_putuint8(data, 0);
903 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
904 isc_region_consume(&r, 3);
907 if (r.length < e_bytes + mod_bytes)
908 DST_RET(ISC_R_NOSPACE);
910 BN_bn2bin(rsa->e, r.base);
911 isc_region_consume(&r, e_bytes);
912 BN_bn2bin(rsa->n, r.base);
914 isc_buffer_add(data, e_bytes + mod_bytes);
926 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
929 unsigned int e_bytes;
934 isc_buffer_remainingregion(data, &r);
936 return (ISC_R_SUCCESS);
940 return (dst__openssl_toresult(ISC_R_NOMEMORY));
945 return (DST_R_INVALIDPUBLICKEY);
953 return (DST_R_INVALIDPUBLICKEY);
955 e_bytes = ((*r.base++) << 8);
956 e_bytes += *r.base++;
960 if (r.length < e_bytes) {
962 return (DST_R_INVALIDPUBLICKEY);
964 rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
968 rsa->n = BN_bin2bn(r.base, r.length, NULL);
970 key->key_size = BN_num_bits(rsa->n);
972 isc_buffer_forward(data, r.length);
975 pkey = EVP_PKEY_new();
978 return (ISC_R_NOMEMORY);
980 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
983 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
985 key->keydata.pkey = pkey;
988 key->keydata.rsa = rsa;
991 return (ISC_R_SUCCESS);
995 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
999 unsigned char *bufs[8];
1000 isc_result_t result;
1003 if (key->keydata.pkey == NULL)
1004 return (DST_R_NULLKEY);
1005 rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
1007 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1009 if (key->keydata.rsa == NULL)
1010 return (DST_R_NULLKEY);
1011 rsa = key->keydata.rsa;
1014 memset(bufs, 0, sizeof(bufs));
1015 for (i = 0; i < 8; i++) {
1016 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
1017 if (bufs[i] == NULL) {
1018 result = ISC_R_NOMEMORY;
1025 priv.elements[i].tag = TAG_RSA_MODULUS;
1026 priv.elements[i].length = BN_num_bytes(rsa->n);
1027 BN_bn2bin(rsa->n, bufs[i]);
1028 priv.elements[i].data = bufs[i];
1031 priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
1032 priv.elements[i].length = BN_num_bytes(rsa->e);
1033 BN_bn2bin(rsa->e, bufs[i]);
1034 priv.elements[i].data = bufs[i];
1037 if (rsa->d != NULL) {
1038 priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
1039 priv.elements[i].length = BN_num_bytes(rsa->d);
1040 BN_bn2bin(rsa->d, bufs[i]);
1041 priv.elements[i].data = bufs[i];
1045 if (rsa->p != NULL) {
1046 priv.elements[i].tag = TAG_RSA_PRIME1;
1047 priv.elements[i].length = BN_num_bytes(rsa->p);
1048 BN_bn2bin(rsa->p, bufs[i]);
1049 priv.elements[i].data = bufs[i];
1053 if (rsa->q != NULL) {
1054 priv.elements[i].tag = TAG_RSA_PRIME2;
1055 priv.elements[i].length = BN_num_bytes(rsa->q);
1056 BN_bn2bin(rsa->q, bufs[i]);
1057 priv.elements[i].data = bufs[i];
1061 if (rsa->dmp1 != NULL) {
1062 priv.elements[i].tag = TAG_RSA_EXPONENT1;
1063 priv.elements[i].length = BN_num_bytes(rsa->dmp1);
1064 BN_bn2bin(rsa->dmp1, bufs[i]);
1065 priv.elements[i].data = bufs[i];
1069 if (rsa->dmq1 != NULL) {
1070 priv.elements[i].tag = TAG_RSA_EXPONENT2;
1071 priv.elements[i].length = BN_num_bytes(rsa->dmq1);
1072 BN_bn2bin(rsa->dmq1, bufs[i]);
1073 priv.elements[i].data = bufs[i];
1077 if (rsa->iqmp != NULL) {
1078 priv.elements[i].tag = TAG_RSA_COEFFICIENT;
1079 priv.elements[i].length = BN_num_bytes(rsa->iqmp);
1080 BN_bn2bin(rsa->iqmp, bufs[i]);
1081 priv.elements[i].data = bufs[i];
1085 if (key->engine != NULL) {
1086 priv.elements[i].tag = TAG_RSA_ENGINE;
1087 priv.elements[i].length = strlen(key->engine) + 1;
1088 priv.elements[i].data = (unsigned char *)key->engine;
1092 if (key->label != NULL) {
1093 priv.elements[i].tag = TAG_RSA_LABEL;
1094 priv.elements[i].length = strlen(key->label) + 1;
1095 priv.elements[i].data = (unsigned char *)key->label;
1101 result = dst__privstruct_writefile(key, &priv, directory);
1106 for (i = 0; i < 8; i++) {
1107 if (bufs[i] == NULL)
1109 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
1115 rsa_check(RSA *rsa, RSA *pub)
1117 /* Public parameters should be the same but if they are not set
1118 * copy them from the public key. */
1120 if (rsa->n != NULL) {
1121 if (BN_cmp(rsa->n, pub->n) != 0)
1122 return (DST_R_INVALIDPRIVATEKEY);
1127 if (rsa->e != NULL) {
1128 if (BN_cmp(rsa->e, pub->e) != 0)
1129 return (DST_R_INVALIDPRIVATEKEY);
1135 if (rsa->n == NULL || rsa->e == NULL)
1136 return (DST_R_INVALIDPRIVATEKEY);
1137 return (ISC_R_SUCCESS);
1141 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
1145 RSA *rsa = NULL, *pubrsa = NULL;
1149 isc_mem_t *mctx = key->mctx;
1150 const char *engine = NULL, *label = NULL;
1151 #if defined(USE_ENGINE) || USE_EVP
1152 EVP_PKEY *pkey = NULL;
1156 if (pub != NULL && pub->keydata.pkey != NULL)
1157 pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
1159 if (pub != NULL && pub->keydata.rsa != NULL) {
1160 pubrsa = pub->keydata.rsa;
1161 pub->keydata.rsa = NULL;
1165 /* read private key file */
1166 ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
1167 if (ret != ISC_R_SUCCESS)
1170 for (i = 0; i < priv.nelements; i++) {
1171 switch (priv.elements[i].tag) {
1172 case TAG_RSA_ENGINE:
1173 engine = (char *)priv.elements[i].data;
1176 label = (char *)priv.elements[i].data;
1183 * Is this key is stored in a HSM?
1184 * See if we can fetch it.
1186 if (label != NULL) {
1189 DST_RET(DST_R_NOENGINE);
1190 e = dst__openssl_getengine(engine);
1192 DST_RET(DST_R_NOENGINE);
1193 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1195 DST_RET(dst__openssl_toresult2(
1196 "ENGINE_load_private_key",
1198 key->engine = isc_mem_strdup(key->mctx, engine);
1199 if (key->engine == NULL)
1200 DST_RET(ISC_R_NOMEMORY);
1201 key->label = isc_mem_strdup(key->mctx, label);
1202 if (key->label == NULL)
1203 DST_RET(ISC_R_NOMEMORY);
1204 rsa = EVP_PKEY_get1_RSA(pkey);
1206 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1207 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1208 DST_RET(DST_R_INVALIDPRIVATEKEY);
1211 key->key_size = EVP_PKEY_bits(pkey);
1213 key->keydata.pkey = pkey;
1216 key->keydata.rsa = rsa;
1217 EVP_PKEY_free(pkey);
1219 dst__privstruct_free(&priv, mctx);
1220 memset(&priv, 0, sizeof(priv));
1221 return (ISC_R_SUCCESS);
1223 DST_RET(DST_R_NOENGINE);
1229 DST_RET(ISC_R_NOMEMORY);
1233 pkey = EVP_PKEY_new();
1235 DST_RET(ISC_R_NOMEMORY);
1236 if (!EVP_PKEY_set1_RSA(pkey, rsa))
1237 DST_RET(ISC_R_FAILURE);
1238 key->keydata.pkey = pkey;
1240 key->keydata.rsa = rsa;
1243 for (i = 0; i < priv.nelements; i++) {
1245 switch (priv.elements[i].tag) {
1246 case TAG_RSA_ENGINE:
1253 bn = BN_bin2bn(priv.elements[i].data,
1254 priv.elements[i].length, NULL);
1256 DST_RET(ISC_R_NOMEMORY);
1259 switch (priv.elements[i].tag) {
1260 case TAG_RSA_MODULUS:
1263 case TAG_RSA_PUBLICEXPONENT:
1266 case TAG_RSA_PRIVATEEXPONENT:
1269 case TAG_RSA_PRIME1:
1272 case TAG_RSA_PRIME2:
1275 case TAG_RSA_EXPONENT1:
1278 case TAG_RSA_EXPONENT2:
1281 case TAG_RSA_COEFFICIENT:
1286 dst__privstruct_free(&priv, mctx);
1287 memset(&priv, 0, sizeof(priv));
1289 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1290 DST_RET(DST_R_INVALIDPRIVATEKEY);
1291 key->key_size = BN_num_bits(rsa->n);
1298 return (ISC_R_SUCCESS);
1303 EVP_PKEY_free(pkey);
1309 opensslrsa_destroy(key);
1310 dst__privstruct_free(&priv, mctx);
1311 memset(&priv, 0, sizeof(priv));
1316 opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
1322 EVP_PKEY *pkey = NULL;
1323 RSA *rsa = NULL, *pubrsa = NULL;
1329 DST_RET(DST_R_NOENGINE);
1330 e = dst__openssl_getengine(engine);
1332 DST_RET(DST_R_NOENGINE);
1333 pkey = ENGINE_load_public_key(e, label, NULL, NULL);
1335 pubrsa = EVP_PKEY_get1_RSA(pkey);
1336 EVP_PKEY_free(pkey);
1338 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1340 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1342 DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
1344 if (engine != NULL) {
1345 key->engine = isc_mem_strdup(key->mctx, engine);
1346 if (key->engine == NULL)
1347 DST_RET(ISC_R_NOMEMORY);
1349 key->engine = isc_mem_strdup(key->mctx, label);
1350 if (key->engine == NULL)
1351 DST_RET(ISC_R_NOMEMORY);
1352 colon = strchr(key->engine, ':');
1356 key->label = isc_mem_strdup(key->mctx, label);
1357 if (key->label == NULL)
1358 DST_RET(ISC_R_NOMEMORY);
1359 rsa = EVP_PKEY_get1_RSA(pkey);
1361 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1362 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1363 DST_RET(DST_R_INVALIDPRIVATEKEY);
1366 key->key_size = EVP_PKEY_bits(pkey);
1368 key->keydata.pkey = pkey;
1371 key->keydata.rsa = rsa;
1372 EVP_PKEY_free(pkey);
1374 return (ISC_R_SUCCESS);
1382 EVP_PKEY_free(pkey);
1389 return(DST_R_NOENGINE);
1393 static dst_func_t opensslrsa_functions = {
1394 opensslrsa_createctx,
1395 opensslrsa_destroyctx,
1399 NULL, /*%< computesecret */
1401 NULL, /*%< paramcompare */
1402 opensslrsa_generate,
1403 opensslrsa_isprivate,
1409 NULL, /*%< cleanup */
1410 opensslrsa_fromlabel,
1412 NULL, /*%< restore */
1416 dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
1417 REQUIRE(funcp != NULL);
1419 if (*funcp == NULL) {
1420 switch (algorithm) {
1421 case DST_ALG_RSASHA256:
1422 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1423 *funcp = &opensslrsa_functions;
1426 case DST_ALG_RSASHA512:
1427 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1428 *funcp = &opensslrsa_functions;
1432 *funcp = &opensslrsa_functions;
1436 return (ISC_R_SUCCESS);
1441 #include <isc/util.h>
1443 EMPTY_TRANSLATION_UNIT
1445 #endif /* OPENSSL */