use of javax.net.ssl.KeyManagerFactory in project cdap by caskdata.
the class ExternalMTLSAuthenticationServerTest method getInvalidKeyManagers.
/**
* Sets up the client's keystore Using a certificate that is not part of the server's trustore
*
* @return
* @throws Exception
*/
@Override
protected KeyManager[] getInvalidKeyManagers() throws Exception {
URL clientKeystoreURL = ExternalMTLSAuthenticationServerTest.class.getClassLoader().getResource("invalid-client" + ".jks");
Assert.assertNotNull(clientKeystoreURL);
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance("JKS");
char[] ksPass = "secret".toCharArray();
try (FileInputStream fis = new FileInputStream(clientKeystoreURL.getPath())) {
ks.load(fis, ksPass);
kmf.init(ks, configuration.get("security.auth.server.ssl.keystore.password", "secret").toCharArray());
}
return kmf.getKeyManagers();
}
use of javax.net.ssl.KeyManagerFactory in project fluency by komamitsu.
the class SSLTestServerSocketFactory method create.
public SSLServerSocket create() throws IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, KeyManagementException {
String trustStorePath = SSLSocketBuilder.class.getClassLoader().getResource("truststore.jks").getFile();
System.getProperties().setProperty("javax.net.ssl.trustStore", trustStorePath);
String keyStorePath = SSLSocketBuilder.class.getClassLoader().getResource("keystore.jks").getFile();
InputStream keystoreStream = null;
try {
KeyStore keystore = KeyStore.getInstance("JKS");
keystoreStream = new FileInputStream(new File(keyStorePath));
keystore.load(keystoreStream, KEYSTORE_PASSWORD.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keystore, KEY_PASSWORD.toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
SSLServerSocket serverSocket = (SSLServerSocket) sslContext.getServerSocketFactory().createServerSocket();
serverSocket.setEnabledCipherSuites(serverSocket.getSupportedCipherSuites());
serverSocket.bind(new InetSocketAddress(0));
return serverSocket;
} finally {
if (keystoreStream != null) {
keystoreStream.close();
}
}
}
use of javax.net.ssl.KeyManagerFactory in project runwar by cfmlprojects.
the class SSLUtil method createSSLContext.
private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, final char[] passphrase, final String[] addCertificatePaths) throws IOException {
KeyManager[] keyManagers;
try {
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, passphrase);
keyManagers = keyManagerFactory.getKeyManagers();
} catch (NoSuchAlgorithmException ex) {
throw new IOException("Unable to initialise KeyManager[]", ex);
} catch (UnrecoverableKeyException ex2) {
throw new IOException("Unable to initialise KeyManager[]", ex2);
} catch (KeyStoreException ex3) {
throw new IOException("Unable to initialise KeyManager[]", ex3);
}
if (addCertificatePaths != null && addCertificatePaths.length > 0) {
for (int length = addCertificatePaths.length, i = 0; i < length; ++i) {
addCertificate(keyStore, new File(addCertificatePaths[i]), "addedKey" + i);
}
}
TrustManager[] trustManagers;
try {
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
trustManagers = trustManagerFactory.getTrustManagers();
} catch (NoSuchAlgorithmException ex4) {
throw new IOException("Unable to initialise TrustManager[]", ex4);
} catch (KeyStoreException ex5) {
throw new IOException("Unable to initialise TrustManager[]", ex5);
}
SSLContext sslContext;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, null);
} catch (NoSuchAlgorithmException ex6) {
throw new IOException("Unable to create and initialise the SSLContext", ex6);
} catch (KeyManagementException ex7) {
throw new IOException("Unable to create and initialise the SSLContext", ex7);
}
Arrays.fill(passphrase, '*');
return sslContext;
}
use of javax.net.ssl.KeyManagerFactory in project incubator-pulsar by apache.
the class SecurityUtility method setupKeyManager.
private static KeyManager[] setupKeyManager(KeyStoreHolder ksh, PrivateKey privateKey, Certificate[] certificates) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
KeyManager[] keyManagers = null;
if (certificates != null && privateKey != null) {
ksh.setPrivateKey("private", privateKey, certificates);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ksh.getKeyStore(), "".toCharArray());
keyManagers = kmf.getKeyManagers();
}
return keyManagers;
}
use of javax.net.ssl.KeyManagerFactory in project webcert by sklintyg.
the class KeystoreBasedSocketFactory method createSSLContext.
private static SSLContext createSSLContext(final KeyStore truststore) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(null, null);
KeyManager[] keymanagers = kmfactory.getKeyManagers();
TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmfactory.init(truststore);
TrustManager[] trustmanagers = tmfactory.getTrustManagers();
SSLContext sslcontext = SSLContext.getInstance(TLS);
sslcontext.init(keymanagers, trustmanagers, null);
return sslcontext;
}
Aggregations