001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006: import org.xbill.DNS.utils.*;
007:
008: /**
009: * The base class for KEY/DNSKEY records, which have identical formats
010: *
011: * @author Brian Wellington
012: */
013:
014: abstract class KEYBase extends Record {
015:
016: protected int flags, proto, alg;
017: protected byte[] key;
018: protected int footprint = -1;
019:
020: protected KEYBase() {
021: }
022:
023: public KEYBase(Name name, int type, int dclass, long ttl,
024: int flags, int proto, int alg, byte[] key) {
025: super (name, type, dclass, ttl);
026: this .flags = checkU16("flags", flags);
027: this .proto = checkU8("proto", proto);
028: this .alg = checkU8("alg", alg);
029: this .key = key;
030: }
031:
032: void rrFromWire(DNSInput in) throws IOException {
033: flags = in.readU16();
034: proto = in.readU8();
035: alg = in.readU8();
036: if (in.remaining() > 0)
037: key = in.readByteArray();
038: }
039:
040: /** Converts the DNSKEY/KEY Record to a String */
041: String rrToString() {
042: StringBuffer sb = new StringBuffer();
043: sb.append(flags);
044: sb.append(" ");
045: sb.append(proto);
046: sb.append(" ");
047: sb.append(alg);
048: if (key != null) {
049: if (Options.check("multiline")) {
050: sb.append(" (\n");
051: sb.append(base64.formatString(key, 64, "\t", true));
052: sb.append(" ; key_tag = ");
053: sb.append(getFootprint());
054: } else {
055: sb.append(" ");
056: sb.append(base64.toString(key));
057: }
058: }
059: return sb.toString();
060: }
061:
062: /**
063: * Returns the flags describing the key's properties
064: */
065: public int getFlags() {
066: return flags;
067: }
068:
069: /**
070: * Returns the protocol that the key was created for
071: */
072: public int getProtocol() {
073: return proto;
074: }
075:
076: /**
077: * Returns the key's algorithm
078: */
079: public int getAlgorithm() {
080: return alg;
081: }
082:
083: /**
084: * Returns the binary data representing the key
085: */
086: public byte[] getKey() {
087: return key;
088: }
089:
090: /**
091: * Returns the key's footprint (after computing it)
092: */
093: public int getFootprint() {
094: if (footprint >= 0)
095: return footprint;
096:
097: int foot = 0;
098:
099: DNSOutput out = new DNSOutput();
100: rrToWire(out, null, false);
101: byte[] rdata = out.toByteArray();
102:
103: if (alg == DNSSEC.Algorithm.RSAMD5) {
104: int d1 = rdata[rdata.length - 3] & 0xFF;
105: int d2 = rdata[rdata.length - 2] & 0xFF;
106: foot = (d1 << 8) + d2;
107: } else {
108: int i;
109: for (i = 0; i < rdata.length - 1; i += 2) {
110: int d1 = rdata[i] & 0xFF;
111: int d2 = rdata[i + 1] & 0xFF;
112: foot += ((d1 << 8) + d2);
113: }
114: if (i < rdata.length) {
115: int d1 = rdata[i] & 0xFF;
116: foot += (d1 << 8);
117: }
118: foot += ((foot >> 16) & 0xFFFF);
119: }
120: footprint = (foot & 0xFFFF);
121: return footprint;
122: }
123:
124: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
125: out.writeU16(flags);
126: out.writeU8(proto);
127: out.writeU8(alg);
128: if (key != null)
129: out.writeByteArray(key);
130: }
131:
132: }
|