View Javadoc

1   package org.eparapher.core.crypto.keystore;
2   
3   import java.io.FileNotFoundException;
4   import java.io.IOException;
5   import java.security.KeyStore;
6   import java.security.KeyStoreException;
7   import java.security.NoSuchAlgorithmException;
8   import java.security.cert.CertificateException;
9   
10  import org.apache.log4j.Logger;
11  
12  public class MSCAPIKeystore extends GenericKeystore implements ITrustStore {
13  
14  	private static Logger log = Logger.getLogger(MSCAPIKeystore.class);
15  	
16  	public MSCAPIKeystore() {
17  		super();
18  	}
19  
20  	public boolean loadKeyStore() {
21  		log.info("loading MS CAPI Keystore for user keys & certificates");
22  		try {
23  			ks = KeyStore.getInstance("Windows-MY");
24  			ks.load(null, null);
25  			this.loadedKeystore=true;
26  			selectAlias();
27  			return true;
28  		} catch (KeyStoreException e) {
29  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
30  		} catch (NoSuchAlgorithmException e) {
31  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
32  		} catch (CertificateException e) {
33  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
34  		} catch (IOException e) {
35  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
36  		}	
37  		return false;
38  	}
39  
40  	public boolean loadPrivateKey() {
41  		if (!this.isKeystoreLoaded())
42  			if (!loadKeyStore())
43  				return false;
44  		return super.loadPrivateKey(null);
45  	}
46  	
47  	public boolean loadKeyStore(String secret) {
48  		return true;
49  	}
50  
51  	public boolean loadPrivateKey(String secret) {
52  		return loadPrivateKey();
53  	}
54  
55  	public boolean loadTrustStore() {
56  		if (!this.loadedKeystore) {
57  		log.info("loading MS CAPI Keystore for trusted certificates");
58  		try {
59  			ks = KeyStore.getInstance("Windows-ROOT");
60  			ks.load(null, null);
61  			this.loadedKeystore=true;
62  			return true;
63  		} catch (KeyStoreException e) {
64  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
65  		} catch (NoSuchAlgorithmException e) {
66  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
67  		} catch (CertificateException e) {
68  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
69  		} catch (IOException e) {
70  			log.error("error on MS CAPI Keystore loading "+e.getLocalizedMessage(),e);
71  		}	
72  		return false;
73  		} else return true;
74  	}
75  
76  	public boolean saveTrustStore() {
77  		return saveKeyStore();
78  	}
79  
80  	public boolean saveKeyStore() {
81  		if (this.loadedKeystore) {
82  			try {
83  				ks.store(null, null);
84  				return true;
85  			} catch (FileNotFoundException e) {
86  				log.error("Error Saving MSCAPI Keystore",e);
87  			} catch (KeyStoreException e) {
88  				log.error("Error Saving MSCAPI Keystore",e);
89  			} catch (NoSuchAlgorithmException e) {
90  				log.error("Error Saving MSCAPI Keystore",e);
91  			} catch (CertificateException e) {
92  				log.error("Error Saving MSCAPI Keystore",e);
93  			} catch (IOException e) {
94  				log.error("Error Saving MSCAPI Keystore",e);
95  			}
96  		} else
97  			log.error("Cannot save a keystore that hasn't been loaded");
98  		return false;
99  	}
100 
101 	public boolean exists() {
102 		log.debug("Verify if MSCAPI Keystore exists and can be loaded");
103 		try {
104 			KeyStore ks = KeyStore.getInstance("Windows-MY");
105 			if (ks !=null)
106 				return true;
107 		} catch (Exception e) {
108 			log.error("Error while loading MS CAPI Keystore : "+e.getLocalizedMessage(),e);
109 		}
110 		return false;
111 	}
112 
113 	public boolean initialize() {
114 		//warn user?
115 		return true;
116 	}
117 }