Skip to content

Instantly share code, notes, and snippets.

@mklimek
Last active June 11, 2021 00:10
Show Gist options
  • Save mklimek/f9d197362c1f2db8c1b76f76ace75859 to your computer and use it in GitHub Desktop.
Save mklimek/f9d197362c1f2db8c1b76f76ace75859 to your computer and use it in GitHub Desktop.
SslUtils - load certificate to SSLContext
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
public class SslUtils {
private static final Logger LOG = LoggerFactory.getLogger(SslUtils.class.getSimpleName());
public static SSLContext getSslContextForCertificateFile(String fileName) {
try {
KeyStore keyStore = SslUtils.getKeyStore(fileName);
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
return sslContext;
} catch (Exception e) {
String msg = "Cannot load certificate from file";
LOG.error(msg, e);
throw new RuntimeException(msg);
}
}
private static KeyStore getKeyStore(String fileName) {
KeyStore keyStore = null;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream inputStream = new FileInputStream(fileName);
Certificate ca;
try {
ca = cf.generateCertificate(inputStream);
LOG.debug("ca={}", ((X509Certificate) ca).getSubjectDN());
} finally {
inputStream.close();
}
String keyStoreType = KeyStore.getDefaultType();
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
} catch (Exception e) {
LOG.error("Error during getting keystore", e);
}
return keyStore;
}
}
@mklimek
Copy link
Author

mklimek commented Mar 30, 2016

Example usage:
OkHttpClient client = new OkHttpClient();
SSLContext sslContext = SslUtils.getSslContextForCertificateFile("BPClass2RootCA-sha2.cer");
client.setSslSocketFactory(sslContext.getSocketFactory());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment