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