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:org.elasticsearch.xpack.ssl.SSLClientAuthTests.java

private SSLContext getSSLContext() {
    try (InputStream in = Files.newInputStream(
            getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks"))) {
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(in, "testclient".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);/*from  ww  w .j  a va  2  s.c om*/
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, "testclient".toCharArray());
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
        return context;
    } catch (Exception e) {
        throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e);
    }
}

From source file:org.jboss.test.syslog.TLSSyslogServer.java

/**
 * Creates custom sslContext from keystore and truststore configured in
 *
 * @see org.productivity.java.syslog4j.server.impl.net.tcp.TCPNetSyslogServer#initialize()
 *//*from w ww.  jav  a 2  s . c  o m*/
@Override
public void initialize() throws SyslogRuntimeException {
    super.initialize();

    try {
        final KeyStore keystore = KeyStore.getInstance("JKS");
        final InputStream is = getClass().getResourceAsStream("/server.keystore");
        if (is == null) {
            System.err.println("Server keystore not found.");
        }
        final char[] keystorePwd = "123456".toCharArray();
        try {
            keystore.load(is, keystorePwd);
        } finally {
            IOUtils.closeQuietly(is);
        }

        final KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, keystorePwd);

        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(),
                new TrustManager[] { new TrustEveryoneTrustManager() }, null);
    } catch (Exception e) {
        System.err.println("Exception occured during SSLContext for TLS syslog server initialization");
        e.printStackTrace();
        throw new SyslogRuntimeException(e);
    }
}

From source file:eu.nullbyte.android.urllib.CertPinningSSLSocketFactory.java

private SSLContext createSSLContext() throws IOException {
    //Log.v(TAG, "createSSLContext()");
    try {/*from  w  w  w  .j  av a2  s  .  c  om*/
        SSLContext context = SSLContext.getInstance("TLS");
        mTrustManager = new CertPinningTrustManager(certificates, lastHost);
        KeyManager[] keyManagers = null;
        if (mClientCertificate != null) {
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(mClientCertificate.getKeyStore(), mClientCertificate.getPassword().toCharArray());
            keyManagers = kmf.getKeyManagers();
        }
        context.init(keyManagers, new TrustManager[] { mTrustManager }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.collabnet.tracker.common.httpClient.SslProtocolSocketFactory.java

private SslProtocolSocketFactory() {
    KeyManager[] keymanagers = null;
    if (System.getProperty(KEY_STORE) != null && System.getProperty(KEY_STORE_PASSWORD) != null) {
        try {/*from  w w w  .ja v a2 s. c om*/
            String type = System.getProperty(KEY_STORE_TYPE, KeyStore.getDefaultType());
            KeyStore keyStore = KeyStore.getInstance(type);
            char[] password = System.getProperty(KEY_STORE_PASSWORD).toCharArray();
            FileInputStream keyStoreInputStream = new FileInputStream(System.getProperty(KEY_STORE));
            keyStore.load(keyStoreInputStream, password);
            keyStoreInputStream.close();
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, password);
            keymanagers = keyManagerFactory.getKeyManagers();
        } catch (Exception e) {
            log(0, "Could not initialize keystore", e);
        }
    }

    hasKeyManager = keymanagers != null;

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(keymanagers, new TrustManager[] { new TrustAllTrustManager() }, null);
        this.socketFactory = sslContext.getSocketFactory();
    } catch (Exception e) {
        log(0, "Could not initialize SSL context", e);
    }
}

From source file:io.hops.hopsworks.api.util.CustomSSLProtocolSocketFactory.java

private KeyManager[] createKeyManagers(final KeyStore keyStore, final String keyPassword)
        throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException {
    if (keyStore == null) {
        LOG.log(Level.SEVERE, "Creating SSL socket but key store is null");
        throw new IllegalArgumentException("KeyStore cannot be null");
    }/*from  w w w.ja  v a 2 s.co m*/
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, keyPassword != null ? keyPassword.toCharArray() : null);

    return kmf.getKeyManagers();
}

From source file:com.youTransactor.uCube.mdm.MDMManager.java

public void initialize(Context context) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

    onSharedPreferenceChanged(settings, null);

    settings.registerOnSharedPreferenceChangeListener(this);

    try {/*w  w  w . jav a2s .  co m*/
        KeyStore keystoreCA = KeyStore.getInstance(KEYSTORE_TYPE);
        keystoreCA.load(context.getResources().openRawResource(R.raw.keystore), PWD);

        KeyStore keystoreClient = null;

        File file = context.getFileStreamPath(KEYSTORE_CLIENT_FILENAME);

        if (file.exists()) {
            keystoreClient = KeyStore.getInstance(KEYSTORE_TYPE);
            InputStream in = new FileInputStream(file);
            keystoreClient.load(in, PWD);
        }

        ready = keystoreClient != null && keystoreClient.getKey(MDM_CLIENT_CERT_ALIAS, PWD) != null;

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keystoreCA);

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
        kmf.init(keystoreClient, PWD);

        sslContext = SSLContext.getInstance("TLS");

        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    } catch (Exception e) {
        LogManager.debug(MDMManager.class.getSimpleName(), "load keystore error", e);
    }
}

