Example usage for javax.net.ssl KeyManagerFactory getKeyManagers

List of usage examples for javax.net.ssl KeyManagerFactory getKeyManagers

Introduction

In this page you can find the example usage for javax.net.ssl KeyManagerFactory getKeyManagers.

Prototype

public final KeyManager[] getKeyManagers() 

Source Link

Document

Returns one key manager for each type of key material.

Usage

From source file:cn.dacas.emmclient.security.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {/*from  w w w .j  a v  a2s .  c o m*/

        // Client should authenticate itself with the valid certificate to Server.
        InputStream clientStream = EmmClientApplication.getContext().getResources()
                .openRawResource(R.raw.production_test_client);
        char[] password = "XXXXXXXXXXXXX".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // Client should also add the CA certificate obtained from server and create TrustManager from it for the client to validate the 
        // identity of the server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = EmmClientApplication.getContext().getResources().openRawResource(R.raw.production_test_ca);

        try {
            trustStore.load(instream, "XXXXXXXX".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager & Keystore
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        return context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:org.zywx.wbpalmstar.platform.certificates.HSSLSocketFactory.java

public HSSLSocketFactory(KeyStore ksP12, String keyPass) throws Exception {
    super(ksP12);
    mSSLContext = SSLContext.getInstance(SSLSocketFactory.TLS);
    KeyManagerFactory kMgrFact = null;
    TrustManager[] tMgrs = null;/*from   w  w  w .j  a v  a  2 s  .c o  m*/
    KeyManager[] kMgrs = null;
    TrustManager tMgr = null;
    tMgr = new HX509TrustManager(ksP12);
    kMgrFact = KeyManagerFactory.getInstance(Http.algorithm);
    if (null != keyPass) {
        kMgrFact.init(ksP12, keyPass.toCharArray());
    } else {
        kMgrFact.init(ksP12, null);
    }
    kMgrs = kMgrFact.getKeyManagers();
    tMgrs = new TrustManager[] { tMgr };
    SecureRandom secureRandom = new java.security.SecureRandom();
    mSSLContext.init(kMgrs, tMgrs, secureRandom);
    if (!Http.isCheckTrustCert()) {
        setHostnameVerifier(new HX509HostnameVerifier());
    } else {
        setHostnameVerifier(STRICT_HOSTNAME_VERIFIER);
    }
}

From source file:com.vtc.basetube.services.volley.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext(Context context) throws IOException {
    try {// w w  w. j  a v  a2s.c  o  m
        // Client should authenticate itself with the valid certificate to
        // Server.
        InputStream clientStream = context.getResources().openRawResource(CERTIFICATE_RESOURCE_CLIENT);
        char[] password = "XXXXXXXXXXXXX".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // Client should also add the CA certificate obtained from server
        // and create TrustManager from it for the client to validate the
        // identity of the server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = context.getResources().openRawResource(CERTIFICATE_RESOURCE_CA);

        try {
            trustStore.load(instream, "XXXXXXXX".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager & Keystore
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        return sslContext;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:net.sf.ufsc.ftp.FTPSClient.java

public FTPSClient() {
    super();/*from  www  .  j ava  2  s. com*/

    try {
        KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
        keyStore.load(null, PASSWORD.toCharArray());

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());

        keyManagerFactory.init(keyStore, PASSWORD.toCharArray());

        SSLContext context = SSLContext.getInstance(PROTOCOL);
        context.init(keyManagerFactory.getKeyManagers(), new TrustManager[] { new SimpleTrustManager() }, null);

        this.socketFactory = new SecureSocketFactory(context);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.oscarehr.olis.OLISProtocolSocketFactory.java

public OLISProtocolSocketFactory() throws Exception {

    String pKeyFile = OscarProperties.getInstance().getProperty("olis_ssl_keystore").trim();
    String pKeyPassword = OscarProperties.getInstance().getProperty("olis_ssl_keystore_password").trim();

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    KeyStore keyStore = KeyStore.getInstance("JKS");
    InputStream keyInput = new FileInputStream(pKeyFile);
    keyStore.load(keyInput, pKeyPassword.toCharArray());
    keyInput.close();//from w  w w .j  a  v a  2s  .c o m
    keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());

    context = SSLContext.getInstance("TLS");
    context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
}

From source file:davmail.util.ClientCertificateTest.java

public void testClientSocket() throws NoSuchAlgorithmException, KeyStoreException, IOException,
        CertificateException, KeyManagementException, UnrecoverableKeyException {

    //System.setProperty("javax.net.ssl.trustStoreProvider", "SunMSCAPI");
    //System.setProperty("javax.net.ssl.trustStoreType", "Windows-ROOT");
    System.setProperty("javax.net.ssl.trustStore", "cacerts");
    System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
    System.setProperty("javax.net.ssl.trustStoreType", "JKS");

    String algorithm = KeyManagerFactory.getDefaultAlgorithm();
    if ("SunX509".equals(algorithm)) {
        algorithm = "NewSunX509";
    } else if ("IbmX509".equals(algorithm)) {
        algorithm = "NewIbmX509";
    }/*  www. java  2 s.co  m*/

    Provider sunMSCAPI = new sun.security.mscapi.SunMSCAPI();
    //Security.insertProviderAt(sunMSCAPI, 1);
    KeyStore keyStore = KeyStore.getInstance("Windows-MY", sunMSCAPI);
    keyStore.load(null, null);

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algorithm);
    keyManagerFactory.init(keyStore, null);

    // Get a list of key managers
    KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();

    // Walk through the key managers and replace all X509 Key Managers with
    // a specialized wrapped DavMail X509 Key Manager
    for (int i = 0; i < keyManagers.length; i++) {
        KeyManager keyManager = keyManagers[i];
        if (keyManager instanceof X509KeyManager) {
            keyManagers[i] = new DavMailX509KeyManager((X509KeyManager) keyManager);
        }
    }

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, null, null);
    SSLSocketFactory sockFactory = sslContext.getSocketFactory();
    SSLSocket sslSock = (SSLSocket) sockFactory.createSocket("localhost", 443);
    sslSock.startHandshake();

}

From source file:com.ldroid.kwei.common.lib.volley.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from w w w  .ja  v  a2  s .  c om

        // Client should authenticate itself with the valid certificate to
        // Server.
        InputStream clientStream = MainApp.getContext().getResources()
                .openRawResource(R.raw.production_test_client);
        char[] password = "XXXXXXXXXXXXX".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // Client should also add the CA certificate obtained from server
        // and create TrustManager from it for the client to validate the
        // identity of the server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = MainApp.getContext().getResources().openRawResource(R.raw.production_test_ca);

        try {
            trustStore.load(instream, "XXXXXXXX".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager & Keystore
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        return context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:org.reficio.ws.it.util.SslTunnel.java

public void start() {
    try {/*ww w.  j  a v a 2s.  c  o  m*/
        sslContext = SSLContext.getInstance("SSLv3");
        KeyManager[] keyManagers = null;
        TrustManager[] trustManagers = null;

        if (keyStore != null) {
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
            X509KeyManager defaultKeyManager = (X509KeyManager) keyManagerFactory.getKeyManagers()[0];
            keyManagers = new KeyManager[] { defaultKeyManager };

        }
        if (trustStore != null) {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(trustStore);
            X509TrustManager defaultTrustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
            trustManagers = new TrustManager[] { defaultTrustManager };
        }

        sslContext.init(keyManagers, trustManagers, new SecureRandom());

        SSLServerSocketFactory socketFactory = sslContext.getServerSocketFactory();
        socket = socketFactory.createServerSocket();
        socket.setReuseAddress(true);
        socket.bind(new InetSocketAddress(sourcePort));
        new ServerThread(socket, run).start();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

From source file:org.opcfoundation.ua.transport.https.HttpsSettings.java

/**
 * Set keystore as the key manager for a https application.   
 *  //from w ww.  ja  va2 s  .c om
 * @param keystore
 * @param password
 * @throws NoSuchAlgorithmException
 * @throws UnrecoverableKeyException
 * @throws KeyStoreException
 */
public void setKeyStore(KeyStore keystore, String password) throws ServiceResultException {
    try {
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystore, password.toCharArray());
        KeyManager kms[] = kmfactory.getKeyManagers();
        keyManager = kms.length == 0 ? null : (X509KeyManager) kms[0];
    } catch (NoSuchAlgorithmException e) {
        throw new ServiceResultException(e);
    } catch (UnrecoverableKeyException e) {
        throw new ServiceResultException(e);
    } catch (KeyStoreException e) {
        throw new ServiceResultException(e);
    }
}

From source file:org.apache.hadoop.security.ssl.ReloadingX509KeyManager.java

private X509ExtendedKeyManager loadKeyManager() throws GeneralSecurityException, IOException {
    KeyStore keyStore = KeyStore.getInstance(type);
    String keyStorePass;/* w w w.  j ava  2s  .co  m*/
    String keyPass;
    if (passwordFileLocation != null) {
        keyStorePass = FileUtils.readFileToString(passwordFileLocation);
        keyPass = keyStorePass;
    } else {
        keyStorePass = keystorePassword;
        keyPass = keyPassword;
    }
    try (FileInputStream in = new FileInputStream(location)) {
        keyStore.load(in, keyStorePass.toCharArray());
        lastLoadedTimestamp = location.lastModified();
        LOG.debug("Loaded keystore file: " + location);
    }

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(SSLFactory.SSLCERTIFICATE);
    kmf.init(keyStore, keyPass.toCharArray());
    X509ExtendedKeyManager keyManager = null;
    KeyManager[] keyManagers = kmf.getKeyManagers();
    for (KeyManager km : keyManagers) {
        if (km instanceof X509ExtendedKeyManager) {
            keyManager = (X509ExtendedKeyManager) km;
            break;
        }
    }

    return keyManager;
}