tkey_test.c 9.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (C) 1999  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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM 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.
 */

/*
 * Principal Author: Brian Wellington (mostly copied from res_test.c)
 */

#include <config.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <isc/assertions.h>
#include <isc/commandline.h>
#include <isc/error.h>
#include <isc/task.h>
#include <isc/timer.h>
#include <isc/app.h>
#include <isc/mutex.h>
#include <isc/boolean.h>
#include <isc/net.h>
#include <isc/socket.h>
38
#include <isc/log.h>
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include "../../isc/util.h"		/* XXX Naughty. */

#include <dns/types.h>
#include <dns/result.h>
#include <dns/message.h>
#include <dns/name.h>
#include <dns/fixedname.h>
#include <dns/resolver.h>
#include <dns/events.h>
#include <dns/tsig.h>
#include <dns/tkey.h>
#include <dns/keyvalues.h>

#define CHECK(str, x) { \
	if ((x) != ISC_R_SUCCESS) { \
		printf("%s: %s\n", (str), isc_result_totext(x)); \
		exit(-1); \
	} \
}

60
61
62
63
#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK (unsigned long)0x7F000001UL
#endif

64
65
66
67
68
69
70
71
72
73
74
75
static void buildquery(void);
static void buildquery2(void);

isc_mutex_t lock;
isc_taskmgr_t *taskmgr;
isc_task_t *task1, *task2;
dst_key_t *ourkey;
isc_socket_t *s;
isc_sockaddr_t address;
dns_message_t *query, *response, *query2, *response2;
isc_mem_t *mctx;
dns_tsigkey_t *tsigkey;
76
isc_log_t *log = NULL;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

static void
senddone(isc_task_t *task, isc_event_t *event) {
	isc_socketevent_t *sevent = (isc_socketevent_t *)event;

	REQUIRE(sevent != NULL);
	REQUIRE(sevent->type == ISC_SOCKEVENT_SENDDONE);
	REQUIRE(task == task1);

	printf("senddone\n");

	isc_event_free(&event);
}

static void
recvdone(isc_task_t *task, isc_event_t *event) {
	isc_socketevent_t *sevent = (isc_socketevent_t *)event;
	isc_buffer_t source;
	isc_result_t result;

	REQUIRE(sevent != NULL);
	REQUIRE(sevent->type == ISC_SOCKEVENT_RECVDONE);
	REQUIRE(task == task1);

	printf("recvdone\n");
	if (sevent->result != ISC_R_SUCCESS) {
		printf("failed\n");
		exit(-1);
	}

	isc_buffer_init(&source, sevent->region.base, sevent->region.length,
			ISC_BUFFERTYPE_BINARY);
	isc_buffer_add(&source, sevent->n);

	response = NULL;
	result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &response);
	CHECK("dns_message_create", result);
	result = dns_message_parse(response, &source, ISC_FALSE);
	CHECK("dns_message_parse", result);

	tsigkey = NULL;
	result = dns_tkey_processdhresponse(query, response, ourkey, &tsigkey);
	CHECK("dns_tkey_processdhresponse", result);
	printf("response ok\n");

	buildquery2();
}

static void
senddone2(isc_task_t *task, isc_event_t *event) {
	isc_socketevent_t *sevent = (isc_socketevent_t *)event;

	REQUIRE(sevent != NULL);
	REQUIRE(sevent->type == ISC_SOCKEVENT_SENDDONE);
	REQUIRE(task == task2);

	printf("senddone2\n");

	isc_event_free(&event);
}

