cert_37.c 5.9 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
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.
Mark Andrews's avatar
Mark Andrews committed
10 11
 */

12
/* RFC2538 */
Mark Andrews's avatar
Mark Andrews committed
13

14 15
#ifndef RDATA_GENERIC_CERT_37_C
#define RDATA_GENERIC_CERT_37_C
Mark Andrews's avatar
Mark Andrews committed
16

17 18
#define RRTYPE_CERT_ATTRIBUTES (0)

19
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
20
fromtext_cert(ARGS_FROMTEXT) {
Mark Andrews's avatar
Mark Andrews committed
21
	isc_token_t token;
Mark Andrews's avatar
Mark Andrews committed
22 23
	dns_secalg_t secalg;
	dns_cert_t cert;
Mark Andrews's avatar
Mark Andrews committed
24

25
	REQUIRE(type == dns_rdatatype_cert);
Mark Andrews's avatar
Mark Andrews committed
26

27
	UNUSED(type);
David Lawrence's avatar
David Lawrence committed
28 29
	UNUSED(rdclass);
	UNUSED(origin);
30
	UNUSED(options);
31
	UNUSED(callbacks);
Mark Andrews's avatar
Mark Andrews committed
32

David Lawrence's avatar
David Lawrence committed
33 34 35
	/*
	 * Cert type.
	 */
36
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
37
				      false));
38
	RETTOK(dns_cert_fromtext(&cert, &token.value.as_textregion));
Mark Andrews's avatar
Mark Andrews committed
39
	RETERR(uint16_tobuffer(cert, target));
40

David Lawrence's avatar
David Lawrence committed
41 42 43
	/*
	 * Key tag.
	 */
44
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
45
				      false));
46
	if (token.value.as_ulong > 0xffffU)
47
		RETTOK(ISC_R_RANGE);
Mark Andrews's avatar
Mark Andrews committed
48 49
	RETERR(uint16_tobuffer(token.value.as_ulong, target));

David Lawrence's avatar
David Lawrence committed
50 51 52
	/*
	 * Algorithm.
	 */
53
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
54
				      false));
55
	RETTOK(dns_secalg_fromtext(&secalg, &token.value.as_textregion));
Mark Andrews's avatar
Mark Andrews committed
56
	RETERR(mem_tobuffer(target, &secalg, 1));
Mark Andrews's avatar
Mark Andrews committed
57

58
	return (isc_base64_tobuffer(lexer, target, -2));
Mark Andrews's avatar
Mark Andrews committed
59 60
}

61
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
62
totext_cert(ARGS_TOTEXT) {
Mark Andrews's avatar
Mark Andrews committed
63
	isc_region_t sr;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
64
	char buf[sizeof("64000 ")];
Mark Andrews's avatar
Mark Andrews committed
65 66
	unsigned int n;

67
	REQUIRE(rdata->type == dns_rdatatype_cert);
68
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
69

David Lawrence's avatar
David Lawrence committed
70
	UNUSED(tctx);
Mark Andrews's avatar
Mark Andrews committed
71 72 73

	dns_rdata_toregion(rdata, &sr);

David Lawrence's avatar
David Lawrence committed
74 75 76
	/*
	 * Type.
	 */
Mark Andrews's avatar
Mark Andrews committed
77 78
	n = uint16_fromregion(&sr);
	isc_region_consume(&sr, 2);
79
	RETERR(dns_cert_totext((dns_cert_t)n, target));
Mark Andrews's avatar
Mark Andrews committed
80 81
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
82 83 84
	/*
	 * Key tag.
	 */
Mark Andrews's avatar
Mark Andrews committed
85 86
	n = uint16_fromregion(&sr);
	isc_region_consume(&sr, 2);
87
	snprintf(buf, sizeof(buf), "%u ", n);
Mark Andrews's avatar
Mark Andrews committed
88 89
	RETERR(str_totext(buf, target));

David Lawrence's avatar
David Lawrence committed
90 91 92
	/*
	 * Algorithm.
	 */
Mark Andrews's avatar
Mark Andrews committed
93 94
	RETERR(dns_secalg_totext(sr.base[0], target));
	isc_region_consume(&sr, 1);
Mark Andrews's avatar
Mark Andrews committed
95

David Lawrence's avatar
David Lawrence committed
96 97 98
	/*
	 * Cert.
	 */
99 100
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" (", target));
101
	RETERR(str_totext(tctx->linebreak, target));
102 103 104 105 106
	if (tctx->width == 0)   /* No splitting */
		RETERR(isc_base64_totext(&sr, 60, "", target));
	else
		RETERR(isc_base64_totext(&sr, tctx->width - 2,
					 tctx->linebreak, target));
107 108
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" )", target));
109
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
110 111
}

