opensslrsa_link.c revision 6098d364b690cb9dabf96e9664c4689c8559bd2e
823N/A/*
823N/A * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
823N/A * Copyright (C) 2000-2003 Internet Software Consortium.
823N/A *
823N/A * Permission to use, copy, modify, and/or distribute this software for any
823N/A * purpose with or without fee is hereby granted, provided that the above
823N/A * copyright notice and this permission notice appear in all copies.
823N/A *
6983N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
6983N/A * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
823N/A * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
823N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
823N/A * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
823N/A * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
6983N/A * PERFORMANCE OF THIS SOFTWARE.
6983N/A */
6983N/A
6983N/A/*
823N/A * Principal Author: Brian Wellington
823N/A * $Id: opensslrsa_link.c,v 1.20 2008/09/24 02:46:22 marka Exp $
823N/A */
823N/A#ifdef OPENSSL
823N/A#ifndef USE_EVP
5175N/A#define USE_EVP 1
6412N/A#endif
823N/A#if USE_EVP
823N/A#define USE_EVP_RSA 1
823N/A#endif
6412N/A
1498N/A#include <config.h>
6412N/A
983N/A#include <isc/entropy.h>
983N/A#include <isc/md5.h>
5175N/A#include <isc/sha1.h>
878N/A#include <isc/mem.h>
6412N/A#include <isc/string.h>
6412N/A#include <isc/util.h>
6412N/A
6412N/A#include <dst/result.h>
6412N/A
823N/A#include "dst_internal.h"
823N/A#include "dst_openssl.h"
823N/A#include "dst_parse.h"
823N/A
823N/A#include <openssl/err.h>
897N/A#include <openssl/objects.h>
6412N/A#include <openssl/rsa.h>
878N/A#if OPENSSL_VERSION_NUMBER > 0x00908000L
878N/A#include <openssl/bn.h>
878N/A#endif
5175N/A#include <openssl/engine.h>
5175N/A
5175N/A/*
6412N/A * We don't use configure for windows so enforce the OpenSSL version
5175N/A * here. Unlike with configure we don't support overriding this test.
5175N/A */
5175N/A#ifdef WIN32
5175N/A#if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
878N/A OPENSSL_VERSION_NUMBER < 0x00908000L) || \
6412N/A OPENSSL_VERSION_NUMBER >= 0x0090804fL)
983N/A#error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
983N/A#endif
983N/A#endif
983N/A
983N/A
983N/A /*
983N/A * XXXMPA Temporarially disable RSA_BLINDING as it requires
983N/A * good quality random data that cannot currently be guarenteed.
983N/A * XXXMPA Find which versions of openssl use pseudo random data
983N/A * and set RSA_FLAG_BLINDING for those.
878N/A */
878N/A
878N/A#if 0
983N/A#if OPENSSL_VERSION_NUMBER < 0x0090601fL
983N/A#define SET_FLAGS(rsa) \
983N/A do { \
983N/A (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
878N/A (rsa)->flags |= RSA_FLAG_BLINDING; \
878N/A } while (0)
878N/A#else
878N/A#define SET_FLAGS(rsa) \
878N/A do { \
878N/A (rsa)->flags |= RSA_FLAG_BLINDING; \
878N/A } while (0)
5175N/A#endif
878N/A#endif
5175N/A
878N/A#if OPENSSL_VERSION_NUMBER < 0x0090601fL
5175N/A#define SET_FLAGS(rsa) \
5175N/A do { \
5175N/A (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
5175N/A (rsa)->flags &= ~RSA_FLAG_BLINDING; \
5175N/A } while (0)
5175N/A#elif defined(RSA_FLAG_NO_BLINDING)
5175N/A#define SET_FLAGS(rsa) \
5175N/A do { \
5175N/A (rsa)->flags &= ~RSA_FLAG_BLINDING; \
5175N/A (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
5175N/A } while (0)
5175N/A#else
5175N/A#define SET_FLAGS(rsa) \
5175N/A do { \
6412N/A (rsa)->flags &= ~RSA_FLAG_BLINDING; \
6412N/A } while (0)
6412N/A#endif
5175N/A
5175N/A#define DST_RET(a) {ret = a; goto err;}
5175N/A
5175N/Astatic isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
5175N/A
5175N/Astatic isc_result_t
5175N/Aopensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
5175N/A#if USE_EVP
5175N/A EVP_MD_CTX *evp_md_ctx;
5175N/A const EVP_MD *type;
5175N/A#endif
5175N/A
5175N/A UNUSED(key);
5175N/A REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
5175N/A dctx->key->key_alg == DST_ALG_RSASHA1 ||
5175N/A dctx->key->key_alg == DST_ALG_NSEC3RSASHA1);
878N/A
823N/A#if USE_EVP
5175N/A evp_md_ctx = EVP_MD_CTX_create();
5175N/A if (evp_md_ctx == NULL)
5175N/A return (ISC_R_NOMEMORY);
5175N/A
5175N/A if (dctx->key->key_alg == DST_ALG_RSAMD5)
878N/A type = EVP_md5(); /* MD5 + RSA */
878N/A else
823N/A type = EVP_sha1(); /* SHA1 + RSA */
878N/A
983N/A if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
983N/A EVP_MD_CTX_destroy(evp_md_ctx);
983N/A return (ISC_R_FAILURE);
878N/A }
1497N/A dctx->ctxdata.evp_md_ctx = evp_md_ctx;
1497N/A#else
878N/A if (dctx->key->key_alg == DST_ALG_RSAMD5) {
878N/A isc_md5_t *md5ctx;
5175N/A
2086N/A md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
878N/A if (md5ctx == NULL)
5175N/A return (ISC_R_NOMEMORY);
878N/A isc_md5_init(md5ctx);
5175N/A dctx->ctxdata.md5ctx = md5ctx;
5175N/A } else {
5175N/A isc_sha1_t *sha1ctx;
5175N/A
5175N/A sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
5175N/A if (sha1ctx == NULL)
5175N/A return (ISC_R_NOMEMORY);
5175N/A isc_sha1_init(sha1ctx);
5175N/A dctx->ctxdata.sha1ctx = sha1ctx;
5175N/A }
5175N/A#endif
5175N/A
5175N/A return (ISC_R_SUCCESS);
5175N/A}
823N/A
823N/Astatic void
878N/Aopensslrsa_destroyctx(dst_context_t *dctx) {
878N/A#if USE_EVP
878N/A EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
878N/A#endif
2487N/A
2487N/A REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
2487N/A dctx->key->key_alg == DST_ALG_RSASHA1 ||
2487N/A dctx->key->key_alg == DST_ALG_NSEC3RSASHA1);
2487N/A
2487N/A#if USE_EVP
2487N/A if (evp_md_ctx != NULL) {
2487N/A EVP_MD_CTX_destroy(evp_md_ctx);
5175N/A dctx->ctxdata.evp_md_ctx = NULL;
5175N/A }
5175N/A#else
5175N/A if (dctx->key->key_alg == DST_ALG_RSAMD5) {
5175N/A isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
5175N/A
5175N/A if (md5ctx != NULL) {
5175N/A isc_md5_invalidate(md5ctx);
5175N/A isc_mem_put(dctx->mctx, md5ctx, sizeof(isc_md5_t));
5175N/A dctx->ctxdata.md5ctx = NULL;
2487N/A }
2487N/A } else {
2487N/A isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
983N/A
983N/A if (sha1ctx != NULL) {
983N/A isc_sha1_invalidate(sha1ctx);
983N/A isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
983N/A dctx->ctxdata.sha1ctx = NULL;
983N/A }
1497N/A }
1497N/A#endif
878N/A}
878N/A
5175N/Astatic isc_result_t
1497N/Aopensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
2086N/A#if USE_EVP
983N/A EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
889N/A#endif
5175N/A
5175N/A REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
5175N/A dctx->key->key_alg == DST_ALG_RSASHA1 ||
5175N/A dctx->key->key_alg == DST_ALG_NSEC3RSASHA1);
5175N/A
5175N/A#if USE_EVP
5175N/A if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
5175N/A return (ISC_R_FAILURE);
5175N/A }
5175N/A#else
5175N/A if (dctx->key->key_alg == DST_ALG_RSAMD5) {
5175N/A isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
5175N/A isc_md5_update(md5ctx, data->base, data->length);
5175N/A } else {
5175N/A isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
5175N/A isc_sha1_update(sha1ctx, data->base, data->length);
5175N/A }
5175N/A#endif
5175N/A return (ISC_R_SUCCESS);
5175N/A}
5175N/A
983N/Astatic isc_result_t
878N/Aopensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
878N/A dst_key_t *key = dctx->key;
878N/A isc_region_t r;
878N/A unsigned int siglen = 0;
878N/A#if USE_EVP
983N/A EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
983N/A EVP_PKEY *pkey = key->keydata.pkey;
983N/A#else
983N/A RSA *rsa = key->keydata.rsa;
878N/A /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */
878N/A unsigned char digest[ISC_SHA1_DIGESTLENGTH];
1497N/A int status;
1497N/A int type;
983N/A unsigned int digestlen;
878N/A char *message;
1497N/A unsigned long err;
1497N/A const char* file;
983N/A int line;
878N/A#endif
5175N/A
1497N/A REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
1497N/A dctx->key->key_alg == DST_ALG_RSASHA1 ||
2086N/A dctx->key->key_alg == DST_ALG_NSEC3RSASHA1);
889N/A
889N/A isc_buffer_availableregion(sig, &r);
889N/A
889N/A#if USE_EVP
889N/A if (r.length < (unsigned int) EVP_PKEY_size(pkey))
878N/A return (ISC_R_NOSPACE);
878N/A
878N/A if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
3853N/A return (ISC_R_FAILURE);
878N/A }
878N/A#else
878N/A if (r.length < (unsigned int) RSA_size(rsa))
878N/A return (ISC_R_NOSPACE);
878N/A
1497N/A if (dctx->key->key_alg == DST_ALG_RSAMD5) {
1497N/A isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
1497N/A isc_md5_final(md5ctx, digest);
1497N/A type = NID_md5;
2086N/A digestlen = ISC_MD5_DIGESTLENGTH;
2086N/A } else {
2086N/A isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
2086N/A isc_sha1_final(sha1ctx, digest);
1497N/A type = NID_sha1;
823N/A digestlen = ISC_SHA1_DIGESTLENGTH;
878N/A }
878N/A
878N/A status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
878N/A if (status == 0) {
878N/A err = ERR_peek_error_line(&file, &line);
878N/A if (err != 0U) {
878N/A message = ERR_error_string(err, NULL);
878N/A }
878N/A return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
983N/A }
983N/A#endif
983N/A
983N/A isc_buffer_add(sig, siglen);
878N/A
983N/A return (ISC_R_SUCCESS);
878N/A}
983N/A
983N/Astatic isc_result_t
983N/Aopensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
878N/A dst_key_t *key = dctx->key;
5175N/A int status = 0;
983N/A#if USE_EVP
983N/A EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
878N/A EVP_PKEY *pkey = key->keydata.pkey;
5175N/A#else
5175N/A /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */
5175N/A unsigned char digest[ISC_SHA1_DIGESTLENGTH];
2086N/A int type;
983N/A unsigned int digestlen;
983N/A RSA *rsa = key->keydata.rsa;
5175N/A#endif
983N/A
983N/A REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
5175N/A dctx->key->key_alg == DST_ALG_RSASHA1 ||
1497N/A dctx->key->key_alg == DST_ALG_NSEC3RSASHA1);
983N/A
983N/A#if USE_EVP
983N/A status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
983N/A#else
983N/A if (dctx->key->key_alg == DST_ALG_RSAMD5) {
5175N/A isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
983N/A isc_md5_final(md5ctx, digest);
983N/A type = NID_md5;
5175N/A digestlen = ISC_MD5_DIGESTLENGTH;
1497N/A } else {
983N/A isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
5175N/A isc_sha1_final(sha1ctx, digest);
5175N/A type = NID_sha1;
5175N/A digestlen = ISC_SHA1_DIGESTLENGTH;
5175N/A }
5175N/A
878N/A if (sig->length < (unsigned int) RSA_size(rsa))
878N/A return (DST_R_VERIFYFAILURE);
878N/A
878N/A status = RSA_verify(type, digest, digestlen, sig->base,
878N/A RSA_size(rsa), rsa);
878N/A#endif
878N/A if (status == 0)
878N/A return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
878N/A
878N/A return (ISC_R_SUCCESS);
5175N/A}
878N/A
878N/Astatic isc_boolean_t
878N/Aopensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
878N/A int status;
878N/A RSA *rsa1 = NULL, *rsa2 = NULL;
878N/A#if USE_EVP
878N/A EVP_PKEY *pkey1, *pkey2;
878N/A#endif
878N/A
878N/A#if USE_EVP
878N/A pkey1 = key1->keydata.pkey;
983N/A pkey2 = key2->keydata.pkey;
983N/A /*
983N/A * The pkey reference will keep these around after
983N/A * the RSA_free() call.
983N/A */
983N/A if (pkey1 != NULL) {
983N/A rsa1 = EVP_PKEY_get1_RSA(pkey1);
983N/A RSA_free(rsa1);
983N/A }
983N/A if (pkey2 != NULL) {
878N/A rsa2 = EVP_PKEY_get1_RSA(pkey2);
5175N/A RSA_free(rsa2);
983N/A }
5175N/A#else
878N/A rsa1 = key1->keydata.rsa;
5175N/A rsa2 = key2->keydata.rsa;
5175N/A#endif
5175N/A
5175N/A if (rsa1 == NULL && rsa2 == NULL)
5175N/A return (ISC_TRUE);
5175N/A else if (rsa1 == NULL || rsa2 == NULL)
5175N/A return (ISC_FALSE);
5175N/A
5175N/A status = BN_cmp(rsa1->n, rsa2->n) ||
5175N/A BN_cmp(rsa1->e, rsa2->e);
5175N/A
5175N/A if (status != 0)
5175N/A return (ISC_FALSE);
5175N/A
5175N/A#if USE_EVP
5175N/A if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
5175N/A (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
5175N/A if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
5175N/A (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
5175N/A return (ISC_FALSE);
5175N/A /*
5175N/A * Can't compare private parameters, BTW does it make sense?
5175N/A */
878N/A return (ISC_TRUE);
878N/A }
878N/A#endif
878N/A
878N/A if (rsa1->d != NULL || rsa2->d != NULL) {
878N/A if (rsa1->d == NULL || rsa2->d == NULL)
878N/A return (ISC_FALSE);
5175N/A status = BN_cmp(rsa1->d, rsa2->d) ||
878N/A BN_cmp(rsa1->p, rsa2->p) ||
5175N/A BN_cmp(rsa1->q, rsa2->q);
5175N/A
943N/A if (status != 0)
5175N/A return (ISC_FALSE);
5175N/A }
5175N/A return (ISC_TRUE);
943N/A}
5175N/A
5175N/Astatic isc_result_t
5175N/Aopensslrsa_generate(dst_key_t *key, int exp) {
5175N/A#if OPENSSL_VERSION_NUMBER > 0x00908000L
5175N/A BN_GENCB cb;
943N/A RSA *rsa = RSA_new();
823N/A BIGNUM *e = BN_new();
5175N/A#if USE_EVP
5175N/A EVP_PKEY *pkey = EVP_PKEY_new();
5175N/A#endif
5175N/A
878N/A if (rsa == NULL || e == NULL)
1065N/A goto err;
1065N/A#if USE_EVP
1065N/A if (pkey == NULL)
6412N/A goto err;
1065N/A if (!EVP_PKEY_set1_RSA(pkey, rsa))
1065N/A goto err;
1065N/A#endif
5175N/A
5175N/A if (exp == 0) {
1065N/A /* RSA_F4 0x10001 */
1065N/A BN_set_bit(e, 0);
5175N/A BN_set_bit(e, 16);
5175N/A } else {
5175N/A /* F5 0x100000001 */
5175N/A BN_set_bit(e, 0);
5175N/A BN_set_bit(e, 32);
5175N/A }
5175N/A
5175N/A BN_GENCB_set_old(&cb, NULL, NULL);
5175N/A
5175N/A if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
5175N/A BN_free(e);
5175N/A SET_FLAGS(rsa);
5175N/A#if USE_EVP
5175N/A key->keydata.pkey = pkey;
5175N/A
5175N/A RSA_free(rsa);
5175N/A#else
5175N/A key->keydata.rsa = rsa;
1065N/A#endif
5175N/A return (ISC_R_SUCCESS);
5175N/A }
5175N/A
5175N/Aerr:
5175N/A#if USE_EVP
5175N/A if (pkey != NULL)
5175N/A EVP_PKEY_free(pkey);
5175N/A#endif
5175N/A if (e != NULL)
5175N/A BN_free(e);
5175N/A if (rsa != NULL)
5175N/A RSA_free(rsa);
5175N/A return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
5175N/A#else
5175N/A RSA *rsa;
5175N/A unsigned long e;
5175N/A#if USE_EVP
5175N/A EVP_PKEY *pkey = EVP_PKEY_new();
1065N/A
5175N/A if (pkey == NULL)
5175N/A return (ISC_R_NOMEMORY);
1065N/A#endif
5175N/A
5175N/A if (exp == 0)
5175N/A e = RSA_F4;
5175N/A else
1065N/A e = 0x40000003;
823N/A rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
if (rsa == NULL) {
#if USE_EVP
EVP_PKEY_free(pkey);
#endif
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
}
SET_FLAGS(rsa);
#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
key->keydata.rsa = rsa;
#endif
return (ISC_R_SUCCESS);
#endif
}
static isc_boolean_t
opensslrsa_isprivate(const dst_key_t *key) {
#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);
return (ISC_TF(rsa != NULL && rsa->d != NULL));
}
static void
opensslrsa_destroy(dst_key_t *key) {
#if USE_EVP
EVP_PKEY *pkey = key->keydata.pkey;
EVP_PKEY_free(pkey);
key->keydata.pkey = NULL;
#else
RSA *rsa = key->keydata.rsa;
RSA_free(rsa);
key->keydata.rsa = NULL;
#endif
}
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;
isc_result_t ret;
RSA *rsa;
#if USE_EVP
EVP_PKEY *pkey;
#endif
#if USE_EVP
REQUIRE(key->keydata.pkey != NULL);
#else
REQUIRE(key->keydata.rsa != NULL);
#endif
#if USE_EVP
pkey = key->keydata.pkey;
rsa = EVP_PKEY_get1_RSA(pkey);
if (rsa == NULL)
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
#else
rsa = key->keydata.rsa;
#endif
isc_buffer_availableregion(data, &r);
e_bytes = BN_num_bytes(rsa->e);
mod_bytes = BN_num_bytes(rsa->n);
if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
if (r.length < 1)
DST_RET(ISC_R_NOSPACE);
isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
} else {
if (r.length < 3)
DST_RET(ISC_R_NOSPACE);
isc_buffer_putuint8(data, 0);
isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
}
if (r.length < e_bytes + mod_bytes)
return (ISC_R_NOSPACE);
isc_buffer_availableregion(data, &r);
BN_bn2bin(rsa->e, r.base);
r.base += e_bytes;
BN_bn2bin(rsa->n, r.base);
isc_buffer_add(data, e_bytes + mod_bytes);
ret = ISC_R_SUCCESS;
err:
#if USE_EVP
if (rsa != NULL)
RSA_free(rsa);
#endif
return (ret);
}
static isc_result_t
opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
RSA *rsa;
isc_region_t r;
unsigned int e_bytes;
#if USE_EVP
EVP_PKEY *pkey;
#endif
isc_buffer_remainingregion(data, &r);
if (r.length == 0)
return (ISC_R_SUCCESS);
rsa = RSA_new();
if (rsa == NULL)
return (dst__openssl_toresult(ISC_R_NOMEMORY));
SET_FLAGS(rsa);
if (r.length < 1) {
RSA_free(rsa);
return (DST_R_INVALIDPUBLICKEY);
}
e_bytes = *r.base++;
r.length--;
if (e_bytes == 0) {
if (r.length < 2) {
RSA_free(rsa);
return (DST_R_INVALIDPUBLICKEY);
}
e_bytes = ((*r.base++) << 8);
e_bytes += *r.base++;
r.length -= 2;
}
if (r.length < e_bytes) {
RSA_free(rsa);
return (DST_R_INVALIDPUBLICKEY);
}
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);
#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
key->keydata.rsa = rsa;
#endif
return (ISC_R_SUCCESS);
}
static isc_result_t
opensslrsa_tofile(const dst_key_t *key, const char *directory) {
int i;
RSA *rsa;
dst_private_t priv;
unsigned char *bufs[8];
isc_result_t result;
#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
if (key->keydata.rsa == NULL)
return (DST_R_NULLKEY);
rsa = key->keydata.rsa;
#endif
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++;
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++;
}
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++;
}
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++;
}
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++;
}
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++;
}
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++;
}
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++;
}
priv.nelements = i;
result = dst__privstruct_writefile(key, &priv, directory);
fail:
#if USE_EVP
RSA_free(rsa);
#endif
for (i = 0; i < 8; i++) {
if (bufs[i] == NULL)
break;
isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
}
return (result);
}
static isc_result_t
opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer) {
dst_private_t priv;
isc_result_t ret;
int i;
RSA *rsa = NULL;
ENGINE *e = NULL;
isc_mem_t *mctx = key->mctx;
const char *name = NULL, *label = NULL;
EVP_PKEY *pkey = NULL;
/* read private key file */
ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
if (ret != ISC_R_SUCCESS)
return (ret);
for (i = 0; i < priv.nelements; i++) {
switch (priv.elements[i].tag) {
case TAG_RSA_ENGINE:
name = (char *)priv.elements[i].data;
break;
case TAG_RSA_LABEL:
label = (char *)priv.elements[i].data;
break;
default:
break;
}
}
/*
* Is this key is stored in a HSM?
* See if we can fetch it.
*/
if (name != NULL || label != NULL) {
INSIST(name != NULL);
INSIST(label != NULL);
e = dst__openssl_getengine(name);
if (e == NULL)
DST_RET(DST_R_NOENGINE);
pkey = ENGINE_load_private_key(e, label, NULL, NULL);
if (pkey == NULL) {
ERR_print_errors_fp(stderr);
DST_RET(ISC_R_FAILURE);
}
key->engine = isc_mem_strdup(key->mctx, name);
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);
key->key_size = EVP_PKEY_bits(pkey);
#if USE_EVP
key->keydata.pkey = pkey;
#else
key->keydata.rsa = EVP_PKEY_get1_RSA(pkey);
if (rsa == NULL)
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
EVP_PKEY_free(pkey);
#endif
dst__privstruct_free(&priv, mctx);
return (ISC_R_SUCCESS);
}
rsa = RSA_new();
if (rsa == NULL)
DST_RET(ISC_R_NOMEMORY);
SET_FLAGS(rsa);
#if USE_EVP
pkey = EVP_PKEY_new();
if (pkey == NULL)
DST_RET(ISC_R_NOMEMORY);
if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
DST_RET(ISC_R_FAILURE);
}
key->keydata.pkey = pkey;
#else
key->keydata.rsa = rsa;
#endif
for (i = 0; i < priv.nelements; i++) {
BIGNUM *bn;
switch (priv.elements[i].tag) {
case TAG_RSA_ENGINE:
continue;
case TAG_RSA_LABEL:
continue;
case TAG_RSA_PIN:
continue;
default:
bn = BN_bin2bn(priv.elements[i].data,
priv.elements[i].length, NULL);
if (bn == NULL)
DST_RET(ISC_R_NOMEMORY);
}
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;
}
}
dst__privstruct_free(&priv, mctx);
key->key_size = BN_num_bits(rsa->n);
#if USE_EVP
RSA_free(rsa);
#endif
return (ISC_R_SUCCESS);
err:
#if USE_EVP
if (pkey != NULL)
EVP_PKEY_free(pkey);
#endif
if (rsa != NULL)
RSA_free(rsa);
opensslrsa_destroy(key);
dst__privstruct_free(&priv, mctx);
memset(&priv, 0, sizeof(priv));
return (ret);
}
static isc_result_t
opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
const char *pin)
{
ENGINE *e = NULL;
isc_result_t ret;
EVP_PKEY *pkey = NULL;
UNUSED(pin);
e = dst__openssl_getengine(engine);
if (e == NULL)
DST_RET(DST_R_NOENGINE);
pkey = ENGINE_load_private_key(e, label, NULL, NULL);
if (pkey == NULL)
DST_RET(ISC_R_NOMEMORY);
key->engine = isc_mem_strdup(key->mctx, label);
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);
key->key_size = EVP_PKEY_bits(pkey);
#if USE_EVP
key->keydata.pkey = pkey;
#else
key->keydata.rsa = EVP_PKEY_get1_RSA(pkey);
EVP_PKEY_free(pkey);
if (key->keydata.rsa == NULL)
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
#endif
return (ISC_R_SUCCESS);
err:
if (pkey != NULL)
EVP_PKEY_free(pkey);
return (ret);
}
static dst_func_t opensslrsa_functions = {
opensslrsa_createctx,
opensslrsa_destroyctx,
opensslrsa_adddata,
opensslrsa_sign,
opensslrsa_verify,
NULL, /*%< computesecret */
opensslrsa_compare,
NULL, /*%< paramcompare */
opensslrsa_generate,
opensslrsa_isprivate,
opensslrsa_destroy,
opensslrsa_todns,
opensslrsa_fromdns,
opensslrsa_tofile,
opensslrsa_parse,
NULL, /*%< cleanup */
opensslrsa_fromlabel,
};
isc_result_t
dst__opensslrsa_init(dst_func_t **funcp) {
REQUIRE(funcp != NULL);
if (*funcp == NULL)
*funcp = &opensslrsa_functions;
return (ISC_R_SUCCESS);
}
#else /* OPENSSL */
#include <isc/util.h>
EMPTY_TRANSLATION_UNIT
#endif /* OPENSSL */
/*! \file */