1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id$ */
19 
20 /* Reviewed: Thu Mar 16 13:39:43 PST 2000 by gson */
21 
22 #ifndef RDATA_ANY_255_TSIG_250_C
23 #define RDATA_ANY_255_TSIG_250_C
24 
25 #define RRTYPE_TSIG_ATTRIBUTES \
26 	(DNS_RDATATYPEATTR_META | DNS_RDATATYPEATTR_NOTQUESTION)
27 
28 static inline isc_result_t
fromtext_any_tsig(ARGS_FROMTEXT)29 fromtext_any_tsig(ARGS_FROMTEXT) {
30 	isc_token_t token;
31 	dns_name_t name;
32 	isc_uint64_t sigtime;
33 	isc_buffer_t buffer;
34 	dns_rcode_t rcode;
35 	long i;
36 	char *e;
37 
38 	REQUIRE(type == dns_rdatatype_tsig);
39 	REQUIRE(rdclass == dns_rdataclass_any);
40 
41 	UNUSED(type);
42 	UNUSED(rdclass);
43 	UNUSED(callbacks);
44 
45 	/*
46 	 * Algorithm Name.
47 	 */
48 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
49 				      ISC_FALSE));
50 	dns_name_init(&name, NULL);
51 	buffer_fromregion(&buffer, &token.value.as_region);
52 	if (origin == NULL)
53 		origin = dns_rootname;
54 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
55 
56 	/*
57 	 * Time Signed: 48 bits.
58 	 */
59 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
60 				      ISC_FALSE));
61 	sigtime = isc_string_touint64(DNS_AS_STR(token), &e, 10);
62 	if (*e != 0)
63 		RETTOK(DNS_R_SYNTAX);
64 	if ((sigtime >> 48) != 0)
65 		RETTOK(ISC_R_RANGE);
66 	RETERR(uint16_tobuffer((isc_uint16_t)(sigtime >> 32), target));
67 	RETERR(uint32_tobuffer((isc_uint32_t)(sigtime & 0xffffffffU), target));
68 
69 	/*
70 	 * Fudge.
71 	 */
72 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
73 				      ISC_FALSE));
74 	if (token.value.as_ulong > 0xffffU)
75 		RETTOK(ISC_R_RANGE);
76 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
77 
78 	/*
79 	 * Signature Size.
80 	 */
81 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
82 				      ISC_FALSE));
83 	if (token.value.as_ulong > 0xffffU)
84 		RETTOK(ISC_R_RANGE);
85 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
86 
87 	/*
88 	 * Signature.
89 	 */
90 	RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
91 
92 	/*
93 	 * Original ID.
94 	 */
95 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
96 				      ISC_FALSE));
97 	if (token.value.as_ulong > 0xffffU)
98 		RETTOK(ISC_R_RANGE);
99 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
100 
101 	/*
102 	 * Error.
103 	 */
104 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
105 				      ISC_FALSE));
106 	if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
107 				!= ISC_R_SUCCESS)
108 	{
109 		i = strtol(DNS_AS_STR(token), &e, 10);
110 		if (*e != 0)
111 			RETTOK(DNS_R_UNKNOWN);
112 		if (i < 0 || i > 0xffff)
113 			RETTOK(ISC_R_RANGE);
114 		rcode = (dns_rcode_t)i;
115 	}
116 	RETERR(uint16_tobuffer(rcode, target));
117 
118 	/*
119 	 * Other Len.
120 	 */
121 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
122 				      ISC_FALSE));
123 	if (token.value.as_ulong > 0xffffU)
124 		RETTOK(ISC_R_RANGE);
125 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
126 
127 	/*
128 	 * Other Data.
129 	 */
130 	return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
131 }
132 
133 static inline isc_result_t
totext_any_tsig(ARGS_TOTEXT)134 totext_any_tsig(ARGS_TOTEXT) {
135 	isc_region_t sr;
136 	isc_region_t sigr;
137 	char buf[sizeof(" 281474976710655 ")];
138 	char *bufp;
139 	dns_name_t name;
140 	dns_name_t prefix;
141 	isc_boolean_t sub;
142 	isc_uint64_t sigtime;
143 	unsigned short n;
144 
145 	REQUIRE(rdata->type == dns_rdatatype_tsig);
146 	REQUIRE(rdata->rdclass == dns_rdataclass_any);
147 	REQUIRE(rdata->length != 0);
148 
149 	dns_rdata_toregion(rdata, &sr);
150 	/*
151 	 * Algorithm Name.
152 	 */
153 	dns_name_init(&name, NULL);
154 	dns_name_init(&prefix, NULL);
155 	dns_name_fromregion(&name, &sr);
156 	sub = name_prefix(&name, tctx->origin, &prefix);
157 	RETERR(dns_name_totext(&prefix, sub, target));
158 	RETERR(str_totext(" ", target));
159 	isc_region_consume(&sr, name_length(&name));
160 
161 	/*
162 	 * Time Signed.
163 	 */
164 	sigtime = ((isc_uint64_t)sr.base[0] << 40) |
165 		  ((isc_uint64_t)sr.base[1] << 32) |
166 		  ((isc_uint64_t)sr.base[2] << 24) |
167 		  ((isc_uint64_t)sr.base[3] << 16) |
168 		  ((isc_uint64_t)sr.base[4] << 8) |
169 		  (isc_uint64_t)sr.base[5];
170 	isc_region_consume(&sr, 6);
171 	bufp = &buf[sizeof(buf) - 1];
172 	*bufp-- = 0;
173 	*bufp-- = ' ';
174 	do {
175 		*bufp-- = decdigits[sigtime % 10];
176 		sigtime /= 10;
177 	} while (sigtime != 0);
178 	bufp++;
179 	RETERR(str_totext(bufp, target));
180 
181 	/*
182 	 * Fudge.
183 	 */
184 	n = uint16_fromregion(&sr);
185 	isc_region_consume(&sr, 2);
186 	sprintf(buf, "%u ", n);
187 	RETERR(str_totext(buf, target));
188 
189 	/*
190 	 * Signature Size.
191 	 */
192 	n = uint16_fromregion(&sr);
193 	isc_region_consume(&sr, 2);
194 	sprintf(buf, "%u", n);
195 	RETERR(str_totext(buf, target));
196 
197 	/*
198 	 * Signature.
199 	 */
200 	REQUIRE(n <= sr.length);
201 	sigr = sr;
202 	sigr.length = n;
203 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
204 		RETERR(str_totext(" (", target));
205 	RETERR(str_totext(tctx->linebreak, target));
206 	if (tctx->width == 0)   /* No splitting */
207 		RETERR(isc_base64_totext(&sigr, 60, "", target));
208 	else
209 		RETERR(isc_base64_totext(&sigr, tctx->width - 2,
210 					 tctx->linebreak, target));
211 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
212 		RETERR(str_totext(" ) ", target));
213 	else
214 		RETERR(str_totext(" ", target));
215 	isc_region_consume(&sr, n);
216 
217 	/*
218 	 * Original ID.
219 	 */
220 	n = uint16_fromregion(&sr);
221 	isc_region_consume(&sr, 2);
222 	sprintf(buf, "%u ", n);
223 	RETERR(str_totext(buf, target));
224 
225 	/*
226 	 * Error.
227 	 */
228 	n = uint16_fromregion(&sr);
229 	isc_region_consume(&sr, 2);
230 	RETERR(dns_tsigrcode_totext((dns_rcode_t)n, target));
231 
232 	/*
233 	 * Other Size.
234 	 */
235 	n = uint16_fromregion(&sr);
236 	isc_region_consume(&sr, 2);
237 	sprintf(buf, " %u ", n);
238 	RETERR(str_totext(buf, target));
239 
240 	/*
241 	 * Other.
242 	 */
243 	if (tctx->width == 0)   /* No splitting */
244 		return (isc_base64_totext(&sr, 60, "", target));
245 	else
246 		return (isc_base64_totext(&sr, 60, " ", target));
247 }
248 
249 static inline isc_result_t
fromwire_any_tsig(ARGS_FROMWIRE)250 fromwire_any_tsig(ARGS_FROMWIRE) {
251 	isc_region_t sr;
252 	dns_name_t name;
253 	unsigned long n;
254 
255 	REQUIRE(type == dns_rdatatype_tsig);
256 	REQUIRE(rdclass == dns_rdataclass_any);
257 
258 	UNUSED(type);
259 	UNUSED(rdclass);
260 
261 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
262 
263 	/*
264 	 * Algorithm Name.
265 	 */
266 	dns_name_init(&name, NULL);
267 	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
268 
269 	isc_buffer_activeregion(source, &sr);
270 	/*
271 	 * Time Signed + Fudge.
272 	 */
273 	if (sr.length < 8)
274 		return (ISC_R_UNEXPECTEDEND);
275 	RETERR(mem_tobuffer(target, sr.base, 8));
276 	isc_region_consume(&sr, 8);
277 	isc_buffer_forward(source, 8);
278 
279 	/*
280 	 * Signature Length + Signature.
281 	 */
282 	if (sr.length < 2)
283 		return (ISC_R_UNEXPECTEDEND);
284 	n = uint16_fromregion(&sr);
285 	if (sr.length < n + 2)
286 		return (ISC_R_UNEXPECTEDEND);
287 	RETERR(mem_tobuffer(target, sr.base, n + 2));
288 	isc_region_consume(&sr, n + 2);
289 	isc_buffer_forward(source, n + 2);
290 
291 	/*
292 	 * Original ID + Error.
293 	 */
294 	if (sr.length < 4)
295 		return (ISC_R_UNEXPECTEDEND);
296 	RETERR(mem_tobuffer(target, sr.base,  4));
297 	isc_region_consume(&sr, 4);
298 	isc_buffer_forward(source, 4);
299 
300 	/*
301 	 * Other Length + Other.
302 	 */
303 	if (sr.length < 2)
304 		return (ISC_R_UNEXPECTEDEND);
305 	n = uint16_fromregion(&sr);
306 	if (sr.length < n + 2)
307 		return (ISC_R_UNEXPECTEDEND);
308 	isc_buffer_forward(source, n + 2);
309 	return (mem_tobuffer(target, sr.base, n + 2));
310 }
311 
312 static inline isc_result_t
towire_any_tsig(ARGS_TOWIRE)313 towire_any_tsig(ARGS_TOWIRE) {
314 	isc_region_t sr;
315 	dns_name_t name;
316 	dns_offsets_t offsets;
317 
318 	REQUIRE(rdata->type == dns_rdatatype_tsig);
319 	REQUIRE(rdata->rdclass == dns_rdataclass_any);
320 	REQUIRE(rdata->length != 0);
321 
322 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
323 	dns_rdata_toregion(rdata, &sr);
324 	dns_name_init(&name, offsets);
325 	dns_name_fromregion(&name, &sr);
326 	RETERR(dns_name_towire(&name, cctx, target));
327 	isc_region_consume(&sr, name_length(&name));
328 	return (mem_tobuffer(target, sr.base, sr.length));
329 }
330 
331 static inline int
compare_any_tsig(ARGS_COMPARE)332 compare_any_tsig(ARGS_COMPARE) {
333 	isc_region_t r1;
334 	isc_region_t r2;
335 	dns_name_t name1;
336 	dns_name_t name2;
337 	int order;
338 
339 	REQUIRE(rdata1->type == rdata2->type);
340 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
341 	REQUIRE(rdata1->type == dns_rdatatype_tsig);
342 	REQUIRE(rdata1->rdclass == dns_rdataclass_any);
343 	REQUIRE(rdata1->length != 0);
344 	REQUIRE(rdata2->length != 0);
345 
346 	dns_rdata_toregion(rdata1, &r1);
347 	dns_rdata_toregion(rdata2, &r2);
348 	dns_name_init(&name1, NULL);
349 	dns_name_init(&name2, NULL);
350 	dns_name_fromregion(&name1, &r1);
351 	dns_name_fromregion(&name2, &r2);
352 	order = dns_name_rdatacompare(&name1, &name2);
353 	if (order != 0)
354 		return (order);
355 	isc_region_consume(&r1, name_length(&name1));
356 	isc_region_consume(&r2, name_length(&name2));
357 	return (isc_region_compare(&r1, &r2));
358 }
359 
360 static inline isc_result_t
fromstruct_any_tsig(ARGS_FROMSTRUCT)361 fromstruct_any_tsig(ARGS_FROMSTRUCT) {
362 	dns_rdata_any_tsig_t *tsig = source;
363 	isc_region_t tr;
364 
365 	REQUIRE(type == dns_rdatatype_tsig);
366 	REQUIRE(rdclass == dns_rdataclass_any);
367 	REQUIRE(source != NULL);
368 	REQUIRE(tsig->common.rdclass == rdclass);
369 	REQUIRE(tsig->common.rdtype == type);
370 
371 	UNUSED(type);
372 	UNUSED(rdclass);
373 
374 	/*
375 	 * Algorithm Name.
376 	 */
377 	RETERR(name_tobuffer(&tsig->algorithm, target));
378 
379 	isc_buffer_availableregion(target, &tr);
380 	if (tr.length < 6 + 2 + 2)
381 		return (ISC_R_NOSPACE);
382 
383 	/*
384 	 * Time Signed: 48 bits.
385 	 */
386 	RETERR(uint16_tobuffer((isc_uint16_t)(tsig->timesigned >> 32),
387 			       target));
388 	RETERR(uint32_tobuffer((isc_uint32_t)(tsig->timesigned & 0xffffffffU),
389 			       target));
390 
391 	/*
392 	 * Fudge.
393 	 */
394 	RETERR(uint16_tobuffer(tsig->fudge, target));
395 
396 	/*
397 	 * Signature Size.
398 	 */
399 	RETERR(uint16_tobuffer(tsig->siglen, target));
400 
401 	/*
402 	 * Signature.
403 	 */
404 	RETERR(mem_tobuffer(target, tsig->signature, tsig->siglen));
405 
406 	isc_buffer_availableregion(target, &tr);
407 	if (tr.length < 2 + 2 + 2)
408 		return (ISC_R_NOSPACE);
409 
410 	/*
411 	 * Original ID.
412 	 */
413 	RETERR(uint16_tobuffer(tsig->originalid, target));
414 
415 	/*
416 	 * Error.
417 	 */
418 	RETERR(uint16_tobuffer(tsig->error, target));
419 
420 	/*
421 	 * Other Len.
422 	 */
423 	RETERR(uint16_tobuffer(tsig->otherlen, target));
424 
425 	/*
426 	 * Other Data.
427 	 */
428 	return (mem_tobuffer(target, tsig->other, tsig->otherlen));
429 }
430 
431 static inline isc_result_t
tostruct_any_tsig(ARGS_TOSTRUCT)432 tostruct_any_tsig(ARGS_TOSTRUCT) {
433 	dns_rdata_any_tsig_t *tsig;
434 	dns_name_t alg;
435 	isc_region_t sr;
436 
437 	REQUIRE(rdata->type == dns_rdatatype_tsig);
438 	REQUIRE(rdata->rdclass == dns_rdataclass_any);
439 	REQUIRE(rdata->length != 0);
440 
441 	tsig = (dns_rdata_any_tsig_t *) target;
442 	tsig->common.rdclass = rdata->rdclass;
443 	tsig->common.rdtype = rdata->type;
444 	ISC_LINK_INIT(&tsig->common, link);
445 
446 	dns_rdata_toregion(rdata, &sr);
447 
448 	/*
449 	 * Algorithm Name.
450 	 */
451 	dns_name_init(&alg, NULL);
452 	dns_name_fromregion(&alg, &sr);
453 	dns_name_init(&tsig->algorithm, NULL);
454 	RETERR(name_duporclone(&alg, mctx, &tsig->algorithm));
455 
456 	isc_region_consume(&sr, name_length(&tsig->algorithm));
457 
458 	/*
459 	 * Time Signed.
460 	 */
461 	INSIST(sr.length >= 6);
462 	tsig->timesigned = ((isc_uint64_t)sr.base[0] << 40) |
463 			   ((isc_uint64_t)sr.base[1] << 32) |
464 			   ((isc_uint64_t)sr.base[2] << 24) |
465 			   ((isc_uint64_t)sr.base[3] << 16) |
466 			   ((isc_uint64_t)sr.base[4] << 8) |
467 			   (isc_uint64_t)sr.base[5];
468 	isc_region_consume(&sr, 6);
469 
470 	/*
471 	 * Fudge.
472 	 */
473 	tsig->fudge = uint16_fromregion(&sr);
474 	isc_region_consume(&sr, 2);
475 
476 	/*
477 	 * Signature Size.
478 	 */
479 	tsig->siglen = uint16_fromregion(&sr);
480 	isc_region_consume(&sr, 2);
481 
482 	/*
483 	 * Signature.
484 	 */
485 	INSIST(sr.length >= tsig->siglen);
486 	tsig->signature = mem_maybedup(mctx, sr.base, tsig->siglen);
487 	if (tsig->signature == NULL)
488 		goto cleanup;
489 	isc_region_consume(&sr, tsig->siglen);
490 
491 	/*
492 	 * Original ID.
493 	 */
494 	tsig->originalid = uint16_fromregion(&sr);
495 	isc_region_consume(&sr, 2);
496 
497 	/*
498 	 * Error.
499 	 */
500 	tsig->error = uint16_fromregion(&sr);
501 	isc_region_consume(&sr, 2);
502 
503 	/*
504 	 * Other Size.
505 	 */
506 	tsig->otherlen = uint16_fromregion(&sr);
507 	isc_region_consume(&sr, 2);
508 
509 	/*
510 	 * Other.
511 	 */
512 	INSIST(sr.length == tsig->otherlen);
513 	tsig->other = mem_maybedup(mctx, sr.base, tsig->otherlen);
514 	if (tsig->other == NULL)
515 		goto cleanup;
516 
517 	tsig->mctx = mctx;
518 	return (ISC_R_SUCCESS);
519 
520  cleanup:
521 	if (mctx != NULL)
522 		dns_name_free(&tsig->algorithm, tsig->mctx);
523 	if (mctx != NULL && tsig->signature != NULL)
524 		isc_mem_free(mctx, tsig->signature);
525 	return (ISC_R_NOMEMORY);
526 }
527 
528 static inline void
freestruct_any_tsig(ARGS_FREESTRUCT)529 freestruct_any_tsig(ARGS_FREESTRUCT) {
530 	dns_rdata_any_tsig_t *tsig = (dns_rdata_any_tsig_t *) source;
531 
532 	REQUIRE(source != NULL);
533 	REQUIRE(tsig->common.rdtype == dns_rdatatype_tsig);
534 	REQUIRE(tsig->common.rdclass == dns_rdataclass_any);
535 
536 	if (tsig->mctx == NULL)
537 		return;
538 
539 	dns_name_free(&tsig->algorithm, tsig->mctx);
540 	if (tsig->signature != NULL)
541 		isc_mem_free(tsig->mctx, tsig->signature);
542 	if (tsig->other != NULL)
543 		isc_mem_free(tsig->mctx, tsig->other);
544 	tsig->mctx = NULL;
545 }
546 
547 static inline isc_result_t
additionaldata_any_tsig(ARGS_ADDLDATA)548 additionaldata_any_tsig(ARGS_ADDLDATA) {
549 	REQUIRE(rdata->type == dns_rdatatype_tsig);
550 	REQUIRE(rdata->rdclass == dns_rdataclass_any);
551 
552 	UNUSED(rdata);
553 	UNUSED(add);
554 	UNUSED(arg);
555 
556 	return (ISC_R_SUCCESS);
557 }
558 
559 static inline isc_result_t
digest_any_tsig(ARGS_DIGEST)560 digest_any_tsig(ARGS_DIGEST) {
561 
562 	REQUIRE(rdata->type == dns_rdatatype_tsig);
563 	REQUIRE(rdata->rdclass == dns_rdataclass_any);
564 
565 	UNUSED(rdata);
566 	UNUSED(digest);
567 	UNUSED(arg);
568 
569 	return (ISC_R_NOTIMPLEMENTED);
570 }
571 
572 static inline isc_boolean_t
checkowner_any_tsig(ARGS_CHECKOWNER)573 checkowner_any_tsig(ARGS_CHECKOWNER) {
574 
575 	REQUIRE(type == dns_rdatatype_tsig);
576 	REQUIRE(rdclass == dns_rdataclass_any);
577 
578 	UNUSED(name);
579 	UNUSED(type);
580 	UNUSED(rdclass);
581 	UNUSED(wildcard);
582 
583 	return (ISC_TRUE);
584 }
585 
586 static inline isc_boolean_t
checknames_any_tsig(ARGS_CHECKNAMES)587 checknames_any_tsig(ARGS_CHECKNAMES) {
588 
589 	REQUIRE(rdata->type == dns_rdatatype_tsig);
590 	REQUIRE(rdata->rdclass == dns_rdataclass_any);
591 
592 	UNUSED(rdata);
593 	UNUSED(owner);
594 	UNUSED(bad);
595 
596 	return (ISC_TRUE);
597 }
598 
599 static inline int
casecompare_any_tsig(ARGS_COMPARE)600 casecompare_any_tsig(ARGS_COMPARE) {
601 	return (compare_any_tsig(rdata1, rdata2));
602 }
603 
604 #endif	/* RDATA_ANY_255_TSIG_250_C */
605