1 /* $NetBSD: key_25.c,v 1.6 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2013 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: 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
) {
52 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
54 RETTOK(dns_keyflags_fromtext(&flags
, &token
.value
.as_textregion
));
55 RETERR(uint16_tobuffer(flags
, target
));
58 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
60 RETTOK(dns_secproto_fromtext(&proto
, &token
.value
.as_textregion
));
61 RETERR(mem_tobuffer(target
, &proto
, 1));
64 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
66 RETTOK(dns_secalg_fromtext(&alg
, &token
.value
.as_textregion
));
67 RETERR(mem_tobuffer(target
, &alg
, 1));
70 if ((flags
& 0xc000) == 0xc000)
71 return (ISC_R_SUCCESS
);
73 result
= isc_base64_tobuffer(lexer
, target
, -1);
74 if (result
!= ISC_R_SUCCESS
)
77 /* Ensure there's at least enough data to compute a key ID for MD5 */
78 if (alg
== DST_ALG_RSAMD5
&& isc_buffer_usedlength(target
) < 7)
79 return (ISC_R_UNEXPECTEDEND
);
81 return (ISC_R_SUCCESS
);
84 static inline isc_result_t
85 totext_key(ARGS_TOTEXT
) {
87 char buf
[sizeof("64000")];
89 unsigned char algorithm
;
90 char namebuf
[DNS_NAME_FORMATSIZE
];
92 REQUIRE(rdata
->type
== 25);
93 REQUIRE(rdata
->length
!= 0);
95 dns_rdata_toregion(rdata
, &sr
);
98 flags
= uint16_fromregion(&sr
);
99 isc_region_consume(&sr
, 2);
100 sprintf(buf
, "%u", flags
);
101 RETERR(str_totext(buf
, target
));
102 RETERR(str_totext(" ", target
));
105 sprintf(buf
, "%u", sr
.base
[0]);
106 isc_region_consume(&sr
, 1);
107 RETERR(str_totext(buf
, target
));
108 RETERR(str_totext(" ", target
));
111 algorithm
= sr
.base
[0];
112 sprintf(buf
, "%u", algorithm
);
113 isc_region_consume(&sr
, 1);
114 RETERR(str_totext(buf
, target
));
117 if ((flags
& 0xc000) == 0xc000)
118 return (ISC_R_SUCCESS
);
120 if ((tctx
->flags
& DNS_STYLEFLAG_RRCOMMENT
) != 0 &&
121 algorithm
== DNS_KEYALG_PRIVATEDNS
) {
123 dns_name_init(&name
, NULL
);
124 dns_name_fromregion(&name
, &sr
);
125 dns_name_format(&name
, namebuf
, sizeof(namebuf
));
130 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
131 RETERR(str_totext(" (", target
));
132 RETERR(str_totext(tctx
->linebreak
, target
));
133 if (tctx
->width
== 0) /* No splitting */
134 RETERR(isc_base64_totext(&sr
, 60, "", target
));
136 RETERR(isc_base64_totext(&sr
, tctx
->width
- 2,
137 tctx
->linebreak
, target
));
139 if ((tctx
->flags
& DNS_STYLEFLAG_RRCOMMENT
) != 0)
140 RETERR(str_totext(tctx
->linebreak
, target
));
141 else if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
142 RETERR(str_totext(" ", target
));
144 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
145 RETERR(str_totext(")", target
));
147 if ((tctx
->flags
& DNS_STYLEFLAG_RRCOMMENT
) != 0) {
150 RETERR(str_totext(" ; key id = ", target
));
151 dns_rdata_toregion(rdata
, &tmpr
);
152 sprintf(buf
, "%u", dst_region_computeid(&tmpr
, algorithm
));
153 RETERR(str_totext(buf
, target
));
154 if (algorithm
== DNS_KEYALG_PRIVATEDNS
) {
155 RETERR(str_totext(tctx
->linebreak
, target
));
156 RETERR(str_totext("; alg = ", target
));
157 RETERR(str_totext(namebuf
, target
));
160 return (ISC_R_SUCCESS
);
163 static inline isc_result_t
164 fromwire_key(ARGS_FROMWIRE
) {
165 unsigned char algorithm
;
175 isc_buffer_activeregion(source
, &sr
);
177 return (ISC_R_UNEXPECTEDEND
);
179 algorithm
= sr
.base
[3];
180 RETERR(mem_tobuffer(target
, sr
.base
, 4));
181 isc_region_consume(&sr
, 4);
182 isc_buffer_forward(source
, 4);
184 if (algorithm
== DNS_KEYALG_PRIVATEDNS
) {
186 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
187 dns_name_init(&name
, NULL
);
188 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
192 * RSAMD5 computes key ID differently from other
193 * algorithms: we need to ensure there's enough data
194 * present for the computation
196 if (algorithm
== DST_ALG_RSAMD5
&& sr
.length
< 3)
197 return (ISC_R_UNEXPECTEDEND
);
199 isc_buffer_activeregion(source
, &sr
);
200 isc_buffer_forward(source
, sr
.length
);
201 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
204 static inline isc_result_t
205 towire_key(ARGS_TOWIRE
) {
208 REQUIRE(rdata
->type
== 25);
209 REQUIRE(rdata
->length
!= 0);
213 dns_rdata_toregion(rdata
, &sr
);
214 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
218 compare_key(ARGS_COMPARE
) {
222 REQUIRE(rdata1
->type
== rdata2
->type
);
223 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
224 REQUIRE(rdata1
->type
== 25);
225 REQUIRE(rdata1
->length
!= 0);
226 REQUIRE(rdata2
->length
!= 0);
228 dns_rdata_toregion(rdata1
, &r1
);
229 dns_rdata_toregion(rdata2
, &r2
);
230 return (isc_region_compare(&r1
, &r2
));
233 static inline isc_result_t
234 fromstruct_key(ARGS_FROMSTRUCT
) {
235 dns_rdata_key_t
*key
= source
;
238 REQUIRE(source
!= NULL
);
239 REQUIRE(key
->common
.rdtype
== type
);
240 REQUIRE(key
->common
.rdclass
== rdclass
);
246 RETERR(uint16_tobuffer(key
->flags
, target
));
249 RETERR(uint8_tobuffer(key
->protocol
, target
));
252 RETERR(uint8_tobuffer(key
->algorithm
, target
));
255 return (mem_tobuffer(target
, key
->data
, key
->datalen
));
258 static inline isc_result_t
259 tostruct_key(ARGS_TOSTRUCT
) {
260 dns_rdata_key_t
*key
= target
;
263 REQUIRE(rdata
->type
== 25);
264 REQUIRE(target
!= NULL
);
265 REQUIRE(rdata
->length
!= 0);
267 key
->common
.rdclass
= rdata
->rdclass
;
268 key
->common
.rdtype
= rdata
->type
;
269 ISC_LINK_INIT(&key
->common
, link
);
271 dns_rdata_toregion(rdata
, &sr
);
275 return (ISC_R_UNEXPECTEDEND
);
276 key
->flags
= uint16_fromregion(&sr
);
277 isc_region_consume(&sr
, 2);
281 return (ISC_R_UNEXPECTEDEND
);
282 key
->protocol
= uint8_fromregion(&sr
);
283 isc_region_consume(&sr
, 1);
287 return (ISC_R_UNEXPECTEDEND
);
288 key
->algorithm
= uint8_fromregion(&sr
);
289 isc_region_consume(&sr
, 1);
292 key
->datalen
= sr
.length
;
293 key
->data
= mem_maybedup(mctx
, sr
.base
, key
->datalen
);
294 if (key
->data
== NULL
)
295 return (ISC_R_NOMEMORY
);
298 return (ISC_R_SUCCESS
);
302 freestruct_key(ARGS_FREESTRUCT
) {
303 dns_rdata_key_t
*key
= (dns_rdata_key_t
*) source
;
305 REQUIRE(source
!= NULL
);
306 REQUIRE(key
->common
.rdtype
== 25);
308 if (key
->mctx
== NULL
)
311 if (key
->data
!= NULL
)
312 isc_mem_free(key
->mctx
, key
->data
);
316 static inline isc_result_t
317 additionaldata_key(ARGS_ADDLDATA
) {
318 REQUIRE(rdata
->type
== 25);
324 return (ISC_R_SUCCESS
);
327 static inline isc_result_t
328 digest_key(ARGS_DIGEST
) {
331 REQUIRE(rdata
->type
== 25);
333 dns_rdata_toregion(rdata
, &r
);
335 return ((digest
)(arg
, &r
));
338 static inline isc_boolean_t
339 checkowner_key(ARGS_CHECKOWNER
) {
351 static inline isc_boolean_t
352 checknames_key(ARGS_CHECKNAMES
) {
354 REQUIRE(rdata
->type
== 25);
364 casecompare_key(ARGS_COMPARE
) {
365 return (compare_key(rdata1
, rdata2
));
368 #endif /* RDATA_GENERIC_KEY_25_C */