-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathIPSECKEYRecord.java
More file actions
259 lines (235 loc) · 6.87 KB
/
Copy pathIPSECKEYRecord.java
File metadata and controls
259 lines (235 loc) · 6.87 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import org.xbill.DNS.utils.base64;
/**
* IPsec Keying Material (RFC 4025)
*
* @author Brian Wellington
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4025">RFC 4025: A Method for Storing IPsec
* Keying Material in DNS</a>
*/
public class IPSECKEYRecord extends Record {
/**
* Algorithm types for IPSECKEY RRs as defined in <a
* href="https://www.iana.org/assignments/ipseckey-rr-parameters/ipseckey-rr-parameters.xhtml#ipseckey-rr-parameters-1">IPSECKEY
* Resource Record Parameters</a>.
*/
public static class Algorithm {
private Algorithm() {}
/**
* A DSA key is present.
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2536">RFC 2536</a>
*/
public static final int DSA = 1;
/**
* A RSA key is present.
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc3110">RFC 3110</a>
*/
public static final int RSA = 2;
/**
* An ECDSA key is present.
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6605">RFC 6605</a>
*/
public static final int ECDSA = 3;
}
/**
* Gateway types for IPSECKEY RRs as defined in <a
* href="https://www.iana.org/assignments/ipseckey-rr-parameters/ipseckey-rr-parameters.xhtml#ipseckey-rr-parameters-2">IPSECKEY
* Resource Record Parameters</a>.
*/
public static class Gateway {
private Gateway() {}
/** No gateway is present */
public static final int None = 0;
/** A 4-byte IPv4 address is present */
public static final int IPv4 = 1;
/** A 16-byte IPv6 address is present */
public static final int IPv6 = 2;
/** A wire-encoded domain name is present */
public static final int Name = 3;
}
private int precedence;
private int gatewayType;
private int algorithmType;
private Object gateway;
private byte[] key;
IPSECKEYRecord() {}
/**
* Creates an IPSECKEY Record from the given data.
*
* @param precedence The record's precedence.
* @param gatewayType The record's gateway type.
* @param algorithmType The record's algorithm type.
* @param gateway The record's gateway.
* @param key The record's public key.
*/
public IPSECKEYRecord(
Name name,
int dclass,
long ttl,
int precedence,
int gatewayType,
int algorithmType,
Object gateway,
byte[] key) {
super(name, Type.IPSECKEY, dclass, ttl);
this.precedence = checkU8("precedence", precedence);
this.gatewayType = checkU8("gatewayType", gatewayType);
this.algorithmType = checkU8("algorithmType", algorithmType);
switch (gatewayType) {
case Gateway.None:
this.gateway = null;
break;
case Gateway.IPv4:
if (!(gateway instanceof InetAddress)) {
throw new IllegalArgumentException("\"gateway\" must be an IPv4 address");
}
this.gateway = gateway;
break;
case Gateway.IPv6:
if (!(gateway instanceof Inet6Address)) {
throw new IllegalArgumentException("\"gateway\" must be an IPv6 address");
}
this.gateway = gateway;
break;
case Gateway.Name:
if (!(gateway instanceof Name)) {
throw new IllegalArgumentException("\"gateway\" must be a DNS name");
}
this.gateway = checkName("gateway", (Name) gateway);
break;
default:
throw new IllegalArgumentException("\"gatewayType\" must be between 0 and 3");
}
this.key = key;
}
@Override
protected void rrFromWire(DNSInput in) throws IOException {
precedence = in.readU8();
gatewayType = in.readU8();
algorithmType = in.readU8();
switch (gatewayType) {
case Gateway.None:
gateway = null;
break;
case Gateway.IPv4:
gateway = InetAddress.getByAddress(in.readByteArray(4));
break;
case Gateway.IPv6:
gateway = InetAddress.getByAddress(in.readByteArray(16));
break;
case Gateway.Name:
gateway = new Name(in);
break;
default:
throw new WireParseException("invalid gateway type");
}
if (in.remaining() > 0) {
key = in.readByteArray();
}
}
@Override
protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
precedence = st.getUInt8();
gatewayType = st.getUInt8();
algorithmType = st.getUInt8();
switch (gatewayType) {
case Gateway.None:
String s = st.getString();
if (!s.equals(".")) {
throw new TextParseException("invalid gateway format");
}
gateway = null;
break;
case Gateway.IPv4:
gateway = st.getAddress(Address.IPv4);
break;
case Gateway.IPv6:
gateway = st.getAddress(Address.IPv6);
break;
case Gateway.Name:
gateway = st.getName(origin);
break;
default:
throw new WireParseException("invalid gateway type");
}
key = st.getBase64(false);
}
@Override
protected String rrToString() {
StringBuilder sb = new StringBuilder();
sb.append(precedence);
sb.append(" ");
sb.append(gatewayType);
sb.append(" ");
sb.append(algorithmType);
sb.append(" ");
switch (gatewayType) {
case Gateway.None:
sb.append(".");
break;
case Gateway.IPv4:
case Gateway.IPv6:
InetAddress gatewayAddr = (InetAddress) gateway;
sb.append(gatewayAddr.getHostAddress());
break;
case Gateway.Name:
sb.append(gateway);
break;
}
if (key != null) {
sb.append(" ");
sb.append(base64.toString(key));
}
return sb.toString();
}
/** Returns the record's precedence. */
public int getPrecedence() {
return precedence;
}
/** Returns the record's gateway type. */
public int getGatewayType() {
return gatewayType;
}
/** Returns the record's algorithm type. */
public int getAlgorithmType() {
return algorithmType;
}
/** Returns the record's gateway. */
public Object getGateway() {
return gateway;
}
/** Returns the record's public key */
public byte[] getKey() {
return key;
}
@Override
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
out.writeU8(precedence);
out.writeU8(gatewayType);
out.writeU8(algorithmType);
switch (gatewayType) {
case Gateway.None:
break;
case Gateway.IPv4:
case Gateway.IPv6:
InetAddress gatewayAddr = (InetAddress) gateway;
out.writeByteArray(gatewayAddr.getAddress());
break;
case Gateway.Name:
Name gatewayName = (Name) gateway;
gatewayName.toWire(out, null, canonical);
break;
}
if (key != null) {
out.writeByteArray(key);
}
}
}