1 /* $NetBSD: opensslrsa_link.c,v 1.10 2015/09/03 07:33:34 christos Exp $ */
4 * Copyright (C) 2004-2009, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Principal Author: Brian Wellington
28 #if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
36 #include <isc/entropy.h>
41 #include <isc/string.h>
44 #include <dst/result.h>
46 #include "dst_internal.h"
47 #include "dst_openssl.h"
48 #include "dst_parse.h"
50 #include <openssl/err.h>
51 #include <openssl/objects.h>
52 #include <openssl/rsa.h>
53 #if OPENSSL_VERSION_NUMBER > 0x00908000L
54 #include <openssl/bn.h>
57 #include <openssl/engine.h>
61 * Limit the size of public exponents.
63 #ifndef RSA_MAX_PUBEXP_BITS
64 #define RSA_MAX_PUBEXP_BITS 35
68 * We don't use configure for windows so enforce the OpenSSL version
69 * here. Unlike with configure we don't support overriding this test.
72 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
73 OPENSSL_VERSION_NUMBER < 0x00908000L) || \
74 OPENSSL_VERSION_NUMBER >= 0x0090804fL)
75 #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
81 * XXXMPA Temporarily disable RSA_BLINDING as it requires
82 * good quality random data that cannot currently be guaranteed.
83 * XXXMPA Find which versions of openssl use pseudo random data
84 * and set RSA_FLAG_BLINDING for those.
88 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
89 #define SET_FLAGS(rsa) \
91 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
92 (rsa)->flags |= RSA_FLAG_BLINDING; \
93 } while (/*CONSTCOND*/0)
95 #define SET_FLAGS(rsa) \
97 (rsa)->flags |= RSA_FLAG_BLINDING; \
98 } while (/*CONSTCOND*/0)
102 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
103 #define SET_FLAGS(rsa) \
105 (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
106 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
107 } while (/*CONSTCOND*/0)
108 #elif defined(RSA_FLAG_NO_BLINDING)
109 #define SET_FLAGS(rsa) \
111 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
112 (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
113 } while (/*CONSTCOND*/0)
115 #define SET_FLAGS(rsa) \
117 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
118 } while (/*CONSTCOND*/0)
121 #define DST_RET(a) {ret = a; goto err;}
123 static isc_result_t
opensslrsa_todns(const dst_key_t
*key
, isc_buffer_t
*data
);
126 opensslrsa_createctx(dst_key_t
*key
, dst_context_t
*dctx
) {
128 EVP_MD_CTX
*evp_md_ctx
;
129 const EVP_MD
*type
= NULL
;
133 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
134 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
135 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
136 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
137 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
140 evp_md_ctx
= EVP_MD_CTX_create();
141 if (evp_md_ctx
== NULL
)
142 return (ISC_R_NOMEMORY
);
144 switch (dctx
->key
->key_alg
) {
146 type
= EVP_md5(); /* MD5 + RSA */
148 case DST_ALG_RSASHA1
:
149 case DST_ALG_NSEC3RSASHA1
:
150 type
= EVP_sha1(); /* SHA1 + RSA */
152 #ifdef HAVE_EVP_SHA256
153 case DST_ALG_RSASHA256
:
154 type
= EVP_sha256(); /* SHA256 + RSA */
157 #ifdef HAVE_EVP_SHA512
158 case DST_ALG_RSASHA512
:
166 if (!EVP_DigestInit_ex(evp_md_ctx
, type
, NULL
)) {
167 EVP_MD_CTX_destroy(evp_md_ctx
);
168 return (dst__openssl_toresult3(dctx
->category
,
172 dctx
->ctxdata
.evp_md_ctx
= evp_md_ctx
;
174 switch (dctx
->key
->key_alg
) {
179 md5ctx
= isc_mem_get(dctx
->mctx
, sizeof(isc_md5_t
));
181 return (ISC_R_NOMEMORY
);
182 isc_md5_init(md5ctx
);
183 dctx
->ctxdata
.md5ctx
= md5ctx
;
186 case DST_ALG_RSASHA1
:
187 case DST_ALG_NSEC3RSASHA1
:
191 sha1ctx
= isc_mem_get(dctx
->mctx
, sizeof(isc_sha1_t
));
193 return (ISC_R_NOMEMORY
);
194 isc_sha1_init(sha1ctx
);
195 dctx
->ctxdata
.sha1ctx
= sha1ctx
;
198 case DST_ALG_RSASHA256
:
200 isc_sha256_t
*sha256ctx
;
202 sha256ctx
= isc_mem_get(dctx
->mctx
,
203 sizeof(isc_sha256_t
));
204 if (sha256ctx
== NULL
)
205 return (ISC_R_NOMEMORY
);
206 isc_sha256_init(sha256ctx
);
207 dctx
->ctxdata
.sha256ctx
= sha256ctx
;
210 case DST_ALG_RSASHA512
:
212 isc_sha512_t
*sha512ctx
;
214 sha512ctx
= isc_mem_get(dctx
->mctx
,
215 sizeof(isc_sha512_t
));
216 if (sha512ctx
== NULL
)
217 return (ISC_R_NOMEMORY
);
218 isc_sha512_init(sha512ctx
);
219 dctx
->ctxdata
.sha512ctx
= sha512ctx
;
227 return (ISC_R_SUCCESS
);
231 opensslrsa_destroyctx(dst_context_t
*dctx
) {
233 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
236 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
237 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
238 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
239 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
240 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
243 if (evp_md_ctx
!= NULL
) {
244 EVP_MD_CTX_destroy(evp_md_ctx
);
245 dctx
->ctxdata
.evp_md_ctx
= NULL
;
248 switch (dctx
->key
->key_alg
) {
251 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
253 if (md5ctx
!= NULL
) {
254 isc_md5_invalidate(md5ctx
);
255 isc_mem_put(dctx
->mctx
, md5ctx
,
257 dctx
->ctxdata
.md5ctx
= NULL
;
261 case DST_ALG_RSASHA1
:
262 case DST_ALG_NSEC3RSASHA1
:
264 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
266 if (sha1ctx
!= NULL
) {
267 isc_sha1_invalidate(sha1ctx
);
268 isc_mem_put(dctx
->mctx
, sha1ctx
,
270 dctx
->ctxdata
.sha1ctx
= NULL
;
274 case DST_ALG_RSASHA256
:
276 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
278 if (sha256ctx
!= NULL
) {
279 isc_sha256_invalidate(sha256ctx
);
280 isc_mem_put(dctx
->mctx
, sha256ctx
,
281 sizeof(isc_sha256_t
));
282 dctx
->ctxdata
.sha256ctx
= NULL
;
286 case DST_ALG_RSASHA512
:
288 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
290 if (sha512ctx
!= NULL
) {
291 isc_sha512_invalidate(sha512ctx
);
292 isc_mem_put(dctx
->mctx
, sha512ctx
,
293 sizeof(isc_sha512_t
));
294 dctx
->ctxdata
.sha512ctx
= NULL
;
305 opensslrsa_adddata(dst_context_t
*dctx
, const isc_region_t
*data
) {
307 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
310 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
311 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
312 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
313 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
314 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
317 if (!EVP_DigestUpdate(evp_md_ctx
, data
->base
, data
->length
)) {
318 return (dst__openssl_toresult3(dctx
->category
,
323 switch (dctx
->key
->key_alg
) {
326 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
328 isc_md5_update(md5ctx
, data
->base
, data
->length
);
331 case DST_ALG_RSASHA1
:
332 case DST_ALG_NSEC3RSASHA1
:
334 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
336 isc_sha1_update(sha1ctx
, data
->base
, data
->length
);
339 case DST_ALG_RSASHA256
:
341 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
343 isc_sha256_update(sha256ctx
, data
->base
, data
->length
);
346 case DST_ALG_RSASHA512
:
348 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
350 isc_sha512_update(sha512ctx
, data
->base
, data
->length
);
357 return (ISC_R_SUCCESS
);
360 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
362 * Digest prefixes from RFC 5702.
364 static unsigned char sha256_prefix
[] =
365 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
366 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
367 static unsigned char sha512_prefix
[] =
368 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
369 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
370 #define PREFIXLEN sizeof(sha512_prefix)
376 opensslrsa_sign(dst_context_t
*dctx
, isc_buffer_t
*sig
) {
377 dst_key_t
*key
= dctx
->key
;
379 unsigned int siglen
= 0;
381 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
382 EVP_PKEY
*pkey
= key
->keydata
.pkey
;
384 RSA
*rsa
= key
->keydata
.rsa
;
385 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
386 unsigned char digest
[PREFIXLEN
+ ISC_SHA512_DIGESTLENGTH
];
389 unsigned int digestlen
= 0;
390 #if OPENSSL_VERSION_NUMBER < 0x00908000L
391 unsigned int prefixlen
= 0;
392 const unsigned char *prefix
= NULL
;
396 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
397 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
398 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
399 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
400 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
402 isc_buffer_availableregion(sig
, &r
);
405 if (r
.length
< (unsigned int) EVP_PKEY_size(pkey
))
406 return (ISC_R_NOSPACE
);
408 if (!EVP_SignFinal(evp_md_ctx
, r
.base
, &siglen
, pkey
)) {
409 return (dst__openssl_toresult3(dctx
->category
,
414 if (r
.length
< (unsigned int) RSA_size(rsa
))
415 return (ISC_R_NOSPACE
);
417 switch (dctx
->key
->key_alg
) {
420 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
422 isc_md5_final(md5ctx
, digest
);
424 digestlen
= ISC_MD5_DIGESTLENGTH
;
427 case DST_ALG_RSASHA1
:
428 case DST_ALG_NSEC3RSASHA1
:
430 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
432 isc_sha1_final(sha1ctx
, digest
);
434 digestlen
= ISC_SHA1_DIGESTLENGTH
;
437 case DST_ALG_RSASHA256
:
439 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
441 isc_sha256_final(digest
, sha256ctx
);
442 digestlen
= ISC_SHA256_DIGESTLENGTH
;
443 #if OPENSSL_VERSION_NUMBER < 0x00908000L
444 prefix
= sha256_prefix
;
445 prefixlen
= sizeof(sha256_prefix
);
451 case DST_ALG_RSASHA512
:
453 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
455 isc_sha512_final(digest
, sha512ctx
);
456 digestlen
= ISC_SHA512_DIGESTLENGTH
;
457 #if OPENSSL_VERSION_NUMBER < 0x00908000L
458 prefix
= sha512_prefix
;
459 prefixlen
= sizeof(sha512_prefix
);
469 #if OPENSSL_VERSION_NUMBER < 0x00908000L
470 switch (dctx
->key
->key_alg
) {
472 case DST_ALG_RSASHA1
:
473 case DST_ALG_NSEC3RSASHA1
:
475 status
= RSA_sign(type
, digest
, digestlen
, r
.base
,
479 case DST_ALG_RSASHA256
:
480 case DST_ALG_RSASHA512
:
481 INSIST(prefix
!= NULL
);
482 INSIST(prefixlen
!= 0);
483 INSIST(prefixlen
+ digestlen
<= sizeof(digest
));
485 memmove(digest
+ prefixlen
, digest
, digestlen
);
486 memmove(digest
, prefix
, prefixlen
);
487 status
= RSA_private_encrypt(digestlen
+ prefixlen
,
501 status
= RSA_sign(type
, digest
, digestlen
, r
.base
, &siglen
, rsa
);
504 return (dst__openssl_toresult3(dctx
->category
,
506 DST_R_OPENSSLFAILURE
));
509 isc_buffer_add(sig
, siglen
);
511 return (ISC_R_SUCCESS
);
515 opensslrsa_verify2(dst_context_t
*dctx
, int maxbits
, const isc_region_t
*sig
) {
516 dst_key_t
*key
= dctx
->key
;
519 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
520 EVP_PKEY
*pkey
= key
->keydata
.pkey
;
524 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
525 unsigned char digest
[ISC_SHA512_DIGESTLENGTH
];
527 unsigned int digestlen
= 0;
528 RSA
*rsa
= key
->keydata
.rsa
;
529 #if OPENSSL_VERSION_NUMBER < 0x00908000L
530 unsigned int prefixlen
= 0;
531 const unsigned char *prefix
= NULL
;
535 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
536 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
537 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
538 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
539 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
542 rsa
= EVP_PKEY_get1_RSA(pkey
);
544 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
545 bits
= BN_num_bits(rsa
->e
);
547 if (bits
> maxbits
&& maxbits
!= 0)
548 return (DST_R_VERIFYFAILURE
);
550 status
= EVP_VerifyFinal(evp_md_ctx
, sig
->base
, sig
->length
, pkey
);
553 return (ISC_R_SUCCESS
);
555 return (dst__openssl_toresult(DST_R_VERIFYFAILURE
));
557 return (dst__openssl_toresult3(dctx
->category
,
559 DST_R_VERIFYFAILURE
));
562 if (BN_num_bits(rsa
->e
) > maxbits
&& maxbits
!= 0)
563 return (DST_R_VERIFYFAILURE
);
565 switch (dctx
->key
->key_alg
) {
568 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
570 isc_md5_final(md5ctx
, digest
);
572 digestlen
= ISC_MD5_DIGESTLENGTH
;
575 case DST_ALG_RSASHA1
:
576 case DST_ALG_NSEC3RSASHA1
:
578 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
580 isc_sha1_final(sha1ctx
, digest
);
582 digestlen
= ISC_SHA1_DIGESTLENGTH
;
585 case DST_ALG_RSASHA256
:
587 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
589 isc_sha256_final(digest
, sha256ctx
);
590 digestlen
= ISC_SHA256_DIGESTLENGTH
;
591 #if OPENSSL_VERSION_NUMBER < 0x00908000L
592 prefix
= sha256_prefix
;
593 prefixlen
= sizeof(sha256_prefix
);
599 case DST_ALG_RSASHA512
:
601 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
603 isc_sha512_final(digest
, sha512ctx
);
604 digestlen
= ISC_SHA512_DIGESTLENGTH
;
605 #if OPENSSL_VERSION_NUMBER < 0x00908000L
606 prefix
= sha512_prefix
;
607 prefixlen
= sizeof(sha512_prefix
);
617 if (sig
->length
!= (unsigned int) RSA_size(rsa
))
618 return (DST_R_VERIFYFAILURE
);
620 #if OPENSSL_VERSION_NUMBER < 0x00908000L
621 switch (dctx
->key
->key_alg
) {
623 case DST_ALG_RSASHA1
:
624 case DST_ALG_NSEC3RSASHA1
:
626 status
= RSA_verify(type
, digest
, digestlen
, sig
->base
,
630 case DST_ALG_RSASHA256
:
631 case DST_ALG_RSASHA512
:
634 * 1024 is big enough for all valid RSA bit sizes
635 * for use with DNSSEC.
637 unsigned char original
[PREFIXLEN
+ 1024];
639 INSIST(prefix
!= NULL
);
640 INSIST(prefixlen
!= 0U);
642 if (RSA_size(rsa
) > (int)sizeof(original
))
643 return (DST_R_VERIFYFAILURE
);
645 status
= RSA_public_decrypt(sig
->length
, sig
->base
,
649 return (dst__openssl_toresult3(
651 "RSA_public_decrypt",
652 DST_R_VERIFYFAILURE
));
653 if (status
!= (int)(prefixlen
+ digestlen
))
654 return (DST_R_VERIFYFAILURE
);
655 if (memcmp(original
, prefix
, prefixlen
))
656 return (DST_R_VERIFYFAILURE
);
657 if (memcmp(original
+ prefixlen
, digest
, digestlen
))
658 return (DST_R_VERIFYFAILURE
);
668 status
= RSA_verify(type
, digest
, digestlen
, sig
->base
,
672 return (dst__openssl_toresult(DST_R_VERIFYFAILURE
));
673 return (ISC_R_SUCCESS
);
678 opensslrsa_verify(dst_context_t
*dctx
, const isc_region_t
*sig
) {
679 return (opensslrsa_verify2(dctx
, 0, sig
));
683 opensslrsa_compare(const dst_key_t
*key1
, const dst_key_t
*key2
) {
685 RSA
*rsa1
= NULL
, *rsa2
= NULL
;
687 EVP_PKEY
*pkey1
, *pkey2
;
691 pkey1
= key1
->keydata
.pkey
;
692 pkey2
= key2
->keydata
.pkey
;
694 * The pkey reference will keep these around after
695 * the RSA_free() call.
698 rsa1
= EVP_PKEY_get1_RSA(pkey1
);
702 rsa2
= EVP_PKEY_get1_RSA(pkey2
);
706 rsa1
= key1
->keydata
.rsa
;
707 rsa2
= key2
->keydata
.rsa
;
710 if (rsa1
== NULL
&& rsa2
== NULL
)
712 else if (rsa1
== NULL
|| rsa2
== NULL
)
715 status
= BN_cmp(rsa1
->n
, rsa2
->n
) ||
716 BN_cmp(rsa1
->e
, rsa2
->e
);
722 if ((rsa1
->flags
& RSA_FLAG_EXT_PKEY
) != 0 ||
723 (rsa2
->flags
& RSA_FLAG_EXT_PKEY
) != 0) {
724 if ((rsa1
->flags
& RSA_FLAG_EXT_PKEY
) == 0 ||
725 (rsa2
->flags
& RSA_FLAG_EXT_PKEY
) == 0)
728 * Can't compare private parameters, BTW does it make sense?
734 if (rsa1
->d
!= NULL
|| rsa2
->d
!= NULL
) {
735 if (rsa1
->d
== NULL
|| rsa2
->d
== NULL
)
737 status
= BN_cmp(rsa1
->d
, rsa2
->d
) ||
738 BN_cmp(rsa1
->p
, rsa2
->p
) ||
739 BN_cmp(rsa1
->q
, rsa2
->q
);
747 #if OPENSSL_VERSION_NUMBER > 0x00908000L
749 progress_cb(int p
, int n
, BN_GENCB
*cb
) {
765 opensslrsa_generate(dst_key_t
*key
, int exp
, void (*callback
)(int)) {
766 #if OPENSSL_VERSION_NUMBER > 0x00908000L
767 isc_result_t ret
= DST_R_OPENSSLFAILURE
;
773 RSA
*rsa
= RSA_new();
774 BIGNUM
*e
= BN_new();
776 EVP_PKEY
*pkey
= EVP_PKEY_new();
779 if (rsa
== NULL
|| e
== NULL
)
784 if (!EVP_PKEY_set1_RSA(pkey
, rsa
))
793 /* (phased-out) F5 0x100000001 */
798 if (callback
== NULL
) {
799 BN_GENCB_set_old(&cb
, NULL
, NULL
);
802 BN_GENCB_set(&cb
, &progress_cb
, u
.dptr
);
805 if (RSA_generate_key_ex(rsa
, key
->key_size
, e
, &cb
)) {
809 key
->keydata
.pkey
= pkey
;
813 key
->keydata
.rsa
= rsa
;
815 return (ISC_R_SUCCESS
);
817 ret
= dst__openssl_toresult2("RSA_generate_key_ex",
818 DST_R_OPENSSLFAILURE
);
829 return (dst__openssl_toresult(ret
));
834 EVP_PKEY
*pkey
= EVP_PKEY_new();
839 return (ISC_R_NOMEMORY
);
848 rsa
= RSA_generate_key(key
->key_size
, e
, NULL
, NULL
);
853 return (dst__openssl_toresult2("RSA_generate_key",
854 DST_R_OPENSSLFAILURE
));
858 if (!EVP_PKEY_set1_RSA(pkey
, rsa
)) {
861 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
863 key
->keydata
.pkey
= pkey
;
866 key
->keydata
.rsa
= rsa
;
869 return (ISC_R_SUCCESS
);
874 opensslrsa_isprivate(const dst_key_t
*key
) {
876 RSA
*rsa
= EVP_PKEY_get1_RSA(key
->keydata
.pkey
);
879 /* key->keydata.pkey still has a reference so rsa is still valid. */
881 RSA
*rsa
= key
->keydata
.rsa
;
883 if (rsa
!= NULL
&& (rsa
->flags
& RSA_FLAG_EXT_PKEY
) != 0)
885 return (ISC_TF(rsa
!= NULL
&& rsa
->d
!= NULL
));
889 opensslrsa_destroy(dst_key_t
*key
) {
891 EVP_PKEY
*pkey
= key
->keydata
.pkey
;
893 key
->keydata
.pkey
= NULL
;
895 RSA
*rsa
= key
->keydata
.rsa
;
897 key
->keydata
.rsa
= NULL
;
903 opensslrsa_todns(const dst_key_t
*key
, isc_buffer_t
*data
) {
905 unsigned int e_bytes
;
906 unsigned int mod_bytes
;
914 REQUIRE(key
->keydata
.pkey
!= NULL
);
916 REQUIRE(key
->keydata
.rsa
!= NULL
);
920 pkey
= key
->keydata
.pkey
;
921 rsa
= EVP_PKEY_get1_RSA(pkey
);
923 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
925 rsa
= key
->keydata
.rsa
;
928 isc_buffer_availableregion(data
, &r
);
930 e_bytes
= BN_num_bytes(rsa
->e
);
931 mod_bytes
= BN_num_bytes(rsa
->n
);
933 if (e_bytes
< 256) { /*%< key exponent is <= 2040 bits */
935 DST_RET(ISC_R_NOSPACE
);
936 isc_buffer_putuint8(data
, (isc_uint8_t
) e_bytes
);
937 isc_region_consume(&r
, 1);
940 DST_RET(ISC_R_NOSPACE
);
941 isc_buffer_putuint8(data
, 0);
942 isc_buffer_putuint16(data
, (isc_uint16_t
) e_bytes
);
943 isc_region_consume(&r
, 3);
946 if (r
.length
< e_bytes
+ mod_bytes
)
947 DST_RET(ISC_R_NOSPACE
);
949 BN_bn2bin(rsa
->e
, r
.base
);
950 isc_region_consume(&r
, e_bytes
);
951 BN_bn2bin(rsa
->n
, r
.base
);
953 isc_buffer_add(data
, e_bytes
+ mod_bytes
);
965 opensslrsa_fromdns(dst_key_t
*key
, isc_buffer_t
*data
) {
968 unsigned int e_bytes
;
974 isc_buffer_remainingregion(data
, &r
);
976 return (ISC_R_SUCCESS
);
981 return (dst__openssl_toresult(ISC_R_NOMEMORY
));
986 return (DST_R_INVALIDPUBLICKEY
);
989 isc_region_consume(&r
, 1);
994 return (DST_R_INVALIDPUBLICKEY
);
996 e_bytes
= (*r
.base
) << 8;
997 isc_region_consume(&r
, 1);
999 isc_region_consume(&r
, 1);
1002 if (r
.length
< e_bytes
) {
1004 return (DST_R_INVALIDPUBLICKEY
);
1006 rsa
->e
= BN_bin2bn(r
.base
, e_bytes
, NULL
);
1007 isc_region_consume(&r
, e_bytes
);
1009 rsa
->n
= BN_bin2bn(r
.base
, r
.length
, NULL
);
1011 key
->key_size
= BN_num_bits(rsa
->n
);
1013 isc_buffer_forward(data
, length
);
1016 pkey
= EVP_PKEY_new();
1019 return (ISC_R_NOMEMORY
);
1021 if (!EVP_PKEY_set1_RSA(pkey
, rsa
)) {
1022 EVP_PKEY_free(pkey
);
1024 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1026 key
->keydata
.pkey
= pkey
;
1029 key
->keydata
.rsa
= rsa
;
1032 return (ISC_R_SUCCESS
);
1036 opensslrsa_tofile(const dst_key_t
*key
, const char *directory
) {
1040 unsigned char *bufs
[8];
1041 isc_result_t result
;
1044 if (key
->keydata
.pkey
== NULL
)
1045 return (DST_R_NULLKEY
);
1046 rsa
= EVP_PKEY_get1_RSA(key
->keydata
.pkey
);
1048 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1050 if (key
->keydata
.rsa
== NULL
)
1051 return (DST_R_NULLKEY
);
1052 rsa
= key
->keydata
.rsa
;
1054 memset(bufs
, 0, sizeof(bufs
));
1056 if (key
->external
) {
1058 result
= dst__privstruct_writefile(key
, &priv
, directory
);
1062 for (i
= 0; i
< 8; i
++) {
1063 bufs
[i
] = isc_mem_get(key
->mctx
, BN_num_bytes(rsa
->n
));
1064 if (bufs
[i
] == NULL
) {
1065 result
= ISC_R_NOMEMORY
;
1072 priv
.elements
[i
].tag
= TAG_RSA_MODULUS
;
1073 priv
.elements
[i
].length
= BN_num_bytes(rsa
->n
);
1074 BN_bn2bin(rsa
->n
, bufs
[i
]);
1075 priv
.elements
[i
].data
= bufs
[i
];
1078 priv
.elements
[i
].tag
= TAG_RSA_PUBLICEXPONENT
;
1079 priv
.elements
[i
].length
= BN_num_bytes(rsa
->e
);
1080 BN_bn2bin(rsa
->e
, bufs
[i
]);
1081 priv
.elements
[i
].data
= bufs
[i
];
1084 if (rsa
->d
!= NULL
) {
1085 priv
.elements
[i
].tag
= TAG_RSA_PRIVATEEXPONENT
;
1086 priv
.elements
[i
].length
= BN_num_bytes(rsa
->d
);
1087 BN_bn2bin(rsa
->d
, bufs
[i
]);
1088 priv
.elements
[i
].data
= bufs
[i
];
1092 if (rsa
->p
!= NULL
) {
1093 priv
.elements
[i
].tag
= TAG_RSA_PRIME1
;
1094 priv
.elements
[i
].length
= BN_num_bytes(rsa
->p
);
1095 BN_bn2bin(rsa
->p
, bufs
[i
]);
1096 priv
.elements
[i
].data
= bufs
[i
];
1100 if (rsa
->q
!= NULL
) {
1101 priv
.elements
[i
].tag
= TAG_RSA_PRIME2
;
1102 priv
.elements
[i
].length
= BN_num_bytes(rsa
->q
);
1103 BN_bn2bin(rsa
->q
, bufs
[i
]);
1104 priv
.elements
[i
].data
= bufs
[i
];
1108 if (rsa
->dmp1
!= NULL
) {
1109 priv
.elements
[i
].tag
= TAG_RSA_EXPONENT1
;
1110 priv
.elements
[i
].length
= BN_num_bytes(rsa
->dmp1
);
1111 BN_bn2bin(rsa
->dmp1
, bufs
[i
]);
1112 priv
.elements
[i
].data
= bufs
[i
];
1116 if (rsa
->dmq1
!= NULL
) {
1117 priv
.elements
[i
].tag
= TAG_RSA_EXPONENT2
;
1118 priv
.elements
[i
].length
= BN_num_bytes(rsa
->dmq1
);
1119 BN_bn2bin(rsa
->dmq1
, bufs
[i
]);
1120 priv
.elements
[i
].data
= bufs
[i
];
1124 if (rsa
->iqmp
!= NULL
) {
1125 priv
.elements
[i
].tag
= TAG_RSA_COEFFICIENT
;
1126 priv
.elements
[i
].length
= BN_num_bytes(rsa
->iqmp
);
1127 BN_bn2bin(rsa
->iqmp
, bufs
[i
]);
1128 priv
.elements
[i
].data
= bufs
[i
];
1132 if (key
->engine
!= NULL
) {
1133 priv
.elements
[i
].tag
= TAG_RSA_ENGINE
;
1134 priv
.elements
[i
].length
= strlen(key
->engine
) + 1;
1135 priv
.elements
[i
].data
= (unsigned char *)key
->engine
;
1139 if (key
->label
!= NULL
) {
1140 priv
.elements
[i
].tag
= TAG_RSA_LABEL
;
1141 priv
.elements
[i
].length
= strlen(key
->label
) + 1;
1142 priv
.elements
[i
].data
= (unsigned char *)key
->label
;
1148 result
= dst__privstruct_writefile(key
, &priv
, directory
);
1153 for (i
= 0; i
< 8; i
++) {
1154 if (bufs
[i
] == NULL
)
1156 isc_mem_put(key
->mctx
, bufs
[i
], BN_num_bytes(rsa
->n
));
1162 rsa_check(RSA
*rsa
, RSA
*pub
)
1164 /* Public parameters should be the same but if they are not set
1165 * copy them from the public key. */
1167 if (rsa
->n
!= NULL
) {
1168 if (BN_cmp(rsa
->n
, pub
->n
) != 0)
1169 return (DST_R_INVALIDPRIVATEKEY
);
1174 if (rsa
->e
!= NULL
) {
1175 if (BN_cmp(rsa
->e
, pub
->e
) != 0)
1176 return (DST_R_INVALIDPRIVATEKEY
);
1182 if (rsa
->n
== NULL
|| rsa
->e
== NULL
)
1183 return (DST_R_INVALIDPRIVATEKEY
);
1184 return (ISC_R_SUCCESS
);
1188 opensslrsa_parse(dst_key_t
*key
, isc_lex_t
*lexer
, dst_key_t
*pub
) {
1192 RSA
*rsa
= NULL
, *pubrsa
= NULL
;
1196 isc_mem_t
*mctx
= key
->mctx
;
1197 const char *engine
= NULL
, *label
= NULL
;
1198 #if defined(USE_ENGINE) || USE_EVP
1199 EVP_PKEY
*pkey
= NULL
;
1202 /* read private key file */
1203 ret
= dst__privstruct_parse(key
, DST_ALG_RSA
, lexer
, mctx
, &priv
);
1204 if (ret
!= ISC_R_SUCCESS
)
1207 if (key
->external
) {
1208 if (priv
.nelements
!= 0)
1209 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1211 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1212 key
->keydata
.pkey
= pub
->keydata
.pkey
;
1213 pub
->keydata
.pkey
= NULL
;
1214 key
->key_size
= pub
->key_size
;
1215 dst__privstruct_free(&priv
, mctx
);
1216 memset(&priv
, 0, sizeof(priv
));
1217 return (ISC_R_SUCCESS
);
1221 if (pub
!= NULL
&& pub
->keydata
.pkey
!= NULL
)
1222 pubrsa
= EVP_PKEY_get1_RSA(pub
->keydata
.pkey
);
1224 if (pub
!= NULL
&& pub
->keydata
.rsa
!= NULL
) {
1225 pubrsa
= pub
->keydata
.rsa
;
1226 pub
->keydata
.rsa
= NULL
;
1230 for (i
= 0; i
< priv
.nelements
; i
++) {
1231 switch (priv
.elements
[i
].tag
) {
1232 case TAG_RSA_ENGINE
:
1233 engine
= (char *)priv
.elements
[i
].data
;
1236 label
= (char *)priv
.elements
[i
].data
;
1244 * Is this key is stored in a HSM?
1245 * See if we can fetch it.
1247 if (label
!= NULL
) {
1250 DST_RET(DST_R_NOENGINE
);
1251 e
= dst__openssl_getengine(engine
);
1253 DST_RET(DST_R_NOENGINE
);
1254 pkey
= ENGINE_load_private_key(e
, label
, NULL
, NULL
);
1256 DST_RET(dst__openssl_toresult2(
1257 "ENGINE_load_private_key",
1259 key
->engine
= isc_mem_strdup(key
->mctx
, engine
);
1260 if (key
->engine
== NULL
)
1261 DST_RET(ISC_R_NOMEMORY
);
1262 key
->label
= isc_mem_strdup(key
->mctx
, label
);
1263 if (key
->label
== NULL
)
1264 DST_RET(ISC_R_NOMEMORY
);
1265 rsa
= EVP_PKEY_get1_RSA(pkey
);
1267 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1268 if (rsa_check(rsa
, pubrsa
) != ISC_R_SUCCESS
)
1269 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1270 if (BN_num_bits(rsa
->e
) > RSA_MAX_PUBEXP_BITS
)
1271 DST_RET(ISC_R_RANGE
);
1274 key
->key_size
= EVP_PKEY_bits(pkey
);
1276 key
->keydata
.pkey
= pkey
;
1279 key
->keydata
.rsa
= rsa
;
1280 EVP_PKEY_free(pkey
);
1282 dst__privstruct_free(&priv
, mctx
);
1283 memset(&priv
, 0, sizeof(priv
));
1284 return (ISC_R_SUCCESS
);
1286 DST_RET(DST_R_NOENGINE
);
1292 DST_RET(ISC_R_NOMEMORY
);
1296 pkey
= EVP_PKEY_new();
1298 DST_RET(ISC_R_NOMEMORY
);
1299 if (!EVP_PKEY_set1_RSA(pkey
, rsa
))
1300 DST_RET(ISC_R_FAILURE
);
1301 key
->keydata
.pkey
= pkey
;
1303 key
->keydata
.rsa
= rsa
;
1306 for (i
= 0; i
< priv
.nelements
; i
++) {
1308 switch (priv
.elements
[i
].tag
) {
1309 case TAG_RSA_ENGINE
:
1314 bn
= BN_bin2bn(priv
.elements
[i
].data
,
1315 priv
.elements
[i
].length
, NULL
);
1317 DST_RET(ISC_R_NOMEMORY
);
1320 switch (priv
.elements
[i
].tag
) {
1321 case TAG_RSA_MODULUS
:
1324 case TAG_RSA_PUBLICEXPONENT
:
1327 case TAG_RSA_PRIVATEEXPONENT
:
1330 case TAG_RSA_PRIME1
:
1333 case TAG_RSA_PRIME2
:
1336 case TAG_RSA_EXPONENT1
:
1339 case TAG_RSA_EXPONENT2
:
1342 case TAG_RSA_COEFFICIENT
:
1347 dst__privstruct_free(&priv
, mctx
);
1348 memset(&priv
, 0, sizeof(priv
));
1350 if (rsa_check(rsa
, pubrsa
) != ISC_R_SUCCESS
)
1351 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1352 if (BN_num_bits(rsa
->e
) > RSA_MAX_PUBEXP_BITS
)
1353 DST_RET(ISC_R_RANGE
);
1354 key
->key_size
= BN_num_bits(rsa
->n
);
1361 return (ISC_R_SUCCESS
);
1366 EVP_PKEY_free(pkey
);
1372 key
->keydata
.generic
= NULL
;
1373 dst__privstruct_free(&priv
, mctx
);
1374 memset(&priv
, 0, sizeof(priv
));
1379 opensslrsa_fromlabel(dst_key_t
*key
, const char *engine
, const char *label
,
1385 EVP_PKEY
*pkey
= NULL
;
1386 RSA
*rsa
= NULL
, *pubrsa
= NULL
;
1392 DST_RET(DST_R_NOENGINE
);
1393 e
= dst__openssl_getengine(engine
);
1395 DST_RET(DST_R_NOENGINE
);
1396 pkey
= ENGINE_load_public_key(e
, label
, NULL
, NULL
);
1398 pubrsa
= EVP_PKEY_get1_RSA(pkey
);
1399 EVP_PKEY_free(pkey
);
1401 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1403 pkey
= ENGINE_load_private_key(e
, label
, NULL
, NULL
);
1405 DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
1407 if (engine
!= NULL
) {
1408 key
->engine
= isc_mem_strdup(key
->mctx
, engine
);
1409 if (key
->engine
== NULL
)
1410 DST_RET(ISC_R_NOMEMORY
);
1412 key
->engine
= isc_mem_strdup(key
->mctx
, label
);
1413 if (key
->engine
== NULL
)
1414 DST_RET(ISC_R_NOMEMORY
);
1415 colon
= strchr(key
->engine
, ':');
1419 key
->label
= isc_mem_strdup(key
->mctx
, label
);
1420 if (key
->label
== NULL
)
1421 DST_RET(ISC_R_NOMEMORY
);
1422 rsa
= EVP_PKEY_get1_RSA(pkey
);
1424 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1425 if (rsa_check(rsa
, pubrsa
) != ISC_R_SUCCESS
)
1426 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1427 if (BN_num_bits(rsa
->e
) > RSA_MAX_PUBEXP_BITS
)
1428 DST_RET(ISC_R_RANGE
);
1431 key
->key_size
= EVP_PKEY_bits(pkey
);
1433 key
->keydata
.pkey
= pkey
;
1436 key
->keydata
.rsa
= rsa
;
1437 EVP_PKEY_free(pkey
);
1439 return (ISC_R_SUCCESS
);
1447 EVP_PKEY_free(pkey
);
1454 return(DST_R_NOENGINE
);
1458 static dst_func_t opensslrsa_functions
= {
1459 opensslrsa_createctx
,
1460 NULL
, /*%< createctx2 */
1461 opensslrsa_destroyctx
,
1466 NULL
, /*%< computesecret */
1468 NULL
, /*%< paramcompare */
1469 opensslrsa_generate
,
1470 opensslrsa_isprivate
,
1476 NULL
, /*%< cleanup */
1477 opensslrsa_fromlabel
,
1479 NULL
, /*%< restore */
1483 dst__opensslrsa_init(dst_func_t
**funcp
, unsigned char algorithm
) {
1484 REQUIRE(funcp
!= NULL
);
1486 if (*funcp
== NULL
) {
1487 switch (algorithm
) {
1488 case DST_ALG_RSASHA256
:
1489 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1490 *funcp
= &opensslrsa_functions
;
1493 case DST_ALG_RSASHA512
:
1494 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1495 *funcp
= &opensslrsa_functions
;
1499 *funcp
= &opensslrsa_functions
;
1503 return (ISC_R_SUCCESS
);
1508 #include <isc/util.h>
1510 EMPTY_TRANSLATION_UNIT
1512 #endif /* OPENSSL */