cert_37.c 6.28 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004, 2005, 2007, 2009, 2011  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Mark Andrews's avatar
Mark Andrews committed
6
7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9
10
11
12
13
14
15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
Mark Andrews's avatar
Mark Andrews committed
16
17
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id$ */
David Lawrence's avatar
David Lawrence committed
19
20

/* Reviewed: Wed Mar 15 21:14:32 EST 2000 by tale */
Mark Andrews's avatar
Mark Andrews committed
21

22
/* RFC2538 */
Mark Andrews's avatar
Mark Andrews committed
23

24
25
#ifndef RDATA_GENERIC_CERT_37_C
#define RDATA_GENERIC_CERT_37_C
Mark Andrews's avatar
Mark Andrews committed
26

27
28
#define RRTYPE_CERT_ATTRIBUTES (0)

29
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
30
fromtext_cert(ARGS_FROMTEXT) {
Mark Andrews's avatar
Mark Andrews committed
31
	isc_token_t token;
Mark Andrews's avatar
Mark Andrews committed
32
33
	dns_secalg_t secalg;
	dns_cert_t cert;
Mark Andrews's avatar
Mark Andrews committed
34
35
36

	REQUIRE(type == 37);

37
	UNUSED(type);
David Lawrence's avatar
David Lawrence committed
38
39
	UNUSED(rdclass);
	UNUSED(origin);
40
	UNUSED(options);
41
	UNUSED(callbacks);
Mark Andrews's avatar
Mark Andrews committed
42

David Lawrence's avatar
David Lawrence committed
43
44
45
	/*
	 * Cert type.
	 */
46
47
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
48
	RETTOK(dns_cert_fromtext(&cert, &token.value.as_textregion));
Mark Andrews's avatar
Mark Andrews committed
49
	RETERR(uint16_tobuffer(cert, target));
50

David Lawrence's avatar
David Lawrence committed
51
52
53
	/*
	 * Key tag.
	 */
54
55
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
				      ISC_FALSE));
56
	if (token.value.as_ulong > 0xffffU)
57
		RETTOK(ISC_R_RANGE);
Mark Andrews's avatar
Mark Andrews committed
58
59
	RETERR(uint16_tobuffer(token.value.as_ulong, target));

David Lawrence's avatar
David Lawrence committed
60
61
62
	/*
	 * Algorithm.
	 */
63
64
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
65
	RETTOK(dns_secalg_fromtext(&secalg, &token.value.as_textregion));
Mark Andrews's avatar
Mark Andrews committed
66
	RETERR(mem_tobuffer(target, &secalg, 1));
Mark Andrews's avatar
Mark Andrews committed
67

68
	return (isc_base64_tobuffer(lexer, target, -1));
Mark Andrews's avatar
Mark Andrews committed
69
70
}

71
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
72
totext_cert(ARGS_TOTEXT) {
Mark Andrews's avatar
Mark Andrews committed
73
	isc_region_t sr;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
74
	char buf[sizeof("64000 ")];
Mark Andrews's avatar
Mark Andrews committed
75
76
77
	unsigned int n;

	REQUIRE(rdata->type == 37);
78
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
79

David Lawrence's avatar
David Lawrence committed
80
	UNUSED(tctx);
Mark Andrews's avatar
Mark Andrews committed
81
82
83

	dns_rdata_toregion(rdata, &sr);

David Lawrence's avatar
David Lawrence committed
84
85
86
	/*
	 * Type.
	 */
Mark Andrews's avatar
Mark Andrews committed
87
88
	n = uint16_fromregion(&sr);
	isc_region_consume(&sr, 2);
89
	RETERR(dns_cert_totext((dns_cert_t)n, target));
Mark Andrews's avatar
Mark Andrews committed
90
91
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
92
93
94
	/*
	 * Key tag.
	 */
Mark Andrews's avatar
Mark Andrews committed
95
96
	n = uint16_fromregion(&sr);
	isc_region_consume(&sr, 2);
Mark Andrews's avatar
Mark Andrews committed
97
	sprintf(buf, "%u ", n);
Mark Andrews's avatar
Mark Andrews committed
98
99
	RETERR(str_totext(buf, target));

David Lawrence's avatar
David Lawrence committed
100
101
102
	/*
	 * Algorithm.
	 */
Mark Andrews's avatar
Mark Andrews committed
103
104
	RETERR(dns_secalg_totext(sr.base[0], target));
	isc_region_consume(&sr, 1);
Mark Andrews's avatar
Mark Andrews committed
105

David Lawrence's avatar
David Lawrence committed
106
107
108
	/*
	 * Cert.
	 */
109
110
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" (", target));
111
	RETERR(str_totext(tctx->linebreak, target));
112
113
114
115
116
	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));
117
118
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" )", target));
119
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
120
121
}

