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: sig_24.c,v 1.68 2009/12/04 22:06:37 tbox Exp */
22 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
26 #ifndef RDATA_GENERIC_SIG_24_C
27 #define RDATA_GENERIC_SIG_24_C
29 #define RRTYPE_SIG_ATTRIBUTES (0)
31 static inline isc_result_t
32 fromtext_sig(ARGS_FROMTEXT
) {
36 dns_rdatatype_t covered
;
41 isc_uint32_t time_signed
, time_expire
;
52 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
54 result
= dns_rdatatype_fromtext(&covered
, &token
.value
.as_textregion
);
55 if (result
!= ISC_R_SUCCESS
&& result
!= ISC_R_NOTIMPLEMENTED
) {
56 i
= strtol(DNS_AS_STR(token
), &e
, 10);
57 if (i
< 0 || i
> 65535)
61 covered
= (dns_rdatatype_t
)i
;
63 RETERR(uint16_tobuffer(covered
, target
));
68 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
70 RETTOK(dns_secalg_fromtext(&c
, &token
.value
.as_textregion
));
71 RETERR(mem_tobuffer(target
, &c
, 1));
76 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
78 if (token
.value
.as_ulong
> 0xffU
)
80 c
= (unsigned char)token
.value
.as_ulong
;
81 RETERR(mem_tobuffer(target
, &c
, 1));
86 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
88 RETERR(uint32_tobuffer(token
.value
.as_ulong
, target
));
91 * Signature expiration.
93 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
95 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &time_expire
));
96 RETERR(uint32_tobuffer(time_expire
, target
));
101 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
103 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &time_signed
));
104 RETERR(uint32_tobuffer(time_signed
, target
));
109 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
111 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
116 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
118 dns_name_init(&name
, NULL
);
119 buffer_fromregion(&buffer
, &token
.value
.as_region
);
120 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
121 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
126 return (isc_base64_tobuffer(lexer
, target
, -1));
129 static inline isc_result_t
130 totext_sig(ARGS_TOTEXT
) {
132 char buf
[sizeof("4294967295")];
133 dns_rdatatype_t covered
;
142 REQUIRE(rdata
->type
== 24);
143 REQUIRE(rdata
->length
!= 0);
145 dns_rdata_toregion(rdata
, &sr
);
150 covered
= uint16_fromregion(&sr
);
151 isc_region_consume(&sr
, 2);
153 * XXXAG We should have something like dns_rdatatype_isknown()
154 * that does the right thing with type 0.
156 if (dns_rdatatype_isknown(covered
) && covered
!= 0) {
157 RETERR(dns_rdatatype_totext(covered
, target
));
159 char buf
[sizeof("65535")];
160 sprintf(buf
, "%u", covered
);
161 RETERR(str_totext(buf
, target
));
163 RETERR(str_totext(" ", target
));
168 sprintf(buf
, "%u", sr
.base
[0]);
169 isc_region_consume(&sr
, 1);
170 RETERR(str_totext(buf
, target
));
171 RETERR(str_totext(" ", target
));
176 sprintf(buf
, "%u", sr
.base
[0]);
177 isc_region_consume(&sr
, 1);
178 RETERR(str_totext(buf
, target
));
179 RETERR(str_totext(" ", target
));
184 ttl
= uint32_fromregion(&sr
);
185 isc_region_consume(&sr
, 4);
186 sprintf(buf
, "%lu", ttl
);
187 RETERR(str_totext(buf
, target
));
188 RETERR(str_totext(" ", target
));
193 exp
= uint32_fromregion(&sr
);
194 isc_region_consume(&sr
, 4);
195 RETERR(dns_time32_totext(exp
, target
));
197 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
198 RETERR(str_totext(" (", target
));
199 RETERR(str_totext(tctx
->linebreak
, target
));
204 when
= uint32_fromregion(&sr
);
205 isc_region_consume(&sr
, 4);
206 RETERR(dns_time32_totext(when
, target
));
207 RETERR(str_totext(" ", target
));
212 foot
= uint16_fromregion(&sr
);
213 isc_region_consume(&sr
, 2);
214 sprintf(buf
, "%lu", foot
);
215 RETERR(str_totext(buf
, target
));
216 RETERR(str_totext(" ", target
));
221 dns_name_init(&name
, NULL
);
222 dns_name_init(&prefix
, NULL
);
223 dns_name_fromregion(&name
, &sr
);
224 isc_region_consume(&sr
, name_length(&name
));
225 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
226 RETERR(dns_name_totext(&prefix
, sub
, target
));
231 RETERR(str_totext(tctx
->linebreak
, target
));
232 RETERR(isc_base64_totext(&sr
, tctx
->width
- 2,
233 tctx
->linebreak
, target
));
234 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
235 RETERR(str_totext(" )", target
));
237 return (ISC_R_SUCCESS
);
240 static inline isc_result_t
241 fromwire_sig(ARGS_FROMWIRE
) {
250 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
252 isc_buffer_activeregion(source
, &sr
);
258 * signature expiration: 4
263 return (ISC_R_UNEXPECTEDEND
);
265 isc_buffer_forward(source
, 18);
266 RETERR(mem_tobuffer(target
, sr
.base
, 18));
271 dns_name_init(&name
, NULL
);
272 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
277 isc_buffer_activeregion(source
, &sr
);
278 isc_buffer_forward(source
, sr
.length
);
279 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
282 static inline isc_result_t
283 towire_sig(ARGS_TOWIRE
) {
286 dns_offsets_t offsets
;
288 REQUIRE(rdata
->type
== 24);
289 REQUIRE(rdata
->length
!= 0);
291 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
292 dns_rdata_toregion(rdata
, &sr
);
298 * signature expiration: 4
302 RETERR(mem_tobuffer(target
, sr
.base
, 18));
303 isc_region_consume(&sr
, 18);
308 dns_name_init(&name
, offsets
);
309 dns_name_fromregion(&name
, &sr
);
310 isc_region_consume(&sr
, name_length(&name
));
311 RETERR(dns_name_towire(&name
, cctx
, target
));
316 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
320 compare_sig(ARGS_COMPARE
) {
327 REQUIRE(rdata1
->type
== rdata2
->type
);
328 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
329 REQUIRE(rdata1
->type
== 24);
330 REQUIRE(rdata1
->length
!= 0);
331 REQUIRE(rdata2
->length
!= 0);
333 dns_rdata_toregion(rdata1
, &r1
);
334 dns_rdata_toregion(rdata2
, &r2
);
336 INSIST(r1
.length
> 18);
337 INSIST(r2
.length
> 18);
340 order
= isc_region_compare(&r1
, &r2
);
344 dns_name_init(&name1
, NULL
);
345 dns_name_init(&name2
, NULL
);
346 dns_rdata_toregion(rdata1
, &r1
);
347 dns_rdata_toregion(rdata2
, &r2
);
348 isc_region_consume(&r1
, 18);
349 isc_region_consume(&r2
, 18);
350 dns_name_fromregion(&name1
, &r1
);
351 dns_name_fromregion(&name2
, &r2
);
352 order
= dns_name_rdatacompare(&name1
, &name2
);
356 isc_region_consume(&r1
, name_length(&name1
));
357 isc_region_consume(&r2
, name_length(&name2
));
359 return (isc_region_compare(&r1
, &r2
));
362 static inline isc_result_t
363 fromstruct_sig(ARGS_FROMSTRUCT
) {
364 dns_rdata_sig_t
*sig
= source
;
367 REQUIRE(source
!= NULL
);
368 REQUIRE(sig
->common
.rdtype
== type
);
369 REQUIRE(sig
->common
.rdclass
== rdclass
);
370 REQUIRE(sig
->signature
!= NULL
|| sig
->siglen
== 0);
378 RETERR(uint16_tobuffer(sig
->covered
, target
));
383 RETERR(uint8_tobuffer(sig
->algorithm
, target
));
388 RETERR(uint8_tobuffer(sig
->labels
, target
));
393 RETERR(uint32_tobuffer(sig
->originalttl
, target
));
398 RETERR(uint32_tobuffer(sig
->timeexpire
, target
));
403 RETERR(uint32_tobuffer(sig
->timesigned
, target
));
408 RETERR(uint16_tobuffer(sig
->keyid
, target
));
413 RETERR(name_tobuffer(&sig
->signer
, target
));
418 return (mem_tobuffer(target
, sig
->signature
, sig
->siglen
));
421 static inline isc_result_t
422 tostruct_sig(ARGS_TOSTRUCT
) {
424 dns_rdata_sig_t
*sig
= target
;
427 REQUIRE(rdata
->type
== 24);
428 REQUIRE(target
!= NULL
);
429 REQUIRE(rdata
->length
!= 0);
431 sig
->common
.rdclass
= rdata
->rdclass
;
432 sig
->common
.rdtype
= rdata
->type
;
433 ISC_LINK_INIT(&sig
->common
, link
);
435 dns_rdata_toregion(rdata
, &sr
);
440 sig
->covered
= uint16_fromregion(&sr
);
441 isc_region_consume(&sr
, 2);
446 sig
->algorithm
= uint8_fromregion(&sr
);
447 isc_region_consume(&sr
, 1);
452 sig
->labels
= uint8_fromregion(&sr
);
453 isc_region_consume(&sr
, 1);
458 sig
->originalttl
= uint32_fromregion(&sr
);
459 isc_region_consume(&sr
, 4);
464 sig
->timeexpire
= uint32_fromregion(&sr
);
465 isc_region_consume(&sr
, 4);
470 sig
->timesigned
= uint32_fromregion(&sr
);
471 isc_region_consume(&sr
, 4);
476 sig
->keyid
= uint16_fromregion(&sr
);
477 isc_region_consume(&sr
, 2);
479 dns_name_init(&signer
, NULL
);
480 dns_name_fromregion(&signer
, &sr
);
481 dns_name_init(&sig
->signer
, NULL
);
482 RETERR(name_duporclone(&signer
, mctx
, &sig
->signer
));
483 isc_region_consume(&sr
, name_length(&sig
->signer
));
488 sig
->siglen
= sr
.length
;
489 sig
->signature
= mem_maybedup(mctx
, sr
.base
, sig
->siglen
);
490 if (sig
->signature
== NULL
)
495 return (ISC_R_SUCCESS
);
499 dns_name_free(&sig
->signer
, mctx
);
500 return (ISC_R_NOMEMORY
);
504 freestruct_sig(ARGS_FREESTRUCT
) {
505 dns_rdata_sig_t
*sig
= (dns_rdata_sig_t
*) source
;
507 REQUIRE(source
!= NULL
);
508 REQUIRE(sig
->common
.rdtype
== 24);
510 if (sig
->mctx
== NULL
)
513 dns_name_free(&sig
->signer
, sig
->mctx
);
514 if (sig
->signature
!= NULL
)
515 isc_mem_free(sig
->mctx
, sig
->signature
);
519 static inline isc_result_t
520 additionaldata_sig(ARGS_ADDLDATA
) {
521 REQUIRE(rdata
->type
== 24);
527 return (ISC_R_SUCCESS
);
530 static inline isc_result_t
531 digest_sig(ARGS_DIGEST
) {
533 REQUIRE(rdata
->type
== 24);
539 return (ISC_R_NOTIMPLEMENTED
);
542 static inline dns_rdatatype_t
543 covers_sig(dns_rdata_t
*rdata
) {
544 dns_rdatatype_t type
;
547 REQUIRE(rdata
->type
== 24);
549 dns_rdata_toregion(rdata
, &r
);
550 type
= uint16_fromregion(&r
);
555 static inline isc_boolean_t
556 checkowner_sig(ARGS_CHECKOWNER
) {
568 static inline isc_boolean_t
569 checknames_sig(ARGS_CHECKNAMES
) {
571 REQUIRE(rdata
->type
== 24);
581 casecompare_sig(ARGS_COMPARE
) {
582 return (compare_sig(rdata1
, rdata2
));
584 #endif /* RDATA_GENERIC_SIG_24_C */