dnssec.c 21 KB
Newer Older
Brian Wellington's avatar
Brian Wellington committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 1999-2002  Internet Software Consortium.
3
 *
Brian Wellington's avatar
Brian Wellington committed
4
5
6
 * 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.
7
 *
8
9
10
11
12
13
14
15
 * 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.
Brian Wellington's avatar
Brian Wellington committed
16
17
18
 */

/*
Mark Andrews's avatar
Mark Andrews committed
19
 * $Id: dnssec.c,v 1.80 2004/03/04 02:44:54 marka Exp $
Brian Wellington's avatar
Brian Wellington committed
20
21
22
23
24
25
26
 */


#include <config.h>

#include <stdlib.h>

27
#include <isc/buffer.h>
28
#include <isc/mem.h>
29
#include <isc/serial.h>
30
#include <isc/string.h>
Bob Halley's avatar
Bob Halley committed
31
#include <isc/util.h>
Brian Wellington's avatar
Brian Wellington committed
32

Bob Halley's avatar
Bob Halley committed
33
#include <dns/db.h>
34
#include <dns/dnssec.h>
35
#include <dns/fixedname.h>
Brian Wellington's avatar
Brian Wellington committed
36
#include <dns/keyvalues.h>
37
#include <dns/message.h>
Brian Wellington's avatar
Brian Wellington committed
38
#include <dns/rdata.h>
39
#include <dns/rdatalist.h>
40
#include <dns/rdataset.h>
41
#include <dns/rdatastruct.h>
42
43
#include <dns/result.h>
#include <dns/tsig.h>		/* for DNS_TSIG_FUDGE */
Brian Wellington's avatar
Brian Wellington committed
44
45
46

#include <dst/result.h>

Brian Wellington's avatar
Brian Wellington committed
47
48
49
50
51
52
53
54
55
#define is_response(msg) (msg->flags & DNS_MESSAGEFLAG_QR)

#define RETERR(x) do { \
	result = (x); \
	if (result != ISC_R_SUCCESS) \
		goto failure; \
	} while (0)


Brian Wellington's avatar
Brian Wellington committed
56
57
58
#define TYPE_SIGN 0
#define TYPE_VERIFY 1

59
60
static isc_result_t
digest_callback(void *arg, isc_region_t *data);
Brian Wellington's avatar
Brian Wellington committed
61

62
63
64
65
66
67
static int
rdata_compare_wrapper(const void *rdata1, const void *rdata2);

static isc_result_t
rdataset_to_sortedarray(dns_rdataset_t *set, isc_mem_t *mctx,
			dns_rdata_t **rdata, int *nrdata);
Brian Wellington's avatar
Brian Wellington committed
68
69
70

static isc_result_t
digest_callback(void *arg, isc_region_t *data) {
Brian Wellington's avatar
Brian Wellington committed
71
	dst_context_t *ctx = arg;
Brian Wellington's avatar
Brian Wellington committed
72

Brian Wellington's avatar
Brian Wellington committed
73
	return (dst_context_adddata(ctx, data));
Brian Wellington's avatar
Brian Wellington committed
74
75
}

76
77
78
/*
 * Make qsort happy.
 */
Brian Wellington's avatar
Brian Wellington committed
79
80
static int
rdata_compare_wrapper(const void *rdata1, const void *rdata2) {
David Lawrence's avatar
David Lawrence committed
81
82
	return (dns_rdata_compare((const dns_rdata_t *)rdata1,
				  (const dns_rdata_t *)rdata2));
Brian Wellington's avatar
Brian Wellington committed
83
84
}

85
86
87
/*
 * Sort the rdataset into an array.
 */
Brian Wellington's avatar
Brian Wellington committed
88
89
90
91
92
static isc_result_t
rdataset_to_sortedarray(dns_rdataset_t *set, isc_mem_t *mctx,
			dns_rdata_t **rdata, int *nrdata)
{
	isc_result_t ret;
Brian Wellington's avatar
Brian Wellington committed
93
	int i = 0, n;
Brian Wellington's avatar
Brian Wellington committed
94
95
	dns_rdata_t *data;

Brian Wellington's avatar
Brian Wellington committed
96
	n = dns_rdataset_count(set);
Brian Wellington's avatar
Brian Wellington committed
97

98
99
100
101
	data = isc_mem_get(mctx, n * sizeof(dns_rdata_t));
	if (data == NULL)
		return (ISC_R_NOMEMORY);

Brian Wellington's avatar
Brian Wellington committed
102
	ret = dns_rdataset_first(set);
Brian Wellington's avatar
Brian Wellington committed
103
104
	if (ret != ISC_R_SUCCESS) {
		isc_mem_put(mctx, data, n * sizeof(dns_rdata_t));
Brian Wellington's avatar
Brian Wellington committed
105
		return (ret);
Brian Wellington's avatar
Brian Wellington committed
106
	}
Brian Wellington's avatar
Brian Wellington committed
107

108
109
110
	/*
	 * Put them in the array.
	 */
Brian Wellington's avatar
Brian Wellington committed
111
	do {
112
		dns_rdata_init(&data[i]);
Brian Wellington's avatar
Brian Wellington committed
113
114
115
		dns_rdataset_current(set, &data[i++]);
	} while (dns_rdataset_next(set) == ISC_R_SUCCESS);

116
117
118
	/*
	 * Sort the array.
	 */
Brian Wellington's avatar
Brian Wellington committed
119
120
121
122
123
124
	qsort(data, n, sizeof(dns_rdata_t), rdata_compare_wrapper);
	*rdata = data;
	*nrdata = n;
	return (ISC_R_SUCCESS);
}

