tlsa_52.c 7.04 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11
 */

Evan Hunt's avatar
Evan Hunt committed
12
/* rfc6698.txt */
13 14 15 16 17 18 19

#ifndef RDATA_GENERIC_TLSA_52_C
#define RDATA_GENERIC_TLSA_52_C

#define RRTYPE_TLSA_ATTRIBUTES 0

static inline isc_result_t
20
generic_fromtext_tlsa(ARGS_FROMTEXT) {
21 22 23 24 25 26 27 28 29 30 31 32
	isc_token_t token;

	UNUSED(type);
	UNUSED(rdclass);
	UNUSED(origin);
	UNUSED(options);
	UNUSED(callbacks);

	/*
	 * Certificate Usage.
	 */
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
33
				      false));
34 35 36 37 38 39 40 41
	if (token.value.as_ulong > 0xffU)
		RETTOK(ISC_R_RANGE);
	RETERR(uint8_tobuffer(token.value.as_ulong, target));

	/*
	 * Selector.
	 */
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
42
				      false));
43 44 45 46 47 48 49 50
	if (token.value.as_ulong > 0xffU)
		RETTOK(ISC_R_RANGE);
	RETERR(uint8_tobuffer(token.value.as_ulong, target));

	/*
	 * Matching type.
	 */
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
51
				      false));
52 53 54 55 56 57 58
	if (token.value.as_ulong > 0xffU)
		RETTOK(ISC_R_RANGE);
	RETERR(uint8_tobuffer(token.value.as_ulong, target));

	/*
	 * Certificate Association Data.
	 */
59
	return (isc_hex_tobuffer(lexer, target, -2));
60 61 62
}

static inline isc_result_t
63
generic_totext_tlsa(ARGS_TOTEXT) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	isc_region_t sr;
	char buf[sizeof("64000 ")];
	unsigned int n;

	REQUIRE(rdata->length != 0);

	UNUSED(tctx);

	dns_rdata_toregion(rdata, &sr);

	/*
	 * Certificate Usage.
	 */
	n = uint8_fromregion(&sr);
	isc_region_consume(&sr, 1);
79
	snprintf(buf, sizeof(buf), "%u ", n);
80 81 82 83 84 85 86
	RETERR(str_totext(buf, target));

	/*
	 * Selector.
	 */
	n = uint8_fromregion(&sr);
	isc_region_consume(&sr, 1);
87
	snprintf(buf, sizeof(buf), "%u ", n);
88 89 90 91 92 93 94
	RETERR(str_totext(buf, target));

	/*
	 * Matching type.
	 */
	n = uint8_fromregion(&sr);
	isc_region_consume(&sr, 1);
