author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 31474 | jdk/test/sun/security/krb5/auto/OkAsDelegateXRealm.java@7b79555b8073 |
child 48668 | 2da4a52715d8 |
permissions | -rw-r--r-- |
4336 | 1 |
/* |
12867 | 2 |
* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
4336 | 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. |
|
4336 | 22 |
*/ |
23 |
||
12867 | 24 |
/* |
25 |
* @test |
|
26 |
* @bug 6853328 7172701 |
|
27 |
* @run main/othervm OkAsDelegateXRealm false |
|
28 |
* KDC no OK-AS-DELEGATE, fail |
|
29 |
* @run main/othervm -Dtest.kdc.policy.ok-as-delegate OkAsDelegateXRealm true |
|
30 |
* KDC set OK-AS-DELEGATE for all, succeed |
|
31 |
* @run main/othervm -Dtest.kdc.policy.ok-as-delegate=host/host.r3.local OkAsDelegateXRealm false |
|
32 |
* KDC set OK-AS-DELEGATE for host/host.r3.local only, fail |
|
33 |
* @run main/othervm -Dtest.kdc.policy.ok-as-delegate=host/host.r3.local,krbtgt/R2,krbtgt/R3 OkAsDelegateXRealm true |
|
34 |
* KDC set OK-AS-DELEGATE for all three, succeed |
|
35 |
* @summary Support OK-AS-DELEGATE flag |
|
36 |
*/ |
|
4336 | 37 |
import java.io.FileOutputStream; |
38 |
import java.io.IOException; |
|
39 |
import java.security.Security; |
|
40 |
import javax.security.auth.callback.Callback; |
|
41 |
import javax.security.auth.callback.CallbackHandler; |
|
42 |
import javax.security.auth.callback.NameCallback; |
|
43 |
import javax.security.auth.callback.PasswordCallback; |
|
44 |
import javax.security.auth.callback.UnsupportedCallbackException; |
|
26629
3b9ed8175488
8042900: Allow com.sun.security.jgss to be in different module than org.ietf.jgss
weijun
parents:
12867
diff
changeset
|
45 |
|
3b9ed8175488
8042900: Allow com.sun.security.jgss to be in different module than org.ietf.jgss
weijun
parents:
12867
diff
changeset
|
46 |
import com.sun.security.jgss.ExtendedGSSContext; |
4336 | 47 |
import org.ietf.jgss.GSSException; |
48 |
import sun.security.jgss.GSSUtil; |
|
49 |
import sun.security.krb5.Config; |
|
50 |
||
51 |
public class OkAsDelegateXRealm implements CallbackHandler { |
|
52 |
||
53 |
/** |
|
54 |
* @param args boolean if the program should succeed |
|
55 |
*/ |
|
56 |
public static void main(String[] args) |
|
57 |
throws Exception { |
|
58 |
||
59 |
// Create and start the KDCs. Here we have 3 realms: R1, R2 and R3. |
|
60 |
// R1 is trusted by R2, and R2 trusted by R3. |
|
61 |
KDC kdc1 = KDC.create("R1"); |
|
12867 | 62 |
kdc1.setOption(KDC.Option.OK_AS_DELEGATE, |
4336 | 63 |
System.getProperty("test.kdc.policy.ok-as-delegate")); |
64 |
kdc1.addPrincipal("dummy", "bogus".toCharArray()); |
|
65 |
kdc1.addPrincipalRandKey("krbtgt/R1"); |
|
66 |
kdc1.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray()); |
|
67 |
||
68 |
KDC kdc2 = KDC.create("R2"); |
|
12867 | 69 |
kdc2.setOption(KDC.Option.OK_AS_DELEGATE, |
4336 | 70 |
System.getProperty("test.kdc.policy.ok-as-delegate")); |
71 |
kdc2.addPrincipalRandKey("krbtgt/R2"); |
|
72 |
kdc2.addPrincipal("krbtgt/R2@R1", "r1->r2".toCharArray()); |
|
73 |
kdc2.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray()); |
|
74 |
||
75 |
KDC kdc3 = KDC.create("R3"); |
|
12867 | 76 |
kdc3.setOption(KDC.Option.OK_AS_DELEGATE, |
4336 | 77 |
System.getProperty("test.kdc.policy.ok-as-delegate")); |
78 |
kdc3.addPrincipalRandKey("krbtgt/R3"); |
|
79 |
kdc3.addPrincipal("krbtgt/R3@R2", "r2->r3".toCharArray()); |
|
80 |
kdc3.addPrincipalRandKey("host/host.r3.local"); |
|
81 |
||
82 |
KDC.saveConfig("krb5-localkdc.conf", kdc1, kdc2, kdc3, |
|
83 |
"forwardable=true", |
|
84 |
"[capaths]", |
|
85 |
"R1 = {", |
|
86 |
" R2 = .", |
|
87 |
" R3 = R2", |
|
88 |
"}", |
|
89 |
"[domain_realm]", |
|
90 |
".r3.local=R3" |
|
91 |
); |
|
92 |
||
93 |
System.setProperty("java.security.krb5.conf", "krb5-localkdc.conf"); |
|
94 |
kdc3.writeKtab("localkdc.ktab"); |
|
95 |
||
96 |
FileOutputStream fos = new FileOutputStream("jaas-localkdc.conf"); |
|
97 |
||
98 |
// Defines the client and server on R1 and R3 respectively. |
|
99 |
fos.write(("com.sun.security.jgss.krb5.initiate {\n" + |
|
100 |
" com.sun.security.auth.module.Krb5LoginModule\n" + |
|
101 |
" required\n" + |
|
102 |
" principal=dummy\n" + |
|
103 |
" doNotPrompt=false\n" + |
|
104 |
" useTicketCache=false\n" + |
|
105 |
" ;\n};\n" + |
|
106 |
"com.sun.security.jgss.krb5.accept {\n" + |
|
107 |
" com.sun.security.auth.module.Krb5LoginModule required\n" + |
|
108 |
" principal=\"host/host.r3.local@R3\"\n" + |
|
109 |
" useKeyTab=true\n" + |
|
110 |
" keyTab=localkdc.ktab\n" + |
|
111 |
" isInitiator=false\n" + |
|
112 |
" storeKey=true;\n};\n" + |
|
113 |
"\n").getBytes()); |
|
114 |
fos.close(); |
|
115 |
||
116 |
Security.setProperty("auth.login.defaultCallbackHandler", |
|
117 |
"OkAsDelegateXRealm"); |
|
118 |
||
119 |
System.setProperty("java.security.auth.login.config", "jaas-localkdc.conf"); |
|
120 |
||
121 |
Config.refresh(); |
|
122 |
||
123 |
Context c = Context.fromJAAS("com.sun.security.jgss.krb5.initiate"); |
|
124 |
Context s = Context.fromJAAS("com.sun.security.jgss.krb5.accept"); |
|
125 |
||
126 |
// Test twice. The frist time the whole cross realm process is tried, |
|
127 |
// the second time the cached service ticket is used. This is to make sure |
|
128 |
// the behaviors are the same, especailly for the case when one of the |
|
129 |
// cross-realm TGTs does not have OK-AS-DELEGATE on. |
|
130 |
||
131 |
for (int i=0; i<2; i++) { |
|
132 |
c.startAsClient("host@host.r3.local", GSSUtil.GSS_KRB5_MECH_OID); |
|
133 |
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
|
26629
3b9ed8175488
8042900: Allow com.sun.security.jgss to be in different module than org.ietf.jgss
weijun
parents:
12867
diff
changeset
|
134 |
((ExtendedGSSContext)c.x()).requestDelegPolicy(true); |
4336 | 135 |
|
136 |
Context.handshake(c, s); |
|
137 |
boolean succeed = true; |
|
138 |
try { |
|
139 |
s.x().getDelegCred(); |
|
140 |
} catch (GSSException gsse) { |
|
141 |
succeed = false; |
|
142 |
} |
|
143 |
if (succeed != Boolean.parseBoolean(args[0])) { |
|
144 |
throw new Exception("Test fail at round #" + i); |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
@Override |
|
150 |
public void handle(Callback[] callbacks) |
|
151 |
throws IOException, UnsupportedCallbackException { |
|
152 |
for (Callback callback : callbacks) { |
|
153 |
if (callback instanceof NameCallback) { |
|
154 |
((NameCallback) callback).setName("dummy"); |
|
155 |
} |
|
156 |
if (callback instanceof PasswordCallback) { |
|
157 |
((PasswordCallback) callback).setPassword("bogus".toCharArray()); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |