< prev index next >

test/jdk/sun/security/ssl/X509TrustManagerImpl/SelfIssuedCert.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 2015, 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  */


 192 
 193     /*
 194      * Define the client side of the test.
 195      *
 196      * If the server prematurely exits, serverReady will be set to true
 197      * to avoid infinite hangs.
 198      */
 199     void doClientSide() throws Exception {
 200         /*
 201          * Wait for server to get started.
 202          */
 203         while (!serverReady) {
 204             Thread.sleep(50);
 205         }
 206 
 207         SSLContext context = getSSLContext(trusedCertStr, null, null);
 208         SSLSocketFactory sslsf = context.getSocketFactory();
 209 
 210         SSLSocket sslSocket =
 211             (SSLSocket)sslsf.createSocket("localhost", serverPort);

 212 
 213         InputStream sslIS = sslSocket.getInputStream();
 214         OutputStream sslOS = sslSocket.getOutputStream();
 215 
 216         sslOS.write(280);
 217         sslOS.flush();
 218         sslIS.read();
 219 
 220         sslSocket.close();
 221     }
 222 
 223     // get the ssl context
 224     private static SSLContext getSSLContext(String trusedCertStr,
 225             String keyCertStr, String keySpecStr) throws Exception {
 226 
 227         // generate certificate from cert string
 228         CertificateFactory cf = CertificateFactory.getInstance("X.509");
 229 
 230         // create a key store
 231         KeyStore ks = KeyStore.getInstance("JKS");


   1 /*
   2  * Copyright (c) 2009, 2018, 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  */


 192 
 193     /*
 194      * Define the client side of the test.
 195      *
 196      * If the server prematurely exits, serverReady will be set to true
 197      * to avoid infinite hangs.
 198      */
 199     void doClientSide() throws Exception {
 200         /*
 201          * Wait for server to get started.
 202          */
 203         while (!serverReady) {
 204             Thread.sleep(50);
 205         }
 206 
 207         SSLContext context = getSSLContext(trusedCertStr, null, null);
 208         SSLSocketFactory sslsf = context.getSocketFactory();
 209 
 210         SSLSocket sslSocket =
 211             (SSLSocket)sslsf.createSocket("localhost", serverPort);
 212         sslSocket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" });
 213 
 214         InputStream sslIS = sslSocket.getInputStream();
 215         OutputStream sslOS = sslSocket.getOutputStream();
 216 
 217         sslOS.write(280);
 218         sslOS.flush();
 219         sslIS.read();
 220 
 221         sslSocket.close();
 222     }
 223 
 224     // get the ssl context
 225     private static SSLContext getSSLContext(String trusedCertStr,
 226             String keyCertStr, String keySpecStr) throws Exception {
 227 
 228         // generate certificate from cert string
 229         CertificateFactory cf = CertificateFactory.getInstance("X.509");
 230 
 231         // create a key store
 232         KeyStore ks = KeyStore.getInstance("JKS");


< prev index next >