125
126
127
128
isc_result_t
dns_dnssec_keyfromrdata(dns_name_t *name, dns_rdata_t *rdata, isc_mem_t *mctx,
			dst_key_t **key)
{
129
	isc_buffer_t b;
130
131
132
133
134
135
136
137
138
	isc_region_t r;

	INSIST(name != NULL);
	INSIST(rdata != NULL);
	INSIST(mctx != NULL);
	INSIST(key != NULL);
	INSIST(*key == NULL);

	dns_rdata_toregion(rdata, &r);
139
	isc_buffer_init(&b, r.base, r.length);
140
	isc_buffer_add(&b, r.length);
Brian Wellington's avatar
Brian Wellington committed
141
	return (dst_key_fromdns(name, rdata->rdclass, &b, mctx, key));
142
143
}

144
static isc_result_t
145
digest_sig(dst_context_t *ctx, dns_rdata_t *sigrdata, dns_rdata_rrsig_t *sig) {
146
147
148
149
150
151
152
153
154
155
156
157
	isc_region_t r;
	isc_result_t ret;
	dns_fixedname_t fname;

	dns_rdata_toregion(sigrdata, &r);
	INSIST(r.length >= 19);

	r.length = 18;
	ret = dst_context_adddata(ctx, &r);
	if (ret != ISC_R_SUCCESS)
		return (ret);
	dns_fixedname_init(&fname);
158
159
160
	RUNTIME_CHECK(dns_name_downcase(&sig->signer,
					dns_fixedname_name(&fname), NULL)
		      == ISC_R_SUCCESS);
161
162
163
164
	dns_name_toregion(dns_fixedname_name(&fname), &r);
	return (dst_context_adddata(ctx, &r));
}

