1 /* 2 * Copyright (c) 2008, 2017, 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 */ 23 24 /* 25 * @test 26 * @bug 6480981 8160624 27 * @summary keytool should be able to import certificates from remote SSL server 28 * @library /test/lib 29 * @build jdk.test.lib.SecurityTools 30 * jdk.test.lib.Utils 31 * jdk.test.lib.Asserts 32 * jdk.test.lib.JDKToolFinder 33 * jdk.test.lib.JDKToolLauncher 34 * jdk.test.lib.Platform 35 * jdk.test.lib.process.* 36 * @run main/othervm PrintSSL 37 */ 38 39 import java.net.ServerSocket; 40 import java.nio.file.Files; 41 import java.nio.file.Paths; 42 import java.util.concurrent.CountDownLatch; 43 import javax.net.ssl.SSLServerSocketFactory; 44 import javax.net.ssl.SSLSocket; 45 import jdk.test.lib.SecurityTools; 46 import jdk.test.lib.process.OutputAnalyzer; 47 48 public class PrintSSL { 49 50 public static void main(String[] args) throws Throwable { 51 Files.deleteIfExists(Paths.get("keystore")); 52 53 // make sure that "-printcert" works with weak algorithms 54 OutputAnalyzer out = SecurityTools.keytool("-genkeypair " 55 + "-keystore keystore -storepass passphrase " 56 + "-keypass passphrase -keyalg rsa -keysize 512 " 57 + "-sigalg MD5withRSA -alias rsa_alias -dname CN=Server"); 58 System.out.println(out.getOutput()); 59 out.shouldHaveExitValue(0); 60 61 int port = new Server().start(); 62 if(port == -1) { 63 throw new RuntimeException("Unable start ssl server."); 64 } 65 String vmOpt = System.getProperty("TESTTOOLVMOPTS"); 66 String cmd = String.format( 67 "-debug %s -printcert -sslserver localhost:%s", 68 ((vmOpt == null) ? "" : vmOpt ), port); 69 70 out = SecurityTools.keytool(cmd); 71 System.out.println(out.getOutput()); 72 out.shouldHaveExitValue(0); 73 } 74 75 private static class Server implements Runnable { 76 77 private volatile int serverPort = -1; 78 private final CountDownLatch untilServerReady = new CountDownLatch(1); 79 80 public int start() throws InterruptedException { 81 82 Thread server = new Thread(this); 83 server.setDaemon(true); 84 server.start(); 85 untilServerReady.await(); 86 return this.getServerPort(); 87 } 88 89 @Override 90 public void run() { 91 92 System.setProperty("javax.net.ssl.keyStorePassword", "passphrase"); 93 System.setProperty("javax.net.ssl.keyStore", "keystore"); 94 SSLServerSocketFactory sslssf = 95 (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); 96 try (ServerSocket server = sslssf.createServerSocket(0)) { 97 this.serverPort = server.getLocalPort(); 98 System.out.printf("%nServer started on: %s%n", getServerPort()); 99 untilServerReady.countDown(); 100 ((SSLSocket)server.accept()).startHandshake(); 101 } catch (Throwable e) { 102 e.printStackTrace(System.out); 103 untilServerReady.countDown(); 104 } 105 106 } 107 108 public int getServerPort() { 109 return this.serverPort; 110 } 111 112 } 113 114 }