/src/bind9/lib/dns/rdata/generic/sshfp_44.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
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 | | /* RFC 4255 */ |
15 | | |
16 | | #ifndef RDATA_GENERIC_SSHFP_44_C |
17 | | #define RDATA_GENERIC_SSHFP_44_C |
18 | | |
19 | 12.5k | #define RRTYPE_SSHFP_ATTRIBUTES (0) |
20 | | |
21 | | static isc_result_t |
22 | 995 | fromtext_sshfp(ARGS_FROMTEXT) { |
23 | 995 | isc_token_t token; |
24 | 995 | int len = -1; |
25 | | |
26 | 995 | REQUIRE(type == dns_rdatatype_sshfp); |
27 | | |
28 | 995 | UNUSED(type); |
29 | 995 | UNUSED(rdclass); |
30 | 995 | UNUSED(origin); |
31 | 995 | UNUSED(options); |
32 | 995 | UNUSED(callbacks); |
33 | | |
34 | | /* |
35 | | * Algorithm. |
36 | | */ |
37 | 995 | RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, |
38 | 995 | false)); |
39 | 990 | if (token.value.as_ulong > 0xffU) { |
40 | 22 | RETTOK(ISC_R_RANGE); |
41 | 22 | } |
42 | 968 | RETERR(uint8_tobuffer(token.value.as_ulong, target)); |
43 | | |
44 | | /* |
45 | | * Digest type. |
46 | | */ |
47 | 968 | RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number, |
48 | 968 | false)); |
49 | 961 | if (token.value.as_ulong > 0xffU) { |
50 | 21 | RETTOK(ISC_R_RANGE); |
51 | 21 | } |
52 | 940 | RETERR(uint8_tobuffer(token.value.as_ulong, target)); |
53 | | |
54 | | /* |
55 | | * Enforce known digest lengths. |
56 | | */ |
57 | 940 | switch (token.value.as_ulong) { |
58 | 5 | case 1: |
59 | 5 | len = ISC_SHA1_DIGESTLENGTH; |
60 | 5 | break; |
61 | 6 | case 2: |
62 | 6 | len = ISC_SHA256_DIGESTLENGTH; |
63 | 6 | break; |
64 | 929 | default: |
65 | 929 | break; |
66 | 940 | } |
67 | | |
68 | | /* |
69 | | * Digest. |
70 | | */ |
71 | 940 | return isc_hex_tobuffer(lexer, target, len); |
72 | 940 | } |
73 | | |
74 | | static isc_result_t |
75 | 2.11k | totext_sshfp(ARGS_TOTEXT) { |
76 | 2.11k | isc_region_t sr; |
77 | 2.11k | char buf[sizeof("64000 ")]; |
78 | 2.11k | unsigned int n; |
79 | | |
80 | 2.11k | REQUIRE(rdata->type == dns_rdatatype_sshfp); |
81 | 2.11k | REQUIRE(rdata->length != 0); |
82 | | |
83 | 2.11k | UNUSED(tctx); |
84 | | |
85 | 2.11k | dns_rdata_toregion(rdata, &sr); |
86 | | |
87 | | /* |
88 | | * Algorithm. |
89 | | */ |
90 | 2.11k | n = uint8_fromregion(&sr); |
91 | 2.11k | isc_region_consume(&sr, 1); |
92 | 2.11k | snprintf(buf, sizeof(buf), "%u ", n); |
93 | 2.11k | RETERR(str_totext(buf, target)); |
94 | | |
95 | | /* |
96 | | * Digest type. |
97 | | */ |
98 | 2.11k | n = uint8_fromregion(&sr); |
99 | 2.11k | isc_region_consume(&sr, 1); |
100 | 2.11k | snprintf(buf, sizeof(buf), "%u", n); |
101 | 2.11k | RETERR(str_totext(buf, target)); |
102 | | |
103 | 2.11k | if (sr.length == 0U) { |
104 | 930 | return ISC_R_SUCCESS; |
105 | 930 | } |
106 | | |
107 | | /* |
108 | | * Digest. |
109 | | */ |
110 | 1.18k | if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) { |
111 | 27 | RETERR(str_totext(" (", target)); |
112 | 27 | } |
113 | 1.18k | RETERR(str_totext(tctx->linebreak, target)); |
114 | 1.18k | if (tctx->width == 0) { /* No splitting */ |
115 | 0 | RETERR(isc_hex_totext(&sr, 0, "", target)); |
116 | 1.18k | } else { |
117 | 1.18k | RETERR(isc_hex_totext(&sr, tctx->width - 2, tctx->linebreak, |
118 | 1.18k | target)); |
119 | 1.18k | } |
120 | 1.18k | if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) { |
121 | 27 | RETERR(str_totext(" )", target)); |
122 | 27 | } |
123 | 1.18k | return ISC_R_SUCCESS; |
124 | 1.18k | } |
125 | | |
126 | | static isc_result_t |
127 | 2.85k | fromwire_sshfp(ARGS_FROMWIRE) { |
128 | 2.85k | isc_region_t sr; |
129 | | |
130 | 2.85k | REQUIRE(type == dns_rdatatype_sshfp); |
131 | | |
132 | 2.85k | UNUSED(type); |
133 | 2.85k | UNUSED(rdclass); |
134 | 2.85k | UNUSED(dctx); |
135 | | |
136 | 2.85k | isc_buffer_activeregion(source, &sr); |
137 | 2.85k | if (sr.length < 2) { |
138 | 10 | return ISC_R_UNEXPECTEDEND; |
139 | 10 | } |
140 | | |
141 | 2.84k | if ((sr.base[1] == 1 && sr.length != ISC_SHA1_DIGESTLENGTH + 2) || |
142 | 2.83k | (sr.base[1] == 2 && sr.length != ISC_SHA256_DIGESTLENGTH + 2)) |
143 | 21 | { |
144 | 21 | return DNS_R_FORMERR; |
145 | 21 | } |
146 | | |
147 | 2.82k | isc_buffer_forward(source, sr.length); |
148 | 2.82k | return mem_tobuffer(target, sr.base, sr.length); |
149 | 2.84k | } |
150 | | |
151 | | static isc_result_t |
152 | 1.07k | towire_sshfp(ARGS_TOWIRE) { |
153 | 1.07k | isc_region_t sr; |
154 | | |
155 | 1.07k | REQUIRE(rdata->type == dns_rdatatype_sshfp); |
156 | 1.07k | REQUIRE(rdata->length != 0); |
157 | | |
158 | 1.07k | UNUSED(cctx); |
159 | | |
160 | 1.07k | dns_rdata_toregion(rdata, &sr); |
161 | 1.07k | return mem_tobuffer(target, sr.base, sr.length); |
162 | 1.07k | } |
163 | | |
164 | | static int |
165 | 1.91k | compare_sshfp(ARGS_COMPARE) { |
166 | 1.91k | isc_region_t r1; |
167 | 1.91k | isc_region_t r2; |
168 | | |
169 | 1.91k | REQUIRE(rdata1->type == rdata2->type); |
170 | 1.91k | REQUIRE(rdata1->rdclass == rdata2->rdclass); |
171 | 1.91k | REQUIRE(rdata1->type == dns_rdatatype_sshfp); |
172 | 1.91k | REQUIRE(rdata1->length != 0); |
173 | 1.91k | REQUIRE(rdata2->length != 0); |
174 | | |
175 | 1.91k | dns_rdata_toregion(rdata1, &r1); |
176 | 1.91k | dns_rdata_toregion(rdata2, &r2); |
177 | 1.91k | return isc_region_compare(&r1, &r2); |
178 | 1.91k | } |
179 | | |
180 | | static isc_result_t |
181 | 0 | fromstruct_sshfp(ARGS_FROMSTRUCT) { |
182 | 0 | dns_rdata_sshfp_t *sshfp = source; |
183 | |
|
184 | 0 | REQUIRE(type == dns_rdatatype_sshfp); |
185 | 0 | REQUIRE(sshfp != NULL); |
186 | 0 | REQUIRE(sshfp->common.rdtype == type); |
187 | 0 | REQUIRE(sshfp->common.rdclass == rdclass); |
188 | |
|
189 | 0 | UNUSED(type); |
190 | 0 | UNUSED(rdclass); |
191 | |
|
192 | 0 | RETERR(uint8_tobuffer(sshfp->algorithm, target)); |
193 | 0 | RETERR(uint8_tobuffer(sshfp->digest_type, target)); |
194 | |
|
195 | 0 | return mem_tobuffer(target, sshfp->digest, sshfp->length); |
196 | 0 | } |
197 | | |
198 | | static isc_result_t |
199 | 0 | tostruct_sshfp(ARGS_TOSTRUCT) { |
200 | 0 | dns_rdata_sshfp_t *sshfp = target; |
201 | 0 | isc_region_t region; |
202 | |
|
203 | 0 | REQUIRE(rdata->type == dns_rdatatype_sshfp); |
204 | 0 | REQUIRE(sshfp != NULL); |
205 | 0 | REQUIRE(rdata->length != 0); |
206 | |
|
207 | 0 | DNS_RDATACOMMON_INIT(sshfp, rdata->type, rdata->rdclass); |
208 | |
|
209 | 0 | dns_rdata_toregion(rdata, ®ion); |
210 | |
|
211 | 0 | sshfp->algorithm = uint8_fromregion(®ion); |
212 | 0 | isc_region_consume(®ion, 1); |
213 | 0 | sshfp->digest_type = uint8_fromregion(®ion); |
214 | 0 | isc_region_consume(®ion, 1); |
215 | 0 | sshfp->length = region.length; |
216 | |
|
217 | 0 | sshfp->digest = region.base; |
218 | 0 | return ISC_R_SUCCESS; |
219 | 0 | } |
220 | | |
221 | | static isc_result_t |
222 | 0 | additionaldata_sshfp(ARGS_ADDLDATA) { |
223 | 0 | REQUIRE(rdata->type == dns_rdatatype_sshfp); |
224 | |
|
225 | 0 | UNUSED(rdata); |
226 | 0 | UNUSED(owner); |
227 | 0 | UNUSED(add); |
228 | 0 | UNUSED(arg); |
229 | |
|
230 | 0 | return ISC_R_SUCCESS; |
231 | 0 | } |
232 | | |
233 | | static isc_result_t |
234 | 0 | digest_sshfp(ARGS_DIGEST) { |
235 | 0 | isc_region_t r; |
236 | |
|
237 | 0 | REQUIRE(rdata->type == dns_rdatatype_sshfp); |
238 | |
|
239 | 0 | dns_rdata_toregion(rdata, &r); |
240 | |
|
241 | 0 | return (digest)(arg, &r); |
242 | 0 | } |
243 | | |
244 | | static bool |
245 | 0 | checkowner_sshfp(ARGS_CHECKOWNER) { |
246 | 0 | REQUIRE(type == dns_rdatatype_sshfp); |
247 | |
|
248 | 0 | UNUSED(name); |
249 | 0 | UNUSED(type); |
250 | 0 | UNUSED(rdclass); |
251 | 0 | UNUSED(wildcard); |
252 | |
|
253 | 0 | return true; |
254 | 0 | } |
255 | | |
256 | | static bool |
257 | 0 | checknames_sshfp(ARGS_CHECKNAMES) { |
258 | 0 | REQUIRE(rdata->type == dns_rdatatype_sshfp); |
259 | |
|
260 | 0 | UNUSED(rdata); |
261 | 0 | UNUSED(owner); |
262 | 0 | UNUSED(bad); |
263 | |
|
264 | 0 | return true; |
265 | 0 | } |
266 | | |
267 | | static int |
268 | 0 | casecompare_sshfp(ARGS_COMPARE) { |
269 | 0 | return compare_sshfp(rdata1, rdata2); |
270 | 0 | } |
271 | | |
272 | | #endif /* RDATA_GENERIC_SSHFP_44_C */ |