1/*
2 * Copyright (C) 2004, 2005, 2007, 2009, 2012  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id$ */
19
20/* Reviewed: Wed Mar 15 21:14:32 EST 2000 by tale */
21
22/* RFC2538 */
23
24#ifndef RDATA_GENERIC_CERT_37_C
25#define RDATA_GENERIC_CERT_37_C
26
27#define RRTYPE_CERT_ATTRIBUTES (0)
28
29static inline isc_result_t
30fromtext_cert(ARGS_FROMTEXT) {
31	isc_token_t token;
32	dns_secalg_t secalg;
33	dns_cert_t cert;
34
35	REQUIRE(type == 37);
36
37	UNUSED(type);
38	UNUSED(rdclass);
39	UNUSED(origin);
40	UNUSED(options);
41	UNUSED(callbacks);
42
43	/*
44	 * Cert type.
45	 */
46	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
47				      ISC_FALSE));
48	RETTOK(dns_cert_fromtext(&cert, &token.value.as_textregion));
49	RETERR(uint16_tobuffer(cert, target));
50
51	/*
52	 * Key tag.
53	 */
54	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
55				      ISC_FALSE));
56	if (token.value.as_ulong > 0xffffU)
57		RETTOK(ISC_R_RANGE);
58	RETERR(uint16_tobuffer(token.value.as_ulong, target));
59
60	/*
61	 * Algorithm.
62	 */
63	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
64				      ISC_FALSE));
65	RETTOK(dns_secalg_fromtext(&secalg, &token.value.as_textregion));
66	RETERR(mem_tobuffer(target, &secalg, 1));
67
68	return (isc_base64_tobuffer(lexer, target, -1));
69}
70
71static inline isc_result_t
72totext_cert(ARGS_TOTEXT) {
73	isc_region_t sr;
74	char buf[sizeof("64000 ")];
75	unsigned int n;
76
77	REQUIRE(rdata->type == 37);
78	REQUIRE(rdata->length != 0);
79
80	UNUSED(tctx);
81
82	dns_rdata_toregion(rdata, &sr);
83
84	/*
85	 * Type.
86	 */
87	n = uint16_fromregion(&sr);
88	isc_region_consume(&sr, 2);
89	RETERR(dns_cert_totext((dns_cert_t)n, target));
90	RETERR(str_totext(" ", target));
91
92	/*
93	 * Key tag.
94	 */
95	n = uint16_fromregion(&sr);
96	isc_region_consume(&sr, 2);
97	sprintf(buf, "%u ", n);
98	RETERR(str_totext(buf, target));
99
100	/*
101	 * Algorithm.
102	 */
103	RETERR(dns_secalg_totext(sr.base[0], target));
104	isc_region_consume(&sr, 1);
105
106	/*
107	 * Cert.
108	 */
109	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
110		RETERR(str_totext(" (", target));
111	RETERR(str_totext(tctx->linebreak, target));
112	RETERR(isc_base64_totext(&sr, tctx->width - 2,
113				 tctx->linebreak, target));
114	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
115		RETERR(str_totext(" )", target));
116	return (ISC_R_SUCCESS);
117}
118
119static inline isc_result_t
120fromwire_cert(ARGS_FROMWIRE) {
121	isc_region_t sr;
122
123	REQUIRE(type == 37);
124
125	UNUSED(type);
126	UNUSED(rdclass);
127	UNUSED(dctx);
128	UNUSED(options);
129
130	isc_buffer_activeregion(source, &sr);
131	if (sr.length < 5)
132		return (ISC_R_UNEXPECTEDEND);
133
134	isc_buffer_forward(source, sr.length);
135	return (mem_tobuffer(target, sr.base, sr.length));
136}
137
138static inline isc_result_t
139towire_cert(ARGS_TOWIRE) {
140	isc_region_t sr;
141
142	REQUIRE(rdata->type == 37);
143	REQUIRE(rdata->length != 0);
144
145	UNUSED(cctx);
146
147	dns_rdata_toregion(rdata, &sr);
148	return (mem_tobuffer(target, sr.base, sr.length));
149}
150
151static inline int
152compare_cert(ARGS_COMPARE) {
153	isc_region_t r1;
154	isc_region_t r2;
155
156	REQUIRE(rdata1->type == rdata2->type);
157	REQUIRE(rdata1->rdclass == rdata2->rdclass);
158	REQUIRE(rdata1->type == 37);
159	REQUIRE(rdata1->length != 0);
160	REQUIRE(rdata2->length != 0);
161
162	dns_rdata_toregion(rdata1, &r1);
163	dns_rdata_toregion(rdata2, &r2);
164	return (isc_region_compare(&r1, &r2));
165}
166
167static inline isc_result_t
168fromstruct_cert(ARGS_FROMSTRUCT) {
169	dns_rdata_cert_t *cert = source;
170
171	REQUIRE(type == 37);
172	REQUIRE(source != NULL);
173	REQUIRE(cert->common.rdtype == type);
174	REQUIRE(cert->common.rdclass == rdclass);
175
176	UNUSED(type);
177	UNUSED(rdclass);
178
179	RETERR(uint16_tobuffer(cert->type, target));
180	RETERR(uint16_tobuffer(cert->key_tag, target));
181	RETERR(uint8_tobuffer(cert->algorithm, target));
182
183	return (mem_tobuffer(target, cert->certificate, cert->length));
184}
185
186static inline isc_result_t
187tostruct_cert(ARGS_TOSTRUCT) {
188	dns_rdata_cert_t *cert = target;
189	isc_region_t region;
190
191	REQUIRE(rdata->type == 37);
192	REQUIRE(target != NULL);
193	REQUIRE(rdata->length != 0);
194
195	cert->common.rdclass = rdata->rdclass;
196	cert->common.rdtype = rdata->type;
197	ISC_LINK_INIT(&cert->common, link);
198
199	dns_rdata_toregion(rdata, &region);
200
201	cert->type = uint16_fromregion(&region);
202	isc_region_consume(&region, 2);
203	cert->key_tag = uint16_fromregion(&region);
204	isc_region_consume(&region, 2);
205	cert->algorithm = uint8_fromregion(&region);
206	isc_region_consume(&region, 1);
207	cert->length = region.length;
208
209	cert->certificate = mem_maybedup(mctx, region.base, region.length);
210	if (cert->certificate == NULL)
211		return (ISC_R_NOMEMORY);
212
213	cert->mctx = mctx;
214	return (ISC_R_SUCCESS);
215}
216
217static inline void
218freestruct_cert(ARGS_FREESTRUCT) {
219	dns_rdata_cert_t *cert = source;
220
221	REQUIRE(cert != NULL);
222	REQUIRE(cert->common.rdtype == 37);
223
224	if (cert->mctx == NULL)
225		return;
226
227	if (cert->certificate != NULL)
228		isc_mem_free(cert->mctx, cert->certificate);
229	cert->mctx = NULL;
230}
231
232static inline isc_result_t
233additionaldata_cert(ARGS_ADDLDATA) {
234	REQUIRE(rdata->type == 37);
235
236	UNUSED(rdata);
237	UNUSED(add);
238	UNUSED(arg);
239
240	return (ISC_R_SUCCESS);
241}
242
243static inline isc_result_t
244digest_cert(ARGS_DIGEST) {
245	isc_region_t r;
246
247	REQUIRE(rdata->type == 37);
248
249	dns_rdata_toregion(rdata, &r);
250
251	return ((digest)(arg, &r));
252}
253
254static inline isc_boolean_t
255checkowner_cert(ARGS_CHECKOWNER) {
256
257	REQUIRE(type == 37);
258
259	UNUSED(name);
260	UNUSED(type);
261	UNUSED(rdclass);
262	UNUSED(wildcard);
263
264	return (ISC_TRUE);
265}
266
267static inline isc_boolean_t
268checknames_cert(ARGS_CHECKNAMES) {
269
270	REQUIRE(rdata->type == 37);
271
272	UNUSED(rdata);
273	UNUSED(owner);
274	UNUSED(bad);
275
276	return (ISC_TRUE);
277}
278
279
280static inline int
281casecompare_cert(ARGS_COMPARE) {
282	return (compare_cert(rdata1, rdata2));
283}
284#endif	/* RDATA_GENERIC_CERT_37_C */
285