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 4854838 27 * @summary Verify that SSL_NULL_WITH_NULL_NULL is returned as ciphersuite if the handshake fails 28 * @author Andreas Sterbenz 29 */ 30 31 import java.io.*; 32 import java.net.ServerSocket; 33 import java.net.Socket; 34 import javax.net.ssl.*; 35 36 public class SSL_NULL { 37 38 private static volatile Boolean result; 39 40 public static void main(String[] args) throws Exception { 41 final SSLServerSocket serverSocket = (SSLServerSocket) 42 SSLServerSocketFactory.getDefault().createServerSocket(0); 43 serverSocket.setEnabledCipherSuites( 44 serverSocket.getSupportedCipherSuites()); 45 new Thread() { 46 public void run() { 47 try { 48 SSLSocket socket = (SSLSocket) serverSocket.accept(); 49 String suite = socket.getSession().getCipherSuite(); 50 if (!"SSL_NULL_WITH_NULL_NULL".equals(suite)) { 51 System.err.println( 52 "Wrong suite for failed handshake: " + 53 "got " + suite + 54 ", expected SSL_NULL_WITH_NULL_NULL"); 55 } else { 56 result = Boolean.TRUE; 57 return; 58 } 59 } catch (IOException e) { 60 System.err.println("Unexpected exception"); 61 e.printStackTrace(); 62 } finally { 63 if (result == null) { 64 result = Boolean.FALSE; 65 } 66 } 67 } 68 }.start(); 69 70 SSLSocket socket = (SSLSocket) 71 SSLSocketFactory.getDefault().createSocket( 72 "localhost", serverSocket.getLocalPort()); 73 socket.setEnabledCipherSuites( 74 new String[] { "SSL_RSA_WITH_RC4_128_MD5" }); 75 try { 76 OutputStream out = socket.getOutputStream(); 77 out.write(0); 78 out.flush(); 79 throw new RuntimeException("No exception received"); 80 } catch (SSLHandshakeException e) { 81 } 82 System.out.println("client: " + socket.getSession().getCipherSuite()); 83 // wait for other thread to set result 84 while (result == null) { 85 Thread.sleep(50); 86 } 87 if (result.booleanValue()) { 88 System.out.println("Test passed"); 89 } else { 90 throw new Exception("Test failed"); 91 } 92 } 93 } | 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 4854838 27 * @summary Verify that SSL_NULL_WITH_NULL_NULL is returned as ciphersuite 28 * if the handshake fails 29 * @author Andreas Sterbenz 30 */ 31 32 import java.io.*; 33 import java.net.ServerSocket; 34 import java.net.Socket; 35 import javax.net.ssl.*; 36 37 public class SSL_NULL { 38 private static volatile Boolean result; 39 40 public static void main(String[] args) throws Exception { 41 final SSLServerSocket serverSocket = (SSLServerSocket) 42 SSLServerSocketFactory.getDefault().createServerSocket(0); 43 serverSocket.setEnabledCipherSuites( 44 serverSocket.getSupportedCipherSuites()); 45 new Thread() { 46 public void run() { 47 try { 48 SSLSocket socket = (SSLSocket) serverSocket.accept(); 49 String suite = socket.getSession().getCipherSuite(); 50 if (!"SSL_NULL_WITH_NULL_NULL".equals(suite)) { 51 System.err.println( 52 "Wrong suite for failed handshake: " + 53 "got " + suite + 54 ", expected SSL_NULL_WITH_NULL_NULL"); 55 } else { 56 result = Boolean.TRUE; 57 return; 58 } 59 } catch (IOException e) { 60 System.err.println("Unexpected exception"); 61 e.printStackTrace(); 62 } finally { 63 if (result == null) { 64 result = Boolean.FALSE; 65 } 66 } 67 } 68 }.start(); 69 70 SSLSocket socket = (SSLSocket) 71 SSLSocketFactory.getDefault().createSocket( 72 "localhost", serverSocket.getLocalPort()); 73 socket.setEnabledCipherSuites( 74 new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" }); 75 try { 76 OutputStream out = socket.getOutputStream(); 77 out.write(0); 78 out.flush(); 79 throw new RuntimeException("No exception received"); 80 } catch (SSLHandshakeException e) { 81 System.out.println("Expected handshake exception: " + e); 82 } 83 84 System.out.println("client: " + socket.getSession().getCipherSuite()); 85 86 // wait for other thread to set result 87 while (result == null) { 88 Thread.sleep(50); 89 } 90 if (result.booleanValue()) { 91 System.out.println("Test passed"); 92 } else { 93 throw new Exception("Test failed"); 94 } 95 } 96 } |