SSL套接字隧道握手异常

时间:2018-07-20 20:39:12

标签: sockets ssl

所以我要创建一个tcp代理服务器的套接字,然后在顶部放置一个SSL套接字以连接到端点。与代理的握手很好,当我调用doSSLHandshake(tunnel,host,port);我得到异常捕获:无法识别的SSL消息,纯文本连接?

我握手正确吗?我什么时候将邮件发送到服务器?

TrustManager trustManager = new AllTrustManager();
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
            .getDefault();

try {

        StartTLSFactory startTlsFatory = new StartTLSFactory(trustManager);

        Socket tunnel = startTlsFatory.createSslSocket(tunnelHost, tunnelPort);
        System.out.println("handshake with tunnel created");    
        SSLSocket socket = (SSLSocket) factory.createSocket(tunnel, host,
                port, true);

        /*
         * register a callback for handshaking completion event
         */
        socket.addHandshakeCompletedListener(
            new HandshakeCompletedListener() {
                public void handshakeCompleted(
                        HandshakeCompletedEvent event) {
                    System.out.println("Handshake finished!");
                    System.out.println(
                        "\t CipherSuite:" + event.getCipherSuite());
                    System.out.println(
                        "\t SessionId " + event.getSession());
                    System.out.println(
                        "\t PeerHost " + event.getSession().getPeerHost());
                }
            }
        );

        socket.setUseClientMode(false);         
        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");         
        System.out.println("About to start handshake with tunnel");
        doSSLHandshake(tunnel,host,port);
    } catch (Exception ex) {
        System.out.println("Exception caught : " + ex.getMessage());

    }
}

private static void doSSLHandshake(Socket socket, String host, int port) throws IOException {
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager(){
                public X509Certificate[] getAcceptedIssuers(){ return null; }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            }
    };

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(socket, host, port, true);
        sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols());
        sslSocket.setEnableSessionCreation(true);
        sslSocket.startHandshake();
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        throw new IOException("Could not do handshake: " + e);
    }
}

0 个答案:

没有答案