Java SSLSocket handshake failure

大兔子大兔子 提交于 2019-12-11 05:29:34

问题


I'm trying to find a way to establish a connection beetwen a Java client and a C server using SSL.

This is the java client:

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class Main {

    public static void main(String[] args) throws IOException {

        SSLSocketFactory sslsockfact = 
            (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslsocket = (SSLSocket) sslsockfact.createSocket(
            args[0], Integer.parseInt(args[1]));
        sslsocket.startHandshake();
        System.in.read();
    }
}

It's only a few functions to establish connection and perform handshake, but I'm getting this error:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: 
  Received fatal alert: handshake_failure
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(
      SSLSocketImpl.java:1657)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(
      SSLSocketImpl.java:932)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(
      SSLSocketImpl.java:1096)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(
      SSLSocketImpl.java:1123)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(
      SSLSocketImpl.java:1107)
    at posslu.Main.main(Main.java:22)
Java Result: 1

Unfortunately I don't have any sources of the server program - I only know the protocol. Is it even possible to connect java and C++ using ssl? AFAIK server is written using openssl.

Any help?

EDIT:

I'm connecting from Windows to Linux and server's using posix sockets.


回答1:


Yes, it's definitely possible to communicate via SSL between Java and C.

The Java client code may be failing because it doesn't recognise the server certificate being sent.

You need to make sure that the server's certificate is present in the Java client's trust store.




回答2:


Try setting this system property:

System.setProperty("javax.net.debug", "all");

This will give you much debugging information on what is happening during the handshake.



来源:https://stackoverflow.com/questions/1928240/java-sslsocket-handshake-failure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!