Blame lib/dns/opensslrsa_link.c

Packit 5ce601
/*
Packit 5ce601
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit 5ce601
 *
Packit 5ce601
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit 5ce601
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service 704ed8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
Packit 5ce601
 *
Packit 5ce601
 * See the COPYRIGHT file distributed with this work for additional
Packit 5ce601
 * information regarding copyright ownership.
Packit 5ce601
 */
Packit 5ce601
Packit 5ce601
#ifdef OPENSSL
Packit 5ce601
#include <config.h>
Packit 5ce601
Packit 5ce601
#ifndef USE_EVP
Packit 5ce601
#if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
Packit 5ce601
#define USE_EVP 0
Packit 5ce601
#else
Packit 5ce601
#define USE_EVP 1
Packit 5ce601
#endif
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#include <stdbool.h>
Packit 5ce601
Packit 5ce601
#include <isc/entropy.h>
Packit 5ce601
#include <isc/md5.h>
Packit 5ce601
#include <isc/mem.h>
Packit 5ce601
#include <isc/safe.h>
Packit 5ce601
#include <isc/sha1.h>
Packit 5ce601
#include <isc/sha2.h>
Packit 5ce601
#include <isc/string.h>
Packit 5ce601
#include <isc/util.h>
Packit 5ce601
Packit 5ce601
#include <pk11/site.h>
Packit 5ce601
Packit 5ce601
#include <dst/result.h>
Packit 5ce601
Packit 5ce601
#include "dst_internal.h"
Packit 5ce601
#include "dst_openssl.h"
Packit 5ce601
#include "dst_parse.h"
Packit 5ce601
Packit 5ce601
#include <openssl/err.h>
Packit 5ce601
#include <openssl/objects.h>
Packit 5ce601
#include <openssl/rsa.h>
Packit 5ce601
#if OPENSSL_VERSION_NUMBER > 0x00908000L
Packit 5ce601
#include <openssl/bn.h>
Packit 5ce601
#endif
Packit 5ce601
#if !defined(OPENSSL_NO_ENGINE)
Packit 5ce601
#include <openssl/engine.h>
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
/*
Packit 5ce601
 * Limit the size of public exponents.
Packit 5ce601
 */
Packit 5ce601
#ifndef RSA_MAX_PUBEXP_BITS
Packit 5ce601
#define RSA_MAX_PUBEXP_BITS    35
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
/*
Packit 5ce601
 * We don't use configure for windows so enforce the OpenSSL version
Packit 5ce601
 * here.  Unlike with configure we don't support overriding this test.
Packit 5ce601
 */
Packit 5ce601
#ifdef WIN32
Packit 5ce601
#if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
Packit 5ce601
       OPENSSL_VERSION_NUMBER < 0x00908000L) || \
Packit 5ce601
      (OPENSSL_VERSION_NUMBER >= 0x0090804fL && \
Packit 5ce601
       OPENSSL_VERSION_NUMBER < 0x10002000L) || \
Packit 5ce601
      OPENSSL_VERSION_NUMBER >= 0x1000205fL)
Packit 5ce601
#error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
Packit 5ce601
#endif
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * XXXMPA  Temporarily disable RSA_BLINDING as it requires
Packit 5ce601
	 * good quality random data that cannot currently be guaranteed.
Packit 5ce601
	 * XXXMPA  Find which versions of openssl use pseudo random data
Packit 5ce601
	 * and set RSA_FLAG_BLINDING for those.
Packit 5ce601
	 */
