2 * Copyright (C) 2004-2010 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
20 * $Id: opensslrsa_link.c,v 1.20.50.8 2010/01/22 02:36:49 marka Exp $
26 #if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
33 #include <isc/entropy.h>
38 #include <isc/string.h>
41 #include <dst/result.h>
43 #include "dst_internal.h"
44 #include "dst_openssl.h"
45 #include "dst_parse.h"
47 #include <openssl/err.h>
48 #include <openssl/objects.h>
49 #include <openssl/rsa.h>
50 #if OPENSSL_VERSION_NUMBER > 0x00908000L
51 #include <openssl/bn.h>
53 #include <openssl/engine.h>
56 * We don't use configure for windows so enforce the OpenSSL version
57 * here. Unlike with configure we don't support overriding this test.
60 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
61 OPENSSL_VERSION_NUMBER < 0x00908000L) || \
62 OPENSSL_VERSION_NUMBER >= 0x0090804fL)
63 #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
69 * XXXMPA Temporarily disable RSA_BLINDING as it requires
70 * good quality random data that cannot currently be guaranteed.
71 * XXXMPA Find which versions of openssl use pseudo random data
72 * and set RSA_FLAG_BLINDING for those.
76 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
77 #define SET_FLAGS(rsa) \
79 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
80 (rsa)->flags |= RSA_FLAG_BLINDING; \
83 #define SET_FLAGS(rsa) \
85 (rsa)->flags |= RSA_FLAG_BLINDING; \
90 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
91 #define SET_FLAGS(rsa) \
93 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
94 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
96 #elif defined(RSA_FLAG_NO_BLINDING)
97 #define SET_FLAGS(rsa) \
99 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
100 (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
103 #define SET_FLAGS(rsa) \
105 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
109 #define DST_RET(a) {ret = a; goto err;}
111 static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
114 opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
116 EVP_MD_CTX *evp_md_ctx;
117 const EVP_MD *type = NULL;
121 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
122 dctx->key->key_alg == DST_ALG_RSASHA1 ||
123 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
124 dctx->key->key_alg == DST_ALG_RSASHA256 ||
125 dctx->key->key_alg == DST_ALG_RSASHA512);
128 evp_md_ctx = EVP_MD_CTX_create();
129 if (evp_md_ctx == NULL)
130 return (ISC_R_NOMEMORY);
132 switch (dctx->key->key_alg) {
134 type = EVP_md5(); /* MD5 + RSA */
136 case DST_ALG_RSASHA1:
137 case DST_ALG_NSEC3RSASHA1:
138 type = EVP_sha1(); /* SHA1 + RSA */
140 #ifdef HAVE_EVP_SHA256
141 case DST_ALG_RSASHA256:
142 type = EVP_sha256(); /* SHA256 + RSA */
145 #ifdef HAVE_EVP_SHA512
146 case DST_ALG_RSASHA512:
154 if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
155 EVP_MD_CTX_destroy(evp_md_ctx);
156 return (ISC_R_FAILURE);
158 dctx->ctxdata.evp_md_ctx = evp_md_ctx;
160 switch (dctx->key->key_alg) {
165 md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
167 return (ISC_R_NOMEMORY);
168 isc_md5_init(md5ctx);
169 dctx->ctxdata.md5ctx = md5ctx;
172 case DST_ALG_RSASHA1:
173 case DST_ALG_NSEC3RSASHA1:
177 sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
179 return (ISC_R_NOMEMORY);
180 isc_sha1_init(sha1ctx);
181 dctx->ctxdata.sha1ctx = sha1ctx;
184 case DST_ALG_RSASHA256:
186 isc_sha256_t *sha256ctx;
188 sha256ctx = isc_mem_get(dctx->mctx,
189 sizeof(isc_sha256_t));
190 if (sha256ctx == NULL)
191 return (ISC_R_NOMEMORY);
192 isc_sha256_init(sha256ctx);
193 dctx->ctxdata.sha256ctx = sha256ctx;
196 case DST_ALG_RSASHA512:
198 isc_sha512_t *sha512ctx;
200 sha512ctx = isc_mem_get(dctx->mctx,
201 sizeof(isc_sha512_t));
202 if (sha512ctx == NULL)
203 return (ISC_R_NOMEMORY);
204 isc_sha512_init(sha512ctx);
205 dctx->ctxdata.sha512ctx = sha512ctx;
213 return (ISC_R_SUCCESS);
217 opensslrsa_destroyctx(dst_context_t *dctx) {
219 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
222 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
223 dctx->key->key_alg == DST_ALG_RSASHA1 ||
224 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
225 dctx->key->key_alg == DST_ALG_RSASHA256 ||
226 dctx->key->key_alg == DST_ALG_RSASHA512);
229 if (evp_md_ctx != NULL) {
230 EVP_MD_CTX_destroy(evp_md_ctx);
231 dctx->ctxdata.evp_md_ctx = NULL;
234 switch (dctx->key->key_alg) {
237 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
239 if (md5ctx != NULL) {
240 isc_md5_invalidate(md5ctx);
241 isc_mem_put(dctx->mctx, md5ctx,
243 dctx->ctxdata.md5ctx = NULL;
247 case DST_ALG_RSASHA1:
248 case DST_ALG_NSEC3RSASHA1:
250 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
252 if (sha1ctx != NULL) {
253 isc_sha1_invalidate(sha1ctx);
254 isc_mem_put(dctx->mctx, sha1ctx,
256 dctx->ctxdata.sha1ctx = NULL;
260 case DST_ALG_RSASHA256:
262 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
264 if (sha256ctx != NULL) {
265 isc_sha256_invalidate(sha256ctx);
266 isc_mem_put(dctx->mctx, sha256ctx,
267 sizeof(isc_sha256_t));
268 dctx->ctxdata.sha256ctx = NULL;
272 case DST_ALG_RSASHA512:
274 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
276 if (sha512ctx != NULL) {
277 isc_sha512_invalidate(sha512ctx);
278 isc_mem_put(dctx->mctx, sha512ctx,
279 sizeof(isc_sha512_t));
280 dctx->ctxdata.sha512ctx = NULL;
291 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
293 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
296 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
297 dctx->key->key_alg == DST_ALG_RSASHA1 ||
298 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
299 dctx->key->key_alg == DST_ALG_RSASHA256 ||
300 dctx->key->key_alg == DST_ALG_RSASHA512);
303 if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
304 return (ISC_R_FAILURE);
307 switch (dctx->key->key_alg) {
310 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
312 isc_md5_update(md5ctx, data->base, data->length);
315 case DST_ALG_RSASHA1:
316 case DST_ALG_NSEC3RSASHA1:
318 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
320 isc_sha1_update(sha1ctx, data->base, data->length);
323 case DST_ALG_RSASHA256:
325 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
327 isc_sha256_update(sha256ctx, data->base, data->length);
330 case DST_ALG_RSASHA512:
332 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
334 isc_sha512_update(sha512ctx, data->base, data->length);
341 return (ISC_R_SUCCESS);
344 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
346 * Digest prefixes from RFC 5702.
348 static unsigned char sha256_prefix[] =
349 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
350 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
351 static unsigned char sha512_prefix[] =
352 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
353 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
354 #define PREFIXLEN sizeof(sha512_prefix)
360 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
361 dst_key_t *key = dctx->key;
363 unsigned int siglen = 0;
365 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
366 EVP_PKEY *pkey = key->keydata.pkey;
368 RSA *rsa = key->keydata.rsa;
369 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
370 unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
373 unsigned int digestlen = 0;
378 #if OPENSSL_VERSION_NUMBER < 0x00908000L
379 unsigned int prefixlen = 0;
380 const unsigned char *prefix = NULL;
384 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
385 dctx->key->key_alg == DST_ALG_RSASHA1 ||
386 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
387 dctx->key->key_alg == DST_ALG_RSASHA256 ||
388 dctx->key->key_alg == DST_ALG_RSASHA512);
390 isc_buffer_availableregion(sig, &r);
393 if (r.length < (unsigned int) EVP_PKEY_size(pkey))
394 return (ISC_R_NOSPACE);
396 if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
397 return (ISC_R_FAILURE);
400 if (r.length < (unsigned int) RSA_size(rsa))
401 return (ISC_R_NOSPACE);
403 switch (dctx->key->key_alg) {
406 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
408 isc_md5_final(md5ctx, digest);
410 digestlen = ISC_MD5_DIGESTLENGTH;
413 case DST_ALG_RSASHA1:
414 case DST_ALG_NSEC3RSASHA1:
416 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
418 isc_sha1_final(sha1ctx, digest);
420 digestlen = ISC_SHA1_DIGESTLENGTH;
423 case DST_ALG_RSASHA256:
425 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
427 isc_sha256_final(digest, sha256ctx);
428 digestlen = ISC_SHA256_DIGESTLENGTH;
429 #if OPENSSL_VERSION_NUMBER < 0x00908000L
430 prefix = sha256_prefix;
431 prefixlen = sizeof(sha256_prefix);
437 case DST_ALG_RSASHA512:
439 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
441 isc_sha512_final(digest, sha512ctx);
442 digestlen = ISC_SHA512_DIGESTLENGTH;
443 #if OPENSSL_VERSION_NUMBER < 0x00908000L
444 prefix = sha512_prefix;
445 prefixlen = sizeof(sha512_prefix);
455 #if OPENSSL_VERSION_NUMBER < 0x00908000L
456 switch (dctx->key->key_alg) {
458 case DST_ALG_RSASHA1:
459 case DST_ALG_NSEC3RSASHA1:
461 status = RSA_sign(type, digest, digestlen, r.base,
465 case DST_ALG_RSASHA256:
466 case DST_ALG_RSASHA512:
467 INSIST(prefix != NULL);
468 INSIST(prefixlen != 0);
469 INSIST(prefixlen + digestlen <= sizeof(digest));
471 memmove(digest + prefixlen, digest, digestlen);
472 memcpy(digest, prefix, prefixlen);
473 status = RSA_private_encrypt(digestlen + prefixlen,
487 status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
490 err = ERR_peek_error_line(&file, &line);
492 message = ERR_error_string(err, NULL);
494 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
498 isc_buffer_add(sig, siglen);
500 return (ISC_R_SUCCESS);
504 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
505 dst_key_t *key = dctx->key;
508 EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
509 EVP_PKEY *pkey = key->keydata.pkey;
511 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
512 unsigned char digest[ISC_SHA512_DIGESTLENGTH];
514 unsigned int digestlen = 0;
515 RSA *rsa = key->keydata.rsa;
516 #if OPENSSL_VERSION_NUMBER < 0x00908000L
517 unsigned int prefixlen = 0;
518 const unsigned char *prefix = NULL;
522 REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
523 dctx->key->key_alg == DST_ALG_RSASHA1 ||
524 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
525 dctx->key->key_alg == DST_ALG_RSASHA256 ||
526 dctx->key->key_alg == DST_ALG_RSASHA512);
529 status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
531 switch (dctx->key->key_alg) {
534 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
536 isc_md5_final(md5ctx, digest);
538 digestlen = ISC_MD5_DIGESTLENGTH;
541 case DST_ALG_RSASHA1:
542 case DST_ALG_NSEC3RSASHA1:
544 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
546 isc_sha1_final(sha1ctx, digest);
548 digestlen = ISC_SHA1_DIGESTLENGTH;
551 case DST_ALG_RSASHA256:
553 isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
555 isc_sha256_final(digest, sha256ctx);
556 digestlen = ISC_SHA256_DIGESTLENGTH;
557 #if OPENSSL_VERSION_NUMBER < 0x00908000L
558 prefix = sha256_prefix;
559 prefixlen = sizeof(sha256_prefix);
565 case DST_ALG_RSASHA512:
567 isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
569 isc_sha512_final(digest, sha512ctx);
570 digestlen = ISC_SHA512_DIGESTLENGTH;
571 #if OPENSSL_VERSION_NUMBER < 0x00908000L
572 prefix = sha512_prefix;
573 prefixlen = sizeof(sha512_prefix);
583 if (sig->length != (unsigned int) RSA_size(rsa))
584 return (DST_R_VERIFYFAILURE);
586 #if OPENSSL_VERSION_NUMBER < 0x00908000L
587 switch (dctx->key->key_alg) {
589 case DST_ALG_RSASHA1:
590 case DST_ALG_NSEC3RSASHA1:
592 status = RSA_verify(type, digest, digestlen, sig->base,
596 case DST_ALG_RSASHA256:
597 case DST_ALG_RSASHA512:
600 * 1024 is big enough for all valid RSA bit sizes
601 * for use with DNSSEC.
603 unsigned char original[PREFIXLEN + 1024];
605 INSIST(prefix != NULL);
606 INSIST(prefixlen != 0U);
608 if (RSA_size(rsa) > (int)sizeof(original))
609 return (DST_R_VERIFYFAILURE);
611 status = RSA_public_decrypt(sig->length, sig->base,
615 return (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_toresult(DST_R_VERIFYFAILURE));
638 return (ISC_R_SUCCESS);
642 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
644 RSA *rsa1 = NULL, *rsa2 = NULL;
646 EVP_PKEY *pkey1, *pkey2;
650 pkey1 = key1->keydata.pkey;
651 pkey2 = key2->keydata.pkey;
653 * The pkey reference will keep these around after
654 * the RSA_free() call.
657 rsa1 = EVP_PKEY_get1_RSA(pkey1);
661 rsa2 = EVP_PKEY_get1_RSA(pkey2);
665 rsa1 = key1->keydata.rsa;
666 rsa2 = key2->keydata.rsa;
669 if (rsa1 == NULL && rsa2 == NULL)
671 else if (rsa1 == NULL || rsa2 == NULL)
674 status = BN_cmp(rsa1->n, rsa2->n) ||
675 BN_cmp(rsa1->e, rsa2->e);
681 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
682 (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
683 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
684 (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
687 * Can't compare private parameters, BTW does it make sense?
693 if (rsa1->d != NULL || rsa2->d != NULL) {
694 if (rsa1->d == NULL || rsa2->d == NULL)
696 status = BN_cmp(rsa1->d, rsa2->d) ||
697 BN_cmp(rsa1->p, rsa2->p) ||
698 BN_cmp(rsa1->q, rsa2->q);
707 opensslrsa_generate(dst_key_t *key, int exp) {
708 #if OPENSSL_VERSION_NUMBER > 0x00908000L
710 RSA *rsa = RSA_new();
711 BIGNUM *e = BN_new();
713 EVP_PKEY *pkey = EVP_PKEY_new();
716 if (rsa == NULL || e == NULL)
721 if (!EVP_PKEY_set1_RSA(pkey, rsa))
735 BN_GENCB_set_old(&cb, NULL, NULL);
737 if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
741 key->keydata.pkey = pkey;
745 key->keydata.rsa = rsa;
747 return (ISC_R_SUCCESS);
759 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
764 EVP_PKEY *pkey = EVP_PKEY_new();
767 return (ISC_R_NOMEMORY);
774 rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
779 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
783 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
786 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
788 key->keydata.pkey = pkey;
791 key->keydata.rsa = rsa;
794 return (ISC_R_SUCCESS);
799 opensslrsa_isprivate(const dst_key_t *key) {
801 RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
804 /* key->keydata.pkey still has a reference so rsa is still valid. */
806 RSA *rsa = key->keydata.rsa;
808 if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
810 return (ISC_TF(rsa != NULL && rsa->d != NULL));
814 opensslrsa_destroy(dst_key_t *key) {
816 EVP_PKEY *pkey = key->keydata.pkey;
818 key->keydata.pkey = NULL;
820 RSA *rsa = key->keydata.rsa;
822 key->keydata.rsa = NULL;
828 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
830 unsigned int e_bytes;
831 unsigned int mod_bytes;
839 REQUIRE(key->keydata.pkey != NULL);
841 REQUIRE(key->keydata.rsa != NULL);
845 pkey = key->keydata.pkey;
846 rsa = EVP_PKEY_get1_RSA(pkey);
848 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
850 rsa = key->keydata.rsa;
853 isc_buffer_availableregion(data, &r);
855 e_bytes = BN_num_bytes(rsa->e);
856 mod_bytes = BN_num_bytes(rsa->n);
858 if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
860 DST_RET(ISC_R_NOSPACE);
861 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
862 isc_region_consume(&r, 1);
865 DST_RET(ISC_R_NOSPACE);
866 isc_buffer_putuint8(data, 0);
867 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
868 isc_region_consume(&r, 3);
871 if (r.length < e_bytes + mod_bytes)
872 DST_RET(ISC_R_NOSPACE);
874 BN_bn2bin(rsa->e, r.base);
875 isc_region_consume(&r, e_bytes);
876 BN_bn2bin(rsa->n, r.base);
878 isc_buffer_add(data, e_bytes + mod_bytes);
890 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
893 unsigned int e_bytes;
898 isc_buffer_remainingregion(data, &r);
900 return (ISC_R_SUCCESS);
904 return (dst__openssl_toresult(ISC_R_NOMEMORY));
909 return (DST_R_INVALIDPUBLICKEY);
917 return (DST_R_INVALIDPUBLICKEY);
919 e_bytes = ((*r.base++) << 8);
920 e_bytes += *r.base++;
924 if (r.length < e_bytes) {
926 return (DST_R_INVALIDPUBLICKEY);
928 rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
932 rsa->n = BN_bin2bn(r.base, r.length, NULL);
934 key->key_size = BN_num_bits(rsa->n);
936 isc_buffer_forward(data, r.length);
939 pkey = EVP_PKEY_new();
942 return (ISC_R_NOMEMORY);
944 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
947 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
949 key->keydata.pkey = pkey;
952 key->keydata.rsa = rsa;
955 return (ISC_R_SUCCESS);
959 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
963 unsigned char *bufs[8];
967 if (key->keydata.pkey == NULL)
968 return (DST_R_NULLKEY);
969 rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
971 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
973 if (key->keydata.rsa == NULL)
974 return (DST_R_NULLKEY);
975 rsa = key->keydata.rsa;
978 for (i = 0; i < 8; i++) {
979 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
980 if (bufs[i] == NULL) {
981 result = ISC_R_NOMEMORY;
988 priv.elements[i].tag = TAG_RSA_MODULUS;
989 priv.elements[i].length = BN_num_bytes(rsa->n);
990 BN_bn2bin(rsa->n, bufs[i]);
991 priv.elements[i].data = bufs[i];
994 priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
995 priv.elements[i].length = BN_num_bytes(rsa->e);
996 BN_bn2bin(rsa->e, bufs[i]);
997 priv.elements[i].data = bufs[i];
1000 if (rsa->d != NULL) {
1001 priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
1002 priv.elements[i].length = BN_num_bytes(rsa->d);
1003 BN_bn2bin(rsa->d, bufs[i]);
1004 priv.elements[i].data = bufs[i];
1008 if (rsa->p != NULL) {
1009 priv.elements[i].tag = TAG_RSA_PRIME1;
1010 priv.elements[i].length = BN_num_bytes(rsa->p);
1011 BN_bn2bin(rsa->p, bufs[i]);
1012 priv.elements[i].data = bufs[i];
1016 if (rsa->q != NULL) {
1017 priv.elements[i].tag = TAG_RSA_PRIME2;
1018 priv.elements[i].length = BN_num_bytes(rsa->q);
1019 BN_bn2bin(rsa->q, bufs[i]);
1020 priv.elements[i].data = bufs[i];
1024 if (rsa->dmp1 != NULL) {
1025 priv.elements[i].tag = TAG_RSA_EXPONENT1;
1026 priv.elements[i].length = BN_num_bytes(rsa->dmp1);
1027 BN_bn2bin(rsa->dmp1, bufs[i]);
1028 priv.elements[i].data = bufs[i];
1032 if (rsa->dmq1 != NULL) {
1033 priv.elements[i].tag = TAG_RSA_EXPONENT2;
1034 priv.elements[i].length = BN_num_bytes(rsa->dmq1);
1035 BN_bn2bin(rsa->dmq1, bufs[i]);
1036 priv.elements[i].data = bufs[i];
1040 if (rsa->iqmp != NULL) {
1041 priv.elements[i].tag = TAG_RSA_COEFFICIENT;
1042 priv.elements[i].length = BN_num_bytes(rsa->iqmp);
1043 BN_bn2bin(rsa->iqmp, bufs[i]);
1044 priv.elements[i].data = bufs[i];
1048 if (key->engine != NULL) {
1049 priv.elements[i].tag = TAG_RSA_ENGINE;
1050 priv.elements[i].length = strlen(key->engine) + 1;
1051 priv.elements[i].data = (unsigned char *)key->engine;
1055 if (key->label != NULL) {
1056 priv.elements[i].tag = TAG_RSA_LABEL;
1057 priv.elements[i].length = strlen(key->label) + 1;
1058 priv.elements[i].data = (unsigned char *)key->label;
1063 result = dst__privstruct_writefile(key, &priv, directory);
1068 for (i = 0; i < 8; i++) {
1069 if (bufs[i] == NULL)
1071 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
1077 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer) {
1083 isc_mem_t *mctx = key->mctx;
1084 const char *name = NULL, *label = NULL;
1085 EVP_PKEY *pkey = NULL;
1087 /* read private key file */
1088 ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
1089 if (ret != ISC_R_SUCCESS)
1092 for (i = 0; i < priv.nelements; i++) {
1093 switch (priv.elements[i].tag) {
1094 case TAG_RSA_ENGINE:
1095 name = (char *)priv.elements[i].data;
1098 label = (char *)priv.elements[i].data;
1105 * Is this key is stored in a HSM?
1106 * See if we can fetch it.
1108 if (name != NULL || label != NULL) {
1109 INSIST(name != NULL);
1110 INSIST(label != NULL);
1111 e = dst__openssl_getengine(name);
1113 DST_RET(DST_R_NOENGINE);
1114 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1116 /* ERR_print_errors_fp(stderr); */
1117 DST_RET(ISC_R_NOTFOUND);
1119 key->engine = isc_mem_strdup(key->mctx, name);
1120 if (key->engine == NULL)
1121 DST_RET(ISC_R_NOMEMORY);
1122 key->label = isc_mem_strdup(key->mctx, label);
1123 if (key->label == NULL)
1124 DST_RET(ISC_R_NOMEMORY);
1125 key->key_size = EVP_PKEY_bits(pkey);
1127 key->keydata.pkey = pkey;
1129 key->keydata.rsa = EVP_PKEY_get1_RSA(pkey);
1131 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1132 EVP_PKEY_free(pkey);
1134 dst__privstruct_free(&priv, mctx);
1135 return (ISC_R_SUCCESS);
1140 DST_RET(ISC_R_NOMEMORY);
1144 pkey = EVP_PKEY_new();
1146 DST_RET(ISC_R_NOMEMORY);
1147 if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
1148 DST_RET(ISC_R_FAILURE);
1150 key->keydata.pkey = pkey;
1152 key->keydata.rsa = rsa;
1155 for (i = 0; i < priv.nelements; i++) {
1157 switch (priv.elements[i].tag) {
1158 case TAG_RSA_ENGINE:
1165 bn = BN_bin2bn(priv.elements[i].data,
1166 priv.elements[i].length, NULL);
1168 DST_RET(ISC_R_NOMEMORY);
1171 switch (priv.elements[i].tag) {
1172 case TAG_RSA_MODULUS:
1175 case TAG_RSA_PUBLICEXPONENT:
1178 case TAG_RSA_PRIVATEEXPONENT:
1181 case TAG_RSA_PRIME1:
1184 case TAG_RSA_PRIME2:
1187 case TAG_RSA_EXPONENT1:
1190 case TAG_RSA_EXPONENT2:
1193 case TAG_RSA_COEFFICIENT:
1198 dst__privstruct_free(&priv, mctx);
1200 key->key_size = BN_num_bits(rsa->n);
1205 return (ISC_R_SUCCESS);
1210 EVP_PKEY_free(pkey);
1214 opensslrsa_destroy(key);
1215 dst__privstruct_free(&priv, mctx);
1216 memset(&priv, 0, sizeof(priv));
1221 opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
1226 EVP_PKEY *pkey = NULL;
1230 e = dst__openssl_getengine(engine);
1232 DST_RET(DST_R_NOENGINE);
1233 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1235 DST_RET(ISC_R_NOTFOUND);
1236 key->engine = isc_mem_strdup(key->mctx, label);
1237 if (key->engine == NULL)
1238 DST_RET(ISC_R_NOMEMORY);
1239 key->label = isc_mem_strdup(key->mctx, label);
1240 if (key->label == NULL)
1241 DST_RET(ISC_R_NOMEMORY);
1242 key->key_size = EVP_PKEY_bits(pkey);
1244 key->keydata.pkey = pkey;
1246 key->keydata.rsa = EVP_PKEY_get1_RSA(pkey);
1247 EVP_PKEY_free(pkey);
1248 if (key->keydata.rsa == NULL)
1249 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1251 return (ISC_R_SUCCESS);
1255 EVP_PKEY_free(pkey);
1259 static dst_func_t opensslrsa_functions = {
1260 opensslrsa_createctx,
1261 opensslrsa_destroyctx,
1265 NULL, /*%< computesecret */
1267 NULL, /*%< paramcompare */
1268 opensslrsa_generate,
1269 opensslrsa_isprivate,
1275 NULL, /*%< cleanup */
1276 opensslrsa_fromlabel,
1280 dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
1281 REQUIRE(funcp != NULL);
1283 if (*funcp == NULL) {
1284 switch (algorithm) {
1285 case DST_ALG_RSASHA256:
1286 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1287 *funcp = &opensslrsa_functions;
1290 case DST_ALG_RSASHA512:
1291 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1292 *funcp = &opensslrsa_functions;
1296 *funcp = &opensslrsa_functions;
1300 return (ISC_R_SUCCESS);
1305 #include <isc/util.h>
1307 EMPTY_TRANSLATION_UNIT
1309 #endif /* OPENSSL */