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: tkey_249.c,v 1.14 2024/04/23 13:34:50 jsg Exp $ */
18
19 /*
20 * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
21 */
22
23 /* draft-ietf-dnsext-tkey-01.txt */
24
25 #ifndef RDATA_GENERIC_TKEY_249_C
26 #define RDATA_GENERIC_TKEY_249_C
27
28 static inline isc_result_t
totext_tkey(ARGS_TOTEXT)29 totext_tkey(ARGS_TOTEXT) {
30 isc_region_t sr, dr;
31 char buf[sizeof("4294967295 ")];
32 unsigned long n;
33 dns_name_t name;
34 dns_name_t prefix;
35 int sub;
36
37 REQUIRE(rdata->type == dns_rdatatype_tkey);
38 REQUIRE(rdata->length != 0);
39
40 dns_rdata_toregion(rdata, &sr);
41
42 /*
43 * Algorithm.
44 */
45 dns_name_init(&name, NULL);
46 dns_name_init(&prefix, NULL);
47 dns_name_fromregion(&name, &sr);
48 sub = name_prefix(&name, tctx->origin, &prefix);
49 RETERR(dns_name_totext(&prefix, sub, target));
50 RETERR(isc_str_tobuffer(" ", target));
51 isc_region_consume(&sr, name_length(&name));
52
53 /*
54 * Inception.
55 */
56 n = uint32_fromregion(&sr);
57 isc_region_consume(&sr, 4);
58 snprintf(buf, sizeof(buf), "%lu ", n);
59 RETERR(isc_str_tobuffer(buf, target));
60
61 /*
62 * Expiration.
63 */
64 n = uint32_fromregion(&sr);
65 isc_region_consume(&sr, 4);
66 snprintf(buf, sizeof(buf), "%lu ", n);
67 RETERR(isc_str_tobuffer(buf, target));
68
69 /*
70 * Mode.
71 */
72 n = uint16_fromregion(&sr);
73 isc_region_consume(&sr, 2);
74 snprintf(buf, sizeof(buf), "%lu ", n);
75 RETERR(isc_str_tobuffer(buf, target));
76
77 /*
78 * Error.
79 */
80 n = uint16_fromregion(&sr);
81 isc_region_consume(&sr, 2);
82 if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
83 RETERR(isc_str_tobuffer(" ", target));
84 else {
85 snprintf(buf, sizeof(buf), "%lu ", n);
86 RETERR(isc_str_tobuffer(buf, target));
87 }
88
89 /*
90 * Key Size.
91 */
92 n = uint16_fromregion(&sr);
93 isc_region_consume(&sr, 2);
94 snprintf(buf, sizeof(buf), "%lu", n);
95 RETERR(isc_str_tobuffer(buf, target));
96
97 /*
98 * Key Data.
99 */
100 REQUIRE(n <= sr.length);
101 dr = sr;
102 dr.length = n;
103 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
104 RETERR(isc_str_tobuffer(" (", target));
105 RETERR(isc_str_tobuffer(tctx->linebreak, target));
106 if (tctx->width == 0) /* No splitting */
107 RETERR(isc_base64_totext(&dr, 60, "", target));
108 else
109 RETERR(isc_base64_totext(&dr, tctx->width - 2,
110 tctx->linebreak, target));
111 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
112 RETERR(isc_str_tobuffer(" ) ", target));
113 else
114 RETERR(isc_str_tobuffer(" ", target));
115 isc_region_consume(&sr, n);
116
117 /*
118 * Other Size.
119 */
120 n = uint16_fromregion(&sr);
121 isc_region_consume(&sr, 2);
122 snprintf(buf, sizeof(buf), "%lu", n);
123 RETERR(isc_str_tobuffer(buf, target));
124
125 /*
126 * Other Data.
127 */
128 REQUIRE(n <= sr.length);
129 if (n != 0U) {
130 dr = sr;
131 dr.length = n;
132 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
133 RETERR(isc_str_tobuffer(" (", target));
134 RETERR(isc_str_tobuffer(tctx->linebreak, target));
135 if (tctx->width == 0) /* No splitting */
136 RETERR(isc_base64_totext(&dr, 60, "", target));
137 else
138 RETERR(isc_base64_totext(&dr, tctx->width - 2,
139 tctx->linebreak, target));
140 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
141 RETERR(isc_str_tobuffer(" )", target));
142 }
143 return (ISC_R_SUCCESS);
144 }
145
146 static inline isc_result_t
fromwire_tkey(ARGS_FROMWIRE)147 fromwire_tkey(ARGS_FROMWIRE) {
148 isc_region_t sr;
149 unsigned long n;
150 dns_name_t name;
151
152 REQUIRE(type == dns_rdatatype_tkey);
153
154 UNUSED(type);
155 UNUSED(rdclass);
156
157 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
158
159 /*
160 * Algorithm.
161 */
162 dns_name_init(&name, NULL);
163 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
164
165 /*
166 * Inception: 4
167 * Expiration: 4
168 * Mode: 2
169 * Error: 2
170 */
171 isc_buffer_activeregion(source, &sr);
172 if (sr.length < 12)
173 return (ISC_R_UNEXPECTEDEND);
174 RETERR(isc_mem_tobuffer(target, sr.base, 12));
175 isc_region_consume(&sr, 12);
176 isc_buffer_forward(source, 12);
177
178 /*
179 * Key Length + Key Data.
180 */
181 if (sr.length < 2)
182 return (ISC_R_UNEXPECTEDEND);
183 n = uint16_fromregion(&sr);
184 if (sr.length < n + 2)
185 return (ISC_R_UNEXPECTEDEND);
186 RETERR(isc_mem_tobuffer(target, sr.base, n + 2));
187 isc_region_consume(&sr, n + 2);
188 isc_buffer_forward(source, n + 2);
189
190 /*
191 * Other Length + Other Data.
192 */
193 if (sr.length < 2)
194 return (ISC_R_UNEXPECTEDEND);
195 n = uint16_fromregion(&sr);
196 if (sr.length < n + 2)
197 return (ISC_R_UNEXPECTEDEND);
198 isc_buffer_forward(source, n + 2);
199 return (isc_mem_tobuffer(target, sr.base, n + 2));
200 }
201
202 static inline isc_result_t
towire_tkey(ARGS_TOWIRE)203 towire_tkey(ARGS_TOWIRE) {
204 isc_region_t sr;
205 dns_name_t name;
206 dns_offsets_t offsets;
207
208 REQUIRE(rdata->type == dns_rdatatype_tkey);
209 REQUIRE(rdata->length != 0);
210
211 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
212 /*
213 * Algorithm.
214 */
215 dns_rdata_toregion(rdata, &sr);
216 dns_name_init(&name, offsets);
217 dns_name_fromregion(&name, &sr);
218 RETERR(dns_name_towire(&name, cctx, target));
219 isc_region_consume(&sr, name_length(&name));
220
221 return (isc_mem_tobuffer(target, sr.base, sr.length));
222 }
223
224 #endif /* RDATA_GENERIC_TKEY_249_C */
225