1 /* $NetBSD: tkey_249.c,v 1.5 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004, 2007, 2009, 2011, 2012, 2014 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.
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 if (tctx
->width
== 0) /* No splitting */
207 RETERR(isc_base64_totext(&dr
, 60, "", target
));
209 RETERR(isc_base64_totext(&dr
, tctx
->width
- 2,
210 tctx
->linebreak
, target
));
211 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
212 RETERR(str_totext(" ) ", target
));
214 RETERR(str_totext(" ", target
));
215 isc_region_consume(&sr
, n
);
220 n
= uint16_fromregion(&sr
);
221 isc_region_consume(&sr
, 2);
222 sprintf(buf
, "%lu", n
);
223 RETERR(str_totext(buf
, target
));
228 REQUIRE(n
<= sr
.length
);
232 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
233 RETERR(str_totext(" (", target
));
234 RETERR(str_totext(tctx
->linebreak
, target
));
235 if (tctx
->width
== 0) /* No splitting */
236 RETERR(isc_base64_totext(&dr
, 60, "", target
));
238 RETERR(isc_base64_totext(&dr
, tctx
->width
- 2,
239 tctx
->linebreak
, target
));
240 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
241 RETERR(str_totext(" )", target
));
243 return (ISC_R_SUCCESS
);
246 static inline isc_result_t
247 fromwire_tkey(ARGS_FROMWIRE
) {
252 REQUIRE(type
== 249);
257 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
262 dns_name_init(&name
, NULL
);
263 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
271 isc_buffer_activeregion(source
, &sr
);
273 return (ISC_R_UNEXPECTEDEND
);
274 RETERR(mem_tobuffer(target
, sr
.base
, 12));
275 isc_region_consume(&sr
, 12);
276 isc_buffer_forward(source
, 12);
279 * Key Length + Key Data.
282 return (ISC_R_UNEXPECTEDEND
);
283 n
= uint16_fromregion(&sr
);
284 if (sr
.length
< n
+ 2)
285 return (ISC_R_UNEXPECTEDEND
);
286 RETERR(mem_tobuffer(target
, sr
.base
, n
+ 2));
287 isc_region_consume(&sr
, n
+ 2);
288 isc_buffer_forward(source
, n
+ 2);
291 * Other Length + Other Data.
294 return (ISC_R_UNEXPECTEDEND
);
295 n
= uint16_fromregion(&sr
);
296 if (sr
.length
< n
+ 2)
297 return (ISC_R_UNEXPECTEDEND
);
298 isc_buffer_forward(source
, n
+ 2);
299 return (mem_tobuffer(target
, sr
.base
, n
+ 2));
302 static inline isc_result_t
303 towire_tkey(ARGS_TOWIRE
) {
306 dns_offsets_t offsets
;
308 REQUIRE(rdata
->type
== 249);
309 REQUIRE(rdata
->length
!= 0);
311 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
315 dns_rdata_toregion(rdata
, &sr
);
316 dns_name_init(&name
, offsets
);
317 dns_name_fromregion(&name
, &sr
);
318 RETERR(dns_name_towire(&name
, cctx
, target
));
319 isc_region_consume(&sr
, name_length(&name
));
321 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
325 compare_tkey(ARGS_COMPARE
) {
332 REQUIRE(rdata1
->type
== rdata2
->type
);
333 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
334 REQUIRE(rdata1
->type
== 249);
335 REQUIRE(rdata1
->length
!= 0);
336 REQUIRE(rdata2
->length
!= 0);
341 dns_rdata_toregion(rdata1
, &r1
);
342 dns_rdata_toregion(rdata2
, &r2
);
343 dns_name_init(&name1
, NULL
);
344 dns_name_init(&name2
, NULL
);
345 dns_name_fromregion(&name1
, &r1
);
346 dns_name_fromregion(&name2
, &r2
);
347 if ((order
= dns_name_rdatacompare(&name1
, &name2
)) != 0)
349 isc_region_consume(&r1
, name_length(&name1
));
350 isc_region_consume(&r2
, name_length(&name2
));
351 return (isc_region_compare(&r1
, &r2
));
354 static inline isc_result_t
355 fromstruct_tkey(ARGS_FROMSTRUCT
) {
356 dns_rdata_tkey_t
*tkey
= source
;
358 REQUIRE(type
== 249);
359 REQUIRE(source
!= NULL
);
360 REQUIRE(tkey
->common
.rdtype
== type
);
361 REQUIRE(tkey
->common
.rdclass
== rdclass
);
369 RETERR(name_tobuffer(&tkey
->algorithm
, target
));
372 * Inception: 32 bits.
374 RETERR(uint32_tobuffer(tkey
->inception
, target
));
379 RETERR(uint32_tobuffer(tkey
->expire
, target
));
384 RETERR(uint16_tobuffer(tkey
->mode
, target
));
389 RETERR(uint16_tobuffer(tkey
->error
, target
));
394 RETERR(uint16_tobuffer(tkey
->keylen
, target
));
399 RETERR(mem_tobuffer(target
, tkey
->key
, tkey
->keylen
));
402 * Other size: 16 bits.
404 RETERR(uint16_tobuffer(tkey
->otherlen
, target
));
409 return (mem_tobuffer(target
, tkey
->other
, tkey
->otherlen
));
412 static inline isc_result_t
413 tostruct_tkey(ARGS_TOSTRUCT
) {
414 dns_rdata_tkey_t
*tkey
= target
;
418 REQUIRE(rdata
->type
== 249);
419 REQUIRE(target
!= NULL
);
420 REQUIRE(rdata
->length
!= 0);
422 tkey
->common
.rdclass
= rdata
->rdclass
;
423 tkey
->common
.rdtype
= rdata
->type
;
424 ISC_LINK_INIT(&tkey
->common
, link
);
426 dns_rdata_toregion(rdata
, &sr
);
431 dns_name_init(&alg
, NULL
);
432 dns_name_fromregion(&alg
, &sr
);
433 dns_name_init(&tkey
->algorithm
, NULL
);
434 RETERR(name_duporclone(&alg
, mctx
, &tkey
->algorithm
));
435 isc_region_consume(&sr
, name_length(&tkey
->algorithm
));
440 tkey
->inception
= uint32_fromregion(&sr
);
441 isc_region_consume(&sr
, 4);
446 tkey
->expire
= uint32_fromregion(&sr
);
447 isc_region_consume(&sr
, 4);
452 tkey
->mode
= uint16_fromregion(&sr
);
453 isc_region_consume(&sr
, 2);
458 tkey
->error
= uint16_fromregion(&sr
);
459 isc_region_consume(&sr
, 2);
464 tkey
->keylen
= uint16_fromregion(&sr
);
465 isc_region_consume(&sr
, 2);
470 INSIST(tkey
->keylen
+ 2U <= sr
.length
);
471 tkey
->key
= mem_maybedup(mctx
, sr
.base
, tkey
->keylen
);
472 if (tkey
->key
== NULL
)
474 isc_region_consume(&sr
, tkey
->keylen
);
479 tkey
->otherlen
= uint16_fromregion(&sr
);
480 isc_region_consume(&sr
, 2);
485 INSIST(tkey
->otherlen
<= sr
.length
);
486 tkey
->other
= mem_maybedup(mctx
, sr
.base
, tkey
->otherlen
);
487 if (tkey
->other
== NULL
)
491 return (ISC_R_SUCCESS
);
495 dns_name_free(&tkey
->algorithm
, mctx
);
496 if (mctx
!= NULL
&& tkey
->key
!= NULL
)
497 isc_mem_free(mctx
, tkey
->key
);
498 return (ISC_R_NOMEMORY
);
502 freestruct_tkey(ARGS_FREESTRUCT
) {
503 dns_rdata_tkey_t
*tkey
= (dns_rdata_tkey_t
*) source
;
505 REQUIRE(source
!= NULL
);
507 if (tkey
->mctx
== NULL
)
510 dns_name_free(&tkey
->algorithm
, tkey
->mctx
);
511 if (tkey
->key
!= NULL
)
512 isc_mem_free(tkey
->mctx
, tkey
->key
);
513 if (tkey
->other
!= NULL
)
514 isc_mem_free(tkey
->mctx
, tkey
->other
);
518 static inline isc_result_t
519 additionaldata_tkey(ARGS_ADDLDATA
) {
524 REQUIRE(rdata
->type
== 249);
526 return (ISC_R_SUCCESS
);
529 static inline isc_result_t
530 digest_tkey(ARGS_DIGEST
) {
535 REQUIRE(rdata
->type
== 249);
537 return (ISC_R_NOTIMPLEMENTED
);
540 static inline isc_boolean_t
541 checkowner_tkey(ARGS_CHECKOWNER
) {
543 REQUIRE(type
== 249);
553 static inline isc_boolean_t
554 checknames_tkey(ARGS_CHECKNAMES
) {
556 REQUIRE(rdata
->type
== 249);
565 static inline isc_result_t
566 casecompare_tkey(ARGS_COMPARE
) {
567 return (compare_tkey(rdata1
, rdata2
));
569 #endif /* RDATA_GENERIC_TKEY_249_C */