7183
|
1 |
/*
|
|
2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
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.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.krb5;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.util.Arrays;
|
|
30 |
import sun.security.krb5.internal.HostAddresses;
|
|
31 |
import sun.security.krb5.internal.KDCOptions;
|
|
32 |
import sun.security.krb5.internal.KRBError;
|
|
33 |
import sun.security.krb5.internal.KerberosTime;
|
|
34 |
import sun.security.krb5.internal.Krb5;
|
|
35 |
import sun.security.krb5.internal.PAData;
|
|
36 |
import sun.security.krb5.internal.crypto.EType;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* A manager class for AS-REQ communications.
|
|
40 |
*
|
|
41 |
* This class does:
|
|
42 |
* 1. Gather information to create AS-REQ
|
|
43 |
* 2. Create and send AS-REQ
|
|
44 |
* 3. Receive AS-REP and KRB-ERROR (-KRB_ERR_RESPONSE_TOO_BIG) and parse them
|
|
45 |
* 4. Emit credentials and secret keys (for JAAS storeKey=true)
|
|
46 |
*
|
|
47 |
* This class does not:
|
|
48 |
* 1. Deal with real communications (KdcComm does it, and TGS-REQ)
|
|
49 |
* a. Name of KDCs for a realm
|
|
50 |
* b. Server availability, timeout, UDP or TCP
|
|
51 |
* d. KRB_ERR_RESPONSE_TOO_BIG
|
|
52 |
*
|
|
53 |
* With this class:
|
|
54 |
* 1. KrbAsReq has only one constructor
|
|
55 |
* 2. Krb5LoginModule and Kinit call a single builder
|
|
56 |
* 3. Better handling of sensitive info
|
|
57 |
*
|
|
58 |
* @since 1.7
|
|
59 |
*/
|
|
60 |
|
|
61 |
public final class KrbAsReqBuilder {
|
|
62 |
|
|
63 |
// Common data for AS-REQ fields
|
|
64 |
private KDCOptions options;
|
|
65 |
private PrincipalName cname;
|
|
66 |
private PrincipalName sname;
|
|
67 |
private KerberosTime from;
|
|
68 |
private KerberosTime till;
|
|
69 |
private KerberosTime rtime;
|
|
70 |
private HostAddresses addresses;
|
|
71 |
|
|
72 |
// Secret source: can't be changed once assigned, only one (of the two
|
|
73 |
// sources) can be set and should be non-null
|
|
74 |
private EncryptionKey[] keys;
|
|
75 |
private char[] password;
|
|
76 |
|
|
77 |
// Used to create a ENC-TIMESTAMP in the 2nd AS-REQ
|
|
78 |
private EncryptionKey pakey;
|
|
79 |
private PAData[] paList; // PA-DATA from both KRB-ERROR and AS-REP.
|
|
80 |
// Used by getKeys() only.
|
|
81 |
// Only AS-REP should be enough per RFC,
|
|
82 |
// combined in case etypes are different.
|
|
83 |
|
|
84 |
// The generated and received:
|
|
85 |
int[] eTypes;
|
|
86 |
private KrbAsReq req;
|
|
87 |
private KrbAsRep rep;
|
|
88 |
|
|
89 |
private static enum State {
|
|
90 |
INIT, // Initialized, can still add more initialization info
|
|
91 |
REQ_OK, // AS-REQ performed
|
|
92 |
DESTROYED, // Destroyed, not usable anymore
|
|
93 |
}
|
|
94 |
private State state;
|
|
95 |
|
|
96 |
// Called by other constructors
|
|
97 |
private KrbAsReqBuilder(PrincipalName cname)
|
|
98 |
throws KrbException {
|
|
99 |
if (cname.getRealm() == null) {
|
|
100 |
cname.setRealm(Config.getInstance().getDefaultRealm());
|
|
101 |
}
|
|
102 |
this.cname = cname;
|
|
103 |
state = State.INIT;
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Creates a builder to be used by {@code cname} with existing keys.
|
|
108 |
*
|
|
109 |
* @param cname the client of the AS-REQ. Must not be null. Might have no
|
|
110 |
* realm, where default realm will be used. This realm will be the target
|
|
111 |
* realm for AS-REQ. I believe a client should only get initial TGT from
|
|
112 |
* its own realm.
|
|
113 |
* @param keys must not be null. if empty, might be quite useless.
|
|
114 |
* This argument will neither be modified nor stored by the method.
|
|
115 |
* @throws KrbException
|
|
116 |
*/
|
|
117 |
public KrbAsReqBuilder(PrincipalName cname, EncryptionKey[] keys)
|
|
118 |
throws KrbException {
|
|
119 |
this(cname);
|
|
120 |
this.keys = new EncryptionKey[keys.length];
|
|
121 |
for (int i=0; i<keys.length; i++) {
|
|
122 |
this.keys[i] = (EncryptionKey)keys[i].clone();
|
|
123 |
}
|
|
124 |
eTypes = EType.getDefaults("default_tkt_enctypes", keys);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Creates a builder to be used by {@code cname} with a known password.
|
|
129 |
*
|
|
130 |
* @param cname the client of the AS-REQ. Must not be null. Might have no
|
|
131 |
* realm, where default realm will be used. This realm will be the target
|
|
132 |
* realm for AS-REQ. I believe a client should only get initial TGT from
|
|
133 |
* its own realm.
|
|
134 |
* @param pass must not be null. This argument will neither be modified
|
|
135 |
* nor stored by the method.
|
|
136 |
* @throws KrbException
|
|
137 |
*/
|
|
138 |
public KrbAsReqBuilder(PrincipalName cname, char[] pass)
|
|
139 |
throws KrbException {
|
|
140 |
this(cname);
|
|
141 |
this.password = pass.clone();
|
|
142 |
eTypes = EType.getDefaults("default_tkt_enctypes");
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Retrieves an array of secret keys for the client. This is useful if
|
|
147 |
* the client supplies password but need keys to act as an acceptor
|
|
148 |
* (in JAAS words, isInitiator=true and storeKey=true)
|
|
149 |
* @return original keys if initiated with keys, or generated keys if
|
|
150 |
* password. In latter case, PA-DATA from server might be used to
|
|
151 |
* generate keys. All "default_tkt_enctypes" keys will be generated,
|
|
152 |
* Never null.
|
|
153 |
* @throws KrbException
|
|
154 |
*/
|
|
155 |
public EncryptionKey[] getKeys() throws KrbException {
|
|
156 |
checkState(State.REQ_OK, "Cannot get keys");
|
|
157 |
if (keys != null) {
|
|
158 |
EncryptionKey[] result = new EncryptionKey[keys.length];
|
|
159 |
for (int i=0; i<keys.length; i++) {
|
|
160 |
result[i] = (EncryptionKey)keys[i].clone();
|
|
161 |
}
|
|
162 |
return result;
|
|
163 |
} else {
|
|
164 |
EncryptionKey[] result = new EncryptionKey[eTypes.length];
|
|
165 |
|
|
166 |
/*
|
|
167 |
* Returns an array of keys. Before KrbAsReqBuilder, all etypes
|
|
168 |
* use the same salt which is either the default one or a new salt
|
|
169 |
* coming from PA-DATA. After KrbAsReqBuilder, each etype uses its
|
|
170 |
* own new salt from PA-DATA. For an etype with no PA-DATA new salt
|
|
171 |
* at all, what salt should it use?
|
|
172 |
*
|
|
173 |
* Commonly, the stored keys are only to be used by an acceptor to
|
|
174 |
* decrypt service ticket in AP-REQ. Most impls only allow keys
|
|
175 |
* from a keytab on acceptor, but unfortunately (?) Java supports
|
|
176 |
* acceptor using password. In this case, if the service ticket is
|
|
177 |
* encrypted using an etype which we don't have PA-DATA new salt,
|
|
178 |
* using the default salt is normally wrong (say, case-insensitive
|
|
179 |
* user name). Instead, we would use the new salt of another etype.
|
|
180 |
*/
|
|
181 |
|
|
182 |
String salt = null; // the saved new salt
|
|
183 |
for (int i=0; i<eTypes.length; i++) {
|
|
184 |
PAData.SaltAndParams snp =
|
|
185 |
PAData.getSaltAndParams(eTypes[i], paList);
|
|
186 |
// First round, only calculate those with new salt
|
|
187 |
if (snp.salt != null) {
|
|
188 |
salt = snp.salt;
|
|
189 |
result[i] = EncryptionKey.acquireSecretKey(password,
|
|
190 |
snp.salt,
|
|
191 |
eTypes[i],
|
|
192 |
snp.params);
|
|
193 |
}
|
|
194 |
}
|
|
195 |
if (salt == null) salt = cname.getSalt();
|
|
196 |
for (int i=0; i<eTypes.length; i++) {
|
|
197 |
// Second round, calculate those with no new salt
|
|
198 |
if (result[i] == null) {
|
|
199 |
PAData.SaltAndParams snp =
|
|
200 |
PAData.getSaltAndParams(eTypes[i], paList);
|
|
201 |
result[i] = EncryptionKey.acquireSecretKey(password,
|
|
202 |
salt,
|
|
203 |
eTypes[i],
|
|
204 |
snp.params);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
return result;
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Sets or clears options. If cleared, default options will be used
|
|
213 |
* at creation time.
|
|
214 |
* @param options
|
|
215 |
*/
|
|
216 |
public void setOptions(KDCOptions options) {
|
|
217 |
checkState(State.INIT, "Cannot specify options");
|
|
218 |
this.options = options;
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Sets or clears target. If cleared, KrbAsReq might choose krbtgt
|
|
223 |
* for cname realm
|
|
224 |
* @param sname
|
|
225 |
*/
|
|
226 |
public void setTarget(PrincipalName sname) {
|
|
227 |
checkState(State.INIT, "Cannot specify target");
|
|
228 |
this.sname = sname;
|
|
229 |
}
|
|
230 |
|
|
231 |
/**
|
|
232 |
* Adds or clears addresses. KrbAsReq might add some if empty
|
|
233 |
* field not allowed
|
|
234 |
* @param addresses
|
|
235 |
*/
|
|
236 |
public void setAddresses(HostAddresses addresses) {
|
|
237 |
checkState(State.INIT, "Cannot specify addresses");
|
|
238 |
this.addresses = addresses;
|
|
239 |
}
|
|
240 |
|
|
241 |
/**
|
|
242 |
* Build a KrbAsReq object from all info fed above. Normally this method
|
|
243 |
* will be called twice: initial AS-REQ and second with pakey
|
|
244 |
* @return the KrbAsReq object
|
|
245 |
* @throws KrbException
|
|
246 |
* @throws IOException
|
|
247 |
*/
|
|
248 |
private KrbAsReq build() throws KrbException, IOException {
|
|
249 |
return new KrbAsReq(pakey,
|
|
250 |
options,
|
|
251 |
cname,
|
|
252 |
sname,
|
|
253 |
from,
|
|
254 |
till,
|
|
255 |
rtime,
|
|
256 |
eTypes,
|
|
257 |
addresses);
|
|
258 |
}
|
|
259 |
|
|
260 |
/**
|
|
261 |
* Parses AS-REP, decrypts enc-part, retrieves ticket and session key
|
|
262 |
* @throws KrbException
|
|
263 |
* @throws Asn1Exception
|
|
264 |
* @throws IOException
|
|
265 |
*/
|
|
266 |
private KrbAsReqBuilder resolve() throws KrbException, Asn1Exception, IOException {
|
|
267 |
if (keys != null) {
|
|
268 |
rep.decryptUsingKeys(keys, req);
|
|
269 |
} else {
|
|
270 |
rep.decryptUsingPassword(password, req, cname);
|
|
271 |
}
|
|
272 |
if (rep.getPA() != null) {
|
|
273 |
if (paList == null || paList.length == 0) {
|
|
274 |
paList = rep.getPA();
|
|
275 |
} else {
|
|
276 |
int extraLen = rep.getPA().length;
|
|
277 |
if (extraLen > 0) {
|
|
278 |
int oldLen = paList.length;
|
|
279 |
paList = Arrays.copyOf(paList, paList.length + extraLen);
|
|
280 |
System.arraycopy(rep.getPA(), 0, paList, oldLen, extraLen);
|
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
return this;
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Communication until AS-REP or non preauth-related KRB-ERROR received
|
|
289 |
* @throws KrbException
|
|
290 |
* @throws IOException
|
|
291 |
*/
|
|
292 |
private KrbAsReqBuilder send() throws KrbException, IOException {
|
|
293 |
boolean preAuthFailedOnce = false;
|
|
294 |
KdcComm comm = new KdcComm(cname.getRealmAsString());
|
|
295 |
while (true) {
|
|
296 |
try {
|
|
297 |
req = build();
|
|
298 |
rep = new KrbAsRep(comm.send(req.encoding()));
|
|
299 |
return this;
|
|
300 |
} catch (KrbException ke) {
|
|
301 |
if (!preAuthFailedOnce && (
|
|
302 |
ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED ||
|
|
303 |
ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED)) {
|
|
304 |
if (Krb5.DEBUG) {
|
|
305 |
System.out.println("KrbAsReqBuilder: " +
|
|
306 |
"PREAUTH FAILED/REQ, re-send AS-REQ");
|
|
307 |
}
|
|
308 |
preAuthFailedOnce = true;
|
|
309 |
KRBError kerr = ke.getError();
|
|
310 |
if (password == null) {
|
|
311 |
pakey = EncryptionKey.findKey(kerr.getEType(), keys);
|
|
312 |
} else {
|
|
313 |
PAData.SaltAndParams snp = PAData.getSaltAndParams(
|
|
314 |
kerr.getEType(), kerr.getPA());
|
|
315 |
if (kerr.getEType() == 0) {
|
|
316 |
// Possible if PA-PW-SALT is in KRB-ERROR. RFC
|
|
317 |
// does not recommend this
|
|
318 |
pakey = EncryptionKey.acquireSecretKey(password,
|
|
319 |
snp.salt == null ? cname.getSalt() : snp.salt,
|
|
320 |
eTypes[0],
|
|
321 |
null);
|
|
322 |
} else {
|
|
323 |
pakey = EncryptionKey.acquireSecretKey(password,
|
|
324 |
snp.salt == null ? cname.getSalt() : snp.salt,
|
|
325 |
kerr.getEType(),
|
|
326 |
snp.params);
|
|
327 |
}
|
|
328 |
}
|
|
329 |
paList = kerr.getPA(); // Update current paList
|
|
330 |
} else {
|
|
331 |
throw ke;
|
|
332 |
}
|
|
333 |
}
|
|
334 |
}
|
|
335 |
}
|
|
336 |
|
|
337 |
/**
|
|
338 |
* Performs AS-REQ send and AS-REP receive.
|
|
339 |
* Maybe a state is needed here, to divide prepare process and getCreds.
|
|
340 |
* @throws KrbException
|
|
341 |
* @throws Asn1Exception
|
|
342 |
* @throws IOException
|
|
343 |
*/
|
|
344 |
public KrbAsReqBuilder action()
|
|
345 |
throws KrbException, Asn1Exception, IOException {
|
|
346 |
checkState(State.INIT, "Cannot call action");
|
|
347 |
state = State.REQ_OK;
|
|
348 |
return send().resolve();
|
|
349 |
}
|
|
350 |
|
|
351 |
/**
|
|
352 |
* Gets Credentials object after action
|
|
353 |
*/
|
|
354 |
public Credentials getCreds() {
|
|
355 |
checkState(State.REQ_OK, "Cannot retrieve creds");
|
|
356 |
return rep.getCreds();
|
|
357 |
}
|
|
358 |
|
|
359 |
/**
|
|
360 |
* Gets another type of Credentials after action
|
|
361 |
*/
|
|
362 |
public sun.security.krb5.internal.ccache.Credentials getCCreds() {
|
|
363 |
checkState(State.REQ_OK, "Cannot retrieve CCreds");
|
|
364 |
return rep.getCCreds();
|
|
365 |
}
|
|
366 |
|
|
367 |
/**
|
|
368 |
* Destroys the object and clears keys and password info.
|
|
369 |
*/
|
|
370 |
public void destroy() {
|
|
371 |
state = State.DESTROYED;
|
|
372 |
if (keys != null) {
|
|
373 |
for (EncryptionKey k: keys) {
|
|
374 |
k.destroy();
|
|
375 |
}
|
|
376 |
keys = null;
|
|
377 |
}
|
|
378 |
if (password != null) {
|
|
379 |
Arrays.fill(password, (char)0);
|
|
380 |
password = null;
|
|
381 |
}
|
|
382 |
}
|
|
383 |
|
|
384 |
/**
|
|
385 |
* Checks if the current state is the specified one.
|
|
386 |
* @param st the expected state
|
|
387 |
* @param msg error message if state is not correct
|
|
388 |
* @throws IllegalStateException if state is not correct
|
|
389 |
*/
|
|
390 |
private void checkState(State st, String msg) {
|
|
391 |
if (state != st) {
|
|
392 |
throw new IllegalStateException(msg + " at " + st + " state");
|
|
393 |
}
|
|
394 |
}
|
|
395 |
}
|