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