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: key_25.c,v 1.53 2009/12/04 22:06:37 tbox Exp */
23 * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
28 #ifndef RDATA_GENERIC_KEY_25_C
29 #define RDATA_GENERIC_KEY_25_C
33 #define RRTYPE_KEY_ATTRIBUTES (0)
35 static inline isc_result_t
36 fromtext_key(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_key(ARGS_TOTEXT
) {
78 char buf
[sizeof("64000")];
80 unsigned char algorithm
;
81 char namebuf
[DNS_NAME_FORMATSIZE
];
83 REQUIRE(rdata
->type
== 25);
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_key(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_key(ARGS_TOWIRE
) {
187 REQUIRE(rdata
->type
== 25);
188 REQUIRE(rdata
->length
!= 0);
192 dns_rdata_toregion(rdata
, &sr
);
193 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
197 compare_key(ARGS_COMPARE
) {
201 REQUIRE(rdata1
->type
== rdata2
->type
);
202 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
203 REQUIRE(rdata1
->type
== 25);
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_key(ARGS_FROMSTRUCT
) {
214 dns_rdata_key_t
*key
= source
;
217 REQUIRE(source
!= NULL
);
218 REQUIRE(key
->common
.rdtype
== type
);
219 REQUIRE(key
->common
.rdclass
== rdclass
);
225 RETERR(uint16_tobuffer(key
->flags
, target
));
228 RETERR(uint8_tobuffer(key
->protocol
, target
));
231 RETERR(uint8_tobuffer(key
->algorithm
, target
));
234 return (mem_tobuffer(target
, key
->data
, key
->datalen
));
237 static inline isc_result_t
238 tostruct_key(ARGS_TOSTRUCT
) {
239 dns_rdata_key_t
*key
= target
;
242 REQUIRE(rdata
->type
== 25);
243 REQUIRE(target
!= NULL
);
244 REQUIRE(rdata
->length
!= 0);
246 key
->common
.rdclass
= rdata
->rdclass
;
247 key
->common
.rdtype
= rdata
->type
;
248 ISC_LINK_INIT(&key
->common
, link
);
250 dns_rdata_toregion(rdata
, &sr
);
254 return (ISC_R_UNEXPECTEDEND
);
255 key
->flags
= uint16_fromregion(&sr
);
256 isc_region_consume(&sr
, 2);
260 return (ISC_R_UNEXPECTEDEND
);
261 key
->protocol
= uint8_fromregion(&sr
);
262 isc_region_consume(&sr
, 1);
266 return (ISC_R_UNEXPECTEDEND
);
267 key
->algorithm
= uint8_fromregion(&sr
);
268 isc_region_consume(&sr
, 1);
271 key
->datalen
= sr
.length
;
272 key
->data
= mem_maybedup(mctx
, sr
.base
, key
->datalen
);
273 if (key
->data
== NULL
)
274 return (ISC_R_NOMEMORY
);
277 return (ISC_R_SUCCESS
);
281 freestruct_key(ARGS_FREESTRUCT
) {
282 dns_rdata_key_t
*key
= (dns_rdata_key_t
*) source
;
284 REQUIRE(source
!= NULL
);
285 REQUIRE(key
->common
.rdtype
== 25);
287 if (key
->mctx
== NULL
)
290 if (key
->data
!= NULL
)
291 isc_mem_free(key
->mctx
, key
->data
);
295 static inline isc_result_t
296 additionaldata_key(ARGS_ADDLDATA
) {
297 REQUIRE(rdata
->type
== 25);
303 return (ISC_R_SUCCESS
);
306 static inline isc_result_t
307 digest_key(ARGS_DIGEST
) {
310 REQUIRE(rdata
->type
== 25);
312 dns_rdata_toregion(rdata
, &r
);
314 return ((digest
)(arg
, &r
));
317 static inline isc_boolean_t
318 checkowner_key(ARGS_CHECKOWNER
) {
330 static inline isc_boolean_t
331 checknames_key(ARGS_CHECKNAMES
) {
333 REQUIRE(rdata
->type
== 25);
343 casecompare_key(ARGS_COMPARE
) {
344 return (compare_key(rdata1
, rdata2
));
347 #endif /* RDATA_GENERIC_KEY_25_C */