4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 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: tsig_250.c,v 1.65 2009/12/04 22:06:37 tbox Exp */
22 /* Reviewed: Thu Mar 16 13:39:43 PST 2000 by gson */
24 #ifndef RDATA_ANY_255_TSIG_250_C
25 #define RDATA_ANY_255_TSIG_250_C
27 #define RRTYPE_TSIG_ATTRIBUTES \
28 (DNS_RDATATYPEATTR_META | DNS_RDATATYPEATTR_NOTQUESTION)
30 static inline isc_result_t
31 fromtext_any_tsig(ARGS_FROMTEXT
) {
41 REQUIRE(rdclass
== 255);
50 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
52 dns_name_init(&name
, NULL
);
53 buffer_fromregion(&buffer
, &token
.value
.as_region
);
54 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
55 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
58 * Time Signed: 48 bits.
60 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
62 sigtime
= isc_string_touint64(DNS_AS_STR(token
), &e
, 10);
65 if ((sigtime
>> 48) != 0)
67 RETERR(uint16_tobuffer((isc_uint16_t
)(sigtime
>> 32), target
));
68 RETERR(uint32_tobuffer((isc_uint32_t
)(sigtime
& 0xffffffffU
), target
));
73 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
75 if (token
.value
.as_ulong
> 0xffffU
)
77 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
82 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
84 if (token
.value
.as_ulong
> 0xffffU
)
86 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
91 RETERR(isc_base64_tobuffer(lexer
, target
, (int)token
.value
.as_ulong
));
96 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
98 if (token
.value
.as_ulong
> 0xffffU
)
100 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
105 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
107 if (dns_tsigrcode_fromtext(&rcode
, &token
.value
.as_textregion
)
110 i
= strtol(DNS_AS_STR(token
), &e
, 10);
112 RETTOK(DNS_R_UNKNOWN
);
113 if (i
< 0 || i
> 0xffff)
115 rcode
= (dns_rcode_t
)i
;
117 RETERR(uint16_tobuffer(rcode
, target
));
122 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
124 if (token
.value
.as_ulong
> 0xffffU
)
126 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
131 return (isc_base64_tobuffer(lexer
, target
, (int)token
.value
.as_ulong
));
134 static inline isc_result_t
135 totext_any_tsig(ARGS_TOTEXT
) {
138 char buf
[sizeof("281474976710655 ")];
143 isc_uint64_t sigtime
;
146 REQUIRE(rdata
->type
== 250);
147 REQUIRE(rdata
->rdclass
== 255);
148 REQUIRE(rdata
->length
!= 0);
150 dns_rdata_toregion(rdata
, &sr
);
154 dns_name_init(&name
, NULL
);
155 dns_name_init(&prefix
, NULL
);
156 dns_name_fromregion(&name
, &sr
);
157 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
158 RETERR(dns_name_totext(&prefix
, sub
, target
));
159 RETERR(str_totext(" ", target
));
160 isc_region_consume(&sr
, name_length(&name
));
165 sigtime
= ((isc_uint64_t
)sr
.base
[0] << 40) |
166 ((isc_uint64_t
)sr
.base
[1] << 32) |
167 ((isc_uint64_t
)sr
.base
[2] << 24) |
168 ((isc_uint64_t
)sr
.base
[3] << 16) |
169 ((isc_uint64_t
)sr
.base
[4] << 8) |
170 (isc_uint64_t
)sr
.base
[5];
171 isc_region_consume(&sr
, 6);
172 bufp
= &buf
[sizeof(buf
) - 1];
176 *bufp
-- = decdigits
[sigtime
% 10];
178 } while (sigtime
!= 0);
180 RETERR(str_totext(bufp
, target
));
185 n
= uint16_fromregion(&sr
);
186 isc_region_consume(&sr
, 2);
187 sprintf(buf
, "%u ", n
);
188 RETERR(str_totext(buf
, target
));
193 n
= uint16_fromregion(&sr
);
194 isc_region_consume(&sr
, 2);
195 sprintf(buf
, "%u", n
);
196 RETERR(str_totext(buf
, target
));
201 REQUIRE(n
<= sr
.length
);
204 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
205 RETERR(str_totext(" (", target
));
206 RETERR(str_totext(tctx
->linebreak
, target
));
207 RETERR(isc_base64_totext(&sigr
, tctx
->width
- 2,
208 tctx
->linebreak
, target
));
209 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
210 RETERR(str_totext(" ) ", target
));
212 RETERR(str_totext(" ", target
));
213 isc_region_consume(&sr
, n
);
218 n
= uint16_fromregion(&sr
);
219 isc_region_consume(&sr
, 2);
220 sprintf(buf
, "%u ", n
);
221 RETERR(str_totext(buf
, target
));
226 n
= uint16_fromregion(&sr
);
227 isc_region_consume(&sr
, 2);
228 if (dns_tsigrcode_totext((dns_rcode_t
)n
, target
) == ISC_R_SUCCESS
)
229 RETERR(str_totext(" ", target
));
231 sprintf(buf
, "%u ", n
);
232 RETERR(str_totext(buf
, target
));
238 n
= uint16_fromregion(&sr
);
239 isc_region_consume(&sr
, 2);
240 sprintf(buf
, "%u ", n
);
241 RETERR(str_totext(buf
, target
));
246 return (isc_base64_totext(&sr
, 60, " ", target
));
249 static inline isc_result_t
250 fromwire_any_tsig(ARGS_FROMWIRE
) {
255 REQUIRE(type
== 250);
256 REQUIRE(rdclass
== 255);
261 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
266 dns_name_init(&name
, NULL
);
267 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
269 isc_buffer_activeregion(source
, &sr
);
271 * Time Signed + Fudge.
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);
280 * Signature Length + Signature.
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);
292 * Original ID + Error.
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);
301 * Other Length + Other.
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));
312 static inline isc_result_t
313 towire_any_tsig(ARGS_TOWIRE
) {
316 dns_offsets_t offsets
;
318 REQUIRE(rdata
->type
== 250);
319 REQUIRE(rdata
->rdclass
== 255);
320 REQUIRE(rdata
->length
!= 0);
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
));
332 compare_any_tsig(ARGS_COMPARE
) {
339 REQUIRE(rdata1
->type
== rdata2
->type
);
340 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
341 REQUIRE(rdata1
->type
== 250);
342 REQUIRE(rdata1
->rdclass
== 255);
343 REQUIRE(rdata1
->length
!= 0);
344 REQUIRE(rdata2
->length
!= 0);
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
);
355 isc_region_consume(&r1
, name_length(&name1
));
356 isc_region_consume(&r2
, name_length(&name2
));
357 return (isc_region_compare(&r1
, &r2
));
360 static inline isc_result_t
361 fromstruct_any_tsig(ARGS_FROMSTRUCT
) {
362 dns_rdata_any_tsig_t
*tsig
= source
;
365 REQUIRE(type
== 250);
366 REQUIRE(rdclass
== 255);
367 REQUIRE(source
!= NULL
);
368 REQUIRE(tsig
->common
.rdclass
== rdclass
);
369 REQUIRE(tsig
->common
.rdtype
== type
);
377 RETERR(name_tobuffer(&tsig
->algorithm
, target
));
379 isc_buffer_availableregion(target
, &tr
);
380 if (tr
.length
< 6 + 2 + 2)
381 return (ISC_R_NOSPACE
);
384 * Time Signed: 48 bits.
386 RETERR(uint16_tobuffer((isc_uint16_t
)(tsig
->timesigned
>> 32),
388 RETERR(uint32_tobuffer((isc_uint32_t
)(tsig
->timesigned
& 0xffffffffU
),
394 RETERR(uint16_tobuffer(tsig
->fudge
, target
));
399 RETERR(uint16_tobuffer(tsig
->siglen
, target
));
404 RETERR(mem_tobuffer(target
, tsig
->signature
, tsig
->siglen
));
406 isc_buffer_availableregion(target
, &tr
);
407 if (tr
.length
< 2 + 2 + 2)
408 return (ISC_R_NOSPACE
);
413 RETERR(uint16_tobuffer(tsig
->originalid
, target
));
418 RETERR(uint16_tobuffer(tsig
->error
, target
));
423 RETERR(uint16_tobuffer(tsig
->otherlen
, target
));
428 return (mem_tobuffer(target
, tsig
->other
, tsig
->otherlen
));
431 static inline isc_result_t
432 tostruct_any_tsig(ARGS_TOSTRUCT
) {
433 dns_rdata_any_tsig_t
*tsig
;
437 REQUIRE(rdata
->type
== 250);
438 REQUIRE(rdata
->rdclass
== 255);
439 REQUIRE(rdata
->length
!= 0);
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
);
446 dns_rdata_toregion(rdata
, &sr
);
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
));
456 isc_region_consume(&sr
, name_length(&tsig
->algorithm
));
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);
473 tsig
->fudge
= uint16_fromregion(&sr
);
474 isc_region_consume(&sr
, 2);
479 tsig
->siglen
= uint16_fromregion(&sr
);
480 isc_region_consume(&sr
, 2);
485 INSIST(sr
.length
>= tsig
->siglen
);
486 tsig
->signature
= mem_maybedup(mctx
, sr
.base
, tsig
->siglen
);
487 if (tsig
->signature
== NULL
)
489 isc_region_consume(&sr
, tsig
->siglen
);
494 tsig
->originalid
= uint16_fromregion(&sr
);
495 isc_region_consume(&sr
, 2);
500 tsig
->error
= uint16_fromregion(&sr
);
501 isc_region_consume(&sr
, 2);
506 tsig
->otherlen
= uint16_fromregion(&sr
);
507 isc_region_consume(&sr
, 2);
512 INSIST(sr
.length
== tsig
->otherlen
);
513 tsig
->other
= mem_maybedup(mctx
, sr
.base
, tsig
->otherlen
);
514 if (tsig
->other
== NULL
)
518 return (ISC_R_SUCCESS
);
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
);
529 freestruct_any_tsig(ARGS_FREESTRUCT
) {
530 dns_rdata_any_tsig_t
*tsig
= (dns_rdata_any_tsig_t
*) source
;
532 REQUIRE(source
!= NULL
);
533 REQUIRE(tsig
->common
.rdclass
== 255);
534 REQUIRE(tsig
->common
.rdtype
== 250);
536 if (tsig
->mctx
== NULL
)
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
);
547 static inline isc_result_t
548 additionaldata_any_tsig(ARGS_ADDLDATA
) {
549 REQUIRE(rdata
->type
== 250);
550 REQUIRE(rdata
->rdclass
== 255);
556 return (ISC_R_SUCCESS
);
559 static inline isc_result_t
560 digest_any_tsig(ARGS_DIGEST
) {
562 REQUIRE(rdata
->type
== 250);
563 REQUIRE(rdata
->rdclass
== 255);
569 return (ISC_R_NOTIMPLEMENTED
);
572 static inline isc_boolean_t
573 checkowner_any_tsig(ARGS_CHECKOWNER
) {
575 REQUIRE(type
== 250);
576 REQUIRE(rdclass
== 255);
586 static inline isc_boolean_t
587 checknames_any_tsig(ARGS_CHECKNAMES
) {
589 REQUIRE(rdata
->type
== 250);
590 REQUIRE(rdata
->rdclass
== 250);
600 casecompare_any_tsig(ARGS_COMPARE
) {
601 return (compare_any_tsig(rdata1
, rdata2
));
604 #endif /* RDATA_ANY_255_TSIG_250_C */