SSLSocketTemplate.java revision 16238:0ad126a1f49e
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 * ============================================= 180 * Define the client and server side operations. 181 * 182 * If the client or server is doing some kind of object creation 183 * that the other side depends on, and that thread prematurely 184 * exits, you may experience a hang. The test harness will 185 * terminate all hung threads after its timeout has expired, 186 * currently 3 minutes by default, but you might try to be 187 * smart about it.... 188 */ 189 190 /* 191 * Is the server ready to serve? 192 */ 193 private final CountDownLatch serverCondition = new CountDownLatch(1); 194 195 /* 196 * Is the client ready to handshake? 197 */ 198 private final CountDownLatch clientCondition = new CountDownLatch(1); 199 200 /* 201 * What's the server port? Use any free port by default 202 */ 203 private volatile int serverPort = 0; 204 205 /* 206 * Define the server side of the test. 207 */ 208 private void doServerSide() throws Exception { 209 // kick start the server side service 210 SSLContext context = createServerSSLContext(); 211 SSLServerSocketFactory sslssf = context.getServerSocketFactory(); 212 SSLServerSocket sslServerSocket = 213 (SSLServerSocket)sslssf.createServerSocket(serverPort); 214 serverPort = sslServerSocket.getLocalPort(); 215 216 // Signal the client, the server is ready to accept connection. 217 serverCondition.countDown(); 218 219 // Try to accept a connection in 30 seconds. 220 SSLSocket sslSocket; 221 try { 222 sslServerSocket.setSoTimeout(30000); 223 sslSocket = (SSLSocket)sslServerSocket.accept(); 224 } catch (SocketTimeoutException ste) { 225 // Ignore the test case if no connection within 30 seconds. 226 System.out.println( 227 "No incoming client connection in 30 seconds. " + 228 "Ignore in server side."); 229 return; 230 } finally { 231 sslServerSocket.close(); 232 } 233 234 // handle the connection 235 try { 236 // Is it the expected client connection? 237 // 238 // Naughty test cases or third party routines may try to 239 // connection to this server port unintentionally. In 240 // order to mitigate the impact of unexpected client 241 // connections and avoid intermittent failure, it should 242 // be checked that the accepted connection is really linked 243 // to the expected client. 244 boolean clientIsReady = 245 clientCondition.await(30L, TimeUnit.SECONDS); 246 247 if (clientIsReady) { 248 // Run the application in server side. 249 runServerApplication(sslSocket); 250 } else { // Otherwise, ignore 251 // We don't actually care about plain socket connections 252 // for TLS communication testing generally. Just ignore 253 // the test if the accepted connection is not linked to 254 // the expected client or the client connection timeout 255 // in 30 seconds. 256 System.out.println( 257 "The client is not the expected one or timeout. " + 258 "Ignore in server side."); 259 } 260 } finally { 261 sslSocket.close(); 262 } 263 } 264 265 /* 266 * Define the client side of the test. 267 */ 268 private void doClientSide() throws Exception { 269 270 // Wait for server to get started. 271 // 272 // The server side takes care of the issue if the server cannot 273 // get started in 90 seconds. The client side would just ignore 274 // the test case if the serer is not ready. 275 boolean serverIsReady = 276 serverCondition.await(90L, TimeUnit.SECONDS); 277 if (!serverIsReady) { 278 System.out.println( 279 "The server is not ready yet in 90 seconds. " + 280 "Ignore in client side."); 281 return; 282 } 283 284 if (isCustomizedClientConnection()) { 285 // Signal the server, the client is ready to communicate. 286 clientCondition.countDown(); 287 288 // Run the application in client side. 289 runClientApplication(serverPort); 290 291 return; 292 } 293 294 SSLContext context = createClientSSLContext(); 295 SSLSocketFactory sslsf = context.getSocketFactory(); 296 297 try (SSLSocket sslSocket = (SSLSocket)sslsf.createSocket()) { 298 try { 299 sslSocket.connect( 300 new InetSocketAddress("localhost", serverPort), 15000); 301 } catch (IOException ioe) { 302 // The server side may be impacted by naughty test cases or 303 // third party routines, and cannot accept connections. 304 // 305 // Just ignore the test if the connection cannot be 306 // established. 307 System.out.println( 308 "Cannot make a connection in 15 seconds. " + 309 "Ignore in client side."); 310 return; 311 } 312 313 // OK, here the client and server get connected. 314 315 // Signal the server, the client is ready to communicate. 316 clientCondition.countDown(); 317 318 // There is still a chance in theory that the server thread may 319 // wait client-ready timeout and then quit. The chance should 320 // be really rare so we don't consider it until it becomes a 321 // real problem. 322 323 // Run the application in client side. 324 runClientApplication(sslSocket); 325 } 326 } 327 328 /* 329 * ============================================= 330 * Stuffs to customize the SSLContext instances. 331 */ 332 333 /* 334 * ======================================= 335 * Certificates and keys used in the test. 336 */ 337 // Trusted certificates. 338 private final static String[] trustedCertStrs = { 339 // SHA256withECDSA, curve prime256v1 340 // Validity 341 // Not Before: Nov 25 04:19:51 2016 GMT 342 // Not After : Nov 5 04:19:51 2037 GMT 343 // Subject Key Identifier: 344 // CA:48:E8:00:C1:42:BD:59:9B:79:D9:B4:B4:CE:3F:68:0C:C8:C4:0C 345 "-----BEGIN CERTIFICATE-----\n" + 346 "MIICHDCCAcGgAwIBAgIJAJtKs6ZEcVjxMAoGCCqGSM49BAMCMDsxCzAJBgNVBAYT\n" + 347 "AlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZj\n" + 348 "ZTAeFw0xNjExMjUwNDE5NTFaFw0zNzExMDUwNDE5NTFaMDsxCzAJBgNVBAYTAlVT\n" + 349 "MQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZjZTBZ\n" + 350 "MBMGByqGSM49AgEGCCqGSM49AwEHA0IABKMO/AFDHZia65RaqMIBX7WBdtzFj8fz\n" + 351 "ggqMADLJhoszS6qfTUDYskETw3uHfB3KAOENsoKX446qFFPuVjxS1aejga0wgaow\n" + 352 "HQYDVR0OBBYEFMpI6ADBQr1Zm3nZtLTOP2gMyMQMMGsGA1UdIwRkMGKAFMpI6ADB\n" + 353 "Qr1Zm3nZtLTOP2gMyMQMoT+kPTA7MQswCQYDVQQGEwJVUzENMAsGA1UEChMESmF2\n" + 354 "YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2Y2WCCQCbSrOmRHFY8TAPBgNV\n" + 355 "HRMBAf8EBTADAQH/MAsGA1UdDwQEAwIBBjAKBggqhkjOPQQDAgNJADBGAiEA5cJ/\n" + 356 "jirBbXxzpZ6kdp/Zb/yrIBnr4jiPGJTLgRTb8s4CIQChUDfP1Zqg0qJVfqFNaL4V\n" + 357 "a0EAeJHXGZnvCGGqHzoxkg==\n" + 358 "-----END CERTIFICATE-----", 359 360 // SHA256withRSA, 2048 bits 361 // Validity 362 // Not Before: Nov 25 04:20:02 2016 GMT 363 // Not After : Nov 5 04:20:02 2037 GMT 364 // Subject Key Identifier: 365 // A2:DC:55:38:E4:47:7C:8B:D3:E0:CA:FA:AD:3A:C8:4A:DD:12:A0:8E 366 "-----BEGIN CERTIFICATE-----\n" + 367 "MIIDpzCCAo+gAwIBAgIJAO586A+hYNXaMA0GCSqGSIb3DQEBCwUAMDsxCzAJBgNV\n" + 368 "BAYTAlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2Vy\n" + 369 "aXZjZTAeFw0xNjExMjUwNDIwMDJaFw0zNzExMDUwNDIwMDJaMDsxCzAJBgNVBAYT\n" + 370 "AlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZj\n" + 371 "ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMm3veDSU4zKXO0aAHos\n" + 372 "cFRXGLBTe+MUJXAtlkNyx7VKaMZNt5wrUuqzyi/r0LFUdRfNCzZf3X8s8HPHQVii\n" + 373 "29tK0y/yeTn4sJTATSmGaAysMJQpKQcfAQ79ItcEGQ721TFQZ3kOBdgp3t/yUYAP\n" + 374 "K2tFze/QbIw72LE52SBnPPPTzyimNw7Ai2MLl4eQlyMkTs7JS07zIiAO5QYbS8s+\n" + 375 "1NW0A3Y+d0B0q8wYEoHGq7QVjOKlSAksfO0tzi4l0Zu6Uf+J5kMAyZ4ZFgEJvGvw\n" + 376 "y/OKJ+soRFH/5cy1SL8B6AWD1y7WXugeeHTHRW1eOjTxqfG1rZqTVd2lfOMER8y1\n" + 377 "bXcCAwEAAaOBrTCBqjAdBgNVHQ4EFgQUotxVOORHfIvT4Mr6rTrISt0SoI4wawYD\n" + 378 "VR0jBGQwYoAUotxVOORHfIvT4Mr6rTrISt0SoI6hP6Q9MDsxCzAJBgNVBAYTAlVT\n" + 379 "MQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZjZYIJ\n" + 380 "AO586A+hYNXaMA8GA1UdEwEB/wQFMAMBAf8wCwYDVR0PBAQDAgEGMA0GCSqGSIb3\n" + 381 "DQEBCwUAA4IBAQAtNZJSFeFU6Yid0WSCs2qLAVaTyHsUNSUPUFIIhFAKxdP4DFS0\n" + 382 "+aeOFwdqizAU3kReAYULsfwEBgO51lPBSpB+9coUNQwu7cc8Q5Xqw/llRB0PrINS\n" + 383 "pZl7PW6Ur2ExTBocnUT9A/nhm8iO4PFD/Ly11sf5OdZihhX69NJ2h3a3gcrLjIpO\n" + 384 "L/ewTOgSi5xs+AGGQa+huN3YVL7dh+/rCUvMZVSBX5PloxWS5MMJi0Ui6YjwCFGO\n" + 385 "J4G9m7pI+gJs/x1UG0AgplMF2UCFfiY1SAeE2nKAeOOXAXEwEjFy0ToVTmqXx7fg\n" + 386 "m9YjhuePxlBrd2DF/YW0pc8gmLnrtm4rKPLz\n" + 387 "-----END CERTIFICATE-----", 388 389 // SHA256withDSA, 2048 bits 390 // Validity 391 // Not Before: Nov 25 04:19:56 2016 GMT 392 // Not After : Nov 5 04:19:56 2037 GMT 393 // Subject Key Identifier: 394 // 19:46:10:43:24:6A:A5:14:BE:E2:92:01:79:F0:4C:5F:E1:AE:81:B5 395 "-----BEGIN CERTIFICATE-----\n" + 396 "MIIFCzCCBLGgAwIBAgIJAOnEn6YZD/sAMAsGCWCGSAFlAwQDAjA7MQswCQYDVQQG\n" + 397 "EwJV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FY\n" + 412 "A4IBBQACggEAZgWC0uflwqQQP1GRU1tolmFZwyVtKre7SjYgCeQBrOa0Xnj/SLaD\n" + 413 "g1HZ1oH0hccaR/45YouJiCretbbsQ77KouldGSGqTHJgRL75Y2z5uvxa60+YxZ0Z\n" + 414 "v8xvZnj4seyOjgJLxSSYSPl5n/F70RaNiCLVz/kGe6OQ8KoAeQjdDTOHXCegO9KX\n" + 415 "tvhM7EaYc8CII9OIR7S7PXJW0hgLKynZcu/Unh02aM0ABh/uLmw1+tvo8e8KTp98\n" + 416 "NKYSVf6kV3/ya58n4h64UbIYL08JoKUM/5SFETcKAZTU0YKZbpWTM79oJMr8oYVk\n" + 417 "P9jKitNsXq0Xkzt5dSO0kfu/kM7zpnaFsqOBrTCBqjAdBgNVHQ4EFgQUGUYQQyRq\n" + 418 "pRS+4pIBefBMX+GugbUwawYDVR0jBGQwYoAUGUYQQyRqpRS+4pIBefBMX+GugbWh\n" + 419 "P6Q9MDsxCzAJBgNVBAYTAlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5K\n" + 420 "U1NFIFRlc3QgU2VyaXZjZYIJAOnEn6YZD/sAMA8GA1UdEwEB/wQFMAMBAf8wCwYD\n" + 421 "VR0PBAQDAgEGMAsGCWCGSAFlAwQDAgNHADBEAiAwBafz5RRR9nc4cCYoYuBlT/D9\n" + 422 "9eayhkjhBY/zYunypwIgNp/JnFR88/T4hh36QfSKBGXId9RBCM6uaOkOKnEGkps=\n" + 423 "-----END CERTIFICATE-----" 424 }; 425 426 // End entity certificate. 427 private final static String[] endEntityCertStrs = { 428 // SHA256withECDSA, curve prime256v1 429 // Validity 430 // Not Before: Nov 25 04:19:51 2016 GMT 431 // Not After : Aug 12 04:19:51 2036 GMT 432 // Authority Key Identifier: 433 // CA:48:E8:00:C1:42:BD:59:9B:79:D9:B4:B4:CE:3F:68:0C:C8:C4:0C 434 "-----BEGIN CERTIFICATE-----\n" + 435 "MIIB1zCCAXygAwIBAgIJAPFq2QL/nUNZMAoGCCqGSM49BAMCMDsxCzAJBgNVBAYT\n" + 436 "AlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2VyaXZj\n" + 437 "ZTAeFw0xNjExMjUwNDE5NTFaFw0zNjA4MTIwNDE5NTFaMFUxCzAJBgNVBAYTAlVT\n" + 438 "MQ0wCwYDVQQKDARKYXZhMR0wGwYDVQQLDBRTdW5KU1NFIFRlc3QgU2VyaXZjZTEY\n" + 439 "MBYGA1UEAwwPUmVncmVzc2lvbiBUZXN0MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD\n" + 440 "QgAE4yvRGVvy9iVATyuHPJVdX6+lh/GLm/sRJ5qLT/3PVFOoNIvlEVNiARo7xhyj\n" + 441 "2p6bnf32gNg5Ye+QCw20VUv9E6NPME0wCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBSO\n" + 442 "hHlHZQp9hyBfSGTSQWeszqMXejAfBgNVHSMEGDAWgBTKSOgAwUK9WZt52bS0zj9o\n" + 443 "DMjEDDAKBggqhkjOPQQDAgNJADBGAiEAu3t6cvFglBAZfkhZlEwB04ZjUFqyfiRj\n" + 444 "4Hr275E4ZoQCIQDUEonJHlmA19J6oobfR5lYsmoqPm1r0DPm/IiNNKGKKA==\n" + 445 "-----END CERTIFICATE-----", 446 447 // SHA256withRSA, 2048 bits 448 // Validity 449 // Not Before: Nov 25 04:20:02 2016 GMT 450 // Not After : Aug 12 04:20:02 2036 GMT 451 // Authority Key Identifier: 452 // A2:DC:55:38:E4:47:7C:8B:D3:E0:CA:FA:AD:3A:C8:4A:DD:12:A0:8E 453 "-----BEGIN CERTIFICATE-----\n" + 454 "MIIDdjCCAl6gAwIBAgIJAJDcIGOlAmBmMA0GCSqGSIb3DQEBCwUAMDsxCzAJBgNV\n" + 455 "BAYTAlVTMQ0wCwYDVQQKEwRKYXZhMR0wGwYDVQQLExRTdW5KU1NFIFRlc3QgU2Vy\n" + 456 "aXZ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\n" + 471 "dDG5uzZeOC845GEH2T3tHDA+F3WwcZG/W+4RR6ZaaHlqPKKMcwFL73YbsqdCiKBv\n" + 472 "p6sXrhIiP0oXImRBRLDlidj5TIOLfAtNM9A=\n" + 473 "-----END CERTIFICATE-----", 474 475 // SHA256withDSA, 2048 bits 476 // Validity 477 // Not Before: Nov 25 04:19:56 2016 GMT 478 // Not After : Aug 12 04:19:56 2036 GMT 479 // Authority Key Identifier: 480 // 19:46:10:43:24:6A:A5:14:BE:E2:92:01:79:F0:4C:5F:E1:AE:81:B5 481 "-----BEGIN CERTIFICATE-----\n" + 482 "MIIE2jCCBICgAwIBAgIJAONcI1oba9V9MAsGCWCGSAFlAwQDAjA7MQswCQYDVQQG\n" + 483 "EwJVUzENMAsGA1UEChMESmF2YTEdMBsGA1UECxMUU3VuSlNTRSBUZXN0IFNlcml2\n" + 484 "Y2UwHhcNMTYxMTI1MDQxOTU2WhcNMzYwODEyMDQxOTU2WjBVMQswCQYDVQQGEwJV\n" + 485 "UzENMAsGA1UECgwESmF2YTEdMBsGA1UECwwUU3VuSlNTRSBUZXN0IFNlcml2Y2Ux\n" + 486 "GDAWBgNVBAMMD1JlZ3Jlc3Npb24gVGVzdDCCA0YwggI5BgcqhkjOOAQBMIICLAKC\n" + 487 "AQEAlrXtlh0gKG/nJ5i48rfNfGBQQYax1QMDN9BTG0yvIK1zdo2BYvGw3X9Bi+n2\n" + 488 "/6GHi3CcbbRV6YIQf1CXHTo/GDvgPBI4DzdJ5w7bLtx2Casel6n12vEptdVlp/AG\n" + 489 "FtNLCFX4gM7sQ7r4tpG9m7Jr9g6o0gT0Q3EVWTQ2s2wMHql8MRdmwJkAsw3aZyXw\n" + 490 "oafWru93MlBQRXsXq4sY/ffOdV+ZDJYkfdhbT1ZUIVce8PNkcZcM7Y7ZDAhTUHkC\n" + 491 "xec4Z4ucqBfMSsKg0rBRtUU/RbQ5V4mjilxl8v/xNUQsYb3QC64fVXcayro93MIM\n" + 492 "/DhS/+/mbv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sgjLEwJmYZvO\n" + 507 "yqNfQnXrkTCb+MLMAiBZLaRTVJrOW3edQjum+SonKKuiN22bRclO6pGuNRCtng==\n" + 508 "-----END CERTIFICATE-----" 509 }; 510 511 // Private key in the format of PKCS#8. 512 private final static String[] endEntityPrivateKeys = { 513 // 514 // EC private key related to cert endEntityCertStrs[0]. 515 // 516 "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgGAy4Pxrd2keM7AdP\n" + 517 "VNUMEO5iO681v4/tstVGfdXkCTuhRANCAATjK9EZW/L2JUBPK4c8lV1fr6WH8Yub\n" + 518 "+xEnmotP/c9UU6g0i+URU2IBGjvGHKPanpud/faA2Dlh75ALDbRVS/0T", 519 520 // 521 // RSA private key related to cert endEntityCertStrs[1]. 522 // 523 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCnR0euJ9ODZpjS\n" + 524 "wfTd/YmMrZ9bgMvqAsMqrRiKuw0TTvPngsUh54OKjj1fA+MHs6ltRMLWF/9gpnte\n" + 525 "JFbI9rWtijNnzx6aFm35ymTIrP6eZbhq+UBaetX6DHj5tT1P5a4FZ0CtoFyrbtjU\n" + 526 "jbrJ8IS0LyTf47Sx2UduFqwX4FQOZf+fj6eJ8l1s2bl5IPHx4VgcVN2OEC53cCfZ\n" + 527 "fpAP1ZZKgmk90OLi60eEUCXFbqiyMnGwA+/OjxiyqakIyNlwSSzhmyqJkoqJZRZQ\n" + 528 "CMJxsx6sM8Nfb1/J6jfjy7O28myY1HN9zCUW3DGlh3G5mrAeLZCg1pBMZeONtkWa\n" + 529 "OfuoSSNfAgMBAAECggEAWnAHKPkPObN2XDvQj1RL0WrtBSOVG2dy7Ne4tQh8ATxm\n" + 530 "UXw56CKq03YjaANJ8xgHObQ7QlSnFTHs8PDkmrIHd1OIh09LVDNcMfhilLwyzKBi\n" + 531 "HDO1vzU6Cn5DyX1bMJ8UfodcSIKyl1zOjdwyaItIs8HpRcJuJtk57SME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YErX+RSLyCJmaqVZMyXP09PBIvNXu2Z\n" + 547 "+gbx5WUC+kA+6zdKEPXowei6i6EHMXYT2AL7395ZbPajZjsCduE3WuUztuHrhtMm\n" + 548 "JI+k1o4rCnSLlX4gWdN1oTs=", 549 550 // 551 // DSA private key related to cert endEntityCertStrs[2]. 552 // 553 "MIICZAIBADCCAjkGByqGSM44BAEwggIsAoIBAQCWte2WHSAob+cnmLjyt818YFBB\n" + 554 "hrHVAwM30FMbTK8grXN2jYFi8bDdf0GL6fb/oYeLcJxttFXpghB/UJcdOj8YO+A8\n" + 555 "EjgPN0nnDtsu3HYJqx6XqfXa8Sm11WWn8AYW00sIVfiAzuxDuvi2kb2bsmv2DqjS\n" + 556 "BPRDcRVZNDazbAweqXwxF2bAmQCzDdpnJfChp9au73cyUFBFexerixj99851X5kM\n" + 557 "liR92FtPVlQhVx7w82RxlwztjtkMCFNQeQLF5zhni5yoF8xKwqDSsFG1RT9FtDlX\n" + 558 "iaOKXGXy//E1RCxhvdALrh9VdxrKuj3cwgz8OFL/7+Zu/+pMF6gUMQCQO44TAiEA\n" + 559 "jkSBlZh8uZwS0YSD+Lwu77E8W+AkwKT1ZmXi+YEyMLUCggEAdlWS2NtpNviEDOnX\n" + 560 "kRity5e7TwoRqypxpk27VE4SM2u9EqfQ5kwYi8zBGPiIBvJGLk+Uk1cVo7NHMVwi\n" + 561 "mRjEFrYqDLYlzwNjNxwNP/BNJgy0ps29EQHdscZw8hrLqqNXlZsezy4zd259xI4Q\n" + 562 "//o93Scd2VUnDBsqu7FVqaVL6bnRF4vtnrX+hlB463F9u47qO7uM0NXswvx2BtR/\n" + 563 "oxBahoXUOm+x/iwR2R/S3oDsiGUzK8wGP08CK++7F6zSVkaMZgxKygfvG0Q8m2IT\n" + 564 "SbMJWZtDeWGFJT5nqYG5el88Ar9MtHfNYd22sqiDy1xFLUQJfRrjNITQJQNWPqkp\n" + 565 "/cSxWAQiAiAKHYbYwEy0XS9J0MeKQmqPswn0nCJKvH+esfMKkZvV3w==" 566 }; 567 568 // Private key names of endEntityPrivateKeys. 569 private final static String[] endEntityPrivateKeyNames = { 570 "EC", 571 "RSA", 572 "DSA", 573 }; 574 575 /* 576 * Create an instance of SSLContext with the specified trust/key materials. 577 */ 578 private SSLContext createSSLContext( 579 String[] trustedMaterials, 580 String[] keyMaterialCerts, 581 String[] keyMaterialKeys, 582 String[] keyMaterialKeyAlgs, 583 ContextParameters params) throws Exception { 584 585 KeyStore ts = null; // trust store 586 KeyStore ks = null; // key store 587 char passphrase[] = "passphrase".toCharArray(); 588 589 // Generate certificate from cert string. 590 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 591 592 // Import the trused certs. 593 ByteArrayInputStream is; 594 if (trustedMaterials != null && trustedMaterials.length != 0) { 595 ts = KeyStore.getInstance("JKS"); 596 ts.load(null, null); 597 598 Certificate[] trustedCert = 599 new Certificate[trustedMaterials.length]; 600 for (int i = 0; i < trustedMaterials.length; i++) { 601 String trustedCertStr = trustedMaterials[i]; 602 603 is = new ByteArrayInputStream(trustedCertStr.getBytes()); 604 try { 605 trustedCert[i] = cf.generateCertificate(is); 606 } finally { 607 is.close(); 608 } 609 610 ts.setCertificateEntry("trusted-cert-" + i, trustedCert[i]); 611 } 612 } 613 614 // Import the key materials. 615 // 616 // Note that certification pathes bigger than one are not supported yet. 617 boolean hasKeyMaterials = 618 (keyMaterialCerts != null) && (keyMaterialCerts.length != 0) && 619 (keyMaterialKeys != null) && (keyMaterialKeys.length != 0) && 620 (keyMaterialKeyAlgs != null) && (keyMaterialKeyAlgs.length != 0) && 621 (keyMaterialCerts.length == keyMaterialKeys.length) && 622 (keyMaterialCerts.length == keyMaterialKeyAlgs.length); 623 if (hasKeyMaterials) { 624 ks = KeyStore.getInstance("JKS"); 625 ks.load(null, null); 626 627 for (int i = 0; i < keyMaterialCerts.length; i++) { 628 String keyCertStr = keyMaterialCerts[i]; 629 630 // generate the private key. 631 PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec( 632 Base64.getMimeDecoder().decode(keyMaterialKeys[i])); 633 KeyFactory kf = 634 KeyFactory.getInstance(keyMaterialKeyAlgs[i]); 635 PrivateKey priKey = kf.generatePrivate(priKeySpec); 636 637 // generate certificate chain 638 is = new ByteArrayInputStream(keyCertStr.getBytes()); 639 Certificate keyCert = null; 640 try { 641 keyCert = cf.generateCertificate(is); 642 } finally { 643 is.close(); 644 } 645 646 Certificate[] chain = new Certificate[] { keyCert }; 647 648 // import the key entry. 649 ks.setKeyEntry("cert-" + keyMaterialKeyAlgs[i], 650 priKey, passphrase, chain); 651 } 652 } 653 654 // Create an SSLContext object. 655 TrustManagerFactory tmf = 656 TrustManagerFactory.getInstance(params.tmAlgorithm); 657 tmf.init(ts); 658 659 SSLContext context = SSLContext.getInstance(params.contextProtocol); 660 if (hasKeyMaterials && ks != null) { 661 KeyManagerFactory kmf = 662 KeyManagerFactory.getInstance(params.kmAlgorithm); 663 kmf.init(ks, passphrase); 664 665 context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 666 } else { 667 context.init(null, tmf.getTrustManagers(), null); 668 } 669 670 return context; 671 } 672 673 /* 674 * ================================================= 675 * Stuffs to boot up the client-server mode testing. 676 */ 677 private Thread clientThread = null; 678 private Thread serverThread = null; 679 private volatile Exception serverException = null; 680 private volatile Exception clientException = null; 681 682 /* 683 * Should we run the client or server in a separate thread? 684 * Both sides can throw exceptions, but do you have a preference 685 * as to which side should be the main thread. 686 */ 687 private static final boolean separateServerThread = false; 688 689 /* 690 * Boot up the testing, used to drive remainder of the test. 691 */ 692 private void bootup() throws Exception { 693 Exception startException = null; 694 try { 695 if (separateServerThread) { 696 startServer(true); 697 startClient(false); 698 } else { 699 startClient(true); 700 startServer(false); 701 } 702 } catch (Exception e) { 703 startException = e; 704 } 705 706 /* 707 * Wait for other side to close down. 708 */ 709 if (separateServerThread) { 710 if (serverThread != null) { 711 serverThread.join(); 712 } 713 } else { 714 if (clientThread != null) { 715 clientThread.join(); 716 } 717 } 718 719 /* 720 * When we get here, the test is pretty much over. 721 * Which side threw the error? 722 */ 723 Exception local; 724 Exception remote; 725 726 if (separateServerThread) { 727 remote = serverException; 728 local = clientException; 729 } else { 730 remote = clientException; 731 local = serverException; 732 } 733 734 Exception exception = null; 735 736 /* 737 * Check various exception conditions. 738 */ 739 if ((local != null) && (remote != null)) { 740 // If both failed, return the curthread's exception. 741 local.initCause(remote); 742 exception = local; 743 } else if (local != null) { 744 exception = local; 745 } else if (remote != null) { 746 exception = remote; 747 } else if (startException != null) { 748 exception = startException; 749 } 750 751 /* 752 * If there was an exception *AND* a startException, 753 * output it. 754 */ 755 if (exception != null) { 756 if (exception != startException && startException != null) { 757 exception.addSuppressed(startException); 758 } 759 throw exception; 760 } 761 762 // Fall-through: no exception to throw! 763 } 764 765 private void startServer(boolean newThread) throws Exception { 766 if (newThread) { 767 serverThread = new Thread() { 768 @Override 769 public void run() { 770 try { 771 doServerSide(); 772 } catch (Exception e) { 773 /* 774 * Our server thread just died. 775 * 776 * Release the client, if not active already... 777 */ 778 logException("Server died", e); 779 serverException = e; 780 } 781 } 782 }; 783 serverThread.start(); 784 } else { 785 try { 786 doServerSide(); 787 } catch (Exception e) { 788 logException("Server failed", e); 789 serverException = e; 790 } 791 } 792 } 793 794 private void startClient(boolean newThread) throws Exception { 795 if (newThread) { 796 clientThread = new Thread() { 797 @Override 798 public void run() { 799 try { 800 doClientSide(); 801 } catch (Exception e) { 802 /* 803 * Our client thread just died. 804 */ 805 logException("Client died", e); 806 clientException = e; 807 } 808 } 809 }; 810 clientThread.start(); 811 } else { 812 try { 813 doClientSide(); 814 } catch (Exception e) { 815 logException("Client failed", e); 816 clientException = e; 817 } 818 } 819 } 820 821 private synchronized void logException(String prefix, Throwable cause) { 822 System.out.println(prefix + ": " + cause); 823 cause.printStackTrace(System.out); 824 } 825} 826