1999-09-27 16:55:45 +00:00
|
|
|
/*
|
2018-03-15 18:32:45 -07:00
|
|
|
* Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2007-06-18 23:47:57 +00: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
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2018-02-23 09:53:12 +01:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*
|
|
|
|
* Portions Copyright (C) Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2007-08-28 07:20:43 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1999-09-27 16:55:45 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
|
|
|
|
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
|
|
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-09-27 16:55:45 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
/*! \file */
|
|
|
|
|
1999-10-27 20:46:33 +00:00
|
|
|
#include <ctype.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
|
2001-12-12 16:43:24 +00:00
|
|
|
#include <isc/mem.h>
|
2017-09-19 16:16:45 +05:30
|
|
|
#include <isc/safe.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <pk11/site.h>
|
|
|
|
|
|
|
|
#include <dst/result.h>
|
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
#include "dst_internal.h"
|
2002-03-19 04:30:57 +00:00
|
|
|
#include "dst_openssl.h"
|
1999-09-27 16:55:45 +00:00
|
|
|
#include "dst_parse.h"
|
|
|
|
|
2018-05-02 14:17:40 +02:00
|
|
|
#define PRIME2 "02"
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define PRIME768 \
|
|
|
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088" \
|
|
|
|
"A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25" \
|
|
|
|
"F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFF" \
|
|
|
|
"F"
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define PRIME1024 \
|
|
|
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" \
|
2000-05-08 14:38:29 +00:00
|
|
|
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF2" \
|
|
|
|
"5F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406" \
|
|
|
|
"B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF"
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define PRIME1536 \
|
|
|
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
|
2001-12-12 17:18:52 +00:00
|
|
|
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
|
|
|
|
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
|
|
|
|
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
|
|
|
|
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
|
|
|
|
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
|
|
|
|
"83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
|
|
|
|
"670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
|
|
|
|
|
2018-05-02 14:17:40 +02:00
|
|
|
static BIGNUM *bn2 = NULL, *bn768 = NULL, *bn1024 = NULL, *bn1536 = NULL;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if !HAVE_DH_GET0_KEY
|
2016-10-31 10:04:37 +11:00
|
|
|
/*
|
|
|
|
* DH_get0_key, DH_set0_key, DH_get0_pqg and DH_set0_pqg
|
|
|
|
* are from OpenSSL 1.1.0.
|
|
|
|
*/
|
2016-10-30 23:46:10 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) {
|
2018-05-03 13:59:04 +02:00
|
|
|
if (pub_key != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*pub_key = dh->pub_key;
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
|
|
|
if (priv_key != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*priv_key = dh->priv_key;
|
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
|
|
|
DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) {
|
2018-05-03 13:59:04 +02:00
|
|
|
if (pub_key != NULL) {
|
|
|
|
BN_free(dh->pub_key);
|
|
|
|
dh->pub_key = pub_key;
|
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
|
2018-05-03 13:59:04 +02:00
|
|
|
if (priv_key != NULL) {
|
|
|
|
BN_free(dh->priv_key);
|
|
|
|
dh->priv_key = priv_key;
|
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
|
2018-05-03 13:59:04 +02:00
|
|
|
return (1);
|
2016-10-31 10:04:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q,
|
|
|
|
const BIGNUM **g) {
|
2018-05-03 13:59:04 +02:00
|
|
|
if (p != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*p = dh->p;
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
|
|
|
if (q != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*q = dh->q;
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
|
|
|
if (g != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
*g = dh->g;
|
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
|
|
|
DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
|
2018-05-03 13:59:04 +02:00
|
|
|
/* If the fields p and g in d are NULL, the corresponding input
|
|
|
|
* parameters MUST be non-NULL. q may remain NULL.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL)) {
|
2020-02-13 21:48:23 +01:00
|
|
|
return (0);
|
2018-05-03 13:59:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
BN_free(dh->p);
|
|
|
|
dh->p = p;
|
|
|
|
}
|
|
|
|
if (q != NULL) {
|
|
|
|
BN_free(dh->q);
|
|
|
|
dh->q = q;
|
|
|
|
}
|
|
|
|
if (g != NULL) {
|
|
|
|
BN_free(dh->g);
|
|
|
|
dh->g = g;
|
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
|
|
|
|
if (q != NULL) {
|
|
|
|
dh->length = BN_num_bits(q);
|
|
|
|
}
|
|
|
|
|
2018-05-03 13:59:04 +02:00
|
|
|
return (1);
|
2016-10-31 10:04:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DH_clear_flags(d, f) (d)->flags &= ~(f)
|
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif /* !HAVE_DH_GET0_KEY */
|
2016-10-31 10:04:37 +11:00
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldh_computesecret(const dst_key_t *pub, const dst_key_t *priv,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t *secret) {
|
|
|
|
DH *dhpub, *dhpriv;
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *pub_key = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
int ret;
|
|
|
|
isc_region_t r;
|
|
|
|
unsigned int len;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
REQUIRE(pub->keydata.dh != NULL);
|
|
|
|
REQUIRE(priv->keydata.dh != NULL);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
dhpub = pub->keydata.dh;
|
|
|
|
dhpriv = priv->keydata.dh;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
len = DH_size(dhpriv);
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(secret, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < len) {
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
|
|
|
|
DH_get0_key(dhpub, &pub_key, NULL);
|
|
|
|
ret = DH_compute_key(r.base, pub_key, dhpriv);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ret <= 0) {
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult2("DH_compute_key",
|
|
|
|
DST_R_COMPUTESECRETFAILURE));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
isc_buffer_add(secret, len);
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_compare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
DH *dh1, *dh2;
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *pub_key1 = NULL, *pub_key2 = NULL;
|
|
|
|
const BIGNUM *priv_key1 = NULL, *priv_key2 = NULL;
|
|
|
|
const BIGNUM *p1 = NULL, *g1 = NULL, *p2 = NULL, *g2 = NULL;
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
dh1 = key1->keydata.dh;
|
|
|
|
dh2 = key2->keydata.dh;
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh1 == NULL && dh2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (dh1 == NULL || dh2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_get0_key(dh1, &pub_key1, &priv_key1);
|
|
|
|
DH_get0_key(dh2, &pub_key2, &priv_key2);
|
|
|
|
DH_get0_pqg(dh1, &p1, NULL, &g1);
|
|
|
|
DH_get0_pqg(dh2, &p2, NULL, &g2);
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
if (BN_cmp(p1, p2) != 0 || BN_cmp(g1, g2) != 0 ||
|
2020-02-13 14:44:37 -08:00
|
|
|
BN_cmp(pub_key1, pub_key2) != 0)
|
|
|
|
{
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
if (priv_key1 != NULL || priv_key2 != NULL) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (priv_key1 == NULL || priv_key2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (BN_cmp(priv_key1, priv_key2) != 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_paramcompare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
DH *dh1, *dh2;
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *p1 = NULL, *g1 = NULL, *p2 = NULL, *g2 = NULL;
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
dh1 = key1->keydata.dh;
|
|
|
|
dh2 = key2->keydata.dh;
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh1 == NULL && dh2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (dh1 == NULL || dh2 == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_get0_pqg(dh1, &p1, NULL, &g1);
|
|
|
|
DH_get0_pqg(dh2, &p2, NULL, &g2);
|
2000-06-02 18:57:51 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (BN_cmp(p1, p2) != 0 || BN_cmp(g1, g2) != 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2009-10-24 09:46:19 +00:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
progress_cb(int p, int n, BN_GENCB *cb) {
|
2009-10-30 05:08:23 +00:00
|
|
|
union {
|
|
|
|
void *dptr;
|
|
|
|
void (*fptr)(int);
|
|
|
|
} u;
|
2009-10-24 09:46:19 +00:00
|
|
|
|
|
|
|
UNUSED(n);
|
2009-10-30 05:08:23 +00:00
|
|
|
|
2015-05-28 14:41:21 +10:00
|
|
|
u.dptr = BN_GENCB_get_arg(cb);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (u.fptr != NULL) {
|
2009-10-30 05:08:23 +00:00
|
|
|
u.fptr(p);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-10-24 09:46:19 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_generate(dst_key_t *key, int generator, void (*callback)(int)) {
|
|
|
|
DH *dh = NULL;
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_GENCB *cb;
|
2015-10-19 10:43:58 +11:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_GENCB _cb;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if OPENSSL_VERSION_NUMBER < 0x10100000L || \
|
|
|
|
* defined(LIBRESSL_VERSION_NUMBER) */
|
2009-10-30 05:08:23 +00:00
|
|
|
union {
|
|
|
|
void *dptr;
|
|
|
|
void (*fptr)(int);
|
|
|
|
} u;
|
2000-06-02 18:57:51 +00:00
|
|
|
|
|
|
|
if (generator == 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
if (key->key_size == 768 || key->key_size == 1024 ||
|
|
|
|
key->key_size == 1536) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BIGNUM *p, *g;
|
2000-06-02 18:57:51 +00:00
|
|
|
dh = DH_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->key_size == 768) {
|
2016-10-31 10:04:37 +11:00
|
|
|
p = BN_dup(bn768);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (key->key_size == 1024) {
|
2016-10-31 10:04:37 +11:00
|
|
|
p = BN_dup(bn1024);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2016-10-31 10:04:37 +11:00
|
|
|
p = BN_dup(bn1536);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
g = BN_dup(bn2);
|
|
|
|
if (dh == NULL || p == NULL || g == NULL) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_free(dh);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (p != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(p);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (g != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(g);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
|
|
|
}
|
|
|
|
DH_set0_pqg(dh, p, NULL, g);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2000-06-02 18:57:51 +00:00
|
|
|
generator = 2;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2007-01-08 01:53:30 +00:00
|
|
|
if (generator != 0) {
|
|
|
|
dh = DH_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh == NULL) {
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-05-28 14:41:21 +10:00
|
|
|
cb = BN_GENCB_new();
|
2015-10-19 10:43:58 +11:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
|
2015-05-28 14:41:21 +10:00
|
|
|
if (cb == NULL) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
|
|
|
|
* !defined(LIBRESSL_VERSION_NUMBER) */
|
2009-10-24 09:46:19 +00:00
|
|
|
if (callback == NULL) {
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_GENCB_set_old(cb, NULL, NULL);
|
2009-10-24 09:46:19 +00:00
|
|
|
} else {
|
2009-10-30 05:08:23 +00:00
|
|
|
u.fptr = callback;
|
2019-09-09 14:05:31 +02:00
|
|
|
BN_GENCB_set(cb, progress_cb, u.dptr);
|
2009-10-24 09:46:19 +00:00
|
|
|
}
|
2007-01-08 01:53:30 +00:00
|
|
|
|
2007-01-08 02:35:58 +00:00
|
|
|
if (!DH_generate_parameters_ex(dh, key->key_size, generator,
|
2015-05-28 14:41:21 +10:00
|
|
|
cb)) {
|
2007-01-08 01:53:30 +00:00
|
|
|
DH_free(dh);
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_GENCB_free(cb);
|
2020-02-12 13:59:18 +01:00
|
|
|
return (dst__openssl_toresult2("DH_generate_parameters_"
|
|
|
|
"ex",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2007-01-08 01:53:30 +00:00
|
|
|
}
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_GENCB_free(cb);
|
2018-02-24 10:47:35 -08:00
|
|
|
cb = NULL;
|
2007-01-08 01:53:30 +00:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
|
|
|
|
if (DH_generate_key(dh) == 0) {
|
|
|
|
DH_free(dh);
|
2012-07-23 15:08:21 +10:00
|
|
|
return (dst__openssl_toresult2("DH_generate_key",
|
|
|
|
DST_R_OPENSSLFAILURE));
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_clear_flags(dh, DH_FLAG_CACHE_MONT_P);
|
2006-12-04 01:54:53 +00:00
|
|
|
key->keydata.dh = dh;
|
2000-06-02 18:57:51 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_isprivate(const dst_key_t *key) {
|
|
|
|
DH *dh = key->keydata.dh;
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *priv_key = NULL;
|
|
|
|
|
|
|
|
DH_get0_key(dh, NULL, &priv_key);
|
2018-04-17 08:29:14 -07:00
|
|
|
return (dh != NULL && priv_key != NULL);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_destroy(dst_key_t *key) {
|
2006-12-04 01:54:53 +00:00
|
|
|
DH *dh = key->keydata.dh;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh == NULL) {
|
2000-06-02 18:57:51 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
|
|
|
|
DH_free(dh);
|
2006-12-04 01:54:53 +00:00
|
|
|
key->keydata.dh = NULL;
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
uint16_toregion(uint16_t val, isc_region_t *region) {
|
2015-08-07 13:16:10 -07:00
|
|
|
*region->base = (val & 0xff00) >> 8;
|
|
|
|
isc_region_consume(region, 1);
|
|
|
|
*region->base = (val & 0x00ff);
|
|
|
|
isc_region_consume(region, 1);
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
static uint16_t
|
2020-02-13 14:44:37 -08:00
|
|
|
uint16_fromregion(isc_region_t *region) {
|
|
|
|
uint16_t val;
|
2000-06-02 18:57:51 +00:00
|
|
|
unsigned char *cp = region->base;
|
|
|
|
|
|
|
|
val = ((unsigned int)(cp[0])) << 8;
|
|
|
|
val |= ((unsigned int)(cp[1]));
|
|
|
|
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(region, 2);
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
return (val);
|
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_todns(const dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
DH *dh;
|
2016-10-31 10:04:37 +11:00
|
|
|
const BIGNUM *pub_key = NULL, *p = NULL, *g = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_region_t r;
|
|
|
|
uint16_t dnslen, plen, glen, publen;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
REQUIRE(key->keydata.dh != NULL);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
dh = key->keydata.dh;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(data, &r);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_get0_pqg(dh, &p, NULL, &g);
|
|
|
|
if (BN_cmp(g, bn2) == 0 &&
|
2020-02-12 13:59:18 +01:00
|
|
|
(BN_cmp(p, bn768) == 0 || BN_cmp(p, bn1024) == 0 ||
|
2020-02-13 14:44:37 -08:00
|
|
|
BN_cmp(p, bn1536) == 0))
|
|
|
|
{
|
1999-09-27 16:55:45 +00:00
|
|
|
plen = 1;
|
|
|
|
glen = 0;
|
2020-02-12 13:59:18 +01:00
|
|
|
} else {
|
2016-10-31 10:04:37 +11:00
|
|
|
plen = BN_num_bytes(p);
|
|
|
|
glen = BN_num_bytes(g);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_get0_key(dh, &pub_key, NULL);
|
|
|
|
publen = BN_num_bytes(pub_key);
|
1999-09-27 16:55:45 +00:00
|
|
|
dnslen = plen + glen + publen + 6;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length < (unsigned int)dnslen) {
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
uint16_toregion(plen, &r);
|
|
|
|
if (plen == 1) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (BN_cmp(p, bn768) == 0) {
|
1999-09-27 16:55:45 +00:00
|
|
|
*r.base = 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (BN_cmp(p, bn1024) == 0) {
|
1999-09-27 16:55:45 +00:00
|
|
|
*r.base = 2;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2001-12-12 17:18:52 +00:00
|
|
|
*r.base = 3;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_bn2bin(p, r.base);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(&r, plen);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
uint16_toregion(glen, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (glen > 0) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_bn2bin(g, r.base);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(&r, glen);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
uint16_toregion(publen, &r);
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_bn2bin(pub_key, r.base);
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(&r, publen);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
isc_buffer_add(data, dnslen);
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_fromdns(dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
DH *dh;
|
|
|
|
BIGNUM *pub_key = NULL, *p = NULL, *g = NULL;
|
1999-09-27 16:55:45 +00:00
|
|
|
isc_region_t r;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint16_t plen, glen, publen;
|
|
|
|
int special = 0;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_remainingregion(data, &r);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (r.length == 0) {
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
dh = DH_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh == NULL) {
|
2005-06-17 02:22:45 +00:00
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_clear_flags(dh, DH_FLAG_CACHE_MONT_P);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the prime length. 1 & 2 are table entries, > 16 means a
|
|
|
|
* prime follows, otherwise an error.
|
|
|
|
*/
|
|
|
|
if (r.length < 2) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
plen = uint16_fromregion(&r);
|
|
|
|
if (plen < 16 && plen != 1 && plen != 2) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
if (r.length < plen) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
if (plen == 1 || plen == 2) {
|
2015-08-07 13:16:10 -07:00
|
|
|
if (plen == 1) {
|
|
|
|
special = *r.base;
|
|
|
|
isc_region_consume(&r, 1);
|
|
|
|
} else {
|
1999-09-27 16:55:45 +00:00
|
|
|
special = uint16_fromregion(&r);
|
2015-08-07 13:16:10 -07:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
switch (special) {
|
2020-02-12 13:59:18 +01:00
|
|
|
case 1:
|
|
|
|
p = BN_dup(bn768);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
p = BN_dup(bn1024);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
p = BN_dup(bn1536);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
2015-08-07 13:16:10 -07:00
|
|
|
} else {
|
2016-10-31 10:04:37 +11:00
|
|
|
p = BN_bin2bn(r.base, plen, NULL);
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(&r, plen);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the generator length. This should be 0 if the prime was
|
|
|
|
* special, but it might not be. If it's 0 and the prime is not
|
|
|
|
* special, we have a problem.
|
|
|
|
*/
|
|
|
|
if (r.length < 2) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
glen = uint16_fromregion(&r);
|
|
|
|
if (r.length < glen) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
if (special != 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (glen == 0) {
|
2016-10-31 10:04:37 +11:00
|
|
|
g = BN_dup(bn2);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2016-10-31 10:04:37 +11:00
|
|
|
g = BN_bin2bn(r.base, glen, NULL);
|
|
|
|
if (g != NULL && BN_cmp(g, bn2) != 0) {
|
1999-09-27 16:55:45 +00:00
|
|
|
DH_free(dh);
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(g);
|
1999-09-27 16:55:45 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
}
|
2015-08-07 13:16:10 -07:00
|
|
|
} else {
|
1999-09-27 16:55:45 +00:00
|
|
|
if (glen == 0) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
g = BN_bin2bn(r.base, glen, NULL);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(&r, glen);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
if (p == NULL || g == NULL) {
|
|
|
|
DH_free(dh);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (p != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(p);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (g != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(g);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
|
|
|
}
|
|
|
|
DH_set0_pqg(dh, p, NULL, g);
|
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
if (r.length < 2) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
|
|
|
publen = uint16_fromregion(&r);
|
|
|
|
if (r.length < publen) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
pub_key = BN_bin2bn(r.base, publen, NULL);
|
|
|
|
if (pub_key == NULL) {
|
|
|
|
DH_free(dh);
|
|
|
|
return (dst__openssl_toresult(ISC_R_NOMEMORY));
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
#if (LIBRESSL_VERSION_NUMBER >= 0x2070000fL) && \
|
|
|
|
(LIBRESSL_VERSION_NUMBER <= 0x2070200fL)
|
2018-05-02 14:18:06 +02:00
|
|
|
/*
|
|
|
|
* LibreSSL << 2.7.3 DH_get0_key requires priv_key to be set when
|
|
|
|
* DH structure is empty, hence we cannot use DH_get0_key().
|
|
|
|
*/
|
|
|
|
dh->pub_key = pub_key;
|
2020-02-12 13:59:18 +01:00
|
|
|
#else /* LIBRESSL_VERSION_NUMBER */
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_set0_key(dh, pub_key, NULL);
|
2018-05-02 14:18:06 +02:00
|
|
|
#endif /* LIBRESSL_VERSION_NUMBER */
|
2015-08-07 13:16:10 -07:00
|
|
|
isc_region_consume(&r, publen);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
key->key_size = BN_num_bits(p);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
isc_buffer_forward(data, plen + glen + publen + 6);
|
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
key->keydata.dh = dh;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_tofile(const dst_key_t *key, const char *directory) {
|
|
|
|
int i;
|
|
|
|
DH *dh;
|
|
|
|
const BIGNUM *pub_key = NULL, *priv_key = NULL, *p = NULL, *g = NULL;
|
|
|
|
dst_private_t priv;
|
2001-12-12 16:43:24 +00:00
|
|
|
unsigned char *bufs[4];
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->keydata.dh == NULL) {
|
1999-09-27 16:55:45 +00:00
|
|
|
return (DST_R_NULLKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->external) {
|
2014-01-30 17:48:10 -08:00
|
|
|
return (DST_R_EXTERNALKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-30 17:48:10 -08:00
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
dh = key->keydata.dh;
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_get0_key(dh, &pub_key, &priv_key);
|
|
|
|
DH_get0_pqg(dh, &p, NULL, &g);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2012-07-23 15:08:21 +10:00
|
|
|
memset(bufs, 0, sizeof(bufs));
|
2001-12-12 16:43:24 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2016-10-31 10:04:37 +11:00
|
|
|
bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(p));
|
2001-12-12 16:43:24 +00:00
|
|
|
}
|
|
|
|
|
2001-12-12 17:09:39 +00:00
|
|
|
i = 0;
|
2001-12-12 16:43:24 +00:00
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_DH_PRIME;
|
2016-10-31 10:04:37 +11:00
|
|
|
priv.elements[i].length = BN_num_bytes(p);
|
|
|
|
BN_bn2bin(p, bufs[i]);
|
2001-12-12 16:43:24 +00:00
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_DH_GENERATOR;
|
2016-10-31 10:04:37 +11:00
|
|
|
priv.elements[i].length = BN_num_bytes(g);
|
|
|
|
BN_bn2bin(g, bufs[i]);
|
2001-12-12 16:43:24 +00:00
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_DH_PRIVATE;
|
2016-10-31 10:04:37 +11:00
|
|
|
priv.elements[i].length = BN_num_bytes(priv_key);
|
|
|
|
BN_bn2bin(priv_key, bufs[i]);
|
2001-12-12 16:43:24 +00:00
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
priv.elements[i].tag = TAG_DH_PUBLIC;
|
2016-10-31 10:04:37 +11:00
|
|
|
priv.elements[i].length = BN_num_bytes(pub_key);
|
|
|
|
BN_bn2bin(pub_key, bufs[i]);
|
2001-12-12 16:43:24 +00:00
|
|
|
priv.elements[i].data = bufs[i];
|
|
|
|
i++;
|
|
|
|
|
|
|
|
priv.nelements = i;
|
|
|
|
result = dst__privstruct_writefile(key, &priv, directory);
|
2019-07-16 15:52:14 +02:00
|
|
|
|
2001-12-12 16:43:24 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (bufs[i] == NULL) {
|
2001-12-12 16:43:24 +00:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
isc_mem_put(key->mctx, bufs[i], BN_num_bytes(p));
|
2001-12-12 16:43:24 +00:00
|
|
|
}
|
|
|
|
return (result);
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
|
1999-09-27 16:55:45 +00:00
|
|
|
dst_private_t priv;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t ret;
|
|
|
|
int i;
|
|
|
|
DH *dh = NULL;
|
|
|
|
BIGNUM *pub_key = NULL, *priv_key = NULL, *p = NULL, *g = NULL;
|
|
|
|
isc_mem_t *mctx;
|
2020-02-12 13:59:18 +01:00
|
|
|
#define DST_RET(a) \
|
|
|
|
{ \
|
|
|
|
ret = a; \
|
|
|
|
goto err; \
|
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2009-09-03 04:09:58 +00:00
|
|
|
UNUSED(pub);
|
2000-06-02 18:57:51 +00:00
|
|
|
mctx = key->mctx;
|
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
/* read private key file */
|
2002-02-27 22:12:06 +00:00
|
|
|
ret = dst__privstruct_parse(key, DST_ALG_DH, lexer, mctx, &priv);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ret != ISC_R_SUCCESS) {
|
1999-09-27 16:55:45 +00:00
|
|
|
return (ret);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (key->external) {
|
2014-01-30 17:48:10 -08:00
|
|
|
DST_RET(DST_R_EXTERNALKEY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-30 17:48:10 -08:00
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
dh = DH_new();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dh == NULL) {
|
1999-10-20 22:14:15 +00:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_clear_flags(dh, DH_FLAG_CACHE_MONT_P);
|
2006-12-04 01:54:53 +00:00
|
|
|
key->keydata.dh = dh;
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2001-04-04 02:02:58 +00:00
|
|
|
for (i = 0; i < priv.nelements; i++) {
|
1999-09-27 16:55:45 +00:00
|
|
|
BIGNUM *bn;
|
2020-02-12 13:59:18 +01:00
|
|
|
bn = BN_bin2bn(priv.elements[i].data, priv.elements[i].length,
|
|
|
|
NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (bn == NULL) {
|
1999-10-20 22:14:15 +00:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
|
|
|
|
switch (priv.elements[i].tag) {
|
2020-02-12 13:59:18 +01:00
|
|
|
case TAG_DH_PRIME:
|
|
|
|
p = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DH_GENERATOR:
|
|
|
|
g = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DH_PRIVATE:
|
|
|
|
priv_key = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DH_PUBLIC:
|
|
|
|
pub_key = bn;
|
|
|
|
break;
|
2001-12-12 17:09:39 +00:00
|
|
|
}
|
1999-09-27 16:55:45 +00:00
|
|
|
}
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__privstruct_free(&priv, mctx);
|
2016-10-31 10:04:37 +11:00
|
|
|
DH_set0_key(dh, pub_key, priv_key);
|
|
|
|
DH_set0_pqg(dh, p, NULL, g);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2016-10-31 10:04:37 +11:00
|
|
|
key->key_size = BN_num_bits(p);
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
err:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (p != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(p);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (g != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(g);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (pub_key != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(pub_key);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (priv_key != NULL) {
|
2016-10-31 10:04:37 +11:00
|
|
|
BN_free(priv_key);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldh_destroy(key);
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__privstruct_free(&priv, mctx);
|
2017-09-19 16:16:45 +05:30
|
|
|
isc_safe_memwipe(&priv, sizeof(priv));
|
1999-09-27 16:55:45 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2001-11-06 23:10:34 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
openssldh_cleanup(void) {
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_free(bn2);
|
2018-10-24 10:02:49 -07:00
|
|
|
bn2 = NULL;
|
|
|
|
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_free(bn768);
|
2018-10-24 10:02:49 -07:00
|
|
|
bn768 = NULL;
|
|
|
|
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_free(bn1024);
|
2018-10-24 10:02:49 -07:00
|
|
|
bn1024 = NULL;
|
|
|
|
|
2015-05-28 14:41:21 +10:00
|
|
|
BN_free(bn1536);
|
2018-10-24 10:02:49 -07:00
|
|
|
bn1536 = NULL;
|
2001-11-06 20:47:59 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 23:36:14 +00:00
|
|
|
static dst_func_t openssldh_functions = {
|
2005-04-27 04:57:32 +00:00
|
|
|
NULL, /*%< createctx */
|
2014-01-14 15:40:56 -08:00
|
|
|
NULL, /*%< createctx2 */
|
2005-04-27 04:57:32 +00:00
|
|
|
NULL, /*%< destroyctx */
|
|
|
|
NULL, /*%< adddata */
|
|
|
|
NULL, /*%< openssldh_sign */
|
|
|
|
NULL, /*%< openssldh_verify */
|
2012-06-14 15:44:20 +10:00
|
|
|
NULL, /*%< openssldh_verify2 */
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldh_computesecret,
|
|
|
|
openssldh_compare,
|
|
|
|
openssldh_paramcompare,
|
|
|
|
openssldh_generate,
|
|
|
|
openssldh_isprivate,
|
|
|
|
openssldh_destroy,
|
|
|
|
openssldh_todns,
|
|
|
|
openssldh_fromdns,
|
|
|
|
openssldh_tofile,
|
2002-02-27 22:12:06 +00:00
|
|
|
openssldh_parse,
|
2001-11-06 20:47:59 +00:00
|
|
|
openssldh_cleanup,
|
2008-03-31 14:42:51 +00:00
|
|
|
NULL, /*%< fromlabel */
|
2011-01-10 05:32:04 +00:00
|
|
|
NULL, /*%< dump */
|
|
|
|
NULL, /*%< restore */
|
2000-06-02 18:57:51 +00:00
|
|
|
};
|
|
|
|
|
2000-06-06 21:58:16 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dst__openssldh_init(dst_func_t **funcp) {
|
2001-11-06 22:51:00 +00:00
|
|
|
REQUIRE(funcp != NULL);
|
|
|
|
if (*funcp == NULL) {
|
2018-05-02 14:17:40 +02:00
|
|
|
if (BN_hex2bn(&bn2, PRIME2) == 0 || bn2 == NULL) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (BN_hex2bn(&bn768, PRIME768) == 0 || bn768 == NULL) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (BN_hex2bn(&bn1024, PRIME1024) == 0 || bn1024 == NULL) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (BN_hex2bn(&bn1536, PRIME1536) == 0 || bn1536 == NULL) {
|
2015-05-28 14:41:21 +10:00
|
|
|
goto cleanup;
|
2018-05-02 14:17:40 +02:00
|
|
|
}
|
2001-11-06 22:51:00 +00:00
|
|
|
*funcp = &openssldh_functions;
|
|
|
|
}
|
2000-06-06 21:58:16 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2015-05-28 14:41:21 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (bn2 != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
BN_free(bn2);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (bn768 != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
BN_free(bn768);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (bn1024 != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
BN_free(bn1024);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (bn1536 != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
BN_free(bn1536);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-05-28 14:41:21 +10:00
|
|
|
return (ISC_R_NOMEMORY);
|
2000-06-06 21:58:16 +00:00
|
|
|
}
|