t_dst.c revision 0942b8e0d660895ae96ffbd447fd0290fe60539c
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews/*
503ffdad3bc62e6458e4b60e329aa448ce1803a1Tinderbox User * Copyright (C) 1999, 2000 Internet Software Consortium.
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews *
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * Permission to use, copy, modify, and distribute this software for any
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * purpose with or without fee is hereby granted, provided that the above
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * copyright notice and this permission notice appear in all copies.
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews *
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews * SOFTWARE.
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <config.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <ctype.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <sys/types.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <dirent.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <errno.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <fcntl.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <limits.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <stdio.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <stdlib.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <string.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <sys/stat.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <unistd.h> /* XXX */
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <isc/assertions.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <isc/error.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <isc/boolean.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <isc/region.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <isc/mem.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <isc/result.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <dst/dst.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <dst/result.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews#include <tests/t_api.h>
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstatic void t1(void);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstatic void t2(void);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
e0a30050c8516a3d54a4f8dcdd88435704a8a3edMark Andrews/*
e0a30050c8516a3d54a4f8dcdd88435704a8a3edMark Andrews * adapted from the original dst_test.c program
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstatic void
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewscleandir(char *path) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews DIR *dirp;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews struct dirent *pe;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char fullname[PATH_MAX + 1];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dirp = opendir(path);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (dirp == NULL) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("opendir(%s) failed %d\n", path, opendir);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews while ((pe = readdir(dirp)) != NULL) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (! strcmp(pe->d_name, "."))
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews continue;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (! strcmp(pe->d_name, ".."))
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews continue;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews strcpy(fullname, path);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews strcat(fullname, "/");
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews strcat(fullname, pe->d_name);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (remove(fullname)) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("remove(%s) failed %d\n", fullname, errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews (void) closedir(dirp);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (rmdir(path)) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("rmdir(%s) failed %d\n", path, errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews}
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstatic void
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsuse(dst_key_t *key, isc_result_t exp_result, int *nfails) {
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_result_t ret;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char *data = "This is some data";
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews unsigned char sig[512];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_t databuf, sigbuf;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_region_t datareg, sigreg;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_init(&sigbuf, sig, sizeof(sig), ISC_BUFFERTYPE_BINARY);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_init(&databuf, data, strlen(data), ISC_BUFFERTYPE_TEXT);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_add(&databuf, strlen(data));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_used(&databuf, &datareg);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_sign(DST_SIGMODE_ALL, key, NULL, &datareg, &sigbuf);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != exp_result) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_sign(%d) returned (%s) expected (%s)\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_key_alg(key), dst_result_totext(ret),
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_result_totext(exp_result));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_remaining(&sigbuf, &sigreg);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_verify(DST_SIGMODE_ALL, key, NULL, &datareg, &sigreg);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != exp_result) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_verify(%d) returned (%s) expected (%s)\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_key_alg(key), dst_result_totext(ret),
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_result_totext(exp_result));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews}
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstatic void
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsdh(char *name1, int id1, char *name2, int id2, isc_mem_t *mctx,
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_result_t exp_result, int *nfails, int *nprobs)
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman{
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_key_t *key1, *key2;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_result_t ret;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews int rval;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char current[PATH_MAX + 1];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char tmp[PATH_MAX + 1];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char *p;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews int alg = DST_ALG_DH;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews int type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews unsigned char array1[1024], array2[1024];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_t b1, b2;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_region_t r1, r2;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews exp_result = exp_result; /* unused */
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman p = getcwd(current, PATH_MAX);;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (p == NULL) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("getcwd failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_key_fromfile(name1, id1, alg, type, mctx, &key1);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != ISC_R_SUCCESS) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_key_fromfile(%d) returned: %s\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews alg, dst_result_totext(ret));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_key_fromfile(name2, id2, alg, type, mctx, &key2);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != ISC_R_SUCCESS) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_key_fromfile(%d) returned: %s\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews alg, dst_result_totext(ret));
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews p = tmpnam(tmp);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (p == NULL) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("tmpnam failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews rval = mkdir(tmp, S_IRWXU | S_IRWXG );
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (rval != 0) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("mkdir failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (chdir(tmp)) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("chdir failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews (void) rmdir(tmp);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_key_tofile(key1, type);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != 0) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_key_tofile(%d) returned: %s\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews alg, dst_result_totext(ret));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews (void) chdir(current);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_key_tofile(key2, type);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != 0) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_key_tofile(%d) returned: %s\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews alg, dst_result_totext(ret));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews (void) chdir(current);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (chdir(current)) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("chdir failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews cleandir(tmp);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_init(&b1, array1, sizeof(array1), ISC_BUFFERTYPE_BINARY);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_computesecret(key1, key2, &b1);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != 0) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_computesecret() returned: %s\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_result_totext(ret));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_init(&b2, array2, sizeof(array2), ISC_BUFFERTYPE_BINARY);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_computesecret(key2, key1, &b2);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != 0) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_computesecret() returned: %s\n",
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_result_totext(ret));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_used(&b1, &r1);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_buffer_used(&b2, &r2);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (r1.length != r2.length || memcmp(r1.base, r2.base, r1.length) != 0)
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("computed secrets don't match\n");
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_key_free(key1);
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman dst_key_free(key2);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews}
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsstatic void
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrewsio(char *name, int id, int alg, int type, isc_mem_t *mctx, isc_result_t exp_result,
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews int *nfails, int *nprobs) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews dst_key_t *key;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews isc_result_t ret;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews int rval;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char current[PATH_MAX + 1];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char tmp[PATH_MAX + 1];
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews char *p;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews p = getcwd(current, PATH_MAX);;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (p == NULL) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("getcwd failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ret = dst_key_fromfile(name, id, alg, type, mctx, &key);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (ret != ISC_R_SUCCESS) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("dst_key_fromfile(%d) returned: %s\n", alg, dst_result_totext(ret));
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nfails;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews p = tmpnam(tmp);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (p == NULL) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("tmpnam failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews rval = mkdir(tmp, S_IRWXU | S_IRWXG );
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (rval != 0) {
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews t_info("mkdir failed %d\n", errno);
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews ++*nprobs;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews return;
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews }
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews
f1c1aab2c9e720399d66d8db5f40515d47c45ecfMark Andrews if (chdir(tmp)) {
t_info("chdir failed %d\n", errno);
(void) rmdir(tmp);
++*nprobs;
return;
}
ret = dst_key_tofile(key, type);
if (ret != 0) {
t_info("dst_key_tofile(%d) returned: %s\n", alg, dst_result_totext(ret));
(void) chdir(current);
++*nfails;
return;
}
if (dst_key_alg(key) != DST_ALG_DH)
use(key, exp_result, nfails);
if (chdir(current)) {
t_info("chdir failed %d\n", errno);
++*nprobs;
return;
}
cleandir(tmp);
dst_key_free(key);
}
static void
generate(int alg, isc_mem_t *mctx, int size, int *nfails) {
isc_result_t ret;
dst_key_t *key;
ret = dst_key_generate("test.", alg, size, 0, 0, 0, mctx, &key);
if (ret != ISC_R_SUCCESS) {
t_info("dst_key_generate(%d) returned: %s\n", alg, dst_result_totext(ret));
++*nfails;
return;
}
if (alg != DST_ALG_DH)
use(key, ISC_R_SUCCESS, nfails);
dst_key_free(key);
}
#define DBUFSIZ 25
static void
get_random(int *nfails) {
unsigned char data1[DBUFSIZ];
unsigned char data2[DBUFSIZ];
isc_buffer_t databuf1;
isc_buffer_t databuf2;
isc_result_t ret;
unsigned int i;
isc_buffer_init(&databuf1, data1, sizeof(data1), ISC_BUFFERTYPE_BINARY);
ret = dst_random_get(sizeof(data1), &databuf1);
if (ret != ISC_R_SUCCESS) {
t_info("random() returned: %s\n", dst_result_totext(ret));
++*nfails;
return;
}
isc_buffer_init(&databuf2, data2, sizeof(data2), ISC_BUFFERTYPE_BINARY);
ret = dst_random_get(sizeof(data2), &databuf2);
if (ret != ISC_R_SUCCESS) {
t_info("random() returned: %s\n", dst_result_totext(ret));
++*nfails;
return;
}
/* weak test, but better than nought */
if (memcmp(data1, data2, DBUFSIZ) == 0) {
t_info("data not random\n");
++*nfails;
}
if (T_debug) {
for (i = 0; i < sizeof(data1); i++)
t_info("data1[%d]: %02x ", i, data1[i]);
for (i = 0; i < sizeof(data2); i++)
t_info("data2[%d]: %02x ", i, data2[i]);
}
}
static char *a1 =
"the dst module provides the capability to "
"generate, store and retrieve public and private keys, "
"sign and verify data using the RSA, DSA and MD5 algorithms, "
"compute Diffie-Hellman shared secrets, "
"and generate random number sequences.";
static void
t1() {
isc_mem_t *mctx;
int nfails;
int nprobs;
int result;
isc_result_t isc_result;
t_assert("dst", 1, T_REQUIRED, a1);
nfails = 0;
nprobs = 0;
mctx = NULL;
isc_result = isc_mem_create(0, 0, &mctx);
if (isc_result != ISC_R_SUCCESS) {
t_info("isc_mem_create failed %d\n", isc_result_totext(isc_result));
t_result(T_UNRESOLVED);
return;
}
t_info("testing use of stored keys [1]\n");
io("test.", 6204, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
mctx, ISC_R_SUCCESS, &nfails, &nprobs);
t_info("testing use of stored keys [2]\n");
io("test.", 54622, DST_ALG_RSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
mctx, ISC_R_SUCCESS, &nfails, &nprobs);
t_info("testing use of stored keys [3]\n");
io("test.", 0, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
mctx, DST_R_NULLKEY, &nfails, &nprobs);
t_info("testing use of stored keys [4]\n");
io("test.", 0, DST_ALG_RSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
mctx, DST_R_NULLKEY, &nfails, &nprobs);
dh("dh.", 18088, "dh.", 48443, mctx, ISC_R_SUCCESS, &nfails, &nprobs);
t_info("testing use of generated keys\n");
generate(DST_ALG_RSA, mctx, 512, &nfails);
generate(DST_ALG_DSA, mctx, 512, &nfails);
generate(DST_ALG_DH, mctx, 512, &nfails);
generate(DST_ALG_DH, mctx, 768, &nfails); /* this one uses a constant */
generate(DST_ALG_HMACMD5, mctx, 512, &nfails);
t_info("testing random number sequence generation\n");
get_random(&nfails);
isc_mem_destroy(&mctx);
result = T_UNRESOLVED;
if ((nfails == 0) && (nprobs == 0))
result = T_PASS;
else if (nfails)
result = T_FAIL;
t_result(result);
}
#define T_SIGMAX 512
#undef NEWSIG /* define NEWSIG to generate the original signature file */
#ifdef NEWSIG
/* write a sig in buf to file at path */
static int
sig_tofile(char *path, isc_buffer_t *buf) {
int rval;
int fd;
int len;
int nprobs;
int cnt;
unsigned char c;
unsigned char val;
cnt = 0;
nprobs = 0;
len = buf->used - buf->current;
t_info("buf: current %d used %d len %d\n", buf->current, buf->used, len);
fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU|S_IRWXO|S_IRWXG);
if (fd < 0) {
t_info("open %s failed %d\n", path, errno);
return(1);
}
while (len) {
c = (unsigned char) isc_buffer_getuint8(buf);
val = ((c >> 4 ) & 0x0f);
if ((0 <= val) && (val <= 9))
val = '0' + val;
else
val = 'A' + val - 10;
rval = write(fd, &val, 1);
if (rval != 1) {
++nprobs;
t_info("write failed %d %d\n", rval, errno);
break;
}
val = (c & 0x0f);
if ((0 <= val) && (val <= 9))
val = '0' + val;
else
val = 'A' + val - 10;
rval = write(fd, &val, 1);
if (rval != 1) {
++nprobs;
t_info("write failed %d %d\n", rval, errno);
break;
}
--len;
++cnt;
if ((cnt % 16) == 0) {
val = '\n';
rval = write(fd, &val, 1);
if (rval != 1) {
++nprobs;
t_info("write failed %d %d\n", rval, errno);
break;
}
}
}
val = '\n';
rval = write(fd, &val, 1);
if (rval != 1) {
++nprobs;
t_info("write failed %d %d\n", rval, errno);
}
(void) close(fd);
return(nprobs);
}
#endif /* NEWSIG */
/* read sig in file at path to buf */
static int
sig_fromfile(char *path, isc_buffer_t *iscbuf) {
int rval;
int len;
int fd;
unsigned char val;
struct stat sb;
char *p;
char *buf;
rval = stat(path, &sb);
if (rval != 0) {
t_info("stat %s failed, errno == %d\n", path, errno);
return(1);
}
buf = (char *) malloc((sb.st_size + 1) * sizeof(unsigned char));
if (buf == NULL) {
t_info("malloc failed, errno == %d\n", errno);
return(1);
}
fd = open(path, O_RDONLY);
if (fd < 0) {
t_info("open failed, errno == %d\n", errno);
(void) free(buf);
return(1);
}
len = sb.st_size;
p = buf;
while (len) {
rval = read(fd, p, len);
if (rval > 0) {
len -= rval;
p += rval;
}
else {
t_info("read failed %d, errno == %d\n", rval, errno);
(void) free(buf);
(void) close(fd);
return(1);
}
}
close(fd);
p = buf;
len = sb.st_size;
while(len) {
if (*p == '\n') {
++p;
--len;
continue;
}
if (('0' <= *p) && (*p <= '9'))
val = *p - '0';
else
val = *p - 'A' + 10;
++p;
val <<= 4;
--len;
if (('0' <= *p) && (*p <= '9'))
val |= (*p - '0');
else
val |= (*p - 'A' + 10);
++p;
--len;
isc_buffer_putuint8(iscbuf, val);
}
(void) free(buf);
return(0);
}
static void
t2_sigchk(char *datapath, char *sigpath, char *keyname,
int id, int alg, int type,
isc_mem_t *mctx, char *expected_result,
int *nfails, int *nprobs) {
int rval;
int len;
int fd;
int exp_res;
dst_key_t *key;
unsigned char sig[T_SIGMAX];
unsigned char *p;
unsigned char *data;
struct stat sb;
isc_result_t isc_result;
isc_buffer_t databuf;
isc_buffer_t sigbuf;
isc_region_t datareg;
isc_region_t sigreg;
/* read data from file in a form usable by dst_verify */
rval = stat(datapath, &sb);
if (rval != 0) {
t_info("t2_sigchk: stat (%s) failed %d\n", datapath, errno);
++*nprobs;
return;
}
data = (unsigned char *) malloc(sb.st_size * sizeof(char));
if (data == NULL) {
t_info("t2_sigchk: malloc failed %d\n", errno);
++*nprobs;
return;
}
fd = open(datapath, O_RDONLY);
if (fd < 0) {
t_info("t2_sigchk: open failed %d\n", errno);
(void) free(data);
++*nprobs;
return;
}
p = data;
len = sb.st_size;
do {
rval = read(fd, p, len);
if (rval > 0) {
len -= rval;
p += rval;
}
} while (len);
(void) close(fd);
/* read key from file in a form usable by dst_verify */
isc_result = dst_key_fromfile(keyname, id, alg, type, mctx, &key);
if (isc_result != ISC_R_SUCCESS) {
t_info("dst_key_fromfile failed %s\n",
isc_result_totext(isc_result));
(void) free(data);
++*nprobs;
return;
}
isc_buffer_init(&databuf, data, sb.st_size, ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&databuf, sb.st_size);
isc_buffer_used(&databuf, &datareg);
#ifdef NEWSIG
/*
* if we're generating a signature for the first time,
* sign the data and save the signature to a file
*/
memset(sig, 0, sizeof(sig));
isc_buffer_init(&sigbuf, sig, sizeof(sig), ISC_BUFFERTYPE_BINARY);
isc_result = dst_sign(DST_SIGMODE_ALL, key, NULL, &datareg, &sigbuf);
if (isc_result != ISC_R_SUCCESS) {
t_info("dst_sign(%d) failed %s\n", dst_result_totext(isc_result));
(void) free(data);
(void) dst_key_free(key);
++*nprobs;
return;
}
rval = sig_tofile(sigpath, &sigbuf);
if (rval != 0) {
t_info("sig_tofile failed\n");
++*nprobs;
(void) free(data);
(void) dst_key_free(key);
return;
}
#endif /* NEWSIG */
memset(sig, 0, sizeof(sig));
isc_buffer_init(&sigbuf, sig, sizeof(sig), ISC_BUFFERTYPE_BINARY);
/* read precomputed signature from file in a form usable by dst_verify */
rval = sig_fromfile(sigpath, &sigbuf);
if (rval != 0) {
t_info("sig_fromfile failed\n");
(void) free(data);
(void) dst_key_free(key);
++*nprobs;
return;
}
/* verify that the key signed the data */
isc_buffer_remaining(&sigbuf, &sigreg);
exp_res = 0;
if (strstr(expected_result, "!"))
exp_res = 1;
isc_result = dst_verify(DST_SIGMODE_ALL, key, NULL, &datareg, &sigreg);
if ( ((exp_res == 0) && (isc_result != ISC_R_SUCCESS)) ||
((exp_res != 0) && (isc_result == ISC_R_SUCCESS))) {
t_info("dst_verify returned %s, expected %s\n",
isc_result_totext(isc_result),
expected_result);
++*nfails;
}
(void) free(data);
(void) dst_key_free(key);
return;
}
/*
* the astute observer will note that t1() signs then verifies data
* during the test but that t2() verifies data that has been
* signed at some earlier time, possibly with an entire different
* version or implementation of the DSA and RSA algorithms
*/
static char *a2 =
"the dst module provides the capability to "
"verify data signed with the RSA and DSA algorithms";
/* av == datafile, sigpath, keyname, keyid, alg, exp_result */
static int
t2_vfy(char **av) {
char *datapath;
char *sigpath;
char *keyname;
char *key;
int keyid;
char *alg;
int algid;
char *exp_result;
int nfails;
int nprobs;
isc_mem_t *mctx;
isc_result_t isc_result;
int result;
datapath = *av++;
sigpath = *av++;
keyname = *av++;
key = *av++;
keyid = atoi(key);
alg = *av++;
exp_result = *av++;
nfails = 0;
nprobs = 0;
if (! strcasecmp(alg, "DST_ALG_DSA"))
algid = DST_ALG_DSA;
else if (! strcasecmp(alg, "DST_ALG_RSA"))
algid = DST_ALG_RSA;
else {
t_info("Unknown algorithm %s\n", alg);
return(T_UNRESOLVED);
}
mctx = NULL;
isc_result = isc_mem_create(0, 0, &mctx);
if (isc_result != ISC_R_SUCCESS) {
t_info("isc_mem_create failed %d\n", isc_result_totext(isc_result));
return(T_UNRESOLVED);
}
t_info("testing %s, %s, %s, %s, %s, %s\n",
datapath, sigpath, keyname, key, alg, exp_result);
t2_sigchk(datapath, sigpath, keyname, keyid,
algid, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
mctx, exp_result,
&nfails, &nprobs);
isc_mem_destroy(&mctx);
result = T_UNRESOLVED;
if (nfails)
result = T_FAIL;
else if ((nfails == 0) && (nprobs == 0))
result = T_PASS;
return(result);
}
static void
t2() {
int result;
t_assert("dst", 2, T_REQUIRED, a2);
result = t_eval("dst_2_data", t2_vfy, 6);
t_result(result);
}
testspec_t T_testlist[] = {
{ t1, "basic dst module verification" },
{ t2, "signature ineffability" },
{ NULL, NULL }
};