4 * Copyright (C) 2004-2009 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
22 * Id: opensslrsa_link.c,v 1.37 2009/10/30 05:08:23 marka Exp
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>
56 #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 (ISC_R_FAILURE
);
161 dctx
->ctxdata
.evp_md_ctx
= evp_md_ctx
;
163 switch (dctx
->key
->key_alg
) {
168 md5ctx
= isc_mem_get(dctx
->mctx
, sizeof(isc_md5_t
));
170 return (ISC_R_NOMEMORY
);
171 isc_md5_init(md5ctx
);
172 dctx
->ctxdata
.md5ctx
= md5ctx
;
175 case DST_ALG_RSASHA1
:
176 case DST_ALG_NSEC3RSASHA1
:
180 sha1ctx
= isc_mem_get(dctx
->mctx
, sizeof(isc_sha1_t
));
182 return (ISC_R_NOMEMORY
);
183 isc_sha1_init(sha1ctx
);
184 dctx
->ctxdata
.sha1ctx
= sha1ctx
;
187 case DST_ALG_RSASHA256
:
189 isc_sha256_t
*sha256ctx
;
191 sha256ctx
= isc_mem_get(dctx
->mctx
,
192 sizeof(isc_sha256_t
));
193 if (sha256ctx
== NULL
)
194 return (ISC_R_NOMEMORY
);
195 isc_sha256_init(sha256ctx
);
196 dctx
->ctxdata
.sha256ctx
= sha256ctx
;
199 case DST_ALG_RSASHA512
:
201 isc_sha512_t
*sha512ctx
;
203 sha512ctx
= isc_mem_get(dctx
->mctx
,
204 sizeof(isc_sha512_t
));
205 if (sha512ctx
== NULL
)
206 return (ISC_R_NOMEMORY
);
207 isc_sha512_init(sha512ctx
);
208 dctx
->ctxdata
.sha512ctx
= sha512ctx
;
216 return (ISC_R_SUCCESS
);
220 opensslrsa_destroyctx(dst_context_t
*dctx
) {
222 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
225 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
226 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
227 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
228 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
229 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
232 if (evp_md_ctx
!= NULL
) {
233 EVP_MD_CTX_destroy(evp_md_ctx
);
234 dctx
->ctxdata
.evp_md_ctx
= NULL
;
237 switch (dctx
->key
->key_alg
) {
240 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
242 if (md5ctx
!= NULL
) {
243 isc_md5_invalidate(md5ctx
);
244 isc_mem_put(dctx
->mctx
, md5ctx
,
246 dctx
->ctxdata
.md5ctx
= NULL
;
250 case DST_ALG_RSASHA1
:
251 case DST_ALG_NSEC3RSASHA1
:
253 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
255 if (sha1ctx
!= NULL
) {
256 isc_sha1_invalidate(sha1ctx
);
257 isc_mem_put(dctx
->mctx
, sha1ctx
,
259 dctx
->ctxdata
.sha1ctx
= NULL
;
263 case DST_ALG_RSASHA256
:
265 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
267 if (sha256ctx
!= NULL
) {
268 isc_sha256_invalidate(sha256ctx
);
269 isc_mem_put(dctx
->mctx
, sha256ctx
,
270 sizeof(isc_sha256_t
));
271 dctx
->ctxdata
.sha256ctx
= NULL
;
275 case DST_ALG_RSASHA512
:
277 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
279 if (sha512ctx
!= NULL
) {
280 isc_sha512_invalidate(sha512ctx
);
281 isc_mem_put(dctx
->mctx
, sha512ctx
,
282 sizeof(isc_sha512_t
));
283 dctx
->ctxdata
.sha512ctx
= NULL
;
294 opensslrsa_adddata(dst_context_t
*dctx
, const isc_region_t
*data
) {
296 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
299 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
300 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
301 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
302 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
303 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
306 if (!EVP_DigestUpdate(evp_md_ctx
, data
->base
, data
->length
)) {
307 return (ISC_R_FAILURE
);
310 switch (dctx
->key
->key_alg
) {
313 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
315 isc_md5_update(md5ctx
, data
->base
, data
->length
);
318 case DST_ALG_RSASHA1
:
319 case DST_ALG_NSEC3RSASHA1
:
321 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
323 isc_sha1_update(sha1ctx
, data
->base
, data
->length
);
326 case DST_ALG_RSASHA256
:
328 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
330 isc_sha256_update(sha256ctx
, data
->base
, data
->length
);
333 case DST_ALG_RSASHA512
:
335 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
337 isc_sha512_update(sha512ctx
, data
->base
, data
->length
);
344 return (ISC_R_SUCCESS
);
347 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
349 * Digest prefixes from RFC 5702.
351 static unsigned char sha256_prefix
[] =
352 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
353 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
354 static unsigned char sha512_prefix
[] =
355 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
356 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
357 #define PREFIXLEN sizeof(sha512_prefix)
363 opensslrsa_sign(dst_context_t
*dctx
, isc_buffer_t
*sig
) {
364 dst_key_t
*key
= dctx
->key
;
366 unsigned int siglen
= 0;
368 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
369 EVP_PKEY
*pkey
= key
->keydata
.pkey
;
371 RSA
*rsa
= key
->keydata
.rsa
;
372 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
373 unsigned char digest
[PREFIXLEN
+ ISC_SHA512_DIGESTLENGTH
];
376 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 (ISC_R_FAILURE
);
403 if (r
.length
< (unsigned int) RSA_size(rsa
))
404 return (ISC_R_NOSPACE
);
406 switch (dctx
->key
->key_alg
) {
409 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
411 isc_md5_final(md5ctx
, digest
);
413 digestlen
= ISC_MD5_DIGESTLENGTH
;
416 case DST_ALG_RSASHA1
:
417 case DST_ALG_NSEC3RSASHA1
:
419 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
421 isc_sha1_final(sha1ctx
, digest
);
423 digestlen
= ISC_SHA1_DIGESTLENGTH
;
426 case DST_ALG_RSASHA256
:
428 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
430 isc_sha256_final(digest
, sha256ctx
);
431 digestlen
= ISC_SHA256_DIGESTLENGTH
;
432 #if OPENSSL_VERSION_NUMBER < 0x00908000L
433 prefix
= sha256_prefix
;
434 prefixlen
= sizeof(sha256_prefix
);
440 case DST_ALG_RSASHA512
:
442 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
444 isc_sha512_final(digest
, sha512ctx
);
445 digestlen
= ISC_SHA512_DIGESTLENGTH
;
446 #if OPENSSL_VERSION_NUMBER < 0x00908000L
447 prefix
= sha512_prefix
;
448 prefixlen
= sizeof(sha512_prefix
);
458 #if OPENSSL_VERSION_NUMBER < 0x00908000L
459 switch (dctx
->key
->key_alg
) {
461 case DST_ALG_RSASHA1
:
462 case DST_ALG_NSEC3RSASHA1
:
464 status
= RSA_sign(type
, digest
, digestlen
, r
.base
,
468 case DST_ALG_RSASHA256
:
469 case DST_ALG_RSASHA512
:
470 INSIST(prefix
!= NULL
);
471 INSIST(prefixlen
!= 0);
472 INSIST(prefixlen
+ digestlen
<= sizeof(digest
));
474 memmove(digest
+ prefixlen
, digest
, digestlen
);
475 memcpy(digest
, prefix
, prefixlen
);
476 status
= RSA_private_encrypt(digestlen
+ prefixlen
,
490 status
= RSA_sign(type
, digest
, digestlen
, r
.base
, &siglen
, rsa
);
493 err
= ERR_peek_error_line(&file
, &line
);
495 message
= ERR_error_string(err
, NULL
);
497 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
501 isc_buffer_add(sig
, siglen
);
503 return (ISC_R_SUCCESS
);
507 opensslrsa_verify(dst_context_t
*dctx
, const isc_region_t
*sig
) {
508 dst_key_t
*key
= dctx
->key
;
511 EVP_MD_CTX
*evp_md_ctx
= dctx
->ctxdata
.evp_md_ctx
;
512 EVP_PKEY
*pkey
= key
->keydata
.pkey
;
514 /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
515 unsigned char digest
[ISC_SHA512_DIGESTLENGTH
];
517 unsigned int digestlen
= 0;
518 RSA
*rsa
= key
->keydata
.rsa
;
519 #if OPENSSL_VERSION_NUMBER < 0x00908000L
520 unsigned int prefixlen
= 0;
521 const unsigned char *prefix
= NULL
;
525 REQUIRE(dctx
->key
->key_alg
== DST_ALG_RSAMD5
||
526 dctx
->key
->key_alg
== DST_ALG_RSASHA1
||
527 dctx
->key
->key_alg
== DST_ALG_NSEC3RSASHA1
||
528 dctx
->key
->key_alg
== DST_ALG_RSASHA256
||
529 dctx
->key
->key_alg
== DST_ALG_RSASHA512
);
532 status
= EVP_VerifyFinal(evp_md_ctx
, sig
->base
, sig
->length
, pkey
);
534 switch (dctx
->key
->key_alg
) {
537 isc_md5_t
*md5ctx
= dctx
->ctxdata
.md5ctx
;
539 isc_md5_final(md5ctx
, digest
);
541 digestlen
= ISC_MD5_DIGESTLENGTH
;
544 case DST_ALG_RSASHA1
:
545 case DST_ALG_NSEC3RSASHA1
:
547 isc_sha1_t
*sha1ctx
= dctx
->ctxdata
.sha1ctx
;
549 isc_sha1_final(sha1ctx
, digest
);
551 digestlen
= ISC_SHA1_DIGESTLENGTH
;
554 case DST_ALG_RSASHA256
:
556 isc_sha256_t
*sha256ctx
= dctx
->ctxdata
.sha256ctx
;
558 isc_sha256_final(digest
, sha256ctx
);
559 digestlen
= ISC_SHA256_DIGESTLENGTH
;
560 #if OPENSSL_VERSION_NUMBER < 0x00908000L
561 prefix
= sha256_prefix
;
562 prefixlen
= sizeof(sha256_prefix
);
568 case DST_ALG_RSASHA512
:
570 isc_sha512_t
*sha512ctx
= dctx
->ctxdata
.sha512ctx
;
572 isc_sha512_final(digest
, sha512ctx
);
573 digestlen
= ISC_SHA512_DIGESTLENGTH
;
574 #if OPENSSL_VERSION_NUMBER < 0x00908000L
575 prefix
= sha512_prefix
;
576 prefixlen
= sizeof(sha512_prefix
);
586 if (sig
->length
!= (unsigned int) RSA_size(rsa
))
587 return (DST_R_VERIFYFAILURE
);
589 #if OPENSSL_VERSION_NUMBER < 0x00908000L
590 switch (dctx
->key
->key_alg
) {
592 case DST_ALG_RSASHA1
:
593 case DST_ALG_NSEC3RSASHA1
:
595 status
= RSA_verify(type
, digest
, digestlen
, sig
->base
,
599 case DST_ALG_RSASHA256
:
600 case DST_ALG_RSASHA512
:
603 * 1024 is big enough for all valid RSA bit sizes
604 * for use with DNSSEC.
606 unsigned char original
[PREFIXLEN
+ 1024];
608 INSIST(prefix
!= NULL
);
609 INSIST(prefixlen
!= 0U);
611 if (RSA_size(rsa
) > (int)sizeof(original
))
612 return (DST_R_VERIFYFAILURE
);
614 status
= RSA_public_decrypt(sig
->length
, sig
->base
,
618 return (DST_R_VERIFYFAILURE
);
619 if (status
!= (int)(prefixlen
+ digestlen
))
620 return (DST_R_VERIFYFAILURE
);
621 if (memcmp(original
, prefix
, prefixlen
))
622 return (DST_R_VERIFYFAILURE
);
623 if (memcmp(original
+ prefixlen
, digest
, digestlen
))
624 return (DST_R_VERIFYFAILURE
);
634 status
= RSA_verify(type
, digest
, digestlen
, sig
->base
,
639 return (dst__openssl_toresult(DST_R_VERIFYFAILURE
));
641 return (ISC_R_SUCCESS
);
645 opensslrsa_compare(const dst_key_t
*key1
, const dst_key_t
*key2
) {
647 RSA
*rsa1
= NULL
, *rsa2
= NULL
;
649 EVP_PKEY
*pkey1
, *pkey2
;
653 pkey1
= key1
->keydata
.pkey
;
654 pkey2
= key2
->keydata
.pkey
;
656 * The pkey reference will keep these around after
657 * the RSA_free() call.
660 rsa1
= EVP_PKEY_get1_RSA(pkey1
);
664 rsa2
= EVP_PKEY_get1_RSA(pkey2
);
668 rsa1
= key1
->keydata
.rsa
;
669 rsa2
= key2
->keydata
.rsa
;
672 if (rsa1
== NULL
&& rsa2
== NULL
)
674 else if (rsa1
== NULL
|| rsa2
== NULL
)
677 status
= BN_cmp(rsa1
->n
, rsa2
->n
) ||
678 BN_cmp(rsa1
->e
, rsa2
->e
);
684 if ((rsa1
->flags
& RSA_FLAG_EXT_PKEY
) != 0 ||
685 (rsa2
->flags
& RSA_FLAG_EXT_PKEY
) != 0) {
686 if ((rsa1
->flags
& RSA_FLAG_EXT_PKEY
) == 0 ||
687 (rsa2
->flags
& RSA_FLAG_EXT_PKEY
) == 0)
690 * Can't compare private parameters, BTW does it make sense?
696 if (rsa1
->d
!= NULL
|| rsa2
->d
!= NULL
) {
697 if (rsa1
->d
== NULL
|| rsa2
->d
== NULL
)
699 status
= BN_cmp(rsa1
->d
, rsa2
->d
) ||
700 BN_cmp(rsa1
->p
, rsa2
->p
) ||
701 BN_cmp(rsa1
->q
, rsa2
->q
);
709 #if OPENSSL_VERSION_NUMBER > 0x00908000L
711 progress_cb(int p
, int n
, BN_GENCB
*cb
)
728 opensslrsa_generate(dst_key_t
*key
, int exp
, void (*callback
)(int)) {
729 #if OPENSSL_VERSION_NUMBER > 0x00908000L
735 RSA
*rsa
= RSA_new();
736 BIGNUM
*e
= BN_new();
738 EVP_PKEY
*pkey
= EVP_PKEY_new();
741 if (rsa
== NULL
|| e
== NULL
)
746 if (!EVP_PKEY_set1_RSA(pkey
, rsa
))
760 if (callback
== NULL
) {
761 BN_GENCB_set_old(&cb
, NULL
, NULL
);
764 BN_GENCB_set(&cb
, &progress_cb
, u
.dptr
);
767 if (RSA_generate_key_ex(rsa
, key
->key_size
, e
, &cb
)) {
771 key
->keydata
.pkey
= pkey
;
775 key
->keydata
.rsa
= rsa
;
777 return (ISC_R_SUCCESS
);
789 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
794 EVP_PKEY
*pkey
= EVP_PKEY_new();
799 return (ISC_R_NOMEMORY
);
808 rsa
= RSA_generate_key(key
->key_size
, e
, NULL
, NULL
);
813 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
817 if (!EVP_PKEY_set1_RSA(pkey
, rsa
)) {
820 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
822 key
->keydata
.pkey
= pkey
;
825 key
->keydata
.rsa
= rsa
;
828 return (ISC_R_SUCCESS
);
833 opensslrsa_isprivate(const dst_key_t
*key
) {
835 RSA
*rsa
= EVP_PKEY_get1_RSA(key
->keydata
.pkey
);
838 /* key->keydata.pkey still has a reference so rsa is still valid. */
840 RSA
*rsa
= key
->keydata
.rsa
;
842 if (rsa
!= NULL
&& (rsa
->flags
& RSA_FLAG_EXT_PKEY
) != 0)
844 return (ISC_TF(rsa
!= NULL
&& rsa
->d
!= NULL
));
848 opensslrsa_destroy(dst_key_t
*key
) {
850 EVP_PKEY
*pkey
= key
->keydata
.pkey
;
852 key
->keydata
.pkey
= NULL
;
854 RSA
*rsa
= key
->keydata
.rsa
;
856 key
->keydata
.rsa
= NULL
;
862 opensslrsa_todns(const dst_key_t
*key
, isc_buffer_t
*data
) {
864 unsigned int e_bytes
;
865 unsigned int mod_bytes
;
873 REQUIRE(key
->keydata
.pkey
!= NULL
);
875 REQUIRE(key
->keydata
.rsa
!= NULL
);
879 pkey
= key
->keydata
.pkey
;
880 rsa
= EVP_PKEY_get1_RSA(pkey
);
882 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
884 rsa
= key
->keydata
.rsa
;
887 isc_buffer_availableregion(data
, &r
);
889 e_bytes
= BN_num_bytes(rsa
->e
);
890 mod_bytes
= BN_num_bytes(rsa
->n
);
892 if (e_bytes
< 256) { /*%< key exponent is <= 2040 bits */
894 DST_RET(ISC_R_NOSPACE
);
895 isc_buffer_putuint8(data
, (isc_uint8_t
) e_bytes
);
896 isc_region_consume(&r
, 1);
899 DST_RET(ISC_R_NOSPACE
);
900 isc_buffer_putuint8(data
, 0);
901 isc_buffer_putuint16(data
, (isc_uint16_t
) e_bytes
);
902 isc_region_consume(&r
, 3);
905 if (r
.length
< e_bytes
+ mod_bytes
)
906 DST_RET(ISC_R_NOSPACE
);
908 BN_bn2bin(rsa
->e
, r
.base
);
909 isc_region_consume(&r
, e_bytes
);
910 BN_bn2bin(rsa
->n
, r
.base
);
912 isc_buffer_add(data
, e_bytes
+ mod_bytes
);
924 opensslrsa_fromdns(dst_key_t
*key
, isc_buffer_t
*data
) {
927 unsigned int e_bytes
;
932 isc_buffer_remainingregion(data
, &r
);
934 return (ISC_R_SUCCESS
);
938 return (dst__openssl_toresult(ISC_R_NOMEMORY
));
943 return (DST_R_INVALIDPUBLICKEY
);
951 return (DST_R_INVALIDPUBLICKEY
);
953 e_bytes
= ((*r
.base
++) << 8);
954 e_bytes
+= *r
.base
++;
958 if (r
.length
< e_bytes
) {
960 return (DST_R_INVALIDPUBLICKEY
);
962 rsa
->e
= BN_bin2bn(r
.base
, e_bytes
, NULL
);
966 rsa
->n
= BN_bin2bn(r
.base
, r
.length
, NULL
);
968 key
->key_size
= BN_num_bits(rsa
->n
);
970 isc_buffer_forward(data
, r
.length
);
973 pkey
= EVP_PKEY_new();
976 return (ISC_R_NOMEMORY
);
978 if (!EVP_PKEY_set1_RSA(pkey
, rsa
)) {
981 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
983 key
->keydata
.pkey
= pkey
;
986 key
->keydata
.rsa
= rsa
;
989 return (ISC_R_SUCCESS
);
993 opensslrsa_tofile(const dst_key_t
*key
, const char *directory
) {
997 unsigned char *bufs
[8];
1001 if (key
->keydata
.pkey
== NULL
)
1002 return (DST_R_NULLKEY
);
1003 rsa
= EVP_PKEY_get1_RSA(key
->keydata
.pkey
);
1005 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1007 if (key
->keydata
.rsa
== NULL
)
1008 return (DST_R_NULLKEY
);
1009 rsa
= key
->keydata
.rsa
;
1012 for (i
= 0; i
< 8; i
++) {
1013 bufs
[i
] = isc_mem_get(key
->mctx
, BN_num_bytes(rsa
->n
));
1014 if (bufs
[i
] == NULL
) {
1015 result
= ISC_R_NOMEMORY
;
1022 priv
.elements
[i
].tag
= TAG_RSA_MODULUS
;
1023 priv
.elements
[i
].length
= BN_num_bytes(rsa
->n
);
1024 BN_bn2bin(rsa
->n
, bufs
[i
]);
1025 priv
.elements
[i
].data
= bufs
[i
];
1028 priv
.elements
[i
].tag
= TAG_RSA_PUBLICEXPONENT
;
1029 priv
.elements
[i
].length
= BN_num_bytes(rsa
->e
);
1030 BN_bn2bin(rsa
->e
, bufs
[i
]);
1031 priv
.elements
[i
].data
= bufs
[i
];
1034 if (rsa
->d
!= NULL
) {
1035 priv
.elements
[i
].tag
= TAG_RSA_PRIVATEEXPONENT
;
1036 priv
.elements
[i
].length
= BN_num_bytes(rsa
->d
);
1037 BN_bn2bin(rsa
->d
, bufs
[i
]);
1038 priv
.elements
[i
].data
= bufs
[i
];
1042 if (rsa
->p
!= NULL
) {
1043 priv
.elements
[i
].tag
= TAG_RSA_PRIME1
;
1044 priv
.elements
[i
].length
= BN_num_bytes(rsa
->p
);
1045 BN_bn2bin(rsa
->p
, bufs
[i
]);
1046 priv
.elements
[i
].data
= bufs
[i
];
1050 if (rsa
->q
!= NULL
) {
1051 priv
.elements
[i
].tag
= TAG_RSA_PRIME2
;
1052 priv
.elements
[i
].length
= BN_num_bytes(rsa
->q
);
1053 BN_bn2bin(rsa
->q
, bufs
[i
]);
1054 priv
.elements
[i
].data
= bufs
[i
];
1058 if (rsa
->dmp1
!= NULL
) {
1059 priv
.elements
[i
].tag
= TAG_RSA_EXPONENT1
;
1060 priv
.elements
[i
].length
= BN_num_bytes(rsa
->dmp1
);
1061 BN_bn2bin(rsa
->dmp1
, bufs
[i
]);
1062 priv
.elements
[i
].data
= bufs
[i
];
1066 if (rsa
->dmq1
!= NULL
) {
1067 priv
.elements
[i
].tag
= TAG_RSA_EXPONENT2
;
1068 priv
.elements
[i
].length
= BN_num_bytes(rsa
->dmq1
);
1069 BN_bn2bin(rsa
->dmq1
, bufs
[i
]);
1070 priv
.elements
[i
].data
= bufs
[i
];
1074 if (rsa
->iqmp
!= NULL
) {
1075 priv
.elements
[i
].tag
= TAG_RSA_COEFFICIENT
;
1076 priv
.elements
[i
].length
= BN_num_bytes(rsa
->iqmp
);
1077 BN_bn2bin(rsa
->iqmp
, bufs
[i
]);
1078 priv
.elements
[i
].data
= bufs
[i
];
1082 if (key
->engine
!= NULL
) {
1083 priv
.elements
[i
].tag
= TAG_RSA_ENGINE
;
1084 priv
.elements
[i
].length
= strlen(key
->engine
) + 1;
1085 priv
.elements
[i
].data
= (unsigned char *)key
->engine
;
1089 if (key
->label
!= NULL
) {
1090 priv
.elements
[i
].tag
= TAG_RSA_LABEL
;
1091 priv
.elements
[i
].length
= strlen(key
->label
) + 1;
1092 priv
.elements
[i
].data
= (unsigned char *)key
->label
;
1098 result
= dst__privstruct_writefile(key
, &priv
, directory
);
1103 for (i
= 0; i
< 8; i
++) {
1104 if (bufs
[i
] == NULL
)
1106 isc_mem_put(key
->mctx
, bufs
[i
], BN_num_bytes(rsa
->n
));
1112 rsa_check(RSA
*rsa
, RSA
*pub
)
1114 /* Public parameters should be the same but if they are not set
1115 * copy them from the public key. */
1117 if (rsa
->n
!= NULL
) {
1118 if (BN_cmp(rsa
->n
, pub
->n
) != 0)
1119 return (DST_R_INVALIDPRIVATEKEY
);
1124 if (rsa
->e
!= NULL
) {
1125 if (BN_cmp(rsa
->e
, pub
->e
) != 0)
1126 return (DST_R_INVALIDPRIVATEKEY
);
1132 if (rsa
->n
== NULL
|| rsa
->e
== NULL
)
1133 return (DST_R_INVALIDPRIVATEKEY
);
1134 return (ISC_R_SUCCESS
);
1138 opensslrsa_parse(dst_key_t
*key
, isc_lex_t
*lexer
, dst_key_t
*pub
) {
1142 RSA
*rsa
= NULL
, *pubrsa
= NULL
;
1144 isc_mem_t
*mctx
= key
->mctx
;
1145 const char *engine
= NULL
, *label
= NULL
;
1146 EVP_PKEY
*pkey
= NULL
;
1149 if (pub
!= NULL
&& pub
->keydata
.pkey
!= NULL
)
1150 pubrsa
= EVP_PKEY_get1_RSA(pub
->keydata
.pkey
);
1152 if (pub
!= NULL
&& pub
->keydata
.rsa
!= NULL
) {
1153 pubrsa
= pub
->keydata
.rsa
;
1154 pub
->keydata
.rsa
= NULL
;
1158 /* read private key file */
1159 ret
= dst__privstruct_parse(key
, DST_ALG_RSA
, lexer
, mctx
, &priv
);
1160 if (ret
!= ISC_R_SUCCESS
)
1163 for (i
= 0; i
< priv
.nelements
; i
++) {
1164 switch (priv
.elements
[i
].tag
) {
1165 case TAG_RSA_ENGINE
:
1166 engine
= (char *)priv
.elements
[i
].data
;
1169 label
= (char *)priv
.elements
[i
].data
;
1176 * Is this key is stored in a HSM?
1177 * See if we can fetch it.
1179 if (label
!= NULL
) {
1181 DST_RET(DST_R_NOENGINE
);
1182 e
= dst__openssl_getengine(engine
);
1184 DST_RET(DST_R_NOENGINE
);
1185 pkey
= ENGINE_load_private_key(e
, label
, NULL
, NULL
);
1187 /* ERR_print_errors_fp(stderr); */
1188 DST_RET(ISC_R_NOTFOUND
);
1190 key
->engine
= isc_mem_strdup(key
->mctx
, engine
);
1191 if (key
->engine
== NULL
)
1192 DST_RET(ISC_R_NOMEMORY
);
1193 key
->label
= isc_mem_strdup(key
->mctx
, label
);
1194 if (key
->label
== NULL
)
1195 DST_RET(ISC_R_NOMEMORY
);
1196 rsa
= EVP_PKEY_get1_RSA(pkey
);
1198 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1199 if (rsa_check(rsa
, pubrsa
) != ISC_R_SUCCESS
)
1200 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1203 key
->key_size
= EVP_PKEY_bits(pkey
);
1205 key
->keydata
.pkey
= pkey
;
1208 key
->keydata
.rsa
= rsa
;
1209 EVP_PKEY_free(pkey
);
1211 dst__privstruct_free(&priv
, mctx
);
1212 memset(&priv
, 0, sizeof(priv
));
1213 return (ISC_R_SUCCESS
);
1218 DST_RET(ISC_R_NOMEMORY
);
1222 pkey
= EVP_PKEY_new();
1224 DST_RET(ISC_R_NOMEMORY
);
1225 if (!EVP_PKEY_set1_RSA(pkey
, rsa
))
1226 DST_RET(ISC_R_FAILURE
);
1227 key
->keydata
.pkey
= pkey
;
1229 key
->keydata
.rsa
= rsa
;
1232 for (i
= 0; i
< priv
.nelements
; i
++) {
1234 switch (priv
.elements
[i
].tag
) {
1235 case TAG_RSA_ENGINE
:
1242 bn
= BN_bin2bn(priv
.elements
[i
].data
,
1243 priv
.elements
[i
].length
, NULL
);
1245 DST_RET(ISC_R_NOMEMORY
);
1248 switch (priv
.elements
[i
].tag
) {
1249 case TAG_RSA_MODULUS
:
1252 case TAG_RSA_PUBLICEXPONENT
:
1255 case TAG_RSA_PRIVATEEXPONENT
:
1258 case TAG_RSA_PRIME1
:
1261 case TAG_RSA_PRIME2
:
1264 case TAG_RSA_EXPONENT1
:
1267 case TAG_RSA_EXPONENT2
:
1270 case TAG_RSA_COEFFICIENT
:
1275 dst__privstruct_free(&priv
, mctx
);
1276 memset(&priv
, 0, sizeof(priv
));
1278 if (rsa_check(rsa
, pubrsa
) != ISC_R_SUCCESS
)
1279 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1280 key
->key_size
= BN_num_bits(rsa
->n
);
1287 return (ISC_R_SUCCESS
);
1292 EVP_PKEY_free(pkey
);
1298 opensslrsa_destroy(key
);
1299 dst__privstruct_free(&priv
, mctx
);
1300 memset(&priv
, 0, sizeof(priv
));
1305 opensslrsa_fromlabel(dst_key_t
*key
, const char *engine
, const char *label
,
1310 EVP_PKEY
*pkey
= NULL
;
1311 RSA
*rsa
= NULL
, *pubrsa
= NULL
;
1317 DST_RET(DST_R_NOENGINE
);
1318 e
= dst__openssl_getengine(engine
);
1320 DST_RET(DST_R_NOENGINE
);
1321 pkey
= ENGINE_load_public_key(e
, label
, NULL
, NULL
);
1323 pubrsa
= EVP_PKEY_get1_RSA(pkey
);
1324 EVP_PKEY_free(pkey
);
1326 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1328 pkey
= ENGINE_load_private_key(e
, label
, NULL
, NULL
);
1330 DST_RET(ISC_R_NOTFOUND
);
1331 if (engine
!= NULL
) {
1332 key
->engine
= isc_mem_strdup(key
->mctx
, engine
);
1333 if (key
->engine
== NULL
)
1334 DST_RET(ISC_R_NOMEMORY
);
1336 key
->engine
= isc_mem_strdup(key
->mctx
, label
);
1337 if (key
->engine
== NULL
)
1338 DST_RET(ISC_R_NOMEMORY
);
1339 colon
= strchr(key
->engine
, ':');
1343 key
->label
= isc_mem_strdup(key
->mctx
, label
);
1344 if (key
->label
== NULL
)
1345 DST_RET(ISC_R_NOMEMORY
);
1346 rsa
= EVP_PKEY_get1_RSA(pkey
);
1348 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE
));
1349 if (rsa_check(rsa
, pubrsa
) != ISC_R_SUCCESS
)
1350 DST_RET(DST_R_INVALIDPRIVATEKEY
);
1353 key
->key_size
= EVP_PKEY_bits(pkey
);
1355 key
->keydata
.pkey
= pkey
;
1358 key
->keydata
.rsa
= rsa
;
1359 EVP_PKEY_free(pkey
);
1361 return (ISC_R_SUCCESS
);
1369 EVP_PKEY_free(pkey
);
1373 static dst_func_t opensslrsa_functions
= {
1374 opensslrsa_createctx
,
1375 opensslrsa_destroyctx
,
1379 NULL
, /*%< computesecret */
1381 NULL
, /*%< paramcompare */
1382 opensslrsa_generate
,
1383 opensslrsa_isprivate
,
1389 NULL
, /*%< cleanup */
1390 opensslrsa_fromlabel
,
1394 dst__opensslrsa_init(dst_func_t
**funcp
, unsigned char algorithm
) {
1395 REQUIRE(funcp
!= NULL
);
1397 if (*funcp
== NULL
) {
1398 switch (algorithm
) {
1399 case DST_ALG_RSASHA256
:
1400 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1401 *funcp
= &opensslrsa_functions
;
1404 case DST_ALG_RSASHA512
:
1405 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1406 *funcp
= &opensslrsa_functions
;
1410 *funcp
= &opensslrsa_functions
;
1414 return (ISC_R_SUCCESS
);
1419 #include <isc/util.h>
1421 EMPTY_TRANSLATION_UNIT
1423 #endif /* OPENSSL */