author | weijun |
Thu, 18 Mar 2010 18:26:37 +0800 | |
changeset 5154 | 07af3c279166 |
parent 4532 | f39917c8cf46 |
child 5506 | 202f599c92aa |
child 5617 | 1b0d8c3d6223 |
permissions | -rw-r--r-- |
1454 | 1 |
/* |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
2 |
* Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
1454 | 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.lang.reflect.Constructor; |
|
25 |
import java.lang.reflect.Field; |
|
26 |
import java.lang.reflect.InvocationTargetException; |
|
27 |
import java.net.*; |
|
28 |
import java.io.*; |
|
29 |
import java.lang.reflect.Method; |
|
30 |
import java.security.SecureRandom; |
|
31 |
import java.util.*; |
|
32 |
import java.util.concurrent.*; |
|
3046 | 33 |
import sun.net.spi.nameservice.NameService; |
34 |
import sun.net.spi.nameservice.NameServiceDescriptor; |
|
1454 | 35 |
import sun.security.krb5.*; |
36 |
import sun.security.krb5.internal.*; |
|
1575
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
37 |
import sun.security.krb5.internal.ccache.CredentialsCache; |
1454 | 38 |
import sun.security.krb5.internal.crypto.KeyUsage; |
39 |
import sun.security.krb5.internal.ktab.KeyTab; |
|
40 |
import sun.security.util.DerInputStream; |
|
41 |
import sun.security.util.DerOutputStream; |
|
42 |
import sun.security.util.DerValue; |
|
43 |
||
44 |
/** |
|
45 |
* A KDC server. |
|
46 |
* <p> |
|
47 |
* Features: |
|
48 |
* <ol> |
|
49 |
* <li> Supports TCP and UDP |
|
50 |
* <li> Supports AS-REQ and TGS-REQ |
|
51 |
* <li> Principal db and other settings hard coded in application |
|
52 |
* <li> Options, say, request preauth or not |
|
53 |
* </ol> |
|
54 |
* Side effects: |
|
55 |
* <ol> |
|
56 |
* <li> The Sun-internal class <code>sun.security.krb5.Config</code> is a |
|
57 |
* singleton and initialized according to Kerberos settings (krb5.conf and |
|
58 |
* java.security.krb5.* system properties). This means once it's initialized |
|
59 |
* it will not automatically notice any changes to these settings (or file |
|
60 |
* changes of krb5.conf). The KDC class normally does not touch these |
|
61 |
* settings (except for the <code>writeKtab()</code> method). However, to make |
|
62 |
* sure nothing ever goes wrong, if you want to make any changes to these |
|
63 |
* settings after calling a KDC method, call <code>Config.refresh()</code> to |
|
64 |
* make sure your changes are reflected in the <code>Config</code> object. |
|
65 |
* </ol> |
|
4336 | 66 |
* System properties recognized: |
67 |
* <ul> |
|
68 |
* <li>test.kdc.save.ccache |
|
69 |
* </ul> |
|
70 |
* Support policies: |
|
71 |
* <ul> |
|
72 |
* <li>ok-as-delegate |
|
73 |
* </ul> |
|
1454 | 74 |
* Issues and TODOs: |
75 |
* <ol> |
|
76 |
* <li> Generates krb5.conf to be used on another machine, currently the kdc is |
|
77 |
* always localhost |
|
78 |
* <li> More options to KDC, say, error output, say, response nonce != |
|
79 |
* request nonce |
|
80 |
* </ol> |
|
81 |
* Note: This program uses internal krb5 classes (including reflection to |
|
82 |
* access private fields and methods). |
|
83 |
* <p> |
|
84 |
* Usages: |
|
85 |
* <p> |
|
86 |
* 1. Init and start the KDC: |
|
87 |
* <pre> |
|
88 |
* KDC kdc = KDC.create("REALM.NAME", port, isDaemon); |
|
89 |
* KDC kdc = KDC.create("REALM.NAME"); |
|
90 |
* </pre> |
|
91 |
* Here, <code>port</code> is the UDP and TCP port number the KDC server |
|
92 |
* listens on. If zero, a random port is chosen, which you can use getPort() |
|
93 |
* later to retrieve the value. |
|
94 |
* <p> |
|
95 |
* If <code>isDaemon</code> is true, the KDC worker threads will be daemons. |
|
96 |
* <p> |
|
97 |
* The shortcut <code>KDC.create("REALM.NAME")</code> has port=0 and |
|
98 |
* isDaemon=false, and is commonly used in an embedded KDC. |
|
99 |
* <p> |
|
100 |
* 2. Adding users: |
|
101 |
* <pre> |
|
102 |
* kdc.addPrincipal(String principal_name, char[] password); |
|
103 |
* kdc.addPrincipalRandKey(String principal_name); |
|
104 |
* </pre> |
|
105 |
* A service principal's name should look like "host/f.q.d.n". The second form |
|
106 |
* generates a random key. To expose this key, call <code>writeKtab()</code> to |
|
107 |
* save the keys into a keytab file. |
|
108 |
* <p> |
|
109 |
* Note that you need to add the principal name krbtgt/REALM.NAME yourself. |
|
110 |
* <p> |
|
111 |
* Note that you can safely add a principal at any time after the KDC is |
|
112 |
* started and before a user requests info on this principal. |
|
113 |
* <p> |
|
114 |
* 3. Other public methods: |
|
115 |
* <ul> |
|
116 |
* <li> <code>getPort</code>: Returns the port number the KDC uses |
|
117 |
* <li> <code>getRealm</code>: Returns the realm name |
|
118 |
* <li> <code>writeKtab</code>: Writes all principals' keys into a keytab file |
|
119 |
* <li> <code>saveConfig</code>: Saves a krb5.conf file to access this KDC |
|
120 |
* <li> <code>setOption</code>: Sets various options |
|
121 |
* </ul> |
|
122 |
* Read the javadoc for details. Lazy developer can use <code>OneKDC</code> |
|
123 |
* directly. |
|
124 |
*/ |
|
125 |
public class KDC { |
|
126 |
||
127 |
// Under the hood. |
|
128 |
||
129 |
// The random generator to generate random keys (including session keys) |
|
130 |
private static SecureRandom secureRandom = new SecureRandom(); |
|
3046 | 131 |
// Principal db. principal -> pass |
1454 | 132 |
private Map<String,char[]> passwords = new HashMap<String,char[]>(); |
133 |
// Realm name |
|
134 |
private String realm; |
|
3046 | 135 |
// KDC |
136 |
private String kdc; |
|
137 |
// Service port number |
|
138 |
private int port; |
|
1454 | 139 |
// The request/response job queue |
140 |
private BlockingQueue<Job> q = new ArrayBlockingQueue<Job>(100); |
|
141 |
// Options |
|
142 |
private Map<Option,Object> options = new HashMap<Option,Object>(); |
|
143 |
||
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
144 |
private Thread thread1, thread2, thread3; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
145 |
DatagramSocket u1 = null; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
146 |
ServerSocket t1 = null; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
147 |
|
1454 | 148 |
/** |
149 |
* Option names, to be expanded forever. |
|
150 |
*/ |
|
151 |
public static enum Option { |
|
152 |
/** |
|
153 |
* Whether pre-authentication is required. Default Boolean.TRUE |
|
154 |
*/ |
|
155 |
PREAUTH_REQUIRED, |
|
156 |
}; |
|
157 |
||
3046 | 158 |
static { |
159 |
System.setProperty("sun.net.spi.nameservice.provider.1", "ns,mock"); |
|
160 |
} |
|
161 |
||
1454 | 162 |
/** |
163 |
* A standalone KDC server. |
|
164 |
*/ |
|
165 |
public static void main(String[] args) throws Exception { |
|
4336 | 166 |
KDC kdc = create("RABBIT.HOLE", "kdc.rabbit.hole", 0, false); |
1454 | 167 |
kdc.addPrincipal("dummy", "bogus".toCharArray()); |
168 |
kdc.addPrincipal("foo", "bar".toCharArray()); |
|
3046 | 169 |
kdc.addPrincipalRandKey("krbtgt/RABBIT.HOLE"); |
170 |
kdc.addPrincipalRandKey("server/host.rabbit.hole"); |
|
171 |
kdc.addPrincipalRandKey("backend/host.rabbit.hole"); |
|
172 |
KDC.saveConfig("krb5.conf", kdc, "forwardable = true"); |
|
1454 | 173 |
} |
174 |
||
175 |
/** |
|
176 |
* Creates and starts a KDC running as a daemon on a random port. |
|
177 |
* @param realm the realm name |
|
178 |
* @return the running KDC instance |
|
179 |
* @throws java.io.IOException for any socket creation error |
|
180 |
*/ |
|
181 |
public static KDC create(String realm) throws IOException { |
|
3046 | 182 |
return create(realm, "kdc." + realm.toLowerCase(), 0, true); |
1454 | 183 |
} |
184 |
||
185 |
/** |
|
186 |
* Creates and starts a KDC server. |
|
187 |
* @param realm the realm name |
|
188 |
* @param port the TCP and UDP port to listen to. A random port will to |
|
189 |
* chosen if zero. |
|
190 |
* @param asDaemon if true, KDC threads will be daemons. Otherwise, not. |
|
191 |
* @return the running KDC instance |
|
192 |
* @throws java.io.IOException for any socket creation error |
|
193 |
*/ |
|
3046 | 194 |
public static KDC create(String realm, String kdc, int port, boolean asDaemon) throws IOException { |
195 |
return new KDC(realm, kdc, port, asDaemon); |
|
1454 | 196 |
} |
197 |
||
198 |
/** |
|
199 |
* Sets an option |
|
200 |
* @param key the option name |
|
201 |
* @param obj the value |
|
202 |
*/ |
|
203 |
public void setOption(Option key, Object value) { |
|
204 |
options.put(key, value); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
208 |
* Write all principals' keys from multiple KDCsinto one keytab file. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
209 |
* Note that the keys for the krbtgt principals will not be written. |
1454 | 210 |
* <p> |
211 |
* Attention: This method references krb5.conf settings. If you need to |
|
212 |
* setup krb5.conf later, please call <code>Config.refresh()</code> after |
|
213 |
* the new setting. For example: |
|
214 |
* <pre> |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
215 |
* KDC.writeKtab("/etc/kdc/ktab", kdc); // Config is initialized, |
1454 | 216 |
* System.setProperty("java.security.krb5.conf", "/home/mykrb5.conf"); |
217 |
* Config.refresh(); |
|
218 |
* </pre> |
|
219 |
* |
|
220 |
* Inside this method there are 2 places krb5.conf is used: |
|
221 |
* <ol> |
|
222 |
* <li> (Fatal) Generating keys: EncryptionKey.acquireSecretKeys |
|
223 |
* <li> (Has workaround) Creating PrincipalName |
|
224 |
* </ol> |
|
225 |
* @param tab The keytab filename to write to. |
|
226 |
* @throws java.io.IOException for any file output error |
|
227 |
* @throws sun.security.krb5.KrbException for any realm and/or principal |
|
228 |
* name error. |
|
229 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
230 |
public static void writeMultiKtab(String tab, KDC... kdcs) |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
231 |
throws IOException, KrbException { |
1454 | 232 |
KeyTab ktab = KeyTab.create(tab); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
233 |
for (KDC kdc: kdcs) { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
234 |
for (String name : kdc.passwords.keySet()) { |
3046 | 235 |
ktab.addEntry(new PrincipalName(name, |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
236 |
name.indexOf('/') < 0 ? |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
237 |
PrincipalName.KRB_NT_UNKNOWN : |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
238 |
PrincipalName.KRB_NT_SRV_HST), |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
239 |
kdc.passwords.get(name)); |
1454 | 240 |
} |
241 |
} |
|
242 |
ktab.save(); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
246 |
* Write a ktab for this KDC. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
247 |
*/ |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
248 |
public void writeKtab(String tab) throws IOException, KrbException { |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
249 |
KDC.writeMultiKtab(tab, this); |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
250 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
251 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
252 |
/** |
1454 | 253 |
* Adds a new principal to this realm with a given password. |
254 |
* @param user the principal's name. For a service principal, use the |
|
255 |
* form of host/f.q.d.n |
|
256 |
* @param pass the password for the principal |
|
257 |
*/ |
|
258 |
public void addPrincipal(String user, char[] pass) { |
|
3046 | 259 |
if (user.indexOf('@') < 0) { |
260 |
user = user + "@" + realm; |
|
261 |
} |
|
1454 | 262 |
passwords.put(user, pass); |
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Adds a new principal to this realm with a random password |
|
267 |
* @param user the principal's name. For a service principal, use the |
|
268 |
* form of host/f.q.d.n |
|
269 |
*/ |
|
270 |
public void addPrincipalRandKey(String user) { |
|
3046 | 271 |
addPrincipal(user, randomPassword()); |
1454 | 272 |
} |
273 |
||
274 |
/** |
|
275 |
* Returns the name of this realm |
|
276 |
* @return the name of this realm |
|
277 |
*/ |
|
278 |
public String getRealm() { |
|
279 |
return realm; |
|
280 |
} |
|
281 |
||
282 |
/** |
|
3046 | 283 |
* Returns the name of kdc |
284 |
* @return the name of kdc |
|
285 |
*/ |
|
286 |
public String getKDC() { |
|
287 |
return kdc; |
|
288 |
} |
|
289 |
||
290 |
/** |
|
1454 | 291 |
* Writes a krb5.conf for one or more KDC that includes KDC locations for |
292 |
* each realm and the default realm name. You can also add extra strings |
|
293 |
* into the file. The method should be called like: |
|
294 |
* <pre> |
|
295 |
* KDC.saveConfig("krb5.conf", kdc1, kdc2, ..., line1, line2, ...); |
|
296 |
* </pre> |
|
297 |
* Here you can provide one or more kdc# and zero or more line# arguments. |
|
298 |
* The line# will be put after [libdefaults] and before [realms]. Therefore |
|
299 |
* you can append new lines into [libdefaults] and/or create your new |
|
300 |
* stanzas as well. Note that a newline character will be appended to |
|
301 |
* each line# argument. |
|
302 |
* <p> |
|
303 |
* For example: |
|
304 |
* <pre> |
|
305 |
* KDC.saveConfig("krb5.conf", this); |
|
306 |
* </pre> |
|
307 |
* generates: |
|
308 |
* <pre> |
|
309 |
* [libdefaults] |
|
310 |
* default_realm = REALM.NAME |
|
311 |
* |
|
312 |
* [realms] |
|
313 |
* REALM.NAME = { |
|
3046 | 314 |
* kdc = host:port_number |
1454 | 315 |
* } |
316 |
* </pre> |
|
317 |
* |
|
318 |
* Another example: |
|
319 |
* <pre> |
|
320 |
* KDC.saveConfig("krb5.conf", kdc1, kdc2, "forwardable = true", "", |
|
321 |
* "[domain_realm]", |
|
322 |
* ".kdc1.com = KDC1.NAME"); |
|
323 |
* </pre> |
|
324 |
* generates: |
|
325 |
* <pre> |
|
326 |
* [libdefaults] |
|
327 |
* default_realm = KDC1.NAME |
|
328 |
* forwardable = true |
|
329 |
* |
|
330 |
* [domain_realm] |
|
331 |
* .kdc1.com = KDC1.NAME |
|
332 |
* |
|
333 |
* [realms] |
|
334 |
* KDC1.NAME = { |
|
3046 | 335 |
* kdc = host:port1 |
1454 | 336 |
* } |
337 |
* KDC2.NAME = { |
|
3046 | 338 |
* kdc = host:port2 |
1454 | 339 |
* } |
340 |
* </pre> |
|
341 |
* @param file the name of the file to write into |
|
342 |
* @param kdc the first (and default) KDC |
|
343 |
* @param more more KDCs or extra lines (in their appearing order) to |
|
344 |
* insert into the krb5.conf file. This method reads each argument's type |
|
345 |
* to determine what it's for. This argument can be empty. |
|
346 |
* @throws java.io.IOException for any file output error |
|
347 |
*/ |
|
348 |
public static void saveConfig(String file, KDC kdc, Object... more) |
|
349 |
throws IOException { |
|
350 |
File f = new File(file); |
|
351 |
StringBuffer sb = new StringBuffer(); |
|
352 |
sb.append("[libdefaults]\ndefault_realm = "); |
|
353 |
sb.append(kdc.realm); |
|
354 |
sb.append("\n"); |
|
355 |
for (Object o: more) { |
|
356 |
if (o instanceof String) { |
|
357 |
sb.append(o); |
|
358 |
sb.append("\n"); |
|
359 |
} |
|
360 |
} |
|
361 |
sb.append("\n[realms]\n"); |
|
362 |
sb.append(realmLineForKDC(kdc)); |
|
363 |
for (Object o: more) { |
|
364 |
if (o instanceof KDC) { |
|
365 |
sb.append(realmLineForKDC((KDC)o)); |
|
366 |
} |
|
367 |
} |
|
368 |
FileOutputStream fos = new FileOutputStream(f); |
|
369 |
fos.write(sb.toString().getBytes()); |
|
370 |
fos.close(); |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Returns the service port of the KDC server. |
|
375 |
* @return the KDC service port |
|
376 |
*/ |
|
377 |
public int getPort() { |
|
378 |
return port; |
|
379 |
} |
|
380 |
||
381 |
// Private helper methods |
|
382 |
||
383 |
/** |
|
384 |
* Private constructor, cannot be called outside. |
|
385 |
* @param realm |
|
386 |
*/ |
|
3046 | 387 |
private KDC(String realm, String kdc) { |
1454 | 388 |
this.realm = realm; |
3046 | 389 |
this.kdc = kdc; |
1454 | 390 |
} |
391 |
||
392 |
/** |
|
393 |
* A constructor that starts the KDC service also. |
|
394 |
*/ |
|
3046 | 395 |
protected KDC(String realm, String kdc, int port, boolean asDaemon) |
1454 | 396 |
throws IOException { |
3046 | 397 |
this(realm, kdc); |
1454 | 398 |
startServer(port, asDaemon); |
399 |
} |
|
400 |
/** |
|
401 |
* Generates a 32-char random password |
|
402 |
* @return the password |
|
403 |
*/ |
|
404 |
private static char[] randomPassword() { |
|
405 |
char[] pass = new char[32]; |
|
406 |
for (int i=0; i<32; i++) |
|
407 |
pass[i] = (char)secureRandom.nextInt(); |
|
408 |
return pass; |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* Generates a random key for the given encryption type. |
|
413 |
* @param eType the encryption type |
|
414 |
* @return the generated key |
|
415 |
* @throws sun.security.krb5.KrbException for unknown/unsupported etype |
|
416 |
*/ |
|
417 |
private static EncryptionKey generateRandomKey(int eType) |
|
418 |
throws KrbException { |
|
419 |
// Is 32 enough for AES256? I should have generated the keys directly |
|
420 |
// but different cryptos have different rules on what keys are valid. |
|
421 |
char[] pass = randomPassword(); |
|
422 |
String algo; |
|
423 |
switch (eType) { |
|
424 |
case EncryptedData.ETYPE_DES_CBC_MD5: algo = "DES"; break; |
|
425 |
case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: algo = "DESede"; break; |
|
426 |
case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: algo = "AES128"; break; |
|
427 |
case EncryptedData.ETYPE_ARCFOUR_HMAC: algo = "ArcFourHMAC"; break; |
|
428 |
case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: algo = "AES256"; break; |
|
429 |
default: algo = "DES"; break; |
|
430 |
} |
|
431 |
return new EncryptionKey(pass, "NOTHING", algo); // Silly |
|
432 |
} |
|
433 |
||
434 |
/** |
|
435 |
* Returns the password for a given principal |
|
436 |
* @param p principal |
|
437 |
* @return the password |
|
438 |
* @throws sun.security.krb5.KrbException when the principal is not inside |
|
439 |
* the database. |
|
440 |
*/ |
|
4336 | 441 |
private char[] getPassword(PrincipalName p, boolean server) |
442 |
throws KrbException { |
|
3046 | 443 |
String pn = p.toString(); |
444 |
if (p.getRealmString() == null) { |
|
445 |
pn = pn + "@" + getRealm(); |
|
446 |
} |
|
447 |
char[] pass = passwords.get(pn); |
|
1454 | 448 |
if (pass == null) { |
4336 | 449 |
throw new KrbException(server? |
450 |
Krb5.KDC_ERR_S_PRINCIPAL_UNKNOWN: |
|
451 |
Krb5.KDC_ERR_C_PRINCIPAL_UNKNOWN); |
|
1454 | 452 |
} |
453 |
return pass; |
|
454 |
} |
|
455 |
||
456 |
/** |
|
3046 | 457 |
* Returns the salt string for the principal. |
1454 | 458 |
* @param p principal |
459 |
* @return the salt |
|
460 |
*/ |
|
461 |
private String getSalt(PrincipalName p) { |
|
462 |
String[] ns = p.getNameStrings(); |
|
3046 | 463 |
String s = p.getRealmString(); |
464 |
if (s == null) s = getRealm(); |
|
465 |
for (String n: p.getNameStrings()) { |
|
466 |
s += n; |
|
1454 | 467 |
} |
3046 | 468 |
return s; |
1454 | 469 |
} |
470 |
||
471 |
/** |
|
472 |
* Returns the key for a given principal of the given encryption type |
|
473 |
* @param p the principal |
|
474 |
* @param etype the encryption type |
|
4336 | 475 |
* @param server looking for a server principal? |
1454 | 476 |
* @return the key |
477 |
* @throws sun.security.krb5.KrbException for unknown/unsupported etype |
|
478 |
*/ |
|
4336 | 479 |
private EncryptionKey keyForUser(PrincipalName p, int etype, boolean server) |
480 |
throws KrbException { |
|
1454 | 481 |
try { |
482 |
// Do not call EncryptionKey.acquireSecretKeys(), otherwise |
|
483 |
// the krb5.conf config file would be loaded. |
|
484 |
Method stringToKey = EncryptionKey.class.getDeclaredMethod("stringToKey", char[].class, String.class, byte[].class, Integer.TYPE); |
|
485 |
stringToKey.setAccessible(true); |
|
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
486 |
Integer kvno = null; |
4532 | 487 |
// For service whose password ending with a number, use it as kvno. |
488 |
// Kvno must be postive. |
|
489 |
if (p.toString().indexOf('/') > 0) { |
|
4336 | 490 |
char[] pass = getPassword(p, server); |
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
491 |
if (Character.isDigit(pass[pass.length-1])) { |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
492 |
kvno = pass[pass.length-1] - '0'; |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
493 |
} |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
494 |
} |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
495 |
return new EncryptionKey((byte[]) stringToKey.invoke( |
4336 | 496 |
null, getPassword(p, server), getSalt(p), null, etype), |
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
497 |
etype, kvno); |
1454 | 498 |
} catch (InvocationTargetException ex) { |
499 |
KrbException ke = (KrbException)ex.getCause(); |
|
500 |
throw ke; |
|
4336 | 501 |
} catch (KrbException ke) { |
502 |
throw ke; |
|
1454 | 503 |
} catch (Exception e) { |
504 |
throw new RuntimeException(e); // should not happen |
|
505 |
} |
|
506 |
} |
|
507 |
||
4336 | 508 |
private Map<String,String> policies = new HashMap<String,String>(); |
509 |
||
510 |
public void setPolicy(String rule, String value) { |
|
511 |
if (value == null) { |
|
512 |
policies.remove(rule); |
|
513 |
} else { |
|
514 |
policies.put(rule, value); |
|
515 |
} |
|
516 |
} |
|
517 |
/** |
|
518 |
* If the provided client/server pair matches a rule |
|
519 |
* |
|
520 |
* A system property named test.kdc.policy.RULE will be consulted. |
|
521 |
* If it's unset, returns false. If its value is "", any pair is |
|
522 |
* matched. Otherwise, it should contains the server name matched. |
|
523 |
* |
|
524 |
* TODO: client name is not used currently. |
|
525 |
* |
|
526 |
* @param c client name |
|
527 |
* @param s server name |
|
528 |
* @param rule rule name |
|
529 |
* @return if a match is found |
|
530 |
*/ |
|
531 |
private boolean configMatch(String c, String s, String rule) { |
|
532 |
String policy = policies.get(rule); |
|
533 |
boolean result = false; |
|
534 |
if (policy == null) { |
|
535 |
result = false; |
|
536 |
} else if (policy.length() == 0) { |
|
537 |
result = true; |
|
538 |
} else { |
|
539 |
String[] names = policy.split("\\s+"); |
|
540 |
for (String name: names) { |
|
541 |
if (name.equals(s)) { |
|
542 |
result = true; |
|
543 |
break; |
|
544 |
} |
|
545 |
} |
|
546 |
} |
|
547 |
if (result) { |
|
548 |
System.out.printf(">>>> Policy match result (%s vs %s on %s) %b\n", |
|
549 |
c, s, rule, result); |
|
550 |
} |
|
551 |
return result; |
|
552 |
} |
|
553 |
||
554 |
||
1454 | 555 |
/** |
556 |
* Processes an incoming request and generates a response. |
|
557 |
* @param in the request |
|
558 |
* @return the response |
|
559 |
* @throws java.lang.Exception for various errors |
|
560 |
*/ |
|
561 |
private byte[] processMessage(byte[] in) throws Exception { |
|
562 |
if ((in[0] & 0x1f) == Krb5.KRB_AS_REQ) |
|
563 |
return processAsReq(in); |
|
564 |
else |
|
565 |
return processTgsReq(in); |
|
566 |
} |
|
567 |
||
568 |
/** |
|
569 |
* Processes a TGS_REQ and generates a TGS_REP (or KRB_ERROR) |
|
570 |
* @param in the request |
|
571 |
* @return the response |
|
572 |
* @throws java.lang.Exception for various errors |
|
573 |
*/ |
|
574 |
private byte[] processTgsReq(byte[] in) throws Exception { |
|
575 |
TGSReq tgsReq = new TGSReq(in); |
|
576 |
try { |
|
577 |
System.out.println(realm + "> " + tgsReq.reqBody.cname + |
|
578 |
" sends TGS-REQ for " + |
|
579 |
tgsReq.reqBody.sname); |
|
580 |
KDCReqBody body = tgsReq.reqBody; |
|
581 |
int etype = 0; |
|
582 |
||
583 |
// Reflection: PAData[] pas = tgsReq.pAData; |
|
584 |
Field f = KDCReq.class.getDeclaredField("pAData"); |
|
585 |
f.setAccessible(true); |
|
586 |
PAData[] pas = (PAData[])f.get(tgsReq); |
|
587 |
||
588 |
Ticket tkt = null; |
|
589 |
EncTicketPart etp = null; |
|
590 |
if (pas == null || pas.length == 0) { |
|
591 |
throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP); |
|
592 |
} else { |
|
593 |
for (PAData pa: pas) { |
|
594 |
if (pa.getType() == Krb5.PA_TGS_REQ) { |
|
595 |
APReq apReq = new APReq(pa.getValue()); |
|
596 |
EncryptedData ed = apReq.authenticator; |
|
597 |
tkt = apReq.ticket; |
|
598 |
etype = tkt.encPart.getEType(); |
|
3046 | 599 |
tkt.sname.setRealm(tkt.realm); |
4336 | 600 |
EncryptionKey kkey = keyForUser(tkt.sname, etype, true); |
1454 | 601 |
byte[] bb = tkt.encPart.decrypt(kkey, KeyUsage.KU_TICKET); |
602 |
DerInputStream derIn = new DerInputStream(bb); |
|
603 |
DerValue der = derIn.getDerValue(); |
|
604 |
etp = new EncTicketPart(der.toByteArray()); |
|
605 |
} |
|
606 |
} |
|
607 |
if (tkt == null) { |
|
608 |
throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP); |
|
609 |
} |
|
610 |
} |
|
4336 | 611 |
EncryptionKey skey = keyForUser(body.sname, etype, true); |
1454 | 612 |
if (skey == null) { |
613 |
throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO |
|
614 |
} |
|
615 |
||
616 |
// Session key for original ticket, TGT |
|
617 |
EncryptionKey ckey = etp.key; |
|
618 |
||
619 |
// Session key for session with the service |
|
620 |
EncryptionKey key = generateRandomKey(etype); |
|
621 |
||
622 |
// Check time, TODO |
|
623 |
KerberosTime till = body.till; |
|
624 |
if (till == null) { |
|
625 |
throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO |
|
626 |
} else if (till.isZero()) { |
|
627 |
till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11); |
|
628 |
} |
|
629 |
||
630 |
boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1]; |
|
631 |
if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) { |
|
632 |
bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true; |
|
633 |
} |
|
634 |
if (body.kdcOptions.get(KDCOptions.FORWARDED) || |
|
635 |
etp.flags.get(Krb5.TKT_OPTS_FORWARDED)) { |
|
636 |
bFlags[Krb5.TKT_OPTS_FORWARDED] = true; |
|
637 |
} |
|
638 |
if (body.kdcOptions.get(KDCOptions.RENEWABLE)) { |
|
639 |
bFlags[Krb5.TKT_OPTS_RENEWABLE] = true; |
|
640 |
//renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7); |
|
641 |
} |
|
642 |
if (body.kdcOptions.get(KDCOptions.PROXIABLE)) { |
|
643 |
bFlags[Krb5.TKT_OPTS_PROXIABLE] = true; |
|
644 |
} |
|
645 |
if (body.kdcOptions.get(KDCOptions.POSTDATED)) { |
|
646 |
bFlags[Krb5.TKT_OPTS_POSTDATED] = true; |
|
647 |
} |
|
648 |
if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) { |
|
649 |
bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true; |
|
650 |
} |
|
4336 | 651 |
|
652 |
if (configMatch("", body.sname.getNameString(), "ok-as-delegate")) { |
|
653 |
bFlags[Krb5.TKT_OPTS_DELEGATE] = true; |
|
654 |
} |
|
1454 | 655 |
bFlags[Krb5.TKT_OPTS_INITIAL] = true; |
656 |
||
657 |
TicketFlags tFlags = new TicketFlags(bFlags); |
|
658 |
EncTicketPart enc = new EncTicketPart( |
|
659 |
tFlags, |
|
660 |
key, |
|
661 |
etp.crealm, |
|
662 |
etp.cname, |
|
663 |
new TransitedEncoding(1, new byte[0]), // TODO |
|
664 |
new KerberosTime(new Date()), |
|
665 |
body.from, |
|
666 |
till, body.rtime, |
|
667 |
body.addresses, |
|
668 |
null); |
|
669 |
Ticket t = new Ticket( |
|
670 |
body.crealm, |
|
671 |
body.sname, |
|
672 |
new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET) |
|
673 |
); |
|
674 |
EncTGSRepPart enc_part = new EncTGSRepPart( |
|
675 |
key, |
|
676 |
new LastReq(new LastReqEntry[]{ |
|
677 |
new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000)) |
|
678 |
}), |
|
679 |
body.getNonce(), // TODO: detect replay |
|
680 |
new KerberosTime(new Date().getTime() + 1000 * 3600 * 24), |
|
681 |
// Next 5 and last MUST be same with ticket |
|
682 |
tFlags, |
|
683 |
new KerberosTime(new Date()), |
|
684 |
body.from, |
|
685 |
till, body.rtime, |
|
686 |
body.crealm, |
|
687 |
body.sname, |
|
688 |
body.addresses |
|
689 |
); |
|
690 |
EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_TGS_REP_PART_SESSKEY); |
|
691 |
TGSRep tgsRep = new TGSRep(null, |
|
692 |
etp.crealm, |
|
693 |
etp.cname, |
|
694 |
t, |
|
695 |
edata); |
|
696 |
System.out.println(" Return " + tgsRep.cname |
|
697 |
+ " ticket for " + tgsRep.ticket.sname); |
|
698 |
||
699 |
DerOutputStream out = new DerOutputStream(); |
|
700 |
out.write(DerValue.createTag(DerValue.TAG_APPLICATION, |
|
701 |
true, (byte)Krb5.KRB_TGS_REP), tgsRep.asn1Encode()); |
|
702 |
return out.toByteArray(); |
|
703 |
} catch (KrbException ke) { |
|
704 |
ke.printStackTrace(System.out); |
|
705 |
KRBError kerr = ke.getError(); |
|
706 |
KDCReqBody body = tgsReq.reqBody; |
|
707 |
System.out.println(" Error " + ke.returnCode() |
|
708 |
+ " " +ke.returnCodeMessage()); |
|
709 |
if (kerr == null) { |
|
710 |
kerr = new KRBError(null, null, null, |
|
711 |
new KerberosTime(new Date()), |
|
712 |
0, |
|
713 |
ke.returnCode(), |
|
714 |
body.crealm, body.cname, |
|
715 |
new Realm(getRealm()), body.sname, |
|
716 |
KrbException.errorMessage(ke.returnCode()), |
|
717 |
null); |
|
718 |
} |
|
719 |
return kerr.asn1Encode(); |
|
720 |
} |
|
721 |
} |
|
722 |
||
723 |
/** |
|
724 |
* Processes a AS_REQ and generates a AS_REP (or KRB_ERROR) |
|
725 |
* @param in the request |
|
726 |
* @return the response |
|
727 |
* @throws java.lang.Exception for various errors |
|
728 |
*/ |
|
729 |
private byte[] processAsReq(byte[] in) throws Exception { |
|
730 |
ASReq asReq = new ASReq(in); |
|
731 |
int[] eTypes = null; |
|
732 |
try { |
|
733 |
System.out.println(realm + "> " + asReq.reqBody.cname + |
|
734 |
" sends AS-REQ for " + |
|
735 |
asReq.reqBody.sname); |
|
736 |
||
737 |
KDCReqBody body = asReq.reqBody; |
|
738 |
||
739 |
// Reflection: int[] eType = body.eType; |
|
740 |
Field f = KDCReqBody.class.getDeclaredField("eType"); |
|
741 |
f.setAccessible(true); |
|
742 |
eTypes = (int[])f.get(body); |
|
743 |
int eType = eTypes[0]; |
|
744 |
||
4336 | 745 |
EncryptionKey ckey = keyForUser(body.cname, eType, false); |
746 |
EncryptionKey skey = keyForUser(body.sname, eType, true); |
|
1454 | 747 |
if (ckey == null) { |
748 |
throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP); |
|
749 |
} |
|
750 |
if (skey == null) { |
|
751 |
throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO |
|
752 |
} |
|
753 |
||
754 |
// Session key |
|
755 |
EncryptionKey key = generateRandomKey(eType); |
|
756 |
// Check time, TODO |
|
757 |
KerberosTime till = body.till; |
|
758 |
if (till == null) { |
|
759 |
throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO |
|
760 |
} else if (till.isZero()) { |
|
761 |
till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11); |
|
762 |
} |
|
763 |
//body.from |
|
764 |
boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1]; |
|
765 |
if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) { |
|
766 |
bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true; |
|
767 |
} |
|
768 |
if (body.kdcOptions.get(KDCOptions.RENEWABLE)) { |
|
769 |
bFlags[Krb5.TKT_OPTS_RENEWABLE] = true; |
|
770 |
//renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7); |
|
771 |
} |
|
772 |
if (body.kdcOptions.get(KDCOptions.PROXIABLE)) { |
|
773 |
bFlags[Krb5.TKT_OPTS_PROXIABLE] = true; |
|
774 |
} |
|
775 |
if (body.kdcOptions.get(KDCOptions.POSTDATED)) { |
|
776 |
bFlags[Krb5.TKT_OPTS_POSTDATED] = true; |
|
777 |
} |
|
778 |
if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) { |
|
779 |
bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true; |
|
780 |
} |
|
781 |
bFlags[Krb5.TKT_OPTS_INITIAL] = true; |
|
782 |
||
783 |
f = KDCReq.class.getDeclaredField("pAData"); |
|
784 |
f.setAccessible(true); |
|
785 |
PAData[] pas = (PAData[])f.get(asReq); |
|
786 |
if (pas == null || pas.length == 0) { |
|
787 |
Object preauth = options.get(Option.PREAUTH_REQUIRED); |
|
788 |
if (preauth == null || preauth.equals(Boolean.TRUE)) { |
|
789 |
throw new KrbException(Krb5.KDC_ERR_PREAUTH_REQUIRED); |
|
790 |
} |
|
791 |
} else { |
|
792 |
try { |
|
793 |
Constructor<EncryptedData> ctor = EncryptedData.class.getDeclaredConstructor(DerValue.class); |
|
794 |
ctor.setAccessible(true); |
|
795 |
EncryptedData data = ctor.newInstance(new DerValue(pas[0].getValue())); |
|
796 |
data.decrypt(ckey, KeyUsage.KU_PA_ENC_TS); |
|
797 |
} catch (Exception e) { |
|
798 |
throw new KrbException(Krb5.KDC_ERR_PREAUTH_FAILED); |
|
799 |
} |
|
800 |
bFlags[Krb5.TKT_OPTS_PRE_AUTHENT] = true; |
|
801 |
} |
|
802 |
||
803 |
TicketFlags tFlags = new TicketFlags(bFlags); |
|
804 |
EncTicketPart enc = new EncTicketPart( |
|
805 |
tFlags, |
|
806 |
key, |
|
807 |
body.crealm, |
|
808 |
body.cname, |
|
809 |
new TransitedEncoding(1, new byte[0]), |
|
810 |
new KerberosTime(new Date()), |
|
811 |
body.from, |
|
812 |
till, body.rtime, |
|
813 |
body.addresses, |
|
814 |
null); |
|
815 |
Ticket t = new Ticket( |
|
816 |
body.crealm, |
|
817 |
body.sname, |
|
818 |
new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET) |
|
819 |
); |
|
820 |
EncASRepPart enc_part = new EncASRepPart( |
|
821 |
key, |
|
822 |
new LastReq(new LastReqEntry[]{ |
|
823 |
new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000)) |
|
824 |
}), |
|
825 |
body.getNonce(), // TODO: detect replay? |
|
826 |
new KerberosTime(new Date().getTime() + 1000 * 3600 * 24), |
|
827 |
// Next 5 and last MUST be same with ticket |
|
828 |
tFlags, |
|
829 |
new KerberosTime(new Date()), |
|
830 |
body.from, |
|
831 |
till, body.rtime, |
|
832 |
body.crealm, |
|
833 |
body.sname, |
|
834 |
body.addresses |
|
835 |
); |
|
836 |
EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_AS_REP_PART); |
|
837 |
ASRep asRep = new ASRep(null, |
|
838 |
body.crealm, |
|
839 |
body.cname, |
|
840 |
t, |
|
841 |
edata); |
|
842 |
||
843 |
System.out.println(" Return " + asRep.cname |
|
844 |
+ " ticket for " + asRep.ticket.sname); |
|
845 |
||
846 |
DerOutputStream out = new DerOutputStream(); |
|
847 |
out.write(DerValue.createTag(DerValue.TAG_APPLICATION, |
|
848 |
true, (byte)Krb5.KRB_AS_REP), asRep.asn1Encode()); |
|
1575
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
849 |
byte[] result = out.toByteArray(); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
850 |
|
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
851 |
// Added feature: |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
852 |
// Write the current issuing TGT into a ccache file specified |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
853 |
// by the system property below. |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
854 |
String ccache = System.getProperty("test.kdc.save.ccache"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
855 |
if (ccache != null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
856 |
asRep.encKDCRepPart = enc_part; |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
857 |
sun.security.krb5.internal.ccache.Credentials credentials = |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
858 |
new sun.security.krb5.internal.ccache.Credentials(asRep); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
859 |
asReq.reqBody.cname.setRealm(getRealm()); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
860 |
CredentialsCache cache = |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
861 |
CredentialsCache.create(asReq.reqBody.cname, ccache); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
862 |
if (cache == null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
863 |
throw new IOException("Unable to create the cache file " + |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
864 |
ccache); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
865 |
} |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
866 |
cache.update(credentials); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
867 |
cache.save(); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
868 |
new File(ccache).deleteOnExit(); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
869 |
} |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
870 |
|
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
871 |
return result; |
1454 | 872 |
} catch (KrbException ke) { |
873 |
ke.printStackTrace(System.out); |
|
874 |
KRBError kerr = ke.getError(); |
|
875 |
KDCReqBody body = asReq.reqBody; |
|
876 |
System.out.println(" Error " + ke.returnCode() |
|
877 |
+ " " +ke.returnCodeMessage()); |
|
878 |
byte[] eData = null; |
|
879 |
if (kerr == null) { |
|
880 |
if (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED || |
|
881 |
ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) { |
|
882 |
PAData pa; |
|
883 |
||
884 |
ETypeInfo2 ei2 = new ETypeInfo2(eTypes[0], null, null); |
|
885 |
DerOutputStream eid = new DerOutputStream(); |
|
886 |
eid.write(DerValue.tag_Sequence, ei2.asn1Encode()); |
|
887 |
||
888 |
pa = new PAData(Krb5.PA_ETYPE_INFO2, eid.toByteArray()); |
|
889 |
||
890 |
DerOutputStream bytes = new DerOutputStream(); |
|
891 |
bytes.write(new PAData(Krb5.PA_ENC_TIMESTAMP, new byte[0]).asn1Encode()); |
|
892 |
bytes.write(pa.asn1Encode()); |
|
893 |
||
894 |
boolean allOld = true; |
|
895 |
for (int i: eTypes) { |
|
896 |
if (i == EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96 || |
|
897 |
i == EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96) { |
|
898 |
allOld = false; |
|
899 |
break; |
|
900 |
} |
|
901 |
} |
|
902 |
if (allOld) { |
|
903 |
ETypeInfo ei = new ETypeInfo(eTypes[0], null); |
|
904 |
eid = new DerOutputStream(); |
|
905 |
eid.write(DerValue.tag_Sequence, ei.asn1Encode()); |
|
906 |
pa = new PAData(Krb5.PA_ETYPE_INFO, eid.toByteArray()); |
|
907 |
bytes.write(pa.asn1Encode()); |
|
908 |
} |
|
909 |
DerOutputStream temp = new DerOutputStream(); |
|
910 |
temp.write(DerValue.tag_Sequence, bytes); |
|
911 |
eData = temp.toByteArray(); |
|
912 |
} |
|
913 |
kerr = new KRBError(null, null, null, |
|
914 |
new KerberosTime(new Date()), |
|
915 |
0, |
|
916 |
ke.returnCode(), |
|
917 |
body.crealm, body.cname, |
|
918 |
new Realm(getRealm()), body.sname, |
|
919 |
KrbException.errorMessage(ke.returnCode()), |
|
920 |
eData); |
|
921 |
} |
|
922 |
return kerr.asn1Encode(); |
|
923 |
} |
|
924 |
} |
|
925 |
||
926 |
/** |
|
927 |
* Generates a line for a KDC to put inside [realms] of krb5.conf |
|
928 |
* @param kdc the KDC |
|
3046 | 929 |
* @return REALM.NAME = { kdc = host:port } |
1454 | 930 |
*/ |
931 |
private static String realmLineForKDC(KDC kdc) { |
|
3046 | 932 |
return String.format(" %s = {\n kdc = %s:%d\n }\n", |
933 |
kdc.realm, |
|
934 |
kdc.kdc, |
|
935 |
kdc.port); |
|
1454 | 936 |
} |
937 |
||
938 |
/** |
|
939 |
* Start the KDC service. This server listens on both UDP and TCP using |
|
940 |
* the same port number. It uses three threads to deal with requests. |
|
941 |
* They can be set to daemon threads if requested. |
|
942 |
* @param port the port number to listen to. If zero, a random available |
|
943 |
* port no less than 8000 will be chosen and used. |
|
944 |
* @param asDaemon true if the KDC threads should be daemons |
|
945 |
* @throws java.io.IOException for any communication error |
|
946 |
*/ |
|
947 |
protected void startServer(int port, boolean asDaemon) throws IOException { |
|
948 |
if (port > 0) { |
|
949 |
u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1")); |
|
950 |
t1 = new ServerSocket(port); |
|
951 |
} else { |
|
952 |
while (true) { |
|
953 |
// Try to find a port number that's both TCP and UDP free |
|
954 |
try { |
|
955 |
port = 8000 + new java.util.Random().nextInt(10000); |
|
956 |
u1 = null; |
|
957 |
u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1")); |
|
958 |
t1 = new ServerSocket(port); |
|
959 |
break; |
|
960 |
} catch (Exception e) { |
|
961 |
if (u1 != null) u1.close(); |
|
962 |
} |
|
963 |
} |
|
964 |
} |
|
965 |
final DatagramSocket udp = u1; |
|
966 |
final ServerSocket tcp = t1; |
|
967 |
System.out.println("Start KDC on " + port); |
|
968 |
||
969 |
this.port = port; |
|
970 |
||
971 |
// The UDP consumer |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
972 |
thread1 = new Thread() { |
1454 | 973 |
public void run() { |
974 |
while (true) { |
|
975 |
try { |
|
976 |
byte[] inbuf = new byte[8192]; |
|
977 |
DatagramPacket p = new DatagramPacket(inbuf, inbuf.length); |
|
978 |
udp.receive(p); |
|
979 |
System.out.println("-----------------------------------------------"); |
|
980 |
System.out.println(">>>>> UDP packet received"); |
|
981 |
q.put(new Job(processMessage(Arrays.copyOf(inbuf, p.getLength())), udp, p)); |
|
982 |
} catch (Exception e) { |
|
983 |
e.printStackTrace(); |
|
984 |
} |
|
985 |
} |
|
986 |
} |
|
987 |
}; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
988 |
thread1.setDaemon(asDaemon); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
989 |
thread1.start(); |
1454 | 990 |
|
991 |
// The TCP consumer |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
992 |
thread2 = new Thread() { |
1454 | 993 |
public void run() { |
994 |
while (true) { |
|
995 |
try { |
|
996 |
Socket socket = tcp.accept(); |
|
997 |
System.out.println("-----------------------------------------------"); |
|
998 |
System.out.println(">>>>> TCP connection established"); |
|
999 |
DataInputStream in = new DataInputStream(socket.getInputStream()); |
|
1000 |
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); |
|
1001 |
byte[] token = new byte[in.readInt()]; |
|
1002 |
in.readFully(token); |
|
1003 |
q.put(new Job(processMessage(token), socket, out)); |
|
1004 |
} catch (Exception e) { |
|
1005 |
e.printStackTrace(); |
|
1006 |
} |
|
1007 |
} |
|
1008 |
} |
|
1009 |
}; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1010 |
thread2.setDaemon(asDaemon); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1011 |
thread2.start(); |
1454 | 1012 |
|
1013 |
// The dispatcher |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1014 |
thread3 = new Thread() { |
1454 | 1015 |
public void run() { |
1016 |
while (true) { |
|
1017 |
try { |
|
1018 |
q.take().send(); |
|
1019 |
} catch (Exception e) { |
|
1020 |
} |
|
1021 |
} |
|
1022 |
} |
|
1023 |
}; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1024 |
thread3.setDaemon(true); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1025 |
thread3.start(); |
1454 | 1026 |
} |
1027 |
||
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1028 |
public void terminate() { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1029 |
try { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1030 |
thread1.stop(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1031 |
thread2.stop(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1032 |
thread3.stop(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1033 |
u1.close(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1034 |
t1.close(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1035 |
} catch (Exception e) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1036 |
// OK |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1037 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1038 |
} |
1454 | 1039 |
/** |
1040 |
* Helper class to encapsulate a job in a KDC. |
|
1041 |
*/ |
|
1042 |
private static class Job { |
|
1043 |
byte[] token; // The received request at creation time and |
|
1044 |
// the response at send time |
|
1045 |
Socket s; // The TCP socket from where the request comes |
|
1046 |
DataOutputStream out; // The OutputStream of the TCP socket |
|
1047 |
DatagramSocket s2; // The UDP socket from where the request comes |
|
1048 |
DatagramPacket dp; // The incoming UDP datagram packet |
|
1049 |
boolean useTCP; // Whether TCP or UDP is used |
|
1050 |
||
1051 |
// Creates a job object for TCP |
|
1052 |
Job(byte[] token, Socket s, DataOutputStream out) { |
|
1053 |
useTCP = true; |
|
1054 |
this.token = token; |
|
1055 |
this.s = s; |
|
1056 |
this.out = out; |
|
1057 |
} |
|
1058 |
||
1059 |
// Creates a job object for UDP |
|
1060 |
Job(byte[] token, DatagramSocket s2, DatagramPacket dp) { |
|
1061 |
useTCP = false; |
|
1062 |
this.token = token; |
|
1063 |
this.s2 = s2; |
|
1064 |
this.dp = dp; |
|
1065 |
} |
|
1066 |
||
1067 |
// Sends the output back to the client |
|
1068 |
void send() { |
|
1069 |
try { |
|
1070 |
if (useTCP) { |
|
1071 |
System.out.println(">>>>> TCP request honored"); |
|
1072 |
out.writeInt(token.length); |
|
1073 |
out.write(token); |
|
1074 |
s.close(); |
|
1075 |
} else { |
|
1076 |
System.out.println(">>>>> UDP request honored"); |
|
1077 |
s2.send(new DatagramPacket(token, token.length, dp.getAddress(), dp.getPort())); |
|
1078 |
} |
|
1079 |
} catch (Exception e) { |
|
1080 |
e.printStackTrace(); |
|
1081 |
} |
|
1082 |
} |
|
1083 |
} |
|
3046 | 1084 |
|
1085 |
public static class KDCNameService implements NameServiceDescriptor { |
|
1086 |
@Override |
|
1087 |
public NameService createNameService() throws Exception { |
|
1088 |
NameService ns = new NameService() { |
|
1089 |
@Override |
|
1090 |
public InetAddress[] lookupAllHostAddr(String host) |
|
1091 |
throws UnknownHostException { |
|
1092 |
// Everything is localhost |
|
1093 |
return new InetAddress[]{ |
|
1094 |
InetAddress.getByAddress(host, new byte[]{127,0,0,1}) |
|
1095 |
}; |
|
1096 |
} |
|
1097 |
@Override |
|
1098 |
public String getHostByAddr(byte[] addr) |
|
1099 |
throws UnknownHostException { |
|
1100 |
// No reverse lookup, PrincipalName use original string |
|
1101 |
throw new UnknownHostException(); |
|
1102 |
} |
|
1103 |
}; |
|
1104 |
return ns; |
|
1105 |
} |
|
1106 |
||
1107 |
@Override |
|
1108 |
public String getProviderName() { |
|
1109 |
return "mock"; |
|
1110 |
} |
|
1111 |
||
1112 |
@Override |
|
1113 |
public String getType() { |
|
1114 |
return "ns"; |
|
1115 |
} |
|
1116 |
} |
|
1454 | 1117 |
} |