author | weijun |
Sat, 06 Nov 2010 09:11:18 +0800 | |
changeset 7172 | 01308fd663b3 |
parent 5975 | 076cd013e5e4 |
child 8396 | 587947f96036 |
permissions | -rw-r--r-- |
4236 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2009, 2010, 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 |
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
28 |
* @run main SSL TLS_KRB5_WITH_RC4_128_SHA |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
29 |
* @run main SSL TLS_KRB5_WITH_RC4_128_MD5 |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
30 |
* @run main SSL TLS_KRB5_WITH_3DES_EDE_CBC_SHA |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
31 |
* @run main SSL TLS_KRB5_WITH_3DES_EDE_CBC_MD5 |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
32 |
* @run main SSL TLS_KRB5_WITH_DES_CBC_SHA |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
33 |
* @run main SSL TLS_KRB5_WITH_DES_CBC_MD5 |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
34 |
* @run main SSL TLS_KRB5_EXPORT_WITH_RC4_40_SHA |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
35 |
* @run main SSL TLS_KRB5_EXPORT_WITH_RC4_40_MD5 |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
36 |
* @run main SSL TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA |
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
37 |
* @run main 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; |
4236 | 51 |
private static final int LOOP_LIMIT = 1; |
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 |
||
4236 | 101 |
final Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
4534 | 102 |
final Context s = Context.fromJAAS("ssl"); |
4236 | 103 |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
104 |
c.startAsClient("host/" + server, GSSUtil.GSS_KRB5_MECH_OID); |
4236 | 105 |
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
106 |
||
107 |
new Thread(new Runnable() { |
|
108 |
public void run() { |
|
109 |
try { |
|
110 |
s.doAs(new JsseServerAction(), null); |
|
111 |
} catch (Exception e) { |
|
112 |
e.printStackTrace(); |
|
113 |
} |
|
114 |
} |
|
115 |
}).start(); |
|
116 |
||
117 |
// Warm the server |
|
118 |
Thread.sleep(2000); |
|
119 |
||
120 |
c.doAs(new JsseClientAction(), null); |
|
121 |
} |
|
122 |
||
123 |
// Following codes copied from |
|
124 |
// http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/lab/part2.html#JSSE |
|
125 |
private static class JsseClientAction implements Action { |
|
126 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
127 |
SSLSocketFactory sslsf = |
|
128 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
129 |
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(server, port); |
4236 | 130 |
|
131 |
// Enable only a KRB5 cipher suite. |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
132 |
String enabledSuites[] = {krb5Cipher}; |
4236 | 133 |
sslSocket.setEnabledCipherSuites(enabledSuites); |
134 |
// Should check for exception if enabledSuites is not supported |
|
135 |
||
136 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
137 |
sslSocket.getInputStream())); |
|
138 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
139 |
sslSocket.getOutputStream())); |
|
140 |
||
141 |
String outStr = "Hello There!\n"; |
|
142 |
out.write(outStr); |
|
143 |
out.flush(); |
|
144 |
System.out.print("Sending " + outStr); |
|
145 |
||
146 |
String inStr = in.readLine(); |
|
147 |
System.out.println("Received " + inStr); |
|
148 |
||
149 |
String cipherSuiteChosen = sslSocket.getSession().getCipherSuite(); |
|
150 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
151 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
152 |
System.out.println("I am: " + self.toString()); |
|
153 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
154 |
System.out.println("Server is: " + peer.toString()); |
|
155 |
||
156 |
sslSocket.close(); |
|
157 |
return null; |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
private static class JsseServerAction implements Action { |
|
162 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
163 |
SSLServerSocketFactory sslssf = |
|
164 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
165 |
SSLServerSocket sslServerSocket = |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
166 |
(SSLServerSocket) sslssf.createServerSocket(0); // any port |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
167 |
port = sslServerSocket.getLocalPort(); |
4236 | 168 |
|
169 |
// Enable only a KRB5 cipher suite. |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
170 |
String enabledSuites[] = {krb5Cipher}; |
4236 | 171 |
sslServerSocket.setEnabledCipherSuites(enabledSuites); |
172 |
// Should check for exception if enabledSuites is not supported |
|
173 |
||
174 |
while (loopCount++ < LOOP_LIMIT) { |
|
175 |
System.out.println("Waiting for incoming connection..."); |
|
176 |
||
177 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
|
178 |
||
179 |
System.out.println("Got connection from client " |
|
180 |
+ sslSocket.getInetAddress()); |
|
181 |
||
182 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
183 |
sslSocket.getInputStream())); |
|
184 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
185 |
sslSocket.getOutputStream())); |
|
186 |
||
187 |
String inStr = in.readLine(); |
|
188 |
System.out.println("Received " + inStr); |
|
189 |
||
190 |
String outStr = inStr + " " + new Date().toString() + "\n"; |
|
191 |
out.write(outStr); |
|
192 |
System.out.println("Sending " + outStr); |
|
193 |
out.flush(); |
|
194 |
||
195 |
String cipherSuiteChosen = |
|
196 |
sslSocket.getSession().getCipherSuite(); |
|
197 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
198 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
199 |
System.out.println("I am: " + self.toString()); |
|
200 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
201 |
System.out.println("Client is: " + peer.toString()); |
|
202 |
||
203 |
sslSocket.close(); |
|
204 |
} |
|
205 |
return null; |
|
206 |
} |
|
207 |
} |
|
208 |
} |