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