List of usage examples for java.security KeyStore load
public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
From source file:Main.java
public static boolean isCACertificateInstalled(File fileCA, String type, char[] password) throws KeyStoreException { KeyStore keyStoreCA = null; try {/*from w ww . j a v a 2s .c o m*/ keyStoreCA = KeyStore.getInstance(type/*, "BC"*/); } catch (Exception e) { e.printStackTrace(); } if (fileCA.exists() && fileCA.canRead()) { try { FileInputStream fileCert = new FileInputStream(fileCA); keyStoreCA.load(fileCert, password); fileCert.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (java.security.cert.CertificateException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Enumeration ex = keyStoreCA.aliases(); Date exportFilename = null; String caAliasValue = ""; while (ex.hasMoreElements()) { String is = (String) ex.nextElement(); Date lastStoredDate = keyStoreCA.getCreationDate(is); if (exportFilename == null || lastStoredDate.after(exportFilename)) { exportFilename = lastStoredDate; caAliasValue = is; } } try { return keyStoreCA.getKey(caAliasValue, password) != null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } } return false; }
From source file:com.geekandroid.sdk.pay.utils.Util.java
private static HttpClient getNewHttpClient() { try {//from w w w. j av a 2 s . com KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(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.solace.samples.cloudfoundry.securesession.controller.SolaceController.java
/** * This utility function installs a certificate into the JRE's trusted * store. Normally you would not do this, but this is provided to * demonstrate how to use TLS, and have the client validate a self-signed * server certificate./*from w ww .j a v a 2s . c om*/ * * @throws Exception */ private static void importCertificate() throws Exception { File file = new File(CERTIFICATE_FILE_NAME); logger.info("Loading certificate from " + file.getAbsolutePath()); // This loads the KeyStore from the default location // (i.e. default for a Clound Foundry app) using the default password. FileInputStream is = new FileInputStream(TRUST_STORE); char[] password = TRUST_STORE_PASSWORD.toCharArray(); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(is, password); is.close(); // Create an ByteArrayInputStream stream from the FileInputStream fis = new FileInputStream(CERTIFICATE_FILE_NAME); DataInputStream dis = new DataInputStream(fis); byte[] bytes = new byte[dis.available()]; dis.readFully(bytes); dis.close(); ByteArrayInputStream certstream = new ByteArrayInputStream(bytes); // This takes that Byte Array and creates a certificate out of it. CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate certs = cf.generateCertificate(certstream); // Finally, store the new certificate in the keystore. keystore.setCertificateEntry(CERTIFICATE_ALIAS, certs); // Save the new keystore contents FileOutputStream out = new FileOutputStream(TRUST_STORE); keystore.store(out, password); out.close(); }
From source file:learn.encryption.ssl.SSLContext_Https.java
public static SSLContext getSSLContext2(String servercerfile, String clientkeyStore, String clientPass) { if (sslContext != null) { return sslContext; }//from w w w . j av a 2s. c o m try { // ??, ??assets //InputStream inputStream = App.getInstance().getAssets().open("serverkey.cer"); InputStream inputStream = new FileInputStream(new File(servercerfile)); // ?? CertificateFactory cerFactory = CertificateFactory.getInstance("X.509"); Certificate cer = cerFactory.generateCertificate(inputStream); // ?KeyStore KeyStore keyStore = KeyStore.getInstance("PKCS12");//eclipse?jksandroidPKCS12?? keyStore.load(null, null); keyStore.setCertificateEntry("trust", cer); // KeyStoreTrustManagerFactory TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); sslContext = SSLContext.getInstance("TLS"); //?clientKeyStore(android??bks) //KeyStore clientKeyStore = KeyStore.getInstance("BKS"); KeyStore clientKeyStore = KeyStore.getInstance("jks"); //clientKeyStore.load(App.getInstance().getAssets().open("clientkey.bks"), "123456".toCharArray()); clientKeyStore.load(new FileInputStream(new File(clientkeyStore)), clientPass.toCharArray()); // ?clientKeyStorekeyManagerFactory KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(clientKeyStore, clientPass.toCharArray()); // ?SSLContext trustManagerFactory.getTrustManagers() sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());//new TrustManager[]{trustManagers}?? } catch (Exception e) { e.printStackTrace(); } return sslContext; }
From source file:com.allblacks.utils.web.HttpUtil.java
static HttpClient getNewHttpClient() { try {/* w w w . j a v a2 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:eu.fthevenet.binjr.data.adapters.HttpDataAdapterBase.java
protected static SSLContext createSslCustomContext() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException, NoSuchProviderException { // Load platform specific Trusted CA keystore logger.trace(() -> "Available Java Security providers: " + Arrays.toString(Security.getProviders())); KeyStore tks; switch (AppEnvironment.getInstance().getOsFamily()) { case WINDOWS: tks = KeyStore.getInstance("Windows-ROOT", "SunMSCAPI"); tks.load(null, null); break;//from w ww . ja v a 2 s.c o m case OSX: tks = KeyStore.getInstance("KeychainStore", "Apple"); tks.load(null, null); break; case LINUX: case UNSUPPORTED: default: tks = null; break; } return SSLContexts.custom().loadTrustMaterial(tks, null).build(); }
From source file:com.nesscomputing.httpclient.internal.HttpClientTrustManagerFactory.java
@Nonnull private static KeyStore loadKeystore(@Nonnull String location, @Nonnull String keystoreType, @Nonnull String keystorePassword) throws GeneralSecurityException, IOException { final KeyStore keystore = KeyStore.getInstance(keystoreType); URL keystoreUrl;// w w w . java 2s . c o m if (StringUtils.startsWithIgnoreCase(location, "classpath:")) { keystoreUrl = Resources.getResource(HttpClientTrustManagerFactory.class, location.substring(10)); } else { keystoreUrl = new URL(location); } keystore.load(keystoreUrl.openStream(), keystorePassword.toCharArray()); return keystore; }
From source file:com.evrythng.java.wrapper.core.api.ApiCommand.java
private static HttpClient wrapClient(final HttpClient base) { try {//ww w. ja v a 2 s . c o m KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory ssf = new WrapperSSLSocketFactory(trustStore); ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", ssf, 443)); return new DefaultHttpClient(ccm, base.getParams()); } catch (Exception ex) { return null; } }
From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java
public static KeyStore newKeyStore() { KeyStore store; try {//from w w w .ja va2 s .c om store = KeyStore.getInstance(KeyStore.getDefaultType()); store.load(null, null); return store; } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex) { Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:org.apache.metron.elasticsearch.client.ElasticsearchClientFactory.java
private static KeyStore getStore(String type, Path path, char[] pass) { KeyStore store; try {//from w w w . j a va2s.c om store = KeyStore.getInstance(type); } catch (KeyStoreException e) { throw new IllegalStateException("Unable to get keystore type '" + type + "'", e); } try (InputStream is = Files.newInputStream(path)) { store.load(is, pass); } catch (IOException | NoSuchAlgorithmException | CertificateException e) { throw new IllegalStateException("Unable to load keystore from path '" + path + "'", e); } return store; }