source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Gateway/NhincSAMLCallbackLib/src/gov/hhs/fha/nhinc/callback/TrustStoreCallbackHandler.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

File size: 4.3 KB
Line 
1package gov.hhs.fha.nhinc.callback;
2
3import com.sun.xml.wss.impl.callback.KeyStoreCallback;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.security.KeyStore;
8import java.security.KeyStoreException;
9import java.security.NoSuchAlgorithmException;
10import java.security.cert.CertificateException;
11import javax.security.auth.callback.Callback;
12import javax.security.auth.callback.CallbackHandler;
13import javax.security.auth.callback.UnsupportedCallbackException;
14import org.apache.commons.logging.Log;
15import org.apache.commons.logging.LogFactory;
16
17/**
18 * This class uses the truststore system properties as established in the
19 * domain.xml file to allow the configuration of the SAML Truststore policy
20 * statements.
21 */
22public class TrustStoreCallbackHandler implements CallbackHandler {
23
24 private KeyStore keyStore = null;
25 private String password;
26 private static final String storeType = "JKS";
27 private static Log log = LogFactory.getLog(TrustStoreCallbackHandler.class);
28
29 /**
30 * Creates the callback handler saving the truststore certificates
31 * information from the truststore file specified by the system properties:
32 * javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword.
33 */
34 public TrustStoreCallbackHandler() {
35 log.debug("Entry TrustStoreCallbackHandler Constructor");
36 InputStream is = null;
37 String storeLoc = System.getProperty("javax.net.ssl.trustStore");
38 if (storeLoc != null) {
39 password = System.getProperty("javax.net.ssl.trustStorePassword");
40 if (password != null) {
41 try {
42 keyStore = KeyStore.getInstance(storeType);
43 is = new FileInputStream(storeLoc);
44 keyStore.load(is, password.toCharArray());
45 } catch (IOException ex) {
46 log.debug("TrustStoreCallbackHandler " + ex);
47 throw new RuntimeException(ex);
48 } catch (NoSuchAlgorithmException ex) {
49 log.debug("TrustStoreCallbackHandler " + ex);
50 throw new RuntimeException(ex);
51 } catch (CertificateException ex) {
52 log.debug("TrustStoreCallbackHandler " + ex);
53 throw new RuntimeException(ex);
54 } catch (KeyStoreException ex) {
55 log.debug("TrustStoreCallbackHandler " + ex);
56 throw new RuntimeException(ex);
57 } finally {
58 try {
59 is.close();
60 } catch (IOException ex) {
61 log.debug("TrustStoreCallbackHandler " + ex);
62 }
63 }
64 } else {
65 log.error("javax.net.ssl.trustStorePassword is not defined in domain.xml");
66 }
67 } else {
68 log.error("javax.net.ssl.trustStore is not defined in domain.xml");
69 }
70 log.debug("Exit TrustStoreCallbackHandler Constructor");
71 }
72
73 /**
74 * Implementing the callback, this method provides the truststore
75 * information to the input Callback object.
76 * @param callbacks The Callback which needs to have truststore information
77 * set.
78 * @throws java.io.IOException
79 * @throws javax.security.auth.callback.UnsupportedCallbackException
80 */
81 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
82 log.debug("Entry TrustStoreCallbackHandler handle callback");
83 for (int i = 0; i < callbacks.length; i++) {
84 if (callbacks[i] instanceof KeyStoreCallback) {
85 KeyStoreCallback cb = (KeyStoreCallback) callbacks[i];
86 //print(cb.getRuntimeProperties());
87 cb.setKeystore(keyStore);
88 log.debug("KeyStoreCallback set keystore: " + keyStore);
89 } else {
90 log.error("Unsupported KeyStoreCallbackHandler Callback: " + callbacks[i]);
91 throw new UnsupportedCallbackException(callbacks[i]);
92 }
93 }
94 log.debug("Exit TrustStoreCallbackHandler handle callback");
95 }
96
97 /*private void print(Map context) {
98 Iterator it = context.keySet().iterator();
99 while (it.hasNext()) {
100 log.debug("Prop " + it.next());
101 }
102 }*/
103}
104
Note: See TracBrowser for help on using the repository browser.