95
	snprintf(buf, sizeof(buf), "%u", n);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	RETERR(str_totext(buf, target));

	/*
	 * Certificate Association Data.
	 */
	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_hex_totext(&sr, 0, "", target));
	else
		RETERR(isc_hex_totext(&sr, 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
115
generic_fromwire_tlsa(ARGS_FROMWIRE) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	isc_region_t sr;

	UNUSED(type);
	UNUSED(rdclass);
	UNUSED(dctx);
	UNUSED(options);

	isc_buffer_activeregion(source, &sr);

	if (sr.length < 3)
		return (ISC_R_UNEXPECTEDEND);

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

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
static inline isc_result_t
fromtext_tlsa(ARGS_FROMTEXT) {

	REQUIRE(type == dns_rdatatype_tlsa);

	return (generic_fromtext_tlsa(rdclass, type, lexer, origin, options,
				      target, callbacks));
}

static inline isc_result_t
totext_tlsa(ARGS_TOTEXT) {

	REQUIRE(rdata->type == dns_rdatatype_tlsa);

	return (generic_totext_tlsa(rdata, tctx, target));
}

static inline isc_result_t
fromwire_tlsa(ARGS_FROMWIRE) {

	REQUIRE(type == dns_rdatatype_tlsa);

	return (generic_fromwire_tlsa(rdclass, type, source, dctx, options,
				      target));
}

158 159 160 161
static inline isc_result_t
towire_tlsa(ARGS_TOWIRE) {
	isc_region_t sr;

162
	REQUIRE(rdata->type == dns_rdatatype_tlsa);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	REQUIRE(rdata->length != 0);

	UNUSED(cctx);

	dns_rdata_toregion(rdata, &sr);
	return (mem_tobuffer(target, sr.base, sr.length));
}

static inline int
compare_tlsa(ARGS_COMPARE) {
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
178
	REQUIRE(rdata1->type == dns_rdatatype_tlsa);
179 180 181 182 183 184 185 186 187
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);

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

static inline isc_result_t
188
generic_fromstruct_tlsa(ARGS_FROMSTRUCT) {
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	dns_rdata_tlsa_t *tlsa = source;

	REQUIRE(source != NULL);
	REQUIRE(tlsa->common.rdtype == type);
	REQUIRE(tlsa->common.rdclass == rdclass);

	UNUSED(type);
	UNUSED(rdclass);

	RETERR(uint8_tobuffer(tlsa->usage, target));
	RETERR(uint8_tobuffer(tlsa->selector, target));
	RETERR(uint8_tobuffer(tlsa->match, target));

	return (mem_tobuffer(target, tlsa->data, tlsa->length));
}

static inline isc_result_t
206
generic_tostruct_tlsa(ARGS_TOSTRUCT) {
207 208 209
	dns_rdata_tlsa_t *tlsa = target;
	isc_region_t region;

210
	REQUIRE(rdata != NULL);
211 212
	REQUIRE(rdata->length != 0);

213 214 215 216
	REQUIRE(tlsa != NULL);
	REQUIRE(tlsa->common.rdclass == rdata->rdclass);
	REQUIRE(tlsa->common.rdtype == rdata->type);
	REQUIRE(!ISC_LINK_LINKED(&tlsa->common, link));
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	dns_rdata_toregion(rdata, &region);

	tlsa->usage = uint8_fromregion(&region);
	isc_region_consume(&region, 1);
	tlsa->selector = uint8_fromregion(&region);
	isc_region_consume(&region, 1);
	tlsa->match = uint8_fromregion(&region);
	isc_region_consume(&region, 1);
	tlsa->length = region.length;

	tlsa->data = mem_maybedup(mctx, region.base, region.length);
	if (tlsa->data == NULL)
		return (ISC_R_NOMEMORY);

	tlsa->mctx = mctx;
	return (ISC_R_SUCCESS);
}

static inline void
237
generic_freestruct_tlsa(ARGS_FREESTRUCT) {
238 239 240 241 242 243 244 245 246 247 248 249
	dns_rdata_tlsa_t *tlsa = source;

	REQUIRE(tlsa != NULL);

	if (tlsa->mctx == NULL)
		return;

	if (tlsa->data != NULL)
		isc_mem_free(tlsa->mctx, tlsa->data);
	tlsa->mctx = NULL;
}

250 251 252 253 254 255 256 257 258 259
static inline isc_result_t
fromstruct_tlsa(ARGS_FROMSTRUCT) {

	REQUIRE(type == dns_rdatatype_tlsa);

	return (generic_fromstruct_tlsa(rdclass, type, source, target));
}

static inline isc_result_t
tostruct_tlsa(ARGS_TOSTRUCT) {
260
	dns_rdata_tlsa_t *tlsa = target;
261 262 263 264

	REQUIRE(rdata->type == dns_rdatatype_tlsa);
	REQUIRE(target != NULL);

265 266 267
	tlsa->common.rdclass = rdata->rdclass;
	tlsa->common.rdtype = rdata->type;
	ISC_LINK_INIT(&tlsa->common, link);
268 269 270 271 272 273

	return (generic_tostruct_tlsa(rdata, target, mctx));
}

static inline void
freestruct_tlsa(ARGS_FREESTRUCT) {
274
	dns_rdata_tlsa_t *tlsa = source;
275 276

	REQUIRE(source != NULL);
277
	REQUIRE(tlsa->common.rdtype == dns_rdatatype_tlsa);
278 279 280 281

	generic_freestruct_tlsa(source);
}

282 283
static inline isc_result_t
additionaldata_tlsa(ARGS_ADDLDATA) {
284
	REQUIRE(rdata->type == dns_rdatatype_tlsa);
285 286 287 288 289 290 291 292 293 294 295 296

	UNUSED(rdata);
	UNUSED(add);
	UNUSED(arg);

	return (ISC_R_SUCCESS);
}

static inline isc_result_t
digest_tlsa(ARGS_DIGEST) {
	isc_region_t r;

297
	REQUIRE(rdata->type == dns_rdatatype_tlsa);
298 299 300 301 302 303

	dns_rdata_toregion(rdata, &r);

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

304
static inline bool
305 306
checkowner_tlsa(ARGS_CHECKOWNER) {

307
	REQUIRE(type == dns_rdatatype_tlsa);
308 309 310 311 312 313

	UNUSED(name);
	UNUSED(type);
	UNUSED(rdclass);
	UNUSED(wildcard);

314
	return (true);
315 316
}

317
static inline bool
318 319
checknames_tlsa(ARGS_CHECKNAMES) {

320
	REQUIRE(rdata->type == dns_rdatatype_tlsa);
321 322 323 324 325

	UNUSED(rdata);
	UNUSED(owner);
	UNUSED(bad);

326
	return (true);
327 328 329 330 331 332 333 334
}

static inline int
casecompare_tlsa(ARGS_COMPARE) {
	return (compare_tlsa(rdata1, rdata2));
}

#endif	/* RDATA_GENERIC_TLSA_52_C */