📄 t_dst.c
字号:
/* * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1999-2001 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 ISC DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL ISC 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: t_dst.c,v 1.47.206.2 2004/06/11 00:30:52 marka Exp $ */#include <config.h>#include <sys/types.h> /* Required for dirent.h */#include <sys/stat.h>#include <dirent.h> /* XXX */#include <errno.h>#include <fcntl.h>#include <limits.h>#include <stdlib.h>#include <unistd.h> /* XXX */#include <isc/buffer.h>#include <isc/dir.h>#include <isc/entropy.h>#include <isc/file.h>#include <isc/mem.h>#include <isc/region.h>#include <isc/string.h>#include <isc/util.h>#include <dns/fixedname.h>#include <dns/name.h>#include <dst/dst.h>#include <dst/result.h>#include <tests/t_api.h>#ifndef PATH_MAX#define PATH_MAX 256#endif/* * Adapted from the original dst_test.c program. * XXXDCL should use isc_dir_*. */static voidcleandir(char *path) { DIR *dirp; struct dirent *pe; char fullname[PATH_MAX + 1]; dirp = opendir(path); if (dirp == NULL) { t_info("opendir(%s) failed %d\n", path, errno); return; } while ((pe = readdir(dirp)) != NULL) { if (! strcmp(pe->d_name, ".")) continue; if (! strcmp(pe->d_name, "..")) continue; strcpy(fullname, path); strcat(fullname, "/"); strcat(fullname, pe->d_name); if (remove(fullname)) t_info("remove(%s) failed %d\n", fullname, errno); } (void)closedir(dirp); if (rmdir(path)) t_info("rmdir(%s) failed %d\n", path, errno); return;}static voiduse(dst_key_t *key, isc_mem_t *mctx, isc_result_t exp_result, int *nfails) { 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; dst_context_t *ctx = NULL; isc_buffer_init(&sigbuf, sig, sizeof(sig)); isc_buffer_init(&databuf, data, strlen(data)); isc_buffer_add(&databuf, strlen(data)); isc_buffer_usedregion(&databuf, &datareg); ret = dst_context_create(key, mctx, &ctx); if (ret != exp_result) { t_info("dst_context_create(%d) returned (%s) expected (%s)\n", dst_key_alg(key), dst_result_totext(ret), dst_result_totext(exp_result)); ++*nfails; return; } if (exp_result != ISC_R_SUCCESS) return; ret = dst_context_adddata(ctx, &datareg); if (ret != ISC_R_SUCCESS) { t_info("dst_context_adddata(%d) returned (%s)\n", dst_key_alg(key), dst_result_totext(ret)); ++*nfails; dst_context_destroy(&ctx); return; } ret = dst_context_sign(ctx, &sigbuf); if (ret != ISC_R_SUCCESS) { t_info("dst_context_sign(%d) returned (%s)\n", dst_key_alg(key), dst_result_totext(ret)); ++*nfails; dst_context_destroy(&ctx); return; } dst_context_destroy(&ctx); isc_buffer_remainingregion(&sigbuf, &sigreg); ret = dst_context_create(key, mctx, &ctx); if (ret != ISC_R_SUCCESS) { t_info("dst_context_create(%d) returned (%s)\n", dst_key_alg(key), dst_result_totext(ret)); ++*nfails; return; } ret = dst_context_adddata(ctx, &datareg); if (ret != ISC_R_SUCCESS) { t_info("dst_context_adddata(%d) returned (%s)\n", dst_key_alg(key), dst_result_totext(ret)); ++*nfails; dst_context_destroy(&ctx); return; } ret = dst_context_verify(ctx, &sigreg); if (ret != exp_result) { t_info("dst_context_verify(%d) returned (%s) expected (%s)\n", dst_key_alg(key), dst_result_totext(ret), dst_result_totext(exp_result)); ++*nfails; dst_context_destroy(&ctx); return; } dst_context_destroy(&ctx);}static voiddh(dns_name_t *name1, int id1, dns_name_t *name2, int id2, isc_mem_t *mctx, isc_result_t exp_result, int *nfails, int *nprobs){ dst_key_t *key1 = NULL, *key2 = NULL; isc_result_t ret; char current[PATH_MAX + 1]; char tmp[PATH_MAX + 1]; char *p; int alg = DST_ALG_DH; int type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE|DST_TYPE_KEY; unsigned char array1[1024], array2[1024]; isc_buffer_t b1, b2; isc_region_t r1, r2; UNUSED(exp_result); p = getcwd(current, PATH_MAX);; if (p == NULL) { t_info("getcwd failed %d\n", errno); ++*nprobs; return; } ret = dst_key_fromfile(name1, id1, alg, type, current, mctx, &key1); if (ret != ISC_R_SUCCESS) { t_info("dst_key_fromfile(%d) returned: %s\n", alg, dst_result_totext(ret)); ++*nfails; return; } ret = dst_key_fromfile(name2, id2, alg, type, current, mctx, &key2); if (ret != ISC_R_SUCCESS) { t_info("dst_key_fromfile(%d) returned: %s\n", alg, dst_result_totext(ret)); ++*nfails; return; } ret = isc_file_mktemplate("/tmp/", tmp, sizeof(tmp)); if (ret != ISC_R_SUCCESS) { t_info("isc_file_mktemplate failed %s\n", isc_result_totext(ret)); ++*nprobs; return; } ret = isc_dir_createunique(tmp); if (ret != ISC_R_SUCCESS) { t_info("isc_dir_createunique failed %s\n", isc_result_totext(ret)); ++*nprobs; return; } ret = dst_key_tofile(key1, type, tmp); if (ret != 0) { t_info("dst_key_tofile(%d) returned: %s\n", alg, dst_result_totext(ret)); ++*nfails; return; } ret = dst_key_tofile(key2, type, tmp); if (ret != 0) { t_info("dst_key_tofile(%d) returned: %s\n", alg, dst_result_totext(ret)); ++*nfails; return; } cleandir(tmp); isc_buffer_init(&b1, array1, sizeof(array1)); ret = dst_key_computesecret(key1, key2, &b1); if (ret != 0) { t_info("dst_computesecret() returned: %s\n", dst_result_totext(ret)); ++*nfails; return; } isc_buffer_init(&b2, array2, sizeof(array2)); ret = dst_key_computesecret(key2, key1, &b2); if (ret != 0) { t_info("dst_computesecret() returned: %s\n", dst_result_totext(ret)); ++*nfails; return; } isc_buffer_usedregion(&b1, &r1); isc_buffer_usedregion(&b2, &r2); if (r1.length != r2.length || memcmp(r1.base, r2.base, r1.length) != 0) { t_info("computed secrets don't match\n"); ++*nfails; return; } dst_key_free(&key1); dst_key_free(&key2);}static voidio(dns_name_t *name, int id, int alg, int type, isc_mem_t *mctx, isc_result_t exp_result, int *nfails, int *nprobs){ dst_key_t *key = NULL; isc_result_t ret; char current[PATH_MAX + 1]; char tmp[PATH_MAX + 1]; char *p; p = getcwd(current, PATH_MAX);; if (p == NULL) { t_info("getcwd failed %d\n", errno); ++*nprobs; return; } ret = dst_key_fromfile(name, id, alg, type, current, mctx, &key); if (ret != ISC_R_SUCCESS) { t_info("dst_key_fromfile(%d) returned: %s\n", alg, dst_result_totext(ret)); ++*nfails; return; } ret = isc_file_mktemplate("/tmp/", tmp, sizeof(tmp)); if (ret != ISC_R_SUCCESS) { t_info("isc_file_mktemplate failed %s\n", isc_result_totext(ret)); ++*nprobs; return; } ret = isc_dir_createunique(tmp); if (ret != ISC_R_SUCCESS) { t_info("mkdir failed %d\n", errno); ++*nprobs; return; } ret = dst_key_tofile(key, type, tmp); if (ret != 0) { t_info("dst_key_tofile(%d) returned: %s\n", alg, dst_result_totext(ret)); ++*nfails; return; } if (dst_key_alg(key) != DST_ALG_DH) use(key, mctx, exp_result, nfails); cleandir(tmp); dst_key_free(&key);}static voidgenerate(int alg, isc_mem_t *mctx, int size, int *nfails) { isc_result_t ret; dst_key_t *key = NULL; ret = dst_key_generate(dns_rootname, alg, size, 0, 0, 0, dns_rdataclass_in, 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, mctx, ISC_R_SUCCESS, nfails); dst_key_free(&key);}#define DBUFSIZ 25static const 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, " "and compute Diffie-Hellman shared secrets.";static voidt1(void) { isc_mem_t *mctx; isc_entropy_t *ectx; int nfails; int nprobs; int result; isc_result_t isc_result; dns_fixedname_t fname; dns_name_t *name; isc_buffer_t b; 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 %s\n", isc_result_totext(isc_result)); t_result(T_UNRESOLVED); return; } ectx = NULL; isc_result = isc_entropy_create(mctx, &ectx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_entropy_create failed %s\n", isc_result_totext(isc_result)); t_result(T_UNRESOLVED); return; } result = isc_entropy_createfilesource(ectx, "randomfile"); if (isc_result != ISC_R_SUCCESS) { t_info("isc_entropy_create failed %s\n", isc_result_totext(isc_result)); t_result(T_UNRESOLVED); return; } isc_result = dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING); if (isc_result != ISC_R_SUCCESS) { t_info("dst_lib_init failed %s\n", isc_result_totext(isc_result)); t_result(T_UNRESOLVED); return; } if (!dst_algorithm_supported(DST_ALG_RSAMD5)) { dst_lib_destroy(); t_info("library built without crypto support\n"); t_result(T_UNTESTED); return; } t_info("testing use of stored keys [1]\n"); dns_fixedname_init(&fname); name = dns_fixedname_name(&fname); isc_buffer_init(&b, "test.", 5); isc_buffer_add(&b, 5); dns_name_fromtext(name, &b, NULL, ISC_FALSE, NULL); io(name, 23616, 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(name, 54622, DST_ALG_RSAMD5, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx, ISC_R_SUCCESS, &nfails, &nprobs); t_info("testing use of stored keys [3]\n"); io(name, 49667, 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(name, 2, DST_ALG_RSAMD5, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx, DST_R_NULLKEY, &nfails, &nprobs); isc_buffer_init(&b, "dh.", 3); isc_buffer_add(&b, 3); dns_name_fromtext(name, &b, NULL, ISC_FALSE, NULL); dh(name, 18602, name, 48957, mctx, ISC_R_SUCCESS, &nfails, &nprobs); t_info("testing use of generated keys\n"); generate(DST_ALG_RSAMD5, mctx, 512, &nfails); generate(DST_ALG_DSA, mctx, 512, &nfails); generate(DST_ALG_DH, mctx, 512, &nfails); /* * This one uses a constant. */ generate(DST_ALG_DH, mctx, 768, &nfails); generate(DST_ALG_HMACMD5, mctx, 512, &nfails); dst_lib_destroy(); isc_entropy_detach(&ectx); 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -