11/*
2- * Copyright (c) 2003, 2021 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2003, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2626 * @bug 4495742
2727 * @summary Add non-blocking SSL/TLS functionality, usable with any
2828 * I/O abstraction
29- *
3029 * This is intended to test many of the basic API calls to the SSLEngine
3130 * interface. This doesn't really exercise much of the SSL code.
3231 *
32+ * @library /test/lib
3333 * @author Brad Wetmore
34+ * @run main/othervm Basics
3435 */
3536
3637import java .security .*;
3738import java .io .*;
3839import java .nio .*;
40+ import java .util .Arrays ;
3941import javax .net .ssl .*;
4042import javax .net .ssl .SSLEngineResult .*;
4143
44+ import jdk .test .lib .security .SecurityUtils ;
45+
4246public class Basics {
4347
44- private static String pathToStores = "../etc" ;
45- private static String keyStoreFile = "keystore" ;
46- private static String trustStoreFile = "truststore" ;
47- private static String passwd = "passphrase" ;
48+ private static final String PATH_TO_STORES = "../etc" ;
49+ private static final String KEY_STORE_FILE = "keystore" ;
50+ private static final String TRUSTSTORE_FILE = "truststore" ;
51+
52+ private static final String KEYSTORE_PATH =
53+ System .getProperty ("test.src" , "./" ) + "/" + PATH_TO_STORES +
54+ "/" + KEY_STORE_FILE ;
55+ private static final String TRUSTSTORE_PATH =
56+ System .getProperty ("test.src" , "./" ) + "/" + PATH_TO_STORES +
57+ "/" + TRUSTSTORE_FILE ;
4858
49- private static String keyFilename =
50- System .getProperty ("test.src" , "./" ) + "/" + pathToStores +
51- "/" + keyStoreFile ;
52- private static String trustFilename =
53- System .getProperty ("test.src" , "./" ) + "/" + pathToStores +
54- "/" + trustStoreFile ;
59+ public static void main (String [] args ) throws Exception {
60+ SecurityUtils .removeFromDisabledTlsAlgs ("TLSv1.1" );
61+
62+ runTest ("TLSv1.3" , "TLS_AES_256_GCM_SHA384" );
63+ runTest ("TLSv1.2" , "TLS_RSA_WITH_AES_256_GCM_SHA384" );
64+ runTest ("TLSv1.1" , "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" );
65+ }
5566
56- public static void main (String args []) throws Exception {
67+ private static void runTest (String protocol , String cipherSuite ) throws Exception {
68+ System .out .printf ("Testing %s with %s%n" , protocol , cipherSuite );
5769
5870 KeyStore ks = KeyStore .getInstance ("JKS" );
5971 KeyStore ts = KeyStore .getInstance ("JKS" );
6072 char [] passphrase = "passphrase" .toCharArray ();
6173
62- ks .load (new FileInputStream (keyFilename ), passphrase );
63- ts .load (new FileInputStream (trustFilename ), passphrase );
74+ ks .load (new FileInputStream (KEYSTORE_PATH ), passphrase );
75+ ts .load (new FileInputStream (TRUSTSTORE_PATH ), passphrase );
6476
6577 KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
6678 kmf .init (ks , passphrase );
@@ -77,75 +89,85 @@ public static void main(String args[]) throws Exception {
7789 System .out .println (ssle );
7890
7991 String [] suites = ssle .getSupportedCipherSuites ();
80- String secondSuite = suites [1 ];
81- String [] oneSuites = new String [] { secondSuite };
92+ // sanity check that the ciphersuite we want to use is still supported
93+ Arrays .stream (suites )
94+ .filter (s -> s .equals (cipherSuite ))
95+ .findFirst ()
96+ .orElseThrow ((() ->
97+ new RuntimeException (cipherSuite +
98+ " is not a supported ciphersuite." )));
8299
83100 printStrings ("Supported Ciphersuites" , suites );
84101 printStrings ("Enabled Ciphersuites" , ssle .getEnabledCipherSuites ());
85- ssle .setEnabledCipherSuites (oneSuites );
102+ ssle .setEnabledCipherSuites (new String [] { cipherSuite } );
86103 printStrings ("Set Ciphersuites" , ssle .getEnabledCipherSuites ());
87104
88105 suites = ssle .getEnabledCipherSuites ();
89106 if ((ssle .getEnabledCipherSuites ().length != 1 ) ||
90- !(suites [0 ].equals (secondSuite ))) {
91- throw new Exception ("set ciphers not what was expected" );
107+ !(suites [0 ].equals (cipherSuite ))) {
108+ throw new RuntimeException ("set ciphers not what was expected" );
92109 }
93110
94111 System .out .println ();
95112
96113 String [] protocols = ssle .getSupportedProtocols ();
97- String secondProtocol = protocols [1 ];
98- String [] oneProtocols = new String [] { protocols [1 ] };
114+ // sanity check that the protocol we want is still supported
115+ Arrays .stream (protocols )
116+ .filter (p -> p .equals (protocol ))
117+ .findFirst ()
118+ .orElseThrow (() ->
119+ new RuntimeException (protocol +
120+ " is not a supported TLS protocol." ));
99121
100122 printStrings ("Supported Protocols" , protocols );
101123 printStrings ("Enabled Protocols" , ssle .getEnabledProtocols ());
102- ssle .setEnabledProtocols (oneProtocols );
124+ ssle .setEnabledProtocols (new String []{ protocol } );
103125 printStrings ("Set Protocols" , ssle .getEnabledProtocols ());
104126
105127 protocols = ssle .getEnabledProtocols ();
106128 if ((ssle .getEnabledProtocols ().length != 1 ) ||
107- !(protocols [0 ].equals (secondProtocol ))) {
108- throw new Exception ("set protocols not what was expected" );
129+ !(protocols [0 ].equals (protocol ))) {
130+ throw new RuntimeException ("set protocols not what was expected" );
109131 }
110132
111133 System .out .println ("Checking get/setUseClientMode" );
112134
113135 ssle .setUseClientMode (true );
114- if (ssle .getUseClientMode () != true ) {
115- throw new Exception ("set/getUseClientMode false" );
136+ if (! ssle .getUseClientMode ()) {
137+ throw new RuntimeException ("set/getUseClientMode false" );
116138 }
117139
118140 ssle .setUseClientMode (false );
119- if (ssle .getUseClientMode () != false ) {
120- throw new Exception ("set/getUseClientMode true" );
141+ if (ssle .getUseClientMode ()) {
142+ throw new RuntimeException ("set/getUseClientMode true" );
121143 }
122144
123145
124146 System .out .println ("Checking get/setClientAuth" );
125147
126148 ssle .setNeedClientAuth (false );
127- if (ssle .getNeedClientAuth () != false ) {
128- throw new Exception ("set/getNeedClientAuth true" );
149+ if (ssle .getNeedClientAuth ()) {
150+ throw new RuntimeException ("set/getNeedClientAuth true" );
129151 }
130152
131153 ssle .setNeedClientAuth (true );
132- if (ssle .getNeedClientAuth () != true ) {
133- throw new Exception ("set/getNeedClientAuth false" );
154+ if (! ssle .getNeedClientAuth ()) {
155+ throw new RuntimeException ("set/getNeedClientAuth false" );
134156 }
135157
136158 ssle .setWantClientAuth (true );
137159
138- if (ssle .getNeedClientAuth () == true ) {
139- throw new Exception ("set/getWantClientAuth need = true" );
160+ if (ssle .getNeedClientAuth ()) {
161+ throw new RuntimeException ("set/getWantClientAuth need = true" );
140162 }
141163
142- if (ssle .getWantClientAuth () != true ) {
143- throw new Exception ("set/getNeedClientAuth false" );
164+ if (! ssle .getWantClientAuth ()) {
165+ throw new RuntimeException ("set/getNeedClientAuth false" );
144166 }
145167
146168 ssle .setWantClientAuth (false );
147- if (ssle .getWantClientAuth () != false ) {
148- throw new Exception ("set/getNeedClientAuth true" );
169+ if (ssle .getWantClientAuth ()) {
170+ throw new RuntimeException ("set/getNeedClientAuth true" );
149171 }
150172
151173 /*
@@ -156,32 +178,27 @@ public static void main(String args[]) throws Exception {
156178 System .out .println ("checking session creation" );
157179
158180 ssle .setEnableSessionCreation (false );
159- if (ssle .getEnableSessionCreation () != false ) {
160- throw new Exception ("set/getSessionCreation true" );
181+ if (ssle .getEnableSessionCreation ()) {
182+ throw new RuntimeException ("set/getSessionCreation true" );
161183 }
162184
163185 ssle .setEnableSessionCreation (true );
164- if (ssle .getEnableSessionCreation () != true ) {
165- throw new Exception ("set/getSessionCreation false" );
186+ if (! ssle .getEnableSessionCreation ()) {
187+ throw new RuntimeException ("set/getSessionCreation false" );
166188 }
167189
168190 /* Checking for overflow wrap/unwrap() */
169191 ByteBuffer smallBB = ByteBuffer .allocate (10 );
170192
171193 if (ssle .wrap (smallBB , smallBB ).getStatus () !=
172194 Status .BUFFER_OVERFLOW ) {
173- throw new Exception ("wrap should have overflowed" );
195+ throw new RuntimeException ("wrap should have overflowed" );
174196 }
175197
176198 // For unwrap(), the BUFFER_OVERFLOW will not be generated
177199 // until received SSL/TLS application data.
178200 // Test test/jdk/javax/net/ssl/SSLEngine/LargePacket.java will check
179201 // BUFFER_OVERFLOW/UNDERFLOW for both wrap() and unwrap().
180- //
181- //if (ssle.unwrap(smallBB, smallBB).getStatus() !=
182- // Status.BUFFER_OVERFLOW) {
183- // throw new Exception("unwrap should have overflowed");
184- //}
185202
186203 SSLSession ssls = ssle .getSession ();
187204
@@ -196,14 +213,18 @@ public static void main(String args[]) throws Exception {
196213 */
197214 if (ssle .wrap (appBB , netBB ).getHandshakeStatus () !=
198215 HandshakeStatus .NEED_UNWRAP ) {
199- throw new Exception ("initial client hello needs unwrap" );
216+ throw new RuntimeException ("initial client hello needs unwrap" );
200217 }
201218
202- /* Checking for overflow wrap/unwrap() */
203-
204- if (ssle .wrap (appBB , netBB ).getStatus () !=
205- Status .BUFFER_OVERFLOW ) {
206- throw new Exception ("unwrap should have overflowed" );
219+ /*
220+ * After the first call to wrap(), the handshake status is
221+ * NEED_UNWRAP and we need to receive data before doing anymore
222+ * handshaking.
223+ */
224+ SSLEngineResult result = ssle .wrap (appBB , netBB );
225+ if (result .getStatus () != Status .OK
226+ && result .bytesConsumed () != 0 && result .bytesProduced () != 0 ) {
227+ throw new RuntimeException ("wrap should have returned without doing anything" );
207228 }
208229
209230 ByteBuffer ro = appBB .asReadOnlyBuffer ();
@@ -218,7 +239,7 @@ public static void main(String args[]) throws Exception {
218239
219240 try {
220241 ssle .unwrap (netBB , ro );
221- throw new Exception ("unwrap wasn't ReadOnlyBufferException" );
242+ throw new RuntimeException ("unwrap wasn't ReadOnlyBufferException" );
222243 } catch (ReadOnlyBufferException e ) {
223244 System .out .println ("Caught the ReadOnlyBuffer: " + e );
224245 }
@@ -233,31 +254,38 @@ public static void main(String args[]) throws Exception {
233254 appBB )).getStatus () !=
234255 Status .BUFFER_UNDERFLOW ) {
235256 System .out .println (sslER );
236- throw new Exception ("unwrap should underflow" );
257+ throw new RuntimeException ("unwrap should underflow" );
237258 }
238259
239260 if ((sslER =
240261 ssle .unwrap (ByteBuffer .wrap (incompleteSSLHeader ),
241262 appBB )).getStatus () !=
242263 Status .BUFFER_UNDERFLOW ) {
243264 System .out .println (sslER );
244- throw new Exception ("unwrap should underflow" );
265+ throw new RuntimeException ("unwrap should underflow" );
245266 }
246267
247268 if ((sslER =
248269 ssle .unwrap (ByteBuffer .wrap (smallv2Header ),
249270 appBB )).getStatus () !=
250271 Status .BUFFER_UNDERFLOW ) {
251272 System .out .println (sslER );
252- throw new Exception ("unwrap should underflow" );
273+ throw new RuntimeException ("unwrap should underflow" );
253274 }
254275
255276 // junk inbound message
256277 try {
278+ /*
279+ * Exceptions are thrown when:
280+ * - the length field is correct but the data can't be decoded.
281+ * - the length field is larger than max allowed.
282+ */
257283 ssle .unwrap (ByteBuffer .wrap (gobblydegook ), appBB );
258- throw new Exception ("Didn't catch the nasty SSLException" );
259- } catch (SSLException e ) {
260- System .out .println ("caught the nasty SSLException: " + e );
284+ throw new RuntimeException ("Expected SSLProtocolException was not thrown "
285+ + "for bad input" );
286+ } catch (SSLProtocolException e ) {
287+ System .out .println ("caught the SSLProtocolException for bad decoding: "
288+ + e );
261289 }
262290
263291 System .out .println ("Test PASSED" );
@@ -278,8 +306,8 @@ public static void main(String args[]) throws Exception {
278306 (byte ) 0x00 };
279307
280308 static byte [] gobblydegook = new byte [] {
281- // "HELLO HELLO"
282- (byte ) 0x48 , (byte ) 0x45 , (byte ) 0x4C , (byte ) 0x4C , (byte ) 0x20 ,
309+ // bad data but correct record length to cause decryption error
310+ (byte ) 0x48 , (byte ) 0x45 , (byte ) 0x4C , (byte ) 0x00 , (byte ) 0x04 ,
283311 (byte ) 0x48 , (byte ) 0x45 , (byte ) 0x4C , (byte ) 0x4C };
284312
285313 static void printStrings (String label , String [] strs ) {
0 commit comments