0

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.

Question I based my code on:

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

AdrianL
  • 335
  • 2
  • 18
  • 1
    You might want to run your JVM with the `-Djavax.net.debug=all` flag and follow [this guide](https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/ReadDebug.html). Which line is throwing the error btw? – Aaron Sep 21 '18 at 13:16
  • Thanks, found the problem by this. My certificate was not the one I wanted. – AdrianL Sep 24 '18 at 12:45
  • Glad to hear it, SSL debug is far from trivial ! – Aaron Sep 24 '18 at 12:46
  • Yeah, that's true. Now I'm encountering a new problem, but that's a different Topic (Thread). – AdrianL Sep 25 '18 at 06:17

0 Answers0