1 package com.verisign.cl;
4 import java.net.SocketTimeoutException;
7 import org.apache.log4j.BasicConfigurator;
8 import org.apache.log4j.Level;
9 import org.apache.log4j.Logger;
10 import org.xbill.DNS.*;
12 import com.verisign.tat.dnssec.CaptiveValidator;
13 import com.verisign.tat.dnssec.SecurityStatus;
14 import com.verisign.tat.dnssec.Util;
16 public class DNSSECValTool {
19 * Invoke with java -jar dnssecvaltool.jar server=127.0.0.1 \
20 * query_file=queries.txt dnskey_query=net dnskey_query=edu
22 private CaptiveValidator validator;
23 private SimpleResolver resolver;
25 private BufferedReader queryStream;
26 private PrintStream errorStream;
27 private Set<Name> zoneNames;
32 public String queryFile;
33 public String dnskeyFile;
34 public List<String> dnskeyNames;
35 public String errorFile;
36 public long count = 0;
37 public boolean debug = false;
40 validator = new CaptiveValidator();
44 * Convert a query line of the form: <qname> <qtype> <flags> to a request
48 * @return A query message
49 * @throws TextParseException
50 * @throws NameTooLongException
52 private Message queryFromString(String query_line)
53 throws TextParseException, NameTooLongException {
55 String[] tokens = query_line.split("[ \t]+");
61 if (tokens.length < 1) {
64 qname = Name.fromString(tokens[0]);
65 if (!qname.isAbsolute()) {
66 qname = Name.concatenate(qname, Name.root);
69 for (int i = 1; i < tokens.length; i++) {
70 if (tokens[i].startsWith("+")) {
71 // For now, we ignore flags as uninteresting
72 // All queries will get the DO bit anyway
76 int type = Type.value(tokens[i]);
81 int cls = DClass.value(tokens[i]);
94 Message query = Message.newQuery(Record.newRecord(qname, qtype, qclass));
100 * Fetch the next query from either the command line or the query
103 * @return a query Message, or null if the query list is exhausted
104 * @throws IOException
106 private Message nextQuery() throws IOException {
108 Message res = queryFromString(query);
113 if (queryStream == null && queryFile != null) {
114 queryStream = new BufferedReader(new FileReader(queryFile));
117 if (queryStream != null) {
118 String line = queryStream.readLine();
123 return queryFromString(line);
130 * Figure out the correct zone from the query by comparing the qname to the
131 * list of trusted DNSKEY owner names.
134 * @return a zone name
135 * @throws IOException
137 private Name zoneFromQuery(Message query) throws IOException {
139 if (zoneNames == null) {
140 zoneNames = new HashSet<Name>();
141 for (String key : validator.listTrustedKeys()) {
142 String[] components = key.split("/");
143 Name keyname = Name.fromString(components[0]);
144 if (!keyname.isAbsolute()) {
145 keyname = Name.concatenate(keyname, Name.root);
147 zoneNames.add(keyname);
151 Name qname = query.getQuestion().getName();
152 for (Name n : zoneNames) {
153 if (qname.subdomain(n)) {
161 private Message resolve(Message query) {
163 return resolver.send(query);
164 } catch (SocketTimeoutException e) {
165 System.err.println("Error: timed out querying " + server + " for " +
166 queryToString(query));
167 } catch (IOException e) {
168 System.err.println("Error: error querying " + server + " for " +
169 queryToString(query) + ":" + e.getMessage());
174 private String queryToString(Message query) {
178 Record question = query.getQuestion();
179 return question.getName() + "/" + Type.string(question.getType()) + "/" +
180 DClass.string(question.getDClass());
183 public void execute() throws IOException {
184 // Configure our resolver
185 resolver = new SimpleResolver(server);
186 resolver.setEDNS(0, 4096, Flags.DO, null);
188 // Create our DNSSEC error stream
189 if (errorFile != null) {
190 errorStream = new PrintStream(new FileOutputStream(errorFile, true));
192 errorStream = System.out;
195 // Prime the validator
196 if (dnskeyFile != null) {
197 validator.addTrustedKeysFromFile(dnskeyFile);
199 for (String name : dnskeyNames) {
200 Message query = queryFromString(name + " DNSKEY");
201 Message response = resolve(query);
202 validator.addTrustedKeysFromResponse(response);
206 // Log our set of trusted keys
207 List<String> trustedKeys = validator.listTrustedKeys();
208 if (trustedKeys.size() == 0) {
209 System.err.println("ERROR: no trusted keys found/provided.");
213 for (String key : validator.listTrustedKeys()) {
214 System.out.println("Trusted Key: " + key);
217 // Iterate over all queries
218 Message query = nextQuery();
223 while (query != null) {
225 Name zone = zoneFromQuery(query);
226 // Skip queries in zones that we don't have keys for
229 System.out.println("DEBUG: skipping query " + queryToString(query));
236 System.out.println("DEBUG: querying for: " + queryToString(query));
239 Message response = resolve(query);
240 if (response == null) {
241 System.out.println("ERROR: No response for query: " + queryToString(query));
244 byte result = validator.validateMessage(response, zone.toString());
247 System.out.println(response);
251 case SecurityStatus.BOGUS:
252 case SecurityStatus.INVALID:
253 errorStream.println("BOGUS Answer:");
254 errorStream.println("Query: " + queryToString(query));
255 errorStream.println("Response:\n" + response);
256 for (String err : validator.getErrorList()) {
257 errorStream.println("Error: " + err);
259 errorStream.println("");
262 case SecurityStatus.INSECURE:
263 case SecurityStatus.INDETERMINATE:
264 case SecurityStatus.UNCHECKED:
265 errorStream.println("Insecure Answer:");
266 errorStream.println("Query: " + queryToString(query));
267 errorStream.println("Response:\n" + response);
268 for (String err : validator.getErrorList()) {
269 errorStream.println("Error: " + err);
273 case SecurityStatus.SECURE:
274 if (debug) System.out.println("DEBUG: response for " + queryToString(query) + " was valid.");
279 if (++total % 1000 == 0) {
280 System.out.println("Completed " + total + " queries: " +
281 validCount + " valid, " + errorCount + " errors.");
284 if (count > 0 && total >= count) {
286 System.out.println("DEBUG: reached maximum number of queries, exiting");
294 System.out.println("Completed " + total + (total > 1 ? " queries" : " query") +
295 ": " + validCount + " valid, " + errorCount + " errors.");
298 private static void usage() {
299 System.err.println("usage: java -jar dnssecvaltool.jar [..options..]");
300 System.err.println(" server: the DNS server to query.");
301 System.err.println(" query: a name [type [flags]] string.");
302 System.err.println(" query_file: a list of queries, one query per line.");
303 System.err.println(" count: send up to'count' queries, then stop.");
304 System.err.println(" dnskey_file: a file containing DNSKEY RRs to trust.");
305 System.err.println(" dnskey_query: query 'server' for DNSKEY at given name to trust, may repeat.");
306 System.err.println(" error_file: write DNSSEC validation failure details to this file.");
309 public static void main(String[] argv) {
311 // Set up Log4J to just log to console.
312 BasicConfigurator.configure();
313 // And raise the log level quite high
314 Logger rootLogger = Logger.getRootLogger();
315 rootLogger.setLevel(Level.FATAL);
317 DNSSECValTool dr = new DNSSECValTool();
320 // Parse the command line options
321 for (String arg : argv) {
323 if (arg.indexOf('=') < 0) {
324 System.err.println("Unrecognized option: " + arg);
329 String[] split_arg = arg.split("=", 2);
330 String opt = split_arg[0];
331 String optarg = split_arg[1];
333 if (opt.equals("server")) {
335 } else if (opt.equals("query")) {
337 } else if (opt.equals("query_file")) {
338 dr.queryFile = optarg;
339 } else if (opt.equals("count")) {
340 dr.count = Util.parseInt(optarg, 0);
341 } else if (opt.equals("error_file")) {
342 dr.errorFile = optarg;
343 } else if (opt.equals("dnskey_file")) {
344 dr.dnskeyFile = optarg;
345 } else if (opt.equals("dnskey_query")) {
346 if (dr.dnskeyNames == null) {
347 dr.dnskeyNames = new ArrayList<String>();
349 dr.dnskeyNames.add(optarg);
350 } else if (opt.equals("debug")) {
351 dr.debug = Boolean.parseBoolean(optarg);
353 System.err.println("Unrecognized option: " + opt);
359 // Check for minimum usage
360 if (dr.server == null) {
361 System.err.println("'server' must be specified");
365 if (dr.query == null && dr.queryFile == null) {
366 System.err.println("Either 'query' or 'query_file' must be specified");
370 if (dr.dnskeyFile == null && dr.dnskeyNames == null) {
371 System.err.println("Either 'dnskey_file' or 'dnskey_query' must be specified");
379 } catch (Exception e) {