Example usage for java.security KeyStore load

List of usage examples for java.security KeyStore load

Introduction

In this page you can find the example usage for java.security KeyStore load.

Prototype

public final void load(InputStream stream, char[] password)
        throws IOException, NoSuchAlgorithmException, CertificateException 

Source Link

Document

Loads this KeyStore from the given input stream.

Usage

From source file:sample.tomcat.SslApplicationTests.java

private SSLSocketFactory secureSocketFactory() throws Exception {
    KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
    truststore.load(getKeyStoreFile(), "password".toCharArray());
    // setup ssl context
    SSLContext ctx = SSLContexts.custom().loadTrustMaterial(truststore)
            .loadKeyMaterial(truststore, "password".toCharArray()).build();
    return ctx.getSocketFactory();
}

From source file:org.ulyssis.ipp.publisher.HttpOutput.java

private SSLContext createSslCustomContext() {
    try {/*w  w  w  .  j  ava  2  s .  co m*/
        SSLContextBuilder builder = SSLContexts.custom();
        if (options.getKeystore().isPresent()) {
            KeyStore cks = KeyStore.getInstance(KeyStore.getDefaultType());
            cks.load(new FileInputStream(options.getKeystore().get().toFile()),
                    options.getKeystorePass().toCharArray());
            builder.loadKeyMaterial(cks, options.getKeystorePass().toCharArray());
        }

        if (options.getTruststore().isPresent()) {
            KeyStore tks = KeyStore.getInstance(KeyStore.getDefaultType());
            tks.load(new FileInputStream(options.getTruststore().get().toFile()),
                    options.getTruststorePass().toCharArray());
            builder.loadTrustMaterial(tks, new TrustSelfSignedStrategy());
        }

        if (!options.getKeystore().isPresent() && !options.getKeystore().isPresent()) {
            return SSLContext.getDefault();
        }

        return builder.build();
    } catch (Exception e) {
        // TODO: DO SOMETHING WITH THE EXCEPTION!
        LOG.error("Exception", e);
    }
    return null;
}

From source file:net.jsign.PESignerTest.java

private KeyStore getKeyStore() throws Exception {
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream("target/test-classes/keystore.jks"), "password".toCharArray());
    return keystore;
}

From source file:com.microsoft.office.core.auth.AbstractAuthenticationFactory.java

/**
 * Creates HttpClient instance for given method and URI.
 *
 * @param method Http method./*www  .  ja v  a2s  . co  m*/
 * @param uri Target URI.
 * @return HttpClient instance prepared to make request.
 */
@SuppressWarnings("deprecation")
public HttpClient createHttpClient(HttpMethod method, URI uri) {
    HttpClient httpclient = super.createHttpClient(method, uri);

    final IAuthenticator creds = Configuration.getAuthenticator();
    if (creds != null) {
        creds.prepareClient(httpclient);
    }

    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getConnectionTimeout());
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, getSocketTimeout());

    if (Configuration.isTrustAll()) {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactory sf = new TrustAllSSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry);
            httpclient = new DefaultHttpClient(ccm, httpclient.getParams());
        } catch (Exception e) {
        }
    }

    return httpclient;
}

From source file:cloud.google.oauth2.MyWayAuthentication.java

/**
 * Load p12 file get private key/*from ww  w .  j a v  a  2  s. co  m*/
 * */
private PrivateKey getPrivateKey(String keyFile, String password) throws KeyStoreException, IOException,
        NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
    KeyStore keystore = KeyStore.getInstance("PKCS12");
    keystore.load(new FileInputStream(keyFile), password.toCharArray());
    PrivateKey privateKey = (PrivateKey) keystore.getKey(GCDStatic.getKeyAlias(), password.toCharArray());

    return privateKey;
}

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

private KeyStore getKeyStore(String storeType, String storePath, String storePassword) throws Exception {
    InputStream keyStoreInput = FileUtils.openInputStream(new File(storePath));

    try {//  ww  w  .ja va  2  s  . co  m
        KeyStore keyStore = KeyStore.getInstance(storeType);
        keyStore.load(keyStoreInput, storePassword.toCharArray());

        return keyStore;
    } finally {
        IOUtils.closeQuietly(keyStoreInput);
    }
}

From source file:com.kixeye.kixmpp.client.KixmppClientTest.java

private SslContext createSslContext() throws Exception {
    Certificate cert;/*from   w  w w. j  a va 2 s . c  o  m*/

    try (InputStream certStream = this.getClass().getResourceAsStream("/bogus_mina_tls.cert")) {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(certStream, "boguspw".toCharArray());
        cert = ks.getCertificate("bogus");
    }

    File certFile = File.createTempFile(UUID.randomUUID().toString().replace("-", ""), null);
    FileOutputStream certFileOutputStream = new FileOutputStream(certFile);
    IOUtils.copy(new StringReader("-----BEGIN CERTIFICATE-----\n"), certFileOutputStream);
    IOUtils.copy(new ByteArrayInputStream(Base64.encodeBase64(cert.getEncoded())), certFileOutputStream);
    IOUtils.copy(new StringReader("\n-----END CERTIFICATE-----"), certFileOutputStream);
    certFileOutputStream.close();

    return SslContext.newClientContext(certFile);
}

From source file:com.lyndir.lhunath.opal.network.SSLFactory.java

private SSLFactory(final File keyStore, final String password) {

    try (InputStream keyStoreStream = new FileInputStream(keyStore)) {
        KeyStore store = KeyStore.getInstance("JKS");
        store.load(keyStoreStream, password.toCharArray());

        TrustManagerFactory tFactory = TrustManagerFactory.getInstance("SunX509");
        tFactory.init(store);/*from   w ww  .j a  va  2  s .c o  m*/

        context = SSLContext.getInstance("TLS");
        context.init(null, tFactory.getTrustManagers(), null);
    } catch (final KeyStoreException e) {
        throw new IllegalArgumentException(
                "Keystore type not supported or keystore could not be used to initialize trust.", e);
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalStateException("Key algorithm not supported.", e);
    } catch (final CertificateException e) {
        throw new IllegalArgumentException("Keystore could not be loaded.", e);
    } catch (final FileNotFoundException e) {
        throw new IllegalArgumentException("Keystore not found.", e);
    } catch (final IOException e) {
        throw new RuntimeException("Could not read the keys from the keystore.", e);
    } catch (final KeyManagementException e) {
        throw new RuntimeException("Could not use the keys for trust.", e);
    }
}

From source file:com.wso2telco.gsma.shorten.BitlyUrlShorten.java

/**
 * Gets the new http client.//from   w  w w .ja  v a2s  . c o m
 *
 * @return the new http client
 */
@SuppressWarnings("deprecation")
public CloseableHttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        org.apache.http.conn.ssl.SSLSocketFactory sf = new SSLSocket(trustStore);
        sf.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:es.uja.photofirma.android.DoConnection.java

/**
 * //w  w w  . ja  va2s  .  c  o m
 * @return DefaultHttpClient(ccm, params)
 */
public HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        //aado timeout
        HttpConnectionParams.setConnectionTimeout(params, 6000); //timeout en establecer conexion
        HttpConnectionParams.setSoTimeout(params, 10000); //timeout en recibir respuesta

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);

    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}