List of usage examples for java.security KeyStore load
public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
From source file:org.apache.cxf.fediz.integrationtests.HTTPTestUtils.java
public static String sendHttpGet(String url, String user, String password, int returnCodeIDP, int returnCodeRP, int idpPort) throws Exception { CloseableHttpClient httpClient = null; try {/*from ww w .j ava 2 s .c om*/ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("localhost", idpPort), new UsernamePasswordCredentials(user, password)); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks")); try { trustStore.load(instream, "clientpass".toCharArray()); } finally { try { instream.close(); } catch (Exception ex) { ex.printStackTrace(); } } SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()); sslContextBuilder.loadKeyMaterial(trustStore, "clientpass".toCharArray()); SSLContext sslContext = sslContextBuilder.build(); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); httpClientBuilder.setSSLSocketFactory(sslSocketFactory); httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy()); httpClient = httpClientBuilder.build(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpClient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } Assert.assertTrue("IDP HTTP Response code: " + response.getStatusLine().getStatusCode() + " [Expected: " + returnCodeIDP + "]", returnCodeIDP == response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() != 200) { return null; } // Redirect to a POST is not supported without user interaction // http://www.ietf.org/rfc/rfc2616.txt // If the 301 status code is received in response to a request other // than GET or HEAD, the user agent MUST NOT automatically redirect the // request unless it can be confirmed by the user, since this might // change the conditions under which the request was issued. Source source = new Source(EntityUtils.toString(entity)); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); FormFields formFields = source.getFormFields(); List<Element> forms = source.getAllElements(HTMLElementName.FORM); Assert.assertEquals("Only one form expected but got " + forms.size(), 1, forms.size()); String postUrl = forms.get(0).getAttributeValue("action"); Assert.assertNotNull("Form field 'wa' not found", formFields.get("wa")); Assert.assertNotNull("Form field 'wresult' not found", formFields.get("wresult")); for (FormField formField : formFields) { if (formField.getUserValueCount() != 0) { nvps.add(new BasicNameValuePair(formField.getName(), formField.getValues().get(0))); } } HttpPost httppost = new HttpPost(postUrl); httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); response = httpClient.execute(httppost); entity = response.getEntity(); System.out.println(response.getStatusLine()); Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode() + " [Expected: " + returnCodeRP + "]", returnCodeRP == response.getStatusLine().getStatusCode()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } return EntityUtils.toString(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources if (httpClient != null) { httpClient.close(); } } }
From source file:fr.inria.ucn.Helpers.java
/** * FIXME: remove once all servers have valid certificate * @return//from w w w .j av a 2 s . co m */ public static boolean isCaCertInstalledHack(String match) { boolean res = false; try { KeyStore ks = KeyStore.getInstance("AndroidCAStore"); ks.load(null, null); Enumeration<String> aliases = ks.aliases(); while (aliases.hasMoreElements()) { String alias = aliases.nextElement(); X509Certificate cert = (X509Certificate) ks.getCertificate(alias); //Log.d(Constants.LOGTAG, "keystore: " + alias + "/" + cert.getIssuerDN().getName()); if (cert.getIssuerDN().getName().contains(match)) { res = true; break; } } } catch (KeyStoreException e) { Log.w(Constants.LOGTAG, "failed to check certificates", e); } catch (NoSuchAlgorithmException e) { } catch (CertificateException e) { } catch (IOException e) { } return res; }
From source file:cn.edu.mju.Thriphoto.net.HttpManager.java
private static HttpClient getNewHttpClient() { try {// w w w . jav a 2 s .com 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(); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); 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); HttpConnectionParams.setConnectionTimeout(params, SET_CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SET_SOCKET_TIMEOUT); HttpClient client = new DefaultHttpClient(ccm, params); // if (NetState.Mobile == NetStateManager.CUR_NETSTATE) { // // ??APN // HttpHost proxy = NetStateManager.getAPN(); // if (null != proxy) { // client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, // proxy); // } // } return client; } catch (Exception e) { return new DefaultHttpClient(); } }
From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java
private static MockWebServer getSslServer(Path keyStorePath, String keyStorePass) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, KeyManagementException, UnrecoverableKeyException { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); try (InputStream is = Files.newInputStream(keyStorePath)) { keyStore.load(is, keyStorePass.toCharArray()); }/*from www .j a v a 2 s. c o m*/ final SSLContext sslContext = new SSLContextBuilder().loadKeyMaterial(keyStore, keyStorePass.toCharArray()) .build(); MockWebServer server = new MockWebServer(sslContext, false); server.enqueue(new MockResponse().setResponseCode(200).setBody("body")); server.start(); return server; }
From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java
private static CloseableHttpClient getSSLClient(Path trustStorePath, String trustStorePass) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {//from ww w. ja va 2 s. c om KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); try (InputStream is = Files.newInputStream(trustStorePath)) { trustStore.load(is, trustStorePass.toCharArray()); } final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore, null).build(); return HttpClients.custom().setSSLContext(sslContext).build(); }
From source file:mitm.common.tools.SMIME.java
private static KeyStore loadKeyStore(String keyFile, String password) throws Exception { File file = new File(keyFile); file = file.getAbsoluteFile();/*from w w w. j a v a 2s . co m*/ KeyStore keyStore = securityFactory.createKeyStore("PKCS12"); /* initialize key store */ keyStore.load(new FileInputStream(file), password != null ? password.toCharArray() : null); return keyStore; }
From source file:net.ymate.framework.commons.HttpClientHelper.java
public static SSLConnectionSocketFactory createConnectionSocketFactory(String certType, URL certFilePath, char[] passwordChars) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException { if (StringUtils.isBlank(certType)) { throw new NullArgumentException("certType"); }//from w ww . ja v a 2 s .co m if (certFilePath == null) { throw new NullArgumentException("certFilePath"); } if (ArrayUtils.isEmpty(passwordChars)) { throw new NullArgumentException("passwordChars"); } KeyStore _keyStore = KeyStore.getInstance(certType); InputStream _certFileStream = null; try { _certFileStream = certFilePath.openStream(); _keyStore.load(_certFileStream, passwordChars); } finally { IOUtils.closeQuietly(_certFileStream); } SSLContext _sslContext = SSLContexts.custom().loadKeyMaterial(_keyStore, passwordChars).build(); return new SSLConnectionSocketFactory(_sslContext, new String[] { "TLSv1" }, null, new DefaultHostnameVerifier()); }
From source file:com.zacwolf.commons.crypto._CRYPTOfactory.java
final public static KeyStore genNewKeyStore(final File keystorefile, final char[] keystorepass) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { final KeyStore ks = KeyStore.getInstance(STORETYPE); ks.load((InputStream) null, keystorepass); final FileOutputStream out = new FileOutputStream(keystorefile); try {/*w w w . j a va2 s .c o m*/ ks.store(out, keystorepass); } finally { out.close(); } return ks; }
From source file:com.zacwolf.commons.crypto._CRYPTOfactory.java
final public static _CRYPTOfactory getInstanceFromKeystore(final InputStream ksin, final char[] keystorepass, final String alias) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException { final KeyStore keystore = KeyStore.getInstance(STORETYPE); keystore.load(ksin, keystorepass); return getInstanceFromKeystore(keystore, keystorepass, alias); }
From source file:net.sf.jsignpdf.utils.KeyStoreUtils.java
public static KeyStore createTrustStore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { final KeyStore trustStore = createKeyStore(); char SEP = File.separatorChar; final File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security"); final File file = new File(dir, "cacerts"); if (file.canRead()) { final KeyStore ks = KeyStore.getInstance("JKS"); final InputStream in = new FileInputStream(file); try {//from w w w . j av a 2s . co m ks.load(in, null); } finally { in.close(); } copyCertificates(ks, trustStore); } return trustStore; }