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