1/*
2 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2013  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id$ */
19
20/*
21 * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
22 */
23
24/* RFC2535 */
25
26#ifndef RDATA_GENERIC_KEY_25_C
27#define RDATA_GENERIC_KEY_25_C
28
29#include <dst/dst.h>
30
31#define RRTYPE_KEY_ATTRIBUTES (0)
32
33static inline isc_result_t
34fromtext_key(ARGS_FROMTEXT) {
35	isc_result_t result;
36	isc_token_t token;
37	dns_secalg_t alg;
38	dns_secproto_t proto;
39	dns_keyflags_t flags;
40
41	REQUIRE(type == 25);
42
43	UNUSED(type);
44	UNUSED(rdclass);
45	UNUSED(origin);
46	UNUSED(options);
47	UNUSED(callbacks);
48
49	/* flags */
50	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
51				      ISC_FALSE));
52	RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
53	RETERR(uint16_tobuffer(flags, target));
54
55	/* protocol */
56	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
57				      ISC_FALSE));
58	RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
59	RETERR(mem_tobuffer(target, &proto, 1));
60
61	/* algorithm */
62	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
63				      ISC_FALSE));
64	RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
65	RETERR(mem_tobuffer(target, &alg, 1));
66
67	/* No Key? */
68	if ((flags & 0xc000) == 0xc000)
69		return (ISC_R_SUCCESS);
70
71	result = isc_base64_tobuffer(lexer, target, -1);
72	if (result != ISC_R_SUCCESS)
73		return (result);
74
75	/* Ensure there's at least enough data to compute a key ID for MD5 */
76	if (alg == DST_ALG_RSAMD5 && isc_buffer_usedlength(target) < 7)
77		return (ISC_R_UNEXPECTEDEND);
78
79	return (ISC_R_SUCCESS);
80}
81
82static inline isc_result_t
83totext_key(ARGS_TOTEXT) {
84	isc_region_t sr;
85	char buf[sizeof("64000")];
86	unsigned int flags;
87	unsigned char algorithm;
88	char namebuf[DNS_NAME_FORMATSIZE];
89
90	REQUIRE(rdata->type == 25);
91	REQUIRE(rdata->length != 0);
92
93	dns_rdata_toregion(rdata, &sr);
94
95	/* flags */
96	flags = uint16_fromregion(&sr);
97	isc_region_consume(&sr, 2);
98	sprintf(buf, "%u", flags);
99	RETERR(str_totext(buf, target));
100	RETERR(str_totext(" ", target));
101
102	/* protocol */
103	sprintf(buf, "%u", sr.base[0]);
104	isc_region_consume(&sr, 1);
105	RETERR(str_totext(buf, target));
106	RETERR(str_totext(" ", target));
107
108	/* algorithm */
109	algorithm = sr.base[0];
110	sprintf(buf, "%u", algorithm);
111	isc_region_consume(&sr, 1);
112	RETERR(str_totext(buf, target));
113
114	/* No Key? */
115	if ((flags & 0xc000) == 0xc000)
116		return (ISC_R_SUCCESS);
117
118	if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0 &&
119	     algorithm == DNS_KEYALG_PRIVATEDNS) {
120		dns_name_t name;
121		dns_name_init(&name, NULL);
122		dns_name_fromregion(&name, &sr);
123		dns_name_format(&name, namebuf, sizeof(namebuf));
124	} else
125		namebuf[0] = 0;
126
127	/* key */
128	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
129		RETERR(str_totext(" (", target));
130	RETERR(str_totext(tctx->linebreak, target));
131	if (tctx->width == 0)   /* No splitting */
132		RETERR(isc_base64_totext(&sr, 60, "", target));
133	else
134		RETERR(isc_base64_totext(&sr, tctx->width - 2,
135					 tctx->linebreak, target));
136
137	if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0)
138		RETERR(str_totext(tctx->linebreak, target));
139	else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
140		RETERR(str_totext(" ", target));
141
142	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
143		RETERR(str_totext(")", target));
144
145	if ((tctx->flags & DNS_STYLEFLAG_RRCOMMENT) != 0) {
146		isc_region_t tmpr;
147
148		RETERR(str_totext(" ; key id = ", target));
149		dns_rdata_toregion(rdata, &tmpr);
150		sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
151		RETERR(str_totext(buf, target));
152		if (algorithm == DNS_KEYALG_PRIVATEDNS) {
153			RETERR(str_totext(tctx->linebreak, target));
154			RETERR(str_totext("; alg = ", target));
155			RETERR(str_totext(namebuf, target));
156		}
157	}
158	return (ISC_R_SUCCESS);
159}
160
161static inline isc_result_t
162fromwire_key(ARGS_FROMWIRE) {
163	unsigned char algorithm;
164	isc_region_t sr;
165
166	REQUIRE(type == 25);
167
168	UNUSED(type);
169	UNUSED(rdclass);
170	UNUSED(dctx);
171	UNUSED(options);
172
173	isc_buffer_activeregion(source, &sr);
174	if (sr.length < 4)
175		return (ISC_R_UNEXPECTEDEND);
176
177	algorithm = sr.base[3];
178	RETERR(mem_tobuffer(target, sr.base, 4));
179	isc_region_consume(&sr, 4);
180	isc_buffer_forward(source, 4);
181
182	if (algorithm == DNS_KEYALG_PRIVATEDNS) {
183		dns_name_t name;
184		dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
185		dns_name_init(&name, NULL);
186		RETERR(dns_name_fromwire(&name, source, dctx, options, target));
187	}
188
189	/*
190	 * RSAMD5 computes key ID differently from other
191	 * algorithms: we need to ensure there's enough data
192	 * present for the computation
193	 */
194	if (algorithm == DST_ALG_RSAMD5 && sr.length < 3)
195		return (ISC_R_UNEXPECTEDEND);
196
197	isc_buffer_activeregion(source, &sr);
198	isc_buffer_forward(source, sr.length);
199	return (mem_tobuffer(target, sr.base, sr.length));
200}
201
202static inline isc_result_t
203towire_key(ARGS_TOWIRE) {
204	isc_region_t sr;
205
206	REQUIRE(rdata->type == 25);
207	REQUIRE(rdata->length != 0);
208
209	UNUSED(cctx);
210
211	dns_rdata_toregion(rdata, &sr);
212	return (mem_tobuffer(target, sr.base, sr.length));
213}
214
215static inline int
216compare_key(ARGS_COMPARE) {
217	isc_region_t r1;
218	isc_region_t r2;
219
220	REQUIRE(rdata1->type == rdata2->type);
221	REQUIRE(rdata1->rdclass == rdata2->rdclass);
222	REQUIRE(rdata1->type == 25);
223	REQUIRE(rdata1->length != 0);
224	REQUIRE(rdata2->length != 0);
225
226	dns_rdata_toregion(rdata1, &r1);
227	dns_rdata_toregion(rdata2, &r2);
228	return (isc_region_compare(&r1, &r2));
229}
230
231static inline isc_result_t
232fromstruct_key(ARGS_FROMSTRUCT) {
233	dns_rdata_key_t *key = source;
234
235	REQUIRE(type == 25);
236	REQUIRE(source != NULL);
237	REQUIRE(key->common.rdtype == type);
238	REQUIRE(key->common.rdclass == rdclass);
239
240	UNUSED(type);
241	UNUSED(rdclass);
242
243	/* Flags */
244	RETERR(uint16_tobuffer(key->flags, target));
245
246	/* Protocol */
247	RETERR(uint8_tobuffer(key->protocol, target));
248
249	/* Algorithm */
250	RETERR(uint8_tobuffer(key->algorithm, target));
251
252	/* Data */
253	return (mem_tobuffer(target, key->data, key->datalen));
254}
255
256static inline isc_result_t
257tostruct_key(ARGS_TOSTRUCT) {
258	dns_rdata_key_t *key = target;
259	isc_region_t sr;
260
261	REQUIRE(rdata->type == 25);
262	REQUIRE(target != NULL);
263	REQUIRE(rdata->length != 0);
264
265	key->common.rdclass = rdata->rdclass;
266	key->common.rdtype = rdata->type;
267	ISC_LINK_INIT(&key->common, link);
268
269	dns_rdata_toregion(rdata, &sr);
270
271	/* Flags */
272	if (sr.length < 2)
273		return (ISC_R_UNEXPECTEDEND);
274	key->flags = uint16_fromregion(&sr);
275	isc_region_consume(&sr, 2);
276
277	/* Protocol */
278	if (sr.length < 1)
279		return (ISC_R_UNEXPECTEDEND);
280	key->protocol = uint8_fromregion(&sr);
281	isc_region_consume(&sr, 1);
282
283	/* Algorithm */
284	if (sr.length < 1)
285		return (ISC_R_UNEXPECTEDEND);
286	key->algorithm = uint8_fromregion(&sr);
287	isc_region_consume(&sr, 1);
288
289	/* Data */
290	key->datalen = sr.length;
291	key->data = mem_maybedup(mctx, sr.base, key->datalen);
292	if (key->data == NULL)
293		return (ISC_R_NOMEMORY);
294
295	key->mctx = mctx;
296	return (ISC_R_SUCCESS);
297}
298
299static inline void
300freestruct_key(ARGS_FREESTRUCT) {
301	dns_rdata_key_t *key = (dns_rdata_key_t *) source;
302
303	REQUIRE(source != NULL);
304	REQUIRE(key->common.rdtype == 25);
305
306	if (key->mctx == NULL)
307		return;
308
309	if (key->data != NULL)
310		isc_mem_free(key->mctx, key->data);
311	key->mctx = NULL;
312}
313
314static inline isc_result_t
315additionaldata_key(ARGS_ADDLDATA) {
316	REQUIRE(rdata->type == 25);
317
318	UNUSED(rdata);
319	UNUSED(add);
320	UNUSED(arg);
321
322	return (ISC_R_SUCCESS);
323}
324
325static inline isc_result_t
326digest_key(ARGS_DIGEST) {
327	isc_region_t r;
328
329	REQUIRE(rdata->type == 25);
330
331	dns_rdata_toregion(rdata, &r);
332
333	return ((digest)(arg, &r));
334}
335
336static inline isc_boolean_t
337checkowner_key(ARGS_CHECKOWNER) {
338
339	REQUIRE(type == 25);
340
341	UNUSED(name);
342	UNUSED(type);
343	UNUSED(rdclass);
344	UNUSED(wildcard);
345
346	return (ISC_TRUE);
347}
348
349static inline isc_boolean_t
350checknames_key(ARGS_CHECKNAMES) {
351
352	REQUIRE(rdata->type == 25);
353
354	UNUSED(rdata);
355	UNUSED(owner);
356	UNUSED(bad);
357
358	return (ISC_TRUE);
359}
360
361static inline int
362casecompare_key(ARGS_COMPARE) {
363	return (compare_key(rdata1, rdata2));
364}
365
366#endif	/* RDATA_GENERIC_KEY_25_C */
367