author | weijun |
Thu, 18 Mar 2010 18:26:37 +0800 | |
changeset 5154 | 07af3c279166 |
parent 4534 | 791203c47f4e |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
4236 | 1 |
/* |
4534 | 2 |
* Copyright 2009-2010 Sun Microsystems, Inc. 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 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
4534 | 26 |
* @bug 6894643 6913636 |
4236 | 27 |
* @summary Test JSSE Kerberos ciphersuite |
28 |
*/ |
|
29 |
import java.io.*; |
|
30 |
import java.net.InetAddress; |
|
31 |
import javax.net.ssl.*; |
|
32 |
import java.security.Principal; |
|
33 |
import java.util.Date; |
|
34 |
import sun.security.jgss.GSSUtil; |
|
4534 | 35 |
import sun.security.krb5.PrincipalName; |
36 |
import sun.security.krb5.internal.ktab.KeyTab; |
|
4236 | 37 |
|
38 |
public class SSL { |
|
39 |
||
40 |
private static final String KRB5_CIPHER = "TLS_KRB5_WITH_3DES_EDE_CBC_SHA"; |
|
41 |
private static final int LOOP_LIMIT = 1; |
|
42 |
private static int loopCount = 0; |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
43 |
private static volatile String server; |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
44 |
private static volatile int port; |
4236 | 45 |
|
46 |
public static void main(String[] args) throws Exception { |
|
47 |
||
48 |
KDC kdc = KDC.create(OneKDC.REALM); |
|
49 |
// Run this after KDC, so our own DNS service can be started |
|
50 |
try { |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
51 |
server = InetAddress.getLocalHost().getHostName().toLowerCase(); |
4236 | 52 |
} catch (java.net.UnknownHostException e) { |
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
53 |
server = "localhost"; |
4236 | 54 |
} |
55 |
||
56 |
kdc.addPrincipal(OneKDC.USER, OneKDC.PASS); |
|
57 |
kdc.addPrincipalRandKey("krbtgt/" + OneKDC.REALM); |
|
58 |
KDC.saveConfig(OneKDC.KRB5_CONF, kdc); |
|
59 |
System.setProperty("java.security.krb5.conf", OneKDC.KRB5_CONF); |
|
60 |
||
4534 | 61 |
// Add 3 versions of keys into keytab |
62 |
KeyTab ktab = KeyTab.create(OneKDC.KTAB); |
|
63 |
PrincipalName service = new PrincipalName( |
|
64 |
"host/" + server, PrincipalName.KRB_NT_SRV_HST); |
|
65 |
ktab.addEntry(service, "pass1".toCharArray(), 1); |
|
66 |
ktab.addEntry(service, "pass2".toCharArray(), 2); |
|
67 |
ktab.addEntry(service, "pass3".toCharArray(), 3); |
|
68 |
ktab.save(); |
|
69 |
||
70 |
// and use the middle one as the real key |
|
71 |
kdc.addPrincipal("host/" + server, "pass2".toCharArray()); |
|
72 |
||
73 |
// JAAS config entry name ssl |
|
74 |
System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF); |
|
75 |
File f = new File(OneKDC.JAAS_CONF); |
|
76 |
FileOutputStream fos = new FileOutputStream(f); |
|
77 |
fos.write(( |
|
78 |
"ssl {\n" + |
|
79 |
" com.sun.security.auth.module.Krb5LoginModule required\n" + |
|
80 |
" principal=\"host/" + server + "\"\n" + |
|
81 |
" useKeyTab=true\n" + |
|
82 |
" keyTab=" + OneKDC.KTAB + "\n" + |
|
83 |
" isInitiator=false\n" + |
|
84 |
" storeKey=true;\n};\n" |
|
85 |
).getBytes()); |
|
86 |
fos.close(); |
|
87 |
f.deleteOnExit(); |
|
88 |
||
4236 | 89 |
final Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
4534 | 90 |
final Context s = Context.fromJAAS("ssl"); |
4236 | 91 |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
92 |
c.startAsClient("host/" + server, GSSUtil.GSS_KRB5_MECH_OID); |
4236 | 93 |
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
94 |
||
95 |
new Thread(new Runnable() { |
|
96 |
public void run() { |
|
97 |
try { |
|
98 |
s.doAs(new JsseServerAction(), null); |
|
99 |
} catch (Exception e) { |
|
100 |
e.printStackTrace(); |
|
101 |
} |
|
102 |
} |
|
103 |
}).start(); |
|
104 |
||
105 |
// Warm the server |
|
106 |
Thread.sleep(2000); |
|
107 |
||
108 |
c.doAs(new JsseClientAction(), null); |
|
109 |
} |
|
110 |
||
111 |
// Following codes copied from |
|
112 |
// http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/lab/part2.html#JSSE |
|
113 |
private static class JsseClientAction implements Action { |
|
114 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
115 |
SSLSocketFactory sslsf = |
|
116 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
117 |
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(server, port); |
4236 | 118 |
|
119 |
// Enable only a KRB5 cipher suite. |
|
120 |
String enabledSuites[] = {KRB5_CIPHER}; |
|
121 |
sslSocket.setEnabledCipherSuites(enabledSuites); |
|
122 |
// Should check for exception if enabledSuites is not supported |
|
123 |
||
124 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
125 |
sslSocket.getInputStream())); |
|
126 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
127 |
sslSocket.getOutputStream())); |
|
128 |
||
129 |
String outStr = "Hello There!\n"; |
|
130 |
out.write(outStr); |
|
131 |
out.flush(); |
|
132 |
System.out.print("Sending " + outStr); |
|
133 |
||
134 |
String inStr = in.readLine(); |
|
135 |
System.out.println("Received " + inStr); |
|
136 |
||
137 |
String cipherSuiteChosen = sslSocket.getSession().getCipherSuite(); |
|
138 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
139 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
140 |
System.out.println("I am: " + self.toString()); |
|
141 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
142 |
System.out.println("Server is: " + peer.toString()); |
|
143 |
||
144 |
sslSocket.close(); |
|
145 |
return null; |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
private static class JsseServerAction implements Action { |
|
150 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
151 |
SSLServerSocketFactory sslssf = |
|
152 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
153 |
SSLServerSocket sslServerSocket = |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
154 |
(SSLServerSocket) sslssf.createServerSocket(0); // any port |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
155 |
port = sslServerSocket.getLocalPort(); |
4236 | 156 |
|
157 |
// Enable only a KRB5 cipher suite. |
|
158 |
String enabledSuites[] = {KRB5_CIPHER}; |
|
159 |
sslServerSocket.setEnabledCipherSuites(enabledSuites); |
|
160 |
// Should check for exception if enabledSuites is not supported |
|
161 |
||
162 |
while (loopCount++ < LOOP_LIMIT) { |
|
163 |
System.out.println("Waiting for incoming connection..."); |
|
164 |
||
165 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
|
166 |
||
167 |
System.out.println("Got connection from client " |
|
168 |
+ sslSocket.getInetAddress()); |
|
169 |
||
170 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
171 |
sslSocket.getInputStream())); |
|
172 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
173 |
sslSocket.getOutputStream())); |
|
174 |
||
175 |
String inStr = in.readLine(); |
|
176 |
System.out.println("Received " + inStr); |
|
177 |
||
178 |
String outStr = inStr + " " + new Date().toString() + "\n"; |
|
179 |
out.write(outStr); |
|
180 |
System.out.println("Sending " + outStr); |
|
181 |
out.flush(); |
|
182 |
||
183 |
String cipherSuiteChosen = |
|
184 |
sslSocket.getSession().getCipherSuite(); |
|
185 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
186 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
187 |
System.out.println("I am: " + self.toString()); |
|
188 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
189 |
System.out.println("Client is: " + peer.toString()); |
|
190 |
||
191 |
sslSocket.close(); |
|
192 |
} |
|
193 |
return null; |
|
194 |
} |
|
195 |
} |
|
196 |
} |