View Javadoc
1   package civitas.common;
2   
3   import java.io.IOException;
4   import java.nio.file.Files;
5   import java.nio.file.Paths;
6   import java.security.KeyStore;
7   import java.security.KeyStoreException;
8   import java.security.NoSuchAlgorithmException;
9   import java.security.PrivateKey;
10  import java.security.UnrecoverableKeyException;
11  import java.security.cert.CertificateException;
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import org.springframework.beans.factory.annotation.Autowired;
16  import org.springframework.stereotype.Controller;
17  
18  @Controller
19  public class GetPrivateKey {
20  
21  	@Autowired
22  	Configuration configuration;
23  
24  	Map<String, PrivateKey> keyCache = new HashMap<>();
25  
26  	public PrivateKey apply(final String storeFile, final String storePassword, final String serverKeyEntry)
27  			throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
28  					UnrecoverableKeyException {
29  
30  		if (null == keyCache.get(serverKeyEntry)) {
31  			char[] pwdArray = storePassword.toCharArray();
32  			KeyStore store = KeyStore.getInstance("JKS");
33  			store.load(Files.newInputStream(Paths.get(storeFile)), pwdArray);
34  			keyCache.put(serverKeyEntry, (PrivateKey) store.getKey(serverKeyEntry, pwdArray));
35  		}
36  		return keyCache.get(serverKeyEntry);
37  	}
38  }