List of usage examples for java.security KeyStore getInstance
public static KeyStore getInstance(String type) throws KeyStoreException
From source file:com.cloud.utils.security.CertificateHelper.java
public static KeyStore loadKeystore(byte[] ksData, String storePassword) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { assert (ksData != null); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new ByteArrayInputStream(ksData), storePassword != null ? storePassword.toCharArray() : null); return ks;/* w w w . j ava2 s. c o m*/ }
From source file:com.vmware.identity.idm.IdmDataCreator.java
private static KeyPair readKeyStore(CredentialDescriptor cd) throws IOException { KeyPair kp = null;//from ww w . j a v a 2 s. c om InputStream is = null; try { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); char[] stsKeystorePassword = cd.getPassword().toCharArray(); is = getInputStream(cd.getFilename()); ks.load(is, stsKeystorePassword); kp = new KeyPair(); kp.setCertificateChain(Arrays.asList(ks.getCertificateChain(cd.getAlias()))); kp.setPrivateKey((PrivateKey) ks.getKey(cd.getAlias(), stsKeystorePassword)); } catch (Exception e) { logger.debug("Caught exception while reading keystore {}", e.toString()); } finally { if (is != null) { is.close(); } } return kp; }
From source file:sit.web.client.HTTPTrustHelper.java
/** * from/* w w w . j av a 2 s . com*/ * http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https * * @param charset * @param port * @return */ public static HttpClient getNewHttpClient(Charset charset, int port) { try { 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, charset.name()); SchemeRegistry registry = new SchemeRegistry(); //registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, port)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }
From source file:com.utest.webservice.client.rest.AuthSSLProtocolSocketFactory.java
private static KeyStore createKeyStore(final URL url, final String password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { if (url == null) { throw new IllegalArgumentException("Keystore url may not be null"); }//from w w w. j ava 2 s . com KeyStore keystore = KeyStore.getInstance("jks"); InputStream is = null; try { is = url.openStream(); keystore.load(is, password != null ? password.toCharArray() : null); } finally { if (is != null) is.close(); } return keystore; }
From source file:com.dalaran.async.task.http.AbstractHTTPService.java
public static HttpClient getNewHttpClient() { try {//from ww w . j av 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(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 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.springcryptoutils.core.keystore.KeyStoreFactoryBean.java
public void afterPropertiesSet() throws KeyStoreException, IOException, NoSuchAlgorithmException, NoSuchProviderException, CertificateException { if ((provider == null) || (provider.length() == 0)) { keystore = KeyStore.getInstance(type); } else {// w ww.ja v a 2s . com keystore = KeyStore.getInstance(type, provider); } keystore.load(location.getInputStream(), password.toCharArray()); }
From source file:info.fetter.logstashforwarder.protocol.LumberjackClient.java
public LumberjackClient(String keyStorePath, String server, int port, int timeout) throws IOException { this.server = server; this.port = port; try {/* ww w .j av a 2s. c o m*/ if (keyStorePath == null) { throw new IOException("Key store not configured"); } if (server == null) { throw new IOException("Server address not configured"); } keyStore = KeyStore.getInstance("JKS"); keyStore.load(new FileInputStream(keyStorePath), null); TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); tmf.init(keyStore); SSLContext context = SSLContext.getInstance("TLS"); context.init(null, tmf.getTrustManagers(), null); SSLSocketFactory socketFactory = context.getSocketFactory(); socket = new Socket(); socket.connect(new InetSocketAddress(InetAddress.getByName(server), port), timeout); socket.setSoTimeout(timeout); sslSocket = (SSLSocket) socketFactory.createSocket(socket, server, port, true); sslSocket.setUseClientMode(true); sslSocket.startHandshake(); output = new DataOutputStream(new BufferedOutputStream(sslSocket.getOutputStream())); input = new DataInputStream(sslSocket.getInputStream()); logger.info("Connected to " + server + ":" + port); } catch (IOException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:energy.usef.environment.tool.security.KeystoreService.java
public byte[] loadSecretKey() { char[] ksPassword = toCharArray(keystorePassword); char[] ksKeyPassword = toCharArray(keystorePKPassword); Key key = null;/* ww w. j a v a 2 s . c o m*/ try (InputStream is = new FileInputStream(keystoreFilename)) { KeyStore ks = KeyStore.getInstance(JCEKS); ks.load(is, ksPassword); key = ks.getKey(keystorePKAlias, ksKeyPassword); } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException | UnrecoverableKeyException e) { LOGGER.error("Exception occured during the loading of the secret key. {}", e); throw new RuntimeException(e); } if (key == null) { return new byte[0]; } LOGGER.info("Algorithm: " + key.getAlgorithm()); LOGGER.info("Format: " + key.getFormat()); return key.getEncoded(); }
From source file:co.cask.cdap.security.tools.KeyStores.java
/** * Create a Java key store with a stored self-signed certificate. * @return Java keystore which has a self signed X.509 certificate *//* w ww . j ava 2s .co m*/ public static KeyStore generatedCertKeyStore(SConfiguration sConf, String password) { try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM); SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM, SECURE_RANDOM_PROVIDER); keyGen.initialize(KEY_SIZE, random); // generate a key pair KeyPair pair = keyGen.generateKeyPair(); int validity = sConf.getInt(Constants.Security.SSL.CERT_VALIDITY, VALIDITY); X509Certificate cert = getCertificate(DISTINGUISHED_NAME, pair, validity, SIGNATURE_ALGORITHM); KeyStore keyStore = KeyStore.getInstance(SSL_KEYSTORE_TYPE); keyStore.load(null, password.toCharArray()); keyStore.setKeyEntry(CERT_ALIAS, pair.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[] { cert }); return keyStore; } catch (Exception e) { throw new RuntimeException( "SSL is enabled but a key store file could not be created. A keystore is required " + "for SSL to be used.", e); } }
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 w w w. j a v a 2 s . com*/ 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; }