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