static void
recvdone2(isc_task_t *task, isc_event_t *event) {
	isc_socketevent_t *sevent = (isc_socketevent_t *)event;
	isc_buffer_t source;
	isc_result_t result;

	REQUIRE(sevent != NULL);
	REQUIRE(sevent->type == ISC_SOCKEVENT_RECVDONE);
	REQUIRE(task == task2);

	printf("recvdone2\n");
	if (sevent->result != ISC_R_SUCCESS) {
		printf("failed\n");
		exit(-1);
	}

	isc_buffer_init(&source, sevent->region.base, sevent->region.length,
			ISC_BUFFERTYPE_BINARY);
	isc_buffer_add(&source, sevent->n);

	response = NULL;
	result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &response2);
	response2->querytsig = query2->tsig;
	query2->tsig = NULL;
	response2->tsigkey = query2->tsigkey;
	query2->tsigkey = NULL;
	CHECK("dns_message_create", result);
	result = dns_message_parse(response2, &source, ISC_FALSE);
	CHECK("dns_message_parse", result);

	result = dns_tkey_processdeleteresponse(query2, response2);
	CHECK("dns_tkey_processdeleteresponse", result);
	printf("response ok\n");
	exit(0);
}

static void
buildquery(void) {
	unsigned char qdata[1024], rdata[2048];
	isc_buffer_t qbuffer;
	isc_region_t r, inr;
	isc_result_t result;

	query = NULL;
	result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &query);
	CHECK("dns_message_create", result);

	result = dns_tkey_builddhquery(query, ourkey, dns_rootname,
				       DNS_TSIG_HMACMD5_NAME, NULL);
	CHECK("dns_tkey_builddhquery", result);

	isc_buffer_init(&qbuffer, qdata, sizeof(qdata), ISC_BUFFERTYPE_BINARY);

	result = dns_message_renderbegin(query, &qbuffer);
	CHECK("dns_message_renderbegin", result);
	result = dns_message_rendersection(query, DNS_SECTION_QUESTION, 0, 0);
	CHECK("dns_message_rendersection(question)", result);
	result = dns_message_rendersection(query, DNS_SECTION_ANSWER, 0, 0);
	CHECK("dns_message_rendersection(answer)", result);
	result = dns_message_rendersection(query, DNS_SECTION_AUTHORITY, 0, 0);
	CHECK("dns_message_rendersection(auth)", result);
	result = dns_message_rendersection(query, DNS_SECTION_ADDITIONAL, 0, 0);
	CHECK("dns_message_rendersection(add)", result);
	result = dns_message_renderend(query);
	CHECK("dns_message_renderend", result);

	isc_buffer_used(&qbuffer, &r);
	result = isc_socket_sendto(s, &r, task1, senddone, NULL, &address);
	CHECK("isc_socket_sendto", result);
	inr.base = rdata;
	inr.length = sizeof(rdata);
	result = isc_socket_recv(s, &inr, 1, task1, recvdone, NULL);
	CHECK("isc_socket_recv", result);
}

static void
buildquery2(void) {
	unsigned char qdata[1024], rdata[2048];
	isc_buffer_t qbuffer;
	isc_region_t r, inr;
	isc_result_t result;

	query2 = NULL;
	result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &query2);
	CHECK("dns_message_create", result);
	query2->tsigkey = tsigkey;

	result = dns_tkey_builddeletequery(query2, tsigkey);
	CHECK("dns_tkey_builddeletequery", result);

	isc_buffer_init(&qbuffer, qdata, sizeof(qdata), ISC_BUFFERTYPE_BINARY);

	result = dns_message_renderbegin(query2, &qbuffer);
	CHECK("dns_message_renderbegin", result);
	result = dns_message_rendersection(query2, DNS_SECTION_QUESTION, 0, 0);
	CHECK("dns_message_rendersection(question)", result);
	result = dns_message_rendersection(query2, DNS_SECTION_ANSWER, 0, 0);
	CHECK("dns_message_rendersection(answer)", result);
	result = dns_message_rendersection(query2, DNS_SECTION_AUTHORITY, 0, 0);
	CHECK("dns_message_rendersection(auth)", result);
	result = dns_message_rendersection(query2, DNS_SECTION_ADDITIONAL,
					   0, 0);
	CHECK("dns_message_rendersection(add)", result);
	result = dns_message_renderend(query2);
	CHECK("dns_message_renderend", result);

	isc_buffer_used(&qbuffer, &r);
	result = isc_socket_sendto(s, &r, task2, senddone2, NULL, &address);
	CHECK("isc_socket_sendto", result);
	inr.base = rdata;
	inr.length = sizeof(rdata);
	result = isc_socket_recv(s, &inr, 1, task2, recvdone2, NULL);
	CHECK("isc_socket_recv", result);
}

