22068
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 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 |
* @bug 7093640
|
|
30 |
* @summary Enable TLS 1.1 and TLS 1.2 by default in client side of SunJSSE
|
|
31 |
* @run main/othervm -Djdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2"
|
|
32 |
* NoOldVersionContext
|
|
33 |
*/
|
|
34 |
|
|
35 |
import javax.net.*;
|
|
36 |
import javax.net.ssl.*;
|
|
37 |
import java.util.Arrays;
|
|
38 |
|
|
39 |
public class NoOldVersionContext {
|
|
40 |
static enum ContextVersion {
|
|
41 |
TLS_CV_01("SSL",
|
|
42 |
new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}),
|
|
43 |
TLS_CV_02("TLS",
|
|
44 |
new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}),
|
|
45 |
TLS_CV_03("SSLv3",
|
|
46 |
new String[] {"SSLv3", "TLSv1"}),
|
|
47 |
TLS_CV_04("TLSv1",
|
|
48 |
new String[] {"SSLv3", "TLSv1"}),
|
|
49 |
TLS_CV_05("TLSv1.1",
|
|
50 |
new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),
|
|
51 |
TLS_CV_06("TLSv1.2",
|
|
52 |
new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),
|
|
53 |
TLS_CV_07("Default",
|
|
54 |
new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"});
|
|
55 |
|
|
56 |
final String contextVersion;
|
|
57 |
final String[] enabledProtocols;
|
|
58 |
final static String[] supportedProtocols = new String[] {
|
|
59 |
"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"};
|
|
60 |
|
|
61 |
ContextVersion(String contextVersion, String[] enabledProtocols) {
|
|
62 |
this.contextVersion = contextVersion;
|
|
63 |
this.enabledProtocols = enabledProtocols;
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
private static boolean checkProtocols(String[] target, String[] expected) {
|
|
68 |
boolean success = true;
|
|
69 |
if (target.length == 0) {
|
|
70 |
System.out.println("\tError: No protocols");
|
|
71 |
success = false;
|
|
72 |
}
|
|
73 |
|
|
74 |
if (!Arrays.equals(target, expected)) {
|
|
75 |
System.out.println("\tError: Expected to get protocols " +
|
|
76 |
Arrays.toString(expected));
|
|
77 |
System.out.println("\tError: The actual protocols " +
|
|
78 |
Arrays.toString(target));
|
|
79 |
success = false;
|
|
80 |
}
|
|
81 |
|
|
82 |
return success;
|
|
83 |
}
|
|
84 |
|
|
85 |
private static boolean checkCipherSuites(String[] target) {
|
|
86 |
boolean success = true;
|
|
87 |
if (target.length == 0) {
|
|
88 |
System.out.println("\tError: No cipher suites");
|
|
89 |
success = false;
|
|
90 |
}
|
|
91 |
|
|
92 |
return success;
|
|
93 |
}
|
|
94 |
|
|
95 |
public static void main(String[] args) throws Exception {
|
|
96 |
boolean failed = false;
|
|
97 |
for (ContextVersion cv : ContextVersion.values()) {
|
|
98 |
System.out.println("Checking SSLContext of " + cv.contextVersion);
|
|
99 |
SSLContext context = SSLContext.getInstance(cv.contextVersion);
|
|
100 |
|
|
101 |
// Default SSLContext is initialized automatically.
|
|
102 |
if (!cv.contextVersion.equals("Default")) {
|
|
103 |
// Use default TK, KM and random.
|
|
104 |
context.init((KeyManager[])null, (TrustManager[])null, null);
|
|
105 |
}
|
|
106 |
|
|
107 |
//
|
|
108 |
// Check SSLContext
|
|
109 |
//
|
|
110 |
// Check default SSLParameters of SSLContext
|
|
111 |
System.out.println("\tChecking default SSLParameters");
|
|
112 |
SSLParameters parameters = context.getDefaultSSLParameters();
|
|
113 |
|
|
114 |
String[] protocols = parameters.getProtocols();
|
|
115 |
failed |= !checkProtocols(protocols, cv.enabledProtocols);
|
|
116 |
|
|
117 |
String[] ciphers = parameters.getCipherSuites();
|
|
118 |
failed |= !checkCipherSuites(ciphers);
|
|
119 |
|
|
120 |
// Check supported SSLParameters of SSLContext
|
|
121 |
System.out.println("\tChecking supported SSLParameters");
|
|
122 |
parameters = context.getSupportedSSLParameters();
|
|
123 |
|
|
124 |
protocols = parameters.getProtocols();
|
|
125 |
failed |= !checkProtocols(protocols, cv.supportedProtocols);
|
|
126 |
|
|
127 |
ciphers = parameters.getCipherSuites();
|
|
128 |
failed |= !checkCipherSuites(ciphers);
|
|
129 |
|
|
130 |
//
|
|
131 |
// Check SSLEngine
|
|
132 |
//
|
|
133 |
// Check SSLParameters of SSLEngine
|
|
134 |
System.out.println();
|
|
135 |
System.out.println("\tChecking SSLEngine of this SSLContext");
|
|
136 |
System.out.println("\tChecking SSLEngine.getSSLParameters()");
|
|
137 |
SSLEngine engine = context.createSSLEngine();
|
|
138 |
engine.setUseClientMode(true);
|
|
139 |
parameters = engine.getSSLParameters();
|
|
140 |
|
|
141 |
protocols = parameters.getProtocols();
|
|
142 |
failed |= !checkProtocols(protocols, cv.enabledProtocols);
|
|
143 |
|
|
144 |
ciphers = parameters.getCipherSuites();
|
|
145 |
failed |= !checkCipherSuites(ciphers);
|
|
146 |
|
|
147 |
System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
|
|
148 |
protocols = engine.getEnabledProtocols();
|
|
149 |
failed |= !checkProtocols(protocols, cv.enabledProtocols);
|
|
150 |
|
|
151 |
System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
|
|
152 |
ciphers = engine.getEnabledCipherSuites();
|
|
153 |
failed |= !checkCipherSuites(ciphers);
|
|
154 |
|
|
155 |
System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
|
|
156 |
protocols = engine.getSupportedProtocols();
|
|
157 |
failed |= !checkProtocols(protocols, cv.supportedProtocols);
|
|
158 |
|
|
159 |
System.out.println(
|
|
160 |
"\tChecking SSLEngine.getSupportedCipherSuites()");
|
|
161 |
ciphers = engine.getSupportedCipherSuites();
|
|
162 |
failed |= !checkCipherSuites(ciphers);
|
|
163 |
|
|
164 |
//
|
|
165 |
// Check SSLSocket
|
|
166 |
//
|
|
167 |
// Check SSLParameters of SSLSocket
|
|
168 |
System.out.println();
|
|
169 |
System.out.println("\tChecking SSLSocket of this SSLContext");
|
|
170 |
System.out.println("\tChecking SSLSocket.getSSLParameters()");
|
|
171 |
SocketFactory fac = context.getSocketFactory();
|
|
172 |
SSLSocket socket = (SSLSocket)fac.createSocket();
|
|
173 |
parameters = socket.getSSLParameters();
|
|
174 |
|
|
175 |
protocols = parameters.getProtocols();
|
|
176 |
failed |= !checkProtocols(protocols, cv.enabledProtocols);
|
|
177 |
|
|
178 |
ciphers = parameters.getCipherSuites();
|
|
179 |
failed |= !checkCipherSuites(ciphers);
|
|
180 |
|
|
181 |
System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
|
|
182 |
protocols = socket.getEnabledProtocols();
|
|
183 |
failed |= !checkProtocols(protocols, cv.enabledProtocols);
|
|
184 |
|
|
185 |
System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
|
|
186 |
ciphers = socket.getEnabledCipherSuites();
|
|
187 |
failed |= !checkCipherSuites(ciphers);
|
|
188 |
|
|
189 |
System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
|
|
190 |
protocols = socket.getSupportedProtocols();
|
|
191 |
failed |= !checkProtocols(protocols, cv.supportedProtocols);
|
|
192 |
|
|
193 |
System.out.println(
|
|
194 |
"\tChecking SSLEngine.getSupportedCipherSuites()");
|
|
195 |
ciphers = socket.getSupportedCipherSuites();
|
|
196 |
failed |= !checkCipherSuites(ciphers);
|
|
197 |
|
|
198 |
//
|
|
199 |
// Check SSLServerSocket
|
|
200 |
//
|
|
201 |
// Check SSLParameters of SSLServerSocket
|
|
202 |
System.out.println();
|
|
203 |
System.out.println("\tChecking SSLServerSocket of this SSLContext");
|
|
204 |
System.out.println("\tChecking SSLServerSocket.getSSLParameters()");
|
|
205 |
SSLServerSocketFactory sf = context.getServerSocketFactory();
|
|
206 |
SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();
|
|
207 |
parameters = ssocket.getSSLParameters();
|
|
208 |
|
|
209 |
protocols = parameters.getProtocols();
|
|
210 |
failed |= !checkProtocols(protocols, cv.supportedProtocols);
|
|
211 |
|
|
212 |
ciphers = parameters.getCipherSuites();
|
|
213 |
failed |= !checkCipherSuites(ciphers);
|
|
214 |
|
|
215 |
System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
|
|
216 |
protocols = ssocket.getEnabledProtocols();
|
|
217 |
failed |= !checkProtocols(protocols, cv.supportedProtocols);
|
|
218 |
|
|
219 |
System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
|
|
220 |
ciphers = ssocket.getEnabledCipherSuites();
|
|
221 |
failed |= !checkCipherSuites(ciphers);
|
|
222 |
|
|
223 |
System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
|
|
224 |
protocols = ssocket.getSupportedProtocols();
|
|
225 |
failed |= !checkProtocols(protocols, cv.supportedProtocols);
|
|
226 |
|
|
227 |
System.out.println(
|
|
228 |
"\tChecking SSLEngine.getSupportedCipherSuites()");
|
|
229 |
ciphers = ssocket.getSupportedCipherSuites();
|
|
230 |
failed |= !checkCipherSuites(ciphers);
|
|
231 |
}
|
|
232 |
|
|
233 |
if (failed) {
|
|
234 |
throw new Exception("Run into problems, see log for more details");
|
|
235 |
} else {
|
|
236 |
System.out.println("\t... Success");
|
|
237 |
}
|
|
238 |
}
|
|
239 |
}
|