2000-09-16 01:12:21 +00:00
|
|
|
/*
|
2014-01-09 23:46:35 +00:00
|
|
|
* Copyright (C) 2004-2009, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
|
2004-03-05 05:14:21 +00:00
|
|
|
* Copyright (C) 2000-2003 Internet Software Consortium.
|
2000-09-16 01:12:21 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2000-09-16 01:12:21 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
2000-09-16 01:12:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Principal Author: Brian Wellington
|
2012-03-08 00:21:15 +11:00
|
|
|
* $Id$
|
2000-09-16 01:12:21 +00:00
|
|
|
*/
|
2001-07-10 04:01:19 +00:00
|
|
|
#ifdef OPENSSL
|
2009-10-27 22:25:37 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#ifndef USE_EVP
|
2009-10-27 22:25:37 +00:00
|
|
|
#if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
|
|
|
|
#define USE_EVP 0
|
|
|
|
#else
|
2008-03-31 14:42:51 +00:00
|
|
|
#define USE_EVP 1
|
|
|
|
#endif
|
2009-10-27 22:25:37 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <isc/entropy.h>
|
|
|
|
#include <isc/md5.h>
|
|
|
|
#include <isc/sha1.h>
|
2009-10-22 02:21:31 +00:00
|
|
|
#include <isc/sha2.h>
|
2000-09-16 01:12:21 +00:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dst/result.h>
|
|
|
|
|
|
|
|
#include "dst_internal.h"
|
2002-03-19 04:30:57 +00:00
|
|
|
#include "dst_openssl.h"
|
2000-09-16 01:12:21 +00:00
|
|
|
#include "dst_parse.h"
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/rsa.h>
|
2006-10-02 01:44:27 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER > 0x00908000L
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#endif
|
2011-03-11 01:11:54 +00:00
|
|
|
#ifdef USE_ENGINE
|
2008-03-31 14:42:51 +00:00
|
|
|
#include <openssl/engine.h>
|
2011-03-11 01:11:54 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2012-06-14 15:44:20 +10:00
|
|
|
/*
|
|
|
|
* Limit the size of public exponents.
|
|
|
|
*/
|
|
|
|
#ifndef RSA_MAX_PUBEXP_BITS
|
|
|
|
#define RSA_MAX_PUBEXP_BITS 35
|
|
|
|
#endif
|
|
|
|
|
2006-10-11 02:15:59 +00:00
|
|
|
/*
|
|
|
|
* We don't use configure for windows so enforce the OpenSSL version
|
|
|
|
* here. Unlike with configure we don't support overriding this test.
|
|
|
|
*/
|
2006-10-11 03:54:56 +00:00
|
|
|
#ifdef WIN32
|
2006-10-11 02:15:59 +00:00
|
|
|
#if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
|
2006-11-07 21:23:06 +00:00
|
|
|
OPENSSL_VERSION_NUMBER < 0x00908000L) || \
|
2008-04-01 23:47:10 +00:00
|
|
|
OPENSSL_VERSION_NUMBER >= 0x0090804fL)
|
2006-10-11 02:15:59 +00:00
|
|
|
#error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-09-30 06:00:40 +00:00
|
|
|
/*
|
2009-01-17 14:45:17 +00:00
|
|
|
* XXXMPA Temporarily disable RSA_BLINDING as it requires
|
|
|
|
* good quality random data that cannot currently be guaranteed.
|
2003-09-30 06:00:40 +00:00
|
|
|
* XXXMPA Find which versions of openssl use pseudo random data
|
|
|
|
* and set RSA_FLAG_BLINDING for those.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
2002-01-15 22:09:45 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x0090601fL
|
|
|
|
#define SET_FLAGS(rsa) \
|
2003-07-24 06:08:20 +00:00
|
|
|
do { \
|
|
|
|
(rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
|
|
|
|
(rsa)->flags |= RSA_FLAG_BLINDING; \
|
|
|
|
} while (0)
|
2002-01-15 22:09:45 +00:00
|
|
|
#else
|
2003-07-24 06:08:20 +00:00
|
|
|
#define SET_FLAGS(rsa) \
|
|
|
|
do { \
|
|
|
|
(rsa)->flags |= RSA_FLAG_BLINDING; \
|
|
|
|
} while (0)
|
2002-01-15 22:09:45 +00:00
|
|
|
#endif
|
2003-09-30 06:00:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x0090601fL
|
|
|
|
#define SET_FLAGS(rsa) \
|
|
|
|
do { \
|
|
|
|
(rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
|
|
|
|
(rsa)->flags &= ~RSA_FLAG_BLINDING; \
|
|
|
|
} while (0)
|
2006-03-01 02:32:46 +00:00
|
|
|
#elif defined(RSA_FLAG_NO_BLINDING)
|
|
|
|
#define SET_FLAGS(rsa) \
|
|
|
|
do { \
|
|
|
|
(rsa)->flags &= ~RSA_FLAG_BLINDING; \
|
|
|
|
(rsa)->flags |= RSA_FLAG_NO_BLINDING; \
|
|
|
|
} while (0)
|
2003-09-30 06:00:40 +00:00
|
|
|
#else
|
|
|
|
#define SET_FLAGS(rsa) \
|
|
|
|
do { \
|
|
|
|
(rsa)->flags &= ~RSA_FLAG_BLINDING; \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
2002-01-15 22:09:45 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#define DST_RET(a) {ret = a; goto err;}
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
|
2008-04-01 00:03:31 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_MD_CTX *evp_md_ctx;
|
2009-10-22 02:21:31 +00:00
|
|
|
const EVP_MD *type = NULL;
|
2008-04-01 00:03:31 +00:00
|
|
|
#endif
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
UNUSED(key);
|
2001-09-15 00:01:58 +00:00
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
|
2008-09-24 02:46:23 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA1 ||
|
2009-10-22 02:21:31 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA512);
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
evp_md_ctx = EVP_MD_CTX_create();
|
|
|
|
if (evp_md_ctx == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
2009-10-22 02:21:31 +00:00
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
2008-03-31 14:42:51 +00:00
|
|
|
type = EVP_md5(); /* MD5 + RSA */
|
2009-10-22 02:21:31 +00:00
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
2008-03-31 14:42:51 +00:00
|
|
|
type = EVP_sha1(); /* SHA1 + RSA */
|
2009-10-22 02:21:31 +00:00
|
|
|
break;
|
2009-10-27 22:25:37 +00:00
|
|
|
#ifdef HAVE_EVP_SHA256
|
2009-10-22 02:21:31 +00:00
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
type = EVP_sha256(); /* SHA256 + RSA */
|
|
|
|
break;
|
2009-10-27 22:25:37 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_EVP_SHA512
|
2009-10-22 02:21:31 +00:00
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
type = EVP_sha512();
|
|
|
|
break;
|
2009-10-27 22:25:37 +00:00
|
|
|
#endif
|
2009-10-22 02:21:31 +00:00
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
|
|
|
|
if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
2012-10-24 12:58:16 -07:00
|
|
|
return (dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestInit_ex",
|
2012-07-23 15:08:21 +10:00
|
|
|
ISC_R_FAILURE));
|
2008-03-31 14:42:51 +00:00
|
|
|
}
|
|
|
|
dctx->ctxdata.evp_md_ctx = evp_md_ctx;
|
|
|
|
#else
|
2009-10-22 02:21:31 +00:00
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
{
|
|
|
|
isc_md5_t *md5ctx;
|
|
|
|
|
|
|
|
md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
|
|
|
|
if (md5ctx == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
isc_md5_init(md5ctx);
|
|
|
|
dctx->ctxdata.md5ctx = md5ctx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
{
|
|
|
|
isc_sha1_t *sha1ctx;
|
|
|
|
|
|
|
|
sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
|
|
|
|
if (sha1ctx == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
isc_sha1_init(sha1ctx);
|
|
|
|
dctx->ctxdata.sha1ctx = sha1ctx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
{
|
|
|
|
isc_sha256_t *sha256ctx;
|
|
|
|
|
2009-10-22 23:48:07 +00:00
|
|
|
sha256ctx = isc_mem_get(dctx->mctx,
|
2009-10-22 02:21:31 +00:00
|
|
|
sizeof(isc_sha256_t));
|
|
|
|
if (sha256ctx == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
isc_sha256_init(sha256ctx);
|
|
|
|
dctx->ctxdata.sha256ctx = sha256ctx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
{
|
|
|
|
isc_sha512_t *sha512ctx;
|
|
|
|
|
2009-10-22 23:48:07 +00:00
|
|
|
sha512ctx = isc_mem_get(dctx->mctx,
|
2009-10-22 02:21:31 +00:00
|
|
|
sizeof(isc_sha512_t));
|
|
|
|
if (sha512ctx == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
isc_sha512_init(sha512ctx);
|
|
|
|
dctx->ctxdata.sha512ctx = sha512ctx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opensslrsa_destroyctx(dst_context_t *dctx) {
|
2008-04-01 00:03:31 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
#endif
|
|
|
|
|
2001-09-15 00:01:58 +00:00
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
|
2008-09-24 02:46:23 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA1 ||
|
2009-10-22 02:21:31 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA512);
|
2001-09-15 00:01:58 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (evp_md_ctx != NULL) {
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
|
|
|
dctx->ctxdata.evp_md_ctx = NULL;
|
|
|
|
}
|
|
|
|
#else
|
2009-10-22 02:21:31 +00:00
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
{
|
|
|
|
isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
|
|
|
|
|
|
|
|
if (md5ctx != NULL) {
|
|
|
|
isc_md5_invalidate(md5ctx);
|
|
|
|
isc_mem_put(dctx->mctx, md5ctx,
|
|
|
|
sizeof(isc_md5_t));
|
|
|
|
dctx->ctxdata.md5ctx = NULL;
|
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
2009-10-22 02:21:31 +00:00
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
{
|
|
|
|
isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
|
|
|
|
|
|
|
|
if (sha1ctx != NULL) {
|
|
|
|
isc_sha1_invalidate(sha1ctx);
|
|
|
|
isc_mem_put(dctx->mctx, sha1ctx,
|
|
|
|
sizeof(isc_sha1_t));
|
|
|
|
dctx->ctxdata.sha1ctx = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
{
|
|
|
|
isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
|
|
|
|
|
|
|
|
if (sha256ctx != NULL) {
|
|
|
|
isc_sha256_invalidate(sha256ctx);
|
|
|
|
isc_mem_put(dctx->mctx, sha256ctx,
|
|
|
|
sizeof(isc_sha256_t));
|
|
|
|
dctx->ctxdata.sha256ctx = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
{
|
|
|
|
isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
|
|
|
|
|
|
|
|
if (sha512ctx != NULL) {
|
|
|
|
isc_sha512_invalidate(sha512ctx);
|
|
|
|
isc_mem_put(dctx->mctx, sha512ctx,
|
|
|
|
sizeof(isc_sha512_t));
|
|
|
|
dctx->ctxdata.sha512ctx = NULL;
|
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
2009-10-22 02:21:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
|
2008-04-01 00:03:31 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
2008-04-01 23:47:10 +00:00
|
|
|
#endif
|
2008-04-01 00:03:31 +00:00
|
|
|
|
2001-09-15 00:01:58 +00:00
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
|
2008-09-24 02:46:23 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA1 ||
|
2009-10-22 02:21:31 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA512);
|
2001-09-15 00:01:58 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
|
2012-10-24 12:58:16 -07:00
|
|
|
return (dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestUpdate",
|
2012-07-23 15:08:21 +10:00
|
|
|
ISC_R_FAILURE));
|
2008-03-31 14:42:51 +00:00
|
|
|
}
|
|
|
|
#else
|
2009-10-22 02:21:31 +00:00
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
{
|
|
|
|
isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
|
|
|
|
|
|
|
|
isc_md5_update(md5ctx, data->base, data->length);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
{
|
|
|
|
isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
|
|
|
|
|
|
|
|
isc_sha1_update(sha1ctx, data->base, data->length);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
{
|
|
|
|
isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
|
|
|
|
|
|
|
|
isc_sha256_update(sha256ctx, data->base, data->length);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
{
|
|
|
|
isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
|
|
|
|
|
|
|
|
isc_sha512_update(sha512ctx, data->base, data->length);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2009-10-27 22:25:37 +00:00
|
|
|
#if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
/*
|
2009-10-28 21:08:18 +00:00
|
|
|
* Digest prefixes from RFC 5702.
|
2009-10-27 22:25:37 +00:00
|
|
|
*/
|
|
|
|
static unsigned char sha256_prefix[] =
|
|
|
|
{ 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
|
|
|
|
0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
|
|
|
|
static unsigned char sha512_prefix[] =
|
|
|
|
{ 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
|
|
|
|
0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
|
|
|
|
#define PREFIXLEN sizeof(sha512_prefix)
|
|
|
|
#else
|
|
|
|
#define PREFIXLEN 0
|
|
|
|
#endif
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
isc_region_t r;
|
2008-03-31 14:42:51 +00:00
|
|
|
unsigned int siglen = 0;
|
|
|
|
#if USE_EVP
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
#else
|
|
|
|
RSA *rsa = key->keydata.rsa;
|
2009-10-22 02:21:31 +00:00
|
|
|
/* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
|
2009-10-27 22:25:37 +00:00
|
|
|
unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
|
2000-09-16 01:12:21 +00:00
|
|
|
int status;
|
2009-10-22 02:21:31 +00:00
|
|
|
int type = 0;
|
|
|
|
unsigned int digestlen = 0;
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
unsigned int prefixlen = 0;
|
|
|
|
const unsigned char *prefix = NULL;
|
|
|
|
#endif
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2001-09-15 00:01:58 +00:00
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
|
2008-09-24 02:46:23 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA1 ||
|
2009-10-22 02:21:31 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA512);
|
2001-09-15 00:01:58 +00:00
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
isc_buffer_availableregion(sig, &r);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (r.length < (unsigned int) EVP_PKEY_size(pkey))
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
|
|
|
|
if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
|
2012-10-24 12:58:16 -07:00
|
|
|
return (dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_SignFinal",
|
2012-07-23 15:08:21 +10:00
|
|
|
ISC_R_FAILURE));
|
2008-03-31 14:42:51 +00:00
|
|
|
}
|
|
|
|
#else
|
2000-09-16 01:12:21 +00:00
|
|
|
if (r.length < (unsigned int) RSA_size(rsa))
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
|
2009-10-22 02:21:31 +00:00
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
{
|
|
|
|
isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
|
|
|
|
|
|
|
|
isc_md5_final(md5ctx, digest);
|
|
|
|
type = NID_md5;
|
|
|
|
digestlen = ISC_MD5_DIGESTLENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
{
|
|
|
|
isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
|
|
|
|
|
|
|
|
isc_sha1_final(sha1ctx, digest);
|
|
|
|
type = NID_sha1;
|
|
|
|
digestlen = ISC_SHA1_DIGESTLENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
{
|
|
|
|
isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
|
|
|
|
|
|
|
|
isc_sha256_final(digest, sha256ctx);
|
|
|
|
digestlen = ISC_SHA256_DIGESTLENGTH;
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
prefix = sha256_prefix;
|
|
|
|
prefixlen = sizeof(sha256_prefix);
|
|
|
|
#else
|
|
|
|
type = NID_sha256;
|
|
|
|
#endif
|
2009-10-22 02:21:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
{
|
|
|
|
isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
|
|
|
|
|
|
|
|
isc_sha512_final(digest, sha512ctx);
|
|
|
|
digestlen = ISC_SHA512_DIGESTLENGTH;
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
prefix = sha512_prefix;
|
|
|
|
prefixlen = sizeof(sha512_prefix);
|
|
|
|
#else
|
|
|
|
type = NID_sha512;
|
|
|
|
#endif
|
2009-10-22 02:21:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
INSIST(type != 0);
|
|
|
|
status = RSA_sign(type, digest, digestlen, r.base,
|
|
|
|
&siglen, rsa);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
INSIST(prefix != NULL);
|
|
|
|
INSIST(prefixlen != 0);
|
|
|
|
INSIST(prefixlen + digestlen <= sizeof(digest));
|
|
|
|
|
|
|
|
memmove(digest + prefixlen, digest, digestlen);
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(digest, prefix, prefixlen);
|
2009-10-27 22:25:37 +00:00
|
|
|
status = RSA_private_encrypt(digestlen + prefixlen,
|
|
|
|
digest, r.base, rsa,
|
|
|
|
RSA_PKCS1_PADDING);
|
|
|
|
if (status < 0)
|
|
|
|
status = 0;
|
|
|
|
else
|
|
|
|
siglen = status;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
INSIST(type != 0);
|
2000-09-16 01:12:21 +00:00
|
|
|
status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
|
2009-10-27 22:25:37 +00:00
|
|
|
#endif
|
2012-07-23 15:08:21 +10:00
|
|
|
if (status == 0)
|
2012-10-24 12:58:16 -07:00
|
|
|
return (dst__openssl_toresult3(dctx->category,
|
|
|
|
"RSA_sign",
|
2012-07-23 15:08:21 +10:00
|
|
|
DST_R_OPENSSLFAILURE));
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
isc_buffer_add(sig, siglen);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2012-06-15 11:54:58 +10:00
|
|
|
opensslrsa_verify2(dst_context_t *dctx, int maxbits, const isc_region_t *sig) {
|
2000-09-16 01:12:21 +00:00
|
|
|
dst_key_t *key = dctx->key;
|
2008-03-31 14:42:51 +00:00
|
|
|
int status = 0;
|
|
|
|
#if USE_EVP
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
2012-06-14 15:44:20 +10:00
|
|
|
RSA *rsa;
|
|
|
|
int bits;
|
2008-03-31 14:42:51 +00:00
|
|
|
#else
|
2009-10-22 02:21:31 +00:00
|
|
|
/* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
|
|
|
|
unsigned char digest[ISC_SHA512_DIGESTLENGTH];
|
|
|
|
int type = 0;
|
|
|
|
unsigned int digestlen = 0;
|
2008-03-31 14:42:51 +00:00
|
|
|
RSA *rsa = key->keydata.rsa;
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
unsigned int prefixlen = 0;
|
|
|
|
const unsigned char *prefix = NULL;
|
|
|
|
#endif
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2001-09-15 00:01:58 +00:00
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
|
2008-09-24 02:46:23 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA1 ||
|
2009-10-22 02:21:31 +00:00
|
|
|
dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_RSASHA512);
|
2001-09-15 00:01:58 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
2012-06-14 15:44:20 +10:00
|
|
|
rsa = EVP_PKEY_get1_RSA(pkey);
|
|
|
|
if (rsa == NULL)
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
bits = BN_num_bits(rsa->e);
|
|
|
|
RSA_free(rsa);
|
|
|
|
if (bits > maxbits && maxbits != 0)
|
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
|
2012-10-24 12:58:16 -07:00
|
|
|
switch (status) {
|
|
|
|
case 1:
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
case 0:
|
|
|
|
return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
|
|
|
|
default:
|
|
|
|
return (dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_VerifyFinal",
|
|
|
|
DST_R_VERIFYFAILURE));
|
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
#else
|
2012-06-14 15:44:20 +10:00
|
|
|
if (BN_num_bits(rsa->e) > maxbits && maxbits != 0)
|
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
|
2009-10-22 02:21:31 +00:00
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
{
|
|
|
|
isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
|
|
|
|
|
|
|
|
isc_md5_final(md5ctx, digest);
|
|
|
|
type = NID_md5;
|
|
|
|
digestlen = ISC_MD5_DIGESTLENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
{
|
|
|
|
isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
|
|
|
|
|
|
|
|
isc_sha1_final(sha1ctx, digest);
|
|
|
|
type = NID_sha1;
|
|
|
|
digestlen = ISC_SHA1_DIGESTLENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
{
|
|
|
|
isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
|
|
|
|
|
|
|
|
isc_sha256_final(digest, sha256ctx);
|
|
|
|
digestlen = ISC_SHA256_DIGESTLENGTH;
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
prefix = sha256_prefix;
|
|
|
|
prefixlen = sizeof(sha256_prefix);
|
|
|
|
#else
|
|
|
|
type = NID_sha256;
|
|
|
|
#endif
|
2009-10-22 02:21:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
{
|
|
|
|
isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
|
|
|
|
|
|
|
|
isc_sha512_final(digest, sha512ctx);
|
|
|
|
digestlen = ISC_SHA512_DIGESTLENGTH;
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
prefix = sha512_prefix;
|
|
|
|
prefixlen = sizeof(sha512_prefix);
|
|
|
|
#else
|
|
|
|
type = NID_sha512;
|
|
|
|
#endif
|
2009-10-22 02:21:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 22:25:37 +00:00
|
|
|
if (sig->length != (unsigned int) RSA_size(rsa))
|
2000-09-16 01:12:21 +00:00
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
|
2009-10-27 22:25:37 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00908000L
|
|
|
|
switch (dctx->key->key_alg) {
|
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
case DST_ALG_NSEC3RSASHA1:
|
|
|
|
INSIST(type != 0);
|
|
|
|
status = RSA_verify(type, digest, digestlen, sig->base,
|
|
|
|
RSA_size(rsa), rsa);
|
|
|
|
break;
|
|
|
|
|
2009-10-27 23:47:45 +00:00
|
|
|
case DST_ALG_RSASHA256:
|
2009-10-27 22:25:37 +00:00
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 1024 is big enough for all valid RSA bit sizes
|
|
|
|
* for use with DNSSEC.
|
|
|
|
*/
|
|
|
|
unsigned char original[PREFIXLEN + 1024];
|
|
|
|
|
|
|
|
INSIST(prefix != NULL);
|
|
|
|
INSIST(prefixlen != 0U);
|
|
|
|
|
|
|
|
if (RSA_size(rsa) > (int)sizeof(original))
|
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
|
|
|
|
status = RSA_public_decrypt(sig->length, sig->base,
|
|
|
|
original, rsa,
|
|
|
|
RSA_PKCS1_PADDING);
|
2009-10-27 23:47:45 +00:00
|
|
|
if (status <= 0)
|
2012-10-24 12:58:16 -07:00
|
|
|
return (dst__openssl_toresult3(
|
|
|
|
dctx->category,
|
2012-07-23 15:08:21 +10:00
|
|
|
"RSA_public_decrypt",
|
|
|
|
DST_R_VERIFYFAILURE));
|
2009-10-27 23:47:45 +00:00
|
|
|
if (status != (int)(prefixlen + digestlen))
|
2009-10-27 22:25:37 +00:00
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
if (memcmp(original, prefix, prefixlen))
|
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
if (memcmp(original + prefixlen, digest, digestlen))
|
|
|
|
return (DST_R_VERIFYFAILURE);
|
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
INSIST(type != 0);
|
2000-09-16 01:12:21 +00:00
|
|
|
status = RSA_verify(type, digest, digestlen, sig->base,
|
2009-10-27 22:25:37 +00:00
|
|
|
RSA_size(rsa), rsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2009-01-14 17:28:47 +00:00
|
|
|
if (status != 1)
|
2012-10-24 12:58:16 -07:00
|
|
|
return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
|
2000-09-16 01:12:21 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2012-10-24 12:58:16 -07:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:44:20 +10:00
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
|
|
|
|
return (opensslrsa_verify2(dctx, 0, sig));
|
|
|
|
}
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
int status;
|
2008-03-31 14:42:51 +00:00
|
|
|
RSA *rsa1 = NULL, *rsa2 = NULL;
|
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY *pkey1, *pkey2;
|
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
pkey1 = key1->keydata.pkey;
|
|
|
|
pkey2 = key2->keydata.pkey;
|
|
|
|
/*
|
|
|
|
* The pkey reference will keep these around after
|
|
|
|
* the RSA_free() call.
|
|
|
|
*/
|
|
|
|
if (pkey1 != NULL) {
|
|
|
|
rsa1 = EVP_PKEY_get1_RSA(pkey1);
|
|
|
|
RSA_free(rsa1);
|
|
|
|
}
|
|
|
|
if (pkey2 != NULL) {
|
|
|
|
rsa2 = EVP_PKEY_get1_RSA(pkey2);
|
|
|
|
RSA_free(rsa2);
|
|
|
|
}
|
|
|
|
#else
|
2006-12-04 01:54:53 +00:00
|
|
|
rsa1 = key1->keydata.rsa;
|
|
|
|
rsa2 = key2->keydata.rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
if (rsa1 == NULL && rsa2 == NULL)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
else if (rsa1 == NULL || rsa2 == NULL)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
|
|
|
status = BN_cmp(rsa1->n, rsa2->n) ||
|
|
|
|
BN_cmp(rsa1->e, rsa2->e);
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
|
|
|
|
(rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
|
|
|
|
if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
|
|
|
|
(rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
/*
|
|
|
|
* Can't compare private parameters, BTW does it make sense?
|
|
|
|
*/
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
if (rsa1->d != NULL || rsa2->d != NULL) {
|
|
|
|
if (rsa1->d == NULL || rsa2->d == NULL)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
status = BN_cmp(rsa1->d, rsa2->d) ||
|
|
|
|
BN_cmp(rsa1->p, rsa2->p) ||
|
|
|
|
BN_cmp(rsa1->q, rsa2->q);
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
2009-10-24 09:46:19 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER > 0x00908000L
|
|
|
|
static int
|
2014-01-14 15:40:56 -08:00
|
|
|
progress_cb(int p, int n, BN_GENCB *cb) {
|
2009-10-30 05:08:23 +00:00
|
|
|
union {
|
|
|
|
void *dptr;
|
|
|
|
void (*fptr)(int);
|
|
|
|
} u;
|
2009-10-24 09:46:19 +00:00
|
|
|
|
|
|
|
UNUSED(n);
|
2009-10-30 05:08:23 +00:00
|
|
|
|
|
|
|
u.dptr = cb->arg;
|
|
|
|
if (u.fptr != NULL)
|
|
|
|
u.fptr(p);
|
2009-10-24 09:46:19 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-02 01:44:27 +00:00
|
|
|
static isc_result_t
|
2009-10-24 09:46:19 +00:00
|
|
|
opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
|
2006-10-02 01:44:27 +00:00
|
|
|
#if OPENSSL_VERSION_NUMBER > 0x00908000L
|
2012-07-24 12:18:10 +10:00
|
|
|
isc_result_t ret = DST_R_OPENSSLFAILURE;
|
2006-10-10 02:30:10 +00:00
|
|
|
BN_GENCB cb;
|
2009-10-30 05:08:23 +00:00
|
|
|
union {
|
|
|
|
void *dptr;
|
|
|
|
void (*fptr)(int);
|
|
|
|
} u;
|
2006-10-10 02:30:10 +00:00
|
|
|
RSA *rsa = RSA_new();
|
|
|
|
BIGNUM *e = BN_new();
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY *pkey = EVP_PKEY_new();
|
|
|
|
#endif
|
2006-03-01 02:32:46 +00:00
|
|
|
|
2006-10-10 02:30:10 +00:00
|
|
|
if (rsa == NULL || e == NULL)
|
2006-03-01 02:32:46 +00:00
|
|
|
goto err;
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (pkey == NULL)
|
|
|
|
goto err;
|
|
|
|
if (!EVP_PKEY_set1_RSA(pkey, rsa))
|
|
|
|
goto err;
|
|
|
|
#endif
|
2006-03-01 02:32:46 +00:00
|
|
|
|
2006-10-02 01:44:27 +00:00
|
|
|
if (exp == 0) {
|
|
|
|
/* RSA_F4 0x10001 */
|
|
|
|
BN_set_bit(e, 0);
|
|
|
|
BN_set_bit(e, 16);
|
|
|
|
} else {
|
2012-06-14 15:44:20 +10:00
|
|
|
/* (phased-out) F5 0x100000001 */
|
2006-10-02 01:44:27 +00:00
|
|
|
BN_set_bit(e, 0);
|
2006-10-10 02:30:10 +00:00
|
|
|
BN_set_bit(e, 32);
|
2006-03-01 02:32:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-24 09:46:19 +00:00
|
|
|
if (callback == NULL) {
|
|
|
|
BN_GENCB_set_old(&cb, NULL, NULL);
|
|
|
|
} else {
|
2009-10-30 05:08:23 +00:00
|
|
|
u.fptr = callback;
|
|
|
|
BN_GENCB_set(&cb, &progress_cb, u.dptr);
|
2009-10-24 09:46:19 +00:00
|
|
|
}
|
2006-03-01 02:32:46 +00:00
|
|
|
|
2006-10-10 02:30:10 +00:00
|
|
|
if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
|
|
|
|
BN_free(e);
|
2006-10-02 01:44:27 +00:00
|
|
|
SET_FLAGS(rsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
key->keydata.pkey = pkey;
|
2008-04-01 23:47:10 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
RSA_free(rsa);
|
|
|
|
#else
|
2006-12-04 02:26:05 +00:00
|
|
|
key->keydata.rsa = rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2006-10-02 01:44:27 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2006-10-10 02:30:10 +00:00
|
|
|
}
|
2012-07-23 15:08:21 +10:00
|
|
|
ret = dst__openssl_toresult2("RSA_generate_key_ex",
|
|
|
|
DST_R_OPENSSLFAILURE);
|
2006-10-02 01:44:27 +00:00
|
|
|
|
2006-03-01 02:32:46 +00:00
|
|
|
err:
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (pkey != NULL)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
#endif
|
2006-10-10 02:30:10 +00:00
|
|
|
if (e != NULL)
|
2006-03-01 02:32:46 +00:00
|
|
|
BN_free(e);
|
2006-10-10 02:30:10 +00:00
|
|
|
if (rsa != NULL)
|
2006-03-01 02:32:46 +00:00
|
|
|
RSA_free(rsa);
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult(ret));
|
2006-10-02 01:44:27 +00:00
|
|
|
#else
|
2000-09-16 01:12:21 +00:00
|
|
|
RSA *rsa;
|
|
|
|
unsigned long e;
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY *pkey = EVP_PKEY_new();
|
2008-04-01 23:47:10 +00:00
|
|
|
|
2009-10-24 09:46:19 +00:00
|
|
|
UNUSED(callback);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (pkey == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
2009-10-24 09:46:19 +00:00
|
|
|
#else
|
|
|
|
UNUSED(callback);
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
if (exp == 0)
|
2006-10-02 01:44:27 +00:00
|
|
|
e = RSA_F4;
|
2000-09-16 01:12:21 +00:00
|
|
|
else
|
2006-10-02 01:44:27 +00:00
|
|
|
e = 0x40000003;
|
2000-09-16 01:12:21 +00:00
|
|
|
rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa == NULL) {
|
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
#endif
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult2("RSA_generate_key",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2008-03-31 14:42:51 +00:00
|
|
|
}
|
2002-01-15 22:09:45 +00:00
|
|
|
SET_FLAGS(rsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
RSA_free(rsa);
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
key->keydata.pkey = pkey;
|
|
|
|
RSA_free(rsa);
|
|
|
|
#else
|
2006-12-04 02:26:05 +00:00
|
|
|
key->keydata.rsa = rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2006-10-02 01:44:27 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_boolean_t
|
|
|
|
opensslrsa_isprivate(const dst_key_t *key) {
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
|
|
|
|
INSIST(rsa != NULL);
|
|
|
|
RSA_free(rsa);
|
|
|
|
/* key->keydata.pkey still has a reference so rsa is still valid. */
|
|
|
|
#else
|
|
|
|
RSA *rsa = key->keydata.rsa;
|
|
|
|
#endif
|
|
|
|
if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
|
|
|
|
return (ISC_TRUE);
|
2001-12-12 17:09:39 +00:00
|
|
|
return (ISC_TF(rsa != NULL && rsa->d != NULL));
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opensslrsa_destroy(dst_key_t *key) {
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
key->keydata.pkey = NULL;
|
|
|
|
#else
|
2006-12-04 02:26:05 +00:00
|
|
|
RSA *rsa = key->keydata.rsa;
|
2000-09-16 01:12:21 +00:00
|
|
|
RSA_free(rsa);
|
2006-12-04 02:26:05 +00:00
|
|
|
key->keydata.rsa = NULL;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
isc_region_t r;
|
|
|
|
unsigned int e_bytes;
|
|
|
|
unsigned int mod_bytes;
|
2008-03-31 14:42:51 +00:00
|
|
|
isc_result_t ret;
|
|
|
|
RSA *rsa;
|
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
REQUIRE(key->keydata.pkey != NULL);
|
|
|
|
#else
|
2006-12-04 01:54:53 +00:00
|
|
|
REQUIRE(key->keydata.rsa != NULL);
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
pkey = key->keydata.pkey;
|
|
|
|
rsa = EVP_PKEY_get1_RSA(pkey);
|
|
|
|
if (rsa == NULL)
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
#else
|
2006-12-04 01:54:53 +00:00
|
|
|
rsa = key->keydata.rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
isc_buffer_availableregion(data, &r);
|
|
|
|
|
|
|
|
e_bytes = BN_num_bytes(rsa->e);
|
|
|
|
mod_bytes = BN_num_bytes(rsa->n);
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
|
2000-09-16 01:12:21 +00:00
|
|
|
if (r.length < 1)
|
2008-03-31 14:42:51 +00:00
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2000-09-16 01:12:21 +00:00
|
|
|
isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
|
2009-10-20 02:59:19 +00:00
|
|
|
isc_region_consume(&r, 1);
|
2000-09-16 01:12:21 +00:00
|
|
|
} else {
|
|
|
|
if (r.length < 3)
|
2008-03-31 14:42:51 +00:00
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2000-09-16 01:12:21 +00:00
|
|
|
isc_buffer_putuint8(data, 0);
|
|
|
|
isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
|
2009-10-20 02:59:19 +00:00
|
|
|
isc_region_consume(&r, 3);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r.length < e_bytes + mod_bytes)
|
2009-10-20 02:59:19 +00:00
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
BN_bn2bin(rsa->e, r.base);
|
2009-10-20 02:59:19 +00:00
|
|
|
isc_region_consume(&r, e_bytes);
|
2000-09-16 01:12:21 +00:00
|
|
|
BN_bn2bin(rsa->n, r.base);
|
|
|
|
|
|
|
|
isc_buffer_add(data, e_bytes + mod_bytes);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
err:
|
|
|
|
#if USE_EVP
|
|
|
|
if (rsa != NULL)
|
|
|
|
RSA_free(rsa);
|
|
|
|
#endif
|
|
|
|
return (ret);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
RSA *rsa;
|
|
|
|
isc_region_t r;
|
|
|
|
unsigned int e_bytes;
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
isc_buffer_remainingregion(data, &r);
|
|
|
|
if (r.length == 0)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
rsa = RSA_new();
|
|
|
|
if (rsa == NULL)
|
2005-06-17 02:22:45 +00:00
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
2002-01-15 22:09:45 +00:00
|
|
|
SET_FLAGS(rsa);
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2001-11-06 17:59:50 +00:00
|
|
|
if (r.length < 1) {
|
|
|
|
RSA_free(rsa);
|
2000-09-16 01:12:21 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2001-11-06 17:59:50 +00:00
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
e_bytes = *r.base++;
|
|
|
|
r.length--;
|
|
|
|
|
|
|
|
if (e_bytes == 0) {
|
2001-11-06 17:59:50 +00:00
|
|
|
if (r.length < 2) {
|
|
|
|
RSA_free(rsa);
|
2000-09-16 01:12:21 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2001-11-06 17:59:50 +00:00
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
e_bytes = ((*r.base++) << 8);
|
|
|
|
e_bytes += *r.base++;
|
|
|
|
r.length -= 2;
|
|
|
|
}
|
|
|
|
|
2001-11-06 17:59:50 +00:00
|
|
|
if (r.length < e_bytes) {
|
|
|
|
RSA_free(rsa);
|
2000-09-16 01:12:21 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2001-11-06 17:59:50 +00:00
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
|
|
|
|
r.base += e_bytes;
|
|
|
|
r.length -= e_bytes;
|
|
|
|
|
|
|
|
rsa->n = BN_bin2bn(r.base, r.length, NULL);
|
|
|
|
|
|
|
|
key->key_size = BN_num_bits(rsa->n);
|
|
|
|
|
|
|
|
isc_buffer_forward(data, r.length);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (pkey == NULL) {
|
|
|
|
RSA_free(rsa);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
RSA_free(rsa);
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
key->keydata.pkey = pkey;
|
|
|
|
RSA_free(rsa);
|
|
|
|
#else
|
2006-12-04 01:54:53 +00:00
|
|
|
key->keydata.rsa = rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_tofile(const dst_key_t *key, const char *directory) {
|
2000-09-25 23:19:39 +00:00
|
|
|
int i;
|
2000-09-16 01:12:21 +00:00
|
|
|
RSA *rsa;
|
|
|
|
dst_private_t priv;
|
2000-09-25 23:19:39 +00:00
|
|
|
unsigned char *bufs[8];
|
|
|
|
isc_result_t result;
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (key->keydata.pkey == NULL)
|
|
|
|
return (DST_R_NULLKEY);
|
|
|
|
rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
|
|
|
|
if (rsa == NULL)
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
#else
|
2006-12-04 01:54:53 +00:00
|
|
|
if (key->keydata.rsa == NULL)
|
2000-09-16 01:12:21 +00:00
|
|
|
return (DST_R_NULLKEY);
|
2006-12-04 01:54:53 +00:00
|
|
|
rsa = key->keydata.rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2012-07-23 15:08:21 +10:00
|
|
|
memset(bufs, 0, sizeof(bufs));
|
2013-09-04 13:53:02 +10:00
|
|
|
|
|
|
|
if (key->external) {
|
|
|
|
priv.nelements = 0;
|
|
|
|
result = dst__privstruct_writefile(key, &priv, directory);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2000-09-25 23:19:39 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
|
|
|
|
if (bufs[i] == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_RSA_MODULUS;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->n);
|
|
|
|
BN_bn2bin(rsa->n, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->e);
|
|
|
|
BN_bn2bin(rsa->e, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa->d != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->d);
|
|
|
|
BN_bn2bin(rsa->d, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa->p != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_PRIME1;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->p);
|
|
|
|
BN_bn2bin(rsa->p, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa->q != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_PRIME2;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->q);
|
|
|
|
BN_bn2bin(rsa->q, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa->dmp1 != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_EXPONENT1;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->dmp1);
|
|
|
|
BN_bn2bin(rsa->dmp1, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa->dmq1 != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_EXPONENT2;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->dmq1);
|
|
|
|
BN_bn2bin(rsa->dmq1, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (rsa->iqmp != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_COEFFICIENT;
|
|
|
|
priv.elements[i].length = BN_num_bytes(rsa->iqmp);
|
|
|
|
BN_bn2bin(rsa->iqmp, bufs[i]);
|
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
}
|
2008-04-01 23:47:10 +00:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
if (key->engine != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_ENGINE;
|
|
|
|
priv.elements[i].length = strlen(key->engine) + 1;
|
|
|
|
priv.elements[i].data = (unsigned char *)key->engine;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->label != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_RSA_LABEL;
|
|
|
|
priv.elements[i].length = strlen(key->label) + 1;
|
|
|
|
priv.elements[i].data = (unsigned char *)key->label;
|
|
|
|
i++;
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2000-09-25 23:19:39 +00:00
|
|
|
priv.nelements = i;
|
2009-07-19 04:18:05 +00:00
|
|
|
result = dst__privstruct_writefile(key, &priv, directory);
|
2000-09-25 23:19:39 +00:00
|
|
|
fail:
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
RSA_free(rsa);
|
|
|
|
#endif
|
2001-12-12 16:43:24 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (bufs[i] == NULL)
|
|
|
|
break;
|
|
|
|
isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
|
|
|
|
}
|
2000-09-25 23:19:39 +00:00
|
|
|
return (result);
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2009-09-03 04:09:58 +00:00
|
|
|
rsa_check(RSA *rsa, RSA *pub)
|
|
|
|
{
|
|
|
|
/* Public parameters should be the same but if they are not set
|
|
|
|
* copy them from the public key. */
|
|
|
|
if (pub != NULL) {
|
|
|
|
if (rsa->n != NULL) {
|
|
|
|
if (BN_cmp(rsa->n, pub->n) != 0)
|
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
|
|
|
} else {
|
|
|
|
rsa->n = pub->n;
|
|
|
|
pub->n = NULL;
|
|
|
|
}
|
|
|
|
if (rsa->e != NULL) {
|
|
|
|
if (BN_cmp(rsa->e, pub->e) != 0)
|
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
|
|
|
} else {
|
|
|
|
rsa->e = pub->e;
|
|
|
|
pub->e = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rsa->n == NULL || rsa->e == NULL)
|
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
|
2000-09-16 01:12:21 +00:00
|
|
|
dst_private_t priv;
|
|
|
|
isc_result_t ret;
|
|
|
|
int i;
|
2009-09-03 04:09:58 +00:00
|
|
|
RSA *rsa = NULL, *pubrsa = NULL;
|
2011-03-11 01:11:54 +00:00
|
|
|
#ifdef USE_ENGINE
|
2008-03-31 14:42:51 +00:00
|
|
|
ENGINE *e = NULL;
|
2011-03-11 01:11:54 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
isc_mem_t *mctx = key->mctx;
|
2009-09-07 12:58:33 +00:00
|
|
|
const char *engine = NULL, *label = NULL;
|
2011-03-11 02:59:37 +00:00
|
|
|
#if defined(USE_ENGINE) || USE_EVP
|
2008-03-31 14:42:51 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2011-03-11 02:59:37 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2009-09-03 04:09:58 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (pub != NULL && pub->keydata.pkey != NULL)
|
|
|
|
pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
|
|
|
|
#else
|
|
|
|
if (pub != NULL && pub->keydata.rsa != NULL) {
|
|
|
|
pubrsa = pub->keydata.rsa;
|
|
|
|
pub->keydata.rsa = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
/* read private key file */
|
2002-02-27 22:12:06 +00:00
|
|
|
ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
|
2000-09-16 01:12:21 +00:00
|
|
|
if (ret != ISC_R_SUCCESS)
|
2012-07-23 15:08:21 +10:00
|
|
|
goto err;
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2013-09-04 13:53:02 +10:00
|
|
|
if (key->external && priv.nelements != 0)
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
for (i = 0; i < priv.nelements; i++) {
|
|
|
|
switch (priv.elements[i].tag) {
|
|
|
|
case TAG_RSA_ENGINE:
|
2009-09-07 12:58:33 +00:00
|
|
|
engine = (char *)priv.elements[i].data;
|
2008-03-31 14:42:51 +00:00
|
|
|
break;
|
|
|
|
case TAG_RSA_LABEL:
|
|
|
|
label = (char *)priv.elements[i].data;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-04 13:53:02 +10:00
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
/*
|
|
|
|
* Is this key is stored in a HSM?
|
|
|
|
* See if we can fetch it.
|
|
|
|
*/
|
2009-09-07 12:58:33 +00:00
|
|
|
if (label != NULL) {
|
2011-03-11 01:11:54 +00:00
|
|
|
#ifdef USE_ENGINE
|
2009-09-07 12:58:33 +00:00
|
|
|
if (engine == NULL)
|
|
|
|
DST_RET(DST_R_NOENGINE);
|
|
|
|
e = dst__openssl_getengine(engine);
|
2008-03-31 14:42:51 +00:00
|
|
|
if (e == NULL)
|
|
|
|
DST_RET(DST_R_NOENGINE);
|
|
|
|
pkey = ENGINE_load_private_key(e, label, NULL, NULL);
|
2012-07-23 15:08:21 +10:00
|
|
|
if (pkey == NULL)
|
|
|
|
DST_RET(dst__openssl_toresult2(
|
|
|
|
"ENGINE_load_private_key",
|
|
|
|
ISC_R_NOTFOUND));
|
2009-09-07 12:58:33 +00:00
|
|
|
key->engine = isc_mem_strdup(key->mctx, engine);
|
2008-03-31 14:42:51 +00:00
|
|
|
if (key->engine == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
|
|
|
key->label = isc_mem_strdup(key->mctx, label);
|
|
|
|
if (key->label == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2009-09-03 04:09:58 +00:00
|
|
|
rsa = EVP_PKEY_get1_RSA(pkey);
|
|
|
|
if (rsa == NULL)
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2012-06-14 15:44:20 +10:00
|
|
|
if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
|
|
|
|
DST_RET(ISC_R_RANGE);
|
2009-09-07 12:58:33 +00:00
|
|
|
if (pubrsa != NULL)
|
|
|
|
RSA_free(pubrsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
key->key_size = EVP_PKEY_bits(pkey);
|
|
|
|
#if USE_EVP
|
|
|
|
key->keydata.pkey = pkey;
|
2009-09-07 12:58:33 +00:00
|
|
|
RSA_free(rsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
#else
|
2009-09-03 04:09:58 +00:00
|
|
|
key->keydata.rsa = rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
#endif
|
|
|
|
dst__privstruct_free(&priv, mctx);
|
2009-09-03 04:09:58 +00:00
|
|
|
memset(&priv, 0, sizeof(priv));
|
2008-03-31 14:42:51 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2011-03-11 01:11:54 +00:00
|
|
|
#else
|
|
|
|
DST_RET(DST_R_NOENGINE);
|
|
|
|
#endif
|
2008-04-01 23:47:10 +00:00
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
rsa = RSA_new();
|
|
|
|
if (rsa == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2002-01-15 22:09:45 +00:00
|
|
|
SET_FLAGS(rsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
|
|
|
|
#if USE_EVP
|
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (pkey == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2009-09-07 12:58:33 +00:00
|
|
|
if (!EVP_PKEY_set1_RSA(pkey, rsa))
|
2008-03-31 14:42:51 +00:00
|
|
|
DST_RET(ISC_R_FAILURE);
|
|
|
|
key->keydata.pkey = pkey;
|
|
|
|
#else
|
2006-12-04 01:54:53 +00:00
|
|
|
key->keydata.rsa = rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
for (i = 0; i < priv.nelements; i++) {
|
|
|
|
BIGNUM *bn;
|
2008-03-31 14:42:51 +00:00
|
|
|
switch (priv.elements[i].tag) {
|
|
|
|
case TAG_RSA_ENGINE:
|
|
|
|
continue;
|
|
|
|
case TAG_RSA_LABEL:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
bn = BN_bin2bn(priv.elements[i].data,
|
|
|
|
priv.elements[i].length, NULL);
|
|
|
|
if (bn == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2008-04-01 23:47:10 +00:00
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
switch (priv.elements[i].tag) {
|
|
|
|
case TAG_RSA_MODULUS:
|
|
|
|
rsa->n = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_PUBLICEXPONENT:
|
|
|
|
rsa->e = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_PRIVATEEXPONENT:
|
|
|
|
rsa->d = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_PRIME1:
|
|
|
|
rsa->p = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_PRIME2:
|
|
|
|
rsa->q = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_EXPONENT1:
|
|
|
|
rsa->dmp1 = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_EXPONENT2:
|
|
|
|
rsa->dmq1 = bn;
|
|
|
|
break;
|
|
|
|
case TAG_RSA_COEFFICIENT:
|
|
|
|
rsa->iqmp = bn;
|
|
|
|
break;
|
2001-12-12 17:09:39 +00:00
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
}
|
|
|
|
dst__privstruct_free(&priv, mctx);
|
2009-09-03 04:09:58 +00:00
|
|
|
memset(&priv, 0, sizeof(priv));
|
2000-09-16 01:12:21 +00:00
|
|
|
|
2009-09-03 04:09:58 +00:00
|
|
|
if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2013-09-04 13:53:02 +10:00
|
|
|
if (!key->external) {
|
|
|
|
if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
|
|
|
|
DST_RET(ISC_R_RANGE);
|
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
key->key_size = BN_num_bits(rsa->n);
|
2009-09-03 04:09:58 +00:00
|
|
|
if (pubrsa != NULL)
|
|
|
|
RSA_free(pubrsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
RSA_free(rsa);
|
|
|
|
#endif
|
2000-09-16 01:12:21 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
err:
|
2008-03-31 14:42:51 +00:00
|
|
|
#if USE_EVP
|
|
|
|
if (pkey != NULL)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
#endif
|
|
|
|
if (rsa != NULL)
|
|
|
|
RSA_free(rsa);
|
2009-09-03 04:09:58 +00:00
|
|
|
if (pubrsa != NULL)
|
|
|
|
RSA_free(pubrsa);
|
2012-06-14 15:44:20 +10:00
|
|
|
key->keydata.generic = NULL;
|
2000-09-16 01:12:21 +00:00
|
|
|
dst__privstruct_free(&priv, mctx);
|
|
|
|
memset(&priv, 0, sizeof(priv));
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2008-03-31 14:42:51 +00:00
|
|
|
static isc_result_t
|
|
|
|
opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
|
|
|
|
const char *pin)
|
|
|
|
{
|
2011-03-11 01:11:54 +00:00
|
|
|
#ifdef USE_ENGINE
|
2008-03-31 14:42:51 +00:00
|
|
|
ENGINE *e = NULL;
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
2009-09-07 12:58:33 +00:00
|
|
|
RSA *rsa = NULL, *pubrsa = NULL;
|
|
|
|
char *colon;
|
2008-03-31 14:42:51 +00:00
|
|
|
|
|
|
|
UNUSED(pin);
|
|
|
|
|
2009-10-05 17:30:49 +00:00
|
|
|
if (engine == NULL)
|
|
|
|
DST_RET(DST_R_NOENGINE);
|
2008-03-31 14:42:51 +00:00
|
|
|
e = dst__openssl_getengine(engine);
|
|
|
|
if (e == NULL)
|
|
|
|
DST_RET(DST_R_NOENGINE);
|
2009-09-07 12:58:33 +00:00
|
|
|
pkey = ENGINE_load_public_key(e, label, NULL, NULL);
|
|
|
|
if (pkey != NULL) {
|
|
|
|
pubrsa = EVP_PKEY_get1_RSA(pkey);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
if (pubrsa == NULL)
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
pkey = ENGINE_load_private_key(e, label, NULL, NULL);
|
|
|
|
if (pkey == NULL)
|
2012-07-23 15:08:21 +10:00
|
|
|
DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
|
|
|
|
ISC_R_NOTFOUND));
|
2009-09-07 12:58:33 +00:00
|
|
|
if (engine != NULL) {
|
|
|
|
key->engine = isc_mem_strdup(key->mctx, engine);
|
|
|
|
if (key->engine == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
|
|
|
} else {
|
|
|
|
key->engine = isc_mem_strdup(key->mctx, label);
|
|
|
|
if (key->engine == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
|
|
|
colon = strchr(key->engine, ':');
|
|
|
|
if (colon != NULL)
|
|
|
|
*colon = '\0';
|
|
|
|
}
|
2008-03-31 14:42:51 +00:00
|
|
|
key->label = isc_mem_strdup(key->mctx, label);
|
|
|
|
if (key->label == NULL)
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2009-09-07 12:58:33 +00:00
|
|
|
rsa = EVP_PKEY_get1_RSA(pkey);
|
|
|
|
if (rsa == NULL)
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2012-06-14 15:44:20 +10:00
|
|
|
if (BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS)
|
|
|
|
DST_RET(ISC_R_RANGE);
|
2009-09-07 12:58:33 +00:00
|
|
|
if (pubrsa != NULL)
|
|
|
|
RSA_free(pubrsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
key->key_size = EVP_PKEY_bits(pkey);
|
|
|
|
#if USE_EVP
|
|
|
|
key->keydata.pkey = pkey;
|
2009-09-07 12:58:33 +00:00
|
|
|
RSA_free(rsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
#else
|
2009-09-07 12:58:33 +00:00
|
|
|
key->keydata.rsa = rsa;
|
2008-03-31 14:42:51 +00:00
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
#endif
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
err:
|
2009-09-07 12:58:33 +00:00
|
|
|
if (rsa != NULL)
|
|
|
|
RSA_free(rsa);
|
|
|
|
if (pubrsa != NULL)
|
|
|
|
RSA_free(pubrsa);
|
2008-03-31 14:42:51 +00:00
|
|
|
if (pkey != NULL)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
return (ret);
|
2011-03-11 01:11:54 +00:00
|
|
|
#else
|
|
|
|
UNUSED(key);
|
|
|
|
UNUSED(engine);
|
|
|
|
UNUSED(label);
|
|
|
|
UNUSED(pin);
|
|
|
|
return(DST_R_NOENGINE);
|
|
|
|
#endif
|
2008-03-31 14:42:51 +00:00
|
|
|
}
|
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
static dst_func_t opensslrsa_functions = {
|
|
|
|
opensslrsa_createctx,
|
2014-01-14 15:40:56 -08:00
|
|
|
NULL, /*%< createctx2 */
|
2000-09-16 01:12:21 +00:00
|
|
|
opensslrsa_destroyctx,
|
|
|
|
opensslrsa_adddata,
|
|
|
|
opensslrsa_sign,
|
|
|
|
opensslrsa_verify,
|
2012-06-14 15:44:20 +10:00
|
|
|
opensslrsa_verify2,
|
2005-04-27 04:57:32 +00:00
|
|
|
NULL, /*%< computesecret */
|
2000-09-16 01:12:21 +00:00
|
|
|
opensslrsa_compare,
|
2005-04-27 04:57:32 +00:00
|
|
|
NULL, /*%< paramcompare */
|
2000-09-16 01:12:21 +00:00
|
|
|
opensslrsa_generate,
|
|
|
|
opensslrsa_isprivate,
|
|
|
|
opensslrsa_destroy,
|
|
|
|
opensslrsa_todns,
|
|
|
|
opensslrsa_fromdns,
|
|
|
|
opensslrsa_tofile,
|
2002-02-27 22:12:06 +00:00
|
|
|
opensslrsa_parse,
|
2005-04-27 04:57:32 +00:00
|
|
|
NULL, /*%< cleanup */
|
2008-03-31 14:42:51 +00:00
|
|
|
opensslrsa_fromlabel,
|
2011-01-10 05:32:04 +00:00
|
|
|
NULL, /*%< dump */
|
|
|
|
NULL, /*%< restore */
|
2000-09-16 01:12:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
isc_result_t
|
2009-10-27 22:25:37 +00:00
|
|
|
dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
|
2001-11-06 22:51:00 +00:00
|
|
|
REQUIRE(funcp != NULL);
|
2009-10-27 22:25:37 +00:00
|
|
|
|
|
|
|
if (*funcp == NULL) {
|
|
|
|
switch (algorithm) {
|
|
|
|
case DST_ALG_RSASHA256:
|
|
|
|
#if defined(HAVE_EVP_SHA256) || !USE_EVP
|
|
|
|
*funcp = &opensslrsa_functions;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA512:
|
|
|
|
#if defined(HAVE_EVP_SHA512) || !USE_EVP
|
|
|
|
*funcp = &opensslrsa_functions;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*funcp = &opensslrsa_functions;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-09-16 01:12:21 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2001-11-20 21:28:41 +00:00
|
|
|
#else /* OPENSSL */
|
|
|
|
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2002-12-13 02:51:41 +00:00
|
|
|
EMPTY_TRANSLATION_UNIT
|
2001-11-20 21:28:41 +00:00
|
|
|
|
2000-09-16 01:12:21 +00:00
|
|
|
#endif /* OPENSSL */
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|