112
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
113
fromwire_cert(ARGS_FROMWIRE) {
Mark Andrews's avatar
Mark Andrews committed
114 115
	isc_region_t sr;

116
	REQUIRE(type == dns_rdatatype_cert);
117

118
	UNUSED(type);
David Lawrence's avatar
David Lawrence committed
119 120
	UNUSED(rdclass);
	UNUSED(dctx);
121
	UNUSED(options);
Mark Andrews's avatar
Mark Andrews committed
122

123
	isc_buffer_activeregion(source, &sr);
Mark Andrews's avatar
Mark Andrews committed
124
	if (sr.length < 5)
125
		return (ISC_R_UNEXPECTEDEND);
Mark Andrews's avatar
Mark Andrews committed
126 127 128 129 130

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

131
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
132
towire_cert(ARGS_TOWIRE) {
Mark Andrews's avatar
Mark Andrews committed
133 134
	isc_region_t sr;

135
	REQUIRE(rdata->type == dns_rdatatype_cert);
136
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
137

David Lawrence's avatar
David Lawrence committed
138
	UNUSED(cctx);
Mark Andrews's avatar
Mark Andrews committed
139 140 141 142 143

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

144
static inline int
David Lawrence's avatar
David Lawrence committed
145
compare_cert(ARGS_COMPARE) {
Mark Andrews's avatar
Mark Andrews committed
146 147 148 149
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
150
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
151
	REQUIRE(rdata1->type == dns_rdatatype_cert);
152 153
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
Mark Andrews's avatar
Mark Andrews committed
154 155 156

	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
157
	return (isc_region_compare(&r1, &r2));
Mark Andrews's avatar
Mark Andrews committed
158 159
}

160
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
161
fromstruct_cert(ARGS_FROMSTRUCT) {
162
	dns_rdata_cert_t *cert = source;
Mark Andrews's avatar
Mark Andrews committed
163

164
	REQUIRE(type == dns_rdatatype_cert);
165 166 167
	REQUIRE(source != NULL);
	REQUIRE(cert->common.rdtype == type);
	REQUIRE(cert->common.rdclass == rdclass);
168

169
	UNUSED(type);
170 171
	UNUSED(rdclass);

172 173 174
	RETERR(uint16_tobuffer(cert->type, target));
	RETERR(uint16_tobuffer(cert->key_tag, target));
	RETERR(uint8_tobuffer(cert->algorithm, target));
Mark Andrews's avatar
Mark Andrews committed
175

176
	return (mem_tobuffer(target, cert->certificate, cert->length));
Mark Andrews's avatar
Mark Andrews committed
177 178
}

179
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
180
tostruct_cert(ARGS_TOSTRUCT) {
181 182
	dns_rdata_cert_t *cert = target;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
183

184
	REQUIRE(rdata->type == dns_rdatatype_cert);
Mark Andrews's avatar
Mark Andrews committed
185
	REQUIRE(target != NULL);
186
	REQUIRE(rdata->length != 0);
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201
	cert->common.rdclass = rdata->rdclass;
	cert->common.rdtype = rdata->type;
	ISC_LINK_INIT(&cert->common, link);

	dns_rdata_toregion(rdata, &region);

	cert->type = uint16_fromregion(&region);
	isc_region_consume(&region, 2);
	cert->key_tag = uint16_fromregion(&region);
	isc_region_consume(&region, 2);
	cert->algorithm = uint8_fromregion(&region);
	isc_region_consume(&region, 1);
	cert->length = region.length;

202 203 204
	cert->certificate = mem_maybedup(mctx, region.base, region.length);
	if (cert->certificate == NULL)
		return (ISC_R_NOMEMORY);
205 206 207

	cert->mctx = mctx;
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
208
}
209

210
static inline void
David Lawrence's avatar
David Lawrence committed
211 212
freestruct_cert(ARGS_FREESTRUCT) {
	dns_rdata_cert_t *cert = source;
213

David Lawrence's avatar
David Lawrence committed
214
	REQUIRE(cert != NULL);
215
	REQUIRE(cert->common.rdtype == dns_rdatatype_cert);
216

217 218 219 220 221 222
	if (cert->mctx == NULL)
		return;

	if (cert->certificate != NULL)
		isc_mem_free(cert->mctx, cert->certificate);
	cert->mctx = NULL;
223
}
224

225
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
226
additionaldata_cert(ARGS_ADDLDATA) {
227
	REQUIRE(rdata->type == dns_rdatatype_cert);
228

229
	UNUSED(rdata);
David Lawrence's avatar
David Lawrence committed
230 231
	UNUSED(add);
	UNUSED(arg);
232

233
	return (ISC_R_SUCCESS);
234 235
}

236
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
237
digest_cert(ARGS_DIGEST) {
Bob Halley's avatar
Bob Halley committed
238 239
	isc_region_t r;

240
	REQUIRE(rdata->type == dns_rdatatype_cert);
Bob Halley's avatar
Bob Halley committed
241 242 243 244 245 246

	dns_rdata_toregion(rdata, &r);

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

247
static inline bool
248 249
checkowner_cert(ARGS_CHECKOWNER) {

250
	REQUIRE(type == dns_rdatatype_cert);
251 252 253 254 255 256

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

257
	return (true);
258 259
}

260
static inline bool
261 262
checknames_cert(ARGS_CHECKNAMES) {

263
	REQUIRE(rdata->type == dns_rdatatype_cert);
264 265 266 267 268

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

269
	return (true);
270 271 272
}


273 274 275 276 277
static inline int
casecompare_cert(ARGS_COMPARE) {
	return (compare_cert(rdata1, rdata2));
}
#endif	/* RDATA_GENERIC_CERT_37_C */