more progress -- still not compiling
[captive-validator.git] / src / com / versign / tat / dnssec / NSEC3ValUtils.java
1 /*
2  * $Id$
3  * 
4  * Copyright (c) 2006 VeriSign. All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer. 2. Redistributions in
11  * binary form must reproduce the above copyright notice, this list of
12  * conditions and the following disclaimer in the documentation and/or other
13  * materials provided with the distribution. 3. The name of the author may not
14  * be used to endorse or promote products derived from this software without
15  * specific prior written permission.
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *  
28  */
29
30 package com.versign.tat.dnssec;
31
32 import java.security.NoSuchAlgorithmException;
33 import java.util.*;
34
35 import org.xbill.DNS.*;
36 import org.xbill.DNS.utils.base32;
37
38 import com.versign.tat.dnssec.SignUtils.ByteArrayComparator;
39
40
41
42 public class NSEC3ValUtils
43 {
44
45   // FIXME: should probably refactor to handle different NSEC3 parameters more
46   // efficiently.
47   // Given a list of NSEC3 RRs, they should be grouped according to
48   // parameters. The idea is to hash and compare for each group independently,
49   // instead of having to skip NSEC3 RRs with the wrong parameters.
50
51
52   private static Name   asterisk_label = Name.fromConstantString("*");
53
54   /**
55    * This is a class to encapsulate a unique set of NSEC3 parameters:
56    * algorithm, iterations, and salt.
57    */
58   private static class NSEC3Parameters
59   {
60     public byte   alg;
61     public byte[] salt;
62     public int    iterations;
63
64     public NSEC3Parameters(NSEC3Record r)
65     {
66       alg = r.getHashAlgorithm();
67       salt = r.getSalt();
68       iterations = r.getIterations();
69     }
70
71     public boolean match(NSEC3Record r, ByteArrayComparator bac)
72     {
73       if (r.getHashAlgorithm() != alg) return false;
74       if (r.getIterations() != iterations) return false;
75
76       if (salt == null && r.getSalt() != null) return false;
77
78       if (bac == null) bac = new ByteArrayComparator();
79       return bac.compare(r.getSalt(), salt) == 0;
80     }
81   }
82
83   /**
84    * This is just a simple class to enapsulate the response to a closest
85    * encloser proof.
86    */
87   private static class CEResponse
88   {
89     public Name        closestEncloser;
90     public NSEC3Record ce_nsec3;
91     public NSEC3Record nc_nsec3;
92
93     public CEResponse(Name ce, NSEC3Record nsec3)
94     {
95       this.closestEncloser = ce;
96       this.ce_nsec3 = nsec3;
97     }
98   }
99
100   public static boolean supportsHashAlgorithm(int alg)
101   {
102     if (alg == NSEC3Record.SHA1_DIGEST_ID) return true;
103     return false;
104   }
105   
106   public static void stripUnknownAlgNSEC3s(List nsec3s)
107   {
108     if (nsec3s == null) return;
109     for (ListIterator i = nsec3s.listIterator(); i.hasNext(); )
110     {
111       NSEC3Record nsec3 = (NSEC3Record) i.next();
112       if (!supportsHashAlgorithm(nsec3.getHashAlgorithm()))
113       {
114         i.remove();
115       }
116     }
117   }
118   
119   /**
120    * Given a list of NSEC3Records that are part of a message, determine the
121    * NSEC3 parameters (hash algorithm, iterations, and salt) present. If there
122    * is more than one distinct grouping, return null;
123    * 
124    * @param nsec3s A list of NSEC3Record object.
125    * @return A set containing a number of objects (NSEC3Parameter objects)
126    *         that correspond to each distinct set of parameters, or null if
127    *         the nsec3s list was empty.
128    */
129   public static NSEC3Parameters nsec3Parameters(List nsec3s)
130   {
131     if (nsec3s == null || nsec3s.size() == 0) return null;
132
133     NSEC3Parameters params = new NSEC3Parameters((NSEC3Record) nsec3s.get(0));
134     ByteArrayComparator bac = new ByteArrayComparator();
135     
136     for (Iterator i = nsec3s.iterator(); i.hasNext();)
137     {
138       if (! params.match((NSEC3Record) i.next(), bac))
139       {
140         return null;
141       }
142     }  
143     return params;
144   }
145
146   /**
147    * In a list of NSEC3Record object pulled from a given message, find the
148    * NSEC3 that directly matches a given name, without hashing.
149    * 
150    * @param n The name in question.
151    * @param nsec3s A list of NSEC3Records from a given message.
152    * @return The matching NSEC3Record, or null if there wasn't one.
153    */
154   // private static NSEC3Record findDirectMatchingNSEC3(Name n, List nsec3s)
155   // {
156   // if (n == null || nsec3s == null) return null;
157   //
158   // for (Iterator i = nsec3s.iterator(); i.hasNext();)
159   // {
160   // NSEC3Record nsec3 = (NSEC3Record) i.next();
161   // if (n.equals(nsec3.getName())) return nsec3;
162   // }
163   //
164   // return null;
165   // }
166   /**
167    * Given a hash and an a zone name, construct an NSEC3 ownername.
168    * 
169    * @param hash The hash of an original name.
170    * @param zonename The zone to use in constructing the NSEC3 name.
171    * @return The NSEC3 name.
172    */
173   private static Name hashName(byte[] hash, Name zonename)
174   {
175     try
176     {
177       return new Name(base32.toString(hash).toLowerCase(), zonename);
178     }
179     catch (TextParseException e)
180     {
181       // Note, this should never happen.
182       return null;
183     }
184   }
185
186   /**
187    * Given a set of NSEC3 parameters, hash a name.
188    * 
189    * @param name The name to hash.
190    * @param params The parameters to hash with.
191    * @return The hash.
192    */
193   private static byte[] hash(Name name, NSEC3Parameters params)
194   {
195     try
196     {
197       return NSEC3Record.hash(name,
198           params.alg,
199           params.iterations,
200           params.salt);
201     }
202     catch (NoSuchAlgorithmException e)
203     {
204 //      st_log.debug("Did not recognize hash algorithm: " + params.alg);
205       return null;
206     }
207   }
208
209   /**
210    * Given the name of a closest encloser, return the name *.closest_encloser.
211    * 
212    * @param closestEncloser The name to start with.
213    * @return The wildcard name.
214    */
215   private static Name ceWildcard(Name closestEncloser)
216   {
217     try
218     {
219       Name wc = Name.concatenate(asterisk_label, closestEncloser);
220       return wc;
221     }
222     catch (NameTooLongException e)
223     {
224       return null;
225     }
226   }
227
228   /**
229    * Given a qname and its proven closest encloser, calculate the "next
230    * closest" name. Basically, this is the name that is one label longer than
231    * the closest encloser that is still a subdomain of qname.
232    * 
233    * @param qname The qname.
234    * @param closestEncloser The closest encloser name.
235    * @return The next closer name.
236    */
237   private static Name nextClosest(Name qname, Name closestEncloser)
238   {
239     int strip = qname.labels() - closestEncloser.labels() - 1;
240     return (strip > 0) ? new Name(qname, strip) : qname;
241   }
242
243   /**
244    * Find the NSEC3Record that matches a hash of a name.
245    * 
246    * @param hash The pre-calculated hash of a name.
247    * @param zonename The name of the zone that the NSEC3s are from.
248    * @param nsec3s A list of NSEC3Records from a given message.
249    * @param params The parameters used for calculating the hash.
250    * @param bac An already allocated ByteArrayComparator, for reuse. This may
251    *          be null.
252    * 
253    * @return The matching NSEC3Record, if one is present.
254    */
255   private static NSEC3Record findMatchingNSEC3(byte[] hash, Name zonename,
256       List nsec3s, NSEC3Parameters params, ByteArrayComparator bac)
257   {
258     Name n = hashName(hash, zonename);
259
260     for (Iterator i = nsec3s.iterator(); i.hasNext();)
261     {
262       NSEC3Record nsec3 = (NSEC3Record) i.next();
263       // Skip nsec3 records that are using different parameters.
264       if (!params.match(nsec3, bac)) continue;
265       if (n.equals(nsec3.getName())) return nsec3;
266     }
267     return null;
268   }
269
270   /**
271    * Given a hash and a candidate NSEC3Record, determine if that NSEC3Record
272    * covers the hash. Covers specifically means that the hash is in between
273    * the owner and next hashes and does not equal either.
274    * 
275    * @param nsec3 The candidate NSEC3Record.
276    * @param hash The precalculated hash.
277    * @param bac An already allocated comparator. This may be null.
278    * @return True if the NSEC3Record covers the hash.
279    */
280   private static boolean nsec3Covers(NSEC3Record nsec3, byte[] hash,
281       ByteArrayComparator bac)
282   {
283     byte[] owner = nsec3.getOwner();
284     byte[] next = nsec3.getNext();
285
286     // This is the "normal case: owner < next and owner < hash < next
287     if (bac.compare(owner, hash) < 0 && bac.compare(hash, next) < 0)
288       return true;
289
290     // this is the end of zone case: next < owner && hash > owner || hash <
291     // next
292     if (bac.compare(next, owner) <= 0
293         && (bac.compare(hash, next) < 0 || bac.compare(owner, hash) < 0))
294       return true;
295      
296     // Otherwise, the NSEC3 does not cover the hash.
297     return false;
298   }
299
300   /**
301    * Given a pre-hashed name, find a covering NSEC3 from among a list of
302    * NSEC3s.
303    * 
304    * @param hash The hash to consider.
305    * @param zonename The name of the zone.
306    * @param nsec3s The list of NSEC3s present in a message.
307    * @param params The NSEC3 parameters used to generate the hash -- NSEC3s
308    *          that do not use those parameters will be skipped.
309    * 
310    * @return A covering NSEC3 if one is present, null otherwise.
311    */
312   private static NSEC3Record findCoveringNSEC3(byte[] hash, Name zonename,
313       List nsec3s, NSEC3Parameters params, ByteArrayComparator bac)
314   {
315     ByteArrayComparator comparator = new ByteArrayComparator();
316
317     for (Iterator i = nsec3s.iterator(); i.hasNext();)
318     {
319       NSEC3Record nsec3 = (NSEC3Record) i.next();
320       if (!params.match(nsec3, bac)) continue;
321
322       if (nsec3Covers(nsec3, hash, comparator)) return nsec3;
323     }
324
325     return null;
326   }
327
328
329   /**
330    * Given a name and a list of NSEC3s, find the candidate closest encloser.
331    * This will be the first ancestor of 'name' (including itself) to have a
332    * matching NSEC3 RR.
333    * 
334    * @param name The name the start with.
335    * @param zonename The name of the zone that the NSEC3s came from.
336    * @param nsec3s The list of NSEC3s.
337    * @param nsec3params The NSEC3 parameters.
338    * @param bac A pre-allocated comparator. May be null.
339    * 
340    * @return A CEResponse containing the closest encloser name and the NSEC3
341    *         RR that matched it, or null if there wasn't one.
342    */
343   private static CEResponse findClosestEncloser(Name name, Name zonename,
344       List nsec3s, NSEC3Parameters params, ByteArrayComparator bac)
345   {
346     Name n = name;
347
348     NSEC3Record nsec3;
349
350     // This scans from longest name to shortest, so the first match we find is
351     // the only viable candidate.
352     // FIXME: modify so that the NSEC3 matching the zone apex need not be
353     // present.
354     while (n.labels() >= zonename.labels())
355     {
356       nsec3 = findMatchingNSEC3(hash(n, params), zonename, nsec3s, params, bac);
357       if (nsec3 != null) return new CEResponse(n, nsec3);
358       n = new Name(n, 1);
359     }
360
361     return null;
362   }
363
364   /**
365    * Given a List of nsec3 RRs, find and prove the closest encloser to qname.
366    * 
367    * @param qname The qname in question.
368    * @param zonename The name of the zone that the NSEC3 RRs come from.
369    * @param nsec3s The list of NSEC3s found the this response (already
370    *          verified).
371    * @param params The NSEC3 parameters found in the response.
372    * @param bac A pre-allocated comparator. May be null.
373    * @param proveDoesNotExist If true, then if the closest encloser turns out
374    *          to be qname, then null is returned.
375    * @return null if the proof isn't completed. Otherwise, return a CEResponse
376    *         object which contains the closest encloser name and the NSEC3
377    *         that matches it.
378    */
379   private static CEResponse proveClosestEncloser(Name qname, Name zonename,
380       List nsec3s, NSEC3Parameters params, ByteArrayComparator bac,
381       boolean proveDoesNotExist)
382   {
383     CEResponse candidate = findClosestEncloser(qname,
384         zonename,
385         nsec3s,
386         params,
387         bac);
388
389     if (candidate == null)
390     {
391 //      st_log.debug("proveClosestEncloser: could not find a "
392 //          + "candidate for the closest encloser.");
393       return null;
394     }
395
396     if (candidate.closestEncloser.equals(qname))
397     {
398       if (proveDoesNotExist)
399       {
400 //        st_log.debug("proveClosestEncloser: proved that qname existed!");
401         return null;
402       }
403       // otherwise, we need to nothing else to prove that qname is its own
404       // closest encloser.
405       return candidate;
406     }
407
408     // If the closest encloser is actually a delegation, then the response
409     // should have been a referral. If it is a DNAME, then it should have been
410     // a DNAME response.
411     if (candidate.ce_nsec3.hasType(Type.NS)
412         && !candidate.ce_nsec3.hasType(Type.SOA))
413     {
414 //      st_log.debug("proveClosestEncloser: closest encloser "
415 //          + "was a delegation!");
416       return null;
417     }
418     if (candidate.ce_nsec3.hasType(Type.DNAME))
419     {
420 //      st_log.debug("proveClosestEncloser: closest encloser was a DNAME!");
421       return null;
422     }
423
424     // Otherwise, we need to show that the next closer name is covered.
425     Name nextClosest = nextClosest(qname, candidate.closestEncloser);
426     
427     byte[] nc_hash = hash(nextClosest, params);
428     candidate.nc_nsec3 = findCoveringNSEC3(nc_hash,
429         zonename,
430         nsec3s,
431         params,
432         bac);
433     if (candidate.nc_nsec3 == null)
434     {
435 //      st_log.debug("Could not find proof that the "
436 //          + "closest encloser was the closest encloser");
437       return null;
438     }
439
440     return candidate;
441   }
442
443   private static int maxIterations(int baseAlg, int keysize)
444   {
445     switch (baseAlg)
446     {
447       case DnsSecVerifier.RSA:
448         if (keysize == 0) return 2500; // the max at 4096
449         if (keysize > 2048) return 2500;
450         if (keysize > 1024) return 500;
451         if (keysize > 0) return 150;
452         break;
453       case DnsSecVerifier.DSA:
454         if (keysize == 0) return 5000; // the max at 2048;
455         if (keysize > 1024) return 5000;
456         if (keysize > 0) return 1500;
457         break;
458     }
459     return -1;
460   }
461   
462   private static boolean validIterations(NSEC3Parameters nsec3params, 
463       RRset dnskey_rrset, DnsSecVerifier verifier)
464   {
465     // for now, we return the maximum iterations based simply on the key
466     // algorithms that may have been used to sign the NSEC3 RRsets.
467     
468     int max_iterations = 0;
469     for (Iterator i = dnskey_rrset.rrs(); i.hasNext();)
470     {
471       DNSKEYRecord dnskey = (DNSKEYRecord) i.next();
472       int baseAlg = verifier.baseAlgorithm(dnskey.getAlgorithm());
473       int iters = maxIterations(baseAlg, 0);
474       max_iterations = max_iterations < iters ? iters : max_iterations;
475     }
476     
477     if (nsec3params.iterations > max_iterations) return false;
478     
479    return true; 
480   }
481   
482   /**
483    * Determine if all of the NSEC3s in a response are legally ignoreable
484    * (i.e., their presence should lead to an INSECURE result). Currently, this
485    * is solely based on iterations.
486    * 
487    * @param nsec3s The list of NSEC3s. If there is more than one set of NSEC3
488    *          parameters present, this test will not be performed.
489    * @param dnskey_rrset The set of validating DNSKEYs.
490    * @param verifier The verifier used to verify the NSEC3 RRsets. This is
491    *          solely used to map algorithm aliases.
492    * @return true if all of the NSEC3s can be legally ignored, false if not.
493    */
494   public static boolean allNSEC3sIgnoreable(List nsec3s, RRset dnskey_rrset, DnsSecVerifier verifier)
495   {
496     NSEC3Parameters params = nsec3Parameters(nsec3s);
497     if (params == null) return false;
498     
499     return !validIterations(params, dnskey_rrset, verifier);
500   }
501   
502   /**
503    * Determine if the set of NSEC3 records provided with a response prove NAME
504    * ERROR. This means that the NSEC3s prove a) the closest encloser exists,
505    * b) the direct child of the closest encloser towards qname doesn't exist,
506    * and c) *.closest encloser does not exist.
507    * 
508    * @param nsec3s The list of NSEC3s.
509    * @param qname The query name to check against.
510    * @param zonename This is the name of the zone that the NSEC3s belong to.
511    *          This may be discovered in any number of ways. A good one is to
512    *          use the signerName from the NSEC3 record's RRSIG.
513    * @return SecurityStatus.SECURE of the Name Error is proven by the NSEC3
514    *         RRs, BOGUS if not, INSECURE if all of the NSEC3s could be validly
515    *         ignored.
516    */
517   public static boolean proveNameError(List nsec3s, Name qname, Name zonename)
518   {
519     if (nsec3s == null || nsec3s.size() == 0) return false;
520
521     NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
522     if (nsec3params == null)
523     {
524 //      st_log.debug("Could not find a single set of " +
525 //          "NSEC3 parameters (multiple parameters present).");
526       return false;
527     }
528     
529     ByteArrayComparator bac = new ByteArrayComparator();
530
531     // First locate and prove the closest encloser to qname. We will use the
532     // variant that fails if the closest encloser turns out to be qname.
533     CEResponse ce = proveClosestEncloser(qname,
534         zonename,
535         nsec3s,
536         nsec3params,
537         bac,
538         true);
539
540     if (ce == null)
541     {
542 //      st_log.debug("proveNameError: failed to prove a closest encloser.");
543       return false;
544     }
545
546     // At this point, we know that qname does not exist. Now we need to prove
547     // that the wildcard does not exist.
548     Name wc = ceWildcard(ce.closestEncloser);
549     byte[] wc_hash = hash(wc, nsec3params);
550     NSEC3Record nsec3 = findCoveringNSEC3(wc_hash,
551         zonename,
552         nsec3s,
553         nsec3params,
554         bac);
555     if (nsec3 == null)
556     {
557 //      st_log.debug("proveNameError: could not prove that the "
558 //          + "applicable wildcard did not exist.");
559       return false;
560     }
561
562     return true;
563   }
564
565   /**
566    * Determine if the set of NSEC3 records provided with a response prove NAME
567    * ERROR when qtype = NSEC3. This is a special case, and (currently anyway)
568    * it suffices to simply prove that the NSEC3 RRset itself does not exist,
569    * without proving that no wildcard could have generated it, etc..
570    * 
571    * @param nsec3s The list of NSEC3s.
572    * @param qname The query name to check against.
573    * @param zonename This is the name of the zone that the NSEC3s belong to.
574    *          This may be discovered in any number of ways. A good one is to
575    *          use the signerName from the NSEC3 record's RRSIG.
576    * @return true of the Name Error is proven by the NSEC3 RRs, false if not.
577    */
578   // public static boolean proveNSEC3NameError(List nsec3s, Name qname,
579   // Name zonename)
580   // {
581   // if (nsec3s == null || nsec3s.size() == 0) return false;
582   //
583   // for (Iterator i = nsec3s.iterator(); i.hasNext(); )
584   // {
585   // NSEC3Record nsec3 = (NSEC3Record) i.next();
586   //      
587   // // Convert owner and next into Names.
588   // Name owner = nsec3.getName();
589   // Name next = null;
590   // try
591   // {
592   // next = new Name(base32.toString(nsec3.getNext()), zonename);
593   // }
594   // catch (TextParseException e)
595   // {
596   // continue;
597   // }
598   //      
599   // // Now see if qname is covered by the NSEC3.
600   //      
601   // // normal case, owner < qname < next.
602   // if (owner.compareTo(next) < 0 && owner.compareTo(qname) < 0 &&
603   // next.compareTo(qname) > 0)
604   // {
605   // st_log.debug("proveNSEC3NameError: found a covering NSEC3: " + nsec3);
606   // return true;
607   // }
608   // // end-of-zone case: next < owner and qname > owner || qname < next.
609   // if (owner.compareTo(next) > 0 && (owner.compareTo(qname) < 0 ||
610   // next.compareTo(qname) > 0))
611   // {
612   // st_log.debug("proveNSEC3NameError: found a covering NSEC3: " + nsec3);
613   // return true;
614   // }
615   // }
616   //    
617   // st_log.debug("proveNSEC3NameError: did not find a covering NSEC3");
618   // return false;
619   // }
620   /**
621    * Determine if the NSEC3s provided in a response prove the NOERROR/NODATA
622    * status. There are a number of different variants to this:
623    * 
624    * 1) Normal NODATA -- qname is matched to an NSEC3 record, type is not
625    * present.
626    * 
627    * 2) ENT NODATA -- because there must be NSEC3 record for
628    * empty-non-terminals, this is the same as #1.
629    * 
630    * 3) NSEC3 ownername NODATA -- qname matched an existing, lone NSEC3
631    * ownername, but qtype was not NSEC3. NOTE: as of nsec-05, this case no
632    * longer exists.
633    * 
634    * 4) Wildcard NODATA -- A wildcard matched the name, but not the type.
635    * 
636    * 5) Opt-In DS NODATA -- the qname is covered by an opt-in span and qtype ==
637    * DS. (or maybe some future record with the same parent-side-only property)
638    * 
639    * @param nsec3s The NSEC3Records to consider.
640    * @param qname The qname in question.
641    * @param qtype The qtype in question.
642    * @param zonename The name of the zone that the NSEC3s came from.
643    * @return true if the NSEC3s prove the proposition.
644    */
645   public static boolean proveNodata(List nsec3s, Name qname, int qtype,
646       Name zonename)
647   {
648     if (nsec3s == null || nsec3s.size() == 0) return false;
649
650     NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
651     if (nsec3params == null)
652     {
653 //      st_log.debug("could not find a single set of "
654 //          + "NSEC3 parameters (multiple parameters present)");
655       return false;
656     }
657     ByteArrayComparator bac = new ByteArrayComparator();
658
659     NSEC3Record nsec3 = findMatchingNSEC3(hash(qname, nsec3params),
660         zonename,
661         nsec3s,
662         nsec3params,
663         bac);
664     // Cases 1 & 2.
665     if (nsec3 != null)
666     {
667       if (nsec3.hasType(qtype))
668       {
669 //        st_log.debug("proveNodata: Matching NSEC3 proved that type existed!");
670         return false;
671       }
672       if (nsec3.hasType(Type.CNAME))
673       {
674 //        st_log.debug("proveNodata: Matching NSEC3 proved "
675 //            + "that a CNAME existed!");
676         return false;
677       }
678       return true;
679     }
680
681     // For cases 3 - 5, we need the proven closest encloser, and it can't
682     // match qname. Although, at this point, we know that it won't since we
683     // just checked that.
684     CEResponse ce = proveClosestEncloser(qname,
685         zonename,
686         nsec3s,
687         nsec3params,
688         bac,
689         true);
690
691     // At this point, not finding a match or a proven closest encloser is a
692     // problem.
693     if (ce == null)
694     {
695 //      st_log.debug("proveNodata: did not match qname, "
696 //          + "nor found a proven closest encloser.");
697       return false;
698     }
699
700     // Case 3: REMOVED
701
702     // Case 4:
703     Name wc = ceWildcard(ce.closestEncloser);
704     nsec3 = findMatchingNSEC3(hash(wc, nsec3params),
705         zonename,
706         nsec3s,
707         nsec3params,
708         bac);
709
710     if (nsec3 != null)
711     {
712       if (nsec3.hasType(qtype))
713       {
714 //        st_log.debug("proveNodata: matching wildcard had qtype!");
715         return false;
716       }
717       return true;
718     }
719
720     // Case 5.
721     if (qtype != Type.DS)
722     {
723 //      st_log.debug("proveNodata: could not find matching NSEC3, "
724 //          + "nor matching wildcard, and qtype is not DS -- no more options.");
725       return false;
726     }
727
728     // We need to make sure that the covering NSEC3 is opt-in.
729     if (!ce.nc_nsec3.getOptInFlag())
730     {
731 //      st_log.debug("proveNodata: covering NSEC3 was not "
732 //          + "opt-in in an opt-in DS NOERROR/NODATA case.");
733       return false;
734     }
735
736     return true;
737   }
738
739   /**
740    * Prove that a positive wildcard match was appropriate (no direct match
741    * RRset).
742    * 
743    * @param nsec3s The NSEC3 records to work with.
744    * @param qname The qname that was matched to the wildard
745    * @param zonename The name of the zone that the NSEC3s come from.
746    * @param wildcard The purported wildcard that matched.
747    * @return true if the NSEC3 records prove this case.
748    */
749   public static boolean proveWildcard(List nsec3s, Name qname, Name zonename,
750       Name wildcard)
751   {
752     if (nsec3s == null || nsec3s.size() == 0) return false;
753     if (qname == null || wildcard == null) return false;
754
755     NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
756     if (nsec3params == null) 
757     {
758 //      st_log.debug("couldn't find a single set of NSEC3 parameters (multiple parameters present).");
759       return false;
760     }
761     
762     ByteArrayComparator bac = new ByteArrayComparator();
763
764     // We know what the (purported) closest encloser is by just looking at the
765     // supposed generating wildcard.
766     CEResponse candidate = new CEResponse(new Name(wildcard, 1), null);
767
768     // Now we still need to prove that the original data did not exist.
769     // Otherwise, we need to show that the next closer name is covered.
770     Name nextClosest = nextClosest(qname, candidate.closestEncloser);
771     candidate.nc_nsec3 = findCoveringNSEC3(hash(nextClosest, nsec3params),
772         zonename,
773         nsec3s,
774         nsec3params,
775         bac);
776
777     if (candidate.nc_nsec3 == null)
778     {
779 //      st_log.debug("proveWildcard: did not find a covering NSEC3 "
780 //          + "that covered the next closer name to " + qname + " from "
781 //          + candidate.closestEncloser + " (derived from wildcard " + wildcard
782 //          + ")");
783       return false;
784     }
785
786     return true;
787   }
788
789   /**
790    * Prove that a DS response either had no DS, or wasn't a delegation point.
791    * 
792    * Fundamentally there are two cases here: normal NODATA and Opt-In NODATA.
793    * 
794    * @param nsec3s The NSEC3 RRs to examine.
795    * @param qname The name of the DS in question.
796    * @param zonename The name of the zone that the NSEC3 RRs come from.
797    * 
798    * @return SecurityStatus.SECURE if it was proven that there is no DS in a
799    *         secure (i.e., not opt-in) way, SecurityStatus.INSECURE if there
800    *         was no DS in an insecure (i.e., opt-in) way,
801    *         SecurityStatus.INDETERMINATE if it was clear that this wasn't a
802    *         delegation point, and SecurityStatus.BOGUS if the proofs don't
803    *         work out.
804    */
805   public static int proveNoDS(List nsec3s, Name qname, Name zonename)
806   {
807     if (nsec3s == null || nsec3s.size() == 0) return SecurityStatus.BOGUS;
808
809     NSEC3Parameters nsec3params = nsec3Parameters(nsec3s);
810     if (nsec3params == null)
811     {
812 //      st_log.debug("couldn't find a single set of " +
813 //          "NSEC3 parameters (multiple parameters present).");
814       return SecurityStatus.BOGUS;      
815     }
816     ByteArrayComparator bac = new ByteArrayComparator();
817
818     // Look for a matching NSEC3 to qname -- this is the normal NODATA case.
819     NSEC3Record nsec3 = findMatchingNSEC3(hash(qname, nsec3params),
820         zonename,
821         nsec3s,
822         nsec3params,
823         bac);
824
825     if (nsec3 != null)
826     {
827       // If the matching NSEC3 has the SOA bit set, it is from the wrong zone
828       // (the child instead of the parent). If it has the DS bit set, then we
829       // were lied to.
830       if (nsec3.hasType(Type.SOA) || nsec3.hasType(Type.DS))
831       {
832         return SecurityStatus.BOGUS;
833       }
834       // If the NSEC3 RR doesn't have the NS bit set, then this wasn't a
835       // delegation point.
836       if (!nsec3.hasType(Type.NS)) return SecurityStatus.INDETERMINATE;
837
838       // Otherwise, this proves no DS.
839       return SecurityStatus.SECURE;
840     }
841
842     // Otherwise, we are probably in the opt-in case.
843     CEResponse ce = proveClosestEncloser(qname,
844         zonename,
845         nsec3s,
846         nsec3params,
847         bac,
848         true);
849     if (ce == null)
850     {
851       return SecurityStatus.BOGUS;
852     }
853
854     // If we had the closest encloser proof, then we need to check that the
855     // covering NSEC3 was opt-in -- the proveClosestEncloser step already
856     // checked to see if the closest encloser was a delegation or DNAME.
857     if (ce.nc_nsec3.getOptInFlag())
858     {
859       return SecurityStatus.SECURE;
860     }
861
862     return SecurityStatus.BOGUS;
863   }
864
865 }