2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2006, 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 sun.security.jgss.krb5;
|
|
27 |
|
|
28 |
import org.ietf.jgss.*;
|
|
29 |
import sun.security.jgss.spi.*;
|
|
30 |
import javax.security.auth.kerberos.*;
|
|
31 |
import sun.security.krb5.PrincipalName;
|
|
32 |
import sun.security.krb5.KrbException;
|
|
33 |
import sun.security.krb5.ServiceName;
|
|
34 |
import java.io.UnsupportedEncodingException;
|
|
35 |
import java.net.InetAddress;
|
|
36 |
import java.net.UnknownHostException;
|
|
37 |
import java.security.Provider;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Implements the GSSNameSpi for the krb5 mechanism.
|
|
41 |
*
|
|
42 |
* @author Mayank Upadhyay
|
|
43 |
*/
|
|
44 |
public class Krb5NameElement
|
|
45 |
implements GSSNameSpi {
|
|
46 |
|
|
47 |
private PrincipalName krb5PrincipalName;
|
|
48 |
|
|
49 |
private String gssNameStr = null;
|
|
50 |
private Oid gssNameType = null;
|
|
51 |
|
|
52 |
// XXX Move this concept into PrincipalName's asn1Encode() sometime
|
|
53 |
private static String CHAR_ENCODING = "UTF-8";
|
|
54 |
|
|
55 |
private Krb5NameElement(PrincipalName principalName,
|
|
56 |
String gssNameStr,
|
|
57 |
Oid gssNameType) {
|
|
58 |
this.krb5PrincipalName = principalName;
|
|
59 |
this.gssNameStr = gssNameStr;
|
|
60 |
this.gssNameType = gssNameType;
|
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Instantiates a new Krb5NameElement object. Internally it stores the
|
|
65 |
* information provided by the input parameters so that they may later
|
|
66 |
* be used for output when a printable representaion of this name is
|
|
67 |
* needed in GSS-API format rather than in Kerberos format.
|
|
68 |
*
|
|
69 |
*/
|
|
70 |
static Krb5NameElement getInstance(String gssNameStr, Oid gssNameType)
|
|
71 |
throws GSSException {
|
|
72 |
|
|
73 |
/*
|
|
74 |
* A null gssNameType implies that the mechanism default
|
|
75 |
* Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL be used.
|
|
76 |
*/
|
|
77 |
if (gssNameType == null)
|
|
78 |
gssNameType = Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL;
|
|
79 |
else
|
|
80 |
if (!gssNameType.equals(GSSName.NT_USER_NAME) &&
|
|
81 |
!gssNameType.equals(GSSName.NT_HOSTBASED_SERVICE) &&
|
|
82 |
!gssNameType.equals(Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL) &&
|
|
83 |
!gssNameType.equals(GSSName.NT_EXPORT_NAME))
|
|
84 |
throw new GSSException(GSSException.BAD_NAMETYPE, -1,
|
|
85 |
gssNameType.toString()
|
|
86 |
+" is an unsupported nametype");
|
|
87 |
|
|
88 |
PrincipalName principalName;
|
|
89 |
try {
|
|
90 |
|
|
91 |
if (gssNameType.equals(GSSName.NT_EXPORT_NAME) ||
|
|
92 |
gssNameType.equals(Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL)) {
|
|
93 |
principalName = new PrincipalName(gssNameStr,
|
|
94 |
PrincipalName.KRB_NT_PRINCIPAL);
|
|
95 |
} else {
|
|
96 |
|
|
97 |
String[] components = getComponents(gssNameStr);
|
|
98 |
|
|
99 |
/*
|
|
100 |
* We have forms of GSS name strings that can come in:
|
|
101 |
*
|
|
102 |
* 1. names of the form "foo" with just one
|
|
103 |
* component. (This might include a "@" but only in escaped
|
|
104 |
* form like "\@")
|
|
105 |
* 2. names of the form "foo@bar" with two components
|
|
106 |
*
|
|
107 |
* The nametypes that are accepted are NT_USER_NAME, and
|
|
108 |
* NT_HOSTBASED_SERVICE.
|
|
109 |
*/
|
|
110 |
|
|
111 |
if (gssNameType.equals(GSSName.NT_USER_NAME))
|
|
112 |
principalName = new PrincipalName(gssNameStr,
|
|
113 |
PrincipalName.KRB_NT_PRINCIPAL);
|
|
114 |
else {
|
|
115 |
String hostName = null;
|
|
116 |
String service = components[0];
|
|
117 |
if (components.length >= 2)
|
|
118 |
hostName = components[1];
|
|
119 |
|
|
120 |
String principal = getHostBasedInstance(service, hostName);
|
|
121 |
principalName = new ServiceName(principal,
|
|
122 |
PrincipalName.KRB_NT_SRV_HST);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
} catch (KrbException e) {
|
|
127 |
throw new GSSException(GSSException.BAD_NAME, -1, e.getMessage());
|
|
128 |
}
|
|
129 |
|
|
130 |
return new Krb5NameElement(principalName, gssNameStr, gssNameType);
|
|
131 |
}
|
|
132 |
|
|
133 |
static Krb5NameElement getInstance(PrincipalName principalName) {
|
|
134 |
return new Krb5NameElement(principalName,
|
|
135 |
principalName.getName(),
|
|
136 |
Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
|
|
137 |
}
|
|
138 |
|
|
139 |
private static String[] getComponents(String gssNameStr)
|
|
140 |
throws GSSException {
|
|
141 |
|
|
142 |
String[] retVal;
|
|
143 |
|
|
144 |
// XXX Perhaps provide this parsing code in PrincipalName
|
|
145 |
|
|
146 |
// Look for @ as in service@host
|
|
147 |
// Assumes host name will not have an escaped '@'
|
|
148 |
int separatorPos = gssNameStr.lastIndexOf('@', gssNameStr.length());
|
|
149 |
|
|
150 |
// Not really a separator if it is escaped. Then this is just part
|
|
151 |
// of the principal name or service name
|
|
152 |
if ((separatorPos > 0) &&
|
|
153 |
(gssNameStr.charAt(separatorPos-1) == '\\')) {
|
|
154 |
// Is the `\` character escaped itself?
|
|
155 |
if ((separatorPos - 2 < 0) ||
|
|
156 |
(gssNameStr.charAt(separatorPos-2) != '\\'))
|
|
157 |
separatorPos = -1;
|
|
158 |
}
|
|
159 |
|
|
160 |
if (separatorPos > 0) {
|
|
161 |
String serviceName = gssNameStr.substring(0, separatorPos);
|
|
162 |
String hostName = gssNameStr.substring(separatorPos+1);
|
|
163 |
retVal = new String[] { serviceName, hostName};
|
|
164 |
} else {
|
|
165 |
retVal = new String[] {gssNameStr};
|
|
166 |
}
|
|
167 |
|
|
168 |
return retVal;
|
|
169 |
|
|
170 |
}
|
|
171 |
|
|
172 |
private static String getHostBasedInstance(String serviceName,
|
|
173 |
String hostName)
|
|
174 |
throws GSSException {
|
|
175 |
StringBuffer temp = new StringBuffer(serviceName);
|
|
176 |
|
|
177 |
try {
|
|
178 |
// A lack of "@" defaults to the service being on the local
|
|
179 |
// host as per RFC 2743
|
|
180 |
// XXX Move this part into JGSS framework
|
|
181 |
if (hostName == null)
|
|
182 |
hostName = InetAddress.getLocalHost().getHostName();
|
|
183 |
|
|
184 |
} catch (UnknownHostException e) {
|
|
185 |
// use hostname as it is
|
|
186 |
}
|
|
187 |
hostName = hostName.toLowerCase();
|
|
188 |
|
|
189 |
temp = temp.append('/').append(hostName);
|
|
190 |
return temp.toString();
|
|
191 |
}
|
|
192 |
|
|
193 |
public final PrincipalName getKrb5PrincipalName() {
|
|
194 |
return krb5PrincipalName;
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Equal method for the GSSNameSpi objects.
|
|
199 |
* If either name denotes an anonymous principal, the call should
|
|
200 |
* return false.
|
|
201 |
*
|
|
202 |
* @param name to be compared with
|
|
203 |
* @returns true if they both refer to the same entity, else false
|
|
204 |
* @exception GSSException with major codes of BAD_NAMETYPE,
|
|
205 |
* BAD_NAME, FAILURE
|
|
206 |
*/
|
|
207 |
public boolean equals(GSSNameSpi other) throws GSSException {
|
|
208 |
|
|
209 |
if (other == this)
|
|
210 |
return true;
|
|
211 |
|
|
212 |
if (other instanceof Krb5NameElement) {
|
|
213 |
Krb5NameElement that = (Krb5NameElement) other;
|
|
214 |
return (this.krb5PrincipalName.getName().equals(
|
|
215 |
that.krb5PrincipalName.getName()));
|
|
216 |
}
|
|
217 |
return false;
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Compares this <code>GSSNameSpi</code> object to another Object
|
|
222 |
* that might be a <code>GSSNameSpi</code>. The behaviour is exactly
|
|
223 |
* the same as in {@link #equals(GSSNameSpi) equals} except that
|
|
224 |
* no GSSException is thrown; instead, false will be returned in the
|
|
225 |
* situation where an error occurs.
|
|
226 |
*
|
|
227 |
* @param another the object to be compared to
|
|
228 |
* @returns true if they both refer to the same entity, else false
|
|
229 |
* @see #equals(GSSNameSpi)
|
|
230 |
*/
|
|
231 |
public boolean equals(Object another) {
|
|
232 |
if (this == another) {
|
|
233 |
return true;
|
|
234 |
}
|
|
235 |
|
|
236 |
try {
|
|
237 |
if (another instanceof Krb5NameElement)
|
|
238 |
return equals((Krb5NameElement) another);
|
|
239 |
} catch (GSSException e) {
|
|
240 |
// ignore exception
|
|
241 |
}
|
|
242 |
return false;
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* Returns a hashcode value for this GSSNameSpi.
|
|
247 |
*
|
|
248 |
* @return a hashCode value
|
|
249 |
*/
|
|
250 |
public int hashCode() {
|
|
251 |
return 37 * 17 + krb5PrincipalName.getName().hashCode();
|
|
252 |
}
|
|
253 |
|
|
254 |
|
|
255 |
/**
|
|
256 |
* Returns the principal name in the form user@REALM or
|
|
257 |
* host/service@REALM but with the following contraints that are
|
|
258 |
* imposed by RFC 1964:
|
|
259 |
* <pre>
|
|
260 |
* (1) all occurrences of the characters `@`, `/`, and `\` within
|
|
261 |
* principal components or realm names shall be quoted with an
|
|
262 |
* immediately-preceding `\`.
|
|
263 |
*
|
|
264 |
* (2) all occurrences of the null, backspace, tab, or newline
|
|
265 |
* characters within principal components or realm names will be
|
|
266 |
* represented, respectively, with `\0`, `\b`, `\t`, or `\n`.
|
|
267 |
*
|
|
268 |
* (3) the `\` quoting character shall not be emitted within an
|
|
269 |
* exported name except to accomodate cases (1) and (2).
|
|
270 |
* </pre>
|
|
271 |
*/
|
|
272 |
public byte[] export() throws GSSException {
|
|
273 |
// XXX Apply the above constraints.
|
|
274 |
byte[] retVal = null;
|
|
275 |
try {
|
|
276 |
retVal = krb5PrincipalName.getName().getBytes(CHAR_ENCODING);
|
|
277 |
} catch (UnsupportedEncodingException e) {
|
|
278 |
// Can't happen
|
|
279 |
}
|
|
280 |
return retVal;
|
|
281 |
}
|
|
282 |
|
|
283 |
/**
|
|
284 |
* Get the mechanism type that this NameElement corresponds to.
|
|
285 |
*
|
|
286 |
* @return the Oid of the mechanism type
|
|
287 |
*/
|
|
288 |
public Oid getMechanism() {
|
|
289 |
return (Krb5MechFactory.GSS_KRB5_MECH_OID);
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
* Returns a string representation for this name. The printed
|
|
294 |
* name type can be obtained by calling getStringNameType().
|
|
295 |
*
|
|
296 |
* @return string form of this name
|
|
297 |
* @see #getStringNameType()
|
|
298 |
* @overrides Object#toString
|
|
299 |
*/
|
|
300 |
public String toString() {
|
|
301 |
return (gssNameStr);
|
|
302 |
// For testing: return (super.toString());
|
|
303 |
}
|
|
304 |
|
|
305 |
/**
|
|
306 |
* Returns the name type oid.
|
|
307 |
*/
|
|
308 |
public Oid getGSSNameType() {
|
|
309 |
return (gssNameType);
|
|
310 |
}
|
|
311 |
|
|
312 |
/**
|
|
313 |
* Returns the oid describing the format of the printable name.
|
|
314 |
*
|
|
315 |
* @return the Oid for the format of the printed name
|
|
316 |
*/
|
|
317 |
public Oid getStringNameType() {
|
|
318 |
// XXX For NT_EXPORT_NAME return a different name type. Infact,
|
|
319 |
// don't even store NT_EXPORT_NAME in the cons.
|
|
320 |
return (gssNameType);
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* Indicates if this name object represents an Anonymous name.
|
|
325 |
*/
|
|
326 |
public boolean isAnonymousName() {
|
|
327 |
return (gssNameType.equals(GSSName.NT_ANONYMOUS));
|
|
328 |
}
|
|
329 |
|
|
330 |
public Provider getProvider() {
|
|
331 |
return Krb5MechFactory.PROVIDER;
|
|
332 |
}
|
|
333 |
|
|
334 |
}
|