test/jdk/sun/security/mscapi/KeyStoreEmptyCertChain.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45193 jdk/test/sun/security/mscapi/KeyStoreEmptyCertChain.java@a4c62ccf8688
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45193
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     1
/*
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     4
 *
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     8
 *
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    13
 * accompanied this code).
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    14
 *
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    18
 *
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    21
 * questions.
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    22
 */
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    23
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    24
/*
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    25
 * @test
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    26
 * @bug 8172244
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    27
 * @summary Verify that no exception is thrown with empty cert chain
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    28
 *    in MSCAPI.
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    29
 * @requires os.family == "windows"
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    30
 * @modules java.base/sun.security.tools.keytool java.base/sun.security.x509
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    31
 * @run main/othervm --add-opens java.base/java.security=ALL-UNNAMED
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    32
 *      KeyStoreEmptyCertChain
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    33
 */
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    34
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    35
import java.security.KeyStore;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    36
import java.security.cert.Certificate;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    37
import sun.security.x509.X500Name;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    38
import sun.security.tools.keytool.CertAndKeyGen;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    39
import java.security.KeyPairGenerator;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    40
import java.security.KeyPair;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    41
import java.security.PrivateKey;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    42
import java.security.KeyStoreSpi;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    43
import java.lang.reflect.*;
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    44
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    45
public class KeyStoreEmptyCertChain {
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    46
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    47
    public static void main(String[] args) {
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    48
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    49
        try {
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    50
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    51
            KeyStore keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    52
            keyStore.load(null, null);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    53
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    54
            // Generate a certificate to use for testing
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    55
            CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    56
            gen.generate(2048);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    57
            Certificate cert =
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    58
                gen.getSelfCertificate(new X500Name("CN=test"), 3600);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    59
            String alias = "JDK-8172244";
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    60
            char[] password = "password".toCharArray();
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    61
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    62
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    63
            // generate a private key for the certificate
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    64
            kpg.initialize(2048);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    65
            KeyPair keyPair = kpg.generateKeyPair();
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    66
            PrivateKey privKey = keyPair.getPrivate();
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    67
            // need to bypass checks to store the private key without the cert
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    68
            Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    69
            spiField.setAccessible(true);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    70
            KeyStoreSpi spi = (KeyStoreSpi) spiField.get(keyStore);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    71
            spi.engineSetKeyEntry(alias, privKey, password, new Certificate[0]);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    72
            keyStore.store(null, null);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    73
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    74
            keyStore.getCertificateAlias(cert);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    75
            keyStore.deleteEntry(alias);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    76
            // test passes if no exception is thrown
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    77
        } catch (Exception ex) {
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    78
            throw new RuntimeException(ex);
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    79
        }
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    80
    }
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents:
diff changeset
    81
}