List of usage examples for java.security KeyStore getInstance
public static KeyStore getInstance(String type) throws KeyStoreException
From source file:com.silverpeas.util.security.SilverpeasX509TrustManager.java
public SilverpeasX509TrustManager(String trustStoreFile, char[] password) { InputStream fis = null;/*from w w w.j a v a 2 s.c o m*/ try { KeyStore trustore = KeyStore.getInstance(KeyStore.getDefaultType()); fis = new FileInputStream(trustStoreFile); trustore.load(fis, password); TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); tmf.init(trustore); TrustManager tms[] = tmf.getTrustManagers(); for (TrustManager trustManager : tms) { if (trustManager instanceof X509TrustManager) { defaultTrustManager = (X509TrustManager) trustManager; return; } } } catch (IOException ioex) { logger.error("Couldn't load trustore " + trustStoreFile, ioex); } catch (GeneralSecurityException secEx) { logger.error("Couldn't create trustore " + trustStoreFile, secEx); } finally { IOUtils.closeQuietly(fis); } }
From source file:org.eclipse.mylyn.internal.commons.http.PollingSslProtocolSocketFactory.java
public PollingSslProtocolSocketFactory() { KeyManager[] keymanagers = null; if (System.getProperty(KEY_STORE) != null && System.getProperty(KEY_STORE_PASSWORD) != null) { try {/* w w w.j a va 2 s. c o m*/ String type = System.getProperty(KEY_STORE_TYPE, KeyStore.getDefaultType()); KeyStore keyStore = KeyStore.getInstance(type); char[] password = System.getProperty(KEY_STORE_PASSWORD).toCharArray(); keyStore.load(new FileInputStream(System.getProperty(KEY_STORE)), password); KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, password); keymanagers = keyManagerFactory.getKeyManagers(); } catch (Exception e) { CommonsHttpPlugin.log(IStatus.ERROR, "Could not initialize keystore", e); //$NON-NLS-1$ } } hasKeyManager = keymanagers != null; try { SSLContext sslContext = SSLContext.getInstance("SSL"); //$NON-NLS-1$ sslContext.init(keymanagers, new TrustManager[] { new TrustAllTrustManager() }, null); this.socketFactory = sslContext.getSocketFactory(); } catch (Exception e) { CommonsHttpPlugin.log(IStatus.ERROR, "Could not initialize SSL context", e); //$NON-NLS-1$ } }
From source file:ee.sk.hwcrypto.demo.signature.TestSigningData.java
private static byte[] sign(byte[] dataToSign, DigestAlgorithm digestAlgorithm) { try {//from www . j a va2 s .co m KeyStore keyStore = KeyStore.getInstance("PKCS12"); try (FileInputStream stream = new FileInputStream(TEST_PKI_CONTAINER)) { keyStore.load(stream, TEST_PKI_CONTAINER_PASSWORD.toCharArray()); } PrivateKey privateKey = (PrivateKey) keyStore.getKey("1", TEST_PKI_CONTAINER_PASSWORD.toCharArray()); final String javaSignatureAlgorithm = "NONEwith" + privateKey.getAlgorithm(); return encrypt(javaSignatureAlgorithm, privateKey, addPadding(dataToSign, digestAlgorithm)); } catch (Exception e) { throw new DigiDoc4JException("Loading private key failed"); } }
From source file:com.evolveum.midpoint.init.ConfigurableProtectorFactory.java
public void init() { Configuration config = configuration.getConfiguration(PROTECTOR_CONFIGURATION); protectorConfig = new ProtectorConfiguration(config); //Extract file if not exists if (config.getString("midpoint.home") == null) { return;/*from w w w .j av a2 s . co m*/ } File ks = new File(protectorConfig.getKeyStorePath()); if (ks.exists()) { return; } //todo improve FileOutputStream fos = null; try { KeyStore keystore = KeyStore.getInstance("jceks"); char[] password = "changeit".toCharArray(); keystore.load(null, password); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secretKey = keyGen.generateKey(); keystore.setKeyEntry("default", secretKey, "midpoint".toCharArray(), null); fos = new FileOutputStream(protectorConfig.getKeyStorePath()); keystore.store(fos, password); fos.close(); } catch (Exception ex) { throw new SystemException("Couldn't generate keystore, reason: " + ex.getMessage(), ex); } finally { IOUtils.closeQuietly(fos); } }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static KeyStore getKeyStore(Context context) { KeyStore keyStore = null;//ww w . j a v a2 s . c o m try { keyStore = KeyStore.getInstance(KEY_PROVIDER); keyStore.load(null); if (!keyStore.containsAlias(KEY_ALIAS)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // for api level 23+ generateNewKey(); } else { // for api level 18 - 22 generateNewKeyOld(context); } } } catch (KeyStoreException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return keyStore; }
From source file:com.cloudhopper.httpclient.util.SchemeFactory.java
static public Scheme createHttpsScheme(File keystoreFile, String keystorePassword, File truststoreFile, String truststorePassword) throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException, IOException, KeyManagementException, CertificateException, UnrecoverableKeyException { if (keystoreFile == null && truststoreFile == null) { // To insure we don't break anything, if keystore and trust store is not specified, // call the legacy createHttpsScheme. return createHttpsScheme(); } else {//from www. ja v a 2s.c o m // Configure https scheme with a keystore to authenticate ourselves to the server // and/or a truststore to verify the server's certificate. KeyStore keystore = null; if (keystoreFile != null) { keystore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(keystoreFile); try { // A null password is valid when the keystore does not have a password. if (keystorePassword != null) { keystore.load(instream, keystorePassword.toCharArray()); } else { keystore.load(instream, null); } } finally { instream.close(); } } KeyStore truststore = null; if (truststoreFile != null) { truststore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(truststoreFile); try { // A null password is valid when the keystore does not have a password. if (truststorePassword != null) { truststore.load(instream, truststorePassword.toCharArray()); } else { truststore.load(instream, null); } } finally { instream.close(); } } // Not sure if identifing which params were passed in as null and calling the // appropriate constructor is necessary, because the Apache Docs don't describe // what happens when we pass in null. Play it conservative rather than test the // behavior. SSLSocketFactory socketFactory; if (keystore != null && truststore != null) { socketFactory = new SSLSocketFactory(keystore, keystorePassword, truststore); } else if (keystore != null) { socketFactory = new SSLSocketFactory(keystore, keystorePassword); } else { socketFactory = new SSLSocketFactory(truststore); } return new Scheme("https", socketFactory, 443); } }
From source file:org.owasp.goatdroid.herdfinancial.requestresponse.CustomSSLSocketFactory.java
public static HttpClient getNewHttpClient() { try {/*from ww w .jav a 2 s . co m*/ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); 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.vmware.identity.tomcat.VECSAwareJSSESocketFactory.java
@Override protected KeyStore getKeystore(String type, String provider, String pass) throws IOException { if ("VKS".equalsIgnoreCase(type)) { System.out.println("Store name in server.xml- " + store); String keystoreName = store; if (keystoreName == null || keystoreName.isEmpty()) { throw new IOException("keystore file must specify the keystore name"); }/*w w w.j a v a2 s . c o m*/ KeyStore ks = null; try { if (provider == null || provider.isEmpty()) { ks = KeyStore.getInstance(type); } else { ks = KeyStore.getInstance(type, provider); } VecsLoadStoreParameter params = new VecsLoadStoreParameter(keystoreName); ks.load(params); } catch (Exception ex) { throw new IOException("Failed to load keystore " + keystoreName, ex); } return ks; } else { return super.getKeystore(type, provider, pass); } }
From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java
public static HttpClient getHttpsClient(byte[] sslCertificateBytes) { DefaultHttpClient httpClient;/*from ww w.j a v a2s. co m*/ Certificate[] sslCertificate; httpClient = new DefaultHttpClient(); try { sslCertificate = convertByteArrayToCertificate(sslCertificateBytes); TrustManagerFactory tf = TrustManagerFactory.getInstance("X509"); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null); for (int i = 0; i < sslCertificate.length; i++) { ks.setCertificateEntry("StartCom" + i, sslCertificate[i]); } tf.init(ks); TrustManager[] tm = tf.getTrustManagers(); SSLContext sslCon = SSLContext.getInstance("SSL"); sslCon.init(null, tm, new SecureRandom()); SSLSocketFactory socketFactory = new SSLSocketFactory(ks); Scheme sch = new Scheme("https", 443, socketFactory); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException | KeyManagementException | UnrecoverableKeyException ex) { Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex); } return httpClient; }
From source file:org.wso2.carbon.identity.cloud.web.jaggery.clients.MutualSSLHttpClient.java
public MutualSSLHttpClient() { String filePath = null;/*from ww w . j a va 2 s . com*/ try { final KeyStore keyStore = KeyStore.getInstance(keyStoreType); filePath = ServerConfiguration.getInstance().getFirstProperty(SecurityKeyStoreLocation); InputStream keystoreInput = new FileInputStream(new File(filePath)); keyStore.load(keystoreInput, ServerConfiguration.getInstance().getFirstProperty(SecurityKeyStorePassword).toCharArray()); final KeyStore trustStore = KeyStore.getInstance(keyStoreType); filePath = ServerConfiguration.getInstance().getFirstProperty(SecurityTrustStoreLocation); InputStream truststoreInput = new FileInputStream(new File(filePath)); trustStore.load(truststoreInput, ServerConfiguration.getInstance().getFirstProperty(SecurityTrustStorePassword).toCharArray()); SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore).loadKeyMaterial(keyStore, ServerConfiguration.getInstance().getFirstProperty(SecurityKeyStorePassword).toCharArray()) .build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext); httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyStoreException e) { log.error("Error while instantiating key store for key store type : " + keyStoreType, e); } catch (FileNotFoundException e) { log.error("File not found in the given path : " + filePath, e); } catch (IOException e) { log.error("Error while loading the key store in the given path : " + filePath, e); } catch (CertificateException e) { log.error("Certificate error in the key store : " + filePath, e); } catch (NoSuchAlgorithmException e) { log.error("Algorithm error in the key store : " + filePath, e); } catch (UnrecoverableKeyException e) { log.error("Error while creating the SSLContext", e); } catch (KeyManagementException e) { log.error("Error while creating the SSLContext", e); } }