test/jdk/sun/security/mscapi/IterateWindowsRootStore.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33868
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     1
/*
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     4
 *
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     8
 *
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    13
 * accompanied this code).
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    14
 *
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    18
 *
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    21
 * questions.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    22
 */
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    23
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    24
import java.io.InputStream;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    25
import java.security.KeyStore;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    26
import java.security.Provider;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    27
import java.security.Security;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    28
import java.security.cert.CRL;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    29
import java.security.cert.CRLException;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    30
import java.security.cert.Certificate;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    31
import java.security.cert.CertificateException;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    32
import java.security.cert.CertificateFactorySpi;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    33
import java.util.Collection;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    34
import java.util.Enumeration;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    35
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    36
/*
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    37
 * @test
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    38
 * @bug 8139436
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    39
 * @summary This test validates an iteration over the Windows-ROOT certificate store
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    40
 *          and retrieving all certificates.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    41
 *          Bug 8139436 reports an issue when 3rd party JCE providers would throw exceptions
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    42
 *          upon creating Certificate objects.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    43
 *          This would for instance happen when using IAIK 3.15 and Elliptic Curve certificates
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    44
 *          are contained in the Windows-ROOT certificate store.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    45
 *          The test uses a simple dummy provider which just throws Exceptions in its CertificateFactory.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    46
 *          To test an external provider, you can use property sun.security.mscapi.testprovider and
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    47
 *          set it to the provider class name which has to be constructible by a constructor without
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    48
 *          arguments. The provider jar has to be added to the classpath.
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    49
 *          E.g. run jtreg with -javaoption:-Dsun.security.mscapi.testprovider=iaik.security.provider.IAIK and
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    50
 *          -cpa:<path to iaik_jce.jar>
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    51
 *
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    52
 * @requires os.family == "windows"
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    53
 * @author Christoph Langer
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    54
 * @run main IterateWindowsRootStore
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    55
 */
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    56
public class IterateWindowsRootStore {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    57
    public static class TestFactory extends CertificateFactorySpi {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    58
        @Override
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    59
        public Certificate engineGenerateCertificate(InputStream inStream) throws CertificateException {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    60
            throw new CertificateException("unimplemented");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    61
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    62
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    63
        @Override
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    64
        public Collection<? extends Certificate> engineGenerateCertificates(InputStream inStream) throws CertificateException {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    65
            throw new CertificateException("unimplemented");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    66
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    67
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    68
        @Override
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    69
        public CRL engineGenerateCRL(InputStream inStream) throws CRLException {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    70
            throw new CRLException("unimplemented");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    71
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    72
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    73
        @Override
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    74
        public Collection<? extends CRL> engineGenerateCRLs(InputStream inStream) throws CRLException {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    75
            throw new CRLException("unimplemented");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    76
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    77
    }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    78
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    79
    public static class TestProvider extends Provider {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    80
        private static final long serialVersionUID = 1L;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    81
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    82
        public TestProvider() {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    83
            super("TestProvider", 0.1, "Test provider for IterateWindowsRootStore");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    84
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    85
            /*
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    86
             * Certificates
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    87
             */
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    88
            this.put("CertificateFactory.X.509", "IterateWindowsRootStore$TestFactory");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    89
            this.put("Alg.Alias.CertificateFactory.X509", "X.509");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    90
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    91
    }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    92
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    93
    public static void main(String[] args) throws Exception {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    94
        // Try to register a JCE provider from property sun.security.mscapi.testprovider in the first slot
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    95
        // otherwise register a dummy provider which would provoke the issue of bug 8139436
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    96
        boolean providerPrepended = false;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    97
        String testprovider = System.getProperty("sun.security.mscapi.testprovider");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    98
        if (testprovider != null && !testprovider.isEmpty()) {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
    99
            try {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   100
                System.out.println("Trying to prepend external JCE provider " + testprovider);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   101
                Class<?> providerclass = Class.forName(testprovider);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   102
                Object provider = providerclass.newInstance();
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   103
                Security.insertProviderAt((Provider)provider, 1);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   104
            } catch (Exception e) {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   105
                System.out.println("Could not load JCE provider " + testprovider +". Exception is:");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   106
                e.printStackTrace(System.out);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   107
            }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   108
            providerPrepended = true;
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   109
            System.out.println("Sucessfully prepended JCE provider " + testprovider);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   110
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   111
        if (!providerPrepended) {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   112
            System.out.println("Trying to prepend dummy JCE provider");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   113
            Security.insertProviderAt(new TestProvider(), 1);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   114
            System.out.println("Sucessfully prepended dummy JCE provider");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   115
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   116
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   117
        // load Windows-ROOT KeyStore
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   118
        KeyStore keyStore = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   119
        keyStore.load(null, null);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   120
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   121
        // iterate KeyStore
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   122
        Enumeration<String> aliases = keyStore.aliases();
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   123
        while (aliases.hasMoreElements()) {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   124
            String alias = aliases.nextElement();
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   125
            System.out.print("Reading certificate for alias: " + alias + "...");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   126
            keyStore.getCertificate(alias);
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   127
            System.out.println(" done.");
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   128
        }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   129
    }
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents:
diff changeset
   130
}