Path: blob/master/test/jdk/sun/security/tools/keytool/DupImport.java
41152 views
/*1* Copyright (c) 2016, 2019, 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 696854226* @summary keytool -importcert cannot deal with duplicate certs27* @modules java.base/sun.security.tools.keytool28* @compile -XDignore.symbol.file DupImport.java29* @run main DupImport pkcs1230* @run main DupImport jks31*/3233import java.io.File;34import java.nio.file.Files;35import java.nio.file.Paths;36import java.security.KeyStore;37import java.security.cert.Certificate;38import java.security.cert.X509Certificate;39import java.util.ArrayList;40import java.util.Arrays;41import java.util.List;4243public class DupImport {4445static String storeType = null;4647public static void main(String[] args) throws Exception {4849storeType = args[0];50Files.deleteIfExists(Paths.get("dup.ks"));5152// Create chain: root -> int -> me53run("-genkeypair -keyalg DSA -alias me -dname CN=Me");54run("-genkeypair -keyalg DSA -alias int -dname CN=Int");55run("-genkeypair -keyalg DSA -alias root -dname CN=Root");5657run("-certreq -alias int -file int.req");58run("-gencert -infile int.req -alias root -rfc -outfile int.resp");59run("-importcert -file int.resp -alias int");6061run("-certreq -alias me -file me.req");62run("-gencert -infile me.req -alias int -rfc -outfile me.resp");63run("-importcert -file me.resp -alias me");6465// Export certs66run("-exportcert -alias me -file me -rfc");67run("-exportcert -alias int -file int -rfc");68run("-exportcert -alias root -file root -rfc");6970// test 1: just the 3 certs71test("me", "int", "root");7273// test 2: 3 chains (without root) concatenated74test("me", "int", "int", "root");7576// test 3: 3 full chains concatenated77test("me", "int", "root", "int", "root", "root");7879// test 4: a mess80test("root", "me", "int", "int", "me", "me", "root", "int");81}8283// Run keytool command with common options84static void run(String s) throws Exception {85sun.security.tools.keytool.Main.main((86"-keystore dup.ks -storepass changeit -keypass changeit "87+ "-storetype " + storeType + " -debug "88+ s).split(" "));89}9091// Test "cat files... | keytool -import"92static void test(String... files) throws Exception {9394System.out.println("Testing " + Arrays.toString(files));9596List<String> all = new ArrayList<>();97for (String file : files) {98all.addAll(Files.readAllLines(Paths.get(file)));99}100Files.write(Paths.get("reply"), all);101102run("-importcert -file reply -alias me");103KeyStore ks = KeyStore.getInstance(104new File("dup.ks"), "changeit".toCharArray());105Certificate[] chain = ks.getCertificateChain("me");106if (chain.length != 3) {107throw new Exception("Length is " + chain.length);108}109110checkName(chain[0], "CN=Me");111checkName(chain[1], "CN=Int");112checkName(chain[2], "CN=Root");113}114115// Check if c's dname is expected116static void checkName(Certificate c, String expected) throws Exception {117X509Certificate x = (X509Certificate)c;118String name = x.getSubjectX500Principal().toString();119if (!expected.equals(name)) {120throw new Exception("Expected " + expected + ", but " + name);121}122}123}124125126