Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.52 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2001, 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. // SunJSSE does not support dynamic system properties, no way to re-use
  25. // system properties in samevm/agentvm mode.
  26.  
  27. /*
  28.  * @test
  29.  * @bug 8051498 8145849
  30.  * @summary JEP 244: TLS Application-Layer Protocol Negotiation Extension
  31.  * @compile MyX509ExtendedKeyManager.java
  32.  * @run main/othervm SSLSocketAlpnTest h2          h2          h2
  33.  * @run main/othervm SSLSocketAlpnTest h2          h2,http/1.1 h2
  34.  * @run main/othervm SSLSocketAlpnTest h2,http/1.1 h2,http/1.1 h2
  35.  * @run main/othervm SSLSocketAlpnTest http/1.1,h2 h2,http/1.1 http/1.1
  36.  * @run main/othervm SSLSocketAlpnTest h4,h3,h2    h1,h2       h2
  37.  * @run main/othervm SSLSocketAlpnTest EMPTY       h2,http/1.1 NONE
  38.  * @run main/othervm SSLSocketAlpnTest h2          EMPTY       NONE
  39.  * @run main/othervm SSLSocketAlpnTest H2          h2          ERROR
  40.  * @run main/othervm SSLSocketAlpnTest h2          http/1.1    ERROR
  41.  * @author Brad Wetmore
  42.  */
  43.  
  44. import java.io.*;
  45. import java.security.KeyStore;
  46.  
  47. import javax.net.ssl.*;
  48. import javax.net.ssl.SSLSocket;
  49.  
  50. public class SSLSocketFailedClientTest {
  51.  
  52.     /*
  53.      * =============================================================
  54.      * Set the various variables needed for the tests, then
  55.      * specify what tests to run on each side.
  56.      */
  57.  
  58.     /*
  59.      * Should we run the client or server in a separate thread?
  60.      * Both sides can throw exceptions, but do you have a preference
  61.      * as to which side should be the main thread.
  62.      */
  63.     static boolean separateServerThread = true;
  64.  
  65.     /*
  66.      * Where do we find the keystores?
  67.      */
  68.     static String pathToStores = "etc";
  69.     static String keyStoreFile = "keystore";
  70.     static String trustStoreFile = "truststore";
  71.     static String passwd = "passphrase";
  72.  
  73.     static String keyFilename = System.getProperty("test.src", ".") + "/"
  74.             + pathToStores + "/" + keyStoreFile;
  75.     static String trustFilename = System.getProperty("test.src", ".") + "/"
  76.             + pathToStores + "/" + trustStoreFile;
  77.  
  78.     /*
  79.      * SSLContext
  80.      */
  81.     SSLContext clientSSLContext = null;
  82.     SSLContext serverSSLContext = null;
  83.  
  84.     /*
  85.      * Is the server ready to serve?
  86.      */
  87.     volatile static boolean serverReady = false;
  88.  
  89.     /*
  90.      * Turn on SSL debugging?
  91.      */
  92.     static boolean debug = false;
  93.  
  94.     /*
  95.      * If the client or server is doing some kind of object creation
  96.      * that the other side depends on, and that thread prematurely
  97.      * exits, you may experience a hang. The test harness will
  98.      * terminate all hung threads after its timeout has expired,
  99.      * currently 3 minutes by default, but you might try to be
  100.      * smart about it....
  101.      */
  102.  
  103.     /*
  104.      * Define the server side of the test.
  105.      *
  106.      * If the server prematurely exits, serverReady will be set to true
  107.      * to avoid infinite hangs.
  108.      */
  109.     void doServerSide() throws Exception {
  110.         SSLServerSocketFactory sslssf = serverSSLContext.getServerSocketFactory();
  111.         SSLServerSocket sslServerSocket
  112.                 = (SSLServerSocket) sslssf.createServerSocket(serverPort);
  113.         // for both client/server to call into X509KM
  114.         sslServerSocket.setNeedClientAuth(true);
  115.  
  116.         serverPort = sslServerSocket.getLocalPort();
  117.  
  118.         /*
  119.          * Signal Client, we're ready for his connect.
  120.          */
  121.         serverReady = true;
  122.  
  123.         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
  124.  
  125.         SSLParameters sslp = sslSocket.getSSLParameters();
  126.  
  127.         /*
  128.          * The default ciphersuite ordering from the SSLContext may not
  129.          * reflect "h2" ciphersuites as being preferred, additionally the
  130.          * client may not send them in an appropriate order. We could resort
  131.          * the suite list if so desired.
  132.          */
  133.         String[] suites = sslp.getCipherSuites();
  134.         sslp.setCipherSuites(suites);
  135.         sslp.setUseCipherSuitesOrder(true); // Set server side order
  136.  
  137.         sslSocket.setSSLParameters(sslp);
  138.  
  139.         sslSocket.startHandshake();
  140.  
  141.         InputStream sslIS = sslSocket.getInputStream();
  142.         OutputStream sslOS = sslSocket.getOutputStream();
  143.  
  144.         sslIS.read();
  145.         sslOS.write(85);
  146.         sslOS.flush();
  147.  
  148.         sslSocket.close();
  149.     }
  150.  
  151.     /*
  152.      * Define the client side of the test.
  153.      *
  154.      * If the server prematurely exits, serverReady will be set to true
  155.      * to avoid infinite hangs.
  156.      */
  157.     void doClientSide() throws Exception {
  158.  
  159.         /*
  160.          * Wait for server to get started.
  161.          */
  162.         while (!serverReady) {
  163.             Thread.sleep(50);
  164.         }
  165.  
  166.         SSLSocketFactory sslsf = clientSSLContext.getSocketFactory();
  167.  
  168.         SSLSocket sslSocket
  169.                 = (SSLSocket) sslsf.createSocket("localhost", serverPort);
  170.  
  171.         SSLParameters sslp = sslSocket.getSSLParameters();
  172.  
  173.         /*
  174.          * The default ciphersuite ordering from the SSLContext may not
  175.          * reflect "h2" ciphersuites as being preferred, additionally the
  176.          * client may not send them in an appropriate order. We could resort
  177.          * the suite list if so desired.
  178.          */
  179.         String[] suites = sslp.getCipherSuites();
  180.         sslp.setCipherSuites(suites);
  181.         sslp.setUseCipherSuitesOrder(true); // Set server side order
  182.  
  183.         sslSocket.setSSLParameters(sslp);
  184.  
  185.         sslSocket.startHandshake();
  186.  
  187.         InputStream sslIS = sslSocket.getInputStream();
  188.         OutputStream sslOS = sslSocket.getOutputStream();
  189.  
  190.         sslOS.write(280);
  191.         sslOS.flush();
  192.         sslIS.read();
  193.  
  194.         sslSocket.close();
  195.     }
  196.  
  197.     /*
  198.      * =============================================================
  199.      * The remainder is just support stuff
  200.      */
  201.     // use any free port by default
  202.     volatile int serverPort = 0;
  203.  
  204.     volatile Exception serverException = null;
  205.     volatile Exception clientException = null;
  206.  
  207.     public static void main(String[] args) throws Exception {
  208.         if (debug) {
  209.             System.setProperty("javax.net.debug", "all");
  210.         }
  211.  
  212.         /*
  213.          * Start the tests.
  214.          */
  215.         try {
  216.             new SSLSocketFailedClientTest();
  217.         } catch (SSLHandshakeException she) {
  218.                 System.out.println("Caught the expected exception: " + she);
  219.         }
  220.  
  221.         System.out.println("Test Passed.");
  222.     }
  223.  
  224.     SSLContext getSSLContext(String keyFilename, String trustFilename)
  225.             throws Exception {
  226.         SSLContext ctx = SSLContext.getInstance("TLS");
  227.  
  228.         // Keystores
  229.         KeyManager[] kms = null;
  230.  
  231.         if (keyFilename != null) {
  232.             KeyStore keyKS = KeyStore.getInstance("JKS");
  233.             keyKS.load(new FileInputStream(keyFilename), passwd.toCharArray());
  234.  
  235.             // Generate KeyManager and TrustManager
  236.             KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  237.             kmf.init(keyKS, passwd.toCharArray());
  238.  
  239.             kms = kmf.getKeyManagers();
  240.         }
  241.  
  242.         KeyStore trustKS = KeyStore.getInstance("JKS");
  243.         trustKS.load(new FileInputStream(trustFilename), passwd.toCharArray());
  244.  
  245.         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
  246.         tmf.init(trustKS);
  247.         TrustManager[] tms = tmf.getTrustManagers();
  248.  
  249.         // initial SSLContext
  250.         ctx.init(kms, tms, null);
  251.  
  252.         return ctx;
  253.     }
  254.  
  255.     Thread clientThread = null;
  256.     Thread serverThread = null;
  257.  
  258.     /*
  259.      * Primary constructor, used to drive remainder of the test.
  260.      *
  261.      * Fork off the other side, then do your work.
  262.      */
  263.     SSLSocketFailedClientTest() throws Exception {
  264.         Exception startException = null;
  265.         clientSSLContext = getSSLContext(null, trustFilename);
  266.         serverSSLContext = getSSLContext(keyFilename, trustFilename);
  267.         try {
  268.             if (separateServerThread) {
  269.                 startServer(true);
  270.                 startClient(false);
  271.             } else {
  272.                 startClient(true);
  273.                 startServer(false);
  274.             }
  275.         } catch (Exception e) {
  276.             startException = e;
  277.         }
  278.  
  279.         /*
  280.          * Wait for other side to close down.
  281.          */
  282.         if (separateServerThread) {
  283.             if (serverThread != null) {
  284.                 serverThread.join();
  285.             }
  286.         } else {
  287.             if (clientThread != null) {
  288.                 clientThread.join();
  289.             }
  290.         }
  291.  
  292.         /*
  293.          * When we get here, the test is pretty much over.
  294.          * Which side threw the error?
  295.          */
  296.         Exception local;
  297.         Exception remote;
  298.  
  299.         if (separateServerThread) {
  300.             remote = serverException;
  301.             local = clientException;
  302.         } else {
  303.             remote = clientException;
  304.             local = serverException;
  305.         }
  306.  
  307.         Exception exception = null;
  308.  
  309.         /*
  310.          * Check various exception conditions.
  311.          */
  312.         if ((local != null) && (remote != null)) {
  313.             // If both failed, return the curthread's exception.
  314.             local.initCause(remote);
  315.             exception = local;
  316.         } else if (local != null) {
  317.             exception = local;
  318.         } else if (remote != null) {
  319.             exception = remote;
  320.         } else if (startException != null) {
  321.             exception = startException;
  322.         }
  323.  
  324.         /*
  325.          * If there was an exception *AND* a startException,
  326.          * output it.
  327.          */
  328.         if (exception != null) {
  329.             if (exception != startException && startException != null) {
  330.                 exception.addSuppressed(startException);
  331.             }
  332.             throw exception;
  333.         }
  334.  
  335.         // Fall-through: no exception to throw!
  336.     }
  337.  
  338.     void startServer(boolean newThread) throws Exception {
  339.         if (newThread) {
  340.             serverThread = new Thread() {
  341.                 @Override
  342.                 public void run() {
  343.                     try {
  344.                         doServerSide();
  345.                     } catch (Exception e) {
  346.                         /*
  347.                          * Our server thread just died.
  348.                          *
  349.                          * Release the client, if not active already...
  350.                          */
  351.                         System.err.println("Server died...");
  352.                         serverReady = true;
  353.                         serverException = e;
  354.                     }
  355.                 }
  356.             };
  357.             serverThread.start();
  358.         } else {
  359.             try {
  360.                 doServerSide();
  361.             } catch (Exception e) {
  362.                 serverException = e;
  363.             } finally {
  364.                 serverReady = true;
  365.             }
  366.         }
  367.     }
  368.  
  369.     void startClient(boolean newThread) throws Exception {
  370.         if (newThread) {
  371.             clientThread = new Thread() {
  372.                 @Override
  373.                 public void run() {
  374.                     try {
  375.                         doClientSide();
  376.                     } catch (Exception e) {
  377.                         /*
  378.                          * Our client thread just died.
  379.                          */
  380.                         System.err.println("Client died...");
  381.                         clientException = e;
  382.                     }
  383.                 }
  384.             };
  385.             clientThread.start();
  386.         } else {
  387.             try {
  388.                 doClientSide();
  389.             } catch (Exception e) {
  390.                 clientException = e;
  391.             }
  392.         }
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement