1 // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2
3 package org.xbill.DNS;
4
5 import java.io.*;
6 import java.util.*;
7 import org.xbill.DNS.utils.*;
8
9 /**
10 * The base class for SIG/RRSIG records, which have identical formats
11 *
12 * @author Brian Wellington
13 */
14
15 abstract class SIGBase extends Record {
16
17 private static final long serialVersionUID = -3738444391533812369L;
18
19 protected int covered;
20 protected int alg, labels;
21 protected long origttl;
22 protected Date expire, timeSigned;
23 protected int footprint;
24 protected Name signer;
25 protected byte [] signature;
26
27 protected
SIGBase()28 SIGBase() {}
29
30 public
SIGBase(Name name, int type, int dclass, long ttl, int covered, int alg, long origttl, Date expire, Date timeSigned, int footprint, Name signer, byte [] signature)31 SIGBase(Name name, int type, int dclass, long ttl, int covered, int alg,
32 long origttl, Date expire, Date timeSigned, int footprint, Name signer,
33 byte [] signature)
34 {
35 super(name, type, dclass, ttl);
36 Type.check(covered);
37 TTL.check(origttl);
38 this.covered = covered;
39 this.alg = checkU8("alg", alg);
40 this.labels = name.labels() - 1;
41 if (name.isWild())
42 this.labels--;
43 this.origttl = origttl;
44 this.expire = expire;
45 this.timeSigned = timeSigned;
46 this.footprint = checkU16("footprint", footprint);
47 this.signer = checkName("signer", signer);
48 this.signature = signature;
49 }
50
51 void
rrFromWire(DNSInput in)52 rrFromWire(DNSInput in) throws IOException {
53 covered = in.readU16();
54 alg = in.readU8();
55 labels = in.readU8();
56 origttl = in.readU32();
57 expire = new Date(1000 * in.readU32());
58 timeSigned = new Date(1000 * in.readU32());
59 footprint = in.readU16();
60 signer = new Name(in);
61 signature = in.readByteArray();
62 }
63
64 void
rdataFromString(Tokenizer st, Name origin)65 rdataFromString(Tokenizer st, Name origin) throws IOException {
66 String typeString = st.getString();
67 covered = Type.value(typeString);
68 if (covered < 0)
69 throw st.exception("Invalid type: " + typeString);
70 String algString = st.getString();
71 alg = DNSSEC.Algorithm.value(algString);
72 if (alg < 0)
73 throw st.exception("Invalid algorithm: " + algString);
74 labels = st.getUInt8();
75 origttl = st.getTTL();
76 expire = FormattedTime.parse(st.getString());
77 timeSigned = FormattedTime.parse(st.getString());
78 footprint = st.getUInt16();
79 signer = st.getName(origin);
80 signature = st.getBase64();
81 }
82
83 /** Converts the RRSIG/SIG Record to a String */
84 String
rrToString()85 rrToString() {
86 StringBuffer sb = new StringBuffer();
87 sb.append (Type.string(covered));
88 sb.append (" ");
89 sb.append (alg);
90 sb.append (" ");
91 sb.append (labels);
92 sb.append (" ");
93 sb.append (origttl);
94 sb.append (" ");
95 if (Options.check("multiline"))
96 sb.append ("(\n\t");
97 sb.append (FormattedTime.format(expire));
98 sb.append (" ");
99 sb.append (FormattedTime.format(timeSigned));
100 sb.append (" ");
101 sb.append (footprint);
102 sb.append (" ");
103 sb.append (signer);
104 if (Options.check("multiline")) {
105 sb.append("\n");
106 sb.append(base64.formatString(signature, 64, "\t",
107 true));
108 } else {
109 sb.append (" ");
110 sb.append(base64.toString(signature));
111 }
112 return sb.toString();
113 }
114
115 /** Returns the RRset type covered by this signature */
116 public int
getTypeCovered()117 getTypeCovered() {
118 return covered;
119 }
120
121 /**
122 * Returns the cryptographic algorithm of the key that generated the signature
123 */
124 public int
getAlgorithm()125 getAlgorithm() {
126 return alg;
127 }
128
129 /**
130 * Returns the number of labels in the signed domain name. This may be
131 * different than the record's domain name if the record is a wildcard
132 * record.
133 */
134 public int
getLabels()135 getLabels() {
136 return labels;
137 }
138
139 /** Returns the original TTL of the RRset */
140 public long
getOrigTTL()141 getOrigTTL() {
142 return origttl;
143 }
144
145 /** Returns the time at which the signature expires */
146 public Date
getExpire()147 getExpire() {
148 return expire;
149 }
150
151 /** Returns the time at which this signature was generated */
152 public Date
getTimeSigned()153 getTimeSigned() {
154 return timeSigned;
155 }
156
157 /** Returns The footprint/key id of the signing key. */
158 public int
getFootprint()159 getFootprint() {
160 return footprint;
161 }
162
163 /** Returns the owner of the signing key */
164 public Name
getSigner()165 getSigner() {
166 return signer;
167 }
168
169 /** Returns the binary data representing the signature */
170 public byte []
getSignature()171 getSignature() {
172 return signature;
173 }
174
175 void
setSignature(byte [] signature)176 setSignature(byte [] signature) {
177 this.signature = signature;
178 }
179
180 void
rrToWire(DNSOutput out, Compression c, boolean canonical)181 rrToWire(DNSOutput out, Compression c, boolean canonical) {
182 out.writeU16(covered);
183 out.writeU8(alg);
184 out.writeU8(labels);
185 out.writeU32(origttl);
186 out.writeU32(expire.getTime() / 1000);
187 out.writeU32(timeSigned.getTime() / 1000);
188 out.writeU16(footprint);
189 signer.toWire(out, null, canonical);
190 out.writeByteArray(signature);
191 }
192
193 }
194