4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 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: dnskey_48.c,v 1.10 2009/12/04 22:06:37 tbox Exp */
23 * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
28 #ifndef RDATA_GENERIC_DNSKEY_48_C
29 #define RDATA_GENERIC_DNSKEY_48_C
33 #define RRTYPE_DNSKEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
35 static inline isc_result_t
36 fromtext_dnskey(ARGS_FROMTEXT
) {
51 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
53 RETTOK(dns_keyflags_fromtext(&flags
, &token
.value
.as_textregion
));
54 RETERR(uint16_tobuffer(flags
, target
));
57 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
59 RETTOK(dns_secproto_fromtext(&proto
, &token
.value
.as_textregion
));
60 RETERR(mem_tobuffer(target
, &proto
, 1));
63 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
65 RETTOK(dns_secalg_fromtext(&alg
, &token
.value
.as_textregion
));
66 RETERR(mem_tobuffer(target
, &alg
, 1));
69 if ((flags
& 0xc000) == 0xc000)
70 return (ISC_R_SUCCESS
);
72 return (isc_base64_tobuffer(lexer
, target
, -1));
75 static inline isc_result_t
76 totext_dnskey(ARGS_TOTEXT
) {
78 char buf
[sizeof("64000")];
80 unsigned char algorithm
;
81 char namebuf
[DNS_NAME_FORMATSIZE
];
83 REQUIRE(rdata
->type
== 48);
84 REQUIRE(rdata
->length
!= 0);
86 dns_rdata_toregion(rdata
, &sr
);
89 flags
= uint16_fromregion(&sr
);
90 isc_region_consume(&sr
, 2);
91 sprintf(buf
, "%u", flags
);
92 RETERR(str_totext(buf
, target
));
93 RETERR(str_totext(" ", target
));
96 sprintf(buf
, "%u", sr
.base
[0]);
97 isc_region_consume(&sr
, 1);
98 RETERR(str_totext(buf
, target
));
99 RETERR(str_totext(" ", target
));
102 algorithm
= sr
.base
[0];
103 sprintf(buf
, "%u", algorithm
);
104 isc_region_consume(&sr
, 1);
105 RETERR(str_totext(buf
, target
));
108 if ((flags
& 0xc000) == 0xc000)
109 return (ISC_R_SUCCESS
);
111 if ((tctx
->flags
& DNS_STYLEFLAG_COMMENT
) != 0 &&
112 algorithm
== DNS_KEYALG_PRIVATEDNS
) {
114 dns_name_init(&name
, NULL
);
115 dns_name_fromregion(&name
, &sr
);
116 dns_name_format(&name
, namebuf
, sizeof(namebuf
));
121 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
122 RETERR(str_totext(" (", target
));
123 RETERR(str_totext(tctx
->linebreak
, target
));
124 RETERR(isc_base64_totext(&sr
, tctx
->width
- 2,
125 tctx
->linebreak
, target
));
127 if ((tctx
->flags
& DNS_STYLEFLAG_COMMENT
) != 0)
128 RETERR(str_totext(tctx
->linebreak
, target
));
129 else if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
130 RETERR(str_totext(" ", target
));
132 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
133 RETERR(str_totext(")", target
));
135 if ((tctx
->flags
& DNS_STYLEFLAG_COMMENT
) != 0) {
138 RETERR(str_totext(" ; key id = ", target
));
139 dns_rdata_toregion(rdata
, &tmpr
);
140 sprintf(buf
, "%u", dst_region_computeid(&tmpr
, algorithm
));
141 RETERR(str_totext(buf
, target
));
142 if (algorithm
== DNS_KEYALG_PRIVATEDNS
) {
143 RETERR(str_totext(tctx
->linebreak
, target
));
144 RETERR(str_totext("; alg = ", target
));
145 RETERR(str_totext(namebuf
, target
));
148 return (ISC_R_SUCCESS
);
151 static inline isc_result_t
152 fromwire_dnskey(ARGS_FROMWIRE
) {
153 unsigned char algorithm
;
163 isc_buffer_activeregion(source
, &sr
);
165 return (ISC_R_UNEXPECTEDEND
);
167 algorithm
= sr
.base
[3];
168 RETERR(mem_tobuffer(target
, sr
.base
, 4));
169 isc_region_consume(&sr
, 4);
170 isc_buffer_forward(source
, 4);
172 if (algorithm
== DNS_KEYALG_PRIVATEDNS
) {
174 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
175 dns_name_init(&name
, NULL
);
176 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
178 isc_buffer_activeregion(source
, &sr
);
179 isc_buffer_forward(source
, sr
.length
);
180 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
183 static inline isc_result_t
184 towire_dnskey(ARGS_TOWIRE
) {
187 REQUIRE(rdata
->type
== 48);
188 REQUIRE(rdata
->length
!= 0);
192 dns_rdata_toregion(rdata
, &sr
);
193 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
197 compare_dnskey(ARGS_COMPARE
) {
201 REQUIRE(rdata1
->type
== rdata2
->type
);
202 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
203 REQUIRE(rdata1
->type
== 48);
204 REQUIRE(rdata1
->length
!= 0);
205 REQUIRE(rdata2
->length
!= 0);
207 dns_rdata_toregion(rdata1
, &r1
);
208 dns_rdata_toregion(rdata2
, &r2
);
209 return (isc_region_compare(&r1
, &r2
));
212 static inline isc_result_t
213 fromstruct_dnskey(ARGS_FROMSTRUCT
) {
214 dns_rdata_dnskey_t
*dnskey
= source
;
217 REQUIRE(source
!= NULL
);
218 REQUIRE(dnskey
->common
.rdtype
== type
);
219 REQUIRE(dnskey
->common
.rdclass
== rdclass
);
225 RETERR(uint16_tobuffer(dnskey
->flags
, target
));
228 RETERR(uint8_tobuffer(dnskey
->protocol
, target
));
231 RETERR(uint8_tobuffer(dnskey
->algorithm
, target
));
234 return (mem_tobuffer(target
, dnskey
->data
, dnskey
->datalen
));
237 static inline isc_result_t
238 tostruct_dnskey(ARGS_TOSTRUCT
) {
239 dns_rdata_dnskey_t
*dnskey
= target
;
242 REQUIRE(rdata
->type
== 48);
243 REQUIRE(target
!= NULL
);
244 REQUIRE(rdata
->length
!= 0);
246 dnskey
->common
.rdclass
= rdata
->rdclass
;
247 dnskey
->common
.rdtype
= rdata
->type
;
248 ISC_LINK_INIT(&dnskey
->common
, link
);
250 dns_rdata_toregion(rdata
, &sr
);
254 return (ISC_R_UNEXPECTEDEND
);
255 dnskey
->flags
= uint16_fromregion(&sr
);
256 isc_region_consume(&sr
, 2);
260 return (ISC_R_UNEXPECTEDEND
);
261 dnskey
->protocol
= uint8_fromregion(&sr
);
262 isc_region_consume(&sr
, 1);
266 return (ISC_R_UNEXPECTEDEND
);
267 dnskey
->algorithm
= uint8_fromregion(&sr
);
268 isc_region_consume(&sr
, 1);
271 dnskey
->datalen
= sr
.length
;
272 dnskey
->data
= mem_maybedup(mctx
, sr
.base
, dnskey
->datalen
);
273 if (dnskey
->data
== NULL
)
274 return (ISC_R_NOMEMORY
);
277 return (ISC_R_SUCCESS
);
281 freestruct_dnskey(ARGS_FREESTRUCT
) {
282 dns_rdata_dnskey_t
*dnskey
= (dns_rdata_dnskey_t
*) source
;
284 REQUIRE(source
!= NULL
);
285 REQUIRE(dnskey
->common
.rdtype
== 48);
287 if (dnskey
->mctx
== NULL
)
290 if (dnskey
->data
!= NULL
)
291 isc_mem_free(dnskey
->mctx
, dnskey
->data
);
295 static inline isc_result_t
296 additionaldata_dnskey(ARGS_ADDLDATA
) {
297 REQUIRE(rdata
->type
== 48);
303 return (ISC_R_SUCCESS
);
306 static inline isc_result_t
307 digest_dnskey(ARGS_DIGEST
) {
310 REQUIRE(rdata
->type
== 48);
312 dns_rdata_toregion(rdata
, &r
);
314 return ((digest
)(arg
, &r
));
317 static inline isc_boolean_t
318 checkowner_dnskey(ARGS_CHECKOWNER
) {
330 static inline isc_boolean_t
331 checknames_dnskey(ARGS_CHECKNAMES
) {
333 REQUIRE(rdata
->type
== 48);
343 casecompare_dnskey(ARGS_COMPARE
) {
346 * Treat ALG 253 (private DNS) subtype name case sensistively.
348 return (compare_dnskey(rdata1
, rdata2
));
351 #endif /* RDATA_GENERIC_DNSKEY_48_C */