/src/pdns/pdns/nsecrecords.cc
Line | Count | Source |
1 | | /* |
2 | | * This file is part of PowerDNS or dnsdist. |
3 | | * Copyright -- PowerDNS.COM B.V. and its contributors |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of version 2 of the GNU General Public License as |
7 | | * published by the Free Software Foundation. |
8 | | * |
9 | | * In addition, for the avoidance of any doubt, permission is granted to |
10 | | * link this program with OpenSSL and to (re)distribute the binaries |
11 | | * produced as the result of such linking. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 | | */ |
22 | | #ifdef HAVE_CONFIG_H |
23 | | #include "config.h" |
24 | | #endif |
25 | | #include "dnsrecords.hh" |
26 | | |
27 | | class NSECBitmapGenerator |
28 | | { |
29 | | public: |
30 | 548 | NSECBitmapGenerator(DNSPacketWriter& pw_): pw(pw_) |
31 | 548 | { |
32 | 548 | memset(res, 0, sizeof(res)); |
33 | 548 | } |
34 | | |
35 | | void set(uint16_t type) |
36 | 1.38k | { |
37 | 1.38k | uint16_t bit = type % 256; |
38 | 1.38k | int window = static_cast<int>(type / 256); |
39 | | |
40 | 1.38k | if (window != oldWindow) { |
41 | 536 | if (oldWindow > -1) { |
42 | 319 | res[0] = static_cast<unsigned char>(oldWindow); |
43 | 319 | res[1] = static_cast<unsigned char>(len); |
44 | 319 | tmp.assign(res, res+len+2); |
45 | 319 | pw.xfrBlob(tmp); |
46 | 319 | } |
47 | 536 | memset(res, 0, sizeof(res)); |
48 | 536 | oldWindow = window; |
49 | 536 | } |
50 | 1.38k | res[2+bit/8] |= 1 << (7-(bit%8)); |
51 | 1.38k | len=1+bit/8; |
52 | 1.38k | } |
53 | | |
54 | | void finish() |
55 | 548 | { |
56 | 548 | res[0] = static_cast<unsigned char>(oldWindow); |
57 | 548 | res[1] = static_cast<unsigned char>(len); |
58 | 548 | if (len) { |
59 | 217 | tmp.assign(res, res+len+2); |
60 | 217 | pw.xfrBlob(tmp); |
61 | 217 | } |
62 | 548 | } |
63 | | |
64 | | private: |
65 | | DNSPacketWriter& pw; |
66 | | /* one byte for the window, |
67 | | one for the length, |
68 | | then the maximum of 32 bytes */ |
69 | | uint8_t res[34]; |
70 | | int oldWindow{-1}; |
71 | | int len{0}; |
72 | | string tmp; |
73 | | }; |
74 | | |
75 | | void NSECBitmap::toPacket(DNSPacketWriter& pw) const |
76 | 548 | { |
77 | 548 | NSECBitmapGenerator nbg(pw); |
78 | 548 | if (d_bitset) { |
79 | 0 | size_t found = 0; |
80 | 0 | size_t l_count = d_bitset->count(); |
81 | 0 | for(size_t idx = 0; idx < nbTypes && found < l_count; ++idx){ |
82 | 0 | if (!d_bitset->test(idx)) { |
83 | 0 | continue; |
84 | 0 | } |
85 | 0 | found++; |
86 | 0 | nbg.set(idx); |
87 | 0 | } |
88 | 0 | } |
89 | 548 | else { |
90 | 1.38k | for (const auto& type : d_set) { |
91 | 1.38k | nbg.set(type); |
92 | 1.38k | } |
93 | 548 | } |
94 | | |
95 | 548 | nbg.finish(); |
96 | 548 | } |
97 | | |
98 | | void NSECBitmap::fromPacket(PacketReader& pr) |
99 | 7.73k | { |
100 | 7.73k | string bitmap; |
101 | 7.73k | pr.xfrBlob(bitmap); |
102 | | |
103 | | // 00 06 20 00 00 00 00 03 -> NS RRSIG NSEC ( 2, 46, 47 ) counts from left |
104 | 7.73k | if (bitmap.empty()) { |
105 | 2.60k | return; |
106 | 2.60k | } |
107 | | |
108 | 5.12k | if (bitmap.size() < 2) { |
109 | 19 | throw MOADNSException("NSEC record with impossibly small bitmap"); |
110 | 19 | } |
111 | | |
112 | 5.10k | size_t position = 0; |
113 | 5.10k | std::optional<uint8_t> lastWindowSeen{std::nullopt}; |
114 | 11.1k | while (position + 1 < bitmap.size()) { |
115 | 7.03k | const auto window = static_cast<uint8_t>(bitmap.at(position++)); |
116 | 7.03k | const auto blen = static_cast<uint8_t>(bitmap.at(position++)); |
117 | | |
118 | | // end if zero padding and ensure packet length |
119 | 7.03k | if (window == 0 && blen == 0) { |
120 | 610 | break; |
121 | 610 | } |
122 | | |
123 | | // rfc4034: "Blocks are present in the NSEC RR RDATA in increasing numerical order." |
124 | 6.42k | if (lastWindowSeen.has_value() && lastWindowSeen.value() >= window) { |
125 | 154 | throw MOADNSException("NSEC record with out of order blocks"); |
126 | 154 | } |
127 | | |
128 | 6.26k | if (blen > 32) { |
129 | 188 | throw MOADNSException("NSEC record with invalid bitmap length"); |
130 | 188 | } |
131 | | |
132 | 6.07k | if (position + blen > bitmap.size()) { |
133 | 45 | throw MOADNSException("NSEC record with bitmap length > packet length"); |
134 | 45 | } |
135 | | |
136 | 6.03k | lastWindowSeen = window; |
137 | | |
138 | 86.6k | for (unsigned int k = 0; k < blen; k++) { |
139 | 80.6k | uint8_t val = bitmap.at(position++); |
140 | 725k | for (int bit = 0; bit < 8 ; ++bit , val >>= 1U) { |
141 | 645k | if ((val & 1U) != 0) { |
142 | 480k | set((7 - bit) + (8 * k) + (256 * window)); |
143 | 480k | } |
144 | 645k | } |
145 | 80.6k | } |
146 | 6.03k | } |
147 | | |
148 | 4.72k | if (position != bitmap.size()) { |
149 | 128 | throw MOADNSException("NSEC record does not use all the record bytes"); |
150 | 128 | } |
151 | | |
152 | 4.72k | } |
153 | | |
154 | | string NSECBitmap::getZoneRepresentation() const |
155 | 1.30k | { |
156 | 1.30k | string ret; |
157 | | |
158 | 1.30k | if (d_bitset) { |
159 | 38 | size_t found = 0; |
160 | 38 | size_t l_count = d_bitset->count(); |
161 | 1.51M | for(size_t idx = 0; idx < nbTypes && found < l_count; ++idx) { |
162 | 1.51M | if (!d_bitset->test(idx)) { |
163 | 1.50M | continue; |
164 | 1.50M | } |
165 | 8.80k | found++; |
166 | | |
167 | 8.80k | ret+=" "; |
168 | 8.80k | ret+=DNSRecordContent::NumberToType(idx); |
169 | 8.80k | } |
170 | 38 | } |
171 | 1.26k | else { |
172 | 6.39k | for(const auto& type : d_set) { |
173 | 6.39k | ret+=" "; |
174 | 6.39k | ret+=DNSRecordContent::NumberToType(type); |
175 | 6.39k | } |
176 | 1.26k | } |
177 | | |
178 | 1.30k | return ret; |
179 | 1.30k | } |
180 | | |
181 | | void NSECRecordContent::report(const ReportIsOnlyCallableByReportAllTypes& /* unused */) |
182 | 4 | { |
183 | 4 | regist(1, 47, &make, &make, "NSEC"); |
184 | 4 | } |
185 | | |
186 | | std::shared_ptr<DNSRecordContent> NSECRecordContent::make(const string& content) |
187 | 519 | { |
188 | 519 | return std::make_shared<NSECRecordContent>(content); |
189 | 519 | } |
190 | | |
191 | | NSECRecordContent::NSECRecordContent(const string& content, const ZoneName& zone) |
192 | 519 | { |
193 | 519 | RecordTextReader rtr(content, zone); |
194 | 519 | rtr.xfrName(d_next); |
195 | | |
196 | 4.05k | while(!rtr.eof()) { |
197 | 3.53k | uint16_t type; |
198 | 3.53k | rtr.xfrType(type); |
199 | 3.53k | set(type); |
200 | 3.53k | } |
201 | 519 | } |
202 | | |
203 | | void NSECRecordContent::toPacket(DNSPacketWriter& pw) const |
204 | 354 | { |
205 | 354 | pw.xfrName(d_next); |
206 | 354 | d_bitmap.toPacket(pw); |
207 | 354 | } |
208 | | |
209 | | std::shared_ptr<NSECRecordContent::DNSRecordContent> NSECRecordContent::make(const DNSRecord & /* dr */, PacketReader& pr) |
210 | 5.36k | { |
211 | 5.36k | auto ret=std::make_shared<NSECRecordContent>(); |
212 | 5.36k | pr.xfrName(ret->d_next); |
213 | | |
214 | 5.36k | ret->d_bitmap.fromPacket(pr); |
215 | 5.36k | if (!pr.eof()) { |
216 | 0 | throw MOADNSException("When parsing NSEC trailing data was not parsed: '" + pr.getRemaining() + "'"); |
217 | 0 | } |
218 | | |
219 | 5.36k | return ret; |
220 | 5.36k | } |
221 | | |
222 | | string NSECRecordContent::getZoneRepresentation(bool /* noDot */) const |
223 | 857 | { |
224 | 857 | string ret; |
225 | 857 | RecordTextWriter rtw(ret); |
226 | 857 | rtw.xfrName(d_next); |
227 | | |
228 | 857 | return ret + d_bitmap.getZoneRepresentation(); |
229 | 857 | } |
230 | | |
231 | | ////// begin of NSEC3 |
232 | | |
233 | | void NSEC3RecordContent::report(const ReportIsOnlyCallableByReportAllTypes& /* unused */) |
234 | 4 | { |
235 | 4 | regist(1, 50, &make, &make, "NSEC3"); |
236 | 4 | } |
237 | | |
238 | | std::shared_ptr<DNSRecordContent> NSEC3RecordContent::make(const string& content) |
239 | 256 | { |
240 | 256 | return std::make_shared<NSEC3RecordContent>(content); |
241 | 256 | } |
242 | | |
243 | | NSEC3RecordContent::NSEC3RecordContent(const string& content, const ZoneName& zone) |
244 | 256 | { |
245 | 256 | RecordTextReader rtr(content, zone); |
246 | 256 | rtr.xfr8BitInt(d_algorithm); |
247 | 256 | rtr.xfr8BitInt(d_flags); |
248 | 256 | rtr.xfr16BitInt(d_iterations); |
249 | | |
250 | 256 | rtr.xfrHexBlob(d_salt); |
251 | 256 | rtr.xfrBase32HexBlob(d_nexthash); |
252 | | |
253 | 1.04k | while(!rtr.eof()) { |
254 | 793 | uint16_t type; |
255 | 793 | rtr.xfrType(type); |
256 | 793 | set(type); |
257 | 793 | } |
258 | 256 | } |
259 | | |
260 | | void NSEC3RecordContent::toPacket(DNSPacketWriter& pw) const |
261 | 173 | { |
262 | 173 | pw.xfr8BitInt(d_algorithm); |
263 | 173 | pw.xfr8BitInt(d_flags); |
264 | 173 | pw.xfr16BitInt(d_iterations); |
265 | 173 | pw.xfr8BitInt(d_salt.length()); |
266 | 173 | pw.xfrBlob(d_salt); |
267 | | |
268 | 173 | pw.xfr8BitInt(d_nexthash.length()); |
269 | 173 | pw.xfrBlob(d_nexthash); |
270 | | |
271 | 173 | d_bitmap.toPacket(pw); |
272 | 173 | } |
273 | | |
274 | | std::shared_ptr<NSEC3RecordContent::DNSRecordContent> NSEC3RecordContent::make(const DNSRecord& /* dr */, PacketReader& pr) |
275 | 1.78k | { |
276 | 1.78k | auto ret=std::make_shared<NSEC3RecordContent>(); |
277 | 1.78k | pr.xfr8BitInt(ret->d_algorithm); |
278 | 1.78k | pr.xfr8BitInt(ret->d_flags); |
279 | 1.78k | pr.xfr16BitInt(ret->d_iterations); |
280 | 1.78k | uint8_t len; |
281 | 1.78k | pr.xfr8BitInt(len); |
282 | 1.78k | pr.xfrBlob(ret->d_salt, len); |
283 | | |
284 | 1.78k | pr.xfr8BitInt(len); |
285 | 1.78k | pr.xfrBlob(ret->d_nexthash, len); |
286 | | |
287 | 1.78k | ret->d_bitmap.fromPacket(pr); |
288 | 1.78k | if (!pr.eof()) { |
289 | 0 | throw MOADNSException("When parsing NSEC3 trailing data was not parsed: '" + pr.getRemaining() + "'"); |
290 | 0 | } |
291 | 1.78k | return ret; |
292 | 1.78k | } |
293 | | |
294 | | string NSEC3RecordContent::getZoneRepresentation(bool /* noDot */) const |
295 | 369 | { |
296 | 369 | string ret; |
297 | 369 | RecordTextWriter rtw(ret); |
298 | 369 | rtw.xfr8BitInt(d_algorithm); |
299 | 369 | rtw.xfr8BitInt(d_flags); |
300 | 369 | rtw.xfr16BitInt(d_iterations); |
301 | | |
302 | 369 | rtw.xfrHexBlob(d_salt); |
303 | 369 | rtw.xfrBase32HexBlob(d_nexthash); |
304 | | |
305 | 369 | return ret + d_bitmap.getZoneRepresentation(); |
306 | 369 | } |
307 | | |
308 | | |
309 | | void NSEC3PARAMRecordContent::report(const ReportIsOnlyCallableByReportAllTypes& /* unused */) |
310 | 4 | { |
311 | 4 | regist(1, 51, &make, &make, "NSEC3PARAM"); |
312 | 4 | regist(254, 51, &make, &make, "NSEC3PARAM"); |
313 | 4 | } |
314 | | |
315 | | std::shared_ptr<DNSRecordContent> NSEC3PARAMRecordContent::make(const string& content) |
316 | 26 | { |
317 | 26 | return std::make_shared<NSEC3PARAMRecordContent>(content); |
318 | 26 | } |
319 | | |
320 | | NSEC3PARAMRecordContent::NSEC3PARAMRecordContent(const string& content, const ZoneName& zone) |
321 | 26 | { |
322 | 26 | RecordTextReader rtr(content, zone); |
323 | 26 | rtr.xfr8BitInt(d_algorithm); |
324 | 26 | rtr.xfr8BitInt(d_flags); |
325 | 26 | rtr.xfr16BitInt(d_iterations); |
326 | 26 | rtr.xfrHexBlob(d_salt); |
327 | 26 | } |
328 | | |
329 | | void NSEC3PARAMRecordContent::toPacket(DNSPacketWriter& pw) const |
330 | 9 | { |
331 | 9 | pw.xfr8BitInt(d_algorithm); |
332 | 9 | pw.xfr8BitInt(d_flags); |
333 | 9 | pw.xfr16BitInt(d_iterations); |
334 | 9 | pw.xfr8BitInt(d_salt.length()); |
335 | | // cerr<<"salt: '"<<makeHexDump(d_salt)<<"', "<<d_salt.length()<<endl; |
336 | 9 | pw.xfrBlob(d_salt); |
337 | 9 | } |
338 | | |
339 | | std::shared_ptr<NSEC3PARAMRecordContent::DNSRecordContent> NSEC3PARAMRecordContent::make(const DNSRecord& /* dr */, PacketReader& pr) |
340 | 795 | { |
341 | 795 | auto ret=std::make_shared<NSEC3PARAMRecordContent>(); |
342 | 795 | pr.xfr8BitInt(ret->d_algorithm); |
343 | 795 | pr.xfr8BitInt(ret->d_flags); |
344 | 795 | pr.xfr16BitInt(ret->d_iterations); |
345 | 795 | uint8_t len; |
346 | 795 | pr.xfr8BitInt(len); |
347 | 795 | pr.xfrHexBlob(ret->d_salt, len); |
348 | 795 | return ret; |
349 | 795 | } |
350 | | |
351 | | string NSEC3PARAMRecordContent::getZoneRepresentation(bool /* noDot */) const |
352 | 41 | { |
353 | 41 | string ret; |
354 | 41 | RecordTextWriter rtw(ret); |
355 | 41 | rtw.xfr8BitInt(d_algorithm); |
356 | 41 | rtw.xfr8BitInt(d_flags); |
357 | 41 | rtw.xfr16BitInt(d_iterations); |
358 | 41 | rtw.xfrHexBlob(d_salt); |
359 | 41 | return ret; |
360 | 41 | } |
361 | | |
362 | | ////// end of NSEC3 |
363 | | |
364 | | ////// begin of CSYNC |
365 | | |
366 | | void CSYNCRecordContent::report(const ReportIsOnlyCallableByReportAllTypes& /* unused */) |
367 | 4 | { |
368 | 4 | regist(1, 62, &make, &make, "CSYNC"); |
369 | 4 | } |
370 | | |
371 | | std::shared_ptr<DNSRecordContent> CSYNCRecordContent::make(const string& content) |
372 | 38 | { |
373 | 38 | return std::make_shared<CSYNCRecordContent>(content); |
374 | 38 | } |
375 | | |
376 | | CSYNCRecordContent::CSYNCRecordContent(const string& content, const ZoneName& zone) |
377 | 38 | { |
378 | 38 | RecordTextReader rtr(content, zone); |
379 | 38 | rtr.xfr32BitInt(d_serial); |
380 | 38 | rtr.xfr16BitInt(d_flags); |
381 | | |
382 | 236 | while(!rtr.eof()) { |
383 | 198 | uint16_t type; |
384 | 198 | rtr.xfrType(type); |
385 | 198 | set(type); |
386 | 198 | } |
387 | 38 | } |
388 | | |
389 | | void CSYNCRecordContent::toPacket(DNSPacketWriter& pw) const |
390 | 32 | { |
391 | 32 | pw.xfr32BitInt(d_serial); |
392 | 32 | pw.xfr16BitInt(d_flags); |
393 | | |
394 | 32 | d_bitmap.toPacket(pw); |
395 | 32 | } |
396 | | |
397 | | std::shared_ptr<CSYNCRecordContent::DNSRecordContent> CSYNCRecordContent::make(const DNSRecord& /* dr */, PacketReader& pr) |
398 | 1.18k | { |
399 | 1.18k | auto ret=std::make_shared<CSYNCRecordContent>(); |
400 | 1.18k | pr.xfr32BitInt(ret->d_serial); |
401 | 1.18k | pr.xfr16BitInt(ret->d_flags); |
402 | | |
403 | 1.18k | ret->d_bitmap.fromPacket(pr); |
404 | 1.18k | if (!pr.eof()) { |
405 | 0 | throw MOADNSException("When parsing CSYNC trailing data was not parsed: '" + pr.getRemaining() + "'"); |
406 | 0 | } |
407 | 1.18k | return ret; |
408 | 1.18k | } |
409 | | |
410 | | string CSYNCRecordContent::getZoneRepresentation(bool /* noDot */) const |
411 | 76 | { |
412 | 76 | string ret; |
413 | 76 | RecordTextWriter rtw(ret); |
414 | 76 | rtw.xfr32BitInt(d_serial); |
415 | 76 | rtw.xfr16BitInt(d_flags); |
416 | | |
417 | 76 | return ret + d_bitmap.getZoneRepresentation(); |
418 | 76 | } |
419 | | |
420 | | ////// end of CSYNC |