aboutsummaryrefslogtreecommitdiff
path: root/src/org/xbill/DNS/KEYBase.java
blob: 59a2c6c06cd563022a48ccdeb997131c18db4d90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS;

import java.io.*;
import java.security.PublicKey;

import org.xbill.DNS.utils.*;

/**
 * The base class for KEY/DNSKEY records, which have identical formats 
 *
 * @author Brian Wellington
 */

abstract class KEYBase extends Record {

private static final long serialVersionUID = 3469321722693285454L;

protected int flags, proto, alg;
protected byte [] key;
protected int footprint = -1;
protected PublicKey publicKey = null;

protected
KEYBase() {}

public
KEYBase(Name name, int type, int dclass, long ttl, int flags, int proto,
	int alg, byte [] key)
{
	super(name, type, dclass, ttl);
	this.flags = checkU16("flags", flags);
	this.proto = checkU8("proto", proto);
	this.alg = checkU8("alg", alg);
	this.key = key;
}

void
rrFromWire(DNSInput in) throws IOException {
	flags = in.readU16();
	proto = in.readU8();
	alg = in.readU8();
	if (in.remaining() > 0)
		key = in.readByteArray();
}

/** Converts the DNSKEY/KEY Record to a String */
String
rrToString() {
	StringBuffer sb = new StringBuffer();
	sb.append(flags);
	sb.append(" ");
	sb.append(proto);
	sb.append(" ");
	sb.append(alg);
	if (key != null) {
		if (Options.check("multiline")) {
			sb.append(" (\n");
			sb.append(base64.formatString(key, 64, "\t", true));
			sb.append(" ; key_tag = ");
			sb.append(getFootprint());
		} else {
			sb.append(" ");
			sb.append(base64.toString(key));
		}
	}
	return sb.toString();
}

/**
 * Returns the flags describing the key's properties
 */
public int
getFlags() {
	return flags;
}

/**
 * Returns the protocol that the key was created for
 */
public int
getProtocol() {
	return proto;
}

/**
 * Returns the key's algorithm
 */
public int
getAlgorithm() {
	return alg;
}

/**
 * Returns the binary data representing the key
 */
public byte []
getKey() {
	return key;
}

/**
 * Returns the key's footprint (after computing it)
 */
public int
getFootprint() {
	if (footprint >= 0)
		return footprint;

	int foot = 0;

	DNSOutput out = new DNSOutput();
	rrToWire(out, null, false);
	byte [] rdata = out.toByteArray();

	if (alg == DNSSEC.Algorithm.RSAMD5) {
		int d1 = rdata[rdata.length - 3] & 0xFF;
		int d2 = rdata[rdata.length - 2] & 0xFF;
		foot = (d1 << 8) + d2;
	}
	else {
		int i; 
		for (i = 0; i < rdata.length - 1; i += 2) {
			int d1 = rdata[i] & 0xFF;
			int d2 = rdata[i + 1] & 0xFF;
			foot += ((d1 << 8) + d2);
		}
		if (i < rdata.length) {
			int d1 = rdata[i] & 0xFF;
			foot += (d1 << 8);
		}
		foot += ((foot >> 16) & 0xFFFF);
	}
	footprint = (foot & 0xFFFF);
	return footprint;
}

/**
 * Returns a PublicKey corresponding to the data in this key.
 * @throws DNSSEC.DNSSECException The key could not be converted.
 */
public PublicKey
getPublicKey() throws DNSSEC.DNSSECException {
	if (publicKey != null)
		return publicKey;

	publicKey = DNSSEC.toPublicKey(this);
	return publicKey;
}

void
rrToWire(DNSOutput out, Compression c, boolean canonical) {
	out.writeU16(flags);
	out.writeU8(proto);
	out.writeU8(alg);
	if (key != null)
		out.writeByteArray(key);
}

}