2012-05-02 23:20:17 +10:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2012-05-02 23:20:17 +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
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2012-05-02 23:20:17 +10:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
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>
|
2021-09-20 15:15:14 +00:00
|
|
|
#include <openssl/evp.h>
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <openssl/objects.h>
|
2022-12-28 17:13:41 +02:00
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
|
|
|
|
#include <openssl/core_names.h>
|
2021-09-20 15:15:14 +00:00
|
|
|
#include <openssl/param_build.h>
|
|
|
|
#endif
|
2020-03-09 16:17:26 +01:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
#include <isc/mem.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.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>
|
|
|
|
|
|
|
|
#include "dst_internal.h"
|
|
|
|
#include "dst_openssl.h"
|
|
|
|
#include "dst_parse.h"
|
2021-09-08 16:31:56 +10:00
|
|
|
#include "openssl_shim.h"
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
#ifndef NID_X9_62_prime256v1
|
|
|
|
#error "P-256 group is not known (NID_X9_62_prime256v1)"
|
|
|
|
#endif /* ifndef NID_X9_62_prime256v1 */
|
|
|
|
#ifndef NID_secp384r1
|
|
|
|
#error "P-384 group is not known (NID_secp384r1)"
|
|
|
|
#endif /* ifndef NID_secp384r1 */
|
|
|
|
|
|
|
|
#define DST_RET(a) \
|
|
|
|
{ \
|
|
|
|
ret = a; \
|
|
|
|
goto err; \
|
|
|
|
}
|
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
static bool
|
|
|
|
opensslecdsa_valid_key_alg(unsigned int key_alg) {
|
|
|
|
switch (key_alg) {
|
|
|
|
case DST_ALG_ECDSA256:
|
|
|
|
case DST_ALG_ECDSA384:
|
|
|
|
return (true);
|
|
|
|
default:
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
static int
|
|
|
|
opensslecdsa_key_alg_to_group_nid(unsigned int key_alg) {
|
|
|
|
switch (key_alg) {
|
|
|
|
case DST_ALG_ECDSA256:
|
|
|
|
return (NID_X9_62_prime256v1);
|
|
|
|
case DST_ALG_ECDSA384:
|
|
|
|
return (NID_secp384r1);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OpenSSL requires us to set the public key portion, but since our private key
|
|
|
|
* file format does not contain it directly, we generate it as needed.
|
|
|
|
*/
|
|
|
|
static EC_POINT *
|
|
|
|
opensslecdsa_generate_public_key(const EC_GROUP *group, const BIGNUM *privkey) {
|
|
|
|
EC_POINT *pubkey = EC_POINT_new(group);
|
|
|
|
if (pubkey == NULL) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (EC_POINT_mul(group, pubkey, privkey, NULL, NULL, NULL) != 1) {
|
|
|
|
EC_POINT_free(pubkey);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (pubkey);
|
|
|
|
}
|
|
|
|
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_API_LEVEL >= 30000
|
2022-12-28 17:13:41 +02:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
opensslecdsa_key_alg_to_group_name(unsigned int key_alg) {
|
|
|
|
switch (key_alg) {
|
|
|
|
case DST_ALG_ECDSA256:
|
|
|
|
return ("P-256");
|
|
|
|
case DST_ALG_ECDSA384:
|
|
|
|
return ("P-384");
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 22:44:23 +02:00
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_generate_pkey(unsigned int key_alg, EVP_PKEY **retkey) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY_CTX *ctx = NULL;
|
|
|
|
EVP_PKEY *params_pkey = NULL;
|
|
|
|
int group_nid = opensslecdsa_key_alg_to_group_nid(key_alg);
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Generate the key's parameters. */
|
|
|
|
ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_CTX_new_from_name",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_paramgen_init(ctx);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_paramgen_init",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, group_nid);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_CTX_set_ec_paramgen_"
|
|
|
|
"curve_nid",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_paramgen(ctx, ¶ms_pkey);
|
|
|
|
if (status != 1 || params_pkey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_paramgen",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
|
|
|
|
/* Generate the key. */
|
|
|
|
ctx = EVP_PKEY_CTX_new(params_pkey, NULL);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_CTX_new",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_keygen_init(ctx);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_keygen_init",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = EVP_PKEY_keygen(ctx, retkey);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_keygen",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
|
|
|
EVP_PKEY_free(params_pkey);
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:15:14 +00:00
|
|
|
static isc_result_t
|
2022-12-28 17:13:41 +02:00
|
|
|
opensslecdsa_create_pkey(unsigned int key_alg, bool private,
|
|
|
|
const unsigned char *key, size_t key_len,
|
|
|
|
EVP_PKEY **pkey) {
|
2021-09-20 15:15:14 +00:00
|
|
|
isc_result_t ret;
|
|
|
|
int status;
|
2022-12-28 17:13:41 +02:00
|
|
|
int group_nid = opensslecdsa_key_alg_to_group_nid(key_alg);
|
|
|
|
const char *groupname = opensslecdsa_key_alg_to_group_name(key_alg);
|
2021-09-20 15:15:14 +00:00
|
|
|
OSSL_PARAM_BLD *bld = NULL;
|
|
|
|
OSSL_PARAM *params = NULL;
|
|
|
|
EVP_PKEY_CTX *ctx = NULL;
|
2022-12-28 17:13:41 +02:00
|
|
|
EC_POINT *pubkey = NULL;
|
|
|
|
EC_GROUP *group = NULL;
|
2021-09-20 15:15:14 +00:00
|
|
|
BIGNUM *priv = NULL;
|
|
|
|
unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
|
|
|
|
|
|
|
|
bld = OSSL_PARAM_BLD_new();
|
|
|
|
if (bld == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("OSSL_PARAM_BLD_new",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = OSSL_PARAM_BLD_push_utf8_string(
|
|
|
|
bld, OSSL_PKEY_PARAM_GROUP_NAME, groupname, 0);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("OSSL_PARAM_BLD_push_"
|
|
|
|
"utf8_string",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (private) {
|
2022-12-28 17:13:41 +02:00
|
|
|
group = EC_GROUP_new_by_curve_name(group_nid);
|
|
|
|
if (group == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EC_GROUP_new_by_"
|
|
|
|
"curve_name",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
2021-11-19 10:32:21 +01:00
|
|
|
priv = BN_bin2bn(key, key_len, NULL);
|
2021-09-20 15:15:14 +00:00
|
|
|
if (priv == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("BN_bin2bn",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
|
|
|
|
priv);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("OSSL_PARAM_BLD_push_BN",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
|
|
|
|
pubkey = opensslecdsa_generate_public_key(group, priv);
|
|
|
|
if (pubkey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
key = buf;
|
|
|
|
key_len = EC_POINT_point2oct(group, pubkey,
|
|
|
|
POINT_CONVERSION_UNCOMPRESSED, buf,
|
|
|
|
sizeof(buf), NULL);
|
|
|
|
if (key_len == 0) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EC_POINT_point2oct",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
2021-09-20 15:15:14 +00:00
|
|
|
} else {
|
2022-12-28 17:13:41 +02:00
|
|
|
INSIST(key_len + 1 <= sizeof(buf));
|
2021-09-20 15:15:14 +00:00
|
|
|
buf[0] = POINT_CONVERSION_UNCOMPRESSED;
|
2021-11-19 10:32:21 +01:00
|
|
|
memmove(buf + 1, key, key_len);
|
2022-12-28 17:13:41 +02:00
|
|
|
key = buf;
|
|
|
|
key_len = key_len + 1;
|
|
|
|
}
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
status = OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
|
|
|
|
key, key_len);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("OSSL_PARAM_BLD_push_"
|
|
|
|
"octet_string",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params = OSSL_PARAM_BLD_to_param(bld);
|
|
|
|
if (params == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("OSSL_PARAM_BLD_to_param",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_CTX_new_from_name",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_fromdata_init(ctx);
|
|
|
|
if (status != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_fromdata_init",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_fromdata(
|
|
|
|
ctx, pkey, private ? EVP_PKEY_KEYPAIR : EVP_PKEY_PUBLIC_KEY,
|
|
|
|
params);
|
|
|
|
if (status != 1 || *pkey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_fromdata",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
2022-12-28 17:13:41 +02:00
|
|
|
OSSL_PARAM_free(params);
|
|
|
|
OSSL_PARAM_BLD_free(bld);
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
|
|
|
BN_clear_free(priv);
|
|
|
|
EC_POINT_free(pubkey);
|
|
|
|
EC_GROUP_free(group);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_validate_pkey_group(unsigned int key_alg, EVP_PKEY *pkey) {
|
|
|
|
const char *groupname = opensslecdsa_key_alg_to_group_name(key_alg);
|
|
|
|
char gname[64];
|
|
|
|
|
|
|
|
if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) != 1) {
|
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
if (strcmp(gname, groupname) != 0) {
|
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-12-28 22:44:23 +02:00
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_generate_pkey(unsigned int key_alg, EVP_PKEY **retkey) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
int group_nid = opensslecdsa_key_alg_to_group_nid(key_alg);
|
|
|
|
|
|
|
|
eckey = EC_KEY_new_by_curve_name(group_nid);
|
|
|
|
if (eckey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EC_KEY_new_by_curve_name",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EC_KEY_generate_key(eckey) != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EC_KEY_generate_key",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (pkey == NULL) {
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
if (EVP_PKEY_set1_EC_KEY(pkey, eckey) != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_set1_EC_KEY",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
*retkey = pkey;
|
|
|
|
pkey = NULL;
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
|
|
|
EC_KEY_free(eckey);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_create_pkey(unsigned int key_alg, bool private,
|
|
|
|
const unsigned char *key, size_t key_len,
|
|
|
|
EVP_PKEY **retkey) {
|
|
|
|
isc_result_t ret = ISC_R_SUCCESS;
|
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
BIGNUM *privkey = NULL;
|
|
|
|
EC_POINT *pubkey = NULL;
|
|
|
|
unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
|
|
|
|
int group_nid = opensslecdsa_key_alg_to_group_nid(key_alg);
|
|
|
|
|
|
|
|
eckey = EC_KEY_new_by_curve_name(group_nid);
|
|
|
|
if (eckey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
|
|
|
|
if (private) {
|
|
|
|
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
|
|
|
|
|
|
|
privkey = BN_bin2bn(key, key_len, NULL);
|
|
|
|
if (privkey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
if (!EC_KEY_set_private_key(eckey, privkey)) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPRIVATEKEY));
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey = opensslecdsa_generate_public_key(group, privkey);
|
|
|
|
if (pubkey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
if (EC_KEY_set_public_key(eckey, pubkey) != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const unsigned char *cp = buf;
|
|
|
|
INSIST(key_len + 1 <= sizeof(buf));
|
|
|
|
buf[0] = POINT_CONVERSION_UNCOMPRESSED;
|
|
|
|
memmove(buf + 1, key, key_len);
|
|
|
|
if (o2i_ECPublicKey(&eckey, &cp, key_len + 1) == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPUBLICKEY));
|
|
|
|
}
|
|
|
|
if (EC_KEY_check_key(eckey) != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPUBLICKEY));
|
|
|
|
}
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
pkey = EVP_PKEY_new();
|
|
|
|
if (pkey == NULL) {
|
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
|
|
|
|
DST_RET(dst__openssl_toresult(ISC_R_FAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
*retkey = pkey;
|
|
|
|
pkey = NULL;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_clear_free(privkey);
|
|
|
|
EC_POINT_free(pubkey);
|
|
|
|
EC_KEY_free(eckey);
|
|
|
|
EVP_PKEY_free(pkey);
|
2021-09-20 15:15:14 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_validate_pkey_group(unsigned int key_alg, EVP_PKEY *pkey) {
|
|
|
|
const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
|
|
|
|
int group_nid = opensslecdsa_key_alg_to_group_nid(key_alg);
|
|
|
|
|
|
|
|
if (EC_GROUP_get_curve_name(EC_KEY_get0_group(eckey)) != group_nid) {
|
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L && OPENSSL_API_LEVEL >= 30000 \
|
|
|
|
*/
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_createctx(dst_key_t *key, dst_context_t *dctx) {
|
2021-09-20 15:15:14 +00:00
|
|
|
isc_result_t ret = ISC_R_SUCCESS;
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_MD_CTX *evp_md_ctx;
|
|
|
|
const EVP_MD *type = NULL;
|
|
|
|
|
|
|
|
UNUSED(key);
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(dctx->key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
REQUIRE(dctx->use == DO_SIGN || dctx->use == DO_VERIFY);
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
evp_md_ctx = EVP_MD_CTX_create();
|
|
|
|
if (evp_md_ctx == NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
if (dctx->key->key_alg == DST_ALG_ECDSA256) {
|
|
|
|
type = EVP_sha256();
|
|
|
|
} else {
|
|
|
|
type = EVP_sha384();
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2021-09-20 15:15:14 +00:00
|
|
|
if (dctx->use == DO_SIGN) {
|
|
|
|
if (EVP_DigestSignInit(evp_md_ctx, NULL, type, NULL,
|
2022-12-28 17:13:41 +02:00
|
|
|
dctx->key->keydata.pkeypair.priv) != 1)
|
2021-09-20 15:15:14 +00:00
|
|
|
{
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestSignInit",
|
|
|
|
ISC_R_FAILURE));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (EVP_DigestVerifyInit(evp_md_ctx, NULL, type, NULL,
|
2022-12-28 17:13:41 +02:00
|
|
|
dctx->key->keydata.pkeypair.pub) != 1)
|
2021-09-20 15:15:14 +00:00
|
|
|
{
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestVerifyInit",
|
|
|
|
ISC_R_FAILURE));
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
dctx->ctxdata.evp_md_ctx = evp_md_ctx;
|
|
|
|
|
2021-09-20 15:15:14 +00:00
|
|
|
err:
|
|
|
|
return (ret);
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opensslecdsa_destroyctx(dst_context_t *dctx) {
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(dctx->key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
REQUIRE(dctx->use == DO_SIGN || dctx->use == DO_VERIFY);
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
if (evp_md_ctx != NULL) {
|
|
|
|
EVP_MD_CTX_destroy(evp_md_ctx);
|
|
|
|
dctx->ctxdata.evp_md_ctx = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
|
2021-09-20 15:15:14 +00:00
|
|
|
isc_result_t ret = ISC_R_SUCCESS;
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(dctx->key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
REQUIRE(dctx->use == DO_SIGN || dctx->use == DO_VERIFY);
|
|
|
|
|
|
|
|
if (dctx->use == DO_SIGN) {
|
|
|
|
if (EVP_DigestSignUpdate(evp_md_ctx, data->base,
|
2022-11-02 19:33:14 +01:00
|
|
|
data->length) != 1)
|
|
|
|
{
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestSignUpdate",
|
|
|
|
ISC_R_FAILURE));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (EVP_DigestVerifyUpdate(evp_md_ctx, data->base,
|
2022-11-02 19:33:14 +01:00
|
|
|
data->length) != 1)
|
|
|
|
{
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestVerifyUpdate",
|
|
|
|
ISC_R_FAILURE));
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2021-09-20 15:15:14 +00:00
|
|
|
err:
|
|
|
|
return (ret);
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-10-31 10:04:37 +11: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);
|
|
|
|
|
|
|
|
while (bytes-- > 0) {
|
|
|
|
*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
|
|
|
|
opensslecdsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
|
|
|
|
isc_result_t ret;
|
|
|
|
dst_key_t *key = dctx->key;
|
2016-10-31 10:04:37 +11:00
|
|
|
isc_region_t region;
|
2012-05-02 23:20:17 +10:00
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
2021-09-20 15:15:14 +00:00
|
|
|
ECDSA_SIG *ecdsasig = NULL;
|
|
|
|
size_t siglen, sigder_len = 0, sigder_alloced = 0;
|
|
|
|
unsigned char *sigder = NULL;
|
|
|
|
const unsigned char *sigder_copy;
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *r, *s;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
REQUIRE(dctx->use == DO_SIGN);
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
|
|
|
siglen = DNS_SIG_ECDSA256SIZE;
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
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-09-20 15:15:14 +00:00
|
|
|
if (EVP_DigestSignFinal(evp_md_ctx, NULL, &sigder_len) != 1) {
|
2012-10-24 12:58:16 -07:00
|
|
|
DST_RET(dst__openssl_toresult3(
|
2021-09-20 15:15:14 +00:00
|
|
|
dctx->category, "EVP_DigestSignFinal", ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-09-20 15:15:14 +00:00
|
|
|
if (sigder_len == 0) {
|
|
|
|
DST_RET(ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
sigder = isc_mem_get(dctx->mctx, sigder_len);
|
|
|
|
sigder_alloced = sigder_len;
|
|
|
|
if (EVP_DigestSignFinal(evp_md_ctx, sigder, &sigder_len) != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult3(
|
|
|
|
dctx->category, "EVP_DigestSignFinal", ISC_R_FAILURE));
|
|
|
|
}
|
|
|
|
sigder_copy = sigder;
|
|
|
|
if (d2i_ECDSA_SIG(&ecdsasig, &sigder_copy, sigder_len) == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category, "d2i_ECDSA_SIG",
|
|
|
|
ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-09-20 15:15:14 +00: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;
|
|
|
|
|
|
|
|
err:
|
2021-09-20 15:15:14 +00:00
|
|
|
if (sigder != NULL && sigder_alloced != 0) {
|
|
|
|
isc_mem_put(dctx->mctx, sigder, sigder_alloced);
|
|
|
|
}
|
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
|
|
|
|
isc_result_t ret;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
int status;
|
|
|
|
unsigned char *cp = sig->base;
|
|
|
|
ECDSA_SIG *ecdsasig = NULL;
|
|
|
|
EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
|
2021-09-20 15:15:14 +00:00
|
|
|
size_t siglen, sigder_len = 0, sigder_alloced = 0;
|
|
|
|
unsigned char *sigder = NULL;
|
|
|
|
unsigned char *sigder_copy;
|
2016-10-31 10:04:37 +11:00
|
|
|
BIGNUM *r = NULL, *s = NULL;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
REQUIRE(dctx->use == DO_VERIFY);
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
|
|
|
siglen = DNS_SIG_ECDSA256SIZE;
|
|
|
|
} else {
|
|
|
|
siglen = DNS_SIG_ECDSA384SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
if (sig->length != siglen) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(DST_R_VERIFYFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
ecdsasig = ECDSA_SIG_new();
|
|
|
|
if (ecdsasig == NULL) {
|
|
|
|
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);
|
2012-05-30 11:52:02 +10:00
|
|
|
/* cp += siglen / 2; */
|
2021-09-20 15:15:14 +00:00
|
|
|
ECDSA_SIG_set0(ecdsasig, r, s);
|
|
|
|
|
|
|
|
status = i2d_ECDSA_SIG(ecdsasig, NULL);
|
|
|
|
if (status < 0) {
|
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category, "i2d_ECDSA_SIG",
|
|
|
|
DST_R_VERIFYFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
sigder_len = (size_t)status;
|
|
|
|
sigder = isc_mem_get(dctx->mctx, sigder_len);
|
|
|
|
sigder_alloced = sigder_len;
|
|
|
|
|
|
|
|
sigder_copy = sigder;
|
|
|
|
status = i2d_ECDSA_SIG(ecdsasig, &sigder_copy);
|
|
|
|
if (status < 0) {
|
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category, "i2d_ECDSA_SIG",
|
|
|
|
DST_R_VERIFYFAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = EVP_DigestVerifyFinal(evp_md_ctx, sigder, sigder_len);
|
2012-05-02 23:20:17 +10:00
|
|
|
|
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:
|
2021-09-20 15:15:14 +00:00
|
|
|
ret = dst__openssl_toresult3(dctx->category,
|
|
|
|
"EVP_DigestVerifyFinal",
|
2012-07-23 15:08:21 +10:00
|
|
|
DST_R_VERIFYFAILURE);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (ecdsasig != NULL) {
|
|
|
|
ECDSA_SIG_free(ecdsasig);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-09-20 15:15:14 +00:00
|
|
|
if (sigder != NULL && sigder_alloced != 0) {
|
|
|
|
isc_mem_put(dctx->mctx, sigder, sigder_alloced);
|
|
|
|
}
|
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_generate(dst_key_t *key, int unused, void (*callback)(int)) {
|
|
|
|
isc_result_t ret;
|
2021-09-20 15:15:14 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2012-05-02 23:20:17 +10:00
|
|
|
UNUSED(unused);
|
|
|
|
UNUSED(callback);
|
|
|
|
|
2022-12-28 22:44:23 +02:00
|
|
|
ret = opensslecdsa_generate_pkey(key->key_alg, &pkey);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
return (ret);
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
key->key_size = EVP_PKEY_bits(pkey);
|
|
|
|
key->keydata.pkeypair.priv = pkey;
|
|
|
|
key->keydata.pkeypair.pub = pkey;
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2012-05-02 23:20:17 +10:00
|
|
|
opensslecdsa_isprivate(const dst_key_t *key) {
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
return (key->keydata.pkeypair.priv != NULL);
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opensslecdsa_destroy(dst_key_t *key) {
|
2022-12-28 17:13:41 +02:00
|
|
|
if (key->keydata.pkeypair.priv != key->keydata.pkeypair.pub) {
|
|
|
|
EVP_PKEY_free(key->keydata.pkeypair.priv);
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
EVP_PKEY_free(key->keydata.pkeypair.pub);
|
|
|
|
key->keydata.pkeypair.pub = NULL;
|
|
|
|
key->keydata.pkeypair.priv = NULL;
|
2012-05-02 23:20:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_todns(const dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey;
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY *eckey = NULL;
|
|
|
|
int len;
|
|
|
|
unsigned char *cp;
|
2021-09-20 15:15:14 +00:00
|
|
|
#else
|
|
|
|
int status;
|
|
|
|
BIGNUM *x = NULL;
|
|
|
|
BIGNUM *y = NULL;
|
|
|
|
size_t keysize = 0;
|
|
|
|
size_t len = 0;
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
2021-09-20 15:15:14 +00:00
|
|
|
isc_region_t r;
|
2012-05-02 23:20:17 +10:00
|
|
|
unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
REQUIRE(key->keydata.pkeypair.pub != NULL);
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
pkey = key->keydata.pkeypair.pub;
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2012-05-02 23:20:17 +10:00
|
|
|
eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
|
|
|
if (eckey == NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(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);
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
/* skip form */
|
|
|
|
len--;
|
2021-09-20 15:15:14 +00:00
|
|
|
#else
|
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
|
|
|
keysize = DNS_KEY_ECDSA256SIZE;
|
|
|
|
} else if (key->key_alg == DST_ALG_ECDSA384) {
|
|
|
|
keysize = DNS_KEY_ECDSA384SIZE;
|
|
|
|
} else {
|
|
|
|
DST_RET(ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = keysize;
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
isc_buffer_availableregion(data, &r);
|
|
|
|
if (r.length < (unsigned int)len) {
|
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2012-05-02 23:20:17 +10:00
|
|
|
cp = buf;
|
|
|
|
if (!i2o_ECPublicKey(eckey, &cp)) {
|
2012-07-23 15:08:21 +10: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);
|
2021-09-20 15:15:14 +00:00
|
|
|
#else
|
|
|
|
status = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &x);
|
|
|
|
if (status != 1 || x == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_get_bn_param",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
status = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &y);
|
|
|
|
if (status != 1 || y == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_get_bn_param",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
BN_bn2bin_fixed(x, &buf[0], keysize / 2);
|
|
|
|
BN_bn2bin_fixed(y, &buf[keysize / 2], keysize / 2);
|
|
|
|
memmove(r.base, buf, len);
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
isc_buffer_add(data, len);
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2021-09-20 15:15:14 +00:00
|
|
|
if (eckey != NULL) {
|
|
|
|
EC_KEY_free(eckey);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (x != NULL) {
|
|
|
|
BN_clear_free(x);
|
|
|
|
}
|
|
|
|
if (y != NULL) {
|
|
|
|
BN_clear_free(y);
|
|
|
|
}
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
isc_result_t ret;
|
2021-09-20 15:15:14 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2012-05-02 23:20:17 +10:00
|
|
|
isc_region_t r;
|
2022-12-28 17:13:41 +02:00
|
|
|
|
2021-09-20 15:15:14 +00:00
|
|
|
size_t len;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
if (key->key_alg == DST_ALG_ECDSA256) {
|
|
|
|
len = DNS_KEY_ECDSA256SIZE;
|
|
|
|
} else {
|
|
|
|
len = DNS_KEY_ECDSA384SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_remainingregion(data, &r);
|
|
|
|
if (r.length == 0) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2021-11-19 10:54:05 +01:00
|
|
|
if (r.length != len) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
ret = opensslecdsa_create_pkey(key->key_alg, false, r.base, len, &pkey);
|
2021-09-20 15:15:14 +00:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
DST_RET(ret);
|
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
|
|
|
|
isc_buffer_forward(data, len);
|
2022-12-28 17:13:41 +02:00
|
|
|
key->key_size = EVP_PKEY_bits(pkey);
|
|
|
|
key->keydata.pkeypair.pub = pkey;
|
2012-05-02 23:20:17 +10:00
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
err:
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_tofile(const dst_key_t *key, const char *directory) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey;
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2012-05-02 23:20:17 +10:00
|
|
|
EC_KEY *eckey = NULL;
|
2021-09-20 15:15:14 +00:00
|
|
|
const BIGNUM *privkey = NULL;
|
|
|
|
#else
|
|
|
|
int status;
|
|
|
|
BIGNUM *privkey = NULL;
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
2012-05-02 23:20:17 +10:00
|
|
|
dst_private_t priv;
|
|
|
|
unsigned char *buf = NULL;
|
2020-02-07 14:20:54 +01:00
|
|
|
unsigned short i;
|
2012-05-02 23:20:17 +10:00
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
if (key->keydata.pkeypair.pub == NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(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;
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__privstruct_writefile(key, &priv, directory));
|
2013-09-04 13:53:02 +10:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
if (key->keydata.pkeypair.priv == NULL) {
|
|
|
|
DST_RET(DST_R_NULLKEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkey = key->keydata.pkeypair.priv;
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2012-05-02 23:20:17 +10:00
|
|
|
eckey = EVP_PKEY_get1_EC_KEY(pkey);
|
|
|
|
if (eckey == NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_get1_EC_KEY",
|
|
|
|
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);
|
|
|
|
if (privkey == NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__openssl_toresult2("EC_KEY_get0_private_key",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
status = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
|
|
|
|
&privkey);
|
|
|
|
if (status != 1 || privkey == NULL) {
|
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_get_bn_param",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
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);
|
|
|
|
|
|
|
|
err:
|
2021-09-20 15:15:14 +00:00
|
|
|
if (buf != NULL && privkey != 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
|
|
|
}
|
2022-09-08 17:19:20 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000
|
2021-09-20 15:15:14 +00:00
|
|
|
if (eckey != NULL) {
|
|
|
|
EC_KEY_free(eckey);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (privkey != NULL) {
|
|
|
|
BN_clear_free(privkey);
|
|
|
|
}
|
2022-09-08 17:19:20 +02:00
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L || OPENSSL_API_LEVEL < 30000 */
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:09:38 +01:00
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
|
|
|
|
const char *pin);
|
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
|
|
|
|
dst_private_t priv;
|
2021-09-20 15:15:14 +00:00
|
|
|
isc_result_t ret;
|
2022-12-28 17:13:41 +02:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2020-02-07 14:20:54 +01:00
|
|
|
const char *engine = NULL;
|
|
|
|
const char *label = NULL;
|
2021-01-05 11:09:38 +01:00
|
|
|
int i, privkey_index = -1;
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2020-02-07 14:20:54 +01:00
|
|
|
/* read private key file */
|
2021-09-20 15:15:14 +00:00
|
|
|
ret = dst__privstruct_parse(key, DST_ALG_ECDSA256, lexer, key->mctx,
|
|
|
|
&priv);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
goto err;
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key->external) {
|
|
|
|
if (priv.nelements != 0 || pub == NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPRIVATEKEY));
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
key->keydata.pkeypair.priv = pub->keydata.pkeypair.priv;
|
|
|
|
key->keydata.pkeypair.pub = pub->keydata.pkeypair.pub;
|
|
|
|
pub->keydata.pkeypair.priv = NULL;
|
|
|
|
pub->keydata.pkeypair.pub = NULL;
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(ISC_R_SUCCESS);
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:09:38 +01:00
|
|
|
for (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;
|
|
|
|
case TAG_ECDSA_PRIVATEKEY:
|
|
|
|
privkey_index = i;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:09:38 +01:00
|
|
|
if (privkey_index < 0) {
|
2021-09-20 15:15:14 +00:00
|
|
|
DST_RET(dst__openssl_toresult(DST_R_INVALIDPRIVATEKEY));
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:09:38 +01:00
|
|
|
if (label != NULL) {
|
2021-09-20 15:15:14 +00:00
|
|
|
ret = opensslecdsa_fromlabel(key, engine, label, NULL);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
goto err;
|
2021-01-05 11:09:38 +01:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
/* Check that the public component matches if given */
|
|
|
|
if (pub != NULL && EVP_PKEY_eq(key->keydata.pkeypair.pub,
|
|
|
|
pub->keydata.pkeypair.pub) != 1)
|
|
|
|
{
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2021-01-05 11:09:38 +01:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
DST_RET(ISC_R_SUCCESS);
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
ret = opensslecdsa_create_pkey(
|
|
|
|
key->key_alg, true, priv.elements[privkey_index].data,
|
|
|
|
priv.elements[privkey_index].length, &pkey);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
goto err;
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
/* Check that the public component matches if given */
|
|
|
|
if (pub != NULL && EVP_PKEY_eq(pkey, pub->keydata.pkeypair.pub) != 1) {
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2021-01-05 11:09:38 +01:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
key->key_size = EVP_PKEY_bits(pkey);
|
|
|
|
key->keydata.pkeypair.priv = pkey;
|
|
|
|
key->keydata.pkeypair.pub = pkey;
|
|
|
|
pkey = NULL;
|
2021-09-20 15:15:14 +00:00
|
|
|
|
|
|
|
err:
|
2022-12-28 17:13:41 +02:00
|
|
|
EVP_PKEY_free(pkey);
|
2021-09-20 15:15:14 +00:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
key->keydata.generic = NULL;
|
|
|
|
}
|
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));
|
2021-09-20 15:15:14 +00:00
|
|
|
|
|
|
|
return (ret);
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
opensslecdsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
|
|
|
|
const char *pin) {
|
2022-12-28 17:13:41 +02:00
|
|
|
EVP_PKEY *privpkey = NULL, *pubpkey = NULL;
|
|
|
|
isc_result_t ret;
|
2021-09-20 15:15:14 +00:00
|
|
|
|
2022-12-28 15:37:33 +02:00
|
|
|
REQUIRE(opensslecdsa_valid_key_alg(key->key_alg));
|
2020-02-07 14:20:54 +01:00
|
|
|
UNUSED(pin);
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
ret = dst__openssl_fromlabel(EVP_PKEY_EC, engine, label, pin, &pubpkey,
|
|
|
|
&privpkey);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
goto err;
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 17:13:41 +02:00
|
|
|
ret = opensslecdsa_validate_pkey_group(key->key_alg, privpkey);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
goto err;
|
2020-02-07 14:20:54 +01:00
|
|
|
}
|
2022-12-28 17:13:41 +02:00
|
|
|
ret = opensslecdsa_validate_pkey_group(key->key_alg, pubpkey);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
goto err;
|
2021-09-20 15:15:14 +00:00
|
|
|
}
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2021-01-05 10:53:17 +01:00
|
|
|
key->engine = isc_mem_strdup(key->mctx, engine);
|
2022-12-28 17:13:41 +02:00
|
|
|
key->label = isc_mem_strdup(key->mctx, label);
|
|
|
|
key->key_size = EVP_PKEY_bits(privpkey);
|
|
|
|
key->keydata.pkeypair.priv = privpkey;
|
|
|
|
key->keydata.pkeypair.pub = pubpkey;
|
|
|
|
privpkey = NULL;
|
|
|
|
pubpkey = NULL;
|
2020-02-07 14:20:54 +01:00
|
|
|
|
2021-01-05 10:53:17 +01:00
|
|
|
err:
|
2022-12-28 17:13:41 +02:00
|
|
|
EVP_PKEY_free(privpkey);
|
|
|
|
EVP_PKEY_free(pubpkey);
|
2021-01-05 10:53:17 +01:00
|
|
|
return (ret);
|
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 */
|
2022-12-28 17:13:41 +02:00
|
|
|
dst__openssl_compare_keypair,
|
2012-05-02 23:20:17 +10:00
|
|
|
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
|
|
|
|
dst__opensslecdsa_init(dst_func_t **funcp) {
|
|
|
|
REQUIRE(funcp != NULL);
|
|
|
|
if (*funcp == NULL) {
|
|
|
|
*funcp = &opensslecdsa_functions;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-02 23:20:17 +10:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|