1 /* $NetBSD: tsig_250.c,v 1.6 2014/12/10 04:37:58 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012 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.
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 if (tctx
->width
== 0) /* No splitting */
208 RETERR(isc_base64_totext(&sigr
, 60, "", target
));
210 RETERR(isc_base64_totext(&sigr
, tctx
->width
- 2,
211 tctx
->linebreak
, target
));
212 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
213 RETERR(str_totext(" ) ", target
));
215 RETERR(str_totext(" ", target
));
216 isc_region_consume(&sr
, n
);
221 n
= uint16_fromregion(&sr
);
222 isc_region_consume(&sr
, 2);
223 sprintf(buf
, "%u ", n
);
224 RETERR(str_totext(buf
, target
));
229 n
= uint16_fromregion(&sr
);
230 isc_region_consume(&sr
, 2);
231 RETERR(dns_tsigrcode_totext((dns_rcode_t
)n
, target
));
236 n
= uint16_fromregion(&sr
);
237 isc_region_consume(&sr
, 2);
238 sprintf(buf
, " %u ", n
);
239 RETERR(str_totext(buf
, target
));
244 if (tctx
->width
== 0) /* No splitting */
245 return (isc_base64_totext(&sr
, 60, "", target
));
247 return (isc_base64_totext(&sr
, 60, " ", target
));
250 static inline isc_result_t
251 fromwire_any_tsig(ARGS_FROMWIRE
) {
256 REQUIRE(type
== 250);
257 REQUIRE(rdclass
== 255);
262 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
267 dns_name_init(&name
, NULL
);
268 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
270 isc_buffer_activeregion(source
, &sr
);
272 * Time Signed + Fudge.
275 return (ISC_R_UNEXPECTEDEND
);
276 RETERR(mem_tobuffer(target
, sr
.base
, 8));
277 isc_region_consume(&sr
, 8);
278 isc_buffer_forward(source
, 8);
281 * Signature Length + Signature.
284 return (ISC_R_UNEXPECTEDEND
);
285 n
= uint16_fromregion(&sr
);
286 if (sr
.length
< n
+ 2)
287 return (ISC_R_UNEXPECTEDEND
);
288 RETERR(mem_tobuffer(target
, sr
.base
, n
+ 2));
289 isc_region_consume(&sr
, n
+ 2);
290 isc_buffer_forward(source
, n
+ 2);
293 * Original ID + Error.
296 return (ISC_R_UNEXPECTEDEND
);
297 RETERR(mem_tobuffer(target
, sr
.base
, 4));
298 isc_region_consume(&sr
, 4);
299 isc_buffer_forward(source
, 4);
302 * Other Length + Other.
305 return (ISC_R_UNEXPECTEDEND
);
306 n
= uint16_fromregion(&sr
);
307 if (sr
.length
< n
+ 2)
308 return (ISC_R_UNEXPECTEDEND
);
309 isc_buffer_forward(source
, n
+ 2);
310 return (mem_tobuffer(target
, sr
.base
, n
+ 2));
313 static inline isc_result_t
314 towire_any_tsig(ARGS_TOWIRE
) {
317 dns_offsets_t offsets
;
319 REQUIRE(rdata
->type
== 250);
320 REQUIRE(rdata
->rdclass
== 255);
321 REQUIRE(rdata
->length
!= 0);
323 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
324 dns_rdata_toregion(rdata
, &sr
);
325 dns_name_init(&name
, offsets
);
326 dns_name_fromregion(&name
, &sr
);
327 RETERR(dns_name_towire(&name
, cctx
, target
));
328 isc_region_consume(&sr
, name_length(&name
));
329 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
333 compare_any_tsig(ARGS_COMPARE
) {
340 REQUIRE(rdata1
->type
== rdata2
->type
);
341 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
342 REQUIRE(rdata1
->type
== 250);
343 REQUIRE(rdata1
->rdclass
== 255);
344 REQUIRE(rdata1
->length
!= 0);
345 REQUIRE(rdata2
->length
!= 0);
347 dns_rdata_toregion(rdata1
, &r1
);
348 dns_rdata_toregion(rdata2
, &r2
);
349 dns_name_init(&name1
, NULL
);
350 dns_name_init(&name2
, NULL
);
351 dns_name_fromregion(&name1
, &r1
);
352 dns_name_fromregion(&name2
, &r2
);
353 order
= dns_name_rdatacompare(&name1
, &name2
);
356 isc_region_consume(&r1
, name_length(&name1
));
357 isc_region_consume(&r2
, name_length(&name2
));
358 return (isc_region_compare(&r1
, &r2
));
361 static inline isc_result_t
362 fromstruct_any_tsig(ARGS_FROMSTRUCT
) {
363 dns_rdata_any_tsig_t
*tsig
= source
;
366 REQUIRE(type
== 250);
367 REQUIRE(rdclass
== 255);
368 REQUIRE(source
!= NULL
);
369 REQUIRE(tsig
->common
.rdclass
== rdclass
);
370 REQUIRE(tsig
->common
.rdtype
== type
);
378 RETERR(name_tobuffer(&tsig
->algorithm
, target
));
380 isc_buffer_availableregion(target
, &tr
);
381 if (tr
.length
< 6 + 2 + 2)
382 return (ISC_R_NOSPACE
);
385 * Time Signed: 48 bits.
387 RETERR(uint16_tobuffer((isc_uint16_t
)(tsig
->timesigned
>> 32),
389 RETERR(uint32_tobuffer((isc_uint32_t
)(tsig
->timesigned
& 0xffffffffU
),
395 RETERR(uint16_tobuffer(tsig
->fudge
, target
));
400 RETERR(uint16_tobuffer(tsig
->siglen
, target
));
405 RETERR(mem_tobuffer(target
, tsig
->signature
, tsig
->siglen
));
407 isc_buffer_availableregion(target
, &tr
);
408 if (tr
.length
< 2 + 2 + 2)
409 return (ISC_R_NOSPACE
);
414 RETERR(uint16_tobuffer(tsig
->originalid
, target
));
419 RETERR(uint16_tobuffer(tsig
->error
, target
));
424 RETERR(uint16_tobuffer(tsig
->otherlen
, target
));
429 return (mem_tobuffer(target
, tsig
->other
, tsig
->otherlen
));
432 static inline isc_result_t
433 tostruct_any_tsig(ARGS_TOSTRUCT
) {
434 dns_rdata_any_tsig_t
*tsig
;
438 REQUIRE(rdata
->type
== 250);
439 REQUIRE(rdata
->rdclass
== 255);
440 REQUIRE(rdata
->length
!= 0);
442 tsig
= (dns_rdata_any_tsig_t
*) target
;
443 tsig
->common
.rdclass
= rdata
->rdclass
;
444 tsig
->common
.rdtype
= rdata
->type
;
445 ISC_LINK_INIT(&tsig
->common
, link
);
447 dns_rdata_toregion(rdata
, &sr
);
452 dns_name_init(&alg
, NULL
);
453 dns_name_fromregion(&alg
, &sr
);
454 dns_name_init(&tsig
->algorithm
, NULL
);
455 RETERR(name_duporclone(&alg
, mctx
, &tsig
->algorithm
));
457 isc_region_consume(&sr
, name_length(&tsig
->algorithm
));
462 INSIST(sr
.length
>= 6);
463 tsig
->timesigned
= ((isc_uint64_t
)sr
.base
[0] << 40) |
464 ((isc_uint64_t
)sr
.base
[1] << 32) |
465 ((isc_uint64_t
)sr
.base
[2] << 24) |
466 ((isc_uint64_t
)sr
.base
[3] << 16) |
467 ((isc_uint64_t
)sr
.base
[4] << 8) |
468 (isc_uint64_t
)sr
.base
[5];
469 isc_region_consume(&sr
, 6);
474 tsig
->fudge
= uint16_fromregion(&sr
);
475 isc_region_consume(&sr
, 2);
480 tsig
->siglen
= uint16_fromregion(&sr
);
481 isc_region_consume(&sr
, 2);
486 INSIST(sr
.length
>= tsig
->siglen
);
487 tsig
->signature
= mem_maybedup(mctx
, sr
.base
, tsig
->siglen
);
488 if (tsig
->signature
== NULL
)
490 isc_region_consume(&sr
, tsig
->siglen
);
495 tsig
->originalid
= uint16_fromregion(&sr
);
496 isc_region_consume(&sr
, 2);
501 tsig
->error
= uint16_fromregion(&sr
);
502 isc_region_consume(&sr
, 2);
507 tsig
->otherlen
= uint16_fromregion(&sr
);
508 isc_region_consume(&sr
, 2);
513 INSIST(sr
.length
== tsig
->otherlen
);
514 tsig
->other
= mem_maybedup(mctx
, sr
.base
, tsig
->otherlen
);
515 if (tsig
->other
== NULL
)
519 return (ISC_R_SUCCESS
);
523 dns_name_free(&tsig
->algorithm
, tsig
->mctx
);
524 if (mctx
!= NULL
&& tsig
->signature
!= NULL
)
525 isc_mem_free(mctx
, tsig
->signature
);
526 return (ISC_R_NOMEMORY
);
530 freestruct_any_tsig(ARGS_FREESTRUCT
) {
531 dns_rdata_any_tsig_t
*tsig
= (dns_rdata_any_tsig_t
*) source
;
533 REQUIRE(source
!= NULL
);
534 REQUIRE(tsig
->common
.rdclass
== 255);
535 REQUIRE(tsig
->common
.rdtype
== 250);
537 if (tsig
->mctx
== NULL
)
540 dns_name_free(&tsig
->algorithm
, tsig
->mctx
);
541 if (tsig
->signature
!= NULL
)
542 isc_mem_free(tsig
->mctx
, tsig
->signature
);
543 if (tsig
->other
!= NULL
)
544 isc_mem_free(tsig
->mctx
, tsig
->other
);
548 static inline isc_result_t
549 additionaldata_any_tsig(ARGS_ADDLDATA
) {
550 REQUIRE(rdata
->type
== 250);
551 REQUIRE(rdata
->rdclass
== 255);
557 return (ISC_R_SUCCESS
);
560 static inline isc_result_t
561 digest_any_tsig(ARGS_DIGEST
) {
563 REQUIRE(rdata
->type
== 250);
564 REQUIRE(rdata
->rdclass
== 255);
570 return (ISC_R_NOTIMPLEMENTED
);
573 static inline isc_boolean_t
574 checkowner_any_tsig(ARGS_CHECKOWNER
) {
576 REQUIRE(type
== 250);
577 REQUIRE(rdclass
== 255);
587 static inline isc_boolean_t
588 checknames_any_tsig(ARGS_CHECKNAMES
) {
590 REQUIRE(rdata
->type
== 250);
591 REQUIRE(rdata
->rdclass
== 250);
601 casecompare_any_tsig(ARGS_COMPARE
) {
602 return (compare_any_tsig(rdata1
, rdata2
));
605 #endif /* RDATA_ANY_255_TSIG_250_C */