List of usage examples for javax.net.ssl KeyManagerFactory getKeyManagers
public final KeyManager[] getKeyManagers()
From source file:Main.java
/** * Generate a SSLSocketFactory wich checks the certificate given * @param context Context to use/*w ww .ja v a 2 s. com*/ * @param rResource int with url of the resource to read the certificate * @parma password String to use with certificate * @return SSLSocketFactory generated to validate this certificate */ public static SSLSocketFactory newSslSocketFactory(Context context, int rResource, String password) throws CertificateException, NoSuchProviderException, KeyStoreException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { // Get an instance of the Bouncy Castle KeyStore format KeyStore trusted = KeyStore.getInstance("BKS"); // Get the raw resource, which contains the keystore with // your trusted certificates (root and any intermediate certs) InputStream is = context.getApplicationContext().getResources().openRawResource(rResource); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC"); X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(is); String alias = "alias";//cert.getSubjectX500Principal().getName(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null); trustStore.setCertificateEntry(alias, cert); KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509"); kmf.init(trustStore, null); KeyManager[] keyManagers = kmf.getKeyManagers(); TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); tmf.init(trustStore); TrustManager[] trustManagers = tmf.getTrustManagers(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyManagers, trustManagers, null); return sslContext.getSocketFactory(); }
From source file:org.talend.daikon.security.SSLContextProvider.java
private static KeyManager[] buildKeyManagers(String path, String storePass, String keytype) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException { InputStream stream = null;//from w w w .j ava 2 s . c om try { if (StringUtils.isEmpty(path)) { return null; } if (!new File(path).exists()) { throw new KeyStoreException("Key store not exist"); } stream = new FileInputStream(path); KeyStore tks = KeyStore.getInstance(keytype); tks.load(stream, storePass.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$ kmf.init(tks, storePass.toCharArray()); return kmf.getKeyManagers(); } finally { if (stream != null) { stream.close(); } } }
From source file:Main.java
private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) { try {/*w w w .ja v a2 s . co m*/ if (bksFile == null || password == null) return null; KeyStore clientKeyStore = KeyStore.getInstance("BKS"); clientKeyStore.load(bksFile, password.toCharArray()); KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(clientKeyStore, password.toCharArray()); return keyManagerFactory.getKeyManagers(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) { try {/* w w w . j a va2 s . c o m*/ if (bksFile != null && password != null) { KeyStore e = KeyStore.getInstance("BKS"); e.load(bksFile, password.toCharArray()); KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(e, password.toCharArray()); return keyManagerFactory.getKeyManagers(); } return null; } catch (KeyStoreException var4) { var4.printStackTrace(); } catch (NoSuchAlgorithmException var5) { var5.printStackTrace(); } catch (UnrecoverableKeyException var6) { var6.printStackTrace(); } catch (CertificateException var7) { var7.printStackTrace(); } catch (IOException var8) { var8.printStackTrace(); } catch (Exception var9) { var9.printStackTrace(); } return null; }
From source file:android.apn.androidpn.server.xmpp.ssl.SSLKeyManagerFactory.java
public static KeyManager[] getKeyManagers(String storeType, String keystore, String keypass) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException { KeyManager[] keyManagers;//w ww. j a v a 2 s. co m if (keystore == null) { keyManagers = null; } else { if (keypass == null) { keypass = ""; } KeyStore keyStore = KeyStore.getInstance(storeType); keyStore.load(new FileInputStream(keystore), keypass.toCharArray()); KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyFactory.init(keyStore, keypass.toCharArray()); keyManagers = keyFactory.getKeyManagers(); } return keyManagers; }
From source file:com.allstate.client.ssl.SSLUtils.java
public static X509KeyManager getKeyManager(KeyStore keyStore, char[] keyStorePassword) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException { KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, keyStorePassword); return (X509KeyManager) keyManagerFactory.getKeyManagers()[0]; }
From source file:com.mendhak.gpslogger.senders.ftp.Ftp.java
public static boolean Upload(String server, String username, String password, int port, boolean useFtps, String protocol, boolean implicit, InputStream inputStream, String fileName) { FTPClient client = null;// ww w .j a v a 2s . c o m try { if (useFtps) { client = new FTPSClient(protocol, implicit); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(null, null); KeyManager km = kmf.getKeyManagers()[0]; ((FTPSClient) client).setKeyManager(km); } else { client = new FTPClient(); } } catch (Exception e) { e.printStackTrace(); } try { Utilities.LogDebug("Connecting to FTP"); client.connect(server, port); Utilities.LogDebug("Logging in to FTP server"); if (client.login(username, password)) { client.enterLocalPassiveMode(); Utilities.LogDebug("Uploading file to FTP server"); FTPFile[] existingDirectory = client.listFiles("GPSLogger"); if (existingDirectory.length <= 0) { client.makeDirectory("GPSLogger"); } client.changeWorkingDirectory("GPSLogger"); boolean result = client.storeFile(fileName, inputStream); inputStream.close(); if (result) { Utilities.LogDebug("Successfully FTPd file"); } else { Utilities.LogDebug("Failed to FTP file"); } } else { Utilities.LogDebug("Could not log in to FTP server"); return false; } } catch (Exception e) { Utilities.LogError("Could not connect or upload to FTP server.", e); } finally { try { Utilities.LogDebug("Logging out of FTP server"); client.logout(); Utilities.LogDebug("Disconnecting from FTP server"); client.disconnect(); } catch (Exception e) { Utilities.LogError("Could not logout or disconnect", e); } } return true; }
From source file:com.amalto.workbench.utils.SSLContextProvider.java
private static KeyManager[] buildKeyManagers(String path, String storePass, String keytype) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException { InputStream stream = null;/*from ww w . java 2s . c o m*/ try { if (StringUtils.isEmpty(path)) { return null; } if (!new File(path).exists()) { throw new KeyStoreException(Messages.bind(Messages.noKeystoreFile_error, path)); } stream = new FileInputStream(path); KeyStore tks = KeyStore.getInstance(keytype); tks.load(stream, storePass.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$ kmf.init(tks, storePass.toCharArray()); return kmf.getKeyManagers(); } finally { IOUtils.closeQuietly(stream); } }
From source file:com.nesscomputing.httpclient.internal.HttpClientTrustManagerFactory.java
@Nonnull private static X509KeyManager getKeyManagerForKeystore(@Nonnull KeyStore keyStore, @Nonnull String password) throws GeneralSecurityException { KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509", "SunJSSE"); keyManagerFactory.init(keyStore, password.toCharArray()); for (KeyManager keyManager : keyManagerFactory.getKeyManagers()) { if (keyManager instanceof X509KeyManager) { return (X509KeyManager) keyManager; }//from w w w .j a va 2 s . co m } throw new IllegalStateException("Couldn't find an X509KeyManager"); }
From source file:org.apache.hadoop.gateway.jetty.JettyHttpsTest.java
private static KeyManager[] createKeyManagers(String keyStoreType, String keyStorePath, String keyStorePassword) throws Exception { KeyStore keyStore = loadKeyStore(keyStoreType, keyStorePath, keyStorePassword); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keyStore, keyStorePassword.toCharArray()); return kmf.getKeyManagers(); }