Path: blob/master/test/jdk/sun/security/mscapi/KeyStoreEmptyCertChain.java
41149 views
/*1* Copyright (c) 2017, 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 817224426* @summary Verify that no exception is thrown with empty cert chain27* in MSCAPI.28* @requires os.family == "windows"29* @modules java.base/sun.security.tools.keytool java.base/sun.security.x50930* @run main/othervm --add-opens java.base/java.security=ALL-UNNAMED31* KeyStoreEmptyCertChain32*/3334import java.security.KeyStore;35import java.security.cert.Certificate;36import sun.security.x509.X500Name;37import sun.security.tools.keytool.CertAndKeyGen;38import java.security.KeyPairGenerator;39import java.security.KeyPair;40import java.security.PrivateKey;41import java.security.KeyStoreSpi;42import java.lang.reflect.*;4344public class KeyStoreEmptyCertChain {4546public static void main(String[] args) {4748try {4950KeyStore keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");51keyStore.load(null, null);5253// Generate a certificate to use for testing54CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");55gen.generate(2048);56Certificate cert =57gen.getSelfCertificate(new X500Name("CN=test"), 3600);58String alias = "JDK-8172244";59char[] password = "password".toCharArray();60KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");6162// generate a private key for the certificate63kpg.initialize(2048);64KeyPair keyPair = kpg.generateKeyPair();65PrivateKey privKey = keyPair.getPrivate();66// need to bypass checks to store the private key without the cert67Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");68spiField.setAccessible(true);69KeyStoreSpi spi = (KeyStoreSpi) spiField.get(keyStore);70spi.engineSetKeyEntry(alias, privKey, password, new Certificate[0]);71keyStore.store(null, null);7273keyStore.getCertificateAlias(cert);74keyStore.deleteEntry(alias);75// test passes if no exception is thrown76} catch (Exception ex) {77throw new RuntimeException(ex);78}79}80}818283