Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/kerberos/KerberosPrincipal.java
38918 views
1
/*
2
* Copyright (c) 2000, 2019, 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
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
83
/**
84
* Enterprise name (alias)
85
*/
86
static final int KRB_NT_ENTERPRISE = 10;
87
88
private transient String fullName;
89
90
private transient String realm;
91
92
private transient int nameType;
93
94
95
/**
96
* Constructs a KerberosPrincipal from the provided string input. The
97
* name type for this principal defaults to
98
* {@link #KRB_NT_PRINCIPAL KRB_NT_PRINCIPAL}
99
* This string is assumed to contain a name in the format
100
* that is specified in Section 2.1.1. (Kerberos Principal Name Form) of
101
* <a href=http://www.ietf.org/rfc/rfc1964.txt> RFC 1964 </a>
102
* (for example, <i>[email protected]</i>, where <i>duke</i>
103
* represents a principal, and <i>FOO.COM</i> represents a realm).
104
*
105
* <p>If the input name does not contain a realm, the default realm
106
* is used. The default realm can be specified either in a Kerberos
107
* configuration file or via the java.security.krb5.realm
108
* system property. For more information,
109
* <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
110
* Kerberos Requirements </a>
111
*
112
* @param name the principal name
113
* @throws IllegalArgumentException if name is improperly
114
* formatted, if name is null, or if name does not contain
115
* the realm to use and the default realm is not specified
116
* in either a Kerberos configuration file or via the
117
* java.security.krb5.realm system property.
118
*/
119
public KerberosPrincipal(String name) {
120
this(name, KRB_NT_PRINCIPAL);
121
}
122
123
/**
124
* Constructs a KerberosPrincipal from the provided string and
125
* name type input. The string is assumed to contain a name in the
126
* format that is specified in Section 2.1 (Mandatory Name Forms) of
127
* <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
128
* Valid name types are specified in Section 6.2 (Principal Names) of
129
* <a href=http://www.ietf.org/rfc/rfc4120.txt>RFC 4120</a>.
130
* The input name must be consistent with the provided name type.
131
* (for example, <i>[email protected]</i>, is a valid input string for the
132
* name type, KRB_NT_PRINCIPAL where <i>duke</i>
133
* represents a principal, and <i>FOO.COM</i> represents a realm).
134
135
* <p> If the input name does not contain a realm, the default realm
136
* is used. The default realm can be specified either in a Kerberos
137
* configuration file or via the java.security.krb5.realm
138
* system property. For more information, see
139
* <a href="../../../../../technotes/guides/security/jgss/tutorials/index.html">
140
* Kerberos Requirements</a>.
141
*
142
* @param name the principal name
143
* @param nameType the name type of the principal
144
* @throws IllegalArgumentException if name is improperly
145
* formatted, if name is null, if the nameType is not supported,
146
* or if name does not contain the realm to use and the default
147
* realm is not specified in either a Kerberos configuration
148
* file or via the java.security.krb5.realm system property.
149
*/
150
151
public KerberosPrincipal(String name, int nameType) {
152
153
PrincipalName krb5Principal = null;
154
155
try {
156
// Appends the default realm if it is missing
157
krb5Principal = new PrincipalName(name,nameType);
158
} catch (KrbException e) {
159
throw new IllegalArgumentException(e.getMessage());
160
}
161
162
// A ServicePermission with a principal in the deduced realm and
163
// any action must be granted if no realm is provided by caller.
164
if (krb5Principal.isRealmDeduced() && !Realm.AUTODEDUCEREALM) {
165
SecurityManager sm = System.getSecurityManager();
166
if (sm != null) {
167
try {
168
sm.checkPermission(new ServicePermission(
169
"@" + krb5Principal.getRealmAsString(), "-"));
170
} catch (SecurityException se) {
171
// Swallow the actual exception to hide info
172
throw new SecurityException("Cannot read realm info");
173
}
174
}
175
}
176
this.nameType = nameType;
177
fullName = krb5Principal.toString();
178
realm = krb5Principal.getRealmString();
179
}
180
/**
181
* Returns the realm component of this Kerberos principal.
182
*
183
* @return the realm component of this Kerberos principal.
184
*/
185
public String getRealm() {
186
return realm;
187
}
188
189
/**
190
* Returns a hashcode for this principal. The hash code is defined to
191
* be the result of the following calculation:
192
* <pre>{@code
193
* hashCode = getName().hashCode();
194
* }</pre>
195
*
196
* @return a hashCode() for the {@code KerberosPrincipal}
197
*/
198
public int hashCode() {
199
return getName().hashCode();
200
}
201
202
/**
203
* Compares the specified Object with this Principal for equality.
204
* Returns true if the given object is also a
205
* {@code KerberosPrincipal} and the two
206
* {@code KerberosPrincipal} instances are equivalent.
207
* More formally two {@code KerberosPrincipal} instances are equal
208
* if the values returned by {@code getName()} are equal.
209
*
210
* @param other the Object to compare to
211
* @return true if the Object passed in represents the same principal
212
* as this one, false otherwise.
213
*/
214
public boolean equals(Object other) {
215
216
if (other == this)
217
return true;
218
219
if (! (other instanceof KerberosPrincipal)) {
220
return false;
221
}
222
String myFullName = getName();
223
String otherFullName = ((KerberosPrincipal) other).getName();
224
return myFullName.equals(otherFullName);
225
}
226
227
/**
228
* Save the KerberosPrincipal object to a stream
229
*
230
* @serialData this {@code KerberosPrincipal} is serialized
231
* by writing out the PrincipalName and the
232
* realm in their DER-encoded form as specified in Section 5.2.2 of
233
* <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
234
*/
235
private void writeObject(ObjectOutputStream oos)
236
throws IOException {
237
238
PrincipalName krb5Principal;
239
try {
240
krb5Principal = new PrincipalName(fullName, nameType);
241
oos.writeObject(krb5Principal.asn1Encode());
242
oos.writeObject(krb5Principal.getRealm().asn1Encode());
243
} catch (Exception e) {
244
throw new IOException(e);
245
}
246
}
247
248
/**
249
* Reads this object from a stream (i.e., deserializes it)
250
*/
251
private void readObject(ObjectInputStream ois)
252
throws IOException, ClassNotFoundException {
253
byte[] asn1EncPrincipal = (byte [])ois.readObject();
254
byte[] encRealm = (byte [])ois.readObject();
255
try {
256
Realm realmObject = new Realm(new DerValue(encRealm));
257
PrincipalName krb5Principal = new PrincipalName(
258
new DerValue(asn1EncPrincipal), realmObject);
259
realm = realmObject.toString();
260
fullName = krb5Principal.toString();
261
nameType = krb5Principal.getNameType();
262
} catch (Exception e) {
263
throw new IOException(e);
264
}
265
}
266
267
/**
268
* The returned string corresponds to the single-string
269
* representation of a Kerberos Principal name as specified in
270
* Section 2.1 of <a href=http://www.ietf.org/rfc/rfc1964.txt>RFC 1964</a>.
271
*
272
* @return the principal name.
273
*/
274
public String getName() {
275
return fullName;
276
}
277
278
/**
279
* Returns the name type of the KerberosPrincipal. Valid name types
280
* are specified in Section 6.2 of
281
* <a href=http://www.ietf.org/rfc/rfc4120.txt> RFC4120</a>.
282
*
283
* @return the name type.
284
*/
285
public int getNameType() {
286
return nameType;
287
}
288
289
// Inherits javadocs from Object
290
public String toString() {
291
return getName();
292
}
293
}
294
295