4 * Copyright (C) 2004, 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: tkey_249.c,v 1.59 2009/12/04 22:06:37 tbox Exp */
23 * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
26 /* draft-ietf-dnsext-tkey-01.txt */
28 #ifndef RDATA_GENERIC_TKEY_249_C
29 #define RDATA_GENERIC_TKEY_249_C
31 #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
33 static inline isc_result_t
34 fromtext_tkey(ARGS_FROMTEXT
) {
51 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
53 dns_name_init(&name
, NULL
);
54 buffer_fromregion(&buffer
, &token
.value
.as_region
);
55 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
56 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
62 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
64 RETERR(uint32_tobuffer(token
.value
.as_ulong
, target
));
69 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
71 RETERR(uint32_tobuffer(token
.value
.as_ulong
, target
));
76 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
78 if (token
.value
.as_ulong
> 0xffffU
)
80 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
85 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
87 if (dns_tsigrcode_fromtext(&rcode
, &token
.value
.as_textregion
)
90 i
= strtol(DNS_AS_STR(token
), &e
, 10);
92 RETTOK(DNS_R_UNKNOWN
);
93 if (i
< 0 || i
> 0xffff)
95 rcode
= (dns_rcode_t
)i
;
97 RETERR(uint16_tobuffer(rcode
, target
));
102 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
104 if (token
.value
.as_ulong
> 0xffffU
)
106 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
111 RETERR(isc_base64_tobuffer(lexer
, target
, (int)token
.value
.as_ulong
));
116 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
118 if (token
.value
.as_ulong
> 0xffffU
)
120 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
125 return (isc_base64_tobuffer(lexer
, target
, (int)token
.value
.as_ulong
));
128 static inline isc_result_t
129 totext_tkey(ARGS_TOTEXT
) {
131 char buf
[sizeof("4294967295 ")];
137 REQUIRE(rdata
->type
== 249);
138 REQUIRE(rdata
->length
!= 0);
140 dns_rdata_toregion(rdata
, &sr
);
145 dns_name_init(&name
, NULL
);
146 dns_name_init(&prefix
, NULL
);
147 dns_name_fromregion(&name
, &sr
);
148 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
149 RETERR(dns_name_totext(&prefix
, sub
, target
));
150 RETERR(str_totext(" ", target
));
151 isc_region_consume(&sr
, name_length(&name
));
156 n
= uint32_fromregion(&sr
);
157 isc_region_consume(&sr
, 4);
158 sprintf(buf
, "%lu ", n
);
159 RETERR(str_totext(buf
, target
));
164 n
= uint32_fromregion(&sr
);
165 isc_region_consume(&sr
, 4);
166 sprintf(buf
, "%lu ", n
);
167 RETERR(str_totext(buf
, target
));
172 n
= uint16_fromregion(&sr
);
173 isc_region_consume(&sr
, 2);
174 sprintf(buf
, "%lu ", n
);
175 RETERR(str_totext(buf
, target
));
180 n
= uint16_fromregion(&sr
);
181 isc_region_consume(&sr
, 2);
182 if (dns_tsigrcode_totext((dns_rcode_t
)n
, target
) == ISC_R_SUCCESS
)
183 RETERR(str_totext(" ", target
));
185 sprintf(buf
, "%lu ", n
);
186 RETERR(str_totext(buf
, target
));
192 n
= uint16_fromregion(&sr
);
193 isc_region_consume(&sr
, 2);
194 sprintf(buf
, "%lu", n
);
195 RETERR(str_totext(buf
, target
));
200 REQUIRE(n
<= sr
.length
);
203 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
204 RETERR(str_totext(" (", target
));
205 RETERR(str_totext(tctx
->linebreak
, target
));
206 RETERR(isc_base64_totext(&dr
, tctx
->width
- 2,
207 tctx
->linebreak
, target
));
208 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
209 RETERR(str_totext(" ) ", target
));
211 RETERR(str_totext(" ", target
));
212 isc_region_consume(&sr
, n
);
217 n
= uint16_fromregion(&sr
);
218 isc_region_consume(&sr
, 2);
219 sprintf(buf
, "%lu", n
);
220 RETERR(str_totext(buf
, target
));
225 REQUIRE(n
<= sr
.length
);
229 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
230 RETERR(str_totext(" (", target
));
231 RETERR(str_totext(tctx
->linebreak
, target
));
232 RETERR(isc_base64_totext(&dr
, tctx
->width
- 2,
233 tctx
->linebreak
, target
));
234 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
235 RETERR(str_totext(" )", target
));
237 return (ISC_R_SUCCESS
);
240 static inline isc_result_t
241 fromwire_tkey(ARGS_FROMWIRE
) {
246 REQUIRE(type
== 249);
251 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
256 dns_name_init(&name
, NULL
);
257 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
265 isc_buffer_activeregion(source
, &sr
);
267 return (ISC_R_UNEXPECTEDEND
);
268 RETERR(mem_tobuffer(target
, sr
.base
, 12));
269 isc_region_consume(&sr
, 12);
270 isc_buffer_forward(source
, 12);
273 * Key Length + Key Data.
276 return (ISC_R_UNEXPECTEDEND
);
277 n
= uint16_fromregion(&sr
);
278 if (sr
.length
< n
+ 2)
279 return (ISC_R_UNEXPECTEDEND
);
280 RETERR(mem_tobuffer(target
, sr
.base
, n
+ 2));
281 isc_region_consume(&sr
, n
+ 2);
282 isc_buffer_forward(source
, n
+ 2);
285 * Other Length + Other Data.
288 return (ISC_R_UNEXPECTEDEND
);
289 n
= uint16_fromregion(&sr
);
290 if (sr
.length
< n
+ 2)
291 return (ISC_R_UNEXPECTEDEND
);
292 isc_buffer_forward(source
, n
+ 2);
293 return (mem_tobuffer(target
, sr
.base
, n
+ 2));
296 static inline isc_result_t
297 towire_tkey(ARGS_TOWIRE
) {
300 dns_offsets_t offsets
;
302 REQUIRE(rdata
->type
== 249);
303 REQUIRE(rdata
->length
!= 0);
305 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
309 dns_rdata_toregion(rdata
, &sr
);
310 dns_name_init(&name
, offsets
);
311 dns_name_fromregion(&name
, &sr
);
312 RETERR(dns_name_towire(&name
, cctx
, target
));
313 isc_region_consume(&sr
, name_length(&name
));
315 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
319 compare_tkey(ARGS_COMPARE
) {
326 REQUIRE(rdata1
->type
== rdata2
->type
);
327 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
328 REQUIRE(rdata1
->type
== 249);
329 REQUIRE(rdata1
->length
!= 0);
330 REQUIRE(rdata2
->length
!= 0);
335 dns_rdata_toregion(rdata1
, &r1
);
336 dns_rdata_toregion(rdata2
, &r2
);
337 dns_name_init(&name1
, NULL
);
338 dns_name_init(&name2
, NULL
);
339 dns_name_fromregion(&name1
, &r1
);
340 dns_name_fromregion(&name2
, &r2
);
341 if ((order
= dns_name_rdatacompare(&name1
, &name2
)) != 0)
343 isc_region_consume(&r1
, name_length(&name1
));
344 isc_region_consume(&r2
, name_length(&name2
));
345 return (isc_region_compare(&r1
, &r2
));
348 static inline isc_result_t
349 fromstruct_tkey(ARGS_FROMSTRUCT
) {
350 dns_rdata_tkey_t
*tkey
= source
;
352 REQUIRE(type
== 249);
353 REQUIRE(source
!= NULL
);
354 REQUIRE(tkey
->common
.rdtype
== type
);
355 REQUIRE(tkey
->common
.rdclass
== rdclass
);
363 RETERR(name_tobuffer(&tkey
->algorithm
, target
));
366 * Inception: 32 bits.
368 RETERR(uint32_tobuffer(tkey
->inception
, target
));
373 RETERR(uint32_tobuffer(tkey
->expire
, target
));
378 RETERR(uint16_tobuffer(tkey
->mode
, target
));
383 RETERR(uint16_tobuffer(tkey
->error
, target
));
388 RETERR(uint16_tobuffer(tkey
->keylen
, target
));
393 RETERR(mem_tobuffer(target
, tkey
->key
, tkey
->keylen
));
396 * Other size: 16 bits.
398 RETERR(uint16_tobuffer(tkey
->otherlen
, target
));
403 return (mem_tobuffer(target
, tkey
->other
, tkey
->otherlen
));
406 static inline isc_result_t
407 tostruct_tkey(ARGS_TOSTRUCT
) {
408 dns_rdata_tkey_t
*tkey
= target
;
412 REQUIRE(rdata
->type
== 249);
413 REQUIRE(target
!= NULL
);
414 REQUIRE(rdata
->length
!= 0);
416 tkey
->common
.rdclass
= rdata
->rdclass
;
417 tkey
->common
.rdtype
= rdata
->type
;
418 ISC_LINK_INIT(&tkey
->common
, link
);
420 dns_rdata_toregion(rdata
, &sr
);
425 dns_name_init(&alg
, NULL
);
426 dns_name_fromregion(&alg
, &sr
);
427 dns_name_init(&tkey
->algorithm
, NULL
);
428 RETERR(name_duporclone(&alg
, mctx
, &tkey
->algorithm
));
429 isc_region_consume(&sr
, name_length(&tkey
->algorithm
));
434 tkey
->inception
= uint32_fromregion(&sr
);
435 isc_region_consume(&sr
, 4);
440 tkey
->expire
= uint32_fromregion(&sr
);
441 isc_region_consume(&sr
, 4);
446 tkey
->mode
= uint16_fromregion(&sr
);
447 isc_region_consume(&sr
, 2);
452 tkey
->error
= uint16_fromregion(&sr
);
453 isc_region_consume(&sr
, 2);
458 tkey
->keylen
= uint16_fromregion(&sr
);
459 isc_region_consume(&sr
, 2);
464 tkey
->key
= mem_maybedup(mctx
, sr
.base
, tkey
->keylen
);
465 if (tkey
->key
== NULL
)
467 isc_region_consume(&sr
, tkey
->keylen
);
472 tkey
->otherlen
= uint16_fromregion(&sr
);
473 isc_region_consume(&sr
, 2);
478 tkey
->other
= mem_maybedup(mctx
, sr
.base
, tkey
->otherlen
);
479 if (tkey
->other
== NULL
)
483 return (ISC_R_SUCCESS
);
487 dns_name_free(&tkey
->algorithm
, mctx
);
488 if (mctx
!= NULL
&& tkey
->key
!= NULL
)
489 isc_mem_free(mctx
, tkey
->key
);
490 return (ISC_R_NOMEMORY
);
494 freestruct_tkey(ARGS_FREESTRUCT
) {
495 dns_rdata_tkey_t
*tkey
= (dns_rdata_tkey_t
*) source
;
497 REQUIRE(source
!= NULL
);
499 if (tkey
->mctx
== NULL
)
502 dns_name_free(&tkey
->algorithm
, tkey
->mctx
);
503 if (tkey
->key
!= NULL
)
504 isc_mem_free(tkey
->mctx
, tkey
->key
);
505 if (tkey
->other
!= NULL
)
506 isc_mem_free(tkey
->mctx
, tkey
->other
);
510 static inline isc_result_t
511 additionaldata_tkey(ARGS_ADDLDATA
) {
516 REQUIRE(rdata
->type
== 249);
518 return (ISC_R_SUCCESS
);
521 static inline isc_result_t
522 digest_tkey(ARGS_DIGEST
) {
527 REQUIRE(rdata
->type
== 249);
529 return (ISC_R_NOTIMPLEMENTED
);
532 static inline isc_boolean_t
533 checkowner_tkey(ARGS_CHECKOWNER
) {
535 REQUIRE(type
== 249);
545 static inline isc_boolean_t
546 checknames_tkey(ARGS_CHECKNAMES
) {
548 REQUIRE(rdata
->type
== 249);
557 static inline isc_result_t
558 casecompare_tkey(ARGS_COMPARE
) {
559 return (compare_tkey(rdata1
, rdata2
));
561 #endif /* RDATA_GENERIC_TKEY_249_C */