List of usage examples for java.security KeyStore load
public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
From source file:it.jnrpe.server.CBindingThread.java
/** * Returns the SSL factory to be used to create the Server Socket * @throws KeyStoreException /*from w w w . j a va2 s .c o m*/ * @throws IOException * @throws FileNotFoundException * @throws CertificateException * @throws UnrecoverableKeyException * @throws KeyManagementException * * @see it.intesa.fi2.client.network.ISSLObjectsFactory#getSSLSocketFactory(String, String, String) */ public SSLServerSocketFactory getSSLSocketFactory(String sKeyStoreFile, String sKeyStorePwd, String sKeyStoreType) throws KeyStoreException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException, KeyManagementException { if (sKeyStoreFile == null) throw new KeyStoreException("KEYSTORE HAS NOT BEEN SPECIFIED"); if (this.getClass().getClassLoader().getResourceAsStream(sKeyStoreFile) == null) throw new KeyStoreException("COULD NOT FIND KEYSTORE '" + sKeyStoreFile + "'"); if (sKeyStorePwd == null) throw new KeyStoreException("KEYSTORE PASSWORD HAS NOT BEEN SPECIFIED"); SSLContext ctx; KeyManagerFactory kmf; try { ctx = SSLContext.getInstance("SSLv3"); kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); //KeyStore ks = getKeystore(sKeyStoreFile, sKeyStorePwd, sKeyStoreType); KeyStore ks = KeyStore.getInstance(sKeyStoreType); ks.load(this.getClass().getClassLoader().getResourceAsStream(sKeyStoreFile), sKeyStorePwd.toCharArray()); char[] passphrase = sKeyStorePwd.toCharArray(); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { throw new SSLException("Unable to initialize SSLSocketFactory.\n" + e.getMessage()); } return ctx.getServerSocketFactory(); }
From source file:com.blackboard.LearnServer.java
private AbstractHttpClient getTrustAllSSLHttpClient() { try {// ww w . j a va 2 s. c om KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new TrustAllSSLSocketFactory(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) { System.out.println("WARNING: Could not create Trust All SSL client, using default" + e.getMessage()); return new DefaultHttpClient(); } }
From source file:com.ntsync.android.sync.client.MyHttpClient.java
private SocketFactory getSSLSocketFactory() { InputStream in = null;// w w w. j ava2 s . co m SocketFactory socketFack = null; try { KeyStore trusted = KeyStore.getInstance("BKS"); in = context.getResources().openRawResource(R.raw.mykeystore); trusted.load(in, "pwd23key".toCharArray()); SSLSocketFactory sslSocketFack = new SSLSocketFactory(trusted); socketFack = sslSocketFack; if (Constants.USE_RELEASE_CONFIG) { sslSocketFack.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); } else { Log.w(TAG, "Disable SSL Hostname verification"); sslSocketFack.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } } catch (GeneralSecurityException e) { Log.e(TAG, "Loading truststore failed.", e); } catch (IOException e) { Log.e(TAG, "Loading truststore failed.", e); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { Log.e(TAG, "closing filescocket failed.", e); } } if (socketFack == null) { Log.w(TAG, "Fallback to custom ssl socket factory."); socketFack = new MySSLSocketFactory(); } return socketFack; }
From source file:hu.balazsbakai.sq.util.RestUtil.java
private DefaultHttpClient getNewTrustedHttpClient() { try {/*from w w w .ja va 2 s.co m*/ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new CustomTrustedSSLSocketFactory(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); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); 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) { LogUtil.e("Exception", e); return new DefaultHttpClient(); } }
From source file:de.betterform.connector.http.ssl.BetterFORMTrustManager.java
private TrustManager[] getCustomX509TrustManagers(final URL url, final String password) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException { TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); if (url == null) { throw new IllegalArgumentException("BetterFORMTrustManager: Keystore url may not be null"); }/*from www. j av a2 s . c o m*/ LOGGER.debug("BetterFORMTrustManager: initializing custom key store"); KeyStore customKeystore = KeyStore.getInstance(KeyStore.getDefaultType()); InputStream is = null; try { is = url.openStream(); customKeystore.load(is, password != null ? password.toCharArray() : null); } finally { if (is != null) is.close(); } trustManagerFactory.init(customKeystore); TrustManager[] customX509TrustManagers = trustManagerFactory.getTrustManagers(); for (int i = 0; i < customX509TrustManagers.length; i++) { if (customX509TrustManagers[i] instanceof X509TrustManager) { customX509TrustManagers[i] = new AuthSSLX509TrustManager( (X509TrustManager) customX509TrustManagers[i]); } } return customX509TrustManagers; }
From source file:DcmSnd.java
private static KeyStore loadKeyStore(String url, char[] password) throws GeneralSecurityException, IOException { KeyStore key = KeyStore.getInstance(toKeyStoreType(url)); InputStream in = openFileOrURL(url); try {// w ww . j ava 2s .c o m key.load(in, password); } finally { in.close(); } return key; }
From source file:org.apache.servicemix.http.processors.CommonsHttpSSLSocketFactory.java
protected final void createUnmanagedFactory(SslParameters ssl) throws Exception { SSLContext context;//w w w . j av a 2 s. c o m if (ssl.getProvider() == null) { context = SSLContext.getInstance(ssl.getProtocol()); } else { context = SSLContext.getInstance(ssl.getProtocol(), ssl.getProvider()); } KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(ssl.getKeyManagerFactoryAlgorithm()); String keyStore = ssl.getKeyStore(); if (keyStore == null) { keyStore = System.getProperty("javax.net.ssl.keyStore"); if (keyStore == null) { throw new IllegalArgumentException( "keyStore or system property javax.net.ssl.keyStore must be set"); } } if (keyStore.startsWith("classpath:")) { try { String res = keyStore.substring(10); URL url = new ClassPathResource(res).getURL(); keyStore = url.toString(); } catch (IOException e) { throw new JBIException("Unable to find keyStore " + keyStore, e); } } String keyStorePassword = ssl.getKeyStorePassword(); if (keyStorePassword == null) { keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); if (keyStorePassword == null) { throw new IllegalArgumentException( "keyStorePassword or system property javax.net.ssl.keyStorePassword must be set"); } } String trustStore = ssl.getTrustStore(); String trustStorePassword = null; if (trustStore == null) { trustStore = System.getProperty("javax.net.ssl.trustStore"); } if (trustStore != null) { if (trustStore.startsWith("classpath:")) { try { String res = trustStore.substring(10); URL url = new ClassPathResource(res).getURL(); trustStore = url.toString(); } catch (IOException e) { throw new JBIException("Unable to find trustStore " + trustStore, e); } } trustStorePassword = ssl.getTrustStorePassword(); if (trustStorePassword == null) { trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword"); if (trustStorePassword == null) { throw new IllegalArgumentException( "trustStorePassword or system property javax.net.ssl.trustStorePassword must be set"); } } } KeyStore ks = KeyStore.getInstance(ssl.getKeyStoreType()); ks.load(Resource.newResource(keyStore).getInputStream(), keyStorePassword.toCharArray()); keyManagerFactory.init(ks, ssl.getKeyPassword() != null ? ssl.getKeyPassword().toCharArray() : keyStorePassword.toCharArray()); if (trustStore != null) { KeyStore ts = KeyStore.getInstance(ssl.getTrustStoreType()); ts.load(Resource.newResource(trustStore).getInputStream(), trustStorePassword.toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(ssl.getTrustManagerFactoryAlgorithm()); trustManagerFactory.init(ts); context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new java.security.SecureRandom()); } else { context.init(keyManagerFactory.getKeyManagers(), null, new java.security.SecureRandom()); } factory = context.getSocketFactory(); }
From source file:net.identio.server.service.authentication.ldap.LdapAuthenticationProvider.java
private void initTrustore(List<LdapAuthMethod> ldapAuthMethods) throws InitializationException { LOG.debug("* Init trust store for SSL connections"); // Init keystore try {/*ww w . ja v a 2s .c om*/ KeyStore ks = KeyStore.getInstance("JKS"); ks.load(null, null); // Add each trust certificate to the keystore for (LdapAuthMethod ldapAuthMethod : ldapAuthMethods) { if (ldapAuthMethod.getTrustCert() != null) { SecurityUtils.addCertificateToKeyStore(ks, SecurityUtils.parseCertificate(ldapAuthMethod.getTrustCert()), ldapAuthMethod.getName()); } } LdapSslSocketFactory.init(ks); } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException ex) { throw new InitializationException("Error when initializing keystore with trusted certificates", ex); } }
From source file:com.lonepulse.travisjr.net.ZombieConfig.java
@Override public HttpClient httpClient() { HttpClient client = super.httpClient(); try {/* w ww . j a v a 2 s.c o m*/ KeyStore keyStore = KeyStore.getInstance("BKS"); InputStream is = TravisJr.Application.getContext().getResources().openRawResource(R.raw.travisjr); try { keyStore.load(is, null); } finally { is.close(); } SSLSocketFactory sslSocketFactory = new SSLSocketFactory(keyStore); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); SchemeRegistry schemeRegistry = ((ThreadSafeClientConnManager) client.getConnectionManager()) .getSchemeRegistry(); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); } catch (Exception e) { Log.e(getClass().getSimpleName(), "HttpClient configuration with a custom SSLSocketFactory failed.", e); } return client; }
From source file:org.ulyssis.ipp.publisher.HttpServerPublisher.java
private SSLContext sslContext() { try {// w w w . j av a 2s .co m KeyStore cks = KeyStore.getInstance(KeyStore.getDefaultType()); cks.load(new FileInputStream(options.getKeystore().get().toFile()), options.getKeystorePass().toCharArray()); SSLContextBuilder builder = SSLContexts.custom(); if (options.getTruststore().isPresent()) { KeyStore tks = KeyStore.getInstance(KeyStore.getDefaultType()); tks.load(new FileInputStream(options.getTruststore().get().toFile()), options.getTruststorePass().toCharArray()); builder.loadTrustMaterial(tks, new TrustSelfSignedStrategy()); } return builder.loadKeyMaterial(cks, options.getKeystorePass().toCharArray()).build(); } catch (Exception e) { // TODO: DO SOMETHING WITH THE EXCEPTION! LOG.error("Exception", e); } return null; }