List of usage examples for java.security KeyStore load
public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
From source file:edu.gmu.isa681.server.Server.java
/** * Creates a TLS server socket factory using the key store and key store password provided to the JVM at runtime. * @return/* w w w. j ava2 s . c om*/ * @throws GeneralSecurityException If an error occurs while creating the TLS factory. * @throws IOException If an error occurs while reading the key store. * * Adapted from Oracle JSSE docs. */ private static SSLServerSocketFactory getSSLServerSocketFactory() throws GeneralSecurityException, IOException { FileInputStream fis = null; try { SSLServerSocketFactory ssf = null; // set up key manager to do server authentication SSLContext ctx = SSLContext.getInstance("TLS"); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); KeyStore ks = KeyStore.getInstance("JKS"); String keyStore = System.getProperty("javax.net.ssl.keyStore"); String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); fis = new FileInputStream(keyStore); ks.load(fis, keyStorePassword.toCharArray()); kmf.init(ks, keyStorePassword.toCharArray()); ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory(); return ssf; } finally { Utils.closeQuitely(fis); } }
From source file:org.apache.commons.httpclient.contrib.ssl.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 www . ja v a 2 s .co m*/ LOG.debug("Initializing key store"); KeyStore keystore = KeyStore.getInstance("jks"); keystore.load(url.openStream(), password != null ? password.toCharArray() : null); return keystore; }
From source file:com.yodlee.sampleapps.helper.OpenSamlHelper.java
/** * Initilize the Keystore.//w w w . ja v a 2 s . c om */ private static void initKeyStore() { InputStream fileInput = null; try { fileInput = new FileInputStream(keystoreFilename); } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } KeyStore keystore = null; try { keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(fileInput, keystorePassword.toCharArray()); privateKey = (PrivateKey) keystore.getKey(keystoreAlias, keystorePassword.toCharArray()); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } if (privateKey == null) throw new RuntimeException(keystoreAlias + " key not found in keystore " + keystoreFilename); X509Certificate cert = null; Certificate[] certificates = new Certificate[0]; try { cert = (X509Certificate) keystore.getCertificate(keystoreAlias); certificates = keystore.getCertificateChain(keystoreAlias); } catch (KeyStoreException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } if (cert == null) throw new RuntimeException(keystoreAlias + " cert not found in keystore " + keystoreFilename); if (certificates == null) throw new RuntimeException(keystoreAlias + " cert chain not found in keystore " + keystoreFilename); certs = new X509Certificate[certificates.length]; System.arraycopy(certificates, 0, certs, 0, certs.length); }
From source file:io.kubernetes.client.util.SSLUtils.java
public static KeyStore createKeyStore(InputStream certInputStream, InputStream keyInputStream, String clientKeyAlgo, char[] clientKeyPassphrase, String keyStoreFile, char[] keyStorePassphrase) throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException {//from w w w .j av a 2 s .com CertificateFactory certFactory = CertificateFactory.getInstance("X509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream); byte[] keyBytes = decodePem(keyInputStream); PrivateKey privateKey; KeyFactory keyFactory = KeyFactory.getInstance(clientKeyAlgo); try { // First let's try PKCS8 privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); } catch (InvalidKeySpecException e) { // Otherwise try PKCS8 RSAPrivateCrtKeySpec keySpec = decodePKCS1(keyBytes); privateKey = keyFactory.generatePrivate(keySpec); } KeyStore keyStore = KeyStore.getInstance("JKS"); if (keyStoreFile != null && keyStoreFile.length() > 0) { keyStore.load(new FileInputStream(keyStoreFile), keyStorePassphrase); } else { loadDefaultKeyStoreFile(keyStore, keyStorePassphrase); } String alias = cert.getSubjectX500Principal().getName(); keyStore.setKeyEntry(alias, privateKey, clientKeyPassphrase, new Certificate[] { cert }); return keyStore; }
From source file:com.glaf.core.security.SecurityUtils.java
/** * keystore?//ww w . j a v a2s.c o m * * @return key ? */ public static Key getPrivateKeyFromKeystore(InputStream ksInputStream, String password, String alias) { try { KeyStore ks = KeyStore.getInstance("JKS", "SUN"); ks.load(ksInputStream, password.toCharArray()); Key privateKey = (PrivateKey) ks.getKey(alias, password.toCharArray()); return privateKey; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:com.glaf.core.security.SecurityUtils.java
/** * keystore?/*from w ww . jav a 2 s. c om*/ * * @return X509Certificate ? */ public static X509Certificate getCertFromKeystore(InputStream keystoreInputStream, String alias, String password) { try { X509Certificate x509cert = null; KeyStore ks = KeyStore.getInstance("JKS", "SUN"); ks.load(keystoreInputStream, password.toCharArray()); x509cert = (X509Certificate) ks.getCertificate(alias); return x509cert; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:org.soyatec.windowsazure.internal.util.ssl.SslUtil.java
/** * Return the KeyStore by given URL and password * /* ww w.j a va2 s . c o m*/ * @param url * @param password * @return KeyStore * @throws Exception */ public static KeyStore getKeyStore(final URL url, final String password) throws Exception { if (url == null) { throw new IllegalArgumentException("Keystore url may not be null"); } 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:cn.mrdear.pay.util.RSAUtils.java
/** * ?//from w ww .j av a 2s . c o m * * @param type * * @param inputStream * ? * @param password * ? * @return */ public static Key getKey(String type, InputStream inputStream, String password) { Assert.isNotEmpty(type); Assert.notNull(inputStream); try { KeyStore keyStore = KeyStore.getInstance(type, PROVIDER); keyStore.load(inputStream, password != null ? password.toCharArray() : null); String alias = keyStore.aliases().hasMoreElements() ? keyStore.aliases().nextElement() : null; return keyStore.getKey(alias, password != null ? password.toCharArray() : null); } catch (KeyStoreException e) { throw new RuntimeException(e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } catch (CertificateException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (UnrecoverableKeyException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:cn.mrdear.pay.util.WebUtils.java
/** * ?// w w w.ja v a 2 s.c om * @param certPath ? * @param passwd ?? * @param uri ? * @param entity xml * @return */ public static String post(String certPath, String passwd, String uri, InputStreamEntity entity) throws Exception { String result = null; KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(certPath)); try { keyStore.load(instream, passwd.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, passwd.toCharArray()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpPost httpPost = new HttpPost(uri); entity.setContentEncoding("UTF-8"); httpPost.setEntity(entity); CloseableHttpResponse httpResponse = httpclient.execute(httpPost); result = consumeResponse(httpResponse); } finally { httpclient.close(); } return result; }
From source file:com.nesscomputing.tinyhttp.ssl.HttpsTrustManagerFactory.java
@Nonnull private static KeyStore loadKeystore(@Nonnull String location, @Nonnull String keystoreType, @Nonnull String keystorePassword) throws GeneralSecurityException, IOException { final KeyStore keystore = KeyStore.getInstance(keystoreType); URL keystoreUrl;/*from ww w. ja v a 2 s . com*/ if (StringUtils.startsWithIgnoreCase(location, "classpath:")) { keystoreUrl = Resources.getResource(HttpsTrustManagerFactory.class, location.substring(10)); } else { keystoreUrl = new URL(location); } keystore.load(keystoreUrl.openStream(), keystorePassword.toCharArray()); return keystore; }