key_25.c 6.43 KB
Newer Older
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1999, 2000  Internet Software Consortium.
3
 *
4 5 6
 * Permission to use, copy, modify, and 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.
7
 *
8 9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM 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.
16 17
 */

18
/* $Id: key_25.c,v 1.35 2000/12/01 01:40:23 gson Exp $ */
19

Bob Halley's avatar
Bob Halley committed
20 21 22 23 24
/*
 * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
 */

/* RFC 2535 */
25

26 27
#ifndef RDATA_GENERIC_KEY_25_C
#define RDATA_GENERIC_KEY_25_C
28

29 30
#include <dst/dst.h>

31 32
#define RRTYPE_KEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)

33
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
34
fromtext_key(ARGS_FROMTEXT) {
35
	isc_token_t token;
36 37 38
	dns_secalg_t alg;
	dns_secproto_t proto;
	dns_keyflags_t flags;
39

40 41
	REQUIRE(type == 25);

Bob Halley's avatar
Bob Halley committed
42 43 44
	UNUSED(rdclass);
	UNUSED(origin);
	UNUSED(downcase);
45

46
	/* flags */
47 48
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
49 50
	RETERR(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
	RETERR(uint16_tobuffer(flags, target));
51

52
	/* protocol */
53 54
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
55 56
	RETERR(dns_secproto_fromtext(&proto, &token.value.as_textregion));
	RETERR(mem_tobuffer(target, &proto, 1));
57

58
	/* algorithm */
59 60
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
61 62
	RETERR(dns_secalg_fromtext(&alg, &token.value.as_textregion));
	RETERR(mem_tobuffer(target, &alg, 1));
63

64 65
	/* No Key? */
	if ((flags & 0xc000) == 0xc000)
66
		return (ISC_R_SUCCESS);
67

68
	return (isc_base64_tobuffer(lexer, target, -1));
69 70
}

71
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
72
totext_key(ARGS_TOTEXT) {
73 74 75
	isc_region_t sr;
	char buf[sizeof "64000"];
	unsigned int flags;
76
	unsigned char algorithm;
77

Bob Halley's avatar
Bob Halley committed
78
	REQUIRE(rdata->type == 25);
79
	REQUIRE(rdata->length != 0);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	dns_rdata_toregion(rdata, &sr);

	/* flags */
	flags = uint16_fromregion(&sr);
	isc_region_consume(&sr, 2);
	sprintf(buf, "%u", flags);
	RETERR(str_totext(buf, target));
	RETERR(str_totext(" ", target));

	/* protocol */
	sprintf(buf, "%u", sr.base[0]);
	isc_region_consume(&sr, 1);
	RETERR(str_totext(buf, target));
	RETERR(str_totext(" ", target));

	/* algorithm */
97 98
	algorithm = sr.base[0];
	sprintf(buf, "%u", algorithm);
99 100 101 102
	isc_region_consume(&sr, 1);
	RETERR(str_totext(buf, target));

	/* No Key? */
103
	if ((flags & 0xc000) == 0xc000)
104
		return (ISC_R_SUCCESS);
105 106

	/* key */
107 108
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" (", target));
109 110 111
	RETERR(str_totext(tctx->linebreak, target));
	RETERR(isc_base64_totext(&sr, tctx->width - 2,
				 tctx->linebreak, target));
112 113
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" )", target));
114

115 116 117 118 119
	if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
		isc_region_t tmpr;

		RETERR(str_totext(" ; key id = ", target));
		dns_rdata_toregion(rdata, &tmpr);
120
		sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
121 122 123
		RETERR(str_totext(buf, target));
	}
	return (ISC_R_SUCCESS);
124 125
}

126
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
127
fromwire_key(ARGS_FROMWIRE) {
128 129
	isc_region_t sr;

Bob Halley's avatar
Bob Halley committed
130 131 132 133
	UNUSED(rdclass);
	UNUSED(dctx);
	UNUSED(downcase);

134 135
	REQUIRE(type == 25);

136
	isc_buffer_activeregion(source, &sr);
137
	if (sr.length < 4)
138
		return (ISC_R_UNEXPECTEDEND);
139 140 141 142 143

	isc_buffer_forward(source, sr.length);
	return (mem_tobuffer(target, sr.base, sr.length));
}

