author | mbalao |
Tue, 12 Nov 2019 12:36:16 -0300 | |
changeset 59036 | 642d7d7ed426 |
parent 55258 | d65d3c37232c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
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 |
package javax.security.auth.kerberos; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import sun.security.krb5.KrbException; |
|
30 |
import sun.security.krb5.PrincipalName; |
|
31 |
import sun.security.krb5.Realm; |
|
32 |
import sun.security.util.*; |
|
33 |
||
34 |
/** |
|
35 |
* This class encapsulates a Kerberos principal. |
|
36 |
* |
|
37 |
* @author Mayank Upadhyay |
|
38 |
* @since 1.4 |
|
39 |
*/ |
|
40 |
||
41 |
public final class KerberosPrincipal |
|
42 |
implements java.security.Principal, java.io.Serializable { |
|
43 |
||
44 |
private static final long serialVersionUID = -7374788026156829911L; |
|
45 |
||
46 |
//name types |
|
47 |
||
48 |
/** |
|
49 |
* unknown name type. |
|
50 |
*/ |
|
51 |
||
52 |
public static final int KRB_NT_UNKNOWN = 0; |
|
53 |
||
54 |
/** |
|
55 |
* user principal name type. |
|
56 |
*/ |
|
57 |
||
58 |
public static final int KRB_NT_PRINCIPAL = 1; |
|
59 |
||
60 |
/** |
|
61 |
* service and other unique instance (krbtgt) name type. |
|
62 |
*/ |
|
63 |
public static final int KRB_NT_SRV_INST = 2; |
|
64 |
||
65 |
/** |
|
66 |
* service with host name as instance (telnet, rcommands) name type. |
|
67 |
*/ |
|
68 |
||
69 |
public static final int KRB_NT_SRV_HST = 3; |
|
70 |
||
71 |
/** |
|
72 |
* service with host as remaining components name type. |
|
73 |
*/ |
|
74 |
||
75 |
public static final int KRB_NT_SRV_XHST = 4; |
|
76 |
||
77 |
/** |
|
78 |
* unique ID name type. |
|
79 |
*/ |
|
80 |
||
81 |
public static final int KRB_NT_UID = 5; |
|
82 |
||
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
83 |
/** |
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
84 |
* Enterprise name (alias) |
59036
642d7d7ed426
8233946: Add @since 13 annotation to KerberosPrincipal.KRB_NT_ENTERPRISE field
mbalao
parents:
55258
diff
changeset
|
85 |
* |
642d7d7ed426
8233946: Add @since 13 annotation to KerberosPrincipal.KRB_NT_ENTERPRISE field
mbalao
parents:
55258
diff
changeset
|
86 |
* @since 13 |
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
87 |
*/ |
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
88 |
public static final int KRB_NT_ENTERPRISE = 10; |
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
89 |
|
2 | 90 |
private transient String fullName; |
91 |
||
92 |
private transient String realm; |
|
93 |
||
94 |
private transient int nameType; |
|
95 |
||
96 |
||
97 |
/** |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
98 |
* Constructs a {@code KerberosPrincipal} from the provided string input. |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
99 |
* The name type for this principal defaults to |
2 | 100 |
* {@link #KRB_NT_PRINCIPAL KRB_NT_PRINCIPAL} |
101 |
* This string is assumed to contain a name in the format |
|
102 |
* that is specified in Section 2.1.1. (Kerberos Principal Name Form) of |
|
103 |
* <a href=http://www.ietf.org/rfc/rfc1964.txt> RFC 1964 </a> |
|
104 |
* (for example, <i>duke@FOO.COM</i>, where <i>duke</i> |
|
105 |
* represents a principal, and <i>FOO.COM</i> represents a realm). |
|
106 |
* |
|
107 |
* <p>If the input name does not contain a realm, the default realm |
|
108 |
* is used. The default realm can be specified either in a Kerberos |
|
109 |
* configuration file or via the java.security.krb5.realm |
|
45665 | 110 |
* system property. For more information, see the |
111 |
* {@extLink security_guide_jgss_tutorial Kerberos Requirements}. |
|
112 |
* Additionally, if a security manager is |
|
33282 | 113 |
* installed, a {@link ServicePermission} must be granted and the service |
114 |
* principal of the permission must minimally be inside the |
|
115 |
* {@code KerberosPrincipal}'s realm. For example, if the result of |
|
116 |
* {@code new KerberosPrincipal("user")} is {@code user@EXAMPLE.COM}, |
|
117 |
* then a {@code ServicePermission} with service principal |
|
118 |
* {@code host/www.example.com@EXAMPLE.COM} (and any action) |
|
119 |
* must be granted. |
|
2 | 120 |
* |
121 |
* @param name the principal name |
|
122 |
* @throws IllegalArgumentException if name is improperly |
|
123 |
* formatted, if name is null, or if name does not contain |
|
124 |
* the realm to use and the default realm is not specified |
|
125 |
* in either a Kerberos configuration file or via the |
|
126 |
* java.security.krb5.realm system property. |
|
33282 | 127 |
* @throws SecurityException if a security manager is installed and |
128 |
* {@code name} does not contain the realm to use, and a proper |
|
129 |
* {@link ServicePermission} as described above is not granted. |
|
2 | 130 |
*/ |
131 |
public KerberosPrincipal(String name) { |
|
33282 | 132 |
this(name, KRB_NT_PRINCIPAL); |
2 | 133 |
} |
134 |
||
135 |
/** |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
136 |
* Constructs a {@code KerberosPrincipal} from the provided string and |
2 | 137 |
* name type input. The string is assumed to contain a name in the |
138 |
* format that is specified in Section 2.1 (Mandatory Name Forms) of |
|
139 |
* <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>. |
|
140 |
* Valid name types are specified in Section 6.2 (Principal Names) of |
|
141 |
* <a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>. |
|
142 |
* The input name must be consistent with the provided name type. |
|
143 |
* (for example, <i>duke@FOO.COM</i>, is a valid input string for the |
|
144 |
* name type, KRB_NT_PRINCIPAL where <i>duke</i> |
|
145 |
* represents a principal, and <i>FOO.COM</i> represents a realm). |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
146 |
* |
33282 | 147 |
* <p>If the input name does not contain a realm, the default realm |
2 | 148 |
* is used. The default realm can be specified either in a Kerberos |
149 |
* configuration file or via the java.security.krb5.realm |
|
45665 | 150 |
* system property. For more information, see the |
151 |
* {@extLink security_guide_jgss_tutorial Kerberos Requirements}. |
|
152 |
* Additionally, if a security manager is |
|
33282 | 153 |
* installed, a {@link ServicePermission} must be granted and the service |
154 |
* principal of the permission must minimally be inside the |
|
155 |
* {@code KerberosPrincipal}'s realm. For example, if the result of |
|
156 |
* {@code new KerberosPrincipal("user")} is {@code user@EXAMPLE.COM}, |
|
157 |
* then a {@code ServicePermission} with service principal |
|
158 |
* {@code host/www.example.com@EXAMPLE.COM} (and any action) |
|
159 |
* must be granted. |
|
2 | 160 |
* |
161 |
* @param name the principal name |
|
162 |
* @param nameType the name type of the principal |
|
163 |
* @throws IllegalArgumentException if name is improperly |
|
164 |
* formatted, if name is null, if the nameType is not supported, |
|
165 |
* or if name does not contain the realm to use and the default |
|
166 |
* realm is not specified in either a Kerberos configuration |
|
167 |
* file or via the java.security.krb5.realm system property. |
|
33282 | 168 |
* @throws SecurityException if a security manager is installed and |
169 |
* {@code name} does not contain the realm to use, and a proper |
|
170 |
* {@link ServicePermission} as described above is not granted. |
|
2 | 171 |
*/ |
172 |
||
173 |
public KerberosPrincipal(String name, int nameType) { |
|
174 |
||
175 |
PrincipalName krb5Principal = null; |
|
176 |
||
177 |
try { |
|
178 |
// Appends the default realm if it is missing |
|
179 |
krb5Principal = new PrincipalName(name,nameType); |
|
180 |
} catch (KrbException e) { |
|
181 |
throw new IllegalArgumentException(e.getMessage()); |
|
182 |
} |
|
183 |
||
33282 | 184 |
if (krb5Principal.isRealmDeduced() && !Realm.AUTODEDUCEREALM) { |
185 |
SecurityManager sm = System.getSecurityManager(); |
|
186 |
if (sm != null) { |
|
187 |
try { |
|
188 |
sm.checkPermission(new ServicePermission( |
|
189 |
"@" + krb5Principal.getRealmAsString(), "-")); |
|
190 |
} catch (SecurityException se) { |
|
191 |
// Swallow the actual exception to hide info |
|
192 |
throw new SecurityException("Cannot read realm info"); |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
2 | 196 |
this.nameType = nameType; |
197 |
fullName = krb5Principal.toString(); |
|
198 |
realm = krb5Principal.getRealmString(); |
|
199 |
} |
|
200 |
/** |
|
201 |
* Returns the realm component of this Kerberos principal. |
|
202 |
* |
|
203 |
* @return the realm component of this Kerberos principal. |
|
204 |
*/ |
|
205 |
public String getRealm() { |
|
206 |
return realm; |
|
207 |
} |
|
208 |
||
209 |
/** |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
210 |
* Returns a hash code for this {@code KerberosPrincipal}. The hash code |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
211 |
* is defined to be the result of the following calculation: |
18830 | 212 |
* <pre>{@code |
2 | 213 |
* hashCode = getName().hashCode(); |
18830 | 214 |
* }</pre> |
2 | 215 |
* |
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
216 |
* @return a hash code for this {@code KerberosPrincipal}. |
2 | 217 |
*/ |
218 |
public int hashCode() { |
|
219 |
return getName().hashCode(); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
223 |
* Compares the specified object with this principal for equality. |
2 | 224 |
* Returns true if the given object is also a |
18830 | 225 |
* {@code KerberosPrincipal} and the two |
226 |
* {@code KerberosPrincipal} instances are equivalent. |
|
227 |
* More formally two {@code KerberosPrincipal} instances are equal |
|
19597
7a3478d94601
8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents:
18830
diff
changeset
|
228 |
* if the values returned by {@code getName()} are equal. |
2 | 229 |
* |
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
230 |
* @param other the object to compare to |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
231 |
* @return true if the object passed in represents the same principal |
2 | 232 |
* as this one, false otherwise. |
233 |
*/ |
|
234 |
public boolean equals(Object other) { |
|
235 |
||
236 |
if (other == this) |
|
237 |
return true; |
|
238 |
||
239 |
if (! (other instanceof KerberosPrincipal)) { |
|
240 |
return false; |
|
241 |
} |
|
19597
7a3478d94601
8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents:
18830
diff
changeset
|
242 |
String myFullName = getName(); |
7a3478d94601
8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents:
18830
diff
changeset
|
243 |
String otherFullName = ((KerberosPrincipal) other).getName(); |
7a3478d94601
8015669: KerberosPrincipal::equals should ignore name-type
weijun
parents:
18830
diff
changeset
|
244 |
return myFullName.equals(otherFullName); |
2 | 245 |
} |
246 |
||
247 |
/** |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
248 |
* Save the {@code KerberosPrincipal} object to a stream |
2 | 249 |
* |
18830 | 250 |
* @serialData this {@code KerberosPrincipal} is serialized |
2 | 251 |
* by writing out the PrincipalName and the |
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
252 |
* Realm in their DER-encoded form as specified in Section 5.2.2 of |
2 | 253 |
* <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>. |
254 |
*/ |
|
255 |
private void writeObject(ObjectOutputStream oos) |
|
13247 | 256 |
throws IOException { |
2 | 257 |
|
13247 | 258 |
PrincipalName krb5Principal; |
2 | 259 |
try { |
13247 | 260 |
krb5Principal = new PrincipalName(fullName, nameType); |
2 | 261 |
oos.writeObject(krb5Principal.asn1Encode()); |
262 |
oos.writeObject(krb5Principal.getRealm().asn1Encode()); |
|
263 |
} catch (Exception e) { |
|
13247 | 264 |
throw new IOException(e); |
2 | 265 |
} |
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Reads this object from a stream (i.e., deserializes it) |
|
270 |
*/ |
|
271 |
private void readObject(ObjectInputStream ois) |
|
13247 | 272 |
throws IOException, ClassNotFoundException { |
2 | 273 |
byte[] asn1EncPrincipal = (byte [])ois.readObject(); |
274 |
byte[] encRealm = (byte [])ois.readObject(); |
|
275 |
try { |
|
13247 | 276 |
Realm realmObject = new Realm(new DerValue(encRealm)); |
277 |
PrincipalName krb5Principal = new PrincipalName( |
|
278 |
new DerValue(asn1EncPrincipal), realmObject); |
|
279 |
realm = realmObject.toString(); |
|
280 |
fullName = krb5Principal.toString(); |
|
2 | 281 |
nameType = krb5Principal.getNameType(); |
282 |
} catch (Exception e) { |
|
13247 | 283 |
throw new IOException(e); |
2 | 284 |
} |
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* The returned string corresponds to the single-string |
|
289 |
* representation of a Kerberos Principal name as specified in |
|
290 |
* Section 2.1 of <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>. |
|
291 |
* |
|
292 |
* @return the principal name. |
|
293 |
*/ |
|
294 |
public String getName() { |
|
295 |
return fullName; |
|
296 |
} |
|
297 |
||
298 |
/** |
|
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
299 |
* Returns the name type of the {@code KerberosPrincipal}. Valid name types |
2 | 300 |
* are specified in Section 6.2 of |
301 |
* <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>. |
|
302 |
* |
|
303 |
* @return the name type. |
|
304 |
*/ |
|
305 |
public int getNameType() { |
|
306 |
return nameType; |
|
307 |
} |
|
308 |
||
27771
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
309 |
/** |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
310 |
* Returns an informative textual representation of this {@code KerberosPrincipal}. |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
311 |
* |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
312 |
* @return an informative textual representation of this {@code KerberosPrincipal}. |
360714d431ab
8061253: Spec cleanup for some security-related classes
weijun
parents:
25859
diff
changeset
|
313 |
*/ |
2 | 314 |
public String toString() { |
315 |
return getName(); |
|
316 |
} |
|
317 |
} |