play.libs.ws.WSSSLContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of framework Show documentation
Show all versions of framework Show documentation
RePlay is a fork of the Play1 framework, created by Codeborne.
package play.libs.ws;
import javax.net.ssl.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class WSSSLContext {
public static SSLContext getSslContext(String keyStore, String keyStorePass, Boolean CAValidation) {
SSLContext sslCTX;
try {
// Keystore
InputStream kss = new FileInputStream(keyStore);
char[] storePass = keyStorePass.toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(kss, storePass);
// Keymanager
char[] certPwd = keyStorePass.toCharArray();
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, certPwd);
KeyManager[] keyManagers = kmf.getKeyManagers();
// Trustmanager
TrustManager[] trustManagers;
if (CAValidation) {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
trustManagers = tmf.getTrustManagers();
} else {
trustManagers = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
}
SecureRandom secureRandom = new SecureRandom();
// SSL context
sslCTX = SSLContext.getInstance("TLS");
sslCTX.init(keyManagers, trustManagers, secureRandom);
} catch (Exception e) {
throw new RuntimeException("Error setting SSL context " + e.toString());
}
return sslCTX;
}
}