144
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
145
towire_key(ARGS_TOWIRE) {
146 147
	isc_region_t sr;

Bob Halley's avatar
Bob Halley committed
148
	REQUIRE(rdata->type == 25);
149
	REQUIRE(rdata->length != 0);
150

151 152
	UNUSED(cctx);

153 154 155 156
	dns_rdata_toregion(rdata, &sr);
	return (mem_tobuffer(target, sr.base, sr.length));
}

157
static inline int
David Lawrence's avatar
David Lawrence committed
158
compare_key(ARGS_COMPARE) {
159 160 161 162
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
163
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
164
	REQUIRE(rdata1->type == 25);
165 166
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
167 168 169 170 171 172

	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
	return (compare_region(&r1, &r2));
}

173
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
174
fromstruct_key(ARGS_FROMSTRUCT) {
175
	dns_rdata_key_t *key = source;
Bob Halley's avatar
Bob Halley committed
176

177
	REQUIRE(type == 25);
178 179 180
	REQUIRE(source != NULL);
	REQUIRE(key->common.rdtype == type);
	REQUIRE(key->common.rdclass == rdclass);
181

182 183
	UNUSED(rdclass);

184 185 186 187 188 189 190 191 192 193
	/* Flags */
	RETERR(uint16_tobuffer(key->flags, target));

	/* Protocol */
	RETERR(uint8_tobuffer(key->protocol, target));

	/* Algorithm */
	RETERR(uint8_tobuffer(key->algorithm, target));

	/* Data */
194
	return (mem_tobuffer(target, key->data, key->datalen));
195 196
}

197
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
198
tostruct_key(ARGS_TOSTRUCT) {
199
	dns_rdata_key_t *key = target;
200
	isc_region_t sr;
201 202

	REQUIRE(rdata->type == 25);
203
	REQUIRE(target != NULL);
204
	REQUIRE(rdata->length != 0);
205

206 207 208
	key->common.rdclass = rdata->rdclass;
	key->common.rdtype = rdata->type;
	ISC_LINK_INIT(&key->common, link);
209

210 211 212 213 214 215 216 217 218 219 220
	dns_rdata_toregion(rdata, &sr);

	/* Flags */
	if (sr.length < 2)
		return (ISC_R_UNEXPECTEDEND);
	key->flags = uint16_fromregion(&sr);
	isc_region_consume(&sr, 2);

	/* Protocol */
	if (sr.length < 1)
		return (ISC_R_UNEXPECTEDEND);
Brian Wellington's avatar
Brian Wellington committed
221
	key->protocol = uint8_fromregion(&sr);
222 223 224 225 226
	isc_region_consume(&sr, 1);

	/* Algorithm */
	if (sr.length < 1)
		return (ISC_R_UNEXPECTEDEND);
Brian Wellington's avatar
Brian Wellington committed
227
	key->algorithm = uint8_fromregion(&sr);
228 229 230 231 232
	isc_region_consume(&sr, 1);

	/* Data */
	key->datalen = sr.length;
	if (key->datalen > 0) {
233
		key->data = mem_maybedup(mctx, sr.base, key->datalen);
234
		if (key->data == NULL)
235
			return (ISC_R_NOMEMORY);
236
	} else
237 238
		key->data = NULL;

239
	key->mctx = mctx;
240
	return (ISC_R_SUCCESS);
241
}
242

243
static inline void
David Lawrence's avatar
David Lawrence committed
244
freestruct_key(ARGS_FREESTRUCT) {
245
	dns_rdata_key_t *key = (dns_rdata_key_t *) source;
246

247
	REQUIRE(source != NULL);
248 249
	REQUIRE(key->common.rdtype == 25);

250 251 252 253 254 255
	if (key->mctx == NULL)
		return;

	if (key->data != NULL)
		isc_mem_free(key->mctx, key->data);
	key->mctx = NULL;
256
}
257

258
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
259
additionaldata_key(ARGS_ADDLDATA) {
260 261
	REQUIRE(rdata->type == 25);

262
	UNUSED(rdata);
Bob Halley's avatar
Bob Halley committed
263 264
	UNUSED(add);
	UNUSED(arg);
265

266
	return (ISC_R_SUCCESS);
267 268
}

269
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
270
digest_key(ARGS_DIGEST) {
Bob Halley's avatar
Bob Halley committed
271 272 273 274 275 276 277 278 279
	isc_region_t r;

	REQUIRE(rdata->type == 25);

	dns_rdata_toregion(rdata, &r);

	return ((digest)(arg, &r));
}

280
#endif	/* RDATA_GENERIC_KEY_25_C */