Code example for SSLEngine

Methods: closeOutboundgetHandshakeStatusgetSessionwrap

0
        if (doLog) {
            System.out.println("\n--- doClose: ");
        } 
        ByteBuffer buffer = ByteBuffer.allocate(
                // +100 because we put the data into the buffer multiple times 
                server.getSession().getPacketBufferSize()+100);
        ByteBuffer app_data_buffer = ByteBuffer.allocate(
                client.getSession().getApplicationBufferSize());
        SSLEngineResult result;
        // first: send 0 just for fun 
        if (doLog) {
            System.out.println("\nServer sends pending outboud data:");
        } 
        print(result = server.wrap(ByteBuffer.wrap(new byte[] {0}), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.OK,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        // second: initiate a close 
        if (doLog) {
            System.out.println("\nServer initiates a closure:");
        } 
        server.closeOutbound();
        // should do nothing: 
        server.closeOutbound();
 
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NEED_WRAP,
                server.getHandshakeStatus());
 
        // will cause SSLException because closure alert was not received yet: 
        // server.closeInbound(); 
 
        // wrap closure alert (previosly sent 0 should not be lost) 
        print(result = server.wrap(ByteBuffer.allocate(0), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NEED_UNWRAP,
                result.getHandshakeStatus());
 
        if (doLog) {
            System.out.println("\nServer sends pending outboud data again:");
        } 
        // will do nothing because closure alert has been sent 
        // and outbound has been closed 
        print(result = server.wrap(ByteBuffer.wrap(new byte[] {0}), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NEED_UNWRAP,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the consumed data differs from expected", 
            0, result.bytesConsumed());
        assertEquals(
            "The length of the produced data differs from expected", 
            0, result.bytesProduced());
 
        // prepare the buffer for reading 
        buffer.flip();
 
        if (doLog) {
            System.out.println(
                    "\nClient receives pending servers' outbound data"); 
        } 
        print(result = client.unwrap(buffer, app_data_buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.OK,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the produced data differs from expected", 
            1, result.bytesProduced());
 
        app_data_buffer.clear();
 
        if (doLog) {
            System.out.println("\nClient receives close notify");
        } 
        print(result = client.unwrap(buffer, app_data_buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NEED_WRAP,
                result.getHandshakeStatus());
        assertTrue(
            "The length of the consumed data differs from expected", 
            result.bytesConsumed() > 0);
        assertEquals(
            "The length of the received data differs from expected", 
            0, result.bytesProduced());
 
        // prepare the buffer for writing 
        app_data_buffer.clear();
 
        // it's needless, but should work (don't cause exceptions): 
        client.closeInbound();
        client.closeOutbound();
 
        if (doLog) {
            System.out.println("\nClient tries to read data again");
        } 
        // CLOSED, 0 consumed, 0 produced 
        print(result = client.unwrap(buffer, app_data_buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NEED_WRAP,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the consumed data differs from expected", 
            0, result.bytesConsumed());
        assertEquals(
            "The length of the received data differs from expected", 
            0, result.bytesProduced());
 
        // prepare the buffer for writing 
        buffer.clear();
 
        if (doLog) {
            System.out.println("\nClient sends responding close notify");
        } 
        print(result = client.wrap(ByteBuffer.wrap(new byte[] {0}), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the consumed data differs from expected", 
            0, result.bytesConsumed());
        assertTrue(
            "The length of the produced data differs from expected", 
            result.bytesProduced() > 0);
        if (doLog) {
            System.out.println(
                    "\nClient tries to send data after closure alert"); 
        } 
        // this data will not be sent (should do nothing - 0 cons, 0 prod) 
        print(result = client.wrap(ByteBuffer.wrap(new byte[] {0}), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the consumed data differs from expected", 
            0, result.bytesConsumed());
        assertEquals(
            "The length of the produced data differs from expected", 
            0, result.bytesProduced());
 
        // prepare the buffers for reading 
        app_data_buffer.clear();
        buffer.flip();
 
        if (doLog) {
            System.out.println("\nServer receives close notify");
        } 
        print(result = server.unwrap(buffer, app_data_buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        assertTrue(
            "The length of the consumed data differs from expected", 
            result.bytesConsumed() > 0);
        assertEquals(
            "The length of the produced data differs from expected", 
            0, result.bytesProduced());
 
        if (doLog) {
            System.out.println("\nServer tries to read after closure");
        } 
        // will be ignored 
        print(result = server.unwrap(buffer, app_data_buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the consumed data differs from expected", 
            0, result.bytesConsumed());
        assertEquals(
            "The length of the produced data differs from expected", 
            0, result.bytesProduced());
 
        // it's needless, but should work: 
        client.closeInbound();
        // will be ignored 
 
        if (doLog) {
            System.out.println("\nServer tries to write after closure");
        } 
        buffer.clear();
        print(result = server.wrap(ByteBuffer.allocate(0), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
Contextual code suggestions in your IDE  Get Codota for Java