1 /***************************** -*- Java -*- ********************************\
3 * Copyright (c) 2009 VeriSign, Inc. All rights reserved. *
5 * This software is provided solely in connection with the terms of the *
6 * license agreement. Any other use without the prior express written *
7 * permission of VeriSign is completely prohibited. The software and *
8 * documentation are "Commercial Items", as that term is defined in 48 *
9 * C.F.R. section 2.101, consisting of "Commercial Computer Software" and *
10 * "Commercial Computer Software Documentation" as such terms are defined *
11 * in 48 C.F.R. section 252.227-7014(a)(5) and 48 C.F.R. section *
12 * 252.227-7014(a)(1), and used in 48 C.F.R. section 12.212 and 48 C.F.R. *
13 * section 227.7202, as applicable. Pursuant to the above and other *
14 * relevant sections of the Code of Federal Regulations, as applicable, *
15 * VeriSign's publications, commercial computer software, and commercial *
16 * computer software documentation are distributed and licensed to United *
17 * States Government end users with only those rights as granted to all *
18 * other end users, according to the terms and conditions contained in the *
19 * license agreement(s) that accompany the products and software *
22 \***************************************************************************/
24 package com.verisign.tat.dnssec;
26 import com.verisign.tat.dnssec.SignUtils.ByteArrayComparator;
28 import org.apache.log4j.Logger;
30 import org.xbill.DNS.*;
31 import org.xbill.DNS.utils.base32;
33 import java.security.NoSuchAlgorithmException;
37 public class NSEC3ValUtils {
38 // FIXME: should probably refactor to handle different NSEC3
39 // parameters more efficiently.
40 // Given a list of NSEC3 RRs, they should be grouped according to
41 // parameters. The idea is to hash and compare for each group
42 // independently, instead of having to skip NSEC3 RRs with the
44 private static Name asterisk_label = Name.fromConstantString("*");
45 private static Logger st_log = Logger.getLogger(NSEC3ValUtils.class);
46 private static final base32 b32 = new base32(base32.Alphabet.BASE32HEX,
49 public static boolean supportsHashAlgorithm(int alg) {
50 if (alg == NSEC3Record.SHA1_DIGEST_ID) {
57 public static void stripUnknownAlgNSEC3s(List<NSEC3Record> nsec3s) {
62 for (ListIterator<NSEC3Record> i = nsec3s.listIterator(); i.hasNext();) {
63 NSEC3Record nsec3 = i.next();
65 if (!supportsHashAlgorithm(nsec3.getHashAlgorithm())) {
71 public static boolean isOptOut(NSEC3Record nsec3) {
72 return (nsec3.getFlags() & NSEC3Record.Flags.OPT_OUT) == NSEC3Record.Flags.OPT_OUT;
76 * Given a list of NSEC3Records that are part of a message,
77 * determine the NSEC3 parameters (hash algorithm, iterations, and
78 * salt) present. If there is more than one distinct grouping,
82 * A list of NSEC3Record object.
83 * @return A set containing a number of objects (NSEC3Parameter
84 * objects) that correspond to each distinct set of
85 * parameters, or null if the nsec3s list was empty.
87 public static NSEC3Parameters nsec3Parameters(List<NSEC3Record> nsec3s) {
88 if ((nsec3s == null) || (nsec3s.size() == 0)) {
92 NSEC3Parameters params = new NSEC3Parameters((NSEC3Record) nsec3s.get(0));
93 ByteArrayComparator bac = new ByteArrayComparator();
95 for (NSEC3Record nsec3 : nsec3s) {
96 if (!params.match(nsec3, bac)) {
105 * Given a hash and an a zone name, construct an NSEC3 ownername.
108 * The hash of an original name.
110 * The zone to use in constructing the NSEC3 name.
111 * @return The NSEC3 name.
113 private static Name hashName(byte[] hash, Name zonename) {
115 return new Name(b32.toString(hash).toLowerCase(), zonename);
116 } catch (TextParseException e) {
117 // Note, this should never happen.
123 * Given a set of NSEC3 parameters, hash a name.
128 * The parameters to hash with.
131 private static byte[] hash(Name name, NSEC3Parameters params) {
133 return params.hash(name);
134 } catch (NoSuchAlgorithmException e) {
135 st_log.warn("Did not recognize hash algorithm: " + params.alg);
141 * Given the name of a closest encloser, return the name *.closest_encloser.
143 * @param closestEncloser
144 * The name to start with.
145 * @return The wildcard name.
147 private static Name ceWildcard(Name closestEncloser) {
149 Name wc = Name.concatenate(asterisk_label, closestEncloser);
152 } catch (NameTooLongException e) {
158 * Given a qname and its proven closest encloser, calculate the
159 * "next closest" name. Basically, this is the name that is one
160 * label longer than the closest encloser that is still a
161 * subdomain of qname.
165 * @param closestEncloser
166 * The closest encloser name.
167 * @return The next closer name.
169 private static Name nextClosest(Name qname, Name closestEncloser) {
170 int strip = qname.labels() - closestEncloser.labels() - 1;
172 return (strip > 0) ? new Name(qname, strip) : qname;
176 * Find the NSEC3Record that matches a hash of a name.
179 * The pre-calculated hash of a name.
181 * The name of the zone that the NSEC3s are from.
183 * A list of NSEC3Records from a given message.
185 * The parameters used for calculating the hash.
187 * An already allocated ByteArrayComparator, for reuse. This may
190 * @return The matching NSEC3Record, if one is present.
192 private static NSEC3Record findMatchingNSEC3(byte[] hash,
194 List<NSEC3Record> nsec3s,
195 NSEC3Parameters params,
196 ByteArrayComparator bac) {
197 Name n = hashName(hash, zonename);
199 for (NSEC3Record nsec3 : nsec3s) {
200 // Skip nsec3 records that are using different parameters.
201 if (!params.match(nsec3, bac)) {
205 if (n.equals(nsec3.getName())) {
214 * Given a hash and a candidate NSEC3Record, determine if that
215 * NSEC3Record covers the hash. Covers specifically means that the
216 * hash is in between the owner and next hashes and does not equal
220 * The candidate NSEC3Record.
222 * The precalculated hash.
224 * An already allocated comparator. This may be null.
225 * @return True if the NSEC3Record covers the hash.
227 private static boolean nsec3Covers(NSEC3Record nsec3,
229 ByteArrayComparator bac) {
230 Name ownerName = nsec3.getName();
231 byte[] owner = b32.fromString(ownerName.getLabelString(0));
232 byte[] next = nsec3.getNext();
234 // This is the "normal case: owner < next and owner < hash < next
235 if ((bac.compare(owner, hash) < 0) && (bac.compare(hash, next) < 0)) {
238 // this is the end of zone case: next < owner && hash > owner || hash <
240 if ((bac.compare(next, owner) <= 0) &&
241 ((bac.compare(hash, next) < 0) || (bac.compare(owner, hash) < 0))) {
245 // Otherwise, the NSEC3 does not cover the hash.
250 * Given a pre-hashed name, find a covering NSEC3 from among a
254 * The hash to consider.
256 * The name of the zone.
258 * The list of NSEC3s present in a message.
260 * The NSEC3 parameters used to generate the hash -- NSEC3s that
261 * do not use those parameters will be skipped.
263 * @return A covering NSEC3 if one is present, null otherwise.
265 private static NSEC3Record findCoveringNSEC3(byte[] hash,
267 List<NSEC3Record> nsec3s,
268 NSEC3Parameters params,
269 ByteArrayComparator bac) {
270 ByteArrayComparator comparator = new ByteArrayComparator();
272 for (NSEC3Record nsec3 : nsec3s) {
273 if (!params.match(nsec3, bac)) {
277 if (nsec3Covers(nsec3, hash, comparator)) {
286 * Given a name and a list of NSEC3s, find the candidate closest
287 * encloser. This will be the first ancestor of 'name' (including
288 * itself) to have a matching NSEC3 RR.
291 * The name the start with.
293 * The name of the zone that the NSEC3s came from.
295 * The list of NSEC3s.
297 * The NSEC3 parameters.
299 * A pre-allocated comparator. May be null.
301 * @return A CEResponse containing the closest encloser name and the NSEC3
302 * RR that matched it, or null if there wasn't one.
304 private static CEResponse findClosestEncloser(Name name,
306 List<NSEC3Record> nsec3s,
307 NSEC3Parameters params,
308 ByteArrayComparator bac) {
313 // This scans from longest name to shortest, so the first match we find
314 // is the only viable candidate.
315 // FIXME: modify so that the NSEC3 matching the zone apex need not be
317 while (n.labels() >= zonename.labels()) {
318 nsec3 = findMatchingNSEC3(hash(n, params), zonename, nsec3s, params, bac);
321 return new CEResponse(n, nsec3);
331 * Given a List of nsec3 RRs, find and prove the closest encloser
335 * The qname in question.
337 * The name of the zone that the NSEC3 RRs come from.
339 * The list of NSEC3s found the this response (already
342 * The NSEC3 parameters found in the response.
344 * A pre-allocated comparator. May be null.
345 * @param proveDoesNotExist
346 * If true, then if the closest encloser turns out to
347 * be qname, then null is returned.
348 * @return null if the proof isn't completed. Otherwise, return a
349 * CEResponse object which contains the closest encloser
350 * name and the NSEC3 that matches it.
352 private static CEResponse proveClosestEncloser(Name qname,
354 List<NSEC3Record> nsec3s,
355 NSEC3Parameters params,
356 ByteArrayComparator bac,
357 boolean proveDoesNotExist,
358 List<String> errorList) {
359 CEResponse candidate = findClosestEncloser(qname, zonename, nsec3s, params, bac);
361 if (candidate == null) {
362 errorList.add("Could not find a candidate for the closest encloser");
363 st_log.debug("proveClosestEncloser: could not find a " +
364 "candidate for the closest encloser.");
369 if (candidate.closestEncloser.equals(qname)) {
370 if (proveDoesNotExist) {
371 errorList.add("Proven closest encloser proved that the qname existed and should not have");
372 st_log.debug("proveClosestEncloser: proved that qname existed!");
377 // otherwise, we need to nothing else to prove that qname
378 // is its own closest encloser.
382 // If the closest encloser is actually a delegation, then the
383 // response should have been a referral. If it is a DNAME,
384 // then it should have been a DNAME response.
385 if (candidate.ce_nsec3.hasType(Type.NS) && !candidate.ce_nsec3.hasType(Type.SOA)) {
386 errorList.add("Proven closest encloser was a delegation");
387 st_log.debug("proveClosestEncloser: closest encloser " +
388 "was a delegation!");
393 if (candidate.ce_nsec3.hasType(Type.DNAME)) {
394 errorList.add("Proven closest encloser was a DNAME");
395 st_log.debug("proveClosestEncloser: closest encloser was a DNAME!");
400 // Otherwise, we need to show that the next closer name is covered.
401 Name nextClosest = nextClosest(qname, candidate.closestEncloser);
403 byte[] nc_hash = hash(nextClosest, params);
404 candidate.nc_nsec3 = findCoveringNSEC3(nc_hash, zonename, nsec3s, params, bac);
406 if (candidate.nc_nsec3 == null) {
407 errorList.add("Could not find proof that the closest encloser was the closest encloser");
408 errorList.add("hash " + hashName(nc_hash, zonename) + " is not covered by any NSEC3 RRs");
409 st_log.debug("Could not find proof that the " +
410 "closest encloser was the closest encloser");
418 private static int maxIterations(int baseAlg, int keysize) {
420 case DnsSecVerifier.RSA:
423 return 2500; // the max at 4096
426 if (keysize > 2048) {
430 if (keysize > 1024) {
440 case DnsSecVerifier.DSA:
443 return 5000; // the max at 2048;
446 if (keysize > 1024) {
460 @SuppressWarnings("rawtypes")
461 private static boolean validIterations(NSEC3Parameters nsec3params,
463 DnsSecVerifier verifier) {
464 // for now, we return the maximum iterations based simply on
465 // the key algorithms that may have been used to sign the
467 int max_iterations = 0;
469 for (Iterator i = dnskey_rrset.rrs(); i.hasNext();) {
470 DNSKEYRecord dnskey = (DNSKEYRecord) i.next();
471 int baseAlg = verifier.baseAlgorithm(dnskey.getAlgorithm());
472 int iters = maxIterations(baseAlg, 0);
473 max_iterations = (max_iterations < iters) ? iters : max_iterations;
476 if (nsec3params.iterations > max_iterations) {
484 * Determine if all of the NSEC3s in a response are legally
485 * ignoreable (i.e., their presence should lead to an INSECURE
486 * result). Currently, this is solely based on iterations.
489 * The list of NSEC3s. If there is more than one set of
490 * NSEC3 parameters present, this test will not be
492 * @param dnskey_rrset
493 * The set of validating DNSKEYs.
495 * The verifier used to verify the NSEC3 RRsets. This
496 * is solely used to map algorithm aliases.
497 * @return true if all of the NSEC3s can be legally ignored, false
500 public static boolean allNSEC3sIgnoreable(List<NSEC3Record> nsec3s,
502 DnsSecVerifier verifier) {
503 NSEC3Parameters params = nsec3Parameters(nsec3s);
505 if (params == null) {
509 return !validIterations(params, dnskey_rrset, verifier);
513 * Determine if the set of NSEC3 records provided with a response
514 * prove NAME ERROR. This means that the NSEC3s prove a) the
515 * closest encloser exists, b) the direct child of the closest
516 * encloser towards qname doesn't exist, and c) *.closest encloser
520 * The list of NSEC3s.
522 * The query name to check against.
524 * This is the name of the zone that the NSEC3s belong
525 * to. This may be discovered in any number of ways. A
526 * good one is to use the signerName from the NSEC3
528 * @return SecurityStatus.SECURE of the Name Error is proven by
529 * the NSEC3 RRs, BOGUS if not, INSECURE if all of the
530 * NSEC3s could be validly ignored.
532 public static boolean proveNameError(List<NSEC3Record> nsec3s,
535 List<String> errorList) {
536 if ((nsec3s == null) || (nsec3s.size() == 0)) {
540 NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
542 if (nsec3params == null) {
543 errorList.add("Could not find a single set of NSEC3 parameters (multiple parameters present");
544 st_log.debug("Could not find a single set of " +
545 "NSEC3 parameters (multiple parameters present).");
550 ByteArrayComparator bac = new ByteArrayComparator();
552 // First locate and prove the closest encloser to qname. We will use the
553 // variant that fails if the closest encloser turns out to be qname.
554 CEResponse ce = proveClosestEncloser(qname, zonename, nsec3s,
555 nsec3params, bac, true, errorList);
558 errorList.add("Failed to find the closest encloser as part of the NSEC3 proof");
559 st_log.debug("proveNameError: failed to prove a closest encloser.");
564 // At this point, we know that qname does not exist. Now we need to
566 // that the wildcard does not exist.
567 Name wc = ceWildcard(ce.closestEncloser);
568 byte[] wc_hash = hash(wc, nsec3params);
569 NSEC3Record nsec3 = findCoveringNSEC3(wc_hash, zonename, nsec3s,
573 errorList.add("Failed to prove that the applicable wildcard did not exist");
574 st_log.debug("proveNameError: could not prove that the " +
575 "applicable wildcard did not exist.");
584 * Determine if the NSEC3s provided in a response prove the
585 * NOERROR/NODATA status. There are a number of different variants
588 * 1) Normal NODATA -- qname is matched to an NSEC3 record, type is not
591 * 2) ENT NODATA -- because there must be NSEC3 record for
592 * empty-non-terminals, this is the same as #1.
594 * 3) NSEC3 ownername NODATA -- qname matched an existing, lone NSEC3
595 * ownername, but qtype was not NSEC3. NOTE: as of nsec-05, this case no
598 * 4) Wildcard NODATA -- A wildcard matched the name, but not the type.
600 * 5) Opt-In DS NODATA -- the qname is covered by an opt-in span and qtype
601 * == DS. (or maybe some future record with the same parent-side-only
605 * The NSEC3Records to consider.
607 * The qname in question.
609 * The qtype in question.
611 * The name of the zone that the NSEC3s came from.
612 * @return true if the NSEC3s prove the proposition.
614 public static boolean proveNodata(List<NSEC3Record> nsec3s,
618 List<String> errorList) {
619 if ((nsec3s == null) || (nsec3s.size() == 0)) {
623 NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
625 if (nsec3params == null) {
626 st_log.debug("could not find a single set of " +
627 "NSEC3 parameters (multiple parameters present)");
632 ByteArrayComparator bac = new ByteArrayComparator();
634 NSEC3Record nsec3 = findMatchingNSEC3(hash(qname, nsec3params),
635 zonename, nsec3s, nsec3params, bac);
639 if (nsec3.hasType(qtype)) {
640 st_log.debug("proveNodata: Matching NSEC3 proved that type existed!");
645 if (nsec3.hasType(Type.CNAME)) {
646 st_log.debug("proveNodata: Matching NSEC3 proved " +
647 "that a CNAME existed!");
655 // For cases 3 - 5, we need the proven closest encloser, and it can't
656 // match qname. Although, at this point, we know that it won't since we
657 // just checked that.
658 CEResponse ce = proveClosestEncloser(qname, zonename, nsec3s,
659 nsec3params, bac, true, errorList);
661 // At this point, not finding a match or a proven closest encloser is a
664 st_log.debug("proveNodata: did not match qname, " +
665 "nor found a proven closest encloser.");
673 Name wc = ceWildcard(ce.closestEncloser);
674 nsec3 = findMatchingNSEC3(hash(wc, nsec3params), zonename, nsec3s,
678 if (nsec3.hasType(qtype)) {
679 st_log.debug("proveNodata: matching wildcard had qtype!");
688 if (qtype != Type.DS) {
689 st_log.debug("proveNodata: could not find matching NSEC3, " +
690 "nor matching wildcard, and qtype is not DS -- no more options.");
695 // We need to make sure that the covering NSEC3 is opt-in.
696 if (!isOptOut(ce.nc_nsec3)) {
697 st_log.debug("proveNodata: covering NSEC3 was not " +
698 "opt-in in an opt-in DS NOERROR/NODATA case.");
707 * Prove that a positive wildcard match was appropriate (no direct
711 * The NSEC3 records to work with.
713 * The qname that was matched to the wildcard
715 * The name of the zone that the NSEC3s come from.
717 * The purported wildcard that matched.
718 * @return true if the NSEC3 records prove this case.
720 public static boolean proveWildcard(List<NSEC3Record> nsec3s,
724 List<String> errorList) {
725 if ((nsec3s == null) || (nsec3s.size() == 0)) {
729 if ((qname == null) || (wildcard == null)) {
733 NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
735 if (nsec3params == null) {
736 errorList.add("Could not find a single set of NSEC3 parameters (multiple parameters present)");
737 st_log.debug("Couldn't find a single set of NSEC3 parameters (multiple parameters present).");
742 ByteArrayComparator bac = new ByteArrayComparator();
744 // We know what the (purported) closest encloser is by just looking at
746 // supposed generating wildcard.
747 CEResponse candidate = new CEResponse(new Name(wildcard, 1), null);
749 // Now we still need to prove that the original data did not exist.
750 // Otherwise, we need to show that the next closer name is covered.
751 Name nextClosest = nextClosest(qname, candidate.closestEncloser);
752 candidate.nc_nsec3 = findCoveringNSEC3(hash(nextClosest, nsec3params),
753 zonename, nsec3s, nsec3params, bac);
755 if (candidate.nc_nsec3 == null) {
756 errorList.add("Did not find a NSEC3 that covered the next closer name to '" +
757 qname + "' from '" + candidate.closestEncloser + "' (derived from the wildcard: " +
759 st_log.debug("proveWildcard: did not find a covering NSEC3 " +
760 "that covered the next closer name to " + qname + " from " +
761 candidate.closestEncloser + " (derived from wildcard " +
771 * Prove that a DS response either had no DS, or wasn't a delegation point.
773 * Fundamentally there are two cases here: normal NODATA and
777 * The NSEC3 RRs to examine.
779 * The name of the DS in question.
781 * The name of the zone that the NSEC3 RRs come from.
783 * @return SecurityStatus.SECURE if it was proven that there is no
784 * DS in a secure (i.e., not opt-in) way,
785 * SecurityStatus.INSECURE if there was no DS in an
786 * insecure (i.e., opt-in) way,
787 * SecurityStatus.INDETERMINATE if it was clear that this
788 * wasn't a delegation point, and SecurityStatus.BOGUS if
789 * the proofs don't work out.
791 public static byte proveNoDS(List<NSEC3Record> nsec3s,
794 List<String> errorList) {
795 if ((nsec3s == null) || (nsec3s.size() == 0)) {
796 return SecurityStatus.BOGUS;
799 NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
801 if (nsec3params == null) {
802 errorList.add("Could not find a single set of NSEC3 parameters (multiple parameters present)");
803 st_log.debug("couldn't find a single set of " +
804 "NSEC3 parameters (multiple parameters present).");
806 return SecurityStatus.BOGUS;
809 ByteArrayComparator bac = new ByteArrayComparator();
811 // Look for a matching NSEC3 to qname -- this is the normal NODATA case.
812 NSEC3Record nsec3 = findMatchingNSEC3(hash(qname, nsec3params),
813 zonename, nsec3s, nsec3params, bac);
816 // If the matching NSEC3 has the SOA bit set, it is from the wrong
817 // zone (the child instead of the parent). If it has the DS bit set,
818 // then we were lied to.
819 if (nsec3.hasType(Type.SOA) || nsec3.hasType(Type.DS)) {
820 errorList.add("Matching NSEC3 is incorrectly from the child " +
821 "instead of the parent (SOA or DS bit set)");
822 return SecurityStatus.BOGUS;
825 // If the NSEC3 RR doesn't have the NS bit set, then this wasn't a
827 if (!nsec3.hasType(Type.NS)) {
828 return SecurityStatus.INDETERMINATE;
831 // Otherwise, this proves no DS.
832 return SecurityStatus.SECURE;
835 // Otherwise, we are probably in the opt-in case.
836 CEResponse ce = proveClosestEncloser(qname, zonename, nsec3s,
837 nsec3params, bac, true, errorList);
840 errorList.add("Failed to prove the closest encloser as part of a 'No DS' proof");
841 return SecurityStatus.BOGUS;
844 // If we had the closest encloser proof, then we need to check that the
845 // covering NSEC3 was opt-in -- the proveClosestEncloser step already
846 // checked to see if the closest encloser was a delegation or DNAME.
847 if (isOptOut(ce.nc_nsec3)) {
848 return SecurityStatus.SECURE;
851 errorList.add("Failed to find a covering NSEC3 for 'No DS' proof");
852 return SecurityStatus.BOGUS;
856 * This is a class to encapsulate a unique set of NSEC3 parameters:
857 * algorithm, iterations, and salt.
859 private static class NSEC3Parameters {
862 public int iterations;
863 private NSEC3PARAMRecord nsec3paramrec;
865 public NSEC3Parameters(NSEC3Record r) {
866 alg = r.getHashAlgorithm();
868 iterations = r.getIterations();
870 nsec3paramrec = new NSEC3PARAMRecord(Name.root, DClass.IN, 0, alg,
871 0, iterations, salt);
874 public boolean match(NSEC3Record r, ByteArrayComparator bac) {
875 if (r.getHashAlgorithm() != alg) {
879 if (r.getIterations() != iterations) {
883 if ((salt == null) && (r.getSalt() != null)) {
892 bac = new ByteArrayComparator();
895 return bac.compare(r.getSalt(), salt) == 0;
898 public byte[] hash(Name name) throws NoSuchAlgorithmException {
899 return nsec3paramrec.hashName(name);
904 * This is just a simple class to encapsulate the response to a closest
907 private static class CEResponse {
908 public Name closestEncloser;
909 public NSEC3Record ce_nsec3;
910 public NSEC3Record nc_nsec3;
912 public CEResponse(Name ce, NSEC3Record nsec3) {
913 this.closestEncloser = ce;
914 this.ce_nsec3 = nsec3;