cert_37.c 5.76 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1999, 2000  Internet Software Consortium.
3
 *
Mark Andrews's avatar
Mark Andrews committed
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.
Mark Andrews's avatar
Mark Andrews committed
16 17
 */

18
/* $Id: cert_37.c,v 1.35 2000/12/01 01:40:16 gson Exp $ */
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

David Lawrence's avatar
David Lawrence committed
22
/* RFC 2538 */
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);

David Lawrence's avatar
David Lawrence committed
37 38 39
	UNUSED(rdclass);
	UNUSED(origin);
	UNUSED(downcase);
Mark Andrews's avatar
Mark Andrews committed
40

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

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

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

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

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

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

David Lawrence's avatar
David Lawrence committed
78
	UNUSED(tctx);
Mark Andrews's avatar
Mark Andrews committed
79 80 81

	dns_rdata_toregion(rdata, &sr);

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

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

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

David Lawrence's avatar
David Lawrence committed
104 105 106
	/*
	 * Cert.
	 */
107 108
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" (", target));
109
	RETERR(str_totext(tctx->linebreak, target));
David Lawrence's avatar
David Lawrence committed
110
	RETERR(isc_base64_totext(&sr, tctx->width - 2,
111
				 tctx->linebreak, target));
112 113
	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
		RETERR(str_totext(" )", target));
114
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
115 116
}

117
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
118
fromwire_cert(ARGS_FROMWIRE) {
Mark Andrews's avatar
Mark Andrews committed
119 120 121
	isc_region_t sr;

	REQUIRE(type == 37);
122

David Lawrence's avatar
David Lawrence committed
123 124 125
	UNUSED(rdclass);
	UNUSED(dctx);
	UNUSED(downcase);
Mark Andrews's avatar
Mark Andrews committed
126

127
	isc_buffer_activeregion(source, &sr);
Mark Andrews's avatar
Mark Andrews committed
128
	if (sr.length < 5)
129
		return (ISC_R_UNEXPECTEDEND);
Mark Andrews's avatar
Mark Andrews committed
130 131 132 133 134

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

135
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
136
towire_cert(ARGS_TOWIRE) {
Mark Andrews's avatar
Mark Andrews committed
137 138 139
	isc_region_t sr;

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

David Lawrence's avatar
David Lawrence committed
142
	UNUSED(cctx);
Mark Andrews's avatar
Mark Andrews committed
143 144 145 146 147

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

148
static inline int
David Lawrence's avatar
David Lawrence committed
149
compare_cert(ARGS_COMPARE) {
Mark Andrews's avatar
Mark Andrews committed
150 151 152 153
	isc_region_t r1;
	isc_region_t r2;

	REQUIRE(rdata1->type == rdata2->type);
154
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
Mark Andrews's avatar
Mark Andrews committed
155
	REQUIRE(rdata1->type == 37);
156 157
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
Mark Andrews's avatar
Mark Andrews committed
158 159 160 161 162 163

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

164
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
165
fromstruct_cert(ARGS_FROMSTRUCT) {
166
	dns_rdata_cert_t *cert = source;
Mark Andrews's avatar
Mark Andrews committed
167 168

	REQUIRE(type == 37);
169 170 171
	REQUIRE(source != NULL);
	REQUIRE(cert->common.rdtype == type);
	REQUIRE(cert->common.rdclass == rdclass);
172

173 174
	UNUSED(rdclass);

175 176 177
	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
178

179
	return (mem_tobuffer(target, cert->certificate, cert->length));
Mark Andrews's avatar
Mark Andrews committed
180 181
}

182
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
183
tostruct_cert(ARGS_TOSTRUCT) {
184 185
	dns_rdata_cert_t *cert = target;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
186 187

	REQUIRE(rdata->type == 37);
Mark Andrews's avatar
Mark Andrews committed
188
	REQUIRE(target != NULL);
189
	REQUIRE(rdata->length != 0);
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	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;

	if (cert->length > 0) {
		cert->certificate = mem_maybedup(mctx, region.base,
						 region.length);
		if (cert->certificate == NULL)
			return (ISC_R_NOMEMORY);
	} else
		cert->certificate = NULL;

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

217
static inline void
David Lawrence's avatar
David Lawrence committed
218 219
freestruct_cert(ARGS_FREESTRUCT) {
	dns_rdata_cert_t *cert = source;
220

David Lawrence's avatar
David Lawrence committed
221
	REQUIRE(cert != NULL);
222
	REQUIRE(cert->common.rdtype == 37);
223

224 225 226 227 228 229
	if (cert->mctx == NULL)
		return;

	if (cert->certificate != NULL)
		isc_mem_free(cert->mctx, cert->certificate);
	cert->mctx = NULL;
230
}
231

232
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
233
additionaldata_cert(ARGS_ADDLDATA) {
234 235
	REQUIRE(rdata->type == 37);

236
	UNUSED(rdata);
David Lawrence's avatar
David Lawrence committed
237 238
	UNUSED(add);
	UNUSED(arg);
239

240
	return (ISC_R_SUCCESS);
241 242
}

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

	REQUIRE(rdata->type == 37);

	dns_rdata_toregion(rdata, &r);

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

254
#endif	/* RDATA_GENERIC_CERT_37_C */