Packit 5ce601
Packit 5ce601
#if 0
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x0090601fL
Packit 5ce601
#define SET_FLAGS(rsa) \
Packit 5ce601
	do { \
Packit 5ce601
	(rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
Packit 5ce601
	(rsa)->flags |= RSA_FLAG_BLINDING; \
Packit 5ce601
	} while (0)
Packit 5ce601
#else
Packit 5ce601
#define SET_FLAGS(rsa) \
Packit 5ce601
	do { \
Packit 5ce601
		(rsa)->flags |= RSA_FLAG_BLINDING; \
Packit 5ce601
	} while (0)
Packit 5ce601
#endif
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x0090601fL
Packit 5ce601
#define SET_FLAGS(rsa) \
Packit 5ce601
	do { \
Packit 5ce601
	(rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
Packit 5ce601
	(rsa)->flags &= ~RSA_FLAG_BLINDING; \
Packit 5ce601
	} while (0)
Packit 5ce601
#elif OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Packit 5ce601
#if defined(RSA_FLAG_NO_BLINDING)
Packit 5ce601
#define SET_FLAGS(rsa) \
Packit 5ce601
	do { \
Packit 5ce601
		(rsa)->flags &= ~RSA_FLAG_BLINDING; \
Packit 5ce601
		(rsa)->flags |= RSA_FLAG_NO_BLINDING; \
Packit 5ce601
	} while (0)
Packit 5ce601
#else
Packit 5ce601
#define SET_FLAGS(rsa) \
Packit 5ce601
	do { \
Packit 5ce601
		(rsa)->flags &= ~RSA_FLAG_BLINDING; \
Packit 5ce601
	} while (0)
Packit 5ce601
#endif
Packit 5ce601
#else
Packit 5ce601
#define SET_FLAGS(rsa) \
Packit 5ce601
	do { \
Packit 5ce601
		RSA_clear_flags(rsa, RSA_FLAG_BLINDING); \
Packit 5ce601
		RSA_set_flags(rsa, RSA_FLAG_NO_BLINDING); \
Packit 5ce601
	} while (0)
Packit 5ce601
#endif
Packit 5ce601
#define DST_RET(a) {ret = a; goto err;}
Packit 5ce601
Packit 5ce601
#if !defined(HAVE_RSA_SET0_KEY)
Packit 5ce601
/* From OpenSSL 1.1.0 */
Packit 5ce601
static int
Packit 5ce601
RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * If the fields n and e in r are NULL, the corresponding input
Packit 5ce601
	 * parameters MUST be non-NULL for n and e.  d may be
Packit 5ce601
	 * left NULL (in case only the public key is used).
Packit 5ce601
	 */
Packit 5ce601
	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) {
Packit 5ce601
		return 0;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (n != NULL) {
Packit 5ce601
		BN_free(r->n);
Packit 5ce601
		r->n = n;
Packit 5ce601
	}
Packit 5ce601
	if (e != NULL) {
Packit 5ce601
		BN_free(r->e);
Packit 5ce601
		r->e = e;
Packit 5ce601
	}
Packit 5ce601
	if (d != NULL) {
Packit 5ce601
		BN_free(r->d);
Packit 5ce601
		r->d = d;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	return 1;
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static int
Packit 5ce601
RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) {
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * If the fields p and q in r are NULL, the corresponding input
Packit 5ce601
	 * parameters MUST be non-NULL.
Packit 5ce601
	 */
Packit 5ce601
	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL)) {
Packit 5ce601
		return 0;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (p != NULL) {
Packit 5ce601
		BN_free(r->p);
Packit 5ce601
		r->p = p;
Packit 5ce601
	}
Packit 5ce601
	if (q != NULL) {
Packit 5ce601
		BN_free(r->q);
Packit 5ce601
		r->q = q;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	return 1;
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static int
Packit 5ce601
RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) {
Packit 5ce601
	/*
Packit 5ce601
	 * If the fields dmp1, dmq1 and iqmp in r are NULL, the
Packit 5ce601
	 * corresponding input parameters MUST be non-NULL.
Packit 5ce601
	 */
Packit 5ce601
	if ((r->dmp1 == NULL && dmp1 == NULL) ||
Packit 5ce601
	    (r->dmq1 == NULL && dmq1 == NULL) ||
Packit 5ce601
	    (r->iqmp == NULL && iqmp == NULL))
Packit 5ce601
	{
Packit 5ce601
		return 0;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (dmp1 != NULL) {
Packit 5ce601
		BN_free(r->dmp1);
Packit 5ce601
		r->dmp1 = dmp1;
Packit 5ce601
	}
Packit 5ce601
	if (dmq1 != NULL) {
Packit 5ce601
		BN_free(r->dmq1);
Packit 5ce601
		r->dmq1 = dmq1;
Packit 5ce601
	}
Packit 5ce601
	if (iqmp != NULL) {
Packit 5ce601
		BN_free(r->iqmp);
Packit 5ce601
		r->iqmp = iqmp;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	return 1;
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
RSA_get0_key(const RSA *r,
Packit 5ce601
	     const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
Packit 5ce601
{
Packit 5ce601
	if (n != NULL) {
Packit 5ce601
		*n = r->n;
Packit 5ce601
	}
Packit 5ce601
	if (e != NULL) {
Packit 5ce601
		*e = r->e;
Packit 5ce601
	}
Packit 5ce601
	if (d != NULL) {
Packit 5ce601
		*d = r->d;
Packit 5ce601
	}
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) {
Packit 5ce601
	if (p != NULL) {
Packit 5ce601
		*p = r->p;
Packit 5ce601
	}
Packit 5ce601
	if (q != NULL) {
Packit 5ce601
		*q = r->q;
Packit 5ce601
	}
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
Packit 5ce601
		    const BIGNUM **iqmp)
Packit 5ce601
{
Packit 5ce601
	if (dmp1 != NULL) {
Packit 5ce601
		*dmp1 = r->dmp1;
Packit 5ce601
	}
Packit 5ce601
	if (dmq1 != NULL) {
Packit 5ce601
		*dmq1 = r->dmq1;
Packit 5ce601
	}
Packit 5ce601
	if (iqmp != NULL) {
Packit 5ce601
		*iqmp = r->iqmp;
Packit 5ce601
	}
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static int
Packit 5ce601
RSA_test_flags(const RSA *r, int flags) {
Packit 5ce601
	return (r->flags & flags);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_MD_CTX *evp_md_ctx;
Packit 5ce601
	const EVP_MD *type = NULL;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	UNUSED(key);
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#else
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * Reject incorrect RSA key lengths.
Packit 5ce601
	 */
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		/* From RFC 3110 */
Packit 5ce601
		if (dctx->key->key_size > 4096)
Packit 5ce601
			return (ISC_R_FAILURE);
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		/* From RFC 5702 */
Packit 5ce601
		if ((dctx->key->key_size < 512) ||
Packit 5ce601
		    (dctx->key->key_size > 4096))
Packit 5ce601
			return (ISC_R_FAILURE);
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		/* From RFC 5702 */
Packit 5ce601
		if ((dctx->key->key_size < 1024) ||
Packit 5ce601
		    (dctx->key->key_size > 4096))
Packit 5ce601
			return (ISC_R_FAILURE);
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	evp_md_ctx = EVP_MD_CTX_create();
Packit 5ce601
	if (evp_md_ctx == NULL)
Packit 5ce601
		return (ISC_R_NOMEMORY);
Packit 5ce601
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
		type = EVP_md5();	/* MD5 + RSA */
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		type = EVP_sha1();	/* SHA1 + RSA */
Packit 5ce601
		break;
Packit 5ce601
#ifdef HAVE_EVP_SHA256
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		type = EVP_sha256();	/* SHA256 + RSA */
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
#ifdef HAVE_EVP_SHA512
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		type = EVP_sha512();
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
Packit 5ce601
		EVP_MD_CTX_destroy(evp_md_ctx);
Packit 5ce601
		return (dst__openssl_toresult3(dctx->category,
Packit 5ce601
					       "EVP_DigestInit_ex",
Packit 5ce601
					       ISC_R_FAILURE));
Packit 5ce601
	}
Packit 5ce601
	dctx->ctxdata.evp_md_ctx = evp_md_ctx;
Packit 5ce601
#else
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
		{
Packit 5ce601
			isc_md5_t *md5ctx;
Packit 5ce601
Packit 5ce601
			md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
Packit 5ce601
			if (md5ctx == NULL)
Packit 5ce601
				return (ISC_R_NOMEMORY);
Packit 5ce601
			isc_md5_init(md5ctx);
Packit 5ce601
			dctx->ctxdata.md5ctx = md5ctx;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		{
Packit 5ce601
			isc_sha1_t *sha1ctx;
Packit 5ce601
Packit 5ce601
			sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
Packit 5ce601
			if (sha1ctx == NULL)
Packit 5ce601
				return (ISC_R_NOMEMORY);
Packit 5ce601
			isc_sha1_init(sha1ctx);
Packit 5ce601
			dctx->ctxdata.sha1ctx = sha1ctx;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		{
Packit 5ce601
			isc_sha256_t *sha256ctx;
Packit 5ce601
Packit 5ce601
			sha256ctx = isc_mem_get(dctx->mctx,
Packit 5ce601
						sizeof(isc_sha256_t));
Packit 5ce601
			if (sha256ctx == NULL)
Packit 5ce601
				return (ISC_R_NOMEMORY);
Packit 5ce601
			isc_sha256_init(sha256ctx);
Packit 5ce601
			dctx->ctxdata.sha256ctx = sha256ctx;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		{
Packit 5ce601
			isc_sha512_t *sha512ctx;
Packit 5ce601
Packit 5ce601
			sha512ctx = isc_mem_get(dctx->mctx,
Packit 5ce601
						sizeof(isc_sha512_t));
Packit 5ce601
			if (sha512ctx == NULL)
Packit 5ce601
				return (ISC_R_NOMEMORY);
Packit 5ce601
			isc_sha512_init(sha512ctx);
Packit 5ce601
			dctx->ctxdata.sha512ctx = sha512ctx;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
opensslrsa_destroyctx(dst_context_t *dctx) {
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#else
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (evp_md_ctx != NULL) {
Packit 5ce601
		EVP_MD_CTX_destroy(evp_md_ctx);
Packit 5ce601
		dctx->ctxdata.evp_md_ctx = NULL;
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
		{
Packit 5ce601
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
Packit 5ce601
Packit 5ce601
			if (md5ctx != NULL) {
Packit 5ce601
				isc_md5_invalidate(md5ctx);
Packit 5ce601
				isc_mem_put(dctx->mctx, md5ctx,
Packit 5ce601
					    sizeof(isc_md5_t));
Packit 5ce601
				dctx->ctxdata.md5ctx = NULL;
Packit 5ce601
			}
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		{
Packit 5ce601
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
Packit 5ce601
Packit 5ce601
			if (sha1ctx != NULL) {
Packit 5ce601
				isc_sha1_invalidate(sha1ctx);
Packit 5ce601
				isc_mem_put(dctx->mctx, sha1ctx,
Packit 5ce601
					    sizeof(isc_sha1_t));
Packit 5ce601
				dctx->ctxdata.sha1ctx = NULL;
Packit 5ce601
			}
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		{
Packit 5ce601
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
Packit 5ce601
Packit 5ce601
			if (sha256ctx != NULL) {
Packit 5ce601
				isc_sha256_invalidate(sha256ctx);
Packit 5ce601
				isc_mem_put(dctx->mctx, sha256ctx,
Packit 5ce601
					    sizeof(isc_sha256_t));
Packit 5ce601
				dctx->ctxdata.sha256ctx = NULL;
Packit 5ce601
			}
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		{
Packit 5ce601
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
Packit 5ce601
Packit 5ce601
			if (sha512ctx != NULL) {
Packit 5ce601
				isc_sha512_invalidate(sha512ctx);
Packit 5ce601
				isc_mem_put(dctx->mctx, sha512ctx,
Packit 5ce601
					    sizeof(isc_sha512_t));
Packit 5ce601
				dctx->ctxdata.sha512ctx = NULL;
Packit 5ce601
			}
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
#endif
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#else
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
Packit 5ce601
		return (dst__openssl_toresult3(dctx->category,
Packit 5ce601
					       "EVP_DigestUpdate",
Packit 5ce601
					       ISC_R_FAILURE));
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
		{
Packit 5ce601
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
Packit 5ce601
Packit 5ce601
			isc_md5_update(md5ctx, data->base, data->length);
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		{
Packit 5ce601
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
Packit 5ce601
Packit 5ce601
			isc_sha1_update(sha1ctx, data->base, data->length);
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		{
Packit 5ce601
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
Packit 5ce601
Packit 5ce601
			isc_sha256_update(sha256ctx, data->base, data->length);
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		{
Packit 5ce601
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
Packit 5ce601
Packit 5ce601
			isc_sha512_update(sha512ctx, data->base, data->length);
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
#endif
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
#if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
/*
Packit 5ce601
 * Digest prefixes from RFC 5702.
Packit 5ce601
 */
Packit 5ce601
static unsigned char sha256_prefix[] =
Packit 5ce601
	 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
Packit 5ce601
	   0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
Packit 5ce601
static unsigned char sha512_prefix[] =
Packit 5ce601
	 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
Packit 5ce601
	   0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
Packit 5ce601
#define PREFIXLEN sizeof(sha512_prefix)
Packit 5ce601
#else
Packit 5ce601
#define PREFIXLEN 0
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
Packit 5ce601
	dst_key_t *key = dctx->key;
Packit 5ce601
	isc_region_t r;
Packit 5ce601
	unsigned int siglen = 0;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
Packit 5ce601
	EVP_PKEY *pkey = key->keydata.pkey;
Packit 5ce601
#else
Packit 5ce601
	RSA *rsa = key->keydata.rsa;
Packit 5ce601
	/* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
Packit 5ce601
	unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
Packit 5ce601
	int status;
Packit 5ce601
	int type = 0;
Packit 5ce601
	unsigned int digestlen = 0;
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
	unsigned int prefixlen = 0;
Packit 5ce601
	const unsigned char *prefix = NULL;
Packit 5ce601
#endif
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#else
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	isc_buffer_availableregion(sig, &r);
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (r.length < (unsigned int) EVP_PKEY_size(pkey))
Packit 5ce601
		return (ISC_R_NOSPACE);
Packit 5ce601
Packit 5ce601
	if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
Packit 5ce601
		return (dst__openssl_toresult3(dctx->category,
Packit 5ce601
					       "EVP_SignFinal",
Packit 5ce601
					       ISC_R_FAILURE));
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	if (r.length < (unsigned int) RSA_size(rsa))
Packit 5ce601
		return (ISC_R_NOSPACE);
Packit 5ce601
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
		{
Packit 5ce601
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
Packit 5ce601
Packit 5ce601
			isc_md5_final(md5ctx, digest);
Packit 5ce601
			type = NID_md5;
Packit 5ce601
			digestlen = ISC_MD5_DIGESTLENGTH;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		{
Packit 5ce601
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
Packit 5ce601
Packit 5ce601
			isc_sha1_final(sha1ctx, digest);
Packit 5ce601
			type = NID_sha1;
Packit 5ce601
			digestlen = ISC_SHA1_DIGESTLENGTH;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		{
Packit 5ce601
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
Packit 5ce601
Packit 5ce601
			isc_sha256_final(digest, sha256ctx);
Packit 5ce601
			digestlen = ISC_SHA256_DIGESTLENGTH;
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
			prefix = sha256_prefix;
Packit 5ce601
			prefixlen = sizeof(sha256_prefix);
Packit 5ce601
#else
Packit 5ce601
			type = NID_sha256;
Packit 5ce601
#endif
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		{
Packit 5ce601
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
Packit 5ce601
Packit 5ce601
			isc_sha512_final(digest, sha512ctx);
Packit 5ce601
			digestlen = ISC_SHA512_DIGESTLENGTH;
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
			prefix = sha512_prefix;
Packit 5ce601
			prefixlen = sizeof(sha512_prefix);
Packit 5ce601
#else
Packit 5ce601
			type = NID_sha512;
Packit 5ce601
#endif
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		INSIST(type != 0);
Packit 5ce601
		status = RSA_sign(type, digest, digestlen, r.base,
Packit 5ce601
				  &siglen, rsa);
Packit 5ce601
		break;
Packit 5ce601
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		INSIST(prefix != NULL);
Packit 5ce601
		INSIST(prefixlen != 0);
Packit 5ce601
		INSIST(prefixlen + digestlen <= sizeof(digest));
Packit 5ce601
Packit 5ce601
		memmove(digest + prefixlen, digest, digestlen);
Packit 5ce601
		memmove(digest, prefix, prefixlen);
Packit 5ce601
		status = RSA_private_encrypt(digestlen + prefixlen,
Packit 5ce601
					     digest, r.base, rsa,
Packit 5ce601
					     RSA_PKCS1_PADDING);
Packit 5ce601
		if (status < 0)
Packit 5ce601
			status = 0;
Packit 5ce601
		else
Packit 5ce601
			siglen = status;
Packit 5ce601
		break;
Packit 5ce601
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	INSIST(type != 0);
Packit 5ce601
	status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
Packit 5ce601
#endif
Packit 5ce601
	if (status == 0)
Packit 5ce601
		return (dst__openssl_toresult3(dctx->category,
Packit 5ce601
					       "RSA_sign",
Packit 5ce601
					       DST_R_OPENSSLFAILURE));
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	isc_buffer_add(sig, siglen);
Packit 5ce601
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_verify2(dst_context_t *dctx, int maxbits, const isc_region_t *sig) {
Packit 5ce601
	dst_key_t *key = dctx->key;
Packit 5ce601
	int status = 0;
Packit 5ce601
	const BIGNUM *e = NULL;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
Packit 5ce601
	EVP_PKEY *pkey = key->keydata.pkey;
Packit 5ce601
	RSA *rsa;
Packit 5ce601
	int bits;
Packit 5ce601
#else
Packit 5ce601
	/* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
Packit 5ce601
	unsigned char digest[ISC_SHA512_DIGESTLENGTH];
Packit 5ce601
	int type = 0;
Packit 5ce601
	unsigned int digestlen = 0;
Packit 5ce601
	RSA *rsa = key->keydata.rsa;
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
	unsigned int prefixlen = 0;
Packit 5ce601
	const unsigned char *prefix = NULL;
Packit 5ce601
#endif
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#else
Packit 5ce601
	REQUIRE(dctx->key->key_alg == DST_ALG_RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
Packit 5ce601
		dctx->key->key_alg == DST_ALG_RSASHA512);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	rsa = EVP_PKEY_get1_RSA(pkey);
Packit 5ce601
	if (rsa == NULL)
Packit 5ce601
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
	RSA_get0_key(rsa, NULL, &e, NULL);
Packit 5ce601
	bits = BN_num_bits(e);
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
	if (bits > maxbits && maxbits != 0)
Packit 5ce601
		return (DST_R_VERIFYFAILURE);
Packit 5ce601
Packit 5ce601
	status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
Packit 5ce601
	switch (status) {
Packit 5ce601
	case 1:
Packit 5ce601
		return (ISC_R_SUCCESS);
Packit 5ce601
	case 0:
Packit 5ce601
		return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
Packit 5ce601
	default:
Packit 5ce601
		return (dst__openssl_toresult3(dctx->category,
Packit 5ce601
					       "EVP_VerifyFinal",
Packit 5ce601
					       DST_R_VERIFYFAILURE));
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	RSA_get0_key(rsa, NULL, &e, NULL);
Packit 5ce601
	if (BN_num_bits(e) > maxbits && maxbits != 0)
Packit 5ce601
		return (DST_R_VERIFYFAILURE);
Packit 5ce601
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
		{
Packit 5ce601
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
Packit 5ce601
Packit 5ce601
			isc_md5_final(md5ctx, digest);
Packit 5ce601
			type = NID_md5;
Packit 5ce601
			digestlen = ISC_MD5_DIGESTLENGTH;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		{
Packit 5ce601
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
Packit 5ce601
Packit 5ce601
			isc_sha1_final(sha1ctx, digest);
Packit 5ce601
			type = NID_sha1;
Packit 5ce601
			digestlen = ISC_SHA1_DIGESTLENGTH;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		{
Packit 5ce601
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
Packit 5ce601
Packit 5ce601
			isc_sha256_final(digest, sha256ctx);
Packit 5ce601
			digestlen = ISC_SHA256_DIGESTLENGTH;
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
			prefix = sha256_prefix;
Packit 5ce601
			prefixlen = sizeof(sha256_prefix);
Packit 5ce601
#else
Packit 5ce601
			type = NID_sha256;
Packit 5ce601
#endif
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		{
Packit 5ce601
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
Packit 5ce601
Packit 5ce601
			isc_sha512_final(digest, sha512ctx);
Packit 5ce601
			digestlen = ISC_SHA512_DIGESTLENGTH;
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
			prefix = sha512_prefix;
Packit 5ce601
			prefixlen = sizeof(sha512_prefix);
Packit 5ce601
#else
Packit 5ce601
			type = NID_sha512;
Packit 5ce601
#endif
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (sig->length != (unsigned int) RSA_size(rsa))
Packit 5ce601
		return (DST_R_VERIFYFAILURE);
Packit 5ce601
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x00908000L
Packit 5ce601
	switch (dctx->key->key_alg) {
Packit 5ce601
#ifndef PK11_MD5_DISABLE
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
#endif
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		INSIST(type != 0);
Packit 5ce601
		status = RSA_verify(type, digest, digestlen, sig->base,
Packit 5ce601
				    RSA_size(rsa), rsa);
Packit 5ce601
		break;
Packit 5ce601
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		{
Packit 5ce601
			/*
Packit 5ce601
			 * 1024 is big enough for all valid RSA bit sizes
Packit 5ce601
			 * for use with DNSSEC.
Packit 5ce601
			 */
Packit 5ce601
			unsigned char original[PREFIXLEN + 1024];
Packit 5ce601
Packit 5ce601
			INSIST(prefix != NULL);
Packit 5ce601
			INSIST(prefixlen != 0U);
Packit 5ce601
Packit 5ce601
			if (RSA_size(rsa) > (int)sizeof(original))
Packit 5ce601
				return (DST_R_VERIFYFAILURE);
Packit 5ce601
Packit 5ce601
			status = RSA_public_decrypt(sig->length, sig->base,
Packit 5ce601
						    original, rsa,
Packit 5ce601
						    RSA_PKCS1_PADDING);
Packit 5ce601
			if (status <= 0)
Packit 5ce601
				return (dst__openssl_toresult3(
Packit 5ce601
						dctx->category,
Packit 5ce601
						"RSA_public_decrypt",
Packit 5ce601
						DST_R_VERIFYFAILURE));
Packit 5ce601
			if (status != (int)(prefixlen + digestlen))
Packit 5ce601
				return (DST_R_VERIFYFAILURE);
Packit 5ce601
			if (!isc_safe_memequal(original, prefix, prefixlen))
Packit 5ce601
				return (DST_R_VERIFYFAILURE);
Packit 5ce601
			if (!isc_safe_memequal(original + prefixlen,
Packit 5ce601
					    digest, digestlen))
Packit 5ce601
				return (DST_R_VERIFYFAILURE);
Packit 5ce601
			status = 1;
Packit 5ce601
		}
Packit 5ce601
		break;
Packit 5ce601
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	INSIST(type != 0);
Packit 5ce601
	status = RSA_verify(type, digest, digestlen, sig->base,
Packit 5ce601
			     RSA_size(rsa), rsa);
Packit 5ce601
#endif
Packit 5ce601
	if (status != 1)
Packit 5ce601
		return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
#endif
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
Packit 5ce601
	return (opensslrsa_verify2(dctx, 0, sig));
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static bool
Packit 5ce601
opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
Packit 5ce601
	int status;
Packit 5ce601
	RSA *rsa1 = NULL, *rsa2 = NULL;
Packit 5ce601
	const BIGNUM *n1 = NULL, *n2 = NULL;
Packit 5ce601
	const BIGNUM *e1 = NULL, *e2 = NULL;
Packit 5ce601
	const BIGNUM *d1 = NULL, *d2 = NULL;
Packit 5ce601
	const BIGNUM *p1 = NULL, *p2 = NULL;
Packit 5ce601
	const BIGNUM *q1 = NULL, *q2 = NULL;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_PKEY *pkey1, *pkey2;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	pkey1 = key1->keydata.pkey;
Packit 5ce601
	pkey2 = key2->keydata.pkey;
Packit 5ce601
	/*
Packit 5ce601
	 * The pkey reference will keep these around after
Packit 5ce601
	 * the RSA_free() call.
Packit 5ce601
	 */
Packit 5ce601
	if (pkey1 != NULL) {
Packit 5ce601
		rsa1 = EVP_PKEY_get1_RSA(pkey1);
Packit 5ce601
		RSA_free(rsa1);
Packit 5ce601
	}
Packit 5ce601
	if (pkey2 != NULL) {
Packit 5ce601
		rsa2 = EVP_PKEY_get1_RSA(pkey2);
Packit 5ce601
		RSA_free(rsa2);
Packit 5ce601
	}
Packit 5ce601
#else
Packit 5ce601
	rsa1 = key1->keydata.rsa;
Packit 5ce601
	rsa2 = key2->keydata.rsa;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	if (rsa1 == NULL && rsa2 == NULL)
Packit 5ce601
		return (true);
Packit 5ce601
	else if (rsa1 == NULL || rsa2 == NULL)
Packit 5ce601
		return (false);
Packit 5ce601
Packit 5ce601
	RSA_get0_key(rsa1, &n1, &e1, &d1;;
Packit 5ce601
	RSA_get0_key(rsa2, &n2, &e2, &d2;;
Packit 5ce601
	status = BN_cmp(n1, n2) || BN_cmp(e1, e2);
Packit 5ce601
Packit 5ce601
	if (status != 0)
Packit 5ce601
		return (false);
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (RSA_test_flags(rsa1, RSA_FLAG_EXT_PKEY) != 0 ||
Packit 5ce601
	    RSA_test_flags(rsa2, RSA_FLAG_EXT_PKEY) != 0) {
Packit 5ce601
		if (RSA_test_flags(rsa1, RSA_FLAG_EXT_PKEY) == 0 ||
Packit 5ce601
		    RSA_test_flags(rsa2, RSA_FLAG_EXT_PKEY) == 0)
Packit 5ce601
			return (false);
Packit 5ce601
		/*
Packit 5ce601
		 * Can't compare private parameters, BTW does it make sense?
Packit 5ce601
		 */
Packit 5ce601
		return (true);
Packit 5ce601
	}
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	if (d1 != NULL || d2 != NULL) {
Packit 5ce601
		if (d1 == NULL || d2 == NULL)
Packit 5ce601
			return (false);
Packit 5ce601
		RSA_get0_factors(rsa1, &p1, &q1;;
Packit 5ce601
		RSA_get0_factors(rsa2, &p2, &q2;;
Packit 5ce601
		status = BN_cmp(d1, d2) || BN_cmp(p1, p1) || BN_cmp(q1, q2);
Packit 5ce601
Packit 5ce601
		if (status != 0)
Packit 5ce601
			return (false);
Packit 5ce601
	}
Packit 5ce601
	return (true);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
#if OPENSSL_VERSION_NUMBER > 0x00908000L
Packit 5ce601
static int
Packit 5ce601
progress_cb(int p, int n, BN_GENCB *cb) {
Packit 5ce601
	union {
Packit 5ce601
		void *dptr;
Packit 5ce601
		void (*fptr)(int);
Packit 5ce601
	} u;
Packit 5ce601
Packit 5ce601
	UNUSED(n);
Packit 5ce601
Packit 5ce601
	u.dptr = BN_GENCB_get_arg(cb);
Packit 5ce601
	if (u.fptr != NULL)
Packit 5ce601
		u.fptr(p);
Packit 5ce601
	return (1);
Packit 5ce601
}
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
Packit 5ce601
#if OPENSSL_VERSION_NUMBER > 0x00908000L
Packit 5ce601
	isc_result_t ret = DST_R_OPENSSLFAILURE;
Packit 5ce601
	union {
Packit 5ce601
		void *dptr;
Packit 5ce601
		void (*fptr)(int);
Packit 5ce601
	} u;
Packit 5ce601
	RSA *rsa = RSA_new();
Packit 5ce601
	BIGNUM *e = BN_new();
Packit 5ce601
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Packit 5ce601
	BN_GENCB _cb;
Packit 5ce601
#endif
Packit 5ce601
	BN_GENCB *cb = BN_GENCB_new();
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_PKEY *pkey = EVP_PKEY_new();
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * Reject incorrect RSA key lengths.
Packit 5ce601
	 */
Packit 5ce601
	switch (key->key_alg) {
Packit 5ce601
	case DST_ALG_RSAMD5:
Packit 5ce601
	case DST_ALG_RSASHA1:
Packit 5ce601
	case DST_ALG_NSEC3RSASHA1:
Packit 5ce601
		/* From RFC 3110 */
Packit 5ce601
		if (key->key_size > 4096)
Packit 5ce601
			goto err;
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA256:
Packit 5ce601
		/* From RFC 5702 */
Packit 5ce601
		if ((key->key_size < 512) ||
Packit 5ce601
		    (key->key_size > 4096))
Packit 5ce601
			goto err;
Packit 5ce601
		break;
Packit 5ce601
	case DST_ALG_RSASHA512:
Packit 5ce601
		/* From RFC 5702 */
Packit 5ce601
		if ((key->key_size < 1024) ||
Packit 5ce601
		    (key->key_size > 4096))
Packit 5ce601
			goto err;
Packit 5ce601
		break;
Packit 5ce601
	default:
Packit 5ce601
		INSIST(0);
Packit 5ce601
		ISC_UNREACHABLE();
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (rsa == NULL || e == NULL || cb == NULL)
Packit 5ce601
		goto err;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (pkey == NULL)
Packit 5ce601
		goto err;
Packit 5ce601
	if (!EVP_PKEY_set1_RSA(pkey, rsa))
Packit 5ce601
		goto err;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	if (exp == 0) {
Packit 5ce601
		/* RSA_F4 0x10001 */
Packit 5ce601
		BN_set_bit(e, 0);
Packit 5ce601
		BN_set_bit(e, 16);
Packit 5ce601
	} else {
Packit 5ce601
		/* (phased-out) F5 0x100000001 */
Packit 5ce601
		BN_set_bit(e, 0);
Packit 5ce601
		BN_set_bit(e, 32);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (callback == NULL) {
Packit 5ce601
		BN_GENCB_set_old(cb, NULL, NULL);
Packit 5ce601
	} else {
Packit 5ce601
		u.fptr = callback;
Packit 5ce601
		BN_GENCB_set(cb, &progress_cb, u.dptr);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (RSA_generate_key_ex(rsa, key->key_size, e, cb)) {
Packit 5ce601
		BN_free(e);
Packit 5ce601
		BN_GENCB_free(cb);
Packit 5ce601
		cb = NULL;
Packit 5ce601
		SET_FLAGS(rsa);
Packit 5ce601
#if USE_EVP
Packit 5ce601
		key->keydata.pkey = pkey;
Packit 5ce601
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
#else
Packit 5ce601
		key->keydata.rsa = rsa;
Packit 5ce601
#endif
Packit 5ce601
		return (ISC_R_SUCCESS);
Packit 5ce601
	}
Packit 5ce601
	ret = dst__openssl_toresult2("RSA_generate_key_ex",
Packit 5ce601
				     DST_R_OPENSSLFAILURE);
Packit 5ce601
Packit 5ce601
 err:
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (pkey != NULL) {
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
		pkey = NULL;
Packit 5ce601
	}
Packit 5ce601
#endif
Packit 5ce601
	if (e != NULL) {
Packit 5ce601
		BN_free(e);
Packit 5ce601
		e = NULL;
Packit 5ce601
	}
Packit 5ce601
	if (rsa != NULL) {
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		rsa = NULL;
Packit 5ce601
	}
Packit 5ce601
	if (cb != NULL) {
Packit 5ce601
		BN_GENCB_free(cb);
Packit 5ce601
		cb = NULL;
Packit 5ce601
	}
Packit 5ce601
	return (dst__openssl_toresult(ret));
Packit 5ce601
#else
Packit 5ce601
	RSA *rsa;
Packit 5ce601
	unsigned long e;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_PKEY *pkey = EVP_PKEY_new();
Packit 5ce601
Packit 5ce601
	UNUSED(callback);
Packit 5ce601
Packit 5ce601
	if (pkey == NULL)
Packit 5ce601
		return (ISC_R_NOMEMORY);
Packit 5ce601
#else
Packit 5ce601
	UNUSED(callback);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	if (exp == 0)
Packit 5ce601
	       e = RSA_F4;
Packit 5ce601
	else
Packit 5ce601
	       e = 0x40000003;
Packit 5ce601
	rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
Packit 5ce601
	if (rsa == NULL) {
Packit 5ce601
#if USE_EVP
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
#endif
Packit 5ce601
		return (dst__openssl_toresult2("RSA_generate_key",
Packit 5ce601
					       DST_R_OPENSSLFAILURE));
Packit 5ce601
	}
Packit 5ce601
	SET_FLAGS(rsa);
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
	}
Packit 5ce601
	key->keydata.pkey = pkey;
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
#else
Packit 5ce601
	key->keydata.rsa = rsa;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
#endif
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static bool
Packit 5ce601
opensslrsa_isprivate(const dst_key_t *key) {
Packit 5ce601
	const BIGNUM *d = NULL;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
Packit 5ce601
	INSIST(rsa != NULL);
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
	/* key->keydata.pkey still has a reference so rsa is still valid. */
Packit 5ce601
#else
Packit 5ce601
	RSA *rsa = key->keydata.rsa;
Packit 5ce601
#endif
Packit 5ce601
	if (rsa != NULL && RSA_test_flags(rsa, RSA_FLAG_EXT_PKEY) != 0)
Packit 5ce601
		return (true);
Packit 5ce601
	RSA_get0_key(rsa, NULL, NULL, &d);
Packit 5ce601
	return (rsa != NULL && d != NULL);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
opensslrsa_destroy(dst_key_t *key) {
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_PKEY *pkey = key->keydata.pkey;
Packit 5ce601
	EVP_PKEY_free(pkey);
Packit 5ce601
	key->keydata.pkey = NULL;
Packit 5ce601
#else
Packit 5ce601
	RSA *rsa = key->keydata.rsa;
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
	key->keydata.rsa = NULL;
Packit 5ce601
#endif
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
Packit 5ce601
	isc_region_t r;
Packit 5ce601
	unsigned int e_bytes;
Packit 5ce601
	unsigned int mod_bytes;
Packit 5ce601
	isc_result_t ret;
Packit 5ce601
	RSA *rsa;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_PKEY *pkey;
Packit 5ce601
#endif
Packit 5ce601
	const BIGNUM *e = NULL, *n = NULL;
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	REQUIRE(key->keydata.pkey != NULL);
Packit 5ce601
#else
Packit 5ce601
	REQUIRE(key->keydata.rsa != NULL);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	pkey = key->keydata.pkey;
Packit 5ce601
	rsa = EVP_PKEY_get1_RSA(pkey);
Packit 5ce601
	if (rsa == NULL)
Packit 5ce601
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
#else
Packit 5ce601
	rsa = key->keydata.rsa;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	isc_buffer_availableregion(data, &r);
Packit 5ce601
Packit 5ce601
	RSA_get0_key(rsa, &n, &e, NULL);
Packit 5ce601
	mod_bytes = BN_num_bytes(n);
Packit 5ce601
	e_bytes = BN_num_bytes(e);
Packit 5ce601
Packit 5ce601
	if (e_bytes < 256) {	/*%< key exponent is <= 2040 bits */
Packit 5ce601
		if (r.length < 1)
Packit 5ce601
			DST_RET(ISC_R_NOSPACE);
Packit 5ce601
		isc_buffer_putuint8(data, (uint8_t) e_bytes);
Packit 5ce601
		isc_region_consume(&r, 1);
Packit 5ce601
	} else {
Packit 5ce601
		if (r.length < 3)
Packit 5ce601
			DST_RET(ISC_R_NOSPACE);
Packit 5ce601
		isc_buffer_putuint8(data, 0);
Packit 5ce601
		isc_buffer_putuint16(data, (uint16_t) e_bytes);
Packit 5ce601
		isc_region_consume(&r, 3);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (r.length < e_bytes + mod_bytes)
Packit 5ce601
		DST_RET(ISC_R_NOSPACE);
Packit 5ce601
Packit 5ce601
	RSA_get0_key(rsa, &n, &e, NULL);
Packit 5ce601
	BN_bn2bin(e, r.base);
Packit 5ce601
	isc_region_consume(&r, e_bytes);
Packit 5ce601
	BN_bn2bin(n, r.base);
Packit 5ce601
Packit 5ce601
	isc_buffer_add(data, e_bytes + mod_bytes);
Packit 5ce601
Packit 5ce601
	ret = ISC_R_SUCCESS;
Packit 5ce601
 err:
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (rsa != NULL)
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
#endif
Packit 5ce601
	return (ret);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
Packit 5ce601
	RSA *rsa;
Packit 5ce601
	isc_region_t r;
Packit 5ce601
	unsigned int e_bytes;
Packit 5ce601
	unsigned int length;
Packit 5ce601
#if USE_EVP
Packit 5ce601
	EVP_PKEY *pkey;
Packit 5ce601
#endif
Packit 5ce601
	BIGNUM *e = NULL, *n = NULL;
Packit 5ce601
Packit 5ce601
	isc_buffer_remainingregion(data, &r);
Packit 5ce601
	if (r.length == 0)
Packit 5ce601
		return (ISC_R_SUCCESS);
Packit 5ce601
	length = r.length;
Packit 5ce601
Packit 5ce601
	rsa = RSA_new();
Packit 5ce601
	if (rsa == NULL)
Packit 5ce601
		return (dst__openssl_toresult(ISC_R_NOMEMORY));
Packit 5ce601
	SET_FLAGS(rsa);
Packit 5ce601
Packit 5ce601
	if (r.length < 1) {
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		return (DST_R_INVALIDPUBLICKEY);
Packit 5ce601
	}
Packit 5ce601
	e_bytes = *r.base;
Packit 5ce601
	isc_region_consume(&r, 1);
Packit 5ce601
Packit 5ce601
	if (e_bytes == 0) {
Packit 5ce601
		if (r.length < 2) {
Packit 5ce601
			RSA_free(rsa);
Packit 5ce601
			return (DST_R_INVALIDPUBLICKEY);
Packit 5ce601
		}
Packit 5ce601
		e_bytes = (*r.base) << 8;
Packit 5ce601
		isc_region_consume(&r, 1);
Packit 5ce601
		e_bytes += *r.base;
Packit 5ce601
		isc_region_consume(&r, 1);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (r.length < e_bytes) {
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		return (DST_R_INVALIDPUBLICKEY);
Packit 5ce601
	}
Packit 5ce601
	e = BN_bin2bn(r.base, e_bytes, NULL);
Packit 5ce601
	isc_region_consume(&r, e_bytes);
Packit 5ce601
	n = BN_bin2bn(r.base, r.length, NULL);
Packit 5ce601
	if (RSA_set0_key(rsa, n, e, NULL) == 0) {
Packit 5ce601
		if (n != NULL) BN_free(n);
Packit 5ce601
		if (e != NULL) BN_free(e);
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		return (ISC_R_NOMEMORY);
Packit 5ce601
	}
Packit 5ce601
	key->key_size = BN_num_bits(n);
Packit 5ce601
Packit 5ce601
	isc_buffer_forward(data, length);
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	pkey = EVP_PKEY_new();
Packit 5ce601
	if (pkey == NULL) {
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		return (ISC_R_NOMEMORY);
Packit 5ce601
	}
Packit 5ce601
	if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
	}
Packit 5ce601
	key->keydata.pkey = pkey;
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
#else
Packit 5ce601
	key->keydata.rsa = rsa;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_tofile(const dst_key_t *key, const char *directory) {
Packit 5ce601
	int i;
Packit 5ce601
	RSA *rsa;
Packit 5ce601
	dst_private_t priv;
Packit 5ce601
	unsigned char *bufs[8];
Packit 5ce601
	isc_result_t result;
Packit 5ce601
	const BIGNUM *n = NULL, *e = NULL, *d = NULL;
Packit 5ce601
	const BIGNUM *p = NULL, *q = NULL;
Packit 5ce601
	const BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (key->keydata.pkey == NULL)
Packit 5ce601
		return (DST_R_NULLKEY);
Packit 5ce601
	rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
Packit 5ce601
	if (rsa == NULL)
Packit 5ce601
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
#else
Packit 5ce601
	if (key->keydata.rsa == NULL)
Packit 5ce601
		return (DST_R_NULLKEY);
Packit 5ce601
	rsa = key->keydata.rsa;
Packit 5ce601
#endif
Packit 5ce601
	memset(bufs, 0, sizeof(bufs));
Packit 5ce601
Packit 5ce601
	RSA_get0_key(rsa, &n, &e, &d);
Packit 5ce601
	RSA_get0_factors(rsa, &p, &q);
Packit 5ce601
	RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
Packit 5ce601
Packit 5ce601
	if (key->external) {
Packit 5ce601
		priv.nelements = 0;
Packit 5ce601
		result = dst__privstruct_writefile(key, &priv, directory);
Packit 5ce601
		goto fail;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	for (i = 0; i < 8; i++) {
Packit 5ce601
		bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(n));
Packit 5ce601
		if (bufs[i] == NULL) {
Packit 5ce601
			result = ISC_R_NOMEMORY;
Packit 5ce601
			goto fail;
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	i = 0;
Packit 5ce601
Packit 5ce601
	priv.elements[i].tag = TAG_RSA_MODULUS;
Packit 5ce601
	priv.elements[i].length = BN_num_bytes(n);
Packit 5ce601
	BN_bn2bin(n, bufs[i]);
Packit 5ce601
	priv.elements[i].data = bufs[i];
Packit 5ce601
	i++;
Packit 5ce601
Packit 5ce601
	priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
Packit 5ce601
	priv.elements[i].length = BN_num_bytes(e);
Packit 5ce601
	BN_bn2bin(e, bufs[i]);
Packit 5ce601
	priv.elements[i].data = bufs[i];
Packit 5ce601
	i++;
Packit 5ce601
Packit 5ce601
	if (d != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
Packit 5ce601
		priv.elements[i].length = BN_num_bytes(d);
Packit 5ce601
		BN_bn2bin(d, bufs[i]);
Packit 5ce601
		priv.elements[i].data = bufs[i];
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (p != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_PRIME1;
Packit 5ce601
		priv.elements[i].length = BN_num_bytes(p);
Packit 5ce601
		BN_bn2bin(p, bufs[i]);
Packit 5ce601
		priv.elements[i].data = bufs[i];
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (q != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_PRIME2;
Packit 5ce601
		priv.elements[i].length = BN_num_bytes(q);
Packit 5ce601
		BN_bn2bin(q, bufs[i]);
Packit 5ce601
		priv.elements[i].data = bufs[i];
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (dmp1 != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_EXPONENT1;
Packit 5ce601
		priv.elements[i].length = BN_num_bytes(dmp1);
Packit 5ce601
		BN_bn2bin(dmp1, bufs[i]);
Packit 5ce601
		priv.elements[i].data = bufs[i];
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (dmq1 != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_EXPONENT2;
Packit 5ce601
		priv.elements[i].length = BN_num_bytes(dmq1);
Packit 5ce601
		BN_bn2bin(dmq1, bufs[i]);
Packit 5ce601
		priv.elements[i].data = bufs[i];
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (iqmp != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_COEFFICIENT;
Packit 5ce601
		priv.elements[i].length = BN_num_bytes(iqmp);
Packit 5ce601
		BN_bn2bin(iqmp, bufs[i]);
Packit 5ce601
		priv.elements[i].data = bufs[i];
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (key->engine != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_ENGINE;
Packit 5ce601
		priv.elements[i].length =
Packit 5ce601
			(unsigned short)strlen(key->engine) + 1;
Packit 5ce601
		priv.elements[i].data = (unsigned char *)key->engine;
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (key->label != NULL) {
Packit 5ce601
		priv.elements[i].tag = TAG_RSA_LABEL;
Packit 5ce601
		priv.elements[i].length =
Packit 5ce601
			(unsigned short)strlen(key->label) + 1;
Packit 5ce601
		priv.elements[i].data = (unsigned char *)key->label;
Packit 5ce601
		i++;
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
Packit 5ce601
	priv.nelements = i;
Packit 5ce601
	result = dst__privstruct_writefile(key, &priv, directory);
Packit 5ce601
 fail:
Packit 5ce601
#if USE_EVP
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
#endif
Packit 5ce601
	for (i = 0; i < 8; i++) {
Packit 5ce601
		if (bufs[i] == NULL)
Packit 5ce601
			break;
Packit 5ce601
		isc_mem_put(key->mctx, bufs[i], BN_num_bytes(n));
Packit 5ce601
	}
Packit 5ce601
	return (result);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
rsa_check(RSA *rsa, RSA *pub) {
Packit 5ce601
	const BIGNUM *n1 = NULL, *n2 = NULL;
Packit 5ce601
	const BIGNUM *e1 = NULL, *e2 = NULL;
Packit 5ce601
	BIGNUM *n = NULL, *e = NULL;
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * Public parameters should be the same but if they are not set
Packit 5ce601
	 * copy them from the public key.
Packit 5ce601
	 */
Packit 5ce601
	RSA_get0_key(rsa, &n1, &e1, NULL);
Packit 5ce601
	if (pub != NULL) {
Packit 5ce601
		RSA_get0_key(pub, &n2, &e2, NULL);
Packit 5ce601
		if (n1 != NULL) {
Packit 5ce601
			if (BN_cmp(n1, n2) != 0)
Packit 5ce601
				return (DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
		} else {
Packit 5ce601
			n = BN_dup(n2);
Packit 5ce601
		}
Packit 5ce601
		if (e1 != NULL) {
Packit 5ce601
			if (BN_cmp(e1, e2) != 0)
Packit 5ce601
				return (DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
		} else {
Packit 5ce601
			e = BN_dup(e2);
Packit 5ce601
		}
Packit 5ce601
		if (RSA_set0_key(rsa, n, e, NULL) == 0) {
Packit 5ce601
			if (n != NULL)
Packit 5ce601
				BN_free(n);
Packit 5ce601
			if (e != NULL)
Packit 5ce601
				BN_free(e);
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	RSA_get0_key(rsa, &n1, &e1, NULL);
Packit 5ce601
	if (n1 == NULL || e1 == NULL)
Packit 5ce601
		return (DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
Packit 5ce601
	dst_private_t priv;
Packit 5ce601
	isc_result_t ret;
Packit 5ce601
	int i;
Packit 5ce601
	RSA *rsa = NULL, *pubrsa = NULL;
Packit 5ce601
#if !defined(OPENSSL_NO_ENGINE)
Packit 5ce601
	ENGINE *ep = NULL;
Packit 5ce601
	const BIGNUM *ex = NULL;
Packit 5ce601
#endif
Packit 5ce601
	isc_mem_t *mctx = key->mctx;
Packit 5ce601
	const char *engine = NULL, *label = NULL;
Packit 5ce601
#if !defined(OPENSSL_NO_ENGINE) || USE_EVP
Packit 5ce601
	EVP_PKEY *pkey = NULL;
Packit 5ce601
#endif
Packit 5ce601
	BIGNUM *n = NULL, *e = NULL, *d = NULL;
Packit 5ce601
	BIGNUM *p = NULL, *q = NULL;
Packit 5ce601
	BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
Packit 5ce601
Packit 5ce601
	/* read private key file */
Packit 5ce601
	ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv;;
Packit 5ce601
	if (ret != ISC_R_SUCCESS)
Packit 5ce601
		goto err;
Packit 5ce601
Packit 5ce601
	if (key->external) {
Packit 5ce601
		if (priv.nelements != 0)
Packit 5ce601
			DST_RET(DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
		if (pub == NULL)
Packit 5ce601
			DST_RET(DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
		key->keydata.pkey = pub->keydata.pkey;
Packit 5ce601
		pub->keydata.pkey = NULL;
Packit 5ce601
		key->key_size = pub->key_size;
Packit 5ce601
		dst__privstruct_free(&priv, mctx);
Packit 5ce601
		isc_safe_memwipe(&priv, sizeof(priv));
Packit 5ce601
		return (ISC_R_SUCCESS);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (pub != NULL && pub->keydata.pkey != NULL)
Packit 5ce601
		pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
Packit 5ce601
#else
Packit 5ce601
	if (pub != NULL && pub->keydata.rsa != NULL) {
Packit 5ce601
		pubrsa = pub->keydata.rsa;
Packit 5ce601
		pub->keydata.rsa = NULL;
Packit 5ce601
	}
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	for (i = 0; i < priv.nelements; i++) {
Packit 5ce601
		switch (priv.elements[i].tag) {
Packit 5ce601
		case TAG_RSA_ENGINE:
Packit 5ce601
			engine = (char *)priv.elements[i].data;
Packit 5ce601
			break;
Packit 5ce601
		case TAG_RSA_LABEL:
Packit 5ce601
			label = (char *)priv.elements[i].data;
Packit 5ce601
			break;
Packit 5ce601
		default:
Packit 5ce601
			break;
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * Is this key is stored in a HSM?
Packit 5ce601
	 * See if we can fetch it.
Packit 5ce601
	 */
Packit 5ce601
	if (label != NULL) {
Packit 5ce601
#if !defined(OPENSSL_NO_ENGINE)
Packit 5ce601
		if (engine == NULL)
Packit 5ce601
			DST_RET(DST_R_NOENGINE);
Packit 5ce601
		ep = dst__openssl_getengine(engine);
Packit 5ce601
		if (ep == NULL)
Packit 5ce601
			DST_RET(DST_R_NOENGINE);
Packit 5ce601
		pkey = ENGINE_load_private_key(ep, label, NULL, NULL);
Packit 5ce601
		if (pkey == NULL)
Packit 5ce601
			DST_RET(dst__openssl_toresult2(
Packit 5ce601
					"ENGINE_load_private_key",
Packit 5ce601
					ISC_R_NOTFOUND));
Packit 5ce601
		key->engine = isc_mem_strdup(key->mctx, engine);
Packit 5ce601
		if (key->engine == NULL)
Packit 5ce601
			DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
		key->label = isc_mem_strdup(key->mctx, label);
Packit 5ce601
		if (key->label == NULL)
Packit 5ce601
			DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
		rsa = EVP_PKEY_get1_RSA(pkey);
Packit 5ce601
		if (rsa == NULL)
Packit 5ce601
			DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
		if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
Packit 5ce601
			DST_RET(DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
		RSA_get0_key(rsa, NULL, &ex, NULL);
Packit 5ce601
		if (BN_num_bits(ex) > RSA_MAX_PUBEXP_BITS)
Packit 5ce601
			DST_RET(ISC_R_RANGE);
Packit 5ce601
		if (pubrsa != NULL)
Packit 5ce601
			RSA_free(pubrsa);
Packit 5ce601
		key->key_size = EVP_PKEY_bits(pkey);
Packit 5ce601
#if USE_EVP
Packit 5ce601
		key->keydata.pkey = pkey;
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
#else
Packit 5ce601
		key->keydata.rsa = rsa;
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
#endif
Packit 5ce601
		dst__privstruct_free(&priv, mctx);
Packit 5ce601
		isc_safe_memwipe(&priv, sizeof(priv));
Packit 5ce601
		return (ISC_R_SUCCESS);
Packit 5ce601
#else
Packit 5ce601
		DST_RET(DST_R_NOENGINE);
Packit 5ce601
#endif
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	rsa = RSA_new();
Packit 5ce601
	if (rsa == NULL)
Packit 5ce601
		DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
	SET_FLAGS(rsa);
Packit 5ce601
Packit 5ce601
#if USE_EVP
Packit 5ce601
	pkey = EVP_PKEY_new();
Packit 5ce601
	if (pkey == NULL)
Packit 5ce601
		DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
	if (!EVP_PKEY_set1_RSA(pkey, rsa))
Packit 5ce601
		DST_RET(ISC_R_FAILURE);
Packit 5ce601
	key->keydata.pkey = pkey;
Packit 5ce601
#else
Packit 5ce601
	key->keydata.rsa = rsa;
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	for (i = 0; i < priv.nelements; i++) {
Packit 5ce601
		BIGNUM *bn;
Packit 5ce601
		switch (priv.elements[i].tag) {
Packit 5ce601
		case TAG_RSA_ENGINE:
Packit 5ce601
			continue;
Packit 5ce601
		case TAG_RSA_LABEL:
Packit 5ce601
			continue;
Packit 5ce601
		default:
Packit 5ce601
			bn = BN_bin2bn(priv.elements[i].data,
Packit 5ce601
				       priv.elements[i].length, NULL);
Packit 5ce601
			if (bn == NULL)
Packit 5ce601
				DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
			switch (priv.elements[i].tag) {
Packit 5ce601
			case TAG_RSA_MODULUS:
Packit 5ce601
				n = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_PUBLICEXPONENT:
Packit 5ce601
				e = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_PRIVATEEXPONENT:
Packit 5ce601
				d = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_PRIME1:
Packit 5ce601
				p = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_PRIME2:
Packit 5ce601
				q = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_EXPONENT1:
Packit 5ce601
				dmp1 = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_EXPONENT2:
Packit 5ce601
				dmq1 = bn;
Packit 5ce601
				break;
Packit 5ce601
			case TAG_RSA_COEFFICIENT:
Packit 5ce601
				iqmp = bn;
Packit 5ce601
				break;
Packit 5ce601
			}
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	dst__privstruct_free(&priv, mctx);
Packit 5ce601
	isc_safe_memwipe(&priv, sizeof(priv));
Packit 5ce601
Packit 5ce601
	if (RSA_set0_key(rsa, n, e, d) == 0) {
Packit 5ce601
		if (n != NULL) BN_free(n);
Packit 5ce601
		if (e != NULL) BN_free(e);
Packit 5ce601
		if (d != NULL) BN_free(d);
Packit 5ce601
	}
Packit 5ce601
	if (RSA_set0_factors(rsa, p, q) == 0) {
Packit 5ce601
		if (p != NULL) BN_free(p);
Packit 5ce601
		if (q != NULL) BN_free(q);
Packit 5ce601
	}
Packit 5ce601
	if (RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp) == 0) {
Packit 5ce601
		if (dmp1 != NULL) BN_free(dmp1);
Packit 5ce601
		if (dmq1 != NULL) BN_free(dmq1);
Packit 5ce601
		if (iqmp != NULL) BN_free(iqmp);
Packit 5ce601
	}
Packit 5ce601
Packit 5ce601
	if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
Packit 5ce601
		DST_RET(DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
	if (BN_num_bits(e) > RSA_MAX_PUBEXP_BITS)
Packit 5ce601
		DST_RET(ISC_R_RANGE);
Packit 5ce601
	key->key_size = BN_num_bits(n);
Packit 5ce601
	if (pubrsa != NULL)
Packit 5ce601
		RSA_free(pubrsa);
Packit 5ce601
#if USE_EVP
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
#endif
Packit 5ce601
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
Packit 5ce601
 err:
Packit 5ce601
#if USE_EVP
Packit 5ce601
	if (pkey != NULL)
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
#endif
Packit 5ce601
	if (rsa != NULL)
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
	if (pubrsa != NULL)
Packit 5ce601
		RSA_free(pubrsa);
Packit 5ce601
	key->keydata.generic = NULL;
Packit 5ce601
	dst__privstruct_free(&priv, mctx);
Packit 5ce601
	isc_safe_memwipe(&priv, sizeof(priv));
Packit 5ce601
	return (ret);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
Packit 5ce601
		     const char *pin)
Packit 5ce601
{
Packit 5ce601
#if !defined(OPENSSL_NO_ENGINE)
Packit 5ce601
	ENGINE *e = NULL;
Packit 5ce601
	isc_result_t ret;
Packit 5ce601
	EVP_PKEY *pkey = NULL;
Packit 5ce601
	RSA *rsa = NULL, *pubrsa = NULL;
Packit 5ce601
	const BIGNUM *ex = NULL;
Packit 5ce601
Packit 5ce601
	UNUSED(pin);
Packit 5ce601
Packit 5ce601
	if (engine == NULL) {
Packit 5ce601
		DST_RET(DST_R_NOENGINE);
Packit 5ce601
	}
Packit 5ce601
	e = dst__openssl_getengine(engine);
Packit 5ce601
	if (e == NULL) {
Packit 5ce601
		DST_RET(DST_R_NOENGINE);
Packit 5ce601
	}
Packit 5ce601
	pkey = ENGINE_load_public_key(e, label, NULL, NULL);
Packit 5ce601
	if (pkey != NULL) {
Packit 5ce601
		pubrsa = EVP_PKEY_get1_RSA(pkey);
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
		if (pubrsa == NULL) {
Packit 5ce601
			DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	pkey = ENGINE_load_private_key(e, label, NULL, NULL);
Packit 5ce601
	if (pkey == NULL) {
Packit 5ce601
		DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
Packit 5ce601
					       ISC_R_NOTFOUND));
Packit 5ce601
	}
Packit 5ce601
	key->engine = isc_mem_strdup(key->mctx, engine);
Packit 5ce601
	if (key->engine == NULL) {
Packit 5ce601
		DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
	}
Packit 5ce601
	key->label = isc_mem_strdup(key->mctx, label);
Packit 5ce601
	if (key->label == NULL) {
Packit 5ce601
		DST_RET(ISC_R_NOMEMORY);
Packit 5ce601
	}
Packit 5ce601
	rsa = EVP_PKEY_get1_RSA(pkey);
Packit 5ce601
	if (rsa == NULL) {
Packit 5ce601
		DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
Packit 5ce601
	}
Packit 5ce601
	if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS) {
Packit 5ce601
		DST_RET(DST_R_INVALIDPRIVATEKEY);
Packit 5ce601
	}
Packit 5ce601
	RSA_get0_key(rsa, NULL, &ex, NULL);
Packit 5ce601
	if (BN_num_bits(ex) > RSA_MAX_PUBEXP_BITS) {
Packit 5ce601
		DST_RET(ISC_R_RANGE);
Packit 5ce601
	}
Packit 5ce601
	if (pubrsa != NULL) {
Packit 5ce601
		RSA_free(pubrsa);
Packit 5ce601
	}
Packit 5ce601
	key->key_size = EVP_PKEY_bits(pkey);
Packit 5ce601
#if USE_EVP
Packit 5ce601
	key->keydata.pkey = pkey;
Packit 5ce601
	RSA_free(rsa);
Packit 5ce601
#else
Packit 5ce601
	key->keydata.rsa = rsa;
Packit 5ce601
	EVP_PKEY_free(pkey);
Packit 5ce601
#endif
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
Packit 5ce601
 err:
Packit 5ce601
	if (rsa != NULL)
Packit 5ce601
		RSA_free(rsa);
Packit 5ce601
	if (pubrsa != NULL)
Packit 5ce601
		RSA_free(pubrsa);
Packit 5ce601
	if (pkey != NULL)
Packit 5ce601
		EVP_PKEY_free(pkey);
Packit 5ce601
	return (ret);
Packit 5ce601
#else
Packit 5ce601
	UNUSED(key);
Packit 5ce601
	UNUSED(engine);
Packit 5ce601
	UNUSED(label);
Packit 5ce601
	UNUSED(pin);
Packit 5ce601
	return(DST_R_NOENGINE);
Packit 5ce601
#endif
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static dst_func_t opensslrsa_functions = {
Packit 5ce601
	opensslrsa_createctx,
Packit 5ce601
	NULL, /*%< createctx2 */
Packit 5ce601
	opensslrsa_destroyctx,
Packit 5ce601
	opensslrsa_adddata,
Packit 5ce601
	opensslrsa_sign,
Packit 5ce601
	opensslrsa_verify,
Packit 5ce601
	opensslrsa_verify2,
Packit 5ce601
	NULL, /*%< computesecret */
Packit 5ce601
	opensslrsa_compare,
Packit 5ce601
	NULL, /*%< paramcompare */
Packit 5ce601
	opensslrsa_generate,
Packit 5ce601
	opensslrsa_isprivate,
Packit 5ce601
	opensslrsa_destroy,
Packit 5ce601
	opensslrsa_todns,
Packit 5ce601
	opensslrsa_fromdns,
Packit 5ce601
	opensslrsa_tofile,
Packit 5ce601
	opensslrsa_parse,
Packit 5ce601
	NULL, /*%< cleanup */
Packit 5ce601
	opensslrsa_fromlabel,
Packit 5ce601
	NULL, /*%< dump */
Packit 5ce601
	NULL, /*%< restore */
Packit 5ce601
};
Packit 5ce601
Packit 5ce601
isc_result_t
Packit 5ce601
dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
Packit 5ce601
	REQUIRE(funcp != NULL);
Packit 5ce601
Packit 5ce601
	if (*funcp == NULL) {
Packit 5ce601
		switch (algorithm) {
Packit 5ce601
		case DST_ALG_RSASHA256:
Packit 5ce601
#if defined(HAVE_EVP_SHA256) || !USE_EVP
Packit 5ce601
			*funcp = &opensslrsa_functions;
Packit 5ce601
#endif
Packit 5ce601
			break;
Packit 5ce601
		case DST_ALG_RSASHA512:
Packit 5ce601
#if defined(HAVE_EVP_SHA512) || !USE_EVP
Packit 5ce601
			*funcp = &opensslrsa_functions;
Packit 5ce601
#endif
Packit 5ce601
			break;
Packit 5ce601
		default:
Packit 5ce601
			*funcp = &opensslrsa_functions;
Packit 5ce601
			break;
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
#else /* OPENSSL */
Packit 5ce601
Packit 5ce601
#include <isc/util.h>
Packit 5ce601
Packit 5ce601
EMPTY_TRANSLATION_UNIT
Packit 5ce601
Packit 5ce601
#endif /* OPENSSL */
Packit 5ce601
/*! \file */