I'm trying to get a http request to a REST-endpoint. I'm bound to okhttp2.7.5 as the framework I use for the project defines it. My research brought me to the following Thread.
And so my code looks right this at the moment:
OkHttpClient httpClient = new OkHttpClient();
SSLContext sslContext;
TrustManager[] trustManagers;
char[] keyPassword = "123456".toCharArray();
String keyLocation = "key.pem";
String host = "https://localhost:";
Integer port = 8150;
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
InputStream keyStoreData = new FileInputStream(keyLocation);
BufferedInputStream bis = new BufferedInputStream(keyStoreData);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
while (bis.available() > 0) {
Certificate cert = certificateFactory.generateCertificate(bis);
keyStore.setCertificateEntry(host + port, cert);
}
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
trustManagers = trustManagerFactory.getTrustManagers();
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, keyPassword);
sslContext = SSLContext.getInstance("SSL");
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
new SecureRandom());
httpClient.setSocketFactory(sslContext.getSocketFactory());
} catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException | KeyManagementException
| CertificateException | IOException e) {
throw new RuntimeException(e);
}
When using it I get a error with following message:
java.lang.RuntimeException: java.security.cert.CertificateParsingException: signed overrun, bytes = 465
I know my certificate is good and valid, as it can be used without any problems in Postman requests.
Best regards for your help folks