author | igerasim |
Thu, 09 Jul 2015 10:37:07 +0300 | |
changeset 31538 | 0981099a3e54 |
parent 25859 | 3317bb8137f4 |
child 32003 | acb12269398a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
16847
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, 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; |
|
33 |
||
34 |
import sun.security.krb5.internal.*; |
|
35 |
import sun.security.util.*; |
|
36 |
import java.net.*; |
|
37 |
import java.util.Vector; |
|
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
10141
diff
changeset
|
38 |
import java.util.Locale; |
2 | 39 |
import java.io.IOException; |
40 |
import java.math.BigInteger; |
|
13247 | 41 |
import java.util.Arrays; |
2 | 42 |
import sun.security.krb5.internal.ccache.CCacheOutputStream; |
3949
00603a93b589
6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents:
2918
diff
changeset
|
43 |
import sun.security.krb5.internal.util.KerberosString; |
2 | 44 |
|
45 |
||
46 |
/** |
|
13247 | 47 |
* Implements the ASN.1 PrincipalName type and its realm in a single class. |
48 |
* <xmp> |
|
49 |
* Realm ::= KerberosString |
|
50 |
* |
|
51 |
* PrincipalName ::= SEQUENCE { |
|
52 |
* name-type [0] Int32, |
|
53 |
* name-string [1] SEQUENCE OF KerberosString |
|
54 |
* } |
|
55 |
* </xmp> |
|
56 |
* This class is immutable. |
|
57 |
* @see Realm |
|
2 | 58 |
*/ |
13247 | 59 |
public class PrincipalName implements Cloneable { |
2 | 60 |
|
61 |
//name types |
|
62 |
||
63 |
/** |
|
64 |
* Name type not known |
|
65 |
*/ |
|
66 |
public static final int KRB_NT_UNKNOWN = 0; |
|
67 |
||
68 |
/** |
|
69 |
* Just the name of the principal as in DCE, or for users |
|
70 |
*/ |
|
71 |
public static final int KRB_NT_PRINCIPAL = 1; |
|
72 |
||
73 |
/** |
|
74 |
* Service and other unique instance (krbtgt) |
|
75 |
*/ |
|
76 |
public static final int KRB_NT_SRV_INST = 2; |
|
77 |
||
78 |
/** |
|
79 |
* Service with host name as instance (telnet, rcommands) |
|
80 |
*/ |
|
81 |
public static final int KRB_NT_SRV_HST = 3; |
|
82 |
||
83 |
/** |
|
84 |
* Service with host as remaining components |
|
85 |
*/ |
|
86 |
public static final int KRB_NT_SRV_XHST = 4; |
|
87 |
||
88 |
/** |
|
89 |
* Unique ID |
|
90 |
*/ |
|
91 |
public static final int KRB_NT_UID = 5; |
|
92 |
||
93 |
/** |
|
94 |
* TGS Name |
|
95 |
*/ |
|
96 |
public static final String TGS_DEFAULT_SRV_NAME = "krbtgt"; |
|
97 |
public static final int TGS_DEFAULT_NT = KRB_NT_SRV_INST; |
|
98 |
||
99 |
public static final char NAME_COMPONENT_SEPARATOR = '/'; |
|
100 |
public static final char NAME_REALM_SEPARATOR = '@'; |
|
101 |
public static final char REALM_COMPONENT_SEPARATOR = '.'; |
|
102 |
||
103 |
public static final String NAME_COMPONENT_SEPARATOR_STR = "/"; |
|
104 |
public static final String NAME_REALM_SEPARATOR_STR = "@"; |
|
105 |
public static final String REALM_COMPONENT_SEPARATOR_STR = "."; |
|
106 |
||
13247 | 107 |
// Instance fields. |
108 |
||
109 |
/** |
|
110 |
* The name type, from PrincipalName's name-type field. |
|
111 |
*/ |
|
112 |
private final int nameType; |
|
113 |
||
114 |
/** |
|
115 |
* The name strings, from PrincipalName's name-strings field. This field |
|
116 |
* must be neither null nor empty. Each entry of it must also be neither |
|
117 |
* null nor empty. Make sure to clone the field when it's passed in or out. |
|
118 |
*/ |
|
119 |
private final String[] nameStrings; |
|
120 |
||
121 |
/** |
|
122 |
* The realm this principal belongs to. |
|
123 |
*/ |
|
124 |
private final Realm nameRealm; // not null |
|
125 |
||
126 |
// cached default salt, not used in clone |
|
127 |
private transient String salt = null; |
|
2 | 128 |
|
13247 | 129 |
// There are 3 basic constructors. All other constructors must call them. |
130 |
// All basic constructors must call validateNameStrings. |
|
131 |
// 1. From name components |
|
132 |
// 2. From name |
|
133 |
// 3. From DER encoding |
|
2 | 134 |
|
13247 | 135 |
/** |
136 |
* Creates a PrincipalName. |
|
137 |
*/ |
|
138 |
public PrincipalName(int nameType, String[] nameStrings, Realm nameRealm) { |
|
139 |
if (nameRealm == null) { |
|
140 |
throw new IllegalArgumentException("Null realm not allowed"); |
|
141 |
} |
|
142 |
validateNameStrings(nameStrings); |
|
143 |
this.nameType = nameType; |
|
144 |
this.nameStrings = nameStrings.clone(); |
|
145 |
this.nameRealm = nameRealm; |
|
146 |
} |
|
2 | 147 |
|
13247 | 148 |
// This method is called by Windows NativeCred.c |
149 |
public PrincipalName(String[] nameParts, String realm) throws RealmException { |
|
150 |
this(KRB_NT_UNKNOWN, nameParts, new Realm(realm)); |
|
2 | 151 |
} |
152 |
||
153 |
public PrincipalName(String[] nameParts, int type) |
|
13247 | 154 |
throws IllegalArgumentException, RealmException { |
155 |
this(type, nameParts, Realm.getDefault()); |
|
2 | 156 |
} |
157 |
||
13247 | 158 |
// Validate a nameStrings argument |
159 |
private static void validateNameStrings(String[] ns) { |
|
160 |
if (ns == null) { |
|
161 |
throw new IllegalArgumentException("Null nameStrings not allowed"); |
|
162 |
} |
|
163 |
if (ns.length == 0) { |
|
164 |
throw new IllegalArgumentException("Empty nameStrings not allowed"); |
|
165 |
} |
|
166 |
for (String s: ns) { |
|
167 |
if (s == null) { |
|
168 |
throw new IllegalArgumentException("Null nameString not allowed"); |
|
169 |
} |
|
170 |
if (s.isEmpty()) { |
|
171 |
throw new IllegalArgumentException("Empty nameString not allowed"); |
|
172 |
} |
|
173 |
} |
|
2 | 174 |
} |
175 |
||
176 |
public Object clone() { |
|
5455
19d2395cfe3e
6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents:
3949
diff
changeset
|
177 |
try { |
19d2395cfe3e
6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents:
3949
diff
changeset
|
178 |
PrincipalName pName = (PrincipalName) super.clone(); |
13247 | 179 |
UNSAFE.putObject(this, NAME_STRINGS_OFFSET, nameStrings.clone()); |
5455
19d2395cfe3e
6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents:
3949
diff
changeset
|
180 |
return pName; |
19d2395cfe3e
6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents:
3949
diff
changeset
|
181 |
} catch (CloneNotSupportedException ex) { |
19d2395cfe3e
6856069: PrincipalName.clone() does not invoke super.clone()
weijun
parents:
3949
diff
changeset
|
182 |
throw new AssertionError("Should never happen"); |
2 | 183 |
} |
184 |
} |
|
185 |
||
13247 | 186 |
private static final long NAME_STRINGS_OFFSET; |
187 |
private static final sun.misc.Unsafe UNSAFE; |
|
188 |
static { |
|
189 |
try { |
|
190 |
sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe(); |
|
191 |
NAME_STRINGS_OFFSET = unsafe.objectFieldOffset( |
|
192 |
PrincipalName.class.getDeclaredField("nameStrings")); |
|
193 |
UNSAFE = unsafe; |
|
194 |
} catch (ReflectiveOperationException e) { |
|
195 |
throw new Error(e); |
|
196 |
} |
|
2 | 197 |
} |
198 |
||
13247 | 199 |
@Override |
200 |
public boolean equals(Object o) { |
|
201 |
if (this == o) { |
|
202 |
return true; |
|
2 | 203 |
} |
13247 | 204 |
if (o instanceof PrincipalName) { |
205 |
PrincipalName other = (PrincipalName)o; |
|
206 |
return nameRealm.equals(other.nameRealm) && |
|
207 |
Arrays.equals(nameStrings, other.nameStrings); |
|
2 | 208 |
} |
13247 | 209 |
return false; |
2 | 210 |
} |
211 |
||
212 |
/** |
|
213 |
* Returns the ASN.1 encoding of the |
|
214 |
* <xmp> |
|
215 |
* PrincipalName ::= SEQUENCE { |
|
216 |
* name-type [0] Int32, |
|
217 |
* name-string [1] SEQUENCE OF KerberosString |
|
218 |
* } |
|
219 |
* |
|
220 |
* KerberosString ::= GeneralString (IA5String) |
|
221 |
* </xmp> |
|
222 |
* |
|
223 |
* <p> |
|
224 |
* This definition reflects the Network Working Group RFC 4120 |
|
225 |
* specification available at |
|
226 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
|
227 |
* http://www.ietf.org/rfc/rfc4120.txt</a>. |
|
228 |
* |
|
229 |
* @param encoding a Der-encoded data. |
|
13247 | 230 |
* @param realm the realm for this name |
2 | 231 |
* @exception Asn1Exception if an error occurs while decoding |
232 |
* an ASN1 encoded data. |
|
233 |
* @exception Asn1Exception if there is an ASN1 encoding error |
|
234 |
* @exception IOException if an I/O error occurs |
|
235 |
* @exception IllegalArgumentException if encoding is null |
|
236 |
* reading encoded data. |
|
237 |
*/ |
|
13247 | 238 |
public PrincipalName(DerValue encoding, Realm realm) |
239 |
throws Asn1Exception, IOException { |
|
240 |
if (realm == null) { |
|
241 |
throw new IllegalArgumentException("Null realm not allowed"); |
|
242 |
} |
|
243 |
nameRealm = realm; |
|
2 | 244 |
DerValue der; |
245 |
if (encoding == null) { |
|
13247 | 246 |
throw new IllegalArgumentException("Null encoding not allowed"); |
2 | 247 |
} |
248 |
if (encoding.getTag() != DerValue.tag_Sequence) { |
|
249 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
250 |
} |
|
251 |
der = encoding.getData().getDerValue(); |
|
252 |
if ((der.getTag() & 0x1F) == 0x00) { |
|
253 |
BigInteger bint = der.getData().getBigInteger(); |
|
254 |
nameType = bint.intValue(); |
|
255 |
} else { |
|
256 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
257 |
} |
|
258 |
der = encoding.getData().getDerValue(); |
|
259 |
if ((der.getTag() & 0x01F) == 0x01) { |
|
260 |
DerValue subDer = der.getData().getDerValue(); |
|
261 |
if (subDer.getTag() != DerValue.tag_SequenceOf) { |
|
262 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
263 |
} |
|
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
264 |
Vector<String> v = new Vector<>(); |
2 | 265 |
DerValue subSubDer; |
266 |
while(subDer.getData().available() > 0) { |
|
267 |
subSubDer = subDer.getData().getDerValue(); |
|
13247 | 268 |
String namePart = new KerberosString(subSubDer).toString(); |
269 |
v.addElement(namePart); |
|
2 | 270 |
} |
13247 | 271 |
nameStrings = new String[v.size()]; |
272 |
v.copyInto(nameStrings); |
|
273 |
validateNameStrings(nameStrings); |
|
2 | 274 |
} else { |
275 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Parse (unmarshal) a <code>PrincipalName</code> from a DER |
|
281 |
* input stream. This form |
|
282 |
* parsing might be used when expanding a value which is part of |
|
283 |
* a constructed sequence and uses explicitly tagged type. |
|
284 |
* |
|
285 |
* @exception Asn1Exception on error. |
|
286 |
* @param data the Der input stream value, which contains one or |
|
287 |
* more marshaled value. |
|
288 |
* @param explicitTag tag number. |
|
289 |
* @param optional indicate if this data field is optional |
|
13247 | 290 |
* @param realm the realm for the name |
291 |
* @return an instance of <code>PrincipalName</code>, or null if the |
|
292 |
* field is optional and missing. |
|
2 | 293 |
*/ |
294 |
public static PrincipalName parse(DerInputStream data, |
|
295 |
byte explicitTag, boolean |
|
13247 | 296 |
optional, |
297 |
Realm realm) |
|
298 |
throws Asn1Exception, IOException, RealmException { |
|
2 | 299 |
|
300 |
if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != |
|
301 |
explicitTag)) |
|
302 |
return null; |
|
303 |
DerValue der = data.getDerValue(); |
|
13247 | 304 |
if (explicitTag != (der.getTag() & (byte)0x1F)) { |
2 | 305 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
13247 | 306 |
} else { |
2 | 307 |
DerValue subDer = der.getData().getDerValue(); |
13247 | 308 |
if (realm == null) { |
309 |
realm = Realm.getDefault(); |
|
310 |
} |
|
311 |
return new PrincipalName(subDer, realm); |
|
2 | 312 |
} |
313 |
} |
|
314 |
||
315 |
||
316 |
// XXX Error checkin consistent with MIT krb5_parse_name |
|
317 |
// Code repetition, realm parsed again by class Realm |
|
13247 | 318 |
private static String[] parseName(String name) { |
2 | 319 |
|
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
320 |
Vector<String> tempStrings = new Vector<>(); |
2 | 321 |
String temp = name; |
322 |
int i = 0; |
|
323 |
int componentStart = 0; |
|
324 |
String component; |
|
325 |
||
326 |
while (i < temp.length()) { |
|
327 |
if (temp.charAt(i) == NAME_COMPONENT_SEPARATOR) { |
|
328 |
/* |
|
329 |
* If this separator is escaped then don't treat it |
|
330 |
* as a separator |
|
331 |
*/ |
|
332 |
if (i > 0 && temp.charAt(i - 1) == '\\') { |
|
333 |
temp = temp.substring(0, i - 1) + |
|
334 |
temp.substring(i, temp.length()); |
|
335 |
continue; |
|
336 |
} |
|
337 |
else { |
|
13247 | 338 |
if (componentStart <= i) { |
2 | 339 |
component = temp.substring(componentStart, i); |
340 |
tempStrings.addElement(component); |
|
341 |
} |
|
342 |
componentStart = i + 1; |
|
343 |
} |
|
13247 | 344 |
} else { |
2 | 345 |
if (temp.charAt(i) == NAME_REALM_SEPARATOR) { |
346 |
/* |
|
347 |
* If this separator is escaped then don't treat it |
|
348 |
* as a separator |
|
349 |
*/ |
|
350 |
if (i > 0 && temp.charAt(i - 1) == '\\') { |
|
351 |
temp = temp.substring(0, i - 1) + |
|
352 |
temp.substring(i, temp.length()); |
|
353 |
continue; |
|
354 |
} else { |
|
355 |
if (componentStart < i) { |
|
356 |
component = temp.substring(componentStart, i); |
|
357 |
tempStrings.addElement(component); |
|
358 |
} |
|
359 |
componentStart = i + 1; |
|
360 |
break; |
|
361 |
} |
|
362 |
} |
|
13247 | 363 |
} |
2 | 364 |
i++; |
365 |
} |
|
366 |
||
13247 | 367 |
if (i == temp.length()) { |
2 | 368 |
component = temp.substring(componentStart, i); |
369 |
tempStrings.addElement(component); |
|
370 |
} |
|
371 |
||
372 |
String[] result = new String[tempStrings.size()]; |
|
373 |
tempStrings.copyInto(result); |
|
374 |
return result; |
|
375 |
} |
|
376 |
||
13247 | 377 |
/** |
378 |
* Constructs a PrincipalName from a string. |
|
379 |
* @param name the name |
|
380 |
* @param type the type |
|
381 |
* @param realm the realm, null if not known. Note that when realm is not |
|
382 |
* null, it will be always used even if there is a realm part in name. When |
|
383 |
* realm is null, will read realm part from name, or try to map a realm |
|
384 |
* (for KRB_NT_SRV_HST), or use the default realm, or fail |
|
385 |
* @throws RealmException |
|
386 |
*/ |
|
387 |
public PrincipalName(String name, int type, String realm) |
|
388 |
throws RealmException { |
|
2 | 389 |
if (name == null) { |
390 |
throw new IllegalArgumentException("Null name not allowed"); |
|
391 |
} |
|
392 |
String[] nameParts = parseName(name); |
|
13247 | 393 |
validateNameStrings(nameParts); |
394 |
if (realm == null) { |
|
395 |
realm = Realm.parseRealmAtSeparator(name); |
|
2 | 396 |
} |
397 |
switch (type) { |
|
398 |
case KRB_NT_SRV_HST: |
|
399 |
if (nameParts.length >= 2) { |
|
2918
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
400 |
String hostName = nameParts[1]; |
2 | 401 |
try { |
2918
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
402 |
// RFC4120 does not recommend canonicalizing a hostname. |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
403 |
// However, for compatibility reason, we will try |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
404 |
// canonicalize it and see if the output looks better. |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
405 |
|
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
406 |
String canonicalized = (InetAddress.getByName(hostName)). |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
407 |
getCanonicalHostName(); |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
408 |
|
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
409 |
// Looks if canonicalized is a longer format of hostName, |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
410 |
// we accept cases like |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
411 |
// bunny -> bunny.rabbit.hole |
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
10141
diff
changeset
|
412 |
if (canonicalized.toLowerCase(Locale.ENGLISH).startsWith( |
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
10141
diff
changeset
|
413 |
hostName.toLowerCase(Locale.ENGLISH)+".")) { |
2918
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
414 |
hostName = canonicalized; |
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
415 |
} |
2 | 416 |
} catch (UnknownHostException e) { |
2918
395b9ffa7cc6
6682516: SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
weijun
parents:
2
diff
changeset
|
417 |
// no canonicalization, use old |
2 | 418 |
} |
10369
e9d2e59e53f0
7059542: JNDI name operations should be locale independent
xuelei
parents:
10141
diff
changeset
|
419 |
nameParts[1] = hostName.toLowerCase(Locale.ENGLISH); |
2 | 420 |
} |
421 |
nameStrings = nameParts; |
|
422 |
nameType = type; |
|
13247 | 423 |
|
424 |
if (realm != null) { |
|
425 |
nameRealm = new Realm(realm); |
|
426 |
} else { |
|
2 | 427 |
// We will try to get realm name from the mapping in |
428 |
// the configuration. If it is not specified |
|
429 |
// we will use the default realm. This nametype does |
|
430 |
// not allow a realm to be specified. The name string must of |
|
431 |
// the form service@host and this is internally changed into |
|
432 |
// service/host by Kerberos |
|
13247 | 433 |
String mapRealm = mapHostToRealm(nameParts[1]); |
434 |
if (mapRealm != null) { |
|
435 |
nameRealm = new Realm(mapRealm); |
|
436 |
} else { |
|
437 |
nameRealm = Realm.getDefault(); |
|
438 |
} |
|
2 | 439 |
} |
440 |
break; |
|
441 |
case KRB_NT_UNKNOWN: |
|
442 |
case KRB_NT_PRINCIPAL: |
|
443 |
case KRB_NT_SRV_INST: |
|
444 |
case KRB_NT_SRV_XHST: |
|
445 |
case KRB_NT_UID: |
|
446 |
nameStrings = nameParts; |
|
447 |
nameType = type; |
|
13247 | 448 |
if (realm != null) { |
449 |
nameRealm = new Realm(realm); |
|
450 |
} else { |
|
451 |
nameRealm = Realm.getDefault(); |
|
452 |
} |
|
2 | 453 |
break; |
454 |
default: |
|
455 |
throw new IllegalArgumentException("Illegal name type"); |
|
456 |
} |
|
457 |
} |
|
458 |
||
13247 | 459 |
public PrincipalName(String name, int type) throws RealmException { |
460 |
this(name, type, (String)null); |
|
461 |
} |
|
462 |
||
2 | 463 |
public PrincipalName(String name) throws RealmException { |
464 |
this(name, KRB_NT_UNKNOWN); |
|
465 |
} |
|
466 |
||
467 |
public PrincipalName(String name, String realm) throws RealmException { |
|
13247 | 468 |
this(name, KRB_NT_UNKNOWN, realm); |
469 |
} |
|
470 |
||
471 |
public static PrincipalName tgsService(String r1, String r2) |
|
472 |
throws KrbException { |
|
473 |
return new PrincipalName(PrincipalName.KRB_NT_SRV_INST, |
|
474 |
new String[] {PrincipalName.TGS_DEFAULT_SRV_NAME, r1}, |
|
475 |
new Realm(r2)); |
|
2 | 476 |
} |
477 |
||
478 |
public String getRealmAsString() { |
|
479 |
return getRealmString(); |
|
480 |
} |
|
481 |
||
482 |
public String getPrincipalNameAsString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
483 |
StringBuilder temp = new StringBuilder(nameStrings[0]); |
2 | 484 |
for (int i = 1; i < nameStrings.length; i++) |
485 |
temp.append(nameStrings[i]); |
|
486 |
return temp.toString(); |
|
487 |
} |
|
488 |
||
489 |
public int hashCode() { |
|
490 |
return toString().hashCode(); |
|
491 |
} |
|
492 |
||
493 |
public String getName() { |
|
494 |
return toString(); |
|
495 |
} |
|
496 |
||
497 |
public int getNameType() { |
|
498 |
return nameType; |
|
499 |
} |
|
500 |
||
501 |
public String[] getNameStrings() { |
|
16847
c8b6eab9a611
8005460: [findbugs] Probably returned array should be cloned
weijun
parents:
14342
diff
changeset
|
502 |
return nameStrings.clone(); |
2 | 503 |
} |
504 |
||
505 |
public byte[][] toByteArray() { |
|
506 |
byte[][] result = new byte[nameStrings.length][]; |
|
507 |
for (int i = 0; i < nameStrings.length; i++) { |
|
508 |
result[i] = new byte[nameStrings[i].length()]; |
|
509 |
result[i] = nameStrings[i].getBytes(); |
|
510 |
} |
|
511 |
return result; |
|
512 |
} |
|
513 |
||
514 |
public String getRealmString() { |
|
13247 | 515 |
return nameRealm.toString(); |
2 | 516 |
} |
517 |
||
518 |
public Realm getRealm() { |
|
519 |
return nameRealm; |
|
520 |
} |
|
521 |
||
522 |
public String getSalt() { |
|
523 |
if (salt == null) { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
524 |
StringBuilder salt = new StringBuilder(); |
13247 | 525 |
salt.append(nameRealm.toString()); |
2 | 526 |
for (int i = 0; i < nameStrings.length; i++) { |
527 |
salt.append(nameStrings[i]); |
|
528 |
} |
|
529 |
return salt.toString(); |
|
530 |
} |
|
531 |
return salt; |
|
532 |
} |
|
533 |
||
534 |
public String toString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
535 |
StringBuilder str = new StringBuilder(); |
2 | 536 |
for (int i = 0; i < nameStrings.length; i++) { |
537 |
if (i > 0) |
|
538 |
str.append("/"); |
|
539 |
str.append(nameStrings[i]); |
|
540 |
} |
|
13247 | 541 |
str.append("@"); |
542 |
str.append(nameRealm.toString()); |
|
2 | 543 |
return str.toString(); |
544 |
} |
|
545 |
||
546 |
public String getNameString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
547 |
StringBuilder str = new StringBuilder(); |
2 | 548 |
for (int i = 0; i < nameStrings.length; i++) { |
549 |
if (i > 0) |
|
550 |
str.append("/"); |
|
551 |
str.append(nameStrings[i]); |
|
552 |
} |
|
553 |
return str.toString(); |
|
554 |
} |
|
555 |
||
556 |
/** |
|
13247 | 557 |
* Encodes a <code>PrincipalName</code> object. Note that only the type and |
558 |
* names are encoded. To encode the realm, call getRealm().asn1Encode(). |
|
2 | 559 |
* @return the byte array of the encoded PrncipalName object. |
560 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
561 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
562 |
* |
|
563 |
*/ |
|
564 |
public byte[] asn1Encode() throws Asn1Exception, IOException { |
|
565 |
DerOutputStream bytes = new DerOutputStream(); |
|
566 |
DerOutputStream temp = new DerOutputStream(); |
|
567 |
BigInteger bint = BigInteger.valueOf(this.nameType); |
|
568 |
temp.putInteger(bint); |
|
569 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp); |
|
570 |
temp = new DerOutputStream(); |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
25859
diff
changeset
|
571 |
DerValue[] der = new DerValue[nameStrings.length]; |
2 | 572 |
for (int i = 0; i < nameStrings.length; i++) { |
3949
00603a93b589
6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents:
2918
diff
changeset
|
573 |
der[i] = new KerberosString(nameStrings[i]).toDerValue(); |
2 | 574 |
} |
575 |
temp.putSequence(der); |
|
576 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp); |
|
577 |
temp = new DerOutputStream(); |
|
578 |
temp.write(DerValue.tag_Sequence, bytes); |
|
579 |
return temp.toByteArray(); |
|
580 |
} |
|
581 |
||
582 |
||
583 |
/** |
|
584 |
* Checks if two <code>PrincipalName</code> objects have identical values in their corresponding data fields. |
|
585 |
* |
|
586 |
* @param pname the other <code>PrincipalName</code> object. |
|
587 |
* @return true if two have identical values, otherwise, return false. |
|
588 |
*/ |
|
589 |
// It is used in <code>sun.security.krb5.internal.ccache</code> package. |
|
590 |
public boolean match(PrincipalName pname) { |
|
591 |
boolean matched = true; |
|
592 |
//name type is just a hint, no two names can be the same ignoring name type. |
|
593 |
// if (this.nameType != pname.nameType) { |
|
594 |
// matched = false; |
|
595 |
// } |
|
596 |
if ((this.nameRealm != null) && (pname.nameRealm != null)) { |
|
597 |
if (!(this.nameRealm.toString().equalsIgnoreCase(pname.nameRealm.toString()))) { |
|
598 |
matched = false; |
|
599 |
} |
|
600 |
} |
|
601 |
if (this.nameStrings.length != pname.nameStrings.length) { |
|
602 |
matched = false; |
|
603 |
} else { |
|
604 |
for (int i = 0; i < this.nameStrings.length; i++) { |
|
605 |
if (!(this.nameStrings[i].equalsIgnoreCase(pname.nameStrings[i]))) { |
|
606 |
matched = false; |
|
607 |
} |
|
608 |
} |
|
609 |
} |
|
610 |
return matched; |
|
611 |
} |
|
612 |
||
613 |
/** |
|
614 |
* Writes data field values of <code>PrincipalName</code> in FCC format to an output stream. |
|
615 |
* |
|
616 |
* @param cos a <code>CCacheOutputStream</code> for writing data. |
|
617 |
* @exception IOException if an I/O exception occurs. |
|
618 |
* @see sun.security.krb5.internal.ccache.CCacheOutputStream |
|
619 |
*/ |
|
620 |
public void writePrincipal(CCacheOutputStream cos) throws IOException { |
|
621 |
cos.write32(nameType); |
|
622 |
cos.write32(nameStrings.length); |
|
13247 | 623 |
byte[] realmBytes = null; |
624 |
realmBytes = nameRealm.toString().getBytes(); |
|
625 |
cos.write32(realmBytes.length); |
|
626 |
cos.write(realmBytes, 0, realmBytes.length); |
|
2 | 627 |
byte[] bytes = null; |
628 |
for (int i = 0; i < nameStrings.length; i++) { |
|
629 |
bytes = nameStrings[i].getBytes(); |
|
630 |
cos.write32(bytes.length); |
|
631 |
cos.write(bytes, 0, bytes.length); |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
/** |
|
636 |
* Returns the instance component of a name. |
|
637 |
* In a multi-component name such as a KRB_NT_SRV_INST |
|
638 |
* name, the second component is returned. |
|
639 |
* Null is returned if there are not two or more |
|
640 |
* components in the name. |
|
641 |
* @returns instance component of a multi-component name. |
|
642 |
*/ |
|
643 |
public String getInstanceComponent() |
|
644 |
{ |
|
645 |
if (nameStrings != null && nameStrings.length >= 2) |
|
646 |
{ |
|
647 |
return new String(nameStrings[1]); |
|
648 |
} |
|
649 |
||
650 |
return null; |
|
651 |
} |
|
652 |
||
653 |
static String mapHostToRealm(String name) { |
|
654 |
String result = null; |
|
655 |
try { |
|
656 |
String subname = null; |
|
657 |
Config c = Config.getInstance(); |
|
14327 | 658 |
if ((result = c.get("domain_realm", name)) != null) |
2 | 659 |
return result; |
660 |
else { |
|
661 |
for (int i = 1; i < name.length(); i++) { |
|
662 |
if ((name.charAt(i) == '.') && (i != name.length() - 1)) { //mapping could be .ibm.com = AUSTIN.IBM.COM |
|
663 |
subname = name.substring(i); |
|
14327 | 664 |
result = c.get("domain_realm", subname); |
2 | 665 |
if (result != null) { |
666 |
break; |
|
667 |
} |
|
668 |
else { |
|
669 |
subname = name.substring(i + 1); //or mapping could be ibm.com = AUSTIN.IBM.COM |
|
14327 | 670 |
result = c.get("domain_realm", subname); |
2 | 671 |
if (result != null) { |
672 |
break; |
|
673 |
} |
|
674 |
} |
|
675 |
} |
|
676 |
} |
|
677 |
} |
|
678 |
} catch (KrbException e) { |
|
679 |
} |
|
680 |
return result; |
|
681 |
} |
|
682 |
||
683 |
} |