author | stefank |
Mon, 25 Aug 2014 09:10:13 +0200 | |
changeset 26314 | f8bc1966fb30 |
parent 25661 | 929c829a8400 |
child 26629 | 3b9ed8175488 |
permissions | -rw-r--r-- |
1454 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18536
diff
changeset
|
2 |
* Copyright (c) 2008, 2013, 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 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; |
|
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
29 |
import java.util.Base64; |
1454 | 30 |
import java.util.HashMap; |
31 |
import java.util.Map; |
|
32 |
import javax.security.auth.Subject; |
|
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
33 |
import javax.security.auth.kerberos.KerberosCredMessage; |
1454 | 34 |
import javax.security.auth.kerberos.KerberosKey; |
35 |
import javax.security.auth.kerberos.KerberosTicket; |
|
36 |
import javax.security.auth.login.LoginContext; |
|
37 |
import org.ietf.jgss.GSSContext; |
|
38 |
import org.ietf.jgss.GSSCredential; |
|
39 |
import org.ietf.jgss.GSSException; |
|
40 |
import org.ietf.jgss.GSSManager; |
|
41 |
import org.ietf.jgss.GSSName; |
|
42 |
import org.ietf.jgss.MessageProp; |
|
43 |
import org.ietf.jgss.Oid; |
|
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
44 |
import com.sun.security.jgss.ExtendedGSSContext; |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
45 |
import com.sun.security.jgss.InquireType; |
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
46 |
import com.sun.security.jgss.AuthorizationDataEntry; |
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
47 |
import com.sun.security.jgss.ExtendedGSSCredential; |
7801
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
48 |
import java.io.ByteArrayInputStream; |
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
49 |
import java.io.ByteArrayOutputStream; |
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
50 |
import java.security.Principal; |
1454 | 51 |
|
52 |
/** |
|
53 |
* Context of a JGSS subject, encapsulating Subject and GSSContext. |
|
54 |
* |
|
55 |
* Three "constructors", which acquire the (private) credentials and fill |
|
56 |
* it into the Subject: |
|
57 |
* |
|
58 |
* 1. static fromJAAS(): Creates a Context using a JAAS login config entry |
|
59 |
* 2. static fromUserPass(): Creates a Context using a username and a password |
|
60 |
* 3. delegated(): A new context which uses the delegated credentials from a |
|
61 |
* previously established acceptor Context |
|
62 |
* |
|
63 |
* Two context initiators, which create the GSSContext object inside: |
|
64 |
* |
|
65 |
* 1. startAsClient() |
|
66 |
* 2. startAsServer() |
|
67 |
* |
|
68 |
* Privileged action: |
|
69 |
* doAs(): Performs an action in the name of the Subject |
|
70 |
* |
|
71 |
* Handshake process: |
|
72 |
* static handShake(initiator, acceptor) |
|
73 |
* |
|
74 |
* A four-phase typical data communication which includes all four GSS |
|
75 |
* actions (wrap, unwrap, getMic and veryfyMiC): |
|
76 |
* static transmit(message, from, to) |
|
77 |
*/ |
|
78 |
public class Context { |
|
79 |
||
80 |
private Subject s; |
|
4336 | 81 |
private ExtendedGSSContext x; |
1454 | 82 |
private String name; |
83 |
private GSSCredential cred; // see static method delegated(). |
|
84 |
||
7801
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
85 |
static boolean usingStream = false; |
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
86 |
|
1454 | 87 |
private Context() {} |
88 |
||
89 |
/** |
|
90 |
* Using the delegated credentials from a previous acceptor |
|
91 |
*/ |
|
92 |
public Context delegated() throws Exception { |
|
93 |
Context out = new Context(); |
|
94 |
out.s = s; |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
95 |
try { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
96 |
out.cred = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
97 |
@Override |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
98 |
public GSSCredential run() throws Exception { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
99 |
GSSCredential cred = x.getDelegCred(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
100 |
if (cred == null && x.getCredDelegState() || |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
101 |
cred != null && !x.getCredDelegState()) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
102 |
throw new Exception("getCredDelegState not match"); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
103 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
104 |
return cred; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
105 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
106 |
}); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
107 |
} catch (PrivilegedActionException pae) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
108 |
throw pae.getException(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
109 |
} |
1454 | 110 |
out.name = name + " as " + out.cred.getName().toString(); |
111 |
return out; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
12199 | 115 |
* No JAAS login at all, can be used to test JGSS without JAAS |
116 |
*/ |
|
117 |
public static Context fromThinAir() throws Exception { |
|
118 |
Context out = new Context(); |
|
119 |
out.s = new Subject(); |
|
120 |
return out; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
1454 | 124 |
* Logins with a JAAS login config entry name |
125 |
*/ |
|
126 |
public static Context fromJAAS(final String name) throws Exception { |
|
127 |
Context out = new Context(); |
|
128 |
out.name = name; |
|
129 |
LoginContext lc = new LoginContext(name); |
|
130 |
lc.login(); |
|
131 |
out.s = lc.getSubject(); |
|
132 |
return out; |
|
133 |
} |
|
134 |
||
15006 | 135 |
/** |
136 |
* Logins with username/password as a new Subject |
|
137 |
*/ |
|
9499 | 138 |
public static Context fromUserPass( |
139 |
String user, char[] pass, boolean storeKey) throws Exception { |
|
15006 | 140 |
return fromUserPass(new Subject(), user, pass, storeKey); |
9499 | 141 |
} |
12199 | 142 |
|
1454 | 143 |
/** |
15006 | 144 |
* Logins with username/password as an existing Subject. The |
145 |
* same subject can be used multiple times to simulate multiple logins. |
|
146 |
* @param s existing subject |
|
1454 | 147 |
*/ |
9499 | 148 |
public static Context fromUserPass(Subject s, |
149 |
String user, char[] pass, boolean storeKey) throws Exception { |
|
1454 | 150 |
Context out = new Context(); |
151 |
out.name = user; |
|
15006 | 152 |
out.s = s; |
1454 | 153 |
Krb5LoginModule krb5 = new Krb5LoginModule(); |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7816
diff
changeset
|
154 |
Map<String, String> map = new HashMap<>(); |
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7816
diff
changeset
|
155 |
Map<String, Object> shared = new HashMap<>(); |
1575
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
156 |
|
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
157 |
if (pass != null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
158 |
map.put("useFirstPass", "true"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
159 |
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
|
160 |
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
|
161 |
} else { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
162 |
map.put("doNotPrompt", "true"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
163 |
map.put("useTicketCache", "true"); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
164 |
if (user != null) { |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
165 |
map.put("principal", user); |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
166 |
} |
e0f1979051b5
6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents:
1574
diff
changeset
|
167 |
} |
1454 | 168 |
if (storeKey) { |
169 |
map.put("storeKey", "true"); |
|
170 |
} |
|
171 |
||
172 |
krb5.initialize(out.s, null, shared, map); |
|
173 |
krb5.login(); |
|
174 |
krb5.commit(); |
|
175 |
return out; |
|
176 |
} |
|
177 |
||
178 |
/** |
|
15006 | 179 |
* Logins with username/keytab as an existing Subject. The |
180 |
* same subject can be used multiple times to simulate multiple logins. |
|
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
181 |
*/ |
15006 | 182 |
public static Context fromUserKtab( |
183 |
String user, String ktab, boolean storeKey) throws Exception { |
|
184 |
return fromUserKtab(new Subject(), user, ktab, storeKey); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Logins with username/keytab as a new subject, |
|
189 |
*/ |
|
190 |
public static Context fromUserKtab(Subject s, |
|
191 |
String user, String ktab, boolean storeKey) throws Exception { |
|
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
192 |
Context out = new Context(); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
193 |
out.name = user; |
15006 | 194 |
out.s = s; |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
195 |
Krb5LoginModule krb5 = new Krb5LoginModule(); |
7977
f47f211cd627
7008713: diamond conversion of kerberos5 and security tools
smarks
parents:
7816
diff
changeset
|
196 |
Map<String, String> map = new HashMap<>(); |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
197 |
|
18536 | 198 |
map.put("isInitiator", "false"); |
5774
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
199 |
map.put("doNotPrompt", "true"); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
200 |
map.put("useTicketCache", "false"); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
201 |
map.put("useKeyTab", "true"); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
202 |
map.put("keyTab", ktab); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
203 |
map.put("principal", user); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
204 |
if (storeKey) { |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
205 |
map.put("storeKey", "true"); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
206 |
} |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
207 |
|
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
208 |
krb5.initialize(out.s, null, null, map); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
209 |
krb5.login(); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
210 |
krb5.commit(); |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
211 |
return out; |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
212 |
} |
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
213 |
|
4b9857e483c1
6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents:
5506
diff
changeset
|
214 |
/** |
1454 | 215 |
* Starts as a client |
216 |
* @param target communication peer |
|
217 |
* @param mech GSS mech |
|
218 |
* @throws java.lang.Exception |
|
219 |
*/ |
|
220 |
public void startAsClient(final String target, final Oid mech) throws Exception { |
|
221 |
doAs(new Action() { |
|
222 |
@Override |
|
223 |
public byte[] run(Context me, byte[] dummy) throws Exception { |
|
224 |
GSSManager m = GSSManager.getInstance(); |
|
4336 | 225 |
me.x = (ExtendedGSSContext)m.createContext( |
226 |
target.indexOf('@') < 0 ? |
|
1454 | 227 |
m.createName(target, null) : |
228 |
m.createName(target, GSSName.NT_HOSTBASED_SERVICE), |
|
229 |
mech, |
|
230 |
cred, |
|
231 |
GSSContext.DEFAULT_LIFETIME); |
|
232 |
return null; |
|
233 |
} |
|
234 |
}, null); |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Starts as a server |
|
239 |
* @param mech GSS mech |
|
240 |
* @throws java.lang.Exception |
|
241 |
*/ |
|
242 |
public void startAsServer(final Oid mech) throws Exception { |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
243 |
startAsServer(null, mech, false); |
9499 | 244 |
} |
245 |
||
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
246 |
public void startAsServer(final String name, final Oid mech) throws Exception { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
247 |
startAsServer(name, mech, false); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
248 |
} |
9499 | 249 |
/** |
250 |
* Starts as a server with the specified service name |
|
251 |
* @param name the service name |
|
252 |
* @param mech GSS mech |
|
253 |
* @throws java.lang.Exception |
|
254 |
*/ |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
255 |
public void startAsServer(final String name, final Oid mech, final boolean asInitiator) throws Exception { |
1454 | 256 |
doAs(new Action() { |
257 |
@Override |
|
258 |
public byte[] run(Context me, byte[] dummy) throws Exception { |
|
259 |
GSSManager m = GSSManager.getInstance(); |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
260 |
me.cred = m.createCredential( |
9499 | 261 |
name == null ? null : |
262 |
(name.indexOf('@') < 0 ? |
|
263 |
m.createName(name, null) : |
|
264 |
m.createName(name, GSSName.NT_HOSTBASED_SERVICE)), |
|
1454 | 265 |
GSSCredential.INDEFINITE_LIFETIME, |
266 |
mech, |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
267 |
asInitiator? |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
268 |
GSSCredential.INITIATE_AND_ACCEPT: |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
269 |
GSSCredential.ACCEPT_ONLY); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
270 |
me.x = (ExtendedGSSContext)m.createContext(me.cred); |
1454 | 271 |
return null; |
272 |
} |
|
273 |
}, null); |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Accesses the internal GSSContext object. Currently it's used for -- |
|
278 |
* |
|
279 |
* 1. calling requestXXX() before handshake |
|
280 |
* 2. accessing source name |
|
281 |
* |
|
282 |
* Note: If the application needs to do any privileged call on this |
|
283 |
* object, please use doAs(). Otherwise, it can be done directly. The |
|
284 |
* methods listed above are all non-privileged calls. |
|
285 |
* |
|
286 |
* @return the GSSContext object |
|
287 |
*/ |
|
4336 | 288 |
public ExtendedGSSContext x() { |
1454 | 289 |
return x; |
290 |
} |
|
291 |
||
292 |
/** |
|
9499 | 293 |
* Accesses the internal subject. |
294 |
* @return the subject |
|
295 |
*/ |
|
296 |
public Subject s() { |
|
297 |
return s; |
|
298 |
} |
|
299 |
||
300 |
/** |
|
16065
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
301 |
* Returns the cred inside, if there is one |
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
302 |
*/ |
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
303 |
public GSSCredential cred() { |
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
304 |
return cred; |
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
305 |
} |
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
306 |
|
2d0b844bfbb6
8000653: SPNEGO tests fail at context.getDelegCred().getRemainingInitLifetime(mechOid)
weijun
parents:
15006
diff
changeset
|
307 |
/** |
1454 | 308 |
* Disposes the GSSContext within |
309 |
* @throws org.ietf.jgss.GSSException |
|
310 |
*/ |
|
311 |
public void dispose() throws GSSException { |
|
312 |
x.dispose(); |
|
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* Does something using the Subject inside |
|
317 |
* @param action the action |
|
318 |
* @param in the input byte |
|
319 |
* @return the output byte |
|
320 |
* @throws java.lang.Exception |
|
321 |
*/ |
|
322 |
public byte[] doAs(final Action action, final byte[] in) throws Exception { |
|
323 |
try { |
|
324 |
return Subject.doAs(s, new PrivilegedExceptionAction<byte[]>() { |
|
325 |
||
326 |
@Override |
|
327 |
public byte[] run() throws Exception { |
|
328 |
return action.run(Context.this, in); |
|
329 |
} |
|
330 |
}); |
|
331 |
} catch (PrivilegedActionException pae) { |
|
332 |
throw pae.getException(); |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Prints status of GSSContext and Subject |
|
338 |
* @throws java.lang.Exception |
|
339 |
*/ |
|
340 |
public void status() throws Exception { |
|
341 |
System.out.println("STATUS OF " + name.toUpperCase()); |
|
342 |
try { |
|
343 |
StringBuffer sb = new StringBuffer(); |
|
344 |
if (x.getAnonymityState()) { |
|
345 |
sb.append("anon, "); |
|
346 |
} |
|
347 |
if (x.getConfState()) { |
|
348 |
sb.append("conf, "); |
|
349 |
} |
|
350 |
if (x.getCredDelegState()) { |
|
351 |
sb.append("deleg, "); |
|
352 |
} |
|
353 |
if (x.getIntegState()) { |
|
354 |
sb.append("integ, "); |
|
355 |
} |
|
356 |
if (x.getMutualAuthState()) { |
|
357 |
sb.append("mutual, "); |
|
358 |
} |
|
359 |
if (x.getReplayDetState()) { |
|
360 |
sb.append("rep det, "); |
|
361 |
} |
|
362 |
if (x.getSequenceDetState()) { |
|
363 |
sb.append("seq det, "); |
|
364 |
} |
|
4336 | 365 |
if (x instanceof ExtendedGSSContext) { |
366 |
if (((ExtendedGSSContext)x).getDelegPolicyState()) { |
|
367 |
sb.append("deleg policy, "); |
|
368 |
} |
|
369 |
} |
|
1454 | 370 |
System.out.println("Context status of " + name + ": " + sb.toString()); |
371 |
System.out.println(x.getSrcName() + " -> " + x.getTargName()); |
|
372 |
} catch (Exception e) { |
|
373 |
;// Don't care |
|
374 |
} |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
375 |
if (s != null) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
376 |
System.out.println("====== START SUBJECT CONTENT ====="); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
377 |
for (Principal p: s.getPrincipals()) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
378 |
System.out.println(" Principal: " + p); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
379 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
380 |
for (Object o : s.getPublicCredentials()) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
381 |
System.out.println(" " + o.getClass()); |
9499 | 382 |
System.out.println(" " + o); |
1454 | 383 |
} |
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
384 |
System.out.println("====== Private Credentials Set ======"); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
385 |
for (Object o : s.getPrivateCredentials()) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
386 |
System.out.println(" " + o.getClass()); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
387 |
if (o instanceof KerberosTicket) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
388 |
KerberosTicket kt = (KerberosTicket) o; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
389 |
System.out.println(" " + kt.getServer() + " for " + kt.getClient()); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
390 |
} else if (o instanceof KerberosKey) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
391 |
KerberosKey kk = (KerberosKey) o; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
392 |
System.out.print(" " + kk.getKeyType() + " " + kk.getVersionNumber() + " " + kk.getAlgorithm() + " "); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
393 |
for (byte b : kk.getEncoded()) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
394 |
System.out.printf("%02X", b & 0xff); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
395 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
396 |
System.out.println(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
397 |
} else if (o instanceof Map) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
398 |
Map map = (Map) o; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
399 |
for (Object k : map.keySet()) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
400 |
System.out.println(" " + k + ": " + map.get(k)); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
401 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
402 |
} else { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
403 |
System.out.println(" " + o); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
404 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
405 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
406 |
System.out.println("====== END SUBJECT CONTENT ====="); |
1454 | 407 |
} |
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
408 |
if (x != null && x instanceof ExtendedGSSContext) { |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
409 |
if (x.isEstablished()) { |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
410 |
ExtendedGSSContext ex = (ExtendedGSSContext)x; |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
411 |
Key k = (Key)ex.inquireSecContext( |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
412 |
InquireType.KRB5_GET_SESSION_KEY); |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
413 |
if (k == null) { |
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
414 |
throw new Exception("(Old) Session key cannot be null"); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
415 |
} |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
416 |
System.out.println("(Old) Session key is: " + k); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
417 |
Key k2 = (Key)ex.inquireSecContext( |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
418 |
InquireType.KRB5_GET_SESSION_KEY_EX); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
419 |
if (k2 == null) { |
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
420 |
throw new Exception("Session key cannot be null"); |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
421 |
} |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
422 |
System.out.println("Session key is: " + k); |
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
423 |
boolean[] flags = (boolean[])ex.inquireSecContext( |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
424 |
InquireType.KRB5_GET_TKT_FLAGS); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
425 |
if (flags == null) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
426 |
throw new Exception("Ticket flags cannot be null"); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
427 |
} |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
428 |
System.out.println("Ticket flags is: " + Arrays.toString(flags)); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
429 |
String authTime = (String)ex.inquireSecContext( |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
430 |
InquireType.KRB5_GET_AUTHTIME); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
431 |
if (authTime == null) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
432 |
throw new Exception("Auth time cannot be null"); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
433 |
} |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
434 |
System.out.println("AuthTime is: " + authTime); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
435 |
if (!x.isInitiator()) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
436 |
AuthorizationDataEntry[] ad = (AuthorizationDataEntry[])ex.inquireSecContext( |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
437 |
InquireType.KRB5_GET_AUTHZ_DATA); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
438 |
System.out.println("AuthzData is: " + Arrays.toString(ad)); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
3482
diff
changeset
|
439 |
} |
25661
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
440 |
try { |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
441 |
KerberosCredMessage tok = (KerberosCredMessage)ex.inquireSecContext( |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
442 |
InquireType.KRB5_GET_KRB_CRED); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
443 |
System.out.println("KRB_CRED is " + |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
444 |
(tok == null?"not ":"") + "available"); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
445 |
if (tok != null) { |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
446 |
System.out.println("From " + tok.getSender() + " to " |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
447 |
+ tok.getRecipient()); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
448 |
System.out.println(Base64.getEncoder().encodeToString(tok.getEncoded())); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
449 |
} |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
450 |
} catch (Exception e) { |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
451 |
System.out.println("KRB_CRED is not available: " + e); |
929c829a8400
8043071: Expose session key and KRB_CRED through extended GSS-API
weijun
parents:
23010
diff
changeset
|
452 |
} |
3482
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
453 |
} |
4aaa66ce712d
6710360: export Kerberos session key to applications
weijun
parents:
1575
diff
changeset
|
454 |
} |
1454 | 455 |
} |
456 |
||
10696 | 457 |
public byte[] wrap(byte[] t, final boolean privacy) |
458 |
throws Exception { |
|
459 |
return doAs(new Action() { |
|
460 |
@Override |
|
461 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
462 |
System.out.printf("wrap %s privacy from %s: ", privacy?"with":"without", me.name); |
|
463 |
MessageProp p1 = new MessageProp(0, privacy); |
|
464 |
byte[] out; |
|
465 |
if (usingStream) { |
|
466 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
467 |
me.x.wrap(new ByteArrayInputStream(input), os, p1); |
|
468 |
out = os.toByteArray(); |
|
469 |
} else { |
|
470 |
out = me.x.wrap(input, 0, input.length, p1); |
|
471 |
} |
|
472 |
System.out.println(printProp(p1)); |
|
473 |
return out; |
|
474 |
} |
|
475 |
}, t); |
|
476 |
} |
|
477 |
||
478 |
public byte[] unwrap(byte[] t, final boolean privacy) |
|
479 |
throws Exception { |
|
480 |
return doAs(new Action() { |
|
481 |
@Override |
|
482 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
483 |
System.out.printf("unwrap %s privacy from %s: ", privacy?"with":"without", me.name); |
|
484 |
MessageProp p1 = new MessageProp(0, privacy); |
|
485 |
byte[] bytes; |
|
486 |
if (usingStream) { |
|
487 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
488 |
me.x.unwrap(new ByteArrayInputStream(input), os, p1); |
|
489 |
bytes = os.toByteArray(); |
|
490 |
} else { |
|
491 |
bytes = me.x.unwrap(input, 0, input.length, p1); |
|
492 |
} |
|
493 |
System.out.println(printProp(p1)); |
|
494 |
return bytes; |
|
495 |
} |
|
496 |
}, t); |
|
497 |
} |
|
498 |
||
499 |
public byte[] getMic(byte[] t) throws Exception { |
|
500 |
return doAs(new Action() { |
|
501 |
@Override |
|
502 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
503 |
MessageProp p1 = new MessageProp(0, true); |
|
504 |
byte[] bytes; |
|
505 |
p1 = new MessageProp(0, true); |
|
506 |
System.out.printf("getMic from %s: ", me.name); |
|
507 |
if (usingStream) { |
|
508 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
509 |
me.x.getMIC(new ByteArrayInputStream(input), os, p1); |
|
510 |
bytes = os.toByteArray(); |
|
511 |
} else { |
|
512 |
bytes = me.x.getMIC(input, 0, input.length, p1); |
|
513 |
} |
|
514 |
System.out.println(printProp(p1)); |
|
515 |
return bytes; |
|
516 |
} |
|
517 |
}, t); |
|
518 |
} |
|
519 |
||
520 |
public void verifyMic(byte[] t, final byte[] msg) throws Exception { |
|
521 |
doAs(new Action() { |
|
522 |
@Override |
|
523 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
524 |
MessageProp p1 = new MessageProp(0, true); |
|
525 |
System.out.printf("verifyMic from %s: ", me.name); |
|
526 |
if (usingStream) { |
|
527 |
me.x.verifyMIC(new ByteArrayInputStream(input), |
|
528 |
new ByteArrayInputStream(msg), p1); |
|
529 |
} else { |
|
530 |
me.x.verifyMIC(input, 0, input.length, |
|
531 |
msg, 0, msg.length, |
|
532 |
p1); |
|
533 |
} |
|
534 |
System.out.println(printProp(p1)); |
|
535 |
return null; |
|
536 |
} |
|
537 |
}, t); |
|
538 |
} |
|
539 |
||
1454 | 540 |
/** |
541 |
* Transmits a message from one Context to another. The sender wraps the |
|
542 |
* message and sends it to the receiver. The receiver unwraps it, creates |
|
543 |
* a MIC of the clear text and sends it back to the sender. The sender |
|
544 |
* verifies the MIC against the message sent earlier. |
|
545 |
* @param message the message |
|
546 |
* @param s1 the sender |
|
547 |
* @param s2 the receiver |
|
548 |
* @throws java.lang.Exception If anything goes wrong |
|
549 |
*/ |
|
550 |
static public void transmit(final String message, final Context s1, |
|
551 |
final Context s2) throws Exception { |
|
552 |
final byte[] messageBytes = message.getBytes(); |
|
553 |
System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n", |
|
554 |
s1.name, s2.name); |
|
10696 | 555 |
byte[] wrapped = s1.wrap(messageBytes, true); |
556 |
byte[] unwrapped = s2.unwrap(wrapped, true); |
|
557 |
if (!Arrays.equals(messageBytes, unwrapped)) { |
|
558 |
throw new Exception("wrap/unwrap mismatch"); |
|
559 |
} |
|
560 |
byte[] mic = s2.getMic(unwrapped); |
|
561 |
s1.verifyMic(mic, messageBytes); |
|
1454 | 562 |
} |
563 |
||
564 |
/** |
|
565 |
* Returns a string description of a MessageProp object |
|
566 |
* @param prop the object |
|
567 |
* @return the description |
|
568 |
*/ |
|
569 |
static public String printProp(MessageProp prop) { |
|
570 |
StringBuffer sb = new StringBuffer(); |
|
571 |
sb.append("MessagePop: "); |
|
572 |
sb.append("QOP="+ prop.getQOP() + ", "); |
|
573 |
sb.append(prop.getPrivacy()?"privacy, ":""); |
|
574 |
sb.append(prop.isDuplicateToken()?"dup, ":""); |
|
575 |
sb.append(prop.isGapToken()?"gap, ":""); |
|
576 |
sb.append(prop.isOldToken()?"old, ":""); |
|
577 |
sb.append(prop.isUnseqToken()?"unseq, ":""); |
|
7801
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
578 |
if (prop.getMinorStatus() != 0) { |
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
579 |
sb.append(prop.getMinorString()+ "(" + prop.getMinorStatus()+")"); |
814c8359b104
6975866: api/org_ietf/jgss/GSSContext/index.html#wrapUnwrapIOTest started to fail since jdk7 b102
weijun
parents:
5774
diff
changeset
|
580 |
} |
1454 | 581 |
return sb.toString(); |
582 |
} |
|
583 |
||
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
584 |
public Context impersonate(final String someone) throws Exception { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
585 |
try { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
586 |
GSSCredential creds = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
587 |
@Override |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
588 |
public GSSCredential run() throws Exception { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
589 |
GSSManager m = GSSManager.getInstance(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
590 |
GSSName other = m.createName(someone, GSSName.NT_USER_NAME); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
591 |
if (Context.this.cred == null) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
592 |
Context.this.cred = m.createCredential(GSSCredential.INITIATE_ONLY); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
593 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
594 |
return ((ExtendedGSSCredential)Context.this.cred).impersonate(other); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
595 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
596 |
}); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
597 |
Context out = new Context(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
598 |
out.s = s; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
599 |
out.cred = creds; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
600 |
out.name = name + " as " + out.cred.getName().toString(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
601 |
return out; |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
602 |
} catch (PrivilegedActionException pae) { |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
603 |
throw pae.getException(); |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
604 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
605 |
} |
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
606 |
|
11526 | 607 |
public byte[] take(final byte[] in) throws Exception { |
608 |
return doAs(new Action() { |
|
609 |
@Override |
|
610 |
public byte[] run(Context me, byte[] input) throws Exception { |
|
611 |
if (me.x.isEstablished()) { |
|
612 |
System.out.println(name + " side established"); |
|
613 |
if (input != null) { |
|
614 |
throw new Exception("Context established but " + |
|
615 |
"still receive token at " + name); |
|
616 |
} |
|
617 |
return null; |
|
618 |
} else { |
|
619 |
if (me.x.isInitiator()) { |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
620 |
System.out.println(name + " call initSecContext"); |
11526 | 621 |
return me.x.initSecContext(input, 0, input.length); |
622 |
} else { |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
14342
diff
changeset
|
623 |
System.out.println(name + " call acceptSecContext"); |
11526 | 624 |
return me.x.acceptSecContext(input, 0, input.length); |
625 |
} |
|
626 |
} |
|
627 |
} |
|
628 |
}, in); |
|
629 |
} |
|
630 |
||
1454 | 631 |
/** |
632 |
* Handshake (security context establishment process) between two Contexts |
|
633 |
* @param c the initiator |
|
634 |
* @param s the acceptor |
|
635 |
* @throws java.lang.Exception |
|
636 |
*/ |
|
637 |
static public void handshake(final Context c, final Context s) throws Exception { |
|
638 |
byte[] t = new byte[0]; |
|
18536 | 639 |
while (true) { |
640 |
if (t != null || !c.x.isEstablished()) t = c.take(t); |
|
641 |
if (t != null || !s.x.isEstablished()) t = s.take(t); |
|
642 |
if (c.x.isEstablished() && s.x.isEstablished()) break; |
|
1454 | 643 |
} |
644 |
} |
|
645 |
} |