1/* $NetBSD: dst_test.c,v 1.2.6.1 2012/06/05 21:15:35 bouyer Exp $ */ 2 3/* 4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1999-2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20/* Id: dst_test.c,v 1.46 2009/09/01 00:22:25 jinmei Exp */ 21 22#include <config.h> 23 24#include <stdlib.h> 25 26#include <unistd.h> /* XXX */ 27 28#include <isc/buffer.h> 29#include <isc/entropy.h> 30#include <isc/mem.h> 31#include <isc/region.h> 32#include <isc/string.h> /* Required for HP/UX (and others?) */ 33 34#include <dns/fixedname.h> 35#include <dns/name.h> 36#include <dns/result.h> 37 38#include <dst/dst.h> 39#include <dst/result.h> 40 41char *current; 42const char *tmp = "/tmp"; 43 44static void 45use(dst_key_t *key, isc_mem_t *mctx) { 46 isc_result_t ret; 47 const char *data = "This is some data"; 48 unsigned char sig[512]; 49 isc_buffer_t databuf, sigbuf; 50 isc_region_t datareg, sigreg; 51 dst_context_t *ctx = NULL; 52 53 isc_buffer_init(&sigbuf, sig, sizeof(sig)); 54 /* 55 * Advance 1 byte for fun. 56 */ 57 isc_buffer_add(&sigbuf, 1); 58 59 isc_buffer_init(&databuf, data, strlen(data)); 60 isc_buffer_add(&databuf, strlen(data)); 61 isc_buffer_usedregion(&databuf, &datareg); 62 63 ret = dst_context_create(key, mctx, &ctx); 64 if (ret != ISC_R_SUCCESS) { 65 printf("contextcreate(%d) returned: %s\n", dst_key_alg(key), 66 isc_result_totext(ret)); 67 return; 68 } 69 ret = dst_context_adddata(ctx, &datareg); 70 if (ret != ISC_R_SUCCESS) { 71 printf("adddata(%d) returned: %s\n", dst_key_alg(key), 72 isc_result_totext(ret)); 73 dst_context_destroy(&ctx); 74 return; 75 } 76 ret = dst_context_sign(ctx, &sigbuf); 77 printf("sign(%d) returned: %s\n", dst_key_alg(key), 78 isc_result_totext(ret)); 79 dst_context_destroy(&ctx); 80 81 isc_buffer_forward(&sigbuf, 1); 82 isc_buffer_remainingregion(&sigbuf, &sigreg); 83 ret = dst_context_create(key, mctx, &ctx); 84 if (ret != ISC_R_SUCCESS) { 85 printf("contextcreate(%d) returned: %s\n", dst_key_alg(key), 86 isc_result_totext(ret)); 87 return; 88 } 89 ret = dst_context_adddata(ctx, &datareg); 90 if (ret != ISC_R_SUCCESS) { 91 printf("adddata(%d) returned: %s\n", dst_key_alg(key), 92 isc_result_totext(ret)); 93 dst_context_destroy(&ctx); 94 return; 95 } 96 ret = dst_context_verify(ctx, &sigreg); 97 printf("verify(%d) returned: %s\n", dst_key_alg(key), 98 isc_result_totext(ret)); 99 dst_context_destroy(&ctx); 100} 101 102static void 103dns(dst_key_t *key, isc_mem_t *mctx) { 104 unsigned char buffer1[2048]; 105 unsigned char buffer2[2048]; 106 isc_buffer_t buf1, buf2; 107 isc_region_t r1, r2; 108 dst_key_t *newkey = NULL; 109 isc_result_t ret; 110 isc_boolean_t match; 111 112 isc_buffer_init(&buf1, buffer1, sizeof(buffer1)); 113 ret = dst_key_todns(key, &buf1); 114 printf("todns(%d) returned: %s\n", dst_key_alg(key), 115 isc_result_totext(ret)); 116 if (ret != ISC_R_SUCCESS) 117 return; 118 ret = dst_key_fromdns(dst_key_name(key), dns_rdataclass_in, 119 &buf1, mctx, &newkey); 120 printf("fromdns(%d) returned: %s\n", dst_key_alg(key), 121 isc_result_totext(ret)); 122 if (ret != ISC_R_SUCCESS) 123 return; 124 isc_buffer_init(&buf2, buffer2, sizeof(buffer2)); 125 ret = dst_key_todns(newkey, &buf2); 126 printf("todns2(%d) returned: %s\n", dst_key_alg(key), 127 isc_result_totext(ret)); 128 if (ret != ISC_R_SUCCESS) 129 return; 130 isc_buffer_usedregion(&buf1, &r1); 131 isc_buffer_usedregion(&buf2, &r2); 132 match = ISC_TF(r1.length == r2.length && 133 memcmp(r1.base, r2.base, r1.length) == 0); 134 printf("compare(%d): %s\n", dst_key_alg(key), 135 match ? "true" : "false"); 136 dst_key_free(&newkey); 137} 138 139static void 140io(dns_name_t *name, int id, int alg, int type, isc_mem_t *mctx) { 141 dst_key_t *key = NULL; 142 isc_result_t ret; 143 144 ret = dst_key_fromfile(name, id, alg, type, current, mctx, &key); 145 printf("read(%d) returned: %s\n", alg, isc_result_totext(ret)); 146 if (ret != 0) 147 return; 148 ret = dst_key_tofile(key, type, tmp); 149 printf("write(%d) returned: %s\n", alg, isc_result_totext(ret)); 150 if (ret != 0) 151 return; 152 use(key, mctx); 153 dns(key, mctx); 154 dst_key_free(&key); 155} 156 157static void 158dh(dns_name_t *name1, int id1, dns_name_t *name2, int id2, isc_mem_t *mctx) { 159 dst_key_t *key1 = NULL, *key2 = NULL; 160 isc_result_t ret; 161 isc_buffer_t b1, b2; 162 isc_region_t r1, r2; 163 unsigned char array1[1024], array2[1024]; 164 int alg = DST_ALG_DH; 165 int type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE|DST_TYPE_KEY; 166 167 ret = dst_key_fromfile(name1, id1, alg, type, current, mctx, &key1); 168 printf("read(%d) returned: %s\n", alg, isc_result_totext(ret)); 169 if (ret != 0) 170 return; 171 ret = dst_key_fromfile(name2, id2, alg, type, current, mctx, &key2); 172 printf("read(%d) returned: %s\n", alg, isc_result_totext(ret)); 173 if (ret != 0) 174 return; 175 176 ret = dst_key_tofile(key1, type, tmp); 177 printf("write(%d) returned: %s\n", alg, isc_result_totext(ret)); 178 if (ret != 0) 179 return; 180 ret = dst_key_tofile(key2, type, tmp); 181 printf("write(%d) returned: %s\n", alg, isc_result_totext(ret)); 182 if (ret != 0) 183 return; 184 185 isc_buffer_init(&b1, array1, sizeof(array1)); 186 ret = dst_key_computesecret(key1, key2, &b1); 187 printf("computesecret() returned: %s\n", isc_result_totext(ret)); 188 if (ret != 0) 189 return; 190 191 isc_buffer_init(&b2, array2, sizeof(array2)); 192 ret = dst_key_computesecret(key2, key1, &b2); 193 printf("computesecret() returned: %s\n", isc_result_totext(ret)); 194 if (ret != 0) 195 return; 196 197 isc_buffer_usedregion(&b1, &r1); 198 isc_buffer_usedregion(&b2, &r2); 199 200 if (r1.length != r2.length || memcmp(r1.base, r2.base, r1.length) != 0) 201 { 202 int i; 203 printf("secrets don't match\n"); 204 printf("secret 1: %d bytes\n", r1.length); 205 for (i = 0; i < (int) r1.length; i++) 206 printf("%02x ", r1.base[i]); 207 printf("\n"); 208 printf("secret 2: %d bytes\n", r2.length); 209 for (i = 0; i < (int) r2.length; i++) 210 printf("%02x ", r2.base[i]); 211 printf("\n"); 212 } 213 dst_key_free(&key1); 214 dst_key_free(&key2); 215} 216 217static void 218generate(int alg, isc_mem_t *mctx) { 219 isc_result_t ret; 220 dst_key_t *key = NULL; 221 222 ret = dst_key_generate(dns_rootname, alg, 512, 0, 0, 0, 223 dns_rdataclass_in, mctx, &key); 224 printf("generate(%d) returned: %s\n", alg, isc_result_totext(ret)); 225 if (ret != ISC_R_SUCCESS) 226 return; 227 228 if (alg != DST_ALG_DH) 229 use(key, mctx); 230 231 dst_key_free(&key); 232} 233 234int 235main(void) { 236 isc_mem_t *mctx = NULL; 237 isc_entropy_t *ectx = NULL; 238 isc_buffer_t b; 239 dns_fixedname_t fname; 240 dns_name_t *name; 241 isc_result_t result; 242 243 result = isc_mem_create(0, 0, &mctx); 244 if (result != ISC_R_SUCCESS) 245 return (1); 246 247 current = isc_mem_get(mctx, 256); 248 if (current == NULL) 249 return (1); 250 if (getcwd(current, 256) == NULL) { 251 perror("getcwd"); 252 return (1); 253 } 254 255 dns_result_register(); 256 257 result = isc_entropy_create(mctx, &ectx); 258 if (result != ISC_R_SUCCESS) 259 return (1); 260 result = isc_entropy_createfilesource(ectx, "randomfile"); 261 if (result != ISC_R_SUCCESS) 262 return (1); 263 dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING|ISC_ENTROPY_GOODONLY); 264 265 dns_fixedname_init(&fname); 266 name = dns_fixedname_name(&fname); 267 isc_buffer_init(&b, "test.", 5); 268 isc_buffer_add(&b, 5); 269 result = dns_name_fromtext(name, &b, NULL, 0, NULL); 270 if (result != ISC_R_SUCCESS) 271 return (1); 272 io(name, 23616, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx); 273 io(name, 54622, DST_ALG_RSAMD5, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, 274 mctx); 275 276 io(name, 49667, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx); 277 io(name, 2, DST_ALG_RSAMD5, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx); 278 279 isc_buffer_init(&b, "dh.", 3); 280 isc_buffer_add(&b, 3); 281 result = dns_name_fromtext(name, &b, NULL, 0, NULL); 282 if (result != ISC_R_SUCCESS) 283 return (1); 284 dh(name, 18602, name, 48957, mctx); 285 286 generate(DST_ALG_RSAMD5, mctx); 287 generate(DST_ALG_DH, mctx); 288 generate(DST_ALG_DSA, mctx); 289 generate(DST_ALG_HMACMD5, mctx); 290 291 dst_lib_destroy(); 292 isc_entropy_detach(&ectx); 293 294 isc_mem_put(mctx, current, 256); 295/* isc_mem_stats(mctx, stdout);*/ 296 isc_mem_destroy(&mctx); 297 298 return (0); 299} 300