|
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 // SunJSSE does not support dynamic system properties, no way to re-use |
|
25 // system properties in samevm/agentvm mode. |
|
26 |
|
27 /* |
|
28 * @test |
|
29 * @summary Test jdk.tls.client.protocols with DTLS |
|
30 * @run main/othervm -Djdk.tls.client.protocols="DTLSv1.0" |
|
31 * CustomizedDTLSDefaultProtocols |
|
32 */ |
|
33 |
|
34 |
|
35 import java.security.Security; |
|
36 import java.util.Arrays; |
|
37 import java.util.HashSet; |
|
38 import java.util.Set; |
|
39 |
|
40 import javax.net.SocketFactory; |
|
41 import javax.net.ssl.KeyManager; |
|
42 import javax.net.ssl.SSLContext; |
|
43 import javax.net.ssl.SSLEngine; |
|
44 import javax.net.ssl.SSLParameters; |
|
45 import javax.net.ssl.SSLServerSocket; |
|
46 import javax.net.ssl.SSLServerSocketFactory; |
|
47 import javax.net.ssl.SSLSocket; |
|
48 import javax.net.ssl.TrustManager; |
|
49 |
|
50 public class CustomizedDTLSDefaultProtocols { |
|
51 |
|
52 enum ContextVersion { |
|
53 TLS_CV_01("DTLS", |
|
54 new String[] {"DTLSv1.0"}), |
|
55 TLS_CV_02("DTLSv1.0", |
|
56 new String[] {"DTLSv1.0"}), |
|
57 TLS_CV_03("DTLSv1.2", |
|
58 new String[] {"DTLSv1.0", "DTLSv1.2"}); |
|
59 |
|
60 final String contextVersion; |
|
61 final String[] enabledProtocols; |
|
62 final static String[] supportedProtocols = new String[] { |
|
63 "DTLSv1.0", "DTLSv1.2"}; |
|
64 |
|
65 ContextVersion(String contextVersion, String[] enabledProtocols) { |
|
66 this.contextVersion = contextVersion; |
|
67 this.enabledProtocols = enabledProtocols; |
|
68 } |
|
69 } |
|
70 |
|
71 private static boolean checkProtocols(String[] target, String[] expected) { |
|
72 boolean success = true; |
|
73 if (target.length == 0) { |
|
74 System.out.println("\tError: No protocols"); |
|
75 success = false; |
|
76 } |
|
77 |
|
78 if (!protocolEquals(target, expected)) { |
|
79 System.out.println("\tError: Expected to get protocols " + |
|
80 Arrays.toString(expected)); |
|
81 success = false; |
|
82 } |
|
83 System.out.println("\t Protocols found " + Arrays.toString(target)); |
|
84 |
|
85 return success; |
|
86 } |
|
87 |
|
88 private static boolean protocolEquals( |
|
89 String[] actualProtocols, |
|
90 String[] expectedProtocols) { |
|
91 if (actualProtocols.length != expectedProtocols.length) { |
|
92 return false; |
|
93 } |
|
94 |
|
95 Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols)); |
|
96 for (String actual : actualProtocols) { |
|
97 if (set.add(actual)) { |
|
98 return false; |
|
99 } |
|
100 } |
|
101 |
|
102 return true; |
|
103 } |
|
104 |
|
105 private static boolean checkCipherSuites(String[] target) { |
|
106 boolean success = true; |
|
107 if (target.length == 0) { |
|
108 System.out.println("\tError: No cipher suites"); |
|
109 success = false; |
|
110 } |
|
111 |
|
112 return success; |
|
113 } |
|
114 |
|
115 public static void main(String[] args) throws Exception { |
|
116 // reset the security property to make sure that the algorithms |
|
117 // and keys used in this test are not disabled. |
|
118 Security.setProperty("jdk.tls.disabledAlgorithms", ""); |
|
119 |
|
120 boolean failed = false; |
|
121 for (ContextVersion cv : ContextVersion.values()) { |
|
122 System.out.println("Checking SSLContext of " + cv.contextVersion); |
|
123 SSLContext context = SSLContext.getInstance(cv.contextVersion); |
|
124 |
|
125 // Default SSLContext is initialized automatically. |
|
126 if (!cv.contextVersion.equals("Default")) { |
|
127 // Use default TK, KM and random. |
|
128 context.init((KeyManager[])null, (TrustManager[])null, null); |
|
129 } |
|
130 |
|
131 // |
|
132 // Check SSLContext |
|
133 // |
|
134 // Check default SSLParameters of SSLContext |
|
135 System.out.println("\tChecking default SSLParameters"); |
|
136 SSLParameters parameters = context.getDefaultSSLParameters(); |
|
137 |
|
138 String[] protocols = parameters.getProtocols(); |
|
139 failed |= !checkProtocols(protocols, cv.enabledProtocols); |
|
140 |
|
141 String[] ciphers = parameters.getCipherSuites(); |
|
142 failed |= !checkCipherSuites(ciphers); |
|
143 |
|
144 // Check supported SSLParameters of SSLContext |
|
145 System.out.println("\tChecking supported SSLParameters"); |
|
146 parameters = context.getSupportedSSLParameters(); |
|
147 |
|
148 protocols = parameters.getProtocols(); |
|
149 failed |= !checkProtocols(protocols, cv.supportedProtocols); |
|
150 |
|
151 ciphers = parameters.getCipherSuites(); |
|
152 failed |= !checkCipherSuites(ciphers); |
|
153 |
|
154 // |
|
155 // Check SSLEngine |
|
156 // |
|
157 // Check SSLParameters of SSLEngine |
|
158 System.out.println(); |
|
159 System.out.println("\tChecking SSLEngine of this SSLContext"); |
|
160 System.out.println("\tChecking SSLEngine.getSSLParameters()"); |
|
161 SSLEngine engine = context.createSSLEngine(); |
|
162 engine.setUseClientMode(true); |
|
163 parameters = engine.getSSLParameters(); |
|
164 |
|
165 protocols = parameters.getProtocols(); |
|
166 failed |= !checkProtocols(protocols, cv.enabledProtocols); |
|
167 |
|
168 ciphers = parameters.getCipherSuites(); |
|
169 failed |= !checkCipherSuites(ciphers); |
|
170 |
|
171 System.out.println("\tChecking SSLEngine.getEnabledProtocols()"); |
|
172 protocols = engine.getEnabledProtocols(); |
|
173 failed |= !checkProtocols(protocols, cv.enabledProtocols); |
|
174 |
|
175 System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()"); |
|
176 ciphers = engine.getEnabledCipherSuites(); |
|
177 failed |= !checkCipherSuites(ciphers); |
|
178 |
|
179 System.out.println("\tChecking SSLEngine.getSupportedProtocols()"); |
|
180 protocols = engine.getSupportedProtocols(); |
|
181 failed |= !checkProtocols(protocols, cv.supportedProtocols); |
|
182 |
|
183 System.out.println( |
|
184 "\tChecking SSLEngine.getSupportedCipherSuites()"); |
|
185 ciphers = engine.getSupportedCipherSuites(); |
|
186 failed |= !checkCipherSuites(ciphers); |
|
187 |
|
188 // |
|
189 // Check SSLSocket |
|
190 // |
|
191 // Check SSLParameters of SSLSocket |
|
192 System.out.println(); |
|
193 System.out.println("\tChecking SSLSocket of this SSLContext"); |
|
194 System.out.println("\tChecking SSLSocket.getSSLParameters()"); |
|
195 SocketFactory fac = context.getSocketFactory(); |
|
196 SSLSocket socket = (SSLSocket)fac.createSocket(); |
|
197 parameters = socket.getSSLParameters(); |
|
198 |
|
199 protocols = parameters.getProtocols(); |
|
200 failed |= !checkProtocols(protocols, cv.enabledProtocols); |
|
201 |
|
202 ciphers = parameters.getCipherSuites(); |
|
203 failed |= !checkCipherSuites(ciphers); |
|
204 |
|
205 System.out.println("\tChecking SSLEngine.getEnabledProtocols()"); |
|
206 protocols = socket.getEnabledProtocols(); |
|
207 failed |= !checkProtocols(protocols, cv.enabledProtocols); |
|
208 |
|
209 System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()"); |
|
210 ciphers = socket.getEnabledCipherSuites(); |
|
211 failed |= !checkCipherSuites(ciphers); |
|
212 |
|
213 System.out.println("\tChecking SSLEngine.getSupportedProtocols()"); |
|
214 protocols = socket.getSupportedProtocols(); |
|
215 failed |= !checkProtocols(protocols, cv.supportedProtocols); |
|
216 |
|
217 System.out.println( |
|
218 "\tChecking SSLEngine.getSupportedCipherSuites()"); |
|
219 ciphers = socket.getSupportedCipherSuites(); |
|
220 failed |= !checkCipherSuites(ciphers); |
|
221 |
|
222 // |
|
223 // Check SSLServerSocket |
|
224 // |
|
225 // Check SSLParameters of SSLServerSocket |
|
226 System.out.println(); |
|
227 System.out.println("\tChecking SSLServerSocket of this SSLContext"); |
|
228 System.out.println("\tChecking SSLServerSocket.getSSLParameters()"); |
|
229 SSLServerSocketFactory sf = context.getServerSocketFactory(); |
|
230 SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket(); |
|
231 parameters = ssocket.getSSLParameters(); |
|
232 |
|
233 protocols = parameters.getProtocols(); |
|
234 failed |= !checkProtocols(protocols, cv.supportedProtocols); |
|
235 |
|
236 ciphers = parameters.getCipherSuites(); |
|
237 failed |= !checkCipherSuites(ciphers); |
|
238 |
|
239 System.out.println("\tChecking SSLEngine.getEnabledProtocols()"); |
|
240 protocols = ssocket.getEnabledProtocols(); |
|
241 failed |= !checkProtocols(protocols, cv.supportedProtocols); |
|
242 |
|
243 System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()"); |
|
244 ciphers = ssocket.getEnabledCipherSuites(); |
|
245 failed |= !checkCipherSuites(ciphers); |
|
246 |
|
247 System.out.println("\tChecking SSLEngine.getSupportedProtocols()"); |
|
248 protocols = ssocket.getSupportedProtocols(); |
|
249 failed |= !checkProtocols(protocols, cv.supportedProtocols); |
|
250 |
|
251 System.out.println( |
|
252 "\tChecking SSLEngine.getSupportedCipherSuites()"); |
|
253 ciphers = ssocket.getSupportedCipherSuites(); |
|
254 failed |= !checkCipherSuites(ciphers); |
|
255 } |
|
256 |
|
257 if (failed) { |
|
258 throw new Exception("Run into problems, see log for more details"); |
|
259 } else { |
|
260 System.out.println("\t... Success"); |
|
261 } |
|
262 } |
|
263 } |