From source file:com.msopentech.thali.utilities.universal.HttpKeySSLSocketFactory.java

public HttpKeySSLSocketFactory(final PublicKey serverPublicKey, final KeyStore clientKeyStore,
        final char[] clientPassPhrase)
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    super((KeyStore) null);

    final ThaliPublicKeyComparer thaliPublicKeyComparer = serverPublicKey == null ? null
            : new ThaliPublicKeyComparer(serverPublicKey);

    TrustManager trustManager = new X509TrustManager() {
        @Override/*from   w w  w  .j ava 2 s .  c o  m*/
        public void checkClientTrusted(X509Certificate[] x509Certificates, String authType)
                throws CertificateException {
            throw new RuntimeException(
                    "We should not have gotten a client trusted call, authType was:" + authType);
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String authType)
                throws CertificateException {
            //TODO: We actually need to restrict authTypes to known secure ones
            if (serverPublicKey == null) {
                return;
            }
            PublicKey rootPublicKey = x509Certificates[x509Certificates.length - 1].getPublicKey();
            if (thaliPublicKeyComparer.KeysEqual(rootPublicKey) == false) {
                throw new RuntimeException("Presented server root key does not match expected server root key");
            }
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(clientKeyStore, clientPassPhrase);

    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagerFactory.getKeyManagers(), new TrustManager[] { trustManager },
            new SecureRandom());
    this.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}

From source file:ucar.httpservices.CustomSSLProtocolSocketFactory.java

private SSLContext trustedauthentication(HttpParams params) throws Exception {
    String keypath = null;//from w ww.j a  v a  2  s.com
    String keypassword = null;
    String trustpath = null;
    String trustpassword = null;
    HTTPSSLProvider provider = null;
    if (params == null)
        return null;
    Object o = params.getParameter(HTTPAuthPolicy.PROVIDER);
    if (o == null)
        return null;
    if (!(o instanceof HTTPSSLProvider))
        throw new HTTPException("CustomSSLProtocolSocketFactory: provide is not SSL provider");
    provider = (HTTPSSLProvider) o;
    keypath = provider.getKeystore();
    keypassword = provider.getKeypassword();
    trustpath = provider.getTruststore();
    trustpassword = provider.getTrustpassword();

    TrustManager[] trustmanagers = null;
    KeyManager[] keymanagers = null;

    KeyStore keystore = buildstore(keypath, keypassword, "key");
    if (keystore != null) {
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance("SunX509");
        kmfactory.init(keystore, keypassword.toCharArray());
        keymanagers = kmfactory.getKeyManagers();
    }
    KeyStore truststore = buildstore(trustpath, trustpassword, "trust");
    if (truststore != null) {
        //todo: TrustManagerFactory trfactory = TrustManagerFactory.getInstance("SunX509");
        //trfactory.init(truststore, trustpassword.toCharArray());
        //trustmanagers = trfactory.getTrustManagers();
        trustmanagers = new TrustManager[] { new CustomX509TrustManager(truststore) };
    }
    if (trustmanagers == null)
        trustmanagers = new TrustManager[] { new CustomX509TrustManager(null) };

    SSLContext sslcontext = SSLContext.getInstance("TSL");
    sslcontext.init(keymanagers, trustmanagers, null);
    return sslcontext;
}

From source file:com.mgmtp.perfload.core.client.web.ssl.LtSSLSocketFactory.java

private KeyManager[] createKeyManagers(final KeyStore keyStore, final char[] password)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    log.debug("Initializing key managers");

    KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmfactory.init(keyStore, password);/*  w w w.j a v  a 2s .  c  o m*/

    return kmfactory.getKeyManagers();
}

From source file:org.eclipse.mylyn.internal.commons.net.PollingSslProtocolSocketFactory.java

private KeyManager[] openKeyStore(String type) throws KeyStoreException, IOException, NoSuchAlgorithmException,
        CertificateException, FileNotFoundException, UnrecoverableKeyException {
    KeyStore keyStore = KeyStore.getInstance(type);
    char[] password = keyStorePassword.toCharArray();
    keyStore.load(new FileInputStream(keyStoreFileName), password);
    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password);
    return keyManagerFactory.getKeyManagers();
}