Path: blob/master/test/jdk/sun/security/pkcs12/SameDN.java
41149 views
/*1* Copyright (c) 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*/2223import static jdk.test.lib.SecurityTools.keytool;2425import java.io.File;26import java.security.KeyStore;2728/*29* @test30* @bug 821577631* @library /test/lib32* @summary Keytool importkeystore may mix up certificate chain entries when DNs conflict33*/34public class SameDN {3536private static final String COMMON = "-keystore ks -storepass changeit ";3738public static final void main(String[] args) throws Exception {39genkeypair("ca1", "CN=CA");40genkeypair("ca2", "CN=CA");41genkeypair("user1", "CN=user");42genkeypair("user2", "CN=user");43gencert("ca1", "user1");44gencert("ca2", "user2");4546KeyStore ks = KeyStore.getInstance(47new File("ks"), "changeit".toCharArray());48if (!ks.getCertificate("ca1").equals(ks.getCertificateChain("user1")[1])) {49throw new Exception("user1 not signed by ca1");50}51if (!ks.getCertificate("ca2").equals(ks.getCertificateChain("user2")[1])) {52throw new Exception("user2 not signed by ca2");53}54}5556static void genkeypair(String alias, String dn) throws Exception {57keytool(COMMON + "-genkeypair -keyalg DSA -alias " + alias + " -dname " + dn)58.shouldHaveExitValue(0);59}6061static void gencert(String issuer, String subject) throws Exception {62keytool(COMMON + "-certreq -alias " + subject + " -file req")63.shouldHaveExitValue(0);64keytool(COMMON + "-gencert -alias " + issuer + " -infile req -outfile cert")65.shouldHaveExitValue(0);66keytool(COMMON + "-importcert -alias " + subject + " -file cert")67.shouldHaveExitValue(0);68}69}707172