| 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 |
package org.globus.gsi; |
| 16 |
|
| 17 |
import org.globus.gsi.stores.ResourceCertStoreParameters; |
| 18 |
import org.globus.gsi.stores.Stores; |
| 19 |
|
| 20 |
import org.globus.gsi.provider.GlobusProvider; |
| 21 |
|
| 22 |
import javax.security.auth.x500.X500Principal; |
| 23 |
import java.security.cert.X509CRLSelector; |
| 24 |
import java.security.cert.CertStore; |
| 25 |
import java.security.cert.X509CRL; |
| 26 |
import java.util.Map; |
| 27 |
import java.util.Collection; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.HashMap; |
| 30 |
import java.util.LinkedList; |
| 31 |
import java.util.ListIterator; |
| 32 |
import java.util.StringTokenizer; |
| 33 |
import java.io.File; |
| 34 |
import org.globus.common.CoGProperties; |
| 35 |
import org.apache.commons.logging.Log; |
| 36 |
import org.apache.commons.logging.LogFactory; |
| 37 |
|
| 38 |
// COMMENT: what should be used instead? Probably a cert-store. but that doesn't have a refresh or such |
| 39 |
// COMMENT: We lost the functionality that stuff is only loaded when it didnt' exist or changed |
| 40 |
|
| 41 |
/** |
| 42 |
* @deprecated |
| 43 |
*/ |
| 44 |
public class CertificateRevocationLists { |
| 45 |
|
| 46 |
static { |
| 47 |
new ProviderLoader(); |
| 48 |
} |
| 49 |
|
| 50 |
private static Log logger = |
| 51 |
LogFactory.getLog(CertificateRevocationLists.class.getName()); |
| 52 |
|
| 53 |
// the list of ca cert locations needed for getDefaultCRL call |
| 54 |
private static String prevCaCertLocations = null; |
| 55 |
// the default crl locations list derived from prevCaCertLocations |
| 56 |
private static String defaultCrlLocations = null; |
| 57 |
private static CertificateRevocationLists defaultCrl = null; |
| 58 |
|
| 59 |
private volatile Map<String, X509CRL> crlIssuerDNMap; |
| 60 |
|
| 61 |
private CertificateRevocationLists() {} |
| 62 |
|
| 63 |
public X509CRL[] getCrls() { |
| 64 |
if (this.crlIssuerDNMap == null) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
Collection crls = this.crlIssuerDNMap.values(); |
| 68 |
return (X509CRL[]) crls.toArray(new X509CRL[crls.size()]); |
| 69 |
} |
| 70 |
|
| 71 |
public Collection<X509CRL> getCRLs(X509CRLSelector selector) { |
| 72 |
Collection<X500Principal> issuers = selector.getIssuers(); |
| 73 |
int size = issuers.size(); |
| 74 |
Collection<X509CRL> retval = new ArrayList<X509CRL>(size); |
| 75 |
// Yup, this stinks. There's loss when we convert from principal to |
| 76 |
// string. Hence, depending on weird encoding effects, we may miss |
| 77 |
// some CRLs. |
| 78 |
Map<String, X509CRL> crlMap = this.crlIssuerDNMap; |
| 79 |
if (crlMap == null) return retval; |
| 80 |
for (X500Principal principal : issuers) { |
| 81 |
String dn = principal.getName(); |
| 82 |
X509CRL crl = crlMap.get(dn); |
| 83 |
if (crl != null) { |
| 84 |
retval.add(crl); |
| 85 |
} |
| 86 |
} |
| 87 |
return retval; |
| 88 |
} |
| 89 |
|
| 90 |
public X509CRL getCrl(String issuerName) { |
| 91 |
if (this.crlIssuerDNMap == null) { |
| 92 |
return null; |
| 93 |
} |
| 94 |
return (X509CRL)this.crlIssuerDNMap.get(issuerName); |
| 95 |
} |
| 96 |
|
| 97 |
public void refresh() { |
| 98 |
reload(null); |
| 99 |
} |
| 100 |
|
| 101 |
public synchronized void reload(String locations) { |
| 102 |
|
| 103 |
if (locations == null) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
StringTokenizer tokens = new StringTokenizer(locations, ","); |
| 108 |
Map<String, X509CRL> newCrlIssuerDNMap = new HashMap<String, X509CRL>(); |
| 109 |
|
| 110 |
while(tokens.hasMoreTokens()) { |
| 111 |
|
| 112 |
try { |
| 113 |
String location = tokens.nextToken().toString().trim(); |
| 114 |
CertStore tmp = Stores.getCRLStore("file:" + location + "/*.r*"); |
| 115 |
Collection<X509CRL> coll = (Collection<X509CRL>) tmp.getCRLs(new X509CRLSelector()); |
| 116 |
for (X509CRL crl : coll) { |
| 117 |
newCrlIssuerDNMap.put(crl.getIssuerX500Principal().getName(), crl); |
| 118 |
} |
| 119 |
} catch (Exception e) { |
| 120 |
throw new RuntimeException(e); |
| 121 |
} |
| 122 |
} |
| 123 |
this.crlIssuerDNMap = newCrlIssuerDNMap; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public static CertificateRevocationLists |
| 128 |
getCertificateRevocationLists(String locations) { |
| 129 |
CertificateRevocationLists crl = new CertificateRevocationLists(); |
| 130 |
crl.reload(locations); |
| 131 |
return crl; |
| 132 |
} |
| 133 |
|
| 134 |
public static synchronized |
| 135 |
CertificateRevocationLists getDefaultCertificateRevocationLists() { |
| 136 |
return getDefault(); |
| 137 |
} |
| 138 |
|
| 139 |
public static void |
| 140 |
setDefaultCertificateRevocationList(CertificateRevocationLists crl) { |
| 141 |
defaultCrl = crl; |
| 142 |
} |
| 143 |
|
| 144 |
public static synchronized CertificateRevocationLists getDefault() { |
| 145 |
if (defaultCrl == null) { |
| 146 |
defaultCrl = new DefaultCertificateRevocationLists(); |
| 147 |
} |
| 148 |
defaultCrl.refresh(); |
| 149 |
return defaultCrl; |
| 150 |
} |
| 151 |
|
| 152 |
public String toString() { |
| 153 |
if (this.crlIssuerDNMap == null) { |
| 154 |
return "crl list is empty"; |
| 155 |
} else { |
| 156 |
return this.crlIssuerDNMap.toString(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
private static class DefaultCertificateRevocationLists |
| 161 |
extends CertificateRevocationLists { |
| 162 |
|
| 163 |
private final long lifetime; |
| 164 |
private long lastRefresh; |
| 165 |
|
| 166 |
public DefaultCertificateRevocationLists() { |
| 167 |
lifetime = CoGProperties.getDefault().getCertCacheLifetime(); |
| 168 |
} |
| 169 |
|
| 170 |
public void refresh() { |
| 171 |
long now = System.currentTimeMillis(); |
| 172 |
if (lastRefresh + lifetime <= now) { |
| 173 |
reload(getDefaultCRLLocations()); |
| 174 |
lastRefresh = now; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
private static synchronized String getDefaultCRLLocations() { |
| 179 |
String caCertLocations = |
| 180 |
CoGProperties.getDefault().getCaCertLocations(); |
| 181 |
|
| 182 |
if (prevCaCertLocations == null || |
| 183 |
!prevCaCertLocations.equals(caCertLocations)) { |
| 184 |
|
| 185 |
if (caCertLocations == null) { |
| 186 |
logger.debug("No CA cert locations specified"); |
| 187 |
prevCaCertLocations = null; |
| 188 |
defaultCrlLocations = null; |
| 189 |
} else { |
| 190 |
StringTokenizer tokens = new StringTokenizer(caCertLocations, ","); |
| 191 |
File crlFile = null; |
| 192 |
LinkedList crlDirs = new LinkedList(); |
| 193 |
while(tokens.hasMoreTokens()) { |
| 194 |
String crlFileName = |
| 195 |
tokens.nextToken().toString().trim(); |
| 196 |
crlFile = new File(crlFileName); |
| 197 |
if (crlFile.isDirectory()) { |
| 198 |
// all all directories |
| 199 |
} else if (crlFile.isFile()) { |
| 200 |
// add parent directory |
| 201 |
crlFileName = crlFile.getParent(); |
| 202 |
} else { |
| 203 |
// skip other types |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
// don't add directories twice |
| 208 |
if (crlFileName != null && |
| 209 |
!crlDirs.contains(crlFileName)) { |
| 210 |
crlDirs.add(crlFileName); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
ListIterator iterator = crlDirs.listIterator(0); |
| 215 |
String locations = null; |
| 216 |
while (iterator.hasNext()) { |
| 217 |
if (locations == null) { |
| 218 |
locations = (String)iterator.next(); |
| 219 |
} else { |
| 220 |
locations = locations + "," |
| 221 |
+ (String)iterator.next(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// set defaults |
| 226 |
prevCaCertLocations = caCertLocations; |
| 227 |
defaultCrlLocations = locations; |
| 228 |
} |
| 229 |
} |
| 230 |
return defaultCrlLocations; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
} |