test/jdk/javax/net/ssl/sanity/interop/JSSEServer.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    24
import java.io.IOException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    25
import java.io.InputStream;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    26
import java.io.OutputStream;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    27
import java.util.concurrent.Executor;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    28
import java.util.concurrent.Executors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    30
import javax.net.ssl.KeyManager;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    31
import javax.net.ssl.SSLContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    32
import javax.net.ssl.SSLServerSocket;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    33
import javax.net.ssl.SSLServerSocketFactory;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    34
import javax.net.ssl.SSLSocket;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    35
import javax.net.ssl.TrustManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class JSSEServer extends CipherTest.Server {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    SSLServerSocket serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    JSSEServer(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super(cipherTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        SSLContext serverContext = SSLContext.getInstance("TLS");
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    44
        serverContext.init(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    45
                new KeyManager[] { CipherTest.keyManager },
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    46
                new TrustManager[] { CipherTest.trustManager },
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    47
                CipherTest.secureRandom);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    49
        SSLServerSocketFactory factory
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    50
                = (SSLServerSocketFactory) serverContext.getServerSocketFactory();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    51
        serverSocket
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    52
                = (SSLServerSocket) factory.createServerSocket(CipherTest.serverPort);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    53
        CipherTest.serverPort = serverSocket.getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        serverSocket.setWantClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void run() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    59
        System.out.println("JSSE Server listening on port " + CipherTest.serverPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Executor exec = Executors.newFixedThreadPool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                            (cipherTest.THREADS, DaemonThreadFactory.INSTANCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                final SSLSocket socket = (SSLSocket)serverSocket.accept();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    65
                socket.setSoTimeout(CipherTest.TIMEOUT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                Runnable r = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                            InputStream in = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                            OutputStream out = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                            handleRequest(in, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                            socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            socket.getSession().invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                            cipherTest.setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                            if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                    socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                    cipherTest.setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                    System.out.println("Exception closing socket on server side:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                exec.execute(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            cipherTest.setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}