2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 02:28:55 +00:00
bind/bin/tests/dst/dst_test.c

280 lines
7.8 KiB
C
Raw Normal View History

/*
2000-02-03 23:08:31 +00:00
* Copyright (C) 1999, 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM 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.
*/
/* $Id: dst_test.c,v 1.29 2000/07/27 09:38:45 tale Exp $ */
2000-06-22 22:00:42 +00:00
#include <config.h>
#include <stdlib.h>
1999-07-13 02:05:01 +00:00
#include <unistd.h> /* XXX */
#include <isc/buffer.h>
2000-06-09 22:34:40 +00:00
#include <isc/entropy.h>
#include <isc/mem.h>
#include <isc/region.h>
#include <isc/string.h> /* Required for HP/UX (and others?) */
#include <dns/fixedname.h>
#include <dns/name.h>
2000-03-18 00:42:07 +00:00
#include <dns/result.h>
#include <dst/dst.h>
#include <dst/result.h>
char *current;
const char *tmp = "/tmp";
static void
2000-06-02 18:59:33 +00:00
use(dst_key_t *key, isc_mem_t *mctx) {
2000-03-06 20:04:15 +00:00
isc_result_t ret;
const char *data = "This is some data";
unsigned char sig[512];
isc_buffer_t databuf, sigbuf;
isc_region_t datareg, sigreg;
2000-06-02 18:59:33 +00:00
dst_context_t *ctx = NULL;
isc_buffer_init(&sigbuf, sig, sizeof(sig));
/*
* Advance 1 byte for fun.
*/
isc_buffer_add(&sigbuf, 1);
isc_buffer_init(&databuf, data, strlen(data));
isc_buffer_add(&databuf, strlen(data));
isc_buffer_usedregion(&databuf, &datareg);
2000-06-02 18:59:33 +00:00
ret = dst_context_create(key, mctx, &ctx);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
printf("contextcreate(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
return;
}
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &datareg);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
printf("adddata(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
dst_context_destroy(&ctx);
return;
}
2000-06-02 18:59:33 +00:00
ret = dst_context_sign(ctx, &sigbuf);
printf("sign(%d) returned: %s\n", dst_key_alg(key),
2000-03-06 20:04:15 +00:00
isc_result_totext(ret));
2000-06-02 18:59:33 +00:00
dst_context_destroy(&ctx);
isc_buffer_forward(&sigbuf, 1);
isc_buffer_remainingregion(&sigbuf, &sigreg);
2000-06-02 18:59:33 +00:00
ret = dst_context_create(key, mctx, &ctx);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
printf("contextcreate(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
return;
}
2000-06-02 18:59:33 +00:00
ret = dst_context_adddata(ctx, &datareg);
if (ret != ISC_R_SUCCESS) {
2000-06-02 18:59:33 +00:00
printf("adddata(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
dst_context_destroy(&ctx);
return;
}
2000-06-02 18:59:33 +00:00
ret = dst_context_verify(ctx, &sigreg);
printf("verify(%d) returned: %s\n", dst_key_alg(key),
2000-03-06 20:04:15 +00:00
isc_result_totext(ret));
2000-06-02 18:59:33 +00:00
dst_context_destroy(&ctx);
}
2000-04-05 22:21:17 +00:00
static void
dns(dst_key_t *key, isc_mem_t *mctx) {
unsigned char buffer1[2048];
unsigned char buffer2[2048];
isc_buffer_t buf1, buf2;
isc_region_t r1, r2;
dst_key_t *newkey = NULL;
isc_result_t ret;
isc_boolean_t match;
isc_buffer_init(&buf1, buffer1, sizeof(buffer1));
2000-04-05 22:21:17 +00:00
ret = dst_key_todns(key, &buf1);
printf("todns(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
if (ret != ISC_R_SUCCESS)
return;
ret = dst_key_fromdns(dst_key_name(key), &buf1, mctx, &newkey);
printf("fromdns(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
if (ret != ISC_R_SUCCESS)
return;
isc_buffer_init(&buf2, buffer2, sizeof(buffer2));
2000-04-05 22:21:17 +00:00
ret = dst_key_todns(newkey, &buf2);
printf("todns2(%d) returned: %s\n", dst_key_alg(key),
isc_result_totext(ret));
if (ret != ISC_R_SUCCESS)
return;
isc_buffer_usedregion(&buf1, &r1);
isc_buffer_usedregion(&buf2, &r2);
match = ISC_TF(r1.length == r2.length &&
memcmp(r1.base, r2.base, r1.length) == 0);
printf("compare(%d): %s\n", dst_key_alg(key),
match ? "true" : "false");
dst_key_free(&newkey);
2000-04-05 22:21:17 +00:00
}
static void
io(dns_name_t *name, int id, int alg, int type, isc_mem_t *mctx) {
dst_key_t *key = NULL;
2000-03-06 20:04:15 +00:00
isc_result_t ret;
2000-06-06 22:01:49 +00:00
ret = dst_key_fromfile(name, id, alg, type, current, mctx, &key);
2000-03-06 20:04:15 +00:00
printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
if (ret != 0)
return;
2000-06-06 22:01:49 +00:00
ret = dst_key_tofile(key, type, tmp);
2000-03-06 20:04:15 +00:00
printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
if (ret != 0)
return;
2000-06-02 18:59:33 +00:00
use(key, mctx);
2000-04-05 22:21:17 +00:00
dns(key, mctx);
dst_key_free(&key);
}
1999-10-27 21:26:08 +00:00
static void
dh(dns_name_t *name1, int id1, dns_name_t *name2, int id2, isc_mem_t *mctx) {
dst_key_t *key1 = NULL, *key2 = NULL;
2000-03-06 20:04:15 +00:00
isc_result_t ret;
1999-10-27 21:26:08 +00:00
isc_buffer_t b1, b2;
isc_region_t r1, r2;
unsigned char array1[1024], array2[1024];
int alg = DST_ALG_DH;
int type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE;
2000-06-06 22:01:49 +00:00
ret = dst_key_fromfile(name1, id1, alg, type, current, mctx, &key1);
2000-03-06 20:04:15 +00:00
printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
1999-10-27 21:26:08 +00:00
if (ret != 0)
return;
2000-06-06 22:01:49 +00:00
ret = dst_key_fromfile(name2, id2, alg, type, current, mctx, &key2);
2000-03-06 20:04:15 +00:00
printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
1999-10-27 21:26:08 +00:00
if (ret != 0)
return;
2000-06-06 22:01:49 +00:00
ret = dst_key_tofile(key1, type, tmp);
2000-03-06 20:04:15 +00:00
printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
1999-10-27 21:26:08 +00:00
if (ret != 0)
return;
2000-06-06 22:01:49 +00:00
ret = dst_key_tofile(key2, type, tmp);
2000-03-06 20:04:15 +00:00
printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
1999-10-27 21:26:08 +00:00
if (ret != 0)
return;
isc_buffer_init(&b1, array1, sizeof(array1));
2000-05-17 22:48:10 +00:00
ret = dst_key_computesecret(key1, key2, &b1);
2000-03-06 20:04:15 +00:00
printf("computesecret() returned: %s\n", isc_result_totext(ret));
1999-10-27 21:26:08 +00:00
if (ret != 0)
return;
isc_buffer_init(&b2, array2, sizeof(array2));
2000-05-17 22:48:10 +00:00
ret = dst_key_computesecret(key2, key1, &b2);
2000-03-06 20:04:15 +00:00
printf("computesecret() returned: %s\n", isc_result_totext(ret));
1999-10-27 21:26:08 +00:00
if (ret != 0)
return;
isc_buffer_usedregion(&b1, &r1);
isc_buffer_usedregion(&b2, &r2);
1999-10-27 21:26:08 +00:00
if (r1.length != r2.length || memcmp(r1.base, r2.base, r1.length) != 0)
{
int i;
printf("secrets don't match\n");
printf("secret 1: %d bytes\n", r1.length);
for (i = 0; i < (int) r1.length; i++)
printf("%02x ", r1.base[i]);
printf("\n");
printf("secret 2: %d bytes\n", r2.length);
for (i = 0; i < (int) r2.length; i++)
printf("%02x ", r2.base[i]);
printf("\n");
}
dst_key_free(&key1);
dst_key_free(&key2);
1999-10-27 21:26:08 +00:00
}
static void
generate(int alg, isc_mem_t *mctx) {
2000-03-06 20:04:15 +00:00
isc_result_t ret;
dst_key_t *key = NULL;
ret = dst_key_generate(dns_rootname, alg, 512, 0, 0, 0, mctx, &key);
2000-03-06 20:04:15 +00:00
printf("generate(%d) returned: %s\n", alg, isc_result_totext(ret));
if (ret != ISC_R_SUCCESS)
return;
1999-10-27 21:26:08 +00:00
if (alg != DST_ALG_DH)
2000-06-02 18:59:33 +00:00
use(key, mctx);
dst_key_free(&key);
}
int
main(void) {
isc_mem_t *mctx = NULL;
2000-06-09 22:34:40 +00:00
isc_entropy_t *ectx = NULL;
isc_buffer_t b;
dns_fixedname_t fname;
dns_name_t *name;
isc_mem_create(0, 0, &mctx);
current = isc_mem_get(mctx, 256);
getcwd(current, 256);
2000-03-06 20:04:15 +00:00
dns_result_register();
2000-06-09 22:34:40 +00:00
isc_entropy_create(mctx, &ectx);
isc_entropy_createfilesource(ectx, "/dev/random");
isc_entropy_createfilesource(ectx, "randomfile");
2000-06-09 22:34:40 +00:00
dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING|ISC_ENTROPY_GOODONLY);
2000-03-06 20:04:15 +00:00
dns_fixedname_init(&fname);
name = dns_fixedname_name(&fname);
isc_buffer_init(&b, "test.", 5);
2000-05-25 18:00:08 +00:00
isc_buffer_add(&b, 5);
dns_name_fromtext(name, &b, NULL, ISC_FALSE, NULL);
io(name, 6204, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
io(name, 54622, DST_ALG_RSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
io(name, 0, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
io(name, 0, DST_ALG_RSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
isc_buffer_init(&b, "dh.", 3);
2000-05-25 18:00:08 +00:00
isc_buffer_add(&b, 3);
dns_name_fromtext(name, &b, NULL, ISC_FALSE, NULL);
dh(name, 18088, name, 48443, mctx);
1999-10-27 21:26:08 +00:00
generate(DST_ALG_RSA, mctx);
1999-10-27 21:26:08 +00:00
generate(DST_ALG_DH, mctx);
generate(DST_ALG_DSA, mctx);
1999-09-02 15:56:33 +00:00
generate(DST_ALG_HMACMD5, mctx);
2000-06-06 22:01:49 +00:00
dst_lib_destroy();
2000-06-09 22:34:40 +00:00
isc_entropy_detach(&ectx);
2000-06-06 22:01:49 +00:00
isc_mem_put(mctx, current, 256);
/* isc_mem_stats(mctx, stdout);*/
isc_mem_destroy(&mctx);
return (0);
}