int
main(int argc, char *argv[]) {
	isc_boolean_t verbose = ISC_FALSE;
	unsigned int workers = 2;
	isc_timermgr_t *timermgr;
	int ch;
	isc_socketmgr_t *socketmgr;
	struct in_addr inaddr;
	isc_result_t result;

	RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);

	mctx = NULL;
	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);

	while ((ch = isc_commandline_parse(argc, argv, "vw:")) != -1) {
		switch (ch) {
		case 'v':
			verbose = ISC_TRUE;
			break;
		case 'w':
			workers = (unsigned int)atoi(isc_commandline_argument);
			break;
		}
	}

	if (verbose) {
		printf("%u workers\n", workers);
		printf("IPv4: %s\n", isc_result_totext(isc_net_probeipv4()));
		printf("IPv6: %s\n", isc_result_totext(isc_net_probeipv6()));
	}

	dns_result_register();
	dst_result_register();

	taskmgr = NULL;
	RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &taskmgr) ==
		      ISC_R_SUCCESS);
	task1 = NULL;
	RUNTIME_CHECK(isc_task_create(taskmgr, mctx, 0, &task1) ==
		      ISC_R_SUCCESS);
	task2 = NULL;
	RUNTIME_CHECK(isc_task_create(taskmgr, mctx, 0, &task2) ==
		      ISC_R_SUCCESS);
	timermgr = NULL;
	RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) == ISC_R_SUCCESS);
	socketmgr = NULL;
	RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);

304
305
306
	RUNTIME_CHECK(isc_log_create(mctx, &log) == ISC_R_SUCCESS);
	RUNTIME_CHECK(dns_tsig_init(log, NULL, mctx) == ISC_R_SUCCESS);
	RUNTIME_CHECK(dns_tkey_init(log, NULL, mctx) == ISC_R_SUCCESS);
307
308
309
310
311
312
313
314
315
316
317
318
319

	argc -= isc_commandline_index;
	argv += isc_commandline_index;

	if (argc != 0)
		printf("ignoring trailing arguments\n");

	s = NULL;
	RUNTIME_CHECK(isc_socket_create(socketmgr, PF_INET,
					isc_sockettype_udp, &s) ==
		      ISC_R_SUCCESS);

	inaddr.s_addr = htonl(INADDR_LOOPBACK);
Bob Halley's avatar
Bob Halley committed
320
	isc_sockaddr_fromin(&address, &inaddr, 53);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

	ourkey = NULL;
	result = dst_key_fromfile("client.", 2982, DNS_KEYALG_DH,
				  DST_TYPE_PRIVATE, mctx, &ourkey);
	CHECK("dst_key_fromfile", result);

	buildquery();

	(void)isc_app_run();

	/*
	 * XXXRTH if we get a control-C before we get to isc_app_run(),
	 * we're in trouble (because we might try to destroy things before
	 * they've been created.
	 */

	isc_task_shutdown(task1);
	isc_task_detach(&task1);
	isc_task_shutdown(task2);
	isc_task_detach(&task2);
	isc_taskmgr_destroy(&taskmgr);

	isc_socket_detach(&s);
	isc_socketmgr_destroy(&socketmgr);
	isc_timermgr_destroy(&timermgr);

	dns_tsig_destroy();
Brian Wellington's avatar
Brian Wellington committed
348
	dns_tkey_destroy();
349
350
351
352
353
354
355
356
357
358
	if (verbose)
		isc_mem_stats(mctx, stdout);
	isc_mem_destroy(&mctx);

	isc_mutex_destroy(&lock);

	isc_app_finish();

	return (0);
}