Example usage for java.security KeyStore getInstance

List of usage examples for java.security KeyStore getInstance

Introduction

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

Prototype

public static KeyStore getInstance(String type) throws KeyStoreException 

Source Link

Document

Returns a keystore object of the specified type.

Usage

From source file:com.weitaomi.systemconfig.wechat.ClientCustomSSL.java

public static String connectKeyStore(String url, String xml, String path, int flag) throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    File file = LoadFileFactory.getFile(path);
    char[] arr = null;
    if (flag == 0) {
        arr = WechatConfig.MCHID.toCharArray();
    }/*from  w  w w.j  av  a2s . c  om*/
    if (flag == 1) {
        arr = WechatConfig.MCHID_OFFICIAL.toCharArray();
    }
    FileInputStream instream = new FileInputStream(file);
    try {
        keyStore.load(instream, arr);
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, arr).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    StringEntity entityRequest = new StringEntity(xml, "utf-8");
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entityRequest);
    //        httpPost.setHeader("Content-Type", "application/json");//; charset=utf-8
    HttpResponse response = httpclient.execute(httpPost);

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new RuntimeException("");
    }
    HttpEntity resEntity = response.getEntity();
    InputStream inputStream = resEntity.getContent();
    return HttpRequestUtils.readInstream(inputStream, "UTF-8");
}

From source file:com.sh.util.SslHttpClientFactoryBean.java

@Override
public HttpClient getObject() throws Exception {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustStrategy allTrust = new TrustStrategy() {
        @Override//w w w. j av a  2  s.c o m
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    };
    SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
            SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

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

    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.exem.flamingo.shared.util.SslHttpClientFactoryBean.java

@Override
public HttpClient getObject() throws Exception {
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustStrategy allTrust = new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }//from w  ww . j ava 2 s.c  om
    };
    SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
            SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

From source file:com.thoughtworks.go.security.KeyStoreManager.java

public void storeX509Certificate(String friendlyName, File storeFile, String passwd, Registration entry)
        throws Exception {
    lazyLoadedStore = KeyStore.getInstance(KEYSTORE_TYPE);
    loadStore(lazyLoadedStore);//from w w  w  .  j a  v a  2s.c  om
    storeCertificate(friendlyName, storeFile, passwd, entry);
}

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 w w w  . ja v a2s .c  om
            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.xacml4j.opensaml.KeyStoreFactory.java

@Override
public KeyStore getObject()
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
    KeyStore ks = KeyStore.getInstance(ksType);
    ks.load(ksLocation.getInputStream(), ksPassword.toCharArray());
    return ks;/*w  ww  .j a v  a  2s  .co  m*/
}

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.github.jmkgreen.keystore.mongo.KeyStoreRest.java

@GET
@Path("create-new-keystore")
public void createNewKeyStore(@QueryParam("name") String name, @QueryParam("password") String password)
        throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {
    KeyStore store = KeyStore.getInstance("JCEKS");
    store.load(null, password.toCharArray());
    keyStoreRepository.save(name, password.toCharArray(), store);
}

From source file:info.guardianproject.cacert.CustomTrust.java

public CustomTrust(Context context, int rawResource, String password) throws IOException, KeyStoreException,
        KeyManagementException, NoSuchAlgorithmException, CertificateException {

    // Setup the SSL context to use the truststore
    ssl_ctx = SSLContext.getInstance("TLS");

    // Setup truststore
    KeyStore ksCACert = KeyStore.getInstance("BKS");
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    InputStream trustStoreStream = context.getResources().openRawResource(rawResource);
    ksCACert.load(trustStoreStream, password.toCharArray());

    //init factory with custom cacert
    trustManagerFactory.init(ksCACert);// w  ww  .j av  a2s.c  o  m
    Log.d("SSL", "CACerts " + ksCACert.size());
    Log.d("SSL", "trustManagerFactory " + trustManagerFactory.getTrustManagers().length);

    // Setup client keystore
    /*
    KeyStore keyStore = KeyStore.getInstance("BKS");
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    InputStream keyStoreStream = context.getResources().openRawResource(R.raw.clientkeystore);
    keyStore.load(keyStoreStream, "testtest".toCharArray());
    keyManagerFactory.init(keyStore, "testtest".toCharArray());
    Log.d("SSL", "Key " + keyStore.size());
            
    Log.d("SSL", "keyManagerFactory " + keyManagerFactory.getKeyManagers().length);
    */

    //nothing implemented yet
    SecureRandom secRand = SecureRandom.getInstance(RANDOM_ALGORITHM);

    ssl_ctx.init(null, trustManagerFactory.getTrustManagers(), secRand);

    socketFactory = (SSLSocketFactory) ssl_ctx.getSocketFactory();

}