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;
972 isc_buffer_remainingregion(data, &r);
974 return (ISC_R_SUCCESS);
978 return (dst__openssl_toresult(ISC_R_NOMEMORY));
983 return (DST_R_INVALIDPUBLICKEY);
991 return (DST_R_INVALIDPUBLICKEY);
993 e_bytes = ((*r.base++) << 8);
994 e_bytes += *r.base++;
998 if (r.length < e_bytes) {
1000 return (DST_R_INVALIDPUBLICKEY);
1002 rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
1004 r.length -= e_bytes;
1006 rsa->n = BN_bin2bn(r.base, r.length, NULL);
1008 key->key_size = BN_num_bits(rsa->n);
1010 isc_buffer_forward(data, r.length);
1013 pkey = EVP_PKEY_new();
1016 return (ISC_R_NOMEMORY);
1018 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
1019 EVP_PKEY_free(pkey);
1021 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1023 key->keydata.pkey = pkey;
1026 key->keydata.rsa = rsa;
1029 return (ISC_R_SUCCESS);
1033 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
1037 unsigned char *bufs[8];
1038 isc_result_t result;
1041 if (key->keydata.pkey == NULL)
1042 return (DST_R_NULLKEY);
1043 rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
1045 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1047 if (key->keydata.rsa == NULL)
1048 return (DST_R_NULLKEY);
1049 rsa = key->keydata.rsa;
1051 memset(bufs, 0, sizeof(bufs));
1053 if (key->external) {
1055 result = dst__privstruct_writefile(key, &priv, directory);
1059 for (i = 0; i < 8; i++) {
1060 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
1061 if (bufs[i] == NULL) {
1062 result = ISC_R_NOMEMORY;
1069 priv.elements[i].tag = TAG_RSA_MODULUS;
1070 priv.elements[i].length = BN_num_bytes(rsa->n);
1071 BN_bn2bin(rsa->n, bufs[i]);
1072 priv.elements[i].data = bufs[i];
1075 priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
1076 priv.elements[i].length = BN_num_bytes(rsa->e);
1077 BN_bn2bin(rsa->e, bufs[i]);
1078 priv.elements[i].data = bufs[i];
1081 if (rsa->d != NULL) {
1082 priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
1083 priv.elements[i].length = BN_num_bytes(rsa->d);
1084 BN_bn2bin(rsa->d, bufs[i]);
1085 priv.elements[i].data = bufs[i];
1089 if (rsa->p != NULL) {
1090 priv.elements[i].tag = TAG_RSA_PRIME1;
1091 priv.elements[i].length = BN_num_bytes(rsa->p);
1092 BN_bn2bin(rsa->p, bufs[i]);
1093 priv.elements[i].data = bufs[i];
1097 if (rsa->q != NULL) {
1098 priv.elements[i].tag = TAG_RSA_PRIME2;
1099 priv.elements[i].length = BN_num_bytes(rsa->q);
1100 BN_bn2bin(rsa->q, bufs[i]);
1101 priv.elements[i].data = bufs[i];
1105 if (rsa->dmp1 != NULL) {
1106 priv.elements[i].tag = TAG_RSA_EXPONENT1;
1107 priv.elements[i].length = BN_num_bytes(rsa->dmp1);
1108 BN_bn2bin(rsa->dmp1, bufs[i]);
1109 priv.elements[i].data = bufs[i];
1113 if (rsa->dmq1 != NULL) {
1114 priv.elements[i].tag = TAG_RSA_EXPONENT2;
1115 priv.elements[i].length = BN_num_bytes(rsa->dmq1);
1116 BN_bn2bin(rsa->dmq1, bufs[i]);
1117 priv.elements[i].data = bufs[i];
1121 if (rsa->iqmp != NULL) {
1122 priv.elements[i].tag = TAG_RSA_COEFFICIENT;
1123 priv.elements[i].length = BN_num_bytes(rsa->iqmp);
1124 BN_bn2bin(rsa->iqmp, bufs[i]);
1125 priv.elements[i].data = bufs[i];
1129 if (key->engine != NULL) {
1130 priv.elements[i].tag = TAG_RSA_ENGINE;
1131 priv.elements[i].length = strlen(key->engine) + 1;
1132 priv.elements[i].data = (unsigned char *)key->engine;
1136 if (key->label != NULL) {
1137 priv.elements[i].tag = TAG_RSA_LABEL;
1138 priv.elements[i].length = strlen(key->label) + 1;
1139 priv.elements[i].data = (unsigned char *)key->label;
1145 result = dst__privstruct_writefile(key, &priv, directory);
1150 for (i = 0; i < 8; i++) {
1151 if (bufs[i] == NULL)
1153 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
1159 rsa_check(RSA *rsa, RSA *pub)
1161 /* Public parameters should be the same but if they are not set
1162 * copy them from the public key. */
1164 if (rsa->n != NULL) {
1165 if (BN_cmp(rsa->n, pub->n) != 0)
1166 return (DST_R_INVALIDPRIVATEKEY);
1171 if (rsa->e != NULL) {
1172 if (BN_cmp(rsa->e, pub->e) != 0)
1173 return (DST_R_INVALIDPRIVATEKEY);
1179 if (rsa->n == NULL || rsa->e == NULL)
1180 return (DST_R_INVALIDPRIVATEKEY);
1181 return (ISC_R_SUCCESS);
1185 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
1189 RSA *rsa = NULL, *pubrsa = NULL;
1193 isc_mem_t *mctx = key->mctx;
1194 const char *engine = NULL, *label = NULL;
1195 #if defined(USE_ENGINE) || USE_EVP
1196 EVP_PKEY *pkey = NULL;
1200 if (pub != NULL && pub->keydata.pkey != NULL)
1201 pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
1203 if (pub != NULL && pub->keydata.rsa != NULL) {
1204 pubrsa = pub->keydata.rsa;
1205 pub->keydata.rsa = NULL;
1209 /* read private key file */
1210 ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
1211 if (ret != ISC_R_SUCCESS)
1214 if (key->external && priv.nelements != 0)
1215 DST_RET(DST_R_INVALIDPRIVATEKEY);
1217 for (i = 0; i < priv.nelements; i++) {
1218 switch (priv.elements[i].tag) {
1219 case TAG_RSA_ENGINE:
1220 engine = (char *)priv.elements[i].data;
1223 label = (char *)priv.elements[i].data;
1231 * Is this key is stored in a HSM?
1232 * See if we can fetch it.
1234 if (label != NULL) {
1237 DST_RET(DST_R_NOENGINE);
1238 e = dst__openssl_getengine(engine);
1240 DST_RET(DST_R_NOENGINE);
1241 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1243 DST_RET(dst__openssl_toresult2(
1244 "ENGINE_load_private_key",
1246 key->engine = isc_mem_strdup(key->mctx, engine);
1247 if (key->engine == NULL)
1248 DST_RET(ISC_R_NOMEMORY);
1249 key->label = isc_mem_strdup(key->mctx, label);
1250 if (key->label == NULL)
1251 DST_RET(ISC_R_NOMEMORY);
1252 rsa = EVP_PKEY_get1_RSA(pkey);
1254 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1255 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1256 DST_RET(DST_R_INVALIDPRIVATEKEY);
1257 if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
1258 DST_RET(ISC_R_RANGE);
1261 key->key_size = EVP_PKEY_bits(pkey);
1263 key->keydata.pkey = pkey;
1266 key->keydata.rsa = rsa;
1267 EVP_PKEY_free(pkey);
1269 dst__privstruct_free(&priv, mctx);
1270 memset(&priv, 0, sizeof(priv));
1271 return (ISC_R_SUCCESS);
1273 DST_RET(DST_R_NOENGINE);
1279 DST_RET(ISC_R_NOMEMORY);
1283 pkey = EVP_PKEY_new();
1285 DST_RET(ISC_R_NOMEMORY);
1286 if (!EVP_PKEY_set1_RSA(pkey, rsa))
1287 DST_RET(ISC_R_FAILURE);
1288 key->keydata.pkey = pkey;
1290 key->keydata.rsa = rsa;
1293 for (i = 0; i < priv.nelements; i++) {
1295 switch (priv.elements[i].tag) {
1296 case TAG_RSA_ENGINE:
1303 bn = BN_bin2bn(priv.elements[i].data,
1304 priv.elements[i].length, NULL);
1306 DST_RET(ISC_R_NOMEMORY);
1309 switch (priv.elements[i].tag) {
1310 case TAG_RSA_MODULUS:
1313 case TAG_RSA_PUBLICEXPONENT:
1316 case TAG_RSA_PRIVATEEXPONENT:
1319 case TAG_RSA_PRIME1:
1322 case TAG_RSA_PRIME2:
1325 case TAG_RSA_EXPONENT1:
1328 case TAG_RSA_EXPONENT2:
1331 case TAG_RSA_COEFFICIENT:
1336 dst__privstruct_free(&priv, mctx);
1337 memset(&priv, 0, sizeof(priv));
1339 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1340 DST_RET(DST_R_INVALIDPRIVATEKEY);
1341 if (!key->external) {
1342 if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
1343 DST_RET(ISC_R_RANGE);
1345 key->key_size = BN_num_bits(rsa->n);
1352 return (ISC_R_SUCCESS);
1357 EVP_PKEY_free(pkey);
1363 key->keydata.generic = NULL;
1364 dst__privstruct_free(&priv, mctx);
1365 memset(&priv, 0, sizeof(priv));
1370 opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
1376 EVP_PKEY *pkey = NULL;
1377 RSA *rsa = NULL, *pubrsa = NULL;
1383 DST_RET(DST_R_NOENGINE);
1384 e = dst__openssl_getengine(engine);
1386 DST_RET(DST_R_NOENGINE);
1387 pkey = ENGINE_load_public_key(e, label, NULL, NULL);
1389 pubrsa = EVP_PKEY_get1_RSA(pkey);
1390 EVP_PKEY_free(pkey);
1392 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1394 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1396 DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
1398 if (engine != NULL) {
1399 key->engine = isc_mem_strdup(key->mctx, engine);
1400 if (key->engine == NULL)
1401 DST_RET(ISC_R_NOMEMORY);
1403 key->engine = isc_mem_strdup(key->mctx, label);
1404 if (key->engine == NULL)
1405 DST_RET(ISC_R_NOMEMORY);
1406 colon = strchr(key->engine, ':');
1410 key->label = isc_mem_strdup(key->mctx, label);
1411 if (key->label == NULL)
1412 DST_RET(ISC_R_NOMEMORY);
1413 rsa = EVP_PKEY_get1_RSA(pkey);
1415 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1416 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1417 DST_RET(DST_R_INVALIDPRIVATEKEY);
1418 if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
1419 DST_RET(ISC_R_RANGE);
1422 key->key_size = EVP_PKEY_bits(pkey);
1424 key->keydata.pkey = pkey;
1427 key->keydata.rsa = rsa;
1428 EVP_PKEY_free(pkey);
1430 return (ISC_R_SUCCESS);
1438 EVP_PKEY_free(pkey);
1445 return(DST_R_NOENGINE);
1449 static dst_func_t opensslrsa_functions = {
1450 opensslrsa_createctx,
1451 opensslrsa_destroyctx,
1456 NULL, /*%< computesecret */
1458 NULL, /*%< paramcompare */
1459 opensslrsa_generate,
1460 opensslrsa_isprivate,
1466 NULL, /*%< cleanup */
1467 opensslrsa_fromlabel,
1469 NULL, /*%< restore */
1473 dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
1474 REQUIRE(funcp != NULL);
1476 if (*funcp == NULL) {
1477 switch (algorithm) {
1478 case DST_ALG_RSASHA256:
1479 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1480 *funcp = &opensslrsa_functions;
1483 case DST_ALG_RSASHA512:
1484 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1485 *funcp = &opensslrsa_functions;
1489 *funcp = &opensslrsa_functions;
1493 return (ISC_R_SUCCESS);
1498 #include <isc/util.h>
1500 EMPTY_TRANSLATION_UNIT
1502 #endif /* OPENSSL */