Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddresses.java
41161 views
1
/*
2
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
*
28
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
29
* Copyright 1997 The Open Group Research Institute. All rights reserved.
30
*/
31
32
package sun.security.krb5.internal;
33
34
import sun.security.krb5.Config;
35
import sun.security.krb5.PrincipalName;
36
import sun.security.krb5.KrbException;
37
import sun.security.krb5.Asn1Exception;
38
import sun.security.util.*;
39
40
import java.net.*;
41
import java.util.*;
42
import java.io.IOException;
43
import sun.security.krb5.internal.ccache.CCacheOutputStream;
44
45
/**
46
* Implements the ASN.1 HostAddresses type.
47
*
48
* <pre>{@code
49
* HostAddresses -- NOTE: subtly different from rfc1510,
50
* -- but has a value mapping and encodes the same
51
* ::= SEQUENCE OF HostAddress
52
*
53
* HostAddress ::= SEQUENCE {
54
* addr-type [0] Int32,
55
* address [1] OCTET STRING
56
* }
57
* }</pre>
58
*
59
* <p>
60
* This definition reflects the Network Working Group RFC 4120
61
* specification available at
62
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
63
* http://www.ietf.org/rfc/rfc4120.txt</a>.
64
*/
65
66
public class HostAddresses implements Cloneable {
67
private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
68
private HostAddress[] addresses = null;
69
private volatile int hashCode = 0;
70
71
// Warning: called by nativeccache.c
72
public HostAddresses(HostAddress[] new_addresses) throws IOException {
73
if (new_addresses != null) {
74
addresses = new HostAddress[new_addresses.length];
75
for (int i = 0; i < new_addresses.length; i++) {
76
if (new_addresses[i] == null) {
77
throw new IOException("Cannot create a HostAddress");
78
} else {
79
addresses[i] = (HostAddress)new_addresses[i].clone();
80
}
81
}
82
}
83
}
84
85
public HostAddresses() throws UnknownHostException {
86
addresses = new HostAddress[1];
87
addresses[0] = new HostAddress();
88
}
89
90
private HostAddresses(int dummy) {}
91
92
public HostAddresses(PrincipalName serverPrincipal)
93
throws UnknownHostException, KrbException {
94
95
String[] components = serverPrincipal.getNameStrings();
96
97
if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||
98
components.length < 2)
99
throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");
100
101
String host = components[1];
102
InetAddress[] addr = InetAddress.getAllByName(host);
103
HostAddress[] hAddrs = new HostAddress[addr.length];
104
105
for (int i = 0; i < addr.length; i++) {
106
hAddrs[i] = new HostAddress(addr[i]);
107
}
108
109
addresses = hAddrs;
110
}
111
112
public Object clone() {
113
HostAddresses new_hostAddresses = new HostAddresses(0);
114
if (addresses != null) {
115
new_hostAddresses.addresses = new HostAddress[addresses.length];
116
for (int i = 0; i < addresses.length; i++) {
117
new_hostAddresses.addresses[i] =
118
(HostAddress)addresses[i].clone();
119
}
120
}
121
return new_hostAddresses;
122
}
123
124
public boolean inList(HostAddress addr) {
125
if (addresses != null) {
126
for (int i = 0; i < addresses.length; i++)
127
if (addresses[i].equals(addr))
128
return true;
129
}
130
return false;
131
}
132
133
public int hashCode() {
134
if (hashCode == 0) {
135
int result = 17;
136
if (addresses != null) {
137
for (int i=0; i < addresses.length; i++) {
138
result = 37*result + addresses[i].hashCode();
139
}
140
}
141
hashCode = result;
142
}
143
return hashCode;
144
145
}
146
147
148
public boolean equals(Object obj) {
149
if (this == obj) {
150
return true;
151
}
152
153
if (!(obj instanceof HostAddresses)) {
154
return false;
155
}
156
157
HostAddresses addrs = (HostAddresses)obj;
158
if ((addresses == null && addrs.addresses != null) ||
159
(addresses != null && addrs.addresses == null))
160
return false;
161
if (addresses != null && addrs.addresses != null) {
162
if (addresses.length != addrs.addresses.length)
163
return false;
164
for (int i = 0; i < addresses.length; i++)
165
if (!addresses[i].equals(addrs.addresses[i]))
166
return false;
167
}
168
return true;
169
}
170
171
/**
172
* Constructs a new <code>HostAddresses</code> object.
173
* @param encoding a single DER-encoded value.
174
* @exception Asn1Exception if an error occurs while decoding an
175
* ASN1 encoded data.
176
* @exception IOException if an I/O error occurs while reading
177
* encoded data.
178
*/
179
public HostAddresses(DerValue encoding)
180
throws Asn1Exception, IOException {
181
Vector<HostAddress> tempAddresses = new Vector<>();
182
DerValue der = null;
183
while (encoding.getData().available() > 0) {
184
der = encoding.getData().getDerValue();
185
tempAddresses.addElement(new HostAddress(der));
186
}
187
if (tempAddresses.size() > 0) {
188
addresses = new HostAddress[tempAddresses.size()];
189
tempAddresses.copyInto(addresses);
190
}
191
}
192
193
194
/**
195
* Encodes a <code>HostAddresses</code> object.
196
* @return byte array of encoded <code>HostAddresses</code> object.
197
* @exception Asn1Exception if an error occurs while decoding an
198
* ASN1 encoded data.
199
* @exception IOException if an I/O error occurs while reading
200
* encoded data.
201
*/
202
public byte[] asn1Encode() throws Asn1Exception, IOException {
203
DerOutputStream bytes = new DerOutputStream();
204
DerOutputStream temp = new DerOutputStream();
205
206
if (addresses != null && addresses.length > 0) {
207
for (int i = 0; i < addresses.length; i++)
208
bytes.write(addresses[i].asn1Encode());
209
}
210
temp.write(DerValue.tag_Sequence, bytes);
211
return temp.toByteArray();
212
}
213
214
/**
215
* Parse (unmarshal) a <code>HostAddresses</code> from a DER input stream.
216
* This form
217
* parsing might be used when expanding a value which is part of
218
* a constructed sequence and uses explicitly tagged type.
219
*
220
* @exception Asn1Exception if an Asn1Exception occurs.
221
* @param data the Der input stream value, which contains one or more
222
* marshaled value.
223
* @param explicitTag tag number.
224
* @param optional indicates if this data field is optional.
225
* @return an instance of <code>HostAddresses</code>.
226
*/
227
public static HostAddresses parse(DerInputStream data,
228
byte explicitTag, boolean optional)
229
throws Asn1Exception, IOException {
230
if ((optional) &&
231
(((byte)data.peekByte() & (byte)0x1F) != explicitTag))
232
return null;
233
DerValue der = data.getDerValue();
234
if (explicitTag != (der.getTag() & (byte)0x1F)) {
235
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
236
} else {
237
DerValue subDer = der.getData().getDerValue();
238
return new HostAddresses(subDer);
239
}
240
}
241
242
/**
243
* Writes data field values in <code>HostAddresses</code> in FCC
244
* format to a <code>CCacheOutputStream</code>.
245
*
246
* @param cos a <code>CCacheOutputStream</code> to be written to.
247
* @exception IOException if an I/O exception occurs.
248
* @see sun.security.krb5.internal.ccache.CCacheOutputStream
249
*/
250
public void writeAddrs(CCacheOutputStream cos) throws IOException {
251
if (addresses == null || addresses.length == 0) {
252
cos.write32(0);
253
return;
254
}
255
cos.write32(addresses.length);
256
for (int i = 0; i < addresses.length; i++) {
257
cos.write16(addresses[i].addrType);
258
cos.write32(addresses[i].address.length);
259
cos.write(addresses[i].address, 0,
260
addresses[i].address.length);
261
}
262
}
263
264
265
public InetAddress[] getInetAddresses() {
266
267
if (addresses == null || addresses.length == 0)
268
return null;
269
270
ArrayList<InetAddress> ipAddrs = new ArrayList<>(addresses.length);
271
272
for (int i = 0; i < addresses.length; i++) {
273
try {
274
if ((addresses[i].addrType == Krb5.ADDRTYPE_INET) ||
275
(addresses[i].addrType == Krb5.ADDRTYPE_INET6)) {
276
ipAddrs.add(addresses[i].getInetAddress());
277
}
278
} catch (java.net.UnknownHostException e) {
279
// Should not happen since IP address given
280
return null;
281
}
282
}
283
284
InetAddress[] retVal = new InetAddress[ipAddrs.size()];
285
return ipAddrs.toArray(retVal);
286
287
}
288
289
/**
290
* Returns all the IP addresses of the local host.
291
*/
292
public static HostAddresses getLocalAddresses() throws IOException
293
{
294
Set<InetAddress> all = new LinkedHashSet<>();
295
try {
296
if (DEBUG) {
297
System.out.println(">>> KrbKdcReq local addresses are:");
298
}
299
String extra = Config.getInstance().getAll(
300
"libdefaults", "extra_addresses");
301
if (extra != null) {
302
for (String s: extra.split("\\s+")) {
303
all.add(InetAddress.getByName(s));
304
if (DEBUG) {
305
System.out.println(" extra_addresses: "
306
+ InetAddress.getByName(s));
307
}
308
}
309
}
310
for (NetworkInterface ni:
311
Collections.list(NetworkInterface.getNetworkInterfaces())) {
312
if (DEBUG) {
313
System.out.println(" NetworkInterface " + ni + ":");
314
System.out.println(" "
315
+ Collections.list(ni.getInetAddresses()));
316
}
317
all.addAll(Collections.list(ni.getInetAddresses()));
318
}
319
return new HostAddresses(all.toArray(new InetAddress[all.size()]));
320
} catch (Exception exc) {
321
throw new IOException(exc.toString());
322
}
323
}
324
325
/**
326
* Creates a new HostAddresses instance from the supplied list
327
* of InetAddresses.
328
*/
329
public HostAddresses(InetAddress[] inetAddresses)
330
{
331
if (inetAddresses == null)
332
{
333
addresses = null;
334
return;
335
}
336
337
addresses = new HostAddress[inetAddresses.length];
338
for (int i = 0; i < inetAddresses.length; i++)
339
addresses[i] = new HostAddress(inetAddresses[i]);
340
}
341
342
@Override
343
public String toString() {
344
return Arrays.toString(addresses);
345
}
346
}
347
348