1 /* |
|
2 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.security.validator; |
|
27 |
|
28 import java.io.*; |
|
29 import java.util.*; |
|
30 |
|
31 import java.security.*; |
|
32 import java.security.cert.*; |
|
33 import java.security.cert.Certificate; |
|
34 |
|
35 import sun.security.action.*; |
|
36 |
|
37 /** |
|
38 * Collection of static utility methods related to KeyStores. |
|
39 * |
|
40 * @author Andreas Sterbenz |
|
41 */ |
|
42 public class KeyStores { |
|
43 |
|
44 private KeyStores() { |
|
45 // empty |
|
46 } |
|
47 |
|
48 // in the future, all accesses to the system cacerts keystore should |
|
49 // go through this class. but not right now. |
|
50 /* |
|
51 private static final String javaHome = |
|
52 (String)AccessController.doPrivileged(new GetPropertyAction("java.home")); |
|
53 |
|
54 private static final char SEP = File.separatorChar; |
|
55 |
|
56 private static KeyStore caCerts; |
|
57 |
|
58 private static KeyStore getKeyStore(String type, String name, |
|
59 char[] password) throws IOException { |
|
60 if (type == null) { |
|
61 type = "JKS"; |
|
62 } |
|
63 try { |
|
64 KeyStore ks = KeyStore.getInstance(type); |
|
65 FileInputStream in = (FileInputStream)AccessController.doPrivileged |
|
66 (new OpenFileInputStreamAction(name)); |
|
67 ks.load(in, password); |
|
68 return ks; |
|
69 } catch (GeneralSecurityException e) { |
|
70 // XXX |
|
71 throw new IOException(); |
|
72 } catch (PrivilegedActionException e) { |
|
73 throw (IOException)e.getCause(); |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 * Return a KeyStore with the contents of the lib/security/cacerts file. |
|
79 * The file is only opened once per JVM invocation and the contents |
|
80 * cached subsequently. |
|
81 * |
|
82 public static synchronized KeyStore getCaCerts() throws IOException { |
|
83 if (caCerts != null) { |
|
84 return caCerts; |
|
85 } |
|
86 String name = javaHome + SEP + "lib" + SEP + "security" + SEP + "cacerts"; |
|
87 caCerts = getKeyStore(null, name, null); |
|
88 return caCerts; |
|
89 } |
|
90 */ |
|
91 |
|
92 /** |
|
93 * Return a Set with all trusted X509Certificates contained in |
|
94 * this KeyStore. |
|
95 */ |
|
96 public static Set<X509Certificate> getTrustedCerts(KeyStore ks) { |
|
97 Set<X509Certificate> set = new HashSet<X509Certificate>(); |
|
98 try { |
|
99 for (Enumeration<String> e = ks.aliases(); e.hasMoreElements(); ) { |
|
100 String alias = e.nextElement(); |
|
101 if (ks.isCertificateEntry(alias)) { |
|
102 Certificate cert = ks.getCertificate(alias); |
|
103 if (cert instanceof X509Certificate) { |
|
104 set.add((X509Certificate)cert); |
|
105 } |
|
106 } else if (ks.isKeyEntry(alias)) { |
|
107 Certificate[] certs = ks.getCertificateChain(alias); |
|
108 if ((certs != null) && (certs.length > 0) && |
|
109 (certs[0] instanceof X509Certificate)) { |
|
110 set.add((X509Certificate)certs[0]); |
|
111 } |
|
112 } |
|
113 } |
|
114 } catch (KeyStoreException e) { |
|
115 // ignore |
|
116 } |
|
117 return set; |
|
118 } |
|
119 |
|
120 } |
|