1 /*	$NetBSD: openpgpkey_61.c,v 1.1.1.2 2015/09/03 07:21:39 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef RDATA_GENERIC_OPENPGPKEY_61_C
20 #define RDATA_GENERIC_OPENPGPKEY_61_C
21 
22 #define RRTYPE_OPENPGPKEY_ATTRIBUTES 0
23 
24 static inline isc_result_t
fromtext_openpgpkey(ARGS_FROMTEXT)25 fromtext_openpgpkey(ARGS_FROMTEXT) {
26 
27 	REQUIRE(type == 61);
28 
29 	UNUSED(type);
30 	UNUSED(rdclass);
31 	UNUSED(callbacks);
32 	UNUSED(options);
33 	UNUSED(origin);
34 
35 	/*
36 	 * Keyring.
37 	 */
38 	return (isc_base64_tobuffer(lexer, target, -1));
39 }
40 
41 static inline isc_result_t
totext_openpgpkey(ARGS_TOTEXT)42 totext_openpgpkey(ARGS_TOTEXT) {
43 	isc_region_t sr;
44 
45 	REQUIRE(rdata->type == 61);
46 	REQUIRE(rdata->length != 0);
47 
48 	dns_rdata_toregion(rdata, &sr);
49 
50 	/*
51 	 * Keyring
52 	 */
53 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
54 		RETERR(str_totext("( ", target));
55 
56 	if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
57 		if (tctx->width == 0)   /* No splitting */
58 			RETERR(isc_base64_totext(&sr, 60, "", target));
59 		else
60 			RETERR(isc_base64_totext(&sr, tctx->width - 2,
61 						 tctx->linebreak, target));
62 	} else
63 		RETERR(str_totext("[omitted]", target));
64 
65 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
66 		RETERR(str_totext(" )", target));
67 
68 	return (ISC_R_SUCCESS);
69 }
70 
71 static inline isc_result_t
fromwire_openpgpkey(ARGS_FROMWIRE)72 fromwire_openpgpkey(ARGS_FROMWIRE) {
73 	isc_region_t sr;
74 
75 	REQUIRE(type == 61);
76 
77 	UNUSED(type);
78 	UNUSED(rdclass);
79 	UNUSED(dctx);
80 	UNUSED(options);
81 
82 	/*
83 	 * Keyring.
84 	 */
85 	isc_buffer_activeregion(source, &sr);
86 	if (sr.length < 1)
87 		return (ISC_R_UNEXPECTEDEND);
88 	isc_buffer_forward(source, sr.length);
89 	return (mem_tobuffer(target, sr.base, sr.length));
90 }
91 
92 static inline isc_result_t
towire_openpgpkey(ARGS_TOWIRE)93 towire_openpgpkey(ARGS_TOWIRE) {
94 	isc_region_t sr;
95 
96 	REQUIRE(rdata->type == 61);
97 	REQUIRE(rdata->length != 0);
98 
99 	UNUSED(cctx);
100 
101 	dns_rdata_toregion(rdata, &sr);
102 	return (mem_tobuffer(target, sr.base, sr.length));
103 }
104 
105 static inline int
compare_openpgpkey(ARGS_COMPARE)106 compare_openpgpkey(ARGS_COMPARE) {
107 	isc_region_t r1;
108 	isc_region_t r2;
109 
110 	REQUIRE(rdata1->type == rdata2->type);
111 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
112 	REQUIRE(rdata1->type == 61);
113 	REQUIRE(rdata1->length != 0);
114 	REQUIRE(rdata2->length != 0);
115 
116 	dns_rdata_toregion(rdata1, &r1);
117 	dns_rdata_toregion(rdata2, &r2);
118 	return (isc_region_compare(&r1, &r2));
119 }
120 
121 static inline isc_result_t
fromstruct_openpgpkey(ARGS_FROMSTRUCT)122 fromstruct_openpgpkey(ARGS_FROMSTRUCT) {
123 	dns_rdata_openpgpkey_t *sig = source;
124 
125 	REQUIRE(type == 61);
126 	REQUIRE(source != NULL);
127 	REQUIRE(sig->common.rdtype == type);
128 	REQUIRE(sig->common.rdclass == rdclass);
129 	REQUIRE(sig->keyring != NULL && sig->length != 0);
130 
131 	UNUSED(type);
132 	UNUSED(rdclass);
133 
134 	/*
135 	 * Keyring.
136 	 */
137 	return (mem_tobuffer(target, sig->keyring, sig->length));
138 }
139 
140 static inline isc_result_t
tostruct_openpgpkey(ARGS_TOSTRUCT)141 tostruct_openpgpkey(ARGS_TOSTRUCT) {
142 	isc_region_t sr;
143 	dns_rdata_openpgpkey_t *sig = target;
144 
145 	REQUIRE(rdata->type == 61);
146 	REQUIRE(target != NULL);
147 	REQUIRE(rdata->length != 0);
148 
149 	sig->common.rdclass = rdata->rdclass;
150 	sig->common.rdtype = rdata->type;
151 	ISC_LINK_INIT(&sig->common, link);
152 
153 	dns_rdata_toregion(rdata, &sr);
154 
155 	/*
156 	 * Keyring.
157 	 */
158 	sig->length = sr.length;
159 	sig->keyring = mem_maybedup(mctx, sr.base, sig->length);
160 	if (sig->keyring == NULL)
161 		goto cleanup;
162 
163 	sig->mctx = mctx;
164 	return (ISC_R_SUCCESS);
165 
166  cleanup:
167 	return (ISC_R_NOMEMORY);
168 }
169 
170 static inline void
freestruct_openpgpkey(ARGS_FREESTRUCT)171 freestruct_openpgpkey(ARGS_FREESTRUCT) {
172 	dns_rdata_openpgpkey_t *sig = (dns_rdata_openpgpkey_t *) source;
173 
174 	REQUIRE(source != NULL);
175 	REQUIRE(sig->common.rdtype == 61);
176 
177 	if (sig->mctx == NULL)
178 		return;
179 
180 	if (sig->keyring != NULL)
181 		isc_mem_free(sig->mctx, sig->keyring);
182 	sig->mctx = NULL;
183 }
184 
185 static inline isc_result_t
additionaldata_openpgpkey(ARGS_ADDLDATA)186 additionaldata_openpgpkey(ARGS_ADDLDATA) {
187 	REQUIRE(rdata->type == 61);
188 
189 	UNUSED(rdata);
190 	UNUSED(add);
191 	UNUSED(arg);
192 
193 	return (ISC_R_SUCCESS);
194 }
195 
196 static inline isc_result_t
digest_openpgpkey(ARGS_DIGEST)197 digest_openpgpkey(ARGS_DIGEST) {
198 	isc_region_t r;
199 
200 	REQUIRE(rdata->type == 61);
201 
202 	dns_rdata_toregion(rdata, &r);
203 
204 	return ((digest)(arg, &r));
205 }
206 
207 static inline isc_boolean_t
checkowner_openpgpkey(ARGS_CHECKOWNER)208 checkowner_openpgpkey(ARGS_CHECKOWNER) {
209 
210 	REQUIRE(type == 61);
211 
212 	UNUSED(name);
213 	UNUSED(type);
214 	UNUSED(rdclass);
215 	UNUSED(wildcard);
216 
217 	return (ISC_TRUE);
218 }
219 
220 static inline isc_boolean_t
checknames_openpgpkey(ARGS_CHECKNAMES)221 checknames_openpgpkey(ARGS_CHECKNAMES) {
222 
223 	REQUIRE(rdata->type == 61);
224 
225 	UNUSED(rdata);
226 	UNUSED(owner);
227 	UNUSED(bad);
228 
229 	return (ISC_TRUE);
230 }
231 
232 static inline int
casecompare_openpgpkey(ARGS_COMPARE)233 casecompare_openpgpkey(ARGS_COMPARE) {
234 	isc_region_t r1;
235 	isc_region_t r2;
236 
237 	REQUIRE(rdata1->type == rdata2->type);
238 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
239 	REQUIRE(rdata1->type == 61);
240 	REQUIRE(rdata1->length != 0);
241 	REQUIRE(rdata2->length != 0);
242 
243 	dns_rdata_toregion(rdata1, &r1);
244 	dns_rdata_toregion(rdata2, &r2);
245 
246 	return (isc_region_compare(&r1, &r2));
247 }
248 
249 #endif	/* RDATA_GENERIC_OPENPGPKEY_61_C */
250