1 /* $NetBSD: sig_24.c,v 1.6 2015/07/08 17:28:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2015 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.
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 sprintf(buf
, "%u", covered
);
160 RETERR(str_totext(buf
, target
));
162 RETERR(str_totext(" ", target
));
167 sprintf(buf
, "%u", sr
.base
[0]);
168 isc_region_consume(&sr
, 1);
169 RETERR(str_totext(buf
, target
));
170 RETERR(str_totext(" ", target
));
175 sprintf(buf
, "%u", sr
.base
[0]);
176 isc_region_consume(&sr
, 1);
177 RETERR(str_totext(buf
, target
));
178 RETERR(str_totext(" ", target
));
183 ttl
= uint32_fromregion(&sr
);
184 isc_region_consume(&sr
, 4);
185 sprintf(buf
, "%lu", ttl
);
186 RETERR(str_totext(buf
, target
));
187 RETERR(str_totext(" ", target
));
192 exp
= uint32_fromregion(&sr
);
193 isc_region_consume(&sr
, 4);
194 RETERR(dns_time32_totext(exp
, target
));
196 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
197 RETERR(str_totext(" (", target
));
198 RETERR(str_totext(tctx
->linebreak
, target
));
203 when
= uint32_fromregion(&sr
);
204 isc_region_consume(&sr
, 4);
205 RETERR(dns_time32_totext(when
, target
));
206 RETERR(str_totext(" ", target
));
211 foot
= uint16_fromregion(&sr
);
212 isc_region_consume(&sr
, 2);
213 sprintf(buf
, "%lu", foot
);
214 RETERR(str_totext(buf
, target
));
215 RETERR(str_totext(" ", target
));
220 dns_name_init(&name
, NULL
);
221 dns_name_init(&prefix
, NULL
);
222 dns_name_fromregion(&name
, &sr
);
223 isc_region_consume(&sr
, name_length(&name
));
224 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
225 RETERR(dns_name_totext(&prefix
, sub
, target
));
230 RETERR(str_totext(tctx
->linebreak
, target
));
231 if (tctx
->width
== 0) /* No splitting */
232 RETERR(isc_base64_totext(&sr
, 60, "", target
));
234 RETERR(isc_base64_totext(&sr
, tctx
->width
- 2,
235 tctx
->linebreak
, target
));
236 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
237 RETERR(str_totext(" )", target
));
239 return (ISC_R_SUCCESS
);
242 static inline isc_result_t
243 fromwire_sig(ARGS_FROMWIRE
) {
252 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
254 isc_buffer_activeregion(source
, &sr
);
260 * signature expiration: 4
265 return (ISC_R_UNEXPECTEDEND
);
267 isc_buffer_forward(source
, 18);
268 RETERR(mem_tobuffer(target
, sr
.base
, 18));
273 dns_name_init(&name
, NULL
);
274 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
279 isc_buffer_activeregion(source
, &sr
);
280 isc_buffer_forward(source
, sr
.length
);
281 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
284 static inline isc_result_t
285 towire_sig(ARGS_TOWIRE
) {
288 dns_offsets_t offsets
;
290 REQUIRE(rdata
->type
== 24);
291 REQUIRE(rdata
->length
!= 0);
293 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
294 dns_rdata_toregion(rdata
, &sr
);
300 * signature expiration: 4
304 RETERR(mem_tobuffer(target
, sr
.base
, 18));
305 isc_region_consume(&sr
, 18);
310 dns_name_init(&name
, offsets
);
311 dns_name_fromregion(&name
, &sr
);
312 isc_region_consume(&sr
, name_length(&name
));
313 RETERR(dns_name_towire(&name
, cctx
, target
));
318 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
322 compare_sig(ARGS_COMPARE
) {
329 REQUIRE(rdata1
->type
== rdata2
->type
);
330 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
331 REQUIRE(rdata1
->type
== 24);
332 REQUIRE(rdata1
->length
!= 0);
333 REQUIRE(rdata2
->length
!= 0);
335 dns_rdata_toregion(rdata1
, &r1
);
336 dns_rdata_toregion(rdata2
, &r2
);
338 INSIST(r1
.length
> 18);
339 INSIST(r2
.length
> 18);
342 order
= isc_region_compare(&r1
, &r2
);
346 dns_name_init(&name1
, NULL
);
347 dns_name_init(&name2
, NULL
);
348 dns_rdata_toregion(rdata1
, &r1
);
349 dns_rdata_toregion(rdata2
, &r2
);
350 isc_region_consume(&r1
, 18);
351 isc_region_consume(&r2
, 18);
352 dns_name_fromregion(&name1
, &r1
);
353 dns_name_fromregion(&name2
, &r2
);
354 order
= dns_name_rdatacompare(&name1
, &name2
);
358 isc_region_consume(&r1
, name_length(&name1
));
359 isc_region_consume(&r2
, name_length(&name2
));
361 return (isc_region_compare(&r1
, &r2
));
364 static inline isc_result_t
365 fromstruct_sig(ARGS_FROMSTRUCT
) {
366 dns_rdata_sig_t
*sig
= source
;
369 REQUIRE(source
!= NULL
);
370 REQUIRE(sig
->common
.rdtype
== type
);
371 REQUIRE(sig
->common
.rdclass
== rdclass
);
372 REQUIRE(sig
->signature
!= NULL
|| sig
->siglen
== 0);
380 RETERR(uint16_tobuffer(sig
->covered
, target
));
385 RETERR(uint8_tobuffer(sig
->algorithm
, target
));
390 RETERR(uint8_tobuffer(sig
->labels
, target
));
395 RETERR(uint32_tobuffer(sig
->originalttl
, target
));
400 RETERR(uint32_tobuffer(sig
->timeexpire
, target
));
405 RETERR(uint32_tobuffer(sig
->timesigned
, target
));
410 RETERR(uint16_tobuffer(sig
->keyid
, target
));
415 RETERR(name_tobuffer(&sig
->signer
, target
));
420 return (mem_tobuffer(target
, sig
->signature
, sig
->siglen
));
423 static inline isc_result_t
424 tostruct_sig(ARGS_TOSTRUCT
) {
426 dns_rdata_sig_t
*sig
= target
;
429 REQUIRE(rdata
->type
== 24);
430 REQUIRE(target
!= NULL
);
431 REQUIRE(rdata
->length
!= 0);
433 sig
->common
.rdclass
= rdata
->rdclass
;
434 sig
->common
.rdtype
= rdata
->type
;
435 ISC_LINK_INIT(&sig
->common
, link
);
437 dns_rdata_toregion(rdata
, &sr
);
442 sig
->covered
= uint16_fromregion(&sr
);
443 isc_region_consume(&sr
, 2);
448 sig
->algorithm
= uint8_fromregion(&sr
);
449 isc_region_consume(&sr
, 1);
454 sig
->labels
= uint8_fromregion(&sr
);
455 isc_region_consume(&sr
, 1);
460 sig
->originalttl
= uint32_fromregion(&sr
);
461 isc_region_consume(&sr
, 4);
466 sig
->timeexpire
= uint32_fromregion(&sr
);
467 isc_region_consume(&sr
, 4);
472 sig
->timesigned
= uint32_fromregion(&sr
);
473 isc_region_consume(&sr
, 4);
478 sig
->keyid
= uint16_fromregion(&sr
);
479 isc_region_consume(&sr
, 2);
481 dns_name_init(&signer
, NULL
);
482 dns_name_fromregion(&signer
, &sr
);
483 dns_name_init(&sig
->signer
, NULL
);
484 RETERR(name_duporclone(&signer
, mctx
, &sig
->signer
));
485 isc_region_consume(&sr
, name_length(&sig
->signer
));
490 sig
->siglen
= sr
.length
;
491 sig
->signature
= mem_maybedup(mctx
, sr
.base
, sig
->siglen
);
492 if (sig
->signature
== NULL
)
497 return (ISC_R_SUCCESS
);
501 dns_name_free(&sig
->signer
, mctx
);
502 return (ISC_R_NOMEMORY
);
506 freestruct_sig(ARGS_FREESTRUCT
) {
507 dns_rdata_sig_t
*sig
= (dns_rdata_sig_t
*) source
;
509 REQUIRE(source
!= NULL
);
510 REQUIRE(sig
->common
.rdtype
== 24);
512 if (sig
->mctx
== NULL
)
515 dns_name_free(&sig
->signer
, sig
->mctx
);
516 if (sig
->signature
!= NULL
)
517 isc_mem_free(sig
->mctx
, sig
->signature
);
521 static inline isc_result_t
522 additionaldata_sig(ARGS_ADDLDATA
) {
523 REQUIRE(rdata
->type
== 24);
529 return (ISC_R_SUCCESS
);
532 static inline isc_result_t
533 digest_sig(ARGS_DIGEST
) {
535 REQUIRE(rdata
->type
== 24);
541 return (ISC_R_NOTIMPLEMENTED
);
544 static inline dns_rdatatype_t
545 covers_sig(dns_rdata_t
*rdata
) {
546 dns_rdatatype_t type
;
549 REQUIRE(rdata
->type
== 24);
551 dns_rdata_toregion(rdata
, &r
);
552 type
= uint16_fromregion(&r
);
557 static inline isc_boolean_t
558 checkowner_sig(ARGS_CHECKOWNER
) {
570 static inline isc_boolean_t
571 checknames_sig(ARGS_CHECKNAMES
) {
573 REQUIRE(rdata
->type
== 24);
583 casecompare_sig(ARGS_COMPARE
) {
584 return (compare_sig(rdata1
, rdata2
));
586 #endif /* RDATA_GENERIC_SIG_24_C */