test/jdk/sun/security/pkcs12/SameDN.java
author weijun
Fri, 15 Nov 2019 09:06:58 +0800
changeset 59104 046e4024e55a
parent 53420 2190f45140b1
permissions -rw-r--r--
8214024: Remove the default keytool -keyalg value Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53420
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     1
/*
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     4
 *
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     8
 *
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    13
 * accompanied this code).
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    14
 *
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    18
 *
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    21
 * questions.
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    22
 */
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    23
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    24
import static jdk.test.lib.SecurityTools.keytool;
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    25
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    26
import java.io.File;
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    27
import java.security.KeyStore;
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    28
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    29
/*
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    30
 * @test
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    31
 * @bug 8215776
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    32
 * @library /test/lib
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    33
 * @summary Keytool importkeystore may mix up certificate chain entries when DNs conflict
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    34
 */
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    35
public class SameDN {
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    36
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    37
    private static final String COMMON = "-keystore ks -storepass changeit ";
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    38
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    39
    public static final void main(String[] args) throws Exception {
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    40
        genkeypair("ca1", "CN=CA");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    41
        genkeypair("ca2", "CN=CA");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    42
        genkeypair("user1", "CN=user");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    43
        genkeypair("user2", "CN=user");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    44
        gencert("ca1", "user1");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    45
        gencert("ca2", "user2");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    46
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    47
        KeyStore ks = KeyStore.getInstance(
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    48
                new File("ks"), "changeit".toCharArray());
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    49
        if (!ks.getCertificate("ca1").equals(ks.getCertificateChain("user1")[1])) {
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    50
            throw new Exception("user1 not signed by ca1");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    51
        }
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    52
        if (!ks.getCertificate("ca2").equals(ks.getCertificateChain("user2")[1])) {
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    53
            throw new Exception("user2 not signed by ca2");
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    54
        }
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    55
    }
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    56
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    57
    static void genkeypair(String alias, String dn) throws Exception {
59104
046e4024e55a 8214024: Remove the default keytool -keyalg value
weijun
parents: 53420
diff changeset
    58
        keytool(COMMON + "-genkeypair -keyalg DSA -alias " + alias + " -dname " + dn)
53420
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    59
                .shouldHaveExitValue(0);
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    60
    }
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    61
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    62
    static void gencert(String issuer, String subject) throws Exception {
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    63
        keytool(COMMON + "-certreq -alias " + subject + " -file req")
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    64
                .shouldHaveExitValue(0);
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    65
        keytool(COMMON + "-gencert -alias " + issuer + " -infile req -outfile cert")
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    66
                .shouldHaveExitValue(0);
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    67
        keytool(COMMON + "-importcert -alias " + subject + " -file cert")
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    68
                .shouldHaveExitValue(0);
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    69
    }
2190f45140b1 8215776: Keytool importkeystore may mix up certificate chain entries when DNs conflict
weijun
parents:
diff changeset
    70
}