1 /*
   2  * Copyright (c) 2003, 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 /**
  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 }