1// Copyright (c) 2002-2004 Brian Wellington (bwelling@xbill.org)
2
3package org.xbill.DNS;
4
5import java.io.*;
6import org.xbill.DNS.utils.*;
7
8/**
9 * DS - contains a Delegation Signer record, which acts as a
10 * placeholder for KEY records in the parent zone.
11 * @see DNSSEC
12 *
13 * @author David Blacka
14 * @author Brian Wellington
15 */
16
17public class DSRecord extends Record {
18
19public static class Digest {
20	private Digest() {}
21
22	/** SHA-1 */
23	public static final int SHA1 = 1;
24
25	/** SHA-256 */
26	public static final int SHA256 = 2;
27
28	/** SHA-384 */
29	public static final int SHA384 = 4;
30}
31
32public static final int SHA1_DIGEST_ID = Digest.SHA1;
33public static final int SHA256_DIGEST_ID = Digest.SHA256;
34public static final int SHA384_DIGEST_ID = Digest.SHA384;
35
36private static final long serialVersionUID = -9001819329700081493L;
37
38private int footprint;
39private int alg;
40private int digestid;
41private byte [] digest;
42
43DSRecord() {}
44
45Record
46getObject() {
47	return new DSRecord();
48}
49
50/**
51 * Creates a DS Record from the given data
52 * @param footprint The original KEY record's footprint (keyid).
53 * @param alg The original key algorithm.
54 * @param digestid The digest id code.
55 * @param digest A hash of the original key.
56 */
57public
58DSRecord(Name name, int dclass, long ttl, int footprint, int alg,
59	 int digestid, byte [] digest)
60{
61	super(name, Type.DS, dclass, ttl);
62	this.footprint = checkU16("footprint", footprint);
63	this.alg = checkU8("alg", alg);
64	this.digestid = checkU8("digestid", digestid);
65	this.digest = digest;
66}
67
68/**
69 * Creates a DS Record from the given data
70 * @param digestid The digest id code.
71 * @param key The key to digest
72 */
73public
74DSRecord(Name name, int dclass, long ttl, int digestid, DNSKEYRecord key)
75{
76	this(name, dclass, ttl, key.getFootprint(), key.getAlgorithm(),
77	     digestid, DNSSEC.generateDSDigest(key, digestid));
78}
79
80void
81rrFromWire(DNSInput in) throws IOException {
82	footprint = in.readU16();
83	alg = in.readU8();
84	digestid = in.readU8();
85	digest = in.readByteArray();
86}
87
88void
89rdataFromString(Tokenizer st, Name origin) throws IOException {
90	footprint = st.getUInt16();
91	alg = st.getUInt8();
92	digestid = st.getUInt8();
93	digest = st.getHex();
94}
95
96/**
97 * Converts rdata to a String
98 */
99String
100rrToString() {
101	StringBuffer sb = new StringBuffer();
102	sb.append(footprint);
103	sb.append(" ");
104	sb.append(alg);
105	sb.append(" ");
106	sb.append(digestid);
107	if (digest != null) {
108		sb.append(" ");
109		sb.append(base16.toString(digest));
110	}
111
112	return sb.toString();
113}
114
115/**
116 * Returns the key's algorithm.
117 */
118public int
119getAlgorithm() {
120	return alg;
121}
122
123/**
124 *  Returns the key's Digest ID.
125 */
126public int
127getDigestID()
128{
129	return digestid;
130}
131
132/**
133 * Returns the binary hash of the key.
134 */
135public byte []
136getDigest() {
137	return digest;
138}
139
140/**
141 * Returns the key's footprint.
142 */
143public int
144getFootprint() {
145	return footprint;
146}
147
148void
149rrToWire(DNSOutput out, Compression c, boolean canonical) {
150	out.writeU16(footprint);
151	out.writeU8(alg);
152	out.writeU8(digestid);
153	if (digest != null)
154		out.writeByteArray(digest);
155}
156
157}
158