Path: blob/master/test/jdk/sun/security/ssl/CipherSuite/SSL_NULL.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 485483826* @summary Verify that SSL_NULL_WITH_NULL_NULL is returned as ciphersuite27* if the handshake fails28* @author Andreas Sterbenz29*/3031import java.io.*;32import java.net.ServerSocket;33import java.net.Socket;34import javax.net.ssl.*;3536public class SSL_NULL {37private static volatile Boolean result;3839public static void main(String[] args) throws Exception {40final SSLServerSocket serverSocket = (SSLServerSocket)41SSLServerSocketFactory.getDefault().createServerSocket(0);42serverSocket.setEnabledCipherSuites(43serverSocket.getSupportedCipherSuites());44new Thread() {45public void run() {46try {47SSLSocket socket = (SSLSocket) serverSocket.accept();48String suite = socket.getSession().getCipherSuite();49if (!"SSL_NULL_WITH_NULL_NULL".equals(suite)) {50System.err.println(51"Wrong suite for failed handshake: " +52"got " + suite +53", expected SSL_NULL_WITH_NULL_NULL");54} else {55result = Boolean.TRUE;56return;57}58} catch (IOException e) {59System.err.println("Unexpected exception");60e.printStackTrace();61} finally {62if (result == null) {63result = Boolean.FALSE;64}65}66}67}.start();6869SSLSocket socket = (SSLSocket)70SSLSocketFactory.getDefault().createSocket(71"localhost", serverSocket.getLocalPort());72socket.setEnabledCipherSuites(73new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" });74try {75OutputStream out = socket.getOutputStream();76out.write(0);77out.flush();78throw new RuntimeException("No exception received");79} catch (SSLHandshakeException e) {80System.out.println("Expected handshake exception: " + e);81}8283System.out.println("client: " + socket.getSession().getCipherSuite());8485// wait for other thread to set result86while (result == null) {87Thread.sleep(50);88}89if (result.booleanValue()) {90System.out.println("Test passed");91} else {92throw new Exception("Test failed");93}94}95}969798