1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: nsec3param_51.c,v 1.13 2020/02/26 18:49:02 florian Exp $ */
18 
19 /*
20  * Copyright (C) 2004  Nominet, Ltd.
21  *
22  * Permission to use, copy, modify, and distribute this software for any
23  * purpose with or without fee is hereby granted, provided that the above
24  * copyright notice and this permission notice appear in all copies.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS" AND NOMINET DISCLAIMS ALL WARRANTIES WITH
27  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
28  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
29  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
30  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
31  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32  * PERFORMANCE OF THIS SOFTWARE.
33  */
34 
35 /* RFC 5155 */
36 
37 #ifndef RDATA_GENERIC_NSEC3PARAM_51_C
38 #define RDATA_GENERIC_NSEC3PARAM_51_C
39 
40 #include <isc/base32.h>
41 
42 static inline isc_result_t
totext_nsec3param(ARGS_TOTEXT)43 totext_nsec3param(ARGS_TOTEXT) {
44 	isc_region_t sr;
45 	unsigned int i, j;
46 	unsigned char hash;
47 	unsigned char flags;
48 	char buf[sizeof("65535 ")];
49 	uint32_t iterations;
50 
51 	REQUIRE(rdata->type == dns_rdatatype_nsec3param);
52 	REQUIRE(rdata->length != 0);
53 
54 	UNUSED(tctx);
55 
56 	dns_rdata_toregion(rdata, &sr);
57 
58 	hash = uint8_fromregion(&sr);
59 	isc_region_consume(&sr, 1);
60 
61 	flags = uint8_fromregion(&sr);
62 	isc_region_consume(&sr, 1);
63 
64 	iterations = uint16_fromregion(&sr);
65 	isc_region_consume(&sr, 2);
66 
67 	snprintf(buf, sizeof(buf), "%u ", hash);
68 	RETERR(isc_str_tobuffer(buf, target));
69 
70 	snprintf(buf, sizeof(buf), "%u ", flags);
71 	RETERR(isc_str_tobuffer(buf, target));
72 
73 	snprintf(buf, sizeof(buf), "%u ", iterations);
74 	RETERR(isc_str_tobuffer(buf, target));
75 
76 	j = uint8_fromregion(&sr);
77 	isc_region_consume(&sr, 1);
78 	INSIST(j <= sr.length);
79 
80 	if (j != 0) {
81 		i = sr.length;
82 		sr.length = j;
83 		RETERR(isc_hex_totext(&sr, 1, "", target));
84 		sr.length = i - j;
85 	} else
86 		RETERR(isc_str_tobuffer("-", target));
87 
88 	return (ISC_R_SUCCESS);
89 }
90 
91 static inline isc_result_t
fromwire_nsec3param(ARGS_FROMWIRE)92 fromwire_nsec3param(ARGS_FROMWIRE) {
93 	isc_region_t sr, rr;
94 	unsigned int saltlen;
95 
96 	REQUIRE(type == dns_rdatatype_nsec3param);
97 
98 	UNUSED(type);
99 	UNUSED(rdclass);
100 	UNUSED(options);
101 	UNUSED(dctx);
102 
103 	isc_buffer_activeregion(source, &sr);
104 	rr = sr;
105 
106 	/* hash(1), flags(1), iterations(2), saltlen(1) */
107 	if (sr.length < 5U)
108 		return (DNS_R_FORMERR);
109 	saltlen = sr.base[4];
110 	isc_region_consume(&sr, 5);
111 
112 	if (sr.length < saltlen)
113 		return (DNS_R_FORMERR);
114 	isc_region_consume(&sr, saltlen);
115 	RETERR(isc_mem_tobuffer(target, rr.base, rr.length));
116 	isc_buffer_forward(source, rr.length);
117 	return (ISC_R_SUCCESS);
118 }
119 
120 static inline isc_result_t
towire_nsec3param(ARGS_TOWIRE)121 towire_nsec3param(ARGS_TOWIRE) {
122 	isc_region_t sr;
123 
124 	REQUIRE(rdata->type == dns_rdatatype_nsec3param);
125 	REQUIRE(rdata->length != 0);
126 
127 	UNUSED(cctx);
128 
129 	dns_rdata_toregion(rdata, &sr);
130 	return (isc_mem_tobuffer(target, sr.base, sr.length));
131 }
132 
133 #endif	/* RDATA_GENERIC_NSEC3PARAM_51_C */
134