1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: tkey_249.c,v 1.6 2020/02/24 17:43:52 florian Exp $ */
/*
* Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
*/
/* draft-ietf-dnsext-tkey-01.txt */
#ifndef RDATA_GENERIC_TKEY_249_C
#define RDATA_GENERIC_TKEY_249_C
#define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
static inline isc_result_t
totext_tkey(ARGS_TOTEXT) {
isc_region_t sr, dr;
char buf[sizeof("4294967295 ")];
unsigned long n;
dns_name_t name;
dns_name_t prefix;
isc_boolean_t sub;
REQUIRE(rdata->type == dns_rdatatype_tkey);
REQUIRE(rdata->length != 0);
dns_rdata_toregion(rdata, &sr);
/*
* Algorithm.
*/
dns_name_init(&name, NULL);
dns_name_init(&prefix, NULL);
dns_name_fromregion(&name, &sr);
sub = name_prefix(&name, tctx->origin, &prefix);
RETERR(dns_name_totext(&prefix, sub, target));
RETERR(str_totext(" ", target));
isc_region_consume(&sr, name_length(&name));
/*
* Inception.
*/
n = uint32_fromregion(&sr);
isc_region_consume(&sr, 4);
snprintf(buf, sizeof(buf), "%lu ", n);
RETERR(str_totext(buf, target));
/*
* Expiration.
*/
n = uint32_fromregion(&sr);
isc_region_consume(&sr, 4);
snprintf(buf, sizeof(buf), "%lu ", n);
RETERR(str_totext(buf, target));
/*
* Mode.
*/
n = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
snprintf(buf, sizeof(buf), "%lu ", n);
RETERR(str_totext(buf, target));
/*
* Error.
*/
n = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
RETERR(str_totext(" ", target));
else {
snprintf(buf, sizeof(buf), "%lu ", n);
RETERR(str_totext(buf, target));
}
/*
* Key Size.
*/
n = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
snprintf(buf, sizeof(buf), "%lu", n);
RETERR(str_totext(buf, target));
/*
* Key Data.
*/
REQUIRE(n <= sr.length);
dr = sr;
dr.length = n;
if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
RETERR(str_totext(" (", target));
RETERR(str_totext(tctx->linebreak, target));
if (tctx->width == 0) /* No splitting */
RETERR(isc_base64_totext(&dr, 60, "", target));
else
RETERR(isc_base64_totext(&dr, tctx->width - 2,
tctx->linebreak, target));
if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
RETERR(str_totext(" ) ", target));
else
RETERR(str_totext(" ", target));
isc_region_consume(&sr, n);
/*
* Other Size.
*/
n = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
snprintf(buf, sizeof(buf), "%lu", n);
RETERR(str_totext(buf, target));
/*
* Other Data.
*/
REQUIRE(n <= sr.length);
if (n != 0U) {
dr = sr;
dr.length = n;
if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
RETERR(str_totext(" (", target));
RETERR(str_totext(tctx->linebreak, target));
if (tctx->width == 0) /* No splitting */
RETERR(isc_base64_totext(&dr, 60, "", target));
else
RETERR(isc_base64_totext(&dr, tctx->width - 2,
tctx->linebreak, target));
if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
RETERR(str_totext(" )", target));
}
return (ISC_R_SUCCESS);
}
static inline isc_result_t
fromwire_tkey(ARGS_FROMWIRE) {
isc_region_t sr;
unsigned long n;
dns_name_t name;
REQUIRE(type == dns_rdatatype_tkey);
UNUSED(type);
UNUSED(rdclass);
dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
/*
* Algorithm.
*/
dns_name_init(&name, NULL);
RETERR(dns_name_fromwire(&name, source, dctx, options, target));
/*
* Inception: 4
* Expiration: 4
* Mode: 2
* Error: 2
*/
isc_buffer_activeregion(source, &sr);
if (sr.length < 12)
return (ISC_R_UNEXPECTEDEND);
RETERR(mem_tobuffer(target, sr.base, 12));
isc_region_consume(&sr, 12);
isc_buffer_forward(source, 12);
/*
* Key Length + Key Data.
*/
if (sr.length < 2)
return (ISC_R_UNEXPECTEDEND);
n = uint16_fromregion(&sr);
if (sr.length < n + 2)
return (ISC_R_UNEXPECTEDEND);
RETERR(mem_tobuffer(target, sr.base, n + 2));
isc_region_consume(&sr, n + 2);
isc_buffer_forward(source, n + 2);
/*
* Other Length + Other Data.
*/
if (sr.length < 2)
return (ISC_R_UNEXPECTEDEND);
n = uint16_fromregion(&sr);
if (sr.length < n + 2)
return (ISC_R_UNEXPECTEDEND);
isc_buffer_forward(source, n + 2);
return (mem_tobuffer(target, sr.base, n + 2));
}
static inline isc_result_t
towire_tkey(ARGS_TOWIRE) {
isc_region_t sr;
dns_name_t name;
dns_offsets_t offsets;
REQUIRE(rdata->type == dns_rdatatype_tkey);
REQUIRE(rdata->length != 0);
dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
/*
* Algorithm.
*/
dns_rdata_toregion(rdata, &sr);
dns_name_init(&name, offsets);
dns_name_fromregion(&name, &sr);
RETERR(dns_name_towire(&name, cctx, target));
isc_region_consume(&sr, name_length(&name));
return (mem_tobuffer(target, sr.base, sr.length));
}
static inline isc_result_t
fromstruct_tkey(ARGS_FROMSTRUCT) {
dns_rdata_tkey_t *tkey = source;
REQUIRE(type == dns_rdatatype_tkey);
REQUIRE(source != NULL);
REQUIRE(tkey->common.rdtype == type);
REQUIRE(tkey->common.rdclass == rdclass);
UNUSED(type);
UNUSED(rdclass);
/*
* Algorithm Name.
*/
RETERR(name_tobuffer(&tkey->algorithm, target));
/*
* Inception: 32 bits.
*/
RETERR(uint32_tobuffer(tkey->inception, target));
/*
* Expire: 32 bits.
*/
RETERR(uint32_tobuffer(tkey->expire, target));
/*
* Mode: 16 bits.
*/
RETERR(uint16_tobuffer(tkey->mode, target));
/*
* Error: 16 bits.
*/
RETERR(uint16_tobuffer(tkey->error, target));
/*
* Key size: 16 bits.
*/
RETERR(uint16_tobuffer(tkey->keylen, target));
/*
* Key.
*/
RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
/*
* Other size: 16 bits.
*/
RETERR(uint16_tobuffer(tkey->otherlen, target));
/*
* Other data.
*/
return (mem_tobuffer(target, tkey->other, tkey->otherlen));
}
static inline isc_result_t
tostruct_tkey(ARGS_TOSTRUCT) {
dns_rdata_tkey_t *tkey = target;
dns_name_t alg;
isc_region_t sr;
REQUIRE(rdata->type == dns_rdatatype_tkey);
REQUIRE(target != NULL);
REQUIRE(rdata->length != 0);
tkey->common.rdclass = rdata->rdclass;
tkey->common.rdtype = rdata->type;
ISC_LINK_INIT(&tkey->common, link);
dns_rdata_toregion(rdata, &sr);
/*
* Algorithm Name.
*/
dns_name_init(&alg, NULL);
dns_name_fromregion(&alg, &sr);
dns_name_init(&tkey->algorithm, NULL);
RETERR(name_duporclone(&alg, &tkey->algorithm));
isc_region_consume(&sr, name_length(&tkey->algorithm));
/*
* Inception.
*/
tkey->inception = uint32_fromregion(&sr);
isc_region_consume(&sr, 4);
/*
* Expire.
*/
tkey->expire = uint32_fromregion(&sr);
isc_region_consume(&sr, 4);
/*
* Mode.
*/
tkey->mode = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
/*
* Error.
*/
tkey->error = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
/*
* Key size.
*/
tkey->keylen = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
/*
* Key.
*/
INSIST(tkey->keylen + 2U <= sr.length);
tkey->key = mem_maybedup(sr.base, tkey->keylen);
if (tkey->key == NULL)
goto cleanup;
isc_region_consume(&sr, tkey->keylen);
/*
* Other size.
*/
tkey->otherlen = uint16_fromregion(&sr);
isc_region_consume(&sr, 2);
/*
* Other.
*/
INSIST(tkey->otherlen <= sr.length);
tkey->other = mem_maybedup(sr.base, tkey->otherlen);
if (tkey->other == NULL)
goto cleanup;
return (ISC_R_SUCCESS);
cleanup:
dns_name_free(&tkey->algorithm);
if (tkey->key != NULL)
free(tkey->key);
return (ISC_R_NOMEMORY);
}
#endif /* RDATA_GENERIC_TKEY_249_C */
|