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:com.prey.net.HttpUtils.java

public static HttpClient getNewHttpClient() {
    try {//from  w  w  w. j  a  v a2 s  .  co m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_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:com.android.providers.downloads.ui.network.SslSocketFactory.java

private static KeyStore createKeyStore(InputStream keyStore, String password)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException {
    KeyStore key = KeyStore.getInstance(KeyStore.getDefaultType());
    try {/*from   w w  w.j a v a  2  s  . c o  m*/
        key.load(keyStore, password.toCharArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        try {
            key.load(null, null);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    return key;
}

From source file:Main.java

public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException,
        KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    InputStream caInput = new BufferedInputStream(stream);
    Certificate ca;/*www. java 2s  .c  o  m*/
    try {
        ca = cf.generateCertificate(caInput);
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        try {
            caInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    return context.getSocketFactory();
}

From source file:ru.elifantiev.yandex.SSLHttpClientFactory.java

public static HttpClient getNewHttpClient() {
    try {//from w w w  .  j  a  v a2  s .  c  o  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new YandexSSLSocketFactory(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);

        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:com.github.restdriver.clientdriver.unit.SecureClientDriverFactoryTest.java

static KeyStore getKeystore() throws Exception {
    ClassLoader loader = SecureClientDriverTest.class.getClassLoader();
    byte[] binaryContent = IOUtils.toByteArray(loader.getResourceAsStream("keystore.jks"));
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new ByteArrayInputStream(binaryContent), "password".toCharArray());
    return keyStore;
}

From source file:android.apn.androidpn.server.xmpp.ssl.SSLTrustManagerFactory.java

public static TrustManager[] getTrustManagers(String storeType, String truststore, String trustpass)
        throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException {
    TrustManager[] trustManagers;
    if (truststore == null) {
        trustManagers = null;//  w  w w .j  av a 2s.  c om
    } else {
        TrustManagerFactory trustFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        if (trustpass == null) {
            trustpass = "";
        }
        KeyStore keyStore = KeyStore.getInstance(storeType);
        keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
        trustFactory.init(keyStore);
        trustManagers = trustFactory.getTrustManagers();
    }
    return trustManagers;
}

From source file:cn.keke.travelmix.EasySSLSocketFactory.java

private static KeyStore getEasyTrustStore()
        throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);
    return trustStore;
}

From source file:com.cloud.utils.security.CertificateHelper.java

public static KeyStore loadKeystore(byte[] ksData, String storePassword)
        throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
    assert (ksData != null);
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(new ByteArrayInputStream(ksData), storePassword != null ? storePassword.toCharArray() : null);

    return ks;//  ww w.j a v  a  2  s . co m
}

From source file:com.cloud.utils.security.CertificateHelper.java

public static KeyStore buildKeystore(String alias, String cert, String privateKey, String storePassword)
        throws KeyStoreException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException,
        IOException {/*from  w  w w . j a  v a2  s  .co m*/

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, storePassword != null ? storePassword.toCharArray() : null);
    Certificate[] certs = new Certificate[1];
    certs[0] = buildCertificate(cert);
    ks.setKeyEntry(alias, buildPrivateKey(privateKey),
            storePassword != null ? storePassword.toCharArray() : null, certs);
    return ks;
}

From source file:br.gov.frameworkdemoiselle.behave.integration.alm.httpsclient.HttpsClient.java

public static HttpClient getNewHttpClient(String encoding) {
    try {//from ww  w  .j ava 2s .c om
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

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

        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();
    }
}