1 /* $NetBSD: keydata_65533.c,v 1.8 2015/07/08 17:28:59 christos Exp $ */
4 * Copyright (C) 2009, 2011-2013, 2015 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 #ifndef GENERIC_KEYDATA_65533_C
20 #define GENERIC_KEYDATA_65533_C 1
23 #include <isc/stdtime.h>
27 #define RRTYPE_KEYDATA_ATTRIBUTES (0)
29 static inline isc_result_t
30 fromtext_keydata(ARGS_FROMTEXT
) {
36 isc_uint32_t refresh
, addhd
, removehd
;
38 REQUIRE(type
== 65533);
47 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
49 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &refresh
));
50 RETERR(uint32_tobuffer(refresh
, target
));
53 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
55 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &addhd
));
56 RETERR(uint32_tobuffer(addhd
, target
));
58 /* remove hold-down */
59 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
61 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &removehd
));
62 RETERR(uint32_tobuffer(removehd
, target
));
65 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
67 RETTOK(dns_keyflags_fromtext(&flags
, &token
.value
.as_textregion
));
68 RETERR(uint16_tobuffer(flags
, target
));
71 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
73 RETTOK(dns_secproto_fromtext(&proto
, &token
.value
.as_textregion
));
74 RETERR(mem_tobuffer(target
, &proto
, 1));
77 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
79 RETTOK(dns_secalg_fromtext(&alg
, &token
.value
.as_textregion
));
80 RETERR(mem_tobuffer(target
, &alg
, 1));
83 if ((flags
& 0xc000) == 0xc000)
84 return (ISC_R_SUCCESS
);
86 result
= isc_base64_tobuffer(lexer
, target
, -1);
87 if (result
!= ISC_R_SUCCESS
)
90 /* Ensure there's at least enough data to compute a key ID for MD5 */
91 if (alg
== DST_ALG_RSAMD5
&& isc_buffer_usedlength(target
) < 19)
92 return (ISC_R_UNEXPECTEDEND
);
94 return (ISC_R_SUCCESS
);
97 static inline isc_result_t
98 totext_keydata(ARGS_TOTEXT
) {
100 char buf
[sizeof("64000")];
102 unsigned char algorithm
;
103 unsigned long refresh
, add
, remove
;
104 char algbuf
[DNS_NAME_FORMATSIZE
];
107 REQUIRE(rdata
->type
== 65533);
109 if ((tctx
->flags
& DNS_STYLEFLAG_KEYDATA
) == 0 || rdata
->length
< 16)
110 return (unknown_totext(rdata
, tctx
, target
));
112 dns_rdata_toregion(rdata
, &sr
);
115 refresh
= uint32_fromregion(&sr
);
116 isc_region_consume(&sr
, 4);
117 RETERR(dns_time32_totext(refresh
, target
));
118 RETERR(str_totext(" ", target
));
121 add
= uint32_fromregion(&sr
);
122 isc_region_consume(&sr
, 4);
123 RETERR(dns_time32_totext(add
, target
));
124 RETERR(str_totext(" ", target
));
126 /* remove hold-down */
127 remove
= uint32_fromregion(&sr
);
128 isc_region_consume(&sr
, 4);
129 RETERR(dns_time32_totext(remove
, target
));
130 RETERR(str_totext(" ", target
));
133 flags
= uint16_fromregion(&sr
);
134 isc_region_consume(&sr
, 2);
135 sprintf(buf
, "%u", flags
);
136 RETERR(str_totext(buf
, target
));
137 RETERR(str_totext(" ", target
));
138 if ((flags
& DNS_KEYFLAG_KSK
) != 0) {
139 if (flags
& DNS_KEYFLAG_REVOKE
)
140 keyinfo
= "revoked KSK";
147 sprintf(buf
, "%u", sr
.base
[0]);
148 isc_region_consume(&sr
, 1);
149 RETERR(str_totext(buf
, target
));
150 RETERR(str_totext(" ", target
));
153 algorithm
= sr
.base
[0];
154 sprintf(buf
, "%u", algorithm
);
155 isc_region_consume(&sr
, 1);
156 RETERR(str_totext(buf
, target
));
159 if ((flags
& 0xc000) == 0xc000)
160 return (ISC_R_SUCCESS
);
163 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
164 RETERR(str_totext(" (", target
));
165 RETERR(str_totext(tctx
->linebreak
, target
));
166 if (tctx
->width
== 0) /* No splitting */
167 RETERR(isc_base64_totext(&sr
, 60, "", target
));
169 RETERR(isc_base64_totext(&sr
, tctx
->width
- 2,
170 tctx
->linebreak
, target
));
172 if ((tctx
->flags
& DNS_STYLEFLAG_RRCOMMENT
) != 0)
173 RETERR(str_totext(tctx
->linebreak
, target
));
174 else if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
175 RETERR(str_totext(" ", target
));
177 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
178 RETERR(str_totext(")", target
));
180 if ((tctx
->flags
& DNS_STYLEFLAG_RRCOMMENT
) != 0) {
182 char rbuf
[ISC_FORMATHTTPTIMESTAMP_SIZE
];
183 char abuf
[ISC_FORMATHTTPTIMESTAMP_SIZE
];
184 char dbuf
[ISC_FORMATHTTPTIMESTAMP_SIZE
];
187 RETERR(str_totext(" ; ", target
));
188 RETERR(str_totext(keyinfo
, target
));
189 dns_secalg_format((dns_secalg_t
) algorithm
, algbuf
,
191 RETERR(str_totext("; alg = ", target
));
192 RETERR(str_totext(algbuf
, target
));
193 RETERR(str_totext("; key id = ", target
));
194 dns_rdata_toregion(rdata
, &tmpr
);
195 /* Skip over refresh, addhd, and removehd */
196 isc_region_consume(&tmpr
, 12);
197 sprintf(buf
, "%u", dst_region_computeid(&tmpr
, algorithm
));
198 RETERR(str_totext(buf
, target
));
200 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0) {
203 isc_stdtime_get(&now
);
205 RETERR(str_totext(tctx
->linebreak
, target
));
206 RETERR(str_totext("; next refresh: ", target
));
207 isc_time_set(&t
, refresh
, 0);
208 isc_time_formathttptimestamp(&t
, rbuf
, sizeof(rbuf
));
209 RETERR(str_totext(rbuf
, target
));
212 RETERR(str_totext(tctx
->linebreak
, target
));
213 RETERR(str_totext("; no trust", target
));
215 RETERR(str_totext(tctx
->linebreak
, target
));
217 RETERR(str_totext("; trusted since: ",
220 RETERR(str_totext("; trust pending: ",
223 isc_time_set(&t
, add
, 0);
224 isc_time_formathttptimestamp(&t
, abuf
,
226 RETERR(str_totext(abuf
, target
));
230 RETERR(str_totext(tctx
->linebreak
, target
));
231 RETERR(str_totext("; removal pending: ",
233 isc_time_set(&t
, remove
, 0);
234 isc_time_formathttptimestamp(&t
, dbuf
,
236 RETERR(str_totext(dbuf
, target
));
241 return (ISC_R_SUCCESS
);
244 static inline isc_result_t
245 fromwire_keydata(ARGS_FROMWIRE
) {
248 REQUIRE(type
== 65533);
255 isc_buffer_activeregion(source
, &sr
);
256 isc_buffer_forward(source
, sr
.length
);
257 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
260 static inline isc_result_t
261 towire_keydata(ARGS_TOWIRE
) {
264 REQUIRE(rdata
->type
== 65533);
268 dns_rdata_toregion(rdata
, &sr
);
269 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
273 compare_keydata(ARGS_COMPARE
) {
277 REQUIRE(rdata1
->type
== rdata2
->type
);
278 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
279 REQUIRE(rdata1
->type
== 65533);
281 dns_rdata_toregion(rdata1
, &r1
);
282 dns_rdata_toregion(rdata2
, &r2
);
283 return (isc_region_compare(&r1
, &r2
));
286 static inline isc_result_t
287 fromstruct_keydata(ARGS_FROMSTRUCT
) {
288 dns_rdata_keydata_t
*keydata
= source
;
290 REQUIRE(type
== 65533);
291 REQUIRE(source
!= NULL
);
292 REQUIRE(keydata
->common
.rdtype
== type
);
293 REQUIRE(keydata
->common
.rdclass
== rdclass
);
299 RETERR(uint32_tobuffer(keydata
->refresh
, target
));
302 RETERR(uint32_tobuffer(keydata
->addhd
, target
));
304 /* Remove hold-down */
305 RETERR(uint32_tobuffer(keydata
->removehd
, target
));
308 RETERR(uint16_tobuffer(keydata
->flags
, target
));
311 RETERR(uint8_tobuffer(keydata
->protocol
, target
));
314 RETERR(uint8_tobuffer(keydata
->algorithm
, target
));
317 return (mem_tobuffer(target
, keydata
->data
, keydata
->datalen
));
320 static inline isc_result_t
321 tostruct_keydata(ARGS_TOSTRUCT
) {
322 dns_rdata_keydata_t
*keydata
= target
;
325 REQUIRE(rdata
->type
== 65533);
326 REQUIRE(target
!= NULL
);
328 keydata
->common
.rdclass
= rdata
->rdclass
;
329 keydata
->common
.rdtype
= rdata
->type
;
330 ISC_LINK_INIT(&keydata
->common
, link
);
332 dns_rdata_toregion(rdata
, &sr
);
336 return (ISC_R_UNEXPECTEDEND
);
337 keydata
->refresh
= uint32_fromregion(&sr
);
338 isc_region_consume(&sr
, 4);
342 return (ISC_R_UNEXPECTEDEND
);
343 keydata
->addhd
= uint32_fromregion(&sr
);
344 isc_region_consume(&sr
, 4);
346 /* Remove hold-down */
348 return (ISC_R_UNEXPECTEDEND
);
349 keydata
->removehd
= uint32_fromregion(&sr
);
350 isc_region_consume(&sr
, 4);
354 return (ISC_R_UNEXPECTEDEND
);
355 keydata
->flags
= uint16_fromregion(&sr
);
356 isc_region_consume(&sr
, 2);
360 return (ISC_R_UNEXPECTEDEND
);
361 keydata
->protocol
= uint8_fromregion(&sr
);
362 isc_region_consume(&sr
, 1);
366 return (ISC_R_UNEXPECTEDEND
);
367 keydata
->algorithm
= uint8_fromregion(&sr
);
368 isc_region_consume(&sr
, 1);
371 keydata
->datalen
= sr
.length
;
372 keydata
->data
= mem_maybedup(mctx
, sr
.base
, keydata
->datalen
);
373 if (keydata
->data
== NULL
)
374 return (ISC_R_NOMEMORY
);
376 keydata
->mctx
= mctx
;
377 return (ISC_R_SUCCESS
);
381 freestruct_keydata(ARGS_FREESTRUCT
) {
382 dns_rdata_keydata_t
*keydata
= (dns_rdata_keydata_t
*) source
;
384 REQUIRE(source
!= NULL
);
385 REQUIRE(keydata
->common
.rdtype
== 65533);
387 if (keydata
->mctx
== NULL
)
390 if (keydata
->data
!= NULL
)
391 isc_mem_free(keydata
->mctx
, keydata
->data
);
392 keydata
->mctx
= NULL
;
395 static inline isc_result_t
396 additionaldata_keydata(ARGS_ADDLDATA
) {
397 REQUIRE(rdata
->type
== 65533);
403 return (ISC_R_SUCCESS
);
406 static inline isc_result_t
407 digest_keydata(ARGS_DIGEST
) {
410 REQUIRE(rdata
->type
== 65533);
412 dns_rdata_toregion(rdata
, &r
);
414 return ((digest
)(arg
, &r
));
417 static inline isc_boolean_t
418 checkowner_keydata(ARGS_CHECKOWNER
) {
420 REQUIRE(type
== 65533);
430 static inline isc_boolean_t
431 checknames_keydata(ARGS_CHECKNAMES
) {
433 REQUIRE(rdata
->type
== 65533);
443 casecompare_keydata(ARGS_COMPARE
) {
444 return (compare_keydata(rdata1
, rdata2
));
447 #endif /* GENERIC_KEYDATA_65533_C */