]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/util/DNSUtil.java
add: check CAA entries
[gigi.git] / src / org / cacert / gigi / util / DNSUtil.java
index f1d5f9f3a86f9415aed483486ae857c5764c9dc2..d0c772aff40861bacfced40503d55f21d58f0531 100644 (file)
@@ -9,6 +9,8 @@ import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.InitialDirContext;
 
+import org.cacert.gigi.util.CAA.CAARecord;
+
 public class DNSUtil {
 
     private static InitialDirContext context;
@@ -25,7 +27,7 @@ public class DNSUtil {
 
     public static String[] getNSNames(String name) throws NamingException {
         Attributes dnsLookup = context.getAttributes(name, new String[] {
-            "NS"
+                "NS"
         });
         return extractTextEntries(dnsLookup.get("NS"));
     }
@@ -39,7 +41,7 @@ public class DNSUtil {
         try {
 
             Attributes dnsLookup = context.getAttributes(name, new String[] {
-                "TXT"
+                    "TXT"
             });
             return extractTextEntries(dnsLookup.get("TXT"));
         } finally {
@@ -61,11 +63,32 @@ public class DNSUtil {
 
     public static String[] getMXEntries(String domain) throws NamingException {
         Attributes dnsLookup = context.getAttributes(domain, new String[] {
-            "MX"
+                "MX"
         });
         return extractTextEntries(dnsLookup.get("MX"));
     }
 
+    public static CAARecord[] getCAAEntries(String domain) throws NamingException {
+        Hashtable<String, String> env = new Hashtable<String, String>();
+        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
+        InitialDirContext context = new InitialDirContext(env);
+
+        Attributes dnsLookup = context.getAttributes(domain, new String[] {
+                "257"
+        });
+        Attribute nsRecords = dnsLookup.get("257");
+        if (nsRecords == null) {
+            return new CAARecord[] {};
+        }
+        CAA.CAARecord[] result = new CAA.CAARecord[nsRecords.size()];
+        for (int i = 0; i < result.length; i++) {
+            byte[] rec = (byte[]) nsRecords.get(i);
+
+            result[i] = new CAA.CAARecord(rec);
+        }
+        return result;
+    }
+
     public static void main(String[] args) throws NamingException {
         if (args[0].equals("MX")) {
             System.out.println(Arrays.toString(getMXEntries(args[1])));
@@ -73,6 +96,8 @@ public class DNSUtil {
             System.out.println(Arrays.toString(getNSNames(args[1])));
         } else if (args[0].equals("TXT")) {
             System.out.println(Arrays.toString(getTXTEntries(args[1], args[2])));
+        } else if (args[0].equals("CAA")) {
+            System.out.println(Arrays.toString(getCAAEntries(args[1])));
         }
     }