Example usage for org.apache.commons.ssl KeyMaterial KeyMaterial

List of usage examples for org.apache.commons.ssl KeyMaterial KeyMaterial

Introduction

In this page you can find the example usage for org.apache.commons.ssl KeyMaterial KeyMaterial.

Prototype

public KeyMaterial(byte[] jks, char[] password) throws GeneralSecurityException, IOException 

Source Link

Usage

From source file:com.eviware.loadui.impl.messaging.socket.SocketMessageEndpointProvider.java

public SocketMessageEndpointProvider() throws IOException, GeneralSecurityException {
    client = new SSLClient();

    client.addTrustMaterial(new TrustMaterial(System.getProperty(LoadUI.TRUST_STORE),
            System.getProperty(LoadUI.TRUST_STORE_PASSWORD).toCharArray()));

    client.setCheckHostname(false); // default setting is "true" for SSLClient
    client.setCheckCRL(false); // default setting is "true" for SSLClient

    client.setKeyMaterial(new KeyMaterial(System.getProperty(LoadUI.KEY_STORE),
            System.getProperty(LoadUI.KEY_STORE_PASSWORD).toCharArray()));
}

From source file:com.eviware.soapui.impl.wsdl.support.http.SoapUIEasySSLProtocolSocketFactory.java

@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException {
    String sslConfig = (String) params.getParameter(SoapUIHostConfiguration.SOAPUI_SSL_CONFIG);

    if (StringUtils.isNullOrEmpty(sslConfig)) {
        return enableSocket((SSLSocket) super.createSocket(host, port, localAddress, localPort, params));
    }/*from  w  ww  . j a v  a  2 s  .c  o m*/

    EasySSLProtocolSocketFactory factory = factoryMap.get(sslConfig);
    if (factory != null) {
        return enableSocket((SSLSocket) factory.createSocket(host, port, localAddress, localPort, params));
    }
    try {
        // try to create new factory for specified config
        factory = new EasySSLProtocolSocketFactory();

        int ix = sslConfig.lastIndexOf(' ');
        String keyStore = sslConfig.substring(0, ix);
        String pwd = sslConfig.substring(ix + 1);

        factory.setKeyMaterial(new KeyMaterial(keyStore, pwd.toCharArray()));
        factoryMap.put(sslConfig, factory);

        return enableSocket((SSLSocket) factory.createSocket(host, port, localAddress, localPort, params));
    } catch (Exception gse) {
        SoapUI.logError(gse);
        return enableSocket((SSLSocket) super.createSocket(host, port, localAddress, localPort, params));
    }
}

From source file:MiGSSLSocketFactory.java

public MiGSSLSocketFactory() throws GeneralSecurityException, IOException {

    super();/* ww w .  j  av a2  s  . c  o m*/
    this.loadConf();

    super.setTrustMaterial(new TrustMaterial(cacertFile));
    super.setCheckHostname(false);
    super.setCheckExpiry(false);
    super.setCheckCRL(false);
    super.setKeyMaterial(new KeyMaterial(certFile, password.toCharArray()));
}

From source file:com.eviware.soapui.impl.wsdl.support.http.SoapUISSLSocketFactory.java

@Override
public Socket createSocket(HttpParams params) throws IOException {
    String sslConfig = (String) params.getParameter(SoapUIHttpRoute.SOAPUI_SSL_CONFIG);

    if (StringUtils.isNullOrEmpty(sslConfig)) {
        return enableSocket((SSLSocket) sslContext.getSocketFactory().createSocket());
    }/*from w  w  w  . j a  v a2  s. co  m*/

    SSLSocketFactory factory = factoryMap.get(sslConfig);

    if (factory != null) {
        if (factory == this)
            return enableSocket((SSLSocket) sslContext.getSocketFactory().createSocket());
        else
            return enableSocket((SSLSocket) factory.createSocket(params));
    }

    try {
        // try to create new factory for specified config
        int ix = sslConfig.lastIndexOf(' ');
        String keyStore = sslConfig.substring(0, ix);
        String pwd = sslConfig.substring(ix + 1);

        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

        if (keyStore.trim().length() > 0) {
            File f = new File(keyStore);

            if (f.exists()) {
                log.info("Initializing Keystore from [" + keyStore + "]");

                try {
                    KeyMaterial km = new KeyMaterial(f, pwd.toCharArray());
                    ks = km.getKeyStore();
                } catch (Exception e) {
                    SoapUI.logError(e);
                    pwd = null;
                }
            }
        }

        factory = new SoapUISSLSocketFactory(ks, pwd);
        factoryMap.put(sslConfig, factory);

        return enableSocket((SSLSocket) factory.createSocket(params));
    } catch (Exception gse) {
        SoapUI.logError(gse);
        return enableSocket((SSLSocket) super.createSocket(params));
    }
}

From source file:org.binding.openhab.samsungac.communicator.AirConditioner.java

