Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/ClientModeClientAuth.java
41152 views
/*1* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 439065926* @summary setNeedClientAuth() isn't working after a handshaker is established27* @run main/othervm ClientModeClientAuth28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @author Brad Wetmore32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;37import java.security.cert.*;3839public class ClientModeClientAuth {4041/*42* =============================================================43* Set the various variables needed for the tests, then44* specify what tests to run on each side.45*/4647/*48* Should we run the client or server in a separate thread?49* Both sides can throw exceptions, but do you have a preference50* as to which side should be the main thread.51*/52static boolean separateServerThread = false;5354/*55* Where do we find the keystores?56*/57static String pathToStores = "../../../../javax/net/ssl/etc";58static String keyStoreFile = "keystore";59static String trustStoreFile = "truststore";60static String passwd = "passphrase";6162/*63* Is the server ready to serve?64*/65volatile static boolean serverReady = false;6667/*68* Define the server side of the test.69*/70void doServerSide() throws Exception {7172ServerSocket serverSocket = null;73serverSocket = new ServerSocket(serverPort);74serverPort = serverSocket.getLocalPort();7576/*77* Signal Client, we're ready for his connect.78*/79serverReady = true;8081Socket socket = serverSocket.accept();82OutputStream out = socket.getOutputStream();83InputStream in = socket.getInputStream();8485/*86* send data to make sure we are ok.87*/88out.write(85);89out.flush();90in.read();9192SSLSocketFactory sslsf =93(SSLSocketFactory) SSLSocketFactory.getDefault();94SSLSocket sslSocket =95(SSLSocket) sslsf.createSocket(96socket, socket.getInetAddress().getHostName(),97socket.getPort(), true);9899sslSocket.setUseClientMode(false);100sslSocket.setNeedClientAuth(true);101102InputStream sslIS = sslSocket.getInputStream();103OutputStream sslOS = sslSocket.getOutputStream();104105sslOS.write(85);106sslOS.flush();107sslIS.read();108109System.out.println("About to get PeerCertificates");110Certificate[] certs =111sslSocket.getSession().getPeerCertificates();112if (certs[0] instanceof X509Certificate) {113System.out.println("Peer: " +114((X509Certificate)certs[0]).getSubjectDN());115}116117sslIS.close();118sslOS.close();119sslSocket.close();120}121122/*123* Define the client side of the test.124*/125void doClientSide() throws Exception {126127/*128* Wait for host to set up his port.129*/130while (!serverReady) {131Thread.sleep(50);132}133134Socket socket = new Socket("localhost", serverPort);135InputStream in = socket.getInputStream();136OutputStream out = socket.getOutputStream();137138in.read();139out.write(280);140out.flush();141142SSLSocketFactory sslsf =143(SSLSocketFactory) SSLSocketFactory.getDefault();144145SSLSocket sslSocket = (SSLSocket)146sslsf.createSocket(socket, socket.getInetAddress().getHostName(),147socket.getPort(), true);148149sslSocket.setUseClientMode(true);150151InputStream sslIS = sslSocket.getInputStream();152OutputStream sslOS = sslSocket.getOutputStream();153154sslIS.read();155sslOS.write(280);156sslOS.flush();157158sslIS.close();159sslOS.close();160sslSocket.close();161}162163/*164* =============================================================165* The remainder is just support stuff166*/167168// use any free port by default169volatile int serverPort = 0;170171volatile Exception serverException = null;172volatile Exception clientException = null;173174public static void main(String[] args) throws Exception {175String keyFilename =176System.getProperty("test.src", "./") + "/" + pathToStores +177"/" + keyStoreFile;178String trustFilename =179System.getProperty("test.src", "./") + "/" + pathToStores +180"/" + trustStoreFile;181182System.setProperty("javax.net.ssl.keyStore", keyFilename);183System.setProperty("javax.net.ssl.keyStorePassword", passwd);184System.setProperty("javax.net.ssl.trustStore", trustFilename);185System.setProperty("javax.net.ssl.trustStorePassword", passwd);186187/*188* Start the tests.189*/190new ClientModeClientAuth();191}192193Thread clientThread = null;194Thread serverThread = null;195196ClientModeClientAuth() throws Exception {197if (separateServerThread) {198startServer(true);199startClient(false);200} else {201startClient(true);202startServer(false);203}204205/*206* Wait for other side to close down.207*/208if (separateServerThread) {209serverThread.join();210} else {211clientThread.join();212}213214/*215* When we get here, the test is pretty much over.216*217* If the main thread excepted, that propagates back218* immediately. If the other thread threw an exception, we219* should report back.220*/221if (serverException != null)222throw serverException;223if (clientException != null)224throw clientException;225}226227void startServer(boolean newThread) throws Exception {228if (newThread) {229serverThread = new Thread() {230public void run() {231try {232doServerSide();233} catch (Exception e) {234/*235* Our server thread just died.236*237* Release the client, if not active already...238*/239System.out.println("Server died...");240serverReady = true;241serverException = e;242}243}244};245serverThread.start();246} else {247doServerSide();248}249}250251void startClient(boolean newThread) throws Exception {252if (newThread) {253clientThread = new Thread() {254public void run() {255try {256doClientSide();257} catch (Exception e) {258/*259* Our client thread just died.260*/261System.out.println("Client died...");262clientException = e;263}264}265};266clientThread.start();267} else {268doClientSide();269}270}271}272273274