Path: blob/master/test/jdk/sun/security/mscapi/IterateWindowsRootStore.java
41152 views
/*1* Copyright (c) 2015, 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 java.io.InputStream;24import java.security.KeyStore;25import java.security.Provider;26import java.security.Security;27import java.security.cert.CRL;28import java.security.cert.CRLException;29import java.security.cert.Certificate;30import java.security.cert.CertificateException;31import java.security.cert.CertificateFactorySpi;32import java.util.Collection;33import java.util.Enumeration;3435/*36* @test37* @bug 813943638* @summary This test validates an iteration over the Windows-ROOT certificate store39* and retrieving all certificates.40* Bug 8139436 reports an issue when 3rd party JCE providers would throw exceptions41* upon creating Certificate objects.42* This would for instance happen when using IAIK 3.15 and Elliptic Curve certificates43* are contained in the Windows-ROOT certificate store.44* The test uses a simple dummy provider which just throws Exceptions in its CertificateFactory.45* To test an external provider, you can use property sun.security.mscapi.testprovider and46* set it to the provider class name which has to be constructible by a constructor without47* arguments. The provider jar has to be added to the classpath.48* E.g. run jtreg with -javaoption:-Dsun.security.mscapi.testprovider=iaik.security.provider.IAIK and49* -cpa:<path to iaik_jce.jar>50*51* @requires os.family == "windows"52* @author Christoph Langer53* @run main IterateWindowsRootStore54*/55public class IterateWindowsRootStore {56public static class TestFactory extends CertificateFactorySpi {57@Override58public Certificate engineGenerateCertificate(InputStream inStream) throws CertificateException {59throw new CertificateException("unimplemented");60}6162@Override63public Collection<? extends Certificate> engineGenerateCertificates(InputStream inStream) throws CertificateException {64throw new CertificateException("unimplemented");65}6667@Override68public CRL engineGenerateCRL(InputStream inStream) throws CRLException {69throw new CRLException("unimplemented");70}7172@Override73public Collection<? extends CRL> engineGenerateCRLs(InputStream inStream) throws CRLException {74throw new CRLException("unimplemented");75}76}7778public static class TestProvider extends Provider {79private static final long serialVersionUID = 1L;8081public TestProvider() {82super("TestProvider", 0.1, "Test provider for IterateWindowsRootStore");8384/*85* Certificates86*/87this.put("CertificateFactory.X.509", "IterateWindowsRootStore$TestFactory");88this.put("Alg.Alias.CertificateFactory.X509", "X.509");89}90}9192public static void main(String[] args) throws Exception {93// Try to register a JCE provider from property sun.security.mscapi.testprovider in the first slot94// otherwise register a dummy provider which would provoke the issue of bug 813943695boolean providerPrepended = false;96String testprovider = System.getProperty("sun.security.mscapi.testprovider");97if (testprovider != null && !testprovider.isEmpty()) {98try {99System.out.println("Trying to prepend external JCE provider " + testprovider);100Class<?> providerclass = Class.forName(testprovider);101Object provider = providerclass.newInstance();102Security.insertProviderAt((Provider)provider, 1);103} catch (Exception e) {104System.out.println("Could not load JCE provider " + testprovider +". Exception is:");105e.printStackTrace(System.out);106}107providerPrepended = true;108System.out.println("Sucessfully prepended JCE provider " + testprovider);109}110if (!providerPrepended) {111System.out.println("Trying to prepend dummy JCE provider");112Security.insertProviderAt(new TestProvider(), 1);113System.out.println("Sucessfully prepended dummy JCE provider");114}115116// load Windows-ROOT KeyStore117KeyStore keyStore = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");118keyStore.load(null, null);119120// iterate KeyStore121Enumeration<String> aliases = keyStore.aliases();122while (aliases.hasMoreElements()) {123String alias = aliases.nextElement();124System.out.print("Reading certificate for alias: " + alias + "...");125keyStore.getCertificate(alias);126System.out.println(" done.");127}128}129}130131132