xref: /minix/external/bsd/bind/dist/bin/tests/dst/dst_test.c (revision 00b67f09)
1 /*	$NetBSD: dst_test.c,v 1.7 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2012, 2014  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/log.h>
36 #include <dns/name.h>
37 #include <dns/result.h>
38 
39 #include <dst/dst.h>
40 #include <dst/result.h>
41 
42 char *current;
43 const char *tmp = "/tmp";
44 
45 static void
use(dst_key_t * key,isc_mem_t * mctx)46 use(dst_key_t *key, isc_mem_t *mctx) {
47 	isc_result_t ret;
48 	const char *data = "This is some data";
49 	unsigned char sig[512];
50 	isc_buffer_t databuf, sigbuf;
51 	isc_region_t datareg, sigreg;
52 	dst_context_t *ctx = NULL;
53 
54 	isc_buffer_init(&sigbuf, sig, sizeof(sig));
55 	/*
56 	 * Advance 1 byte for fun.
57 	 */
58 	isc_buffer_add(&sigbuf, 1);
59 
60 	isc_buffer_constinit(&databuf, data, strlen(data));
61 	isc_buffer_add(&databuf, strlen(data));
62 	isc_buffer_usedregion(&databuf, &datareg);
63 
64 	ret = dst_context_create3(key, mctx,
65 				  DNS_LOGCATEGORY_GENERAL, ISC_TRUE, &ctx);
66 	if (ret != ISC_R_SUCCESS) {
67 		printf("contextcreate(%d) returned: %s\n", dst_key_alg(key),
68 		       isc_result_totext(ret));
69 		return;
70 	}
71 	ret = dst_context_adddata(ctx, &datareg);
72 	if (ret != ISC_R_SUCCESS) {
73 		printf("adddata(%d) returned: %s\n", dst_key_alg(key),
74 		       isc_result_totext(ret));
75 		dst_context_destroy(&ctx);
76 		return;
77 	}
78 	ret = dst_context_sign(ctx, &sigbuf);
79 	printf("sign(%d) returned: %s\n", dst_key_alg(key),
80 	       isc_result_totext(ret));
81 	dst_context_destroy(&ctx);
82 
83 	isc_buffer_forward(&sigbuf, 1);
84 	isc_buffer_remainingregion(&sigbuf, &sigreg);
85 	ret = dst_context_create3(key, mctx,
86 				  DNS_LOGCATEGORY_GENERAL, ISC_FALSE, &ctx);
87 	if (ret != ISC_R_SUCCESS) {
88 		printf("contextcreate(%d) returned: %s\n", dst_key_alg(key),
89 		       isc_result_totext(ret));
90 		return;
91 	}
92 	ret = dst_context_adddata(ctx, &datareg);
93 	if (ret != ISC_R_SUCCESS) {
94 		printf("adddata(%d) returned: %s\n", dst_key_alg(key),
95 		       isc_result_totext(ret));
96 		dst_context_destroy(&ctx);
97 		return;
98 	}
99 	ret = dst_context_verify(ctx, &sigreg);
100 	printf("verify(%d) returned: %s\n", dst_key_alg(key),
101 	       isc_result_totext(ret));
102 	dst_context_destroy(&ctx);
103 }
104 
105 static void
dns(dst_key_t * key,isc_mem_t * mctx)106 dns(dst_key_t *key, isc_mem_t *mctx) {
107 	unsigned char buffer1[2048];
108 	unsigned char buffer2[2048];
109 	isc_buffer_t buf1, buf2;
110 	isc_region_t r1, r2;
111 	dst_key_t *newkey = NULL;
112 	isc_result_t ret;
113 	isc_boolean_t match;
114 
115 	isc_buffer_init(&buf1, buffer1, sizeof(buffer1));
116 	ret = dst_key_todns(key, &buf1);
117 	printf("todns(%d) returned: %s\n", dst_key_alg(key),
118 	       isc_result_totext(ret));
119 	if (ret != ISC_R_SUCCESS)
120 		return;
121 	ret = dst_key_fromdns(dst_key_name(key), dns_rdataclass_in,
122 			      &buf1, mctx, &newkey);
123 	printf("fromdns(%d) returned: %s\n", dst_key_alg(key),
124 	       isc_result_totext(ret));
125 	if (ret != ISC_R_SUCCESS)
126 		return;
127 	isc_buffer_init(&buf2, buffer2, sizeof(buffer2));
128 	ret = dst_key_todns(newkey, &buf2);
129 	printf("todns2(%d) returned: %s\n", dst_key_alg(key),
130 	       isc_result_totext(ret));
131 	if (ret != ISC_R_SUCCESS)
132 		return;
133 	isc_buffer_usedregion(&buf1, &r1);
134 	isc_buffer_usedregion(&buf2, &r2);
135 	match = ISC_TF(r1.length == r2.length &&
136 		       memcmp(r1.base, r2.base, r1.length) == 0);
137 	printf("compare(%d): %s\n", dst_key_alg(key),
138 	       match ? "true" : "false");
139 	dst_key_free(&newkey);
140 }
141 
142 static void
io(dns_name_t * name,int id,int alg,int type,isc_mem_t * mctx)143 io(dns_name_t *name, int id, int alg, int type, isc_mem_t *mctx) {
144 	dst_key_t *key = NULL;
145 	isc_result_t ret;
146 
147 	ret = dst_key_fromfile(name, id, alg, type, current, mctx, &key);
148 	printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
149 	if (ret != 0)
150 		return;
151 	ret = dst_key_tofile(key, type, tmp);
152 	printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
153 	if (ret != 0)
154 		return;
155 	use(key, mctx);
156 	dns(key, mctx);
157 	dst_key_free(&key);
158 }
159 
160 static void
dh(dns_name_t * name1,int id1,dns_name_t * name2,int id2,isc_mem_t * mctx)161 dh(dns_name_t *name1, int id1, dns_name_t *name2, int id2, isc_mem_t *mctx) {
162 	dst_key_t *key1 = NULL, *key2 = NULL;
163 	isc_result_t ret;
164 	isc_buffer_t b1, b2;
165 	isc_region_t r1, r2;
166 	unsigned char array1[1024], array2[1024];
167 	int alg = DST_ALG_DH;
168 	int type = DST_TYPE_PUBLIC|DST_TYPE_PRIVATE|DST_TYPE_KEY;
169 
170 	ret = dst_key_fromfile(name1, id1, alg, type, current, mctx, &key1);
171 	printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
172 	if (ret != 0)
173 		return;
174 	ret = dst_key_fromfile(name2, id2, alg, type, current, mctx, &key2);
175 	printf("read(%d) returned: %s\n", alg, isc_result_totext(ret));
176 	if (ret != 0)
177 		return;
178 
179 	ret = dst_key_tofile(key1, type, tmp);
180 	printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
181 	if (ret != 0)
182 		return;
183 	ret = dst_key_tofile(key2, type, tmp);
184 	printf("write(%d) returned: %s\n", alg, isc_result_totext(ret));
185 	if (ret != 0)
186 		return;
187 
188 	isc_buffer_init(&b1, array1, sizeof(array1));
189 	ret = dst_key_computesecret(key1, key2, &b1);
190 	printf("computesecret() returned: %s\n", isc_result_totext(ret));
191 	if (ret != 0)
192 		return;
193 
194 	isc_buffer_init(&b2, array2, sizeof(array2));
195 	ret = dst_key_computesecret(key2, key1, &b2);
196 	printf("computesecret() returned: %s\n", isc_result_totext(ret));
197 	if (ret != 0)
198 		return;
199 
200 	isc_buffer_usedregion(&b1, &r1);
201 	isc_buffer_usedregion(&b2, &r2);
202 
203 	if (r1.length != r2.length || memcmp(r1.base, r2.base, r1.length) != 0)
204 	{
205 		int i;
206 		printf("secrets don't match\n");
207 		printf("secret 1: %d bytes\n", r1.length);
208 		for (i = 0; i < (int) r1.length; i++)
209 			printf("%02x ", r1.base[i]);
210 		printf("\n");
211 		printf("secret 2: %d bytes\n", r2.length);
212 		for (i = 0; i < (int) r2.length; i++)
213 			printf("%02x ", r2.base[i]);
214 		printf("\n");
215 	}
216 	dst_key_free(&key1);
217 	dst_key_free(&key2);
218 }
219 
220 static void
generate(int alg,isc_mem_t * mctx)221 generate(int alg, isc_mem_t *mctx) {
222 	isc_result_t ret;
223 	dst_key_t *key = NULL;
224 
225 	ret = dst_key_generate(dns_rootname, alg, 512, 0, 0, 0,
226 			       dns_rdataclass_in, mctx, &key);
227 	printf("generate(%d) returned: %s\n", alg, isc_result_totext(ret));
228 	if (ret != ISC_R_SUCCESS)
229 		return;
230 
231 	if (alg != DST_ALG_DH)
232 		use(key, mctx);
233 
234 	dst_key_free(&key);
235 }
236 
237 int
main(void)238 main(void) {
239 	isc_mem_t *mctx = NULL;
240 	isc_entropy_t *ectx = NULL;
241 	isc_buffer_t b;
242 	dns_fixedname_t fname;
243 	dns_name_t *name;
244 	isc_result_t result;
245 
246 	result = isc_mem_create(0, 0, &mctx);
247 	if (result != ISC_R_SUCCESS)
248 		return (1);
249 
250 	current = isc_mem_get(mctx, 256);
251 	if (current == NULL)
252 		return (1);
253 	if (getcwd(current, 256) == NULL) {
254 		perror("getcwd");
255 		return (1);
256 	}
257 
258 	dns_result_register();
259 
260 	result = isc_entropy_create(mctx, &ectx);
261 	if (result != ISC_R_SUCCESS)
262 		return (1);
263 	result = isc_entropy_createfilesource(ectx, "randomfile");
264 	if (result != ISC_R_SUCCESS)
265 		return (1);
266 	dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING|ISC_ENTROPY_GOODONLY);
267 
268 	dns_fixedname_init(&fname);
269 	name = dns_fixedname_name(&fname);
270 	isc_buffer_constinit(&b, "test.", 5);
271 	isc_buffer_add(&b, 5);
272 	result = dns_name_fromtext(name, &b, NULL, 0, NULL);
273 	if (result != ISC_R_SUCCESS)
274 		return (1);
275 	io(name, 23616, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
276 	io(name, 54622, DST_ALG_RSAMD5, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
277 	   mctx);
278 
279 	io(name, 49667, DST_ALG_DSA, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
280 	io(name, 2, DST_ALG_RSAMD5, DST_TYPE_PRIVATE|DST_TYPE_PUBLIC, mctx);
281 
282 	isc_buffer_constinit(&b, "dh.", 3);
283 	isc_buffer_add(&b, 3);
284 	result = dns_name_fromtext(name, &b, NULL, 0, NULL);
285 	if (result != ISC_R_SUCCESS)
286 		return (1);
287 	dh(name, 18602, name, 48957, mctx);
288 
289 	generate(DST_ALG_RSAMD5, mctx);
290 	generate(DST_ALG_DH, mctx);
291 	generate(DST_ALG_DSA, mctx);
292 	generate(DST_ALG_HMACMD5, mctx);
293 
294 	dst_lib_destroy();
295 	isc_entropy_detach(&ectx);
296 
297 	isc_mem_put(mctx, current, 256);
298 /*	isc_mem_stats(mctx, stdout);*/
299 	isc_mem_destroy(&mctx);
300 
301 	return (0);
302 }
303