author | weijun |
Sun, 01 May 2011 14:22:32 +0800 | |
changeset 9542 | 6f715a596a2e |
parent 9499 | f3115698a012 |
child 11911 | f960f3b33af3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7977
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, 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.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.ArrayList; |
|
38 |
import java.util.Arrays; |
|
39 |
import java.io.IOException; |
|
40 |
import java.io.FileInputStream; |
|
41 |
import java.io.FileOutputStream; |
|
42 |
import java.io.File; |
|
9499 | 43 |
import java.io.FileNotFoundException; |
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
44 |
import java.util.Comparator; |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
45 |
import java.util.HashMap; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
46 |
import java.util.Map; |
2 | 47 |
import java.util.StringTokenizer; |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
48 |
import java.util.Vector; |
2 | 49 |
|
50 |
/** |
|
51 |
* This class represents key table. The key table functions deal with storing |
|
52 |
* and retrieving service keys for use in authentication exchanges. |
|
53 |
* |
|
9499 | 54 |
* A KeyTab object is always constructed, if the file specified does not |
55 |
* exist, it's still valid but empty. If there is an I/O error or file format |
|
56 |
* error, it's invalid. |
|
57 |
* |
|
58 |
* The class is immutable on the read side (the write side is only used by |
|
59 |
* the ktab tool). |
|
60 |
* |
|
2 | 61 |
* @author Yanni Zhang |
62 |
*/ |
|
63 |
public class KeyTab implements KeyTabConstants { |
|
9499 | 64 |
|
2 | 65 |
private static final boolean DEBUG = Krb5.DEBUG; |
9499 | 66 |
private static String defaultTabName = null; |
67 |
||
68 |
// Attention: Currently there is no way to remove a keytab from this map, |
|
69 |
// this might lead to a memory leak. |
|
70 |
private static Map<String,KeyTab> map = new HashMap<>(); |
|
71 |
||
72 |
// KeyTab file does not exist. Note: a missing keytab is still valid |
|
73 |
private boolean isMissing = false; |
|
74 |
||
75 |
// KeyTab file is invalid, possibly an I/O error or a file format error. |
|
76 |
private boolean isValid = true; |
|
77 |
||
78 |
private final String tabName; |
|
79 |
private long lastModified; |
|
80 |
private int kt_vno; |
|
81 |
||
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7037
diff
changeset
|
82 |
private Vector<KeyTabEntry> entries = new Vector<>(); |
2 | 83 |
|
9499 | 84 |
/** |
85 |
* Constructs a KeyTab object. |
|
86 |
* |
|
87 |
* If there is any I/O error or format errot during the loading, the |
|
88 |
* isValid flag is set to false, and all half-read entries are dismissed. |
|
89 |
* @param filename path name for the keytab file, must not be null |
|
90 |
*/ |
|
91 |
private KeyTab(String filename) { |
|
92 |
tabName = filename; |
|
93 |
try { |
|
94 |
lastModified = new File(tabName).lastModified(); |
|
9542 | 95 |
try (KeyTabInputStream kis = |
96 |
new KeyTabInputStream(new FileInputStream(filename))) { |
|
97 |
load(kis); |
|
98 |
} |
|
9499 | 99 |
} catch (FileNotFoundException e) { |
100 |
entries.clear(); |
|
101 |
isMissing = true; |
|
102 |
} catch (Exception ioe) { |
|
103 |
entries.clear(); |
|
104 |
isValid = false; |
|
2 | 105 |
} |
106 |
} |
|
107 |
||
108 |
/** |
|
9499 | 109 |
* Read a keytab file. Returns a new object and save it into cache when |
110 |
* new content (modified since last read) is available. If keytab file is |
|
111 |
* invalid, the old object will be returned. This is a safeguard for |
|
112 |
* partial-written keytab files or non-stable network. Please note that |
|
113 |
* a missing keytab is valid, which is equivalent to an empty keytab. |
|
114 |
* |
|
115 |
* @param s file name of keytab, must not be null |
|
116 |
* @return the keytab object, can be invalid, but never null. |
|
2 | 117 |
*/ |
9499 | 118 |
private synchronized static KeyTab getInstance0(String s) { |
119 |
long lm = new File(s).lastModified(); |
|
120 |
KeyTab old = map.get(s); |
|
121 |
if (old != null && old.isValid() && old.lastModified == lm) { |
|
122 |
return old; |
|
2 | 123 |
} |
9499 | 124 |
KeyTab ktab = new KeyTab(s); |
125 |
if (ktab.isValid()) { // A valid new keytab |
|
126 |
map.put(s, ktab); |
|
127 |
return ktab; |
|
128 |
} else if (old != null) { // An existing old one |
|
129 |
return old; |
|
130 |
} else { |
|
131 |
return ktab; // first read is invalid |
|
132 |
} |
|
2 | 133 |
} |
134 |
||
135 |
/** |
|
9499 | 136 |
* Gets a KeyTab object. |
137 |
* @param s the key tab file name. |
|
138 |
* @return the KeyTab object, never null. |
|
139 |
*/ |
|
140 |
public static KeyTab getInstance(String s) { |
|
141 |
if (s == null) { |
|
142 |
return getInstance(); |
|
143 |
} else { |
|
144 |
return getInstance0(s); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Gets a KeyTab object. |
|
150 |
* @param file the key tab file. |
|
151 |
* @return the KeyTab object, never null. |
|
152 |
*/ |
|
153 |
public static KeyTab getInstance(File file) { |
|
154 |
if (file == null) { |
|
155 |
return getInstance(); |
|
156 |
} else { |
|
157 |
return getInstance0(file.getPath()); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Gets the default KeyTab object. |
|
163 |
* @return the KeyTab object, never null. |
|
2 | 164 |
*/ |
165 |
public static KeyTab getInstance() { |
|
9499 | 166 |
return getInstance(getDefaultTabName()); |
167 |
} |
|
168 |
||
169 |
public boolean isMissing() { |
|
170 |
return isMissing; |
|
171 |
} |
|
172 |
||
173 |
public boolean isValid() { |
|
174 |
return isValid; |
|
2 | 175 |
} |
176 |
||
177 |
/** |
|
178 |
* The location of keytab file will be read from the configuration file |
|
179 |
* If it is not specified, consider user.home as the keytab file's |
|
180 |
* default location. |
|
9499 | 181 |
* @return never null |
2 | 182 |
*/ |
9499 | 183 |
private static String getDefaultTabName() { |
184 |
if (defaultTabName != null) { |
|
185 |
return defaultTabName; |
|
2 | 186 |
} else { |
187 |
String kname = null; |
|
188 |
try { |
|
189 |
String keytab_names = Config.getInstance().getDefault |
|
190 |
("default_keytab_name", "libdefaults"); |
|
191 |
if (keytab_names != null) { |
|
192 |
StringTokenizer st = new StringTokenizer(keytab_names, " "); |
|
193 |
while (st.hasMoreTokens()) { |
|
194 |
kname = parse(st.nextToken()); |
|
9499 | 195 |
if (new File(kname).exists()) { |
2 | 196 |
break; |
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
} catch (KrbException e) { |
|
201 |
kname = null; |
|
202 |
} |
|
203 |
||
204 |
if (kname == null) { |
|
205 |
String user_home = |
|
206 |
java.security.AccessController.doPrivileged( |
|
207 |
new sun.security.action.GetPropertyAction("user.home")); |
|
208 |
||
209 |
if (user_home == null) { |
|
210 |
user_home = |
|
211 |
java.security.AccessController.doPrivileged( |
|
212 |
new sun.security.action.GetPropertyAction("user.dir")); |
|
213 |
} |
|
214 |
||
9499 | 215 |
kname = user_home + File.separator + "krb5.keytab"; |
2 | 216 |
} |
9499 | 217 |
defaultTabName = kname; |
2 | 218 |
return kname; |
219 |
} |
|
220 |
} |
|
221 |
||
9499 | 222 |
/** |
223 |
* Parses some common keytab name formats |
|
224 |
* @param name never null |
|
225 |
* @return never null |
|
226 |
*/ |
|
2 | 227 |
private static String parse(String name) { |
9499 | 228 |
String kname; |
2 | 229 |
if ((name.length() >= 5) && |
230 |
(name.substring(0, 5).equalsIgnoreCase("FILE:"))) { |
|
231 |
kname = name.substring(5); |
|
232 |
} else if ((name.length() >= 9) && |
|
233 |
(name.substring(0, 9).equalsIgnoreCase("ANY:FILE:"))) { |
|
234 |
// this format found in MIT's krb5.ini. |
|
235 |
kname = name.substring(9); |
|
236 |
} else if ((name.length() >= 7) && |
|
237 |
(name.substring(0, 7).equalsIgnoreCase("SRVTAB:"))) { |
|
238 |
// this format found in MIT's krb5.ini. |
|
239 |
kname = name.substring(7); |
|
240 |
} else |
|
241 |
kname = name; |
|
242 |
return kname; |
|
243 |
} |
|
244 |
||
245 |
private void load(KeyTabInputStream kis) |
|
246 |
throws IOException, RealmException { |
|
247 |
||
248 |
entries.clear(); |
|
249 |
kt_vno = kis.readVersion(); |
|
250 |
if (kt_vno == KRB5_KT_VNO_1) { |
|
251 |
kis.setNativeByteOrder(); |
|
252 |
} |
|
253 |
int entryLength = 0; |
|
254 |
KeyTabEntry entry; |
|
255 |
while (kis.available() > 0) { |
|
256 |
entryLength = kis.readEntryLength(); |
|
257 |
entry = kis.readEntry(entryLength, kt_vno); |
|
258 |
if (DEBUG) { |
|
259 |
System.out.println(">>> KeyTab: load() entry length: " + |
|
260 |
entryLength + "; type: " + |
|
261 |
(entry != null? entry.keyType : 0)); |
|
262 |
} |
|
263 |
if (entry != null) |
|
264 |
entries.addElement(entry); |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Reads all keys for a service from the keytab file that have |
|
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
270 |
* etypes that have been configured for use. If there are multiple |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
271 |
* keys with same etype, the one with the highest kvno is returned. |
2 | 272 |
* @param service the PrincipalName of the requested service |
9499 | 273 |
* @return an array containing all the service keys, never null |
2 | 274 |
*/ |
275 |
public EncryptionKey[] readServiceKeys(PrincipalName service) { |
|
276 |
KeyTabEntry entry; |
|
277 |
EncryptionKey key; |
|
278 |
int size = entries.size(); |
|
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7037
diff
changeset
|
279 |
ArrayList<EncryptionKey> keys = new ArrayList<>(size); |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
280 |
for (int i = size-1; i >= 0; i--) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
281 |
entry = entries.elementAt(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
282 |
if (entry.service.match(service)) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
283 |
if (EType.isSupported(entry.keyType)) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
284 |
key = new EncryptionKey(entry.keyblock, |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
285 |
entry.keyType, |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
286 |
new Integer(entry.keyVersion)); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
287 |
keys.add(key); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
288 |
if (DEBUG) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
289 |
System.out.println("Added key: " + entry.keyType + |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
290 |
"version: " + entry.keyVersion); |
2 | 291 |
} |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
292 |
} else if (DEBUG) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
293 |
System.out.println("Found unsupported keytype (" + |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
294 |
entry.keyType + ") for " + service); |
2 | 295 |
} |
296 |
} |
|
297 |
} |
|
298 |
size = keys.size(); |
|
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
299 |
EncryptionKey[] retVal = keys.toArray(new EncryptionKey[size]); |
2 | 300 |
|
301 |
// Sort keys according to default_tkt_enctypes |
|
302 |
if (DEBUG) { |
|
303 |
System.out.println("Ordering keys wrt default_tkt_enctypes list"); |
|
304 |
} |
|
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
305 |
|
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
306 |
final int[] etypes = EType.getDefaults("default_tkt_enctypes"); |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
307 |
|
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
308 |
// Sort the keys, k1 is preferred than k2 if: |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
309 |
// 1. k1's etype appears earlier in etypes than k2's |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
310 |
// 2. If same, k1's KVNO is higher |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
311 |
Arrays.sort(retVal, new Comparator<EncryptionKey>() { |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
312 |
@Override |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
313 |
public int compare(EncryptionKey o1, EncryptionKey o2) { |
5974
f0531b7dfebe
6844907: krb5 etype order should be from strong to weak
weijun
parents:
5506
diff
changeset
|
314 |
if (etypes != null) { |
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
315 |
int o1EType = o1.getEType(); |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
316 |
int o2EType = o2.getEType(); |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
317 |
if (o1EType != o2EType) { |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
318 |
for (int i=0; i<etypes.length; i++) { |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
319 |
if (etypes[i] == o1EType) { |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
320 |
return -1; |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
321 |
} else if (etypes[i] == o2EType) { |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
322 |
return 1; |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
323 |
} |
2 | 324 |
} |
5974
f0531b7dfebe
6844907: krb5 etype order should be from strong to weak
weijun
parents:
5506
diff
changeset
|
325 |
// Neither o1EType nor o2EType in default_tkt_enctypes, |
f0531b7dfebe
6844907: krb5 etype order should be from strong to weak
weijun
parents:
5506
diff
changeset
|
326 |
// therefore won't be used in AS-REQ. We do not care |
f0531b7dfebe
6844907: krb5 etype order should be from strong to weak
weijun
parents:
5506
diff
changeset
|
327 |
// about their order, use kvno is OK. |
2 | 328 |
} |
329 |
} |
|
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
330 |
return o2.getKeyVersionNumber().intValue() |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
331 |
- o1.getKeyVersionNumber().intValue(); |
2 | 332 |
} |
3626
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
333 |
}); |
78722c321f85
6867665: Problem with keytabs with multiple kvno's (key versions)
weijun
parents:
3317
diff
changeset
|
334 |
|
2 | 335 |
return retVal; |
336 |
} |
|
337 |
||
338 |
||
339 |
||
340 |
/** |
|
341 |
* Searches for the service entry in the keytab file. |
|
342 |
* The etype of the key must be one that has been configured |
|
343 |
* to be used. |
|
344 |
* @param service the PrincipalName of the requested service. |
|
345 |
* @return true if the entry is found, otherwise, return false. |
|
346 |
*/ |
|
347 |
public boolean findServiceEntry(PrincipalName service) { |
|
348 |
KeyTabEntry entry; |
|
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
349 |
for (int i = 0; i < entries.size(); i++) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
350 |
entry = entries.elementAt(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
351 |
if (entry.service.match(service)) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
352 |
if (EType.isSupported(entry.keyType)) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
353 |
return true; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
354 |
} else if (DEBUG) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
355 |
System.out.println("Found unsupported keytype (" + |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
356 |
entry.keyType + ") for " + service); |
2 | 357 |
} |
358 |
} |
|
359 |
} |
|
360 |
return false; |
|
361 |
} |
|
362 |
||
9499 | 363 |
public String tabName() { |
364 |
return tabName; |
|
2 | 365 |
} |
366 |
||
9499 | 367 |
/////////////////// THE WRITE SIDE /////////////////////// |
368 |
/////////////// only used by ktab tool ////////////////// |
|
369 |
||
2 | 370 |
/** |
371 |
* Adds a new entry in the key table. |
|
372 |
* @param service the service which will have a new entry in the key table. |
|
373 |
* @param psswd the password which generates the key. |
|
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
374 |
* @param kvno the kvno to use, -1 means automatic increasing |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
375 |
* @param append false if entries with old kvno would be removed. |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
376 |
* Note: if kvno is not -1, entries with the same kvno are always removed |
2 | 377 |
*/ |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
378 |
public void addEntry(PrincipalName service, char[] psswd, |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
379 |
int kvno, boolean append) throws KrbException { |
2 | 380 |
|
381 |
EncryptionKey[] encKeys = EncryptionKey.acquireSecretKeys( |
|
382 |
psswd, service.getSalt()); |
|
383 |
||
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
384 |
// There should be only one maximum KVNO value for all etypes, so that |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
385 |
// all added keys can have the same KVNO. |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
386 |
|
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
387 |
int maxKvno = 0; // only useful when kvno == -1 |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
388 |
for (int i = entries.size()-1; i >= 0; i--) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
389 |
KeyTabEntry e = entries.get(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
390 |
if (e.service.match(service)) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
391 |
if (e.keyVersion > maxKvno) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
392 |
maxKvno = e.keyVersion; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
393 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
394 |
if (!append || e.keyVersion == kvno) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
395 |
entries.removeElementAt(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
396 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
397 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
398 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
399 |
if (kvno == -1) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
400 |
kvno = maxKvno + 1; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
401 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
402 |
|
2 | 403 |
for (int i = 0; encKeys != null && i < encKeys.length; i++) { |
404 |
int keyType = encKeys[i].getEType(); |
|
405 |
byte[] keyValue = encKeys[i].getBytes(); |
|
406 |
||
407 |
KeyTabEntry newEntry = new KeyTabEntry(service, |
|
408 |
service.getRealm(), |
|
409 |
new KerberosTime(System.currentTimeMillis()), |
|
410 |
kvno, keyType, keyValue); |
|
411 |
entries.addElement(newEntry); |
|
412 |
} |
|
413 |
} |
|
414 |
||
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3626
diff
changeset
|
415 |
/** |
2 | 416 |
* Gets the list of service entries in key table. |
417 |
* @return array of <code>KeyTabEntry</code>. |
|
418 |
*/ |
|
419 |
public KeyTabEntry[] getEntries() { |
|
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
420 |
KeyTabEntry[] kentries = new KeyTabEntry[entries.size()]; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
421 |
for (int i = 0; i < kentries.length; i++) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
422 |
kentries[i] = entries.elementAt(i); |
2 | 423 |
} |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
424 |
return kentries; |
2 | 425 |
} |
426 |
||
427 |
/** |
|
428 |
* Creates a new default key table. |
|
429 |
*/ |
|
430 |
public synchronized static KeyTab create() |
|
431 |
throws IOException, RealmException { |
|
9499 | 432 |
String dname = getDefaultTabName(); |
2 | 433 |
return create(dname); |
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* Creates a new default key table. |
|
438 |
*/ |
|
439 |
public synchronized static KeyTab create(String name) |
|
440 |
throws IOException, RealmException { |
|
441 |
||
9542 | 442 |
try (KeyTabOutputStream kos = |
443 |
new KeyTabOutputStream(new FileOutputStream(name))) { |
|
444 |
kos.writeVersion(KRB5_KT_VNO); |
|
445 |
} |
|
9499 | 446 |
return new KeyTab(name); |
2 | 447 |
} |
448 |
||
449 |
/** |
|
450 |
* Saves the file at the directory. |
|
451 |
*/ |
|
452 |
public synchronized void save() throws IOException { |
|
9542 | 453 |
try (KeyTabOutputStream kos = |
454 |
new KeyTabOutputStream(new FileOutputStream(tabName))) { |
|
455 |
kos.writeVersion(kt_vno); |
|
456 |
for (int i = 0; i < entries.size(); i++) { |
|
457 |
kos.writeEntry(entries.elementAt(i)); |
|
458 |
} |
|
2 | 459 |
} |
460 |
} |
|
461 |
||
462 |
/** |
|
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
463 |
* Removes entries from the key table. |
2 | 464 |
* @param service the service <code>PrincipalName</code>. |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
465 |
* @param etype the etype to match, remove all if -1 |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
466 |
* @param kvno what kvno to remove, -1 for all, -2 for old |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
467 |
* @return the number of entries deleted |
2 | 468 |
*/ |
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
469 |
public int deleteEntries(PrincipalName service, int etype, int kvno) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
470 |
int count = 0; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
471 |
|
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
472 |
// Remember the highest KVNO for each etype. Used for kvno == -2 |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
473 |
Map<Integer,Integer> highest = new HashMap<>(); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
474 |
|
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
475 |
for (int i = entries.size()-1; i >= 0; i--) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
476 |
KeyTabEntry e = entries.get(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
477 |
if (service.match(e.getService())) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
478 |
if (etype == -1 || e.keyType == etype) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
479 |
if (kvno == -2) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
480 |
// Two rounds for kvno == -2. In the first round (here), |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
481 |
// only find out highest KVNO for each etype |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
482 |
if (highest.containsKey(e.keyType)) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
483 |
int n = highest.get(e.keyType); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
484 |
if (e.keyVersion > n) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
485 |
highest.put(e.keyType, e.keyVersion); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
486 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
487 |
} else { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
488 |
highest.put(e.keyType, e.keyVersion); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
489 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
490 |
} else if (kvno == -1 || e.keyVersion == kvno) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
491 |
entries.removeElementAt(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
492 |
count++; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
493 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
494 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
495 |
} |
2 | 496 |
} |
3317 | 497 |
|
7037
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
498 |
// Second round for kvno == -2, remove old entries |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
499 |
if (kvno == -2) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
500 |
for (int i = entries.size()-1; i >= 0; i--) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
501 |
KeyTabEntry e = entries.get(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
502 |
if (service.match(e.getService())) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
503 |
if (etype == -1 || e.keyType == etype) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
504 |
int n = highest.get(e.keyType); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
505 |
if (e.keyVersion != n) { |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
506 |
entries.removeElementAt(i); |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
507 |
count++; |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
508 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
509 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
510 |
} |
9fd3887c8010
6950546: "ktab -d name etype" to "ktab -d name [-e etype] [kvno | all | old]"
weijun
parents:
5974
diff
changeset
|
511 |
} |
3317 | 512 |
} |
513 |
return count; |
|
2 | 514 |
} |
515 |
||
516 |
/** |
|
517 |
* Creates key table file version. |
|
518 |
* @param file the key table file. |
|
519 |
* @exception IOException. |
|
520 |
*/ |
|
521 |
public synchronized void createVersion(File file) throws IOException { |
|
9542 | 522 |
try (KeyTabOutputStream kos = |
523 |
new KeyTabOutputStream(new FileOutputStream(file))) { |
|
524 |
kos.write16(KRB5_KT_VNO); |
|
525 |
} |
|
2 | 526 |
} |
527 |
} |