Path: blob/master/test/jdk/javax/net/ssl/SSLSession/HttpsURLConnectionLocalCertificateChain.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 4395238 4354003 4387961 439526626* @summary A test of many of the new functionality to go into JSSE 1.127* Fixed 4395238: The new certificate chains APIs should really be28* returning certs, not x509 certs29* Fixed 4354003: Need API to get client certificate chain30* Fixed 4387961: HostnameVerifier needs to pass various hostnames31* Fixed 4395266: HttpsURLConnection should be made protected32* @run main/othervm HttpsURLConnectionLocalCertificateChain33*34* SunJSSE does not support dynamic system properties, no way to re-use35* system properties in samevm/agentvm mode.36* @author Brad Wetmore37*/3839import java.io.*;40import java.net.*;41import javax.net.ssl.*;42import java.security.cert.*;4344public class HttpsURLConnectionLocalCertificateChain45implements HandshakeCompletedListener,46HostnameVerifier {4748/*49* =============================================================50* Set the various variables needed for the tests, then51* specify what tests to run on each side.52*/5354/*55* Should we run the client or server in a separate thread?56* Both sides can throw exceptions, but do you have a preference57* as to which side should be the main thread.58*/59static boolean separateServerThread = false;6061/*62* Where do we find the keystores?63*/64static String pathToStores = "../etc";65static String keyStoreFile = "keystore";66static String trustStoreFile = "truststore";67static String passwd = "passphrase";6869/*70* Is the server ready to serve?71*/72volatile static boolean serverReady = false;7374/*75* Default Verifier76*/77public boolean verify(String hostname, SSLSession session) {78try {79Certificate [] certs = session.getPeerCertificates();8081for (int i = 0; i< certs.length; i++) {82if (certs[i] instanceof X509Certificate) {83System.out.println("Hostname Verification cert #1: ");84// System.out.println(certs[i].toString());85}86}87} catch (Exception e) {88serverException = e;89}90return true;91}9293/*94* The event sent by the app.95*/96HandshakeCompletedEvent event;9798/*99* Provide the Listener for the HandshakeCompletedEvent100* Store the event now, we'll examine it later as we're101* finishing the test...102*/103public void handshakeCompleted(HandshakeCompletedEvent theEvent) {104event = theEvent;105}106107void examineHandshakeCompletedEvent() throws Exception {108/*109* Also check the types during compile. We changed110* from cert.x509 to certs.111*/112dumpCerts("examineHandshakeCompletedEvent received",113event.getPeerCertificates());114dumpCerts("examineHandshakeCompletedEvent sent",115event.getLocalCertificates());116}117118synchronized void dumpCerts(String where, Certificate [] certs)119throws Exception {120121System.out.println("");122System.out.println(where + ":");123124if (certs == null) {125throw new Exception("certs == null");126}127128for (int i = 0; i< certs.length; i++) {129if (certs[i] instanceof X509Certificate) {130System.out.println("cert #1: " +131((X509Certificate) certs[i]).getSubjectDN());132}133}134}135136void doServerSide() throws Exception {137138SSLServerSocketFactory sslssf;139SSLServerSocket sslServerSocket;140141System.out.println("Starting Server...");142sslssf =143(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();144sslServerSocket =145(SSLServerSocket) sslssf.createServerSocket(serverPort);146serverPort = sslServerSocket.getLocalPort();147System.out.println("Kicking off Client...");148149serverReady = true;150151SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();152sslSocket.setNeedClientAuth(true);153sslSocket.addHandshakeCompletedListener(this);154155InputStream sslIS = sslSocket.getInputStream();156OutputStream sslOS = sslSocket.getOutputStream();157DataOutputStream out = new DataOutputStream(sslOS);158159System.out.println("Server reading request...");160sslIS.read();161162System.out.println("Server replying...");163try {164out.writeBytes("HTTP/1.0 200 OK\r\n");165out.writeBytes("Content-Length: " + 1 + "\r\n");166out.writeBytes("Content-Type: text/html\r\n\r\n");167out.write(57);168out.flush();169} catch (IOException ie) {170serverException = ie;171}172173System.out.println("Server getting certs...");174SSLSession sslSession = sslSocket.getSession();175dumpCerts("ServerSide sent", sslSession.getLocalCertificates());176dumpCerts("ServerSide received", sslSession.getPeerCertificates());177178/*179* Won't bother closing IS/sockets this time, we're exiting...180*/181182/*183* We'll eventually get this event, wait for it.184*/185while (event == null) {186Thread.sleep(1000);187}188189System.out.println("Server examining Event...");190examineHandshakeCompletedEvent();191}192193void doClientSide() throws Exception {194195/*196* Wait for server to get started.197*/198while (!serverReady) {199Thread.sleep(50);200}201202System.out.println("Starting Client...");203204String url = "https://localhost:" + serverPort;205System.out.println("connecting to: " + url);206URL myURL = new URL(url);207HttpsURLConnection myURLc;208209System.out.println("Client setting up URL/connecting...");210myURLc = (HttpsURLConnection) myURL.openConnection();211myURLc.setHostnameVerifier(this);212myURLc.connect();213214InputStream sslIS = myURLc.getInputStream();215216System.out.println("Client reading...");217sslIS.read();218219System.out.println("Client dumping certs...");220221dumpCerts("ClientSide received", myURLc.getServerCertificates());222dumpCerts("ClientSide sent", myURLc.getLocalCertificates());223224/*225* Won't bother closing IS/sockets this time, we're exiting...226*/227}228/*229* =============================================================230* The remainder is just support stuff231*/232233// use any free port by default234volatile int serverPort = 0;235236volatile Exception serverException = null;237volatile Exception clientException = null;238239public static void main(String[] args) throws Exception {240String keyFilename =241System.getProperty("test.src", "./") + "/" + pathToStores +242"/" + keyStoreFile;243String trustFilename =244System.getProperty("test.src", "./") + "/" + pathToStores +245"/" + trustStoreFile;246247System.setProperty("javax.net.ssl.keyStore", keyFilename);248System.setProperty("javax.net.ssl.keyStorePassword", passwd);249System.setProperty("javax.net.ssl.trustStore", trustFilename);250System.setProperty("javax.net.ssl.trustStorePassword", passwd);251252/*253* Start the tests.254*/255new HttpsURLConnectionLocalCertificateChain();256}257258Thread clientThread = null;259Thread serverThread = null;260261HttpsURLConnectionLocalCertificateChain () throws Exception {262if (separateServerThread) {263startServer(true);264startClient(false);265} else {266startClient(true);267startServer(false);268}269270/*271* Wait for other side to close down.272*/273if (separateServerThread) {274serverThread.join();275} else {276clientThread.join();277}278279/*280* When we get here, the test is pretty much over.281*282* If the main thread excepted, that propagates back283* immediately. If the other thread threw an exception, we284* should report back.285*/286if (serverException != null)287throw serverException;288if (clientException != null)289throw clientException;290}291292void startServer(boolean newThread) throws Exception {293if (newThread) {294serverThread = new Thread() {295public void run() {296try {297doServerSide();298} catch (Exception e) {299/*300* Our server thread just died.301*302* Release the client, if not active already...303*/304System.out.println("Server died...");305serverReady = true;306serverException = e;307}308}309};310serverThread.start();311} else {312doServerSide();313}314}315316void startClient(boolean newThread) throws Exception {317if (newThread) {318clientThread = new Thread() {319public void run() {320try {321doClientSide();322} catch (Exception e) {323/*324* Our client thread just died.325*/326System.out.println("Client died...");327clientException = e;328}329}330};331clientThread.start();332} else {333doClientSide();334}335}336}337338339