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