blob: a44f703a970f47e3b4391e95869a621f1166fc47 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @see AccessKeyStore.sh
26 */
27
28import java.security.Provider;
29import java.security.*;
30import java.security.cert.*;
31import java.security.cert.Certificate;
32import java.security.interfaces.RSAKey;
33import java.util.Enumeration;
34
35public class AccessKeyStore {
36
37 public static void main(String[] args) throws Exception {
38
39 // Check if the provider is available
40 try {
41 Class.forName("sun.security.mscapi.SunMSCAPI");
42
43 } catch (Exception e) {
44 System.out.println(
45 "The SunMSCAPI provider is not available on this platform: " +
46 e);
47 return;
48 }
49
50 // Check that a security manager has been installed
51 if (System.getSecurityManager() == null) {
52 throw new Exception("A security manager has not been installed");
53 }
54
55 Provider p = Security.getProvider("SunMSCAPI");
56
57 System.out.println("SunMSCAPI provider classname is " +
58 p.getClass().getName());
59
60 KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);
61
62 /*
63 * If a SecurityManager exists then this will trigger a
64 * SecurityException if the following permission has not
65 * been granted:
66 *
67 * SecurityPermission("authProvider.SunMSCAPI")
68 */
69 try {
70
71 keyStore.load(null, null);
72
73 if (args.length > 0 && "-deny".equals(args[0])) {
74 throw new Exception(
75 "Expected KeyStore.load to throw a SecurityException");
76 }
77
78 } catch (SecurityException se) {
79
80 if (args.length > 0 && "-deny".equals(args[0])) {
81 System.out.println("Caught the expected exception: " + se);
82 return;
83 } else {
84 throw se;
85 }
86 }
87
88 int i = 0;
89 for (Enumeration e = keyStore.aliases(); e.hasMoreElements(); ) {
90 String alias = (String) e.nextElement();
91 displayEntry(keyStore, alias, i++);
92 }
93 }
94
95 private static void displayEntry(KeyStore keyStore, String alias,
96 int index) throws KeyStoreException, NoSuchAlgorithmException {
97
98 if (keyStore.isKeyEntry(alias)) {
99 System.out.println("[" + index + "]\n " + alias +
100 " [key-entry]\n");
101
102 try {
103
104 Key key = keyStore.getKey(alias, null);
105
106 if (key instanceof RSAKey) {
107 System.out.println(" Key type: " + key.getAlgorithm() +
108 " (" + ((RSAKey)key).getModulus().bitLength() +
109 " bit)\n");
110 } else {
111 System.out.println(" Key type: " + key.getAlgorithm() +
112 "\n");
113 }
114
115 } catch (UnrecoverableKeyException e) {
116 System.out.println(" Key type: Unknown\n");
117 }
118
119 Certificate[] chain = keyStore.getCertificateChain(alias);
120 if (chain != null) {
121 System.out.println(" Certificate chain: ");
122 for (int i = 0; i < chain.length; i ++) {
123 System.out.println(" ["+ (i + 1) + "]");
124 displayCert(chain[i], " ");
125 }
126 }
127
128 } else {
129 System.out.println("[" + index + "]\n " + alias +
130 " [trusted-cert-entry]\n");
131 Certificate[] chain = keyStore.getCertificateChain(alias);
132 if (chain != null) {
133 System.out.println(" Certificate chain: ");
134 for (int i = 0; i < chain.length; i ++) {
135 System.out.println(" ["+ (i + 1) + "]");
136 displayCert(chain[i], " ");
137 }
138 }
139 }
140 System.out.println("-------------------------------------------------");
141 }
142
143 private static void displayCert(Certificate cert, String tab) {
144 if (cert instanceof X509Certificate) {
145 X509Certificate x = (X509Certificate) cert;
146 System.out.println(
147 tab + "Owner: " + x.getSubjectDN().toString() + "\n" +
148 tab + "Issuer: " + x.getIssuerDN().toString() + "\n" +
149 tab + "Serial number: " + x.getSerialNumber().toString(16) +
150 "\n"+
151 tab + "Valid from: " + x.getNotBefore().toString() + "\n" +
152 tab + " until: " + x.getNotAfter().toString());
153 } else {
154 System.out.println(tab + "[unknown certificate format]");
155 }
156 System.out.println();
157 }
158}