| 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.proxy; |
| 16 |
|
| 17 |
import org.globus.gsi.util.CertificateUtil; |
| 18 |
|
| 19 |
import org.globus.gsi.trustmanager.X509ProxyCertPathValidator; |
| 20 |
|
| 21 |
import org.globus.gsi.X509ProxyCertPathParameters; |
| 22 |
|
| 23 |
import org.globus.gsi.provider.simple.SimpleMemoryCertStoreParams; |
| 24 |
import org.globus.gsi.provider.simple.SimpleMemoryKeyStoreLoadStoreParameter; |
| 25 |
import org.globus.gsi.provider.simple.SimpleMemoryProvider; |
| 26 |
import org.globus.gsi.provider.simple.SimpleMemorySigningPolicyStore; |
| 27 |
|
| 28 |
import java.security.Security; |
| 29 |
import java.util.Map; |
| 30 |
import java.util.HashMap; |
| 31 |
import java.util.Hashtable; |
| 32 |
import java.security.KeyStore; |
| 33 |
import java.security.cert.CertStore; |
| 34 |
import java.security.cert.X509Certificate; |
| 35 |
import org.globus.gsi.TrustedCertificates; |
| 36 |
import org.globus.gsi.SigningPolicy; |
| 37 |
import org.globus.gsi.CertificateRevocationLists; |
| 38 |
import org.globus.gsi.bc.BouncyCastleUtil; |
| 39 |
import org.globus.util.I18n; |
| 40 |
import org.apache.commons.logging.Log; |
| 41 |
import org.apache.commons.logging.LogFactory; |
| 42 |
|
| 43 |
/** |
| 44 |
* Performs certificate/proxy path validation. It supports both old |
| 45 |
* style Globus proxy as well as the new proxy certificate format. It |
| 46 |
* checks BasicConstraints, KeyUsage, and ProxyCertInfo (if |
| 47 |
* applicable) extensions. It also checks for presence in CRLs and |
| 48 |
* signing policy compliance. This validator requires that each CA be |
| 49 |
* installed with signing policy. It also provides a callback interface |
| 50 |
* for custom policy checking of restricted proxies. <BR> Currently, |
| 51 |
* does <B>not</B> perform the following checks for the new proxy |
| 52 |
* certificates: <OL> <LI> Check if proxy serial number is unique (and |
| 53 |
* the version number) <LI> Check for empty subject names </OL> |
| 54 |
*/ |
| 55 |
|
| 56 |
public class ProxyPathValidator { |
| 57 |
|
| 58 |
static { |
| 59 |
Security.addProvider(new SimpleMemoryProvider()); |
| 60 |
} |
| 61 |
|
| 62 |
private static I18n i18n = I18n.getI18n("org.globus.gsi.proxy.errors", |
| 63 |
ProxyPathValidator.class.getClassLoader()); |
| 64 |
|
| 65 |
private static Log logger = |
| 66 |
LogFactory.getLog(ProxyPathValidator.class.getName()); |
| 67 |
|
| 68 |
private X509ProxyCertPathValidator validator = new X509ProxyCertPathValidator(); |
| 69 |
private boolean rejectLimitedProxyCheck = false; |
| 70 |
private boolean limited = false; |
| 71 |
private X509Certificate identityCert = null; |
| 72 |
private Hashtable proxyPolicyHandlers = null; |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns if the validated proxy path is limited. A proxy path |
| 76 |
* is limited when a limited proxy is present anywhere after the |
| 77 |
* first non-impersonation proxy certificate. |
| 78 |
* |
| 79 |
* @return true if the validated path is limited |
| 80 |
*/ |
| 81 |
public boolean isLimited() { |
| 82 |
return this.limited; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the identity certificate. The first certificates in the |
| 87 |
* path that is not an impersonation proxy, e.g. it could be a |
| 88 |
* restricted proxy or end-entity certificate |
| 89 |
* |
| 90 |
* @return <code>X509Certificate</code> the identity certificate |
| 91 |
*/ |
| 92 |
public X509Certificate getIdentityCertificate() { |
| 93 |
return this.identityCert; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns the subject name of the identity certificate (in the |
| 98 |
* Globus format) |
| 99 |
* @see #getIdentityCertificate |
| 100 |
* @return the subject name of the identity certificate in the |
| 101 |
* Globus format |
| 102 |
*/ |
| 103 |
public String getIdentity() { |
| 104 |
return BouncyCastleUtil.getIdentity(this.identityCert); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Removes a restricted proxy policy handler. |
| 109 |
* |
| 110 |
* @param id the Oid of the policy handler to remove. |
| 111 |
* @return <code>ProxyPolicyHandler</code> the removed handler, or |
| 112 |
* null if there is no handler registered under that |
| 113 |
* id. |
| 114 |
*/ |
| 115 |
public ProxyPolicyHandler removeProxyPolicyHandler(String id) { |
| 116 |
return (id != null && this.proxyPolicyHandlers != null) ? |
| 117 |
(ProxyPolicyHandler)this.proxyPolicyHandlers.remove(id) : |
| 118 |
null; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Sets a restricted proxy policy handler. |
| 123 |
* |
| 124 |
* @param id the Oid of the proxy policy to install the handler for. |
| 125 |
* @param handler the proxy policy handler. |
| 126 |
* @return <code>ProxyPolicyHandler</code> the previous handler |
| 127 |
* installed under the specified id. Usually, will be null. |
| 128 |
*/ |
| 129 |
public ProxyPolicyHandler setProxyPolicyHandler(String id, |
| 130 |
ProxyPolicyHandler handler) { |
| 131 |
if (id == null) { |
| 132 |
throw new IllegalArgumentException(i18n.getMessage("proxyPolicyId")); |
| 133 |
} |
| 134 |
if (handler == null) { |
| 135 |
throw new IllegalArgumentException(i18n. |
| 136 |
getMessage("proxyPolicyHandler")); |
| 137 |
} |
| 138 |
if (this.proxyPolicyHandlers == null) { |
| 139 |
this.proxyPolicyHandlers = new Hashtable(); |
| 140 |
} |
| 141 |
return (ProxyPolicyHandler)this.proxyPolicyHandlers.put(id, handler); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Retrieves a restricted proxy policy handler for a given policy id. |
| 146 |
* |
| 147 |
* @param id the Oid of the proxy policy to get the handler for. |
| 148 |
* @return <code>ProxyPolicyHandler</code> the policy handler |
| 149 |
* registered for the given id or null if none is |
| 150 |
* registered. |
| 151 |
*/ |
| 152 |
public ProxyPolicyHandler getProxyPolicyHandler(String id) { |
| 153 |
return (id != null && this.proxyPolicyHandlers != null) ? |
| 154 |
(ProxyPolicyHandler)this.proxyPolicyHandlers.get(id) : |
| 155 |
null; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Resets the internal state. Useful for reusing the same |
| 160 |
* instance for validating multiple certificate paths. |
| 161 |
*/ |
| 162 |
public void reset() { |
| 163 |
this.rejectLimitedProxyCheck= false; |
| 164 |
this.limited = false; |
| 165 |
this.identityCert = null; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* If set, the validate rejects certificate chain if limited proxy if found |
| 170 |
*/ |
| 171 |
public void setRejectLimitedProxyCheck(boolean rejectLimProxy) { |
| 172 |
this.rejectLimitedProxyCheck = rejectLimProxy; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Performs <B>all</B> certificate path validation including |
| 177 |
* checking of the signatures, validity of the certificates, |
| 178 |
* extension checking, etc.<BR> |
| 179 |
* It uses the PureTLS code to do basic cert signature checking |
| 180 |
* checking and then calls {@link #validate(X509Certificate[], |
| 181 |
* TrustedCertificates) validate} for further checks. |
| 182 |
* |
| 183 |
* @param certPath the certificate path to validate. |
| 184 |
* @param trustedCerts the trusted (CA) certificates. |
| 185 |
* @exception ProxyPathValidatorException if certificate |
| 186 |
* path validation fails. |
| 187 |
*/ |
| 188 |
public void validate(X509Certificate[] certPath, |
| 189 |
X509Certificate[] trustedCerts) |
| 190 |
throws ProxyPathValidatorException { |
| 191 |
validate(certPath, trustedCerts, null); |
| 192 |
} |
| 193 |
|
| 194 |
public void validate(X509Certificate[] certPath, |
| 195 |
X509Certificate[] trustedCerts, |
| 196 |
CertificateRevocationLists crls) |
| 197 |
throws ProxyPathValidatorException { |
| 198 |
validate(certPath, trustedCerts, crls, null); |
| 199 |
} |
| 200 |
|
| 201 |
public void validate(X509Certificate[] certPath, |
| 202 |
X509Certificate[] trustedCerts, |
| 203 |
CertificateRevocationLists crls, |
| 204 |
SigningPolicy[] signingPolicies) |
| 205 |
throws ProxyPathValidatorException { |
| 206 |
|
| 207 |
validate(certPath, trustedCerts, crls, signingPolicies, null); |
| 208 |
} |
| 209 |
|
| 210 |
public void validate(X509Certificate[] certPath, |
| 211 |
X509Certificate[] trustedCerts, |
| 212 |
CertificateRevocationLists crls, |
| 213 |
SigningPolicy[] signingPolicies, |
| 214 |
Boolean enforceSigningPolicy) |
| 215 |
throws ProxyPathValidatorException { |
| 216 |
|
| 217 |
if (certPath == null) { |
| 218 |
throw new IllegalArgumentException(i18n.getMessage("certsNull")); |
| 219 |
} |
| 220 |
|
| 221 |
// If trusted certificates is not null, but signing policy is, |
| 222 |
// then this might fail down the line. |
| 223 |
TrustedCertificates trustedCertificates = null; |
| 224 |
if (trustedCerts != null) { |
| 225 |
trustedCertificates = new TrustedCertificates(trustedCerts, |
| 226 |
signingPolicies); |
| 227 |
} |
| 228 |
|
| 229 |
validate(certPath, trustedCertificates, crls, enforceSigningPolicy); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Performs certificate path validation. Does <B>not</B> check |
| 234 |
* the cert signatures but it performs all other checks like |
| 235 |
* the extension checking, validity checking, restricted policy |
| 236 |
* checking, CRL checking, etc. |
| 237 |
* |
| 238 |
* @param certPath the certificate path to validate. |
| 239 |
* @exception ProxyPathValidatorException if certificate |
| 240 |
* path validation fails. |
| 241 |
*/ |
| 242 |
protected void validate(X509Certificate [] certPath) |
| 243 |
throws ProxyPathValidatorException { |
| 244 |
validate(certPath, |
| 245 |
(TrustedCertificates)null, |
| 246 |
(CertificateRevocationLists)null); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Performs certificate path validation. Does <B>not</B> check |
| 251 |
* the cert signatures but it performs all other checks like |
| 252 |
* the extension checking, validity checking, restricted policy |
| 253 |
* checking, CRL checking, etc. |
| 254 |
* |
| 255 |
* @param certPath the certificate path to validate. |
| 256 |
* @param trustedCerts the trusted (CA) certificates. If null, |
| 257 |
* the default trusted certificates will be used. |
| 258 |
* @exception ProxyPathValidatorException if certificate |
| 259 |
* path validation fails. |
| 260 |
*/ |
| 261 |
protected void validate(X509Certificate [] certPath, |
| 262 |
TrustedCertificates trustedCerts) |
| 263 |
throws ProxyPathValidatorException { |
| 264 |
validate(certPath, trustedCerts, null); |
| 265 |
} |
| 266 |
|
| 267 |
protected void validate(X509Certificate [] certPath, |
| 268 |
TrustedCertificates trustedCerts, |
| 269 |
CertificateRevocationLists crlsList) |
| 270 |
throws ProxyPathValidatorException { |
| 271 |
|
| 272 |
validate(certPath, trustedCerts, crlsList, null); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Performs certificate path validation. Does <B>not</B> check |
| 277 |
* the cert signatures but it performs all other checks like |
| 278 |
* the extension checking, validity checking, restricted policy |
| 279 |
* checking, CRL checking, etc. |
| 280 |
* |
| 281 |
* @param certPath the certificate path to validate. |
| 282 |
* @param trustedCerts the trusted (CA) certificates. If null, |
| 283 |
* the default trusted certificates will be used. |
| 284 |
* @param crlsList the certificate revocation list. If null, |
| 285 |
* the default certificate revocation list will be used. |
| 286 |
* @exception ProxyPathValidatorException if certificate |
| 287 |
* path validation fails. |
| 288 |
*/ |
| 289 |
protected synchronized void validate(X509Certificate [] certPath, |
| 290 |
TrustedCertificates trustedCerts, |
| 291 |
CertificateRevocationLists crlsList, |
| 292 |
Boolean enforceSigningPolicy) |
| 293 |
throws ProxyPathValidatorException { |
| 294 |
|
| 295 |
if (certPath == null) { |
| 296 |
throw new IllegalArgumentException(i18n.getMessage("certsNull")); |
| 297 |
} |
| 298 |
|
| 299 |
if (crlsList == null) { |
| 300 |
crlsList = CertificateRevocationLists.getDefaultCertificateRevocationLists(); |
| 301 |
} |
| 302 |
|
| 303 |
if (trustedCerts == null) { |
| 304 |
trustedCerts = TrustedCertificates.getDefault(); |
| 305 |
} |
| 306 |
|
| 307 |
try { |
| 308 |
SimpleMemoryKeyStoreLoadStoreParameter ksParams = new SimpleMemoryKeyStoreLoadStoreParameter(); |
| 309 |
SimpleMemoryCertStoreParams csParams = new SimpleMemoryCertStoreParams(null, crlsList.getCrls()); |
| 310 |
ksParams.setCerts(trustedCerts.getCertificates()); |
| 311 |
Map<String,ProxyPolicyHandler> initHandlers = new HashMap<String,ProxyPolicyHandler>(); |
| 312 |
if (this.proxyPolicyHandlers != null) { |
| 313 |
initHandlers.putAll(proxyPolicyHandlers); |
| 314 |
} |
| 315 |
KeyStore ks = KeyStore.getInstance(SimpleMemoryProvider.KEYSTORE_TYPE, SimpleMemoryProvider.PROVIDER_NAME); |
| 316 |
CertStore cs = CertStore.getInstance(SimpleMemoryProvider.CERTSTORE_TYPE, csParams, SimpleMemoryProvider.PROVIDER_NAME); |
| 317 |
SimpleMemorySigningPolicyStore spStore = new SimpleMemorySigningPolicyStore(trustedCerts.getSigningPolicies()); |
| 318 |
ks.load(ksParams); |
| 319 |
X509ProxyCertPathParameters params = new X509ProxyCertPathParameters(ks, cs, |
| 320 |
spStore, this.rejectLimitedProxyCheck, initHandlers); |
| 321 |
validator.engineValidate(CertificateUtil.getCertPath(certPath), params); |
| 322 |
this.identityCert = validator.getIdentityCertificate(); |
| 323 |
this.limited = validator.isLimited(); |
| 324 |
} catch (Exception e) { |
| 325 |
throw new ProxyPathValidatorException( |
| 326 |
ProxyPathValidatorException.FAILURE, |
| 327 |
e); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
protected synchronized void setValidator(X509ProxyCertPathValidator validator) { |
| 332 |
this.validator = validator; |
| 333 |
} |
| 334 |
|
| 335 |
} |