|
1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. 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 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. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8164879 |
|
27 * @library /lib/testlibrary ../../ |
|
28 * @modules java.base/sun.security.util |
|
29 * @summary Verify AES/GCM's limits set in the jdk.tls.keyLimits property |
|
30 * @run main SSLSocketKeyLimit 0 server AES/GCM/NoPadding keyupdate 1000000 |
|
31 * @run main SSLSocketKeyLimit 0 client AES/GCM/NoPadding keyupdate 1000000 |
|
32 * @run main SSLSocketKeyLimit 1 client AES/GCM/NoPadding keyupdate 2^22 |
|
33 */ |
|
34 |
|
35 /** |
|
36 * Verify AES/GCM's limits set in the jdk.tls.keyLimits property |
|
37 * start a new handshake sequence to renegotiate the symmetric key with an |
|
38 * SSLSocket connection. This test verifies the handshake method was called |
|
39 * via debugging info. It does not verify the renegotiation was successful |
|
40 * as that is very hard. |
|
41 */ |
|
42 |
|
43 import javax.net.ssl.KeyManagerFactory; |
|
44 import javax.net.ssl.SSLContext; |
|
45 import javax.net.ssl.SSLServerSocket; |
|
46 import javax.net.ssl.SSLServerSocketFactory; |
|
47 import javax.net.ssl.SSLSocket; |
|
48 import javax.net.ssl.SSLSocketFactory; |
|
49 import javax.net.ssl.TrustManagerFactory; |
|
50 import java.io.ByteArrayInputStream; |
|
51 import java.io.ByteArrayOutputStream; |
|
52 import java.io.File; |
|
53 import java.io.InputStream; |
|
54 import java.io.OutputStream; |
|
55 import java.io.PrintWriter; |
|
56 import java.security.KeyStore; |
|
57 import java.security.SecureRandom; |
|
58 import java.util.Arrays; |
|
59 |
|
60 import jdk.testlibrary.ProcessTools; |
|
61 import jdk.testlibrary.Utils; |
|
62 import jdk.testlibrary.OutputAnalyzer; |
|
63 import sun.security.util.HexDumpEncoder; |
|
64 |
|
65 public class SSLSocketKeyLimit { |
|
66 |
|
67 SSLSocket svr, c; |
|
68 SSLServerSocketFactory ssf; |
|
69 SSLServerSocket ss; |
|
70 SSLSocketFactory sf; |
|
71 InputStream in; |
|
72 OutputStream out; |
|
73 |
|
74 static boolean serverReady = false; |
|
75 static int serverPort = 12345; |
|
76 |
|
77 static String pathToStores = "../../../../javax/net/ssl/etc/"; |
|
78 static String keyStoreFile = "keystore"; |
|
79 static String passwd = "passphrase"; |
|
80 static int dataLen = 10240; |
|
81 static byte[] data = new byte[dataLen]; |
|
82 static boolean serverwrite = true; |
|
83 int totalDataLen = 0; |
|
84 static boolean done = false; |
|
85 |
|
86 SSLSocketKeyLimit() { |
|
87 in = new ByteArrayInputStream(new byte[dataLen]); |
|
88 out = new ByteArrayOutputStream(); |
|
89 } |
|
90 |
|
91 SSLContext initContext() throws Exception { |
|
92 SSLContext sc = SSLContext.getInstance("TLSv1.3"); |
|
93 KeyStore ks = KeyStore.getInstance( |
|
94 new File(System.getProperty("javax.net.ssl.keyStore")), |
|
95 passwd.toCharArray()); |
|
96 KeyManagerFactory kmf = |
|
97 KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
|
98 kmf.init(ks, passwd.toCharArray()); |
|
99 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
|
100 tmf.init(ks); |
|
101 sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom()); |
|
102 return sc; |
|
103 } |
|
104 |
|
105 /** |
|
106 * args should have two values: server|client, <limit size> |
|
107 * Prepending 'p' is for internal use only. |
|
108 */ |
|
109 public static void main(String args[]) throws Exception { |
|
110 if (args[0].compareTo("p") != 0) { |
|
111 |
|
112 boolean expectedFail = (Integer.parseInt(args[0]) == 1); |
|
113 if (expectedFail) { |
|
114 System.out.println("Test expected to not find updated msg"); |
|
115 } |
|
116 //Write security property file to overwrite default |
|
117 File f = new File("keyusage."+ System.nanoTime()); |
|
118 PrintWriter p = new PrintWriter(f); |
|
119 p.write("jdk.tls.keyLimits="); |
|
120 for (int i = 2; i < args.length; i++) { |
|
121 p.write(" "+ args[i]); |
|
122 } |
|
123 p.close(); |
|
124 System.out.println("Keyusage path = " + f.getAbsolutePath()); |
|
125 System.setProperty("test.java.opts", |
|
126 "-Dtest.src=" + System.getProperty("test.src") + |
|
127 " -Dtest.jdk=" + System.getProperty("test.jdk") + |
|
128 " -Djavax.net.debug=ssl " + |
|
129 " -Djava.security.properties=" + f.getName()); |
|
130 |
|
131 System.out.println("test.java.opts: " + |
|
132 System.getProperty("test.java.opts")); |
|
133 |
|
134 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, |
|
135 Utils.addTestJavaOpts("SSLSocketKeyLimit", "p", args[1])); |
|
136 |
|
137 OutputAnalyzer output = ProcessTools.executeProcess(pb); |
|
138 try { |
|
139 if (expectedFail) { |
|
140 output.shouldNotContain("KeyUpdate: write key updated"); |
|
141 output.shouldNotContain("KeyUpdate: read key updated"); |
|
142 } else { |
|
143 output.shouldContain("KeyUpdate: write key updated"); |
|
144 output.shouldContain("KeyUpdate: read key updated"); |
|
145 } |
|
146 } catch (Exception e) { |
|
147 throw e; |
|
148 } finally { |
|
149 System.out.println("-- BEGIN Stdout:"); |
|
150 System.out.println(output.getStdout()); |
|
151 System.out.println("-- END Stdout"); |
|
152 System.out.println("-- BEGIN Stderr:"); |
|
153 System.out.println(output.getStderr()); |
|
154 System.out.println("-- END Stderr"); |
|
155 } |
|
156 return; |
|
157 } |
|
158 |
|
159 if (args.length > 0 && args[0].compareToIgnoreCase("client") == 0) { |
|
160 serverwrite = false; |
|
161 } |
|
162 |
|
163 String keyFilename = |
|
164 System.getProperty("test.src", "./") + "/" + pathToStores + |
|
165 "/" + keyStoreFile; |
|
166 |
|
167 System.setProperty("javax.net.ssl.keyStore", keyFilename); |
|
168 System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
169 |
|
170 Arrays.fill(data, (byte)0x0A); |
|
171 Thread ts = new Thread(new Server()); |
|
172 |
|
173 ts.start(); |
|
174 while (!serverReady) { |
|
175 Thread.sleep(100); |
|
176 } |
|
177 new Client().run(); |
|
178 ts.interrupt(); |
|
179 ts.join(10000); // 10sec |
|
180 System.exit(0); |
|
181 } |
|
182 |
|
183 void write(SSLSocket s) throws Exception { |
|
184 int i = 0; |
|
185 in = s.getInputStream(); |
|
186 out = s.getOutputStream(); |
|
187 System.out.print("Write-side writing... "); |
|
188 while (i++ < 150) { |
|
189 out.write(data, 0, dataLen); |
|
190 } |
|
191 out.flush(); |
|
192 out.write(0x0D); |
|
193 out.flush(); |
|
194 |
|
195 // Let read side all the data |
|
196 while (!done) { |
|
197 Thread.sleep(100); |
|
198 } |
|
199 } |
|
200 |
|
201 |
|
202 void read(SSLSocket s) throws Exception { |
|
203 byte[] buf = new byte[dataLen]; |
|
204 int len; |
|
205 int i = 0; |
|
206 try { |
|
207 System.out.println("connected " + s.getSession().getCipherSuite()); |
|
208 in = s.getInputStream(); |
|
209 out = s.getOutputStream(); |
|
210 while (true) { |
|
211 len = in.read(buf, 0, dataLen); |
|
212 System.out.print("."); |
|
213 for (byte b: buf) { |
|
214 if (b == 0x0A || b == 0x0D) { |
|
215 continue; |
|
216 } |
|
217 System.out.println("\nData invalid: " + new HexDumpEncoder().encode(buf)); |
|
218 break; |
|
219 } |
|
220 |
|
221 if (len > 0 && buf[len-1] == 0x0D) { |
|
222 System.out.print("got end byte"); |
|
223 break; |
|
224 } |
|
225 out.write(i++); |
|
226 totalDataLen += len; |
|
227 } |
|
228 } catch (Exception e) { |
|
229 System.out.println("\n" + e.getMessage()); |
|
230 e.printStackTrace(); |
|
231 } finally { |
|
232 // Tell write side that we are done reading |
|
233 done = true; |
|
234 } |
|
235 System.out.println("\nTotalDataLen = " + totalDataLen); |
|
236 } |
|
237 |
|
238 static class Server extends SSLSocketKeyLimit implements Runnable { |
|
239 Server() { |
|
240 super(); |
|
241 try { |
|
242 ssf = initContext().getServerSocketFactory(); |
|
243 ss = (SSLServerSocket) ssf.createServerSocket(serverPort); |
|
244 serverPort = ss.getLocalPort(); |
|
245 } catch (Exception e) { |
|
246 System.out.println("server: " + e.getMessage()); |
|
247 e.printStackTrace(); |
|
248 } |
|
249 } |
|
250 |
|
251 public void run() { |
|
252 |
|
253 try { |
|
254 serverReady = true; |
|
255 System.out.println("Server waiting... "); |
|
256 svr = (SSLSocket) ss.accept(); |
|
257 if (serverwrite) { |
|
258 write(svr); |
|
259 } else { |
|
260 read(svr); |
|
261 } |
|
262 |
|
263 svr.close(); |
|
264 } catch (Exception e) { |
|
265 System.out.println("server: " + e.getMessage()); |
|
266 e.printStackTrace(); |
|
267 } |
|
268 System.out.println("Server closed"); |
|
269 } |
|
270 } |
|
271 |
|
272 |
|
273 static class Client extends SSLSocketKeyLimit implements Runnable { |
|
274 Client() { |
|
275 super(); |
|
276 } |
|
277 |
|
278 public void run() { |
|
279 try { |
|
280 sf = initContext().getSocketFactory(); |
|
281 System.out.print("Client connecting... "); |
|
282 c = (SSLSocket)sf.createSocket("localhost", serverPort); |
|
283 System.out.println("connected. " + c.getSession().getCipherSuite()); |
|
284 |
|
285 // Opposite of what the server does |
|
286 if (!serverwrite) { |
|
287 write(c); |
|
288 } else { |
|
289 read(c); |
|
290 } |
|
291 |
|
292 } catch (Exception e) { |
|
293 System.err.println("client: " + e.getMessage()); |
|
294 e.printStackTrace(); |
|
295 } |
|
296 } |
|
297 } |
|
298 } |