author | weijun |
Tue, 11 Aug 2009 12:20:32 +0800 | |
changeset 3483 | a16fce1820ef |
parent 2 | 90ce3da70b43 |
child 3949 | 00603a93b589 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
2 |
* Portions Copyright 2000-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
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.ccache.CredentialsCache; |
|
36 |
import java.util.StringTokenizer; |
|
37 |
import sun.security.krb5.internal.ktab.*; |
|
38 |
import sun.security.krb5.internal.crypto.EType; |
|
39 |
import java.io.File; |
|
40 |
import java.io.IOException; |
|
41 |
import java.util.Date; |
|
42 |
import java.util.Vector; |
|
43 |
import java.io.BufferedReader; |
|
44 |
import java.io.InputStreamReader; |
|
45 |
import java.io.UnsupportedEncodingException; |
|
46 |
import java.net.InetAddress; |
|
47 |
||
48 |
/** |
|
49 |
* This class encapsulates the concept of a Kerberos service |
|
50 |
* credential. That includes a Kerberos ticket and an associated |
|
51 |
* session key. |
|
52 |
*/ |
|
53 |
public class Credentials { |
|
54 |
||
55 |
Ticket ticket; |
|
56 |
PrincipalName client; |
|
57 |
PrincipalName server; |
|
58 |
EncryptionKey key; |
|
59 |
TicketFlags flags; |
|
60 |
KerberosTime authTime; |
|
61 |
KerberosTime startTime; |
|
62 |
KerberosTime endTime; |
|
63 |
KerberosTime renewTill; |
|
64 |
HostAddresses cAddr; |
|
65 |
EncryptionKey serviceKey; |
|
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
66 |
AuthorizationData authzData; |
2 | 67 |
private static boolean DEBUG = Krb5.DEBUG; |
68 |
private static CredentialsCache cache; |
|
69 |
static boolean alreadyLoaded = false; |
|
70 |
private static boolean alreadyTried = false; |
|
71 |
private static native Credentials acquireDefaultNativeCreds(); |
|
72 |
||
73 |
public Credentials(Ticket new_ticket, |
|
74 |
PrincipalName new_client, |
|
75 |
PrincipalName new_server, |
|
76 |
EncryptionKey new_key, |
|
77 |
TicketFlags new_flags, |
|
78 |
KerberosTime authTime, |
|
79 |
KerberosTime new_startTime, |
|
80 |
KerberosTime new_endTime, |
|
81 |
KerberosTime renewTill, |
|
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
82 |
HostAddresses cAddr, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
83 |
AuthorizationData authzData) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
84 |
this(new_ticket, new_client, new_server, new_key, new_flags, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
85 |
authTime, new_startTime, new_endTime, renewTill, cAddr); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
86 |
this.authzData = authzData; |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
87 |
} |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
88 |
|
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
89 |
public Credentials(Ticket new_ticket, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
90 |
PrincipalName new_client, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
91 |
PrincipalName new_server, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
92 |
EncryptionKey new_key, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
93 |
TicketFlags new_flags, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
94 |
KerberosTime authTime, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
95 |
KerberosTime new_startTime, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
96 |
KerberosTime new_endTime, |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
97 |
KerberosTime renewTill, |
2 | 98 |
HostAddresses cAddr) { |
99 |
ticket = new_ticket; |
|
100 |
client = new_client; |
|
101 |
server = new_server; |
|
102 |
key = new_key; |
|
103 |
flags = new_flags; |
|
104 |
this.authTime = authTime; |
|
105 |
startTime = new_startTime; |
|
106 |
endTime = new_endTime; |
|
107 |
this.renewTill = renewTill; |
|
108 |
this.cAddr = cAddr; |
|
109 |
} |
|
110 |
||
111 |
public Credentials(byte[] encoding, |
|
112 |
String client, |
|
113 |
String server, |
|
114 |
byte[] keyBytes, |
|
115 |
int keyType, |
|
116 |
boolean[] flags, |
|
117 |
Date authTime, |
|
118 |
Date startTime, |
|
119 |
Date endTime, |
|
120 |
Date renewTill, |
|
121 |
InetAddress[] cAddrs) throws KrbException, IOException { |
|
122 |
this(new Ticket(encoding), |
|
123 |
new PrincipalName(client, PrincipalName.KRB_NT_PRINCIPAL), |
|
124 |
new PrincipalName(server, PrincipalName.KRB_NT_SRV_INST), |
|
125 |
new EncryptionKey(keyType, keyBytes), |
|
126 |
(flags == null? null: new TicketFlags(flags)), |
|
127 |
(authTime == null? null: new KerberosTime(authTime)), |
|
128 |
(startTime == null? null: new KerberosTime(startTime)), |
|
129 |
(endTime == null? null: new KerberosTime(endTime)), |
|
130 |
(renewTill == null? null: new KerberosTime(renewTill)), |
|
131 |
null); // caddrs are in the encoding at this point |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Acquires a service ticket for the specified service |
|
136 |
* principal. If the service ticket is not already available, it |
|
137 |
* obtains a new one from the KDC. |
|
138 |
*/ |
|
139 |
/* |
|
140 |
public Credentials(Credentials tgt, PrincipalName service) |
|
141 |
throws KrbException { |
|
142 |
} |
|
143 |
*/ |
|
144 |
||
145 |
public final PrincipalName getClient() { |
|
146 |
return client; |
|
147 |
} |
|
148 |
||
149 |
public final PrincipalName getServer() { |
|
150 |
return server; |
|
151 |
} |
|
152 |
||
153 |
public final EncryptionKey getSessionKey() { |
|
154 |
return key; |
|
155 |
} |
|
156 |
||
157 |
public final Date getAuthTime() { |
|
158 |
if (authTime != null) { |
|
159 |
return authTime.toDate(); |
|
160 |
} else { |
|
161 |
return null; |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
public final Date getStartTime() { |
|
166 |
if (startTime != null) |
|
167 |
{ |
|
168 |
return startTime.toDate(); |
|
169 |
} |
|
170 |
return null; |
|
171 |
} |
|
172 |
||
173 |
public final Date getEndTime() { |
|
174 |
if (endTime != null) |
|
175 |
{ |
|
176 |
return endTime.toDate(); |
|
177 |
} |
|
178 |
return null; |
|
179 |
} |
|
180 |
||
181 |
public final Date getRenewTill() { |
|
182 |
if (renewTill != null) |
|
183 |
{ |
|
184 |
return renewTill.toDate(); |
|
185 |
} |
|
186 |
return null; |
|
187 |
} |
|
188 |
||
189 |
public final boolean[] getFlags() { |
|
190 |
if (flags == null) // Can be in a KRB-CRED |
|
191 |
return null; |
|
192 |
return flags.toBooleanArray(); |
|
193 |
} |
|
194 |
||
195 |
public final InetAddress[] getClientAddresses() { |
|
196 |
||
197 |
if (cAddr == null) |
|
198 |
return null; |
|
199 |
||
200 |
return cAddr.getInetAddresses(); |
|
201 |
} |
|
202 |
||
203 |
public final byte[] getEncoded() { |
|
204 |
byte[] retVal = null; |
|
205 |
try { |
|
206 |
retVal = ticket.asn1Encode(); |
|
207 |
} catch (Asn1Exception e) { |
|
208 |
if (DEBUG) |
|
209 |
System.out.println(e); |
|
210 |
} catch (IOException ioe) { |
|
211 |
if (DEBUG) |
|
212 |
System.out.println(ioe); |
|
213 |
} |
|
214 |
return retVal; |
|
215 |
} |
|
216 |
||
217 |
public boolean isForwardable() { |
|
218 |
return flags.get(Krb5.TKT_OPTS_FORWARDABLE); |
|
219 |
} |
|
220 |
||
221 |
public boolean isRenewable() { |
|
222 |
return flags.get(Krb5.TKT_OPTS_RENEWABLE); |
|
223 |
} |
|
224 |
||
225 |
public Ticket getTicket() { |
|
226 |
return ticket; |
|
227 |
} |
|
228 |
||
229 |
public TicketFlags getTicketFlags() { |
|
230 |
return flags; |
|
231 |
} |
|
232 |
||
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
233 |
public AuthorizationData getAuthzData() { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
234 |
return authzData; |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
2
diff
changeset
|
235 |
} |
2 | 236 |
/** |
237 |
* Checks if the service ticket returned by the KDC has the OK-AS-DELEGATE |
|
238 |
* flag set |
|
239 |
* @return true if OK-AS_DELEGATE flag is set, otherwise, return false. |
|
240 |
*/ |
|
241 |
public boolean checkDelegate() { |
|
242 |
return (flags.get(Krb5.TKT_OPTS_DELEGATE)); |
|
243 |
} |
|
244 |
||
245 |
public Credentials renew() throws KrbException, IOException { |
|
246 |
KDCOptions options = new KDCOptions(); |
|
247 |
options.set(KDCOptions.RENEW, true); |
|
248 |
/* |
|
249 |
* Added here to pass KrbKdcRep.check:73 |
|
250 |
*/ |
|
251 |
options.set(KDCOptions.RENEWABLE, true); |
|
252 |
||
253 |
return new KrbTgsReq(options, |
|
254 |
this, |
|
255 |
server, |
|
256 |
null, // from |
|
257 |
null, // till |
|
258 |
null, // rtime |
|
259 |
null, // eTypes |
|
260 |
cAddr, |
|
261 |
null, |
|
262 |
null, |
|
263 |
null).sendAndGetCreds(); |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Returns a TGT for the given client principal from a ticket cache. |
|
268 |
* |
|
269 |
* @param princ the client principal. A value of null means that the |
|
270 |
* default principal name in the credentials cache will be used. |
|
271 |
* @param ticketCache the path to the tickets file. A value |
|
272 |
* of null will be accepted to indicate that the default |
|
273 |
* path should be searched |
|
274 |
* @returns the TGT credentials or null if none were found. If the tgt |
|
275 |
* expired, it is the responsibility of the caller to determine this. |
|
276 |
*/ |
|
277 |
public static Credentials acquireTGTFromCache(PrincipalName princ, |
|
278 |
String ticketCache) |
|
279 |
throws KrbException, IOException { |
|
280 |
||
281 |
if (ticketCache == null) { |
|
282 |
// The default ticket cache on Windows is not a file. |
|
283 |
String os = java.security.AccessController.doPrivileged( |
|
284 |
new sun.security.action.GetPropertyAction("os.name")); |
|
285 |
if (os.toUpperCase().startsWith("WINDOWS")) { |
|
286 |
Credentials creds = acquireDefaultCreds(); |
|
287 |
if (creds == null) { |
|
288 |
if (DEBUG) { |
|
289 |
System.out.println(">>> Found no TGT's in LSA"); |
|
290 |
} |
|
291 |
return null; |
|
292 |
} |
|
293 |
if (princ != null) { |
|
294 |
if (creds.getClient().equals(princ)) { |
|
295 |
if (DEBUG) { |
|
296 |
System.out.println(">>> Obtained TGT from LSA: " |
|
297 |
+ creds); |
|
298 |
} |
|
299 |
return creds; |
|
300 |
} else { |
|
301 |
if (DEBUG) { |
|
302 |
System.out.println(">>> LSA contains TGT for " |
|
303 |
+ creds.getClient() |
|
304 |
+ " not " |
|
305 |
+ princ); |
|
306 |
} |
|
307 |
return null; |
|
308 |
} |
|
309 |
} else { |
|
310 |
if (DEBUG) { |
|
311 |
System.out.println(">>> Obtained TGT from LSA: " |
|
312 |
+ creds); |
|
313 |
} |
|
314 |
return creds; |
|
315 |
} |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
/* |
|
320 |
* Returns the appropriate cache. If ticketCache is null, it is the |
|
321 |
* default cache otherwise it is the cache filename contained in it. |
|
322 |
*/ |
|
323 |
CredentialsCache ccache = |
|
324 |
CredentialsCache.getInstance(princ, ticketCache); |
|
325 |
||
326 |
if (ccache == null) |
|
327 |
return null; |
|
328 |
||
329 |
sun.security.krb5.internal.ccache.Credentials tgtCred = |
|
330 |
ccache.getDefaultCreds(); |
|
331 |
||
332 |
if (EType.isSupported(tgtCred.getEType())) { |
|
333 |
return tgtCred.setKrbCreds(); |
|
334 |
} else { |
|
335 |
if (DEBUG) { |
|
336 |
System.out.println( |
|
337 |
">>> unsupported key type found the default TGT: " + |
|
338 |
tgtCred.getEType()); |
|
339 |
} |
|
340 |
return null; |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Returns a TGT for the given client principal via an AS-Exchange. |
|
346 |
* This method causes pre-authentication data to be sent in the |
|
347 |
* AS-REQ. |
|
348 |
* |
|
349 |
* @param princ the client principal. This value cannot be null. |
|
350 |
* @param secretKey the secret key of the client principal.This value |
|
351 |
* cannot be null. |
|
352 |
* @returns the TGT credentials |
|
353 |
*/ |
|
354 |
public static Credentials acquireTGT(PrincipalName princ, |
|
355 |
EncryptionKey[] secretKeys, |
|
356 |
char[] password) |
|
357 |
throws KrbException, IOException { |
|
358 |
||
359 |
if (princ == null) |
|
360 |
throw new IllegalArgumentException( |
|
361 |
"Cannot have null principal to do AS-Exchange"); |
|
362 |
||
363 |
if (secretKeys == null) |
|
364 |
throw new IllegalArgumentException( |
|
365 |
"Cannot have null secretKey to do AS-Exchange"); |
|
366 |
||
367 |
KrbAsRep asRep = null; |
|
368 |
try { |
|
369 |
asRep = sendASRequest(princ, secretKeys, null); |
|
370 |
} catch (KrbException ke) { |
|
371 |
if ((ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) || |
|
372 |
(ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED)) { |
|
373 |
// process pre-auth info |
|
374 |
if (DEBUG) { |
|
375 |
System.out.println("AcquireTGT: PREAUTH FAILED/REQUIRED," + |
|
376 |
" re-send AS-REQ"); |
|
377 |
} |
|
378 |
||
379 |
KRBError error = ke.getError(); |
|
380 |
// update salt in PrincipalName |
|
381 |
byte[] newSalt = error.getSalt(); |
|
382 |
if (newSalt != null && newSalt.length > 0) { |
|
383 |
princ.setSalt(new String(newSalt)); |
|
384 |
} |
|
385 |
||
386 |
// refresh keys |
|
387 |
if (password != null) { |
|
388 |
secretKeys = EncryptionKey.acquireSecretKeys(password, |
|
389 |
princ.getSalt(), true, |
|
390 |
error.getEType(), error.getParams()); |
|
391 |
} |
|
392 |
asRep = sendASRequest(princ, secretKeys, ke.getError()); |
|
393 |
} else { |
|
394 |
throw ke; |
|
395 |
} |
|
396 |
} |
|
397 |
return asRep.getCreds(); |
|
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Sends the AS-REQ |
|
402 |
*/ |
|
403 |
private static KrbAsRep sendASRequest(PrincipalName princ, |
|
404 |
EncryptionKey[] secretKeys, KRBError error) |
|
405 |
throws KrbException, IOException { |
|
406 |
||
407 |
// %%% |
|
408 |
KrbAsReq asReq = null; |
|
409 |
if (error == null) { |
|
410 |
asReq = new KrbAsReq(princ, secretKeys); |
|
411 |
} else { |
|
412 |
asReq = new KrbAsReq(princ, secretKeys, true, |
|
413 |
error.getEType(), error.getSalt(), error.getParams()); |
|
414 |
} |
|
415 |
||
416 |
String kdc = null; |
|
417 |
KrbAsRep asRep = null; |
|
418 |
try { |
|
419 |
kdc = asReq.send(); |
|
420 |
asRep = asReq.getReply(secretKeys); |
|
421 |
} catch (KrbException ke) { |
|
422 |
if (ke.returnCode() == Krb5.KRB_ERR_RESPONSE_TOO_BIG) { |
|
423 |
asReq.send(princ.getRealmString(), kdc, true); |
|
424 |
asRep = asReq.getReply(secretKeys); |
|
425 |
} else { |
|
426 |
throw ke; |
|
427 |
} |
|
428 |
} |
|
429 |
return asRep; |
|
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Acquires default credentials. |
|
434 |
* <br>The possible locations for default credentials cache is searched in |
|
435 |
* the following order: |
|
436 |
* <ol> |
|
437 |
* <li> The directory and cache file name specified by "KRB5CCNAME" system. |
|
438 |
* property. |
|
439 |
* <li> The directory and cache file name specified by "KRB5CCNAME" |
|
440 |
* environment variable. |
|
441 |
* <li> A cache file named krb5cc_{user.name} at {user.home} directory. |
|
442 |
* </ol> |
|
443 |
* @return a <code>KrbCreds</code> object if the credential is found, |
|
444 |
* otherwise return null. |
|
445 |
*/ |
|
446 |
||
447 |
// this method is intentionally changed to not check if the caller's |
|
448 |
// principal name matches cache file's principal name. |
|
449 |
// It assumes that the GSS call has |
|
450 |
// the privilege to access the default cache file. |
|
451 |
||
452 |
public static synchronized Credentials acquireDefaultCreds() { |
|
453 |
Credentials result = null; |
|
454 |
||
455 |
if (cache == null) { |
|
456 |
cache = CredentialsCache.getInstance(); |
|
457 |
} |
|
458 |
if (cache != null) { |
|
459 |
if (DEBUG) { |
|
460 |
System.out.println(">>> KrbCreds found the default ticket " + |
|
461 |
"granting ticket in credential cache."); |
|
462 |
} |
|
463 |
sun.security.krb5.internal.ccache.Credentials temp = |
|
464 |
cache.getDefaultCreds(); |
|
465 |
if (EType.isSupported(temp.getEType())) { |
|
466 |
result = temp.setKrbCreds(); |
|
467 |
} else { |
|
468 |
if (DEBUG) { |
|
469 |
System.out.println( |
|
470 |
">>> unsupported key type found the default TGT: " + |
|
471 |
temp.getEType()); |
|
472 |
} |
|
473 |
} |
|
474 |
} |
|
475 |
if (result == null) { |
|
476 |
// Doesn't seem to be a default cache on this system or |
|
477 |
// TGT has unsupported encryption type |
|
478 |
||
479 |
if (!alreadyTried) { |
|
480 |
// See if there's any native code to load |
|
481 |
try { |
|
482 |
ensureLoaded(); |
|
483 |
} catch (Exception e) { |
|
484 |
if (DEBUG) { |
|
485 |
System.out.println("Can not load credentials cache"); |
|
486 |
e.printStackTrace(); |
|
487 |
} |
|
488 |
alreadyTried = true; |
|
489 |
} |
|
490 |
} |
|
491 |
if (alreadyLoaded) { |
|
492 |
// There is some native code |
|
493 |
if (DEBUG) |
|
494 |
System.out.println(">> Acquire default native Credentials"); |
|
495 |
result = acquireDefaultNativeCreds(); |
|
496 |
// only TGT with DES key will be returned by native method |
|
497 |
} |
|
498 |
} |
|
499 |
return result; |
|
500 |
} |
|
501 |
||
502 |
||
503 |
/** |
|
504 |
* Gets service credential from key table. The credential is used to |
|
505 |
* decrypt the received client message |
|
506 |
* and authenticate the client by verifying the client's credential. |
|
507 |
* |
|
508 |
* @param serviceName the name of service, using format component@realm |
|
509 |
* @param keyTabFile the file of key table. |
|
510 |
* @return a <code>KrbCreds</code> object. |
|
511 |
*/ |
|
512 |
public static Credentials getServiceCreds(String serviceName, |
|
513 |
File keyTabFile) { |
|
514 |
EncryptionKey k = null; |
|
515 |
PrincipalName service = null; |
|
516 |
Credentials result = null; |
|
517 |
try { |
|
518 |
service = new PrincipalName(serviceName); |
|
519 |
if (service.getRealm() == null) { |
|
520 |
String realm = Config.getInstance().getDefaultRealm(); |
|
521 |
if (realm == null) { |
|
522 |
return null; |
|
523 |
} else { |
|
524 |
service.setRealm(realm); |
|
525 |
} |
|
526 |
} |
|
527 |
} catch (RealmException e) { |
|
528 |
if (DEBUG) { |
|
529 |
e.printStackTrace(); |
|
530 |
} |
|
531 |
return null; |
|
532 |
} catch (KrbException e) { |
|
533 |
if (DEBUG) { |
|
534 |
e.printStackTrace(); |
|
535 |
} |
|
536 |
return null; |
|
537 |
} |
|
538 |
KeyTab kt; |
|
539 |
if (keyTabFile == null) { |
|
540 |
kt = KeyTab.getInstance(); |
|
541 |
} else { |
|
542 |
kt = KeyTab.getInstance(keyTabFile); |
|
543 |
} |
|
544 |
if ((kt != null) && (kt.findServiceEntry(service))) { |
|
545 |
k = kt.readServiceKey(service); |
|
546 |
result = new Credentials(null, service, null, null, null, |
|
547 |
null, null, null, null, null); |
|
548 |
result.serviceKey = k; |
|
549 |
} |
|
550 |
return result; |
|
551 |
} |
|
552 |
||
553 |
||
554 |
||
555 |
/** |
|
556 |
* Acquires credentials for a specified service using initial credential. |
|
557 |
* When the service has a different realm |
|
558 |
* from the initial credential, we do cross-realm authentication |
|
559 |
* - first, we use the current credential to get |
|
560 |
* a cross-realm credential from the local KDC, then use that |
|
561 |
* cross-realm credential to request service credential |
|
562 |
* from the foreigh KDC. |
|
563 |
* |
|
564 |
* @param service the name of service principal using format |
|
565 |
* components@realm |
|
566 |
* @param ccreds client's initial credential. |
|
567 |
* @exception IOException if an error occurs in reading the credentials |
|
568 |
* cache |
|
569 |
* @exception KrbException if an error occurs specific to Kerberos |
|
570 |
* @return a <code>Credentials</code> object. |
|
571 |
*/ |
|
572 |
||
573 |
public static Credentials acquireServiceCreds(String service, |
|
574 |
Credentials ccreds) |
|
575 |
throws KrbException, IOException { |
|
576 |
return CredentialsUtil.acquireServiceCreds(service, ccreds); |
|
577 |
} |
|
578 |
||
579 |
||
580 |
/* |
|
581 |
* This method does the real job to request the service credential. |
|
582 |
*/ |
|
583 |
||
584 |
private static Credentials serviceCreds(ServiceName service, |
|
585 |
Credentials ccreds) |
|
586 |
throws KrbException, IOException { |
|
587 |
return new KrbTgsReq( |
|
588 |
new KDCOptions(), |
|
589 |
ccreds, |
|
590 |
service, |
|
591 |
null, // KerberosTime from |
|
592 |
null, // KerberosTime till |
|
593 |
null, // KerberosTime rtime |
|
594 |
null, // int[] eTypes |
|
595 |
null, // HostAddresses addresses |
|
596 |
null, // AuthorizationData |
|
597 |
null, // Ticket[] additionalTickets |
|
598 |
null // EncryptionKey subSessionKey |
|
599 |
).sendAndGetCreds(); |
|
600 |
} |
|
601 |
||
602 |
public CredentialsCache getCache() { |
|
603 |
return cache; |
|
604 |
} |
|
605 |
||
606 |
public EncryptionKey getServiceKey() { |
|
607 |
return serviceKey; |
|
608 |
} |
|
609 |
||
610 |
/* |
|
611 |
* Prints out debug info. |
|
612 |
*/ |
|
613 |
public static void printDebug(Credentials c) { |
|
614 |
System.out.println(">>> DEBUG: ----Credentials----"); |
|
615 |
System.out.println("\tclient: " + c.client.toString()); |
|
616 |
System.out.println("\tserver: " + c.server.toString()); |
|
617 |
System.out.println("\tticket: realm: " + c.ticket.realm.toString()); |
|
618 |
System.out.println("\t sname: " + c.ticket.sname.toString()); |
|
619 |
if (c.startTime != null) { |
|
620 |
System.out.println("\tstartTime: " + c.startTime.getTime()); |
|
621 |
} |
|
622 |
System.out.println("\tendTime: " + c.endTime.getTime()); |
|
623 |
System.out.println(" ----Credentials end----"); |
|
624 |
} |
|
625 |
||
626 |
||
627 |
static void ensureLoaded() { |
|
628 |
java.security.AccessController.doPrivileged( |
|
629 |
new java.security.PrivilegedAction<Void> () { |
|
630 |
public Void run() { |
|
631 |
System.loadLibrary("w2k_lsa_auth"); |
|
632 |
return null; |
|
633 |
} |
|
634 |
}); |
|
635 |
alreadyLoaded = true; |
|
636 |
} |
|
637 |
||
638 |
public String toString() { |
|
639 |
StringBuffer buffer = new StringBuffer("Credentials:"); |
|
640 |
buffer.append("\nclient=").append(client); |
|
641 |
buffer.append("\nserver=").append(server); |
|
642 |
if (authTime != null) { |
|
643 |
buffer.append("\nauthTime=").append(authTime); |
|
644 |
} |
|
645 |
if (startTime != null) { |
|
646 |
buffer.append("\nstartTime=").append(startTime); |
|
647 |
} |
|
648 |
buffer.append("\nendTime=").append(endTime); |
|
649 |
buffer.append("\nrenewTill=").append(renewTill); |
|
650 |
buffer.append("\nflags: ").append(flags); |
|
651 |
buffer.append("\nEType (int): ").append(key.getEType()); |
|
652 |
return buffer.toString(); |
|
653 |
} |
|
654 |
||
655 |
} |