author | alanb |
Fri, 02 Nov 2012 15:50:11 +0000 | |
changeset 14342 | 8435a30053c1 |
parent 13247 | 74902cfeb9c6 |
child 14413 | e954df027393 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13247
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, 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 |
/* |
|
27 |
* |
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
29 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
30 |
*/ |
|
31 |
||
32 |
package sun.security.krb5; |
|
33 |
||
34 |
import sun.security.krb5.internal.*; |
|
35 |
import sun.security.krb5.internal.crypto.*; |
|
36 |
import java.io.IOException; |
|
37 |
import java.net.UnknownHostException; |
|
38 |
||
39 |
/** |
|
40 |
* This class encapsulates a Kerberos TGS-REQ that is sent from the |
|
41 |
* client to the KDC. |
|
42 |
*/ |
|
7183 | 43 |
public class KrbTgsReq { |
2 | 44 |
|
45 |
private PrincipalName princName; |
|
46 |
private PrincipalName servName; |
|
47 |
private TGSReq tgsReqMessg; |
|
48 |
private KerberosTime ctime; |
|
49 |
private Ticket secondTicket = null; |
|
50 |
private boolean useSubkey = false; |
|
51 |
EncryptionKey tgsReqKey; |
|
52 |
||
53 |
private static final boolean DEBUG = Krb5.DEBUG; |
|
54 |
||
7183 | 55 |
private byte[] obuf; |
56 |
private byte[] ibuf; |
|
2 | 57 |
|
58 |
// Used in CredentialsUtil |
|
59 |
public KrbTgsReq(Credentials asCreds, |
|
60 |
PrincipalName sname) |
|
61 |
throws KrbException, IOException { |
|
62 |
this(new KDCOptions(), |
|
63 |
asCreds, |
|
64 |
sname, |
|
65 |
null, // KerberosTime from |
|
66 |
null, // KerberosTime till |
|
67 |
null, // KerberosTime rtime |
|
68 |
null, // eTypes, // null, // int[] eTypes |
|
69 |
null, // HostAddresses addresses |
|
70 |
null, // AuthorizationData authorizationData |
|
71 |
null, // Ticket[] additionalTickets |
|
72 |
null); // EncryptionKey subSessionKey |
|
73 |
} |
|
74 |
||
73 | 75 |
// Called by Credentials, KrbCred |
76 |
KrbTgsReq( |
|
77 |
KDCOptions options, |
|
78 |
Credentials asCreds, |
|
79 |
PrincipalName sname, |
|
80 |
KerberosTime from, |
|
81 |
KerberosTime till, |
|
82 |
KerberosTime rtime, |
|
83 |
int[] eTypes, |
|
84 |
HostAddresses addresses, |
|
85 |
AuthorizationData authorizationData, |
|
86 |
Ticket[] additionalTickets, |
|
87 |
EncryptionKey subKey) throws KrbException, IOException { |
|
2 | 88 |
|
73 | 89 |
princName = asCreds.client; |
90 |
servName = sname; |
|
91 |
ctime = new KerberosTime(KerberosTime.NOW); |
|
2 | 92 |
|
93 |
||
73 | 94 |
// check if they are valid arguments. The optional fields |
95 |
// should be consistent with settings in KDCOptions. |
|
96 |
if (options.get(KDCOptions.FORWARDABLE) && |
|
97 |
(!(asCreds.flags.get(Krb5.TKT_OPTS_FORWARDABLE)))) { |
|
98 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
99 |
} |
|
100 |
if (options.get(KDCOptions.FORWARDED)) { |
|
101 |
if (!(asCreds.flags.get(KDCOptions.FORWARDABLE))) |
|
102 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
103 |
} |
|
104 |
if (options.get(KDCOptions.PROXIABLE) && |
|
105 |
(!(asCreds.flags.get(Krb5.TKT_OPTS_PROXIABLE)))) { |
|
106 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
107 |
} |
|
108 |
if (options.get(KDCOptions.PROXY)) { |
|
109 |
if (!(asCreds.flags.get(KDCOptions.PROXIABLE))) |
|
110 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
111 |
} |
|
112 |
if (options.get(KDCOptions.ALLOW_POSTDATE) && |
|
113 |
(!(asCreds.flags.get(Krb5.TKT_OPTS_MAY_POSTDATE)))) { |
|
114 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
115 |
} |
|
116 |
if (options.get(KDCOptions.RENEWABLE) && |
|
117 |
(!(asCreds.flags.get(Krb5.TKT_OPTS_RENEWABLE)))) { |
|
118 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
119 |
} |
|
2 | 120 |
|
73 | 121 |
if (options.get(KDCOptions.POSTDATED)) { |
122 |
if (!(asCreds.flags.get(KDCOptions.POSTDATED))) |
|
123 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
124 |
} else { |
|
125 |
if (from != null) from = null; |
|
126 |
} |
|
127 |
if (options.get(KDCOptions.RENEWABLE)) { |
|
128 |
if (!(asCreds.flags.get(KDCOptions.RENEWABLE))) |
|
129 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
130 |
} else { |
|
131 |
if (rtime != null) rtime = null; |
|
132 |
} |
|
133 |
if (options.get(KDCOptions.ENC_TKT_IN_SKEY)) { |
|
134 |
if (additionalTickets == null) |
|
135 |
throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS); |
|
136 |
// in TGS_REQ there could be more than one additional |
|
137 |
// tickets, but in file-based credential cache, |
|
138 |
// there is only one additional ticket field. |
|
139 |
secondTicket = additionalTickets[0]; |
|
140 |
} else { |
|
141 |
if (additionalTickets != null) |
|
142 |
additionalTickets = null; |
|
143 |
} |
|
2 | 144 |
|
73 | 145 |
tgsReqMessg = createRequest( |
146 |
options, |
|
147 |
asCreds.ticket, |
|
148 |
asCreds.key, |
|
149 |
ctime, |
|
150 |
princName, |
|
151 |
servName, |
|
152 |
from, |
|
153 |
till, |
|
154 |
rtime, |
|
155 |
eTypes, |
|
156 |
addresses, |
|
157 |
authorizationData, |
|
158 |
additionalTickets, |
|
159 |
subKey); |
|
160 |
obuf = tgsReqMessg.asn1Encode(); |
|
2 | 161 |
|
73 | 162 |
// XXX We need to revisit this to see if can't move it |
163 |
// up such that FORWARDED flag set in the options |
|
164 |
// is included in the marshaled request. |
|
165 |
/* |
|
166 |
* If this is based on a forwarded ticket, record that in the |
|
167 |
* options, because the returned TgsRep will contain the |
|
168 |
* FORWARDED flag set. |
|
169 |
*/ |
|
170 |
if (asCreds.flags.get(KDCOptions.FORWARDED)) |
|
171 |
options.set(KDCOptions.FORWARDED, true); |
|
2 | 172 |
|
173 |
||
73 | 174 |
} |
2 | 175 |
|
176 |
/** |
|
177 |
* Sends a TGS request to the realm of the target. |
|
178 |
* @throws KrbException |
|
179 |
* @throws IOException |
|
180 |
*/ |
|
7183 | 181 |
public void send() throws IOException, KrbException { |
2 | 182 |
String realmStr = null; |
183 |
if (servName != null) |
|
184 |
realmStr = servName.getRealmString(); |
|
7183 | 185 |
KdcComm comm = new KdcComm(realmStr); |
186 |
ibuf = comm.send(obuf); |
|
2 | 187 |
} |
188 |
||
189 |
public KrbTgsRep getReply() |
|
190 |
throws KrbException, IOException { |
|
191 |
return new KrbTgsRep(ibuf, this); |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Sends the request, waits for a reply, and returns the Credentials. |
|
196 |
* Used in Credentials, KrbCred, and internal/CredentialsUtil. |
|
197 |
*/ |
|
198 |
public Credentials sendAndGetCreds() throws IOException, KrbException { |
|
199 |
KrbTgsRep tgs_rep = null; |
|
200 |
String kdc = null; |
|
7183 | 201 |
send(); |
202 |
tgs_rep = getReply(); |
|
2 | 203 |
return tgs_rep.getCreds(); |
204 |
} |
|
205 |
||
206 |
KerberosTime getCtime() { |
|
207 |
return ctime; |
|
208 |
} |
|
209 |
||
210 |
private TGSReq createRequest( |
|
211 |
KDCOptions kdc_options, |
|
212 |
Ticket ticket, |
|
213 |
EncryptionKey key, |
|
214 |
KerberosTime ctime, |
|
215 |
PrincipalName cname, |
|
216 |
PrincipalName sname, |
|
217 |
KerberosTime from, |
|
218 |
KerberosTime till, |
|
219 |
KerberosTime rtime, |
|
220 |
int[] eTypes, |
|
221 |
HostAddresses addresses, |
|
222 |
AuthorizationData authorizationData, |
|
223 |
Ticket[] additionalTickets, |
|
224 |
EncryptionKey subKey) |
|
225 |
throws Asn1Exception, IOException, KdcErrException, KrbApErrException, |
|
226 |
UnknownHostException, KrbCryptoException { |
|
227 |
KerberosTime req_till = null; |
|
228 |
if (till == null) { |
|
7183 | 229 |
req_till = new KerberosTime(0); |
2 | 230 |
} else { |
231 |
req_till = till; |
|
232 |
} |
|
233 |
||
234 |
/* |
|
235 |
* RFC 4120, Section 5.4.2. |
|
236 |
* For KRB_TGS_REP, the ciphertext is encrypted in the |
|
237 |
* sub-session key from the Authenticator, or if absent, |
|
238 |
* the session key from the ticket-granting ticket used |
|
239 |
* in the request. |
|
240 |
* |
|
241 |
* To support this, use tgsReqKey to remember which key to use. |
|
242 |
*/ |
|
243 |
tgsReqKey = key; |
|
244 |
||
245 |
int[] req_eTypes = null; |
|
246 |
if (eTypes == null) { |
|
247 |
req_eTypes = EType.getDefaults("default_tgs_enctypes"); |
|
248 |
if (req_eTypes == null) { |
|
249 |
throw new KrbCryptoException( |
|
250 |
"No supported encryption types listed in default_tgs_enctypes"); |
|
251 |
} |
|
252 |
} else { |
|
253 |
req_eTypes = eTypes; |
|
254 |
} |
|
255 |
||
256 |
EncryptionKey reqKey = null; |
|
257 |
EncryptedData encAuthorizationData = null; |
|
258 |
if (authorizationData != null) { |
|
259 |
byte[] ad = authorizationData.asn1Encode(); |
|
260 |
if (subKey != null) { |
|
261 |
reqKey = subKey; |
|
262 |
tgsReqKey = subKey; // Key to use to decrypt reply |
|
263 |
useSubkey = true; |
|
264 |
encAuthorizationData = new EncryptedData(reqKey, ad, |
|
265 |
KeyUsage.KU_TGS_REQ_AUTH_DATA_SUBKEY); |
|
266 |
} else |
|
267 |
encAuthorizationData = new EncryptedData(key, ad, |
|
268 |
KeyUsage.KU_TGS_REQ_AUTH_DATA_SESSKEY); |
|
269 |
} |
|
270 |
||
271 |
KDCReqBody reqBody = new KDCReqBody( |
|
272 |
kdc_options, |
|
273 |
cname, |
|
274 |
sname, |
|
275 |
from, |
|
276 |
req_till, |
|
277 |
rtime, |
|
278 |
Nonce.value(), |
|
279 |
req_eTypes, |
|
280 |
addresses, |
|
281 |
encAuthorizationData, |
|
282 |
additionalTickets); |
|
283 |
||
284 |
byte[] temp = reqBody.asn1Encode(Krb5.KRB_TGS_REQ); |
|
285 |
// if the checksum type is one of the keyed checksum types, |
|
286 |
// use session key. |
|
287 |
Checksum cksum; |
|
288 |
switch (Checksum.CKSUMTYPE_DEFAULT) { |
|
289 |
case Checksum.CKSUMTYPE_RSA_MD4_DES: |
|
290 |
case Checksum.CKSUMTYPE_DES_MAC: |
|
291 |
case Checksum.CKSUMTYPE_DES_MAC_K: |
|
292 |
case Checksum.CKSUMTYPE_RSA_MD4_DES_K: |
|
293 |
case Checksum.CKSUMTYPE_RSA_MD5_DES: |
|
294 |
case Checksum.CKSUMTYPE_HMAC_SHA1_DES3_KD: |
|
295 |
case Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR: |
|
296 |
case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES128: |
|
297 |
case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES256: |
|
298 |
cksum = new Checksum(Checksum.CKSUMTYPE_DEFAULT, temp, key, |
|
299 |
KeyUsage.KU_PA_TGS_REQ_CKSUM); |
|
300 |
break; |
|
301 |
case Checksum.CKSUMTYPE_CRC32: |
|
302 |
case Checksum.CKSUMTYPE_RSA_MD4: |
|
303 |
case Checksum.CKSUMTYPE_RSA_MD5: |
|
304 |
default: |
|
305 |
cksum = new Checksum(Checksum.CKSUMTYPE_DEFAULT, temp); |
|
306 |
} |
|
307 |
||
308 |
// Usage will be KeyUsage.KU_PA_TGS_REQ_AUTHENTICATOR |
|
309 |
||
310 |
byte[] tgs_ap_req = new KrbApReq( |
|
311 |
new APOptions(), |
|
312 |
ticket, |
|
313 |
key, |
|
314 |
cname, |
|
315 |
cksum, |
|
316 |
ctime, |
|
317 |
reqKey, |
|
318 |
null, |
|
319 |
null).getMessage(); |
|
320 |
||
321 |
PAData[] tgsPAData = new PAData[1]; |
|
322 |
tgsPAData[0] = new PAData(Krb5.PA_TGS_REQ, tgs_ap_req); |
|
323 |
||
324 |
return new TGSReq(tgsPAData, reqBody); |
|
325 |
} |
|
326 |
||
327 |
TGSReq getMessage() { |
|
328 |
return tgsReqMessg; |
|
329 |
} |
|
330 |
||
331 |
Ticket getSecondTicket() { |
|
332 |
return secondTicket; |
|
333 |
} |
|
334 |
||
335 |
private static void debug(String message) { |
|
336 |
// System.err.println(">>> KrbTgsReq: " + message); |
|
337 |
} |
|
338 |
||
339 |
boolean usedSubkey() { |
|
340 |
return useSubkey; |
|
341 |
} |
|
342 |
||
343 |
} |