test/jdk/javax/net/ssl/sanity/interop/CipherTest.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Test that all ciphersuites work in all versions and all client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * authentication types. The way this is setup the server is stateless and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * all checking is done on the client side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The test is multithreaded to speed it up, especially on multiprocessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * machines. To simplify debugging, run with -DnumThreads=1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class CipherTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // use any available port for the server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    final int THREADS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // assume that if we do not read anything for 20 seconds, something
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // has gone wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    final static int TIMEOUT = 20 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static KeyStore trustStore, keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static X509ExtendedKeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static X509TrustManager trustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static SecureRandom secureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static PeerFactory peerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static abstract class Server implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        final CipherTest cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Server(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            this.cipherTest = cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public abstract void run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        void handleRequest(InputStream in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            boolean newline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                sb.append((char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                if (ch == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                } else if (ch == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    if (newline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        // 2nd newline in a row, end of request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    newline = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    newline = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            String request = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (request.startsWith("GET / HTTP/1.") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new IOException("Invalid request: " + request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            out.write("HTTP/1.0 200 OK\r\n\r\n".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static class TestParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
        CipherSuite cipherSuite;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   105
        Protocol protocol;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String clientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   108
        TestParameters(CipherSuite cipherSuite, Protocol protocol,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                String clientAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            this.cipherSuite = cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.clientAuth = clientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        boolean isEnabled() {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   116
            return cipherSuite.supportedByProtocol(protocol);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            String s = cipherSuite + " in " + protocol + " mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (clientAuth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                s += " with " + clientAuth + " client authentication";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private List<TestParameters> tests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private Iterator<TestParameters> testIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private SSLSocketFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private boolean failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private CipherTest(PeerFactory peerFactory) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        THREADS = Integer.parseInt(System.getProperty("numThreads", "4"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        SSLSocket socket = (SSLSocket)factory.createSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String[] cipherSuites = socket.getSupportedCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String[] protocols = socket.getSupportedProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String[] clientAuths = {null, "RSA", "DSA"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        tests = new ArrayList<TestParameters>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            cipherSuites.length * protocols.length * clientAuths.length);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   142
        for (int j = 0; j < protocols.length; j++) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   143
            String protocol = protocols[j];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   144
            if (protocol.equals(Protocol.SSLV2HELLO.name)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   145
                System.out.println("Skipping SSLv2Hello protocol");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   146
                continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   147
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   148
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   149
            for (int i = 0; i < cipherSuites.length; i++) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   150
                String cipherSuite = cipherSuites[i];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   151
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   152
                // skip kerberos cipher suites and TLS_EMPTY_RENEGOTIATION_INFO_SCSV
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   153
                if (cipherSuite.startsWith("TLS_KRB5") || cipherSuite.equals(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   154
                        CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV.name())) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   155
                    System.out.println("Skipping unsupported test for " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   156
                                        cipherSuite + " of " + protocol);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   157
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   158
                }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   159
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   160
                if (!peerFactory.isSupported(cipherSuite, protocol)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                for (int k = 0; k < clientAuths.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    String clientAuth = clientAuths[k];
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   166
                    // no client with anonymous cipher suites;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   167
                    // TLS 1.3 doesn't support DSA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   168
                    if ((clientAuth != null && cipherSuite.contains("DH_anon"))
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   169
                            || ("DSA".equals(clientAuth) && "TLSv1.3".equals(protocol))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   172
                    tests.add(new TestParameters(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   173
                            CipherSuite.cipherSuite(cipherSuite),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   174
                            Protocol.protocol(protocol),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   175
                            clientAuth));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        testIterator = tests.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    synchronized void setFailed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Thread[] threads = new Thread[THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (int i = 0; i < THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                threads[i] = new Thread(peerFactory.newClient(this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    "Client " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            threads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            for (int i = 0; i < THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                threads[i].join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new Exception("*** Test '" + peerFactory.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                "' failed ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            System.out.println("Test '" + peerFactory.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                "' completed successfully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    synchronized TestParameters getTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (testIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return (TestParameters)testIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    SSLSocketFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    static abstract class Client implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        final CipherTest cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Client(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.cipherTest = cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        public final void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                TestParameters params = cipherTest.getTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    // no more tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   244
                if (!params.isEnabled()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    System.out.println("Skipping disabled test " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    runTest(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    System.out.println("Passed " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    cipherTest.setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    System.out.println("** Failed " + params + "**");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        abstract void runTest(TestParameters params) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        void sendRequest(InputStream in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            out.write("GET / HTTP/1.0\r\n\r\n".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                sb.append((char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            String response = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (response.startsWith("HTTP/1.0 200 ") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new IOException("Invalid response: " + response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    // for some reason, ${test.src} has a different value when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // test is called from the script and when it is called directly...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static String pathToStores = "../../etc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    static String pathToStoresSH = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    static char[] passwd = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static File PATH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static KeyStore readKeyStore(String name) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        File file = new File(PATH, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        InputStream in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ks.load(in, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static void main(PeerFactory peerFactory, String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String relPath;
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   303
        if ((args != null) && (args.length > 0) && args[0].equals("sh")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            relPath = pathToStoresSH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            relPath = pathToStores;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        PATH = new File(System.getProperty("test.src", "."), relPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        CipherTest.peerFactory = peerFactory;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   310
        System.out.println(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            "Initializing test '" + peerFactory.getName() + "'...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        secureRandom = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        secureRandom.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        trustStore = readKeyStore(trustStoreFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        keyStore = readKeyStore(keyStoreFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        KeyManagerFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            KeyManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                KeyManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        keyFactory.init(keyStore, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        keyManager = (X509ExtendedKeyManager)keyFactory.getKeyManagers()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        trustManager = new AlwaysTrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        CipherTest cipherTest = new CipherTest(peerFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        Thread serverThread = new Thread(peerFactory.newServer(cipherTest),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            "Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        serverThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        System.out.println("Done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        cipherTest.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        time = System.currentTimeMillis() - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        System.out.println("Done. (" + time + " ms)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    static abstract class PeerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        abstract String getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        abstract Client newClient(CipherTest cipherTest) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        abstract Server newServer(CipherTest cipherTest) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   342
        boolean isSupported(String cipherSuite, String protocol) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   343
            // ignore exportable cipher suite for TLSv1.1
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   344
            if (protocol.equals("TLSv1.1")
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   345
                    && (cipherSuite.indexOf("_EXPORT_WITH") != -1)) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   346
                    System.out.println("Skipping obsoleted test for " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   347
                                        cipherSuite + " of " + protocol);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   348
                    return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   349
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   350
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   351
            // ignore obsoleted cipher suite for the specified protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   352
            // TODO
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   353
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   354
            // ignore unsupported cipher suite for the specified protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   355
            // TODO
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   356
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
// we currently don't do any chain verification. we assume that works ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
// and we can speed up the test. we could also just add a plain certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
// chain comparision with our trusted certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
class AlwaysTrustManager implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public AlwaysTrustManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void checkClientTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public void checkServerTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return new X509Certificate[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
class MyX509KeyManager extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private final X509ExtendedKeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private String authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    MyX509KeyManager(X509ExtendedKeyManager keyManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        this.keyManager = keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    void setAuthType(String authType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        this.authType = authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return keyManager.getClientAliases(authType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public String chooseClientAlias(String[] keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return keyManager.chooseClientAlias(new String[] {authType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            issuers, socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public String chooseEngineClientAlias(String[] keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return keyManager.chooseEngineClientAlias(new String[] {authType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            issuers, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public String chooseServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public String chooseEngineServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return keyManager.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return keyManager.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
class DaemonThreadFactory implements ThreadFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    final static ThreadFactory INSTANCE = new DaemonThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private final static ThreadFactory DEFAULT = Executors.defaultThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Thread t = DEFAULT.newThread(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}