forked from PowerDNS/pdns
/
validate.cc
1424 lines (1213 loc) · 50.2 KB
/
validate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "validate.hh"
#include "misc.hh"
#include "dnssecinfra.hh"
#include "dnsseckeeper.hh"
#include "rec-lua-conf.hh"
#include "base32.hh"
#include "logger.hh"
bool g_dnssecLOG{false};
time_t g_signatureInceptionSkew{0};
uint16_t g_maxNSEC3Iterations{0};
#define LOG(x) if(g_dnssecLOG) { g_log <<Logger::Warning << x; }
static bool isAZoneKey(const DNSKEYRecordContent& key)
{
/* rfc4034 Section 2.1.1:
"Bit 7 of the Flags field is the Zone Key flag. If bit 7 has value 1,
then the DNSKEY record holds a DNS zone key, and the DNSKEY RR's
owner name MUST be the name of a zone. If bit 7 has value 0, then
the DNSKEY record holds some other type of DNS public key and MUST
NOT be used to verify RRSIGs that cover RRsets."
Let's check that this is a ZONE key, even though there is no other
types of DNSKEYs at the moment.
*/
return (key.d_flags & 256) != 0;
}
static bool isRevokedKey(const DNSKEYRecordContent& key)
{
/* rfc5011 Section 3 */
return (key.d_flags & 128) != 0;
}
static vector<shared_ptr<DNSKEYRecordContent > > getByTag(const skeyset_t& keys, uint16_t tag, uint8_t algorithm)
{
vector<shared_ptr<DNSKEYRecordContent>> ret;
for (const auto& key : keys) {
if (!isAZoneKey(*key)) {
LOG("Key for tag "<<std::to_string(tag)<<" and algorithm "<<std::to_string(algorithm)<<" is not a zone key, skipping"<<endl;);
continue;
}
if (isRevokedKey(*key)) {
LOG("Key for tag "<<std::to_string(tag)<<" and algorithm "<<std::to_string(algorithm)<<" has been revoked, skipping"<<endl;);
continue;
}
if (key->d_protocol == 3 && key->getTag() == tag && key->d_algorithm == algorithm) {
ret.push_back(key);
}
}
return ret;
}
bool isCoveredByNSEC3Hash(const std::string& h, const std::string& beginHash, const std::string& nextHash)
{
return ((beginHash < h && h < nextHash) || // no wrap BEGINNING --- HASH -- END
(nextHash > h && beginHash > nextHash) || // wrap HASH --- END --- BEGINNING
(nextHash < beginHash && beginHash < h) || // wrap other case END --- BEGINNING --- HASH
(beginHash == nextHash && h != beginHash)); // "we have only 1 NSEC3 record, LOL!"
}
bool isCoveredByNSEC3Hash(const DNSName& h, const DNSName& beginHash, const DNSName& nextHash)
{
return ((beginHash.canonCompare(h) && h.canonCompare(nextHash)) || // no wrap BEGINNING --- HASH -- END
(h.canonCompare(nextHash) && nextHash.canonCompare(beginHash)) || // wrap HASH --- END --- BEGINNING
(nextHash.canonCompare(beginHash) && beginHash.canonCompare(h)) || // wrap other case END --- BEGINNING --- HASH
(beginHash == nextHash && h != beginHash)); // "we have only 1 NSEC3 record, LOL!"
}
bool isCoveredByNSEC(const DNSName& name, const DNSName& begin, const DNSName& next)
{
return ((begin.canonCompare(name) && name.canonCompare(next)) || // no wrap BEGINNING --- NAME --- NEXT
(name.canonCompare(next) && next.canonCompare(begin)) || // wrap NAME --- NEXT --- BEGINNING
(next.canonCompare(begin) && begin.canonCompare(name)) || // wrap other case NEXT --- BEGINNING --- NAME
(begin == next && name != begin)); // "we have only 1 NSEC record, LOL!"
}
static bool nsecProvesENT(const DNSName& name, const DNSName& begin, const DNSName& next)
{
/* if name is an ENT:
- begin < name
- next is a child of name
*/
return begin.canonCompare(name) && next != name && next.isPartOf(name);
}
using nsec3HashesCache = std::map<std::tuple<DNSName, std::string, uint16_t>, std::string>;
static std::string getHashFromNSEC3(const DNSName& qname, const std::shared_ptr<NSEC3RecordContent>& nsec3, nsec3HashesCache& cache)
{
std::string result;
if (g_maxNSEC3Iterations && nsec3->d_iterations > g_maxNSEC3Iterations) {
return result;
}
auto key = std::make_tuple(qname, nsec3->d_salt, nsec3->d_iterations);
auto it = cache.find(key);
if (it != cache.end())
{
return it->second;
}
result = hashQNameWithSalt(nsec3->d_salt, nsec3->d_iterations, qname);
cache[key] = result;
return result;
}
/* There is no delegation at this exact point if:
- the name exists but the NS type is not set
- the name does not exist
One exception, if the name is covered by an opt-out NSEC3
it doesn't prove that an insecure delegation doesn't exist.
*/
bool denialProvesNoDelegation(const DNSName& zone, const std::vector<DNSRecord>& dsrecords)
{
nsec3HashesCache cache;
for (const auto& record : dsrecords) {
if (record.d_type == QType::NSEC) {
const auto nsec = getRR<NSECRecordContent>(record);
if (!nsec) {
continue;
}
if (record.d_name == zone) {
return !nsec->isSet(QType::NS);
}
if (isCoveredByNSEC(zone, record.d_name, nsec->d_next)) {
return true;
}
}
else if (record.d_type == QType::NSEC3) {
const auto nsec3 = getRR<NSEC3RecordContent>(record);
if (!nsec3) {
continue;
}
const string h = getHashFromNSEC3(zone, nsec3, cache);
if (h.empty()) {
return false;
}
const string beginHash = fromBase32Hex(record.d_name.getRawLabels()[0]);
if (beginHash == h) {
return !nsec3->isSet(QType::NS);
}
if (isCoveredByNSEC3Hash(h, beginHash, nsec3->d_nexthash)) {
return !(nsec3->isOptOut());
}
}
}
return false;
}
/* RFC 4035 section-5.3.4:
"If the number of labels in an RRset's owner name is greater than the
Labels field of the covering RRSIG RR, then the RRset and its
covering RRSIG RR were created as a result of wildcard expansion."
*/
bool isWildcardExpanded(unsigned int labelCount, const std::shared_ptr<RRSIGRecordContent>& sign)
{
if (sign && sign->d_labels < labelCount) {
return true;
}
return false;
}
static bool isWildcardExpanded(const DNSName& owner, const std::vector<std::shared_ptr<RRSIGRecordContent> >& signatures)
{
if (signatures.empty()) {
return false;
}
const auto& sign = signatures.at(0);
unsigned int labelsCount = owner.countLabels();
return isWildcardExpanded(labelsCount, sign);
}
bool isWildcardExpandedOntoItself(const DNSName& owner, unsigned int labelCount, const std::shared_ptr<RRSIGRecordContent>& sign)
{
if (owner.isWildcard() && (labelCount - 1) == sign->d_labels) {
/* this is a wildcard alright, but it has not been expanded */
return true;
}
return false;
}
static bool isWildcardExpandedOntoItself(const DNSName& owner, const std::vector<std::shared_ptr<RRSIGRecordContent> >& signatures)
{
if (signatures.empty()) {
return false;
}
const auto& sign = signatures.at(0);
unsigned int labelsCount = owner.countLabels();
return isWildcardExpandedOntoItself(owner, labelsCount, sign);
}
/* if this is a wildcard NSEC, the owner name has been modified
to match the name. Make sure we use the original '*' form. */
DNSName getNSECOwnerName(const DNSName& initialOwner, const std::vector<std::shared_ptr<RRSIGRecordContent> >& signatures)
{
DNSName result = initialOwner;
if (signatures.empty()) {
return result;
}
const auto& sign = signatures.at(0);
unsigned int labelsCount = initialOwner.countLabels();
if (sign && sign->d_labels < labelsCount) {
do {
result.chopOff();
labelsCount--;
}
while (sign->d_labels < labelsCount);
result = g_wildcarddnsname + result;
}
return result;
}
static bool isNSECAncestorDelegation(const DNSName& signer, const DNSName& owner, const std::shared_ptr<NSECRecordContent>& nsec)
{
return nsec->isSet(QType::NS) &&
!nsec->isSet(QType::SOA) &&
signer.countLabels() < owner.countLabels();
}
bool isNSEC3AncestorDelegation(const DNSName& signer, const DNSName& owner, const std::shared_ptr<NSEC3RecordContent>& nsec3)
{
return nsec3->isSet(QType::NS) &&
!nsec3->isSet(QType::SOA) &&
signer.countLabels() < owner.countLabels();
}
static bool provesNoDataWildCard(const DNSName& qname, const uint16_t qtype, const DNSName& closestEncloser, const cspmap_t& validrrsets)
{
const DNSName wildcard = g_wildcarddnsname + closestEncloser;
LOG("Trying to prove that there is no data in wildcard for "<<qname<<"/"<<QType(qtype)<<endl);
for (const auto& v : validrrsets) {
LOG("Do have: "<<v.first.first<<"/"<<DNSRecordContent::NumberToType(v.first.second)<<endl);
if (v.first.second == QType::NSEC) {
for (const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
auto nsec = std::dynamic_pointer_cast<NSECRecordContent>(r);
if (!nsec) {
continue;
}
DNSName owner = getNSECOwnerName(v.first.first, v.second.signatures);
if (owner != wildcard) {
continue;
}
LOG("\tWildcard matches");
if (qtype == 0 || isTypeDenied(nsec, QType(qtype))) {
LOG(" and proves that the type did not exist"<<endl);
return true;
}
LOG(" BUT the type did exist!"<<endl);
return false;
}
}
}
return false;
}
DNSName getClosestEncloserFromNSEC(const DNSName& name, const DNSName& owner, const DNSName& next)
{
DNSName commonWithOwner(name.getCommonLabels(owner));
DNSName commonWithNext(name.getCommonLabels(next));
if (commonWithOwner.countLabels() >= commonWithNext.countLabels()) {
return commonWithOwner;
}
return commonWithNext;
}
/*
This function checks whether the non-existence of a wildcard covering qname|qtype
is proven by the NSEC records in validrrsets.
*/
static bool provesNoWildCard(const DNSName& qname, const uint16_t qtype, const DNSName& closestEncloser, const cspmap_t & validrrsets)
{
LOG("Trying to prove that there is no wildcard for "<<qname<<"/"<<QType(qtype)<<endl);
const DNSName wildcard = g_wildcarddnsname + closestEncloser;
for (const auto& v : validrrsets) {
LOG("Do have: "<<v.first.first<<"/"<<DNSRecordContent::NumberToType(v.first.second)<<endl);
if (v.first.second == QType::NSEC) {
for (const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
auto nsec = std::dynamic_pointer_cast<NSECRecordContent>(r);
if (!nsec) {
continue;
}
const DNSName owner = getNSECOwnerName(v.first.first, v.second.signatures);
LOG("Comparing owner: "<<owner<<" with target: "<<wildcard<<endl);
if (qname != owner && qname.isPartOf(owner) && nsec->isSet(QType::DNAME)) {
/* rfc6672 section 5.3.2: DNAME Bit in NSEC Type Map
In any negative response, the NSEC or NSEC3 [RFC5155] record type
bitmap SHOULD be checked to see that there was no DNAME that could
have been applied. If the DNAME bit in the type bitmap is set and
the query name is a subdomain of the closest encloser that is
asserted, then DNAME substitution should have been done, but the
substitution has not been done as specified.
*/
LOG("\tThe qname is a subdomain of the NSEC and the DNAME bit is set"<<endl);
return false;
}
if (wildcard != owner && isCoveredByNSEC(wildcard, owner, nsec->d_next)) {
LOG("\tWildcard is covered"<<endl);
return true;
}
}
}
}
return false;
}
/*
This function checks whether the non-existence of a wildcard covering qname|qtype
is proven by the NSEC3 records in validrrsets.
If `wildcardExists` is not NULL, if will be set to true if a wildcard exists
for this qname but doesn't have this qtype.
*/
static bool provesNSEC3NoWildCard(const DNSName& closestEncloser, uint16_t const qtype, const cspmap_t& validrrsets, bool* wildcardExists, nsec3HashesCache& cache)
{
auto wildcard = g_wildcarddnsname + closestEncloser;
LOG("Trying to prove that there is no wildcard for "<<wildcard<<"/"<<QType(qtype)<<endl);
for (const auto& v : validrrsets) {
LOG("Do have: "<<v.first.first<<"/"<<DNSRecordContent::NumberToType(v.first.second)<<endl);
if (v.first.second == QType::NSEC3) {
for (const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(r);
if (!nsec3) {
continue;
}
const DNSName signer = getSigner(v.second.signatures);
if (!v.first.first.isPartOf(signer)) {
continue;
}
string h = getHashFromNSEC3(wildcard, nsec3, cache);
if (h.empty()) {
return false;
}
LOG("\tWildcard hash: "<<toBase32Hex(h)<<endl);
string beginHash=fromBase32Hex(v.first.first.getRawLabels()[0]);
LOG("\tNSEC3 hash: "<<toBase32Hex(beginHash)<<" -> "<<toBase32Hex(nsec3->d_nexthash)<<endl);
if (beginHash == h) {
LOG("\tWildcard hash matches");
if (wildcardExists) {
*wildcardExists = true;
}
/* RFC 6840 section 4.1 "Clarifications on Nonexistence Proofs":
Ancestor delegation NSEC or NSEC3 RRs MUST NOT be used to assume
nonexistence of any RRs below that zone cut, which include all RRs at
that (original) owner name other than DS RRs, and all RRs below that
owner name regardless of type.
*/
if (qtype != QType::DS && isNSEC3AncestorDelegation(signer, v.first.first, nsec3)) {
/* this is an "ancestor delegation" NSEC3 RR */
LOG(" BUT an ancestor delegation NSEC3 RR can only deny the existence of a DS"<<endl);
return false;
}
if (qtype == 0 || isTypeDenied(nsec3, QType(qtype))) {
LOG(" and proves that the type did not exist"<<endl);
return true;
}
LOG(" BUT the type did exist!"<<endl);
return false;
}
if (isCoveredByNSEC3Hash(h, beginHash, nsec3->d_nexthash)) {
LOG("\tWildcard hash is covered"<<endl);
return true;
}
}
}
}
return false;
}
dState matchesNSEC(const DNSName& name, uint16_t qtype, const DNSName& nsecOwner, const std::shared_ptr<NSECRecordContent>& nsec, const std::vector<std::shared_ptr<RRSIGRecordContent>>& signatures)
{
const DNSName signer = getSigner(signatures);
if (!name.isPartOf(signer) || !nsecOwner.isPartOf(signer)) {
return dState::INCONCLUSIVE;
}
const DNSName owner = getNSECOwnerName(nsecOwner, signatures);
/* RFC 6840 section 4.1 "Clarifications on Nonexistence Proofs":
Ancestor delegation NSEC or NSEC3 RRs MUST NOT be used to assume
nonexistence of any RRs below that zone cut, which include all RRs at
that (original) owner name other than DS RRs, and all RRs below that
owner name regardless of type.
*/
if (name.isPartOf(owner) && isNSECAncestorDelegation(signer, owner, nsec)) {
/* this is an "ancestor delegation" NSEC RR */
if (!(qtype == QType::DS && name == owner)) {
LOG("An ancestor delegation NSEC RR can only deny the existence of a DS"<<endl);
return dState::NODENIAL;
}
}
/* check if the type is denied */
if (name == owner) {
if (!isTypeDenied(nsec, QType(qtype))) {
LOG("Does _not_ deny existence of type "<<QType(qtype)<<endl);
return dState::NODENIAL;
}
LOG("Denies existence of type "<<QType(qtype)<<endl);
return dState::NXQTYPE;
}
if (name.isPartOf(owner) && nsec->isSet(QType::DNAME)) {
/* rfc6672 section 5.3.2: DNAME Bit in NSEC Type Map
In any negative response, the NSEC or NSEC3 [RFC5155] record type
bitmap SHOULD be checked to see that there was no DNAME that could
have been applied. If the DNAME bit in the type bitmap is set and
the query name is a subdomain of the closest encloser that is
asserted, then DNAME substitution should have been done, but the
substitution has not been done as specified.
*/
LOG("The DNAME bit is set and the query name is a subdomain of that NSEC");
return dState::NODENIAL;
}
if (isCoveredByNSEC(name, owner, nsec->d_next)) {
LOG(name<<" is covered by ("<<owner<<" to "<<nsec->d_next<<") ");
if (nsecProvesENT(name, owner, nsec->d_next)) {
LOG("Denies existence of type "<<name<<"/"<<QType(qtype)<<" by proving that "<<name<<" is an ENT"<<endl);
return dState::NXQTYPE;
}
return dState::NXDOMAIN;
}
return dState::INCONCLUSIVE;
}
/*
This function checks whether the existence of qname|qtype is denied by the NSEC and NSEC3
in validrrsets.
- If `referralToUnsigned` is true and qtype is QType::DS, this functions returns NODENIAL
if a NSEC or NSEC3 proves that the name exists but no NS type exists, as specified in RFC 5155 section 8.9.
- If `wantsNoDataProof` is set but a NSEC proves that the whole name does not exist, the function will return
NXQTYPE is the name is proven to be ENT and NXDOMAIN otherwise.
- If `needWildcardProof` is false, the proof that a wildcard covering this qname|qtype is not checked. It is
useful when we have a positive answer synthesized from a wildcard and we only need to prove that the exact
name does not exist.
*/
dState getDenial(const cspmap_t &validrrsets, const DNSName& qname, const uint16_t qtype, bool referralToUnsigned, bool wantsNoDataProof, bool needWildcardProof, unsigned int wildcardLabelsCount)
{
nsec3HashesCache cache;
bool nsec3Seen = false;
if (!needWildcardProof && wildcardLabelsCount == 0) {
throw PDNSException("Invalid wildcard labels count for the validation of a positive answer synthesized from a wildcard");
}
for (const auto& v : validrrsets) {
LOG("Do have: "<<v.first.first<<"/"<<DNSRecordContent::NumberToType(v.first.second)<<endl);
if (v.first.second==QType::NSEC) {
for (const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
if (v.second.signatures.empty()) {
continue;
}
auto nsec = std::dynamic_pointer_cast<NSECRecordContent>(r);
if (!nsec) {
continue;
}
const DNSName owner = getNSECOwnerName(v.first.first, v.second.signatures);
const DNSName signer = getSigner(v.second.signatures);
if (!v.first.first.isPartOf(signer) || !owner.isPartOf(signer) ) {
continue;
}
/* RFC 6840 section 4.1 "Clarifications on Nonexistence Proofs":
Ancestor delegation NSEC or NSEC3 RRs MUST NOT be used to assume
nonexistence of any RRs below that zone cut, which include all RRs at
that (original) owner name other than DS RRs, and all RRs below that
owner name regardless of type.
*/
if (qname.isPartOf(owner) && isNSECAncestorDelegation(signer, owner, nsec)) {
/* this is an "ancestor delegation" NSEC RR */
if (!(qtype == QType::DS && qname == owner)) {
LOG("An ancestor delegation NSEC RR can only deny the existence of a DS"<<endl);
return dState::NODENIAL;
}
}
if (qtype == QType::DS && !qname.isRoot() && signer == qname) {
LOG("A NSEC RR from the child zone cannot deny the existence of a DS"<<endl);
continue;
}
/* check if the type is denied */
if (qname == owner) {
if (!isTypeDenied(nsec, QType(qtype))) {
LOG("Does _not_ deny existence of type "<<QType(qtype)<<endl);
return dState::NODENIAL;
}
LOG("Denies existence of type "<<QType(qtype)<<endl);
/*
* RFC 4035 Section 2.3:
* The bitmap for the NSEC RR at a delegation point requires special
* attention. Bits corresponding to the delegation NS RRset and any
* RRsets for which the parent zone has authoritative data MUST be set
*/
if (referralToUnsigned && qtype == QType::DS && !nsec->isSet(QType::NS)) {
LOG("However, no NS record exists at this level!"<<endl);
return dState::NODENIAL;
}
/* we know that the name exists (but this qtype doesn't) so except
if the answer was generated by a wildcard expansion, no wildcard
could have matched (rfc4035 section 5.4 bullet 1) */
if (needWildcardProof && (!isWildcardExpanded(owner, v.second.signatures) || isWildcardExpandedOntoItself(owner, v.second.signatures))) {
needWildcardProof = false;
}
if (!needWildcardProof) {
return dState::NXQTYPE;
}
DNSName closestEncloser = getClosestEncloserFromNSEC(qname, owner, nsec->d_next);
if (provesNoWildCard(qname, qtype, closestEncloser, validrrsets)) {
return dState::NXQTYPE;
}
LOG("But the existence of a wildcard is not denied for "<<qname<<"/"<<endl);
return dState::NODENIAL;
}
if (qname.isPartOf(owner) && nsec->isSet(QType::DNAME)) {
/* rfc6672 section 5.3.2: DNAME Bit in NSEC Type Map
In any negative response, the NSEC or NSEC3 [RFC5155] record type
bitmap SHOULD be checked to see that there was no DNAME that could
have been applied. If the DNAME bit in the type bitmap is set and
the query name is a subdomain of the closest encloser that is
asserted, then DNAME substitution should have been done, but the
substitution has not been done as specified.
*/
LOG("The DNAME bit is set and the query name is a subdomain of that NSEC");
return dState::NODENIAL;
}
/* check if the whole NAME is denied existing */
if (isCoveredByNSEC(qname, owner, nsec->d_next)) {
LOG(qname<<" is covered by ("<<owner<<" to "<<nsec->d_next<<") ");
if (nsecProvesENT(qname, owner, nsec->d_next)) {
if (wantsNoDataProof) {
/* if the name is an ENT and we received a NODATA answer,
we are fine with a NSEC proving that the name does not exist. */
LOG("Denies existence of type "<<qname<<"/"<<QType(qtype)<<" by proving that "<<qname<<" is an ENT"<<endl);
return dState::NXQTYPE;
}
else {
/* but for a NXDOMAIN proof, this doesn't make sense! */
LOG("but it tries to deny the existence of "<<qname<<" by proving that "<<qname<<" is an ENT, this does not make sense!"<<endl);
return dState::NODENIAL;
}
}
if (!needWildcardProof) {
LOG("and we did not need a wildcard proof"<<endl);
return dState::NXDOMAIN;
}
LOG("but we do need a wildcard proof so ");
DNSName closestEncloser = getClosestEncloserFromNSEC(qname, owner, nsec->d_next);
if (wantsNoDataProof) {
LOG("looking for NODATA proof"<<endl);
if (provesNoDataWildCard(qname, qtype, closestEncloser, validrrsets)) {
return dState::NXQTYPE;
}
}
else {
LOG("looking for NO wildcard proof"<<endl);
if (provesNoWildCard(qname, qtype, closestEncloser, validrrsets)) {
return dState::NXDOMAIN;
}
}
LOG("But the existence of a wildcard is not denied for "<<qname<<"/"<<endl);
return dState::NODENIAL;
}
LOG("Did not deny existence of "<<QType(qtype)<<", "<<v.first.first<<"?="<<qname<<", "<<nsec->isSet(qtype)<<", next: "<<nsec->d_next<<endl);
}
} else if(v.first.second==QType::NSEC3) {
for (const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(r);
if (!nsec3) {
continue;
}
if (v.second.signatures.empty()) {
continue;
}
const DNSName signer = getSigner(v.second.signatures);
if (!v.first.first.isPartOf(signer)) {
LOG("Owner "<<v.first.first<<" is not part of the signer "<<signer<<", ignoring"<<endl);
continue;
}
if (qtype == QType::DS && !qname.isRoot() && signer == qname) {
LOG("A NSEC3 RR from the child zone cannot deny the existence of a DS"<<endl);
continue;
}
string h = getHashFromNSEC3(qname, nsec3, cache);
if (h.empty()) {
LOG("Unsupported hash, ignoring"<<endl);
return dState::INSECURE;
}
nsec3Seen = true;
LOG("\tquery hash: "<<toBase32Hex(h)<<endl);
string beginHash=fromBase32Hex(v.first.first.getRawLabels()[0]);
// If the name exists, check if the qtype is denied
if (beginHash == h) {
/* RFC 6840 section 4.1 "Clarifications on Nonexistence Proofs":
Ancestor delegation NSEC or NSEC3 RRs MUST NOT be used to assume
nonexistence of any RRs below that zone cut, which include all RRs at
that (original) owner name other than DS RRs, and all RRs below that
owner name regardless of type.
*/
if (qtype != QType::DS && isNSEC3AncestorDelegation(signer, v.first.first, nsec3)) {
/* this is an "ancestor delegation" NSEC3 RR */
LOG("An ancestor delegation NSEC3 RR can only deny the existence of a DS"<<endl);
return dState::NODENIAL;
}
if (!isTypeDenied(nsec3, QType(qtype))) {
LOG("Does _not_ deny existence of type "<<QType(qtype)<<" for name "<<qname<<" (not opt-out)."<<endl);
return dState::NODENIAL;
}
LOG("Denies existence of type "<<QType(qtype)<<" for name "<<qname<<" (not opt-out)."<<endl);
/*
* RFC 5155 section 8.9:
* If there is an NSEC3 RR present in the response that matches the
* delegation name, then the validator MUST ensure that the NS bit is
* set and that the DS bit is not set in the Type Bit Maps field of the
* NSEC3 RR.
*/
if (referralToUnsigned && qtype == QType::DS && !nsec3->isSet(QType::NS)) {
LOG("However, no NS record exists at this level!"<<endl);
return dState::NODENIAL;
}
return dState::NXQTYPE;
}
}
}
}
/* if we have no NSEC3 records, we are done */
if (!nsec3Seen) {
return dState::NODENIAL;
}
DNSName closestEncloser(qname);
bool found = false;
if (needWildcardProof) {
/* We now need to look for a NSEC3 covering the closest (provable) encloser
RFC 5155 section-7.2.1
RFC 7129 section-5.5
*/
LOG("Now looking for the closest encloser for "<<qname<<endl);
while (found == false && closestEncloser.chopOff()) {
for(const auto& v : validrrsets) {
if(v.first.second==QType::NSEC3) {
for(const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(r);
if (!nsec3) {
continue;
}
const DNSName signer = getSigner(v.second.signatures);
if (!v.first.first.isPartOf(signer)) {
LOG("Owner "<<v.first.first<<" is not part of the signer "<<signer<<", ignoring"<<endl);
continue;
}
string h = getHashFromNSEC3(closestEncloser, nsec3, cache);
if (h.empty()) {
return dState::INSECURE;
}
string beginHash=fromBase32Hex(v.first.first.getRawLabels()[0]);
LOG("Comparing "<<toBase32Hex(h)<<" ("<<closestEncloser<<") against "<<toBase32Hex(beginHash)<<endl);
if (beginHash == h) {
/* If the closest encloser is a delegation NS we know nothing about the names in the child zone. */
if (isNSEC3AncestorDelegation(signer, v.first.first, nsec3)) {
LOG("An ancestor delegation NSEC3 RR can only deny the existence of a DS"<<endl);
continue;
}
LOG("Closest encloser for "<<qname<<" is "<<closestEncloser<<endl);
found = true;
if (nsec3->isSet(QType::DNAME)) {
/* rfc6672 section 5.3.2: DNAME Bit in NSEC Type Map
In any negative response, the NSEC or NSEC3 [RFC5155] record type
bitmap SHOULD be checked to see that there was no DNAME that could
have been applied. If the DNAME bit in the type bitmap is set and
the query name is a subdomain of the closest encloser that is
asserted, then DNAME substitution should have been done, but the
substitution has not been done as specified.
*/
LOG("\tThe closest encloser NSEC3 has the DNAME bit is set"<<endl);
return dState::NODENIAL;
}
break;
}
}
}
if (found == true) {
break;
}
}
}
}
else {
/* RFC 5155 section-7.2.6:
"It is not necessary to return an NSEC3 RR that matches the closest encloser,
as the existence of this closest encloser is proven by the presence of the
expanded wildcard in the response.
*/
found = true;
unsigned int closestEncloserLabelsCount = closestEncloser.countLabels();
while (wildcardLabelsCount > 0 && closestEncloserLabelsCount > wildcardLabelsCount) {
closestEncloser.chopOff();
closestEncloserLabelsCount--;
}
}
bool nextCloserFound = false;
bool isOptOut = false;
if (found == true) {
/* now that we have found the closest (provable) encloser,
we can construct the next closer (RFC7129 section-5.5) name
and look for a NSEC3 RR covering it */
unsigned int labelIdx = qname.countLabels() - closestEncloser.countLabels();
if (labelIdx >= 1) {
DNSName nextCloser(closestEncloser);
nextCloser.prependRawLabel(qname.getRawLabel(labelIdx - 1));
LOG("Looking for a NSEC3 covering the next closer name "<<nextCloser<<endl);
for(const auto& v : validrrsets) {
if(v.first.second==QType::NSEC3) {
for(const auto& r : v.second.records) {
LOG("\t"<<r->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(r);
if(!nsec3)
continue;
string h = getHashFromNSEC3(nextCloser, nsec3, cache);
if (h.empty()) {
return dState::INSECURE;
}
const DNSName signer = getSigner(v.second.signatures);
if (!v.first.first.isPartOf(signer)) {
LOG("Owner "<<v.first.first<<" is not part of the signer "<<signer<<", ignoring"<<endl);
continue;
}
string beginHash=fromBase32Hex(v.first.first.getRawLabels()[0]);
LOG("Comparing "<<toBase32Hex(h)<<" against "<<toBase32Hex(beginHash)<<" -> "<<toBase32Hex(nsec3->d_nexthash)<<endl);
if (isCoveredByNSEC3Hash(h, beginHash, nsec3->d_nexthash)) {
LOG("Denies existence of name "<<qname<<"/"<<QType(qtype));
nextCloserFound = true;
if (nsec3->isOptOut()) {
LOG(" but is opt-out!");
isOptOut = true;
}
LOG(endl);
break;
}
LOG("Did not cover us ("<<qname<<"), start="<<v.first.first<<", us="<<toBase32Hex(h)<<", end="<<toBase32Hex(nsec3->d_nexthash)<<endl);
}
}
if (nextCloserFound) {
break;
}
}
}
}
if (nextCloserFound) {
bool wildcardExists = false;
/* RFC 7129 section-5.6 */
if (needWildcardProof && !provesNSEC3NoWildCard(closestEncloser, qtype, validrrsets, &wildcardExists, cache)) {
if (!isOptOut) {
LOG("But the existence of a wildcard is not denied for "<<qname<<"/"<<QType(qtype)<<endl);
return dState::NODENIAL;
}
}
if (isOptOut) {
return dState::OPTOUT;
}
else {
if (wildcardExists) {
return dState::NXQTYPE;
}
return dState::NXDOMAIN;
}
}
// There were no valid NSEC(3) records
return dState::NODENIAL;
}
/*
* Finds all the zone-cuts between begin (longest name) and end (shortest name),
* returns them all zone cuts, including end, but (possibly) not begin
*/
static const vector<DNSName> getZoneCuts(const DNSName& begin, const DNSName& end, DNSRecordOracle& dro)
{
vector<DNSName> ret;
if(!begin.isPartOf(end))
throw PDNSException(end.toLogString() + "is not part of " + begin.toLogString());
DNSName qname(end);
vector<string> labelsToAdd = begin.makeRelative(end).getRawLabels();
// The shortest name is assumed to a zone cut
ret.push_back(qname);
while(qname != begin) {
bool foundCut = false;
if (labelsToAdd.empty())
break;
qname.prependRawLabel(labelsToAdd.back());
labelsToAdd.pop_back();
auto records = dro.get(qname, (uint16_t)QType::NS);
for (const auto& record : records) {
if(record.d_type != QType::NS || record.d_name != qname)
continue;
foundCut = true;
break;
}
if (foundCut)
ret.push_back(qname);
}
return ret;
}
bool isRRSIGNotExpired(const time_t now, const shared_ptr<RRSIGRecordContent>& sig)
{
// Should use https://www.rfc-editor.org/rfc/rfc4034.txt section 3.1.5
return sig->d_sigexpire >= now;
}
bool isRRSIGIncepted(const time_t now, const shared_ptr<RRSIGRecordContent>& sig)
{
// Should use https://www.rfc-editor.org/rfc/rfc4034.txt section 3.1.5
return sig->d_siginception - g_signatureInceptionSkew <= now;
}
static bool checkSignatureWithKey(time_t now, const shared_ptr<RRSIGRecordContent> sig, const shared_ptr<DNSKEYRecordContent> key, const std::string& msg)
{
bool result = false;
try {
/* rfc4035:
- The validator's notion of the current time MUST be less than or equal to the time listed in the RRSIG RR's Expiration field.
- The validator's notion of the current time MUST be greater than or equal to the time listed in the RRSIG RR's Inception field.
*/
if (isRRSIGIncepted(now, sig) && isRRSIGNotExpired(now, sig)) {
auto dke = DNSCryptoKeyEngine::makeFromPublicKeyString(key->d_algorithm, key->d_key);
result = dke->verify(msg, sig->d_signature);
LOG("signature by key with tag "<<sig->d_tag<<" and algorithm "<<DNSSECKeeper::algorithm2name(sig->d_algorithm)<<" was " << (result ? "" : "NOT ")<<"valid"<<endl);
}
else {
LOG("Signature is "<<((sig->d_siginception - g_signatureInceptionSkew > now) ? "not yet valid" : "expired")<<" (inception: "<<sig->d_siginception<<", inception skew: "<<g_signatureInceptionSkew<<", expiration: "<<sig->d_sigexpire<<", now: "<<now<<")"<<endl);
}
}
catch (const std::exception& e) {
LOG("Could not make a validator for signature: "<<e.what()<<endl);
}
return result;
}
vState validateWithKeySet(time_t now, const DNSName& name, const sortedRecords_t& toSign, const vector<shared_ptr<RRSIGRecordContent> >& signatures, const skeyset_t& keys, bool validateAllSigs)
{
bool foundKey = false;
bool isValid = false;
bool allExpired = true;
bool noneIncepted = true;
for(const auto& signature : signatures) {
unsigned int labelCount = name.countLabels();
if (signature->d_labels > labelCount) {
LOG(name<<": Discarding invalid RRSIG whose label count is "<<signature->d_labels<<" while the RRset owner name has only "<<labelCount<<endl);
continue;
}
auto keysMatchingTag = getByTag(keys, signature->d_tag, signature->d_algorithm);
if (keysMatchingTag.empty()) {
LOG("No key provided for "<<signature->d_tag<<" and algorithm "<<std::to_string(signature->d_algorithm)<<endl;);
continue;
}
string msg = getMessageForRRSET(name, *signature, toSign, true);
for (const auto& key : keysMatchingTag) {
bool signIsValid = checkSignatureWithKey(now, signature, key, msg);
foundKey = true;
if (signIsValid) {
isValid = true;
LOG("Validated "<<name<<"/"<<DNSRecordContent::NumberToType(signature->d_type)<<endl);
// cerr<<"valid"<<endl;
// cerr<<"! validated "<<i->first.first<<"/"<<)<<endl;
}
else {
LOG("signature invalid"<<endl);
if (isRRSIGIncepted(now, signature)) {
noneIncepted = false;
}
if (isRRSIGNotExpired(now, signature)) {
allExpired = false;
}
}
if (signIsValid && !validateAllSigs) {
return vState::Secure;
}
}
}
if (isValid) {
return vState::Secure;
}
if (!foundKey) {
return vState::BogusNoValidRRSIG;
}
if (noneIncepted) {
return vState::BogusSignatureNotYetValid;
}
if (allExpired) {
return vState::BogusSignatureExpired;
}