xref: /openbsd-src/usr.bin/dig/lib/dns/rdata/generic/sig_24.c (revision 1fb015a8af3a7e9b85db2510147a155826ef04d9)
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: sig_24.c,v 1.13 2020/09/14 08:40:43 florian Exp $ */
18 
19 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
20 
21 /* RFC2535 */
22 
23 #ifndef RDATA_GENERIC_SIG_24_C
24 #define RDATA_GENERIC_SIG_24_C
25 
26 static inline isc_result_t
totext_sig(ARGS_TOTEXT)27 totext_sig(ARGS_TOTEXT) {
28 	isc_region_t sr;
29 	char buf[sizeof("4294967295")];
30 	dns_rdatatype_t covered;
31 	unsigned long ttl;
32 	unsigned long when;
33 	unsigned long exp;
34 	unsigned long foot;
35 	dns_name_t name;
36 	dns_name_t prefix;
37 	int sub;
38 
39 	REQUIRE(rdata->type == dns_rdatatype_sig);
40 	REQUIRE(rdata->length != 0);
41 
42 	dns_rdata_toregion(rdata, &sr);
43 
44 	/*
45 	 * Type covered.
46 	 */
47 	covered = uint16_fromregion(&sr);
48 	isc_region_consume(&sr, 2);
49 
50 	RETERR(dns_rdatatype_totext(covered, target));
51 	RETERR(isc_str_tobuffer(" ", target));
52 
53 	/*
54 	 * Algorithm.
55 	 */
56 	snprintf(buf, sizeof(buf), "%u", sr.base[0]);
57 	isc_region_consume(&sr, 1);
58 	RETERR(isc_str_tobuffer(buf, target));
59 	RETERR(isc_str_tobuffer(" ", target));
60 
61 	/*
62 	 * Labels.
63 	 */
64 	snprintf(buf, sizeof(buf), "%u", sr.base[0]);
65 	isc_region_consume(&sr, 1);
66 	RETERR(isc_str_tobuffer(buf, target));
67 	RETERR(isc_str_tobuffer(" ", target));
68 
69 	/*
70 	 * Ttl.
71 	 */
72 	ttl = uint32_fromregion(&sr);
73 	isc_region_consume(&sr, 4);
74 	snprintf(buf, sizeof(buf), "%lu", ttl);
75 	RETERR(isc_str_tobuffer(buf, target));
76 	RETERR(isc_str_tobuffer(" ", target));
77 
78 	/*
79 	 * Sig exp.
80 	 */
81 	exp = uint32_fromregion(&sr);
82 	isc_region_consume(&sr, 4);
83 	RETERR(dns_time32_totext(exp, target));
84 
85 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
86 		RETERR(isc_str_tobuffer(" (", target));
87 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
88 
89 	/*
90 	 * Time signed.
91 	 */
92 	when = uint32_fromregion(&sr);
93 	isc_region_consume(&sr, 4);
94 	RETERR(dns_time32_totext(when, target));
95 	RETERR(isc_str_tobuffer(" ", target));
96 
97 	/*
98 	 * Footprint.
99 	 */
100 	foot = uint16_fromregion(&sr);
101 	isc_region_consume(&sr, 2);
102 	snprintf(buf, sizeof(buf), "%lu", foot);
103 	RETERR(isc_str_tobuffer(buf, target));
104 	RETERR(isc_str_tobuffer(" ", target));
105 
106 	/*
107 	 * Signer.
108 	 */
109 	dns_name_init(&name, NULL);
110 	dns_name_init(&prefix, NULL);
111 	dns_name_fromregion(&name, &sr);
112 	isc_region_consume(&sr, name_length(&name));
113 	sub = name_prefix(&name, tctx->origin, &prefix);
114 	RETERR(dns_name_totext(&prefix, sub, target));
115 
116 	/*
117 	 * Sig.
118 	 */
119 	RETERR(isc_str_tobuffer(tctx->linebreak, target));
120 	if (tctx->width == 0)   /* No splitting */
121 		RETERR(isc_base64_totext(&sr, 60, "", target));
122 	else
123 		RETERR(isc_base64_totext(&sr, tctx->width - 2,
124 					 tctx->linebreak, target));
125 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
126 		RETERR(isc_str_tobuffer(" )", target));
127 
128 	return (ISC_R_SUCCESS);
129 }
130 
131 static inline isc_result_t
fromwire_sig(ARGS_FROMWIRE)132 fromwire_sig(ARGS_FROMWIRE) {
133 	isc_region_t sr;
134 	dns_name_t name;
135 
136 	REQUIRE(type == dns_rdatatype_sig);
137 
138 	UNUSED(type);
139 	UNUSED(rdclass);
140 
141 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
142 
143 	isc_buffer_activeregion(source, &sr);
144 	/*
145 	 * type covered: 2
146 	 * algorithm: 1
147 	 * labels: 1
148 	 * original ttl: 4
149 	 * signature expiration: 4
150 	 * time signed: 4
151 	 * key footprint: 2
152 	 */
153 	if (sr.length < 18)
154 		return (ISC_R_UNEXPECTEDEND);
155 
156 	isc_buffer_forward(source, 18);
157 	RETERR(isc_mem_tobuffer(target, sr.base, 18));
158 
159 	/*
160 	 * Signer.
161 	 */
162 	dns_name_init(&name, NULL);
163 	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
164 
165 	/*
166 	 * Sig.
167 	 */
168 	isc_buffer_activeregion(source, &sr);
169 	isc_buffer_forward(source, sr.length);
170 	return (isc_mem_tobuffer(target, sr.base, sr.length));
171 }
172 
173 static inline isc_result_t
towire_sig(ARGS_TOWIRE)174 towire_sig(ARGS_TOWIRE) {
175 	isc_region_t sr;
176 	dns_name_t name;
177 	dns_offsets_t offsets;
178 
179 	REQUIRE(rdata->type == dns_rdatatype_sig);
180 	REQUIRE(rdata->length != 0);
181 
182 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
183 	dns_rdata_toregion(rdata, &sr);
184 	/*
185 	 * type covered: 2
186 	 * algorithm: 1
187 	 * labels: 1
188 	 * original ttl: 4
189 	 * signature expiration: 4
190 	 * time signed: 4
191 	 * key footprint: 2
192 	 */
193 	RETERR(isc_mem_tobuffer(target, sr.base, 18));
194 	isc_region_consume(&sr, 18);
195 
196 	/*
197 	 * Signer.
198 	 */
199 	dns_name_init(&name, offsets);
200 	dns_name_fromregion(&name, &sr);
201 	isc_region_consume(&sr, name_length(&name));
202 	RETERR(dns_name_towire(&name, cctx, target));
203 
204 	/*
205 	 * Signature.
206 	 */
207 	return (isc_mem_tobuffer(target, sr.base, sr.length));
208 }
209 
210 static inline dns_rdatatype_t
covers_sig(dns_rdata_t * rdata)211 covers_sig(dns_rdata_t *rdata) {
212 	dns_rdatatype_t type;
213 	isc_region_t r;
214 
215 	REQUIRE(rdata->type == dns_rdatatype_sig);
216 
217 	dns_rdata_toregion(rdata, &r);
218 	type = uint16_fromregion(&r);
219 
220 	return (type);
221 }
222 
223 #endif	/* RDATA_GENERIC_SIG_24_C */
224