2 * Copyright (C) 2004-2009, 2011, 2012, 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 * 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_toresult3(dctx->category,
163 dctx->ctxdata.evp_md_ctx = evp_md_ctx;
165 switch (dctx->key->key_alg) {
170 md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
172 return (ISC_R_NOMEMORY);
173 isc_md5_init(md5ctx);
174 dctx->ctxdata.md5ctx = md5ctx;
177 case DST_ALG_RSASHA1:
178 case DST_ALG_NSEC3RSASHA1:
182 sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
184 return (ISC_R_NOMEMORY);
185 isc_sha1_init(sha1ctx);
186 dctx->ctxdata.sha1ctx = sha1ctx;
189 case DST_ALG_RSASHA256:
191 isc_sha256_t *sha256ctx;
193 sha256ctx = isc_mem_get(dctx->mctx,
194 sizeof(isc_sha256_t));
195 if (sha256ctx == NULL)
196 return (ISC_R_NOMEMORY);
197 isc_sha256_init(sha256ctx);
198 dctx->ctxdata.sha256ctx = sha256ctx;
201 case DST_ALG_RSASHA512:
203 isc_sha512_t *sha512ctx;
205 sha512ctx = isc_mem_get(dctx->mctx,
206 sizeof(isc_sha512_t));
207 if (sha512ctx == NULL)
208 return (ISC_R_NOMEMORY);
209 isc_sha512_init(sha512ctx);
210 dctx->ctxdata.sha512ctx = sha512ctx;
218 return (ISC_R_SUCCESS);
222 opensslrsa_destroyctx(dst_context_t *dctx) {
224 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
227 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
228 dctx->key->key_alg == DST_ALG_RSASHA1 ||
229 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
230 dctx->key->key_alg == DST_ALG_RSASHA256 ||
231 dctx->key->key_alg == DST_ALG_RSASHA512);
234 if (evp_md_ctx != NULL) {
235 EVP_MD_CTX_destroy(evp_md_ctx);
236 dctx->ctxdata.evp_md_ctx = NULL;
239 switch (dctx->key->key_alg) {
242 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
244 if (md5ctx != NULL) {
245 isc_md5_invalidate(md5ctx);
246 isc_mem_put(dctx->mctx, md5ctx,
248 dctx->ctxdata.md5ctx = NULL;
252 case DST_ALG_RSASHA1:
253 case DST_ALG_NSEC3RSASHA1:
255 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
257 if (sha1ctx != NULL) {
258 isc_sha1_invalidate(sha1ctx);
259 isc_mem_put(dctx->mctx, sha1ctx,
261 dctx->ctxdata.sha1ctx = NULL;
265 case DST_ALG_RSASHA256:
267 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
269 if (sha256ctx != NULL) {
270 isc_sha256_invalidate(sha256ctx);
271 isc_mem_put(dctx->mctx, sha256ctx,
272 sizeof(isc_sha256_t));
273 dctx->ctxdata.sha256ctx = NULL;
277 case DST_ALG_RSASHA512:
279 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
281 if (sha512ctx != NULL) {
282 isc_sha512_invalidate(sha512ctx);
283 isc_mem_put(dctx->mctx, sha512ctx,
284 sizeof(isc_sha512_t));
285 dctx->ctxdata.sha512ctx = NULL;
296 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
298 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
301 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
302 dctx->key->key_alg == DST_ALG_RSASHA1 ||
303 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
304 dctx->key->key_alg == DST_ALG_RSASHA256 ||
305 dctx->key->key_alg == DST_ALG_RSASHA512);
308 if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
309 return (dst__openssl_toresult3(dctx->category,
314 switch (dctx->key->key_alg) {
317 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
319 isc_md5_update(md5ctx, data->base, data->length);
322 case DST_ALG_RSASHA1:
323 case DST_ALG_NSEC3RSASHA1:
325 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
327 isc_sha1_update(sha1ctx, data->base, data->length);
330 case DST_ALG_RSASHA256:
332 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
334 isc_sha256_update(sha256ctx, data->base, data->length);
337 case DST_ALG_RSASHA512:
339 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
341 isc_sha512_update(sha512ctx, data->base, data->length);
348 return (ISC_R_SUCCESS);
351 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
353 * Digest prefixes from RFC 5702.
355 static unsigned char sha256_prefix[] =
356 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
357 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
358 static unsigned char sha512_prefix[] =
359 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
360 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
361 #define PREFIXLEN sizeof(sha512_prefix)
367 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
368 dst_key_t *key = dctx->key;
370 unsigned int siglen = 0;
372 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
373 EVP_PKEY *pkey = key->keydata.pkey;
375 RSA *rsa = key->keydata.rsa;
376 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
377 unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
380 unsigned int digestlen = 0;
381 #if OPENSSL_VERSION_NUMBER < 0x00908000L
382 unsigned int prefixlen = 0;
383 const unsigned char *prefix = NULL;
387 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
388 dctx->key->key_alg == DST_ALG_RSASHA1 ||
389 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
390 dctx->key->key_alg == DST_ALG_RSASHA256 ||
391 dctx->key->key_alg == DST_ALG_RSASHA512);
393 isc_buffer_availableregion(sig, &r);
396 if (r.length < (unsigned int) EVP_PKEY_size(pkey))
397 return (ISC_R_NOSPACE);
399 if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
400 return (dst__openssl_toresult3(dctx->category,
405 if (r.length < (unsigned int) RSA_size(rsa))
406 return (ISC_R_NOSPACE);
408 switch (dctx->key->key_alg) {
411 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
413 isc_md5_final(md5ctx, digest);
415 digestlen = ISC_MD5_DIGESTLENGTH;
418 case DST_ALG_RSASHA1:
419 case DST_ALG_NSEC3RSASHA1:
421 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
423 isc_sha1_final(sha1ctx, digest);
425 digestlen = ISC_SHA1_DIGESTLENGTH;
428 case DST_ALG_RSASHA256:
430 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
432 isc_sha256_final(digest, sha256ctx);
433 digestlen = ISC_SHA256_DIGESTLENGTH;
434 #if OPENSSL_VERSION_NUMBER < 0x00908000L
435 prefix = sha256_prefix;
436 prefixlen = sizeof(sha256_prefix);
442 case DST_ALG_RSASHA512:
444 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
446 isc_sha512_final(digest, sha512ctx);
447 digestlen = ISC_SHA512_DIGESTLENGTH;
448 #if OPENSSL_VERSION_NUMBER < 0x00908000L
449 prefix = sha512_prefix;
450 prefixlen = sizeof(sha512_prefix);
460 #if OPENSSL_VERSION_NUMBER < 0x00908000L
461 switch (dctx->key->key_alg) {
463 case DST_ALG_RSASHA1:
464 case DST_ALG_NSEC3RSASHA1:
466 status = RSA_sign(type, digest, digestlen, r.base,
470 case DST_ALG_RSASHA256:
471 case DST_ALG_RSASHA512:
472 INSIST(prefix != NULL);
473 INSIST(prefixlen != 0);
474 INSIST(prefixlen + digestlen <= sizeof(digest));
476 memmove(digest + prefixlen, digest, digestlen);
477 memmove(digest, prefix, prefixlen);
478 status = RSA_private_encrypt(digestlen + prefixlen,
492 status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
495 return (dst__openssl_toresult3(dctx->category,
497 DST_R_OPENSSLFAILURE));
500 isc_buffer_add(sig, siglen);
502 return (ISC_R_SUCCESS);
506 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
507 dst_key_t *key = dctx->key;
510 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
511 EVP_PKEY *pkey = key->keydata.pkey;
513 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
514 unsigned char digest[ISC_SHA512_DIGESTLENGTH];
516 unsigned int digestlen = 0;
517 RSA *rsa = key->keydata.rsa;
518 #if OPENSSL_VERSION_NUMBER < 0x00908000L
519 unsigned int prefixlen = 0;
520 const unsigned char *prefix = NULL;
524 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
525 dctx->key->key_alg == DST_ALG_RSASHA1 ||
526 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
527 dctx->key->key_alg == DST_ALG_RSASHA256 ||
528 dctx->key->key_alg == DST_ALG_RSASHA512);
531 status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
534 return (ISC_R_SUCCESS);
536 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
538 return (dst__openssl_toresult3(dctx->category,
540 DST_R_VERIFYFAILURE));
543 switch (dctx->key->key_alg) {
546 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
548 isc_md5_final(md5ctx, digest);
550 digestlen = ISC_MD5_DIGESTLENGTH;
553 case DST_ALG_RSASHA1:
554 case DST_ALG_NSEC3RSASHA1:
556 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
558 isc_sha1_final(sha1ctx, digest);
560 digestlen = ISC_SHA1_DIGESTLENGTH;
563 case DST_ALG_RSASHA256:
565 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
567 isc_sha256_final(digest, sha256ctx);
568 digestlen = ISC_SHA256_DIGESTLENGTH;
569 #if OPENSSL_VERSION_NUMBER < 0x00908000L
570 prefix = sha256_prefix;
571 prefixlen = sizeof(sha256_prefix);
577 case DST_ALG_RSASHA512:
579 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
581 isc_sha512_final(digest, sha512ctx);
582 digestlen = ISC_SHA512_DIGESTLENGTH;
583 #if OPENSSL_VERSION_NUMBER < 0x00908000L
584 prefix = sha512_prefix;
585 prefixlen = sizeof(sha512_prefix);
595 if (sig->length != (unsigned int) RSA_size(rsa))
596 return (DST_R_VERIFYFAILURE);
598 #if OPENSSL_VERSION_NUMBER < 0x00908000L
599 switch (dctx->key->key_alg) {
601 case DST_ALG_RSASHA1:
602 case DST_ALG_NSEC3RSASHA1:
604 status = RSA_verify(type, digest, digestlen, sig->base,
608 case DST_ALG_RSASHA256:
609 case DST_ALG_RSASHA512:
612 * 1024 is big enough for all valid RSA bit sizes
613 * for use with DNSSEC.
615 unsigned char original[PREFIXLEN + 1024];
617 INSIST(prefix != NULL);
618 INSIST(prefixlen != 0U);
620 if (RSA_size(rsa) > (int)sizeof(original))
621 return (DST_R_VERIFYFAILURE);
623 status = RSA_public_decrypt(sig->length, sig->base,
627 return (dst__openssl_toresult3(
629 "RSA_public_decrypt",
630 DST_R_VERIFYFAILURE));
631 if (status != (int)(prefixlen + digestlen))
632 return (DST_R_VERIFYFAILURE);
633 if (memcmp(original, prefix, prefixlen))
634 return (DST_R_VERIFYFAILURE);
635 if (memcmp(original + prefixlen, digest, digestlen))
636 return (DST_R_VERIFYFAILURE);
646 status = RSA_verify(type, digest, digestlen, sig->base,
650 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
651 return (ISC_R_SUCCESS);
656 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
658 RSA *rsa1 = NULL, *rsa2 = NULL;
660 EVP_PKEY *pkey1, *pkey2;
664 pkey1 = key1->keydata.pkey;
665 pkey2 = key2->keydata.pkey;
667 * The pkey reference will keep these around after
668 * the RSA_free() call.
671 rsa1 = EVP_PKEY_get1_RSA(pkey1);
675 rsa2 = EVP_PKEY_get1_RSA(pkey2);
679 rsa1 = key1->keydata.rsa;
680 rsa2 = key2->keydata.rsa;
683 if (rsa1 == NULL && rsa2 == NULL)
685 else if (rsa1 == NULL || rsa2 == NULL)
688 status = BN_cmp(rsa1->n, rsa2->n) ||
689 BN_cmp(rsa1->e, rsa2->e);
695 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
696 (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
697 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
698 (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
701 * Can't compare private parameters, BTW does it make sense?
707 if (rsa1->d != NULL || rsa2->d != NULL) {
708 if (rsa1->d == NULL || rsa2->d == NULL)
710 status = BN_cmp(rsa1->d, rsa2->d) ||
711 BN_cmp(rsa1->p, rsa2->p) ||
712 BN_cmp(rsa1->q, rsa2->q);
720 #if OPENSSL_VERSION_NUMBER > 0x00908000L
722 progress_cb(int p, int n, BN_GENCB *cb)
739 opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
740 #if OPENSSL_VERSION_NUMBER > 0x00908000L
741 isc_result_t ret = DST_R_OPENSSLFAILURE;
747 RSA *rsa = RSA_new();
748 BIGNUM *e = BN_new();
750 EVP_PKEY *pkey = EVP_PKEY_new();
753 if (rsa == NULL || e == NULL)
758 if (!EVP_PKEY_set1_RSA(pkey, rsa))
772 if (callback == NULL) {
773 BN_GENCB_set_old(&cb, NULL, NULL);
776 BN_GENCB_set(&cb, &progress_cb, u.dptr);
779 if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
783 key->keydata.pkey = pkey;
787 key->keydata.rsa = rsa;
789 return (ISC_R_SUCCESS);
791 ret = dst__openssl_toresult2("RSA_generate_key_ex",
792 DST_R_OPENSSLFAILURE);
803 return (dst__openssl_toresult(ret));
808 EVP_PKEY *pkey = EVP_PKEY_new();
813 return (ISC_R_NOMEMORY);
822 rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
827 return (dst__openssl_toresult2("RSA_generate_key",
828 DST_R_OPENSSLFAILURE));
832 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
835 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
837 key->keydata.pkey = pkey;
840 key->keydata.rsa = rsa;
843 return (ISC_R_SUCCESS);
848 opensslrsa_isprivate(const dst_key_t *key) {
850 RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
853 /* key->keydata.pkey still has a reference so rsa is still valid. */
855 RSA *rsa = key->keydata.rsa;
857 if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
859 return (ISC_TF(rsa != NULL && rsa->d != NULL));
863 opensslrsa_destroy(dst_key_t *key) {
865 EVP_PKEY *pkey = key->keydata.pkey;
867 key->keydata.pkey = NULL;
869 RSA *rsa = key->keydata.rsa;
871 key->keydata.rsa = NULL;
877 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
879 unsigned int e_bytes;
880 unsigned int mod_bytes;
888 REQUIRE(key->keydata.pkey != NULL);
890 REQUIRE(key->keydata.rsa != NULL);
894 pkey = key->keydata.pkey;
895 rsa = EVP_PKEY_get1_RSA(pkey);
897 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
899 rsa = key->keydata.rsa;
902 isc_buffer_availableregion(data, &r);
904 e_bytes = BN_num_bytes(rsa->e);
905 mod_bytes = BN_num_bytes(rsa->n);
907 if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
909 DST_RET(ISC_R_NOSPACE);
910 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
911 isc_region_consume(&r, 1);
914 DST_RET(ISC_R_NOSPACE);
915 isc_buffer_putuint8(data, 0);
916 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
917 isc_region_consume(&r, 3);
920 if (r.length < e_bytes + mod_bytes)
921 DST_RET(ISC_R_NOSPACE);
923 BN_bn2bin(rsa->e, r.base);
924 isc_region_consume(&r, e_bytes);
925 BN_bn2bin(rsa->n, r.base);
927 isc_buffer_add(data, e_bytes + mod_bytes);
939 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
942 unsigned int e_bytes;
947 isc_buffer_remainingregion(data, &r);
949 return (ISC_R_SUCCESS);
953 return (dst__openssl_toresult(ISC_R_NOMEMORY));
958 return (DST_R_INVALIDPUBLICKEY);
966 return (DST_R_INVALIDPUBLICKEY);
968 e_bytes = ((*r.base++) << 8);
969 e_bytes += *r.base++;
973 if (r.length < e_bytes) {
975 return (DST_R_INVALIDPUBLICKEY);
977 rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
981 rsa->n = BN_bin2bn(r.base, r.length, NULL);
983 key->key_size = BN_num_bits(rsa->n);
985 isc_buffer_forward(data, r.length);
988 pkey = EVP_PKEY_new();
991 return (ISC_R_NOMEMORY);
993 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
996 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
998 key->keydata.pkey = pkey;
1001 key->keydata.rsa = rsa;
1004 return (ISC_R_SUCCESS);
1008 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
1012 unsigned char *bufs[8];
1013 isc_result_t result;
1016 if (key->keydata.pkey == NULL)
1017 return (DST_R_NULLKEY);
1018 rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
1020 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1022 if (key->keydata.rsa == NULL)
1023 return (DST_R_NULLKEY);
1024 rsa = key->keydata.rsa;
1027 memset(bufs, 0, sizeof(bufs));
1028 for (i = 0; i < 8; i++) {
1029 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
1030 if (bufs[i] == NULL) {
1031 result = ISC_R_NOMEMORY;
1038 priv.elements[i].tag = TAG_RSA_MODULUS;
1039 priv.elements[i].length = BN_num_bytes(rsa->n);
1040 BN_bn2bin(rsa->n, bufs[i]);
1041 priv.elements[i].data = bufs[i];
1044 priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
1045 priv.elements[i].length = BN_num_bytes(rsa->e);
1046 BN_bn2bin(rsa->e, bufs[i]);
1047 priv.elements[i].data = bufs[i];
1050 if (rsa->d != NULL) {
1051 priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
1052 priv.elements[i].length = BN_num_bytes(rsa->d);
1053 BN_bn2bin(rsa->d, bufs[i]);
1054 priv.elements[i].data = bufs[i];
1058 if (rsa->p != NULL) {
1059 priv.elements[i].tag = TAG_RSA_PRIME1;
1060 priv.elements[i].length = BN_num_bytes(rsa->p);
1061 BN_bn2bin(rsa->p, bufs[i]);
1062 priv.elements[i].data = bufs[i];
1066 if (rsa->q != NULL) {
1067 priv.elements[i].tag = TAG_RSA_PRIME2;
1068 priv.elements[i].length = BN_num_bytes(rsa->q);
1069 BN_bn2bin(rsa->q, bufs[i]);
1070 priv.elements[i].data = bufs[i];
1074 if (rsa->dmp1 != NULL) {
1075 priv.elements[i].tag = TAG_RSA_EXPONENT1;
1076 priv.elements[i].length = BN_num_bytes(rsa->dmp1);
1077 BN_bn2bin(rsa->dmp1, bufs[i]);
1078 priv.elements[i].data = bufs[i];
1082 if (rsa->dmq1 != NULL) {
1083 priv.elements[i].tag = TAG_RSA_EXPONENT2;
1084 priv.elements[i].length = BN_num_bytes(rsa->dmq1);
1085 BN_bn2bin(rsa->dmq1, bufs[i]);
1086 priv.elements[i].data = bufs[i];
1090 if (rsa->iqmp != NULL) {
1091 priv.elements[i].tag = TAG_RSA_COEFFICIENT;
1092 priv.elements[i].length = BN_num_bytes(rsa->iqmp);
1093 BN_bn2bin(rsa->iqmp, bufs[i]);
1094 priv.elements[i].data = bufs[i];
1098 if (key->engine != NULL) {
1099 priv.elements[i].tag = TAG_RSA_ENGINE;
1100 priv.elements[i].length = strlen(key->engine) + 1;
1101 priv.elements[i].data = (unsigned char *)key->engine;
1105 if (key->label != NULL) {
1106 priv.elements[i].tag = TAG_RSA_LABEL;
1107 priv.elements[i].length = strlen(key->label) + 1;
1108 priv.elements[i].data = (unsigned char *)key->label;
1114 result = dst__privstruct_writefile(key, &priv, directory);
1119 for (i = 0; i < 8; i++) {
1120 if (bufs[i] == NULL)
1122 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
1128 rsa_check(RSA *rsa, RSA *pub)
1130 /* Public parameters should be the same but if they are not set
1131 * copy them from the public key. */
1133 if (rsa->n != NULL) {
1134 if (BN_cmp(rsa->n, pub->n) != 0)
1135 return (DST_R_INVALIDPRIVATEKEY);
1140 if (rsa->e != NULL) {
1141 if (BN_cmp(rsa->e, pub->e) != 0)
1142 return (DST_R_INVALIDPRIVATEKEY);
1148 if (rsa->n == NULL || rsa->e == NULL)
1149 return (DST_R_INVALIDPRIVATEKEY);
1150 return (ISC_R_SUCCESS);
1154 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
1158 RSA *rsa = NULL, *pubrsa = NULL;
1162 isc_mem_t *mctx = key->mctx;
1163 const char *engine = NULL, *label = NULL;
1164 #if defined(USE_ENGINE) || USE_EVP
1165 EVP_PKEY *pkey = NULL;
1169 if (pub != NULL && pub->keydata.pkey != NULL)
1170 pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
1172 if (pub != NULL && pub->keydata.rsa != NULL) {
1173 pubrsa = pub->keydata.rsa;
1174 pub->keydata.rsa = NULL;
1178 /* read private key file */
1179 ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
1180 if (ret != ISC_R_SUCCESS)
1183 for (i = 0; i < priv.nelements; i++) {
1184 switch (priv.elements[i].tag) {
1185 case TAG_RSA_ENGINE:
1186 engine = (char *)priv.elements[i].data;
1189 label = (char *)priv.elements[i].data;
1196 * Is this key is stored in a HSM?
1197 * See if we can fetch it.
1199 if (label != NULL) {
1202 DST_RET(DST_R_NOENGINE);
1203 e = dst__openssl_getengine(engine);
1205 DST_RET(DST_R_NOENGINE);
1206 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1208 DST_RET(dst__openssl_toresult2(
1209 "ENGINE_load_private_key",
1211 key->engine = isc_mem_strdup(key->mctx, engine);
1212 if (key->engine == NULL)
1213 DST_RET(ISC_R_NOMEMORY);
1214 key->label = isc_mem_strdup(key->mctx, label);
1215 if (key->label == NULL)
1216 DST_RET(ISC_R_NOMEMORY);
1217 rsa = EVP_PKEY_get1_RSA(pkey);
1219 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1220 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1221 DST_RET(DST_R_INVALIDPRIVATEKEY);
1224 key->key_size = EVP_PKEY_bits(pkey);
1226 key->keydata.pkey = pkey;
1229 key->keydata.rsa = rsa;
1230 EVP_PKEY_free(pkey);
1232 dst__privstruct_free(&priv, mctx);
1233 memset(&priv, 0, sizeof(priv));
1234 return (ISC_R_SUCCESS);
1236 DST_RET(DST_R_NOENGINE);
1242 DST_RET(ISC_R_NOMEMORY);
1246 pkey = EVP_PKEY_new();
1248 DST_RET(ISC_R_NOMEMORY);
1249 if (!EVP_PKEY_set1_RSA(pkey, rsa))
1250 DST_RET(ISC_R_FAILURE);
1251 key->keydata.pkey = pkey;
1253 key->keydata.rsa = rsa;
1256 for (i = 0; i < priv.nelements; i++) {
1258 switch (priv.elements[i].tag) {
1259 case TAG_RSA_ENGINE:
1266 bn = BN_bin2bn(priv.elements[i].data,
1267 priv.elements[i].length, NULL);
1269 DST_RET(ISC_R_NOMEMORY);
1272 switch (priv.elements[i].tag) {
1273 case TAG_RSA_MODULUS:
1276 case TAG_RSA_PUBLICEXPONENT:
1279 case TAG_RSA_PRIVATEEXPONENT:
1282 case TAG_RSA_PRIME1:
1285 case TAG_RSA_PRIME2:
1288 case TAG_RSA_EXPONENT1:
1291 case TAG_RSA_EXPONENT2:
1294 case TAG_RSA_COEFFICIENT:
1299 dst__privstruct_free(&priv, mctx);
1300 memset(&priv, 0, sizeof(priv));
1302 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1303 DST_RET(DST_R_INVALIDPRIVATEKEY);
1304 key->key_size = BN_num_bits(rsa->n);
1311 return (ISC_R_SUCCESS);
1316 EVP_PKEY_free(pkey);
1322 opensslrsa_destroy(key);
1323 dst__privstruct_free(&priv, mctx);
1324 memset(&priv, 0, sizeof(priv));
1329 opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
1335 EVP_PKEY *pkey = NULL;
1336 RSA *rsa = NULL, *pubrsa = NULL;
1342 DST_RET(DST_R_NOENGINE);
1343 e = dst__openssl_getengine(engine);
1345 DST_RET(DST_R_NOENGINE);
1346 pkey = ENGINE_load_public_key(e, label, NULL, NULL);
1348 pubrsa = EVP_PKEY_get1_RSA(pkey);
1349 EVP_PKEY_free(pkey);
1351 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1353 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1355 DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
1357 if (engine != NULL) {
1358 key->engine = isc_mem_strdup(key->mctx, engine);
1359 if (key->engine == NULL)
1360 DST_RET(ISC_R_NOMEMORY);
1362 key->engine = isc_mem_strdup(key->mctx, label);
1363 if (key->engine == NULL)
1364 DST_RET(ISC_R_NOMEMORY);
1365 colon = strchr(key->engine, ':');
1369 key->label = isc_mem_strdup(key->mctx, label);
1370 if (key->label == NULL)
1371 DST_RET(ISC_R_NOMEMORY);
1372 rsa = EVP_PKEY_get1_RSA(pkey);
1374 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1375 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1376 DST_RET(DST_R_INVALIDPRIVATEKEY);
1379 key->key_size = EVP_PKEY_bits(pkey);
1381 key->keydata.pkey = pkey;
1384 key->keydata.rsa = rsa;
1385 EVP_PKEY_free(pkey);
1387 return (ISC_R_SUCCESS);
1395 EVP_PKEY_free(pkey);
1402 return(DST_R_NOENGINE);
1406 static dst_func_t opensslrsa_functions = {
1407 opensslrsa_createctx,
1408 opensslrsa_destroyctx,
1412 NULL, /*%< computesecret */
1414 NULL, /*%< paramcompare */
1415 opensslrsa_generate,
1416 opensslrsa_isprivate,
1422 NULL, /*%< cleanup */
1423 opensslrsa_fromlabel,
1425 NULL, /*%< restore */
1429 dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
1430 REQUIRE(funcp != NULL);
1432 if (*funcp == NULL) {
1433 switch (algorithm) {
1434 case DST_ALG_RSASHA256:
1435 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1436 *funcp = &opensslrsa_functions;
1439 case DST_ALG_RSASHA512:
1440 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1441 *funcp = &opensslrsa_functions;
1445 *funcp = &opensslrsa_functions;
1449 return (ISC_R_SUCCESS);
1454 #include <isc/util.h>
1456 EMPTY_TRANSLATION_UNIT
1458 #endif /* OPENSSL */