author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 25859 | jdk/src/java.security.jgss/share/classes/sun/security/jgss/krb5/Krb5Util.java@3317bb8137f4 |
child 51398 | 3c389a284345 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
15649
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, 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 |
* Retrieves the ServiceCreds for the specified server principal from |
191 |
* the Subject in the specified AccessControlContext. If not found, and if |
|
192 |
* useSubjectCredsOnly is false, then obtain from a LoginContext. |
|
193 |
* |
|
194 |
* NOTE: This method is also used by JSSE Kerberos Cipher Suites |
|
195 |
*/ |
|
196 |
public static ServiceCreds getServiceCreds(GSSCaller caller, |
|
2 | 197 |
String serverPrincipal, AccessControlContext acc) |
198 |
throws LoginException { |
|
199 |
||
200 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 201 |
ServiceCreds sc = null; |
202 |
if (accSubj != null) { |
|
203 |
sc = ServiceCreds.getInstance(accSubj, serverPrincipal); |
|
2 | 204 |
} |
9499 | 205 |
if (sc == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
206 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
207 |
sc = ServiceCreds.getInstance(subject, serverPrincipal); |
|
2 | 208 |
} |
9499 | 209 |
return sc; |
2 | 210 |
} |
211 |
||
212 |
public static KerberosTicket credsToTicket(Credentials serviceCreds) { |
|
213 |
EncryptionKey sessionKey = serviceCreds.getSessionKey(); |
|
214 |
return new KerberosTicket( |
|
215 |
serviceCreds.getEncoded(), |
|
216 |
new KerberosPrincipal(serviceCreds.getClient().getName()), |
|
217 |
new KerberosPrincipal(serviceCreds.getServer().getName(), |
|
218 |
KerberosPrincipal.KRB_NT_SRV_INST), |
|
219 |
sessionKey.getBytes(), |
|
220 |
sessionKey.getEType(), |
|
221 |
serviceCreds.getFlags(), |
|
222 |
serviceCreds.getAuthTime(), |
|
223 |
serviceCreds.getStartTime(), |
|
224 |
serviceCreds.getEndTime(), |
|
225 |
serviceCreds.getRenewTill(), |
|
226 |
serviceCreds.getClientAddresses()); |
|
227 |
}; |
|
228 |
||
229 |
public static Credentials ticketToCreds(KerberosTicket kerbTicket) |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
230 |
throws KrbException, IOException { |
2 | 231 |
return new Credentials( |
232 |
kerbTicket.getEncoded(), |
|
233 |
kerbTicket.getClient().getName(), |
|
234 |
kerbTicket.getServer().getName(), |
|
235 |
kerbTicket.getSessionKey().getEncoded(), |
|
236 |
kerbTicket.getSessionKeyType(), |
|
237 |
kerbTicket.getFlags(), |
|
238 |
kerbTicket.getAuthTime(), |
|
239 |
kerbTicket.getStartTime(), |
|
240 |
kerbTicket.getEndTime(), |
|
241 |
kerbTicket.getRenewTill(), |
|
242 |
kerbTicket.getClientAddresses()); |
|
243 |
} |
|
9499 | 244 |
|
245 |
/** |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
246 |
* 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
|
247 |
* @param ktab the javax..KeyTab object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
248 |
* @return the sun..KeyTab object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
249 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
250 |
public static sun.security.krb5.internal.ktab.KeyTab |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
251 |
snapshotFromJavaxKeyTab(KeyTab ktab) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
252 |
return KerberosSecrets.getJavaxSecurityAuthKerberosAccess() |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
253 |
.keyTabTakeSnapshot(ktab); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
254 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
255 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
256 |
/** |
9499 | 257 |
* 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
|
258 |
* @param ktab the javax..KeyTab object |
9499 | 259 |
* @param cname the PrincipalName |
260 |
* @return the EKeys, never null, might be empty |
|
261 |
*/ |
|
262 |
public static EncryptionKey[] keysFromJavaxKeyTab( |
|
263 |
KeyTab ktab, PrincipalName cname) { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
264 |
return snapshotFromJavaxKeyTab(ktab).readServiceKeys(cname); |
9499 | 265 |
} |
2 | 266 |
} |