xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/generic/openpgpkey_61.c (revision 2718af68c3efc72c9769069b5c7f9ed36f6b9def)
1 /*	$NetBSD: openpgpkey_61.c,v 1.7 2021/02/19 16:42:17 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #ifndef RDATA_GENERIC_OPENPGPKEY_61_C
15 #define RDATA_GENERIC_OPENPGPKEY_61_C
16 
17 #define RRTYPE_OPENPGPKEY_ATTRIBUTES 0
18 
19 static inline isc_result_t
20 fromtext_openpgpkey(ARGS_FROMTEXT) {
21 	REQUIRE(type == dns_rdatatype_openpgpkey);
22 
23 	UNUSED(type);
24 	UNUSED(rdclass);
25 	UNUSED(callbacks);
26 	UNUSED(options);
27 	UNUSED(origin);
28 
29 	/*
30 	 * Keyring.
31 	 */
32 	return (isc_base64_tobuffer(lexer, target, -2));
33 }
34 
35 static inline isc_result_t
36 totext_openpgpkey(ARGS_TOTEXT) {
37 	isc_region_t sr;
38 
39 	REQUIRE(rdata->type == dns_rdatatype_openpgpkey);
40 	REQUIRE(rdata->length != 0);
41 
42 	dns_rdata_toregion(rdata, &sr);
43 
44 	/*
45 	 * Keyring
46 	 */
47 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
48 		RETERR(str_totext("( ", target));
49 	}
50 
51 	if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
52 		if (tctx->width == 0) { /* No splitting */
53 			RETERR(isc_base64_totext(&sr, 60, "", target));
54 		} else {
55 			RETERR(isc_base64_totext(&sr, tctx->width - 2,
56 						 tctx->linebreak, target));
57 		}
58 	} else {
59 		RETERR(str_totext("[omitted]", target));
60 	}
61 
62 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
63 		RETERR(str_totext(" )", target));
64 	}
65 
66 	return (ISC_R_SUCCESS);
67 }
68 
69 static inline isc_result_t
70 fromwire_openpgpkey(ARGS_FROMWIRE) {
71 	isc_region_t sr;
72 
73 	REQUIRE(type == dns_rdatatype_openpgpkey);
74 
75 	UNUSED(type);
76 	UNUSED(rdclass);
77 	UNUSED(dctx);
78 	UNUSED(options);
79 
80 	/*
81 	 * Keyring.
82 	 */
83 	isc_buffer_activeregion(source, &sr);
84 	if (sr.length < 1) {
85 		return (ISC_R_UNEXPECTEDEND);
86 	}
87 	isc_buffer_forward(source, sr.length);
88 	return (mem_tobuffer(target, sr.base, sr.length));
89 }
90 
91 static inline isc_result_t
92 towire_openpgpkey(ARGS_TOWIRE) {
93 	isc_region_t sr;
94 
95 	REQUIRE(rdata->type == dns_rdatatype_openpgpkey);
96 	REQUIRE(rdata->length != 0);
97 
98 	UNUSED(cctx);
99 
100 	dns_rdata_toregion(rdata, &sr);
101 	return (mem_tobuffer(target, sr.base, sr.length));
102 }
103 
104 static inline int
105 compare_openpgpkey(ARGS_COMPARE) {
106 	isc_region_t r1;
107 	isc_region_t r2;
108 
109 	REQUIRE(rdata1->type == rdata2->type);
110 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
111 	REQUIRE(rdata1->type == dns_rdatatype_openpgpkey);
112 	REQUIRE(rdata1->length != 0);
113 	REQUIRE(rdata2->length != 0);
114 
115 	dns_rdata_toregion(rdata1, &r1);
116 	dns_rdata_toregion(rdata2, &r2);
117 	return (isc_region_compare(&r1, &r2));
118 }
119 
120 static inline isc_result_t
121 fromstruct_openpgpkey(ARGS_FROMSTRUCT) {
122 	dns_rdata_openpgpkey_t *sig = source;
123 
124 	REQUIRE(type == dns_rdatatype_openpgpkey);
125 	REQUIRE(sig != NULL);
126 	REQUIRE(sig->common.rdtype == type);
127 	REQUIRE(sig->common.rdclass == rdclass);
128 	REQUIRE(sig->keyring != NULL && sig->length != 0);
129 
130 	UNUSED(type);
131 	UNUSED(rdclass);
132 
133 	/*
134 	 * Keyring.
135 	 */
136 	return (mem_tobuffer(target, sig->keyring, sig->length));
137 }
138 
139 static inline isc_result_t
140 tostruct_openpgpkey(ARGS_TOSTRUCT) {
141 	isc_region_t sr;
142 	dns_rdata_openpgpkey_t *sig = target;
143 
144 	REQUIRE(rdata->type == dns_rdatatype_openpgpkey);
145 	REQUIRE(sig != NULL);
146 	REQUIRE(rdata->length != 0);
147 
148 	sig->common.rdclass = rdata->rdclass;
149 	sig->common.rdtype = rdata->type;
150 	ISC_LINK_INIT(&sig->common, link);
151 
152 	dns_rdata_toregion(rdata, &sr);
153 
154 	/*
155 	 * Keyring.
156 	 */
157 	sig->length = sr.length;
158 	sig->keyring = mem_maybedup(mctx, sr.base, sig->length);
159 	if (sig->keyring == NULL) {
160 		goto cleanup;
161 	}
162 
163 	sig->mctx = mctx;
164 	return (ISC_R_SUCCESS);
165 
166 cleanup:
167 	return (ISC_R_NOMEMORY);
168 }
169 
170 static inline void
171 freestruct_openpgpkey(ARGS_FREESTRUCT) {
172 	dns_rdata_openpgpkey_t *sig = (dns_rdata_openpgpkey_t *)source;
173 
174 	REQUIRE(sig != NULL);
175 	REQUIRE(sig->common.rdtype == dns_rdatatype_openpgpkey);
176 
177 	if (sig->mctx == NULL) {
178 		return;
179 	}
180 
181 	if (sig->keyring != NULL) {
182 		isc_mem_free(sig->mctx, sig->keyring);
183 	}
184 	sig->mctx = NULL;
185 }
186 
187 static inline isc_result_t
188 additionaldata_openpgpkey(ARGS_ADDLDATA) {
189 	REQUIRE(rdata->type == dns_rdatatype_openpgpkey);
190 
191 	UNUSED(rdata);
192 	UNUSED(add);
193 	UNUSED(arg);
194 
195 	return (ISC_R_SUCCESS);
196 }
197 
198 static inline isc_result_t
199 digest_openpgpkey(ARGS_DIGEST) {
200 	isc_region_t r;
201 
202 	REQUIRE(rdata->type == dns_rdatatype_openpgpkey);
203 
204 	dns_rdata_toregion(rdata, &r);
205 
206 	return ((digest)(arg, &r));
207 }
208 
209 static inline bool
210 checkowner_openpgpkey(ARGS_CHECKOWNER) {
211 	REQUIRE(type == dns_rdatatype_openpgpkey);
212 
213 	UNUSED(name);
214 	UNUSED(type);
215 	UNUSED(rdclass);
216 	UNUSED(wildcard);
217 
218 	return (true);
219 }
220 
221 static inline bool
222 checknames_openpgpkey(ARGS_CHECKNAMES) {
223 	REQUIRE(rdata->type == dns_rdatatype_openpgpkey);
224 
225 	UNUSED(rdata);
226 	UNUSED(owner);
227 	UNUSED(bad);
228 
229 	return (true);
230 }
231 
232 static inline int
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 == dns_rdatatype_openpgpkey);
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