Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / lib / dns / rdata / generic / rrsig_46.c
blob4ac68a6ea9714dafbe1d3a2223c2b57a9ef4679a
1 /* $NetBSD: rrsig_46.c,v 1.7 2015/07/08 17:28:59 christos Exp $ */
3 /*
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.
20 /* Id */
22 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
24 /* RFC2535 */
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) {
33 isc_token_t token;
34 unsigned char c;
35 long i;
36 dns_rdatatype_t covered;
37 char *e;
38 isc_result_t result;
39 dns_name_t name;
40 isc_buffer_t buffer;
41 isc_uint32_t time_signed, time_expire;
43 REQUIRE(type == 46);
45 UNUSED(type);
46 UNUSED(rdclass);
47 UNUSED(callbacks);
50 * Type covered.
52 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
53 ISC_FALSE));
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)
58 RETTOK(ISC_R_RANGE);
59 if (*e != 0)
60 RETTOK(result);
61 covered = (dns_rdatatype_t)i;
63 RETERR(uint16_tobuffer(covered, target));
66 * Algorithm.
68 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
69 ISC_FALSE));
70 RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
71 RETERR(mem_tobuffer(target, &c, 1));
74 * Labels.
76 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
77 ISC_FALSE));
78 if (token.value.as_ulong > 0xffU)
79 RETTOK(ISC_R_RANGE);
80 c = (unsigned char)token.value.as_ulong;
81 RETERR(mem_tobuffer(target, &c, 1));
84 * Original ttl.
86 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
87 ISC_FALSE));
88 RETERR(uint32_tobuffer(token.value.as_ulong, target));
91 * Signature expiration.
93 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
94 ISC_FALSE));
95 if (strlen(DNS_AS_STR(token)) <= 10U &&
96 *DNS_AS_STR(token) != '-' && *DNS_AS_STR(token) != '+') {
97 char *end;
98 unsigned long u;
99 isc_uint64_t u64;
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)
105 RETTOK(ISC_R_RANGE);
106 time_expire = u;
107 } else
108 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
109 RETERR(uint32_tobuffer(time_expire, target));
112 * Time signed.
114 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
115 ISC_FALSE));
116 if (strlen(DNS_AS_STR(token)) <= 10U &&
117 *DNS_AS_STR(token) != '-' && *DNS_AS_STR(token) != '+') {
118 char *end;
119 unsigned long u;
120 isc_uint64_t u64;
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)
126 RETTOK(ISC_R_RANGE);
127 time_signed = u;
128 } else
129 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
130 RETERR(uint32_tobuffer(time_signed, target));
133 * Key footprint.
135 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
136 ISC_FALSE));
137 RETERR(uint16_tobuffer(token.value.as_ulong, target));
140 * Signer.
142 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
143 ISC_FALSE));
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));
150 * Sig.
152 return (isc_base64_tobuffer(lexer, target, -1));
155 static inline isc_result_t
156 totext_rrsig(ARGS_TOTEXT) {
157 isc_region_t sr;
158 char buf[sizeof("4294967295")];
159 dns_rdatatype_t covered;
160 unsigned long ttl;
161 unsigned long when;
162 unsigned long exp;
163 unsigned long foot;
164 dns_name_t name;
166 REQUIRE(rdata->type == 46);
167 REQUIRE(rdata->length != 0);
169 dns_rdata_toregion(rdata, &sr);
172 * Type covered.
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));
182 } else {
183 sprintf(buf, "TYPE%u", covered);
184 RETERR(str_totext(buf, target));
186 RETERR(str_totext(" ", target));
189 * Algorithm.
191 sprintf(buf, "%u", sr.base[0]);
192 isc_region_consume(&sr, 1);
193 RETERR(str_totext(buf, target));
194 RETERR(str_totext(" ", target));
197 * Labels.
199 sprintf(buf, "%u", sr.base[0]);
200 isc_region_consume(&sr, 1);
201 RETERR(str_totext(buf, target));
202 RETERR(str_totext(" ", target));
205 * Ttl.
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));
217 * Sig exp.
219 exp = uint32_fromregion(&sr);
220 isc_region_consume(&sr, 4);
221 RETERR(dns_time32_totext(exp, target));
222 RETERR(str_totext(" ", target));
225 * Time signed.
227 when = uint32_fromregion(&sr);
228 isc_region_consume(&sr, 4);
229 RETERR(dns_time32_totext(when, target));
230 RETERR(str_totext(" ", target));
233 * Footprint.
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));
242 * Signer.
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));
250 * Sig.
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));
256 else
257 RETERR(isc_base64_totext(&sr, tctx->width - 2,
258 tctx->linebreak, target));
259 } else
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) {
270 isc_region_t sr;
271 dns_name_t name;
273 REQUIRE(type == 46);
275 UNUSED(type);
276 UNUSED(rdclass);
278 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
280 isc_buffer_activeregion(source, &sr);
282 * type covered: 2
283 * algorithm: 1
284 * labels: 1
285 * original ttl: 4
286 * signature expiration: 4
287 * time signed: 4
288 * key footprint: 2
290 if (sr.length < 18)
291 return (ISC_R_UNEXPECTEDEND);
293 isc_buffer_forward(source, 18);
294 RETERR(mem_tobuffer(target, sr.base, 18));
297 * Signer.
299 dns_name_init(&name, NULL);
300 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
303 * Sig.
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) {
312 isc_region_t sr;
313 dns_name_t name;
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);
322 * type covered: 2
323 * algorithm: 1
324 * labels: 1
325 * original ttl: 4
326 * signature expiration: 4
327 * time signed: 4
328 * key footprint: 2
330 RETERR(mem_tobuffer(target, sr.base, 18));
331 isc_region_consume(&sr, 18);
334 * Signer.
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));
342 * Signature.
344 return (mem_tobuffer(target, sr.base, sr.length));
347 static inline int
348 compare_rrsig(ARGS_COMPARE) {
349 isc_region_t r1;
350 isc_region_t r2;
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;
367 REQUIRE(type == 46);
368 REQUIRE(source != NULL);
369 REQUIRE(sig->common.rdtype == type);
370 REQUIRE(sig->common.rdclass == rdclass);
371 REQUIRE(sig->signature != NULL || sig->siglen == 0);
373 UNUSED(type);
374 UNUSED(rdclass);
377 * Type covered.
379 RETERR(uint16_tobuffer(sig->covered, target));
382 * Algorithm.
384 RETERR(uint8_tobuffer(sig->algorithm, target));
387 * Labels.
389 RETERR(uint8_tobuffer(sig->labels, target));
392 * Original TTL.
394 RETERR(uint32_tobuffer(sig->originalttl, target));
397 * Expire time.
399 RETERR(uint32_tobuffer(sig->timeexpire, target));
402 * Time signed.
404 RETERR(uint32_tobuffer(sig->timesigned, target));
407 * Key ID.
409 RETERR(uint16_tobuffer(sig->keyid, target));
412 * Signer name.
414 RETERR(name_tobuffer(&sig->signer, target));
417 * Signature.
419 return (mem_tobuffer(target, sig->signature, sig->siglen));
422 static inline isc_result_t
423 tostruct_rrsig(ARGS_TOSTRUCT) {
424 isc_region_t sr;
425 dns_rdata_rrsig_t *sig = target;
426 dns_name_t signer;
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);
439 * Type covered.
441 sig->covered = uint16_fromregion(&sr);
442 isc_region_consume(&sr, 2);
445 * Algorithm.
447 sig->algorithm = uint8_fromregion(&sr);
448 isc_region_consume(&sr, 1);
451 * Labels.
453 sig->labels = uint8_fromregion(&sr);
454 isc_region_consume(&sr, 1);
457 * Original TTL.
459 sig->originalttl = uint32_fromregion(&sr);
460 isc_region_consume(&sr, 4);
463 * Expire time.
465 sig->timeexpire = uint32_fromregion(&sr);
466 isc_region_consume(&sr, 4);
469 * Time signed.
471 sig->timesigned = uint32_fromregion(&sr);
472 isc_region_consume(&sr, 4);
475 * Key ID.
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));
487 * Signature.
489 sig->siglen = sr.length;
490 sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
491 if (sig->signature == NULL)
492 goto cleanup;
495 sig->mctx = mctx;
496 return (ISC_R_SUCCESS);
498 cleanup:
499 if (mctx != NULL)
500 dns_name_free(&sig->signer, mctx);
501 return (ISC_R_NOMEMORY);
504 static inline void
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)
512 return;
514 dns_name_free(&sig->signer, sig->mctx);
515 if (sig->signature != NULL)
516 isc_mem_free(sig->mctx, sig->signature);
517 sig->mctx = NULL;
520 static inline isc_result_t
521 additionaldata_rrsig(ARGS_ADDLDATA) {
522 REQUIRE(rdata->type == 46);
524 UNUSED(rdata);
525 UNUSED(add);
526 UNUSED(arg);
528 return (ISC_R_SUCCESS);
531 static inline isc_result_t
532 digest_rrsig(ARGS_DIGEST) {
534 REQUIRE(rdata->type == 46);
536 UNUSED(rdata);
537 UNUSED(digest);
538 UNUSED(arg);
540 return (ISC_R_NOTIMPLEMENTED);
543 static inline dns_rdatatype_t
544 covers_rrsig(dns_rdata_t *rdata) {
545 dns_rdatatype_t type;
546 isc_region_t r;
548 REQUIRE(rdata->type == 46);
550 dns_rdata_toregion(rdata, &r);
551 type = uint16_fromregion(&r);
553 return (type);
556 static inline isc_boolean_t
557 checkowner_rrsig(ARGS_CHECKOWNER) {
559 REQUIRE(type == 46);
561 UNUSED(name);
562 UNUSED(type);
563 UNUSED(rdclass);
564 UNUSED(wildcard);
566 return (ISC_TRUE);
569 static inline isc_boolean_t
570 checknames_rrsig(ARGS_CHECKNAMES) {
572 REQUIRE(rdata->type == 46);
574 UNUSED(rdata);
575 UNUSED(owner);
576 UNUSED(bad);
578 return (ISC_TRUE);
581 static inline int
582 casecompare_rrsig(ARGS_COMPARE) {
583 isc_region_t r1;
584 isc_region_t r2;
585 dns_name_t name1;
586 dns_name_t name2;
587 int order;
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);
600 r1.length = 18;
601 r2.length = 18;
602 order = isc_region_compare(&r1, &r2);
603 if (order != 0)
604 return (order);
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);
615 if (order != 0)
616 return (order);
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 */