简体   繁体   中英

SSL Handshake failure Java

I was learning SSL communications and I came across this issue. I am writing a simple client which tries to handshake with a local apache server. The server is https enabled. I added the server certificates to all possible trust stores (the one in jdk & the one used by the program also). But the handshake status is never reaching FINISHED. It is stuck at NEED_TASK status continuously I get a sun.security.ssl.Handshaker$DelegatedTask at the first entry into loop. Thereafter the status is NEED_TASK and the task is null. . Where is my understanding wrong \\ flaw with the below code ?

Note : I took the code from the following tutorial :

http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html#KRB

The handshake code which is stuck at NEED_TASK status is below :

void doHandshake(SocketChannel socketChannel, SSLEngine engine,
            ByteBuffer myNetData, ByteBuffer peerNetData) throws Exception {

        // Create byte buffers to use for holding application data
        int appBufferSize = engine.getSession().getApplicationBufferSize();
        ByteBuffer myAppData = ByteBuffer.allocate(appBufferSize);
        ByteBuffer peerAppData = ByteBuffer.allocate(appBufferSize);
        // Begin handshake
        engine.beginHandshake();
        SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
        int i=0;
        // Process handshaking message
        while (hs != SSLEngineResult.HandshakeStatus.FINISHED &&
            hs != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
            i++;
            switch (hs) {

            case NEED_UNWRAP:
                // Receive handshaking data from peer
                if (socketChannel.read(peerNetData) < 0) {
                    // The channel has reached end-of-stream
                }

                // Process incoming handshaking data
                peerNetData.flip();
                SSLEngineResult res = engine.unwrap(peerNetData, peerAppData);
                peerNetData.compact();
                hs = res.getHandshakeStatus();

                // Check status
                switch (res.getStatus()) {
                case OK :
                    // Handle OK status
                    break;

                // Handle other status: BUFFER_UNDERFLOW, BUFFER_OVERFLOW, CLOSED

                }
                break;

            case NEED_WRAP :
                // Empty the local network packet buffer.
                myNetData.clear();

                // Generate handshaking data
                res = engine.wrap(myAppData, myNetData);
                hs = res.getHandshakeStatus();

                // Check status
                switch (res.getStatus()) {
                case OK :
                    myNetData.flip();

                    // Send the handshaking data to peer
                    while (myNetData.hasRemaining()) {
                        socketChannel.write(myNetData);
                    }
                    break;

                // Handle other status:  BUFFER_OVERFLOW, BUFFER_UNDERFLOW, CLOSED

                }
                break;

            case NEED_TASK :
                Runnable task =engine.getDelegatedTask();
                if(task!= null) {
                    //task.run();
                    new Thread(task).start();
                }// Handle blocking tasks

                break;

            // Handle other status:  // FINISHED or NOT_HANDSHAKING

            }
        }
        // Processes after handshaking

    }

Any help would be appreciated.

The problem was my incomplete understanding of engine.getHandShakeStatus() method.

I changed the above code to

 case NEED_TASK :
            Runnable task =engine.getDelegatedTask();
            if(task!= null) {
                //task.run();
               new Thread(task).start();
            }
            hs = engine.getHandshakeStatus();
            break;

And now it finished the handshake

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM