1// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2
3package org.xbill.DNS;
4
5import java.io.*;
6import java.util.*;
7import 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
15abstract class SIGBase extends Record {
16
17private static final long serialVersionUID = -3738444391533812369L;
18
19protected int covered;
20protected int alg, labels;
21protected long origttl;
22protected Date expire, timeSigned;
23protected int footprint;
24protected Name signer;
25protected byte [] signature;
26
27protected
28SIGBase() {}
29
30public
31SIGBase(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
51void
52rrFromWire(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
64void
65rdataFromString(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 */
84String
85rrToString() {
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 */
116public int
117getTypeCovered() {
118	return covered;
119}
120
121/**
122 * Returns the cryptographic algorithm of the key that generated the signature
123 */
124public int
125getAlgorithm() {
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 */
134public int
135getLabels() {
136	return labels;
137}
138
139/** Returns the original TTL of the RRset */
140public long
141getOrigTTL() {
142	return origttl;
143}
144
145/** Returns the time at which the signature expires */
146public Date
147getExpire() {
148	return expire;
149}
150
151/** Returns the time at which this signature was generated */
152public Date
153getTimeSigned() {
154	return timeSigned;
155}
156
157/** Returns The footprint/key id of the signing key.  */
158public int
159getFootprint() {
160	return footprint;
161}
162
163/** Returns the owner of the signing key */
164public Name
165getSigner() {
166	return signer;
167}
168
169/** Returns the binary data representing the signature */
170public byte []
171getSignature() {
172	return signature;
173}
174
175void
176setSignature(byte [] signature) {
177	this.signature = signature;
178}
179
180void
181rrToWire(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