| 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.provider; |
| 16 |
|
| 17 |
import static org.junit.Assert.assertEquals; |
| 18 |
import static org.junit.Assert.assertFalse; |
| 19 |
import static org.junit.Assert.assertNotNull; |
| 20 |
import static org.junit.Assert.assertNull; |
| 21 |
import static org.junit.Assert.assertTrue; |
| 22 |
import static org.junit.Assert.fail; |
| 23 |
|
| 24 |
import org.apache.commons.io.FileUtils; |
| 25 |
import org.globus.gsi.testutils.DirSetupUtil; |
| 26 |
import org.globus.gsi.testutils.FileSetupUtil; |
| 27 |
import org.globus.gsi.util.CertificateLoadUtil; |
| 28 |
|
| 29 |
import org.globus.gsi.stores.PEMKeyStore; |
| 30 |
//import org.globus.gsi.stores.PEMKeyStoreParameters; |
| 31 |
|
| 32 |
import org.globus.gsi.X509Credential; |
| 33 |
|
| 34 |
import org.globus.gsi.provider.GlobusProvider; |
| 35 |
import org.globus.gsi.provider.KeyStoreParametersFactory; |
| 36 |
|
| 37 |
import java.io.ByteArrayInputStream; |
| 38 |
import java.io.ByteArrayOutputStream; |
| 39 |
import java.io.File; |
| 40 |
import java.io.FileInputStream; |
| 41 |
import java.io.InputStream; |
| 42 |
import java.security.Key; |
| 43 |
import java.security.KeyStore; |
| 44 |
import java.security.KeyStoreException; |
| 45 |
import java.security.PrivateKey; |
| 46 |
import java.security.Security; |
| 47 |
import java.security.UnrecoverableKeyException; |
| 48 |
import java.security.KeyStore.LoadStoreParameter; |
| 49 |
import java.security.cert.Certificate; |
| 50 |
import java.security.cert.X509Certificate; |
| 51 |
import java.util.Enumeration; |
| 52 |
import java.util.HashMap; |
| 53 |
import java.util.Iterator; |
| 54 |
import java.util.Map; |
| 55 |
import java.util.Properties; |
| 56 |
import java.util.Vector; |
| 57 |
|
| 58 |
import org.globus.util.GlobusResource; |
| 59 |
import org.junit.After; |
| 60 |
import org.junit.Before; |
| 61 |
import org.junit.Test; |
| 62 |
|
| 63 |
/** |
| 64 |
* FILL ME |
| 65 |
* |
| 66 |
* @author [email protected] |
| 67 |
*/ |
| 68 |
public class TestPEMFileBasedKeyStore { |
| 69 |
|
| 70 |
DirSetupUtil trustedDirectory; |
| 71 |
DirSetupUtil defaultTrustedDirectory; |
| 72 |
Vector<X509Certificate> testTrustedCertificates = new Vector<X509Certificate>(); |
| 73 |
FileSetupUtil proxyFile1; |
| 74 |
FileSetupUtil proxyFile2; |
| 75 |
FileSetupUtil certFile; |
| 76 |
FileSetupUtil keyFile; |
| 77 |
FileSetupUtil keyEncFile; |
| 78 |
|
| 79 |
Map<FileSetupUtil, X509Certificate> trustedCertificates = new HashMap<FileSetupUtil, X509Certificate>(); |
| 80 |
Map<FileSetupUtil, X509Credential> proxyCertificates = new HashMap<FileSetupUtil, X509Credential>(); |
| 81 |
|
| 82 |
@Before |
| 83 |
public void setUp() throws Exception { |
| 84 |
|
| 85 |
ClassLoader loader = TestPEMFileBasedKeyStore.class.getClassLoader(); |
| 86 |
|
| 87 |
String[] trustedCertFilenames = new String[]{"testTrustStore/1c3f2ca8.0", "testTrustStore/b38b4d8c.0"}; |
| 88 |
this.trustedDirectory = new DirSetupUtil(trustedCertFilenames); |
| 89 |
this.trustedDirectory.createTempDirectory(); |
| 90 |
this.trustedDirectory.copy(); |
| 91 |
for (String trustedCertFilename : trustedCertFilenames) { |
| 92 |
InputStream in = null; |
| 93 |
try { |
| 94 |
in = loader.getResourceAsStream(trustedCertFilename); |
| 95 |
if (in == null) { |
| 96 |
throw new Exception("Unable to load: " + trustedCertFilename); |
| 97 |
} |
| 98 |
this.trustedCertificates.put(this.trustedDirectory.getFileSetupUtil(trustedCertFilename), |
| 99 |
CertificateLoadUtil.loadCertificate(in)); |
| 100 |
} finally { |
| 101 |
if (in != null) { |
| 102 |
in.close(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
String[] defaultTrustedCert = new String[]{"testTrustStore/d1b603c3.0"}; |
| 108 |
this.defaultTrustedDirectory = new DirSetupUtil(defaultTrustedCert); |
| 109 |
this.defaultTrustedDirectory.createTempDirectory(); |
| 110 |
this.defaultTrustedDirectory.copy(); |
| 111 |
for (String aDefaultTrustedCert : defaultTrustedCert) { |
| 112 |
InputStream in = null; |
| 113 |
try { |
| 114 |
in = loader.getResourceAsStream(aDefaultTrustedCert); |
| 115 |
if (in == null) { |
| 116 |
throw new Exception("Unable to load: " + aDefaultTrustedCert); |
| 117 |
} |
| 118 |
this.trustedCertificates.put( |
| 119 |
this.defaultTrustedDirectory.getFileSetupUtil(aDefaultTrustedCert), |
| 120 |
CertificateLoadUtil.loadCertificate(in)); |
| 121 |
} finally { |
| 122 |
if (in != null) { |
| 123 |
in.close(); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
// String proxyFilename1 = "validatorTest/gsi2fullproxy.pem"; |
| 130 |
String proxyFilename1 = "validatorTest/gsi3independentFromLimitedProxy.pem"; |
| 131 |
this.proxyFile1 = new FileSetupUtil(proxyFilename1); |
| 132 |
this.proxyFile1.copyFileToTemp(); |
| 133 |
this.proxyCertificates.put(this.proxyFile1, |
| 134 |
new X509Credential(loader.getResourceAsStream(proxyFilename1), loader.getResourceAsStream(proxyFilename1))); |
| 135 |
|
| 136 |
String proxyFilename2 = "validatorTest/gsi3FromPathOneProxy.pem"; |
| 137 |
this.proxyFile2 = new FileSetupUtil(proxyFilename2); |
| 138 |
this.proxyFile2.copyFileToTemp(); |
| 139 |
this.proxyCertificates.put(this.proxyFile2, |
| 140 |
new X509Credential(loader.getResourceAsStream(proxyFilename2), |
| 141 |
loader.getResourceAsStream(proxyFilename2))); |
| 142 |
|
| 143 |
String certFilename = "validatorTest/testeec2.pem"; |
| 144 |
this.certFile = new FileSetupUtil(certFilename); |
| 145 |
this.certFile.copyFileToTemp(); |
| 146 |
String keyFilename = "validatorTest/testeec2-private.pem"; |
| 147 |
this.keyFile = new FileSetupUtil(keyFilename); |
| 148 |
this.keyFile.copyFileToTemp(); |
| 149 |
String keyEncFilename = "validatorTest/testeec2-private-enc.pem"; |
| 150 |
this.keyEncFile = new FileSetupUtil(keyEncFilename); |
| 151 |
this.keyEncFile.copyFileToTemp(); |
| 152 |
|
| 153 |
|
| 154 |
Security.addProvider(new GlobusProvider()); |
| 155 |
} |
| 156 |
|
| 157 |
@Test |
| 158 |
public void testCreationDate() throws Exception { |
| 159 |
KeyStore store = KeyStore.getInstance("PEMFilebasedKeyStore", "Globus"); |
| 160 |
|
| 161 |
// Parameters in properties file |
| 162 |
Properties properties = new Properties(); |
| 163 |
properties.setProperty(PEMKeyStore.DEFAULT_DIRECTORY_KEY, |
| 164 |
"file:"+ this.defaultTrustedDirectory.getTempDirectoryName()); |
| 165 |
properties.setProperty(PEMKeyStore.DIRECTORY_LIST_KEY, |
| 166 |
"file:" + this.trustedDirectory.getTempDirectoryName() + "/*.0"); |
| 167 |
|
| 168 |
InputStream ins = null; |
| 169 |
try { |
| 170 |
ins = getProperties(properties); |
| 171 |
store.load(ins, null); |
| 172 |
} finally { |
| 173 |
if (ins != null) { |
| 174 |
ins.close(); |
| 175 |
} |
| 176 |
} |
| 177 |
Enumeration<String> aliases = store.aliases(); |
| 178 |
if (aliases.hasMoreElements()) { |
| 179 |
String alias = aliases.nextElement(); |
| 180 |
assertNotNull(store.getCreationDate(alias)); |
| 181 |
} |
| 182 |
assertNull(store.getCreationDate("FakeAlias")); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void testTrustedCerts() throws Exception { |
| 188 |
|
| 189 |
PEMKeyStore store = new PEMKeyStore(); |
| 190 |
|
| 191 |
// Parameters in properties file |
| 192 |
Properties properties = new Properties(); |
| 193 |
properties.setProperty(PEMKeyStore.DEFAULT_DIRECTORY_KEY, |
| 194 |
"file:" + this.defaultTrustedDirectory.getTempDirectoryName()); |
| 195 |
properties.setProperty(PEMKeyStore.DIRECTORY_LIST_KEY, |
| 196 |
"file:" + this.trustedDirectory.getTempDirectoryName() + "/*.0"); |
| 197 |
|
| 198 |
InputStream ins = null; |
| 199 |
try { |
| 200 |
ins = getProperties(properties); |
| 201 |
store.engineLoad(ins, null); |
| 202 |
} finally { |
| 203 |
if (ins != null) { |
| 204 |
ins.close(); |
| 205 |
} |
| 206 |
} |
| 207 |
testLoadedStore(store); |
| 208 |
Iterator<FileSetupUtil> iterator = this.trustedCertificates.keySet().iterator(); |
| 209 |
FileSetupUtil util = iterator.next(); |
| 210 |
testDelete(store, util.getTempFilename(), util); |
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void testParameterLoad() throws Exception { |
| 215 |
PEMKeyStore keystore = loadFromParameters(); |
| 216 |
testLoadedStore(keystore); |
| 217 |
} |
| 218 |
|
| 219 |
private PEMKeyStore loadFromParameters() throws Exception { |
| 220 |
LoadStoreParameter params = KeyStoreParametersFactory.createTrustStoreParameters( |
| 221 |
"file:" + this.trustedDirectory.getTempDirectoryName(), |
| 222 |
"file:" + this.defaultTrustedDirectory.getTempDirectoryName() |
| 223 |
); |
| 224 |
PEMKeyStore keystore = new PEMKeyStore(); |
| 225 |
keystore.engineLoad(params); |
| 226 |
return keystore; |
| 227 |
} |
| 228 |
|
| 229 |
private void testLoadedStore(PEMKeyStore store) throws KeyStoreException { |
| 230 |
Enumeration aliases = store.engineAliases(); |
| 231 |
assertTrue(aliases.hasMoreElements()); |
| 232 |
|
| 233 |
// alias to certificate test to be added. |
| 234 |
Iterator<FileSetupUtil> iterator = this.trustedCertificates.keySet().iterator(); |
| 235 |
String alias; |
| 236 |
FileSetupUtil util; |
| 237 |
while (iterator.hasNext()) { |
| 238 |
util = iterator.next(); |
| 239 |
alias = util.getTempFile().toURI().toASCIIString(); |
| 240 |
assertTrue(store.engineIsCertificateEntry(alias)); |
| 241 |
Certificate certificate = store.engineGetCertificate(alias); |
| 242 |
assertNotNull(certificate); |
| 243 |
assertEquals(certificate, this.trustedCertificates.get(util)); |
| 244 |
String storeAlias = store.engineGetCertificateAlias(certificate); |
| 245 |
assertEquals(alias, storeAlias); |
| 246 |
} |
| 247 |
assertFalse(store.engineIsCertificateEntry("FakeCert")); |
| 248 |
} |
| 249 |
|
| 250 |
private void testDelete(PEMKeyStore store, String alias, FileSetupUtil util) throws Exception { |
| 251 |
// test delete |
| 252 |
store.engineDeleteEntry(alias); |
| 253 |
|
| 254 |
assertNull(store.engineGetCertificate(alias)); |
| 255 |
assertNotNull(util); |
| 256 |
File tempFile = util.getTempFile(); |
| 257 |
assertNotNull(tempFile); |
| 258 |
} |
| 259 |
|
| 260 |
@Test |
| 261 |
public void testProxyCerts() throws Exception { |
| 262 |
|
| 263 |
PEMKeyStore store = new PEMKeyStore(); |
| 264 |
// Parameters in properties file |
| 265 |
Properties properties = new Properties(); |
| 266 |
properties.setProperty(PEMKeyStore.PROXY_FILENAME, |
| 267 |
"file:"+ this.proxyFile1.getAbsoluteFilename()); |
| 268 |
|
| 269 |
InputStream ins = null; |
| 270 |
try { |
| 271 |
ins = getProperties(properties); |
| 272 |
store.engineLoad(ins, null); |
| 273 |
} finally { |
| 274 |
if (ins != null) { |
| 275 |
ins.close(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
Enumeration aliases = store.engineAliases(); |
| 280 |
assert (aliases.hasMoreElements()); |
| 281 |
// proxy file 1 |
| 282 |
String proxyId1 = new GlobusResource(this.proxyFile1.getTempFile().getAbsolutePath()).getFile().toString();//getURL().toExternalForm(); |
| 283 |
Key key = store.engineGetKey("file:"+ this.proxyFile1.getAbsoluteFilename(), null); |
| 284 |
|
| 285 |
assertTrue(store.engineIsKeyEntry("file:"+ this.proxyFile1.getAbsoluteFilename())); |
| 286 |
assertNotNull(key != null); |
| 287 |
assertTrue(key instanceof PrivateKey); |
| 288 |
Certificate[] certificates = store.engineGetCertificateChain(this.proxyFile1.getURL().toExternalForm()); |
| 289 |
assertNotNull(certificates != null); |
| 290 |
assertTrue(certificates instanceof X509Certificate[]); |
| 291 |
key = null; |
| 292 |
|
| 293 |
// assert (this.proxyCertificates.get(this.proxyFile1.getAbsoluteFilename()).equals(certificates[0])); |
| 294 |
|
| 295 |
properties.setProperty(PEMKeyStore.PROXY_FILENAME, |
| 296 |
"file:" + this.proxyFile2.getAbsoluteFilename()); |
| 297 |
ins = null; |
| 298 |
try { |
| 299 |
ins = getProperties(properties); |
| 300 |
store.engineLoad(ins, null); |
| 301 |
} finally { |
| 302 |
if (ins != null) { |
| 303 |
ins.close(); |
| 304 |
} |
| 305 |
} |
| 306 |
// proxy file 2 |
| 307 |
String proxyId2 = new GlobusResource(this.proxyFile2.getTempFile().getAbsolutePath()).getURL().toExternalForm(); |
| 308 |
key = store.engineGetKey("file:" + this.proxyFile2.getAbsoluteFilename(), null); |
| 309 |
assertTrue(store.engineIsKeyEntry("file:" + this.proxyFile2.getAbsoluteFilename())); |
| 310 |
assertNotNull(key); |
| 311 |
assertTrue(key instanceof PrivateKey); |
| 312 |
|
| 313 |
certificates = store.engineGetCertificateChain(proxyId1); |
| 314 |
assertNotNull(certificates != null); |
| 315 |
assertTrue(certificates instanceof X509Certificate[]); |
| 316 |
|
| 317 |
// assert (this.proxyCertificates.get(this.proxyFile2.getTempFilename()).equals(certificates[0])); |
| 318 |
|
| 319 |
|
| 320 |
// test delete |
| 321 |
store.engineDeleteEntry(proxyId1); |
| 322 |
|
| 323 |
certificates = store.engineGetCertificateChain(proxyId1); |
| 324 |
assertEquals(0, certificates.length); |
| 325 |
assertFalse((new File("file:"+ this.proxyFile1.getAbsoluteFilename())).exists()); |
| 326 |
assertFalse(store.engineIsKeyEntry(proxyId1)); |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
@Test |
| 331 |
public void testUserCerts() throws Exception { |
| 332 |
PEMKeyStore store = new PEMKeyStore(); |
| 333 |
// Parameters in properties file |
| 334 |
Properties properties = new Properties(); |
| 335 |
properties.setProperty(PEMKeyStore.CERTIFICATE_FILENAME, new GlobusResource( |
| 336 |
this.certFile.getTempFile().getAbsolutePath()).getURL().toExternalForm()); |
| 337 |
properties.setProperty(PEMKeyStore.KEY_FILENAME, new GlobusResource(this.keyFile.getTempFile().getAbsolutePath()) |
| 338 |
.getURL().toExternalForm()); |
| 339 |
InputStream ins = null; |
| 340 |
try { |
| 341 |
ins = getProperties(properties); |
| 342 |
store.engineLoad(ins, null); |
| 343 |
} finally { |
| 344 |
if (ins != null) { |
| 345 |
ins.close(); |
| 346 |
} |
| 347 |
} |
| 348 |
Enumeration aliases = store.engineAliases(); |
| 349 |
assertTrue(aliases.hasMoreElements()); |
| 350 |
String alias = (String) aliases.nextElement(); |
| 351 |
Key key = store.engineGetKey(alias, null); |
| 352 |
assertNotNull(key); |
| 353 |
assertTrue(key instanceof PrivateKey); |
| 354 |
|
| 355 |
Certificate[] chain = store.engineGetCertificateChain(alias); |
| 356 |
assertNotNull(chain); |
| 357 |
|
| 358 |
Certificate certificate = store.engineGetCertificate(alias); |
| 359 |
assertNull(certificate); |
| 360 |
|
| 361 |
X509Credential x509Credential = new X509Credential(new FileInputStream(this.certFile.getAbsoluteFilename()), |
| 362 |
new FileInputStream(this.keyFile.getAbsoluteFilename())); |
| 363 |
|
| 364 |
assertEquals(key, x509Credential.getPrivateKey()); |
| 365 |
Certificate[] x509CredentialChain = x509Credential.getCertificateChain(); |
| 366 |
assertEquals(chain.length, x509CredentialChain.length); |
| 367 |
for (int i = 0; i < chain.length; i++) { |
| 368 |
assert (chain[i].equals(x509CredentialChain[i])); |
| 369 |
} |
| 370 |
|
| 371 |
store = new PEMKeyStore(); |
| 372 |
properties.setProperty(PEMKeyStore.CERTIFICATE_FILENAME, |
| 373 |
new GlobusResource(this.certFile.getTempFile().getAbsolutePath()).getURL().toExternalForm()); |
| 374 |
properties.setProperty(PEMKeyStore.KEY_FILENAME, |
| 375 |
new GlobusResource(this.keyEncFile.getTempFile().getAbsolutePath()).getURL().toExternalForm()); |
| 376 |
try { |
| 377 |
ins = getProperties(properties); |
| 378 |
store.engineLoad(ins, null); |
| 379 |
} finally { |
| 380 |
if (ins != null) { |
| 381 |
ins.close(); |
| 382 |
} |
| 383 |
} |
| 384 |
aliases = store.engineAliases(); |
| 385 |
assert (aliases.hasMoreElements()); |
| 386 |
alias = (String) aliases.nextElement(); |
| 387 |
|
| 388 |
try { |
| 389 |
store.engineGetKey(alias, null); |
| 390 |
fail(); |
| 391 |
} catch (UnrecoverableKeyException e) { |
| 392 |
//this had better fail |
| 393 |
} |
| 394 |
key = store.engineGetKey(alias, "test".toCharArray()); |
| 395 |
assertNotNull(key); |
| 396 |
assertTrue(key instanceof PrivateKey); |
| 397 |
chain = store.engineGetCertificateChain(alias); |
| 398 |
assertNotNull(chain); |
| 399 |
} |
| 400 |
|
| 401 |
private InputStream getProperties(Properties properties) throws Exception { |
| 402 |
|
| 403 |
ByteArrayOutputStream stream = null; |
| 404 |
ByteArrayInputStream ins = null; |
| 405 |
|
| 406 |
try { |
| 407 |
stream = new ByteArrayOutputStream(); |
| 408 |
properties.store(stream, "Test Properties"); |
| 409 |
|
| 410 |
// load all the CA files |
| 411 |
ins = new ByteArrayInputStream(stream.toByteArray()); |
| 412 |
|
| 413 |
} finally { |
| 414 |
if (stream != null) { |
| 415 |
stream.close(); |
| 416 |
} |
| 417 |
} |
| 418 |
return ins; |
| 419 |
} |
| 420 |
public static boolean deleteDir(File dir) { |
| 421 |
if (dir.isDirectory()) { |
| 422 |
String[] dirContent = dir.list(); |
| 423 |
for (int i=0; i<dirContent.length; i++){ |
| 424 |
boolean success = deleteDir(new File(dir, dirContent[i])); |
| 425 |
if (!success) { |
| 426 |
return false; |
| 427 |
} |
| 428 |
} |
| 429 |
} // The directory is now empty so delete it |
| 430 |
return dir.delete(); |
| 431 |
} |
| 432 |
@After |
| 433 |
public void tearDown() throws Exception { |
| 434 |
|
| 435 |
deleteDir(new File(trustedDirectory.getTempDirectoryName())); |
| 436 |
this.proxyFile1.deleteFile(); |
| 437 |
this.proxyFile2.deleteFile(); |
| 438 |
this.certFile.deleteFile(); |
| 439 |
this.keyFile.deleteFile(); |
| 440 |
this.keyEncFile.deleteFile(); |
| 441 |
} |
| 442 |
} |