test/jdk/sun/security/mscapi/AccessKeyStore.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 40268 jdk/test/sun/security/mscapi/AccessKeyStore.java@5d2c9cf567a7
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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
     2
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    25
 * @test
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    26
 * @bug 6324295 6931562 8154113
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    27
 * @modules jdk.crypto.mscapi
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    28
 * @run main/othervm/java.security.policy==access.policy AccessKeyStore pass
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    29
 * @run main/othervm/java.security.policy==noaccess.policy AccessKeyStore fail
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    30
 * @summary Confirm that right permissions are granted to access keystores.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.interfaces.RSAKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class AccessKeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    44
        // Check for security manager and required arg(s)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (System.getSecurityManager() == null) {
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    46
            throw new Exception("Missing security manager");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    48
        if (args.length <= 0) {
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    49
            throw new Exception("Missing expected test status");
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    50
        }
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    51
        boolean shouldPass = args[0].equalsIgnoreCase("pass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Provider p = Security.getProvider("SunMSCAPI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println("SunMSCAPI provider classname is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            p.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         * If a SecurityManager exists then this will trigger a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         * SecurityException if the following permission has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * been granted:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         *     SecurityPermission("authProvider.SunMSCAPI")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            keyStore.load(null, null);
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    68
            if (!shouldPass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    "Expected KeyStore.load to throw a SecurityException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (SecurityException se) {
40268
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    73
            if (!shouldPass) {
5d2c9cf567a7 8154113: java.security.AccessControlException: access denied ("java.security.SecurityPermission" "authProvider.SunMSCAPI")
valeriep
parents: 33868
diff changeset
    74
                System.out.println("Expected exception thrown: " + se);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int i = 0;
33868
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents: 5506
diff changeset
    82
        for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements(); ) {
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents: 5506
diff changeset
    83
            String alias = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            displayEntry(keyStore, alias, i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static void displayEntry(KeyStore keyStore, String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int index) throws KeyStoreException, NoSuchAlgorithmException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (keyStore.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.out.println("[" + index + "]\n    " + alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                " [key-entry]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                Key key = keyStore.getKey(alias, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (key instanceof RSAKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    System.out.println("    Key type: " + key.getAlgorithm() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        " (" + ((RSAKey)key).getModulus().bitLength() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        " bit)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    System.out.println("    Key type: " + key.getAlgorithm() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.out.println("    Key type: Unknown\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.out.println("    Certificate chain: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    System.out.println("        ["+ (i + 1) + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    displayCert(chain[i], "            ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("[" + index + "]\n    " + alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                " [trusted-cert-entry]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.println("    Certificate chain: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    System.out.println("        ["+ (i + 1) + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    displayCert(chain[i], "            ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println("-------------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static void displayCert(Certificate cert, String tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (cert instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            X509Certificate x = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                tab + "Owner: " + x.getSubjectDN().toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                tab + "Issuer: " + x.getIssuerDN().toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                tab + "Serial number: " + x.getSerialNumber().toString(16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                "\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                tab + "Valid from: " + x.getNotBefore().toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                tab + "     until: " + x.getNotAfter().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.out.println(tab + "[unknown certificate format]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}