< prev index next >

test/jdk/sun/security/ssl/SSLContextImpl/TrustTrustedCert.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 114 
 115     @Override
 116     protected SSLContext createServerSSLContext() throws Exception {
 117         return generateSSLContext();
 118     }
 119 
 120     @Override
 121     protected void configureServerSocket(SSLServerSocket socket) {
 122         socket.setNeedClientAuth(true);
 123     }
 124 
 125     @Override
 126     protected void runServerApplication(SSLSocket socket) throws Exception {
 127         InputStream sslIS = socket.getInputStream();
 128         OutputStream sslOS = socket.getOutputStream();
 129 
 130         try {
 131             sslIS.read();
 132             sslOS.write('A');
 133             sslOS.flush();
 134         } catch (SSLHandshakeException e) {
 135             if (expectFail && !e.toString().contains("certificate_unknown")) {
 136                 throw new RuntimeException(
 137                         "Expected to see certificate_unknown in exception output",
 138                         e);
 139             }
 140         }
 141     }
 142 
 143     @Override
 144     protected SSLContext createClientSSLContext() throws Exception {
 145         return generateSSLContext();
 146     }
 147 
 148     @Override
 149     protected void runClientApplication(SSLSocket socket) throws Exception {
 150         // enable the specified TLS protocol
 151         socket.setEnabledProtocols(new String[] { tlsProtocol });
 152 
 153         InputStream sslIS = socket.getInputStream();
 154         OutputStream sslOS = socket.getOutputStream();
 155 
 156         try {
 157             sslOS.write('B');
 158             sslOS.flush();
 159             sslIS.read();
 160         } catch (SSLHandshakeException e) {

 161             // focus on the CertPathValidatorException
 162             Throwable t = e.getCause().getCause();
 163             if ((t == null)
 164                     || (expectFail && !t.toString().contains("MD5withRSA"))) {
 165                 throw new RuntimeException(
 166                         "Expected to see MD5withRSA in exception output", t);



 167             }
 168         }
 169     }
 170 
 171     /*
 172      * =============================================================
 173      * The remainder is just support stuff
 174      */
 175     private static String tmAlgorithm;        // trust manager
 176     private static String tlsProtocol;        // trust manager
 177     // set this flag to test context of CertificateException
 178     private static boolean expectFail;
 179 
 180     private static void parseArguments(String[] args) {
 181         tmAlgorithm = args[0];
 182         tlsProtocol = args[1];
 183         expectFail = Boolean.parseBoolean(args[2]);
 184     }
 185 
 186     private static SSLContext generateSSLContext() throws Exception {


   1 /*
   2  * Copyright (c) 2011, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 114 
 115     @Override
 116     protected SSLContext createServerSSLContext() throws Exception {
 117         return generateSSLContext();
 118     }
 119 
 120     @Override
 121     protected void configureServerSocket(SSLServerSocket socket) {
 122         socket.setNeedClientAuth(true);
 123     }
 124 
 125     @Override
 126     protected void runServerApplication(SSLSocket socket) throws Exception {
 127         InputStream sslIS = socket.getInputStream();
 128         OutputStream sslOS = socket.getOutputStream();
 129 
 130         try {
 131             sslIS.read();
 132             sslOS.write('A');
 133             sslOS.flush();
 134         } catch (SSLException ssle) {
 135             if (!expectFail) {
 136                 throw ssle;
 137             }   // Otherwise, ignore.


 138         }
 139     }
 140 
 141     @Override
 142     protected SSLContext createClientSSLContext() throws Exception {
 143         return generateSSLContext();
 144     }
 145 
 146     @Override
 147     protected void runClientApplication(SSLSocket socket) throws Exception {
 148         // enable the specified TLS protocol
 149         socket.setEnabledProtocols(new String[] { tlsProtocol });
 150 
 151         InputStream sslIS = socket.getInputStream();
 152         OutputStream sslOS = socket.getOutputStream();
 153 
 154         try {
 155             sslOS.write('B');
 156             sslOS.flush();
 157             sslIS.read();
 158         } catch (SSLHandshakeException e) {
 159             if (expectFail) {
 160             // focus on the CertPathValidatorException
 161                 Throwable t = e.getCause().getCause();
 162                 if (t == null || !t.toString().contains("MD5withRSA")) {

 163                     throw new RuntimeException(
 164                         "Expected to see MD5withRSA in exception output", t);
 165                 }
 166             } else {
 167                 throw e;
 168             }
 169         }
 170     }
 171 
 172     /*
 173      * =============================================================
 174      * The remainder is just support stuff
 175      */
 176     private static String tmAlgorithm;        // trust manager
 177     private static String tlsProtocol;        // trust manager
 178     // set this flag to test context of CertificateException
 179     private static boolean expectFail;
 180 
 181     private static void parseArguments(String[] args) {
 182         tmAlgorithm = args[0];
 183         tlsProtocol = args[1];
 184         expectFail = Boolean.parseBoolean(args[2]);
 185     }
 186 
 187     private static SSLContext generateSSLContext() throws Exception {


< prev index next >