Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/stores/Stores.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15
16 package org.globus.gsi.stores;
17
18 import java.io.IOException;
19 import java.security.GeneralSecurityException;
20 import java.security.InvalidAlgorithmParameterException;
21 import java.security.KeyStore;
22 import java.security.KeyStoreException;
23 import java.security.NoSuchAlgorithmException;
24 import java.security.NoSuchProviderException;
25 import java.security.cert.CertStore;
26 import java.security.cert.CertificateException;
27 import java.util.HashMap;
28
29 import org.globus.common.CoGProperties;
30 import org.globus.gsi.provider.GlobusProvider;
31 import org.globus.gsi.provider.KeyStoreParametersFactory;
32
33 /**
34 * @author Jerome Revillard
35 *
36 */
37 public class Stores {
38 private static String defaultCAFilesPattern = "*.0";
39 private static String defaultCRLFilesPattern = "*.r*";
40 private static String defaultSigningPolicyFilesPattern = "*.signing_policy";
41
42 private static final HashMap<String, ReloadableTrustStore> TRUST_STORES = new HashMap<String, ReloadableTrustStore>();
43 private static final HashMap<String, ReloadableCrlStore> CRL_STORES = new HashMap<String, ReloadableCrlStore>();
44 private static final HashMap<String, ReloadableCaCertStore> CA_CERT_STORES = new HashMap<String, ReloadableCaCertStore>();
45 private static final HashMap<String, ResourceSigningPolicyStore> SIGNING_POLICY_STORES = new HashMap<String, ResourceSigningPolicyStore>();
46 private final static long CACHE_TIME_MILLIS = 3600 * 1000;
47
48 public static KeyStore getDefaultTrustStore() throws GeneralSecurityException, IOException {
49 String pattern = "file:" + CoGProperties.getDefault().getCaCertLocations() + "/" + defaultCAFilesPattern;
50 return getTrustStore(pattern);
51 }
52
53 public static KeyStore getTrustStore(String casLocationPattern) throws GeneralSecurityException, IOException {
54 synchronized (TRUST_STORES) {
55 ReloadableTrustStore reloadableKeystore = TRUST_STORES.get(casLocationPattern);
56 if (reloadableKeystore != null) {
57 return reloadableKeystore.getTrustStore();
58 }
59 reloadableKeystore = new ReloadableTrustStore(casLocationPattern);
60 TRUST_STORES.put(casLocationPattern, reloadableKeystore);
61 return reloadableKeystore.getTrustStore();
62 }
63 }
64
65 public static CertStore getDefaultCACertStore() throws GeneralSecurityException, NoSuchAlgorithmException {
66 String pattern = "file:" + CoGProperties.getDefault().getCaCertLocations() + "/" + defaultCAFilesPattern;
67 return getCACertStore(pattern);
68 }
69
70 public static CertStore getCACertStore(String casLocationPattern) throws GeneralSecurityException,
71 NoSuchAlgorithmException {
72 synchronized (CA_CERT_STORES) {
73 ReloadableCaCertStore reloadableCaCertStore = CA_CERT_STORES.get(casLocationPattern);
74 if (reloadableCaCertStore == null) {
75 reloadableCaCertStore = new ReloadableCaCertStore(casLocationPattern);
76 CA_CERT_STORES.put(casLocationPattern, reloadableCaCertStore);
77 }
78 return reloadableCaCertStore.getCaCertStore();
79 }
80 }
81
82 public static CertStore getDefaultCRLStore() throws GeneralSecurityException, NoSuchAlgorithmException {
83 String pattern = "file:" + CoGProperties.getDefault().getCaCertLocations() + "/" + defaultCRLFilesPattern;
84 return getCRLStore(pattern);
85 }
86
87 public static CertStore getCRLStore(String crlsLocationPattern) throws GeneralSecurityException,
88 NoSuchAlgorithmException {
89 synchronized (CRL_STORES) {
90 ReloadableCrlStore reloadableCrlStore = CRL_STORES.get(crlsLocationPattern);
91 if (reloadableCrlStore == null) {
92 reloadableCrlStore = new ReloadableCrlStore(crlsLocationPattern);
93 CRL_STORES.put(crlsLocationPattern, reloadableCrlStore);
94 }
95 return reloadableCrlStore.getCrlStore();
96 }
97 }
98
99 public static ResourceSigningPolicyStore getDefaultSigningPolicyStore() throws GeneralSecurityException {
100 String pattern = "file:" + CoGProperties.getDefault().getCaCertLocations() + "/"
101 + defaultSigningPolicyFilesPattern;
102 return getSigningPolicyStore(pattern);
103 }
104
105 public static ResourceSigningPolicyStore getSigningPolicyStore(String signingPolicyLocationPattern)
106 throws GeneralSecurityException {
107 synchronized (SIGNING_POLICY_STORES) {
108 ResourceSigningPolicyStore signingPolicyStore = SIGNING_POLICY_STORES.get(signingPolicyLocationPattern);
109 if (signingPolicyStore == null) {
110 signingPolicyStore = new ResourceSigningPolicyStore(new ResourceSigningPolicyStoreParameters(
111 signingPolicyLocationPattern));
112 SIGNING_POLICY_STORES.put(signingPolicyLocationPattern, signingPolicyStore);
113 }
114 return signingPolicyStore;
115 }
116 }
117
118 public static String getDefaultCAFilesPattern() {
119 return defaultCAFilesPattern;
120 }
121
122 public static void setDefaultCAFilesPattern(String defaultCAFilesPattern) {
123 synchronized (TRUST_STORES) {
124 synchronized (CA_CERT_STORES) {
125 if (defaultCAFilesPattern == null || Stores.defaultCAFilesPattern.equals(defaultCAFilesPattern)) {
126 return;
127 }
128 Stores.defaultCAFilesPattern = defaultCAFilesPattern;
129 // Clear if we change the default pattern to prevent potential
130 // memory issue;
131 TRUST_STORES.clear();
132 CA_CERT_STORES.clear();
133 }
134 }
135 }
136
137 public static String getDefaultCRLFilesPattern() {
138 return defaultCRLFilesPattern;
139 }
140
141 public static void setDefaultCRLFilesPattern(String defaultCRLFilesPattern) {
142 synchronized (CRL_STORES) {
143 if (defaultCRLFilesPattern == null || Stores.defaultCRLFilesPattern.equals(defaultCRLFilesPattern)) {
144 return;
145 }
146 Stores.defaultCRLFilesPattern = defaultCRLFilesPattern;
147 // Clear if we change the default pattern to prevent potential
148 // memory issue;
149 CRL_STORES.clear();
150 }
151 }
152
153 public static String getDefaultSigningPolicyFilesPattern() {
154 return defaultSigningPolicyFilesPattern;
155 }
156
157 public static void setDefaultSigningPolicyFilesPattern(String defaultSigningPolicyFilesPattern) {
158 synchronized (SIGNING_POLICY_STORES) {
159 if (defaultSigningPolicyFilesPattern == null
160 || Stores.defaultSigningPolicyFilesPattern.equals(defaultSigningPolicyFilesPattern)) {
161 return;
162 }
163 Stores.defaultSigningPolicyFilesPattern = defaultSigningPolicyFilesPattern;
164 // Clear if we change the default pattern to prevent potential
165 // memory issue;
166 SIGNING_POLICY_STORES.clear();
167 }
168 }
169
170 private static class ReloadableTrustStore {
171 private final String casLocationPattern;
172 private final KeyStore keyStore;
173 private long lastUpdateTime;
174
175 protected ReloadableTrustStore(String casLocationPattern) throws KeyStoreException, NoSuchProviderException,
176 NoSuchAlgorithmException, CertificateException, IOException {
177 this.casLocationPattern = casLocationPattern;
178 keyStore = KeyStore.getInstance(GlobusProvider.KEYSTORE_TYPE, GlobusProvider.PROVIDER_NAME);
179 reload();
180 }
181
182 private void reload() throws NoSuchAlgorithmException, CertificateException, IOException {
183 keyStore.load(KeyStoreParametersFactory.createTrustStoreParameters(casLocationPattern));
184 lastUpdateTime = System.currentTimeMillis();
185 }
186
187 protected boolean isStillValid() {
188 return lastUpdateTime + CACHE_TIME_MILLIS > System.currentTimeMillis();
189 }
190
191 protected KeyStore getTrustStore() throws NoSuchAlgorithmException, CertificateException, IOException {
192 if (!isStillValid()) {
193 reload();
194 }
195 return keyStore;
196 }
197 }
198
199 private static class ReloadableCrlStore {
200 private final String crlsLocationPattern;
201 private CertStore certStore;
202 private long lastUpdateTime;
203
204 protected ReloadableCrlStore(String crlsLocationPattern) throws InvalidAlgorithmParameterException,
205 NoSuchAlgorithmException {
206 this.crlsLocationPattern = crlsLocationPattern;
207 load();
208 }
209
210 private void load() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
211 certStore = CertStore.getInstance(GlobusProvider.CERTSTORE_TYPE, new ResourceCertStoreParameters(null,
212 crlsLocationPattern));
213 lastUpdateTime = System.currentTimeMillis();
214 }
215
216 protected boolean isStillValid() {
217 return lastUpdateTime + CACHE_TIME_MILLIS > System.currentTimeMillis();
218 }
219
220 protected CertStore getCrlStore() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
221 if (!isStillValid()) {
222 load();
223 }
224 return certStore;
225 }
226 }
227
228 private static class ReloadableCaCertStore {
229 private final String casLocationPattern;
230 private CertStore certStore;
231 private long lastUpdateTime;
232
233 protected ReloadableCaCertStore(String casLocationPattern) throws InvalidAlgorithmParameterException,
234 NoSuchAlgorithmException {
235 this.casLocationPattern = casLocationPattern;
236 load();
237 }
238
239 private void load() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
240 certStore = CertStore.getInstance(GlobusProvider.CERTSTORE_TYPE, new ResourceCertStoreParameters(
241 casLocationPattern, null));
242 lastUpdateTime = System.currentTimeMillis();
243 }
244
245 protected boolean isStillValid() {
246 return lastUpdateTime + CACHE_TIME_MILLIS > System.currentTimeMillis();
247 }
248
249 protected CertStore getCaCertStore() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
250 if (!isStillValid()) {
251 load();
252 }
253 return certStore;
254 }
255 }
256 }