home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / TSIG.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  6.8 KB  |  254 lines

  1. package org.xbill.DNS;
  2.  
  3. import java.util.Date;
  4. import org.xbill.DNS.utils.base16;
  5. import org.xbill.DNS.utils.base64;
  6. import org.xbill.DNS.utils.hmacSigner;
  7.  
  8. public class TSIG {
  9.    public static final Name HMAC = Name.fromConstantString("HMAC-MD5.SIG-ALG.REG.INT.");
  10.    public static final short FUDGE = 300;
  11.    private Name name;
  12.    private Name alg;
  13.    private byte[] key;
  14.  
  15.    static {
  16.       if (Options.check("verbosehmac")) {
  17.          hmacSigner.verbose = true;
  18.       }
  19.  
  20.    }
  21.  
  22.    public TSIG(Name name, byte[] key) {
  23.       this.name = name;
  24.       this.alg = HMAC;
  25.       this.key = key;
  26.    }
  27.  
  28.    public TSIG(String name, String key) {
  29.       byte[] keyArray;
  30.       if (key.length() > 1 && key.charAt(0) == ':') {
  31.          keyArray = base16.fromString(key.substring(1));
  32.       } else {
  33.          keyArray = base64.fromString(key);
  34.       }
  35.  
  36.       if (keyArray == null) {
  37.          throw new IllegalArgumentException("Invalid TSIG key string");
  38.       } else {
  39.          Name keyname;
  40.          try {
  41.             keyname = Name.fromString(name, Name.root);
  42.          } catch (TextParseException var6) {
  43.             throw new IllegalArgumentException("Invalid TSIG key name");
  44.          }
  45.  
  46.          this.name = keyname;
  47.          this.alg = HMAC;
  48.          this.key = keyArray;
  49.       }
  50.    }
  51.  
  52.    public TSIGRecord generate(Message m, byte[] b, int error, TSIGRecord old) {
  53.       Date timeSigned;
  54.       if (error != 18) {
  55.          timeSigned = new Date();
  56.       } else {
  57.          timeSigned = old.getTimeSigned();
  58.       }
  59.  
  60.       hmacSigner h = null;
  61.       if (error == 0 || error == 18) {
  62.          h = new hmacSigner(this.key);
  63.       }
  64.  
  65.       int fudge = Options.intValue("tsigfudge");
  66.       if (fudge < 0 || fudge > 32767) {
  67.          fudge = 300;
  68.       }
  69.  
  70.       if (old != null) {
  71.          DNSOutput out = new DNSOutput();
  72.          out.writeU16(old.getSignature().length);
  73.          if (h != null) {
  74.             h.addData(out.toByteArray());
  75.             h.addData(old.getSignature());
  76.          }
  77.       }
  78.  
  79.       if (h != null) {
  80.          h.addData(b);
  81.       }
  82.  
  83.       DNSOutput out = new DNSOutput();
  84.       this.name.toWireCanonical(out);
  85.       out.writeU16(255);
  86.       out.writeU32(0L);
  87.       this.alg.toWireCanonical(out);
  88.       long time = timeSigned.getTime() / 1000L;
  89.       int timeHigh = (int)(time >> 32);
  90.       long timeLow = time & 4294967295L;
  91.       out.writeU16(timeHigh);
  92.       out.writeU32(timeLow);
  93.       out.writeU16(fudge);
  94.       out.writeU16(error);
  95.       out.writeU16(0);
  96.       if (h != null) {
  97.          h.addData(out.toByteArray());
  98.       }
  99.  
  100.       byte[] signature;
  101.       if (h != null) {
  102.          signature = h.sign();
  103.       } else {
  104.          signature = new byte[0];
  105.       }
  106.  
  107.       byte[] other = (byte[])null;
  108.       if (error == 18) {
  109.          out = new DNSOutput();
  110.          time = (new Date()).getTime() / 1000L;
  111.          timeHigh = (int)(time >> 32);
  112.          timeLow = time & 4294967295L;
  113.          out.writeU16(timeHigh);
  114.          out.writeU32(timeLow);
  115.          other = out.toByteArray();
  116.       }
  117.  
  118.       return new TSIGRecord(this.name, 255, 0L, this.alg, timeSigned, fudge, signature, m.getHeader().getID(), error, other);
  119.    }
  120.  
  121.    public void apply(Message m, int error, TSIGRecord old) {
  122.       Record r = this.generate(m, m.toWire(), error, old);
  123.       m.addRecord(r, 3);
  124.       m.tsigState = 3;
  125.    }
  126.  
  127.    public void apply(Message m, TSIGRecord old) {
  128.       this.apply(m, 0, old);
  129.    }
  130.  
  131.    public void applyStream(Message m, TSIGRecord old, boolean first) {
  132.       if (first) {
  133.          this.apply(m, old);
  134.       } else {
  135.          Date timeSigned = new Date();
  136.          hmacSigner h = new hmacSigner(this.key);
  137.          int fudge = Options.intValue("tsigfudge");
  138.          if (fudge < 0 || fudge > 32767) {
  139.             fudge = 300;
  140.          }
  141.  
  142.          DNSOutput out = new DNSOutput();
  143.          out.writeU16(old.getSignature().length);
  144.          h.addData(out.toByteArray());
  145.          h.addData(old.getSignature());
  146.          h.addData(m.toWire());
  147.          out = new DNSOutput();
  148.          long time = timeSigned.getTime() / 1000L;
  149.          int timeHigh = (int)(time >> 32);
  150.          long timeLow = time & 4294967295L;
  151.          out.writeU16(timeHigh);
  152.          out.writeU32(timeLow);
  153.          out.writeU16(fudge);
  154.          h.addData(out.toByteArray());
  155.          byte[] signature = h.sign();
  156.          byte[] other = (byte[])null;
  157.          Record r = new TSIGRecord(this.name, 255, 0L, this.alg, timeSigned, fudge, signature, m.getHeader().getID(), 0, other);
  158.          m.addRecord(r, 3);
  159.          m.tsigState = 3;
  160.       }
  161.    }
  162.  
  163.    public byte verify(Message m, byte[] b, int length, TSIGRecord old) {
  164.       TSIGRecord tsig = m.getTSIG();
  165.       hmacSigner h = new hmacSigner(this.key);
  166.       if (tsig == null) {
  167.          return 1;
  168.       } else if (tsig.getName().equals(this.name) && tsig.getAlgorithm().equals(this.alg)) {
  169.          long now = System.currentTimeMillis();
  170.          long then = tsig.getTimeSigned().getTime();
  171.          long fudge = (long)tsig.getFudge();
  172.          if (Math.abs(now - then) > fudge * 1000L) {
  173.             if (Options.check("verbose")) {
  174.                System.err.println("BADTIME failure");
  175.             }
  176.  
  177.             return 18;
  178.          } else {
  179.             if (old != null && tsig.getError() != 17 && tsig.getError() != 16) {
  180.                DNSOutput out = new DNSOutput();
  181.                out.writeU16(old.getSignature().length);
  182.                h.addData(out.toByteArray());
  183.                h.addData(old.getSignature());
  184.             }
  185.  
  186.             m.getHeader().decCount(3);
  187.             byte[] header = m.getHeader().toWire();
  188.             m.getHeader().incCount(3);
  189.             h.addData(header);
  190.             int len = m.tsigstart - header.length;
  191.             h.addData(b, header.length, len);
  192.             DNSOutput out = new DNSOutput();
  193.             tsig.getName().toWireCanonical(out);
  194.             out.writeU16(tsig.dclass);
  195.             out.writeU32(tsig.ttl);
  196.             tsig.getAlgorithm().toWireCanonical(out);
  197.             long time = tsig.getTimeSigned().getTime() / 1000L;
  198.             int timeHigh = (int)(time >> 32);
  199.             long timeLow = time & 4294967295L;
  200.             out.writeU16(timeHigh);
  201.             out.writeU32(timeLow);
  202.             out.writeU16(tsig.getFudge());
  203.             out.writeU16(tsig.getError());
  204.             if (tsig.getOther() != null) {
  205.                out.writeU16(tsig.getOther().length);
  206.                out.writeByteArray(tsig.getOther());
  207.             } else {
  208.                out.writeU16(0);
  209.             }
  210.  
  211.             h.addData(out.toByteArray());
  212.             if (h.verify(tsig.getSignature())) {
  213.                return 0;
  214.             } else {
  215.                if (Options.check("verbose")) {
  216.                   System.err.println("BADSIG failure");
  217.                }
  218.  
  219.                return 16;
  220.             }
  221.          }
  222.       } else {
  223.          if (Options.check("verbose")) {
  224.             System.err.println("BADKEY failure");
  225.          }
  226.  
  227.          return 17;
  228.       }
  229.    }
  230.  
  231.    public int verify(Message m, byte[] b, TSIGRecord old) {
  232.       return this.verify(m, b, b.length, old);
  233.    }
  234.  
  235.    public int recordLength() {
  236.       return this.name.length() + 10 + HMAC.length() + 8 + 18 + 4 + 8;
  237.    }
  238.  
  239.    // $FF: synthetic method
  240.    static byte[] access$0(TSIG var0) {
  241.       return var0.key;
  242.    }
  243.  
  244.    // $FF: synthetic method
  245.    static Name access$1(TSIG var0) {
  246.       return var0.name;
  247.    }
  248.  
  249.    // $FF: synthetic method
  250.    static Name access$2(TSIG var0) {
  251.       return var0.alg;
  252.    }
  253. }
  254.