4 * Copyright (c) 2005 VeriSign, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 package se.rfc.unbound;
35 import java.security.*;
37 import org.xbill.DNS.*;
38 import org.xbill.DNS.security.*;
40 import se.rfc.unbound.SecurityStatus;
41 import se.rfc.unbound.Util;
44 * A class for performing basic DNSSEC verification. The DNSJAVA package
45 * contains a similar class. This is a re-implementation that allows us to have
46 * finer control over the validation process.
48 public class DnsSecVerifier
50 public static final int UNKNOWN = 0;
51 public static final int RSA = 1;
52 public static final int DSA = 2;
55 * This is a mapping of DNSSEC algorithm numbers/private identifiers to JCA
56 * algorithm identifiers.
58 private HashMap mAlgorithmMap;
60 private static class AlgEntry
62 public String jcaName;
66 public AlgEntry(String name, int dnssecAlg, boolean isDSA)
69 this.dnssecAlg = dnssecAlg;
74 public DnsSecVerifier()
76 mAlgorithmMap = new HashMap();
78 // set the default algorithm map.
79 mAlgorithmMap.put(new Integer(DNSSEC.RSAMD5), new AlgEntry("MD5withRSA",
80 DNSSEC.RSAMD5, false));
81 mAlgorithmMap.put(new Integer(DNSSEC.DSA), new AlgEntry("SHA1withDSA", DNSSEC.DSA,
83 mAlgorithmMap.put(new Integer(DNSSEC.RSASHA1), new AlgEntry(
84 "SHA1withRSA", DNSSEC.RSASHA1, false));
87 private boolean isDSA(int algorithm)
89 // shortcut the standard algorithms
90 if (algorithm == DNSSEC.DSA) return true;
91 if (algorithm == DNSSEC.RSASHA1) return false;
92 if (algorithm == DNSSEC.RSAMD5) return false;
94 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(new Integer(algorithm));
95 if (entry != null) return entry.isDSA;
99 public void init(Properties config)
101 if (config == null) return;
103 // Algorithm configuration
105 // For now, we just accept new identifiers for existing algoirthms.
106 // FIXME: handle private identifiers.
107 List aliases = Util.parseConfigPrefix(config, "dns.algorithm.");
109 for (Iterator i = aliases.iterator(); i.hasNext();)
111 Util.ConfigEntry entry = (Util.ConfigEntry) i.next();
113 Integer alg_alias = new Integer(Util.parseInt(entry.key, -1));
114 Integer alg_orig = new Integer(Util.parseInt(entry.value, -1));
116 if (!mAlgorithmMap.containsKey(alg_orig))
118 // log.warn("Unable to alias " + alg_alias + " to unknown algorithm "
123 if (mAlgorithmMap.containsKey(alg_alias))
125 // log.warn("Algorithm alias " + alg_alias
126 // + " is already defined and cannot be redefined");
130 mAlgorithmMap.put(alg_alias, mAlgorithmMap.get(alg_orig));
133 // for debugging purposes, log the entire algorithm map table.
134 for (Iterator i = mAlgorithmMap.keySet().iterator(); i.hasNext(); )
136 Integer alg = (Integer) i.next();
137 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(alg);
138 // if (entry == null)
139 // log.warn("DNSSEC alg " + alg + " has a null entry!");
141 // log.debug("DNSSEC alg " + alg + " maps to " + entry.jcaName
142 // + " (" + entry.dnssecAlg + ")");
147 * Find the matching DNSKEY(s) to an RRSIG within a DNSKEY rrset. Normally
148 * this will only return one DNSKEY. It can return more than one, since
149 * KeyID/Footprints are not guaranteed to be unique.
151 * @param dnskey_rrset The DNSKEY rrset to search.
152 * @param signature The RRSIG to match against.
153 * @return A List contains a one or more DNSKEYRecord objects, or null if a
154 * matching DNSKEY could not be found.
156 private List findKey(RRset dnskey_rrset, RRSIGRecord signature)
158 if (!signature.getSigner().equals(dnskey_rrset.getName()))
160 // log.trace("findKey: could not find appropriate key because "
161 // + "incorrect keyset was supplied. Wanted: " + signature.getSigner()
162 // + ", got: " + dnskey_rrset.getName());
166 int keyid = signature.getFootprint();
167 int alg = signature.getAlgorithm();
169 List res = new ArrayList(dnskey_rrset.size());
171 for (Iterator i = dnskey_rrset.rrs(); i.hasNext();)
173 DNSKEYRecord r = (DNSKEYRecord) i.next();
174 if (r.getAlgorithm() == alg && r.getFootprint() == keyid)
182 // log.trace("findKey: could not find a key matching "
183 // + "the algorithm and footprint in supplied keyset. ");
190 * Check to see if a signature looks valid (i.e., matches the rrset in
191 * question, in the validity period, etc.)
193 * @param rrset The rrset that the signature belongs to.
194 * @param sigrec The signature record to check.
195 * @return A value of DNSSEC.Secure if it looks OK, DNSSEC.Faile if it looks
198 private byte checkSignature(RRset rrset, RRSIGRecord sigrec)
200 if (rrset == null || sigrec == null) return DNSSEC.Failed;
201 if (!rrset.getName().equals(sigrec.getName()))
203 // log.debug("Signature name does not match RRset name");
204 return SecurityStatus.BOGUS;
206 if (rrset.getType() != sigrec.getTypeCovered())
208 // log.debug("Signature type does not match RRset type");
209 return SecurityStatus.BOGUS;
212 Date now = new Date();
213 Date start = sigrec.getTimeSigned();
214 Date expire = sigrec.getExpire();
215 if (now.before(start))
217 // log.debug("Signature is not yet valid");
218 return SecurityStatus.BOGUS;
221 if (now.after(expire))
223 // log.debug("Signature has expired (now = " + now + ", sig expires = "
225 return SecurityStatus.BOGUS;
228 return SecurityStatus.SECURE;
231 public PublicKey parseDNSKEY(DNSKEYRecord key)
233 AlgEntry ae = (AlgEntry) mAlgorithmMap
234 .get(new Integer(key.getAlgorithm()));
235 if (key.getAlgorithm() != ae.dnssecAlg)
237 // Recast the DNSKEYRecord in question as one using the offical
238 // algorithm, to work around the lack of alias support in the underlying
239 // KEYConverter class from DNSjava
241 key = new DNSKEYRecord(key.getName(), key.getDClass(), key.getTTL(),
242 key.getFlags(), key.getProtocol(), ae.dnssecAlg, key.getKey());
245 return KEYConverter.parseRecord(key);
250 * Actually cryptographically verify a signature over the rrset. The RRSIG
251 * record must match the rrset being verified (see checkSignature).
253 * @param rrset The rrset to verify.
254 * @param sigrec The signature to verify with.
255 * @param key The (public) key associated with the RRSIG record.
256 * @return A security status code: SECURE if it worked, BOGUS if not,
257 * UNCHECKED if we just couldn't actually do the function.
259 public byte verifySignature(RRset rrset, RRSIGRecord sigrec,
264 PublicKey pk = parseDNSKEY(key);
268 // log.warn("Could not convert DNSKEY record to a JCA public key: "
270 return SecurityStatus.UNCHECKED;
273 byte[] data = SignUtils.generateSigData(rrset, sigrec);
275 Signature signer = getSignature(sigrec.getAlgorithm());
278 return SecurityStatus.BOGUS;
281 signer.initVerify(pk);
284 byte[] sig = sigrec.getSignature();
285 if (isDSA(sigrec.getAlgorithm()))
287 sig = SignUtils.convertDSASignature(sig);
289 if (!signer.verify(sig))
291 // log.info("Signature failed to verify cryptographically");
292 // log.debug("Failed signature: " + sigrec);
293 return SecurityStatus.BOGUS;
295 // log.trace("Signature verified: " + sigrec);
296 return SecurityStatus.SECURE;
298 catch (IOException e)
300 // log.error("I/O error", e);
302 catch (GeneralSecurityException e)
304 // log.error("Security error", e);
307 // FIXME: Since I'm not sure what would cause an exception here (failure
308 // to have the required crypto?)
309 // We default to UNCHECKED instead of BOGUS. This could be wrong.
310 return SecurityStatus.UNCHECKED;
315 * Verify an RRset against a particular signature.
317 * @return DNSSEC.Secure if the signature verfied, DNSSEC.Failed if it did
318 * not verify (for any reason), and DNSSEC.Insecure if verification
319 * could not be completed (usually because the public key was not
322 public byte verifySignature(RRset rrset, RRSIGRecord sigrec, RRset key_rrset)
324 byte result = checkSignature(rrset, sigrec);
325 if (result != SecurityStatus.SECURE) return result;
327 List keys = findKey(key_rrset, sigrec);
331 // log.trace("could not find appropriate key");
332 return SecurityStatus.BOGUS;
335 byte status = SecurityStatus.UNCHECKED;
337 for (Iterator i = keys.iterator(); i.hasNext();)
339 DNSKEYRecord key = (DNSKEYRecord) i.next();
340 status = verifySignature(rrset, sigrec, key);
342 if (status == SecurityStatus.SECURE) break;
349 * Verifies an RRset. This routine does not modify the RRset. This RRset is
350 * presumed to be verifiable, and the correct DNSKEY rrset is presumed to
353 * @return SecurityStatus.SECURE if the rrest verified positively,
354 * SecurityStatus.BOGUS otherwise.
356 public byte verify(RRset rrset, RRset key_rrset)
358 Iterator i = rrset.sigs();
362 // log.info("RRset failed to verify due to lack of signatures");
363 return SecurityStatus.BOGUS;
368 RRSIGRecord sigrec = (RRSIGRecord) i.next();
370 byte res = verifySignature(rrset, sigrec, key_rrset);
372 if (res == SecurityStatus.SECURE) return res;
375 // log.info("RRset failed to verify: all signatures were BOGUS");
376 return SecurityStatus.BOGUS;
380 * Verify an RRset against a single DNSKEY. Use this when you must be
381 * certain that an RRset signed and verifies with a particular DNSKEY (as
382 * opposed to a particular DNSKEY rrset).
384 * @param rrset The rrset to verify.
385 * @param dnskey The DNSKEY to verify with.
386 * @return SecurityStatus.SECURE if the rrset verified, BOGUS otherwise.
388 public byte verify(RRset rrset, DNSKEYRecord dnskey)
390 // Iterate over RRSIGS
392 Iterator i = rrset.sigs();
395 // log.info("RRset failed to verify due to lack of signatures");
396 return SecurityStatus.BOGUS;
401 RRSIGRecord sigrec = (RRSIGRecord) i.next();
403 // Skip RRSIGs that do not match our given key's footprint.
404 if (sigrec.getFootprint() != dnskey.getFootprint()) continue;
406 byte res = verifySignature(rrset, sigrec, dnskey);
408 if (res == SecurityStatus.SECURE) return res;
411 // log.info("RRset failed to verify: all signatures were BOGUS");
412 return SecurityStatus.BOGUS;
415 public boolean supportsAlgorithm(int algorithm)
417 return mAlgorithmMap.containsKey(new Integer(algorithm));
420 public boolean supportsAlgorithm(Name private_id)
422 return mAlgorithmMap.containsKey(private_id);
425 public int baseAlgorithm(int algorithm)
435 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(new Integer(algorithm));
436 if (entry == null) return UNKNOWN;
437 if (entry.isDSA) return DSA;
441 /** @return the appropriate Signature object for this keypair. */
442 private Signature getSignature(int algorithm)
449 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(new Integer(algorithm));
452 // log.info("DNSSEC algorithm " + algorithm + " not recognized.");
455 // TODO: should we cache the instance?
456 s = Signature.getInstance(entry.jcaName);
458 catch (NoSuchAlgorithmException e)
460 // log.error("error getting Signature object", e);
466 // TODO: enable private algorithm support in dnsjava.
467 // Right now, this cannot be used because the DNSKEYRecord object doesn't
469 // the private key name.
470 // private Signature getSignature(Name private_alg)
472 // Signature s = null;
476 // String alg_id = (String) mAlgorithmMap.get(private_alg);
477 // if (alg_id == null)
479 // log.debug("DNSSEC private algorithm '" + private_alg
480 // + "' not recognized.");
484 // s = Signature.getInstance(alg_id);
486 // catch (NoSuchAlgorithmException e)
488 // log.error("error getting Signature object", e);