001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006: import java.util.*;
007: import org.xbill.DNS.utils.*;
008:
009: /**
010: * The base class for SIG/RRSIG records, which have identical formats
011: *
012: * @author Brian Wellington
013: */
014:
015: abstract class SIGBase extends Record {
016:
017: protected int covered;
018: protected int alg, labels;
019: protected long origttl;
020: protected Date expire, timeSigned;
021: protected int footprint;
022: protected Name signer;
023: protected byte[] signature;
024:
025: protected SIGBase() {
026: }
027:
028: public SIGBase(Name name, int type, int dclass, long ttl,
029: int covered, int alg, long origttl, Date expire,
030: Date timeSigned, int footprint, Name signer,
031: byte[] signature) {
032: super (name, type, dclass, ttl);
033: Type.check(covered);
034: TTL.check(origttl);
035: this .covered = covered;
036: this .alg = checkU8("alg", alg);
037: this .labels = name.labels() - 1;
038: if (name.isWild())
039: this .labels--;
040: this .origttl = origttl;
041: this .expire = expire;
042: this .timeSigned = timeSigned;
043: this .footprint = checkU16("footprint", footprint);
044: this .signer = checkName("signer", signer);
045: this .signature = signature;
046: }
047:
048: void rrFromWire(DNSInput in) throws IOException {
049: covered = in.readU16();
050: alg = in.readU8();
051: labels = in.readU8();
052: origttl = in.readU32();
053: expire = new Date(1000 * in.readU32());
054: timeSigned = new Date(1000 * in.readU32());
055: footprint = in.readU16();
056: signer = new Name(in);
057: signature = in.readByteArray();
058: }
059:
060: void rdataFromString(Tokenizer st, Name origin) throws IOException {
061: String typeString = st.getString();
062: covered = Type.value(typeString);
063: if (covered < 0)
064: throw st.exception("Invalid type: " + typeString);
065: String algString = st.getString();
066: alg = DNSSEC.Algorithm.value(algString);
067: if (alg < 0)
068: throw st.exception("Invalid algorithm: " + algString);
069: labels = st.getUInt8();
070: origttl = st.getTTL();
071: expire = FormattedTime.parse(st.getString());
072: timeSigned = FormattedTime.parse(st.getString());
073: footprint = st.getUInt16();
074: signer = st.getName(origin);
075: signature = st.getBase64();
076: }
077:
078: /** Converts the RRSIG/SIG Record to a String */
079: String rrToString() {
080: StringBuffer sb = new StringBuffer();
081: sb.append(Type.string(covered));
082: sb.append(" ");
083: sb.append(alg);
084: sb.append(" ");
085: sb.append(labels);
086: sb.append(" ");
087: sb.append(origttl);
088: sb.append(" ");
089: if (Options.check("multiline"))
090: sb.append("(\n\t");
091: sb.append(FormattedTime.format(expire));
092: sb.append(" ");
093: sb.append(FormattedTime.format(timeSigned));
094: sb.append(" ");
095: sb.append(footprint);
096: sb.append(" ");
097: sb.append(signer);
098: if (Options.check("multiline")) {
099: sb.append("\n");
100: sb.append(base64.formatString(signature, 64, "\t", true));
101: } else {
102: sb.append(" ");
103: sb.append(base64.toString(signature));
104: }
105: return sb.toString();
106: }
107:
108: /** Returns the RRset type covered by this signature */
109: public int getTypeCovered() {
110: return covered;
111: }
112:
113: /**
114: * Returns the cryptographic algorithm of the key that generated the signature
115: */
116: public int getAlgorithm() {
117: return alg;
118: }
119:
120: /**
121: * Returns the number of labels in the signed domain name. This may be
122: * different than the record's domain name if the record is a wildcard
123: * record.
124: */
125: public int getLabels() {
126: return labels;
127: }
128:
129: /** Returns the original TTL of the RRset */
130: public long getOrigTTL() {
131: return origttl;
132: }
133:
134: /** Returns the time at which the signature expires */
135: public Date getExpire() {
136: return expire;
137: }
138:
139: /** Returns the time at which this signature was generated */
140: public Date getTimeSigned() {
141: return timeSigned;
142: }
143:
144: /** Returns The footprint/key id of the signing key. */
145: public int getFootprint() {
146: return footprint;
147: }
148:
149: /** Returns the owner of the signing key */
150: public Name getSigner() {
151: return signer;
152: }
153:
154: /** Returns the binary data representing the signature */
155: public byte[] getSignature() {
156: return signature;
157: }
158:
159: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
160: out.writeU16(covered);
161: out.writeU8(alg);
162: out.writeU8(labels);
163: out.writeU32(origttl);
164: out.writeU32(expire.getTime() / 1000);
165: out.writeU32(timeSigned.getTime() / 1000);
166: out.writeU16(footprint);
167: signer.toWire(out, null, canonical);
168: out.writeByteArray(signature);
169: }
170:
171: }
|