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 org.apache.log4j.Logger;
28 import org.xbill.DNS.*;
29 import org.xbill.DNS.security.*;
33 import java.security.*;
39 * A class for performing basic DNSSEC verification. The DNSJAVA package
40 * contains a similar class. This is a re-implementation that allows us to have
41 * finer control over the validation process.
43 public class DnsSecVerifier {
44 public static final int UNKNOWN = 0;
45 public static final int RSA = 1;
46 public static final int DSA = 2;
47 private Logger log = Logger.getLogger(this.getClass());
50 * This is a mapping of DNSSEC algorithm numbers/private identifiers to JCA
51 * algorithm identifiers.
53 private HashMap<Integer, AlgEntry> mAlgorithmMap;
55 public DnsSecVerifier() {
56 mAlgorithmMap = new HashMap<Integer, AlgEntry>();
58 // set the default algorithm map.
59 mAlgorithmMap.put(new Integer(DNSSEC.RSAMD5),
60 new AlgEntry("MD5withRSA", DNSSEC.RSAMD5, false));
61 mAlgorithmMap.put(new Integer(DNSSEC.DSA),
62 new AlgEntry("SHA1withDSA", DNSSEC.DSA, true));
63 mAlgorithmMap.put(new Integer(DNSSEC.RSASHA1),
64 new AlgEntry("SHA1withRSA", DNSSEC.RSASHA1, false));
65 mAlgorithmMap.put(new Integer(DNSSEC.DSA_NSEC3_SHA1),
66 new AlgEntry("SHA1withDSA", DNSSEC.DSA, true));
67 mAlgorithmMap.put(new Integer(DNSSEC.RSA_NSEC3_SHA1),
68 new AlgEntry("SHA1withRSA", DNSSEC.RSASHA1, false));
69 mAlgorithmMap.put(new Integer(DNSSEC.RSASHA256),
70 new AlgEntry("SHA256withRSA", DNSSEC.RSASHA256, false));
71 mAlgorithmMap.put(new Integer(DNSSEC.RSASHA512),
72 new AlgEntry("SHA512withRSA", DNSSEC.RSASHA512, false));
75 private boolean isDSA(int algorithm) {
76 // shortcut the standard algorithms
77 if (algorithm == DNSSEC.DSA) {
81 if (algorithm == DNSSEC.RSASHA1) {
85 if (algorithm == DNSSEC.RSAMD5) {
89 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(new Integer(algorithm));
98 public void init(Properties config) {
103 // Algorithm configuration
105 // For now, we just accept new identifiers for existing algorithms.
106 // FIXME: handle private identifiers.
107 List<Util.ConfigEntry> aliases = Util.parseConfigPrefix(config,
110 for (Util.ConfigEntry entry : aliases) {
111 Integer alg_alias = new Integer(Util.parseInt(entry.key, -1));
112 Integer alg_orig = new Integer(Util.parseInt(entry.value, -1));
114 if (!mAlgorithmMap.containsKey(alg_orig)) {
115 log.warn("Unable to alias " + alg_alias +
116 " to unknown algorithm " + alg_orig);
121 if (mAlgorithmMap.containsKey(alg_alias)) {
122 log.warn("Algorithm alias " + alg_alias +
123 " is already defined and cannot be redefined");
128 mAlgorithmMap.put(alg_alias, mAlgorithmMap.get(alg_orig));
131 // for debugging purposes, log the entire algorithm map table.
132 for (Integer alg : mAlgorithmMap.keySet()) {
133 AlgEntry entry = mAlgorithmMap.get(alg);
136 log.warn("DNSSEC alg " + alg + " has a null entry!");
138 log.debug("DNSSEC alg " + alg + " maps to " + entry.jcaName +
139 " (" + entry.dnssecAlg + ")");
145 * Find the matching DNSKEY(s) to an RRSIG within a DNSKEY rrset. Normally
146 * this will only return one DNSKEY. It can return more than one, since
147 * KeyID/Footprints are not guaranteed to be unique.
149 * @param dnskey_rrset
150 * The DNSKEY rrset to search.
152 * 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 @SuppressWarnings("unchecked")
157 private List<DNSKEYRecord> findKey(RRset dnskey_rrset, RRSIGRecord signature) {
158 if (!signature.getSigner().equals(dnskey_rrset.getName())) {
159 log.trace("findKey: could not find appropriate key because " +
160 "incorrect keyset was supplied. Wanted: " +
161 signature.getSigner() + ", got: " + dnskey_rrset.getName());
166 int keyid = signature.getFootprint();
167 int alg = signature.getAlgorithm();
169 List<DNSKEYRecord> res = new ArrayList<DNSKEYRecord>(dnskey_rrset.size());
171 for (Iterator i = dnskey_rrset.rrs(); i.hasNext();) {
172 DNSKEYRecord r = (DNSKEYRecord) i.next();
174 if ((r.getAlgorithm() == alg) && (r.getFootprint() == keyid)) {
179 if (res.size() == 0) {
180 log.trace("findKey: could not find a key matching " +
181 "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.)
194 * The rrset that the signature belongs to.
196 * The signature record to check.
197 * @return A value of DNSSEC.Secure if it looks OK, DNSSEC.Faile if it looks
200 private byte checkSignature(RRset rrset, RRSIGRecord sigrec) {
201 if ((rrset == null) || (sigrec == null)) {
202 return DNSSEC.Failed;
205 if (!rrset.getName().equals(sigrec.getName())) {
206 log.debug("Signature name does not match RRset name");
208 return SecurityStatus.BOGUS;
211 if (rrset.getType() != sigrec.getTypeCovered()) {
212 log.debug("Signature type does not match RRset type");
214 return SecurityStatus.BOGUS;
217 Date now = new Date();
218 Date start = sigrec.getTimeSigned();
219 Date expire = sigrec.getExpire();
221 if (now.before(start)) {
222 log.debug("Signature is not yet valid");
224 return SecurityStatus.BOGUS;
227 if (now.after(expire)) {
228 log.debug("Signature has expired (now = " + now +
229 ", sig expires = " + expire);
231 return SecurityStatus.BOGUS;
234 return SecurityStatus.SECURE;
237 public PublicKey parseDNSKEY(DNSKEYRecord key) {
238 AlgEntry ae = (AlgEntry) mAlgorithmMap.get(new Integer(
239 key.getAlgorithm()));
241 if (key.getAlgorithm() != ae.dnssecAlg) {
242 // Recast the DNSKEYRecord in question as one using the offical
243 // algorithm, to work around the lack of alias support in the
245 // KEYConverter class from DNSjava
246 key = new DNSKEYRecord(key.getName(), key.getDClass(),
247 key.getTTL(), key.getFlags(), key.getProtocol(),
248 ae.dnssecAlg, key.getKey());
251 return KEYConverter.parseRecord(key);
255 * Actually cryptographically verify a signature over the rrset. The RRSIG
256 * record must match the rrset being verified (see checkSignature).
259 * The rrset to verify.
261 * The signature to verify with.
263 * The (public) key associated with the RRSIG record.
264 * @return A security status code: SECURE if it worked, BOGUS if not,
265 * UNCHECKED if we just couldn't actually do the function.
267 public byte verifySignature(RRset rrset, RRSIGRecord sigrec,
270 PublicKey pk = parseDNSKEY(key);
274 "Could not convert DNSKEY record to a JCA public key: " +
277 return SecurityStatus.UNCHECKED;
280 byte [] data = SignUtils.generateSigData(rrset, sigrec);
282 Signature signer = getSignature(sigrec.getAlgorithm());
284 if (signer == null) {
285 return SecurityStatus.BOGUS;
288 signer.initVerify(pk);
291 byte [] sig = sigrec.getSignature();
293 if (isDSA(sigrec.getAlgorithm())) {
294 sig = SignUtils.convertDSASignature(sig);
297 if (!signer.verify(sig)) {
298 log.info("Signature failed to verify cryptographically");
299 log.debug("Failed signature: " + sigrec);
301 return SecurityStatus.BOGUS;
304 log.trace("Signature verified: " + sigrec);
306 return SecurityStatus.SECURE;
307 } catch (IOException e) {
308 log.error("I/O error", e);
309 } catch (GeneralSecurityException e) {
310 log.error("Security error", e);
313 // FIXME: Since I'm not sure what would cause an exception here (failure
314 // to have the required crypto?)
315 // We default to UNCHECKED instead of BOGUS. This could be wrong.
316 return SecurityStatus.UNCHECKED;
320 * Verify an RRset against a particular signature.
322 * @return DNSSEC.Secure if the signature verfied, DNSSEC.Failed if it did
323 * not verify (for any reason), and DNSSEC.Insecure if verification
324 * could not be completed (usually because the public key was not
327 public byte verifySignature(RRset rrset, RRSIGRecord sigrec, RRset key_rrset) {
328 byte result = checkSignature(rrset, sigrec);
330 if (result != SecurityStatus.SECURE) {
334 List<DNSKEYRecord> keys = findKey(key_rrset, sigrec);
337 log.trace("could not find appropriate key");
339 return SecurityStatus.BOGUS;
342 byte status = SecurityStatus.UNCHECKED;
344 for (DNSKEYRecord key : keys) {
345 status = verifySignature(rrset, sigrec, key);
347 if (status == SecurityStatus.SECURE) {
356 * Verifies an RRset. This routine does not modify the RRset. This RRset is
357 * presumed to be verifiable, and the correct DNSKEY rrset is presumed to
360 * @return SecurityStatus.SECURE if the rrest verified positively,
361 * SecurityStatus.BOGUS otherwise.
363 @SuppressWarnings("unchecked")
364 public byte verify(RRset rrset, RRset key_rrset) {
365 Iterator i = rrset.sigs();
368 log.info("RRset failed to verify due to lack of signatures");
370 return SecurityStatus.BOGUS;
373 while (i.hasNext()) {
374 RRSIGRecord sigrec = (RRSIGRecord) i.next();
376 byte res = verifySignature(rrset, sigrec, key_rrset);
378 if (res == SecurityStatus.SECURE) {
383 log.info("RRset failed to verify: all signatures were BOGUS");
385 return SecurityStatus.BOGUS;
389 * Verify an RRset against a single DNSKEY. Use this when you must be
390 * certain that an RRset signed and verifies with a particular DNSKEY (as
391 * opposed to a particular DNSKEY rrset).
394 * The rrset to verify.
396 * The DNSKEY to verify with.
397 * @return SecurityStatus.SECURE if the rrset verified, BOGUS otherwise.
399 @SuppressWarnings("unchecked")
400 public byte verify(RRset rrset, DNSKEYRecord dnskey) {
401 // Iterate over RRSIGS
402 Iterator i = rrset.sigs();
405 log.info("RRset failed to verify due to lack of signatures");
407 return SecurityStatus.BOGUS;
410 while (i.hasNext()) {
411 RRSIGRecord sigrec = (RRSIGRecord) i.next();
413 // Skip RRSIGs that do not match our given key's footprint.
414 if (sigrec.getFootprint() != dnskey.getFootprint()) {
418 byte res = verifySignature(rrset, sigrec, dnskey);
420 if (res == SecurityStatus.SECURE) {
425 log.info("RRset failed to verify: all signatures were BOGUS");
427 return SecurityStatus.BOGUS;
430 public boolean supportsAlgorithm(int algorithm) {
431 return mAlgorithmMap.containsKey(new Integer(algorithm));
434 public boolean supportsAlgorithm(Name private_id) {
435 return mAlgorithmMap.containsKey(private_id);
438 public int baseAlgorithm(int algorithm) {
448 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(new Integer(algorithm));
461 /** @return the appropriate Signature object for this keypair. */
462 private Signature getSignature(int algorithm) {
466 AlgEntry entry = (AlgEntry) mAlgorithmMap.get(new Integer(algorithm));
469 log.info("DNSSEC algorithm " + algorithm + " not recognized.");
474 // TODO: should we cache the instance?
475 s = Signature.getInstance(entry.jcaName);
476 } catch (NoSuchAlgorithmException e) {
477 log.error("error getting Signature object", e);
483 private static class AlgEntry {
484 public String jcaName;
485 public boolean isDSA;
486 public int dnssecAlg;
488 public AlgEntry(String name, int dnssecAlg, boolean isDSA) {
490 this.dnssecAlg = dnssecAlg;
495 // TODO: enable private algorithm support in dnsjava.
496 // Right now, this cannot be used because the DNSKEYRecord object doesn't
498 // the private key name.
499 // private Signature getSignature(Name private_alg)
501 // Signature s = null;
505 // String alg_id = (String) mAlgorithmMap.get(private_alg);
506 // if (alg_id == null)
508 // log.debug("DNSSEC private algorithm '" + private_alg
509 // + "' not recognized.");
513 // s = Signature.getInstance(alg_id);
515 // catch (NoSuchAlgorithmException e)
517 // log.error("error getting Signature object", e);