jdk/test/sun/security/mscapi/AccessKeyStore.java
author vinnie
Fri, 29 Apr 2011 00:21:54 +0100
changeset 9533 13cc5e8eb9f1
parent 5506 202f599c92aa
child 33868 9c1bde39fe18
permissions -rw-r--r--
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @see AccessKeyStore.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.interfaces.RSAKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class AccessKeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        // Check if the provider is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            Class.forName("sun.security.mscapi.SunMSCAPI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                "The SunMSCAPI provider is not available on this platform: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // Check that a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (System.getSecurityManager() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            throw new Exception("A security manager has not been installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Provider p = Security.getProvider("SunMSCAPI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("SunMSCAPI provider classname is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            p.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * If a SecurityManager exists then this will trigger a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * SecurityException if the following permission has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * been granted:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         *     SecurityPermission("authProvider.SunMSCAPI")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            keyStore.load(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (args.length > 0 && "-deny".equals(args[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    "Expected KeyStore.load to throw a SecurityException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (args.length > 0 && "-deny".equals(args[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                System.out.println("Caught the expected exception: " + se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw se;
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
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (Enumeration e = keyStore.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            String alias = (String) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            displayEntry(keyStore, alias, i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static void displayEntry(KeyStore keyStore, String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int index) throws KeyStoreException, NoSuchAlgorithmException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (keyStore.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.println("[" + index + "]\n    " + alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                " [key-entry]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                Key key = keyStore.getKey(alias, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (key instanceof RSAKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    System.out.println("    Key type: " + key.getAlgorithm() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        " (" + ((RSAKey)key).getModulus().bitLength() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        " bit)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    System.out.println("    Key type: " + key.getAlgorithm() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                System.out.println("    Key type: Unknown\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("    Certificate chain: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    System.out.println("        ["+ (i + 1) + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    displayCert(chain[i], "            ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println("[" + index + "]\n    " + alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                " [trusted-cert-entry]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                System.out.println("    Certificate chain: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    System.out.println("        ["+ (i + 1) + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    displayCert(chain[i], "            ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        System.out.println("-------------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static void displayCert(Certificate cert, String tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (cert instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            X509Certificate x = (X509Certificate) cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                tab + "Owner: " + x.getSubjectDN().toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                tab + "Issuer: " + x.getIssuerDN().toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                tab + "Serial number: " + x.getSerialNumber().toString(16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                "\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                tab + "Valid from: " + x.getNotBefore().toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                tab + "     until: " + x.getNotAfter().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            System.out.println(tab + "[unknown certificate format]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}