author | weijun |
Fri, 27 Nov 2009 08:51:28 +0800 | |
changeset 4336 | 4c792c19266e |
parent 3483 | a16fce1820ef |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
1454 | 1 |
/* |
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
2 |
* Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
1454 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
import com.sun.security.auth.module.Krb5LoginModule; |
|
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
25 |
import java.security.Key; |
1454 | 26 |
import java.security.PrivilegedActionException; |
27 |
import java.security.PrivilegedExceptionAction; |
|
28 |
import java.util.Arrays; |
|
29 |
import java.util.HashMap; |
|
30 |
import java.util.Map; |
|
31 |
import javax.security.auth.Subject; |
|
32 |
import javax.security.auth.kerberos.KerberosKey; |
|
33 |
import javax.security.auth.kerberos.KerberosTicket; |
|
34 |
import javax.security.auth.login.LoginContext; |
|
35 |
import org.ietf.jgss.GSSContext; |
|
36 |
import org.ietf.jgss.GSSCredential; |
|
37 |
import org.ietf.jgss.GSSException; |
|
38 |
import org.ietf.jgss.GSSManager; |
|
39 |
import org.ietf.jgss.GSSName; |
|
40 |
import org.ietf.jgss.MessageProp; |
|
41 |
import org.ietf.jgss.Oid; |
|
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
42 |
import com.sun.security.jgss.ExtendedGSSContext; |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
43 |
import com.sun.security.jgss.InquireType; |
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
44 |
import com.sun.security.jgss.AuthorizationDataEntry; |
1454 | 45 |
|
46 |
/** |
|
47 |
* Context of a JGSS subject, encapsulating Subject and GSSContext. |
|
48 |
* |
|
49 |
* Three "constructors", which acquire the (private) credentials and fill |
|
50 |
* it into the Subject: |
|
51 |
* |
|
52 |
* 1. static fromJAAS(): Creates a Context using a JAAS login config entry |
|
53 |
* 2. static fromUserPass(): Creates a Context using a username and a password |
|
54 |
* 3. delegated(): A new context which uses the delegated credentials from a |
|
55 |
* previously established acceptor Context |
|
56 |
* |
|
57 |
* Two context initiators, which create the GSSContext object inside: |
|
58 |
* |
|
59 |
* 1. startAsClient() |
|
60 |
* 2. startAsServer() |
|
61 |
* |
|
62 |
* Privileged action: |
|
63 |
* doAs(): Performs an action in the name of the Subject |
|
64 |
* |
|
65 |
* Handshake process: |
|
66 |
* static handShake(initiator, acceptor) |
|
67 |
* |
|
68 |
* A four-phase typical data communication which includes all four GSS |
|
69 |
* actions (wrap, unwrap, getMic and veryfyMiC): |
|
70 |
* static transmit(message, from, to) |
|
71 |
*/ |
|
72 |
public class Context { |
|
73 |
||
74 |
private Subject s; |
|
4336 | 75 |
private ExtendedGSSContext x; |
1454 | 76 |
private boolean f; // context established? |
77 |
private String name; |
|
78 |
private GSSCredential cred; // see static method delegated(). |
|
79 |
||
80 |
private Context() {} |
|
81 |
||
82 |
/** |
|
83 |
* Using the delegated credentials from a previous acceptor |
|
84 |
* @param c |
|
85 |
*/ |
|
86 |
public Context delegated() throws Exception { |
|
87 |
Context out = new Context(); |
|
88 |
out.s = s; |
|
89 |
out.cred = x.getDelegCred(); |
|
90 |
out.name = name + " as " + out.cred.getName().toString(); |
|
91 |
return out; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Logins with a JAAS login config entry name |
|
96 |
*/ |
|
97 |
public static Context fromJAAS(final String name) throws Exception { |
|
98 |
Context out = new Context(); |
|
99 |
out.name = name; |
|
100 |
LoginContext lc = new LoginContext(name); |
|
101 |
lc.login(); |
|
102 |
out.s = lc.getSubject(); |
|
103 |
return out; |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Logins with a username and a password, using Krb5LoginModule directly |
|
108 |
* @param storeKey true if key should be saved, used on acceptor side |
|
109 |
*/ |
|
110 |
public static Context fromUserPass(String user, char[] pass, boolean storeKey) throws Exception { |
|
111 |
Context out = new Context(); |
|
112 |
out.name = user; |
|
113 |
out.s = new Subject(); |
|
114 |
Krb5LoginModule krb5 = new Krb5LoginModule(); |
|
115 |
Map<String, String> map = new HashMap<String, String>(); |
|
1575
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
116 |
Map<String, Object> shared = new HashMap<String, Object>(); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
117 |
|
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
118 |
if (pass != null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
119 |
map.put("useFirstPass", "true"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
120 |
shared.put("javax.security.auth.login.name", user); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
121 |
shared.put("javax.security.auth.login.password", pass); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
122 |
} else { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
123 |
map.put("doNotPrompt", "true"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
124 |
map.put("useTicketCache", "true"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
125 |
if (user != null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
126 |
map.put("principal", user); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
127 |
} |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
128 |
} |
1454 | 129 |
if (storeKey) { |
130 |
map.put("storeKey", "true"); |
|
131 |
} |
|
132 |
||
133 |
krb5.initialize(out.s, null, shared, map); |
|
134 |
krb5.login(); |
|
135 |
krb5.commit(); |
|
136 |
return out; |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Starts as a client |
|
141 |
* @param target communication peer |
|
142 |
* @param mech GSS mech |
|
143 |
* @throws java.lang.Exception |
|
144 |
*/ |
|
145 |
public void startAsClient(final String target, final Oid mech) throws Exception { |
|
146 |
doAs(new Action() { |
|
147 |
@Override |
|
148 |
public byte[] run(Context me, byte[] dummy) throws Exception { |
|
149 |
GSSManager m = GSSManager.getInstance(); |
|
4336 | 150 |
me.x = (ExtendedGSSContext)m.createContext( |
151 |
target.indexOf('@') < 0 ? |
|
1454 | 152 |
m.createName(target, null) : |
153 |
m.createName(target, GSSName.NT_HOSTBASED_SERVICE), |
|
154 |
mech, |
|
155 |
cred, |
|
156 |
GSSContext.DEFAULT_LIFETIME); |
|
157 |
return null; |
|
158 |
} |
|
159 |
}, null); |
|
160 |
f = false; |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Starts as a server |
|
165 |
* @param mech GSS mech |
|
166 |
* @throws java.lang.Exception |
|
167 |
*/ |
|
168 |
public void startAsServer(final Oid mech) throws Exception { |
|
169 |
doAs(new Action() { |
|
170 |
@Override |
|
171 |
public byte[] run(Context me, byte[] dummy) throws Exception { |
|
172 |
GSSManager m = GSSManager.getInstance(); |
|
4336 | 173 |
me.x = (ExtendedGSSContext)m.createContext(m.createCredential( |
1454 | 174 |
null, |
175 |
GSSCredential.INDEFINITE_LIFETIME, |
|
176 |
mech, |
|
177 |
GSSCredential.ACCEPT_ONLY)); |
|
178 |
return null; |
|
179 |
} |
|
180 |
}, null); |
|
181 |
f = false; |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Accesses the internal GSSContext object. Currently it's used for -- |
|
186 |
* |
|
187 |
* 1. calling requestXXX() before handshake |
|
188 |
* 2. accessing source name |
|
189 |
* |
|
190 |
* Note: If the application needs to do any privileged call on this |
|
191 |
* object, please use doAs(). Otherwise, it can be done directly. The |
|
192 |
* methods listed above are all non-privileged calls. |
|
193 |
* |
|
194 |
* @return the GSSContext object |
|
195 |
*/ |
|
4336 | 196 |
public ExtendedGSSContext x() { |
1454 | 197 |
return x; |
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* Disposes the GSSContext within |
|
202 |
* @throws org.ietf.jgss.GSSException |
|
203 |
*/ |
|
204 |
public void dispose() throws GSSException { |
|
205 |
x.dispose(); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Does something using the Subject inside |
|
210 |
* @param action the action |
|
211 |
* @param in the input byte |
|
212 |
* @return the output byte |
|
213 |
* @throws java.lang.Exception |
|
214 |
*/ |
|
215 |
public byte[] doAs(final Action action, final byte[] in) throws Exception { |
|
216 |
try { |
|
217 |
return Subject.doAs(s, new PrivilegedExceptionAction<byte[]>() { |
|
218 |
||
219 |
@Override |
|
220 |
public byte[] run() throws Exception { |
|
221 |
return action.run(Context.this, in); |
|
222 |
} |
|
223 |
}); |
|
224 |
} catch (PrivilegedActionException pae) { |
|
225 |
throw pae.getException(); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* Prints status of GSSContext and Subject |
|
231 |
* @throws java.lang.Exception |
|
232 |
*/ |
|
233 |
public void status() throws Exception { |
|
234 |
System.out.println("STATUS OF " + name.toUpperCase()); |
|
235 |
try { |
|
236 |
StringBuffer sb = new StringBuffer(); |
|
237 |
if (x.getAnonymityState()) { |
|
238 |
sb.append("anon, "); |
|
239 |
} |
|
240 |
if (x.getConfState()) { |
|
241 |
sb.append("conf, "); |
|
242 |
} |
|
243 |
if (x.getCredDelegState()) { |
|
244 |
sb.append("deleg, "); |
|
245 |
} |
|
246 |
if (x.getIntegState()) { |
|
247 |
sb.append("integ, "); |
|
248 |
} |
|
249 |
if (x.getMutualAuthState()) { |
|
250 |
sb.append("mutual, "); |
|
251 |
} |
|
252 |
if (x.getReplayDetState()) { |
|
253 |
sb.append("rep det, "); |
|
254 |
} |
|
255 |
if (x.getSequenceDetState()) { |
|
256 |
sb.append("seq det, "); |
|
257 |
} |
|
4336 | 258 |
if (x instanceof ExtendedGSSContext) { |
259 |
if (((ExtendedGSSContext)x).getDelegPolicyState()) { |
|
260 |
sb.append("deleg policy, "); |
|
261 |
} |
|
262 |
} |
|
1454 | 263 |
System.out.println("Context status of " + name + ": " + sb.toString()); |
264 |
System.out.println(x.getSrcName() + " -> " + x.getTargName()); |
|
265 |
} catch (Exception e) { |
|
266 |
;// Don't care |
|
267 |
} |
|
268 |
System.out.println("====================================="); |
|
269 |
for (Object o : s.getPrivateCredentials()) { |
|
270 |
System.out.println(" " + o.getClass()); |
|
271 |
if (o instanceof KerberosTicket) { |
|
272 |
KerberosTicket kt = (KerberosTicket) o; |
|
273 |
System.out.println(" " + kt.getServer() + " for " + kt.getClient()); |
|
274 |
} else if (o instanceof KerberosKey) { |
|
275 |
KerberosKey kk = (KerberosKey) o; |
|
276 |
System.out.print(" " + kk.getKeyType() + " " + kk.getVersionNumber() + " " + kk.getAlgorithm() + " "); |
|
277 |
for (byte b : kk.getEncoded()) { |
|
278 |
System.out.printf("%02X", b & 0xff); |
|
279 |
} |
|
280 |
System.out.println(); |
|
281 |
} else if (o instanceof Map) { |
|
282 |
Map map = (Map) o; |
|
283 |
for (Object k : map.keySet()) { |
|
284 |
System.out.println(" " + k + ": " + map.get(k)); |
|
285 |
} |
|
286 |
} |
|
287 |
} |
|
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
288 |
if (x != null && x instanceof ExtendedGSSContext) { |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
289 |
if (x.isEstablished()) { |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
290 |
ExtendedGSSContext ex = (ExtendedGSSContext)x; |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
291 |
Key k = (Key)ex.inquireSecContext( |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
292 |
InquireType.KRB5_GET_SESSION_KEY); |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
293 |
if (k == null) { |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
294 |
throw new Exception("Session key cannot be null"); |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
295 |
} |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
296 |
System.out.println("Session key is: " + k); |
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
297 |
boolean[] flags = (boolean[])ex.inquireSecContext( |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
298 |
InquireType.KRB5_GET_TKT_FLAGS); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
299 |
if (flags == null) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
300 |
throw new Exception("Ticket flags cannot be null"); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
301 |
} |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
302 |
System.out.println("Ticket flags is: " + Arrays.toString(flags)); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
303 |
String authTime = (String)ex.inquireSecContext( |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
304 |
InquireType.KRB5_GET_AUTHTIME); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
305 |
if (authTime == null) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
306 |
throw new Exception("Auth time cannot be null"); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
307 |
} |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
308 |
System.out.println("AuthTime is: " + authTime); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
309 |
if (!x.isInitiator()) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
310 |
AuthorizationDataEntry[] ad = (AuthorizationDataEntry[])ex.inquireSecContext( |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
311 |
InquireType.KRB5_GET_AUTHZ_DATA); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
312 |
System.out.println("AuthzData is: " + Arrays.toString(ad)); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
313 |
} |
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
314 |
} |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
315 |
} |
1454 | 316 |
} |
317 |
||
318 |
/** |
|
319 |
* Transmits a message from one Context to another. The sender wraps the |
|
320 |
* message and sends it to the receiver. The receiver unwraps it, creates |
|
321 |
* a MIC of the clear text and sends it back to the sender. The sender |
|
322 |
* verifies the MIC against the message sent earlier. |
|
323 |
* @param message the message |
|
324 |
* @param s1 the sender |
|
325 |
* @param s2 the receiver |
|
326 |
* @throws java.lang.Exception If anything goes wrong |
|
327 |
*/ |
|
328 |
static public void transmit(final String message, final Context s1, |
|
329 |
final Context s2) throws Exception { |
|
330 |
final byte[] messageBytes = message.getBytes(); |
|
331 |
System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n", |
|
332 |
s1.name, s2.name); |
|
333 |
||
334 |
byte[] t = s1.doAs(new Action() { |
|
335 |
@Override |
|
336 |
public byte[] run(Context me, byte[] dummy) throws Exception { |
|
337 |
System.out.println("wrap"); |
|
338 |
MessageProp p1 = new MessageProp(0, true); |
|
339 |
byte[] out = me.x.wrap(messageBytes, 0, messageBytes.length, p1); |
|
340 |
System.out.println(printProp(p1)); |
|
341 |
return out; |
|
342 |
} |
|
343 |
}, null); |
|
344 |
||
345 |
t = s2.doAs(new Action() { |
|
346 |
@Override |
|
347 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
348 |
MessageProp p1 = new MessageProp(0, true); |
|
349 |
byte[] bytes = me.x.unwrap(input, 0, input.length, p1); |
|
350 |
if (!Arrays.equals(messageBytes, bytes)) |
|
351 |
throw new Exception("wrap/unwrap mismatch"); |
|
352 |
System.out.println("unwrap"); |
|
353 |
System.out.println(printProp(p1)); |
|
354 |
p1 = new MessageProp(0, true); |
|
355 |
System.out.println("getMIC"); |
|
356 |
bytes = me.x.getMIC(bytes, 0, bytes.length, p1); |
|
357 |
System.out.println(printProp(p1)); |
|
358 |
return bytes; |
|
359 |
} |
|
360 |
}, t); |
|
361 |
// Re-unwrap should make p2.isDuplicateToken() returns true |
|
362 |
s1.doAs(new Action() { |
|
363 |
@Override |
|
364 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
365 |
MessageProp p1 = new MessageProp(0, true); |
|
366 |
System.out.println("verifyMIC"); |
|
367 |
me.x.verifyMIC(input, 0, input.length, |
|
368 |
messageBytes, 0, messageBytes.length, |
|
369 |
p1); |
|
370 |
System.out.println(printProp(p1)); |
|
371 |
return null; |
|
372 |
} |
|
373 |
}, t); |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Returns a string description of a MessageProp object |
|
378 |
* @param prop the object |
|
379 |
* @return the description |
|
380 |
*/ |
|
381 |
static public String printProp(MessageProp prop) { |
|
382 |
StringBuffer sb = new StringBuffer(); |
|
383 |
sb.append("MessagePop: "); |
|
384 |
sb.append("QOP="+ prop.getQOP() + ", "); |
|
385 |
sb.append(prop.getPrivacy()?"privacy, ":""); |
|
386 |
sb.append(prop.isDuplicateToken()?"dup, ":""); |
|
387 |
sb.append(prop.isGapToken()?"gap, ":""); |
|
388 |
sb.append(prop.isOldToken()?"old, ":""); |
|
389 |
sb.append(prop.isUnseqToken()?"unseq, ":""); |
|
390 |
sb.append(prop.getMinorString()+ "(" + prop.getMinorStatus()+")"); |
|
391 |
return sb.toString(); |
|
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* Handshake (security context establishment process) between two Contexts |
|
396 |
* @param c the initiator |
|
397 |
* @param s the acceptor |
|
398 |
* @throws java.lang.Exception |
|
399 |
*/ |
|
400 |
static public void handshake(final Context c, final Context s) throws Exception { |
|
401 |
byte[] t = new byte[0]; |
|
402 |
while (!c.f || !s.f) { |
|
403 |
t = c.doAs(new Action() { |
|
404 |
@Override |
|
405 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
406 |
if (me.x.isEstablished()) { |
|
407 |
me.f = true; |
|
408 |
System.out.println(c.name + " side established"); |
|
1574 | 409 |
if (input != null) { |
410 |
throw new Exception("Context established but " + |
|
411 |
"still receive token at " + c.name); |
|
412 |
} |
|
1454 | 413 |
return null; |
414 |
} else { |
|
415 |
System.out.println(c.name + " call initSecContext"); |
|
416 |
return me.x.initSecContext(input, 0, input.length); |
|
417 |
} |
|
418 |
} |
|
419 |
}, t); |
|
420 |
||
421 |
t = s.doAs(new Action() { |
|
422 |
@Override |
|
423 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
424 |
if (me.x.isEstablished()) { |
|
425 |
me.f = true; |
|
426 |
System.out.println(s.name + " side established"); |
|
1574 | 427 |
if (input != null) { |
428 |
throw new Exception("Context established but " + |
|
429 |
"still receive token at " + s.name); |
|
430 |
} |
|
1454 | 431 |
return null; |
432 |
} else { |
|
433 |
System.out.println(s.name + " called acceptSecContext"); |
|
434 |
return me.x.acceptSecContext(input, 0, input.length); |
|
435 |
} |
|
436 |
} |
|
437 |
}, t); |
|
438 |
} |
|
439 |
} |
|
440 |
} |