Plus Ultra
Plus Ultra

Reputation: 129

javax.net.ssl.SSLHandshakeException: PKIX path building failed: : unable to find valid certification path to requested target

I'm working on a mini Java XMPP project using Vysper server and Smack client, I have two modules one for the server and the other for the client similar to a microservice architecture but a pure Java project with no Springboot. I have my configuration setup on the server side and running but when I try to establish a connection on the Client side I get the below exception

Exception in thread "main" java.lang.RuntimeException: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I have generated an SSL/TSL certificate on my Keystore using shell scripting and imported the same on the client module but the exception persists. Below is my code

XmppServerConfig-Server Module

public class XmppServerConfig {

    private final StorageRegistry storageRegistry;
    private static final Logger log = LoggerFactory.getLogger(XmppServerConfig.class);

    public XmppServerConfig(StorageRegistry storageRegistry) {
        this.storageRegistry = storageRegistry;
    }

    public void config() throws Exception {
        WebSocketEndpoint wsEndpoint = new WebSocketEndpoint();
        wsEndpoint.setContextPath("/xmpp");
        wsEndpoint.setPort(9000);
        XMPPServer server = new XMPPServer("domain.com");
        TCPEndpoint endpoint = new TCPEndpoint();
        endpoint.setPort(7000);
        server.addEndpoint(wsEndpoint);
        server.addEndpoint(endpoint);
        server.setStorageProviderRegistry(storageRegistry.get());
        server.setTLSCertificateInfo(new File("part-to/tls-cert/cleaned_myserver.p12"), "password");

        try {
            server.start();
            System.out.println("server is running...");
            log.info("server is running... on port {}", server.getServerRuntimeContext());
        } catch (Exception e) {
            System.out.println("error occurred while starting server" + e.getLocalizedMessage());
            throw  new RuntimeException( "error occurred while starting server", e);
        }
    }
}

TCPConnectionConfiguration - Client Module

public class TCPConnectionConfiguration {

    public ModularXmppClientToServerConnectionConfiguration connectXmppServer() {
        try {
            System.out.println("ConnectingXMPP server connected.");
            return ModularXmppClientToServerConnectionConfiguration.builder()
                    .setSecurityMode(ConnectionConfiguration.SecurityMode.required)
                    .setPort(7000)
                    .setHost("127.0.0.1")
                    .setSendPresence(true)
                    .setResource("Smack")
                    .setXmppDomain("domain.com")
                    .setCustomSSLContext(SSLConfig.getSSLContext())
                    .build();
        } catch (Exception e) {
            String causeMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
            System.out.println("Unable to connect to XMPP server: " + causeMessage);
            throw new RuntimeException("Unable to connect to XMPP server", e);
        }
    }
}

SSLContextConfig-Client Module

public class SSLContextConfig {
    public static SSLContext getSSLContext() throws Exception {
            String keystorePath = "part-to/tls-cert/client_keystore.p12";
            String keystorePassword = "aspace@1234";
            String truststorePath = "part-to/tls-cert/client_keystore.p12";
            String truststorePassword = "aspace@1234";

            KeyStore keystore = KeyStore.getInstance("PKCS12");
            keystore.load(new FileInputStream(keystorePath), keystorePassword.toCharArray());

            KeyStore truststore = KeyStore.getInstance("PKCS12");
            truststore.load(new FileInputStream(truststorePath), truststorePassword.toCharArray());

            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(truststore);

            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, trustManagerFactory.getTrustManagers(), null);

            return sslContext;
        }
}

What am I missing and how can this bug be resolved?

Upvotes: 0

Views: 26

Answers (0)

Related Questions