SSLSocketTemplate.java revision 16369:0d27cd70c137
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24//
25// Please run in othervm mode.  SunJSSE does not support dynamic system
26// properties, no way to re-use system properties in samevm/agentvm mode.
27//
28
29/*
30 * @test
31 * @bug 8161106 8170329
32 * @summary Improve SSLSocket test template
33 * @run main/othervm SSLSocketTemplate
34 */
35import java.io.*;
36import javax.net.ssl.*;
37import java.net.InetSocketAddress;
38import java.net.SocketTimeoutException;
39import java.security.KeyStore;
40import java.security.PrivateKey;
41import java.security.KeyFactory;
42import java.security.cert.Certificate;
43import java.security.cert.CertificateFactory;
44import java.security.spec.*;
45import java.util.Base64;
46
47import java.util.concurrent.CountDownLatch;
48import java.util.concurrent.TimeUnit;
49
50/**
51 * Template to help speed your client/server tests.
52 *
53 * Two examples that use this template:
54 *    test/sun/security/ssl/ServerHandshaker/AnonCipherWithWantClientAuth.java
55 *    test/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java
56 */
57public class SSLSocketTemplate {
58
59    /*
60     * ==================
61     * Run the test case.
62     */
63    public static void main(String[] args) throws Exception {
64        (new SSLSocketTemplate()).run();
65    }
66
67    /*
68     * Run the test case.
69     */
70    public void run() throws Exception {
71        bootup();
72    }
73
74    /*
75     * Define the server side application of the test for the specified socket.
76     */
77    protected void runServerApplication(SSLSocket socket) throws Exception {
78        // here comes the test logic
79        InputStream sslIS = socket.getInputStream();
80        OutputStream sslOS = socket.getOutputStream();
81
82        sslIS.read();
83        sslOS.write(85);
84        sslOS.flush();
85    }
86
87    /*
88     * Define the client side application of the test for the specified socket.
89     * This method is used if the returned value of
90     * isCustomizedClientConnection() is false.
91     *
92     * @param socket may be null is no client socket is generated.
93     *
94     * @see #isCustomizedClientConnection()
95     */
96    protected void runClientApplication(SSLSocket socket) throws Exception {
97        InputStream sslIS = socket.getInputStream();
98        OutputStream sslOS = socket.getOutputStream();
99
100        sslOS.write(280);
101        sslOS.flush();
102        sslIS.read();
103    }
104
105    /*
106     * Define the client side application of the test for the specified
107     * server port.  This method is used if the returned value of
108     * isCustomizedClientConnection() is true.
109     *
110     * Note that the client need to connect to the server port by itself
111     * for the actual message exchange.
112     *
113     * @see #isCustomizedClientConnection()
114     */
115    protected void runClientApplication(int serverPort) throws Exception {
116        // blank
117    }
118
119    /*
120     * Create an instance of SSLContext for client use.
121     */
122    protected SSLContext createClientSSLContext() throws Exception {
123        return createSSLContext(trustedCertStrs,
124                endEntityCertStrs, endEntityPrivateKeys,
125                endEntityPrivateKeyNames,
126                getClientContextParameters());
127    }
128
129    /*
130     * Create an instance of SSLContext for server use.
131     */
132    protected SSLContext createServerSSLContext() throws Exception {
133        return createSSLContext(trustedCertStrs,
134                endEntityCertStrs, endEntityPrivateKeys,
135                endEntityPrivateKeyNames,
136                getServerContextParameters());
137    }
138
139    /*
140     * The parameters used to configure SSLContext.
141     */
142    protected static final class ContextParameters {
143        final String contextProtocol;
144        final String tmAlgorithm;
145        final String kmAlgorithm;
146
147        ContextParameters(String contextProtocol,
148                String tmAlgorithm, String kmAlgorithm) {
149
150            this.contextProtocol = contextProtocol;
151            this.tmAlgorithm = tmAlgorithm;
152            this.kmAlgorithm = kmAlgorithm;
153        }
154    }
155
156    /*
157     * Get the client side parameters of SSLContext.
158     */
159    protected ContextParameters getClientContextParameters() {
160        return new ContextParameters("TLS", "PKIX", "NewSunX509");
161    }
162
163    /*
164     * Get the server side parameters of SSLContext.
165     */
166    protected ContextParameters getServerContextParameters() {
167        return new ContextParameters("TLS", "PKIX", "NewSunX509");
168    }
169
170    /*
171     * Does the client side use customized connection other than
172     * explicit Socket.connect(), for example, URL.openConnection()?
173     */
174    protected boolean isCustomizedClientConnection() {
175        return false;
176    }
177
178    /*
179     * Configure the server side socket.
180     */
181    protected void configureServerSocket(SSLServerSocket socket) {
182
183    }
184
185    /*
186     * =============================================
187     * Define the client and server side operations.
188     *
189     * If the client or server is doing some kind of object creation
190     * that the other side depends on, and that thread prematurely
191     * exits, you may experience a hang.  The test harness will
192     * terminate all hung threads after its timeout has expired,
193     * currently 3 minutes by default, but you might try to be
194     * smart about it....
195     */
196
197    /*
198     * Is the server ready to serve?
199     */
200    private final CountDownLatch serverCondition = new CountDownLatch(1);
201
202    /*
203     * Is the client ready to handshake?
204     */
205    private final CountDownLatch clientCondition = new CountDownLatch(1);
206
207    /*
208     * What's the server port?  Use any free port by default
209     */
210    private volatile int serverPort = 0;
211
212    /*
213     * Define the server side of the test.
214     */
215    private void doServerSide() throws Exception {
216        // kick start the server side service
217        SSLContext context = createServerSSLContext();
218        SSLServerSocketFactory sslssf = context.getServerSocketFactory();
219        SSLServerSocket sslServerSocket =
220                (SSLServerSocket)sslssf.createServerSocket(serverPort);
221        configureServerSocket(sslServerSocket);
222        serverPort = sslServerSocket.getLocalPort();
223
224        // Signal the client, the server is ready to accept connection.
225        serverCondition.countDown();
226
227        // Try to accept a connection in 30 seconds.
228        SSLSocket sslSocket;
229        try {
230            sslServerSocket.setSoTimeout(30000);
231            sslSocket = (SSLSocket)sslServerSocket.accept();
232        } catch (SocketTimeoutException ste) {
233            // Ignore the test case if no connection within 30 seconds.
234            System.out.println(
235                "No incoming client connection in 30 seconds. " +
236                "Ignore in server side.");
237            return;
238        } finally {
239            sslServerSocket.close();
240        }
241
242        // handle the connection
243        try {
244            // Is it the expected client connection?
245            //
246            // Naughty test cases or third party routines may try to
247            // connection to this server port unintentionally.  In
248            // order to mitigate the impact of unexpected client
249            // connections and avoid intermittent failure, it should
250            // be checked that the accepted connection is really linked
251            // to the expected client.
252            boolean clientIsReady =
253                    clientCondition.await(30L, TimeUnit.SECONDS);
254
255            if (clientIsReady) {
256                // Run the application in server side.
257                runServerApplication(sslSocket);
258            } else {    // Otherwise, ignore
259                // We don't actually care about plain socket connections
260                // for TLS communication testing generally.  Just ignore
261                // the test if the accepted connection is not linked to
262                // the expected client or the client connection timeout
263                // in 30 seconds.
264                System.out.println(
265                        "The client is not the expected one or timeout. " +
266                        "Ignore in server side.");
267            }
268        } finally {
269            sslSocket.close();
270        }
271    }
272
273    /*
274     * Define the client side of the test.
275     */
276    private void doClientSide() throws Exception {
277
278        // Wait for server to get started.
279        //
280        // The server side takes care of the issue if the server cannot
281        // get started in 90 seconds.  The client side would just ignore
282        // the test case if the serer is not ready.
283        boolean serverIsReady =
284                serverCondition.await(90L, TimeUnit.SECONDS);
285        if (!serverIsReady) {
286            System.out.println(
287                    "The server is not ready yet in 90 seconds. " +
288                    "Ignore in client side.");
289            return;
290        }
291
292        if (isCustomizedClientConnection()) {
293            // Signal the server, the client is ready to communicate.
294            clientCondition.countDown();
295
296            // Run the application in client side.
297            runClientApplication(serverPort);
298
299            return;
300        }
301
302        SSLContext context = createClientSSLContext();
303        SSLSocketFactory sslsf = context.getSocketFactory();
304
305        try (SSLSocket sslSocket = (SSLSocket)sslsf.createSocket()) {
306            try {
307                sslSocket.connect(
308                        new InetSocketAddress("localhost", serverPort), 15000);
309            } catch (IOException ioe) {
310                // The server side may be impacted by naughty test cases or
311                // third party routines, and cannot accept connections.
312                //
313                // Just ignore the test if the connection cannot be
314                // established.
315                System.out.println(
316                        "Cannot make a connection in 15 seconds. " +
317                        "Ignore in client side.");
318                return;
319            }
320
321            // OK, here the client and server get connected.
322
323            // Signal the server, the client is ready to communicate.
324            clientCondition.countDown();
325
326            // There is still a chance in theory that the server thread may
327            // wait client-ready timeout and then quit.  The chance should
328            // be really rare so we don't consider it until it becomes a
329            // real problem.
330
331            // Run the application in client side.
332            runClientApplication(sslSocket);
333        }
334    }
335
336    /*
337     * =============================================
338     * Stuffs to customize the SSLContext instances.
339     */
340
341    /*
342     * =======================================
343     * Certificates and keys used in the test.
344     */
345    // Trusted certificates.
346    private final static String[] trustedCertStrs = {
347        // SHA256withECDSA, curve prime256v1
348        // Validity
349        //    Not Before: Nov 25 04:19:51 2016 GMT
350        //    Not After : Nov  5 04:19:51 2037 GMT
351        // Subject Key Identifier:
352        //    CA:48:E8:00:C1:42:BD:59:9B:79:D9:B4:B4:CE:3F:68:0C:C8:C4:0C
353        "-----BEGIN CERTIFICATE-----\n" +
354        "MIICHDCCAcGgAwIBAgIJAJtKs6ZEcVjxMAoGCCqGSM49BAMCMDsxCzAJBgNVBAYT\n" +
355        "AlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZj\n" +
356        "ZTAeFw0xNjExMjUwNDE5NTFaFw0zNzExMDUwNDE5NTFaMDsxCzAJBgNVBAYTAlVT\n" +
357        "MQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZjZTBZ\n" +
358        "MBMGByqGSM49AgEGCCqGSM49AwEHA0IABKMO/AFDHZia65RaqMIBX7WBdtzFj8fz\n" +
359        "ggqMADLJhoszS6qfTUDYskETw3uHfB3KAOENsoKX446qFFPuVjxS1aejga0wgaow\n" +
360        "HQYDVR0OBBYEFMpI6ADBQr1Zm3nZtLTOP2gMyMQMMGsGA1UdIwRkMGKAFMpI6ADB\n" +
361        "Qr1Zm3nZtLTOP2gMyMQMoT+kPTA7MQswCQYDVQQGEwJVUzENMAsGA1UEChMESmF2\n" +
362        "YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2WCCQCbSrOmRHFY8TAPBgNV\n" +
363        "HRMBAf8EBTADAQH/MAsGA1UdDwQEAwIBBjAKBggqhkjOPQQDAgNJADBGAiEA5cJ/\n" +
364        "jirBbXxzpZ6kdp/Zb/yrIBnr4jiPGJTLgRTb8s4CIQChUDfP1Zqg0qJVfqFNaL4V\n" +
365        "a0EAeJHXGZnvCGGqHzoxkg==\n" +
366        "-----END CERTIFICATE-----",
367
368        // SHA256withRSA, 2048 bits
369        // Validity
370        //     Not Before: Nov 25 04:20:02 2016 GMT
371        //     Not After : Nov  5 04:20:02 2037 GMT
372        // Subject Key Identifier:
373        //     A2:DC:55:38:E4:47:7C:8B:D3:E0:CA:FA:AD:3A:C8:4A:DD:12:A0:8E
374        "-----BEGIN CERTIFICATE-----\n" +
375        "MIIDpzCCAo+gAwIBAgIJAO586A+hYNXaMA0GCSqGSIb3DQEBCwUAMDsxCzAJBgNV\n" +
376        "BAYTAlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2Vy\n" +
377        "aXZjZTAeFw0xNjExMjUwNDIwMDJaFw0zNzExMDUwNDIwMDJaMDsxCzAJBgNVBAYT\n" +
378        "AlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZj\n" +
379        "ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMm3veDSU4zKXO0aAHos\n" +
380        "cFRXGLBTe+MUJXAtlkNyx7VKaMZNt5wrUuqzyi/r0LFUdRfNCzZf3X8s8HPHQVii\n" +
381        "29tK0y/yeTn4sJTATSmGaAysMJQpKQcfAQ79ItcEGQ721TFQZ3kOBdgp3t/yUYAP\n" +
382        "K2tFze/QbIw72LE52SBnPPPTzyimNw7Ai2MLl4eQlyMkTs7JS07zIiAO5QYbS8s+\n" +
383        "1NW0A3Y+d0B0q8wYEoHGq7QVjOKlSAksfO0tzi4l0Zu6Uf+J5kMAyZ4ZFgEJvGvw\n" +
384        "y/OKJ+soRFH/5cy1SL8B6AWD1y7WXugeeHTHRW1eOjTxqfG1rZqTVd2lfOMER8y1\n" +
385        "bXcCAwEAAaOBrTCBqjAdBgNVHQ4EFgQUotxVOORHfIvT4Mr6rTrISt0SoI4wawYD\n" +
386        "VR0jBGQwYoAUotxVOORHfIvT4Mr6rTrISt0SoI6hP6Q9MDsxCzAJBgNVBAYTAlVT\n" +
387        "MQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZjZYIJ\n" +
388        "AO586A+hYNXaMA8GA1UdEwEB/wQFMAMBAf8wCwYDVR0PBAQDAgEGMA0GCSqGSIb3\n" +
389        "DQEBCwUAA4IBAQAtNZJSFeFU6Yid0WSCs2qLAVaTyHsUNSUPUFIIhFAKxdP4DFS0\n" +
390        "+aeOFwdqizAU3kReAYULsfwEBgO51lPBSpB+9coUNQwu7cc8Q5Xqw/llRB0PrINS\n" +
391        "pZl7PW6Ur2ExTBocnUT9A/nhm8iO4PFD/Ly11sf5OdZihhX69NJ2h3a3gcrLjIpO\n" +
392        "L/ewTOgSi5xs+AGGQa+huN3YVL7dh+/rCUvMZVSBX5PloxWS5MMJi0Ui6YjwCFGO\n" +
393        "J4G9m7pI+gJs/x1UG0AgplMF2UCFfiY1SAeE2nKAeOOXAXEwEjFy0ToVTmqXx7fg\n" +
394        "m9YjhuePxlBrd2DF/YW0pc8gmLnrtm4rKPLz\n" +
395        "-----END CERTIFICATE-----",
396
397        // SHA256withDSA, 2048 bits
398        // Validity
399        //     Not Before: Nov 25 04:19:56 2016 GMT
400        //     Not After : Nov  5 04:19:56 2037 GMT
401        // Subject Key Identifier:
402        //     19:46:10:43:24:6A:A5:14:BE:E2:92:01:79:F0:4C:5F:E1:AE:81:B5
403        "-----BEGIN CERTIFICATE-----\n" +
404        "MIIFCzCCBLGgAwIBAgIJAOnEn6YZD/sAMAsGCWCGSAFlAwQDAjA7MQswCQYDVQQG\n" +
405        "EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +
406        "Y2UwHhcNMTYxMTI1MDQxOTU2WhcNMzcxMTA1MDQxOTU2WjA7MQswCQYDVQQGEwJV\n" +
407        "UzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2Uw\n" +
408        "ggNGMIICOQYHKoZIzjgEATCCAiwCggEBAJa17ZYdIChv5yeYuPK3zXxgUEGGsdUD\n" +
409        "AzfQUxtMryCtc3aNgWLxsN1/QYvp9v+hh4twnG20VemCEH9Qlx06Pxg74DwSOA83\n" +
410        "SecO2y7cdgmrHpep9drxKbXVZafwBhbTSwhV+IDO7EO6+LaRvZuya/YOqNIE9ENx\n" +
411        "FVk0NrNsDB6pfDEXZsCZALMN2mcl8KGn1q7vdzJQUEV7F6uLGP33znVfmQyWJH3Y\n" +
412        "W09WVCFXHvDzZHGXDO2O2QwIU1B5AsXnOGeLnKgXzErCoNKwUbVFP0W0OVeJo4pc\n" +
413        "ZfL/8TVELGG90AuuH1V3Gsq6PdzCDPw4Uv/v5m7/6kwXqBQxAJA7jhMCIQCORIGV\n" +
414        "mHy5nBLRhIP4vC7vsTxb4CTApPVmZeL5gTIwtQKCAQB2VZLY22k2+IQM6deRGK3L\n" +
415        "l7tPChGrKnGmTbtUThIza70Sp9DmTBiLzMEY+IgG8kYuT5STVxWjs0cxXCKZGMQW\n" +
416        "tioMtiXPA2M3HA0/8E0mDLSmzb0RAd2xxnDyGsuqo1eVmx7PLjN3bn3EjhD/+j3d\n" +
417        "Jx3ZVScMGyq7sVWppUvpudEXi+2etf6GUHjrcX27juo7u4zQ1ezC/HYG1H+jEFqG\n" +
418        "hdQ6b7H+LBHZH9LegOyIZTMrzAY/TwIr77sXrNJWRoxmDErKB+8bRDybYhNJswlZ\n" +
419        "m0N5YYUlPmepgbl6XzwCv0y0d81h3bayqIPLXEUtRAl9GuM0hNAlA1Y+qSn9xLFY\n" +
420        "A4IBBQACggEAZgWC0uflwqQQP1GRU1tolmFZwyVtKre7SjYgCeQBrOa0Xnj/SLaD\n" +
421        "g1HZ1oH0hccaR/45YouJiCretbbsQ77KouldGSGqTHJgRL75Y2z5uvxa60+YxZ0Z\n" +
422        "v8xvZnj4seyOjgJLxSSYSPl5n/F70RaNiCLVz/kGe6OQ8KoAeQjdDTOHXCegO9KX\n" +
423        "tvhM7EaYc8CII9OIR7S7PXJW0hgLKynZcu/Unh02aM0ABh/uLmw1+tvo8e8KTp98\n" +
424        "NKYSVf6kV3/ya58n4h64UbIYL08JoKUM/5SFETcKAZTU0YKZbpWTM79oJMr8oYVk\n" +
425        "P9jKitNsXq0Xkzt5dSO0kfu/kM7zpnaFsqOBrTCBqjAdBgNVHQ4EFgQUGUYQQyRq\n" +
426        "pRS+4pIBefBMX+GugbUwawYDVR0jBGQwYoAUGUYQQyRqpRS+4pIBefBMX+GugbWh\n" +
427        "P6Q9MDsxCzAJBgNVBAYTAlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5K\n" +
428        "U1NFIFRlc3QgU2VyaXZjZYIJAOnEn6YZD/sAMA8GA1UdEwEB/wQFMAMBAf8wCwYD\n" +
429        "VR0PBAQDAgEGMAsGCWCGSAFlAwQDAgNHADBEAiAwBafz5RRR9nc4cCYoYuBlT/D9\n" +
430        "9eayhkjhBY/zYunypwIgNp/JnFR88/T4hh36QfSKBGXId9RBCM6uaOkOKnEGkps=\n" +
431        "-----END CERTIFICATE-----"
432        };
433
434    // End entity certificate.
435    private final static String[] endEntityCertStrs = {
436        // SHA256withECDSA, curve prime256v1
437        // Validity
438        //     Not Before: Nov 25 04:19:51 2016 GMT
439        //     Not After : Aug 12 04:19:51 2036 GMT
440        // Authority Key Identifier:
441        //     CA:48:E8:00:C1:42:BD:59:9B:79:D9:B4:B4:CE:3F:68:0C:C8:C4:0C
442        "-----BEGIN CERTIFICATE-----\n" +
443        "MIIB1zCCAXygAwIBAgIJAPFq2QL/nUNZMAoGCCqGSM49BAMCMDsxCzAJBgNVBAYT\n" +
444        "AlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZj\n" +
445        "ZTAeFw0xNjExMjUwNDE5NTFaFw0zNjA4MTIwNDE5NTFaMFUxCzAJBgNVBAYTAlVT\n" +
446        "MQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZjZTEY\n" +
447        "MBYGA1UEAwwPUmVncmVzc2lvbiBUZXN0MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD\n" +
448        "QgAE4yvRGVvy9iVATyuHPJVdX6+lh/GLm/sRJ5qLT/3PVFOoNIvlEVNiARo7xhyj\n" +
449        "2p6bnf32gNg5Ye+QCw20VUv9E6NPME0wCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBSO\n" +
450        "hHlHZQp9hyBfSGTSQWeszqMXejAfBgNVHSMEGDAWgBTKSOgAwUK9WZt52bS0zj9o\n" +
451        "DMjEDDAKBggqhkjOPQQDAgNJADBGAiEAu3t6cvFglBAZfkhZlEwB04ZjUFqyfiRj\n" +
452        "4Hr275E4ZoQCIQDUEonJHlmA19J6oobfR5lYsmoqPm1r0DPm/IiNNKGKKA==\n" +
453        "-----END CERTIFICATE-----",
454
455        // SHA256withRSA, 2048 bits
456        // Validity
457        //     Not Before: Nov 25 04:20:02 2016 GMT
458        //     Not After : Aug 12 04:20:02 2036 GMT
459        // Authority Key Identifier:
460        //     A2:DC:55:38:E4:47:7C:8B:D3:E0:CA:FA:AD:3A:C8:4A:DD:12:A0:8E
461        "-----BEGIN CERTIFICATE-----\n" +
462        "MIIDdjCCAl6gAwIBAgIJAJDcIGOlAmBmMA0GCSqGSIb3DQEBCwUAMDsxCzAJBgNV\n" +
463        "BAYTAlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2Vy\n" +
464        "aXZjZTAeFw0xNjExMjUwNDIwMDJaFw0zNjA4MTIwNDIwMDJaMFUxCzAJBgNVBAYT\n" +
465        "AlVTMQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZj\n" +
466        "ZTEYMBYGA1UEAwwPUmVncmVzc2lvbiBUZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOC\n" +
467        "AQ8AMIIBCgKCAQEAp0dHrifTg2aY0sH03f2JjK2fW4DL6gLDKq0YirsNE07z54LF\n" +
468        "IeeDio49XwPjB7OpbUTC1hf/YKZ7XiRWyPa1rYozZ88emhZt+cpkyKz+nmW4avlA\n" +
469        "WnrV+gx4+bU9T+WuBWdAraBcq27Y1I26yfCEtC8k3+O0sdlHbhasF+BUDmX/n4+n\n" +
470        "ifJdbNm5eSDx8eFYHFTdjhAud3An2X6QD9WWSoJpPdDi4utHhFAlxW6osjJxsAPv\n" +
471        "zo8YsqmpCMjZcEks4ZsqiZKKiWUWUAjCcbMerDPDX29fyeo348uztvJsmNRzfcwl\n" +
472        "FtwxpYdxuZqwHi2QoNaQTGXjjbZFmjn7qEkjXwIDAQABo2MwYTALBgNVHQ8EBAMC\n" +
473        "A+gwHQYDVR0OBBYEFP+henfufE6Znr60lRkmayadVdxnMB8GA1UdIwQYMBaAFKLc\n" +
474        "VTjkR3yL0+DK+q06yErdEqCOMBIGA1UdEQEB/wQIMAaHBH8AAAEwDQYJKoZIhvcN\n" +
475        "AQELBQADggEBAK56pV2XoAIkrHFTCkWtYX518nuvkzN6a6BqPKALQlmlbJnq/lhV\n" +
476        "tPQx79b0j7tow28l2ze/3M0hRb5Ft/d/7mITZNMR+0owk4U51AU2NacRt7fpoxu5\n" +
477        "wX3hTa4VgX2+BAXeoWF+Yzy6Jj5gAVmSLzBnkTUH0d+EyL1pp+DFE3QdvZqf3+nP\n" +
478        "zkxz15h3iW8FwI+7/19MX2j2XB/sG8mJpqoszWw8lM4qCa2eWyCbqSHhPi+/+rGg\n" +
479        "dDG5uzZeOC845GEH2T3tHDA+F3WwcZG/W+4RR6ZaaHlqPKKMcwFL73YbsqdCiKBv\n" +
480        "p6sXrhIiP0oXImRBRLDlidj5TIOLfAtNM9A=\n" +
481        "-----END CERTIFICATE-----",
482
483        // SHA256withDSA, 2048 bits
484        // Validity
485        //    Not Before: Nov 25 04:19:56 2016 GMT
486        //    Not After : Aug 12 04:19:56 2036 GMT
487        // Authority Key Identifier:
488        //     19:46:10:43:24:6A:A5:14:BE:E2:92:01:79:F0:4C:5F:E1:AE:81:B5
489        "-----BEGIN CERTIFICATE-----\n" +
490        "MIIE2jCCBICgAwIBAgIJAONcI1oba9V9MAsGCWCGSAFlAwQDAjA7MQswCQYDVQQG\n" +
491        "EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" +
492        "Y2UwHhcNMTYxMTI1MDQxOTU2WhcNMzYwODEyMDQxOTU2WjBVMQswCQYDVQQGEwJV\n" +
493        "UzENMAsGA1UECgwESmF2YTEdMBsGA1UECwwUU3VuSlNTRSBUZXN0IFNlcml2Y2Ux\n" +
494        "GDAWBgNVBAMMD1JlZ3Jlc3Npb24gVGVzdDCCA0YwggI5BgcqhkjOOAQBMIICLAKC\n" +
495        "AQEAlrXtlh0gKG/nJ5i48rfNfGBQQYax1QMDN9BTG0yvIK1zdo2BYvGw3X9Bi+n2\n" +
496        "/6GHi3CcbbRV6YIQf1CXHTo/GDvgPBI4DzdJ5w7bLtx2Casel6n12vEptdVlp/AG\n" +
497        "FtNLCFX4gM7sQ7r4tpG9m7Jr9g6o0gT0Q3EVWTQ2s2wMHql8MRdmwJkAsw3aZyXw\n" +
498        "oafWru93MlBQRXsXq4sY/ffOdV+ZDJYkfdhbT1ZUIVce8PNkcZcM7Y7ZDAhTUHkC\n" +
499        "xec4Z4ucqBfMSsKg0rBRtUU/RbQ5V4mjilxl8v/xNUQsYb3QC64fVXcayro93MIM\n" +
500        "/DhS/+/mbv/qTBeoFDEAkDuOEwIhAI5EgZWYfLmcEtGEg/i8Lu+xPFvgJMCk9WZl\n" +
501        "4vmBMjC1AoIBAHZVktjbaTb4hAzp15EYrcuXu08KEasqcaZNu1ROEjNrvRKn0OZM\n" +
502        "GIvMwRj4iAbyRi5PlJNXFaOzRzFcIpkYxBa2Kgy2Jc8DYzccDT/wTSYMtKbNvREB\n" +
503        "3bHGcPIay6qjV5WbHs8uM3dufcSOEP/6Pd0nHdlVJwwbKruxVamlS+m50ReL7Z61\n" +
504        "/oZQeOtxfbuO6ju7jNDV7ML8dgbUf6MQWoaF1Dpvsf4sEdkf0t6A7IhlMyvMBj9P\n" +
505        "Aivvuxes0lZGjGYMSsoH7xtEPJtiE0mzCVmbQ3lhhSU+Z6mBuXpfPAK/TLR3zWHd\n" +
506        "trKog8tcRS1ECX0a4zSE0CUDVj6pKf3EsVgDggEFAAKCAQBEGmdP55PyE3M+Q3fU\n" +
507        "dCGq0sbKw/04xPVhaNYRnRKNR82n+wb8bMCI1vvFqXy1BB6svti4mTHbQZ8+bQXm\n" +
508        "gyce67uYMwIa5BIk6omNGCeW/kd4ruPgyFxeb6O/Y/7w6AWyRmQttlxRA5M5OhSC\n" +
509        "tVS4oVC1KK1EfHAUh7mu8S8GrWJoJAWA3PM97Oy/HSGCEUl6HGEu1m7FHPhOKeYG\n" +
510        "cLkSaov5cbCYO76smHchI+tdUciVqeL3YKQdS+KAzsQoeAZIu/WpbaI1V+5/rSG1\n" +
511        "I94uBITLCjlJfJZ1aredCDrRXOFH7qgSBhM8/WzwFpFCnnpbSKMgrcrKubsFmW9E\n" +
512        "jQhXo2MwYTALBgNVHQ8EBAMCA+gwHQYDVR0OBBYEFNA9PhQOjB+05fxxXPNqe0OT\n" +
513        "doCjMB8GA1UdIwQYMBaAFBlGEEMkaqUUvuKSAXnwTF/hroG1MBIGA1UdEQEB/wQI\n" +
514        "MAaHBH8AAAEwCwYJYIZIAWUDBAMCA0cAMEQCIE0LM2sZi+L8tjH9sgjLEwJmYZvO\n" +
515        "yqNfQnXrkTCb+MLMAiBZLaRTVJrOW3edQjum+SonKKuiN22bRclO6pGuNRCtng==\n" +
516        "-----END CERTIFICATE-----"
517        };
518
519    // Private key in the format of PKCS#8.
520    private final static String[] endEntityPrivateKeys = {
521        //
522        // EC private key related to cert endEntityCertStrs[0].
523        //
524        "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgGAy4Pxrd2keM7AdP\n" +
525        "VNUMEO5iO681v4/tstVGfdXkCTuhRANCAATjK9EZW/L2JUBPK4c8lV1fr6WH8Yub\n" +
526        "+xEnmotP/c9UU6g0i+URU2IBGjvGHKPanpud/faA2Dlh75ALDbRVS/0T",
527
528        //
529        // RSA private key related to cert endEntityCertStrs[1].
530        //
531        "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCnR0euJ9ODZpjS\n" +
532        "wfTd/YmMrZ9bgMvqAsMqrRiKuw0TTvPngsUh54OKjj1fA+MHs6ltRMLWF/9gpnte\n" +
533        "JFbI9rWtijNnzx6aFm35ymTIrP6eZbhq+UBaetX6DHj5tT1P5a4FZ0CtoFyrbtjU\n" +
534        "jbrJ8IS0LyTf47Sx2UduFqwX4FQOZf+fj6eJ8l1s2bl5IPHx4VgcVN2OEC53cCfZ\n" +
535        "fpAP1ZZKgmk90OLi60eEUCXFbqiyMnGwA+/OjxiyqakIyNlwSSzhmyqJkoqJZRZQ\n" +
536        "CMJxsx6sM8Nfb1/J6jfjy7O28myY1HN9zCUW3DGlh3G5mrAeLZCg1pBMZeONtkWa\n" +
537        "OfuoSSNfAgMBAAECggEAWnAHKPkPObN2XDvQj1RL0WrtBSOVG2dy7Ne4tQh8ATxm\n" +
538        "UXw56CKq03YjaANJ8xgHObQ7QlSnFTHs8PDkmrIHd1OIh09LVDNcMfhilLwyzKBi\n" +
539        "HDO1vzU6Cn5DyX1bMJ8UfodcSIKyl1zOjdwyaItIs8HpRcJuJtk57SME18PIrh9H\n" +
540        "EWchWSxTvPvKDY2bhb4vBMgVPfTQO3yc8gY/1J5vKPqDpyEuCGjV13zd/AoL/u5A\n" +
541        "sG10hQ2veJ9KAn1xwPwEoAkCdNLL8vPB1rCbeMZNamqHZRihfoOgDnnJIf3FtUFF\n" +
542        "8bS2FM2kGQR+05SZdhBmJl0obPrbBWil/2vxqeFrgQKBgQDZl1yQsFss2BKK2VAh\n" +
543        "9PKc8tU1v6RpHQZhJEDSct2slHQS5DF6bWl5kJiPevXSvcnjgoPamZf7Joii+Rds\n" +
544        "3xtPQmRw2vQmXBQzQS1YoRuh4hUlkdFWCjUNNg1kvipue6FO4PVg3ViP7llN8PXK\n" +
545        "rSpVxn0a36UiN9jN2zMOUs6tJwKBgQDEzlqa7DghMli7TUdL44uvD2/ceiHqHMMw\n" +
546        "5eLsEHeRVn/EVU99beKw/dAOGwRssTpCd9h7fwzQ2503/Qb/Goe0nKE7+xvt3/sE\n" +
547        "n2Y8Qfv1W1+hGb2qU2jhQaR5bZrLZp0+BgRuQ4ttpYvzopYe4FLZWhDBA0zsGyu0\n" +
548        "nCi7lUSrCQKBgGeGYW8hyS9r2l6fiEWvsiLEUnbRKFsuiRN82S6HojpzI0q9sWDL\n" +
549        "X6yMBFn3qa/LxpttRGikPTAsJERN+Tw+ZlLuhrU/J3x8wMumDfomJOx/kYofd5bV\n" +
550        "ImqXtgWhiLSqM5RA6d5dUb6hK3Iu2/LDMuo+ltVLZNkD8y32RbNh6J1vAoGAbLqQ\n" +
551        "pgyhSf3Vtc0Q+aVB87p0k3tKJ1wynl4zSzYhyMLgHakAHIzL8/qVqmVUwXP8euJZ\n" +
552        "UIk1nGHobxk0d1XB6Y+rKEcn+/iFZt1ljx7pQ3ly0L824NXqGKC6bHeYUI1li/Gp\n" +
553        "Gv3oFvCh7D1D8NUAEKLIpMndAohUUhkAC/qAkHkCgYEAzSIarDNquayV+umC1SXm\n" +
554        "Zo6XLuzWjudLxPd2lyCfwR2aRKlrb+5OFYErX+RSLyCJmaqVZMyXP09PBIvNXu2Z\n" +
555        "+gbx5WUC+kA+6zdKEPXowei6i6EHMXYT2AL7395ZbPajZjsCduE3WuUztuHrhtMm\n" +
556        "JI+k1o4rCnSLlX4gWdN1oTs=",
557
558        //
559        // DSA private key related to cert endEntityCertStrs[2].
560        //
561        "MIICZAIBADCCAjkGByqGSM44BAEwggIsAoIBAQCWte2WHSAob+cnmLjyt818YFBB\n" +
562        "hrHVAwM30FMbTK8grXN2jYFi8bDdf0GL6fb/oYeLcJxttFXpghB/UJcdOj8YO+A8\n" +
563        "EjgPN0nnDtsu3HYJqx6XqfXa8Sm11WWn8AYW00sIVfiAzuxDuvi2kb2bsmv2DqjS\n" +
564        "BPRDcRVZNDazbAweqXwxF2bAmQCzDdpnJfChp9au73cyUFBFexerixj99851X5kM\n" +
565        "liR92FtPVlQhVx7w82RxlwztjtkMCFNQeQLF5zhni5yoF8xKwqDSsFG1RT9FtDlX\n" +
566        "iaOKXGXy//E1RCxhvdALrh9VdxrKuj3cwgz8OFL/7+Zu/+pMF6gUMQCQO44TAiEA\n" +
567        "jkSBlZh8uZwS0YSD+Lwu77E8W+AkwKT1ZmXi+YEyMLUCggEAdlWS2NtpNviEDOnX\n" +
568        "kRity5e7TwoRqypxpk27VE4SM2u9EqfQ5kwYi8zBGPiIBvJGLk+Uk1cVo7NHMVwi\n" +
569        "mRjEFrYqDLYlzwNjNxwNP/BNJgy0ps29EQHdscZw8hrLqqNXlZsezy4zd259xI4Q\n" +
570        "//o93Scd2VUnDBsqu7FVqaVL6bnRF4vtnrX+hlB463F9u47qO7uM0NXswvx2BtR/\n" +
571        "oxBahoXUOm+x/iwR2R/S3oDsiGUzK8wGP08CK++7F6zSVkaMZgxKygfvG0Q8m2IT\n" +
572        "SbMJWZtDeWGFJT5nqYG5el88Ar9MtHfNYd22sqiDy1xFLUQJfRrjNITQJQNWPqkp\n" +
573        "/cSxWAQiAiAKHYbYwEy0XS9J0MeKQmqPswn0nCJKvH+esfMKkZvV3w=="
574        };
575
576    // Private key names of endEntityPrivateKeys.
577    private final static String[] endEntityPrivateKeyNames = {
578        "EC",
579        "RSA",
580        "DSA",
581        };
582
583    /*
584     * Create an instance of SSLContext with the specified trust/key materials.
585     */
586    private SSLContext createSSLContext(
587            String[] trustedMaterials,
588            String[] keyMaterialCerts,
589            String[] keyMaterialKeys,
590            String[] keyMaterialKeyAlgs,
591            ContextParameters params) throws Exception {
592
593        KeyStore ts = null;     // trust store
594        KeyStore ks = null;     // key store
595        char passphrase[] = "passphrase".toCharArray();
596
597        // Generate certificate from cert string.
598        CertificateFactory cf = CertificateFactory.getInstance("X.509");
599
600        // Import the trused certs.
601        ByteArrayInputStream is;
602        if (trustedMaterials != null && trustedMaterials.length != 0) {
603            ts = KeyStore.getInstance("JKS");
604            ts.load(null, null);
605
606            Certificate[] trustedCert =
607                    new Certificate[trustedMaterials.length];
608            for (int i = 0; i < trustedMaterials.length; i++) {
609                String trustedCertStr = trustedMaterials[i];
610
611                is = new ByteArrayInputStream(trustedCertStr.getBytes());
612                try {
613                    trustedCert[i] = cf.generateCertificate(is);
614                } finally {
615                    is.close();
616                }
617
618                ts.setCertificateEntry("trusted-cert-" + i, trustedCert[i]);
619            }
620        }
621
622        // Import the key materials.
623        //
624        // Note that certification pathes bigger than one are not supported yet.
625        boolean hasKeyMaterials =
626            (keyMaterialCerts != null) && (keyMaterialCerts.length != 0) &&
627            (keyMaterialKeys != null) && (keyMaterialKeys.length != 0) &&
628            (keyMaterialKeyAlgs != null) && (keyMaterialKeyAlgs.length != 0) &&
629            (keyMaterialCerts.length == keyMaterialKeys.length) &&
630            (keyMaterialCerts.length == keyMaterialKeyAlgs.length);
631        if (hasKeyMaterials) {
632            ks = KeyStore.getInstance("JKS");
633            ks.load(null, null);
634
635            for (int i = 0; i < keyMaterialCerts.length; i++) {
636                String keyCertStr = keyMaterialCerts[i];
637
638                // generate the private key.
639                PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
640                    Base64.getMimeDecoder().decode(keyMaterialKeys[i]));
641                KeyFactory kf =
642                    KeyFactory.getInstance(keyMaterialKeyAlgs[i]);
643                PrivateKey priKey = kf.generatePrivate(priKeySpec);
644
645                // generate certificate chain
646                is = new ByteArrayInputStream(keyCertStr.getBytes());
647                Certificate keyCert = null;
648                try {
649                    keyCert = cf.generateCertificate(is);
650                } finally {
651                    is.close();
652                }
653
654                Certificate[] chain = new Certificate[] { keyCert };
655
656                // import the key entry.
657                ks.setKeyEntry("cert-" + keyMaterialKeyAlgs[i],
658                        priKey, passphrase, chain);
659            }
660        }
661
662        // Create an SSLContext object.
663        TrustManagerFactory tmf =
664                TrustManagerFactory.getInstance(params.tmAlgorithm);
665        tmf.init(ts);
666
667        SSLContext context = SSLContext.getInstance(params.contextProtocol);
668        if (hasKeyMaterials && ks != null) {
669            KeyManagerFactory kmf =
670                    KeyManagerFactory.getInstance(params.kmAlgorithm);
671            kmf.init(ks, passphrase);
672
673            context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
674        } else {
675            context.init(null, tmf.getTrustManagers(), null);
676        }
677
678        return context;
679    }
680
681    /*
682     * =================================================
683     * Stuffs to boot up the client-server mode testing.
684     */
685    private Thread clientThread = null;
686    private Thread serverThread = null;
687    private volatile Exception serverException = null;
688    private volatile Exception clientException = null;
689
690    /*
691     * Should we run the client or server in a separate thread?
692     * Both sides can throw exceptions, but do you have a preference
693     * as to which side should be the main thread.
694     */
695    private static final boolean separateServerThread = false;
696
697    /*
698     * Boot up the testing, used to drive remainder of the test.
699     */
700    private void bootup() throws Exception {
701        Exception startException = null;
702        try {
703            if (separateServerThread) {
704                startServer(true);
705                startClient(false);
706            } else {
707                startClient(true);
708                startServer(false);
709            }
710        } catch (Exception e) {
711            startException = e;
712        }
713
714        /*
715         * Wait for other side to close down.
716         */
717        if (separateServerThread) {
718            if (serverThread != null) {
719                serverThread.join();
720            }
721        } else {
722            if (clientThread != null) {
723                clientThread.join();
724            }
725        }
726
727        /*
728         * When we get here, the test is pretty much over.
729         * Which side threw the error?
730         */
731        Exception local;
732        Exception remote;
733
734        if (separateServerThread) {
735            remote = serverException;
736            local = clientException;
737        } else {
738            remote = clientException;
739            local = serverException;
740        }
741
742        Exception exception = null;
743
744        /*
745         * Check various exception conditions.
746         */
747        if ((local != null) && (remote != null)) {
748            // If both failed, return the curthread's exception.
749            local.initCause(remote);
750            exception = local;
751        } else if (local != null) {
752            exception = local;
753        } else if (remote != null) {
754            exception = remote;
755        } else if (startException != null) {
756            exception = startException;
757        }
758
759        /*
760         * If there was an exception *AND* a startException,
761         * output it.
762         */
763        if (exception != null) {
764            if (exception != startException && startException != null) {
765                exception.addSuppressed(startException);
766            }
767            throw exception;
768        }
769
770        // Fall-through: no exception to throw!
771    }
772
773    private void startServer(boolean newThread) throws Exception {
774        if (newThread) {
775            serverThread = new Thread() {
776                @Override
777                public void run() {
778                    try {
779                        doServerSide();
780                    } catch (Exception e) {
781                        /*
782                         * Our server thread just died.
783                         *
784                         * Release the client, if not active already...
785                         */
786                        logException("Server died", e);
787                        serverException = e;
788                    }
789                }
790            };
791            serverThread.start();
792        } else {
793            try {
794                doServerSide();
795            } catch (Exception e) {
796                logException("Server failed", e);
797                serverException = e;
798            }
799        }
800    }
801
802    private void startClient(boolean newThread) throws Exception {
803        if (newThread) {
804            clientThread = new Thread() {
805                @Override
806                public void run() {
807                    try {
808                        doClientSide();
809                    } catch (Exception e) {
810                        /*
811                         * Our client thread just died.
812                         */
813                        logException("Client died", e);
814                        clientException = e;
815                    }
816                }
817            };
818            clientThread.start();
819        } else {
820            try {
821                doClientSide();
822            } catch (Exception e) {
823                logException("Client failed", e);
824                clientException = e;
825            }
826        }
827    }
828
829    private synchronized void logException(String prefix, Throwable cause) {
830        System.out.println(prefix + ": " + cause);
831        cause.printStackTrace(System.out);
832    }
833}
834