122
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
123
fromwire_cert(ARGS_FROMWIRE) {
Mark Andrews's avatar
Mark Andrews committed
124
125
126
	isc_region_t sr;

	REQUIRE(type == 37);
127

128
	UNUSED(type);
David Lawrence's avatar
David Lawrence committed
129
130
	UNUSED(rdclass);
	UNUSED(dctx);
131
	UNUSED(options);
Mark Andrews's avatar
Mark Andrews committed
132

133
	isc_buffer_activeregion(source, &sr);
Mark Andrews's avatar
Mark Andrews committed
134
	if (sr.length < 5)
135
		return (ISC_R_UNEXPECTEDEND);
Mark Andrews's avatar
Mark Andrews committed
136
137
138
139
140

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

141
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
142
towire_cert(ARGS_TOWIRE) {
Mark Andrews's avatar
Mark Andrews committed
143
144
145
	isc_region_t sr;

	REQUIRE(rdata->type == 37);
146
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
147

David Lawrence's avatar
David Lawrence committed
148
	UNUSED(cctx);
Mark Andrews's avatar
Mark Andrews committed
149
150
151
152
153

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

154
static inline int
David Lawrence's avatar
David Lawrence committed
155
compare_cert(ARGS_COMPARE) {
Mark Andrews's avatar
Mark Andrews committed
156
157
158
159
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
160
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
Mark Andrews's avatar
Mark Andrews committed
161
	REQUIRE(rdata1->type == 37);
162
163
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
Mark Andrews's avatar
Mark Andrews committed
164
165
166

	dns_rdata_toregion(rdata1, &r1);
	dns_rdata_toregion(rdata2, &r2);
167
	return (isc_region_compare(&r1, &r2));
Mark Andrews's avatar
Mark Andrews committed
168
169
}

170
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
171
fromstruct_cert(ARGS_FROMSTRUCT) {
172
	dns_rdata_cert_t *cert = source;
Mark Andrews's avatar
Mark Andrews committed
173
174

	REQUIRE(type == 37);
175
176
177
	REQUIRE(source != NULL);
	REQUIRE(cert->common.rdtype == type);
	REQUIRE(cert->common.rdclass == rdclass);
178

179
	UNUSED(type);
180
181
	UNUSED(rdclass);

182
183
184
	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
185

186
	return (mem_tobuffer(target, cert->certificate, cert->length));
Mark Andrews's avatar
Mark Andrews committed
187
188
}

189
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
190
tostruct_cert(ARGS_TOSTRUCT) {
191
192
	dns_rdata_cert_t *cert = target;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
193
194

	REQUIRE(rdata->type == 37);
Mark Andrews's avatar
Mark Andrews committed
195
	REQUIRE(target != NULL);
196
	REQUIRE(rdata->length != 0);
197

198
199
200
201
202
203
204
205
206
207
208
209
210
211
	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;

212
213
214
	cert->certificate = mem_maybedup(mctx, region.base, region.length);
	if (cert->certificate == NULL)
		return (ISC_R_NOMEMORY);
215
216
217

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

220
static inline void
David Lawrence's avatar
David Lawrence committed
221
222
freestruct_cert(ARGS_FREESTRUCT) {
	dns_rdata_cert_t *cert = source;
223

David Lawrence's avatar
David Lawrence committed
224
	REQUIRE(cert != NULL);
225
	REQUIRE(cert->common.rdtype == 37);
226

227
228
229
230
231
232
	if (cert->mctx == NULL)
		return;

	if (cert->certificate != NULL)
		isc_mem_free(cert->mctx, cert->certificate);
	cert->mctx = NULL;
233
}
234

235
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
236
additionaldata_cert(ARGS_ADDLDATA) {
237
238
	REQUIRE(rdata->type == 37);

239
	UNUSED(rdata);
David Lawrence's avatar
David Lawrence committed
240
241
	UNUSED(add);
	UNUSED(arg);
242

243
	return (ISC_R_SUCCESS);
244
245
}

246
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
247
digest_cert(ARGS_DIGEST) {
Bob Halley's avatar
Bob Halley committed
248
249
250
251
252
253
254
255
256
	isc_region_t r;

	REQUIRE(rdata->type == 37);

	dns_rdata_toregion(rdata, &r);

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

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
static inline isc_boolean_t
checkowner_cert(ARGS_CHECKOWNER) {

	REQUIRE(type == 37);

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

	return (ISC_TRUE);
}

static inline isc_boolean_t
checknames_cert(ARGS_CHECKNAMES) {

	REQUIRE(rdata->type == 37);

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

	return (ISC_TRUE);
}


283
284
285
286
287
static inline int
casecompare_cert(ARGS_COMPARE) {
	return (compare_cert(rdata1, rdata2));
}
#endif	/* RDATA_GENERIC_CERT_37_C */