author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 2942 | 37d9baeb7518 |
child 9499 | f3115698a012 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2009, 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; |
|
31 |
import javax.security.auth.Subject; |
|
32 |
import javax.security.auth.login.LoginException; |
|
33 |
import java.security.AccessControlContext; |
|
34 |
import sun.security.jgss.GSSUtil; |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
35 |
import sun.security.jgss.GSSCaller; |
2 | 36 |
|
37 |
import sun.security.krb5.Credentials; |
|
38 |
import sun.security.krb5.EncryptionKey; |
|
39 |
import sun.security.krb5.KrbException; |
|
40 |
import java.io.IOException; |
|
41 |
import java.util.List; |
|
42 |
/** |
|
43 |
* Utilities for obtaining and converting Kerberos tickets. |
|
44 |
* |
|
45 |
*/ |
|
46 |
public class Krb5Util { |
|
47 |
||
48 |
static final boolean DEBUG = |
|
49 |
java.security.AccessController.doPrivileged( |
|
50 |
new sun.security.action.GetBooleanAction |
|
51 |
("sun.security.krb5.debug")).booleanValue(); |
|
52 |
||
53 |
/** |
|
54 |
* Default constructor |
|
55 |
*/ |
|
56 |
private Krb5Util() { // Cannot create one of these |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Retrieve the service ticket for serverPrincipal from caller's Subject |
|
61 |
* or from Subject obtained by logging in, or if not found, via the |
|
62 |
* Ticket Granting Service using the TGT obtained from the Subject. |
|
63 |
* |
|
64 |
* Caller must have permission to: |
|
65 |
* - access and update Subject's private credentials |
|
66 |
* - create LoginContext |
|
67 |
* - read the auth.login.defaultCallbackHandler security property |
|
68 |
* |
|
69 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
70 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
71 |
public static KerberosTicket getTicketFromSubjectAndTgs(GSSCaller caller, |
2 | 72 |
String clientPrincipal, String serverPrincipal, String tgsPrincipal, |
73 |
AccessControlContext acc) |
|
74 |
throws LoginException, KrbException, IOException { |
|
75 |
||
76 |
// 1. Try to find service ticket in acc subject |
|
77 |
Subject accSubj = Subject.getSubject(acc); |
|
78 |
KerberosTicket ticket = (KerberosTicket) SubjectComber.find(accSubj, |
|
79 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
|
80 |
||
81 |
if (ticket != null) { |
|
82 |
return ticket; // found it |
|
83 |
} |
|
84 |
||
85 |
Subject loginSubj = null; |
|
86 |
if (!GSSUtil.useSubjectCredsOnly(caller)) { |
|
87 |
// 2. Try to get ticket from login |
|
88 |
try { |
|
89 |
loginSubj = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
90 |
ticket = (KerberosTicket) SubjectComber.find(loginSubj, |
|
91 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
|
92 |
if (ticket != null) { |
|
93 |
return ticket; // found it |
|
94 |
} |
|
95 |
} catch (LoginException e) { |
|
96 |
// No login entry to use |
|
97 |
// ignore and continue |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
// Service ticket not found in subject or login |
|
102 |
// Try to get TGT to acquire service ticket |
|
103 |
||
104 |
// 3. Try to get TGT from acc subject |
|
105 |
KerberosTicket tgt = (KerberosTicket) SubjectComber.find(accSubj, |
|
106 |
tgsPrincipal, clientPrincipal, KerberosTicket.class); |
|
107 |
||
108 |
boolean fromAcc; |
|
109 |
if (tgt == null && loginSubj != null) { |
|
110 |
// 4. Try to get TGT from login subject |
|
111 |
tgt = (KerberosTicket) SubjectComber.find(loginSubj, |
|
112 |
tgsPrincipal, clientPrincipal, KerberosTicket.class); |
|
113 |
fromAcc = false; |
|
114 |
} else { |
|
115 |
fromAcc = true; |
|
116 |
} |
|
117 |
||
118 |
// 5. Try to get service ticket using TGT |
|
119 |
if (tgt != null) { |
|
120 |
Credentials tgtCreds = ticketToCreds(tgt); |
|
121 |
Credentials serviceCreds = Credentials.acquireServiceCreds( |
|
122 |
serverPrincipal, tgtCreds); |
|
123 |
if (serviceCreds != null) { |
|
124 |
ticket = credsToTicket(serviceCreds); |
|
125 |
||
126 |
// Store service ticket in acc's Subject |
|
127 |
if (fromAcc && accSubj != null && !accSubj.isReadOnly()) { |
|
128 |
accSubj.getPrivateCredentials().add(ticket); |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
return ticket; |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Retrieves the ticket corresponding to the client/server principal |
|
137 |
* pair from the Subject in the specified AccessControlContext. |
|
138 |
* If the ticket can not be found in the Subject, and if |
|
139 |
* useSubjectCredsOnly is false, then obtain ticket from |
|
140 |
* a LoginContext. |
|
141 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
142 |
static KerberosTicket getTicket(GSSCaller caller, |
2 | 143 |
String clientPrincipal, String serverPrincipal, |
144 |
AccessControlContext acc) throws LoginException { |
|
145 |
||
146 |
// Try to get ticket from acc's Subject |
|
147 |
Subject accSubj = Subject.getSubject(acc); |
|
148 |
KerberosTicket ticket = (KerberosTicket) |
|
149 |
SubjectComber.find(accSubj, serverPrincipal, clientPrincipal, |
|
150 |
KerberosTicket.class); |
|
151 |
||
152 |
// Try to get ticket from Subject obtained from GSSUtil |
|
153 |
if (ticket == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
154 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
155 |
ticket = (KerberosTicket) SubjectComber.find(subject, |
|
156 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
|
157 |
} |
|
158 |
return ticket; |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Retrieves the caller's Subject, or Subject obtained by logging in |
|
163 |
* via the specified caller. |
|
164 |
* |
|
165 |
* Caller must have permission to: |
|
166 |
* - access the Subject |
|
167 |
* - create LoginContext |
|
168 |
* - read the auth.login.defaultCallbackHandler security property |
|
169 |
* |
|
170 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
171 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
172 |
public static Subject getSubject(GSSCaller caller, |
2 | 173 |
AccessControlContext acc) throws LoginException { |
174 |
||
175 |
// Try to get the Subject from acc |
|
176 |
Subject subject = Subject.getSubject(acc); |
|
177 |
||
178 |
// Try to get Subject obtained from GSSUtil |
|
179 |
if (subject == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
180 |
subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
181 |
} |
|
182 |
return subject; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Retrieves the keys for the specified server principal from |
|
187 |
* the Subject in the specified AccessControlContext. |
|
188 |
* If the ticket can not be found in the Subject, and if |
|
189 |
* useSubjectCredsOnly is false, then obtain keys from |
|
190 |
* a LoginContext. |
|
191 |
* |
|
192 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
193 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
194 |
public static KerberosKey[] getKeys(GSSCaller caller, |
2 | 195 |
String serverPrincipal, AccessControlContext acc) |
196 |
throws LoginException { |
|
197 |
||
198 |
Subject accSubj = Subject.getSubject(acc); |
|
199 |
List<KerberosKey> kkeys = (List<KerberosKey>)SubjectComber.findMany( |
|
200 |
accSubj, serverPrincipal, null, KerberosKey.class); |
|
201 |
||
202 |
if (kkeys == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
203 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
204 |
kkeys = (List<KerberosKey>) SubjectComber.findMany(subject, |
|
205 |
serverPrincipal, null, KerberosKey.class); |
|
206 |
} |
|
207 |
||
208 |
int len; |
|
209 |
if (kkeys != null && (len = kkeys.size()) > 0) { |
|
210 |
KerberosKey[] keys = new KerberosKey[len]; |
|
211 |
kkeys.toArray(keys); |
|
212 |
return keys; |
|
213 |
} else { |
|
214 |
return null; |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
public static KerberosTicket credsToTicket(Credentials serviceCreds) { |
|
219 |
EncryptionKey sessionKey = serviceCreds.getSessionKey(); |
|
220 |
return new KerberosTicket( |
|
221 |
serviceCreds.getEncoded(), |
|
222 |
new KerberosPrincipal(serviceCreds.getClient().getName()), |
|
223 |
new KerberosPrincipal(serviceCreds.getServer().getName(), |
|
224 |
KerberosPrincipal.KRB_NT_SRV_INST), |
|
225 |
sessionKey.getBytes(), |
|
226 |
sessionKey.getEType(), |
|
227 |
serviceCreds.getFlags(), |
|
228 |
serviceCreds.getAuthTime(), |
|
229 |
serviceCreds.getStartTime(), |
|
230 |
serviceCreds.getEndTime(), |
|
231 |
serviceCreds.getRenewTill(), |
|
232 |
serviceCreds.getClientAddresses()); |
|
233 |
}; |
|
234 |
||
235 |
public static Credentials ticketToCreds(KerberosTicket kerbTicket) |
|
236 |
throws KrbException, IOException { |
|
237 |
return new Credentials( |
|
238 |
kerbTicket.getEncoded(), |
|
239 |
kerbTicket.getClient().getName(), |
|
240 |
kerbTicket.getServer().getName(), |
|
241 |
kerbTicket.getSessionKey().getEncoded(), |
|
242 |
kerbTicket.getSessionKeyType(), |
|
243 |
kerbTicket.getFlags(), |
|
244 |
kerbTicket.getAuthTime(), |
|
245 |
kerbTicket.getStartTime(), |
|
246 |
kerbTicket.getEndTime(), |
|
247 |
kerbTicket.getRenewTill(), |
|
248 |
kerbTicket.getClientAddresses()); |
|
249 |
} |
|
250 |
} |