jdk/test/sun/security/pkcs11/sslecc/CipherTest.java
author wetmore
Tue, 12 Mar 2013 15:31:49 -0700
changeset 16067 36055e4b5305
parent 16045 9d08c3b9a6a0
child 16913 a6f4d1626ad9
permissions -rw-r--r--
8009925: Back out AEAD CipherSuites temporarily Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
     2
 * Copyright (c) 2002, 2011, 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() {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   117
            return TLSCipherStatus.isEnabled(cipherSuite, protocol);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            String s = cipherSuite + " in " + protocol + " mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (clientAuth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                s += " with " + clientAuth + " client authentication";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   128
        static enum TLSCipherStatus {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   129
            // cipher suites supported since TLS 1.2
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   130
            CS_01("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", 0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   131
            CS_02("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",   0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   132
            CS_03("TLS_RSA_WITH_AES_256_CBC_SHA256",         0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   133
            CS_04("TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384",  0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   134
            CS_05("TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",    0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   135
            CS_06("TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",     0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   136
            CS_07("TLS_DHE_DSS_WITH_AES_256_CBC_SHA256",     0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   137
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   138
            CS_08("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", 0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   139
            CS_09("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",   0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   140
            CS_10("TLS_RSA_WITH_AES_128_CBC_SHA256",         0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   141
            CS_11("TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256",  0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   142
            CS_12("TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256",    0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   143
            CS_13("TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",     0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   144
            CS_14("TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",     0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   145
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   146
            CS_15("TLS_DH_anon_WITH_AES_256_CBC_SHA256",     0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   147
            CS_16("TLS_DH_anon_WITH_AES_128_CBC_SHA256",     0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   148
            CS_17("TLS_RSA_WITH_NULL_SHA256",                0x0303, 0xFFFF),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   149
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   150
            // cipher suites obsoleted since TLS 1.2
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   151
            CS_50("SSL_RSA_WITH_DES_CBC_SHA",                0x0000, 0x0303),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   152
            CS_51("SSL_DHE_RSA_WITH_DES_CBC_SHA",            0x0000, 0x0303),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   153
            CS_52("SSL_DHE_DSS_WITH_DES_CBC_SHA",            0x0000, 0x0303),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   154
            CS_53("SSL_DH_anon_WITH_DES_CBC_SHA",            0x0000, 0x0303),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   155
            CS_54("TLS_KRB5_WITH_DES_CBC_SHA",               0x0000, 0x0303),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   156
            CS_55("TLS_KRB5_WITH_DES_CBC_MD5",               0x0000, 0x0303),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   157
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   158
            // cipher suites obsoleted since TLS 1.1
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   159
            CS_60("SSL_RSA_EXPORT_WITH_RC4_40_MD5",          0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   160
            CS_61("SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",      0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   161
            CS_62("SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",       0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   162
            CS_63("SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",   0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   163
            CS_64("SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA",   0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   164
            CS_65("SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",   0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   165
            CS_66("TLS_KRB5_EXPORT_WITH_RC4_40_SHA",         0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   166
            CS_67("TLS_KRB5_EXPORT_WITH_RC4_40_MD5",         0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   167
            CS_68("TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA",     0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   168
            CS_69("TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5",     0x0000, 0x0302),
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   169
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   170
            // ignore TLS_EMPTY_RENEGOTIATION_INFO_SCSV always
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   171
            CS_99("TLS_EMPTY_RENEGOTIATION_INFO_SCSV",       0xFFFF, 0x0000);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   172
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   173
            // the cipher suite name
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   174
            final String cipherSuite;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   175
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   176
            // supported since protocol version
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   177
            final int supportedSince;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   178
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   179
            // obsoleted since protocol version
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   180
            final int obsoletedSince;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   181
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   182
            TLSCipherStatus(String cipherSuite,
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   183
                    int supportedSince, int obsoletedSince) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   184
                this.cipherSuite = cipherSuite;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   185
                this.supportedSince = supportedSince;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   186
                this.obsoletedSince = obsoletedSince;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   187
            }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   188
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   189
            static boolean isEnabled(String cipherSuite, String protocol) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   190
                int versionNumber = toVersionNumber(protocol);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   191
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   192
                if (versionNumber < 0) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   193
                    return true;  // unlikely to happen
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   194
                }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   195
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   196
                for (TLSCipherStatus status : TLSCipherStatus.values()) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   197
                    if (cipherSuite.equals(status.cipherSuite)) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   198
                        if ((versionNumber < status.supportedSince) ||
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   199
                            (versionNumber >= status.obsoletedSince)) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   200
                            return false;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   201
                        }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   202
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   203
                        return true;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   204
                    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   205
                }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   206
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   207
                return true;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   208
            }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   209
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   210
            private static int toVersionNumber(String protocol) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   211
                int versionNumber = -1;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   212
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   213
                switch (protocol) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   214
                    case "SSLv2Hello":
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   215
                        versionNumber = 0x0002;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   216
                        break;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   217
                    case "SSLv3":
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   218
                        versionNumber = 0x0300;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   219
                        break;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   220
                    case "TLSv1":
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   221
                        versionNumber = 0x0301;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   222
                        break;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   223
                    case "TLSv1.1":
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   224
                        versionNumber = 0x0302;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   225
                        break;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   226
                    case "TLSv1.2":
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   227
                        versionNumber = 0x0303;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   228
                        break;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   229
                    default:
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   230
                        // unlikely to happen
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   231
                }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   232
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   233
                return versionNumber;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   234
            }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   235
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private List<TestParameters> tests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private Iterator<TestParameters> testIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private SSLSocketFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private boolean failed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private CipherTest(PeerFactory peerFactory) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        THREADS = Integer.parseInt(System.getProperty("numThreads", "4"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        SSLSocket socket = (SSLSocket)factory.createSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        String[] cipherSuites = socket.getSupportedCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        String[] protocols = socket.getSupportedProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        String[] clientAuths = {null, "RSA", "DSA", "ECDSA"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        tests = new ArrayList<TestParameters>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            cipherSuites.length * protocols.length * clientAuths.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        for (int i = 0; i < cipherSuites.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            String cipherSuite = cipherSuites[i];
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   254
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            for (int j = 0; j < protocols.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                String protocol = protocols[j];
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   257
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   258
                if (!peerFactory.isSupported(cipherSuite, protocol)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   261
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                for (int k = 0; k < clientAuths.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    String clientAuth = clientAuths[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    if ((clientAuth != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                            (cipherSuite.indexOf("DH_anon") != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        // no client with anonymous ciphersuites
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   269
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    tests.add(new TestParameters(cipherSuite, protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        clientAuth));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7039
diff changeset
   275
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        testIterator = tests.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    synchronized void setFailed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Thread[] threads = new Thread[THREADS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        for (int i = 0; i < THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                threads[i] = new Thread(peerFactory.newClient(this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    "Client " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            threads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            for (int i = 0; i < THREADS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                threads[i].join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new Exception("*** Test '" + peerFactory.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                "' failed ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            System.out.println("Test '" + peerFactory.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                "' completed successfully");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    synchronized TestParameters getTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (testIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return (TestParameters)testIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    SSLSocketFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static abstract class Client implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        final CipherTest cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Client(CipherTest cipherTest) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            this.cipherTest = cipherTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        public final void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                TestParameters params = cipherTest.getTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    // no more tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (params.isEnabled() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    System.out.println("Skipping disabled test " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    runTest(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    System.out.println("Passed " + params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    cipherTest.setFailed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    System.out.println("** Failed " + params + "**");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    e.printStackTrace();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        abstract void runTest(TestParameters params) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        void sendRequest(InputStream in, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            out.write("GET / HTTP/1.0\r\n\r\n".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                sb.append((char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            String response = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (response.startsWith("HTTP/1.0 200 ") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                throw new IOException("Invalid response: " + response);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    // for some reason, ${test.src} has a different value when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // test is called from the script and when it is called directly...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    static String pathToStores = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    static String pathToStoresSH = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    static char[] passwd = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    static File PATH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private static KeyStore readKeyStore(String name) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        File file = new File(PATH, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        InputStream in = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        ks.load(in, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public static void main(PeerFactory peerFactory, String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        String relPath;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 2
diff changeset
   400
        if ((args != null) && (args.length > 0) && args[0].equals("sh")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            relPath = pathToStoresSH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            relPath = pathToStores;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        PATH = new File(System.getProperty("test.src", "."), relPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        CipherTest.peerFactory = peerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        System.out.print(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            "Initializing test '" + peerFactory.getName() + "'...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        secureRandom = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        secureRandom.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        trustStore = readKeyStore(trustStoreFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        keyStore = readKeyStore(keyStoreFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        KeyManagerFactory keyFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            KeyManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                KeyManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        keyFactory.init(keyStore, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        keyManager = (X509ExtendedKeyManager)keyFactory.getKeyManagers()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        trustManager = new AlwaysTrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        CipherTest cipherTest = new CipherTest(peerFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        Thread serverThread = new Thread(peerFactory.newServer(cipherTest),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            "Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        serverThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        System.out.println("Done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        cipherTest.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        time = System.currentTimeMillis() - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        System.out.println("Done. (" + time + " ms)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    static abstract class PeerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        abstract String getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        abstract Client newClient(CipherTest cipherTest) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        abstract Server newServer(CipherTest cipherTest) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   439
        boolean isSupported(String cipherSuite, String protocol) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   440
            // skip kerberos cipher suites
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   441
            if (cipherSuite.startsWith("TLS_KRB5")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   442
                System.out.println("Skipping unsupported test for " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   443
                                    cipherSuite + " of " + protocol);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   444
                return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   445
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   446
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   447
            // skip SSLv2Hello protocol
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   448
            if (protocol.equals("SSLv2Hello")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   449
                System.out.println("Skipping unsupported test for " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   450
                                    cipherSuite + " of " + protocol);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   451
                return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   452
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   453
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   454
            // ignore exportable cipher suite for TLSv1.1
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   455
            if (protocol.equals("TLSv1.1")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   456
                if (cipherSuite.indexOf("_EXPORT_WITH") != -1) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   457
                    System.out.println("Skipping obsoleted test for " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   458
                                        cipherSuite + " of " + protocol);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   459
                    return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   460
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   461
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   462
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
// we currently don't do any chain verification. we assume that works ok
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
// and we can speed up the test. we could also just add a plain certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
// chain comparision with our trusted certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
class AlwaysTrustManager implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public AlwaysTrustManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public void checkClientTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public void checkServerTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return new X509Certificate[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
class MyX509KeyManager extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private final X509ExtendedKeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private String authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    MyX509KeyManager(X509ExtendedKeyManager keyManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this.keyManager = keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    void setAuthType(String authType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        this.authType = "ECDSA".equals(authType) ? "EC" : authType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return keyManager.getClientAliases(authType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public String chooseClientAlias(String[] keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return keyManager.chooseClientAlias(new String[] {authType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            issuers, socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public String chooseEngineClientAlias(String[] keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (authType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return keyManager.chooseEngineClientAlias(new String[] {authType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            issuers, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public String chooseServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public String chooseEngineServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        throw new UnsupportedOperationException("Servers not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return keyManager.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return keyManager.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
class DaemonThreadFactory implements ThreadFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    final static ThreadFactory INSTANCE = new DaemonThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private final static ThreadFactory DEFAULT = Executors.defaultThreadFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        Thread t = DEFAULT.newThread(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
}