author | vinnie |
Thu, 03 Dec 2009 21:30:27 +0000 | |
changeset 4346 | aee26e5a53b0 |
parent 4344 | ebd0b1f2e4a2 |
child 4534 | 791203c47f4e |
permissions | -rw-r--r-- |
4236 | 1 |
/* |
2 |
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
|
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 |
|
26 |
* @bug 6894643 |
|
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; |
|
35 |
||
36 |
public class SSL { |
|
37 |
||
38 |
private static final String KRB5_CIPHER = "TLS_KRB5_WITH_3DES_EDE_CBC_SHA"; |
|
39 |
private static final int LOOP_LIMIT = 1; |
|
40 |
private static final char[] PASS = "secret".toCharArray(); |
|
41 |
private static int loopCount = 0; |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
42 |
private static volatile String server; |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
43 |
private static volatile int port; |
4236 | 44 |
|
45 |
public static void main(String[] args) throws Exception { |
|
46 |
||
47 |
KDC kdc = KDC.create(OneKDC.REALM); |
|
48 |
// Run this after KDC, so our own DNS service can be started |
|
49 |
try { |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
50 |
server = InetAddress.getLocalHost().getHostName().toLowerCase(); |
4236 | 51 |
} catch (java.net.UnknownHostException e) { |
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
52 |
server = "localhost"; |
4236 | 53 |
} |
54 |
||
55 |
kdc.addPrincipal(OneKDC.USER, OneKDC.PASS); |
|
56 |
kdc.addPrincipalRandKey("krbtgt/" + OneKDC.REALM); |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
57 |
kdc.addPrincipal("host/" + server, PASS); |
4236 | 58 |
KDC.saveConfig(OneKDC.KRB5_CONF, kdc); |
59 |
System.setProperty("java.security.krb5.conf", OneKDC.KRB5_CONF); |
|
60 |
||
61 |
final Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
62 |
final Context s = Context.fromUserPass("host/" + server, PASS, true); |
4236 | 63 |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
64 |
c.startAsClient("host/" + server, GSSUtil.GSS_KRB5_MECH_OID); |
4236 | 65 |
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID); |
66 |
||
67 |
new Thread(new Runnable() { |
|
68 |
public void run() { |
|
69 |
try { |
|
70 |
s.doAs(new JsseServerAction(), null); |
|
71 |
} catch (Exception e) { |
|
72 |
e.printStackTrace(); |
|
73 |
} |
|
74 |
} |
|
75 |
}).start(); |
|
76 |
||
77 |
// Warm the server |
|
78 |
Thread.sleep(2000); |
|
79 |
||
80 |
c.doAs(new JsseClientAction(), null); |
|
81 |
} |
|
82 |
||
83 |
// Following codes copied from |
|
84 |
// http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/lab/part2.html#JSSE |
|
85 |
private static class JsseClientAction implements Action { |
|
86 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
87 |
SSLSocketFactory sslsf = |
|
88 |
(SSLSocketFactory) SSLSocketFactory.getDefault(); |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
89 |
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(server, port); |
4236 | 90 |
|
91 |
// Enable only a KRB5 cipher suite. |
|
92 |
String enabledSuites[] = {KRB5_CIPHER}; |
|
93 |
sslSocket.setEnabledCipherSuites(enabledSuites); |
|
94 |
// Should check for exception if enabledSuites is not supported |
|
95 |
||
96 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
97 |
sslSocket.getInputStream())); |
|
98 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
99 |
sslSocket.getOutputStream())); |
|
100 |
||
101 |
String outStr = "Hello There!\n"; |
|
102 |
out.write(outStr); |
|
103 |
out.flush(); |
|
104 |
System.out.print("Sending " + outStr); |
|
105 |
||
106 |
String inStr = in.readLine(); |
|
107 |
System.out.println("Received " + inStr); |
|
108 |
||
109 |
String cipherSuiteChosen = sslSocket.getSession().getCipherSuite(); |
|
110 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
111 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
112 |
System.out.println("I am: " + self.toString()); |
|
113 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
114 |
System.out.println("Server is: " + peer.toString()); |
|
115 |
||
116 |
sslSocket.close(); |
|
117 |
return null; |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
private static class JsseServerAction implements Action { |
|
122 |
public byte[] run(Context s, byte[] input) throws Exception { |
|
123 |
SSLServerSocketFactory sslssf = |
|
124 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
125 |
SSLServerSocket sslServerSocket = |
|
4346
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
126 |
(SSLServerSocket) sslssf.createServerSocket(0); // any port |
aee26e5a53b0
6906854: SSL/Krb5 testcase should not use a fixed port number
vinnie
parents:
4344
diff
changeset
|
127 |
port = sslServerSocket.getLocalPort(); |
4236 | 128 |
|
129 |
// Enable only a KRB5 cipher suite. |
|
130 |
String enabledSuites[] = {KRB5_CIPHER}; |
|
131 |
sslServerSocket.setEnabledCipherSuites(enabledSuites); |
|
132 |
// Should check for exception if enabledSuites is not supported |
|
133 |
||
134 |
while (loopCount++ < LOOP_LIMIT) { |
|
135 |
System.out.println("Waiting for incoming connection..."); |
|
136 |
||
137 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
|
138 |
||
139 |
System.out.println("Got connection from client " |
|
140 |
+ sslSocket.getInetAddress()); |
|
141 |
||
142 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
143 |
sslSocket.getInputStream())); |
|
144 |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( |
|
145 |
sslSocket.getOutputStream())); |
|
146 |
||
147 |
String inStr = in.readLine(); |
|
148 |
System.out.println("Received " + inStr); |
|
149 |
||
150 |
String outStr = inStr + " " + new Date().toString() + "\n"; |
|
151 |
out.write(outStr); |
|
152 |
System.out.println("Sending " + outStr); |
|
153 |
out.flush(); |
|
154 |
||
155 |
String cipherSuiteChosen = |
|
156 |
sslSocket.getSession().getCipherSuite(); |
|
157 |
System.out.println("Cipher suite in use: " + cipherSuiteChosen); |
|
158 |
Principal self = sslSocket.getSession().getLocalPrincipal(); |
|
159 |
System.out.println("I am: " + self.toString()); |
|
160 |
Principal peer = sslSocket.getSession().getPeerPrincipal(); |
|
161 |
System.out.println("Client is: " + peer.toString()); |
|
162 |
||
163 |
sslSocket.close(); |
|
164 |
} |
|
165 |
return null; |
|
166 |
} |
|
167 |
} |
|
168 |
} |