Example usage for javax.net.ssl KeyManagerFactory init

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

Introduction

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

Prototype

public final void init(KeyStore ks, char[] password)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException 

Source Link

Document

Initializes this factory with a source of key material.

Usage

From source file:org.kuali.mobility.push.factory.iOSFeedbackConnectionFactory.java

@Override
public SSLSocket makeObject() throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(certPath.getInputStream(), certPassword.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
    keyManagerFactory.init(keyStore, certPassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
    trustManagerFactory.init(keyStore);/*from   ww w.  ja  va2s  .c  om*/
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.startHandshake();
    return socket;
}

From source file:org.kuali.mobility.push.factory.iOSConnectionFactory.java

@Override
public SSLSocket makeObject() throws Exception {
    SSLSocket socket = null;/*ww w.j  a  v a2  s. c  om*/
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(certPath.getInputStream(), certPassword.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
    keyManagerFactory.init(keyStore, certPassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
    trustManagerFactory.init(keyStore);
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
    socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.startHandshake();
    return socket;
}

From source file:com.terradue.dsi.wire.KeyManagerProvider.java

@Override
public KeyManager[] get() {
    final char[] password = this.password.toCharArray();

    try {/*from w  w w . j  av  a  2  s.c  o m*/
        final KeyStore store = new KeyMaterial(certificate, certificate, password).getKeyStore();
        store.load(null, password);

        // initialize key and trust managers -> default behavior
        final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        // password for key and store have to be the same IIRC
        keyManagerFactory.init(store, password);

        return keyManagerFactory.getKeyManagers();
    } catch (Exception e) {
        throw new ProvisionException("Impossible to initialize SSL certificate/key", e);
    }
}

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);/*ww w.  ja  v  a 2 s.c  o  m*/
        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:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java

/**
 * Ssl context./*www .  j  av a 2s .  c  om*/
 *
 * @return the SSL context
 * @throws Exception
 *           the exception
 */
@Bean(name = SERVER_SSL_CONTEXT)
public SSLContext sslContext() throws Exception {
    char[] password = HARDCODED_NSA_APPROVED_PASSWORD.toCharArray();

    KeyStore keystore = getKeyStore();
    keystore.load(getResource(JKS_LOCATION), password);

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

    kmf.init(keystore, password);

    return createContext(keystore, kmf);
}

From source file:org.xdi.net.SslDefaultHttpClient.java

private KeyManager[] getKeyManagers() throws Exception {
    KeyStore keyStore = getKeyStore(this.keyStoreType, this.keyStorePath, this.keyStorePassword);

    KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmFactory.init(keyStore, this.keyStorePassword.toCharArray());

    return kmFactory.getKeyManagers();
}

From source file:com.linkedin.pinot.common.utils.ClientSSLContextGenerator.java

private KeyManager[] setupKeyManagers() {
    if (_keyStoreFile == null) {
        return null;
    }//from  ww w  . j a v a2s . c o m
    try {
        KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
        LOGGER.info("Setting up keystore with file {}", _keyStoreFile);
        keyStore.load(new FileInputStream(new File(_keyStoreFile)), _keyStorePassword.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYMANAGER_FACTORY_ALGORITHM);
        kmf.init(keyStore, _keyStorePassword.toCharArray());
        LOGGER.info("Successfully initialized keystore");
        return kmf.getKeyManagers();
    } catch (Exception e) {
        Utils.rethrowException(e);
    }
    return null;
}

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.j a v 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 {// w ww.j a  va 2s. c  o  m
        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.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//w  w w.ja  v a 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);
}