private void connect() throws Exception {
    if (isConnected()) {
        return;//from   www. j a  v  a  2 s  .  c o m
    } else {
        logger.debug("Disconnected so we'll try again");
        disconnect();
    }

    if (CERTIFICATE_FILE_NAME != null && new File(CERTIFICATE_FILE_NAME).isFile()) {
        if (CERTIFICATE_PASSWORD == null) {
            CERTIFICATE_PASSWORD = "";
        }
        try {
            SSLClient client = new SSLClient();

            client.addTrustMaterial(TrustMaterial.DEFAULT);
            client.setCheckHostname(false);
            client.setKeyMaterial(new KeyMaterial(CERTIFICATE_FILE_NAME, CERTIFICATE_PASSWORD.toCharArray()));
            client.setConnectTimeout(10000);
            socket = (SSLSocket) client.createSocket(IP, PORT);
            socket.setSoTimeout(30000);
            socket.startHandshake();
        } catch (Exception e) {
            throw new Exception("Could not connect using certificate: " + CERTIFICATE_FILE_NAME, e);
        }
    } else {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                }
            } };

            ctx.init(null, trustAllCerts, null);
            socket = (SSLSocket) ctx.getSocketFactory().createSocket(IP, PORT);
            socket.setSoTimeout(10000);
            socket.startHandshake();
        } catch (Exception e) {
            throw new Exception("Cannot connect to " + IP + ":" + PORT, e);
        }
    }
    handleResponse();
}

From source file:org.helios.collector.url.URLCollector.java

/**
 * Load keystore for CLIENT-CERT protected endpoints
 * /*w  w w  . j a  va2 s.  c  o m*/
 * @return
 * @throws GeneralSecurityException
 * @throws Exception
 */
private KeyMaterial createKeyMaterial() throws GeneralSecurityException, Exception {
    KeyMaterial km = null;
    char[] password = keyStorePassphrase.toCharArray();
    File f = new File(keyStoreLocation);
    if (f.exists()) {
        try {
            km = new KeyMaterial(keyStoreLocation, password);
            trace("Keystore location is: " + keyStoreLocation + "");
        } catch (GeneralSecurityException gse) {
            if (logErrors) {
                error("Exception occured while loading keystore from the following location: "
                        + keyStoreLocation, gse);
                throw gse;
            }
        }
    } else {
        error("Unable to load Keystore from the following location: " + keyStoreLocation);
        throw new CollectorException(
                "Unable to load Keystore from the following location: " + keyStoreLocation);
    }
    return km;
}

From source file:org.openhab.binding.samsungac.internal.AirConditioner.java

private void connect() throws Exception {
    if (isConnected()) {
        return;//  w  ww  .  j av a2  s .  c o m
    } else {
        logger.debug("Disconnected so we'll try again");
        disconnect();
    }

    if (CERTIFICATE_FILE_NAME != null && new File(CERTIFICATE_FILE_NAME).isFile()) {
        if (CERTIFICATE_PASSWORD == null) {
            CERTIFICATE_PASSWORD = "";
        }
        try {
            SSLClient client = new SSLClient();

            client.addTrustMaterial(TrustMaterial.DEFAULT);
            client.setCheckHostname(false);
            client.setKeyMaterial(new KeyMaterial(CERTIFICATE_FILE_NAME, CERTIFICATE_PASSWORD.toCharArray()));
            client.setConnectTimeout(10000);
            socket = (SSLSocket) client.createSocket(IP, PORT);
            socket.setSoTimeout(2000);
            socket.startHandshake();
        } catch (Exception e) {
            throw new Exception("Could not connect using certificate: " + CERTIFICATE_FILE_NAME, e);
        }
    } else {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                }
            } };

            ctx.init(null, trustAllCerts, null);
            socket = (SSLSocket) ctx.getSocketFactory().createSocket(IP, PORT);
            socket.setSoTimeout(2000);
            socket.startHandshake();
        } catch (Exception e) {
            throw new Exception("Cannot connect to " + IP + ":" + PORT, e);
        }
    }
    handleResponse();
}

From source file:org.wso2.carbon.andes.authentication.andes.oauth.OAuthTokenValidaterStubFactory.java

/**
 * This is required to create a trusted connection with the external entity.
 * Have to manually configure it since we use CommonHTTPTransport(axis2 transport) in axis2.
 * @return an EasySSLProtocolSocketFactory for SSL communication.
 *//*  ww  w  . j a va2s.  co m*/
private EasySSLProtocolSocketFactory createProtocolSocketFactory() throws OAuthTokenValidationException {
    try {
        EasySSLProtocolSocketFactory easySSLPSFactory = new EasySSLProtocolSocketFactory();
        JKSStore jksKeyStore = OAuthConfigurationManager.getInstance().getJksKeyStore();
        String keyStoreLocation = jksKeyStore.getStoreLocation();
        char[] password = jksKeyStore.getPassword().toCharArray();
        File keyStoreFile = new File(keyStoreLocation);
        if (keyStoreFile.exists()) {
            KeyMaterial km = new KeyMaterial(keyStoreLocation, password);
            easySSLPSFactory.setKeyMaterial(km);
            return easySSLPSFactory;
        } else {
            String errorMsg = "Unable to load Keystore from the following location: " + keyStoreLocation;
            throw new OAuthTokenValidationException(errorMsg);
        }
    } catch (IOException e) {
        String errorMsg = "Failed to initiate EasySSLProtocolSocketFactory.";
        throw new OAuthTokenValidationException(errorMsg, e);
    } catch (GeneralSecurityException e) {
        String errorMsg = "Failed to set the key material in easy ssl factory.";
        throw new OAuthTokenValidationException(errorMsg, e);
    }
}