jdk/test/sun/security/pkcs11/sslecc/CipherTest.java
author xuelei
Sat, 12 Jun 2010 00:42:51 -0700
changeset 6856 533f4ad71f88
parent 5506 202f599c92aa
child 7039 6464c8e62a18
permissions -rw-r--r--
6914943: Implement final TLS renegotiation fix Summary: RFC 5746 implementation 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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            String s = cipherSuite + " in " + protocol + " mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (clientAuth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                s += " with " + clientAuth + " client authentication";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private List<TestParameters> tests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private Iterator<TestParameters> testIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private SSLSocketFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private CipherTest(PeerFactory peerFactory) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        THREADS = Integer.parseInt(System.getProperty("numThreads", "4"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        SSLSocket socket = (SSLSocket)factory.createSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String[] cipherSuites = socket.getSupportedCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String[] protocols = socket.getSupportedProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String[] clientAuths = {null, "RSA", "DSA", "ECDSA"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        tests = new ArrayList<TestParameters>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            cipherSuites.length * protocols.length * clientAuths.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (int i = 0; i < cipherSuites.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            String cipherSuite = cipherSuites[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (peerFactory.isSupported(cipherSuite) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // skip kerberos cipher suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (cipherSuite.startsWith("TLS_KRB5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            for (int j = 0; j < protocols.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                String protocol = protocols[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (protocol.equals("SSLv2Hello")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                for (int k = 0; k < clientAuths.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    String clientAuth = clientAuths[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if ((clientAuth != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            (cipherSuite.indexOf("DH_anon") != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        // no client with anonymous ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    tests.add(new TestParameters(cipherSuite, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        clientAuth));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        testIterator = tests.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    synchronized void setFailed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Thread[] threads = new Thread[THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                threads[i] = new Thread(peerFactory.newClient(this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    "Client " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            threads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            for (int i = 0; i < THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                threads[i].join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new Exception("*** Test '" + peerFactory.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                "' failed ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            System.out.println("Test '" + peerFactory.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                "' completed successfully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    synchronized TestParameters getTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (testIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return (TestParameters)testIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    SSLSocketFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static abstract class Client implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        final CipherTest cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Client(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            this.cipherTest = cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        public final void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                TestParameters params = cipherTest.getTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    // no more tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (params.isEnabled() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    System.out.println("Skipping disabled test " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    runTest(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    System.out.println("Passed " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    cipherTest.setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    System.out.println("** Failed " + params + "**");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        abstract void runTest(TestParameters params) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        void sendRequest(InputStream in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            out.write("GET / HTTP/1.0\r\n\r\n".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                sb.append((char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            String response = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (response.startsWith("HTTP/1.0 200 ") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new IOException("Invalid response: " + response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // for some reason, ${test.src} has a different value when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // test is called from the script and when it is called directly...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
//    static String pathToStores = "../../etc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    static String pathToStores = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static String pathToStoresSH = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    static char[] passwd = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static File PATH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private static KeyStore readKeyStore(String name) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        File file = new File(PATH, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        InputStream in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        ks.load(in, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public static void main(PeerFactory peerFactory, String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        String relPath;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   300
        if ((args != null) && (args.length > 0) && args[0].equals("sh")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            relPath = pathToStoresSH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            relPath = pathToStores;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        PATH = new File(System.getProperty("test.src", "."), relPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        CipherTest.peerFactory = peerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        System.out.print(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            "Initializing test '" + peerFactory.getName() + "'...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        secureRandom = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        secureRandom.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        trustStore = readKeyStore(trustStoreFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        keyStore = readKeyStore(keyStoreFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        KeyManagerFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            KeyManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                KeyManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        keyFactory.init(keyStore, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        keyManager = (X509ExtendedKeyManager)keyFactory.getKeyManagers()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        trustManager = new AlwaysTrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        CipherTest cipherTest = new CipherTest(peerFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        Thread serverThread = new Thread(peerFactory.newServer(cipherTest),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            "Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        serverThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        System.out.println("Done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        cipherTest.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        time = System.currentTimeMillis() - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        System.out.println("Done. (" + time + " ms)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    static abstract class PeerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        abstract String getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        abstract Client newClient(CipherTest cipherTest) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        abstract Server newServer(CipherTest cipherTest) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        boolean isSupported(String cipherSuite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
// we currently don't do any chain verification. we assume that works ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
// and we can speed up the test. we could also just add a plain certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
// chain comparision with our trusted certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
class AlwaysTrustManager implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public AlwaysTrustManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void checkClientTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void checkServerTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return new X509Certificate[0];
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
class MyX509KeyManager extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private final X509ExtendedKeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private String authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    MyX509KeyManager(X509ExtendedKeyManager keyManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        this.keyManager = keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    void setAuthType(String authType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        this.authType = "ECDSA".equals(authType) ? "EC" : authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return keyManager.getClientAliases(authType, issuers);
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 String chooseClientAlias(String[] keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return keyManager.chooseClientAlias(new String[] {authType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            issuers, socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public String chooseEngineClientAlias(String[] keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            Principal[] issuers, SSLEngine engine) {
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.chooseEngineClientAlias(new String[] {authType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            issuers, engine);
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[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public String chooseServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public String chooseEngineServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return keyManager.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return keyManager.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
class DaemonThreadFactory implements ThreadFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    final static ThreadFactory INSTANCE = new DaemonThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private final static ThreadFactory DEFAULT = Executors.defaultThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        Thread t = DEFAULT.newThread(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}