Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/stores/PEMKeyStore.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 static org.globus.gsi.util.CertificateIOUtil.writeCertificate;
19
20 import org.globus.gsi.CredentialException;
21 import org.globus.gsi.X509Credential;
22
23 import org.globus.gsi.provider.KeyStoreParametersFactory;
24
25 import org.apache.commons.logging.LogFactory;
26
27 import org.apache.commons.logging.Log;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.security.Key;
34 import java.security.KeyStore;
35 import java.security.KeyStoreException;
36 import java.security.KeyStoreSpi;
37 import java.security.NoSuchAlgorithmException;
38 import java.security.PrivateKey;
39 import java.security.UnrecoverableKeyException;
40 import java.security.cert.Certificate;
41 import java.security.cert.CertificateEncodingException;
42 import java.security.cert.CertificateException;
43 import java.security.cert.TrustAnchor;
44 import java.security.cert.X509Certificate;
45 import java.util.Collections;
46 import java.util.Date;
47 import java.util.Enumeration;
48 import java.util.HashMap;
49 import java.util.Hashtable;
50 import java.util.Map;
51 import java.util.Set;
52 import java.util.HashSet;
53 import java.util.Properties;
54
55 import org.globus.util.GlobusResource;
56 import org.globus.util.GlobusPathMatchingResourcePatternResolver;
57
58 import org.globus.gsi.util.CertificateIOUtil;
59
60 /**
61 * This class provides a KeyStore implementation that supports trusted
62 * certificates stored in PEM format and proxy certificates stored in PEM
63 * format. It reads trusted certificates from multiple directories and a proxy
64 * certificate from a file.
65 *
66 * @version ${version}
67 * @since 1.0
68 */
69 public class PEMKeyStore extends KeyStoreSpi {
70
71 // Default trusted certificates directory
72 public static final String DEFAULT_DIRECTORY_KEY = "default_directory";
73 // List of directory names to load certificates from
74 // JGLOBUS-90 : does it take certificate file names in this list?
75 public static final String DIRECTORY_LIST_KEY = "directory_list";
76 // X.509 Certificate file name, should be set along with KEY_FILENAME
77 public static final String CERTIFICATE_FILENAME = "certificateFilename";
78 // Key, typically private key, accompanying the certificate
79 public static final String KEY_FILENAME = "keyFilename";
80 // X.509 PRoxy Cerificate file name
81 public static final String PROXY_FILENAME = "proxyFilename";
82
83 private static Log logger = LogFactory.getLog(PEMKeyStore.class
84 .getCanonicalName());
85
86 // Map from alias to the object (either key or certificate)
87 private Map<String, SecurityObjectWrapper<?>> aliasObjectMap = new Hashtable<String, SecurityObjectWrapper<?>>();
88 // Map from trusted certificate to filename
89 private Map<Certificate, String> certFilenameMap = new HashMap<Certificate, String>();
90
91 // default directory for trusted certificates
92 private File defaultDirectory;
93 private ResourceSecurityWrapperStore<ResourceTrustAnchor, TrustAnchor> caDelegate = new ResourceCACertStore();
94 private ResourceSecurityWrapperStore<ResourceProxyCredential, X509Credential> proxyDelegate = new ResourceProxyCredentialStore();
95
96 private boolean inMemoryOnly = false;
97
98 public void setCACertStore(
99 ResourceSecurityWrapperStore<ResourceTrustAnchor, TrustAnchor> caCertStore) {
100 this.caDelegate = caCertStore;
101 }
102
103 public void setProxyDelegate(
104 ResourceSecurityWrapperStore<ResourceProxyCredential, X509Credential> proxyDelegate) {
105 this.proxyDelegate = proxyDelegate;
106 }
107
108 private CredentialWrapper getKeyEntry(String alias) {
109
110 SecurityObjectWrapper<?> object = this.aliasObjectMap.get(alias);
111 if ((object != null) && (object instanceof CredentialWrapper)) {
112 return (CredentialWrapper) object;
113 }
114 return null;
115 }
116
117 private ResourceTrustAnchor getCertificateEntry(String alias) {
118
119 SecurityObjectWrapper<?> object = this.aliasObjectMap.get(alias);
120 if ((object != null) && (object instanceof ResourceTrustAnchor)) {
121 return (ResourceTrustAnchor) object;
122 }
123 return null;
124 }
125
126 /**
127 * Get the key referenced by the specified alias.
128 *
129 * @param s
130 * The key's alias.
131 * @param chars
132 * The key's password.
133 * @return The key reference by the alias or null.
134 * @throws NoSuchAlgorithmException
135 * If the key is encoded with an invalid algorithm.
136 * @throws UnrecoverableKeyException
137 * If the key can not be retrieved.
138 */
139 @Override
140 public Key engineGetKey(String s, char[] chars)
141 throws NoSuchAlgorithmException, UnrecoverableKeyException {
142
143 CredentialWrapper credential = getKeyEntry(s);
144 Key key = null;
145 if (credential != null) {
146 try {
147 String password = null;
148 if (chars != null) {
149 password = new String(chars);
150 }
151 key = credential.getCredential().getPrivateKey(password);
152 } catch (ResourceStoreException e) {
153 throw new UnrecoverableKeyException(e.getMessage());
154 } catch (CredentialException e) {
155 throw new UnrecoverableKeyException(e.getMessage());
156 }
157 }
158 return key;
159 }
160
161 /**
162 * Does the supplied alias refer to a key in this key store.
163 *
164 * @param s
165 * The alias.
166 * @return True if the alias refers to a key.
167 */
168 @Override
169 public boolean engineIsKeyEntry(String s) {
170 return getKeyEntry(s) != null;
171 }
172
173 /**
174 * Persist the security material in this keystore. If the object has a path
175 * associated with it, the object will be persisted to that path. Otherwise
176 * it will be stored in the default certificate directory. As a result, the
177 * parameters of this method are ignored.
178 *
179 * @param outputStream
180 * This parameter is ignored.
181 * @param chars
182 * This parameter is ignored.
183 * @throws IOException
184 * @throws NoSuchAlgorithmException
185 * @throws CertificateException
186 */
187 @Override
188 public void engineStore(OutputStream outputStream, char[] chars)
189 throws IOException, NoSuchAlgorithmException, CertificateException {
190 for (SecurityObjectWrapper<?> object : this.aliasObjectMap.values()) {
191 if (object instanceof Storable) {
192 try {
193 ((Storable) object).store();
194 } catch (ResourceStoreException e) {
195 throw new CertificateException(e);
196 }
197 }
198 }
199 }
200
201 /**
202 * Get the creation date for the object referenced by the alias.
203 *
204 * @param s
205 * The alias of the security object.
206 * @return The creation date of the security object.
207 */
208 @Override
209 public Date engineGetCreationDate(String s) {
210 try {
211 ResourceTrustAnchor trustAnchor = getCertificateEntry(s);
212 if (trustAnchor != null) {
213 return trustAnchor.getTrustAnchor().getTrustedCert()
214 .getNotBefore();
215 } else {
216 CredentialWrapper credential = getKeyEntry(s);
217 if (credential != null) {
218 return credential.getCredential().getNotBefore();
219 }
220 }
221 } catch (ResourceStoreException e) {
222 return null;
223 }
224 return null;
225 }
226
227 /**
228 * Get the alias associated with the supplied certificate.
229 *
230 * @param certificate
231 * The certificate to query
232 * @return The certificate's alias or null if the certificate is not present
233 * in this keystore.
234 */
235 @Override
236 public String engineGetCertificateAlias(Certificate certificate) {
237 return this.certFilenameMap.get(certificate);
238 }
239
240 /**
241 * Get the certificateChain for the key referenced by the alias.
242 *
243 * @param s
244 * The key alias.
245 * @return The key's certificate chain or a 0 length array if the key is not
246 * in the keystore.
247 */
248 @Override
249 public Certificate[] engineGetCertificateChain(String s) {
250 CredentialWrapper credential = getKeyEntry(s);
251 X509Certificate[] chain = new X509Certificate[0];
252 if (credential != null) {
253 try {
254 chain = credential.getCredential().getCertificateChain();
255 } catch (ResourceStoreException e) {
256 logger.warn(e.getMessage(), e);
257 chain = null;
258 }
259 }
260 return chain;
261 }
262
263 /**
264 * Get the certificate referenced by the supplied alias.
265 *
266 * @param s
267 * The alias.
268 * @return The Certificate or null if the alias does not exist in the
269 * keyStore.
270 */
271 @Override
272 public Certificate engineGetCertificate(String s) {
273 ResourceTrustAnchor trustAnchor = getCertificateEntry(s);
274 if (trustAnchor != null) {
275 try {
276 return trustAnchor.getTrustAnchor().getTrustedCert();
277 } catch (ResourceStoreException e) {
278 return null;
279 }
280 }
281 return null;
282 }
283
284 /**
285 * Load the keystore based on parameters in the LoadStoreParameter. The
286 * parameter object must be an instance of FileBasedKeyStoreParameters.
287 *
288 * @param loadStoreParameter
289 * The parameters to load.
290 * @throws IOException
291 * @throws NoSuchAlgorithmException
292 * @throws CertificateException
293 */
294 @Override
295 public void engineLoad(KeyStore.LoadStoreParameter loadStoreParameter)
296 throws IOException, NoSuchAlgorithmException, CertificateException {
297 if (!(loadStoreParameter instanceof KeyStoreParametersFactory.FileStoreParameters)) {
298 throw new IllegalArgumentException("Unable to process parameters: "
299 + loadStoreParameter);
300 }
301 KeyStoreParametersFactory.FileStoreParameters params = (KeyStoreParametersFactory.FileStoreParameters) loadStoreParameter;
302 String defaultDirectoryString = (String) params
303 .getProperty(DEFAULT_DIRECTORY_KEY);
304 String directoryListString = (String) params
305 .getProperty(DIRECTORY_LIST_KEY);
306 String certFilename = (String) params.getProperty(CERTIFICATE_FILENAME);
307 String keyFilename = (String) params.getProperty(KEY_FILENAME);
308 String proxyFilename = (String) params.getProperty(PROXY_FILENAME);
309 initialize(defaultDirectoryString, directoryListString, proxyFilename,
310 certFilename, keyFilename);
311 }
312
313 /**
314 * Load the keystore from the supplied input stream. Unlike many other
315 * implementations of keystore (most notably the default JKS
316 * implementation), the input stream does not hold the keystore objects.
317 * Instead, it must be a properties file defining the locations of the
318 * keystore objects. The password is not used.
319 *
320 * @param inputStream
321 * An input stream to the properties file.
322 * @param chars
323 * The password is not used.
324 * @throws IOException
325 * @throws NoSuchAlgorithmException
326 * @throws CertificateException
327 */
328 @Override
329 public void engineLoad(InputStream inputStream, char[] chars)
330 throws IOException, NoSuchAlgorithmException, CertificateException {
331 try {
332 Properties properties = new Properties();
333 if(inputStream != null){
334 properties.load(inputStream);
335 if (properties.size() == 0) {
336 throw new CertificateException(
337 "Properties file for configuration was empty?");
338 }
339 }else{
340 if(chars == null){
341 // keyStore.load(null,null) -> in memory only keystore
342 inMemoryOnly = true;
343 }
344 }
345 String defaultDirectoryString = properties
346 .getProperty(DEFAULT_DIRECTORY_KEY);
347 String directoryListString = properties
348 .getProperty(DIRECTORY_LIST_KEY);
349 String proxyFilename = properties.getProperty(PROXY_FILENAME);
350 String certFilename = properties.getProperty(CERTIFICATE_FILENAME);
351 String keyFilename = properties.getProperty(KEY_FILENAME);
352 initialize(defaultDirectoryString, directoryListString,
353 proxyFilename, certFilename, keyFilename);
354 } finally {
355 if(inputStream != null){
356 try {
357 inputStream.close();
358 } catch (IOException e) {
359 logger.info("Error closing inputStream", e);
360 }
361 }
362 }
363 }
364
365 /**
366 * Initialize resources from filename, proxyfile name
367 *
368 * @param defaultDirectoryString
369 * Name of the default directory name as:
370 * "file: directory name"
371 * @param directoryListString
372 * @param proxyFilename
373 * @param certFilename
374 * @param keyFilename
375 *
376 * @throws IOException
377 * @throws CertificateException
378 */
379 private void initialize(String defaultDirectoryString,
380 String directoryListString, String proxyFilename,
381 String certFilename, String keyFilename) throws IOException,
382 CertificateException {
383
384 if (defaultDirectoryString != null) {
385 defaultDirectory = new GlobusPathMatchingResourcePatternResolver().getResource(defaultDirectoryString).getFile();
386 if (!defaultDirectory.exists()) {
387 boolean directoryMade = defaultDirectory.mkdirs();
388 if (!directoryMade) {
389 throw new IOException(
390 "Unable to create default certificate directory");
391 }
392 }
393 loadDirectories(defaultDirectoryString);
394 }
395 if (directoryListString != null) {
396 loadDirectories(directoryListString);
397 }
398 try {
399 if (proxyFilename != null && proxyFilename.length() > 0) {
400 loadProxyCertificate(proxyFilename);
401 }
402 if ((certFilename != null && certFilename.length() > 0)
403 && (keyFilename != null && keyFilename.length() > 0)) {
404 loadCertificateKey(certFilename, keyFilename);
405 }
406 } catch (ResourceStoreException e) {
407 throw new CertificateException(e);
408 } catch (CredentialException e) {
409 e.printStackTrace();
410 throw new CertificateException(e);
411 }
412 }
413
414 private void loadProxyCertificate(String proxyFilename)
415 throws ResourceStoreException {
416
417 if (proxyFilename == null) {
418 return;
419 }
420
421 proxyDelegate.loadWrappers(proxyFilename);
422 Map<String, ResourceProxyCredential> wrapperMap = proxyDelegate
423 .getWrapperMap();
424 for (ResourceProxyCredential credential : wrapperMap.values()) {
425 this.aliasObjectMap.put(proxyFilename, credential);
426 }
427 }
428
429 private void loadCertificateKey(String userCertFilename,
430 String userKeyFilename) throws CredentialException,
431 ResourceStoreException {
432 GlobusPathMatchingResourcePatternResolver resolver = new GlobusPathMatchingResourcePatternResolver();
433
434 if ((userCertFilename == null) || (userKeyFilename == null)) {
435 return;
436 }
437 // File certFile = new File(userCertFilename);
438 // File keyFile = new File(userKeyFilename);
439 GlobusResource certResource = resolver.getResource(userCertFilename);
440 GlobusResource keyResource = resolver.getResource(userKeyFilename);
441 CertKeyCredential credential = new CertKeyCredential(certResource,
442 keyResource);
443 // What do we name this alias?
444 String alias = userCertFilename + ":" + userKeyFilename;
445 this.aliasObjectMap.put(alias, credential);
446 }
447
448 private void loadDirectories(String directoryList)
449 throws CertificateException {
450
451 try {
452 caDelegate.loadWrappers(directoryList);
453 Map<String, ResourceTrustAnchor> wrapperMap = caDelegate
454 .getWrapperMap();
455 Set<String> knownCerts = new HashSet<String>();
456 // The alias hashing merits explanation. Loading all the files in a directory triggers a
457 // deadlock bug for old jglobus clients if the directory contains repeated CAs (like the
458 // modern IGTF bundle does). So, we ignore the cert if the alias is incorrect or already seen.
459 // However, we track all the certs we ignore and load any that were completely ignored due to
460 // aliases. So, non-hashed directories will still work.
461 Map<String, String> ignoredAlias = new HashMap<String, String>();
462 Map<String, ResourceTrustAnchor> ignoredAnchor = new HashMap<String, ResourceTrustAnchor>();
463 Map<String, X509Certificate> ignoredCert = new HashMap<String, X509Certificate>();
464 for (ResourceTrustAnchor trustAnchor : wrapperMap.values()) {
465 String alias = trustAnchor.getResourceURL().toExternalForm();
466 TrustAnchor tmpTrustAnchor = trustAnchor.getTrustAnchor();
467 X509Certificate trustCert = tmpTrustAnchor.getTrustedCert();
468 String hash = CertificateIOUtil.nameHash(trustCert.getSubjectX500Principal());
469 if (this.aliasObjectMap == null) {
470 System.out.println("Alias Map Null");
471 }
472 boolean hash_in_alias = !alias.contains(hash);
473 if (knownCerts.contains(hash) || !hash_in_alias) {
474 if (!hash_in_alias) {
475 ignoredAlias.put(hash, alias);
476 ignoredAnchor.put(hash, trustAnchor);
477 ignoredCert.put(hash, trustCert);
478 }
479 continue;
480 }
481 knownCerts.add(hash);
482 this.aliasObjectMap.put(alias, trustAnchor);
483 certFilenameMap.put(trustCert, alias);
484 }
485 // Add any CA we skipped above.
486 for (String hash : ignoredAlias.keySet()) {
487 if (knownCerts.contains(hash)) {
488 continue;
489 }
490 String alias = ignoredAlias.get(hash);
491 this.aliasObjectMap.put(alias, ignoredAnchor.get(hash));
492 certFilenameMap.put(ignoredCert.get(hash), alias);
493 }
494 } catch (ResourceStoreException e) {
495 throw new CertificateException("",e);
496 }
497 }
498
499 /**
500 * Delete a security object from this keystore.
501 *
502 * @param s
503 * The alias of the object to delete.
504 * @throws KeyStoreException
505 */
506 @Override
507 public void engineDeleteEntry(String s) throws KeyStoreException {
508
509 SecurityObjectWrapper<?> object = this.aliasObjectMap.remove(s);
510 if (object != null) {
511 if (object instanceof ResourceTrustAnchor) {
512
513 ResourceTrustAnchor descriptor = (ResourceTrustAnchor) object;
514 Certificate cert;
515 try {
516 cert = descriptor.getTrustAnchor().getTrustedCert();
517 } catch (ResourceStoreException e) {
518 throw new KeyStoreException(e);
519 }
520 this.certFilenameMap.remove(cert);
521 boolean success = descriptor.getFile().delete();
522 if (!success) {
523 // JGLOBUS-91 : warn? throw error?
524 logger.info("Unable to delete certificate");
525 }
526 } else if (object instanceof ResourceProxyCredential) {
527
528 ResourceProxyCredential proxy = (ResourceProxyCredential) object;
529 try {
530 proxy.getCredential();
531 } catch (ResourceStoreException e) {
532 throw new KeyStoreException(e);
533 }
534 boolean success = proxy.getFile().delete();
535 if (!success) {
536 // JGLOBUS-91 : warn? throw error?
537 logger.info("Unable to delete credential");
538 }
539 }
540 }
541 }
542
543 /**
544 * Get an enumertion of all of the aliases in this keystore.
545 *
546 * @return An enumeration of the aliases in this keystore.
547 */
548 @Override
549 public Enumeration<String> engineAliases() {
550
551 return Collections.enumeration(this.aliasObjectMap.keySet());
552 }
553
554 /**
555 * Add a new private key to the keystore.
556 *
557 * @param s
558 * The alias for the object.
559 * @param key
560 * The private key.
561 * @param chars
562 * The password.
563 * @param certificates
564 * The key's certificate chain.
565 * @throws KeyStoreException
566 */
567 @Override
568 public void engineSetKeyEntry(String s, Key key, char[] chars,
569 Certificate[] certificates) throws KeyStoreException {
570
571 if (!(key instanceof PrivateKey)) {
572 throw new KeyStoreException("PrivateKey expected");
573 }
574
575 if (!(certificates instanceof X509Certificate[])) {
576 throw new KeyStoreException(
577 "Certificate chain of X509Certificate expected");
578 }
579 CredentialWrapper wrapper;
580 X509Credential credential = new X509Credential((PrivateKey) key,
581 (X509Certificate[]) certificates);
582 if (credential.isEncryptedKey()) {
583 wrapper = createCertKeyCredential(s, credential);
584 } else {
585 wrapper = createProxyCredential(s, credential);
586 }
587 storeWrapper(wrapper);
588 this.aliasObjectMap.put(wrapper.getAlias(), wrapper);
589 }
590
591 @SuppressWarnings("rawtypes")
592 private CredentialWrapper createProxyCredential(String s,
593 X509Credential credential) throws KeyStoreException {
594 CredentialWrapper wrapper;
595 CredentialWrapper proxyCredential = getKeyEntry(s);
596 File file;
597 if (proxyCredential != null
598 && proxyCredential instanceof AbstractResourceSecurityWrapper) {
599 AbstractResourceSecurityWrapper proxyWrapper = (AbstractResourceSecurityWrapper) proxyCredential;
600 file = proxyWrapper.getFile();
601 } else {
602 // JGLOBUS-91 : should alias be file name? or generate?
603 file = new File(defaultDirectory, s + "-key.pem");
604 }
605 try {
606 wrapper = new ResourceProxyCredential(inMemoryOnly, new GlobusResource(file.getAbsolutePath()),
607 credential);
608 } catch (ResourceStoreException e) {
609 throw new KeyStoreException(e);
610 }
611 return wrapper;
612 }
613
614 private CredentialWrapper createCertKeyCredential(String s,
615 X509Credential credential) throws KeyStoreException {
616 GlobusResource certResource;
617 GlobusResource keyResource;
618 CredentialWrapper wrapper;
619 CredentialWrapper credentialWrapper = getKeyEntry(s);
620 if (credentialWrapper != null
621 && credentialWrapper instanceof CertKeyCredential) {
622 CertKeyCredential certKeyCred = (CertKeyCredential) credentialWrapper;
623 certResource = certKeyCred.getCertificateFile();
624 keyResource = certKeyCred.getKeyFile();
625 } else {
626 certResource = new GlobusResource(new File(defaultDirectory, s
627 + ".0").getAbsolutePath());
628 keyResource = new GlobusResource(new File(defaultDirectory, s
629 + "-key.pem").getAbsolutePath());
630 }
631 try {
632 wrapper = new CertKeyCredential(certResource, keyResource,
633 credential);
634 } catch (ResourceStoreException e) {
635 throw new KeyStoreException(e);
636 }
637 return wrapper;
638 }
639
640 private void storeWrapper(CredentialWrapper wrapper)
641 throws KeyStoreException {
642 if(!inMemoryOnly){
643 try {
644 wrapper.store();
645 } catch (ResourceStoreException e) {
646 throw new KeyStoreException("Error storing credential", e);
647 }
648 }
649 }
650
651 /**
652 * currently unsupported.
653 *
654 * @param s
655 * The key's alias
656 * @param bytes
657 * The encoded private key.
658 * @param certificates
659 * The key's certificate chain.
660 * @throws KeyStoreException
661 */
662 @Override
663 public void engineSetKeyEntry(String s, byte[] bytes,
664 Certificate[] certificates) throws KeyStoreException {
665 throw new UnsupportedOperationException();
666 // JGLOBUS-91
667 }
668
669 /**
670 * Does the specified alias exist in this keystore?
671 *
672 * @param s
673 * The alias.
674 * @return True if the alias refers to a security object in the keystore.
675 */
676 @Override
677 public boolean engineContainsAlias(String s) {
678 return this.aliasObjectMap.containsKey(s);
679 }
680
681 /**
682 * Get the number of security objects stored in this keystore.
683 *
684 * @return The number of security objects.
685 */
686 @Override
687 public int engineSize() {
688 return this.aliasObjectMap.size();
689 }
690
691 /**
692 * Does the supplied alias refer to a certificate in this keystore?
693 *
694 * @param s
695 * The alias.
696 * @return True if this store contains a certificate with the specified
697 * alias.
698 */
699 @Override
700 public boolean engineIsCertificateEntry(String s) {
701 return getCertificateEntry(s) != null;
702 }
703
704 /**
705 * Add a certificate to the keystore.
706 *
707 * @param alias
708 * The certificate alias.
709 * @param certificate
710 * The certificate to store.
711 * @throws KeyStoreException
712 */
713 @Override
714 public void engineSetCertificateEntry(String alias, Certificate certificate)
715 throws KeyStoreException {
716
717 if (!(certificate instanceof X509Certificate)) {
718 throw new KeyStoreException(
719 "Certificate must be instance of X509Certificate");
720 }
721 File file;
722 ResourceTrustAnchor trustAnchor = getCertificateEntry(alias);
723 if (trustAnchor != null) {
724 file = trustAnchor.getFile();
725 } else {
726 file = new File(defaultDirectory, alias);
727 }
728 X509Certificate x509Cert = (X509Certificate) certificate;
729 try {
730 if(!inMemoryOnly){
731 writeCertificate(x509Cert, file);
732 }
733 ResourceTrustAnchor anchor = new ResourceTrustAnchor(inMemoryOnly,
734 new GlobusResource(file.getAbsolutePath()), new TrustAnchor(x509Cert,
735 null));
736 this.aliasObjectMap.put(alias, anchor);
737 this.certFilenameMap.put(x509Cert, alias);
738 } catch (ResourceStoreException e) {
739 throw new KeyStoreException(e);
740 } catch (IOException e) {
741 throw new KeyStoreException(e);
742 } catch (CertificateEncodingException e) {
743 throw new KeyStoreException(e);
744 }
745 }
746
747 }