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.github.restdriver.clientdriver.integration.SecureClientDriverTest.java

static KeyStore getKeystore()
        throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
    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:com.google.jenkins.plugins.credentials.oauth.P12ServiceAccountConfigTestUtil.java

private static KeyStore createKeyStore(KeyPair keyPair) throws KeyStoreException, CertificateException,
        NoSuchAlgorithmException, IOException, OperatorCreationException, NoSuchProviderException {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(null, null);
    keyStore.setKeyEntry(DEFAULT_P12_ALIAS, keyPair.getPrivate(), DEFAULT_P12_SECRET.toCharArray(),
            new Certificate[] { generateCertificate(keyPair) });
    return keyStore;
}

From source file:fi.iki.dezgeg.matkakorttiwidget.matkakortti.NonverifyingSSLSocketFactory.java

public static AbstractHttpClient createNonverifyingHttpClient() throws Exception {

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);

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

From source file:org.elasticsearch.client.RestClientBuilderIntegTests.java

private static SSLContext getSslContext() throws Exception {
    SSLContext sslContext = SSLContext.getInstance("TLS");
    try (InputStream in = RestClientBuilderIntegTests.class.getResourceAsStream("/testks.jks")) {
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(in, "password".toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(keyStore, "password".toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(keyStore);/*  ww w .ja  v  a  2  s.  com*/
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    }
    return sslContext;
}

From source file:com.github.restdriver.clientdriver.integration.SecureClientDriverRuleTest.java

private static KeyStore getKeystore() {
    try {//from   www.  j  a  v  a2 s  .  c  om
        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;
    } catch (Exception e) {
        throw new ClientDriverSetupException("Key store could not be loaded.", e);
    }
}

From source file:org.cvasilak.jboss.mobile.admin.net.ssl.CustomHTTPClient.java

public static synchronized AbstractHttpClient getHttpClient() {
    try {// ww  w .j  a v a  2  s . c  om
        if (client == null) {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

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

            client = new DefaultHttpClient(ccm, params);
        }
    } catch (Exception e) {
        Log.d(TAG, "unable to create http client", e);
    }

    return client;
}

From source file:org.changhong.sync.web.MySSLSocketFactory.java

public static DefaultHttpClient getNewHttpClient() {
    try {// w  ww .j a  v  a  2  s  .c  om
        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);

        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.cloud.utils.security.CertificateHelper.java

public static byte[] buildAndSaveKeystore(List<Ternary<String, String, String>> certs, String storePassword)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        InvalidKeySpecException {
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, storePassword != null ? storePassword.toCharArray() : null);

    //name,cert,key
    for (Ternary<String, String, String> cert : certs) {
        if (cert.third() == null) {
            Certificate c = buildCertificate(cert.second());
            ks.setCertificateEntry(cert.first(), c);
        } else {/*from  ww w .ja  v a2  s . c  o  m*/
            Certificate[] c = new Certificate[certs.size()];
            int i = certs.size();
            for (Ternary<String, String, String> ct : certs) {
                c[i - 1] = buildCertificate(ct.second());
                i--;
            }
            ks.setKeyEntry(cert.first(), buildPrivateKey(cert.third()),
                    storePassword != null ? storePassword.toCharArray() : null, c);
        }
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ks.store(os, storePassword != null ? storePassword.toCharArray() : null);
    os.close();
    return os.toByteArray();
}

From source file:org.qi4j.library.http.AbstractSecureJettyTest.java

@BeforeClass
public static void beforeSecureClass() throws IOException, GeneralSecurityException {
    defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String string, SSLSession ssls) {
            return true;
        }//  w ww. j a  v  a 2 s  .  co m

    });
    KeyStore truststore = KeyStore.getInstance("JCEKS");
    truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance(getX509Algorithm());
    caTrustManagerFactory.init(truststore);
    sslCtx.init(null, caTrustManagerFactory.getTrustManagers(), null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}

From source file:com.pieframework.runtime.utils.CertificateUtils.java

public static X509Certificate getCertificate(File certificateFile, String pass, String certAlias) {
    X509Certificate certificate = null;

    try {/*from  w ww. j a  v a  2s  . c o m*/
        FileInputStream cert = new FileInputStream(certificateFile);
        KeyStore pfxStore = KeyStore.getInstance("pkcs12");
        pfxStore.load(cert, pass.toCharArray());
        if (StringUtils.empty(certAlias) && pfxStore.size() > 0) {
            certAlias = pfxStore.aliases().nextElement();
        }
        certificate = (X509Certificate) pfxStore.getCertificate(certAlias);
        cert.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return certificate;
}