Path: blob/master/test/jdk/sun/security/tools/keytool/PrintSSL.java
41152 views
/*1* Copyright (c) 2008, 2017, 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 6480981 816062426* @summary keytool should be able to import certificates from remote SSL server27* @library /test/lib28* @build jdk.test.lib.SecurityTools29* jdk.test.lib.Utils30* jdk.test.lib.Asserts31* jdk.test.lib.JDKToolFinder32* jdk.test.lib.JDKToolLauncher33* jdk.test.lib.Platform34* jdk.test.lib.process.*35* @run main/othervm PrintSSL36*/3738import java.net.ServerSocket;39import java.nio.file.Files;40import java.nio.file.Paths;41import java.util.concurrent.CountDownLatch;42import javax.net.ssl.SSLServerSocketFactory;43import javax.net.ssl.SSLSocket;44import jdk.test.lib.SecurityTools;45import jdk.test.lib.process.OutputAnalyzer;4647public class PrintSSL {4849public static void main(String[] args) throws Throwable {50Files.deleteIfExists(Paths.get("keystore"));5152// make sure that "-printcert" works with weak algorithms53OutputAnalyzer out = SecurityTools.keytool("-genkeypair "54+ "-keystore keystore -storepass passphrase "55+ "-keypass passphrase -keyalg rsa -keysize 1024 "56+ "-sigalg MD5withRSA -alias rsa_alias -dname CN=Server");57System.out.println(out.getOutput());58out.shouldHaveExitValue(0);5960int port = new Server().start();61if(port == -1) {62throw new RuntimeException("Unable start ssl server.");63}64String vmOpt = System.getProperty("TESTTOOLVMOPTS");65String cmd = String.format(66"-debug %s -printcert -sslserver localhost:%s",67((vmOpt == null) ? "" : vmOpt ), port);6869out = SecurityTools.keytool(cmd);70System.out.println(out.getOutput());71out.shouldHaveExitValue(0);72}7374private static class Server implements Runnable {7576private volatile int serverPort = -1;77private final CountDownLatch untilServerReady = new CountDownLatch(1);7879public int start() throws InterruptedException {8081Thread server = new Thread(this);82server.setDaemon(true);83server.start();84untilServerReady.await();85return this.getServerPort();86}8788@Override89public void run() {9091System.setProperty("javax.net.ssl.keyStorePassword", "passphrase");92System.setProperty("javax.net.ssl.keyStore", "keystore");93SSLServerSocketFactory sslssf =94(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();95try (ServerSocket server = sslssf.createServerSocket(0)) {96this.serverPort = server.getLocalPort();97System.out.printf("%nServer started on: %s%n", getServerPort());98untilServerReady.countDown();99((SSLSocket)server.accept()).startHandshake();100} catch (Throwable e) {101e.printStackTrace(System.out);102untilServerReady.countDown();103}104105}106107public int getServerPort() {108return this.serverPort;109}110111}112}113114115116