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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#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; }
uint16_t g_maxRRSIGsPerRecordToConsider{0};
uint16_t g_maxNSEC3sPerRecordToConsider{0};
uint16_t g_maxDNSKEYsToConsider{0};
uint16_t g_maxDSsToConsider{0};
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& hash, const std::string& beginHash, const std::string& nextHash)
{
return ((beginHash < hash && hash < nextHash) || // no wrap BEGINNING --- HASH -- END
(nextHash > hash && beginHash > nextHash) || // wrap HASH --- END --- BEGINNING
(nextHash < beginHash && beginHash < hash) || // wrap other case END --- BEGINNING --- HASH
(beginHash == nextHash && hash != beginHash)); // "we have only 1 NSEC3 record, LOL!"
}
bool isCoveredByNSEC3Hash(const DNSName& name, const DNSName& beginHash, const DNSName& nextHash)
{
return ((beginHash.canonCompare(name) && name.canonCompare(nextHash)) || // no wrap BEGINNING --- HASH -- END
(name.canonCompare(nextHash) && nextHash.canonCompare(beginHash)) || // wrap HASH --- END --- BEGINNING
(nextHash.canonCompare(beginHash) && beginHash.canonCompare(name)) || // wrap other case END --- BEGINNING --- HASH
(beginHash == nextHash && name != 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);
}
[[nodiscard]] std::string getHashFromNSEC3(const DNSName& qname, uint16_t iterations, const std::string& salt, pdns::validation::ValidationContext& context)
{
std::string result;
if (g_maxNSEC3Iterations != 0 && iterations > g_maxNSEC3Iterations) {
return result;
}
auto key = std::tuple(qname, salt, iterations);
auto iter = context.d_nsec3Cache.find(key);
if (iter != context.d_nsec3Cache.end())
{
return iter->second;
}
if (context.d_nsec3IterationsRemainingQuota < iterations) {
// we throw here because we cannot take the risk that the result
// be cached, since a different query can try to validate the
// same result with a bigger NSEC3 iterations quota
throw pdns::validation::TooManySEC3IterationsException();
}
result = hashQNameWithSalt(salt, iterations, qname);
context.d_nsec3IterationsRemainingQuota -= iterations;
context.d_nsec3Cache[key] = result;
return result;
}
[[nodiscard]] static std::string getHashFromNSEC3(const DNSName& qname, const NSEC3RecordContent& nsec3, pdns::validation::ValidationContext& context)
{
return getHashFromNSEC3(qname, nsec3.d_iterations, nsec3.d_salt, context);
}
/* 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, pdns::validation::ValidationContext& context)
{
uint16_t nsec3sConsidered = 0;
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;
}
if (g_maxNSEC3sPerRecordToConsider > 0 && nsec3sConsidered >= g_maxNSEC3sPerRecordToConsider) {
return false;
}
nsec3sConsidered++;
const string hash = getHashFromNSEC3(zone, *nsec3, context);
if (hash.empty()) {
return false;
}
const string beginHash = fromBase32Hex(record.d_name.getRawLabels()[0]);
if (beginHash == hash) {
return !nsec3->isSet(QType::NS);
}
if (isCoveredByNSEC3Hash(hash, 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)
{
return sign->d_labels < labelCount;
}
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)
{
/* this is a wildcard alright, but it has not been expanded */
return owner.isWildcard() && (labelCount - 1) == sign->d_labels;
}
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 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& validset : validrrsets) {
LOG("Do have: "<<validset.first.first<<"/"<<DNSRecordContent::NumberToType(validset.first.second)<<endl);
if (validset.first.second == QType::NSEC) {
for (const auto& record : validset.second.records) {
LOG("\t"<<record->getZoneRepresentation()<<endl);
auto nsec = std::dynamic_pointer_cast<NSECRecordContent>(record);
if (!nsec) {
continue;
}
DNSName owner = getNSECOwnerName(validset.first.first, validset.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& validset : validrrsets) {
LOG("Do have: "<<validset.first.first<<"/"<<DNSRecordContent::NumberToType(validset.first.second)<<endl);
if (validset.first.second == QType::NSEC) {
for (const auto& records : validset.second.records) {
LOG("\t"<<records->getZoneRepresentation()<<endl);
auto nsec = std::dynamic_pointer_cast<const NSECRecordContent>(records);
if (!nsec) {
continue;
}
const DNSName owner = getNSECOwnerName(validset.first.first, validset.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, pdns::validation::ValidationContext& context)
{
auto wildcard = g_wildcarddnsname + closestEncloser;
LOG("Trying to prove that there is no wildcard for "<<wildcard<<"/"<<QType(qtype)<<endl);
for (const auto& validset : validrrsets) {
LOG("Do have: "<<validset.first.first<<"/"<<DNSRecordContent::NumberToType(validset.first.second)<<endl);
if (validset.first.second == QType::NSEC3) {
for (const auto& records : validset.second.records) {
LOG("\t"<<records->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(records);
if (!nsec3) {
continue;
}
const DNSName signer = getSigner(validset.second.signatures);
if (!validset.first.first.isPartOf(signer)) {
continue;
}
string hash = getHashFromNSEC3(wildcard, *nsec3, context);
if (hash.empty()) {
LOG("Unsupported hash, ignoring"<<endl);
return false;
}
LOG("\tWildcard hash: "<<toBase32Hex(hash)<<endl);
string beginHash=fromBase32Hex(validset.first.first.getRawLabels()[0]);
LOG(":\tNSEC3 hash: "<<toBase32Hex(beginHash)<<" -> "<<toBase32Hex(nsec3->d_nexthash)<<endl);
if (beginHash == hash) {
LOG("\tWildcard hash matches");
if (wildcardExists != nullptr) {
*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, validset.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(hash, 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;
}
if (qtype == QType::DS && signer == name) {
LOG("The NSEC comes from the child zone and cannot be used to deny a DS"<<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;
}
[[nodiscard]] uint64_t getNSEC3DenialProofWorstCaseIterationsCount(uint8_t maxLabels, uint16_t iterations, size_t saltLength)
{
return (iterations + 1 + (saltLength > 0 ? 1 : 0)) * maxLabels;
}
/*
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 if 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, pdns::validation::ValidationContext& context, bool needWildcardProof, unsigned int wildcardLabelsCount) // NOLINT(readability-function-cognitive-complexity): https://github.com/PowerDNS/pdns/issues/12791
{
bool nsec3Seen = false;
if (!needWildcardProof && wildcardLabelsCount == 0) {
throw PDNSException("Invalid wildcard labels count for the validation of a positive answer synthesized from a wildcard");
}
uint8_t numberOfLabelsOfParentZone{std::numeric_limits<uint8_t>::max()};
uint16_t nsec3sConsidered = 0;
for (const auto& validset : validrrsets) {
LOG("Do have: "<<validset.first.first<<"/"<<DNSRecordContent::NumberToType(validset.first.second)<<endl);
if (validset.first.second==QType::NSEC) {
for (const auto& record : validset.second.records) {
LOG("\t"<<record->getZoneRepresentation()<<endl);
if (validset.second.signatures.empty()) {
continue;
}
auto nsec = std::dynamic_pointer_cast<const NSECRecordContent>(record);
if (!nsec) {
continue;
}
const DNSName owner = getNSECOwnerName(validset.first.first, validset.second.signatures);
const DNSName signer = getSigner(validset.second.signatures);
if (!validset.first.first.isPartOf(signer) || !owner.isPartOf(signer) ) {
continue;
}
/* The NSEC is either a delegation one, from the parent zone, and
* must have the NS bit set but not the SOA one, or a regular NSEC
* either at apex (signer == owner) or with the SOA or NS bits clear.
*/
const bool notApex = signer.countLabels() < owner.countLabels();
if (notApex && nsec->isSet(QType::NS) && nsec->isSet(QType::SOA)) {
LOG("However, that NSEC is not at the apex and has both the NS and the SOA bits set!"<<endl);
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) {
if (!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, validset.second.signatures) || isWildcardExpandedOntoItself(owner, validset.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"<< endl);
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;
}
/* 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)<<", "<<validset.first.first<<"?="<<qname<<", "<<nsec->isSet(qtype)<<", next: "<<nsec->d_next<<endl);
}
} else if(validset.first.second==QType::NSEC3) {
for (const auto& record : validset.second.records) {
LOG("\t"<<record->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(record);
if (!nsec3) {
continue;
}
if (validset.second.signatures.empty()) {
continue;
}
const DNSName& hashedOwner = validset.first.first;
const DNSName signer = getSigner(validset.second.signatures);
if (!hashedOwner.isPartOf(signer)) {
LOG("Owner "<<hashedOwner<<" is not part of the signer "<<signer<<", ignoring"<<endl);
continue;
}
numberOfLabelsOfParentZone = std::min(numberOfLabelsOfParentZone, static_cast<uint8_t>(signer.countLabels()));
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;
}
if (g_maxNSEC3sPerRecordToConsider > 0 && nsec3sConsidered >= g_maxNSEC3sPerRecordToConsider) {
LOG("Too many NSEC3s for this record"<<endl);
return dState::NODENIAL;
}
nsec3sConsidered++;
string hash = getHashFromNSEC3(qname, *nsec3, context);
if (hash.empty()) {
LOG("Unsupported hash, ignoring"<<endl);
return dState::INSECURE;
}
nsec3Seen = true;
LOG("\tquery hash: "<<toBase32Hex(hash)<<endl);
string beginHash = fromBase32Hex(hashedOwner.getRawLabels()[0]);
// If the name exists, check if the qtype is denied
if (beginHash == hash) {
/* The NSEC3 is either a delegation one, from the parent zone, and
* must have the NS bit set but not the SOA one, or a regular NSEC3
* either at apex (signer == owner) or with the SOA or NS bits clear.
*/
const bool notApex = signer.countLabels() < qname.countLabels();
if (notApex && nsec3->isSet(QType::NS) && nsec3->isSet(QType::SOA)) {
LOG("However, that NSEC3 is not at the apex and has both the NS and the SOA bits set!"<<endl);
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 (qtype != QType::DS && isNSEC3AncestorDelegation(signer, qname, 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) {
if (!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) {
nsec3sConsidered = 0;
/* 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 && closestEncloser.chopOff() && closestEncloser.countLabels() >= numberOfLabelsOfParentZone) {
for(const auto& validset : validrrsets) {
if(validset.first.second==QType::NSEC3) {
for(const auto& record : validset.second.records) {
LOG("\t"<<record->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<NSEC3RecordContent>(record);
if (!nsec3) {
continue;
}
const DNSName signer = getSigner(validset.second.signatures);
if (!validset.first.first.isPartOf(signer)) {
LOG("Owner "<<validset.first.first<<" is not part of the signer "<<signer<<", ignoring"<<endl);
continue;
}
if (g_maxNSEC3sPerRecordToConsider > 0 && nsec3sConsidered >= g_maxNSEC3sPerRecordToConsider) {
LOG("Too many NSEC3s for this record"<<endl);
return dState::NODENIAL;
}
nsec3sConsidered++;
string hash = getHashFromNSEC3(closestEncloser, *nsec3, context);
if (hash.empty()) {
LOG(": Unsupported hash, ignoring"<<endl);
return dState::INSECURE;
}
string beginHash=fromBase32Hex(validset.first.first.getRawLabels()[0]);
LOG("Comparing "<<toBase32Hex(hash)<<" ("<<closestEncloser<<") against "<<toBase32Hex(beginHash)<<endl);
if (beginHash == hash) {
/* If the closest encloser is a delegation NS we know nothing about the names in the child zone. */
if (isNSEC3AncestorDelegation(signer, validset.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) {
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) {
/* 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));
nsec3sConsidered = 0;
LOG("Looking for a NSEC3 covering the next closer name "<<nextCloser<<endl);
for (const auto& validset : validrrsets) {
if (validset.first.second == QType::NSEC3) {
for (const auto& record : validset.second.records) {
LOG("\t"<<record->getZoneRepresentation()<<endl);
auto nsec3 = std::dynamic_pointer_cast<const NSEC3RecordContent>(record);
if (!nsec3) {
continue;
}
if (g_maxNSEC3sPerRecordToConsider > 0 && nsec3sConsidered >= g_maxNSEC3sPerRecordToConsider) {
LOG("Too many NSEC3s for this record"<<endl);
return dState::NODENIAL;
}
nsec3sConsidered++;
string hash = getHashFromNSEC3(nextCloser, *nsec3, context);
if (hash.empty()) {
LOG("Unsupported hash, ignoring"<<endl);
return dState::INSECURE;
}
const DNSName signer = getSigner(validset.second.signatures);
if (!validset.first.first.isPartOf(signer)) {
LOG("Owner "<<validset.first.first<<" is not part of the signer "<<signer<<", ignoring"<<endl);
continue;
}
string beginHash=fromBase32Hex(validset.first.first.getRawLabels()[0]);
LOG("Comparing "<<toBase32Hex(hash)<<" against "<<toBase32Hex(beginHash)<<" -> "<<toBase32Hex(nsec3->d_nexthash)<<endl);
if (isCoveredByNSEC3Hash(hash, 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="<<validset.first.first<<", us="<<toBase32Hex(hash)<<", 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, context)) {
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;
}
if (wildcardExists) {
return dState::NXQTYPE;
}
return dState::NXDOMAIN;
}
// There were no valid NSEC(3) records
return dState::NODENIAL;
}
bool isRRSIGNotExpired(const time_t now, const std::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 std::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;
}
namespace {
[[nodiscard]] bool checkSignatureInceptionAndExpiry(const DNSName& qname, time_t now, const std::shared_ptr<RRSIGRecordContent>& sig, vState& ede)
{
/* 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)) {
return true;
}
ede = ((sig->d_siginception - g_signatureInceptionSkew) > now) ? vState::BogusSignatureNotYetValid : vState::BogusSignatureExpired;
LOG("Signature is "<<(ede == vState::BogusSignatureNotYetValid ? "not yet valid" : "expired")<<" (inception: "<<sig->d_siginception<<", inception skew: "<<g_signatureInceptionSkew<<", expiration: "<<sig->d_sigexpire<<", now: "<<now<<")"<<endl);
return false;
}
[[nodiscard]] bool checkSignatureWithKey(const DNSName& qname, const std::shared_ptr<RRSIGRecordContent>& sig, const std::shared_ptr<DNSKEYRecordContent>& key, const std::string& msg, vState& ede)
{
bool result = false;
try {
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);
if (!result) {
ede = vState::BogusNoValidRRSIG;
}
}
catch (const std::exception& e) {
LOG("Could not make a validator for signature: "<<e.what()<<endl);
ede = vState::BogusUnsupportedDNSKEYAlgo;
}
return result;
}
}
vState validateWithKeySet(time_t now, const DNSName& name, const sortedRecords_t& toSign, const vector<shared_ptr<RRSIGRecordContent> >& signatures, const skeyset_t& keys, pdns::validation::ValidationContext& context, bool validateAllSigs)
{
bool missingKey = false;
bool isValid = false;
bool allExpired = true;
bool noneIncepted = true;
uint16_t signaturesConsidered = 0;
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;
}
vState ede = vState::Indeterminate;
if (!checkSignatureInceptionAndExpiry(name, now, signature, ede)) {
if (isRRSIGIncepted(now, signature)) {
noneIncepted = false;
}
if (isRRSIGNotExpired(now, signature)) {
allExpired = false;
}
continue;
}
if (g_maxRRSIGsPerRecordToConsider > 0 && signaturesConsidered >= g_maxRRSIGsPerRecordToConsider) {
LOG("We have already considered "<<std::to_string(signaturesConsidered)<<" RRSIG"<<addS(signaturesConsidered)<<" for this record, stopping now"<<endl;);
// possibly going Bogus, the RRSIGs have not been validated so Insecure would be wrong
break;
}
signaturesConsidered++;
context.d_validationsCounter++;
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;);
missingKey = true;
continue;
}
string msg = getMessageForRRSET(name, *signature, toSign, true);
uint16_t dnskeysConsidered = 0;
for (const auto& key : keysMatchingTag) {
if (g_maxDNSKEYsToConsider > 0 && dnskeysConsidered >= g_maxDNSKEYsToConsider) {
LOG("We have already considered "<<std::to_string(dnskeysConsidered)<<" DNSKEY"<<addS(dnskeysConsidered)<<" for tag "<<std::to_string(signature->d_tag)<<" and algorithm "<<std::to_string(signature->d_algorithm)<<", not considering the remaining ones for this signature"<<endl;);
return isValid ? vState::Secure : vState::BogusNoValidRRSIG;
}
dnskeysConsidered++;
bool signIsValid = checkSignatureWithKey(name, signature, key, msg, ede);
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 (missingKey) {
return vState::BogusNoValidRRSIG;
}
if (noneIncepted) {
// ede should be vState::BogusSignatureNotYetValid
return vState::BogusSignatureNotYetValid;
}
if (allExpired) {
// ede should be vState::BogusSignatureExpired);
return vState::BogusSignatureExpired;
}
return vState::BogusNoValidRRSIG;
}
// returns vState
// should return vState, zone cut and validated keyset
// i.e. www.7bits.nl -> insecure/7bits.nl/[]
// www.powerdnssec.org -> secure/powerdnssec.org/[keys]
// www.dnssec-failed.org -> bogus/dnssec-failed.org/[]
cspmap_t harvestCSPFromRecs(const vector<DNSRecord>& recs)
{
cspmap_t cspmap;
for(const auto& rec : recs) {
// cerr<<"res "<<rec.d_name<<"/"<<rec.d_type<<endl;
if (rec.d_type == QType::OPT) {
continue;
}
if(rec.d_type == QType::RRSIG) {
auto rrc = getRR<RRSIGRecordContent>(rec);
if (rrc) {
cspmap[{rec.d_name,rrc->d_type}].signatures.push_back(rrc);
}
}
else {
cspmap[{rec.d_name, rec.d_type}].records.insert(rec.d_content);
}
}
return cspmap;
}
bool getTrustAnchor(const map<DNSName,dsmap_t>& anchors, const DNSName& zone, dsmap_t &res)
{
const auto& iter = anchors.find(zone);
if (iter == anchors.cend()) {
return false;
}
res = iter->second;
return true;
}
bool haveNegativeTrustAnchor(const map<DNSName,std::string>& negAnchors, const DNSName& zone, std::string& reason)
{
const auto& iter = negAnchors.find(zone);
if (iter == negAnchors.cend()) {
return false;
}
reason = iter->second;
return true;
}
vState validateDNSKeysAgainstDS(time_t now, const DNSName& zone, const dsmap_t& dsmap, const skeyset_t& tkeys, const sortedRecords_t& toSign, const vector<shared_ptr<RRSIGRecordContent> >& sigs, skeyset_t& validkeys, pdns::validation::ValidationContext& context)
{
/*
* Check all DNSKEY records against all DS records and place all DNSKEY records
* that have DS records (that we support the algo for) in the tentative key storage
*/
uint16_t dssConsidered = 0;
for (const auto& dsrc : dsmap) {
if (g_maxDSsToConsider > 0 && dssConsidered > g_maxDSsToConsider) {
LOG("We have already considered "<<std::to_string(dssConsidered)<<" DS"<<addS(dssConsidered)<<", not considering the remaining ones"<<endl;);
return vState::BogusNoValidDNSKEY;
}
++dssConsidered;
uint16_t dnskeysConsidered = 0;
auto record = getByTag(tkeys, dsrc.d_tag, dsrc.d_algorithm);
// cerr<<"looking at DS with tag "<<dsrc.d_tag<<", algo "<<DNSSECKeeper::algorithm2name(dsrc.d_algorithm)<<", digest "<<std::to_string(dsrc.d_digesttype)<<" for "<<zone<<", got "<<r.size()<<" DNSKEYs for tag"<<endl;
for (const auto& drc : record) {
bool isValid = false;
bool dsCreated = false;
DSRecordContent dsrc2;
if (g_maxDNSKEYsToConsider > 0 && dnskeysConsidered >= g_maxDNSKEYsToConsider) {
LOG("We have already considered "<<std::to_string(dnskeysConsidered)<<" DNSKEY"<<addS(dnskeysConsidered)<<" for tag "<<std::to_string(dsrc.d_tag)<<" and algorithm "<<std::to_string(dsrc.d_algorithm)<<", not considering the remaining ones for this DS"<<endl;);
// we need to break because we can have a partially validated set
// where the KSK signs the ZSK(s), and even if we don't
// we are going to try to get the correct EDE status (revoked, expired, ...)
break;
}
dnskeysConsidered++;
try {
dsrc2 = makeDSFromDNSKey(zone, *drc, dsrc.d_digesttype);
dsCreated = true;
isValid = dsrc == dsrc2;
}
catch (const std::exception &e) {
LOG("Unable to make DS from DNSKey: "<<e.what()<<endl);
}
if (isValid) {
LOG("got valid DNSKEY (it matches the DS) with tag "<<dsrc.d_tag<<" and algorithm "<<std::to_string(dsrc.d_algorithm)<<" for "<<zone<<endl);
validkeys.insert(drc);
}
else {
if (dsCreated) {
LOG("DNSKEY did not match the DS, parent DS: "<<dsrc.getZoneRepresentation() << " ! = "<<dsrc2.getZoneRepresentation()<<endl);
}
}
}
}
vState ede = vState::BogusNoValidDNSKEY;
// cerr<<"got "<<validkeys.size()<<"/"<<tkeys.size()<<" valid/tentative keys"<<endl;
// these counts could be off if we somehow ended up with
// duplicate keys. Should switch to a type that prevents that.
if (!tkeys.empty() && validkeys.size() < tkeys.size()) {
// this should mean that we have one or more DS-validated DNSKEYs
// but not a fully validated DNSKEY set, yet
// one of these valid DNSKEYs should be able to validate the
// whole set
uint16_t signaturesConsidered = 0;
for (const auto& sig : sigs) {
if (!checkSignatureInceptionAndExpiry(zone, now, sig, ede)) {
continue;
}
// cerr<<"got sig for keytag "<<i->d_tag<<" matching "<<getByTag(tkeys, i->d_tag).size()<<" keys of which "<<getByTag(validkeys, i->d_tag).size()<<" valid"<<endl;
auto bytag = getByTag(validkeys, sig->d_tag, sig->d_algorithm);
if (bytag.empty()) {
continue;
}
if (g_maxRRSIGsPerRecordToConsider > 0 && signaturesConsidered >= g_maxRRSIGsPerRecordToConsider) {
LOG("We have already considered "<<std::to_string(signaturesConsidered)<<" RRSIG"<<addS(signaturesConsidered)<<" for this record, stopping now"<<endl;);
// possibly going Bogus, the RRSIGs have not been validated so Insecure would be wrong
return vState::BogusNoValidDNSKEY;
}
string msg = getMessageForRRSET(zone, *sig, toSign);
uint16_t dnskeysConsidered = 0;
for (const auto& key : bytag) {
if (g_maxDNSKEYsToConsider > 0 && dnskeysConsidered >= g_maxDNSKEYsToConsider) {
LOG("We have already considered "<<std::to_string(dnskeysConsidered)<<" DNSKEY"<<addS(dnskeysConsidered)<<" for tag "<<std::to_string(sig->d_tag)<<" and algorithm "<<std::to_string(sig->d_algorithm)<<", not considering the remaining ones for this signature"<<endl;);
return vState::BogusNoValidDNSKEY;
}
dnskeysConsidered++;
if (g_maxRRSIGsPerRecordToConsider > 0 && signaturesConsidered >= g_maxRRSIGsPerRecordToConsider) {
LOG("We have already considered "<<std::to_string(signaturesConsidered)<<" RRSIG"<<addS(signaturesConsidered)<<" for this record, stopping now"<<endl;);
// possibly going Bogus, the RRSIGs have not been validated so Insecure would be wrong
return vState::BogusNoValidDNSKEY;
}
// cerr<<"validating : ";
bool signIsValid = checkSignatureWithKey(zone, sig, key, msg, ede);
signaturesConsidered++;
context.d_validationsCounter++;
if (signIsValid) {
LOG("validation succeeded - whole DNSKEY set is valid"<<endl);
validkeys = tkeys;
break;
}
LOG("Validation did not succeed!"<<endl);
}
if (validkeys.size() == tkeys.size()) {
// we validated the whole DNSKEY set already */
break;
}
// if(validkeys.empty()) cerr<<"did not manage to validate DNSKEY set based on DS-validated KSK, only passing KSK on"<<endl;
}
}
if (validkeys.size() < tkeys.size()) {
/* so we failed to validate the whole set, let's try to find out why exactly */
bool dnskeyAlgoSupported = false;
bool dsDigestSupported = false;
for (const auto& dsrc : dsmap)
{
if (DNSCryptoKeyEngine::isAlgorithmSupported(dsrc.d_algorithm)) {
dnskeyAlgoSupported = true;
if (DNSCryptoKeyEngine::isDigestSupported(dsrc.d_digesttype)) {
dsDigestSupported = true;
}
}
}
if (!dnskeyAlgoSupported) {
return vState::BogusUnsupportedDNSKEYAlgo;
}
if (!dsDigestSupported) {
return vState::BogusUnsupportedDSDigestType;
}
bool zoneKey = false;
bool notRevoked = false;
bool validProtocol = false;
for (const auto& key : tkeys) {
if (!isAZoneKey(*key)) {
continue;
}
zoneKey = true;
if (isRevokedKey(*key)) {
continue;
}
notRevoked = true;
if (key->d_protocol != 3) {
continue;
}
validProtocol = true;
}
if (!zoneKey) {
return vState::BogusNoZoneKeyBitSet;
}
if (!notRevoked) {
return vState::BogusRevokedDNSKEY;
}
if (!validProtocol) {
return vState::BogusInvalidDNSKEYProtocol;
}
return ede;
}
return vState::Secure;
}
bool isSupportedDS(const DSRecordContent& dsrec)
{
if (!DNSCryptoKeyEngine::isAlgorithmSupported(dsrec.d_algorithm)) {
LOG("Discarding DS "<<dsrec.d_tag<<" because we don't support algorithm number "<<std::to_string(dsrec.d_algorithm)<<endl);
return false;
}
if (!DNSCryptoKeyEngine::isDigestSupported(dsrec.d_digesttype)) {
LOG("Discarding DS "<<dsrec.d_tag<<" because we don't support digest number "<<std::to_string(dsrec.d_digesttype)<<endl);
return false;
}
return true;
}
DNSName getSigner(const std::vector<std::shared_ptr<RRSIGRecordContent> >& signatures)
{
for (const auto& sig : signatures) {
if (sig) {
return sig->d_signer;
}
}
return {};
}
const std::string& vStateToString(vState state)
{
static const std::vector<std::string> vStates = {"Indeterminate", "Insecure", "Secure", "NTA", "TA", "Bogus - No valid DNSKEY", "Bogus - Invalid denial", "Bogus - Unable to get DSs", "Bogus - Unable to get DNSKEYs", "Bogus - Self Signed DS", "Bogus - No RRSIG", "Bogus - No valid RRSIG", "Bogus - Missing negative indication", "Bogus - Signature not yet valid", "Bogus - Signature expired", "Bogus - Unsupported DNSKEY algorithm", "Bogus - Unsupported DS digest type", "Bogus - No zone key bit set", "Bogus - Revoked DNSKEY", "Bogus - Invalid DNSKEY Protocol" };
return vStates.at(static_cast<size_t>(state));
}
std::ostream& operator<<(std::ostream &ostr, const vState dstate)
{
ostr<<vStateToString(dstate);
return ostr;
}
std::ostream& operator<<(std::ostream &ostr, const dState dstate)
{
static const std::vector<std::string> dStates = {"no denial", "inconclusive", "nxdomain", "nxqtype", "empty non-terminal", "insecure", "opt-out"};
ostr<<dStates.at(static_cast<size_t>(dstate));
return ostr;
}
void updateDNSSECValidationState(vState& state, const vState stateUpdate)
{
if (stateUpdate == vState::TA) {
state = vState::Secure;
}
else if (stateUpdate == vState::NTA) {
state = vState::Insecure;
}
else if (vStateIsBogus(stateUpdate) || state == vState::Indeterminate) {
state = stateUpdate;
}
else if (stateUpdate == vState::Insecure) {
if (!vStateIsBogus(state)) {
state = vState::Insecure;
}
}
}
|