2
|
1 |
/*
|
|
2 |
* Portions Copyright 2000-2006 Sun Microsystems, Inc. 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. 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.internal.ktab;
|
|
33 |
|
|
34 |
import sun.security.krb5.*;
|
|
35 |
import sun.security.krb5.internal.*;
|
|
36 |
import sun.security.krb5.internal.crypto.*;
|
|
37 |
import java.util.Vector;
|
|
38 |
import java.util.ArrayList;
|
|
39 |
import java.util.Arrays;
|
|
40 |
import java.io.IOException;
|
|
41 |
import java.io.FileInputStream;
|
|
42 |
import java.io.FileOutputStream;
|
|
43 |
import java.io.File;
|
|
44 |
import java.util.StringTokenizer;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* This class represents key table. The key table functions deal with storing
|
|
48 |
* and retrieving service keys for use in authentication exchanges.
|
|
49 |
*
|
|
50 |
* @author Yanni Zhang
|
|
51 |
*/
|
|
52 |
public class KeyTab implements KeyTabConstants {
|
|
53 |
int kt_vno;
|
|
54 |
private static KeyTab singleton = null;
|
|
55 |
private static final boolean DEBUG = Krb5.DEBUG;
|
|
56 |
private static String name;
|
|
57 |
private Vector<KeyTabEntry> entries = new Vector<KeyTabEntry> ();
|
|
58 |
|
|
59 |
private KeyTab(String filename) throws IOException, RealmException {
|
|
60 |
init(filename);
|
|
61 |
}
|
|
62 |
|
|
63 |
public static KeyTab getInstance(String s) {
|
|
64 |
name = parse(s);
|
|
65 |
if (name == null) {
|
|
66 |
return getInstance();
|
|
67 |
}
|
|
68 |
return getInstance(new File(name));
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Gets the single instance of KeyTab class.
|
|
73 |
* @param file the key tab file.
|
|
74 |
* @return single instance of KeyTab;
|
|
75 |
* return null if error occurs while reading data out of the file.
|
|
76 |
*/
|
|
77 |
public static KeyTab getInstance(File file) {
|
|
78 |
try {
|
|
79 |
if (!(file.exists())) {
|
|
80 |
singleton = null;
|
|
81 |
} else {
|
|
82 |
String fname = file.getAbsolutePath();
|
|
83 |
// Since this class deals with file I/O operations,
|
|
84 |
// we want only one class instance existing.
|
|
85 |
if (singleton != null) {
|
|
86 |
File kfile = new File(singleton.name);
|
|
87 |
String kname = kfile.getAbsolutePath();
|
|
88 |
if (kname.equalsIgnoreCase(fname)) {
|
|
89 |
if (DEBUG) {
|
|
90 |
System.out.println("KeyTab instance already exists");
|
|
91 |
}
|
|
92 |
}
|
|
93 |
} else {
|
|
94 |
singleton = new KeyTab(fname);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
} catch (Exception e) {
|
|
98 |
singleton = null;
|
|
99 |
if (DEBUG) {
|
|
100 |
System.out.println("Could not obtain an instance of KeyTab" +
|
|
101 |
e.getMessage());
|
|
102 |
}
|
|
103 |
}
|
|
104 |
return singleton;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Gets the single instance of KeyTab class.
|
|
109 |
* @return single instance of KeyTab; return null if default keytab file
|
|
110 |
* does not exist, or error occurs while reading data from the file.
|
|
111 |
*/
|
|
112 |
public static KeyTab getInstance() {
|
|
113 |
try {
|
|
114 |
name = getDefaultKeyTab();
|
|
115 |
if (name != null) {
|
|
116 |
singleton = getInstance(new File(name));
|
|
117 |
}
|
|
118 |
} catch (Exception e) {
|
|
119 |
singleton = null;
|
|
120 |
if (DEBUG) {
|
|
121 |
System.out.println("Could not obtain an instance of KeyTab" +
|
|
122 |
e.getMessage());
|
|
123 |
}
|
|
124 |
}
|
|
125 |
return singleton;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* The location of keytab file will be read from the configuration file
|
|
130 |
* If it is not specified, consider user.home as the keytab file's
|
|
131 |
* default location.
|
|
132 |
*/
|
|
133 |
private static String getDefaultKeyTab() {
|
|
134 |
if (name != null) {
|
|
135 |
return name;
|
|
136 |
} else {
|
|
137 |
String kname = null;
|
|
138 |
try {
|
|
139 |
String keytab_names = Config.getInstance().getDefault
|
|
140 |
("default_keytab_name", "libdefaults");
|
|
141 |
if (keytab_names != null) {
|
|
142 |
StringTokenizer st = new StringTokenizer(keytab_names, " ");
|
|
143 |
while (st.hasMoreTokens()) {
|
|
144 |
kname = parse(st.nextToken());
|
|
145 |
if (kname != null) {
|
|
146 |
break;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
} catch (KrbException e) {
|
|
151 |
kname = null;
|
|
152 |
}
|
|
153 |
|
|
154 |
if (kname == null) {
|
|
155 |
String user_home =
|
|
156 |
java.security.AccessController.doPrivileged(
|
|
157 |
new sun.security.action.GetPropertyAction("user.home"));
|
|
158 |
|
|
159 |
if (user_home == null) {
|
|
160 |
user_home =
|
|
161 |
java.security.AccessController.doPrivileged(
|
|
162 |
new sun.security.action.GetPropertyAction("user.dir"));
|
|
163 |
}
|
|
164 |
|
|
165 |
if (user_home != null) {
|
|
166 |
kname = user_home + File.separator + "krb5.keytab";
|
|
167 |
}
|
|
168 |
}
|
|
169 |
return kname;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
private static String parse(String name) {
|
|
174 |
String kname = null;
|
|
175 |
if (name == null) {
|
|
176 |
return null;
|
|
177 |
}
|
|
178 |
if ((name.length() >= 5) &&
|
|
179 |
(name.substring(0, 5).equalsIgnoreCase("FILE:"))) {
|
|
180 |
kname = name.substring(5);
|
|
181 |
} else if ((name.length() >= 9) &&
|
|
182 |
(name.substring(0, 9).equalsIgnoreCase("ANY:FILE:"))) {
|
|
183 |
// this format found in MIT's krb5.ini.
|
|
184 |
kname = name.substring(9);
|
|
185 |
} else if ((name.length() >= 7) &&
|
|
186 |
(name.substring(0, 7).equalsIgnoreCase("SRVTAB:"))) {
|
|
187 |
// this format found in MIT's krb5.ini.
|
|
188 |
kname = name.substring(7);
|
|
189 |
} else
|
|
190 |
kname = name;
|
|
191 |
return kname;
|
|
192 |
}
|
|
193 |
|
|
194 |
private synchronized void init(String filename)
|
|
195 |
throws IOException, RealmException {
|
|
196 |
|
|
197 |
if (filename != null) {
|
|
198 |
KeyTabInputStream kis =
|
|
199 |
new KeyTabInputStream(new FileInputStream(filename));
|
|
200 |
load(kis);
|
|
201 |
kis.close();
|
|
202 |
name = filename;
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
private void load(KeyTabInputStream kis)
|
|
207 |
throws IOException, RealmException {
|
|
208 |
|
|
209 |
entries.clear();
|
|
210 |
kt_vno = kis.readVersion();
|
|
211 |
if (kt_vno == KRB5_KT_VNO_1) {
|
|
212 |
kis.setNativeByteOrder();
|
|
213 |
}
|
|
214 |
int entryLength = 0;
|
|
215 |
KeyTabEntry entry;
|
|
216 |
while (kis.available() > 0) {
|
|
217 |
entryLength = kis.readEntryLength();
|
|
218 |
entry = kis.readEntry(entryLength, kt_vno);
|
|
219 |
if (DEBUG) {
|
|
220 |
System.out.println(">>> KeyTab: load() entry length: " +
|
|
221 |
entryLength + "; type: " +
|
|
222 |
(entry != null? entry.keyType : 0));
|
|
223 |
}
|
|
224 |
if (entry != null)
|
|
225 |
entries.addElement(entry);
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Reads the service key from the keytab file.
|
|
231 |
* @param service the PrincipalName of the requested service.
|
|
232 |
* @return the last service key in the keytab
|
|
233 |
*/
|
|
234 |
public EncryptionKey readServiceKey(PrincipalName service) {
|
|
235 |
KeyTabEntry entry = null;
|
|
236 |
if (entries != null) {
|
|
237 |
// Find latest entry for this service that has an etype
|
|
238 |
// that has been configured for use
|
|
239 |
for (int i = entries.size()-1; i >= 0; i--) {
|
|
240 |
entry = entries.elementAt(i);
|
|
241 |
if (entry.service.match(service)) {
|
|
242 |
if (EType.isSupported(entry.keyType)) {
|
|
243 |
return new EncryptionKey(entry.keyblock,
|
|
244 |
entry.keyType,
|
|
245 |
new Integer(entry.keyVersion));
|
|
246 |
} else if (DEBUG) {
|
|
247 |
System.out.println("Found unsupported keytype (" +
|
|
248 |
entry.keyType + ") for " + service);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
return null;
|
|
254 |
}
|
|
255 |
|
|
256 |
/**
|
|
257 |
* Reads all keys for a service from the keytab file that have
|
|
258 |
* etypes that have been configured for use.
|
|
259 |
* @param service the PrincipalName of the requested service
|
|
260 |
* @return an array containing all the service keys
|
|
261 |
*/
|
|
262 |
public EncryptionKey[] readServiceKeys(PrincipalName service) {
|
|
263 |
KeyTabEntry entry;
|
|
264 |
EncryptionKey key;
|
|
265 |
int size = entries.size();
|
|
266 |
ArrayList<EncryptionKey> keys = new ArrayList<EncryptionKey> (size);
|
|
267 |
if (entries != null) {
|
|
268 |
for (int i = size-1; i >= 0; i--) {
|
|
269 |
entry = entries.elementAt(i);
|
|
270 |
if (entry.service.match(service)) {
|
|
271 |
if (EType.isSupported(entry.keyType)) {
|
|
272 |
key = new EncryptionKey(entry.keyblock,
|
|
273 |
entry.keyType,
|
|
274 |
new Integer(entry.keyVersion));
|
|
275 |
keys.add(key);
|
|
276 |
if (DEBUG) {
|
|
277 |
System.out.println("Added key: " + entry.keyType +
|
|
278 |
"version: " + entry.keyVersion);
|
|
279 |
}
|
|
280 |
} else if (DEBUG) {
|
|
281 |
System.out.println("Found unsupported keytype (" +
|
|
282 |
entry.keyType + ") for " + service);
|
|
283 |
}
|
|
284 |
}
|
|
285 |
}
|
|
286 |
}
|
|
287 |
|
|
288 |
size = keys.size();
|
|
289 |
if (size == 0)
|
|
290 |
return null;
|
|
291 |
EncryptionKey[] retVal = new EncryptionKey[size];
|
|
292 |
|
|
293 |
// Sort keys according to default_tkt_enctypes
|
|
294 |
int pos = 0;
|
|
295 |
EncryptionKey k;
|
|
296 |
if (DEBUG) {
|
|
297 |
System.out.println("Ordering keys wrt default_tkt_enctypes list");
|
|
298 |
}
|
|
299 |
int[] etypes = EType.getDefaults("default_tkt_enctypes");
|
|
300 |
if (etypes == null || etypes == EType.getBuiltInDefaults()) {
|
|
301 |
// Either no supported types specified in default_tkt_enctypes
|
|
302 |
// or no default_tkt_enctypes entry at all. For both cases,
|
|
303 |
// just return supported keys in the order retrieved
|
|
304 |
for (int i = 0; i < size; i++) {
|
|
305 |
retVal[pos++] = keys.get(i);
|
|
306 |
}
|
|
307 |
} else {
|
|
308 |
for (int j = 0; j < etypes.length && pos < size; j++) {
|
|
309 |
int target = etypes[j];
|
|
310 |
for (int i = 0; i < size && pos < size; i++) {
|
|
311 |
k = keys.get(i);
|
|
312 |
if (k != null && k.getEType() == target) {
|
|
313 |
if (DEBUG) {
|
|
314 |
System.out.println(pos + ": " + k);
|
|
315 |
}
|
|
316 |
retVal[pos++] = k;
|
|
317 |
keys.set(i, null); // Cleared from consideration
|
|
318 |
}
|
|
319 |
}
|
|
320 |
}
|
|
321 |
// copy the rest
|
|
322 |
for (int i = 0; i < size && pos < size; i++) {
|
|
323 |
k = keys.get(i);
|
|
324 |
if (k != null) {
|
|
325 |
retVal[pos++] = k;
|
|
326 |
}
|
|
327 |
}
|
|
328 |
}
|
|
329 |
if (pos != size) {
|
|
330 |
throw new RuntimeException(
|
|
331 |
"Internal Error: did not copy all keys;expecting " + size +
|
|
332 |
"; got " + pos);
|
|
333 |
}
|
|
334 |
return retVal;
|
|
335 |
}
|
|
336 |
|
|
337 |
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Searches for the service entry in the keytab file.
|
|
341 |
* The etype of the key must be one that has been configured
|
|
342 |
* to be used.
|
|
343 |
* @param service the PrincipalName of the requested service.
|
|
344 |
* @return true if the entry is found, otherwise, return false.
|
|
345 |
*/
|
|
346 |
public boolean findServiceEntry(PrincipalName service) {
|
|
347 |
KeyTabEntry entry;
|
|
348 |
if (entries != null) {
|
|
349 |
for (int i = 0; i < entries.size(); i++) {
|
|
350 |
entry = entries.elementAt(i);
|
|
351 |
if (entry.service.match(service)) {
|
|
352 |
if (EType.isSupported(entry.keyType)) {
|
|
353 |
return true;
|
|
354 |
} else if (DEBUG) {
|
|
355 |
System.out.println("Found unsupported keytype (" +
|
|
356 |
entry.keyType + ") for " + service);
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
}
|
|
361 |
return false;
|
|
362 |
}
|
|
363 |
|
|
364 |
public static String tabName() {
|
|
365 |
return name;
|
|
366 |
}
|
|
367 |
|
|
368 |
/**
|
|
369 |
* Adds a new entry in the key table.
|
|
370 |
* @param service the service which will have a new entry in the key table.
|
|
371 |
* @param psswd the password which generates the key.
|
|
372 |
*/
|
|
373 |
public void addEntry(PrincipalName service, char[] psswd)
|
|
374 |
throws KrbException {
|
|
375 |
|
|
376 |
EncryptionKey[] encKeys = EncryptionKey.acquireSecretKeys(
|
|
377 |
psswd, service.getSalt());
|
|
378 |
|
|
379 |
for (int i = 0; encKeys != null && i < encKeys.length; i++) {
|
|
380 |
int keyType = encKeys[i].getEType();
|
|
381 |
byte[] keyValue = encKeys[i].getBytes();
|
|
382 |
int result = retrieveEntry(service, keyType);
|
|
383 |
int kvno = 1;
|
|
384 |
if (result != -1) {
|
|
385 |
KeyTabEntry oldEntry = entries.elementAt(result);
|
|
386 |
kvno = oldEntry.keyVersion;
|
|
387 |
entries.removeElementAt(result);
|
|
388 |
kvno += 1;
|
|
389 |
} else
|
|
390 |
kvno = 1;
|
|
391 |
|
|
392 |
KeyTabEntry newEntry = new KeyTabEntry(service,
|
|
393 |
service.getRealm(),
|
|
394 |
new KerberosTime(System.currentTimeMillis()),
|
|
395 |
kvno, keyType, keyValue);
|
|
396 |
if (entries == null)
|
|
397 |
entries = new Vector<KeyTabEntry> ();
|
|
398 |
entries.addElement(newEntry);
|
|
399 |
}
|
|
400 |
}
|
|
401 |
|
|
402 |
|
|
403 |
/**
|
|
404 |
* Retrieves the key table entry with the specified service name.
|
|
405 |
* @param service the service which may have an entry in the key table.
|
|
406 |
* @return -1 if the entry is not found, else return the entry index
|
|
407 |
* in the list.
|
|
408 |
*/
|
|
409 |
private int retrieveEntry(PrincipalName service, int keyType) {
|
|
410 |
int found = -1;
|
|
411 |
KeyTabEntry e;
|
|
412 |
if (entries != null) {
|
|
413 |
for (int i = 0; i < entries.size(); i++) {
|
|
414 |
e = entries.elementAt(i);
|
|
415 |
if (service.match(e.getService()) &&
|
|
416 |
(keyType == -1 || e.keyType == keyType)) {
|
|
417 |
return i;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
}
|
|
421 |
return found;
|
|
422 |
}
|
|
423 |
|
|
424 |
/**
|
|
425 |
* Gets the list of service entries in key table.
|
|
426 |
* @return array of <code>KeyTabEntry</code>.
|
|
427 |
*/
|
|
428 |
public KeyTabEntry[] getEntries() {
|
|
429 |
if (entries != null) {
|
|
430 |
KeyTabEntry[] kentries = new KeyTabEntry[entries.size()];
|
|
431 |
for (int i = 0; i < kentries.length; i++) {
|
|
432 |
kentries[i] = entries.elementAt(i);
|
|
433 |
}
|
|
434 |
return kentries;
|
|
435 |
} else {
|
|
436 |
return null;
|
|
437 |
}
|
|
438 |
}
|
|
439 |
|
|
440 |
/**
|
|
441 |
* Creates a new default key table.
|
|
442 |
*/
|
|
443 |
public synchronized static KeyTab create()
|
|
444 |
throws IOException, RealmException {
|
|
445 |
String dname = getDefaultKeyTab();
|
|
446 |
return create(dname);
|
|
447 |
}
|
|
448 |
|
|
449 |
/**
|
|
450 |
* Creates a new default key table.
|
|
451 |
*/
|
|
452 |
public synchronized static KeyTab create(String name)
|
|
453 |
throws IOException, RealmException {
|
|
454 |
|
|
455 |
KeyTabOutputStream kos =
|
|
456 |
new KeyTabOutputStream(new FileOutputStream(name));
|
|
457 |
kos.writeVersion(KRB5_KT_VNO);
|
|
458 |
kos.close();
|
|
459 |
singleton = new KeyTab(name);
|
|
460 |
return singleton;
|
|
461 |
}
|
|
462 |
|
|
463 |
/**
|
|
464 |
* Saves the file at the directory.
|
|
465 |
*/
|
|
466 |
public synchronized void save() throws IOException {
|
|
467 |
KeyTabOutputStream kos =
|
|
468 |
new KeyTabOutputStream(new FileOutputStream(name));
|
|
469 |
kos.writeVersion(kt_vno);
|
|
470 |
for (int i = 0; i < entries.size(); i++) {
|
|
471 |
kos.writeEntry(entries.elementAt(i));
|
|
472 |
}
|
|
473 |
kos.close();
|
|
474 |
}
|
|
475 |
|
|
476 |
/**
|
|
477 |
* Removes an entry from the key table.
|
|
478 |
* @param service the service <code>PrincipalName</code>.
|
|
479 |
*/
|
|
480 |
public void deleteEntry(PrincipalName service) {
|
|
481 |
int result = retrieveEntry(service, -1);
|
|
482 |
if (result != -1) {
|
|
483 |
entries.removeElementAt(result);
|
|
484 |
}
|
|
485 |
}
|
|
486 |
|
|
487 |
/**
|
|
488 |
* Creates key table file version.
|
|
489 |
* @param file the key table file.
|
|
490 |
* @exception IOException.
|
|
491 |
*/
|
|
492 |
public synchronized void createVersion(File file) throws IOException {
|
|
493 |
KeyTabOutputStream kos =
|
|
494 |
new KeyTabOutputStream(new FileOutputStream(file));
|
|
495 |
kos.write16(KRB5_KT_VNO);
|
|
496 |
kos.close();
|
|
497 |
}
|
|
498 |
|
|
499 |
public static void refresh() {
|
|
500 |
if (singleton != null) {
|
|
501 |
if (DEBUG) {
|
|
502 |
System.out.println("Refreshing Keytab");
|
|
503 |
}
|
|
504 |
singleton = null;
|
|
505 |
}
|
|
506 |
}
|
|
507 |
}
|