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 if the handshake fails
28 * @author Andreas Sterbenz
29 */
30
31import java.io.*;
32import java.net.ServerSocket;
33import java.net.Socket;
34import javax.net.ssl.*;
35
36public 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}
94