2012-05-02 23:20:17 +10:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2012-05-02 23:20:17 +10:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2012-05-02 23:20:17 +10:00
|
|
|
*/
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
/*! \file */
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/ecdsa.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/objects.h>
|
2020-02-07 14:20:54 +01:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
|
|
|
#include <openssl/engine.h>
|
|
|
|
#endif
|
2020-03-09 16:17:26 +01:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
#include <isc/mem.h>
|
2017-09-19 16:16:45 +05:30
|
|
|
#include <isc/safe.h>
|
2012-05-02 23:20:17 +10:00
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/keyvalues.h>
|
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <dst/result.h>
|
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
#include "dst_internal.h"
|
|
|
|
#include "dst_openssl.h"
|
|
|
|
#include "dst_parse.h"
|
|
|
|
|
|
|
|
#ifndef NID_X9_62_prime256v1
|
|
|
|
#error "P-256 group is not known (NID_X9_62_prime256v1)"
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef NID_X9_62_prime256v1 */
|
2012-05-02 23:20:17 +10:00
|
|
|
#ifndef NID_secp384r1
|
|
|
|
#error "P-384 group is not known (NID_secp384r1)"
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef NID_secp384r1 */
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define DST_RET(a) \
|
|
|
|
{ \
|
|
|
|
ret = a; \
|
|
|
|
goto err; \
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if !HAVE_ECDSA_SIG_GET0
|
2016-10-31 10:04:37 +11:00
|
|
|
/* From OpenSSL 1.1 */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) {
|
2018-05-03 13:59:04 +02:00
|
|
|
if (pr != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*pr = sig->r;
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
|
|
|
if (ps != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*ps = sig->s;
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
|
2018-05-03 13:59:04 +02:00
|
|
|
if (r == NULL || s == NULL) {
|
2020-02-13 21:48:23 +01:00
|
|
|
return (0);
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
|
|
|
|
BN_clear_free(sig->r);
|
|
|
|
BN_clear_free(sig->s);
|
|
|
|
sig->r = r;
|
|
|
|
sig->s = s;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
return (1);
|
2016-10-31 10:04:37 +11:00
|
|
|
}
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif /* !HAVE_ECDSA_SIG_GET0 */
|
2016-10-31 10:04:37 +11:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_createctx(dst_key_t *key, dst_context_t *dctx) {
|
|
|
|
EVP_MD_CTX *evp_md_ctx;
|
2012-05-02 23:20:17 +10:00
|
|
|
const EVP_MD *type = NULL;
|
|
|
|
|
|
|
|
UNUSED(key);
|
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
|
|
|
|
evp_md_ctx = EVP_MD_CTX_create();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (evp_md_ctx == NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (dctx->key->key_alg == DST_ALG_ECDSA256) {
|
2012-05-02 23:20:17 +10:00
|
|
|
type = EVP_sha256();
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2012-05-02 23:20:17 +10:00
|
|
|
type = EVP_sha384();
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
2020-02-12 13:59:18 +01:00
|
|
|
return (dst__openssl_toresult3(
|
|
|
|
dctx->category, "EVP_DigestInit_ex", ISC_R_FAILURE));
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
dctx->ctxdata.evp_md_ctx = evp_md_ctx;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_destroyctx(dst_context_t *dctx) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
|
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
|
|
|
|
if (evp_md_ctx != NULL) {
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
|
|
|
dctx->ctxdata.evp_md_ctx = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
|
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
|
2020-02-12 13:59:18 +01:00
|
|
|
return (dst__openssl_toresult3(
|
|
|
|
dctx->category, "EVP_DigestUpdate", ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
BN_bn2bin_fixed(const BIGNUM *bn, unsigned char *buf, int size) {
|
2012-05-02 23:20:17 +10:00
|
|
|
int bytes = size - BN_num_bytes(bn);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
while (bytes-- > 0) {
|
2012-05-02 23:20:17 +10:00
|
|
|
*buf++ = 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
BN_bn2bin(bn, buf);
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
|
|
|
|
isc_result_t ret;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
isc_region_t region;
|
|
|
|
ECDSA_SIG *ecdsasig;
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
EC_KEY *eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
|
|
|
unsigned int dgstlen, siglen;
|
2012-05-02 23:20:17 +10:00
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *r, *s;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey == NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_FAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
2012-05-02 23:20:17 +10:00
|
|
|
siglen = DNS_SIG_ECDSA256SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2012-05-02 23:20:17 +10:00
|
|
|
siglen = DNS_SIG_ECDSA384SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
isc_buffer_availableregion(sig, ®ion);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (region.length < siglen) {
|
2012-05-02 23:20:17 +10:00
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2021-01-05 10:02:53 +01:00
|
|
|
if (!EVP_DigestFinal_ex(evp_md_ctx, digest, &dgstlen)) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult3(
|
2021-01-05 10:02:53 +01:00
|
|
|
dctx->category, "EVP_DigestFinal_ex", ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
ecdsasig = ECDSA_do_sign(digest, dgstlen, eckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ecdsasig == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category, "ECDSA_do_sign",
|
2012-07-23 15:08:21 +10:00
|
|
|
DST_R_SIGNFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
ECDSA_SIG_get0(ecdsasig, &r, &s);
|
|
|
|
BN_bn2bin_fixed(r, region.base, siglen / 2);
|
|
|
|
isc_region_consume(®ion, siglen / 2);
|
|
|
|
BN_bn2bin_fixed(s, region.base, siglen / 2);
|
|
|
|
isc_region_consume(®ion, siglen / 2);
|
2012-05-02 23:20:17 +10:00
|
|
|
ECDSA_SIG_free(ecdsasig);
|
|
|
|
isc_buffer_add(sig, siglen);
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2019-09-09 14:05:31 +02:00
|
|
|
EC_KEY_free(eckey);
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
|
|
|
|
isc_result_t ret;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
int status;
|
2012-05-02 23:20:17 +10:00
|
|
|
unsigned char *cp = sig->base;
|
2020-02-13 14:44:37 -08:00
|
|
|
ECDSA_SIG *ecdsasig = NULL;
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
EC_KEY *eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
|
|
|
unsigned int dgstlen, siglen;
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
|
|
|
BIGNUM *r = NULL, *s = NULL;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey == NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_FAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
2012-05-02 23:20:17 +10:00
|
|
|
siglen = DNS_SIG_ECDSA256SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2012-05-02 23:20:17 +10:00
|
|
|
siglen = DNS_SIG_ECDSA384SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sig->length != siglen) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (DST_R_VERIFYFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!EVP_DigestFinal_ex(evp_md_ctx, digest, &dgstlen)) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult3(
|
|
|
|
dctx->category, "EVP_DigestFinal_ex", ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
ecdsasig = ECDSA_SIG_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ecdsasig == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
r = BN_bin2bn(cp, siglen / 2, NULL);
|
2012-05-02 23:20:17 +10:00
|
|
|
cp += siglen / 2;
|
2016-10-31 10:04:37 +11:00
|
|
|
s = BN_bin2bn(cp, siglen / 2, NULL);
|
|
|
|
ECDSA_SIG_set0(ecdsasig, r, s);
|
2012-05-30 11:52:02 +10:00
|
|
|
/* cp += siglen / 2; */
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
status = ECDSA_do_verify(digest, dgstlen, ecdsasig, eckey);
|
2012-07-23 15:08:21 +10:00
|
|
|
switch (status) {
|
|
|
|
case 1:
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
ret = dst__openssl_toresult(DST_R_VERIFYFAILURE);
|
|
|
|
break;
|
|
|
|
default:
|
2020-02-12 13:59:18 +01:00
|
|
|
ret = dst__openssl_toresult3(dctx->category, "ECDSA_do_verify",
|
2012-07-23 15:08:21 +10:00
|
|
|
DST_R_VERIFYFAILURE);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ecdsasig != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
ECDSA_SIG_free(ecdsasig);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2019-09-09 14:05:31 +02:00
|
|
|
EC_KEY_free(eckey);
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
bool ret;
|
|
|
|
int status;
|
|
|
|
EVP_PKEY *pkey1 = key1->keydata.pkey;
|
|
|
|
EVP_PKEY *pkey2 = key2->keydata.pkey;
|
|
|
|
EC_KEY *eckey1 = NULL;
|
|
|
|
EC_KEY *eckey2 = NULL;
|
2012-05-02 23:20:17 +10:00
|
|
|
const BIGNUM *priv1, *priv2;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey1 == NULL && pkey2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (pkey1 == NULL || pkey2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
eckey1 = EVP_PKEY_get1_EC_KEY(pkey1);
|
|
|
|
eckey2 = EVP_PKEY_get1_EC_KEY(pkey2);
|
|
|
|
if (eckey1 == NULL && eckey2 == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(true);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (eckey1 == NULL || eckey2 == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
status = EVP_PKEY_cmp(pkey1, pkey2);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (status != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
priv1 = EC_KEY_get0_private_key(eckey1);
|
|
|
|
priv2 = EC_KEY_get0_private_key(eckey2);
|
|
|
|
if (priv1 != NULL || priv2 != NULL) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (priv1 == NULL || priv2 == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (BN_cmp(priv1, priv2) != 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
ret = true;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey1 != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY_free(eckey1);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (eckey2 != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY_free(eckey2);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_generate(dst_key_t *key, int unused, void (*callback)(int)) {
|
2012-05-02 23:20:17 +10:00
|
|
|
isc_result_t ret;
|
2020-02-13 14:44:37 -08:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
int group_nid;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
UNUSED(unused);
|
|
|
|
UNUSED(callback);
|
|
|
|
|
2014-09-29 10:18:54 +10:00
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
2012-05-02 23:20:17 +10:00
|
|
|
group_nid = NID_X9_62_prime256v1;
|
2014-09-29 10:18:54 +10:00
|
|
|
key->key_size = DNS_KEY_ECDSA256SIZE * 4;
|
|
|
|
} else {
|
2012-05-02 23:20:17 +10:00
|
|
|
group_nid = NID_secp384r1;
|
2014-09-29 10:18:54 +10:00
|
|
|
key->key_size = DNS_KEY_ECDSA384SIZE * 4;
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
eckey = EC_KEY_new_by_curve_name(group_nid);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey == NULL) {
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult2("EC_KEY_new_by_curve_name",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (EC_KEY_generate_key(eckey) != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult2("EC_KEY_generate_key",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
pkey = EVP_PKEY_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
|
|
|
|
EVP_PKEY_free(pkey);
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(ISC_R_FAILURE);
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
key->keydata.pkey = pkey;
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2019-09-09 14:05:31 +02:00
|
|
|
EC_KEY_free(eckey);
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_isprivate(const dst_key_t *key) {
|
|
|
|
bool ret;
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
2020-02-13 14:44:37 -08:00
|
|
|
EC_KEY *eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
2012-05-02 23:45:44 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
ret = (eckey != NULL && EC_KEY_get0_private_key(eckey) != NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY_free(eckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_destroy(dst_key_t *key) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
key->keydata.pkey = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_todns(const dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
isc_region_t r;
|
|
|
|
int len;
|
2012-05-02 23:20:17 +10:00
|
|
|
unsigned char *cp;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
REQUIRE(key->keydata.pkey != NULL);
|
|
|
|
|
|
|
|
pkey = key->keydata.pkey;
|
|
|
|
eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey == NULL) {
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult(ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
len = i2o_ECPublicKey(eckey, NULL);
|
|
|
|
/* skip form */
|
|
|
|
len--;
|
|
|
|
|
|
|
|
isc_buffer_availableregion(data, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < (unsigned int)len) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
cp = buf;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!i2o_ECPublicKey(eckey, &cp)) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(r.base, buf + 1, len);
|
2012-05-02 23:20:17 +10:00
|
|
|
isc_buffer_add(data, len);
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2019-09-09 14:05:31 +02:00
|
|
|
EC_KEY_free(eckey);
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
isc_region_t r;
|
|
|
|
int group_nid;
|
|
|
|
unsigned int len;
|
2012-05-02 23:20:17 +10:00
|
|
|
const unsigned char *cp;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
|
|
|
|
key->key_alg == DST_ALG_ECDSA384);
|
|
|
|
|
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
|
|
|
len = DNS_KEY_ECDSA256SIZE;
|
|
|
|
group_nid = NID_X9_62_prime256v1;
|
|
|
|
} else {
|
|
|
|
len = DNS_KEY_ECDSA384SIZE;
|
|
|
|
group_nid = NID_secp384r1;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_remainingregion(data, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length == 0) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (r.length < len) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
eckey = EC_KEY_new_by_curve_name(group_nid);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey == NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:45:44 +00:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
buf[0] = POINT_CONVERSION_UNCOMPRESSED;
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(buf + 1, r.base, len);
|
2012-05-02 23:20:17 +10:00
|
|
|
cp = buf;
|
2020-02-12 13:59:18 +01:00
|
|
|
if (o2i_ECPublicKey(&eckey, (const unsigned char **)&cp,
|
2020-02-13 21:48:23 +01:00
|
|
|
(long)len + 1) == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPUBLICKEY));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (EC_KEY_check_key(eckey) != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPUBLICKEY));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
pkey = EVP_PKEY_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
|
|
|
|
EVP_PKEY_free(pkey);
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(ISC_R_FAILURE));
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_forward(data, len);
|
|
|
|
key->keydata.pkey = pkey;
|
2014-09-29 10:18:54 +10:00
|
|
|
key->key_size = len * 4;
|
2012-05-02 23:20:17 +10:00
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY_free(eckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
opensslecdsa_tofile(const dst_key_t *key, const char *directory) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
const BIGNUM *privkey;
|
|
|
|
dst_private_t priv;
|
2012-05-02 23:20:17 +10:00
|
|
|
unsigned char *buf = NULL;
|
2020-02-07 14:20:54 +01:00
|
|
|
unsigned short i;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->keydata.pkey == NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (DST_R_NULLKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2013-09-04 13:53:02 +10:00
|
|
|
if (key->external) {
|
|
|
|
priv.nelements = 0;
|
|
|
|
return (dst__privstruct_writefile(key, &priv, directory));
|
|
|
|
}
|
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
pkey = key->keydata.pkey;
|
|
|
|
eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (eckey == NULL) {
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
privkey = EC_KEY_get0_private_key(eckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (privkey == NULL) {
|
2020-02-07 14:20:54 +01:00
|
|
|
ret = dst__openssl_toresult(DST_R_OPENSSLFAILURE);
|
|
|
|
goto err;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
buf = isc_mem_get(key->mctx, BN_num_bytes(privkey));
|
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_ECDSA_PRIVATEKEY;
|
|
|
|
priv.elements[i].length = BN_num_bytes(privkey);
|
2012-05-02 23:20:17 +10:00
|
|
|
BN_bn2bin(privkey, buf);
|
2020-02-07 14:20:54 +01:00
|
|
|
priv.elements[i].data = buf;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (key->engine != NULL) {
|
|
|
|
priv.elements[i].tag = TAG_ECDSA_ENGINE;
|
|
|
|
priv.elements[i].length = (unsigned short)strlen(key->engine) +
|
|
|
|
1;
|
|
|
|
priv.elements[i].data = (unsigned char *)key->engine;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->label != NULL) {
|
2020-12-15 13:13:26 +01:00
|
|
|
priv.elements[i].tag = TAG_ECDSA_LABEL;
|
2020-02-07 14:20:54 +01:00
|
|
|
priv.elements[i].length = (unsigned short)strlen(key->label) +
|
|
|
|
1;
|
|
|
|
priv.elements[i].data = (unsigned char *)key->label;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv.nelements = i;
|
2012-05-02 23:20:17 +10:00
|
|
|
ret = dst__privstruct_writefile(key, &priv, directory);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2019-09-09 14:05:31 +02:00
|
|
|
EC_KEY_free(eckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (buf != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
isc_mem_put(key->mctx, buf, BN_num_bytes(privkey));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-07 14:20:54 +01:00
|
|
|
ecdsa_check(EC_KEY *eckey, EC_KEY *pubeckey) {
|
2012-05-02 23:20:17 +10:00
|
|
|
const EC_POINT *pubkey;
|
|
|
|
|
2020-12-15 14:09:05 +01:00
|
|
|
pubkey = EC_KEY_get0_public_key(eckey);
|
|
|
|
if (pubkey != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-12-15 14:09:05 +01:00
|
|
|
} else if (pubeckey != NULL) {
|
|
|
|
pubkey = EC_KEY_get0_public_key(pubeckey);
|
|
|
|
if (pubkey == NULL) {
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
if (EC_KEY_set_public_key(eckey, pubkey) != 1) {
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
if (EC_KEY_check_key(eckey) == 1) {
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
uses_engine(const dst_private_t *priv, const char **engine,
|
|
|
|
const char **label) {
|
|
|
|
for (unsigned short i = 0; i < priv->nelements; i++) {
|
|
|
|
switch (priv->elements[i].tag) {
|
|
|
|
case TAG_ECDSA_ENGINE:
|
|
|
|
*engine = (char *)priv->elements[i].data;
|
|
|
|
break;
|
|
|
|
case TAG_ECDSA_LABEL:
|
|
|
|
*label = (char *)priv->elements[i].data;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
if (*label != NULL) {
|
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
return (false);
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-07 14:20:54 +01:00
|
|
|
load_privkey_from_privstruct(EC_KEY *eckey, dst_private_t *priv) {
|
|
|
|
BIGNUM *privkey = BN_bin2bn(priv->elements[0].data,
|
|
|
|
priv->elements[0].length, NULL);
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
if (privkey == NULL) {
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
if (!EC_KEY_set_private_key(eckey, privkey)) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
BN_clear_free(privkey);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
|
|
|
static isc_result_t
|
|
|
|
load_pubkey_from_engine(EC_KEY *eckey, const char *engine, const char *label) {
|
2020-11-30 12:28:11 +01:00
|
|
|
EC_KEY *key;
|
|
|
|
ENGINE *ep;
|
|
|
|
EVP_PKEY *pubkey;
|
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
if (engine == NULL || label == NULL) {
|
|
|
|
return (DST_R_NOENGINE);
|
2013-09-04 13:53:02 +10:00
|
|
|
}
|
2013-09-04 23:46:16 +00:00
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
ep = dst__openssl_getengine(engine);
|
2020-02-07 14:20:54 +01:00
|
|
|
if (ep == NULL) {
|
|
|
|
return (DST_R_NOENGINE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-30 17:48:10 -08:00
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
pubkey = ENGINE_load_public_key(ep, label, NULL, NULL);
|
2020-02-07 14:20:54 +01:00
|
|
|
if (pubkey == NULL) {
|
|
|
|
return (dst__openssl_toresult2("ENGINE_load_public_key",
|
|
|
|
ISC_R_NOTFOUND));
|
|
|
|
}
|
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
key = EVP_PKEY_get1_EC_KEY(pubkey);
|
2020-02-07 14:20:54 +01:00
|
|
|
EVP_PKEY_free(pubkey);
|
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
if (key == NULL) {
|
2014-01-30 17:48:10 -08:00
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-30 17:48:10 -08:00
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
EC_KEY_set_public_key(eckey, EC_KEY_get0_public_key(key));
|
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
load_privkey_from_engine(EC_KEY *eckey, const char *engine, const char *label) {
|
2020-11-30 12:28:11 +01:00
|
|
|
EC_KEY *key;
|
|
|
|
ENGINE *ep;
|
|
|
|
EVP_PKEY *privkey;
|
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
if (engine == NULL || label == NULL) {
|
|
|
|
return (DST_R_NOENGINE);
|
|
|
|
}
|
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
ep = dst__openssl_getengine(engine);
|
2020-02-07 14:20:54 +01:00
|
|
|
if (ep == NULL) {
|
|
|
|
return (DST_R_NOENGINE);
|
|
|
|
}
|
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
privkey = ENGINE_load_private_key(ep, label, NULL, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (privkey == NULL) {
|
2020-02-07 14:20:54 +01:00
|
|
|
return (dst__openssl_toresult2("ENGINE_load_private_key",
|
|
|
|
ISC_R_NOTFOUND));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
key = EVP_PKEY_get1_EC_KEY(privkey);
|
2020-02-07 14:20:54 +01:00
|
|
|
EVP_PKEY_free(privkey);
|
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
if (key == NULL) {
|
2020-02-07 14:20:54 +01:00
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2020-11-30 12:28:11 +01:00
|
|
|
EC_KEY_set_private_key(eckey, EC_KEY_get0_private_key(key));
|
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static isc_result_t
|
|
|
|
load_pubkey_from_engine(EC_KEY *eckey, const char *engine, const char *label) {
|
|
|
|
UNUSED(eckey);
|
|
|
|
UNUSED(engine);
|
|
|
|
UNUSED(label);
|
|
|
|
|
|
|
|
return (DST_R_NOENGINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
load_privkey_from_engine(EC_KEY *eckey, const char *engine, const char *label) {
|
|
|
|
UNUSED(eckey);
|
|
|
|
UNUSED(engine);
|
|
|
|
UNUSED(label);
|
|
|
|
|
|
|
|
return (DST_R_NOENGINE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
load_privkey(EC_KEY *eckey, dst_private_t *priv, const char **engine,
|
|
|
|
const char **label) {
|
|
|
|
if (uses_engine(priv, engine, label)) {
|
|
|
|
return (load_privkey_from_engine(eckey, *engine, *label));
|
|
|
|
} else {
|
|
|
|
return (load_privkey_from_privstruct(eckey, priv));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
2014-01-30 17:48:10 -08:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
static isc_result_t
|
|
|
|
eckey_to_pkey(EC_KEY *eckey, EVP_PKEY **pkey) {
|
|
|
|
REQUIRE(pkey != NULL && *pkey == NULL);
|
|
|
|
|
|
|
|
*pkey = EVP_PKEY_new();
|
|
|
|
if (*pkey == NULL) {
|
|
|
|
return (ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
if (!EVP_PKEY_set1_EC_KEY(*pkey, eckey)) {
|
|
|
|
EVP_PKEY_free(*pkey);
|
|
|
|
*pkey = NULL;
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
finalize_eckey(dst_key_t *key, EC_KEY *eckey, const char *engine,
|
|
|
|
const char *label) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
|
|
|
|
result = eckey_to_pkey(eckey, &pkey);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
key->keydata.pkey = pkey;
|
2020-02-07 14:20:54 +01:00
|
|
|
|
|
|
|
if (label != NULL) {
|
|
|
|
key->label = isc_mem_strdup(key->mctx, label);
|
|
|
|
key->engine = isc_mem_strdup(key->mctx, engine);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
2014-09-29 10:18:54 +10:00
|
|
|
key->key_size = DNS_KEY_ECDSA256SIZE * 4;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-09-29 10:18:54 +10:00
|
|
|
key->key_size = DNS_KEY_ECDSA384SIZE * 4;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst__key_to_eckey(dst_key_t *key, EC_KEY **eckey) {
|
|
|
|
REQUIRE(eckey != NULL && *eckey == NULL);
|
|
|
|
|
|
|
|
int group_nid;
|
|
|
|
switch (key->key_alg) {
|
|
|
|
case DST_ALG_ECDSA256:
|
|
|
|
group_nid = NID_X9_62_prime256v1;
|
|
|
|
break;
|
|
|
|
case DST_ALG_ECDSA384:
|
|
|
|
group_nid = NID_secp384r1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
|
|
|
ISC_UNREACHABLE();
|
|
|
|
}
|
|
|
|
*eckey = EC_KEY_new_by_curve_name(group_nid);
|
|
|
|
if (*eckey == NULL) {
|
|
|
|
return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
|
|
|
|
dst_private_t priv;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
EC_KEY *pubeckey = NULL;
|
|
|
|
const char *engine = NULL;
|
|
|
|
const char *label = NULL;
|
|
|
|
|
|
|
|
/* read private key file */
|
|
|
|
result = dst__privstruct_parse(key, DST_ALG_ECDSA256, lexer, key->mctx,
|
|
|
|
&priv);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->external) {
|
|
|
|
if (priv.nelements != 0 || pub == NULL) {
|
|
|
|
result = DST_R_INVALIDPRIVATEKEY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
key->keydata.pkey = pub->keydata.pkey;
|
|
|
|
pub->keydata.pkey = NULL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pub != NULL && pub->keydata.pkey != NULL) {
|
|
|
|
pubeckey = EVP_PKEY_get1_EC_KEY(pub->keydata.pkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = dst__key_to_eckey(key, &eckey);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = load_privkey(eckey, &priv, &engine, &label);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ecdsa_check(eckey, pubeckey) != ISC_R_SUCCESS) {
|
|
|
|
result = DST_R_INVALIDPRIVATEKEY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = finalize_eckey(key, eckey, engine, label);
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (pubeckey != NULL) {
|
|
|
|
EC_KEY_free(pubeckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (eckey != NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY_free(eckey);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
dst__privstruct_free(&priv, key->mctx);
|
2017-09-19 16:16:45 +05:30
|
|
|
isc_safe_memwipe(&priv, sizeof(priv));
|
2020-02-07 14:20:54 +01:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
|
|
|
|
const char *pin) {
|
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
EC_KEY *pubeckey = NULL;
|
|
|
|
|
|
|
|
UNUSED(pin);
|
|
|
|
|
|
|
|
result = dst__key_to_eckey(key, &eckey);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = dst__key_to_eckey(key, &pubeckey);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = load_pubkey_from_engine(pubeckey, engine, label);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = load_privkey_from_engine(eckey, engine, label);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ecdsa_check(eckey, pubeckey) != ISC_R_SUCCESS) {
|
|
|
|
result = DST_R_INVALIDPRIVATEKEY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = finalize_eckey(key, eckey, engine, label);
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (pubeckey != NULL) {
|
|
|
|
EC_KEY_free(pubeckey);
|
|
|
|
}
|
|
|
|
if (eckey != NULL) {
|
|
|
|
EC_KEY_free(eckey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#else
|
|
|
|
UNUSED(key);
|
|
|
|
UNUSED(engine);
|
|
|
|
UNUSED(label);
|
|
|
|
UNUSED(pin);
|
|
|
|
return (DST_R_NOENGINE);
|
|
|
|
#endif
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static dst_func_t opensslecdsa_functions = {
|
|
|
|
opensslecdsa_createctx,
|
2014-01-14 15:40:56 -08:00
|
|
|
NULL, /*%< createctx2 */
|
2012-05-02 23:20:17 +10:00
|
|
|
opensslecdsa_destroyctx,
|
|
|
|
opensslecdsa_adddata,
|
|
|
|
opensslecdsa_sign,
|
|
|
|
opensslecdsa_verify,
|
2012-06-14 15:44:20 +10:00
|
|
|
NULL, /*%< verify2 */
|
2012-05-02 23:20:17 +10:00
|
|
|
NULL, /*%< computesecret */
|
|
|
|
opensslecdsa_compare,
|
|
|
|
NULL, /*%< paramcompare */
|
|
|
|
opensslecdsa_generate,
|
|
|
|
opensslecdsa_isprivate,
|
|
|
|
opensslecdsa_destroy,
|
|
|
|
opensslecdsa_todns,
|
|
|
|
opensslecdsa_fromdns,
|
|
|
|
opensslecdsa_tofile,
|
|
|
|
opensslecdsa_parse,
|
2020-02-07 14:20:54 +01:00
|
|
|
NULL, /*%< cleanup */
|
|
|
|
opensslecdsa_fromlabel, /*%< fromlabel */
|
|
|
|
NULL, /*%< dump */
|
|
|
|
NULL, /*%< restore */
|
2012-05-02 23:20:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dst__opensslecdsa_init(dst_func_t **funcp) {
|
2012-05-02 23:20:17 +10:00
|
|
|
REQUIRE(funcp != NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*funcp == NULL) {
|
2012-05-02 23:20:17 +10:00
|
|
|
*funcp = &opensslecdsa_functions;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|