Example usage for javax.net.ssl TrustManagerFactory init

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

Introduction

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

Prototype

public final void init(ManagerFactoryParameters spec) throws InvalidAlgorithmParameterException 

Source Link

Document

Initializes this factory with a source of provider-specific trust material.

Usage

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

private TrustManager[] createTrustManagers(final KeyStore trustStore)
        throws NoSuchAlgorithmException, KeyStoreException {
    if (trustStore == null) {
        LOG.log(Level.SEVERE, "Creating SSL socket but trust store is null");
        throw new IllegalArgumentException("TrustStore cannot be null");
    }//from w w  w. j  a v a 2 s  . c  o  m
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustStore);
    return tmf.getTrustManagers();
}

From source file:org.hyperic.util.security.DefaultSSLProviderImpl.java

private TrustManagerFactory getTrustManagerFactory(final KeyStore keystore)
        throws KeyStoreException, IOException {
    try {/*from  w w  w.  j  a v a 2  s  . com*/
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keystore);
        return trustManagerFactory;
    } catch (NoSuchAlgorithmException e) {
        // no support for algorithm, if this happens we're kind of screwed
        // we're using the default so it should never happen
        log.error("The algorithm is not supported: " + e, e);
        throw new KeyStoreException(e);
    }
}

From source file:org.opendaylight.aaa.cert.impl.CertificateManagerService.java

@Override
public SSLContext getServerContext() {
    String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
    if (algorithm == null) {
        algorithm = "SunX509";
    }/*from  ww w  . ja  v  a2  s.com*/
    SSLContext serverContext = null;
    try {
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
        kmf.init(aaaCertProvider.getODLKeyStore(),
                aaaCertProvider.getOdlKeyStoreInfo().getStorePassword().toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
        tmf.init(aaaCertProvider.getTrustKeyStore());

        serverContext = SSLContext.getInstance(KeyStoreConstant.TLS_PROTOCOL);
        serverContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    } catch (final NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException
            | KeyManagementException e) {
        LOG.error("Error while creating SSLContext ", e);
    }
    return serverContext;
}

From source file:org.projectforge.business.ldap.MyTrustManager.java

public MyTrustManager() {
    try {//from w ww .j av  a 2s .  co  m
        final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        // create a TrustManager using our KeyStore
        final TrustManagerFactory factory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        factory.init(keyStore);
        this.trustManager = getX509TrustManager(factory.getTrustManagers());
    } catch (final KeyStoreException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final NoSuchAlgorithmException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final CertificateException ex) {
        log.error("Exception encountered " + ex, ex);
    } catch (final IOException ex) {
        log.error("Exception encountered " + ex, ex);
    }
}

From source file:org.keycloak.truststore.JSSETruststoreConfigurator.java

public TrustManager[] getTrustManagers() {
    if (provider == null) {
        return null;
    }/*from   w ww .j  av  a  2s.co  m*/

    if (tm == null) {
        synchronized (this) {
            if (tm == null) {
                TrustManagerFactory tmf = null;
                try {
                    tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                    tmf.init(provider.getTruststore());
                    tm = tmf.getTrustManagers();
                } catch (Exception e) {
                    throw new RuntimeException("Failed to initialize TrustManager: ", e);
                }
            }
        }
    }
    return tm;
}

From source file:ddf.security.settings.impl.SecuritySettingsServiceImpl.java

@Override
public TLSClientParameters getTLSParameters() {
    TLSClientParameters tlsParams = new TLSClientParameters();
    try {/*from  ww  w.j av a 2  s  .  com*/
        TrustManagerFactory trustFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustFactory.init(trustStore);
        TrustManager[] tm = trustFactory.getTrustManagers();
        tlsParams.setTrustManagers(tm);

        KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyFactory.init(keyStore, keystorePassword.toCharArray());
        KeyManager[] km = keyFactory.getKeyManagers();
        tlsParams.setKeyManagers(km);
    } catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException e) {
        LOGGER.warn(
                "Could not fully load keystore/truststore into TLSParameters. Parameters may not be fully functional.",
                e);
    }

    FiltersType filter = new FiltersType();
    filter.getInclude().addAll(SSL_ALLOWED_ALGORITHMS);
    filter.getExclude().addAll(SSL_DISALLOWED_ALGORITHMS);
    tlsParams.setCipherSuitesFilter(filter);

    return tlsParams;
}

