List of usage examples for java.security KeyStore load
public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
From source file:com.github.jmkgreen.keystore.mongo.KeyStoreRest.java
@GET @Path("create-new-keystore") public void createNewKeyStore(@QueryParam("name") String name, @QueryParam("password") String password) throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException { KeyStore store = KeyStore.getInstance("JCEKS"); store.load(null, password.toCharArray()); keyStoreRepository.save(name, password.toCharArray(), store); }
From source file:org.xacml4j.opensaml.KeyStoreFactory.java
@Override public KeyStore getObject() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { KeyStore ks = KeyStore.getInstance(ksType); ks.load(ksLocation.getInputStream(), ksPassword.toCharArray()); return ks;//from w w w. j a va 2s . co m }
From source file:net.sf.keystore_explorer.crypto.keystore.KeyStoreUtil.java
/** * Load an MS CAPI Store as a KeyStore. The KeyStore is not file based and * therefore does not need to be saved.//ww w. ja v a2s.c o m * * @param msCapiStoreType * MS CAPI Store Type * @return The MS CAPI Store as a KeyStore * @throws CryptoException * Problem encountered loading the KeyStore */ public static KeyStore loadMsCapiStore(MsCapiStoreType msCapiStoreType) throws CryptoException { if (!areMsCapiStoresSupported()) { // May previously have been set on an MSCAPI supporting JRE ApplicationSettings.getInstance().setUseWindowsTrustedRootCertificates(false); throw new CryptoException(res.getString("MsCapiStoresNotSupported.exception.message")); } KeyStore keyStore = null; try { keyStore = KeyStore.getInstance(msCapiStoreType.jce(), MS_CAPI.jce()); } catch (KeyStoreException ex) { throw new CryptoException(MessageFormat.format(res.getString("NoCreateKeyStore.exception.message"), msCapiStoreType.jce()), ex); } catch (NoSuchProviderException ex) { throw new CryptoException(MessageFormat.format(res.getString("NoCreateKeyStore.exception.message"), msCapiStoreType.jce()), ex); } try { keyStore.load(null, null); } catch (NoSuchAlgorithmException ex) { throw new CryptoException(MessageFormat.format(res.getString("NoLoadKeyStoreType.exception.message"), msCapiStoreType.jce()), ex); } catch (CertificateException ex) { throw new CryptoException(MessageFormat.format(res.getString("NoLoadKeyStoreType.exception.message"), msCapiStoreType.jce()), ex); } catch (IOException ex) { throw new CryptoException(MessageFormat.format(res.getString("NoLoadKeyStoreType.exception.message"), msCapiStoreType.jce()), ex); } return keyStore; }
From source file:org.anhonesteffort.flock.registration.HttpClientFactory.java
public DefaultHttpClient buildClient() throws RegistrationApiException { try {/*from w w w . j a v a 2 s . co m*/ AssetManager assetManager = context.getAssets(); InputStream keyStoreInputStream = assetManager.open("flock.store"); KeyStore trustStore = KeyStore.getInstance("BKS"); trustStore.load(keyStoreInputStream, "owsflock".toCharArray()); SSLSocketFactory appSSLSocketFactory = new SSLSocketFactory(trustStore); DefaultHttpClient client = new DefaultHttpClient(); SchemeRegistry schemeRegistry = client.getConnectionManager().getSchemeRegistry(); Scheme httpsScheme = new Scheme("https", appSSLSocketFactory, 443); schemeRegistry.register(httpsScheme); return client; } catch (Exception e) { Log.e(getClass().getName(), "caught exception while constructing HttpClient client", e); throw new RegistrationApiException( "caught exception while constructing HttpClient client: " + e.toString()); } }
From source file:org.owasp.proxy.Main.java
private static SSLContextSelector getClientSSLContextSelector(Configuration config) { String type = config.keystoreType; char[] password = config.keyStorePassword == null ? null : config.keyStorePassword.toCharArray(); File location = config.keyStoreLocation == null ? null : new File(config.keyStoreLocation); if (type != null) { KeyStore ks = null;//from w ww .j a v a2 s . c o m if (type.equals("PKCS11")) { try { int slot = config.pkcs11SlotLocation; ks = KeystoreUtils.getPKCS11Keystore("PKCS11", location, slot, password); } catch (Exception e) { System.err.println(e.getLocalizedMessage()); System.exit(2); } } else { try { FileInputStream in = new FileInputStream(location); ks = KeyStore.getInstance(type); ks.load(in, password); } catch (Exception e) { System.err.println(e.getLocalizedMessage()); System.exit(2); } } String alias = config.keyStoreAlias; if (alias == null) { try { Map<String, String> aliases = KeystoreUtils.getAliases(ks); if (aliases.size() > 0) { System.err.println("Keystore contains the following aliases: \n"); for (String a : aliases.keySet()) { System.err.println("Alias \"" + a + "\"" + " : " + aliases.get(a)); } alias = aliases.keySet().iterator().next(); System.err.println("Using " + alias + " : " + aliases.get(alias)); } else { System.err.println("Keystore contains no aliases!"); System.exit(3); } } catch (KeyStoreException kse) { System.err.println(kse.getLocalizedMessage()); System.exit(4); } } try { final X509KeyManager km = KeystoreUtils.getKeyManagerForAlias(ks, alias, password); return new DefaultClientContextSelector(km); } catch (GeneralSecurityException gse) { System.err.println(gse.getLocalizedMessage()); System.exit(5); } } return new DefaultClientContextSelector(); }
From source file:com.fanmei.pay4j.http.WeixinSSLRequestExecutor.java
public WeixinSSLRequestExecutor(WeixinConfig weixinConfig) throws WeixinException { InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream(weixinConfig.getCertificateFile()); try {// www .j a v a 2s. c o m String password = weixinConfig.getAccount().getCertificateKey(); KeyStore keyStore = KeyStore.getInstance(Constants.PKCS12); keyStore.load(inputStream, password.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(Constants.SunX509); kmf.init(keyStore, password.toCharArray()); SSLContext sslContext = SSLContext.getInstance(Constants.TLS); sslContext.init(kmf.getKeyManagers(), null, new java.security.SecureRandom()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (Exception e) { throw WeixinException.of("Key load error", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } }
From source file:com.wudaosoft.net.httpclient.SSLContextBuilder.java
public SSLContext buildPKCS12() { Args.notEmpty(password, "password"); Args.notNull(cert, "cert"); char[] pwd = password.toCharArray(); try {/*ww w.j a va 2s. c o m*/ KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(cert.openStream(), pwd); // & ? KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, pwd); // SSLContext SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(kmf.getKeyManagers(), null, new SecureRandom()); return sslContext; } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; throw new RuntimeException(e); } }
From source file:edu.vt.alerts.android.library.util.HttpClientFactory.java
private KeyStore getTrustStore(Context context) throws Exception { KeyStore trustStore = KeyStore.getInstance("PKCS12"); trustStore.load(context.getResources().openRawResource(R.raw.truststore), "changeit".toCharArray()); return trustStore; }
From source file:org.everit.osgi.authentication.cas.tests.SecureHttpClient.java
public SecureHttpClient(final String principal, final BundleContext bundleContext) throws Exception { this.principal = principal; httpClientContext = HttpClientContext.create(); httpClientContext.setCookieStore(new BasicCookieStore()); KeyStore trustStore = KeyStore.getInstance("jks"); trustStore.load(bundleContext.getBundle().getResource("/jetty-keystore").openStream(), "changeit".toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(trustStore); TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustManagers, new SecureRandom()); httpClient = HttpClientBuilder.create().setSslcontext(sslContext) .setRedirectStrategy(new DefaultRedirectStrategy()).build(); }
From source file:com.ibm.iotf.client.AbstractClient.java
static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile, final String password) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException { Security.addProvider(new BouncyCastleProvider()); X509Certificate caCert = null; if (caCrtFile != null) { // load CA certificate PEMReader reader = new PEMReader( new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(caCrtFile))))); caCert = (X509Certificate) reader.readObject(); reader.close();//from ww w . ja va 2 s. co m } else { ClassLoader classLoader = AbstractClient.class.getClassLoader(); PEMReader reader = new PEMReader( new InputStreamReader(classLoader.getResource(SERVER_MESSAGING_PEM).openStream())); caCert = (X509Certificate) reader.readObject(); reader.close(); } PEMReader reader = new PEMReader( new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(crtFile))))); X509Certificate cert = (X509Certificate) reader.readObject(); reader.close(); // load client private key reader = new PEMReader( new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(keyFile))))); KeyPair key = (KeyPair) reader.readObject(); reader.close(); TrustManagerFactory tmf = null; if (caCert != null) { // CA certificate is used to authenticate server KeyStore caKs = KeyStore.getInstance("JKS"); //caKs.load(null, null); caKs.load(null, null); caKs.setCertificateEntry("ca-certificate", caCert); tmf = TrustManagerFactory.getInstance("PKIX"); tmf.init(caKs); } // client key and certificates are sent to server so it can authenticate us KeyStore ks = KeyStore.getInstance("JKS"); ks.load(null, null); ks.setCertificateEntry("certificate", cert); ks.setKeyEntry("private-key", key.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[] { cert }); KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX"); kmf.init(ks, password.toCharArray()); // finally, create SSL socket factory SSLContext context = SSLContext.getInstance("TLSv1.2"); if (tmf != null) { context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); } else { context.init(kmf.getKeyManagers(), null, null); } return context.getSocketFactory(); }