[507] | 1 | package gov.hhs.fha.nhinc.callback;
|
---|
| 2 |
|
---|
| 3 | import com.sun.xml.wss.impl.callback.KeyStoreCallback;
|
---|
| 4 | import java.io.FileInputStream;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.io.InputStream;
|
---|
| 7 | import java.security.KeyStore;
|
---|
| 8 | import java.security.KeyStoreException;
|
---|
| 9 | import java.security.NoSuchAlgorithmException;
|
---|
| 10 | import java.security.cert.CertificateException;
|
---|
| 11 | import javax.security.auth.callback.Callback;
|
---|
| 12 | import javax.security.auth.callback.CallbackHandler;
|
---|
| 13 | import javax.security.auth.callback.UnsupportedCallbackException;
|
---|
| 14 | import org.apache.commons.logging.Log;
|
---|
| 15 | import 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 | */
|
---|
| 22 | public 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 |
|
---|