author | weijun |
Tue, 14 Aug 2018 22:39:34 +0800 | |
changeset 51398 | 3c389a284345 |
parent 47216 | 71c04702a3d5 |
child 55613 | a55b46a208d1 |
child 58678 | 9cf78a70fa4f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2018, 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.KerberosPrincipal; |
|
9499 | 30 |
import javax.security.auth.kerberos.KeyTab; |
2 | 31 |
import javax.security.auth.Subject; |
32 |
import javax.security.auth.login.LoginException; |
|
33 |
import java.security.AccessControlContext; |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
34 |
|
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
35 |
import sun.security.action.GetBooleanAction; |
2 | 36 |
import sun.security.jgss.GSSUtil; |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
37 |
import sun.security.jgss.GSSCaller; |
2 | 38 |
|
39 |
import sun.security.krb5.Credentials; |
|
40 |
import sun.security.krb5.EncryptionKey; |
|
41 |
import sun.security.krb5.KrbException; |
|
42 |
import java.io.IOException; |
|
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
43 |
import sun.security.krb5.KerberosSecrets; |
9499 | 44 |
import sun.security.krb5.PrincipalName; |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
45 |
|
2 | 46 |
/** |
47 |
* Utilities for obtaining and converting Kerberos tickets. |
|
48 |
*/ |
|
49 |
public class Krb5Util { |
|
50 |
||
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
51 |
static final boolean DEBUG = GetBooleanAction |
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
52 |
.privilegedGetProperty("sun.security.krb5.debug"); |
2 | 53 |
|
54 |
/** |
|
55 |
* Default constructor |
|
56 |
*/ |
|
57 |
private Krb5Util() { // Cannot create one of these |
|
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* Retrieve the service ticket for serverPrincipal from caller's Subject |
|
62 |
* or from Subject obtained by logging in, or if not found, via the |
|
63 |
* Ticket Granting Service using the TGT obtained from the Subject. |
|
64 |
* |
|
65 |
* Caller must have permission to: |
|
66 |
* - access and update Subject's private credentials |
|
67 |
* - create LoginContext |
|
68 |
* - read the auth.login.defaultCallbackHandler security property |
|
69 |
* |
|
70 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
71 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
72 |
public static KerberosTicket getTicketFromSubjectAndTgs(GSSCaller caller, |
2 | 73 |
String clientPrincipal, String serverPrincipal, String tgsPrincipal, |
74 |
AccessControlContext acc) |
|
75 |
throws LoginException, KrbException, IOException { |
|
76 |
||
77 |
// 1. Try to find service ticket in acc subject |
|
78 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 79 |
KerberosTicket ticket = SubjectComber.find(accSubj, |
2 | 80 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
81 |
||
82 |
if (ticket != null) { |
|
83 |
return ticket; // found it |
|
84 |
} |
|
85 |
||
86 |
Subject loginSubj = null; |
|
87 |
if (!GSSUtil.useSubjectCredsOnly(caller)) { |
|
88 |
// 2. Try to get ticket from login |
|
89 |
try { |
|
90 |
loginSubj = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
9499 | 91 |
ticket = SubjectComber.find(loginSubj, |
2 | 92 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
93 |
if (ticket != null) { |
|
94 |
return ticket; // found it |
|
95 |
} |
|
96 |
} catch (LoginException e) { |
|
97 |
// No login entry to use |
|
98 |
// ignore and continue |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
// Service ticket not found in subject or login |
|
103 |
// Try to get TGT to acquire service ticket |
|
104 |
||
105 |
// 3. Try to get TGT from acc subject |
|
9499 | 106 |
KerberosTicket tgt = SubjectComber.find(accSubj, |
2 | 107 |
tgsPrincipal, clientPrincipal, KerberosTicket.class); |
108 |
||
109 |
boolean fromAcc; |
|
110 |
if (tgt == null && loginSubj != null) { |
|
111 |
// 4. Try to get TGT from login subject |
|
9499 | 112 |
tgt = SubjectComber.find(loginSubj, |
2 | 113 |
tgsPrincipal, clientPrincipal, KerberosTicket.class); |
114 |
fromAcc = false; |
|
115 |
} else { |
|
116 |
fromAcc = true; |
|
117 |
} |
|
118 |
||
119 |
// 5. Try to get service ticket using TGT |
|
120 |
if (tgt != null) { |
|
121 |
Credentials tgtCreds = ticketToCreds(tgt); |
|
122 |
Credentials serviceCreds = Credentials.acquireServiceCreds( |
|
123 |
serverPrincipal, tgtCreds); |
|
124 |
if (serviceCreds != null) { |
|
125 |
ticket = credsToTicket(serviceCreds); |
|
126 |
||
127 |
// Store service ticket in acc's Subject |
|
128 |
if (fromAcc && accSubj != null && !accSubj.isReadOnly()) { |
|
129 |
accSubj.getPrivateCredentials().add(ticket); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
return ticket; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Retrieves the ticket corresponding to the client/server principal |
|
138 |
* pair from the Subject in the specified AccessControlContext. |
|
139 |
* If the ticket can not be found in the Subject, and if |
|
140 |
* useSubjectCredsOnly is false, then obtain ticket from |
|
141 |
* a LoginContext. |
|
142 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
143 |
static KerberosTicket getTicket(GSSCaller caller, |
2 | 144 |
String clientPrincipal, String serverPrincipal, |
145 |
AccessControlContext acc) throws LoginException { |
|
146 |
||
147 |
// Try to get ticket from acc's Subject |
|
148 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 149 |
KerberosTicket ticket = |
2 | 150 |
SubjectComber.find(accSubj, serverPrincipal, clientPrincipal, |
151 |
KerberosTicket.class); |
|
152 |
||
153 |
// Try to get ticket from Subject obtained from GSSUtil |
|
154 |
if (ticket == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
155 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
9499 | 156 |
ticket = SubjectComber.find(subject, |
2 | 157 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
158 |
} |
|
159 |
return ticket; |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Retrieves the caller's Subject, or Subject obtained by logging in |
|
164 |
* via the specified caller. |
|
165 |
* |
|
166 |
* Caller must have permission to: |
|
167 |
* - access the Subject |
|
168 |
* - create LoginContext |
|
169 |
* - read the auth.login.defaultCallbackHandler security property |
|
170 |
* |
|
171 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
172 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
173 |
public static Subject getSubject(GSSCaller caller, |
2 | 174 |
AccessControlContext acc) throws LoginException { |
175 |
||
176 |
// Try to get the Subject from acc |
|
177 |
Subject subject = Subject.getSubject(acc); |
|
178 |
||
179 |
// Try to get Subject obtained from GSSUtil |
|
180 |
if (subject == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
181 |
subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
182 |
} |
|
183 |
return subject; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
9499 | 187 |
* Retrieves the ServiceCreds for the specified server principal from |
188 |
* the Subject in the specified AccessControlContext. If not found, and if |
|
189 |
* useSubjectCredsOnly is false, then obtain from a LoginContext. |
|
190 |
* |
|
191 |
* NOTE: This method is also used by JSSE Kerberos Cipher Suites |
|
192 |
*/ |
|
193 |
public static ServiceCreds getServiceCreds(GSSCaller caller, |
|
2 | 194 |
String serverPrincipal, AccessControlContext acc) |
195 |
throws LoginException { |
|
196 |
||
197 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 198 |
ServiceCreds sc = null; |
199 |
if (accSubj != null) { |
|
200 |
sc = ServiceCreds.getInstance(accSubj, serverPrincipal); |
|
2 | 201 |
} |
9499 | 202 |
if (sc == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
203 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
204 |
sc = ServiceCreds.getInstance(subject, serverPrincipal); |
|
2 | 205 |
} |
9499 | 206 |
return sc; |
2 | 207 |
} |
208 |
||
209 |
public static KerberosTicket credsToTicket(Credentials serviceCreds) { |
|
210 |
EncryptionKey sessionKey = serviceCreds.getSessionKey(); |
|
211 |
return new KerberosTicket( |
|
212 |
serviceCreds.getEncoded(), |
|
213 |
new KerberosPrincipal(serviceCreds.getClient().getName()), |
|
214 |
new KerberosPrincipal(serviceCreds.getServer().getName(), |
|
215 |
KerberosPrincipal.KRB_NT_SRV_INST), |
|
216 |
sessionKey.getBytes(), |
|
217 |
sessionKey.getEType(), |
|
218 |
serviceCreds.getFlags(), |
|
219 |
serviceCreds.getAuthTime(), |
|
220 |
serviceCreds.getStartTime(), |
|
221 |
serviceCreds.getEndTime(), |
|
222 |
serviceCreds.getRenewTill(), |
|
223 |
serviceCreds.getClientAddresses()); |
|
224 |
}; |
|
225 |
||
226 |
public static Credentials ticketToCreds(KerberosTicket kerbTicket) |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
227 |
throws KrbException, IOException { |
2 | 228 |
return new Credentials( |
229 |
kerbTicket.getEncoded(), |
|
230 |
kerbTicket.getClient().getName(), |
|
231 |
kerbTicket.getServer().getName(), |
|
232 |
kerbTicket.getSessionKey().getEncoded(), |
|
233 |
kerbTicket.getSessionKeyType(), |
|
234 |
kerbTicket.getFlags(), |
|
235 |
kerbTicket.getAuthTime(), |
|
236 |
kerbTicket.getStartTime(), |
|
237 |
kerbTicket.getEndTime(), |
|
238 |
kerbTicket.getRenewTill(), |
|
239 |
kerbTicket.getClientAddresses()); |
|
240 |
} |
|
9499 | 241 |
|
242 |
/** |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
243 |
* A helper method to get a sun..KeyTab from a javax..KeyTab |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
244 |
* @param ktab the javax..KeyTab object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
245 |
* @return the sun..KeyTab object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
246 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
247 |
public static sun.security.krb5.internal.ktab.KeyTab |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
248 |
snapshotFromJavaxKeyTab(KeyTab ktab) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
249 |
return KerberosSecrets.getJavaxSecurityAuthKerberosAccess() |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
250 |
.keyTabTakeSnapshot(ktab); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
251 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
252 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
253 |
/** |
9499 | 254 |
* A helper method to get EncryptionKeys from a javax..KeyTab |
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
255 |
* @param ktab the javax..KeyTab object |
9499 | 256 |
* @param cname the PrincipalName |
257 |
* @return the EKeys, never null, might be empty |
|
258 |
*/ |
|
259 |
public static EncryptionKey[] keysFromJavaxKeyTab( |
|
260 |
KeyTab ktab, PrincipalName cname) { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
261 |
return snapshotFromJavaxKeyTab(ktab).readServiceKeys(cname); |
9499 | 262 |
} |
2 | 263 |
} |