4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
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.
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.
20 /* Id: dst_test.c,v 1.46 2009/09/01 00:22:25 jinmei Exp */
26 #include <unistd.h> /* XXX */
28 #include <isc/buffer.h>
29 #include <isc/entropy.h>
31 #include <isc/region.h>
32 #include <isc/string.h> /* Required for HP/UX (and others?) */
34 #include <dns/fixedname.h>
36 #include <dns/result.h>
39 #include <dst/result.h>
42 const char *tmp
= "/tmp";
45 use(dst_key_t
*key
, isc_mem_t
*mctx
) {
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
;
53 isc_buffer_init(&sigbuf
, sig
, sizeof(sig
));
55 * Advance 1 byte for fun.
57 isc_buffer_add(&sigbuf
, 1);
59 isc_buffer_init(&databuf
, data
, strlen(data
));
60 isc_buffer_add(&databuf
, strlen(data
));
61 isc_buffer_usedregion(&databuf
, &datareg
);
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
));
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
);
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
);
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
));
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
);
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
);
103 dns(dst_key_t
*key
, isc_mem_t
*mctx
) {
104 unsigned char buffer1
[2048];
105 unsigned char buffer2
[2048];
106 isc_buffer_t buf1
, buf2
;
108 dst_key_t
*newkey
= NULL
;
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
)
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
)
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
)
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
);
140 io(dns_name_t
*name
, int id
, int alg
, int type
, isc_mem_t
*mctx
) {
141 dst_key_t
*key
= NULL
;
144 ret
= dst_key_fromfile(name
, id
, alg
, type
, current
, mctx
, &key
);
145 printf("read(%d) returned: %s\n", alg
, isc_result_totext(ret
));
148 ret
= dst_key_tofile(key
, type
, tmp
);
149 printf("write(%d) returned: %s\n", alg
, isc_result_totext(ret
));
158 dh(dns_name_t
*name1
, int id1
, dns_name_t
*name2
, int id2
, isc_mem_t
*mctx
) {
159 dst_key_t
*key1
= NULL
, *key2
= NULL
;
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
;
167 ret
= dst_key_fromfile(name1
, id1
, alg
, type
, current
, mctx
, &key1
);
168 printf("read(%d) returned: %s\n", alg
, isc_result_totext(ret
));
171 ret
= dst_key_fromfile(name2
, id2
, alg
, type
, current
, mctx
, &key2
);
172 printf("read(%d) returned: %s\n", alg
, isc_result_totext(ret
));
176 ret
= dst_key_tofile(key1
, type
, tmp
);
177 printf("write(%d) returned: %s\n", alg
, isc_result_totext(ret
));
180 ret
= dst_key_tofile(key2
, type
, tmp
);
181 printf("write(%d) returned: %s\n", alg
, isc_result_totext(ret
));
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
));
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
));
197 isc_buffer_usedregion(&b1
, &r1
);
198 isc_buffer_usedregion(&b2
, &r2
);
200 if (r1
.length
!= r2
.length
|| memcmp(r1
.base
, r2
.base
, r1
.length
) != 0)
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
]);
208 printf("secret 2: %d bytes\n", r2
.length
);
209 for (i
= 0; i
< (int) r2
.length
; i
++)
210 printf("%02x ", r2
.base
[i
]);
218 generate(int alg
, isc_mem_t
*mctx
) {
220 dst_key_t
*key
= NULL
;
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
)
228 if (alg
!= DST_ALG_DH
)
236 isc_mem_t
*mctx
= NULL
;
237 isc_entropy_t
*ectx
= NULL
;
239 dns_fixedname_t fname
;
243 result
= isc_mem_create(0, 0, &mctx
);
244 if (result
!= ISC_R_SUCCESS
)
247 current
= isc_mem_get(mctx
, 256);
250 if (getcwd(current
, 256) == NULL
) {
255 dns_result_register();
257 result
= isc_entropy_create(mctx
, &ectx
);
258 if (result
!= ISC_R_SUCCESS
)
260 result
= isc_entropy_createfilesource(ectx
, "randomfile");
261 if (result
!= ISC_R_SUCCESS
)
263 dst_lib_init(mctx
, ectx
, ISC_ENTROPY_BLOCKING
|ISC_ENTROPY_GOODONLY
);
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
)
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
,
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
);
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
)
284 dh(name
, 18602, name
, 48957, mctx
);
286 generate(DST_ALG_RSAMD5
, mctx
);
287 generate(DST_ALG_DH
, mctx
);
288 generate(DST_ALG_DSA
, mctx
);
289 generate(DST_ALG_HMACMD5
, mctx
);
292 isc_entropy_detach(&ectx
);
294 isc_mem_put(mctx
, current
, 256);
295 /* isc_mem_stats(mctx, stdout);*/
296 isc_mem_destroy(&mctx
);