From source file:jp.pigumer.mqtt.Client.java

Optional<TrustManager[]> initTrustManagers() {
    return loadKeyStore().map(keyStore -> {
        try {// w ww. j  ava 2s .com
            Security.addProvider(new BouncyCastleProvider());
            TrustManagerFactory tmf = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(keyStore);
            return tmf.getTrustManagers();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "failed load", e);
            return null;
        }
    });
}

From source file:org.apache.streams.cassandra.CassandraClient.java

public void start() throws Exception {

    Objects.nonNull(config);/*from  w  w  w  . j a v a2 s . c  o  m*/

    LOGGER.info("CassandraClient.start {}", config);

    Cluster.Builder builder = Cluster.builder().withPort(config.getPort().intValue()).withoutJMXReporting()
            .withoutMetrics()
            .withSocketOptions(new SocketOptions().setConnectTimeoutMillis(DEFAULT_CONNECT_TIMEOUT_MILLIS * 10)
                    .setReadTimeoutMillis(DEFAULT_READ_TIMEOUT_MILLIS * 10));

    if (config.getSsl() != null && config.getSsl().getEnabled() == true) {

        Ssl ssl = config.getSsl();

        KeyStore ks = KeyStore.getInstance("JKS");

        InputStream trustStore = new FileInputStream(ssl.getTrustStore());
        ks.load(trustStore, ssl.getTrustStorePassword().toCharArray());
        InputStream keyStore = new FileInputStream(ssl.getKeyStore());
        ks.load(keyStore, ssl.getKeyStorePassword().toCharArray());

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

        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, ssl.getKeyStorePassword().toCharArray());

        SSLContext sslContext = SSLContext.getInstance("SSLv3");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        SSLOptions sslOptions = JdkSSLOptions.builder().withSSLContext(sslContext).build();

        builder = builder.withSSL(sslOptions);
    }

    Collection<InetSocketAddress> addresses = new ArrayList<>();
    for (String h : config.getHosts()) {
        LOGGER.info("Adding Host: {}", h);
        InetSocketAddress socketAddress = new InetSocketAddress(h, config.getPort().intValue());
        addresses.add(socketAddress);
    }
    builder.addContactPointsWithPorts(addresses);

    if (StringUtils.isNotBlank(config.getUser()) && StringUtils.isNotBlank(config.getPassword())) {
        builder.withCredentials(config.getUser(), config.getPassword());
    }
    cluster = builder.build();

    Objects.nonNull(cluster);

    try {
        Metadata metadata = cluster.getMetadata();
        LOGGER.info("Connected to cluster: {}\n", metadata.getClusterName());
        for (Host host : metadata.getAllHosts()) {
            LOGGER.info("Datacenter: {}; Host: {}; Rack: {}\n", host.getDatacenter(), host.getAddress(),
                    host.getRack());
        }
    } catch (Exception e) {
        LOGGER.error("Exception: {}", e);
        throw e;
    }

    try {
        session = cluster.connect();
    } catch (Exception e) {
        LOGGER.error("Exception: {}", e);
        throw e;
    }

    Objects.nonNull(session);

}

From source file:com.qpark.eip.core.spring.security.https.EipX509TrustManager.java

/**
 * Initialize.//from   w  w  w  .ja  v a2s . c om
 * 
 * @throws Exception
 */
@PostConstruct
public void init() throws Exception {
    // create a "default" JSSE X509TrustManager.
    this.ks = KeyStore.getInstance("JKS");
    if (this.keystore != null) {
        this.ks.load(this.keystore.getInputStream(), this.keystorePassword);
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
    tmf.init(this.ks);

    TrustManager tms[] = tmf.getTrustManagers();

    /*
     * Iterate over the returned trust managers, look for an instance of
     * X509TrustManager. If found, use that as our "default" trust manager.
     */
    for (TrustManager tm : tms) {
        if (tm instanceof X509TrustManager) {
            this.sunJSSEX509TrustManager = (X509TrustManager) tm;
            return;
        }
    }

    /*
     * Find some other way to initialize, or else we have to fail the
     * constructor.
     */
    throw new Exception("Couldn't initialize");
}

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 {/*from  w w w. j a  v  a2s  . com*/
        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);
    }
}