dst_test.c revision 4124057bf54f0fc75b06b35487130bbd9249ee7f
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington/*
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * Copyright (C) 1999, 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington * copyright notice and this permission notice appear in all copies.
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington *
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington */
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <ctype.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdio.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdlib.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <string.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <unistd.h> /* XXX */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/assertions.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/error.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/boolean.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/region.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/mem.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/result.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dst/dst.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dst/result.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinchar *current, *tmp = "/tmp";
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinuse(dst_key_t *key) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_t ret;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein char *data = "This is some data";
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned char sig[512];
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_t databuf, sigbuf;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_region_t datareg, sigreg;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_init(&sigbuf, sig, sizeof(sig), ISC_BUFFERTYPE_BINARY);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /* Advance 1 byte for fun */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_add(&sigbuf, 1);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_init(&databuf, data, strlen(data), ISC_BUFFERTYPE_TEXT);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_add(&databuf, strlen(data));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_used(&databuf, &datareg);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ret = dst_sign(DST_SIGMODE_ALL, key, NULL, &datareg, &sigbuf);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("sign(%d) returned: %s\n", dst_key_alg(key),
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_totext(ret));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_forward(&sigbuf, 1);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_remaining(&sigbuf, &sigreg);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ret = dst_verify(DST_SIGMODE_ALL, key, NULL, &datareg, &sigreg);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("verify(%d) returned: %s\n", dst_key_alg(key),
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_totext(ret));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinio(char *name, int id, int alg, int type, isc_mem_t *mctx) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dst_key_t *key;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_t ret;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein chdir(current);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ret = dst_key_fromfile(name, id, alg, type, mctx, &key);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (ret != 0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein chdir(tmp);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ret = dst_key_tofile(key, type);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (ret != 0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein use(key);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dst_key_free(key);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeindh(char *name1, int id1, char *name2, int id2, isc_mem_t *mctx) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dst_key_t *key1, *key2;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_t ret;
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington isc_buffer_t b1, b2;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_region_t r1, r2;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington unsigned char array1[1024], array2[1024];
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington int alg = DST_ALG_DH;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington int type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington chdir(current);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = dst_key_fromfile(name1, id1, alg, type, mctx, &key1);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != 0)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = dst_key_fromfile(name2, id2, alg, type, mctx, &key2);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (ret != 0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington chdir(tmp);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = dst_key_tofile(key1, type);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != 0)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return;
47ad4fad771b9c570bcf57793d6a931e0ca9856cAndreas Gustafsson ret = dst_key_tofile(key2, type);
47ad4fad771b9c570bcf57793d6a931e0ca9856cAndreas Gustafsson printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != 0)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_buffer_init(&b1, array1, sizeof(array1), ISC_BUFFERTYPE_BINARY);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ret = dst_computesecret(key1, key2, &b1);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("computesecret() returned: %s\n", isc_result_totext(ret));
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson if (ret != 0)
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson return;
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson isc_buffer_init(&b2, array2, sizeof(array2), ISC_BUFFERTYPE_BINARY);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson ret = dst_computesecret(key2, key1, &b2);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson printf("computesecret() returned: %s\n", isc_result_totext(ret));
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson if (ret != 0)
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews return;
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson isc_buffer_used(&b1, &r1);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson isc_buffer_used(&b2, &r2);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson if (r1.length != r2.length || memcmp(r1.base, r2.base, r1.length) != 0)
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson {
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson int i;
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson printf("secrets don't match\n");
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson printf("secret 1: %d bytes\n", r1.length);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson for (i = 0; i < (int) r1.length; i++)
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson printf("%02x ", r1.base[i]);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson printf("\n");
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("secret 2: %d bytes\n", r2.length);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson for (i = 0; i < (int) r2.length; i++)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("%02x ", r2.base[i]);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("\n");
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dst_key_free(key1);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dst_key_free(key2);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonstatic void
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtongenerate(int alg, isc_mem_t *mctx) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_result_t ret;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dst_key_t *key;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = dst_key_generate("test.", alg, 512, 0, 0, 0, mctx, &key);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("generate(%d) returned: %s\n", alg, isc_result_totext(ret));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (alg != DST_ALG_DH)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington use(key);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dst_key_free(key);
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington}
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellingtonstatic void
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellingtonget_random() {
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington unsigned char data[25];
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington isc_buffer_t databuf;
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington isc_result_t ret;
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington unsigned int i;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_buffer_init(&databuf, data, sizeof data, ISC_BUFFERTYPE_BINARY);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = dst_random_get(sizeof(data), &databuf);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("random() returned: %s\n", isc_result_totext(ret));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington for (i = 0; i < sizeof data; i++)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("%02x ", data[i]);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington printf("\n");
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonint
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonmain() {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_mem_t *mctx = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_mem_create(0, 0, &mctx);
5e2f4725611cf90870268a798409ce3ebe684474Brian Wellington
5e2f4725611cf90870268a798409ce3ebe684474Brian Wellington current = isc_mem_get(mctx, 256);
5e2f4725611cf90870268a798409ce3ebe684474Brian Wellington getcwd(current, 256);
5e2f4725611cf90870268a798409ce3ebe684474Brian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dns_result_register();
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dst_result_register();
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington io("test.", 6204, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington io("test.", 54622, DST_ALG_RSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington io("test.", 0, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington io("test.", 0, DST_ALG_RSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington dh("dh.", 18088, "dh.", 48443, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington generate(DST_ALG_RSA, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington generate(DST_ALG_DH, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington generate(DST_ALG_DSA, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington generate(DST_ALG_HMACMD5, mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington get_random();
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington isc_mem_put(mctx, current, 256);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington/* isc_mem_stats(mctx, stdout);*/
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson isc_mem_destroy(&mctx);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington exit(0);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington