author | weijun |
Fri, 12 Aug 2011 11:20:45 +0800 | |
changeset 10327 | 37a3f951eb3a |
parent 9499 | f3115698a012 |
child 11107 | fc8efc57da08 |
permissions | -rw-r--r-- |
4236 | 1 |
/* |
8396
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
2 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
4236 | 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. |
|
4236 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
4534 | 26 |
* @bug 6894643 6913636 |
4236 | 27 |
* @summary Test JSSE Kerberos ciphersuite |
8396
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
28 |
* @run main/othervm SSL TLS_KRB5_WITH_RC4_128_SHA |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
29 |
* @run main/othervm SSL TLS_KRB5_WITH_RC4_128_MD5 |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
30 |
* @run main/othervm SSL TLS_KRB5_WITH_3DES_EDE_CBC_SHA |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
31 |
* @run main/othervm SSL TLS_KRB5_WITH_3DES_EDE_CBC_MD5 |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
32 |
* @run main/othervm SSL TLS_KRB5_WITH_DES_CBC_SHA |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
33 |
* @run main/othervm SSL TLS_KRB5_WITH_DES_CBC_MD5 |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
34 |
* @run main/othervm SSL TLS_KRB5_EXPORT_WITH_RC4_40_SHA |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
35 |
* @run main/othervm SSL TLS_KRB5_EXPORT_WITH_RC4_40_MD5 |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
36 |
* @run main/othervm SSL TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA |
587947f96036
7018928: test failure: sun/security/krb5/auto/SSL.java
weijun
parents:
7172
diff
changeset
|
37 |
* @run main/othervm SSL TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5 |
4236 | 38 |
*/ |
39 |
import java.io.*; |
|
40 |
import java.net.InetAddress; |
|
41 |
import javax.net.ssl.*; |
|
42 |
import java.security.Principal; |
|
43 |
import java.util.Date; |
|
44 |
import sun.security.jgss.GSSUtil; |
|
4534 | 45 |
import sun.security.krb5.PrincipalName; |
46 |
import sun.security.krb5.internal.ktab.KeyTab; |
|
4236 | 47 |
|
48 |
public class SSL { |
|
49 |
||
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
50 |
private static String krb5Cipher; |
9499 | 51 |
private static final int LOOP_LIMIT = 3; |
4236 | 52 |
private static int loopCount = 0; |
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
53 |
private static volatile String server; |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
54 |
private static volatile int port; |
4236 | 55 |
|
56 |
public static void main(String[] args) throws Exception { |
|
57 |
||
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
58 |
krb5Cipher = args[0]; |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
59 |
|
4236 | 60 |
KDC kdc = KDC.create(OneKDC.REALM); |
61 |
// Run this after KDC, so our own DNS service can be started |
|
62 |
try { |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
63 |
server = InetAddress.getLocalHost().getHostName().toLowerCase(); |
4236 | 64 |
} catch (java.net.UnknownHostException e) { |
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
65 |
server = "localhost"; |
4236 | 66 |
} |
67 |
||
68 |
kdc.addPrincipal(OneKDC.USER, OneKDC.PASS); |
|
69 |
kdc.addPrincipalRandKey("krbtgt/" + OneKDC.REALM); |
|
70 |
KDC.saveConfig(OneKDC.KRB5_CONF, kdc); |
|
71 |
System.setProperty("java.security.krb5.conf", OneKDC.KRB5_CONF); |
|
72 |
||
4534 | 73 |
// Add 3 versions of keys into keytab |
74 |
KeyTab ktab = KeyTab.create(OneKDC.KTAB); |
|
75 |
PrincipalName service = new PrincipalName( |
|
76 |
"host/" + server, PrincipalName.KRB_NT_SRV_HST); |
|
7172 | 77 |
ktab.addEntry(service, "pass1".toCharArray(), 1, true); |
78 |
ktab.addEntry(service, "pass2".toCharArray(), 2, true); |
|
79 |
ktab.addEntry(service, "pass3".toCharArray(), 3, true); |
|
4534 | 80 |
ktab.save(); |
81 |
||
82 |
// and use the middle one as the real key |
|
83 |
kdc.addPrincipal("host/" + server, "pass2".toCharArray()); |
|
84 |
||
85 |
// JAAS config entry name ssl |
|
86 |
System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF); |
|
87 |
File f = new File(OneKDC.JAAS_CONF); |
|
88 |
FileOutputStream fos = new FileOutputStream(f); |
|
89 |
fos.write(( |
|
90 |
"ssl {\n" + |
|
91 |
" com.sun.security.auth.module.Krb5LoginModule required\n" + |
|
92 |
" principal=\"host/" + server + "\"\n" + |
|
93 |
" useKeyTab=true\n" + |
|
94 |
" keyTab=" + OneKDC.KTAB + "\n" + |
|
95 |
" isInitiator=false\n" + |
|
96 |
" storeKey=true;\n};\n" |
|
97 |
).getBytes()); |
|
98 |
fos.close(); |
|
99 |
f.deleteOnExit(); |
|
100 |
||
9499 | 101 |
Context c; |
4534 | 102 |
final Context s = Context.fromJAAS("ssl"); |
4236 | 103 |
|
9499 | 104 |
// There's no keytab file when server starts. |
4236 | 105 |
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
106 |
||
9499 | 107 |
Thread server = new Thread(new Runnable() { |
4236 | 108 |
public void run() { |
109 |
try { |
|
110 |
s.doAs(new JsseServerAction(), null); |
|
111 |
} catch (Exception e) { |
|
112 |
e.printStackTrace(); |
|
113 |
} |
|
114 |
} |
|
9499 | 115 |
}); |
116 |
server.setDaemon(true); |
|
117 |
server.start(); |
|
4236 | 118 |
|
119 |
// Warm the server |
|
120 |
Thread.sleep(2000); |
|
121 |
||
9499 | 122 |
// Now create the keytab |
123 |
||
124 |
/* |
|
125 |
// Add 3 versions of keys into keytab |
|
126 |
KeyTab ktab = KeyTab.create(OneKDC.KTAB); |
|
127 |
PrincipalName service = new PrincipalName( |
|
128 |
"host/" + server, PrincipalName.KRB_NT_SRV_HST); |
|
129 |
ktab.addEntry(service, "pass1".toCharArray(), 1); |
|
130 |
ktab.addEntry(service, "pass2".toCharArray(), 2); |
|
131 |
ktab.addEntry(service, "pass3".toCharArray(), 3); |
|
132 |
ktab.save(); |
|
133 |
||
134 |
// and use the middle one as the real key |
|
135 |
kdc.addPrincipal("host/" + server, "pass2".toCharArray()); |
|
136 |
*/ |
|
137 |
c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
|
138 |
c.startAsClient("host/" + server, GSSUtil.GSS_KRB5_MECH_OID); |
|
4236 | 139 |
c.doAs(new JsseClientAction(), null); |
9499 | 140 |
|
141 |
// Add another version of key, make sure it can be loaded |
|
142 |
Thread.sleep(2000); |
|
143 |
ktab = KeyTab.getInstance(OneKDC.KTAB); |
|
144 |
ktab.addEntry(service, "pass4".toCharArray(), 4, true); |
|
145 |
ktab.save(); |
|
146 |
kdc.addPrincipal("host/" + server, "pass4".toCharArray()); |
|
147 |
||
148 |
c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
|
149 |
c.startAsClient("host/" + server, GSSUtil.GSS_KRB5_MECH_OID); |
|
150 |
c.doAs(new JsseClientAction(), null); |
|
151 |
||
152 |
// Revoke the old key |
|
153 |
/*Thread.sleep(2000); |
|
154 |
ktab = KeyTab.create(OneKDC.KTAB); |
|
155 |
ktab.addEntry(service, "pass5".toCharArray(), 5, false); |
|
156 |
ktab.save(); |
|
157 |
||
158 |
c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
|
159 |
c.startAsClient("host/" + server, GSSUtil.GSS_KRB5_MECH_OID); |
|
160 |
try { |
|
161 |
c.doAs(new JsseClientAction(), null); |
|
162 |
throw new Exception("Should fail this time."); |
|
163 |
} catch (SSLException e) { |
|
164 |
// Correct behavior. |
|
165 |
}*/ |
|
4236 | 166 |
} |
167 |
||
168 |
// Following codes copied from |
|
169 |
// http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/lab/part2.html#JSSE |
|
170 |
private static class JsseClientAction implements Action { |
|
171 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
172 |
SSLSocketFactory sslsf = |
|
173 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
9499 | 174 |
System.out.println("Connecting " + server + ":" + port); |
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
175 |
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(server, port); |
4236 | 176 |
|
177 |
// Enable only a KRB5 cipher suite. |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
178 |
String enabledSuites[] = {krb5Cipher}; |
4236 | 179 |
sslSocket.setEnabledCipherSuites(enabledSuites); |
180 |
// Should check for exception if enabledSuites is not supported |
|
181 |
||
182 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
183 |
sslSocket.getInputStream())); |
|
184 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
185 |
sslSocket.getOutputStream())); |
|
186 |
||
187 |
String outStr = "Hello There!\n"; |
|
188 |
out.write(outStr); |
|
189 |
out.flush(); |
|
190 |
System.out.print("Sending " + outStr); |
|
191 |
||
192 |
String inStr = in.readLine(); |
|
193 |
System.out.println("Received " + inStr); |
|
194 |
||
195 |
String cipherSuiteChosen = sslSocket.getSession().getCipherSuite(); |
|
196 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
197 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
198 |
System.out.println("I am: " + self.toString()); |
|
199 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
200 |
System.out.println("Server is: " + peer.toString()); |
|
201 |
||
202 |
sslSocket.close(); |
|
9499 | 203 |
// This line should not be needed. It's the server's duty to |
204 |
// forget the old key |
|
205 |
//sslSocket.getSession().invalidate(); |
|
4236 | 206 |
return null; |
207 |
} |
|
208 |
} |
|
209 |
||
210 |
private static class JsseServerAction implements Action { |
|
211 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
212 |
SSLServerSocketFactory sslssf = |
|
213 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
214 |
SSLServerSocket sslServerSocket = |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
215 |
(SSLServerSocket) sslssf.createServerSocket(0); // any port |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
216 |
port = sslServerSocket.getLocalPort(); |
9499 | 217 |
System.out.println("Listening on " + port); |
4236 | 218 |
|
219 |
// Enable only a KRB5 cipher suite. |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
220 |
String enabledSuites[] = {krb5Cipher}; |
4236 | 221 |
sslServerSocket.setEnabledCipherSuites(enabledSuites); |
222 |
// Should check for exception if enabledSuites is not supported |
|
223 |
||
224 |
while (loopCount++ < LOOP_LIMIT) { |
|
225 |
System.out.println("Waiting for incoming connection..."); |
|
226 |
||
227 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
|
228 |
||
229 |
System.out.println("Got connection from client " |
|
230 |
+ sslSocket.getInetAddress()); |
|
231 |
||
232 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
233 |
sslSocket.getInputStream())); |
|
234 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
235 |
sslSocket.getOutputStream())); |
|
236 |
||
237 |
String inStr = in.readLine(); |
|
238 |
System.out.println("Received " + inStr); |
|
239 |
||
240 |
String outStr = inStr + " " + new Date().toString() + "\n"; |
|
241 |
out.write(outStr); |
|
242 |
System.out.println("Sending " + outStr); |
|
243 |
out.flush(); |
|
244 |
||
245 |
String cipherSuiteChosen = |
|
246 |
sslSocket.getSession().getCipherSuite(); |
|
247 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
248 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
249 |
System.out.println("I am: " + self.toString()); |
|
250 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
251 |
System.out.println("Client is: " + peer.toString()); |
|
252 |
||
253 |
sslSocket.close(); |
|
254 |
} |
|
255 |
return null; |
|
256 |
} |
|
257 |
} |
|
258 |
} |