author | weijun |
Mon, 13 Jul 2015 17:44:34 +0800 | |
changeset 31643 | abad00f2c027 |
parent 31429 | ce4193650b40 |
child 31801 | 1297473ab237 |
permissions | -rw-r--r-- |
1454 | 1 |
/* |
30038
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
2 |
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
1454 | 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.*; |
|
31429
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
33 |
|
3046 | 34 |
import sun.net.spi.nameservice.NameService; |
35 |
import sun.net.spi.nameservice.NameServiceDescriptor; |
|
1454 | 36 |
import sun.security.krb5.*; |
37 |
import sun.security.krb5.internal.*; |
|
1575
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
38 |
import sun.security.krb5.internal.ccache.CredentialsCache; |
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
39 |
import sun.security.krb5.internal.crypto.EType; |
1454 | 40 |
import sun.security.krb5.internal.crypto.KeyUsage; |
41 |
import sun.security.krb5.internal.ktab.KeyTab; |
|
42 |
import sun.security.util.DerInputStream; |
|
43 |
import sun.security.util.DerOutputStream; |
|
44 |
import sun.security.util.DerValue; |
|
45 |
||
46 |
/** |
|
47 |
* A KDC server. |
|
48 |
* <p> |
|
49 |
* Features: |
|
50 |
* <ol> |
|
51 |
* <li> Supports TCP and UDP |
|
52 |
* <li> Supports AS-REQ and TGS-REQ |
|
53 |
* <li> Principal db and other settings hard coded in application |
|
54 |
* <li> Options, say, request preauth or not |
|
55 |
* </ol> |
|
56 |
* Side effects: |
|
57 |
* <ol> |
|
58 |
* <li> The Sun-internal class <code>sun.security.krb5.Config</code> is a |
|
59 |
* singleton and initialized according to Kerberos settings (krb5.conf and |
|
60 |
* java.security.krb5.* system properties). This means once it's initialized |
|
61 |
* it will not automatically notice any changes to these settings (or file |
|
62 |
* changes of krb5.conf). The KDC class normally does not touch these |
|
63 |
* settings (except for the <code>writeKtab()</code> method). However, to make |
|
64 |
* sure nothing ever goes wrong, if you want to make any changes to these |
|
65 |
* settings after calling a KDC method, call <code>Config.refresh()</code> to |
|
66 |
* make sure your changes are reflected in the <code>Config</code> object. |
|
67 |
* </ol> |
|
4336 | 68 |
* System properties recognized: |
69 |
* <ul> |
|
70 |
* <li>test.kdc.save.ccache |
|
71 |
* </ul> |
|
1454 | 72 |
* Issues and TODOs: |
73 |
* <ol> |
|
74 |
* <li> Generates krb5.conf to be used on another machine, currently the kdc is |
|
75 |
* always localhost |
|
76 |
* <li> More options to KDC, say, error output, say, response nonce != |
|
77 |
* request nonce |
|
78 |
* </ol> |
|
79 |
* Note: This program uses internal krb5 classes (including reflection to |
|
80 |
* access private fields and methods). |
|
81 |
* <p> |
|
82 |
* Usages: |
|
83 |
* <p> |
|
84 |
* 1. Init and start the KDC: |
|
85 |
* <pre> |
|
86 |
* KDC kdc = KDC.create("REALM.NAME", port, isDaemon); |
|
87 |
* KDC kdc = KDC.create("REALM.NAME"); |
|
88 |
* </pre> |
|
89 |
* Here, <code>port</code> is the UDP and TCP port number the KDC server |
|
90 |
* listens on. If zero, a random port is chosen, which you can use getPort() |
|
91 |
* later to retrieve the value. |
|
92 |
* <p> |
|
93 |
* If <code>isDaemon</code> is true, the KDC worker threads will be daemons. |
|
94 |
* <p> |
|
95 |
* The shortcut <code>KDC.create("REALM.NAME")</code> has port=0 and |
|
96 |
* isDaemon=false, and is commonly used in an embedded KDC. |
|
97 |
* <p> |
|
98 |
* 2. Adding users: |
|
99 |
* <pre> |
|
100 |
* kdc.addPrincipal(String principal_name, char[] password); |
|
101 |
* kdc.addPrincipalRandKey(String principal_name); |
|
102 |
* </pre> |
|
103 |
* A service principal's name should look like "host/f.q.d.n". The second form |
|
104 |
* generates a random key. To expose this key, call <code>writeKtab()</code> to |
|
105 |
* save the keys into a keytab file. |
|
106 |
* <p> |
|
107 |
* Note that you need to add the principal name krbtgt/REALM.NAME yourself. |
|
108 |
* <p> |
|
109 |
* Note that you can safely add a principal at any time after the KDC is |
|
110 |
* started and before a user requests info on this principal. |
|
111 |
* <p> |
|
112 |
* 3. Other public methods: |
|
113 |
* <ul> |
|
114 |
* <li> <code>getPort</code>: Returns the port number the KDC uses |
|
115 |
* <li> <code>getRealm</code>: Returns the realm name |
|
116 |
* <li> <code>writeKtab</code>: Writes all principals' keys into a keytab file |
|
117 |
* <li> <code>saveConfig</code>: Saves a krb5.conf file to access this KDC |
|
118 |
* <li> <code>setOption</code>: Sets various options |
|
119 |
* </ul> |
|
120 |
* Read the javadoc for details. Lazy developer can use <code>OneKDC</code> |
|
121 |
* directly. |
|
122 |
*/ |
|
123 |
public class KDC { |
|
124 |
||
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
125 |
public static final int DEFAULT_LIFETIME = 39600; |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
126 |
public static final int DEFAULT_RENEWTIME = 86400; |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
127 |
|
1454 | 128 |
// Under the hood. |
129 |
||
130 |
// The random generator to generate random keys (including session keys) |
|
131 |
private static SecureRandom secureRandom = new SecureRandom(); |
|
7183 | 132 |
|
133 |
// Principal db. principal -> pass. A case-insensitive TreeMap is used |
|
134 |
// so that even if the client provides a name with different case, the KDC |
|
135 |
// can still locate the principal and give back correct salt. |
|
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
136 |
private TreeMap<String,char[]> passwords = new TreeMap<> |
7183 | 137 |
(String.CASE_INSENSITIVE_ORDER); |
138 |
||
1454 | 139 |
// Realm name |
140 |
private String realm; |
|
3046 | 141 |
// KDC |
142 |
private String kdc; |
|
143 |
// Service port number |
|
144 |
private int port; |
|
1454 | 145 |
// The request/response job queue |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
146 |
private BlockingQueue<Job> q = new ArrayBlockingQueue<>(100); |
1454 | 147 |
// Options |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
148 |
private Map<Option,Object> options = new HashMap<>(); |
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
149 |
// Realm-specific krb5.conf settings |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
150 |
private List<String> conf = new ArrayList<>(); |
1454 | 151 |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
152 |
private Thread thread1, thread2, thread3; |
30038
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
153 |
private volatile boolean udpConsumerReady = false; |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
154 |
private volatile boolean tcpConsumerReady = false; |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
155 |
private volatile boolean dispatcherReady = false; |
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
156 |
DatagramSocket u1 = null; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
157 |
ServerSocket t1 = null; |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
158 |
|
31429
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
159 |
public static enum KtabMode { APPEND, EXISTING }; |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
160 |
|
1454 | 161 |
/** |
162 |
* Option names, to be expanded forever. |
|
163 |
*/ |
|
164 |
public static enum Option { |
|
165 |
/** |
|
166 |
* Whether pre-authentication is required. Default Boolean.TRUE |
|
167 |
*/ |
|
168 |
PREAUTH_REQUIRED, |
|
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
169 |
/** |
5802
ea99d72d3c19
6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents:
5774
diff
changeset
|
170 |
* Only issue TGT in RC4 |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
171 |
*/ |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
172 |
ONLY_RC4_TGT, |
5802
ea99d72d3c19
6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents:
5774
diff
changeset
|
173 |
/** |
7183 | 174 |
* Use RC4 as the first in preauth |
5802
ea99d72d3c19
6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents:
5774
diff
changeset
|
175 |
*/ |
7183 | 176 |
RC4_FIRST_PREAUTH, |
177 |
/** |
|
178 |
* Use only one preauth, so that some keys are not easy to generate |
|
179 |
*/ |
|
180 |
ONLY_ONE_PREAUTH, |
|
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
181 |
/** |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
182 |
* Set all name-type to a value in response |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
183 |
*/ |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
184 |
RESP_NT, |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
185 |
/** |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
186 |
* Multiple ETYPE-INFO-ENTRY with same etype but different salt |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
187 |
*/ |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
188 |
DUP_ETYPE, |
12867 | 189 |
/** |
190 |
* What backend server can be delegated to |
|
191 |
*/ |
|
192 |
OK_AS_DELEGATE, |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
193 |
/** |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
194 |
* Allow S4U2self, List<String> of middle servers. |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
195 |
* If not set, means KDC does not understand S4U2self at all, therefore |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
196 |
* would ignore any PA-FOR-USER request and send a ticket using the |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
197 |
* cname of teh requestor. If set, it returns FORWARDABLE tickets to |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
198 |
* a server with its name in the list |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
199 |
*/ |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
200 |
ALLOW_S4U2SELF, |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
201 |
/** |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
202 |
* Allow S4U2proxy, Map<String,List<String>> of middle servers to |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
203 |
* backends. If not set or a backend not in a server's list, |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
204 |
* Krb5.KDC_ERR_POLICY will be send for S4U2proxy request. |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
205 |
*/ |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
206 |
ALLOW_S4U2PROXY, |
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
207 |
/** |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
208 |
* Sensitive accounts can never be delegated. |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
209 |
*/ |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
210 |
SENSITIVE_ACCOUNTS, |
30959 | 211 |
/** |
212 |
* If true, will check if TGS-REQ contains a non-null addresses field. |
|
213 |
*/ |
|
214 |
CHECK_ADDRESSES, |
|
1454 | 215 |
}; |
216 |
||
3046 | 217 |
static { |
218 |
System.setProperty("sun.net.spi.nameservice.provider.1", "ns,mock"); |
|
219 |
} |
|
220 |
||
1454 | 221 |
/** |
222 |
* A standalone KDC server. |
|
223 |
*/ |
|
224 |
public static void main(String[] args) throws Exception { |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
225 |
int port = args.length > 0 ? Integer.parseInt(args[0]) : 0; |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
226 |
KDC kdc = create("RABBIT.HOLE", "kdc.rabbit.hole", port, false); |
1454 | 227 |
kdc.addPrincipal("dummy", "bogus".toCharArray()); |
228 |
kdc.addPrincipal("foo", "bar".toCharArray()); |
|
3046 | 229 |
kdc.addPrincipalRandKey("krbtgt/RABBIT.HOLE"); |
230 |
kdc.addPrincipalRandKey("server/host.rabbit.hole"); |
|
231 |
kdc.addPrincipalRandKey("backend/host.rabbit.hole"); |
|
232 |
KDC.saveConfig("krb5.conf", kdc, "forwardable = true"); |
|
1454 | 233 |
} |
234 |
||
235 |
/** |
|
236 |
* Creates and starts a KDC running as a daemon on a random port. |
|
237 |
* @param realm the realm name |
|
238 |
* @return the running KDC instance |
|
239 |
* @throws java.io.IOException for any socket creation error |
|
240 |
*/ |
|
241 |
public static KDC create(String realm) throws IOException { |
|
3046 | 242 |
return create(realm, "kdc." + realm.toLowerCase(), 0, true); |
1454 | 243 |
} |
244 |
||
7183 | 245 |
public static KDC existing(String realm, String kdc, int port) { |
246 |
KDC k = new KDC(realm, kdc); |
|
247 |
k.port = port; |
|
248 |
return k; |
|
249 |
} |
|
250 |
||
1454 | 251 |
/** |
252 |
* Creates and starts a KDC server. |
|
253 |
* @param realm the realm name |
|
254 |
* @param port the TCP and UDP port to listen to. A random port will to |
|
255 |
* chosen if zero. |
|
256 |
* @param asDaemon if true, KDC threads will be daemons. Otherwise, not. |
|
257 |
* @return the running KDC instance |
|
258 |
* @throws java.io.IOException for any socket creation error |
|
259 |
*/ |
|
3046 | 260 |
public static KDC create(String realm, String kdc, int port, boolean asDaemon) throws IOException { |
261 |
return new KDC(realm, kdc, port, asDaemon); |
|
1454 | 262 |
} |
263 |
||
264 |
/** |
|
265 |
* Sets an option |
|
266 |
* @param key the option name |
|
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
267 |
* @param value the value |
1454 | 268 |
*/ |
269 |
public void setOption(Option key, Object value) { |
|
12867 | 270 |
if (value == null) { |
271 |
options.remove(key); |
|
272 |
} else { |
|
273 |
options.put(key, value); |
|
274 |
} |
|
1454 | 275 |
} |
276 |
||
277 |
/** |
|
12199 | 278 |
* Writes or appends keys into a keytab. |
279 |
* <p> |
|
280 |
* Attention: This is the most basic one of a series of methods below on |
|
281 |
* keytab creation or modification. All these methods reference krb5.conf |
|
282 |
* settings. If you need to modify krb5.conf or switch to another krb5.conf |
|
283 |
* later, please call <code>Config.refresh()</code> again. For example: |
|
284 |
* <pre> |
|
285 |
* kdc.writeKtab("/etc/kdc/ktab", true); // Config is initialized, |
|
286 |
* System.setProperty("java.security.krb5.conf", "/home/mykrb5.conf"); |
|
287 |
* Config.refresh(); |
|
288 |
* </pre> |
|
289 |
* Inside this method there are 2 places krb5.conf is used: |
|
290 |
* <ol> |
|
291 |
* <li> (Fatal) Generating keys: EncryptionKey.acquireSecretKeys |
|
292 |
* <li> (Has workaround) Creating PrincipalName |
|
293 |
* </ol> |
|
294 |
* @param tab the keytab file name |
|
9499 | 295 |
* @param append true if append, otherwise, overwrite. |
12199 | 296 |
* @param names the names to write into, write all if names is empty |
9499 | 297 |
*/ |
12199 | 298 |
public void writeKtab(String tab, boolean append, String... names) |
9499 | 299 |
throws IOException, KrbException { |
300 |
KeyTab ktab = append ? KeyTab.getInstance(tab) : KeyTab.create(tab); |
|
12199 | 301 |
Iterable<String> entries = |
302 |
(names.length != 0) ? Arrays.asList(names): passwords.keySet(); |
|
303 |
for (String name : entries) { |
|
304 |
char[] pass = passwords.get(name); |
|
305 |
int kvno = 0; |
|
306 |
if (Character.isDigit(pass[pass.length-1])) { |
|
307 |
kvno = pass[pass.length-1] - '0'; |
|
9499 | 308 |
} |
15006 | 309 |
PrincipalName pn = new PrincipalName(name, |
310 |
name.indexOf('/') < 0 ? |
|
311 |
PrincipalName.KRB_NT_UNKNOWN : |
|
312 |
PrincipalName.KRB_NT_SRV_HST); |
|
313 |
ktab.addEntry(pn, |
|
314 |
getSalt(pn), |
|
12199 | 315 |
pass, |
316 |
kvno, |
|
317 |
true); |
|
9499 | 318 |
} |
319 |
ktab.save(); |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Writes all principals' keys from multiple KDCs into one keytab file. |
|
1454 | 324 |
* @throws java.io.IOException for any file output error |
325 |
* @throws sun.security.krb5.KrbException for any realm and/or principal |
|
326 |
* name error. |
|
327 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
328 |
public static void writeMultiKtab(String tab, KDC... kdcs) |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
329 |
throws IOException, KrbException { |
12199 | 330 |
KeyTab.create(tab).save(); // Empty the old keytab |
331 |
appendMultiKtab(tab, kdcs); |
|
9499 | 332 |
} |
333 |
||
334 |
/** |
|
335 |
* Appends all principals' keys from multiple KDCs to one keytab file. |
|
336 |
*/ |
|
337 |
public static void appendMultiKtab(String tab, KDC... kdcs) |
|
338 |
throws IOException, KrbException { |
|
12199 | 339 |
for (KDC kdc: kdcs) { |
340 |
kdc.writeKtab(tab, true); |
|
341 |
} |
|
1454 | 342 |
} |
343 |
||
344 |
/** |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
345 |
* Write a ktab for this KDC. |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
346 |
*/ |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
347 |
public void writeKtab(String tab) throws IOException, KrbException { |
12199 | 348 |
writeKtab(tab, false); |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
349 |
} |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
350 |
|
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
1575
diff
changeset
|
351 |
/** |
9499 | 352 |
* Appends keys in this KDC to a ktab. |
353 |
*/ |
|
354 |
public void appendKtab(String tab) throws IOException, KrbException { |
|
12199 | 355 |
writeKtab(tab, true); |
9499 | 356 |
} |
357 |
||
358 |
/** |
|
1454 | 359 |
* Adds a new principal to this realm with a given password. |
360 |
* @param user the principal's name. For a service principal, use the |
|
361 |
* form of host/f.q.d.n |
|
362 |
* @param pass the password for the principal |
|
363 |
*/ |
|
364 |
public void addPrincipal(String user, char[] pass) { |
|
3046 | 365 |
if (user.indexOf('@') < 0) { |
366 |
user = user + "@" + realm; |
|
367 |
} |
|
1454 | 368 |
passwords.put(user, pass); |
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Adds a new principal to this realm with a random password |
|
373 |
* @param user the principal's name. For a service principal, use the |
|
374 |
* form of host/f.q.d.n |
|
375 |
*/ |
|
376 |
public void addPrincipalRandKey(String user) { |
|
3046 | 377 |
addPrincipal(user, randomPassword()); |
1454 | 378 |
} |
379 |
||
380 |
/** |
|
381 |
* Returns the name of this realm |
|
382 |
* @return the name of this realm |
|
383 |
*/ |
|
384 |
public String getRealm() { |
|
385 |
return realm; |
|
386 |
} |
|
387 |
||
388 |
/** |
|
3046 | 389 |
* Returns the name of kdc |
390 |
* @return the name of kdc |
|
391 |
*/ |
|
392 |
public String getKDC() { |
|
393 |
return kdc; |
|
394 |
} |
|
395 |
||
396 |
/** |
|
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
397 |
* Add realm-specific krb5.conf setting |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
398 |
*/ |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
399 |
public void addConf(String s) { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
400 |
conf.add(s); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
401 |
} |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
402 |
|
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
403 |
/** |
1454 | 404 |
* Writes a krb5.conf for one or more KDC that includes KDC locations for |
405 |
* each realm and the default realm name. You can also add extra strings |
|
406 |
* into the file. The method should be called like: |
|
407 |
* <pre> |
|
408 |
* KDC.saveConfig("krb5.conf", kdc1, kdc2, ..., line1, line2, ...); |
|
409 |
* </pre> |
|
410 |
* Here you can provide one or more kdc# and zero or more line# arguments. |
|
411 |
* The line# will be put after [libdefaults] and before [realms]. Therefore |
|
412 |
* you can append new lines into [libdefaults] and/or create your new |
|
413 |
* stanzas as well. Note that a newline character will be appended to |
|
414 |
* each line# argument. |
|
415 |
* <p> |
|
416 |
* For example: |
|
417 |
* <pre> |
|
418 |
* KDC.saveConfig("krb5.conf", this); |
|
419 |
* </pre> |
|
420 |
* generates: |
|
421 |
* <pre> |
|
422 |
* [libdefaults] |
|
423 |
* default_realm = REALM.NAME |
|
424 |
* |
|
425 |
* [realms] |
|
426 |
* REALM.NAME = { |
|
3046 | 427 |
* kdc = host:port_number |
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
428 |
* # realm-specific settings |
1454 | 429 |
* } |
430 |
* </pre> |
|
431 |
* |
|
432 |
* Another example: |
|
433 |
* <pre> |
|
434 |
* KDC.saveConfig("krb5.conf", kdc1, kdc2, "forwardable = true", "", |
|
435 |
* "[domain_realm]", |
|
436 |
* ".kdc1.com = KDC1.NAME"); |
|
437 |
* </pre> |
|
438 |
* generates: |
|
439 |
* <pre> |
|
440 |
* [libdefaults] |
|
441 |
* default_realm = KDC1.NAME |
|
442 |
* forwardable = true |
|
443 |
* |
|
444 |
* [domain_realm] |
|
445 |
* .kdc1.com = KDC1.NAME |
|
446 |
* |
|
447 |
* [realms] |
|
448 |
* KDC1.NAME = { |
|
3046 | 449 |
* kdc = host:port1 |
1454 | 450 |
* } |
451 |
* KDC2.NAME = { |
|
3046 | 452 |
* kdc = host:port2 |
1454 | 453 |
* } |
454 |
* </pre> |
|
455 |
* @param file the name of the file to write into |
|
456 |
* @param kdc the first (and default) KDC |
|
457 |
* @param more more KDCs or extra lines (in their appearing order) to |
|
458 |
* insert into the krb5.conf file. This method reads each argument's type |
|
459 |
* to determine what it's for. This argument can be empty. |
|
460 |
* @throws java.io.IOException for any file output error |
|
461 |
*/ |
|
462 |
public static void saveConfig(String file, KDC kdc, Object... more) |
|
463 |
throws IOException { |
|
464 |
File f = new File(file); |
|
465 |
StringBuffer sb = new StringBuffer(); |
|
466 |
sb.append("[libdefaults]\ndefault_realm = "); |
|
467 |
sb.append(kdc.realm); |
|
468 |
sb.append("\n"); |
|
469 |
for (Object o: more) { |
|
470 |
if (o instanceof String) { |
|
471 |
sb.append(o); |
|
472 |
sb.append("\n"); |
|
473 |
} |
|
474 |
} |
|
475 |
sb.append("\n[realms]\n"); |
|
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
476 |
sb.append(kdc.realmLine()); |
1454 | 477 |
for (Object o: more) { |
478 |
if (o instanceof KDC) { |
|
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
479 |
sb.append(((KDC)o).realmLine()); |
1454 | 480 |
} |
481 |
} |
|
482 |
FileOutputStream fos = new FileOutputStream(f); |
|
483 |
fos.write(sb.toString().getBytes()); |
|
484 |
fos.close(); |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* Returns the service port of the KDC server. |
|
489 |
* @return the KDC service port |
|
490 |
*/ |
|
491 |
public int getPort() { |
|
492 |
return port; |
|
493 |
} |
|
494 |
||
495 |
// Private helper methods |
|
496 |
||
497 |
/** |
|
498 |
* Private constructor, cannot be called outside. |
|
499 |
* @param realm |
|
500 |
*/ |
|
3046 | 501 |
private KDC(String realm, String kdc) { |
1454 | 502 |
this.realm = realm; |
3046 | 503 |
this.kdc = kdc; |
1454 | 504 |
} |
505 |
||
506 |
/** |
|
507 |
* A constructor that starts the KDC service also. |
|
508 |
*/ |
|
3046 | 509 |
protected KDC(String realm, String kdc, int port, boolean asDaemon) |
1454 | 510 |
throws IOException { |
3046 | 511 |
this(realm, kdc); |
1454 | 512 |
startServer(port, asDaemon); |
513 |
} |
|
514 |
/** |
|
515 |
* Generates a 32-char random password |
|
516 |
* @return the password |
|
517 |
*/ |
|
518 |
private static char[] randomPassword() { |
|
519 |
char[] pass = new char[32]; |
|
5622 | 520 |
for (int i=0; i<31; i++) |
1454 | 521 |
pass[i] = (char)secureRandom.nextInt(); |
5622 | 522 |
// The last char cannot be a number, otherwise, keyForUser() |
523 |
// believes it's a sign of kvno |
|
524 |
pass[31] = 'Z'; |
|
1454 | 525 |
return pass; |
526 |
} |
|
527 |
||
528 |
/** |
|
529 |
* Generates a random key for the given encryption type. |
|
530 |
* @param eType the encryption type |
|
531 |
* @return the generated key |
|
532 |
* @throws sun.security.krb5.KrbException for unknown/unsupported etype |
|
533 |
*/ |
|
534 |
private static EncryptionKey generateRandomKey(int eType) |
|
535 |
throws KrbException { |
|
536 |
// Is 32 enough for AES256? I should have generated the keys directly |
|
537 |
// but different cryptos have different rules on what keys are valid. |
|
538 |
char[] pass = randomPassword(); |
|
539 |
String algo; |
|
540 |
switch (eType) { |
|
541 |
case EncryptedData.ETYPE_DES_CBC_MD5: algo = "DES"; break; |
|
542 |
case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: algo = "DESede"; break; |
|
543 |
case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: algo = "AES128"; break; |
|
544 |
case EncryptedData.ETYPE_ARCFOUR_HMAC: algo = "ArcFourHMAC"; break; |
|
545 |
case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: algo = "AES256"; break; |
|
546 |
default: algo = "DES"; break; |
|
547 |
} |
|
548 |
return new EncryptionKey(pass, "NOTHING", algo); // Silly |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* Returns the password for a given principal |
|
553 |
* @param p principal |
|
554 |
* @return the password |
|
555 |
* @throws sun.security.krb5.KrbException when the principal is not inside |
|
556 |
* the database. |
|
557 |
*/ |
|
4336 | 558 |
private char[] getPassword(PrincipalName p, boolean server) |
559 |
throws KrbException { |
|
3046 | 560 |
String pn = p.toString(); |
561 |
if (p.getRealmString() == null) { |
|
562 |
pn = pn + "@" + getRealm(); |
|
563 |
} |
|
564 |
char[] pass = passwords.get(pn); |
|
1454 | 565 |
if (pass == null) { |
4336 | 566 |
throw new KrbException(server? |
567 |
Krb5.KDC_ERR_S_PRINCIPAL_UNKNOWN: |
|
15006 | 568 |
Krb5.KDC_ERR_C_PRINCIPAL_UNKNOWN, pn.toString()); |
1454 | 569 |
} |
570 |
return pass; |
|
571 |
} |
|
572 |
||
573 |
/** |
|
3046 | 574 |
* Returns the salt string for the principal. |
1454 | 575 |
* @param p principal |
576 |
* @return the salt |
|
577 |
*/ |
|
15006 | 578 |
protected String getSalt(PrincipalName p) { |
7183 | 579 |
String pn = p.toString(); |
580 |
if (p.getRealmString() == null) { |
|
581 |
pn = pn + "@" + getRealm(); |
|
582 |
} |
|
583 |
if (passwords.containsKey(pn)) { |
|
584 |
try { |
|
585 |
// Find the principal name with correct case. |
|
586 |
p = new PrincipalName(passwords.ceilingEntry(pn).getKey()); |
|
587 |
} catch (RealmException re) { |
|
588 |
// Won't happen |
|
589 |
} |
|
590 |
} |
|
3046 | 591 |
String s = p.getRealmString(); |
592 |
if (s == null) s = getRealm(); |
|
593 |
for (String n: p.getNameStrings()) { |
|
594 |
s += n; |
|
1454 | 595 |
} |
3046 | 596 |
return s; |
1454 | 597 |
} |
598 |
||
599 |
/** |
|
600 |
* Returns the key for a given principal of the given encryption type |
|
601 |
* @param p the principal |
|
602 |
* @param etype the encryption type |
|
4336 | 603 |
* @param server looking for a server principal? |
1454 | 604 |
* @return the key |
605 |
* @throws sun.security.krb5.KrbException for unknown/unsupported etype |
|
606 |
*/ |
|
4336 | 607 |
private EncryptionKey keyForUser(PrincipalName p, int etype, boolean server) |
608 |
throws KrbException { |
|
1454 | 609 |
try { |
610 |
// Do not call EncryptionKey.acquireSecretKeys(), otherwise |
|
611 |
// the krb5.conf config file would be loaded. |
|
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
612 |
Integer kvno = null; |
4532 | 613 |
// For service whose password ending with a number, use it as kvno. |
614 |
// Kvno must be postive. |
|
615 |
if (p.toString().indexOf('/') > 0) { |
|
4336 | 616 |
char[] pass = getPassword(p, server); |
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
617 |
if (Character.isDigit(pass[pass.length-1])) { |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
618 |
kvno = pass[pass.length-1] - '0'; |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
619 |
} |
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
620 |
} |
7183 | 621 |
return new EncryptionKey(EncryptionKeyDotStringToKey( |
622 |
getPassword(p, server), getSalt(p), null, etype), |
|
4168
1a8d21bb898c
6893158: AP_REQ check should use key version number
weijun
parents:
3046
diff
changeset
|
623 |
etype, kvno); |
4336 | 624 |
} catch (KrbException ke) { |
625 |
throw ke; |
|
1454 | 626 |
} catch (Exception e) { |
627 |
throw new RuntimeException(e); // should not happen |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* Processes an incoming request and generates a response. |
|
633 |
* @param in the request |
|
634 |
* @return the response |
|
635 |
* @throws java.lang.Exception for various errors |
|
636 |
*/ |
|
21961
50019af27ca3
8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents:
18536
diff
changeset
|
637 |
protected byte[] processMessage(byte[] in) throws Exception { |
1454 | 638 |
if ((in[0] & 0x1f) == Krb5.KRB_AS_REQ) |
639 |
return processAsReq(in); |
|
640 |
else |
|
641 |
return processTgsReq(in); |
|
642 |
} |
|
643 |
||
644 |
/** |
|
645 |
* Processes a TGS_REQ and generates a TGS_REP (or KRB_ERROR) |
|
646 |
* @param in the request |
|
647 |
* @return the response |
|
648 |
* @throws java.lang.Exception for various errors |
|
649 |
*/ |
|
21961
50019af27ca3
8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents:
18536
diff
changeset
|
650 |
protected byte[] processTgsReq(byte[] in) throws Exception { |
1454 | 651 |
TGSReq tgsReq = new TGSReq(in); |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
652 |
PrincipalName service = tgsReq.reqBody.sname; |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
653 |
if (options.containsKey(KDC.Option.RESP_NT)) { |
13247 | 654 |
service = new PrincipalName((int)options.get(KDC.Option.RESP_NT), |
655 |
service.getNameStrings(), service.getRealm()); |
|
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
656 |
} |
1454 | 657 |
try { |
658 |
System.out.println(realm + "> " + tgsReq.reqBody.cname + |
|
659 |
" sends TGS-REQ for " + |
|
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
660 |
service + ", " + tgsReq.reqBody.kdcOptions); |
1454 | 661 |
KDCReqBody body = tgsReq.reqBody; |
7183 | 662 |
int[] eTypes = KDCReqBodyDotEType(body); |
663 |
int e2 = eTypes[0]; // etype for outgoing session key |
|
664 |
int e3 = eTypes[0]; // etype for outgoing ticket |
|
1454 | 665 |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
666 |
PAData[] pas = KDCReqDotPAData(tgsReq); |
1454 | 667 |
|
668 |
Ticket tkt = null; |
|
669 |
EncTicketPart etp = null; |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
670 |
|
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
671 |
PrincipalName cname = null; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
672 |
boolean allowForwardable = true; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
673 |
|
1454 | 674 |
if (pas == null || pas.length == 0) { |
675 |
throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP); |
|
676 |
} else { |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
677 |
PrincipalName forUserCName = null; |
1454 | 678 |
for (PAData pa: pas) { |
679 |
if (pa.getType() == Krb5.PA_TGS_REQ) { |
|
680 |
APReq apReq = new APReq(pa.getValue()); |
|
681 |
EncryptedData ed = apReq.authenticator; |
|
682 |
tkt = apReq.ticket; |
|
7183 | 683 |
int te = tkt.encPart.getEType(); |
684 |
EncryptionKey kkey = keyForUser(tkt.sname, te, true); |
|
1454 | 685 |
byte[] bb = tkt.encPart.decrypt(kkey, KeyUsage.KU_TICKET); |
686 |
DerInputStream derIn = new DerInputStream(bb); |
|
687 |
DerValue der = derIn.getDerValue(); |
|
688 |
etp = new EncTicketPart(der.toByteArray()); |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
689 |
// Finally, cname will be overwritten by PA-FOR-USER |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
690 |
// if it exists. |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
691 |
cname = etp.cname; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
692 |
System.out.println(realm + "> presenting a ticket of " |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
693 |
+ etp.cname + " to " + tkt.sname); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
694 |
} else if (pa.getType() == Krb5.PA_FOR_USER) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
695 |
if (options.containsKey(Option.ALLOW_S4U2SELF)) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
696 |
PAForUserEnc p4u = new PAForUserEnc( |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
697 |
new DerValue(pa.getValue()), null); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
698 |
forUserCName = p4u.name; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
699 |
System.out.println(realm + "> presenting a PA_FOR_USER " |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
700 |
+ " in the name of " + p4u.name); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
701 |
} |
1454 | 702 |
} |
703 |
} |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
704 |
if (forUserCName != null) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
705 |
List<String> names = (List<String>)options.get(Option.ALLOW_S4U2SELF); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
706 |
if (!names.contains(cname.toString())) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
707 |
// Mimic the normal KDC behavior. When a server is not |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
708 |
// allowed to send S4U2self, do not send an error. |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
709 |
// Instead, send a ticket which is useless later. |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
710 |
allowForwardable = false; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
711 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
712 |
cname = forUserCName; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
713 |
} |
1454 | 714 |
if (tkt == null) { |
715 |
throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP); |
|
716 |
} |
|
717 |
} |
|
718 |
||
719 |
// Session key for original ticket, TGT |
|
720 |
EncryptionKey ckey = etp.key; |
|
721 |
||
722 |
// Session key for session with the service |
|
7183 | 723 |
EncryptionKey key = generateRandomKey(e2); |
1454 | 724 |
|
725 |
// Check time, TODO |
|
726 |
KerberosTime till = body.till; |
|
727 |
if (till == null) { |
|
728 |
throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO |
|
729 |
} else if (till.isZero()) { |
|
730 |
till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11); |
|
731 |
} |
|
732 |
||
733 |
boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1]; |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
734 |
if (body.kdcOptions.get(KDCOptions.FORWARDABLE) |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
735 |
&& allowForwardable) { |
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
736 |
List<String> sensitives = (List<String>) |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
737 |
options.get(Option.SENSITIVE_ACCOUNTS); |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
738 |
if (sensitives != null && sensitives.contains(cname.toString())) { |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
739 |
// Cannot make FORWARDABLE |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
740 |
} else { |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
741 |
bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true; |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
742 |
} |
1454 | 743 |
} |
30959 | 744 |
if (options.containsKey(Option.CHECK_ADDRESSES) |
745 |
&& body.kdcOptions.get(KDCOptions.FORWARDED) |
|
746 |
&& body.addresses == null) { |
|
747 |
throw new KrbException(Krb5.KDC_ERR_BADOPTION); |
|
748 |
} |
|
1454 | 749 |
if (body.kdcOptions.get(KDCOptions.FORWARDED) || |
750 |
etp.flags.get(Krb5.TKT_OPTS_FORWARDED)) { |
|
751 |
bFlags[Krb5.TKT_OPTS_FORWARDED] = true; |
|
752 |
} |
|
753 |
if (body.kdcOptions.get(KDCOptions.RENEWABLE)) { |
|
754 |
bFlags[Krb5.TKT_OPTS_RENEWABLE] = true; |
|
755 |
//renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7); |
|
756 |
} |
|
757 |
if (body.kdcOptions.get(KDCOptions.PROXIABLE)) { |
|
758 |
bFlags[Krb5.TKT_OPTS_PROXIABLE] = true; |
|
759 |
} |
|
760 |
if (body.kdcOptions.get(KDCOptions.POSTDATED)) { |
|
761 |
bFlags[Krb5.TKT_OPTS_POSTDATED] = true; |
|
762 |
} |
|
763 |
if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) { |
|
764 |
bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true; |
|
765 |
} |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
766 |
if (body.kdcOptions.get(KDCOptions.CNAME_IN_ADDL_TKT)) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
767 |
if (!options.containsKey(Option.ALLOW_S4U2PROXY)) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
768 |
// Don't understand CNAME_IN_ADDL_TKT |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
769 |
throw new KrbException(Krb5.KDC_ERR_BADOPTION); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
770 |
} else { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
771 |
Map<String,List<String>> map = (Map<String,List<String>>) |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
772 |
options.get(Option.ALLOW_S4U2PROXY); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
773 |
Ticket second = KDCReqBodyDotFirstAdditionalTicket(body); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
774 |
EncryptionKey key2 = keyForUser(second.sname, second.encPart.getEType(), true); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
775 |
byte[] bb = second.encPart.decrypt(key2, KeyUsage.KU_TICKET); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
776 |
DerInputStream derIn = new DerInputStream(bb); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
777 |
DerValue der = derIn.getDerValue(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
778 |
EncTicketPart tktEncPart = new EncTicketPart(der.toByteArray()); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
779 |
if (!tktEncPart.flags.get(Krb5.TKT_OPTS_FORWARDABLE)) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
780 |
//throw new KrbException(Krb5.KDC_ERR_BADOPTION); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
781 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
782 |
PrincipalName client = tktEncPart.cname; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
783 |
System.out.println(realm + "> and an additional ticket of " |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
784 |
+ client + " to " + second.sname); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
785 |
if (map.containsKey(cname.toString())) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
786 |
if (map.get(cname.toString()).contains(service.toString())) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
787 |
System.out.println(realm + "> S4U2proxy OK"); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
788 |
} else { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
789 |
throw new KrbException(Krb5.KDC_ERR_BADOPTION); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
790 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
791 |
} else { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
792 |
throw new KrbException(Krb5.KDC_ERR_BADOPTION); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
793 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
794 |
cname = client; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
795 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
796 |
} |
4336 | 797 |
|
12867 | 798 |
String okAsDelegate = (String)options.get(Option.OK_AS_DELEGATE); |
799 |
if (okAsDelegate != null && ( |
|
800 |
okAsDelegate.isEmpty() || |
|
801 |
okAsDelegate.contains(service.getNameString()))) { |
|
4336 | 802 |
bFlags[Krb5.TKT_OPTS_DELEGATE] = true; |
803 |
} |
|
1454 | 804 |
bFlags[Krb5.TKT_OPTS_INITIAL] = true; |
805 |
||
806 |
TicketFlags tFlags = new TicketFlags(bFlags); |
|
807 |
EncTicketPart enc = new EncTicketPart( |
|
808 |
tFlags, |
|
809 |
key, |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
810 |
cname, |
1454 | 811 |
new TransitedEncoding(1, new byte[0]), // TODO |
812 |
new KerberosTime(new Date()), |
|
813 |
body.from, |
|
31643
abad00f2c027
8058290: JAAS Krb5LoginModule has suspect ticket-renewal logic, relies on clockskew grace
weijun
parents:
31429
diff
changeset
|
814 |
till, etp.renewTill, |
30959 | 815 |
body.addresses != null ? body.addresses |
816 |
: etp.caddr, |
|
1454 | 817 |
null); |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
818 |
EncryptionKey skey = keyForUser(service, e3, true); |
7183 | 819 |
if (skey == null) { |
820 |
throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO |
|
821 |
} |
|
1454 | 822 |
Ticket t = new Ticket( |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
823 |
service, |
1454 | 824 |
new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET) |
825 |
); |
|
826 |
EncTGSRepPart enc_part = new EncTGSRepPart( |
|
827 |
key, |
|
828 |
new LastReq(new LastReqEntry[]{ |
|
829 |
new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000)) |
|
830 |
}), |
|
831 |
body.getNonce(), // TODO: detect replay |
|
832 |
new KerberosTime(new Date().getTime() + 1000 * 3600 * 24), |
|
833 |
// Next 5 and last MUST be same with ticket |
|
834 |
tFlags, |
|
835 |
new KerberosTime(new Date()), |
|
836 |
body.from, |
|
31643
abad00f2c027
8058290: JAAS Krb5LoginModule has suspect ticket-renewal logic, relies on clockskew grace
weijun
parents:
31429
diff
changeset
|
837 |
till, etp.renewTill, |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
838 |
service, |
30959 | 839 |
body.addresses |
1454 | 840 |
); |
841 |
EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_TGS_REP_PART_SESSKEY); |
|
842 |
TGSRep tgsRep = new TGSRep(null, |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
843 |
cname, |
1454 | 844 |
t, |
845 |
edata); |
|
846 |
System.out.println(" Return " + tgsRep.cname |
|
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
847 |
+ " ticket for " + tgsRep.ticket.sname + ", flags " |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
848 |
+ tFlags); |
1454 | 849 |
|
850 |
DerOutputStream out = new DerOutputStream(); |
|
851 |
out.write(DerValue.createTag(DerValue.TAG_APPLICATION, |
|
852 |
true, (byte)Krb5.KRB_TGS_REP), tgsRep.asn1Encode()); |
|
853 |
return out.toByteArray(); |
|
854 |
} catch (KrbException ke) { |
|
855 |
ke.printStackTrace(System.out); |
|
856 |
KRBError kerr = ke.getError(); |
|
857 |
KDCReqBody body = tgsReq.reqBody; |
|
858 |
System.out.println(" Error " + ke.returnCode() |
|
859 |
+ " " +ke.returnCodeMessage()); |
|
860 |
if (kerr == null) { |
|
861 |
kerr = new KRBError(null, null, null, |
|
862 |
new KerberosTime(new Date()), |
|
863 |
0, |
|
864 |
ke.returnCode(), |
|
13247 | 865 |
body.cname, |
866 |
service, |
|
1454 | 867 |
KrbException.errorMessage(ke.returnCode()), |
868 |
null); |
|
869 |
} |
|
870 |
return kerr.asn1Encode(); |
|
871 |
} |
|
872 |
} |
|
873 |
||
874 |
/** |
|
875 |
* Processes a AS_REQ and generates a AS_REP (or KRB_ERROR) |
|
876 |
* @param in the request |
|
877 |
* @return the response |
|
878 |
* @throws java.lang.Exception for various errors |
|
879 |
*/ |
|
21961
50019af27ca3
8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents:
18536
diff
changeset
|
880 |
protected byte[] processAsReq(byte[] in) throws Exception { |
1454 | 881 |
ASReq asReq = new ASReq(in); |
882 |
int[] eTypes = null; |
|
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7183
diff
changeset
|
883 |
List<PAData> outPAs = new ArrayList<>(); |
7183 | 884 |
|
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
885 |
PrincipalName service = asReq.reqBody.sname; |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
886 |
if (options.containsKey(KDC.Option.RESP_NT)) { |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
887 |
service = new PrincipalName(service.getNameStrings(), |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
888 |
(int)options.get(KDC.Option.RESP_NT)); |
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
889 |
} |
1454 | 890 |
try { |
891 |
System.out.println(realm + "> " + asReq.reqBody.cname + |
|
892 |
" sends AS-REQ for " + |
|
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
893 |
service + ", " + asReq.reqBody.kdcOptions); |
1454 | 894 |
|
895 |
KDCReqBody body = asReq.reqBody; |
|
896 |
||
7183 | 897 |
eTypes = KDCReqBodyDotEType(body); |
1454 | 898 |
int eType = eTypes[0]; |
899 |
||
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
900 |
// Maybe server does not support aes256, but a kinit does |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
901 |
if (!EType.isSupported(eType)) { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
902 |
if (eTypes.length < 2) { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
903 |
throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
904 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
905 |
eType = eTypes[1]; |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
906 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
907 |
|
4336 | 908 |
EncryptionKey ckey = keyForUser(body.cname, eType, false); |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
909 |
EncryptionKey skey = keyForUser(service, eType, true); |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
910 |
|
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
911 |
if (options.containsKey(KDC.Option.ONLY_RC4_TGT)) { |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
912 |
int tgtEType = EncryptedData.ETYPE_ARCFOUR_HMAC; |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
913 |
boolean found = false; |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
914 |
for (int i=0; i<eTypes.length; i++) { |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
915 |
if (eTypes[i] == tgtEType) { |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
916 |
found = true; |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
917 |
break; |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
918 |
} |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
919 |
} |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
920 |
if (!found) { |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
921 |
throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
922 |
} |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
923 |
skey = keyForUser(service, tgtEType, true); |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
924 |
} |
1454 | 925 |
if (ckey == null) { |
926 |
throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP); |
|
927 |
} |
|
928 |
if (skey == null) { |
|
929 |
throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO |
|
930 |
} |
|
931 |
||
932 |
// Session key |
|
933 |
EncryptionKey key = generateRandomKey(eType); |
|
934 |
// Check time, TODO |
|
935 |
KerberosTime till = body.till; |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
936 |
KerberosTime rtime = body.rtime; |
1454 | 937 |
if (till == null) { |
938 |
throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO |
|
939 |
} else if (till.isZero()) { |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
940 |
till = new KerberosTime( |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
941 |
new Date().getTime() + 1000 * DEFAULT_LIFETIME); |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
942 |
} |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
943 |
if (rtime == null && body.kdcOptions.get(KDCOptions.RENEWABLE)) { |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
944 |
rtime = new KerberosTime( |
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
945 |
new Date().getTime() + 1000 * DEFAULT_RENEWTIME); |
1454 | 946 |
} |
947 |
//body.from |
|
948 |
boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1]; |
|
949 |
if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) { |
|
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
950 |
List<String> sensitives = (List<String>) |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
951 |
options.get(Option.SENSITIVE_ACCOUNTS); |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
952 |
if (sensitives != null && sensitives.contains(body.cname.toString())) { |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
953 |
// Cannot make FORWARDABLE |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
954 |
} else { |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
955 |
bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true; |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
956 |
} |
1454 | 957 |
} |
958 |
if (body.kdcOptions.get(KDCOptions.RENEWABLE)) { |
|
959 |
bFlags[Krb5.TKT_OPTS_RENEWABLE] = true; |
|
960 |
//renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7); |
|
961 |
} |
|
962 |
if (body.kdcOptions.get(KDCOptions.PROXIABLE)) { |
|
963 |
bFlags[Krb5.TKT_OPTS_PROXIABLE] = true; |
|
964 |
} |
|
965 |
if (body.kdcOptions.get(KDCOptions.POSTDATED)) { |
|
966 |
bFlags[Krb5.TKT_OPTS_POSTDATED] = true; |
|
967 |
} |
|
968 |
if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) { |
|
969 |
bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true; |
|
970 |
} |
|
971 |
bFlags[Krb5.TKT_OPTS_INITIAL] = true; |
|
972 |
||
7183 | 973 |
// Creating PA-DATA |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
974 |
DerValue[] pas2 = null, pas = null; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
975 |
if (options.containsKey(KDC.Option.DUP_ETYPE)) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
976 |
int n = (Integer)options.get(KDC.Option.DUP_ETYPE); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
977 |
switch (n) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
978 |
case 1: // customer's case in 7067974 |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
979 |
pas2 = new DerValue[] { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
980 |
new DerValue(new ETypeInfo2(1, null, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
981 |
new DerValue(new ETypeInfo2(1, "", null).asn1Encode()), |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
15006
diff
changeset
|
982 |
new DerValue(new ETypeInfo2(1, realm, new byte[]{1}).asn1Encode()), |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
983 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
984 |
pas = new DerValue[] { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
985 |
new DerValue(new ETypeInfo(1, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
986 |
new DerValue(new ETypeInfo(1, "").asn1Encode()), |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
15006
diff
changeset
|
987 |
new DerValue(new ETypeInfo(1, realm).asn1Encode()), |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
988 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
989 |
break; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
990 |
case 2: // we still reject non-null s2kparams and prefer E2 over E |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
991 |
pas2 = new DerValue[] { |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
15006
diff
changeset
|
992 |
new DerValue(new ETypeInfo2(1, realm, new byte[]{1}).asn1Encode()), |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
993 |
new DerValue(new ETypeInfo2(1, null, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
994 |
new DerValue(new ETypeInfo2(1, "", null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
995 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
996 |
pas = new DerValue[] { |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
15006
diff
changeset
|
997 |
new DerValue(new ETypeInfo(1, realm).asn1Encode()), |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
998 |
new DerValue(new ETypeInfo(1, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
999 |
new DerValue(new ETypeInfo(1, "").asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1000 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1001 |
break; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1002 |
case 3: // but only E is wrong |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1003 |
pas = new DerValue[] { |
16504
1e8ff2df7152
8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents:
15006
diff
changeset
|
1004 |
new DerValue(new ETypeInfo(1, realm).asn1Encode()), |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1005 |
new DerValue(new ETypeInfo(1, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1006 |
new DerValue(new ETypeInfo(1, "").asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1007 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1008 |
break; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1009 |
case 4: // we also ignore rc4-hmac |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1010 |
pas = new DerValue[] { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1011 |
new DerValue(new ETypeInfo(23, "ANYTHING").asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1012 |
new DerValue(new ETypeInfo(1, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1013 |
new DerValue(new ETypeInfo(1, "").asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1014 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1015 |
break; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1016 |
case 5: // "" should be wrong, but we accept it now |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1017 |
// See s.s.k.internal.PAData$SaltAndParams |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1018 |
pas = new DerValue[] { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1019 |
new DerValue(new ETypeInfo(1, "").asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1020 |
new DerValue(new ETypeInfo(1, null).asn1Encode()), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1021 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1022 |
break; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1023 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1024 |
} else { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1025 |
int[] epas = eTypes; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1026 |
if (options.containsKey(KDC.Option.RC4_FIRST_PREAUTH)) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1027 |
for (int i=1; i<epas.length; i++) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1028 |
if (epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1029 |
epas[i] = epas[0]; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1030 |
epas[0] = EncryptedData.ETYPE_ARCFOUR_HMAC; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1031 |
break; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1032 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1033 |
}; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1034 |
} else if (options.containsKey(KDC.Option.ONLY_ONE_PREAUTH)) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1035 |
epas = new int[] { eTypes[0] }; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1036 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1037 |
pas2 = new DerValue[epas.length]; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1038 |
for (int i=0; i<epas.length; i++) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1039 |
pas2[i] = new DerValue(new ETypeInfo2( |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1040 |
epas[i], |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1041 |
epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC ? |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1042 |
null : getSalt(body.cname), |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1043 |
null).asn1Encode()); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1044 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1045 |
boolean allOld = true; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1046 |
for (int i: eTypes) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1047 |
if (i == EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96 || |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1048 |
i == EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1049 |
allOld = false; |
7183 | 1050 |
break; |
1051 |
} |
|
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1052 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1053 |
if (allOld) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1054 |
pas = new DerValue[epas.length]; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1055 |
for (int i=0; i<epas.length; i++) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1056 |
pas[i] = new DerValue(new ETypeInfo( |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1057 |
epas[i], |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1058 |
epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC ? |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1059 |
null : getSalt(body.cname) |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1060 |
).asn1Encode()); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1061 |
} |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1062 |
} |
7183 | 1063 |
} |
1064 |
||
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1065 |
DerOutputStream eid; |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1066 |
if (pas2 != null) { |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1067 |
eid = new DerOutputStream(); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1068 |
eid.putSequence(pas2); |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1069 |
outPAs.add(new PAData(Krb5.PA_ETYPE_INFO2, eid.toByteArray())); |
7183 | 1070 |
} |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
10141
diff
changeset
|
1071 |
if (pas != null) { |
7183 | 1072 |
eid = new DerOutputStream(); |
1073 |
eid.putSequence(pas); |
|
1074 |
outPAs.add(new PAData(Krb5.PA_ETYPE_INFO, eid.toByteArray())); |
|
1075 |
} |
|
1076 |
||
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1077 |
PAData[] inPAs = KDCReqDotPAData(asReq); |
7183 | 1078 |
if (inPAs == null || inPAs.length == 0) { |
1454 | 1079 |
Object preauth = options.get(Option.PREAUTH_REQUIRED); |
1080 |
if (preauth == null || preauth.equals(Boolean.TRUE)) { |
|
1081 |
throw new KrbException(Krb5.KDC_ERR_PREAUTH_REQUIRED); |
|
1082 |
} |
|
1083 |
} else { |
|
1084 |
try { |
|
7183 | 1085 |
EncryptedData data = newEncryptedData(new DerValue(inPAs[0].getValue())); |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
1086 |
EncryptionKey pakey = keyForUser(body.cname, data.getEType(), false); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5627
diff
changeset
|
1087 |
data.decrypt(pakey, KeyUsage.KU_PA_ENC_TS); |
1454 | 1088 |
} catch (Exception e) { |
1089 |
throw new KrbException(Krb5.KDC_ERR_PREAUTH_FAILED); |
|
1090 |
} |
|
1091 |
bFlags[Krb5.TKT_OPTS_PRE_AUTHENT] = true; |
|
1092 |
} |
|
1093 |
||
1094 |
TicketFlags tFlags = new TicketFlags(bFlags); |
|
1095 |
EncTicketPart enc = new EncTicketPart( |
|
1096 |
tFlags, |
|
1097 |
key, |
|
1098 |
body.cname, |
|
1099 |
new TransitedEncoding(1, new byte[0]), |
|
1100 |
new KerberosTime(new Date()), |
|
1101 |
body.from, |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
1102 |
till, rtime, |
1454 | 1103 |
body.addresses, |
1104 |
null); |
|
1105 |
Ticket t = new Ticket( |
|
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
1106 |
service, |
1454 | 1107 |
new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET) |
1108 |
); |
|
1109 |
EncASRepPart enc_part = new EncASRepPart( |
|
1110 |
key, |
|
1111 |
new LastReq(new LastReqEntry[]{ |
|
1112 |
new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000)) |
|
1113 |
}), |
|
1114 |
body.getNonce(), // TODO: detect replay? |
|
1115 |
new KerberosTime(new Date().getTime() + 1000 * 3600 * 24), |
|
1116 |
// Next 5 and last MUST be same with ticket |
|
1117 |
tFlags, |
|
1118 |
new KerberosTime(new Date()), |
|
1119 |
body.from, |
|
27946
9f99b93cbbb2
8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents:
24631
diff
changeset
|
1120 |
till, rtime, |
10141
664ba64d2472
7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents:
9499
diff
changeset
|
1121 |
service, |
1454 | 1122 |
body.addresses |
1123 |
); |
|
1124 |
EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_AS_REP_PART); |
|
7183 | 1125 |
ASRep asRep = new ASRep( |
1126 |
outPAs.toArray(new PAData[outPAs.size()]), |
|
1454 | 1127 |
body.cname, |
1128 |
t, |
|
1129 |
edata); |
|
1130 |
||
1131 |
System.out.println(" Return " + asRep.cname |
|
28670
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
1132 |
+ " ticket for " + asRep.ticket.sname + ", flags " |
bb9afe681988
8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents:
27946
diff
changeset
|
1133 |
+ tFlags); |
1454 | 1134 |
|
1135 |
DerOutputStream out = new DerOutputStream(); |
|
1136 |
out.write(DerValue.createTag(DerValue.TAG_APPLICATION, |
|
1137 |
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
|
1138 |
byte[] result = out.toByteArray(); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1139 |
|
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1140 |
// Added feature: |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1141 |
// 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
|
1142 |
// by the system property below. |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1143 |
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
|
1144 |
if (ccache != null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1145 |
asRep.encKDCRepPart = enc_part; |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1146 |
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
|
1147 |
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
|
1148 |
CredentialsCache cache = |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1149 |
CredentialsCache.create(asReq.reqBody.cname, ccache); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1150 |
if (cache == null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1151 |
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
|
1152 |
ccache); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1153 |
} |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1154 |
cache.update(credentials); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1155 |
cache.save(); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1156 |
} |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1157 |
|
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1456
diff
changeset
|
1158 |
return result; |
1454 | 1159 |
} catch (KrbException ke) { |
1160 |
ke.printStackTrace(System.out); |
|
1161 |
KRBError kerr = ke.getError(); |
|
1162 |
KDCReqBody body = asReq.reqBody; |
|
1163 |
System.out.println(" Error " + ke.returnCode() |
|
1164 |
+ " " +ke.returnCodeMessage()); |
|
1165 |
byte[] eData = null; |
|
1166 |
if (kerr == null) { |
|
1167 |
if (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED || |
|
1168 |
ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) { |
|
1169 |
DerOutputStream bytes = new DerOutputStream(); |
|
1170 |
bytes.write(new PAData(Krb5.PA_ENC_TIMESTAMP, new byte[0]).asn1Encode()); |
|
7183 | 1171 |
for (PAData p: outPAs) { |
1172 |
bytes.write(p.asn1Encode()); |
|
1454 | 1173 |
} |
1174 |
DerOutputStream temp = new DerOutputStream(); |
|
1175 |
temp.write(DerValue.tag_Sequence, bytes); |
|
1176 |
eData = temp.toByteArray(); |
|
1177 |
} |
|
1178 |
kerr = new KRBError(null, null, null, |
|
1179 |
new KerberosTime(new Date()), |
|
1180 |
0, |
|
1181 |
ke.returnCode(), |
|
13247 | 1182 |
body.cname, |
1183 |
service, |
|
1454 | 1184 |
KrbException.errorMessage(ke.returnCode()), |
1185 |
eData); |
|
1186 |
} |
|
1187 |
return kerr.asn1Encode(); |
|
1188 |
} |
|
1189 |
} |
|
1190 |
||
1191 |
/** |
|
1192 |
* Generates a line for a KDC to put inside [realms] of krb5.conf |
|
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1193 |
* @return REALM.NAME = { kdc = host:port etc } |
1454 | 1194 |
*/ |
24631
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1195 |
private String realmLine() { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1196 |
StringBuilder sb = new StringBuilder(); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1197 |
sb.append(realm).append(" = {\n kdc = ") |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1198 |
.append(kdc).append(':').append(port).append('\n'); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1199 |
for (String s: conf) { |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1200 |
sb.append(" ").append(s).append('\n'); |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1201 |
} |
dafd257bc7f3
8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents:
23010
diff
changeset
|
1202 |
return sb.append("}\n").toString(); |
1454 | 1203 |
} |
1204 |
||
1205 |
/** |
|
1206 |
* Start the KDC service. This server listens on both UDP and TCP using |
|
1207 |
* the same port number. It uses three threads to deal with requests. |
|
1208 |
* They can be set to daemon threads if requested. |
|
1209 |
* @param port the port number to listen to. If zero, a random available |
|
1210 |
* port no less than 8000 will be chosen and used. |
|
1211 |
* @param asDaemon true if the KDC threads should be daemons |
|
1212 |
* @throws java.io.IOException for any communication error |
|
1213 |
*/ |
|
1214 |
protected void startServer(int port, boolean asDaemon) throws IOException { |
|
1215 |
if (port > 0) { |
|
1216 |
u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1")); |
|
1217 |
t1 = new ServerSocket(port); |
|
1218 |
} else { |
|
1219 |
while (true) { |
|
1220 |
// Try to find a port number that's both TCP and UDP free |
|
1221 |
try { |
|
1222 |
port = 8000 + new java.util.Random().nextInt(10000); |
|
1223 |
u1 = null; |
|
1224 |
u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1")); |
|
1225 |
t1 = new ServerSocket(port); |
|
1226 |
break; |
|
1227 |
} catch (Exception e) { |
|
1228 |
if (u1 != null) u1.close(); |
|
1229 |
} |
|
1230 |
} |
|
1231 |
} |
|
1232 |
final DatagramSocket udp = u1; |
|
1233 |
final ServerSocket tcp = t1; |
|
1234 |
System.out.println("Start KDC on " + port); |
|
1235 |
||
1236 |
this.port = port; |
|
1237 |
||
1238 |
// The UDP consumer |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1239 |
thread1 = new Thread() { |
1454 | 1240 |
public void run() { |
30038
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1241 |
udpConsumerReady = true; |
1454 | 1242 |
while (true) { |
1243 |
try { |
|
1244 |
byte[] inbuf = new byte[8192]; |
|
1245 |
DatagramPacket p = new DatagramPacket(inbuf, inbuf.length); |
|
1246 |
udp.receive(p); |
|
1247 |
System.out.println("-----------------------------------------------"); |
|
1248 |
System.out.println(">>>>> UDP packet received"); |
|
1249 |
q.put(new Job(processMessage(Arrays.copyOf(inbuf, p.getLength())), udp, p)); |
|
1250 |
} catch (Exception e) { |
|
1251 |
e.printStackTrace(); |
|
1252 |
} |
|
1253 |
} |
|
1254 |
} |
|
1255 |
}; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1256 |
thread1.setDaemon(asDaemon); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1257 |
thread1.start(); |
1454 | 1258 |
|
1259 |
// The TCP consumer |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1260 |
thread2 = new Thread() { |
1454 | 1261 |
public void run() { |
30038
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1262 |
tcpConsumerReady = true; |
1454 | 1263 |
while (true) { |
1264 |
try { |
|
1265 |
Socket socket = tcp.accept(); |
|
1266 |
System.out.println("-----------------------------------------------"); |
|
1267 |
System.out.println(">>>>> TCP connection established"); |
|
1268 |
DataInputStream in = new DataInputStream(socket.getInputStream()); |
|
1269 |
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); |
|
1270 |
byte[] token = new byte[in.readInt()]; |
|
1271 |
in.readFully(token); |
|
1272 |
q.put(new Job(processMessage(token), socket, out)); |
|
1273 |
} catch (Exception e) { |
|
1274 |
e.printStackTrace(); |
|
1275 |
} |
|
1276 |
} |
|
1277 |
} |
|
1278 |
}; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1279 |
thread2.setDaemon(asDaemon); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1280 |
thread2.start(); |
1454 | 1281 |
|
1282 |
// The dispatcher |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1283 |
thread3 = new Thread() { |
1454 | 1284 |
public void run() { |
30038
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1285 |
dispatcherReady = true; |
1454 | 1286 |
while (true) { |
1287 |
try { |
|
1288 |
q.take().send(); |
|
1289 |
} catch (Exception e) { |
|
1290 |
} |
|
1291 |
} |
|
1292 |
} |
|
1293 |
}; |
|
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1294 |
thread3.setDaemon(true); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1295 |
thread3.start(); |
30038
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1296 |
|
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1297 |
// wait for the KDC is ready |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1298 |
try { |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1299 |
while (!isReady()) { |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1300 |
Thread.sleep(100); |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1301 |
} |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1302 |
} catch(InterruptedException e) { |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1303 |
throw new IOException(e); |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1304 |
} |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1305 |
} |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1306 |
|
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1307 |
boolean isReady() { |
632939157af2
8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents:
28670
diff
changeset
|
1308 |
return udpConsumerReady && tcpConsumerReady && dispatcherReady; |
1454 | 1309 |
} |
1310 |
||
4531
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1311 |
public void terminate() { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1312 |
try { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1313 |
thread1.stop(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1314 |
thread2.stop(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1315 |
thread3.stop(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1316 |
u1.close(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1317 |
t1.close(); |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1318 |
} catch (Exception e) { |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1319 |
// OK |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1320 |
} |
3a9206343ab2
6843127: krb5 should not try to access unavailable kdc too often
weijun
parents:
4336
diff
changeset
|
1321 |
} |
31429
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1322 |
|
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1323 |
public static void startKDC(final String host, final String krbConfFileName, |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1324 |
final String realm, final Map<String, String> principals, |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1325 |
final String ktab, final KtabMode mode) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1326 |
|
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1327 |
try { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1328 |
KDC kdc = KDC.create(realm, host, 0, true); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1329 |
kdc.setOption(KDC.Option.PREAUTH_REQUIRED, Boolean.FALSE); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1330 |
KDC.saveConfig(krbConfFileName, kdc); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1331 |
|
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1332 |
// Add principals |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1333 |
if (principals != null) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1334 |
principals.forEach((name, password) -> { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1335 |
if (password == null || password.isEmpty()) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1336 |
System.out.println(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1337 |
"KDC:add a principal '%s' with a random " + |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1338 |
"password", name)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1339 |
kdc.addPrincipalRandKey(name); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1340 |
} else { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1341 |
System.out.println(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1342 |
"KDC:add a principal '%s' with '%s' password", |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1343 |
name, password)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1344 |
kdc.addPrincipal(name, password.toCharArray()); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1345 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1346 |
}); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1347 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1348 |
|
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1349 |
// Create or append keys to existing keytab file |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1350 |
if (ktab != null) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1351 |
File ktabFile = new File(ktab); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1352 |
switch(mode) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1353 |
case APPEND: |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1354 |
if (ktabFile.exists()) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1355 |
System.out.println(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1356 |
"KDC:append keys to an exising keytab " |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1357 |
+ "file %s", ktab)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1358 |
kdc.appendKtab(ktab); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1359 |
} else { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1360 |
System.out.println(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1361 |
"KDC:create a new keytab file %s", ktab)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1362 |
kdc.writeKtab(ktab); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1363 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1364 |
break; |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1365 |
case EXISTING: |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1366 |
System.out.println(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1367 |
"KDC:use an existing keytab file %s", ktab)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1368 |
break; |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1369 |
default: |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1370 |
throw new RuntimeException(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1371 |
"KDC:unsupported keytab mode: %s", mode)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1372 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1373 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1374 |
|
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1375 |
System.out.println(String.format( |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1376 |
"KDC: started on %s:%s with '%s' realm", |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1377 |
host, kdc.getPort(), realm)); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1378 |
} catch (Exception e) { |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1379 |
throw new RuntimeException("KDC: unexpected exception", e); |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1380 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1381 |
|
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1382 |
} |
ce4193650b40
8075301: Tests for sun.security.krb5.principal system property
weijun
parents:
30959
diff
changeset
|
1383 |
|
1454 | 1384 |
/** |
1385 |
* Helper class to encapsulate a job in a KDC. |
|
1386 |
*/ |
|
1387 |
private static class Job { |
|
1388 |
byte[] token; // The received request at creation time and |
|
1389 |
// the response at send time |
|
1390 |
Socket s; // The TCP socket from where the request comes |
|
1391 |
DataOutputStream out; // The OutputStream of the TCP socket |
|
1392 |
DatagramSocket s2; // The UDP socket from where the request comes |
|
1393 |
DatagramPacket dp; // The incoming UDP datagram packet |
|
1394 |
boolean useTCP; // Whether TCP or UDP is used |
|
1395 |
||
1396 |
// Creates a job object for TCP |
|
1397 |
Job(byte[] token, Socket s, DataOutputStream out) { |
|
1398 |
useTCP = true; |
|
1399 |
this.token = token; |
|
1400 |
this.s = s; |
|
1401 |
this.out = out; |
|
1402 |
} |
|
1403 |
||
1404 |
// Creates a job object for UDP |
|
1405 |
Job(byte[] token, DatagramSocket s2, DatagramPacket dp) { |
|
1406 |
useTCP = false; |
|
1407 |
this.token = token; |
|
1408 |
this.s2 = s2; |
|
1409 |
this.dp = dp; |
|
1410 |
} |
|
1411 |
||
1412 |
// Sends the output back to the client |
|
1413 |
void send() { |
|
1414 |
try { |
|
1415 |
if (useTCP) { |
|
1416 |
System.out.println(">>>>> TCP request honored"); |
|
1417 |
out.writeInt(token.length); |
|
1418 |
out.write(token); |
|
1419 |
s.close(); |
|
1420 |
} else { |
|
1421 |
System.out.println(">>>>> UDP request honored"); |
|
1422 |
s2.send(new DatagramPacket(token, token.length, dp.getAddress(), dp.getPort())); |
|
1423 |
} |
|
1424 |
} catch (Exception e) { |
|
1425 |
e.printStackTrace(); |
|
1426 |
} |
|
1427 |
} |
|
1428 |
} |
|
3046 | 1429 |
|
1430 |
public static class KDCNameService implements NameServiceDescriptor { |
|
1431 |
@Override |
|
1432 |
public NameService createNameService() throws Exception { |
|
1433 |
NameService ns = new NameService() { |
|
1434 |
@Override |
|
1435 |
public InetAddress[] lookupAllHostAddr(String host) |
|
1436 |
throws UnknownHostException { |
|
1437 |
// Everything is localhost |
|
1438 |
return new InetAddress[]{ |
|
1439 |
InetAddress.getByAddress(host, new byte[]{127,0,0,1}) |
|
1440 |
}; |
|
1441 |
} |
|
1442 |
@Override |
|
1443 |
public String getHostByAddr(byte[] addr) |
|
1444 |
throws UnknownHostException { |
|
1445 |
// No reverse lookup, PrincipalName use original string |
|
1446 |
throw new UnknownHostException(); |
|
1447 |
} |
|
1448 |
}; |
|
1449 |
return ns; |
|
1450 |
} |
|
1451 |
||
1452 |
@Override |
|
1453 |
public String getProviderName() { |
|
1454 |
return "mock"; |
|
1455 |
} |
|
1456 |
||
1457 |
@Override |
|
1458 |
public String getType() { |
|
1459 |
return "ns"; |
|
1460 |
} |
|
1461 |
} |
|
7183 | 1462 |
|
1463 |
// Calling private methods thru reflections |
|
1464 |
private static final Field getPADataField; |
|
1465 |
private static final Field getEType; |
|
1466 |
private static final Constructor<EncryptedData> ctorEncryptedData; |
|
1467 |
private static final Method stringToKey; |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1468 |
private static final Field getAddlTkt; |
7183 | 1469 |
|
1470 |
static { |
|
1471 |
try { |
|
1472 |
ctorEncryptedData = EncryptedData.class.getDeclaredConstructor(DerValue.class); |
|
1473 |
ctorEncryptedData.setAccessible(true); |
|
1474 |
getPADataField = KDCReq.class.getDeclaredField("pAData"); |
|
1475 |
getPADataField.setAccessible(true); |
|
1476 |
getEType = KDCReqBody.class.getDeclaredField("eType"); |
|
1477 |
getEType.setAccessible(true); |
|
1478 |
stringToKey = EncryptionKey.class.getDeclaredMethod( |
|
1479 |
"stringToKey", |
|
1480 |
char[].class, String.class, byte[].class, Integer.TYPE); |
|
1481 |
stringToKey.setAccessible(true); |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1482 |
getAddlTkt = KDCReqBody.class.getDeclaredField("additionalTickets"); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1483 |
getAddlTkt.setAccessible(true); |
7183 | 1484 |
} catch (NoSuchFieldException nsfe) { |
1485 |
throw new AssertionError(nsfe); |
|
1486 |
} catch (NoSuchMethodException nsme) { |
|
1487 |
throw new AssertionError(nsme); |
|
1488 |
} |
|
1489 |
} |
|
1490 |
private EncryptedData newEncryptedData(DerValue der) { |
|
1491 |
try { |
|
1492 |
return ctorEncryptedData.newInstance(der); |
|
1493 |
} catch (Exception e) { |
|
1494 |
throw new AssertionError(e); |
|
1495 |
} |
|
1496 |
} |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1497 |
private static PAData[] KDCReqDotPAData(KDCReq req) { |
7183 | 1498 |
try { |
1499 |
return (PAData[])getPADataField.get(req); |
|
1500 |
} catch (Exception e) { |
|
1501 |
throw new AssertionError(e); |
|
1502 |
} |
|
1503 |
} |
|
1504 |
private static int[] KDCReqBodyDotEType(KDCReqBody body) { |
|
1505 |
try { |
|
1506 |
return (int[]) getEType.get(body); |
|
1507 |
} catch (Exception e) { |
|
1508 |
throw new AssertionError(e); |
|
1509 |
} |
|
1510 |
} |
|
1511 |
private static byte[] EncryptionKeyDotStringToKey(char[] password, String salt, |
|
1512 |
byte[] s2kparams, int keyType) throws KrbCryptoException { |
|
1513 |
try { |
|
1514 |
return (byte[])stringToKey.invoke( |
|
1515 |
null, password, salt, s2kparams, keyType); |
|
1516 |
} catch (InvocationTargetException ex) { |
|
1517 |
throw (KrbCryptoException)ex.getCause(); |
|
1518 |
} catch (Exception e) { |
|
1519 |
throw new AssertionError(e); |
|
1520 |
} |
|
1521 |
} |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1522 |
private static Ticket KDCReqBodyDotFirstAdditionalTicket(KDCReqBody body) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1523 |
try { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1524 |
return ((Ticket[])getAddlTkt.get(body))[0]; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1525 |
} catch (Exception e) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1526 |
throw new AssertionError(e); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1527 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
13247
diff
changeset
|
1528 |
} |
1454 | 1529 |
} |