Brian Wellington's avatar
Brian Wellington committed
165
166
167
168
169
isc_result_t
dns_dnssec_sign(dns_name_t *name, dns_rdataset_t *set, dst_key_t *key,
		isc_stdtime_t *inception, isc_stdtime_t *expire,
		isc_mem_t *mctx, isc_buffer_t *buffer, dns_rdata_t *sigrdata)
{
170
	dns_rdata_rrsig_t sig;
171
	dns_rdata_t tmpsigrdata;
Brian Wellington's avatar
Brian Wellington committed
172
173
	dns_rdata_t *rdatas;
	int nrdatas, i;
174
	isc_buffer_t sigbuf, envbuf;
Brian Wellington's avatar
Brian Wellington committed
175
	isc_region_t r;
Brian Wellington's avatar
Brian Wellington committed
176
	dst_context_t *ctx = NULL;
Brian Wellington's avatar
Brian Wellington committed
177
	isc_result_t ret;
178
179
	isc_buffer_t *databuf = NULL;
	char data[256 + 8];
Michael Graff's avatar
Michael Graff committed
180
	isc_uint32_t flags;
Brian Wellington's avatar
Brian Wellington committed
181
	unsigned int sigsize;
182
	dns_fixedname_t fnewname;
Brian Wellington's avatar
Brian Wellington committed
183
184

	REQUIRE(name != NULL);
185
	REQUIRE(dns_name_countlabels(name) <= 255);
Brian Wellington's avatar
Brian Wellington committed
186
187
188
189
190
191
192
193
194
195
	REQUIRE(set != NULL);
	REQUIRE(key != NULL);
	REQUIRE(inception != NULL);
	REQUIRE(expire != NULL);
	REQUIRE(mctx != NULL);
	REQUIRE(sigrdata != NULL);

	if (*inception >= *expire)
		return (DNS_R_INVALIDTIME);

196
197
198
	/*
	 * Is the key allowed to sign data?
	 */
Brian Wellington's avatar
Brian Wellington committed
199
200
201
202
203
204
205
206
	flags = dst_key_flags(key);
	if (flags & DNS_KEYTYPE_NOAUTH)
		return (DNS_R_KEYUNAUTHORIZED);
	if ((flags & DNS_KEYFLAG_OWNERMASK) != DNS_KEYOWNER_ZONE)
		return (DNS_R_KEYUNAUTHORIZED);

	sig.mctx = mctx;
	sig.common.rdclass = set->rdclass;
207
	sig.common.rdtype = dns_rdatatype_rrsig;
Brian Wellington's avatar
Brian Wellington committed
208
209
	ISC_LINK_INIT(&sig.common, link);

210
211
	dns_name_init(&sig.signer, NULL);
	dns_name_clone(dst_key_name(key), &sig.signer);
Brian Wellington's avatar
Brian Wellington committed
212
213
214

	sig.covered = set->type;
	sig.algorithm = dst_key_alg(key);
215
	sig.labels = dns_name_countlabels(name) - 1;
Brian Wellington's avatar
Brian Wellington committed
216
217
218
219
220
221
	if (dns_name_iswildcard(name))
		sig.labels--;
	sig.originalttl = set->ttl;
	sig.timesigned = *inception;
	sig.timeexpire = *expire;
	sig.keyid = dst_key_id(key);
Brian Wellington's avatar
Brian Wellington committed
222
	ret = dst_key_sigsize(key, &sigsize);
Brian Wellington's avatar
Brian Wellington committed
223
224
225
	if (ret != ISC_R_SUCCESS)
		return (ret);
	sig.siglen = sigsize;
226
	/*
227
	 * The actual contents of sig.signature are not important yet, since
228
229
	 * they're not used in digest_sig().
	 */
Brian Wellington's avatar
Brian Wellington committed
230
231
	sig.signature = isc_mem_get(mctx, sig.siglen);
	if (sig.signature == NULL)
232
		return (ISC_R_NOMEMORY);
Brian Wellington's avatar
Brian Wellington committed
233

234
235
236
237
	ret = isc_buffer_allocate(mctx, &databuf, sigsize + 256 + 18);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_signature;

238
239
	dns_rdata_init(&tmpsigrdata);
	ret = dns_rdata_fromstruct(&tmpsigrdata, sig.common.rdclass,
240
				   sig.common.rdtype, &sig, databuf);
Brian Wellington's avatar
Brian Wellington committed
241
	if (ret != ISC_R_SUCCESS)
242
		goto cleanup_databuf;
Brian Wellington's avatar
Brian Wellington committed
243

Brian Wellington's avatar
Brian Wellington committed
244
245
	ret = dst_context_create(key, mctx, &ctx);
	if (ret != ISC_R_SUCCESS)
246
		goto cleanup_databuf;
Brian Wellington's avatar
Brian Wellington committed
247

248
249
250
	/*
	 * Digest the SIG rdata.
	 */
251
	ret = digest_sig(ctx, &tmpsigrdata, &sig);
Brian Wellington's avatar
Brian Wellington committed
252
	if (ret != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
253
		goto cleanup_context;
Brian Wellington's avatar
Brian Wellington committed
254

255
	dns_fixedname_init(&fnewname);
256
257
	RUNTIME_CHECK(dns_name_downcase(name, dns_fixedname_name(&fnewname),
					NULL) == ISC_R_SUCCESS);
258
	dns_name_toregion(dns_fixedname_name(&fnewname), &r);
Brian Wellington's avatar
Brian Wellington committed
259

260
261
262
	/*
	 * Create an envelope for each rdata: <name|type|class|ttl>.
	 */
263
	isc_buffer_init(&envbuf, data, sizeof(data));
Brian Wellington's avatar
Brian Wellington committed
264
265
266
267
268
	memcpy(data, r.base, r.length);
	isc_buffer_add(&envbuf, r.length);
	isc_buffer_putuint16(&envbuf, set->type);
	isc_buffer_putuint16(&envbuf, set->rdclass);
	isc_buffer_putuint32(&envbuf, set->ttl);
269

Brian Wellington's avatar
Brian Wellington committed
270
271
	ret = rdataset_to_sortedarray(set, mctx, &rdatas, &nrdatas);
	if (ret != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
272
		goto cleanup_context;
273
	isc_buffer_usedregion(&envbuf, &r);
Brian Wellington's avatar
Brian Wellington committed
274
275
276
277
278

	for (i = 0; i < nrdatas; i++) {
		isc_uint16_t len;
		isc_buffer_t lenbuf;
		isc_region_t lenr;
279

280
281
282
283
284
285
		/*
		 * Skip duplicates.
		 */
		if (i > 0 && dns_rdata_compare(&rdatas[i], &rdatas[i-1]) == 0)
		    continue;

286
287
288
		/*
		 * Digest the envelope.
		 */
Brian Wellington's avatar
Brian Wellington committed
289
		ret = dst_context_adddata(ctx, &r);
Brian Wellington's avatar
Brian Wellington committed
290
291
292
		if (ret != ISC_R_SUCCESS)
			goto cleanup_array;

293
294
295
		/*
		 * Digest the length of the rdata.
		 */
296
		isc_buffer_init(&lenbuf, &len, sizeof(len));
297
298
		INSIST(rdatas[i].length < 65536);
		isc_buffer_putuint16(&lenbuf, (isc_uint16_t)rdatas[i].length);
299
		isc_buffer_usedregion(&lenbuf, &lenr);
Brian Wellington's avatar
Brian Wellington committed
300
		ret = dst_context_adddata(ctx, &lenr);
Brian Wellington's avatar
Brian Wellington committed
301
302
303
		if (ret != ISC_R_SUCCESS)
			goto cleanup_array;

304
305
306
		/*
		 * Digest the rdata.
		 */
Brian Wellington's avatar
Brian Wellington committed
307
		ret = dns_rdata_digest(&rdatas[i], digest_callback, ctx);
Brian Wellington's avatar
Brian Wellington committed
308
309
310
		if (ret != ISC_R_SUCCESS)
			goto cleanup_array;
	}
311

312
	isc_buffer_init(&sigbuf, sig.signature, sig.siglen);
Brian Wellington's avatar
Brian Wellington committed
313
	ret = dst_context_sign(ctx, &sigbuf);
Brian Wellington's avatar
Brian Wellington committed
314
315
	if (ret != ISC_R_SUCCESS)
		goto cleanup_array;
316
	isc_buffer_usedregion(&sigbuf, &r);
Brian Wellington's avatar
Brian Wellington committed
317
	if (r.length != sig.siglen) {
318
		ret = ISC_R_NOSPACE;
Brian Wellington's avatar
Brian Wellington committed
319
320
321
322
323
324
325
326
327
		goto cleanup_array;
	}
	memcpy(sig.signature, r.base, sig.siglen);

	ret = dns_rdata_fromstruct(sigrdata, sig.common.rdclass,
				  sig.common.rdtype, &sig, buffer);

cleanup_array:
	isc_mem_put(mctx, rdatas, nrdatas * sizeof(dns_rdata_t));
Brian Wellington's avatar
Brian Wellington committed
328
329
cleanup_context:
	dst_context_destroy(&ctx);
330
331
332
cleanup_databuf:
	if (databuf != NULL)
		isc_buffer_free(&databuf);
Brian Wellington's avatar
Brian Wellington committed
333
334
335
336
337
338
339
cleanup_signature:
	isc_mem_put(mctx, sig.signature, sig.siglen);

	return (ret);
}

isc_result_t
340
341
342
dns_dnssec_verify2(dns_name_t *name, dns_rdataset_t *set, dst_key_t *key,
		   isc_boolean_t ignoretime, isc_mem_t *mctx,
		   dns_rdata_t *sigrdata, dns_name_t *wild)
Brian Wellington's avatar
Brian Wellington committed
343
{
344
	dns_rdata_rrsig_t sig;
345
	dns_fixedname_t fnewname;
Brian Wellington's avatar
Brian Wellington committed
346
347
348
349
350
351
352
	isc_region_t r;
	isc_buffer_t envbuf;
	dns_rdata_t *rdatas;
	int nrdatas, i;
	isc_stdtime_t now;
	isc_result_t ret;
	unsigned char data[300];
Brian Wellington's avatar
Brian Wellington committed
353
	dst_context_t *ctx = NULL;
354
	int labels = 0;
Michael Graff's avatar
Michael Graff committed
355
	isc_uint32_t flags;
Brian Wellington's avatar
Brian Wellington committed
356
357
358
359
360

	REQUIRE(name != NULL);
	REQUIRE(set != NULL);
	REQUIRE(key != NULL);
	REQUIRE(mctx != NULL);
361
	REQUIRE(sigrdata != NULL && sigrdata->type == dns_rdatatype_rrsig);
Brian Wellington's avatar
Brian Wellington committed
362

363
	ret = dns_rdata_tostruct(sigrdata, &sig, NULL);
Brian Wellington's avatar
Brian Wellington committed
364
365
366
	if (ret != ISC_R_SUCCESS)
		return (ret);

367
	if (isc_serial_lt(sig.timeexpire, sig.timesigned))
368
369
		return (DNS_R_SIGINVALID);

370
371
	if (!ignoretime) {
		isc_stdtime_get(&now);
Brian Wellington's avatar
Brian Wellington committed
372

373
374
375
		/*
		 * Is SIG temporally valid?
		 */
376
		if (isc_serial_lt((isc_uint32_t)now, sig.timesigned))
377
			return (DNS_R_SIGFUTURE);
378
		else if (isc_serial_lt(sig.timeexpire, (isc_uint32_t)now))
379
380
			return (DNS_R_SIGEXPIRED);
	}
Brian Wellington's avatar
Brian Wellington committed
381

382
383
384
	/*
	 * Is the key allowed to sign data?
	 */
Brian Wellington's avatar
Brian Wellington committed
385
386
387
388
389
390
	flags = dst_key_flags(key);
	if (flags & DNS_KEYTYPE_NOAUTH)
		return (DNS_R_KEYUNAUTHORIZED);
	if ((flags & DNS_KEYFLAG_OWNERMASK) != DNS_KEYOWNER_ZONE)
		return (DNS_R_KEYUNAUTHORIZED);

Brian Wellington's avatar
Brian Wellington committed
391
392
393
394
	ret = dst_context_create(key, mctx, &ctx);
	if (ret != ISC_R_SUCCESS)
		goto cleanup_struct;

395
396
397
398
	/*
	 * Digest the SIG rdata (not including the signature).
	 */
	ret = digest_sig(ctx, sigrdata, &sig);
Brian Wellington's avatar
Brian Wellington committed
399
	if (ret != ISC_R_SUCCESS)
400
		goto cleanup_context;
Brian Wellington's avatar
Brian Wellington committed
401

402
403
404
	/*
	 * If the name is an expanded wildcard, use the wildcard name.
	 */
405
	dns_fixedname_init(&fnewname);
406
	labels = dns_name_countlabels(name) - 1;
407
	if (labels - sig.labels > 0) {
408
409
		dns_name_split(name, sig.labels + 1, NULL,
			       dns_fixedname_name(&fnewname));
410
411
412
413
		RUNTIME_CHECK(dns_name_downcase(dns_fixedname_name(&fnewname),
						dns_fixedname_name(&fnewname),
						NULL)
			      == ISC_R_SUCCESS);
414
415
	}
	else
416
417
418
		dns_name_downcase(name, dns_fixedname_name(&fnewname), NULL);

	dns_name_toregion(dns_fixedname_name(&fnewname), &r);
Brian Wellington's avatar
Brian Wellington committed
419

420
421
422
	/*
	 * Create an envelope for each rdata: <name|type|class|ttl>.
	 */
423
	isc_buffer_init(&envbuf, data, sizeof(data));
Brian Wellington's avatar
Brian Wellington committed
424
425
426
427
428
429
430
431
432
433
	if (labels - sig.labels > 0) {
		isc_buffer_putuint8(&envbuf, 1);
		isc_buffer_putuint8(&envbuf, '*');
		memcpy(data + 2, r.base, r.length);
	}
	else
		memcpy(data, r.base, r.length);
	isc_buffer_add(&envbuf, r.length);
	isc_buffer_putuint16(&envbuf, set->type);
	isc_buffer_putuint16(&envbuf, set->rdclass);
434
	isc_buffer_putuint32(&envbuf, sig.originalttl);
Brian Wellington's avatar
Brian Wellington committed
435
436
437

	ret = rdataset_to_sortedarray(set, mctx, &rdatas, &nrdatas);
	if (ret != ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
438
439
		goto cleanup_context;

440
	isc_buffer_usedregion(&envbuf, &r);
Brian Wellington's avatar
Brian Wellington committed
441
442
443
444
445
446

	for (i = 0; i < nrdatas; i++) {
		isc_uint16_t len;
		isc_buffer_t lenbuf;
		isc_region_t lenr;

447
448
449
450
451
452
		/*
		 * Skip duplicates.
		 */
		if (i > 0 && dns_rdata_compare(&rdatas[i], &rdatas[i-1]) == 0)
		    continue;

453
454
455
		/*
		 * Digest the envelope.
		 */
Brian Wellington's avatar
Brian Wellington committed
456
		ret = dst_context_adddata(ctx, &r);
Brian Wellington's avatar
Brian Wellington committed
457
458
459
		if (ret != ISC_R_SUCCESS)
			goto cleanup_array;

460
461
462
		/*
		 * Digest the rdata length.
		 */
463
		isc_buffer_init(&lenbuf, &len, sizeof(len));
464
465
		INSIST(rdatas[i].length < 65536);
		isc_buffer_putuint16(&lenbuf, (isc_uint16_t)rdatas[i].length);
466
		isc_buffer_usedregion(&lenbuf, &lenr);
Brian Wellington's avatar
Brian Wellington committed
467

468
469
470
		/*
		 * Digest the rdata.
		 */
Brian Wellington's avatar
Brian Wellington committed
471
		ret = dst_context_adddata(ctx, &lenr);
Brian Wellington's avatar
Brian Wellington committed
472
473
		if (ret != ISC_R_SUCCESS)
			goto cleanup_array;
Brian Wellington's avatar
Brian Wellington committed
474
		ret = dns_rdata_digest(&rdatas[i], digest_callback, ctx);
Brian Wellington's avatar
Brian Wellington committed
475
476
477
478
479
480
		if (ret != ISC_R_SUCCESS)
			goto cleanup_array;
	}

	r.base = sig.signature;
	r.length = sig.siglen;
Brian Wellington's avatar
Brian Wellington committed
481
482
	ret = dst_context_verify(ctx, &r);
	if (ret == DST_R_VERIFYFAILURE)
Brian Wellington's avatar
Brian Wellington committed
483
484
485
486
		ret = DNS_R_SIGINVALID;

cleanup_array:
	isc_mem_put(mctx, rdatas, nrdatas * sizeof(dns_rdata_t));
Brian Wellington's avatar
Brian Wellington committed
487
488
cleanup_context:
	dst_context_destroy(&ctx);
Brian Wellington's avatar
Brian Wellington committed
489
490
491
cleanup_struct:
	dns_rdata_freestruct(&sig);

492
493
494
495
496
497
498
	if (ret == ISC_R_SUCCESS && labels - sig.labels > 0) {
		if (wild != NULL) 
			RUNTIME_CHECK(dns_name_concatenate(dns_wildcardname,
					         dns_fixedname_name(&fnewname),
						 wild, NULL) == ISC_R_SUCCESS);
		ret = DNS_R_FROMWILDCARD;
	}
Brian Wellington's avatar
Brian Wellington committed
499
500
501
	return (ret);
}

502
503
504
505
506
507
508
509
510
511
512
513
514
515
isc_result_t
dns_dnssec_verify(dns_name_t *name, dns_rdataset_t *set, dst_key_t *key,
		  isc_boolean_t ignoretime, isc_mem_t *mctx,
		  dns_rdata_t *sigrdata)
{
	isc_result_t result;

	result = dns_dnssec_verify2(name, set, key, ignoretime, mctx,
				    sigrdata, NULL);
	if (result == DNS_R_FROMWILDCARD)
		result = ISC_R_SUCCESS;
	return (result);
}

516
517
518
#define is_zone_key(key) ((dst_key_flags(key) & DNS_KEYFLAG_OWNERMASK) \
			  == DNS_KEYOWNER_ZONE)

519
isc_result_t
520
521
522
dns_dnssec_findzonekeys2(dns_db_t *db, dns_dbversion_t *ver,
			dns_dbnode_t *node, dns_name_t *name,
			const char *directory, isc_mem_t *mctx,
523
524
			unsigned int maxkeys, dst_key_t **keys,
			unsigned int *nkeys)
525
526
{
	dns_rdataset_t rdataset;
527
	dns_rdata_t rdata = DNS_RDATA_INIT;
528
	isc_result_t result;
Brian Wellington's avatar
Brian Wellington committed
529
	dst_key_t *pubkey = NULL;
530
531
532
533
	unsigned int count = 0;

	*nkeys = 0;
	dns_rdataset_init(&rdataset);
534
	RETERR(dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, 0, 0,
535
536
				   &rdataset, NULL));
	RETERR(dns_rdataset_first(&rdataset));
537
538
539
	while (result == ISC_R_SUCCESS && count < maxkeys) {
		pubkey = NULL;
		dns_rdataset_current(&rdataset, &rdata);
540
		RETERR(dns_dnssec_keyfromrdata(name, &rdata, mctx, &pubkey));
541
542
		if (!is_zone_key(pubkey))
			goto next;
543
		keys[count] = NULL;
544
545
546
		result = dst_key_fromfile(dst_key_name(pubkey),
					  dst_key_id(pubkey),
					  dst_key_alg(pubkey),
547
					  DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
548
					  directory,
549
					  mctx, &keys[count]);
550
		if (result == ISC_R_FILENOTFOUND)
551
552
553
554
			goto next;
		if (result != ISC_R_SUCCESS)
			goto failure;
		if ((dst_key_flags(keys[count]) & DNS_KEYTYPE_NOAUTH) != 0) {
555
			dst_key_free(&keys[count]);
556
			goto next;
557
		}
558
		count++;
559
 next:
560
		dst_key_free(&pubkey);
561
		dns_rdata_reset(&rdata);
562
563
		result = dns_rdataset_next(&rdataset);
	}
564
	if (result != ISC_R_NOMORE)
565
		goto failure;
566
	if (count == 0)
567
568
569
570
		result = ISC_R_NOTFOUND;
	else
		result = ISC_R_SUCCESS;

571
572
573
574
 failure:
	if (dns_rdataset_isassociated(&rdataset))
		dns_rdataset_disassociate(&rdataset);
	if (pubkey != NULL)
575
		dst_key_free(&pubkey);
576
577
578
	*nkeys = count;
	return (result);
}
Brian Wellington's avatar
Brian Wellington committed
579

580
581
582
583
584
585
586
587
588
589
isc_result_t
dns_dnssec_findzonekeys(dns_db_t *db, dns_dbversion_t *ver,
			dns_dbnode_t *node, dns_name_t *name, isc_mem_t *mctx,
			unsigned int maxkeys, dst_key_t **keys,
			unsigned int *nkeys)
{
	return (dns_dnssec_findzonekeys2(db, ver, node, name, NULL, mctx,
					 maxkeys, keys, nkeys));
}

Brian Wellington's avatar
Brian Wellington committed
590
591
isc_result_t
dns_dnssec_signmessage(dns_message_t *msg, dst_key_t *key) {
592
	dns_rdata_sig_t sig;	/* SIG(0) */
Brian Wellington's avatar
Brian Wellington committed
593
594
595
596
	unsigned char data[512];
	unsigned char header[DNS_MESSAGE_HEADERLEN];
	isc_buffer_t headerbuf, databuf, sigbuf;
	unsigned int sigsize;
Brian Wellington's avatar
Brian Wellington committed
597
	isc_buffer_t *dynbuf = NULL;
Brian Wellington's avatar
Brian Wellington committed
598
599
600
601
602
	dns_rdata_t *rdata;
	dns_rdatalist_t *datalist;
	dns_rdataset_t *dataset;
	isc_region_t r;
	isc_stdtime_t now;
Brian Wellington's avatar
Brian Wellington committed
603
	dst_context_t *ctx = NULL;
Brian Wellington's avatar
Brian Wellington committed
604
605
606
607
608
609
610
611
	isc_mem_t *mctx;
	isc_result_t result;
	isc_boolean_t signeedsfree = ISC_TRUE;

	REQUIRE(msg != NULL);
	REQUIRE(key != NULL);

	if (is_response(msg))
612
		REQUIRE(msg->query.base != NULL);
Brian Wellington's avatar
Brian Wellington committed
613
614
615

	mctx = msg->mctx;

616
	memset(&sig, 0, sizeof(sig));
Brian Wellington's avatar
Brian Wellington committed
617
618

	sig.mctx = mctx;
619
	sig.common.rdclass = dns_rdataclass_any;
620
	sig.common.rdtype = dns_rdatatype_sig;	/* SIG(0) */
Brian Wellington's avatar
Brian Wellington committed
621
622
623
624
	ISC_LINK_INIT(&sig.common, link);

	sig.covered = 0;
	sig.algorithm = dst_key_alg(key);
625
	sig.labels = 0; /* the root name */
Brian Wellington's avatar
Brian Wellington committed
626
627
	sig.originalttl = 0;

628
	isc_stdtime_get(&now);
Brian Wellington's avatar
Brian Wellington committed
629
630
631
632
633
	sig.timesigned = now - DNS_TSIG_FUDGE;
	sig.timeexpire = now + DNS_TSIG_FUDGE;

	sig.keyid = dst_key_id(key);

634
635
	dns_name_init(&sig.signer, NULL);
	dns_name_clone(dst_key_name(key), &sig.signer);
Brian Wellington's avatar
Brian Wellington committed
636
637
638

	sig.siglen = 0;
	sig.signature = NULL;
639

640
	isc_buffer_init(&databuf, data, sizeof(data));
Brian Wellington's avatar
Brian Wellington committed
641

Brian Wellington's avatar
Brian Wellington committed
642
	RETERR(dst_context_create(key, mctx, &ctx));
Brian Wellington's avatar
Brian Wellington committed
643

644
645
646
647
648
649
	/*
	 * Digest the fields of the SIG - we can cheat and use
	 * dns_rdata_fromstruct.  Since siglen is 0, the digested data
	 * is identical to dns format.
	 */
	RETERR(dns_rdata_fromstruct(NULL, dns_rdataclass_any,
650
651
				    dns_rdatatype_sig /* SIG(0) */,
				    &sig, &databuf));
652
653
654
655
656
657
	isc_buffer_usedregion(&databuf, &r);
	RETERR(dst_context_adddata(ctx, &r));

	/*
	 * If this is a response, digest the query.
	 */
Brian Wellington's avatar
Brian Wellington committed
658
	if (is_response(msg))
659
		RETERR(dst_context_adddata(ctx, &msg->query));
Brian Wellington's avatar
Brian Wellington committed
660

661
662
663
	/*
	 * Digest the header.
	 */
664
	isc_buffer_init(&headerbuf, header, sizeof(header));
Brian Wellington's avatar
Brian Wellington committed
665
	dns_message_renderheader(msg, &headerbuf);
666
	isc_buffer_usedregion(&headerbuf, &r);
Brian Wellington's avatar
Brian Wellington committed
667
	RETERR(dst_context_adddata(ctx, &r));
Brian Wellington's avatar
Brian Wellington committed
668

669
670
671
	/*
	 * Digest the remainder of the message.
	 */
672
	isc_buffer_usedregion(msg->buffer, &r);
Brian Wellington's avatar
Brian Wellington committed
673
	isc_region_consume(&r, DNS_MESSAGE_HEADERLEN);
Brian Wellington's avatar
Brian Wellington committed
674
	RETERR(dst_context_adddata(ctx, &r));
Brian Wellington's avatar
Brian Wellington committed
675

Brian Wellington's avatar
Brian Wellington committed
676
	RETERR(dst_key_sigsize(key, &sigsize));
Brian Wellington's avatar
Brian Wellington committed
677
678
679
680
681
682
683
	sig.siglen = sigsize;
	sig.signature = (unsigned char *) isc_mem_get(mctx, sig.siglen);
	if (sig.signature == NULL) {
		result = ISC_R_NOMEMORY;
		goto failure;
	}

684
	isc_buffer_init(&sigbuf, sig.signature, sig.siglen);
Brian Wellington's avatar
Brian Wellington committed
685
	RETERR(dst_context_sign(ctx, &sigbuf));
686
	dst_context_destroy(&ctx);
Brian Wellington's avatar
Brian Wellington committed
687
688
689

	rdata = NULL;
	RETERR(dns_message_gettemprdata(msg, &rdata));
690
	RETERR(isc_buffer_allocate(msg->mctx, &dynbuf, 1024));
691
	RETERR(dns_rdata_fromstruct(rdata, dns_rdataclass_any,
Mark Andrews's avatar
Mark Andrews committed
692
693
				    dns_rdatatype_sig /* SIG(0) */,
				    &sig, dynbuf));
Brian Wellington's avatar
Brian Wellington committed
694

695
	isc_mem_put(mctx, sig.signature, sig.siglen);
Brian Wellington's avatar
Brian Wellington committed
696
697
698
699
700
701
	signeedsfree = ISC_FALSE;

	dns_message_takebuffer(msg, &dynbuf);

	datalist = NULL;
	RETERR(dns_message_gettemprdatalist(msg, &datalist));
702
	datalist->rdclass = dns_rdataclass_any;
Mark Andrews's avatar
Mark Andrews committed
703
	datalist->type = dns_rdatatype_sig;	/* SIG(0) */
Brian Wellington's avatar
Brian Wellington committed
704
705
706
707
708
709
710
	datalist->covers = 0;
	datalist->ttl = 0;
	ISC_LIST_INIT(datalist->rdata);
	ISC_LIST_APPEND(datalist->rdata, rdata, link);
	dataset = NULL;
	RETERR(dns_message_gettemprdataset(msg, &dataset));
	dns_rdataset_init(dataset);
711
	RUNTIME_CHECK(dns_rdatalist_tordataset(datalist, dataset) == ISC_R_SUCCESS);
712
	msg->sig0 = dataset;
Brian Wellington's avatar
Brian Wellington committed
713
714
715
716
717
718

	return (ISC_R_SUCCESS);

failure:
	if (dynbuf != NULL)
		isc_buffer_free(&dynbuf);
719
	if (signeedsfree)
720
		isc_mem_put(mctx, sig.signature, sig.siglen);
Brian Wellington's avatar
Brian Wellington committed
721
722
	if (ctx != NULL)
		dst_context_destroy(&ctx);
Brian Wellington's avatar
Brian Wellington committed
723
724
725
726
727

	return (result);
}

isc_result_t
728
729
730
dns_dnssec_verifymessage(isc_buffer_t *source, dns_message_t *msg,
			 dst_key_t *key)
{
Mark Andrews's avatar
Mark Andrews committed
731
	dns_rdata_sig_t sig;	/* SIG(0) */
Brian Wellington's avatar
Brian Wellington committed
732
	unsigned char header[DNS_MESSAGE_HEADERLEN];
733
	dns_rdata_t rdata = DNS_RDATA_INIT;
734
	isc_region_t r, source_r, sig_r, header_r;
Brian Wellington's avatar
Brian Wellington committed
735
	isc_stdtime_t now;
Brian Wellington's avatar
Brian Wellington committed
736
	dst_context_t *ctx = NULL;
Brian Wellington's avatar
Brian Wellington committed
737
738
739
740
741
	isc_mem_t *mctx;
	isc_result_t result;
	isc_uint16_t addcount;
	isc_boolean_t signeedsfree = ISC_FALSE;

742
	REQUIRE(source != NULL);
Brian Wellington's avatar
Brian Wellington committed
743
744
745
746
747
	REQUIRE(msg != NULL);
	REQUIRE(key != NULL);

	mctx = msg->mctx;

748
749
	msg->verify_attempted = 1;

750
	if (is_response(msg)) {
751
		if (msg->query.base == NULL)
752
753
754
			return (DNS_R_UNEXPECTEDTSIG);
	}

755
	isc_buffer_usedregion(source, &source_r);
756

757
758
	RETERR(dns_rdataset_first(msg->sig0));
	dns_rdataset_current(msg->sig0, &rdata);
Brian Wellington's avatar
Brian Wellington committed
759

760
	RETERR(dns_rdata_tostruct(&rdata, &sig, NULL));
Brian Wellington's avatar
Brian Wellington committed
761
762
	signeedsfree = ISC_TRUE;

763
764
765
766
767
	if (sig.labels != 0) {
		result = DNS_R_SIGINVALID;
		goto failure;
	}

768
769
770
771
772
773
	if (isc_serial_lt(sig.timeexpire, sig.timesigned)) {
		result = DNS_R_SIGINVALID;
		msg->sig0status = dns_tsigerror_badtime;
		goto failure;
	}

774
	isc_stdtime_get(&now);
775
	if (isc_serial_lt((isc_uint32_t)now, sig.timesigned)) {
Brian Wellington's avatar
Brian Wellington committed
776
777
778
779
		result = DNS_R_SIGFUTURE;
		msg->sig0status = dns_tsigerror_badtime;
		goto failure;
	}
780
	else if (isc_serial_lt(sig.timeexpire, (isc_uint32_t)now)) {
Brian Wellington's avatar
Brian Wellington committed
781
782
783
784
785
		result = DNS_R_SIGEXPIRED;
		msg->sig0status = dns_tsigerror_badtime;
		goto failure;
	}

786
787
788
789
790
	if (!dns_name_equal(dst_key_name(key), &sig.signer)) {
		result = DNS_R_SIGINVALID;
		msg->sig0status = dns_tsigerror_badkey;
		goto failure;
	}
Brian Wellington's avatar
Brian Wellington committed
791

Brian Wellington's avatar
Brian Wellington committed
792
	RETERR(dst_context_create(key, mctx, &ctx));
Brian Wellington's avatar
Brian Wellington committed
793

794
795
796
797
798
799
800
	/*
 	 * Digest the SIG(0) record, except for the signature.
	 */
	dns_rdata_toregion(&rdata, &r);
	r.length -= sig.siglen;
	RETERR(dst_context_adddata(ctx, &r));

801
802
803
	/*
	 * If this is a response, digest the query.
	 */
Brian Wellington's avatar
Brian Wellington committed
804
	if (is_response(msg))
805
		RETERR(dst_context_adddata(ctx, &msg->query));
Brian Wellington's avatar
Brian Wellington committed
806

807
808
809
	/*
	 * Extract the header.
	 */
810
	memcpy(header, source_r.base, DNS_MESSAGE_HEADERLEN);
Brian Wellington's avatar
Brian Wellington committed
811

812
813
814
	/*
	 * Decrement the additional field counter.
	 */
Brian Wellington's avatar
Brian Wellington committed
815
	memcpy(&addcount, &header[DNS_MESSAGE_HEADERLEN - 2], 2);
Brian Wellington's avatar
Brian Wellington committed
816
	addcount = htons((isc_uint16_t)(ntohs(addcount) - 1));
Brian Wellington's avatar
Brian Wellington committed
817
818
	memcpy(&header[DNS_MESSAGE_HEADERLEN - 2], &addcount, 2);

819
820
821
	/*
	 * Digest the modified header.
	 */
Brian Wellington's avatar
Brian Wellington committed
822
823
	header_r.base = (unsigned char *) header;
	header_r.length = DNS_MESSAGE_HEADERLEN;
Brian Wellington's avatar
Brian Wellington committed
824
	RETERR(dst_context_adddata(ctx, &header_r));
Brian Wellington's avatar
Brian Wellington committed
825

826
827
828
	/*
	 * Digest all non-SIG(0) records.
	 */
829
	r.base = source_r.base + DNS_MESSAGE_HEADERLEN;
Brian Wellington's avatar
Brian Wellington committed
830
	r.length = msg->sigstart - DNS_MESSAGE_HEADERLEN;
Brian Wellington's avatar
Brian Wellington committed
831
	RETERR(dst_context_adddata(ctx, &r));
Brian Wellington's avatar
Brian Wellington committed
832
833
834

	sig_r.base = sig.signature;
	sig_r.length = sig.siglen;
Brian Wellington's avatar
Brian Wellington committed
835
	result = dst_context_verify(ctx, &sig_r);
Brian Wellington's avatar
Brian Wellington committed
836
837
838
839
840
	if (result != ISC_R_SUCCESS) {
		msg->sig0status = dns_tsigerror_badsig;
		goto failure;
	}

841
	msg->verified_sig = 1;
Brian Wellington's avatar
Brian Wellington committed
842

843
	dst_context_destroy(&ctx);
Brian Wellington's avatar
Brian Wellington committed
844
845
846
847
848
849
850
	dns_rdata_freestruct(&sig);

	return (ISC_R_SUCCESS);

failure:
	if (signeedsfree)
		dns_rdata_freestruct(&sig);
Brian Wellington's avatar
Brian Wellington committed
851
852
	if (ctx != NULL)
		dst_context_destroy(&ctx);
Brian Wellington's avatar
Brian Wellington committed
853
854
855

	return (result);
}