1999-07-12 20:08:42 +00:00
|
|
|
/*
|
2000-05-08 14:38:29 +00:00
|
|
|
* Portions Copyright (C) 1999, 2000 Internet Software Consortium.
|
2000-06-09 20:58:39 +00:00
|
|
|
* Portions Copyright (C) 1995-2000 by Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-05-08 14:38:29 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
1999-07-12 20:08:42 +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
|
|
|
*
|
2000-05-08 14:38:29 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
|
|
|
|
* NETWORK ASSOCIATES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
* SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR NETWORK
|
|
|
|
* ASSOCIATES 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-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Principal Author: Brian Wellington
|
2000-09-08 14:23:49 +00:00
|
|
|
* $Id: openssl_link.c,v 1.37 2000/09/08 14:23:46 bwelling Exp $
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-05-08 14:38:29 +00:00
|
|
|
#if defined(OPENSSL)
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-06-09 22:32:20 +00:00
|
|
|
#include <isc/entropy.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2000-06-07 17:22:31 +00:00
|
|
|
#include <isc/sha1.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-07-12 20:08:42 +00:00
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
#include <dst/result.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include "dst_internal.h"
|
|
|
|
#include "dst_parse.h"
|
|
|
|
|
|
|
|
#include <openssl/dsa.h>
|
2000-06-09 22:32:20 +00:00
|
|
|
#include <openssl/rand.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-09 23:31:55 +00:00
|
|
|
static RAND_METHOD *rm = NULL;
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static isc_result_t openssldsa_todns(const dst_key_t *key, isc_buffer_t *data);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
static isc_result_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_createctx(dst_key_t *key, dst_context_t *dctx) {
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_t *sha1ctx;
|
2000-05-02 03:54:17 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
UNUSED(key);
|
2000-05-02 03:54:17 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
|
|
|
|
isc_sha1_init(sha1ctx);
|
|
|
|
dctx->opaque = sha1ctx;
|
2000-06-02 18:57:51 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
static void
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_destroyctx(dst_context_t *dctx) {
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_t *sha1ctx = dctx->opaque;
|
2000-05-02 03:54:17 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
if (sha1ctx != NULL) {
|
|
|
|
isc_sha1_invalidate(sha1ctx);
|
|
|
|
isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
|
|
|
|
dctx->opaque = NULL;
|
2000-06-06 21:58:16 +00:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
2000-05-02 03:54:17 +00:00
|
|
|
|
|
|
|
static isc_result_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_t *sha1ctx = dctx->opaque;
|
2000-05-02 03:54:17 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_update(sha1ctx, data->base, data->length);
|
2000-06-02 18:57:51 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static int
|
|
|
|
BN_bn2bin_fixed(BIGNUM *bn, unsigned char *buf, int size) {
|
|
|
|
int bytes = size - BN_num_bytes(bn);
|
|
|
|
while (bytes-- > 0)
|
|
|
|
*buf++ = 0;
|
|
|
|
BN_bn2bin(bn, buf);
|
|
|
|
return (size);
|
2000-08-01 01:33:37 +00:00
|
|
|
}
|
2000-05-02 03:54:17 +00:00
|
|
|
|
|
|
|
static isc_result_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_t *sha1ctx = dctx->opaque;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
DSA *dsa = key->opaque;
|
2000-06-02 18:57:51 +00:00
|
|
|
DSA_SIG *dsasig;
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_region_t r;
|
|
|
|
unsigned char digest[ISC_SHA1_DIGESTLENGTH];
|
2000-05-02 03:54:17 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_buffer_availableregion(sig, &r);
|
2000-06-07 17:22:31 +00:00
|
|
|
if (r.length < ISC_SHA1_DIGESTLENGTH * 2 + 1)
|
2000-06-02 18:57:51 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_final(sha1ctx, digest);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
dsasig = DSA_do_sign(digest, ISC_SHA1_DIGESTLENGTH, dsa);
|
2000-06-02 18:57:51 +00:00
|
|
|
if (dsasig == NULL)
|
|
|
|
return (DST_R_SIGNFAILURE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
*r.base++ = (key->key_size - 512)/64;
|
2000-06-07 17:22:31 +00:00
|
|
|
BN_bn2bin_fixed(dsasig->r, r.base, ISC_SHA1_DIGESTLENGTH);
|
|
|
|
r.base += ISC_SHA1_DIGESTLENGTH;
|
|
|
|
BN_bn2bin_fixed(dsasig->s, r.base, ISC_SHA1_DIGESTLENGTH);
|
|
|
|
r.base += ISC_SHA1_DIGESTLENGTH;
|
2000-06-02 18:57:51 +00:00
|
|
|
DSA_SIG_free(dsasig);
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_buffer_add(sig, ISC_SHA1_DIGESTLENGTH * 2 + 1);
|
2000-06-02 18:57:51 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static isc_result_t
|
|
|
|
openssldsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_t *sha1ctx = dctx->opaque;
|
|
|
|
dst_key_t *key = dctx->key;
|
|
|
|
DSA *dsa = key->opaque;
|
2000-06-02 18:57:51 +00:00
|
|
|
DSA_SIG *dsasig;
|
2000-06-07 17:22:31 +00:00
|
|
|
int status = 0;
|
|
|
|
unsigned char digest[ISC_SHA1_DIGESTLENGTH];
|
2000-06-02 18:57:51 +00:00
|
|
|
unsigned char *cp = sig->base;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_sha1_final(sha1ctx, digest);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
if (sig->length < 2 * ISC_SHA1_DIGESTLENGTH + 1)
|
2000-06-02 18:57:51 +00:00
|
|
|
return (DST_R_VERIFYFAILURE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
cp++; /* Skip T */
|
|
|
|
dsasig = DSA_SIG_new();
|
2000-06-07 17:22:31 +00:00
|
|
|
dsasig->r = BN_bin2bn(cp, ISC_SHA1_DIGESTLENGTH, NULL);
|
|
|
|
cp += ISC_SHA1_DIGESTLENGTH;
|
|
|
|
dsasig->s = BN_bin2bn(cp, ISC_SHA1_DIGESTLENGTH, NULL);
|
|
|
|
cp += ISC_SHA1_DIGESTLENGTH;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
status = DSA_do_verify(digest, ISC_SHA1_DIGESTLENGTH, dsasig, dsa);
|
2000-06-02 18:57:51 +00:00
|
|
|
DSA_SIG_free(dsasig);
|
|
|
|
if (status == 0)
|
|
|
|
return (DST_R_VERIFYFAILURE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
openssldsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
int status;
|
|
|
|
DSA *dsa1, *dsa2;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
dsa1 = (DSA *) key1->opaque;
|
|
|
|
dsa2 = (DSA *) key2->opaque;
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
if (dsa1 == NULL && dsa2 == NULL)
|
2000-06-02 18:57:51 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
else if (dsa1 == NULL || dsa2 == NULL)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
|
|
|
status = BN_cmp(dsa1->p, dsa2->p) ||
|
|
|
|
BN_cmp(dsa1->q, dsa2->q) ||
|
|
|
|
BN_cmp(dsa1->g, dsa2->g) ||
|
|
|
|
BN_cmp(dsa1->pub_key, dsa2->pub_key);
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
|
|
|
if (dsa1->priv_key != NULL || dsa2->priv_key != NULL) {
|
|
|
|
if (dsa1->priv_key == NULL || dsa2->priv_key == NULL)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
if (BN_cmp(dsa1->priv_key, dsa2->priv_key))
|
|
|
|
return (ISC_FALSE);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static isc_result_t
|
2000-06-12 18:05:15 +00:00
|
|
|
openssldsa_generate(dst_key_t *key, int unused) {
|
2000-06-02 18:57:51 +00:00
|
|
|
DSA *dsa;
|
|
|
|
unsigned char dns_array[DST_KEY_MAXSIZE];
|
2000-06-07 17:22:31 +00:00
|
|
|
unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
|
2000-06-09 22:32:20 +00:00
|
|
|
isc_buffer_t dns;
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_region_t r;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
UNUSED(unused);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-12 18:05:15 +00:00
|
|
|
result = dst__entropy_getdata(rand_array, sizeof(rand_array),
|
|
|
|
ISC_FALSE);
|
2000-06-02 18:57:51 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
dsa = DSA_generate_parameters(key->key_size, rand_array,
|
2000-06-07 17:22:31 +00:00
|
|
|
ISC_SHA1_DIGESTLENGTH, NULL, NULL,
|
2000-06-02 18:57:51 +00:00
|
|
|
NULL, NULL);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
if (dsa == NULL)
|
2000-08-10 22:28:36 +00:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
if (DSA_generate_key(dsa) == 0) {
|
|
|
|
DSA_free(dsa);
|
2000-08-10 22:28:36 +00:00
|
|
|
return (DST_R_OPENSSLFAILURE);
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
key->opaque = dsa;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_buffer_init(&dns, dns_array, sizeof(dns_array));
|
|
|
|
result = openssldsa_todns(key, &dns);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
DSA_free(dsa);
|
|
|
|
return (result);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_buffer_usedregion(&dns, &r);
|
2000-09-08 14:23:49 +00:00
|
|
|
key->key_id = dst_region_computeid(&r, key->key_alg);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
static isc_boolean_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_isprivate(const dst_key_t *key) {
|
1999-09-23 20:54:38 +00:00
|
|
|
DSA *dsa = (DSA *) key->opaque;
|
1999-10-29 12:56:58 +00:00
|
|
|
return (ISC_TF(dsa != NULL && dsa->priv_key != NULL));
|
1999-09-23 20:54:38 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
static void
|
|
|
|
openssldsa_destroy(dst_key_t *key) {
|
|
|
|
DSA *dsa = key->opaque;
|
|
|
|
DSA_free(dsa);
|
2000-08-16 00:30:56 +00:00
|
|
|
key->opaque = NULL;
|
2000-06-02 18:57:51 +00:00
|
|
|
}
|
1999-09-23 20:54:38 +00:00
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_todns(const dst_key_t *key, isc_buffer_t *data) {
|
1999-07-12 20:08:42 +00:00
|
|
|
DSA *dsa;
|
|
|
|
isc_region_t r;
|
|
|
|
int dnslen;
|
2000-04-05 22:22:51 +00:00
|
|
|
unsigned int t, p_bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
REQUIRE(key->opaque != NULL);
|
|
|
|
|
|
|
|
dsa = (DSA *) key->opaque;
|
|
|
|
|
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-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
t = (BN_num_bytes(dsa->p) - 64) / 8;
|
|
|
|
if (t > 8)
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
2000-04-05 22:22:51 +00:00
|
|
|
p_bytes = 64 + 8 * t;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
dnslen = 1 + (key->key_size * 3)/8 + ISC_SHA1_DIGESTLENGTH;
|
1999-07-12 20:08:42 +00:00
|
|
|
if (r.length < (unsigned int) dnslen)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
*r.base++ = t;
|
2000-06-07 17:22:31 +00:00
|
|
|
BN_bn2bin_fixed(dsa->q, r.base, ISC_SHA1_DIGESTLENGTH);
|
|
|
|
r.base += ISC_SHA1_DIGESTLENGTH;
|
1999-07-12 20:08:42 +00:00
|
|
|
BN_bn2bin_fixed(dsa->p, r.base, key->key_size/8);
|
2000-04-05 22:22:51 +00:00
|
|
|
r.base += p_bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
BN_bn2bin_fixed(dsa->g, r.base, key->key_size/8);
|
2000-04-05 22:22:51 +00:00
|
|
|
r.base += p_bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
BN_bn2bin_fixed(dsa->pub_key, r.base, key->key_size/8);
|
2000-04-05 22:22:51 +00:00
|
|
|
r.base += p_bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
isc_buffer_add(data, dnslen);
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
|
1999-07-12 20:08:42 +00:00
|
|
|
DSA *dsa;
|
|
|
|
isc_region_t r;
|
|
|
|
unsigned int t, p_bytes;
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_mem_t *mctx = key->mctx;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-05-11 22:47:00 +00:00
|
|
|
UNUSED(mctx);
|
1999-07-12 20:08:42 +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);
|
1999-07-12 20:08:42 +00:00
|
|
|
if (r.length == 0)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
dsa = DSA_new();
|
|
|
|
if (dsa == NULL)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
t = (unsigned int) *r.base++;
|
|
|
|
if (t > 8) {
|
|
|
|
DSA_free(dsa);
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
p_bytes = 64 + 8 * t;
|
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
if (r.length < 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes) {
|
1999-07-12 20:08:42 +00:00
|
|
|
DSA_free(dsa);
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_INVALIDPUBLICKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
dsa->q = BN_bin2bn(r.base, ISC_SHA1_DIGESTLENGTH, NULL);
|
|
|
|
r.base += ISC_SHA1_DIGESTLENGTH;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
dsa->p = BN_bin2bn(r.base, p_bytes, NULL);
|
2000-04-05 22:22:51 +00:00
|
|
|
r.base += p_bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
dsa->g = BN_bin2bn(r.base, p_bytes, NULL);
|
2000-04-05 22:22:51 +00:00
|
|
|
r.base += p_bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
dsa->pub_key = BN_bin2bn(r.base, p_bytes, NULL);
|
2000-04-05 22:22:51 +00:00
|
|
|
r.base += p_bytes;
|
1999-07-12 20:08:42 +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);
|
2000-08-08 16:13:40 +00:00
|
|
|
r.length = 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes;
|
2000-09-08 14:23:49 +00:00
|
|
|
key->key_id = dst_region_computeid(&r, key->key_alg);
|
1999-07-12 20:08:42 +00:00
|
|
|
key->key_size = p_bytes * 8;
|
|
|
|
|
2000-06-07 17:22:31 +00:00
|
|
|
isc_buffer_forward(data, 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
key->opaque = (void *) dsa;
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
2000-06-06 21:58:16 +00:00
|
|
|
openssldsa_tofile(const dst_key_t *key, const char *directory) {
|
1999-07-12 20:08:42 +00:00
|
|
|
int cnt = 0;
|
|
|
|
DSA *dsa;
|
|
|
|
dst_private_t priv;
|
|
|
|
unsigned char bufs[5][128];
|
|
|
|
|
|
|
|
if (key->opaque == NULL)
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_NULLKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
dsa = (DSA *) key->opaque;
|
|
|
|
|
|
|
|
priv.elements[cnt].tag = TAG_DSA_PRIME;
|
|
|
|
priv.elements[cnt].length = BN_num_bytes(dsa->p);
|
|
|
|
BN_bn2bin(dsa->p, bufs[cnt]);
|
1999-08-27 21:12:28 +00:00
|
|
|
priv.elements[cnt].data = bufs[cnt];
|
|
|
|
cnt++;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
priv.elements[cnt].tag = TAG_DSA_SUBPRIME;
|
|
|
|
priv.elements[cnt].length = BN_num_bytes(dsa->q);
|
|
|
|
BN_bn2bin(dsa->q, bufs[cnt]);
|
1999-08-27 21:12:28 +00:00
|
|
|
priv.elements[cnt].data = bufs[cnt];
|
|
|
|
cnt++;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
priv.elements[cnt].tag = TAG_DSA_BASE;
|
|
|
|
priv.elements[cnt].length = BN_num_bytes(dsa->g);
|
|
|
|
BN_bn2bin(dsa->g, bufs[cnt]);
|
1999-08-27 21:12:28 +00:00
|
|
|
priv.elements[cnt].data = bufs[cnt];
|
|
|
|
cnt++;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
priv.elements[cnt].tag = TAG_DSA_PRIVATE;
|
|
|
|
priv.elements[cnt].length = BN_num_bytes(dsa->priv_key);
|
|
|
|
BN_bn2bin(dsa->priv_key, bufs[cnt]);
|
1999-08-27 21:12:28 +00:00
|
|
|
priv.elements[cnt].data = bufs[cnt];
|
|
|
|
cnt++;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
priv.elements[cnt].tag = TAG_DSA_PUBLIC;
|
|
|
|
priv.elements[cnt].length = BN_num_bytes(dsa->pub_key);
|
|
|
|
BN_bn2bin(dsa->pub_key, bufs[cnt]);
|
1999-08-27 21:12:28 +00:00
|
|
|
priv.elements[cnt].data = bufs[cnt];
|
|
|
|
cnt++;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
priv.nelements = cnt;
|
2000-06-06 21:58:16 +00:00
|
|
|
return (dst__privstruct_writefile(key, &priv, directory));
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
static isc_result_t
|
2000-06-06 21:58:16 +00:00
|
|
|
openssldsa_fromfile(dst_key_t *key, const isc_uint16_t id, const char *filename)
|
|
|
|
{
|
1999-07-12 20:08:42 +00:00
|
|
|
dst_private_t priv;
|
2000-03-06 20:06:01 +00:00
|
|
|
isc_result_t ret;
|
1999-07-12 20:08:42 +00:00
|
|
|
isc_buffer_t dns;
|
|
|
|
isc_region_t r;
|
|
|
|
unsigned char dns_array[1024];
|
|
|
|
int i;
|
|
|
|
DSA *dsa = NULL;
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_mem_t *mctx = key->mctx;
|
1999-07-12 20:08:42 +00:00
|
|
|
#define DST_RET(a) {ret = a; goto err;}
|
|
|
|
|
|
|
|
/* read private key file */
|
2000-06-06 21:58:16 +00:00
|
|
|
ret = dst__privstruct_parsefile(key, id, filename, mctx, &priv);
|
1999-10-20 22:14:15 +00:00
|
|
|
if (ret != ISC_R_SUCCESS)
|
1999-07-12 20:08:42 +00:00
|
|
|
return (ret);
|
|
|
|
|
|
|
|
dsa = DSA_new();
|
|
|
|
if (dsa == NULL)
|
1999-10-20 22:14:15 +00:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
1999-07-12 20:08:42 +00:00
|
|
|
key->opaque = dsa;
|
|
|
|
|
|
|
|
for (i=0; i < priv.nelements; i++) {
|
|
|
|
BIGNUM *bn;
|
|
|
|
bn = BN_bin2bn(priv.elements[i].data,
|
|
|
|
priv.elements[i].length, NULL);
|
|
|
|
if (bn == NULL)
|
1999-10-20 22:14:15 +00:00
|
|
|
DST_RET(ISC_R_NOMEMORY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
switch (priv.elements[i].tag) {
|
|
|
|
case TAG_DSA_PRIME:
|
|
|
|
dsa->p = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DSA_SUBPRIME:
|
|
|
|
dsa->q = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DSA_BASE:
|
|
|
|
dsa->g = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DSA_PRIVATE:
|
|
|
|
dsa->priv_key = bn;
|
|
|
|
break;
|
|
|
|
case TAG_DSA_PUBLIC:
|
|
|
|
dsa->pub_key = bn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__privstruct_free(&priv, mctx);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
key->key_size = BN_num_bits(dsa->p);
|
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_init(&dns, dns_array, sizeof(dns_array));
|
2000-06-02 18:57:51 +00:00
|
|
|
ret = openssldsa_todns(key, &dns);
|
1999-10-20 22:14:15 +00:00
|
|
|
if (ret != ISC_R_SUCCESS)
|
1999-07-12 20:08:42 +00:00
|
|
|
DST_RET(ret);
|
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_usedregion(&dns, &r);
|
2000-09-08 14:23:49 +00:00
|
|
|
key->key_id = dst_region_computeid(&r, key->key_alg);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
if (key->key_id != id)
|
1999-09-01 18:56:19 +00:00
|
|
|
DST_RET(DST_R_INVALIDPRIVATEKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
err:
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_destroy(key);
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__privstruct_free(&priv, mctx);
|
1999-07-12 20:08:42 +00:00
|
|
|
memset(&priv, 0, sizeof(priv));
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2000-06-02 23:36:14 +00:00
|
|
|
static dst_func_t openssldsa_functions = {
|
2000-06-02 18:57:51 +00:00
|
|
|
openssldsa_createctx,
|
|
|
|
openssldsa_destroyctx,
|
|
|
|
openssldsa_adddata,
|
|
|
|
openssldsa_sign,
|
|
|
|
openssldsa_verify,
|
|
|
|
NULL, /* computesecret */
|
|
|
|
openssldsa_compare,
|
|
|
|
NULL, /* paramcompare */
|
|
|
|
openssldsa_generate,
|
|
|
|
openssldsa_isprivate,
|
|
|
|
openssldsa_destroy,
|
|
|
|
openssldsa_todns,
|
|
|
|
openssldsa_fromdns,
|
|
|
|
openssldsa_tofile,
|
|
|
|
openssldsa_fromfile,
|
|
|
|
};
|
2000-05-11 22:48:12 +00:00
|
|
|
|
2000-06-06 21:58:16 +00:00
|
|
|
isc_result_t
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__openssldsa_init(dst_func_t **funcp) {
|
2000-06-02 18:57:51 +00:00
|
|
|
REQUIRE(funcp != NULL && *funcp == NULL);
|
|
|
|
*funcp = &openssldsa_functions;
|
2000-06-06 21:58:16 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dst__openssldsa_destroy(void) {
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
2000-06-09 22:32:20 +00:00
|
|
|
static int
|
|
|
|
entropy_get(unsigned char *buf, int num) {
|
|
|
|
isc_result_t result;
|
|
|
|
if (num < 0)
|
|
|
|
return (-1);
|
2000-06-09 23:31:55 +00:00
|
|
|
result = dst__entropy_getdata(buf, (unsigned int) num, ISC_FALSE);
|
|
|
|
return (result == ISC_R_SUCCESS ? num : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
entropy_getpseudo(unsigned char *buf, int num) {
|
|
|
|
isc_result_t result;
|
|
|
|
if (num < 0)
|
|
|
|
return (-1);
|
|
|
|
result = dst__entropy_getdata(buf, (unsigned int) num, ISC_TRUE);
|
|
|
|
return (result == ISC_R_SUCCESS ? num : -1);
|
2000-06-09 22:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
entropy_add(const void *buf, int num, double entropy) {
|
|
|
|
/*
|
|
|
|
* Do nothing. The only call to this provides no useful data anyway.
|
|
|
|
*/
|
|
|
|
UNUSED(buf);
|
|
|
|
UNUSED(num);
|
|
|
|
UNUSED(entropy);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dst__openssl_init(void) {
|
|
|
|
CRYPTO_set_mem_functions(dst__mem_alloc, dst__mem_realloc,
|
|
|
|
dst__mem_free);
|
2000-06-09 23:31:55 +00:00
|
|
|
rm = dst__mem_alloc(sizeof(RAND_METHOD));
|
|
|
|
if (rm == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
rm->seed = NULL;
|
|
|
|
rm->bytes = entropy_get;
|
|
|
|
rm->cleanup = NULL;
|
|
|
|
rm->add = entropy_add;
|
|
|
|
rm->pseudorand = entropy_getpseudo;
|
|
|
|
rm->status = NULL;
|
|
|
|
RAND_set_rand_method(rm);
|
2000-06-09 22:32:20 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-06-09 23:31:55 +00:00
|
|
|
void
|
|
|
|
dst__openssl_destroy(void) {
|
|
|
|
dst__mem_free(rm);
|
|
|
|
}
|
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
#endif /* OPENSSL */
|