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:com.apporiented.hermesftp.utils.SecurityUtil.java

/**
 * Create the security context required for SSL communication.
 * /*from www . jav  a  2s.  c  om*/
 * @param keyStoreFile The name of the keystore file.
 * @param keyStorePassword The password for the keystore.
 * @return The context.
 * @throws FtpConfigException Thrown on error in configuration.
 */
public static SSLContext createSslContext(String keyStoreFile, char[] keyStorePassword)
        throws FtpConfigException {
    SSLContext sslContext;
    try {
        /* Get keystore file and password */
        InputStream ksInputStream = getKeyStoreInputStream(keyStoreFile);

        /*
         * Get the java keystore object an key manager. A keystore is where keys and
         * certificates are kept.
         */
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(ksInputStream, keyStorePassword);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(keystore, keyStorePassword);

        /*
         * An SSLContext is an environment for implementing JSSE. It is used to create a
         * ServerSocketFactory
         */
        sslContext = SSLContext.getInstance("SSL");
        sslContext.init(kmf.getKeyManagers(), null, null);
    } catch (KeyManagementException e) {

        throw new SecurityException("A key management authorization problem occurred.");
    } catch (FileNotFoundException e) {
        throw new SecurityException("The key store file could not be found.");
    } catch (KeyStoreException e) {
        throw new SecurityException("A key store problem occurred.");
    } catch (NoSuchAlgorithmException e) {
        throw new SecurityException("The hash algorithm is not supported.");
    } catch (CertificateException e) {
        throw new SecurityException("Certificate could not be loaded.");
    } catch (UnrecoverableKeyException e) {
        throw new SecurityException("Key store cannot be recovered.");
    } catch (IOException e) {
        throw new SecurityException("Reading the key store failed.");
    }
    return sslContext;
}

From source file:com.android.beyondemail.SSLSocketFactory.java

private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }/*from  ww  w .j  a v a2  s.c  o m*/
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keystore, password != null ? password.toCharArray() : null);
    return kmfactory.getKeyManagers();
}

From source file:org.apache.commons.httpclient.contrib.ssl.AuthSSLProtocolSocketFactory.java

private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }//from w ww . ja  v a2 s . co m
    LOG.debug("Initializing key manager");
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keystore, password != null ? password.toCharArray() : null);
    return kmfactory.getKeyManagers();
}

From source file:nl.nn.adapterframework.http.AuthSSLProtocolSocketFactory.java

private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password, String algorithm)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }/*from ww  w  .  j ava  2  s  .c o  m*/
    log.debug("Initializing key manager");
    if (StringUtils.isEmpty(algorithm)) {
        algorithm = KeyManagerFactory.getDefaultAlgorithm();
        log.debug("using default KeyManager algorithm [" + algorithm + "]");
    } else {
        log.debug("using configured KeyManager algorithm [" + algorithm + "]");
    }
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(algorithm);
    kmfactory.init(keystore, password != null ? password.toCharArray() : null);
    return kmfactory.getKeyManagers();
}

From source file:com.app.mvc.http.ext.AuthSSLProtocolSocketFactory.java

private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    if (keystore == null) {
        throw new IllegalArgumentException("Keystore may not be null");
    }//w  ww  . j av a2 s.  co m
    log.debug("Initializing key manager");
    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keystore, password != null ? password.toCharArray() : null);
    return kmfactory.getKeyManagers();
}

From source file:com.servoy.j2db.util.SecuritySupport.java

public static SSLContext getSSLContext(Properties settings) throws Exception {

    // set up key manager to do server authentication
    SSLContext ctx = SSLContext.getInstance("TLS"); //$NON-NLS-1$
    KeyManagerFactory kmf = null;
    try {// w  w  w  .ja v a 2s  .c  o  m
        kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
    } catch (Exception e) {
        Debug.log("couldn't get SunX509, now trying ibm");
        kmf = KeyManagerFactory.getInstance("IbmX509"); //$NON-NLS-1$
    }

    initKeyStoreAndPassphrase(settings);

    kmf.init(keyStore, passphrase);
    ctx.init(kmf.getKeyManagers(), null, null);

    return ctx;

}

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java

/**
 * Builds an SSLConect that trusts the trust material in the KeyStore
 *
 * @param trustMaterial//from w  w w. j a  v  a  2 s . c  o  m
 * @return
 */
public static SSLContext buildContext(KeyStore trustMaterial) {
    SSLContext ctx;
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustMaterial);

        KeyManagerFactory keyMgr = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyMgr.init(trustMaterial, new char[0]);

        ctx = SSLContext.getInstance("TLS");
        ctx.init(keyMgr.getKeyManagers(), tmf.getTrustManagers(), null);
    } catch (KeyStoreException | UnrecoverableKeyException | KeyManagementException
            | NoSuchAlgorithmException ex) {
        Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
        ctx = null;
    }

    return ctx;
}

From source file:com.cloudbees.tftwoway.Client.java

public static KeyManager[] getKeyManager() throws Exception {
    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    KeyStore store = KeyStore.getInstance("JKS");

    PrivateKey clientKey = loadRSAKey(PRIVATE_KEY);
    X509Certificate clientCert = loadX509Key(CERTIFICATE);

    store.load(null);/*from   w w  w  .  j av a  2  s .c om*/
    store.setKeyEntry("key", clientKey, "123123".toCharArray(), new Certificate[] { clientCert });

    keyManagerFactory.init(store, "123123".toCharArray());

    return keyManagerFactory.getKeyManagers();
}

From source file:com.alphabetbloc.accessmrs.utilities.NetworkUtils.java

public static SSLContext createSslContext() throws GeneralSecurityException, IOException {

    // TrustStore
    KeyStore trustStore = FileUtils.loadSslStore(FileUtils.MY_TRUSTSTORE);
    if (trustStore == null)
        throw new IOException("Access denied. Ensure credential storage is available.");
    MyTrustManager myTrustManager = new MyTrustManager(trustStore);
    TrustManager[] tms = new TrustManager[] { myTrustManager };

    // KeyStore//from   w ww.  jav  a2 s .c o m
    KeyManager[] kms = null;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getApp());
    boolean useClientAuth = prefs.getBoolean(App.getApp().getString(R.string.key_client_auth), false);
    if (useClientAuth) {
        KeyStore keyStore = FileUtils.loadSslStore(FileUtils.MY_KEYSTORE);
        if (keyStore == null)
            throw new IOException("Access denied. Ensure credential storage is available.");
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, EncryptionUtil.getPassword().toCharArray());
        kms = kmf.getKeyManagers();
    }

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(kms, tms, null);
    return context;
}

From source file:org.asynchttpclient.test.TestUtils.java

private static KeyManager[] createKeyManagers() throws GeneralSecurityException, IOException {
    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream keyStoreStream = TestUtils.class.getClassLoader()
            .getResourceAsStream("ssltest-cacerts.jks")) {
        char[] keyStorePassword = "changeit".toCharArray();
        ks.load(keyStoreStream, keyStorePassword);
    }//www .j  a v  a  2  s .c  o m
    assert (ks.size() > 0);

    // Set up key manager factory to use our key store
    char[] certificatePassword = "changeit".toCharArray();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, certificatePassword);

    // Initialize the SSLContext to work with our key managers.
    return kmf.getKeyManagers();
}