2017-07-31 15:26:00 +02:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-07-31 15:26:00 +02: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 http://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.
|
2017-07-31 15:26:00 +02:00
|
|
|
*/
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if !USE_PKCS11
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2018-07-19 11:29:05 -04:00
|
|
|
#if HAVE_OPENSSL_ED25519 || HAVE_OPENSSL_ED448
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
|
2017-07-31 15:26:00 +02:00
|
|
|
#include <isc/mem.h>
|
2018-06-12 11:26:04 +02:00
|
|
|
#include <isc/result.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/safe.h>
|
2017-07-31 15:26:00 +02: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>
|
|
|
|
|
2017-07-31 15:26:00 +02:00
|
|
|
#include "dst_internal.h"
|
|
|
|
#include "dst_openssl.h"
|
|
|
|
#include "dst_parse.h"
|
2020-02-12 13:59:18 +01:00
|
|
|
#include "openssl_shim.h"
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define DST_RET(a) \
|
|
|
|
{ \
|
|
|
|
ret = a; \
|
|
|
|
goto err; \
|
|
|
|
}
|
2018-06-12 11:26:04 +02:00
|
|
|
|
|
|
|
#if HAVE_OPENSSL_ED25519
|
|
|
|
|
2017-07-31 15:26:00 +02:00
|
|
|
#ifndef NID_ED25519
|
|
|
|
#error "Ed25519 group is not known (NID_ED25519)"
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef NID_ED25519 */
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
/* OpenSSL doesn't provide direct access to key values */
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define PUBPREFIXLEN 12
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static const unsigned char ed25519_pub_prefix[] = { 0x30, 0x2a, 0x30, 0x05,
|
|
|
|
0x06, 0x03, 0x2b, 0x65,
|
|
|
|
0x70, 0x03, 0x21, 0x00 };
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed25519_to_ossl(const unsigned char *key) {
|
|
|
|
unsigned char buf[PUBPREFIXLEN + DNS_KEY_ED25519SIZE];
|
2017-07-31 15:26:00 +02:00
|
|
|
const unsigned char *p;
|
|
|
|
|
|
|
|
memmove(buf, ed25519_pub_prefix, PUBPREFIXLEN);
|
|
|
|
memmove(buf + PUBPREFIXLEN, key, DNS_KEY_ED25519SIZE);
|
|
|
|
p = buf;
|
|
|
|
return (d2i_PUBKEY(NULL, &p, PUBPREFIXLEN + DNS_KEY_ED25519SIZE));
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed25519_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
|
|
|
unsigned char buf[PUBPREFIXLEN + DNS_KEY_ED25519SIZE];
|
2017-07-31 15:26:00 +02:00
|
|
|
unsigned char *p;
|
2020-02-13 14:44:37 -08:00
|
|
|
int len;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
len = i2d_PUBKEY(pkey, NULL);
|
|
|
|
if ((len <= DNS_KEY_ED25519SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PUBPREFIXLEN + DNS_KEY_ED25519SIZE)) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
p = buf;
|
|
|
|
len = i2d_PUBKEY(pkey, &p);
|
|
|
|
if ((len <= DNS_KEY_ED25519SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PUBPREFIXLEN + DNS_KEY_ED25519SIZE)) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
memmove(key, buf + len - DNS_KEY_ED25519SIZE, DNS_KEY_ED25519SIZE);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define PRIVPREFIXLEN 16
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static const unsigned char ed25519_priv_prefix[] = { 0x30, 0x2e, 0x02, 0x01,
|
|
|
|
0x00, 0x30, 0x05, 0x06,
|
|
|
|
0x03, 0x2b, 0x65, 0x70,
|
|
|
|
0x04, 0x22, 0x04, 0x20 };
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed25519_to_ossl(const unsigned char *key) {
|
|
|
|
unsigned char buf[PRIVPREFIXLEN + DNS_KEY_ED25519SIZE];
|
2017-07-31 15:26:00 +02:00
|
|
|
const unsigned char *p;
|
|
|
|
|
|
|
|
memmove(buf, ed25519_priv_prefix, PRIVPREFIXLEN);
|
|
|
|
memmove(buf + PRIVPREFIXLEN, key, DNS_KEY_ED25519SIZE);
|
|
|
|
p = buf;
|
|
|
|
return (d2i_PrivateKey(NID_ED25519, NULL, &p,
|
|
|
|
PRIVPREFIXLEN + DNS_KEY_ED25519SIZE));
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed25519_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
|
|
|
unsigned char buf[PRIVPREFIXLEN + DNS_KEY_ED25519SIZE];
|
2017-07-31 15:26:00 +02:00
|
|
|
unsigned char *p;
|
2020-02-13 14:44:37 -08:00
|
|
|
int len;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
len = i2d_PrivateKey(pkey, NULL);
|
|
|
|
if ((len <= DNS_KEY_ED25519SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PRIVPREFIXLEN + DNS_KEY_ED25519SIZE)) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
p = buf;
|
|
|
|
len = i2d_PrivateKey(pkey, &p);
|
|
|
|
if ((len <= DNS_KEY_ED25519SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PRIVPREFIXLEN + DNS_KEY_ED25519SIZE)) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
memmove(key, buf + len - DNS_KEY_ED25519SIZE, DNS_KEY_ED25519SIZE);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#else /* HAVE_OPENSSL_ED25519 */
|
|
|
|
|
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed25519_to_ossl(const unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(key);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed25519_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(pkey);
|
|
|
|
UNUSED(key);
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed25519_to_ossl(const unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(key);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed25519_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(pkey);
|
|
|
|
UNUSED(key);
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_OPENSSL_ED25519 */
|
|
|
|
|
|
|
|
#if HAVE_OPENSSL_ED448
|
|
|
|
|
|
|
|
#ifndef NID_ED448
|
|
|
|
#error "Ed448 group is not known (NID_ED448)"
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef NID_ED448 */
|
2018-06-12 11:26:04 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static const unsigned char ed448_pub_prefix[] = { 0x30, 0x43, 0x30, 0x05,
|
|
|
|
0x06, 0x03, 0x2b, 0x65,
|
|
|
|
0x71, 0x03, 0x3a, 0x00 };
|
2018-06-12 11:26:04 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed448_to_ossl(const unsigned char *key) {
|
|
|
|
unsigned char buf[PUBPREFIXLEN + DNS_KEY_ED448SIZE];
|
2018-06-12 11:26:04 +02:00
|
|
|
const unsigned char *p;
|
|
|
|
|
|
|
|
memmove(buf, ed448_pub_prefix, PUBPREFIXLEN);
|
|
|
|
memmove(buf + PUBPREFIXLEN, key, DNS_KEY_ED448SIZE);
|
|
|
|
p = buf;
|
|
|
|
return (d2i_PUBKEY(NULL, &p, PUBPREFIXLEN + DNS_KEY_ED448SIZE));
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed448_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
|
|
|
unsigned char buf[PUBPREFIXLEN + DNS_KEY_ED448SIZE];
|
2018-06-12 11:26:04 +02:00
|
|
|
unsigned char *p;
|
2020-02-13 14:44:37 -08:00
|
|
|
int len;
|
2018-06-12 11:26:04 +02:00
|
|
|
|
|
|
|
len = i2d_PUBKEY(pkey, NULL);
|
|
|
|
if ((len <= DNS_KEY_ED448SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PUBPREFIXLEN + DNS_KEY_ED448SIZE)) {
|
2018-06-12 11:26:04 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-06-12 11:26:04 +02:00
|
|
|
p = buf;
|
|
|
|
len = i2d_PUBKEY(pkey, &p);
|
|
|
|
if ((len <= DNS_KEY_ED448SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PUBPREFIXLEN + DNS_KEY_ED448SIZE)) {
|
2018-06-12 11:26:04 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-06-12 11:26:04 +02:00
|
|
|
memmove(key, buf + len - DNS_KEY_ED448SIZE, DNS_KEY_ED448SIZE);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static const unsigned char ed448_priv_prefix[] = { 0x30, 0x47, 0x02, 0x01,
|
|
|
|
0x00, 0x30, 0x05, 0x06,
|
|
|
|
0x03, 0x2b, 0x65, 0x71,
|
|
|
|
0x04, 0x3b, 0x04, 0x39 };
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed448_to_ossl(const unsigned char *key) {
|
|
|
|
unsigned char buf[PRIVPREFIXLEN + DNS_KEY_ED448SIZE];
|
2017-07-31 15:26:00 +02:00
|
|
|
const unsigned char *p;
|
|
|
|
|
|
|
|
memmove(buf, ed448_priv_prefix, PRIVPREFIXLEN);
|
|
|
|
memmove(buf + PRIVPREFIXLEN, key, DNS_KEY_ED448SIZE);
|
|
|
|
p = buf;
|
|
|
|
return (d2i_PrivateKey(NID_ED448, NULL, &p,
|
|
|
|
PRIVPREFIXLEN + DNS_KEY_ED448SIZE));
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed448_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
|
|
|
unsigned char buf[PRIVPREFIXLEN + DNS_KEY_ED448SIZE];
|
2017-07-31 15:26:00 +02:00
|
|
|
unsigned char *p;
|
2020-02-13 14:44:37 -08:00
|
|
|
int len;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
len = i2d_PrivateKey(pkey, NULL);
|
|
|
|
if ((len <= DNS_KEY_ED448SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PRIVPREFIXLEN + DNS_KEY_ED448SIZE)) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
p = buf;
|
|
|
|
len = i2d_PrivateKey(pkey, &p);
|
|
|
|
if ((len <= DNS_KEY_ED448SIZE) ||
|
2020-02-13 21:48:23 +01:00
|
|
|
(len > PRIVPREFIXLEN + DNS_KEY_ED448SIZE)) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
memmove(key, buf + len - DNS_KEY_ED448SIZE, DNS_KEY_ED448SIZE);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#else /* HAVE_OPENSSL_ED448 */
|
|
|
|
|
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed448_to_ossl(const unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(key);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
pub_ed448_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(pkey);
|
|
|
|
UNUSED(key);
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static EVP_PKEY *
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed448_to_ossl(const unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(key);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
priv_ed448_from_ossl(EVP_PKEY *pkey, unsigned char *key) {
|
2018-06-12 11:26:04 +02:00
|
|
|
UNUSED(pkey);
|
|
|
|
UNUSED(key);
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_OPENSSL_ED448 */
|
|
|
|
|
2017-07-31 15:26:00 +02:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_createctx(dst_key_t *key, dst_context_t *dctx) {
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_t *buf = NULL;
|
|
|
|
|
|
|
|
UNUSED(key);
|
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_ED448);
|
|
|
|
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dctx->mctx, &buf, 64);
|
2017-07-31 15:26:00 +02:00
|
|
|
dctx->ctxdata.generic = buf;
|
|
|
|
|
2020-02-02 08:35:46 +01:00
|
|
|
return (ISC_R_SUCCESS);
|
2017-07-31 15:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_destroyctx(dst_context_t *dctx) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_buffer_t *buf = (isc_buffer_t *)dctx->ctxdata.generic;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_ED448);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (buf != NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_free(&buf);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
dctx->ctxdata.generic = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_buffer_t *buf = (isc_buffer_t *)dctx->ctxdata.generic;
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_t *nbuf = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_region_t r;
|
|
|
|
unsigned int length;
|
|
|
|
isc_result_t result;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(dctx->key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
dctx->key->key_alg == DST_ALG_ED448);
|
|
|
|
|
|
|
|
result = isc_buffer_copyregion(buf, data);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
length = isc_buffer_length(buf) + data->length + 64;
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dctx->mctx, &nbuf, length);
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_usedregion(buf, &r);
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)isc_buffer_copyregion(nbuf, &r);
|
|
|
|
(void)isc_buffer_copyregion(nbuf, data);
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_free(&buf);
|
|
|
|
dctx->ctxdata.generic = nbuf;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
|
|
|
|
isc_result_t ret;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
isc_region_t tbsreg;
|
|
|
|
isc_region_t sigreg;
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_buffer_t *buf = (isc_buffer_t *)dctx->ctxdata.generic;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t siglen;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
key->key_alg == DST_ALG_ED448);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->key_alg == DST_ALG_ED25519) {
|
2017-07-31 15:26:00 +02:00
|
|
|
siglen = DNS_SIG_ED25519SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2017-07-31 15:26:00 +02:00
|
|
|
siglen = DNS_SIG_ED448SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
isc_buffer_availableregion(sig, &sigreg);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sigreg.length < (unsigned int)siglen) {
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_RET(ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
isc_buffer_usedregion(buf, &tbsreg);
|
|
|
|
|
2018-10-04 12:19:10 +02:00
|
|
|
if (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult3(
|
|
|
|
dctx->category, "EVP_DigestSignInit", ISC_R_FAILURE));
|
2018-06-12 11:26:04 +02:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (EVP_DigestSign(ctx, sigreg.base, &siglen, tbsreg.base,
|
|
|
|
tbsreg.length) != 1) {
|
|
|
|
DST_RET(dst__openssl_toresult3(dctx->category, "EVP_DigestSign",
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_R_SIGNFAILURE));
|
2018-06-12 11:26:04 +02:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_buffer_add(sig, (unsigned int)siglen);
|
2017-07-31 15:26:00 +02:00
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2019-09-09 14:05:31 +02:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_free(&buf);
|
|
|
|
dctx->ctxdata.generic = NULL;
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
|
|
|
|
isc_result_t ret;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
int status;
|
|
|
|
isc_region_t tbsreg;
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_buffer_t *buf = (isc_buffer_t *)dctx->ctxdata.generic;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int siglen = 0;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
key->key_alg == DST_ALG_ED448);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if HAVE_OPENSSL_ED25519
|
|
|
|
if (key->key_alg == DST_ALG_ED25519) {
|
2017-07-31 15:26:00 +02:00
|
|
|
siglen = DNS_SIG_ED25519SIZE;
|
2018-06-12 11:26:04 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_OPENSSL_ED25519 */
|
2018-06-12 11:26:04 +02:00
|
|
|
#if HAVE_OPENSSL_ED448
|
|
|
|
if (key->key_alg == DST_ALG_ED448) {
|
2017-07-31 15:26:00 +02:00
|
|
|
siglen = DNS_SIG_ED448SIZE;
|
2018-06-12 11:26:04 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_OPENSSL_ED448 */
|
2018-06-12 11:26:04 +02:00
|
|
|
if (siglen == 0) {
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sig->length != siglen) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_VERIFYFAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
isc_buffer_usedregion(buf, &tbsreg);
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
if (EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey) != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult3(
|
|
|
|
dctx->category, "EVP_DigestVerifyInit", ISC_R_FAILURE));
|
2018-06-12 11:26:04 +02:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
status = EVP_DigestVerify(ctx, sig->base, siglen, tbsreg.base,
|
|
|
|
tbsreg.length);
|
2017-07-31 15:26:00 +02: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, "EVP_DigestVerify",
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_R_VERIFYFAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2019-09-09 14:05:31 +02:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_free(&buf);
|
|
|
|
dctx->ctxdata.generic = NULL;
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
int status;
|
2017-07-31 15:26:00 +02:00
|
|
|
EVP_PKEY *pkey1 = key1->keydata.pkey;
|
|
|
|
EVP_PKEY *pkey2 = key2->keydata.pkey;
|
|
|
|
|
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
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
status = EVP_PKEY_cmp(pkey1, pkey2);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (status == 1) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2017-07-31 15:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_generate(dst_key_t *key, int unused, void (*callback)(int)) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
2017-07-31 15:26:00 +02:00
|
|
|
EVP_PKEY_CTX *ctx = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
int nid = 0, status;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
key->key_alg == DST_ALG_ED448);
|
|
|
|
UNUSED(unused);
|
|
|
|
UNUSED(callback);
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if HAVE_OPENSSL_ED25519
|
2017-07-31 15:26:00 +02:00
|
|
|
if (key->key_alg == DST_ALG_ED25519) {
|
|
|
|
nid = NID_ED25519;
|
|
|
|
key->key_size = DNS_KEY_ED25519SIZE;
|
2018-06-12 11:26:04 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_OPENSSL_ED25519 */
|
2018-06-12 11:26:04 +02:00
|
|
|
#if HAVE_OPENSSL_ED448
|
|
|
|
if (key->key_alg == DST_ALG_ED448) {
|
2017-07-31 15:26:00 +02:00
|
|
|
nid = NID_ED448;
|
|
|
|
key->key_size = DNS_KEY_ED448SIZE;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_OPENSSL_ED448 */
|
2018-06-12 11:26:04 +02:00
|
|
|
if (nid == 0) {
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
ctx = EVP_PKEY_CTX_new_id(nid, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ctx == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (dst__openssl_toresult2("EVP_PKEY_CTX_new_id",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
status = EVP_PKEY_keygen_init(ctx);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (status != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_keygen_init",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
status = EVP_PKEY_keygen(ctx, &pkey);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (status != 1) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult2("EVP_PKEY_keygen",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02: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
|
|
|
EVP_PKEY_CTX_free(ctx);
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_isprivate(const dst_key_t *key) {
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
|
|
|
int len;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
len = i2d_PrivateKey(pkey, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (len > 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
/* can check if first error is EC_R_INVALID_PRIVATE_KEY */
|
2020-03-25 17:00:07 +01:00
|
|
|
while (ERR_get_error() != 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
/**/
|
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2017-07-31 15:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_destroy(dst_key_t *key) {
|
2017-07-31 15:26:00 +02: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
|
|
|
openssleddsa_todns(const dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
EVP_PKEY *pkey = key->keydata.pkey;
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_region_t r;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(pkey != NULL);
|
|
|
|
|
|
|
|
pkey = key->keydata.pkey;
|
|
|
|
switch (key->key_alg) {
|
|
|
|
case DST_ALG_ED25519:
|
|
|
|
isc_buffer_availableregion(data, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < DNS_KEY_ED25519SIZE) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
result = pub_ed25519_from_ossl(pkey, r.base);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_add(data, DNS_KEY_ED25519SIZE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
return (result);
|
|
|
|
case DST_ALG_ED448:
|
|
|
|
isc_buffer_availableregion(data, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < DNS_KEY_ED448SIZE) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
result = pub_ed448_from_ossl(pkey, r.base);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_add(data, DNS_KEY_ED448SIZE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
return (result);
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2018-11-07 15:00:07 +07:00
|
|
|
ISC_UNREACHABLE();
|
2017-07-31 15:26:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
EVP_PKEY *pkey;
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_region_t r;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
key->key_alg == DST_ALG_ED448);
|
|
|
|
|
|
|
|
isc_buffer_remainingregion(data, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length == 0) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
if (key->key_alg == DST_ALG_ED25519) {
|
|
|
|
len = DNS_KEY_ED25519SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < len) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
pkey = pub_ed25519_to_ossl(r.base);
|
|
|
|
} else {
|
|
|
|
len = DNS_KEY_ED448SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < len) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
pkey = pub_ed448_to_ossl(r.base);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (dst__openssl_toresult(ISC_R_FAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_buffer_forward(data, len);
|
|
|
|
key->keydata.pkey = pkey;
|
|
|
|
key->key_size = len;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_tofile(const dst_key_t *key, const char *directory) {
|
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
dst_private_t priv;
|
2017-07-31 15:26:00 +02:00
|
|
|
unsigned char *buf = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int len;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
key->key_alg == DST_ALG_ED448);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->keydata.pkey == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (DST_R_NULLKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
if (key->external) {
|
|
|
|
priv.nelements = 0;
|
|
|
|
return (dst__privstruct_writefile(key, &priv, directory));
|
|
|
|
}
|
|
|
|
|
|
|
|
pkey = key->keydata.pkey;
|
|
|
|
if (key->key_alg == DST_ALG_ED25519) {
|
|
|
|
len = DNS_KEY_ED25519SIZE;
|
|
|
|
buf = isc_mem_get(key->mctx, len);
|
|
|
|
priv.elements[0].tag = TAG_EDDSA_PRIVATEKEY;
|
|
|
|
priv.elements[0].length = len;
|
|
|
|
ret = priv_ed25519_from_ossl(pkey, buf);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(ret));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
priv.elements[0].data = buf;
|
|
|
|
priv.nelements = 1;
|
|
|
|
ret = dst__privstruct_writefile(key, &priv, directory);
|
|
|
|
} else {
|
|
|
|
len = DNS_KEY_ED448SIZE;
|
|
|
|
buf = isc_mem_get(key->mctx, len);
|
|
|
|
priv.elements[0].tag = TAG_EDDSA_PRIVATEKEY;
|
|
|
|
priv.elements[0].length = len;
|
|
|
|
ret = priv_ed448_from_ossl(pkey, buf);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(ret));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
priv.elements[0].data = buf;
|
|
|
|
priv.nelements = 1;
|
|
|
|
ret = dst__privstruct_writefile(key, &priv, directory);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (buf != NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_mem_put(key->mctx, buf, len);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
eddsa_check(EVP_PKEY *privkey, dst_key_t *pub) {
|
2017-07-31 15:26:00 +02:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pub == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
pkey = pub->keydata.pkey;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (EVP_PKEY_cmp(privkey, pkey) == 1) {
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssleddsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
|
2017-07-31 15:26:00 +02:00
|
|
|
dst_private_t priv;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t ret;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
unsigned int len;
|
|
|
|
isc_mem_t *mctx = key->mctx;
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
REQUIRE(key->key_alg == DST_ALG_ED25519 ||
|
|
|
|
key->key_alg == DST_ALG_ED448);
|
|
|
|
|
|
|
|
/* read private key file */
|
|
|
|
ret = dst__privstruct_parse(key, DST_ALG_ED25519, lexer, mctx, &priv);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
2017-07-31 15:26:00 +02:00
|
|
|
goto err;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
if (key->external) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (priv.nelements != 0) {
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (pub == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
key->keydata.pkey = pub->keydata.pkey;
|
|
|
|
pub->keydata.pkey = NULL;
|
|
|
|
dst__privstruct_free(&priv, mctx);
|
2017-09-19 16:16:45 +05:30
|
|
|
isc_safe_memwipe(&priv, sizeof(priv));
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->key_alg == DST_ALG_ED25519) {
|
|
|
|
len = DNS_KEY_ED25519SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (priv.elements[0].length < len) {
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
pkey = priv_ed25519_to_ossl(priv.elements[0].data);
|
|
|
|
} else {
|
|
|
|
len = DNS_KEY_ED448SIZE;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (priv.elements[0].length < len) {
|
2017-07-31 15:26:00 +02:00
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
pkey = priv_ed448_to_ossl(priv.elements[0].data);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (pkey == NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
if (eddsa_check(pkey, pub) != ISC_R_SUCCESS) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
|
|
|
}
|
|
|
|
key->keydata.pkey = pkey;
|
|
|
|
key->key_size = len;
|
|
|
|
ret = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2017-07-31 15:26:00 +02:00
|
|
|
dst__privstruct_free(&priv, mctx);
|
2017-09-19 16:16:45 +05:30
|
|
|
isc_safe_memwipe(&priv, sizeof(priv));
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static dst_func_t openssleddsa_functions = {
|
|
|
|
openssleddsa_createctx,
|
|
|
|
NULL, /*%< createctx2 */
|
|
|
|
openssleddsa_destroyctx,
|
|
|
|
openssleddsa_adddata,
|
|
|
|
openssleddsa_sign,
|
|
|
|
openssleddsa_verify,
|
|
|
|
NULL, /*%< verify2 */
|
|
|
|
NULL, /*%< computesecret */
|
|
|
|
openssleddsa_compare,
|
|
|
|
NULL, /*%< paramcompare */
|
|
|
|
openssleddsa_generate,
|
|
|
|
openssleddsa_isprivate,
|
|
|
|
openssleddsa_destroy,
|
|
|
|
openssleddsa_todns,
|
|
|
|
openssleddsa_fromdns,
|
|
|
|
openssleddsa_tofile,
|
|
|
|
openssleddsa_parse,
|
|
|
|
NULL, /*%< cleanup */
|
|
|
|
NULL, /*%< fromlabel */
|
|
|
|
NULL, /*%< dump */
|
|
|
|
NULL, /*%< restore */
|
|
|
|
};
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dst__openssleddsa_init(dst_func_t **funcp) {
|
2017-07-31 15:26:00 +02:00
|
|
|
REQUIRE(funcp != NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*funcp == NULL) {
|
2017-07-31 15:26:00 +02:00
|
|
|
*funcp = &openssleddsa_functions;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-07-31 15:26:00 +02:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-07-19 11:29:05 -04:00
|
|
|
#endif /* HAVE_OPENSSL_ED25519 || HAVE_OPENSSL_ED448 */
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif /* !USE_PKCS11 */
|
2017-07-31 15:26:00 +02:00
|
|
|
|
|
|
|
/*! \file */
|