]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/rdata/generic/keydata_65533.c
Update BIND to 9.9.7.
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / rdata / generic / keydata_65533.c
1 /*
2  * Copyright (C) 2009, 2011-2013, 2015  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 #ifndef GENERIC_KEYDATA_65533_C
18 #define GENERIC_KEYDATA_65533_C 1
19
20 #include <isc/time.h>
21 #include <isc/stdtime.h>
22
23 #include <dst/dst.h>
24
25 #define RRTYPE_KEYDATA_ATTRIBUTES (0)
26
27 static inline isc_result_t
28 fromtext_keydata(ARGS_FROMTEXT) {
29         isc_result_t result;
30         isc_token_t token;
31         dns_secalg_t alg;
32         dns_secproto_t proto;
33         dns_keyflags_t flags;
34         isc_uint32_t refresh, addhd, removehd;
35
36         REQUIRE(type == 65533);
37
38         UNUSED(type);
39         UNUSED(rdclass);
40         UNUSED(origin);
41         UNUSED(options);
42         UNUSED(callbacks);
43
44         /* refresh timer */
45         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
46                                       ISC_FALSE));
47         RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &refresh));
48         RETERR(uint32_tobuffer(refresh, target));
49
50         /* add hold-down */
51         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
52                                       ISC_FALSE));
53         RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &addhd));
54         RETERR(uint32_tobuffer(addhd, target));
55
56         /* remove hold-down */
57         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
58                                       ISC_FALSE));
59         RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &removehd));
60         RETERR(uint32_tobuffer(removehd, target));
61
62         /* flags */
63         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
64                                       ISC_FALSE));
65         RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
66         RETERR(uint16_tobuffer(flags, target));
67
68         /* protocol */
69         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
70                                       ISC_FALSE));
71         RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
72         RETERR(mem_tobuffer(target, &proto, 1));
73
74         /* algorithm */
75         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
76                                       ISC_FALSE));
77         RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
78         RETERR(mem_tobuffer(target, &alg, 1));
79
80         /* No Key? */
81         if ((flags & 0xc000) == 0xc000)
82                 return (ISC_R_SUCCESS);
83
84         result = isc_base64_tobuffer(lexer, target, -1);
85         if (result != ISC_R_SUCCESS)
86                 return (result);
87
88         /* Ensure there's at least enough data to compute a key ID for MD5 */
89         if (alg == DST_ALG_RSAMD5 && isc_buffer_usedlength(target) < 19)
90                 return (ISC_R_UNEXPECTEDEND);
91
92         return (ISC_R_SUCCESS);
93 }
94
95 static inline isc_result_t
96 totext_keydata(ARGS_TOTEXT) {
97         isc_region_t sr;
98         char buf[sizeof("64000")];
99         unsigned int flags;
100         unsigned char algorithm;
101         unsigned long refresh, add, remove;
102         char algbuf[DNS_NAME_FORMATSIZE];
103         const char *keyinfo;
104
105         REQUIRE(rdata->type == 65533);
106
107         if ((tctx->flags & DNS_STYLEFLAG_KEYDATA) == 0 || rdata->length < 16)
108                 return (unknown_totext(rdata, tctx, target));
109
110         dns_rdata_toregion(rdata, &sr);
111
112         /* refresh timer */
113         refresh = uint32_fromregion(&sr);
114         isc_region_consume(&sr, 4);
115         RETERR(dns_time32_totext(refresh, target));
116         RETERR(str_totext(" ", target));
117
118         /* add hold-down */
119         add = uint32_fromregion(&sr);
120         isc_region_consume(&sr, 4);
121         RETERR(dns_time32_totext(add, target));
122         RETERR(str_totext(" ", target));
123
124         /* remove hold-down */
125         remove = uint32_fromregion(&sr);
126         isc_region_consume(&sr, 4);
127         RETERR(dns_time32_totext(remove, target));
128         RETERR(str_totext(" ", target));
129
130         /* flags */
131         flags = uint16_fromregion(&sr);
132         isc_region_consume(&sr, 2);
133         sprintf(buf, "%u", flags);
134         RETERR(str_totext(buf, target));
135         RETERR(str_totext(" ", target));
136         if ((flags & DNS_KEYFLAG_KSK) != 0) {
137                 if (flags & DNS_KEYFLAG_REVOKE)
138                         keyinfo = "revoked KSK";
139                 else
140                         keyinfo = "KSK";
141         } else
142                 keyinfo = "ZSK";
143
144         /* protocol */
145         sprintf(buf, "%u", sr.base[0]);
146         isc_region_consume(&sr, 1);
147         RETERR(str_totext(buf, target));
148         RETERR(str_totext(" ", target));
149
150         /* algorithm */
151         algorithm = sr.base[0];
152         sprintf(buf, "%u", algorithm);
153         isc_region_consume(&sr, 1);
154         RETERR(str_totext(buf, target));
155
156         /* No Key? */
157         if ((flags & 0xc000) == 0xc000)
158                 return (ISC_R_SUCCESS);
159
160         /* key */
161         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
162                 RETERR(str_totext(" (", target));
163         RETERR(str_totext(tctx->linebreak, target));
164         if (tctx->width == 0)   /* No splitting */
165                 RETERR(isc_base64_totext(&sr, 60, "", target));
166         else
167                 RETERR(isc_base64_totext(&sr, tctx->width - 2,
168                                          tctx->linebreak, target));
169
170         if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0)
171                 RETERR(str_totext(tctx->linebreak, target));
172         else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
173                 RETERR(str_totext(" ", target));
174
175         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
176                 RETERR(str_totext(")", target));
177
178         if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0) {
179                 isc_region_t tmpr;
180                 char rbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
181                 char abuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
182                 char dbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
183                 isc_time_t t;
184
185                 RETERR(str_totext(" ; ", target));
186                 RETERR(str_totext(keyinfo, target));
187                 dns_secalg_format((dns_secalg_t) algorithm, algbuf,
188                                   sizeof(algbuf));
189                 RETERR(str_totext("; alg = ", target));
190                 RETERR(str_totext(algbuf, target));
191                 RETERR(str_totext("; key id = ", target));
192                 dns_rdata_toregion(rdata, &tmpr);
193                 /* Skip over refresh, addhd, and removehd */
194                 isc_region_consume(&tmpr, 12);
195                 sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
196                 RETERR(str_totext(buf, target));
197
198                 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
199                         isc_stdtime_t now;
200
201                         isc_stdtime_get(&now);
202
203                         RETERR(str_totext(tctx->linebreak, target));
204                         RETERR(str_totext("; next refresh: ", target));
205                         isc_time_set(&t, refresh, 0);
206                         isc_time_formathttptimestamp(&t, rbuf, sizeof(rbuf));
207                         RETERR(str_totext(rbuf, target));
208
209                         if (add == 0) {
210                                 RETERR(str_totext(tctx->linebreak, target));
211                                 RETERR(str_totext("; no trust", target));
212                         } else {
213                                 RETERR(str_totext(tctx->linebreak, target));
214                                 if (add < now) {
215                                         RETERR(str_totext("; trusted since: ",
216                                                           target));
217                                 } else {
218                                         RETERR(str_totext("; trust pending: ",
219                                                           target));
220                                 }
221                                 isc_time_set(&t, add, 0);
222                                 isc_time_formathttptimestamp(&t, abuf,
223                                                              sizeof(abuf));
224                                 RETERR(str_totext(abuf, target));
225                         }
226
227                         if (remove != 0) {
228                                 RETERR(str_totext(tctx->linebreak, target));
229                                 RETERR(str_totext("; removal pending: ",
230                                                   target));
231                                 isc_time_set(&t, remove, 0);
232                                 isc_time_formathttptimestamp(&t, dbuf,
233                                                              sizeof(dbuf));
234                                 RETERR(str_totext(dbuf, target));
235                         }
236                 }
237
238         }
239         return (ISC_R_SUCCESS);
240 }
241
242 static inline isc_result_t
243 fromwire_keydata(ARGS_FROMWIRE) {
244         isc_region_t sr;
245
246         REQUIRE(type == 65533);
247
248         UNUSED(type);
249         UNUSED(rdclass);
250         UNUSED(dctx);
251         UNUSED(options);
252
253         isc_buffer_activeregion(source, &sr);
254         isc_buffer_forward(source, sr.length);
255         return (mem_tobuffer(target, sr.base, sr.length));
256 }
257
258 static inline isc_result_t
259 towire_keydata(ARGS_TOWIRE) {
260         isc_region_t sr;
261
262         REQUIRE(rdata->type == 65533);
263
264         UNUSED(cctx);
265
266         dns_rdata_toregion(rdata, &sr);
267         return (mem_tobuffer(target, sr.base, sr.length));
268 }
269
270 static inline int
271 compare_keydata(ARGS_COMPARE) {
272         isc_region_t r1;
273         isc_region_t r2;
274
275         REQUIRE(rdata1->type == rdata2->type);
276         REQUIRE(rdata1->rdclass == rdata2->rdclass);
277         REQUIRE(rdata1->type == 65533);
278
279         dns_rdata_toregion(rdata1, &r1);
280         dns_rdata_toregion(rdata2, &r2);
281         return (isc_region_compare(&r1, &r2));
282 }
283
284 static inline isc_result_t
285 fromstruct_keydata(ARGS_FROMSTRUCT) {
286         dns_rdata_keydata_t *keydata = source;
287
288         REQUIRE(type == 65533);
289         REQUIRE(source != NULL);
290         REQUIRE(keydata->common.rdtype == type);
291         REQUIRE(keydata->common.rdclass == rdclass);
292
293         UNUSED(type);
294         UNUSED(rdclass);
295
296         /* Refresh timer */
297         RETERR(uint32_tobuffer(keydata->refresh, target));
298
299         /* Add hold-down */
300         RETERR(uint32_tobuffer(keydata->addhd, target));
301
302         /* Remove hold-down */
303         RETERR(uint32_tobuffer(keydata->removehd, target));
304
305         /* Flags */
306         RETERR(uint16_tobuffer(keydata->flags, target));
307
308         /* Protocol */
309         RETERR(uint8_tobuffer(keydata->protocol, target));
310
311         /* Algorithm */
312         RETERR(uint8_tobuffer(keydata->algorithm, target));
313
314         /* Data */
315         return (mem_tobuffer(target, keydata->data, keydata->datalen));
316 }
317
318 static inline isc_result_t
319 tostruct_keydata(ARGS_TOSTRUCT) {
320         dns_rdata_keydata_t *keydata = target;
321         isc_region_t sr;
322
323         REQUIRE(rdata->type == 65533);
324         REQUIRE(target != NULL);
325
326         keydata->common.rdclass = rdata->rdclass;
327         keydata->common.rdtype = rdata->type;
328         ISC_LINK_INIT(&keydata->common, link);
329
330         dns_rdata_toregion(rdata, &sr);
331
332         /* Refresh timer */
333         if (sr.length < 4)
334                 return (ISC_R_UNEXPECTEDEND);
335         keydata->refresh = uint32_fromregion(&sr);
336         isc_region_consume(&sr, 4);
337
338         /* Add hold-down */
339         if (sr.length < 4)
340                 return (ISC_R_UNEXPECTEDEND);
341         keydata->addhd = uint32_fromregion(&sr);
342         isc_region_consume(&sr, 4);
343
344         /* Remove hold-down */
345         if (sr.length < 4)
346                 return (ISC_R_UNEXPECTEDEND);
347         keydata->removehd = uint32_fromregion(&sr);
348         isc_region_consume(&sr, 4);
349
350         /* Flags */
351         if (sr.length < 2)
352                 return (ISC_R_UNEXPECTEDEND);
353         keydata->flags = uint16_fromregion(&sr);
354         isc_region_consume(&sr, 2);
355
356         /* Protocol */
357         if (sr.length < 1)
358                 return (ISC_R_UNEXPECTEDEND);
359         keydata->protocol = uint8_fromregion(&sr);
360         isc_region_consume(&sr, 1);
361
362         /* Algorithm */
363         if (sr.length < 1)
364                 return (ISC_R_UNEXPECTEDEND);
365         keydata->algorithm = uint8_fromregion(&sr);
366         isc_region_consume(&sr, 1);
367
368         /* Data */
369         keydata->datalen = sr.length;
370         keydata->data = mem_maybedup(mctx, sr.base, keydata->datalen);
371         if (keydata->data == NULL)
372                 return (ISC_R_NOMEMORY);
373
374         keydata->mctx = mctx;
375         return (ISC_R_SUCCESS);
376 }
377
378 static inline void
379 freestruct_keydata(ARGS_FREESTRUCT) {
380         dns_rdata_keydata_t *keydata = (dns_rdata_keydata_t *) source;
381
382         REQUIRE(source != NULL);
383         REQUIRE(keydata->common.rdtype == 65533);
384
385         if (keydata->mctx == NULL)
386                 return;
387
388         if (keydata->data != NULL)
389                 isc_mem_free(keydata->mctx, keydata->data);
390         keydata->mctx = NULL;
391 }
392
393 static inline isc_result_t
394 additionaldata_keydata(ARGS_ADDLDATA) {
395         REQUIRE(rdata->type == 65533);
396
397         UNUSED(rdata);
398         UNUSED(add);
399         UNUSED(arg);
400
401         return (ISC_R_SUCCESS);
402 }
403
404 static inline isc_result_t
405 digest_keydata(ARGS_DIGEST) {
406         isc_region_t r;
407
408         REQUIRE(rdata->type == 65533);
409
410         dns_rdata_toregion(rdata, &r);
411
412         return ((digest)(arg, &r));
413 }
414
415 static inline isc_boolean_t
416 checkowner_keydata(ARGS_CHECKOWNER) {
417
418         REQUIRE(type == 65533);
419
420         UNUSED(name);
421         UNUSED(type);
422         UNUSED(rdclass);
423         UNUSED(wildcard);
424
425         return (ISC_TRUE);
426 }
427
428 static inline isc_boolean_t
429 checknames_keydata(ARGS_CHECKNAMES) {
430
431         REQUIRE(rdata->type == 65533);
432
433         UNUSED(rdata);
434         UNUSED(owner);
435         UNUSED(bad);
436
437         return (ISC_TRUE);
438 }
439
440 static inline int
441 casecompare_keydata(ARGS_COMPARE) {
442         return (compare_keydata(rdata1, rdata2));
443 }
444
445 #endif /* GENERIC_KEYDATA_65533_C */