1 /* $NetBSD: rrsig_46.c,v 1.7 2015/07/08 17:28:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2015 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.
22 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
26 #ifndef RDATA_GENERIC_RRSIG_46_C
27 #define RDATA_GENERIC_RRSIG_46_C
29 #define RRTYPE_RRSIG_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
31 static inline isc_result_t
32 fromtext_rrsig(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 if (strlen(DNS_AS_STR(token
)) <= 10U &&
96 *DNS_AS_STR(token
) != '-' && *DNS_AS_STR(token
) != '+') {
101 u64
= u
= strtoul(DNS_AS_STR(token
), &end
, 10);
102 if (u
== ULONG_MAX
|| *end
!= 0)
103 RETTOK(DNS_R_SYNTAX
);
104 if (u64
> 0xffffffffUL
)
108 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &time_expire
));
109 RETERR(uint32_tobuffer(time_expire
, target
));
114 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
116 if (strlen(DNS_AS_STR(token
)) <= 10U &&
117 *DNS_AS_STR(token
) != '-' && *DNS_AS_STR(token
) != '+') {
122 u64
= u
= strtoul(DNS_AS_STR(token
), &end
, 10);
123 if (u
== ULONG_MAX
|| *end
!= 0)
124 RETTOK(DNS_R_SYNTAX
);
125 if (u64
> 0xffffffffUL
)
129 RETTOK(dns_time32_fromtext(DNS_AS_STR(token
), &time_signed
));
130 RETERR(uint32_tobuffer(time_signed
, target
));
135 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
137 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
142 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
144 dns_name_init(&name
, NULL
);
145 buffer_fromregion(&buffer
, &token
.value
.as_region
);
146 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
147 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
152 return (isc_base64_tobuffer(lexer
, target
, -1));
155 static inline isc_result_t
156 totext_rrsig(ARGS_TOTEXT
) {
158 char buf
[sizeof("4294967295")];
159 dns_rdatatype_t covered
;
166 REQUIRE(rdata
->type
== 46);
167 REQUIRE(rdata
->length
!= 0);
169 dns_rdata_toregion(rdata
, &sr
);
174 covered
= uint16_fromregion(&sr
);
175 isc_region_consume(&sr
, 2);
177 * XXXAG We should have something like dns_rdatatype_isknown()
178 * that does the right thing with type 0.
180 if (dns_rdatatype_isknown(covered
) && covered
!= 0) {
181 RETERR(dns_rdatatype_totext(covered
, target
));
183 sprintf(buf
, "TYPE%u", covered
);
184 RETERR(str_totext(buf
, target
));
186 RETERR(str_totext(" ", target
));
191 sprintf(buf
, "%u", sr
.base
[0]);
192 isc_region_consume(&sr
, 1);
193 RETERR(str_totext(buf
, target
));
194 RETERR(str_totext(" ", target
));
199 sprintf(buf
, "%u", sr
.base
[0]);
200 isc_region_consume(&sr
, 1);
201 RETERR(str_totext(buf
, target
));
202 RETERR(str_totext(" ", target
));
207 ttl
= uint32_fromregion(&sr
);
208 isc_region_consume(&sr
, 4);
209 sprintf(buf
, "%lu", ttl
);
210 RETERR(str_totext(buf
, target
));
212 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
213 RETERR(str_totext(" (", target
));
214 RETERR(str_totext(tctx
->linebreak
, target
));
219 exp
= uint32_fromregion(&sr
);
220 isc_region_consume(&sr
, 4);
221 RETERR(dns_time32_totext(exp
, target
));
222 RETERR(str_totext(" ", target
));
227 when
= uint32_fromregion(&sr
);
228 isc_region_consume(&sr
, 4);
229 RETERR(dns_time32_totext(when
, target
));
230 RETERR(str_totext(" ", target
));
235 foot
= uint16_fromregion(&sr
);
236 isc_region_consume(&sr
, 2);
237 sprintf(buf
, "%lu", foot
);
238 RETERR(str_totext(buf
, target
));
239 RETERR(str_totext(" ", target
));
244 dns_name_init(&name
, NULL
);
245 dns_name_fromregion(&name
, &sr
);
246 isc_region_consume(&sr
, name_length(&name
));
247 RETERR(dns_name_totext(&name
, ISC_FALSE
, target
));
252 RETERR(str_totext(tctx
->linebreak
, target
));
253 if ((tctx
->flags
& DNS_STYLEFLAG_NOCRYPTO
) == 0) {
254 if (tctx
->width
== 0) /* No splitting */
255 RETERR(isc_base64_totext(&sr
, 60, "", target
));
257 RETERR(isc_base64_totext(&sr
, tctx
->width
- 2,
258 tctx
->linebreak
, target
));
260 RETERR(str_totext("[omitted]", target
));
262 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
263 RETERR(str_totext(" )", target
));
265 return (ISC_R_SUCCESS
);
268 static inline isc_result_t
269 fromwire_rrsig(ARGS_FROMWIRE
) {
278 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
280 isc_buffer_activeregion(source
, &sr
);
286 * signature expiration: 4
291 return (ISC_R_UNEXPECTEDEND
);
293 isc_buffer_forward(source
, 18);
294 RETERR(mem_tobuffer(target
, sr
.base
, 18));
299 dns_name_init(&name
, NULL
);
300 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
305 isc_buffer_activeregion(source
, &sr
);
306 isc_buffer_forward(source
, sr
.length
);
307 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
310 static inline isc_result_t
311 towire_rrsig(ARGS_TOWIRE
) {
314 dns_offsets_t offsets
;
316 REQUIRE(rdata
->type
== 46);
317 REQUIRE(rdata
->length
!= 0);
319 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
320 dns_rdata_toregion(rdata
, &sr
);
326 * signature expiration: 4
330 RETERR(mem_tobuffer(target
, sr
.base
, 18));
331 isc_region_consume(&sr
, 18);
336 dns_name_init(&name
, offsets
);
337 dns_name_fromregion(&name
, &sr
);
338 isc_region_consume(&sr
, name_length(&name
));
339 RETERR(dns_name_towire(&name
, cctx
, target
));
344 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
348 compare_rrsig(ARGS_COMPARE
) {
352 REQUIRE(rdata1
->type
== rdata2
->type
);
353 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
354 REQUIRE(rdata1
->type
== 46);
355 REQUIRE(rdata1
->length
!= 0);
356 REQUIRE(rdata2
->length
!= 0);
358 dns_rdata_toregion(rdata1
, &r1
);
359 dns_rdata_toregion(rdata2
, &r2
);
360 return (isc_region_compare(&r1
, &r2
));
363 static inline isc_result_t
364 fromstruct_rrsig(ARGS_FROMSTRUCT
) {
365 dns_rdata_rrsig_t
*sig
= source
;
368 REQUIRE(source
!= NULL
);
369 REQUIRE(sig
->common
.rdtype
== type
);
370 REQUIRE(sig
->common
.rdclass
== rdclass
);
371 REQUIRE(sig
->signature
!= NULL
|| sig
->siglen
== 0);
379 RETERR(uint16_tobuffer(sig
->covered
, target
));
384 RETERR(uint8_tobuffer(sig
->algorithm
, target
));
389 RETERR(uint8_tobuffer(sig
->labels
, target
));
394 RETERR(uint32_tobuffer(sig
->originalttl
, target
));
399 RETERR(uint32_tobuffer(sig
->timeexpire
, target
));
404 RETERR(uint32_tobuffer(sig
->timesigned
, target
));
409 RETERR(uint16_tobuffer(sig
->keyid
, target
));
414 RETERR(name_tobuffer(&sig
->signer
, target
));
419 return (mem_tobuffer(target
, sig
->signature
, sig
->siglen
));
422 static inline isc_result_t
423 tostruct_rrsig(ARGS_TOSTRUCT
) {
425 dns_rdata_rrsig_t
*sig
= target
;
428 REQUIRE(rdata
->type
== 46);
429 REQUIRE(target
!= NULL
);
430 REQUIRE(rdata
->length
!= 0);
432 sig
->common
.rdclass
= rdata
->rdclass
;
433 sig
->common
.rdtype
= rdata
->type
;
434 ISC_LINK_INIT(&sig
->common
, link
);
436 dns_rdata_toregion(rdata
, &sr
);
441 sig
->covered
= uint16_fromregion(&sr
);
442 isc_region_consume(&sr
, 2);
447 sig
->algorithm
= uint8_fromregion(&sr
);
448 isc_region_consume(&sr
, 1);
453 sig
->labels
= uint8_fromregion(&sr
);
454 isc_region_consume(&sr
, 1);
459 sig
->originalttl
= uint32_fromregion(&sr
);
460 isc_region_consume(&sr
, 4);
465 sig
->timeexpire
= uint32_fromregion(&sr
);
466 isc_region_consume(&sr
, 4);
471 sig
->timesigned
= uint32_fromregion(&sr
);
472 isc_region_consume(&sr
, 4);
477 sig
->keyid
= uint16_fromregion(&sr
);
478 isc_region_consume(&sr
, 2);
480 dns_name_init(&signer
, NULL
);
481 dns_name_fromregion(&signer
, &sr
);
482 dns_name_init(&sig
->signer
, NULL
);
483 RETERR(name_duporclone(&signer
, mctx
, &sig
->signer
));
484 isc_region_consume(&sr
, name_length(&sig
->signer
));
489 sig
->siglen
= sr
.length
;
490 sig
->signature
= mem_maybedup(mctx
, sr
.base
, sig
->siglen
);
491 if (sig
->signature
== NULL
)
496 return (ISC_R_SUCCESS
);
500 dns_name_free(&sig
->signer
, mctx
);
501 return (ISC_R_NOMEMORY
);
505 freestruct_rrsig(ARGS_FREESTRUCT
) {
506 dns_rdata_rrsig_t
*sig
= (dns_rdata_rrsig_t
*) source
;
508 REQUIRE(source
!= NULL
);
509 REQUIRE(sig
->common
.rdtype
== 46);
511 if (sig
->mctx
== NULL
)
514 dns_name_free(&sig
->signer
, sig
->mctx
);
515 if (sig
->signature
!= NULL
)
516 isc_mem_free(sig
->mctx
, sig
->signature
);
520 static inline isc_result_t
521 additionaldata_rrsig(ARGS_ADDLDATA
) {
522 REQUIRE(rdata
->type
== 46);
528 return (ISC_R_SUCCESS
);
531 static inline isc_result_t
532 digest_rrsig(ARGS_DIGEST
) {
534 REQUIRE(rdata
->type
== 46);
540 return (ISC_R_NOTIMPLEMENTED
);
543 static inline dns_rdatatype_t
544 covers_rrsig(dns_rdata_t
*rdata
) {
545 dns_rdatatype_t type
;
548 REQUIRE(rdata
->type
== 46);
550 dns_rdata_toregion(rdata
, &r
);
551 type
= uint16_fromregion(&r
);
556 static inline isc_boolean_t
557 checkowner_rrsig(ARGS_CHECKOWNER
) {
569 static inline isc_boolean_t
570 checknames_rrsig(ARGS_CHECKNAMES
) {
572 REQUIRE(rdata
->type
== 46);
582 casecompare_rrsig(ARGS_COMPARE
) {
589 REQUIRE(rdata1
->type
== rdata2
->type
);
590 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
591 REQUIRE(rdata1
->type
== 46);
592 REQUIRE(rdata1
->length
!= 0);
593 REQUIRE(rdata2
->length
!= 0);
595 dns_rdata_toregion(rdata1
, &r1
);
596 dns_rdata_toregion(rdata2
, &r2
);
598 INSIST(r1
.length
> 18);
599 INSIST(r2
.length
> 18);
602 order
= isc_region_compare(&r1
, &r2
);
606 dns_name_init(&name1
, NULL
);
607 dns_name_init(&name2
, NULL
);
608 dns_rdata_toregion(rdata1
, &r1
);
609 dns_rdata_toregion(rdata2
, &r2
);
610 isc_region_consume(&r1
, 18);
611 isc_region_consume(&r2
, 18);
612 dns_name_fromregion(&name1
, &r1
);
613 dns_name_fromregion(&name2
, &r2
);
614 order
= dns_name_rdatacompare(&name1
, &name2
);
618 isc_region_consume(&r1
, name_length(&name1
));
619 isc_region_consume(&r2
, name_length(&name2
));
621 return (isc_region_compare(&r1
, &r2
));
624 #endif /* RDATA_GENERIC_RRSIG_46_C */