| 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.apache.commons.logging.Log; |
| 18 |
import org.apache.commons.logging.LogFactory; |
| 19 |
import org.globus.gsi.util.CertificateUtil; |
| 20 |
|
| 21 |
import org.globus.gsi.trustmanager.X509ProxyCertPathValidator; |
| 22 |
|
| 23 |
import org.globus.gsi.stores.ResourceCertStoreParameters; |
| 24 |
import org.globus.gsi.stores.ResourceSigningPolicyStore; |
| 25 |
import org.globus.gsi.stores.ResourceSigningPolicyStoreParameters; |
| 26 |
|
| 27 |
import org.globus.gsi.provider.GlobusProvider; |
| 28 |
import org.globus.gsi.provider.KeyStoreParametersFactory; |
| 29 |
|
| 30 |
import java.io.File; |
| 31 |
import java.security.cert.CertStore; |
| 32 |
import java.security.KeyStore; |
| 33 |
import java.security.PrivateKey; |
| 34 |
import java.security.cert.X509Certificate; |
| 35 |
import java.security.cert.CertificateEncodingException; |
| 36 |
import java.io.IOException; |
| 37 |
import java.io.InputStream; |
| 38 |
import java.io.OutputStream; |
| 39 |
import java.io.Serializable; |
| 40 |
import org.globus.common.ChainedIOException; |
| 41 |
import org.globus.common.CoGProperties; |
| 42 |
import org.globus.gsi.bc.BouncyCastleUtil; |
| 43 |
|
| 44 |
/** |
| 45 |
* Provides a Java object representation of Globus credential which can include the proxy file or host |
| 46 |
* certificates. |
| 47 |
* @deprecated |
| 48 |
*/ |
| 49 |
public class GlobusCredential implements Serializable { |
| 50 |
|
| 51 |
private Log logger = LogFactory.getLog(getClass()); |
| 52 |
private X509Credential cred; |
| 53 |
private static GlobusCredential defaultCred; |
| 54 |
private static transient long credentialLastModified = -1; |
| 55 |
// indicates if default credential was explicitely set |
| 56 |
// and if so - if the credential expired it try |
| 57 |
// to load the proxy from a file. |
| 58 |
private static transient boolean credentialSet = false; |
| 59 |
private static transient File credentialFile = null; |
| 60 |
|
| 61 |
static { |
| 62 |
new ProviderLoader(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Creates a GlobusCredential from a private key and a certificate chain. |
| 67 |
* |
| 68 |
* @param key |
| 69 |
* the private key |
| 70 |
* @param certs |
| 71 |
* the certificate chain |
| 72 |
*/ |
| 73 |
public GlobusCredential(PrivateKey key, X509Certificate[] certs) { |
| 74 |
cred = new X509Credential(key, certs); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Creates a GlobusCredential from a proxy file. |
| 79 |
* |
| 80 |
* @param proxyFile |
| 81 |
* the file to load the credential from. |
| 82 |
* @exception GlobusCredentialException |
| 83 |
* if the credential failed to load. |
| 84 |
*/ |
| 85 |
public GlobusCredential(String proxyFile) throws GlobusCredentialException { |
| 86 |
try { |
| 87 |
cred = new X509Credential(proxyFile); |
| 88 |
} catch (Exception e) { |
| 89 |
throw new GlobusCredentialException(GlobusCredentialException.FAILURE, e.getMessage(), e); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Creates a GlobusCredential from certificate file and a unencrypted key file. |
| 95 |
* |
| 96 |
* @param certFile |
| 97 |
* the file containing the certificate |
| 98 |
* @param unencryptedKeyFile |
| 99 |
* the file containing the private key. The key must be unencrypted. |
| 100 |
* @exception GlobusCredentialException |
| 101 |
* if something goes wrong. |
| 102 |
*/ |
| 103 |
public GlobusCredential(String certFile, String unencryptedKeyFile) throws GlobusCredentialException { |
| 104 |
|
| 105 |
if (certFile == null || unencryptedKeyFile == null) { |
| 106 |
throw new IllegalArgumentException(); |
| 107 |
} |
| 108 |
|
| 109 |
try { |
| 110 |
cred = new X509Credential(certFile, unencryptedKeyFile); |
| 111 |
} catch (Exception e) { |
| 112 |
throw new GlobusCredentialException(GlobusCredentialException.FAILURE, e.getMessage(), e); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Creates a GlobusCredential from an input stream. |
| 118 |
* |
| 119 |
* @param input |
| 120 |
* the stream to load the credential from. |
| 121 |
* @exception GlobusCredentialException |
| 122 |
* if the credential failed to load. |
| 123 |
*/ |
| 124 |
public GlobusCredential(InputStream input) throws GlobusCredentialException { |
| 125 |
|
| 126 |
try { |
| 127 |
cred = new X509Credential(input); |
| 128 |
} catch (Exception e) { |
| 129 |
throw new GlobusCredentialException(GlobusCredentialException.FAILURE, e.getMessage(), e); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Saves the credential into a specified output stream. The self-signed certificates in the certificate |
| 135 |
* chain will not be saved. The output stream should always be closed after calling this function. |
| 136 |
* |
| 137 |
* @param out |
| 138 |
* the output stream to write the credential to. |
| 139 |
* @exception IOException |
| 140 |
* if any error occurred during saving. |
| 141 |
*/ |
| 142 |
public void save(OutputStream out) throws IOException { |
| 143 |
|
| 144 |
try { |
| 145 |
cred.save(out); |
| 146 |
} catch (CertificateEncodingException e) { |
| 147 |
throw new ChainedIOException(e.getMessage(), e); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Verifies the validity of the credentials. All certificate path validation is performed using trusted |
| 153 |
* certificates in default locations. |
| 154 |
* |
| 155 |
* @exception GlobusCredentialException |
| 156 |
* if one of the certificates in the chain expired or if path validiation fails. |
| 157 |
*/ |
| 158 |
public void verify() throws GlobusCredentialException { |
| 159 |
try { |
| 160 |
String caCertsLocation = "file:" + CoGProperties.getDefault().getCaCertLocations(); |
| 161 |
String crlPattern = caCertsLocation + "/*.r*"; |
| 162 |
String sigPolPattern = caCertsLocation + "/*.signing_policy"; |
| 163 |
KeyStore keyStore = KeyStore.getInstance(GlobusProvider.KEYSTORE_TYPE, GlobusProvider.PROVIDER_NAME); |
| 164 |
CertStore crlStore = CertStore.getInstance(GlobusProvider.CERTSTORE_TYPE, new ResourceCertStoreParameters(null,crlPattern)); |
| 165 |
ResourceSigningPolicyStore sigPolStore = new ResourceSigningPolicyStore(new ResourceSigningPolicyStoreParameters(sigPolPattern)); |
| 166 |
keyStore.load(KeyStoreParametersFactory.createTrustStoreParameters(caCertsLocation)); |
| 167 |
X509ProxyCertPathParameters parameters = new X509ProxyCertPathParameters(keyStore, crlStore, sigPolStore, false); |
| 168 |
X509ProxyCertPathValidator validator = new X509ProxyCertPathValidator(); |
| 169 |
validator.engineValidate(CertificateUtil.getCertPath(this.cred.getCertificateChain()), parameters); |
| 170 |
} catch (Exception e) { |
| 171 |
e.printStackTrace(); |
| 172 |
throw new GlobusCredentialException(GlobusCredentialException.FAILURE, e.getMessage(), e); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns the identity certificate of this credential. The identity certificate is the first certificate |
| 178 |
* in the chain that is not an impersonation proxy certificate. |
| 179 |
* |
| 180 |
* @return <code>X509Certificate</code> the identity cert. Null, if unable to get the identity certificate |
| 181 |
* (an error occurred) |
| 182 |
*/ |
| 183 |
public X509Certificate getIdentityCertificate() { |
| 184 |
return cred.getIdentityCertificate(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the path length constraint. The shortest length in the chain of certificates is returned as the |
| 189 |
* credential's path length. |
| 190 |
* |
| 191 |
* @return The path length constraint of the credential. -1 is any error occurs. |
| 192 |
*/ |
| 193 |
public int getPathConstraint() { |
| 194 |
return cred.getPathConstraint(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Returns the identity of this credential. |
| 199 |
* |
| 200 |
* @see #getIdentityCertificate() |
| 201 |
* |
| 202 |
* @return The identity cert in Globus format (e.g. /C=US/..). Null, if unable to get the identity (an |
| 203 |
* error occurred) |
| 204 |
*/ |
| 205 |
public String getIdentity() { |
| 206 |
return cred.getIdentity(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns the private key of this credential. |
| 211 |
* |
| 212 |
* @return <code>PrivateKey</code> the private key |
| 213 |
*/ |
| 214 |
public PrivateKey getPrivateKey() { |
| 215 |
try { |
| 216 |
return (PrivateKey) cred.getPrivateKey(); |
| 217 |
} catch (Exception e) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns the certificate chain of this credential. |
| 224 |
* |
| 225 |
* @return <code>X509Certificate []</code> the certificate chain |
| 226 |
*/ |
| 227 |
public X509Certificate[] getCertificateChain() { |
| 228 |
return cred.getCertificateChain(); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns the number of certificates in the credential without the self-signed certificates. |
| 233 |
* |
| 234 |
* @return number of certificates without counting self-signed certificates |
| 235 |
*/ |
| 236 |
public int getCertNum() { |
| 237 |
return cred.getCertNum(); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Returns strength of the private/public key in bits. |
| 242 |
* |
| 243 |
* @return strength of the key in bits. Returns -1 if unable to determine it. |
| 244 |
*/ |
| 245 |
public int getStrength() { |
| 246 |
try { |
| 247 |
return cred.getStrength(); |
| 248 |
} catch (Exception e) { |
| 249 |
return -1; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns the subject DN of the first certificate in the chain. |
| 255 |
* |
| 256 |
* @return subject DN. |
| 257 |
*/ |
| 258 |
public String getSubject() { |
| 259 |
return cred.getSubject(); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Returns the issuer DN of the first certificate in the chain. |
| 264 |
* |
| 265 |
* @return issuer DN. |
| 266 |
*/ |
| 267 |
public String getIssuer() { |
| 268 |
return cred.getIssuer(); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Returns the certificate type of the first certificate in the chain. Returns -1 if unable to determine |
| 273 |
* the certificate type (an error occurred) |
| 274 |
* |
| 275 |
* @see BouncyCastleUtil#getCertificateType(X509Certificate) |
| 276 |
* |
| 277 |
* @return the type of first certificate in the chain. -1 if unable to determine the certificate type. |
| 278 |
*/ |
| 279 |
public int getProxyType() { |
| 280 |
return cred.getProxyType().getCode(); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Returns time left of this credential. The time left of the credential is based on the certificate with |
| 285 |
* the shortest validity time. |
| 286 |
* |
| 287 |
* @return time left in seconds. Returns 0 if the certificate has expired. |
| 288 |
*/ |
| 289 |
public long getTimeLeft() { |
| 290 |
return cred.getTimeLeft(); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Returns the default credential. The default credential is usually the user proxy certificate. <BR> |
| 295 |
* The credential will be loaded on the initial call. It must not be expired. All subsequent calls to this |
| 296 |
* function return cached credential object. Once the credential is cached, and the underlying file |
| 297 |
* changes, the credential will be reloaded. |
| 298 |
* |
| 299 |
* @return the default credential. |
| 300 |
* @exception GlobusCredentialException |
| 301 |
* if the credential expired or some other error with the credential. |
| 302 |
*/ |
| 303 |
public synchronized static GlobusCredential getDefaultCredential() throws GlobusCredentialException { |
| 304 |
if (defaultCred == null) { |
| 305 |
reloadDefaultCredential(); |
| 306 |
} else if (!credentialSet) { |
| 307 |
if (credentialFile.lastModified() == credentialLastModified) { |
| 308 |
defaultCred.verify(); |
| 309 |
} else { |
| 310 |
defaultCred = null; |
| 311 |
reloadDefaultCredential(); |
| 312 |
} |
| 313 |
} |
| 314 |
return defaultCred; |
| 315 |
} |
| 316 |
|
| 317 |
private static void reloadDefaultCredential() |
| 318 |
throws GlobusCredentialException { |
| 319 |
String proxyLocation = CoGProperties.getDefault().getProxyFile(); |
| 320 |
defaultCred = new GlobusCredential(proxyLocation); |
| 321 |
credentialFile = new File(proxyLocation); |
| 322 |
credentialLastModified = credentialFile.lastModified(); |
| 323 |
defaultCred.verify(); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Sets default credential. |
| 328 |
* |
| 329 |
* @param cred |
| 330 |
* the credential to set a default. |
| 331 |
*/ |
| 332 |
public synchronized static void setDefaultCredential(GlobusCredential cred) { |
| 333 |
credentialSet = (cred != null); |
| 334 |
} |
| 335 |
|
| 336 |
public String toString() { |
| 337 |
return cred.toString(); |
| 338 |
} |
| 339 |
|
| 340 |
} |