author | igerasim |
Tue, 12 Nov 2019 01:36:17 -0800 | |
changeset 59024 | b046ba510bbc |
parent 58331 | e4ce29f6094e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58331
e4ce29f6094e
8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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.Asn1Exception; |
|
36 |
import sun.security.util.*; |
|
37 |
import java.net.InetAddress; |
|
38 |
import java.net.Inet4Address; |
|
39 |
import java.net.Inet6Address; |
|
40 |
import java.net.UnknownHostException; |
|
41 |
import java.io.IOException; |
|
32013
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
42 |
import java.util.Arrays; |
2 | 43 |
|
44 |
/** |
|
45 |
* Implements the ASN.1 HostAddress type. |
|
46 |
* |
|
32003 | 47 |
* <pre>{@code |
2 | 48 |
* HostAddress ::= SEQUENCE { |
49 |
* addr-type [0] Int32, |
|
50 |
* address [1] OCTET STRING |
|
51 |
* } |
|
32003 | 52 |
* }</pre> |
2 | 53 |
* |
54 |
* <p> |
|
55 |
* This definition reflects the Network Working Group RFC 4120 |
|
56 |
* specification available at |
|
57 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
|
58 |
* http://www.ietf.org/rfc/rfc4120.txt</a>. |
|
59 |
*/ |
|
60 |
||
61 |
public class HostAddress implements Cloneable { |
|
62 |
int addrType; |
|
63 |
byte[] address = null; |
|
64 |
||
65 |
private static InetAddress localInetAddress; //caches local inet address |
|
66 |
private static final boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG; |
|
67 |
private volatile int hashCode = 0; |
|
68 |
||
69 |
private HostAddress(int dummy) {} |
|
70 |
||
71 |
public Object clone() { |
|
72 |
HostAddress new_hostAddress = new HostAddress(0); |
|
73 |
new_hostAddress.addrType = addrType; |
|
74 |
if (address != null) { |
|
75 |
new_hostAddress.address = address.clone(); |
|
76 |
} |
|
77 |
return new_hostAddress; |
|
78 |
} |
|
79 |
||
80 |
||
81 |
public int hashCode() { |
|
82 |
if (hashCode == 0) { |
|
83 |
int result = 17; |
|
84 |
result = 37*result + addrType; |
|
85 |
if (address != null) { |
|
86 |
for (int i=0; i < address.length; i++) { |
|
87 |
result = 37*result + address[i]; |
|
88 |
} |
|
89 |
} |
|
90 |
hashCode = result; |
|
91 |
} |
|
92 |
return hashCode; |
|
93 |
||
94 |
} |
|
95 |
||
96 |
public boolean equals(Object obj) { |
|
97 |
if (this == obj) { |
|
98 |
return true; |
|
99 |
} |
|
100 |
||
101 |
if (!(obj instanceof HostAddress)) { |
|
102 |
return false; |
|
103 |
} |
|
104 |
||
105 |
HostAddress h = (HostAddress)obj; |
|
106 |
if (addrType != h.addrType || |
|
107 |
(address != null && h.address == null) || |
|
108 |
(address == null && h.address != null)) |
|
109 |
return false; |
|
110 |
if (address != null && h.address != null) { |
|
111 |
if (address.length != h.address.length) |
|
112 |
return false; |
|
113 |
for (int i = 0; i < address.length; i++) |
|
114 |
if (address[i] != h.address[i]) |
|
115 |
return false; |
|
116 |
} |
|
117 |
return true; |
|
118 |
} |
|
119 |
||
120 |
private static synchronized InetAddress getLocalInetAddress() |
|
121 |
throws UnknownHostException { |
|
122 |
||
123 |
if (localInetAddress == null) { |
|
124 |
localInetAddress = InetAddress.getLocalHost(); |
|
125 |
} |
|
126 |
if (localInetAddress == null) { |
|
127 |
throw new UnknownHostException(); |
|
128 |
} |
|
129 |
return (localInetAddress); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Gets the InetAddress of this HostAddress. |
|
134 |
* @return the IP address for this specified host. |
|
32003 | 135 |
* @exception UnknownHostException if no IP address for the host could be found. |
2 | 136 |
* |
137 |
*/ |
|
138 |
public InetAddress getInetAddress() throws UnknownHostException { |
|
139 |
// the type of internet addresses is 2. |
|
140 |
if (addrType == Krb5.ADDRTYPE_INET || |
|
141 |
addrType == Krb5.ADDRTYPE_INET6) { |
|
142 |
return (InetAddress.getByAddress(address)); |
|
143 |
} else { |
|
144 |
// if it is other type (ISO address, XNS address, etc) |
|
145 |
return null; |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
private int getAddrType(InetAddress inetAddress) { |
|
150 |
int addressType = 0; |
|
151 |
if (inetAddress instanceof Inet4Address) |
|
152 |
addressType = Krb5.ADDRTYPE_INET; |
|
153 |
else if (inetAddress instanceof Inet6Address) |
|
154 |
addressType = Krb5.ADDRTYPE_INET6; |
|
155 |
return (addressType); |
|
156 |
} |
|
157 |
||
158 |
// implicit default not in Config.java |
|
159 |
public HostAddress() throws UnknownHostException { |
|
160 |
InetAddress inetAddress = getLocalInetAddress(); |
|
161 |
addrType = getAddrType(inetAddress); |
|
162 |
address = inetAddress.getAddress(); |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Creates a HostAddress from the specified address and address type. |
|
167 |
* |
|
58331
e4ce29f6094e
8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents:
47216
diff
changeset
|
168 |
* Warning: called by nativeccache.c. |
e4ce29f6094e
8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents:
47216
diff
changeset
|
169 |
* |
2 | 170 |
* @param new_addrType the value of the address type which matches the defined |
171 |
* address family constants in the Berkeley Standard |
|
172 |
* Distributions of Unix. |
|
173 |
* @param new_address network address. |
|
174 |
* @exception KrbApErrException if address type and address length do not match defined value. |
|
175 |
* |
|
176 |
*/ |
|
177 |
public HostAddress(int new_addrType, byte[] new_address) |
|
178 |
throws KrbApErrException, UnknownHostException { |
|
179 |
switch(new_addrType) { |
|
180 |
case Krb5.ADDRTYPE_INET: //Internet address |
|
181 |
if (new_address.length != 4) |
|
182 |
throw new KrbApErrException(0, "Invalid Internet address"); |
|
183 |
break; |
|
184 |
case Krb5.ADDRTYPE_CHAOS: |
|
185 |
if (new_address.length != 2) //CHAOSnet address |
|
186 |
throw new KrbApErrException(0, "Invalid CHAOSnet address"); |
|
187 |
break; |
|
188 |
case Krb5.ADDRTYPE_ISO: // ISO address |
|
189 |
break; |
|
190 |
case Krb5.ADDRTYPE_IPX: // XNS address |
|
191 |
if (new_address.length != 6) |
|
192 |
throw new KrbApErrException(0, "Invalid XNS address"); |
|
193 |
break; |
|
194 |
case Krb5.ADDRTYPE_APPLETALK: //AppleTalk DDP address |
|
195 |
if (new_address.length != 3) |
|
196 |
throw new KrbApErrException(0, "Invalid DDP address"); |
|
197 |
break; |
|
198 |
case Krb5.ADDRTYPE_DECNET: //DECnet Phase IV address |
|
199 |
if (new_address.length != 2) |
|
200 |
throw new KrbApErrException(0, "Invalid DECnet Phase IV address"); |
|
201 |
break; |
|
202 |
case Krb5.ADDRTYPE_INET6: //Internet IPv6 address |
|
203 |
if (new_address.length != 16) |
|
204 |
throw new KrbApErrException(0, "Invalid Internet IPv6 address"); |
|
205 |
break; |
|
206 |
} |
|
207 |
||
208 |
addrType = new_addrType; |
|
209 |
if (new_address != null) { |
|
210 |
address = new_address.clone(); |
|
211 |
} |
|
212 |
if (DEBUG) { |
|
213 |
if (addrType == Krb5.ADDRTYPE_INET || |
|
214 |
addrType == Krb5.ADDRTYPE_INET6) { |
|
215 |
System.out.println("Host address is " + |
|
216 |
InetAddress.getByAddress(address)); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
public HostAddress(InetAddress inetAddress) { |
|
222 |
addrType = getAddrType(inetAddress); |
|
223 |
address = inetAddress.getAddress(); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Constructs a host address from a single DER-encoded value. |
|
228 |
* @param encoding a single DER-encoded value. |
|
229 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
230 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
231 |
* |
|
232 |
*/ |
|
233 |
public HostAddress(DerValue encoding) throws Asn1Exception, IOException { |
|
234 |
DerValue der = encoding.getData().getDerValue(); |
|
235 |
if ((der.getTag() & (byte)0x1F) == (byte)0x00) { |
|
236 |
addrType = der.getData().getBigInteger().intValue(); |
|
237 |
} |
|
238 |
else |
|
239 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
240 |
der = encoding.getData().getDerValue(); |
|
241 |
if ((der.getTag() & (byte)0x1F) == (byte)0x01) { |
|
242 |
address = der.getData().getOctetString(); |
|
243 |
} |
|
244 |
else |
|
245 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
246 |
if (encoding.getData().available() > 0) |
|
247 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Encodes a HostAddress object. |
|
252 |
* @return a byte array of encoded HostAddress object. |
|
253 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
254 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
255 |
* |
|
256 |
*/ |
|
257 |
||
258 |
public byte[] asn1Encode() throws Asn1Exception, IOException { |
|
259 |
DerOutputStream bytes = new DerOutputStream(); |
|
260 |
DerOutputStream temp = new DerOutputStream(); |
|
261 |
temp.putInteger(this.addrType); |
|
262 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp); |
|
263 |
temp = new DerOutputStream(); |
|
264 |
temp.putOctetString(address); |
|
265 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp); |
|
266 |
temp = new DerOutputStream(); |
|
267 |
temp.write(DerValue.tag_Sequence, bytes); |
|
268 |
return temp.toByteArray(); |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Parses (unmarshal) a host address from a DER input stream. This form |
|
273 |
* parsing might be used when expanding a value which is part of |
|
274 |
* a constructed sequence and uses explicitly tagged type. |
|
275 |
* |
|
276 |
* @exception Asn1Exception on error. |
|
277 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
278 |
* @param data the Der input stream value, which contains one or more marshaled value. |
|
279 |
* @param explicitTag tag number. |
|
280 |
* @param optional indicates if this data field is optional |
|
281 |
* @return an instance of HostAddress. |
|
282 |
* |
|
283 |
*/ |
|
284 |
public static HostAddress parse(DerInputStream data, byte explicitTag, |
|
285 |
boolean optional) |
|
286 |
throws Asn1Exception, IOException{ |
|
287 |
if ((optional) && |
|
288 |
(((byte)data.peekByte() & (byte)0x1F) != explicitTag)) { |
|
289 |
return null; |
|
290 |
} |
|
291 |
DerValue der = data.getDerValue(); |
|
292 |
if (explicitTag != (der.getTag() & (byte)0x1F)) { |
|
293 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
294 |
} |
|
295 |
else { |
|
296 |
DerValue subDer = der.getData().getDerValue(); |
|
297 |
return new HostAddress(subDer); |
|
298 |
} |
|
299 |
} |
|
300 |
||
32013
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
301 |
@Override |
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
302 |
public String toString() { |
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
303 |
StringBuilder sb = new StringBuilder(); |
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
304 |
sb.append(Arrays.toString(address)); |
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
305 |
sb.append('(').append(addrType).append(')'); |
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
306 |
return sb.toString(); |
e7ad0380f7be
8132111: Do not request for addresses for forwarded TGT
weijun
parents:
32003
diff
changeset
|
307 |
} |
2 | 308 |
} |