author | weijun |
Tue, 11 Dec 2012 13:14:56 +0800 | |
changeset 14769 | 22808af10edd |
parent 14413 | e954df027393 |
child 15006 | 10d6aacdd67f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9499 | 2 |
* Copyright (c) 2003, 2011, 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 javax.security.auth.kerberos.KerberosTicket; |
|
29 |
import javax.security.auth.kerberos.KerberosKey; |
|
30 |
import javax.security.auth.kerberos.KerberosPrincipal; |
|
9499 | 31 |
import javax.security.auth.kerberos.KeyTab; |
2 | 32 |
import javax.security.auth.Subject; |
33 |
import javax.security.auth.login.LoginException; |
|
34 |
import java.security.AccessControlContext; |
|
35 |
import sun.security.jgss.GSSUtil; |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
36 |
import sun.security.jgss.GSSCaller; |
2 | 37 |
|
38 |
import sun.security.krb5.Credentials; |
|
39 |
import sun.security.krb5.EncryptionKey; |
|
40 |
import sun.security.krb5.KrbException; |
|
41 |
import java.io.IOException; |
|
9499 | 42 |
import java.util.ArrayList; |
2 | 43 |
import java.util.List; |
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
44 |
import sun.security.krb5.KerberosSecrets; |
9499 | 45 |
import sun.security.krb5.PrincipalName; |
2 | 46 |
/** |
47 |
* Utilities for obtaining and converting Kerberos tickets. |
|
48 |
* |
|
49 |
*/ |
|
50 |
public class Krb5Util { |
|
51 |
||
52 |
static final boolean DEBUG = |
|
53 |
java.security.AccessController.doPrivileged( |
|
54 |
new sun.security.action.GetBooleanAction |
|
55 |
("sun.security.krb5.debug")).booleanValue(); |
|
56 |
||
57 |
/** |
|
58 |
* Default constructor |
|
59 |
*/ |
|
60 |
private Krb5Util() { // Cannot create one of these |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Retrieve the service ticket for serverPrincipal from caller's Subject |
|
65 |
* or from Subject obtained by logging in, or if not found, via the |
|
66 |
* Ticket Granting Service using the TGT obtained from the Subject. |
|
67 |
* |
|
68 |
* Caller must have permission to: |
|
69 |
* - access and update Subject's private credentials |
|
70 |
* - create LoginContext |
|
71 |
* - read the auth.login.defaultCallbackHandler security property |
|
72 |
* |
|
73 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
74 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
75 |
public static KerberosTicket getTicketFromSubjectAndTgs(GSSCaller caller, |
2 | 76 |
String clientPrincipal, String serverPrincipal, String tgsPrincipal, |
77 |
AccessControlContext acc) |
|
78 |
throws LoginException, KrbException, IOException { |
|
79 |
||
80 |
// 1. Try to find service ticket in acc subject |
|
81 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 82 |
KerberosTicket ticket = SubjectComber.find(accSubj, |
2 | 83 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
84 |
||
85 |
if (ticket != null) { |
|
86 |
return ticket; // found it |
|
87 |
} |
|
88 |
||
89 |
Subject loginSubj = null; |
|
90 |
if (!GSSUtil.useSubjectCredsOnly(caller)) { |
|
91 |
// 2. Try to get ticket from login |
|
92 |
try { |
|
93 |
loginSubj = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
9499 | 94 |
ticket = SubjectComber.find(loginSubj, |
2 | 95 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
96 |
if (ticket != null) { |
|
97 |
return ticket; // found it |
|
98 |
} |
|
99 |
} catch (LoginException e) { |
|
100 |
// No login entry to use |
|
101 |
// ignore and continue |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
// Service ticket not found in subject or login |
|
106 |
// Try to get TGT to acquire service ticket |
|
107 |
||
108 |
// 3. Try to get TGT from acc subject |
|
9499 | 109 |
KerberosTicket tgt = SubjectComber.find(accSubj, |
2 | 110 |
tgsPrincipal, clientPrincipal, KerberosTicket.class); |
111 |
||
112 |
boolean fromAcc; |
|
113 |
if (tgt == null && loginSubj != null) { |
|
114 |
// 4. Try to get TGT from login subject |
|
9499 | 115 |
tgt = SubjectComber.find(loginSubj, |
2 | 116 |
tgsPrincipal, clientPrincipal, KerberosTicket.class); |
117 |
fromAcc = false; |
|
118 |
} else { |
|
119 |
fromAcc = true; |
|
120 |
} |
|
121 |
||
122 |
// 5. Try to get service ticket using TGT |
|
123 |
if (tgt != null) { |
|
124 |
Credentials tgtCreds = ticketToCreds(tgt); |
|
125 |
Credentials serviceCreds = Credentials.acquireServiceCreds( |
|
126 |
serverPrincipal, tgtCreds); |
|
127 |
if (serviceCreds != null) { |
|
128 |
ticket = credsToTicket(serviceCreds); |
|
129 |
||
130 |
// Store service ticket in acc's Subject |
|
131 |
if (fromAcc && accSubj != null && !accSubj.isReadOnly()) { |
|
132 |
accSubj.getPrivateCredentials().add(ticket); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
return ticket; |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Retrieves the ticket corresponding to the client/server principal |
|
141 |
* pair from the Subject in the specified AccessControlContext. |
|
142 |
* If the ticket can not be found in the Subject, and if |
|
143 |
* useSubjectCredsOnly is false, then obtain ticket from |
|
144 |
* a LoginContext. |
|
145 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
146 |
static KerberosTicket getTicket(GSSCaller caller, |
2 | 147 |
String clientPrincipal, String serverPrincipal, |
148 |
AccessControlContext acc) throws LoginException { |
|
149 |
||
150 |
// Try to get ticket from acc's Subject |
|
151 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 152 |
KerberosTicket ticket = |
2 | 153 |
SubjectComber.find(accSubj, serverPrincipal, clientPrincipal, |
154 |
KerberosTicket.class); |
|
155 |
||
156 |
// Try to get ticket from Subject obtained from GSSUtil |
|
157 |
if (ticket == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
158 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
9499 | 159 |
ticket = SubjectComber.find(subject, |
2 | 160 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
161 |
} |
|
162 |
return ticket; |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Retrieves the caller's Subject, or Subject obtained by logging in |
|
167 |
* via the specified caller. |
|
168 |
* |
|
169 |
* Caller must have permission to: |
|
170 |
* - access the Subject |
|
171 |
* - create LoginContext |
|
172 |
* - read the auth.login.defaultCallbackHandler security property |
|
173 |
* |
|
174 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
175 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
176 |
public static Subject getSubject(GSSCaller caller, |
2 | 177 |
AccessControlContext acc) throws LoginException { |
178 |
||
179 |
// Try to get the Subject from acc |
|
180 |
Subject subject = Subject.getSubject(acc); |
|
181 |
||
182 |
// Try to get Subject obtained from GSSUtil |
|
183 |
if (subject == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
184 |
subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
185 |
} |
|
186 |
return subject; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
9499 | 190 |
* Credentials of a service, the private secret to authenticate its |
191 |
* identity, which can be: |
|
192 |
* 1. Some KerberosKeys (generated from password) |
|
193 |
* 2. A KeyTab (for a typical service) |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
194 |
* 3. A TGT (for S4U2proxy extension) |
2 | 195 |
* |
9499 | 196 |
* Note that some creds can coexist. For example, a user2user service |
197 |
* can use its keytab (or keys) if the client can successfully obtain a |
|
198 |
* normal service ticket, otherwise, it can uses the TGT (actually, the |
|
199 |
* session key of the TGT) if the client can only acquire a service ticket |
|
200 |
* of ENC-TKT-IN-SKEY style. |
|
2 | 201 |
*/ |
9499 | 202 |
public static class ServiceCreds { |
203 |
private KerberosPrincipal kp; |
|
204 |
private List<KeyTab> ktabs; |
|
205 |
private List<KerberosKey> kk; |
|
206 |
private Subject subj; |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
207 |
private KerberosTicket tgt; |
9499 | 208 |
|
209 |
private static ServiceCreds getInstance( |
|
210 |
Subject subj, String serverPrincipal) { |
|
211 |
||
212 |
ServiceCreds sc = new ServiceCreds(); |
|
213 |
sc.subj = subj; |
|
214 |
||
215 |
for (KerberosPrincipal p: subj.getPrincipals(KerberosPrincipal.class)) { |
|
216 |
if (serverPrincipal == null || |
|
217 |
p.getName().equals(serverPrincipal)) { |
|
218 |
sc.kp = p; |
|
219 |
serverPrincipal = p.getName(); |
|
220 |
break; |
|
221 |
} |
|
222 |
} |
|
223 |
if (sc.kp == null) { |
|
224 |
// Compatibility with old behavior: even when there is no |
|
225 |
// KerberosPrincipal, we can find one from KerberosKeys |
|
226 |
List<KerberosKey> keys = SubjectComber.findMany( |
|
14769 | 227 |
subj, serverPrincipal, null, KerberosKey.class); |
9499 | 228 |
if (!keys.isEmpty()) { |
229 |
sc.kp = keys.get(0).getPrincipal(); |
|
230 |
serverPrincipal = sc.kp.getName(); |
|
231 |
if (DEBUG) { |
|
232 |
System.out.println(">>> ServiceCreds: no kp?" |
|
233 |
+ " find one from kk: " + serverPrincipal); |
|
234 |
} |
|
235 |
} else { |
|
236 |
return null; |
|
237 |
} |
|
238 |
} |
|
239 |
sc.ktabs = SubjectComber.findMany( |
|
240 |
subj, null, null, KeyTab.class); |
|
241 |
sc.kk = SubjectComber.findMany( |
|
242 |
subj, serverPrincipal, null, KerberosKey.class); |
|
14769 | 243 |
sc.tgt = SubjectComber.find( |
244 |
subj, null, serverPrincipal, KerberosTicket.class); |
|
245 |
if (sc.ktabs.isEmpty() && sc.kk.isEmpty() && sc.tgt == null) { |
|
9499 | 246 |
return null; |
247 |
} |
|
248 |
return sc; |
|
249 |
} |
|
250 |
||
251 |
public String getName() { |
|
252 |
return kp.getName(); |
|
253 |
} |
|
254 |
||
255 |
public KerberosKey[] getKKeys() { |
|
14769 | 256 |
List<KerberosKey> keys = new ArrayList<>(); |
257 |
for (KerberosKey k: kk) { |
|
258 |
keys.add(k); |
|
259 |
} |
|
260 |
for (KeyTab ktab: ktabs) { |
|
261 |
for (KerberosKey k: ktab.getKeys(kp)) { |
|
262 |
keys.add(k); |
|
9499 | 263 |
} |
264 |
} |
|
14769 | 265 |
return keys.toArray(new KerberosKey[keys.size()]); |
9499 | 266 |
} |
267 |
||
268 |
public EncryptionKey[] getEKeys() { |
|
269 |
KerberosKey[] kkeys = getKKeys(); |
|
270 |
EncryptionKey[] ekeys = new EncryptionKey[kkeys.length]; |
|
271 |
for (int i=0; i<ekeys.length; i++) { |
|
272 |
ekeys[i] = new EncryptionKey( |
|
273 |
kkeys[i].getEncoded(), kkeys[i].getKeyType(), |
|
274 |
new Integer(kkeys[i].getVersionNumber())); |
|
275 |
} |
|
276 |
return ekeys; |
|
277 |
} |
|
278 |
||
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
279 |
public Credentials getInitCred() { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
280 |
if (tgt == null) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
281 |
return null; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
282 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
283 |
try { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
284 |
return ticketToCreds(tgt); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
285 |
} catch (KrbException | IOException e) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
286 |
return null; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
287 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
288 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
289 |
|
9499 | 290 |
public void destroy() { |
291 |
kp = null; |
|
292 |
ktabs = null; |
|
293 |
kk = null; |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
294 |
tgt = null; |
9499 | 295 |
} |
296 |
} |
|
297 |
/** |
|
298 |
* Retrieves the ServiceCreds for the specified server principal from |
|
299 |
* the Subject in the specified AccessControlContext. If not found, and if |
|
300 |
* useSubjectCredsOnly is false, then obtain from a LoginContext. |
|
301 |
* |
|
302 |
* NOTE: This method is also used by JSSE Kerberos Cipher Suites |
|
303 |
*/ |
|
304 |
public static ServiceCreds getServiceCreds(GSSCaller caller, |
|
2 | 305 |
String serverPrincipal, AccessControlContext acc) |
306 |
throws LoginException { |
|
307 |
||
308 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 309 |
ServiceCreds sc = null; |
310 |
if (accSubj != null) { |
|
311 |
sc = ServiceCreds.getInstance(accSubj, serverPrincipal); |
|
2 | 312 |
} |
9499 | 313 |
if (sc == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
314 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
315 |
sc = ServiceCreds.getInstance(subject, serverPrincipal); |
|
2 | 316 |
} |
9499 | 317 |
return sc; |
2 | 318 |
} |
319 |
||
320 |
public static KerberosTicket credsToTicket(Credentials serviceCreds) { |
|
321 |
EncryptionKey sessionKey = serviceCreds.getSessionKey(); |
|
322 |
return new KerberosTicket( |
|
323 |
serviceCreds.getEncoded(), |
|
324 |
new KerberosPrincipal(serviceCreds.getClient().getName()), |
|
325 |
new KerberosPrincipal(serviceCreds.getServer().getName(), |
|
326 |
KerberosPrincipal.KRB_NT_SRV_INST), |
|
327 |
sessionKey.getBytes(), |
|
328 |
sessionKey.getEType(), |
|
329 |
serviceCreds.getFlags(), |
|
330 |
serviceCreds.getAuthTime(), |
|
331 |
serviceCreds.getStartTime(), |
|
332 |
serviceCreds.getEndTime(), |
|
333 |
serviceCreds.getRenewTill(), |
|
334 |
serviceCreds.getClientAddresses()); |
|
335 |
}; |
|
336 |
||
337 |
public static Credentials ticketToCreds(KerberosTicket kerbTicket) |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
338 |
throws KrbException, IOException { |
2 | 339 |
return new Credentials( |
340 |
kerbTicket.getEncoded(), |
|
341 |
kerbTicket.getClient().getName(), |
|
342 |
kerbTicket.getServer().getName(), |
|
343 |
kerbTicket.getSessionKey().getEncoded(), |
|
344 |
kerbTicket.getSessionKeyType(), |
|
345 |
kerbTicket.getFlags(), |
|
346 |
kerbTicket.getAuthTime(), |
|
347 |
kerbTicket.getStartTime(), |
|
348 |
kerbTicket.getEndTime(), |
|
349 |
kerbTicket.getRenewTill(), |
|
350 |
kerbTicket.getClientAddresses()); |
|
351 |
} |
|
9499 | 352 |
|
353 |
/** |
|
354 |
* A helper method to get EncryptionKeys from a javax..KeyTab |
|
355 |
* @param ktab the javax..KeyTab class |
|
356 |
* @param cname the PrincipalName |
|
357 |
* @return the EKeys, never null, might be empty |
|
358 |
*/ |
|
359 |
public static EncryptionKey[] keysFromJavaxKeyTab( |
|
360 |
KeyTab ktab, PrincipalName cname) { |
|
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
361 |
return KerberosSecrets.getJavaxSecurityAuthKerberosAccess(). |
9499 | 362 |
keyTabGetEncryptionKeys(ktab, cname); |
363 |
} |
|
364 |
||
2 | 365 |
} |